1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. */
3 #define _XOPEN_SOURCE 500
15 #include <sys/types.h>
16 #include <X11/keysym.h>
18 #include <X11/Xutil.h>
21 eprint(const char *errstr, ...) {
25 vfprintf(stderr, errstr, ap);
31 get_password() { /* only run as root */
36 eprint("slock: cannot retrieve password entry (make sure to suid slock)\n");
37 pw = getpwuid(getuid());
44 sp = getspnam(getenv("USER"));
50 if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
51 eprint("slock: cannot drop privileges\n");
56 main(int argc, char **argv) {
57 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
58 char buf[32], passwd[256];
70 XSetWindowAttributes wa;
72 if((argc > 1) && !strncmp(argv[1], "-v", 3))
73 eprint("slock-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
75 if(!(dpy = XOpenDisplay(0)))
76 eprint("slock: cannot open display\n");
77 screen = DefaultScreen(dpy);
78 root = RootWindow(dpy, screen);
81 wa.override_redirect = 1;
82 wa.background_pixel = BlackPixel(dpy, screen);
83 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
84 0, DefaultDepth(dpy, screen), CopyFromParent,
85 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
86 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
87 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
88 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
89 XDefineCursor(dpy, w, invisible);
91 for(len = 1000; len; len--) {
92 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
93 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
97 if((running = running && (len > 0))) {
98 for(len = 1000; len; len--) {
99 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
109 /* main event loop */
110 while(running && !XNextEvent(dpy, &ev))
111 if(ev.type == KeyPress) {
113 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
114 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
115 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
116 || IsPrivateKeypadKey(ksym))
121 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
133 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
134 memcpy(passwd + len, buf, num);
140 XUngrabPointer(dpy, CurrentTime);
141 XFreePixmap(dpy, pmap);
142 XDestroyWindow(dpy, w);