faster grab
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index 40b220c..d54b1b3 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -38,7 +38,7 @@ static void setup(void);
 
 static char text[BUFSIZ] = "";
 static int bh, mw, mh;
-static int inputw = 0;
+static int inputw;
 static int lines = 0;
 static int monitor = -1;
 static int promptw;
@@ -57,7 +57,7 @@ static DC *dc;
 static Item *items = NULL;
 static Item *matches, *matchend;
 static Item *prev, *curr, *next, *sel;
-static Window root, win;
+static Window win;
 
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 
@@ -78,7 +78,7 @@ main(int argc, char *argv[]) {
                        fast = True;
                else if(!strcmp(argv[i], "-i"))
                        fstrncmp = strncasecmp;
-               else if(i == argc-1)
+               else if(i+1 == argc)
                        goto usage;
                /* double flags */
                else if(!strcmp(argv[i], "-l"))
@@ -104,14 +104,14 @@ main(int argc, char *argv[]) {
        initfont(dc, font);
 
        if(fast) {
-               setup();
+               grabkeyboard();
                readstdin();
        }
        else {
                readstdin();
-               setup();
+               grabkeyboard();
        }
-       match();
+       setup();
        run();
        return EXIT_FAILURE;
 
@@ -209,7 +209,8 @@ grabkeyboard(void) {
        int i;
 
        for(i = 0; i < 1000; i++) {
-               if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
+               if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
+                                GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
                        return;
                usleep(1000);
        }
@@ -220,9 +221,9 @@ void
 insert(const char *s, ssize_t n) {
        if(strlen(text) + n > sizeof text - 1)
                return;
-       memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
+       memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
        if(n > 0)
-               memcpy(text + cursor, s, n);
+               memcpy(&text[cursor], s, n);
        cursor += n;
        match();
 }
@@ -444,19 +445,21 @@ paste(void) {
 
 void
 readstdin(void) {
-       char buf[sizeof text], *p;
-       size_t i, size = 0;
+       char buf[sizeof text], *p, *maxstr = NULL;
+       size_t i, max = 0, size = 0;
 
        for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
-               if(i+1 == size / sizeof *items || !items)
+               if(i+1 >= size / sizeof *items)
                        if(!(items = realloc(items, (size += BUFSIZ))))
                                eprintf("cannot realloc %u bytes:", size);
                if((p = strchr(buf, '\n')))
                        *p = '\0';
                if(!(items[i].text = strdup(buf)))
                        eprintf("cannot strdup %u bytes:", strlen(buf)+1);
-               inputw = MAX(inputw, textw(dc, items[i].text));
+               if(strlen(items[i].text) > max)
+                       max = strlen(maxstr = items[i].text);
        }
+       inputw = maxstr ? textw(dc, maxstr) : 0;
 }
 
 void
@@ -485,22 +488,21 @@ run(void) {
 
 void
 setup(void) {
-       int x, y, screen;
+       int x, y, screen = DefaultScreen(dc->dpy);
+       Window root = RootWindow(dc->dpy, screen);
        XSetWindowAttributes wa;
 #ifdef XINERAMA
        int n;
        XineramaScreenInfo *info;
 #endif
 
-       screen = DefaultScreen(dc->dpy);
-       root = RootWindow(dc->dpy, screen);
-       utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
-
        normcol[ColBG] = getcolor(dc, normbgcolor);
        normcol[ColFG] = getcolor(dc, normfgcolor);
        selcol[ColBG] = getcolor(dc, selbgcolor);
        selcol[ColFG] = getcolor(dc, selfgcolor);
 
+       utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
+
        /* menu geometry */
        bh = dc->font.height + 2;
        lines = MAX(lines, 0);
@@ -537,9 +539,10 @@ setup(void) {
                            DefaultVisual(dc->dpy, screen),
                            CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 
-       grabkeyboard();
        resizedc(dc, mw, mh);
        inputw = MIN(inputw, mw/3);
        promptw = prompt ? textw(dc, prompt) : 0;
        XMapRaised(dc->dpy, win);
+       drawmenu();
+       match();
 }