X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=77a61b982f52f1345fda297f8b74b6255e2586ce;hb=c9465859a6eac7763d0b9049dca6bd34c163d8f6;hp=c3cad1d85c5909d29856fab7402965b68bf454b0;hpb=e980c7ff1814b533f674eec535af6c5658b1bc62;p=dmenu.git diff --git a/main.c b/main.c index c3cad1d..77a61b9 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(void) { unsigned int tw, w; if(!curr) @@ -69,25 +70,24 @@ calcoffsets() } static void -drawmenu() -{ +drawmenu(void) { Item *i; dc.x = 0; 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,21 +95,20 @@ 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); } static void -match(char *pattern) -{ +match(char *pattern) { unsigned int plen; Item *i, *j; @@ -121,19 +120,8 @@ match(char *pattern) nitem = 0; for(i = allitems; i; i=i->next) - if(!plen || !strncmp(pattern, i->text, plen)) { - if(!j) - item = i; - else - j->right = i; - i->left = j; - i->right = NULL; - j = i; - nitem++; - } - for(i = allitems; i; i=i->next) - if(plen && strncmp(pattern, i->text, plen) - && strstr(i->text, pattern)) { + if(plen ? !strncmp(pattern, i->text, plen) : + strncmp(pattern, i->text, plen) && strstr(i->text, pattern)) { if(!j) item = i; else @@ -149,8 +137,7 @@ match(char *pattern) } static void -kpress(XKeyEvent * e) -{ +kpress(XKeyEvent * e) { char buf[32]; int num, prev_nitem; unsigned int i, len; @@ -210,10 +197,8 @@ kpress(XKeyEvent * e) } break; case XK_Return: - if(e->state & ShiftMask) { - if(text) - fprintf(stdout, "%s", text); - } + if((e->state & ShiftMask) && text) + fprintf(stdout, "%s", text); else if(sel) fprintf(stdout, "%s", sel->text); else if(text) @@ -249,8 +234,7 @@ kpress(XKeyEvent * e) } static char * -readstdin() -{ +readstdin(void) { static char *maxname = NULL; char *p, buf[1024]; unsigned int len = 0, max = 0; @@ -287,9 +271,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,18 +292,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 +322,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 +349,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 +358,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 +374,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;