aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnselm R. Garbe <arg@10kloc.org>2006-09-08 07:33:20 +0200
committerAnselm R. Garbe <arg@10kloc.org>2006-09-08 07:33:20 +0200
commit32f7fe483538be60e529415c8cbd9cfbb4ab86d9 (patch)
tree5949b1aa7263a9aeacae026ac74dd4ba4fdc5c93
parent0fa5a339ffac530eb596cc1b93c4ee696ef0ec74 (diff)
implemented early keyboard grab for dmenu with a timeout for stdin data writers to prevent endless grabbings of the keyboard
-rw-r--r--config.arg.h1
-rw-r--r--config.default.h1
-rw-r--r--main.c23
3 files changed, 21 insertions, 4 deletions
diff --git a/config.arg.h b/config.arg.h
index a19e463..5b99a2c 100644
--- a/config.arg.h
+++ b/config.arg.h
@@ -8,3 +8,4 @@
8#define SELFGCOLOR "#eeeeee" 8#define SELFGCOLOR "#eeeeee"
9#define NORMBGCOLOR "#333333" 9#define NORMBGCOLOR "#333333"
10#define NORMFGCOLOR "#dddddd" 10#define NORMFGCOLOR "#dddddd"
11#define STDIN_TIMEOUT 3 /* seconds */
diff --git a/config.default.h b/config.default.h
index 1f66553..0bae3a1 100644
--- a/config.default.h
+++ b/config.default.h
@@ -8,3 +8,4 @@
8#define SELFGCOLOR "#eeeeee" 8#define SELFGCOLOR "#eeeeee"
9#define NORMBGCOLOR "#333366" 9#define NORMBGCOLOR "#333366"
10#define NORMFGCOLOR "#cccccc" 10#define NORMFGCOLOR "#cccccc"
11#define STDIN_TIMEOUT 3 /* seconds */
diff --git a/main.c b/main.c
index 6ca043d..03c8b1f 100644
--- a/main.c
+++ b/main.c
@@ -11,6 +11,8 @@
11#include <stdio.h> 11#include <stdio.h>
12#include <string.h> 12#include <string.h>
13#include <unistd.h> 13#include <unistd.h>
14#include <sys/select.h>
15#include <sys/time.h>
14#include <X11/cursorfont.h> 16#include <X11/cursorfont.h>
15#include <X11/Xutil.h> 17#include <X11/Xutil.h>
16#include <X11/keysym.h> 18#include <X11/keysym.h>
@@ -290,6 +292,8 @@ int
290main(int argc, char *argv[]) 292main(int argc, char *argv[])
291{ 293{
292 char *maxname; 294 char *maxname;
295 fd_set rd;
296 struct timeval timeout;
293 Item *i; 297 Item *i;
294 XEvent ev; 298 XEvent ev;
295 XSetWindowAttributes wa; 299 XSetWindowAttributes wa;
@@ -307,13 +311,23 @@ main(int argc, char *argv[])
307 screen = DefaultScreen(dpy); 311 screen = DefaultScreen(dpy);
308 root = RootWindow(dpy, screen); 312 root = RootWindow(dpy, screen);
309 313
310 maxname = readstdin(); 314 /* Note, the select() construction allows to grab all keypresses as
311 315 * early as possible, to not loose them. But if there is no standard
312 /* grab as early as possible, but after reading all items!!! */ 316 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
317 * seconds. This is convenience behavior for rapid typers.
318 */
313 while(XGrabKeyboard(dpy, root, True, GrabModeAsync, 319 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
314 GrabModeAsync, CurrentTime) != GrabSuccess) 320 GrabModeAsync, CurrentTime) != GrabSuccess)
315 usleep(1000); 321 usleep(1000);
316 322
323 timeout.tv_usec = 0;
324 timeout.tv_sec = STDIN_TIMEOUT;
325 FD_ZERO(&rd);
326 FD_SET(STDIN_FILENO, &rd);
327 if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
328 goto UninitializedEnd;
329 maxname = readstdin();
330
317 /* style */ 331 /* style */
318 dc.sel[ColBG] = getcolor(SELBGCOLOR); 332 dc.sel[ColBG] = getcolor(SELBGCOLOR);
319 dc.sel[ColFG] = getcolor(SELFGCOLOR); 333 dc.sel[ColFG] = getcolor(SELFGCOLOR);
@@ -366,7 +380,6 @@ main(int argc, char *argv[])
366 } 380 }
367 } 381 }
368 382
369 XUngrabKeyboard(dpy, CurrentTime);
370 while(allitems) { 383 while(allitems) {
371 i = allitems->next; 384 i = allitems->next;
372 free(allitems->text); 385 free(allitems->text);
@@ -380,6 +393,8 @@ main(int argc, char *argv[])
380 XFreePixmap(dpy, dc.drawable); 393 XFreePixmap(dpy, dc.drawable);
381 XFreeGC(dpy, dc.gc); 394 XFreeGC(dpy, dc.gc);
382 XDestroyWindow(dpy, win); 395 XDestroyWindow(dpy, win);
396UninitializedEnd:
397 XUngrabKeyboard(dpy, CurrentTime);
383 XCloseDisplay(dpy); 398 XCloseDisplay(dpy);
384 399
385 return ret; 400 return ret;