X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=e4c490336814ef8c8b7153320a67bc5eef1fd4dc;hb=4042a11e5174633e9beeeec120937856e1d0f74f;hp=29b313ba5ccd6b32215f362f9f7567233ce1562d;hpb=9911455f5b7787c999a570faf33cf6ad0bec056c;p=dmenu.git diff --git a/main.c b/main.c index 29b313b..e4c4903 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ -/* (C)opyright MMVI-MMVII Anselm R. Garbe - * (C)opyright MMVI-MMVII Sander van Dijk - * See LICENSE file for license details. - */ +/* © 2006-2007 Anselm R. Garbe + * © 2006-2007 Sander van Dijk + * See LICENSE file for license details. */ #include "dmenu.h" #include #include @@ -108,11 +107,17 @@ drawmenu(void) { XFlush(dpy); } -static void +static Bool grabkeyboard(void) { - while(XGrabKeyboard(dpy, win, True, GrabModeAsync, - GrabModeAsync, CurrentTime) != GrabSuccess) + unsigned int len; + + for(len = 1000; len; len--) { + if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime) + == GrabSuccess) + break; usleep(1000); + } + return len > 0; } static unsigned long @@ -130,6 +135,8 @@ initfont(const char *fontstr) { char *def, **missing; int i, n; + if(!fontstr || fontstr[0] == '\0') + eprint("error, cannot load font: '%s'\n", fontstr); missing = NULL; if(dc.font.set) XFreeFontSet(dpy, dc.font.set); @@ -210,6 +217,13 @@ kpress(XKeyEvent * e) { len = strlen(text); buf[0] = 0; num = XLookupString(e, buf, sizeof buf, &ksym, 0); + if(IsKeypadKey(ksym)) { + if(ksym == XK_KP_Enter) { + ksym = XK_Return; + } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) { + ksym = (ksym - XK_KP_0) + XK_0; + } + } if(IsFunctionKey(ksym) || IsKeypadKey(ksym) || IsMiscFunctionKey(ksym) || IsPFKey(ksym) || IsPrivateKeypadKey(ksym)) @@ -445,7 +459,7 @@ main(int argc, char *argv[]) { if(++i < argc) selfg = argv[i]; } else if(!strncmp(argv[i], "-v", 3)) - eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n"); + eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n"); else usage(); setlocale(LC_CTYPE, ""); @@ -454,6 +468,24 @@ main(int argc, char *argv[]) { eprint("dmenu: cannot open display\n"); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); + if(isatty(STDIN_FILENO)) { + maxname = readstdin(); + running = grabkeyboard(); + } + else { /* prevent keypress loss */ + running = grabkeyboard(); + maxname = readstdin(); + } + /* init modifier map */ + modmap = XGetModifierMapping(dpy); + for (i = 0; i < 8; i++) { + for (j = 0; j < modmap->max_keypermod; j++) { + if(modmap->modifiermap[i * modmap->max_keypermod + j] + == XKeysymToKeycode(dpy, XK_Num_Lock)) + numlockmask = (1 << i); + } + } + XFreeModifiermap(modmap); /* style */ dc.norm[ColBG] = initcolor(normbg); dc.norm[ColFG] = initcolor(normfg); @@ -477,27 +509,6 @@ main(int argc, char *argv[]) { XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); if(!dc.font.set) XSetFont(dpy, dc.gc, dc.font.xfont->fid); - drawmenu(); - XMapRaised(dpy, win); - XMaskEvent(dpy, ExposureMask, &ev); - drawmenu(); - if(isatty(STDIN_FILENO)) { - maxname = readstdin(); - grabkeyboard(); - } - else { /* prevent keypress loss */ - grabkeyboard(); - maxname = readstdin(); - } - /* init modifier map */ - modmap = XGetModifierMapping(dpy); - for(i = 0; i < 8; i++) - for(j = 0; j < modmap->max_keypermod; j++) { - if(modmap->modifiermap[i * modmap->max_keypermod + j] - == XKeysymToKeycode(dpy, XK_Num_Lock)) - numlockmask = (1 << i); - } - XFreeModifiermap(modmap); if(maxname) cmdw = textw(maxname); if(cmdw > mw / 3) @@ -508,6 +519,7 @@ main(int argc, char *argv[]) { promptw = mw / 5; text[0] = 0; match(text); + XMapRaised(dpy, win); drawmenu(); XSync(dpy, False); @@ -519,6 +531,10 @@ main(int argc, char *argv[]) { case KeyPress: kpress(&ev.xkey); break; + case Expose: + if(ev.xexpose.count == 0) + drawmenu(); + break; } /* cleanup */