diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-04 04:57:48 -0800 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-04 04:57:48 -0800 |
commit | 450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb (patch) | |
tree | e8580a7aa9944a5936416cafbdee79f4756d1ec0 | |
parent | 33163fb30686fdac5891ec860c19996ebec7683a (diff) | |
download | xkbdlock-450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb.tar.gz xkbdlock-450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb.tar.xz |
attempt to grab keyboard for 500ms
-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) |