]> git.armaanb.net Git - slock.git/blobdiff - slock.c
consistently use () with sizeof
[slock.git] / slock.c
diff --git a/slock.c b/slock.c
index df5c3fea36f2e0f1b7ccdb1e035588bd0f0a5d00..68e813f4e86041ed7be45492771adcdfcb7d9187 100644 (file)
--- a/slock.c
+++ b/slock.c
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <X11/extensions/Xrandr.h>
 #include <X11/keysym.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
@@ -25,7 +26,7 @@
 enum {
        INIT,
        INPUT,
-       EMPTY,
+       FAILED,
        NUMCOLS
 };
 
@@ -41,6 +42,10 @@ typedef struct {
 static Lock **locks;
 static int nscreens;
 static Bool running = True;
+static Bool failure = False;
+static Bool rr;
+static int rrevbase;
+static int rrerrbase;
 
 static void
 die(const char *errstr, ...)
@@ -128,7 +133,7 @@ readpw(Display *dpy, const char *pws)
        while (running && !XNextEvent(dpy, &ev)) {
                if (ev.type == KeyPress) {
                        buf[0] = 0;
-                       num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
+                       num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
                        if (IsKeypadKey(ksym)) {
                                if (ksym == XK_KP_Enter)
                                        ksym = XK_Return;
@@ -149,8 +154,10 @@ readpw(Display *dpy, const char *pws)
 #else
                                running = !!strcmp(crypt(passwd, pws), pws);
 #endif
-                               if (running)
+                               if (running) {
                                        XBell(dpy, 100);
+                                       failure = True;
+                               }
                                len = 0;
                                break;
                        case XK_Escape:
@@ -161,7 +168,7 @@ readpw(Display *dpy, const char *pws)
                                        --len;
                                break;
                        default:
-                               if (num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
+                               if (num && !iscntrl((int) buf[0]) && (len + num < sizeof(passwd))) {
                                        memcpy(passwd + len, buf, num);
                                        len += num;
                                }
@@ -174,13 +181,20 @@ readpw(Display *dpy, const char *pws)
                                }
                        } else if (llen != 0 && len == 0) {
                                for (screen = 0; screen < nscreens; screen++) {
-                                       XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[EMPTY]);
+                                       XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[failure || failonclear ? FAILED : INIT]);
                                        XClearWindow(dpy, locks[screen]->win);
                                }
                        }
                        llen = len;
-               }
-               else for (screen = 0; screen < nscreens; screen++)
+               } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
+                       XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
+                       for (screen = 0; screen < nscreens; screen++) {
+                               if (locks[screen]->win == rre->window) {
+                                       XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
+                                       XClearWindow(dpy, locks[screen]->win);
+                               }
+                       }
+               } else for (screen = 0; screen < nscreens; screen++)
                        XRaiseWindow(dpy, locks[screen]->win);
        }
 }
@@ -236,6 +250,8 @@ lockscreen(Display *dpy, int screen)
        invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
        XDefineCursor(dpy, lock->win, invisible);
        XMapRaised(dpy, lock->win);
+       if (rr)
+               XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
        for (len = 1000; len; len--) {
                if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
                    GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
@@ -295,6 +311,7 @@ main(int argc, char **argv) {
 
        if (!(dpy = XOpenDisplay(0)))
                die("slock: cannot open display\n");
+       rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
        /* Get the number of screens in display "dpy" and blank them all. */
        nscreens = ScreenCount(dpy);
        locks = malloc(sizeof(Lock *) * nscreens);