X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=03c8b1f68d78cfb733053bc8ee3f2413709d113f;hb=0e5f467aa85e318ed27fc5d6d3826426a06ef3c3;hp=c3cad1d85c5909d29856fab7402965b68bf454b0;hpb=e980c7ff1814b533f674eec535af6c5658b1bc62;p=dmenu.git diff --git a/main.c b/main.c index c3cad1d..03c8b1f 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -77,17 +79,17 @@ drawmenu() dc.y = 0; dc.w = mw; dc.h = mh; - drawtext(NULL, 0, False); + drawtext(NULL, dc.norm); /* print command */ if(cmdw && item) dc.w = cmdw; - drawtext(text[0] ? text : NULL, 0, False); + drawtext(text[0] ? text : NULL, dc.norm); dc.x += cmdw; if(curr) { dc.w = SPACE; - drawtext((curr && curr->left) ? "<" : NULL, 0, False); + drawtext((curr && curr->left) ? "<" : NULL, dc.norm); dc.x += dc.w; /* determine maximum items */ @@ -95,13 +97,13 @@ drawmenu() dc.w = textw(i->text); if(dc.w > mw / 3) dc.w = mw / 3; - drawtext(i->text, sel == i ? 1 : 0, sel == i); + drawtext(i->text, (sel == i) ? dc.sel : dc.norm); dc.x += dc.w; } dc.x = mw - SPACE; dc.w = SPACE; - drawtext(next ? ">" : NULL, 0, False); + drawtext(next ? ">" : NULL, dc.norm); } XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); XFlush(dpy); @@ -290,6 +292,8 @@ int main(int argc, char *argv[]) { char *maxname; + fd_set rd; + struct timeval timeout; Item *i; XEvent ev; XSetWindowAttributes wa; @@ -307,18 +311,28 @@ 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.bg[0] = getcolor(NORMBGCOLOR); - dc.fg[0] = getcolor(NORMFGCOLOR); - dc.bg[1] = getcolor(SELBGCOLOR); - dc.fg[1] = getcolor(SELFGCOLOR); + dc.sel[ColBG] = getcolor(SELBGCOLOR); + dc.sel[ColFG] = getcolor(SELFGCOLOR); + dc.norm[ColBG] = getcolor(NORMBGCOLOR); + dc.norm[ColFG] = getcolor(NORMFGCOLOR); setfont(FONT); wa.override_redirect = 1; @@ -327,7 +341,7 @@ main(int argc, char *argv[]) mx = my = 0; mw = DisplayWidth(dpy, screen); - mh = dc.font.height + 4; + mh = dc.font.height + 2; win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, DefaultDepth(dpy, screen), CopyFromParent, @@ -354,6 +368,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 +377,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 +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;