Xinqi Bao's Git
72afd1e025529fa2683497e1c13239990a2be944
1 /* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
4 #define _XOPEN_SOURCE 500
17 #include <sys/types.h>
18 #include <X11/keysym.h>
20 #include <X11/Xutil.h>
23 main(int argc
, char **argv
) {
24 char curs
[] = {0, 0, 0, 0, 0, 0, 0, 0};
25 char buf
[32], passwd
[256];
41 XSetWindowAttributes wa
;
43 if((argc
> 1) && !strncmp(argv
[1], "-v", 3)) {
44 fputs("slock-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
48 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr
);
52 sp
= getspnam(getenv("USER"));
55 pw
= getpwuid(getuid());
58 if(!(dpy
= XOpenDisplay(0))) {
59 fputs("slock: cannot open display\n", stderr
);
62 screen
= DefaultScreen(dpy
);
67 wa
.override_redirect
= 1;
68 wa
.background_pixel
= BlackPixel(dpy
, screen
);
69 w
= XCreateWindow(dpy
, RootWindow(dpy
, screen
), 0, 0,
70 DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
71 0, DefaultDepth(dpy
, screen
), CopyFromParent
,
72 DefaultVisual(dpy
, screen
), CWOverrideRedirect
| CWBackPixel
, &wa
);
74 XAllocNamedColor(dpy
, DefaultColormap(dpy
, screen
), "black", &black
, &dummy
);
75 pmap
= XCreateBitmapFromData(dpy
, w
, curs
, 8, 8);
76 invisible
= XCreatePixmapCursor(dpy
, pmap
, pmap
, &black
, &black
, 0, 0);
77 XDefineCursor(dpy
, w
, invisible
);
78 running
= XGrabPointer(dpy
, RootWindow(dpy
, screen
), False
,
79 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
,
80 GrabModeAsync
, GrabModeSync
, None
, invisible
, CurrentTime
) == GrabSuccess
81 && XGrabKeyboard(dpy
, RootWindow(dpy
, screen
), True
, GrabModeAsync
,
82 GrabModeAsync
, CurrentTime
) == GrabSuccess
;
87 while(running
&& !XNextEvent(dpy
, &ev
))
88 if(ev
.type
== KeyPress
) {
91 num
= XLookupString(&ev
.xkey
, buf
, sizeof(buf
), &ksym
, 0);
92 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
93 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
94 || IsPrivateKeypadKey(ksym
))
99 if((running
= strncmp(crypt(passwd
, sp
->sp_pwdp
), sp
->sp_pwdp
, sizeof(passwd
))))
101 if((running
= strncmp(crypt(passwd
, pw
->pw_passwd
), pw
->pw_passwd
, sizeof(passwd
))))
114 if(num
&& !iscntrl((int) buf
[0])) {
117 strncat(passwd
, buf
, sizeof(passwd
));
119 strncpy(passwd
, buf
, sizeof(passwd
));
124 XUngrabPointer(dpy
, CurrentTime
);
125 XFreePixmap(dpy
, pmap
);
126 XDestroyWindow(dpy
, w
);