timplemented early keyboard grab for dmenu with a timeout for stdin data writers to prevent endless grabbings of the keyboard - dmenu - Dmenu fork with xft fonts.
 (HTM) git clone git://r-36.net/dmenu
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 32f7fe483538be60e529415c8cbd9cfbb4ab86d9
 (DIR) parent 0fa5a339ffac530eb596cc1b93c4ee696ef0ec74
 (HTM) Author: Anselm R. Garbe <arg@10kloc.org>
       Date:   Fri,  8 Sep 2006 07:33:20 +0200
       
       implemented early keyboard grab for dmenu with a timeout for stdin data writers to prevent endless grabbings of the keyboard
       Diffstat:
         config.arg.h                        |       1 +
         config.default.h                    |       1 +
         main.c                              |      23 +++++++++++++++++++----
       
       3 files changed, 21 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/config.arg.h b/config.arg.h
       t@@ -8,3 +8,4 @@
        #define SELFGCOLOR                "#eeeeee"
        #define NORMBGCOLOR                "#333333"
        #define NORMFGCOLOR                "#dddddd"
       +#define STDIN_TIMEOUT                3 /* seconds */
 (DIR) diff --git a/config.default.h b/config.default.h
       t@@ -8,3 +8,4 @@
        #define SELFGCOLOR                "#eeeeee"
        #define NORMBGCOLOR                "#333366"
        #define NORMFGCOLOR                "#cccccc"
       +#define STDIN_TIMEOUT                3 /* seconds */
 (DIR) diff --git a/main.c b/main.c
       t@@ -11,6 +11,8 @@
        #include <stdio.h>
        #include <string.h>
        #include <unistd.h>
       +#include <sys/select.h>
       +#include <sys/time.h>
        #include <X11/cursorfont.h>
        #include <X11/Xutil.h>
        #include <X11/keysym.h>
       t@@ -290,6 +292,8 @@ int
        main(int argc, char *argv[])
        {
                char *maxname;
       +        fd_set rd;
       +        struct timeval timeout;
                Item *i;
                XEvent ev;
                XSetWindowAttributes wa;
       t@@ -307,13 +311,23 @@ main(int argc, char *argv[])
                screen = DefaultScreen(dpy);
                root = RootWindow(dpy, screen);
        
       -        maxname = readstdin();
       -
       -        /* grab as early as possible, but after reading all items!!! */
       +        /* Note, the select() construction allows to grab all keypresses as
       +         * early as possible, to not loose them. But if there is no standard
       +         * input supplied, we will make sure to exit after MAX_WAIT_STDIN
       +         * seconds. This is convenience behavior for rapid typers.
       +         */ 
                while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
                                 GrabModeAsync, CurrentTime) != GrabSuccess)
                        usleep(1000);
        
       +        timeout.tv_usec = 0;
       +        timeout.tv_sec = STDIN_TIMEOUT;
       +        FD_ZERO(&rd);
       +        FD_SET(STDIN_FILENO, &rd);
       +        if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
       +                goto UninitializedEnd;
       +        maxname = readstdin();
       +
                /* style */
                dc.sel[ColBG] = getcolor(SELBGCOLOR);
                dc.sel[ColFG] = getcolor(SELFGCOLOR);
       t@@ -366,7 +380,6 @@ main(int argc, char *argv[])
                        }
                }
        
       -        XUngrabKeyboard(dpy, CurrentTime);
                while(allitems) {
                        i = allitems->next;
                        free(allitems->text);
       t@@ -380,6 +393,8 @@ main(int argc, char *argv[])
                XFreePixmap(dpy, dc.drawable);
                XFreeGC(dpy, dc.gc);
                XDestroyWindow(dpy, win);
       +UninitializedEnd:
       +        XUngrabKeyboard(dpy, CurrentTime);
                XCloseDisplay(dpy);
        
                return ret;