X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=b733feb5fc76a6c20578feab338b1fc4409a41e2;hb=03c2b05bb2265e0ef7ba33b7f6df36bd6e0ce54d;hp=c1d48ddcad51685a4f62fe224e4abdc349f85b3d;hpb=8a10b35c64ebe8791ed5c7325b31d86e269b308c;p=dmenu.git diff --git a/main.c b/main.c index c1d48dd..b733feb 100644 --- a/main.c +++ b/main.c @@ -108,6 +108,13 @@ drawmenu(void) { XFlush(dpy); } +static void +grabkeyboard(void) { + while(XGrabKeyboard(dpy, root, True, GrabModeAsync, + GrabModeAsync, CurrentTime) != GrabSuccess) + usleep(1000); +} + static unsigned long initcolor(const char *colstr) { Colormap cmap = DefaultColormap(dpy, screen); @@ -282,12 +289,8 @@ kpress(XKeyEvent * e) { } break; case XK_BackSpace: - if((i = len)) { - prev_nitem = nitem; - do { - text[--i] = 0; - match(text); - } while(i && nitem && prev_nitem == nitem); + if(len) { + text[--len] = 0; match(text); } break; @@ -418,10 +421,6 @@ main(int argc, char *argv[]) { XModifierKeymap *modmap; XSetWindowAttributes wa; - if(isatty(STDIN_FILENO)) { - fputs("error: dmenu can't run in an interactive shell\n", stdout); - usage(); - } /* command line args */ for(i = 1; i < argc; i++) if(!strncmp(argv[i], "-b", 3)) { @@ -455,10 +454,14 @@ main(int argc, char *argv[]) { eprint("dmenu: cannot open display\n"); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - while(XGrabKeyboard(dpy, root, True, GrabModeAsync, - GrabModeAsync, CurrentTime) != GrabSuccess) - usleep(1000); - maxname = readstdin(); + 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++) {