diff options
-rw-r--r-- | xlockkbd.c | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -1,6 +1,7 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <X11/Xlib.h> #include <X11/keysym.h> @@ -12,9 +13,21 @@ static void die(const char *s) static bool grab_kbd(Display *d, int screen) { - return (XGrabKeyboard(d, RootWindow(d, screen), True, GrabModeAsync, - GrabModeAsync, CurrentTime) - == GrabSuccess); + Window w = RootWindow(d, screen); + int res; + + // attempt to grab keyboard for 500ms if already grabbed. + // thanks to slock code for the pointer. + for (int i = 0;;) { + res = XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, + CurrentTime); + if (res != AlreadyGrabbed || ++i > 5) + break; + + usleep(100000); + } + + return (res == GrabSuccess); } static void wait_for_key(Display *d) |