+}
+
+static Lock *
+lockscreen(Display *dpy, int screen)
+{
+ char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
+ int i;
+ Lock *lock;
+ XColor color, dummy;
+ XSetWindowAttributes wa;
+ Cursor invisible;
+
+ if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
+ return NULL;
+
+ lock->screen = screen;
+ lock->root = RootWindow(dpy, lock->screen);
+
+ for (i = 0; i < NUMCOLS; i++) {
+ XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
+ lock->colors[i] = color.pixel;
+ }
+
+ /* init */
+ wa.override_redirect = 1;
+ wa.background_pixel = lock->colors[INIT];
+ lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
+ 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
+ DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
+ lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
+ invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
+ XDefineCursor(dpy, lock->win, invisible);
+
+ /* Try to grab mouse pointer *and* keyboard, else fail the lock */
+ if (XGrabPointer(dpy, lock->root, False, ButtonPressMask |
+ ButtonReleaseMask | PointerMotionMask, GrabModeAsync, GrabModeAsync,
+ None, invisible, CurrentTime) != GrabSuccess) {
+ fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
+ running = 0;
+ unlockscreen(dpy, lock);
+ return NULL;
+ }
+
+ if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync,
+ CurrentTime) != GrabSuccess) {
+ fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
+ running = 0;
+ unlockscreen(dpy, lock);
+ return NULL;
+ }
+
+ XMapRaised(dpy, lock->win);
+ if (rr)
+ XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
+
+ XSelectInput(dpy, lock->root, SubstructureNotifyMask);
+ return lock;
+}
+
+static void
+usage(void)
+{
+ die("usage: slock [-v] [cmd [arg ...]]\n");
+}
+
+int
+main(int argc, char **argv) {
+#ifndef HAVE_BSD_AUTH
+ const char *pws;
+#endif
+ Display *dpy;
+ int s, nlocks;
+
+ ARGBEGIN {
+ case 'v':
+ fprintf(stderr, "slock-"VERSION"\n");
+ return 0;
+ default:
+ usage();
+ } ARGEND
+
+#ifdef __linux__
+ dontkillme();
+#endif
+
+ /* Check if the current user has a password entry */
+ errno = 0;
+ if (!getpwuid(getuid())) {
+ if (errno == 0)
+ die("slock: no password entry for current user\n");
+ else
+ die("slock: getpwuid: %s\n", strerror(errno));
+ }
+
+#ifndef HAVE_BSD_AUTH
+ pws = getpw();
+ if (strlen(pws) < 2)
+ die("slock: failed to get user password hash.\n");
+#endif
+
+ if (!(dpy = XOpenDisplay(NULL)))
+ die("slock: cannot open display\n");
+
+ /* check for Xrandr support */
+ rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
+
+ /* get number of screens in display "dpy" and blank them */
+ nscreens = ScreenCount(dpy);
+ if (!(locks = malloc(sizeof(Lock *) * nscreens))) {
+ XCloseDisplay(dpy);
+ die("slock: out of memory\n");
+ }
+ for (nlocks = 0, s = 0; s < nscreens; s++) {
+ if ((locks[s] = lockscreen(dpy, s)) != NULL)
+ nlocks++;
+ }
+ XSync(dpy, 0);
+
+ /* did we actually manage to lock anything? */
+ if (nlocks == 0) {
+ /* nothing to protect */
+ cleanup(dpy);
+ return 1;
+ }
+
+ /* run post-lock command */
+ if (argc > 0) {
+ switch (fork()) {
+ case -1:
+ cleanup(dpy);
+ die("slock: fork failed: %s\n", strerror(errno));
+ case 0:
+ if (close(ConnectionNumber(dpy)) < 0)
+ die("slock: close: %s\n", strerror(errno));
+ execvp(argv[0], argv);
+ fprintf(stderr, "slock: execvp %s: %s\n", argv[0],
+ strerror(errno));
+ _exit(1);
+ }
+ }
+
+ /* everything is now blank. Wait for the correct password */
+#ifdef HAVE_BSD_AUTH
+ readpw(dpy);
+#else
+ readpw(dpy, pws);
+#endif
+
+ /* password ok, unlock everything and quit */
+ for (s = 0; s < nscreens; s++)
+ unlockscreen(dpy, locks[s]);
+
+ cleanup(dpy);
+