JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
before leaning things up
[dwm.git] / menu.c
diff --git a/menu.c b/menu.c
index 650fc57..f150dbc 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -12,9 +12,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <time.h>
 #include <unistd.h>
 #include <X11/cursorfont.h>
 #include <X11/Xutil.h>
@@ -31,7 +28,6 @@ struct Item {
 static Display *dpy;
 static Window root;
 static Window win;
-static XRectangle rect;
 static Bool done = False;
 
 static Item *allitem = NULL;   /* first of all items */
@@ -41,14 +37,14 @@ static Item *nextoff = NULL;
 static Item *prevoff = NULL;
 static Item *curroff = NULL;
 
-static int screen;
+static int screen, mx, my, mw, mh;
 static char *title = NULL;
 static char text[4096];
 static int ret = 0;
 static int nitem = 0;
 static unsigned int cmdw = 0;
-static unsigned int twidth = 0;
-static unsigned int cwidth = 0;
+static unsigned int tw = 0;
+static unsigned int cw = 0;
 static const int seek = 30;            /* 30px */
 
 static Brush brush = {0};
@@ -59,11 +55,7 @@ static void kpress(XKeyEvent * e);
 static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
 
 static void
-usage()
-{
-       fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
-       exit(1);
-}
+usage() { error("usage: gridmenu [-v] [-t <title>]\n"); }
 
 static void
 update_offsets()
@@ -74,21 +66,21 @@ update_offsets()
                return;
 
        for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
-               tw = textwidth(&brush.font, nextoff->text);
-               if(tw > rect.width / 3)
-                       tw = rect.width / 3;
+               tw = textw(&brush.font, nextoff->text);
+               if(tw > mw / 3)
+                       tw = mw / 3;
                w += tw + brush.font.height;
-               if(w > rect.width)
+               if(w > mw)
                        break;
        }
 
        w = cmdw + 2 * seek;
        for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
-               tw = textwidth(&brush.font, prevoff->left->text);
-               if(tw > rect.width / 3)
-                       tw = rect.width / 3;
+               tw = textw(&brush.font, prevoff->left->text);
+               if(tw > mw / 3)
+                       tw = mw / 3;
                w += tw + brush.font.height;
-               if(w > rect.width)
+               if(w > mw)
                        break;
        }
 }
@@ -103,9 +95,9 @@ update_items(char *pattern)
                return;
 
        if(!title || *pattern)
-               cmdw = cwidth;
+               cmdw = cw;
        else
-               cmdw = twidth;
+               cmdw = tw;
 
        item = j = NULL;
        nitem = 0;
@@ -143,42 +135,40 @@ update_items(char *pattern)
 static void
 draw_menu()
 {
-       unsigned int offx = 0;
        Item *i;
 
-       brush.rect = rect;
-       brush.rect.x = 0;
-       brush.rect.y = 0;
+       brush.x = 0;
+       brush.y = 0;
+       brush.w = mw;
+       brush.h = mh;
        draw(dpy, &brush, False, 0);
 
        /* print command */
        if(!title || text[0]) {
-               cmdw = cwidth;
+               cmdw = cw;
                if(cmdw && item)
-                       brush.rect.width = cmdw;
+                       brush.w = cmdw;
                draw(dpy, &brush, False, text);
        }
        else {
-               cmdw = twidth;
-               brush.rect.width = cmdw;
+               cmdw = tw;
+               brush.w = cmdw;
                draw(dpy, &brush, False, title);
        }
-       offx += brush.rect.width;
+       brush.x += brush.w;
 
        if(curroff) {
-               brush.rect.x = offx;
-               brush.rect.width = seek;
-               offx += brush.rect.width;
+               brush.w = seek;
                draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
+               brush.x += brush.w;
 
                /* determine maximum items */
                for(i = curroff; i != nextoff; i=i->right) {
                        brush.border = False;
-                       brush.rect.x = offx;
-                       brush.rect.width = textwidth(&brush.font, i->text);
-                       if(brush.rect.width > rect.width / 3)
-                               brush.rect.width = rect.width / 3;
-                       brush.rect.width += brush.font.height;
+                       brush.w = textw(&brush.font, i->text);
+                       if(brush.w > mw / 3)
+                               brush.w = mw / 3;
+                       brush.w += brush.font.height;
                        if(sel == i) {
                                swap((void **)&brush.fg, (void **)&brush.bg);
                                draw(dpy, &brush, True, i->text);
@@ -186,15 +176,14 @@ draw_menu()
                        }
                        else
                                draw(dpy, &brush, False, i->text);
-                       offx += brush.rect.width;
+                       brush.x += brush.w;
                }
 
-               brush.rect.x = rect.width - seek;
-               brush.rect.width = seek;
+               brush.x = mw - seek;
+               brush.w = seek;
                draw(dpy, &brush, False, nextoff ? ">" : 0);
        }
-       XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width,
-                       rect.height, 0, 0);
+       XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
        XFlush(dpy);
 }
 
