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 == 2) && !strcmp("-v", argv[1]))
73 eprint("slock-"VERSION", © 2006-2007 Anselm R. Garbe\n");
75 eprint("usage: slock [-v]\n");
77 if(!(dpy = XOpenDisplay(0)))
78 eprint("slock: cannot open display\n");
79 screen = DefaultScreen(dpy);
80 root = RootWindow(dpy, screen);
83 wa.override_redirect = 1;
84 wa.background_pixel = BlackPixel(dpy, screen);
85 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
86 0, DefaultDepth(dpy, screen), CopyFromParent,
87 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
88 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
89 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
90 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
91 XDefineCursor(dpy, w, invisible);
93 for(len = 1000; len; len--) {
94 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
95 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
99 if((running = running && (len > 0))) {
100 for(len = 1000; len; len--) {
101 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
111 /* main event loop */
112 while(running && !XNextEvent(dpy, &ev))
113 if(ev.type == KeyPress) {
115 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
116 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
117 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
118 || IsPrivateKeypadKey(ksym))
123 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
135 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
136 memcpy(passwd + len, buf, num);
142 XUngrabPointer(dpy, CurrentTime);
143 XFreePixmap(dpy, pmap);
144 XDestroyWindow(dpy, w);