also, don't set the font all the time
[dmenu.git] / main.c
diff --git a/main.c b/main.c
index c1d48dd..0f07773 100644 (file)
--- 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);
@@ -196,7 +203,7 @@ match(char *pattern) {
 static void
 kpress(XKeyEvent * e) {
        char buf[32];
-       int i, num, prev_nitem;
+       int i, num;
        unsigned int len;
        KeySym ksym;
 
@@ -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++) {
@@ -490,6 +493,8 @@ main(int argc, char *argv[]) {
        dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
        dc.gc = XCreateGC(dpy, root, 0, 0);
        XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
+       if(!dc.font.set)
+               XSetFont(dpy, dc.gc, dc.font.xfont->fid);
        if(maxname)
                cmdw = textw(maxname);
        if(cmdw > mw / 3)