@@ -217,26 +206,12 @@ kpress(XKeyEvent * e)
        /* first check if a control mask is omitted */
        if(e->state & ControlMask) {
                switch (ksym) {
-               case XK_H:
+               default:        /* ignore other control sequences */
+                       return;
+                       break;
                case XK_h:
                        ksym = XK_BackSpace;
                        break;
-               case XK_I:
-               case XK_i:
-                       ksym = XK_Tab;
-                       break;
-               case XK_J:
-               case XK_j:
-                       ksym = XK_Return;
-                       break;
-               case XK_N:
-               case XK_n:
-                       ksym = XK_Right;
-                       break;
-               case XK_P:
-               case XK_p:
-                       ksym = XK_Left;
-                       break;
                case XK_U:
                case XK_u:
                        text[0] = 0;
@@ -247,12 +222,9 @@ kpress(XKeyEvent * e)
                case XK_bracketleft:
                        ksym = XK_Escape;
                        break;
-               default:        /* ignore other control sequences */
-                       return;
-                       break;
                }
        }
-       switch (ksym) {
+       switch(ksym) {
        case XK_Left:
                if(!(sel && sel->left))
                        return;
@@ -304,7 +276,7 @@ kpress(XKeyEvent * e)
                }
                break;
        default:
-               if((num == 1) && !iscntrl((int) buf[0])) {
+               if(num && !iscntrl((int) buf[0])) {
                        buf[num] = 0;
                        if(len > 0)
                                strncat(text, buf, sizeof(text));
@@ -399,36 +371,35 @@ main(int argc, char *argv[])
        wa.background_pixmap = ParentRelative;
        wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
 
-       rect.width = DisplayWidth(dpy, screen);
-       rect.height = labelheight(&brush.font);
-       rect.y = DisplayHeight(dpy, screen) - rect.height;
-       rect.x = 0;
+       mx = my = 0;
+       mw = DisplayWidth(dpy, screen);
+       mh = texth(&brush.font);
 
-       win = XCreateWindow(dpy, root, rect.x, rect.y,
-                       rect.width, rect.height, 0, DefaultDepth(dpy, screen),
-                       CopyFromParent, DefaultVisual(dpy, screen),
+       win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
+                       DefaultDepth(dpy, screen), CopyFromParent,
+                       DefaultVisual(dpy, screen),
                        CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
        XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
        XFlush(dpy);
 
        /* pixmap */
        brush.gc = XCreateGC(dpy, root, 0, 0);
-       brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
+       brush.drawable = XCreatePixmap(dpy, win, mw, mh,
                        DefaultDepth(dpy, screen));
        XFlush(dpy);
 
        if(maxname)
-               cwidth = textwidth(&brush.font, maxname) + brush.font.height;
-       if(cwidth > rect.width / 3)
-               cwidth = rect.width / 3;
+               cw = textw(&brush.font, maxname) + brush.font.height;
+       if(cw > mw / 3)
+               cw = mw / 3;
 
        if(title) {
-               twidth = textwidth(&brush.font, title) + brush.font.height;
-               if(twidth > rect.width / 3)
-                       twidth = rect.width / 3;
+               tw = textw(&brush.font, title) + brush.font.height;
+               if(tw > mw / 3)
+                       tw = mw / 3;
        }
 
-       cmdw = title ? twidth : cwidth;
+       cmdw = title ? tw : cw;
 
        text[0] = 0;
        update_items(text);
@@ -437,21 +408,18 @@ main(int argc, char *argv[])
        XFlush(dpy);
 
        /* main event loop */
-       while(!XNextEvent(dpy, &ev)) {
+       while(!done && !XNextEvent(dpy, &ev)) {
                switch (ev.type) {
-                       case KeyPress:
-                               kpress(&ev.xkey);
-                               break;
-                       case Expose:
-                               if(ev.xexpose.count == 0) {
-                                       draw_menu();
-                               }
-                               break;
-                       default:
-                               break;
-               }
-               if(done)
+               case KeyPress:
+                       kpress(&ev.xkey);
+                       break;
+               case Expose:
+                       if(ev.xexpose.count == 0)
+                               draw_menu();
                        break;
+               default:
+                       break;
+               }
        }
 
        XUngrabKeyboard(dpy, CurrentTime);