Simplifying parsing and data structure building
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index 5b41cb0..5fefae8 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;
 };
@@ -74,6 +73,7 @@ static void setupdc(void);
 static void calcgeom(void);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
 static void calcgeom(void);
 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 void calcmenu(struct Menu *menu);
 static void grabpointer(void);
 static struct Menu *parsestdin(void);
 static void calcmenu(struct Menu *menu);
 static void grabpointer(void);
@@ -86,8 +86,8 @@ static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
 static void drawmenu(struct Menu *currmenu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
 static void drawmenu(struct Menu *currmenu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
-static void freewindow(struct Menu *menu);
-static void cleanup(struct Menu *rootmenu);
+static void freemenu(struct Menu *menu);
+static void cleanup(void);
 static void usage(void);
 
 /* global variables (X stuff and geometries) */
 static void usage(void);
 
 /* global variables (X stuff and geometries) */
@@ -146,7 +146,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;
 }
 
@@ -249,14 +252,18 @@ 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->h = item->label ? geom.itemh : geom.separator;
        }
        item->y = 0;
        item->h = item->label ? geom.itemh : geom.separator;
@@ -304,96 +311,92 @@ 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)
-                               ;
 
 
-                       item->next = curritem;
+       /* 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");
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               /* find last item in the new menu */
+               for (item = menu->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;
+               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)
+                       ;
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               item->next = curritem;
+               curritem->prev = item;
+       } else if (level > prevmenu->level) {   /* item begins a new menu */
+               menu = allocmenu(prevmenu, curritem, level);
 
 
-               } else if (level > prevmenu->level) {   /* item begins a new menu */
-                       menu = allocmenu(prevmenu, curritem, level);
+               /* find last item in the previous menu */
+               for (item = prevmenu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       for (item = prevmenu->list; item->next != NULL; item = item->next)
-                               ;
+               prevmenu = menu;
+               menu->caller = item;
+               item->submenu = menu;
+               curritem->prev = NULL;
+       }
 
 
-                       item->submenu = menu;
-                       menu->caller = item;
+       return rootmenu;
+}
 
 
-                       curritem->prev = NULL;
-                       curritem->next = NULL;
+/* 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;
 
 
-                       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;
@@ -409,6 +412,7 @@ calcmenu(struct Menu *menu)
        XGlyphInfo ext;
        struct Item *item;
        int labelwidth;
        XGlyphInfo ext;
        struct Item *item;
        int labelwidth;
+       int width, height;
 
        /* calculate items positions and menu width and height */
        menu->w = geom.itemw;
 
        /* calculate items positions and menu width and height */
        menu->w = geom.itemw;
@@ -426,28 +430,30 @@ 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;
        if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
                if (geom.screenw - geom.cursx >= menu->w)
                        menu->x = geom.cursx;
        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 > menu->w)
-                       menu->x = geom.cursx - menu->w;
+               else if (geom.cursx > width)
+                       menu->x = geom.cursx - width;
 
 
-               if (geom.screenh - geom.cursy >= menu->h)
+               if (geom.screenh - geom.cursy >= height)
                        menu->y = geom.cursy;
                        menu->y = geom.cursy;
-               else if (geom.screenh > menu->h)
-                       menu->y = geom.screenh - menu->h;
+               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) >= menu->w)
+               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;
 
                        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) > menu->h)
+               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 > menu->h)
+               else if (geom.screenh - menu->parent->y > height)
                        menu->y = menu->parent->y;
                        menu->y = menu->parent->y;
-               else if (geom.screenh > menu->h)
-                       menu->y = geom.screenh - menu->h;
+               else if (geom.screenh > height)
+                       menu->y = geom.screenh - height;
        }
 
        /* update menu geometry */
        }
 
        /* update menu geometry */
@@ -595,14 +601,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 */
@@ -807,28 +811,36 @@ selectitem:
 
 /* recursivelly free pixmaps and destroy windows */
 static void
 
 /* recursivelly free pixmaps and destroy windows */
 static void
-freewindow(struct Menu *menu)
+freemenu(struct Menu *menu)
 {
        struct Item *item;
 {
        struct Item *item;
+       struct Item *tmp;
 
 
-       for (item = menu->list; item != NULL; item = item->next)
+       item = menu->list;
+       while (item != NULL) {
                if (item->submenu != NULL)
                if (item->submenu != NULL)
-                       freewindow(item->submenu);
+                       freemenu(item->submenu);
+               tmp = item;
+               item = item->next;
+               if (tmp->label != tmp->output)
+                       free(tmp->label);
+               free(tmp->output);
+               free(tmp);
+       }
 
        XFreePixmap(dpy, menu->pixmap);
        XftDrawDestroy(menu->draw);
        XDestroyWindow(dpy, menu->win);
 
        XFreePixmap(dpy, menu->pixmap);
        XftDrawDestroy(menu->draw);
        XDestroyWindow(dpy, menu->win);
+       free(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);
 
-       freewindow(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]);