[hackers] [slock] Exit as soon as possible on input grabbing error

From: Quentin Rameau <quinq_AT_fifth.space>
Date: Tue, 30 Aug 2016 17:33:09 +0200

We want to know at once if slock failed or not to lock the screen, not
seing a black screen for a whole second (or two) and then die.
Thanks to ^7heo for reporting this.
---
 slock.c | 48 +++++++++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 25 deletions(-)
diff --git a/slock.c b/slock.c
index 210d5c8..7950dc6 100644
--- a/slock.c
+++ b/slock.c
_AT_@ -222,7 +222,6 @@ static Lock *
 lockscreen(Display *dpy, int screen)
 {
 	char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
-	unsigned int len;
 	int i;
 	Lock *lock;
 	XColor color, dummy;
_AT_@ -249,34 +248,33 @@ lockscreen(Display *dpy, int screen)
 	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);
-	XMapRaised(dpy, lock->win);
-	if (rr)
-		XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
 
 	/* Try to grab mouse pointer *and* keyboard, else fail the lock */
-	for (len = 1000; len; len--) {
-		if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
-		    GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
-			break;
-		usleep(1000);
+	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 (!len) {
-		fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
-	} else {
-		for (len = 1000; len; len--) {
-			if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
-				/* everything fine, we grabbed both inputs */
-				XSelectInput(dpy, lock->root, SubstructureNotifyMask);
-				return lock;
-			}
-			usleep(1000);
-		}
-		fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
+
+	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;
 	}
-	/* grabbing one of the inputs failed */
-	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
-- 
2.9.3
Received on Tue Aug 30 2016 - 17:33:09 CEST

This archive was generated by hypermail 2.3.0 : Tue Aug 30 2016 - 17:36:16 CEST