summaryrefslogtreecommitdiff
path: root/xmouseless.c
diff options
context:
space:
mode:
authorJohannes Bensmann <johannesbensmann@gmx.de>2019-06-10 09:52:26 +0200
committerJohannes Bensmann <johannesbensmann@gmx.de>2019-06-10 09:52:26 +0200
commit5077622825373893a1a48cd057277068bebe9884 (patch)
tree5d0703fec10a48c943c0e05e9b89aca9acf5f30f /xmouseless.c
parentb0fbf0c7cb5783923486322560f63aa7fcd1cb5b (diff)
grab keys until success
Diffstat (limited to 'xmouseless.c')
-rw-r--r--xmouseless.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/xmouseless.c b/xmouseless.c
index e6acd29..5e714e7 100644
--- a/xmouseless.c
+++ b/xmouseless.c
@@ -84,6 +84,8 @@ void click(int button, int is_press) {
}
void init_x() {
+ int i;
+
/* initialize support for concurrent threads */
XInitThreads();
@@ -91,16 +93,27 @@ void init_x() {
screen=DefaultScreen(dpy);
root = RootWindow(dpy, screen);
- XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
-
/* turn auto key repeat off */
XAutoRepeatOff(dpy);
+
+ /* grab keys until success */
+ for (i = 0; i < 100; i++) {
+ if (XGrabKeyboard(dpy, root, False, GrabModeAsync,
+ GrabModeAsync, CurrentTime) == GrabSuccess)
+ return;
+ usleep(10000);
+ }
+
+ printf("grab keyboard failed");
+ close_x(EXIT_FAILURE);
}
-void close_x() {
+void close_x(int exit_status) {
/* turn auto repeat on again */
XAutoRepeatOn(dpy);
+ XUngrabKey(dpy, AnyKey, AnyModifier, root);
XCloseDisplay(dpy);
+ exit(exit_status);
}
void *moveforever(void *val) {
@@ -191,8 +204,7 @@ void handle_keyrelease(XKeyEvent event) {
/* exit */
for (i = 0; i < LENGTH(exit_keys); i++) {
if (exit_keys[i] == keysym) {
- close_x();
- exit(0);
+ close_x(EXIT_SUCCESS);
}
}
}