X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=60567b2332c2bac1a536e2b77bf15aaac6c9df27;hb=81bcf078f6c510314f140471efb8952160fdb3ad;hp=51fa20fa0079aeb251f50c3f6408d83edcd54399;hpb=13ef97e65ea5a713a3d5ab46916d1ac6d071b825;p=dmenu.git diff --git a/main.c b/main.c index 51fa20f..60567b2 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -40,8 +42,7 @@ static Window root; static Window win; static void -calcoffsets() -{ +calcoffsets() { unsigned int tw, w; if(!curr) @@ -69,8 +70,7 @@ calcoffsets() } static void -drawmenu() -{ +drawmenu() { Item *i; dc.x = 0; @@ -108,8 +108,7 @@ drawmenu() } static void -match(char *pattern) -{ +match(char *pattern) { unsigned int plen; Item *i, *j; @@ -149,8 +148,7 @@ match(char *pattern) } static void -kpress(XKeyEvent * e) -{ +kpress(XKeyEvent * e) { char buf[32]; int num, prev_nitem; unsigned int i, len; @@ -249,8 +247,7 @@ kpress(XKeyEvent * e) } static char * -readstdin() -{ +readstdin() { static char *maxname = NULL; char *p, buf[1024]; unsigned int len = 0, max = 0; @@ -287,9 +284,10 @@ Display *dpy; DC dc = {0}; int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { char *maxname; + fd_set rd; + struct timeval timeout; Item *i; XEvent ev; XSetWindowAttributes wa; @@ -307,13 +305,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); @@ -354,6 +362,8 @@ main(int argc, char *argv[]) /* main event loop */ while(running && !XNextEvent(dpy, &ev)) { switch (ev.type) { + default: /* ignore all crap */ + break; case KeyPress: kpress(&ev.xkey); break; @@ -361,12 +371,9 @@ main(int argc, char *argv[]) if(ev.xexpose.count == 0) drawmenu(); break; - default: - break; } } - XUngrabKeyboard(dpy, CurrentTime); while(allitems) { i = allitems->next; free(allitems->text); @@ -380,6 +387,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;