summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-03-04 04:57:48 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-03-04 04:57:48 -0800
commit450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb (patch)
treee8580a7aa9944a5936416cafbdee79f4756d1ec0
parent33163fb30686fdac5891ec860c19996ebec7683a (diff)
downloadxkbdlock-450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb.tar.gz
xkbdlock-450ce37c4b4d6d9e3430e30cc0e7c9f2b0fec1fb.tar.xz
attempt to grab keyboard for 500ms
-rw-r--r--xlockkbd.c19
1 files 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 <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)