Adding icons patch
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index 890f590..abab13d 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -28,7 +28,6 @@ struct DC {
        XftColor border;
        XftColor separator;
 
        XftColor border;
        XftColor separator;
 
-       Drawable d;
        GC gc;
        XftFont *font;
 };
        GC gc;
        XftFont *font;
 };
@@ -71,11 +70,12 @@ struct Menu {
 static void getresources(void);
 static void getcolor(const char *s, XftColor *color);
 static void setupdc(void);
 static void getresources(void);
 static void getcolor(const char *s, XftColor *color);
 static void setupdc(void);
-static void calcgeom(void);
+static void calcgeom(struct Geometry *geom);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
+static struct Menu *buildmenutree(unsigned level, const char *label, const char *output);
 static struct Menu *parsestdin(void);
 static struct Menu *parsestdin(void);
-static void calcmenu(struct Menu *menu);
+static void calcmenu(struct Geometry *geom, struct Menu *menu);
 static void grabpointer(void);
 static void grabkeyboard(void);
 static struct Menu *getmenu(struct Menu *currmenu, Window win);
 static void grabpointer(void);
 static void grabkeyboard(void);
 static struct Menu *getmenu(struct Menu *currmenu, Window win);
@@ -87,24 +87,25 @@ static void drawmenu(struct Menu *currmenu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
 static void freemenu(struct Menu *menu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
 static void freemenu(struct Menu *menu);
-static void cleanup(struct Menu *rootmenu);
+static void cleanup(void);
 static void usage(void);
 
 static void usage(void);
 
-/* global variables (X stuff and geometries) */
+/* X stuff */
 static Display *dpy;
 static int screen;
 static Visual *visual;
 static Window rootwin;
 static Colormap colormap;
 static struct DC dc;
 static Display *dpy;
 static int screen;
 static Visual *visual;
 static Window rootwin;
 static Colormap colormap;
 static struct DC dc;
-static struct Geometry geom;
 
 #include "config.h"
 
 
 #include "config.h"
 
+/* xmenu: generate menu from stdin and print selected entry to stdout */
 int
 main(int argc, char *argv[])
 {
        struct Menu *rootmenu;
 int
 main(int argc, char *argv[])
 {
        struct Menu *rootmenu;
+       struct Geometry geom;
        int ch;
 
        while ((ch = getopt(argc, argv, "")) != -1) {
        int ch;
 
        while ((ch = getopt(argc, argv, "")) != -1) {
@@ -131,13 +132,13 @@ main(int argc, char *argv[])
        /* setup */
        getresources();
        setupdc();
        /* setup */
        getresources();
        setupdc();
-       calcgeom();
+       calcgeom(&geom);
 
        /* generate menus and recalculate them */
        rootmenu = parsestdin();
        if (rootmenu == NULL)
                errx(1, "no menu generated");
 
        /* generate menus and recalculate them */
        rootmenu = parsestdin();
        if (rootmenu == NULL)
                errx(1, "no menu generated");
-       calcmenu(rootmenu);
+       calcmenu(&geom, rootmenu);
 
        /* grab mouse and keyboard */
        grabpointer();
 
        /* grab mouse and keyboard */
        grabpointer();
@@ -146,7 +147,10 @@ main(int argc, char *argv[])
        /* run event loop */
        run(rootmenu);
 
        /* run event loop */
        run(rootmenu);
 
-       cleanup(rootmenu);
+       /* freeing stuff */
+       freemenu(rootmenu);
+       cleanup();
+
        return 0;
 }
 
        return 0;
 }
 
@@ -226,19 +230,19 @@ setupdc(void)
 
 /* calculate menu and screen geometry */
 static void
 
 /* calculate menu and screen geometry */
 static void
-calcgeom(void)
+calcgeom(struct Geometry *geom)
 {
        Window w1, w2;  /* unused variables */
        int a, b;       /* unused variables */
        unsigned mask;  /* unused variable */
 
 {
        Window w1, w2;  /* unused variables */
        int a, b;       /* unused variables */
        unsigned mask;  /* unused variable */
 
-       XQueryPointer(dpy, rootwin, &w1, &w2, &geom.cursx, &geom.cursy, &a, &b, &mask);
-       geom.screenw = DisplayWidth(dpy, screen);
-       geom.screenh = DisplayHeight(dpy, screen);
-       geom.itemh = dc.font->height + padding_pixels * 2;
-       geom.itemw = width_pixels;
-       geom.border = border_pixels;
-       geom.separator = separator_pixels;
+       XQueryPointer(dpy, rootwin, &w1, &w2, &geom->cursx, &geom->cursy, &a, &b, &mask);
+       geom->screenw = DisplayWidth(dpy, screen);
+       geom->screenh = DisplayHeight(dpy, screen);
+       geom->itemh = dc.font->height + padding_pixels * 2;
+       geom->itemw = width_pixels;
+       geom->border = border_pixels;
+       geom->separator = separator_pixels;
 }
 
 /* allocate an item */
 }
 
 /* allocate an item */
@@ -249,17 +253,21 @@ allocitem(const char *label, const char *output)
 
        if ((item = malloc(sizeof *item)) == NULL)
                err(1, "malloc");
 
        if ((item = malloc(sizeof *item)) == NULL)
                err(1, "malloc");
-       if (*label == '\0') {
+       if (label == NULL) {
                item->label = NULL;
                item->output = NULL;
        } else {
                if ((item->label = strdup(label)) == NULL)
                        err(1, "strdup");
                item->label = NULL;
                item->output = NULL;
        } else {
                if ((item->label = strdup(label)) == NULL)
                        err(1, "strdup");
-               if ((item->output = strdup(output)) == NULL)
-                       err(1, "strdup");
+               if (label == output) {
+                       item->output = item->label;
+               } else {
+                       if ((item->output = strdup(output)) == NULL)
+                               err(1, "strdup");
+               }
        }
        item->y = 0;
        }
        item->y = 0;
-       item->h = item->label ? geom.itemh : geom.separator;
+       item->h = 0;
        if (item->label == NULL)
                item->labellen = 0;
        else
        if (item->label == NULL)
                item->labellen = 0;
        else
@@ -283,7 +291,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
        menu->list = list;
        menu->caller = NULL;
        menu->selected = NULL;
        menu->list = list;
        menu->caller = NULL;
        menu->selected = NULL;
-       menu->w = geom.itemw;
+       menu->w = 0;    /* calculated by calcmenu() */
        menu->h = 0;    /* calculated by calcmenu() */
        menu->x = 0;    /* calculated by calcmenu() */
        menu->y = 0;    /* calculated by calcmenu() */
        menu->h = 0;    /* calculated by calcmenu() */
        menu->x = 0;    /* calculated by calcmenu() */
        menu->y = 0;    /* calculated by calcmenu() */
@@ -295,7 +303,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
        swa.save_under = True;  /* pop-up windows should save_under*/
        swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
                       | PointerMotionMask | LeaveWindowMask;
        swa.save_under = True;  /* pop-up windows should save_under*/
        swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
                       | PointerMotionMask | LeaveWindowMask;
-       menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
+       menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0,
                                  CopyFromParent, CopyFromParent, CopyFromParent,
                                  CWOverrideRedirect | CWBackPixel |
                                  CWBorderPixel | CWEventMask | CWSaveUnder,
                                  CopyFromParent, CopyFromParent, CopyFromParent,
                                  CWOverrideRedirect | CWBackPixel |
                                  CWBorderPixel | CWEventMask | CWSaveUnder,
@@ -304,96 +312,94 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
        return menu;
 }
 
        return menu;
 }
 
-/* create menus and items from the stdin */
+/* build the menu tree */
 static struct Menu *
 static struct Menu *
-parsestdin(void)
+buildmenutree(unsigned level, const char *label, const char *output)
 {
 {
-       char *s, buf[BUFSIZ];
-       char *label, *output;
-       unsigned level = 0;
+       static struct Menu *prevmenu = NULL;    /* menu the previous item was added to */
+       static struct Menu *rootmenu = NULL;    /* menu to be returned */
+       struct Item *curritem = NULL;           /* item currently being read */
+       struct Item *item;                      /* dummy item for loops */
+       struct Menu *menu;                      /* dummy menu for loops */
        unsigned i;
        unsigned i;
-       struct Item *curritem = NULL;   /* item currently being read */
-       struct Menu *prevmenu = NULL;   /* menu the previous item was added to */
-       struct Item *item;              /* dummy item for loops */
-       struct Menu *menu;              /* dummy menu for loops */
-       struct Menu *rootmenu;          /* menu to be returned */
-
-       rootmenu = NULL;
-
-       while (fgets(buf, BUFSIZ, stdin) != NULL) {
-               level = 0;
-               s = buf;
-
-               while (*s == '\t') {
-                       level++;
-                       s++;
-               }
-
-               label = output = s;
-
-               while (*s != '\0' && *s != '\t' && *s != '\n')
-                       s++;
-
-               while (*s == '\t')
-                       *s++ = '\0';
-
-               if (*s != '\0' && *s != '\n')
-                       output = s;
-
-               while (*s != '\0' && *s != '\n')
-                       s++;
-
-               if (*s == '\n')
-                       *s = '\0';
-
-               curritem = allocitem(label, output);
-
-               if (prevmenu == NULL) {                 /* there is no menu yet */
-                        menu = allocmenu(NULL, curritem, level);
-                        rootmenu = menu;
-                        prevmenu = menu;
-                        curritem->prev = NULL;
-                        curritem->next = NULL;
-               } else if (level < prevmenu->level) {   /* item is continuation of a parent menu*/
-                       for (menu = prevmenu, i = level;
-                             menu != NULL && i < prevmenu->level;
-                             menu = menu->parent, i++)
-                               ;
 
 
-                       if (menu == NULL)
-                               errx(1, "reached NULL menu");
-
-                       for (item = menu->list; item->next != NULL; item = item->next)
-                               ;
+       /* create the item */
+       curritem = allocitem(label, output);
+
+       /* put the item in the menu tree */
+       if (prevmenu == NULL) {                 /* there is no menu yet */
+                menu = allocmenu(NULL, curritem, level);
+                rootmenu = menu;
+                prevmenu = menu;
+                curritem->prev = NULL;
+       } else if (level < prevmenu->level) {   /* item is continuation of a parent menu */
+               /* go up the menu tree until find the menu this item continues */
+               for (menu = prevmenu, i = level;
+                         menu != NULL && i != prevmenu->level;
+                         menu = menu->parent, i++)
+                       ;
+               if (menu == NULL)
+                       errx(1, "reached NULL menu");
 
 
-                       item->next = curritem;
+               /* find last item in the new menu */
+               for (item = menu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               prevmenu = menu;
+               item->next = curritem;
+               curritem->prev = item;
+       } else if (level == prevmenu->level) {  /* item is a continuation of current menu */
+               /* find last item in the previous menu */
+               for (item = prevmenu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       prevmenu = menu;
-               } else if (level == prevmenu->level) {  /* item is a continuation of current menu */
-                       for (item = prevmenu->list; item->next != NULL; item = item->next)
-                               ;
-                       item->next = curritem;
+               item->next = curritem;
+               curritem->prev = item;
+       } else if (level > prevmenu->level) {   /* item begins a new menu */
+               menu = allocmenu(prevmenu, curritem, level);
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               /* find last item in the previous menu */
+               for (item = prevmenu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-               } else if (level > prevmenu->level) {   /* item begins a new menu */
-                       menu = allocmenu(prevmenu, curritem, level);
+               prevmenu = menu;
+               menu->caller = item;
+               item->submenu = menu;
+               curritem->prev = NULL;
+       }
 
 
-                       for (item = prevmenu->list; item->next != NULL; item = item->next)
-                               ;
+       return rootmenu;
+}
 
 
-                       item->submenu = menu;
-                       menu->caller = item;
+/* create menus and items from the stdin */
+static struct Menu *
+parsestdin(void)
+{
+       struct Menu *rootmenu;
+       char *s, buf[BUFSIZ];
+       char *label, *output;
+       unsigned level = 0;
 
 
-                       curritem->prev = NULL;
-                       curritem->next = NULL;
+       rootmenu = NULL;
 
 
-                       prevmenu = menu;
+       while (fgets(buf, BUFSIZ, stdin) != NULL) {
+               /* get the indentation level */
+               level = strspn(buf, "\t");
+
+               /* get the label */
+               s = level + buf;
+               label = strtok(s, "\t\n");
+
+               /* get the output */
+               output = strtok(NULL, "\n");
+               if (output == NULL) {
+                       output = label;
+               } else {
+                       while (*output == '\t')
+                               output++;
                }
                }
+
+               rootmenu = buildmenutree(level, label, output);
        }
 
        return rootmenu;
        }
 
        return rootmenu;
@@ -401,7 +407,7 @@ parsestdin(void)
 
 /* recursivelly calculate menu geometry and set window hints */
 static void
 
 /* recursivelly calculate menu geometry and set window hints */
 static void
-calcmenu(struct Menu *menu)
+calcmenu(struct Geometry *geom, struct Menu *menu)
 {
        static XClassHint classh = {PROGNAME, PROGNAME};
        XWindowChanges changes;
 {
        static XClassHint classh = {PROGNAME, PROGNAME};
        XWindowChanges changes;
@@ -412,13 +418,15 @@ calcmenu(struct Menu *menu)
        int width, height;
 
        /* calculate items positions and menu width and height */
        int width, height;
 
        /* calculate items positions and menu width and height */
-       menu->w = geom.itemw;
+       menu->w = geom->itemw;
        for (item = menu->list; item != NULL; item = item->next) {
                item->y = menu->h;
        for (item = menu->list; item != NULL; item = item->next) {
                item->y = menu->h;
+
                if (item->label == NULL)   /* height for separator item */
                if (item->label == NULL)   /* height for separator item */
-                       menu->h += geom.separator;
+                       item->h = geom->separator;
                else
                else
-                       menu->h += geom.itemh;
+                       item->h = geom->itemh;
+               menu->h += item->h;
 
                XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
                                   item->labellen, &ext);
 
                XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
                                   item->labellen, &ext);
@@ -427,38 +435,39 @@ calcmenu(struct Menu *menu)
        }
 
        /* calculate menu's x and y positions */
        }
 
        /* calculate menu's x and y positions */
-       width = menu->w + geom.border * 2;
-       height = menu->h + geom.border * 2;
+       width = menu->w + geom->border * 2;
+       height = menu->h + geom->border * 2;
        if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
        if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
-               if (geom.screenw - geom.cursx >= menu->w)
-                       menu->x = geom.cursx;
-               else if (geom.cursx > width)
-                       menu->x = geom.cursx - width;
-
-               if (geom.screenh - geom.cursy >= height)
-                       menu->y = geom.cursy;
-               else if (geom.screenh > height)
-                       menu->y = geom.screenh - height;
+               if (geom->screenw - geom->cursx >= menu->w)
+                       menu->x = geom->cursx;
+               else if (geom->cursx > width)
+                       menu->x = geom->cursx - width;
+
+               if (geom->screenh - geom->cursy >= height)
+                       menu->y = geom->cursy;
+               else if (geom->screenh > height)
+                       menu->y = geom->screenh - height;
        } else {                    /* else, calculate in respect to parent menu */
        } else {                    /* else, calculate in respect to parent menu */
-               if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
-                       menu->x = menu->parent->x + menu->parent->w + geom.border;
-               else if (menu->parent->x > menu->w + geom.border)
-                       menu->x = menu->parent->x - menu->w - geom.border;
+               if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width)
+                       menu->x = menu->parent->x + menu->parent->w + geom->border;
+               else if (menu->parent->x > menu->w + geom->border)
+                       menu->x = menu->parent->x - menu->w - geom->border;
 
 
-               if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
+               if (geom->screenh - (menu->caller->y + menu->parent->y) > height)
                        menu->y = menu->caller->y + menu->parent->y;
                        menu->y = menu->caller->y + menu->parent->y;
-               else if (geom.screenh - menu->parent->y > height)
+               else if (geom->screenh - menu->parent->y > height)
                        menu->y = menu->parent->y;
                        menu->y = menu->parent->y;
-               else if (geom.screenh > height)
-                       menu->y = geom.screenh - height;
+               else if (geom->screenh > height)
+                       menu->y = geom->screenh - height;
        }
 
        /* update menu geometry */
        }
 
        /* update menu geometry */
+       changes.border_width = geom->border;
        changes.height = menu->h;
        changes.width = menu->w;
        changes.x = menu->x;
        changes.y = menu->y;
        changes.height = menu->h;
        changes.width = menu->w;
        changes.x = menu->x;
        changes.y = menu->y;
-       XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
+       XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
 
        /* set window manager hints */
        sizeh.flags = PMaxSize | PMinSize;
 
        /* set window manager hints */
        sizeh.flags = PMaxSize | PMinSize;
@@ -475,7 +484,7 @@ calcmenu(struct Menu *menu)
        /* calculate positions of submenus */
        for (item = menu->list; item != NULL; item = item->next) {
                if (item->submenu != NULL)
        /* calculate positions of submenus */
        for (item = menu->list; item != NULL; item = item->next) {
                if (item->submenu != NULL)
-                       calcmenu(item->submenu);
+                       calcmenu(geom, item->submenu);
        }
 }
 
        }
 }
 
@@ -598,14 +607,12 @@ mapmenu(struct Menu *currmenu)
 static void
 drawseparator(struct Menu *menu, struct Item *item)
 {
 static void
 drawseparator(struct Menu *menu, struct Item *item)
 {
-       int linex, liney, linew;
+       int y;
 
 
-       linex = dc.font->height;
-       liney = item->y + item->h/2;
-       linew = menu->w - dc.font->height;
+       y = item->y + item->h/2;
 
        XSetForeground(dpy, dc.gc, dc.separator.pixel);
 
        XSetForeground(dpy, dc.gc, dc.separator.pixel);
-       XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
+       XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
 }
 
 /* draw regular item */
 }
 
 /* draw regular item */
@@ -821,6 +828,9 @@ freemenu(struct Menu *menu)
                        freemenu(item->submenu);
                tmp = item;
                item = item->next;
                        freemenu(item->submenu);
                tmp = item;
                item = item->next;
+               if (tmp->label != tmp->output)
+                       free(tmp->label);
+               free(tmp->output);
                free(tmp);
        }
 
                free(tmp);
        }
 
@@ -832,13 +842,11 @@ freemenu(struct Menu *menu)
 
 /* cleanup and exit */
 static void
 
 /* cleanup and exit */
 static void
-cleanup(struct Menu *rootmenu)
+cleanup(void)
 {
        XUngrabPointer(dpy, CurrentTime);
        XUngrabKeyboard(dpy, CurrentTime);
 
 {
        XUngrabPointer(dpy, CurrentTime);
        XUngrabKeyboard(dpy, CurrentTime);
 
-       freemenu(rootmenu);
-
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
        XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
        XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);