1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. */
3 #define _XOPEN_SOURCE 500
14 #include <sys/types.h>
15 #include <X11/keysym.h>
17 #include <X11/Xutil.h>
20 get_password() { /* only run as root */
25 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr);
28 pw = getpwuid(getuid());
35 sp = getspnam(getenv("USER"));
41 if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
42 fputs("slock: cannot drop privileges\n",stdout);
49 main(int argc, char **argv) {
50 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
51 char buf[32], passwd[256];
63 XSetWindowAttributes wa;
65 if((argc > 1) && !strncmp(argv[1], "-v", 3)) {
66 fputs("slock-"VERSION", © 2006-2007 Anselm R. Garbe\n", stdout);
70 if(!(dpy = XOpenDisplay(0))) {
71 fputs("slock: cannot open display\n", stderr);
74 screen = DefaultScreen(dpy);
75 root = RootWindow(dpy, screen);
78 wa.override_redirect = 1;
79 wa.background_pixel = BlackPixel(dpy, screen);
80 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
81 0, DefaultDepth(dpy, screen), CopyFromParent,
82 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
83 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
84 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
85 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
86 XDefineCursor(dpy, w, invisible);
88 for(len = 1000; len; len--) {
89 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
90 GrabModeAsync, GrabModeSync, None, invisible, CurrentTime) == GrabSuccess)
94 if((running = running && (len > 0))) {
95 for(len = 1000; len; len--) {
96 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
106 /* main event loop */
107 while(running && !XNextEvent(dpy, &ev))
108 if(ev.type == KeyPress) {
110 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
111 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
112 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
113 || IsPrivateKeypadKey(ksym))
118 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
130 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
131 memcpy(passwd + len, buf, num);
137 XUngrabPointer(dpy, CurrentTime);
138 XFreePixmap(dpy, pmap);
139 XDestroyWindow(dpy, w);