From 450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 4 Mar 2024 04:57:48 -0800 Subject: attempt to grab keyboard for 500ms --- xlockkbd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/xlockkbd.c b/xlockkbd.c index fd9e659..3e98621 100644 --- a/xlockkbd.c +++ b/xlockkbd.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -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) -- cgit v1.2.3-70-g09d2