Improved README
[xmenu] / xmenu.c
... / ...
CommitLineData
1#include <err.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <X11/Xlib.h>
7#include <X11/Xutil.h>
8#include <X11/Xresource.h>
9#include <X11/XKBlib.h>
10#include <X11/Xft/Xft.h>
11
12#define PROGNAME "xmenu"
13#define ITEMPREV 0
14#define ITEMNEXT 1
15
16/* macros */
17#define LEN(x) (sizeof (x) / sizeof (x[0]))
18#define MAX(x,y) ((x)>(y)?(x):(y))
19#define MIN(x,y) ((x)<(y)?(x):(y))
20
21/* color enum */
22enum {ColorFG, ColorBG, ColorLast};
23
24/* draw context structure */
25struct DC {
26 XftColor normal[ColorLast];
27 XftColor selected[ColorLast];
28 XftColor border;
29 XftColor separator;
30
31 Drawable d;
32 GC gc;
33 XftFont *font;
34};
35
36/* menu geometry structure */
37struct MenuGeometry {
38 int itemb; /* item border */
39 int itemw; /* item width */
40 int itemh; /* item height */
41 int border; /* window border width */
42 int separator; /* menu separator width */
43};
44
45/* screen geometry structure */
46struct ScreenGeometry {
47 int cursx, cursy; /* cursor position */
48 int screenw, screenh; /* screen width and height */
49};
50
51/* menu item structure */
52struct Item {
53 char *label; /* string to be drawed on menu */
54 char *output; /* string to be outputed when item is clicked */
55 int y; /* item y position relative to menu */
56 int h; /* item height */
57 size_t labellen; /* strlen(label) */
58 struct Item *prev; /* previous item */
59 struct Item *next; /* next item */
60 struct Menu *submenu; /* submenu spawned by clicking on item */
61};
62
63/* menu structure */
64struct Menu {
65 struct Menu *parent; /* parent menu */
66 struct Item *caller; /* item that spawned the menu */
67 struct Item *list; /* list of items contained by the menu */
68 struct Item *selected; /* item currently selected in the menu */
69 int x, y, w, h; /* menu geometry */
70 unsigned level; /* menu level relative to root */
71 Drawable pixmap; /* pixmap to draw the menu on */
72 XftDraw *draw;
73 Window win; /* menu window to map on the screen */
74};
75
76/* function declarations */
77static void getcolor(const char *s, XftColor *color);
78static void getresources(void);
79static void setupdc(void);
80static void setupgeom(void);
81static struct Item *allocitem(const char *label, const char *output);
82static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
83static struct Menu *getmenu(Window win);
84static struct Item *getitem(struct Menu *menu, int y);
85static void drawseparator(struct Menu *menu, struct Item *item);
86static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
87static void drawmenu(void);
88static void calcscreengeom(void);
89static void calcmenu(struct Menu *menu);
90static void grabpointer(void);
91static void grabkeyboard(void);
92static void setcurrmenu(struct Menu *currmenu_new);
93static void parsestdin(void);
94static void run(void);
95static void freewindow(struct Menu *menu);
96static void cleanup(void);
97static void usage(void);
98
99/* X variables */
100static Colormap colormap;
101static Display *dpy;
102static Visual *visual;
103static Window rootwin;
104static int screen;
105static struct DC dc;
106static struct ScreenGeometry screengeom;
107
108/* menu variables */
109static struct Menu *rootmenu = NULL;
110static struct Menu *currmenu = NULL;
111static struct MenuGeometry geom;
112
113#include "config.h"
114
115int
116main(int argc, char *argv[])
117{
118 int ch;
119
120 while ((ch = getopt(argc, argv, "")) != -1) {
121 switch (ch) {
122 default:
123 usage();
124 break;
125 }
126 }
127 argc -= optind;
128 argv += optind;
129
130 /* open connection to server and set X variables */
131 if ((dpy = XOpenDisplay(NULL)) == NULL)
132 errx(1, "cannot open display");
133 screen = DefaultScreen(dpy);
134 visual = DefaultVisual(dpy, screen);
135 rootwin = RootWindow(dpy, screen);
136 colormap = DefaultColormap(dpy, screen);
137
138 /* setup */
139 getresources();
140 setupdc();
141 setupgeom();
142
143 /* generate menus and recalculate them */
144 parsestdin();
145 if (rootmenu == NULL)
146 errx(1, "no menu generated");
147 calcscreengeom();
148 calcmenu(rootmenu);
149
150 /* grab mouse and keyboard */
151 grabpointer();
152 grabkeyboard();
153
154 /* run event loop */
155 run();
156
157 cleanup();
158 return 0;
159}
160
161/* read xrdb for configuration options */
162static void
163getresources(void)
164{
165 char *xrm;
166 long n;
167 char *type;
168 XrmDatabase xdb;
169 XrmValue xval;
170
171 XrmInitialize();
172 if ((xrm = XResourceManagerString(dpy)) == NULL)
173 return;
174
175 xdb = XrmGetStringDatabase(xrm);
176
177 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
178 if ((n = strtol(xval.addr, NULL, 10)) > 0)
179 border_pixels = n;
180 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
181 if ((n = strtol(xval.addr, NULL, 10)) > 0)
182 separator_pixels = n;
183 if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True)
184 if ((n = strtol(xval.addr, NULL, 10)) > 0)
185 padding_pixels = n;
186 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
187 if ((n = strtol(xval.addr, NULL, 10)) > 0)
188 width_pixels = n;
189 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
190 background_color = strdup(xval.addr);
191 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
192 foreground_color = strdup(xval.addr);
193 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
194 selbackground_color = strdup(xval.addr);
195 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
196 selforeground_color = strdup(xval.addr);
197 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
198 separator_color = strdup(xval.addr);
199 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
200 border_color = strdup(xval.addr);
201 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
202 font = strdup(xval.addr);
203
204 XrmDestroyDatabase(xdb);
205}
206
207/* get color from color string */
208static void
209getcolor(const char *s, XftColor *color)
210{
211 if(!XftColorAllocName(dpy, visual, colormap, s, color))
212 errx(1, "cannot allocate color: %s", s);
213}
214
215/* init draw context */
216static void
217setupdc(void)
218{
219 /* get color pixels */
220 getcolor(background_color, &dc.normal[ColorBG]);
221 getcolor(foreground_color, &dc.normal[ColorFG]);
222 getcolor(selbackground_color, &dc.selected[ColorBG]);
223 getcolor(selforeground_color, &dc.selected[ColorFG]);
224 getcolor(separator_color, &dc.separator);
225 getcolor(border_color, &dc.border);
226
227 /* try to get font */
228 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
229 errx(1, "cannot load font");
230
231 /* create common GC */
232 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
233}
234
235/* init menu geometry values */
236static void
237setupgeom(void)
238{
239 geom.itemb = padding_pixels;
240 geom.itemh = dc.font->height + padding_pixels * 2;
241 geom.itemw = width_pixels;
242 geom.border = border_pixels;
243 geom.separator = separator_pixels;
244}
245
246/* allocate an item */
247static struct Item *
248allocitem(const char *label, const char *output)
249{
250 struct Item *item;
251
252 if ((item = malloc(sizeof *item)) == NULL)
253 err(1, "malloc");
254 if (*label == '\0') {
255 item->label = NULL;
256 item->output = NULL;
257 } else {
258 if ((item->label = strdup(label)) == NULL)
259 err(1, "strdup");
260 if ((item->output = strdup(output)) == NULL)
261 err(1, "strdup");
262 }
263 item->y = 0;
264 item->h = item->label ? geom.itemh : geom.separator;
265 if (item->label == NULL)
266 item->labellen = 0;
267 else
268 item->labellen = strlen(item->label);
269 item->next = NULL;
270 item->submenu = NULL;
271
272 return item;
273}
274
275/* allocate a menu */
276static struct Menu *
277allocmenu(struct Menu *parent, struct Item *list, unsigned level)
278{
279 XSetWindowAttributes swa;
280 struct Menu *menu;
281
282 if ((menu = malloc(sizeof *menu)) == NULL)
283 err(1, "malloc");
284 menu->parent = parent;
285 menu->list = list;
286 menu->caller = NULL;
287 menu->selected = NULL;
288 menu->w = geom.itemw;
289 menu->h = 0; /* calculated by calcmenu() */
290 menu->x = 0; /* calculated by calcmenu() */
291 menu->y = 0; /* calculated by calcmenu() */
292 menu->level = level;
293
294 swa.override_redirect = True;
295 swa.background_pixel = dc.normal[ColorBG].pixel;
296 swa.border_pixel = dc.border.pixel;
297 swa.save_under = True; /* pop-up windows should save_under*/
298 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
299 | PointerMotionMask | LeaveWindowMask;
300 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
301 CopyFromParent, CopyFromParent, CopyFromParent,
302 CWOverrideRedirect | CWBackPixel |
303 CWBorderPixel | CWEventMask | CWSaveUnder,
304 &swa);
305
306 return menu;
307}
308
309/* create menus and items from the stdin */
310static void
311parsestdin(void)
312{
313 char *s, buf[BUFSIZ];
314 char *label, *output;
315 unsigned level = 0;
316 unsigned i;
317 struct Item *curritem = NULL; /* item currently being read */
318 struct Menu *prevmenu = NULL; /* menu the previous item was added to */
319 struct Item *item; /* dummy item for for loops */
320 struct Menu *menu; /* dummy menu for for loops */
321 size_t count = 0; /* number of items in the current menu */
322
323 while (fgets(buf, BUFSIZ, stdin) != NULL) {
324 level = 0;
325 s = buf;
326
327 while (*s == '\t') {
328 level++;
329 s++;
330 }
331
332 label = output = s;
333
334 while (*s != '\0' && *s != '\t' && *s != '\n')
335 s++;
336
337 while (*s == '\t')
338 *s++ = '\0';
339
340 if (*s != '\0' && *s != '\n')
341 output = s;
342
343 while (*s != '\0' && *s != '\n')
344 s++;
345
346 if (*s == '\n')
347 *s = '\0';
348
349 curritem = allocitem(label, output);
350
351 if (prevmenu == NULL) { /* there is no menu yet */
352 menu = allocmenu(NULL, curritem, level);
353 rootmenu = menu;
354 prevmenu = menu;
355 count = 1;
356 curritem->prev = NULL;
357 curritem->next = NULL;
358 } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
359 for (menu = prevmenu, i = level;
360 menu != NULL && i < prevmenu->level;
361 menu = menu->parent, i++)
362 ;
363
364 if (menu == NULL)
365 errx(1, "reached NULL menu");
366
367 for (item = menu->list; item->next != NULL; item = item->next)
368 ;
369
370 item->next = curritem;
371
372 curritem->prev = item;
373 curritem->next = NULL;
374
375 prevmenu = menu;
376 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
377 for (item = prevmenu->list; item->next != NULL; item = item->next)
378 ;
379 item->next = curritem;
380
381 curritem->prev = item;
382 curritem->next = NULL;
383
384 } else if (level > prevmenu->level) { /* item begins a new menu */
385 menu = allocmenu(prevmenu, curritem, level);
386
387 for (item = prevmenu->list; item->next != NULL; item = item->next)
388 ;
389
390 item->submenu = menu;
391 menu->caller = item;
392
393 curritem->prev = NULL;
394 curritem->next = NULL;
395
396 prevmenu = menu;
397 }
398 count++;
399 }
400}
401
402/* calculate screen geometry */
403static void
404calcscreengeom(void)
405{
406 Window w1, w2; /* unused variables */
407 int a, b; /* unused variables */
408 unsigned mask; /* unused variable */
409
410 XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
411 screengeom.screenw = DisplayWidth(dpy, screen);
412 screengeom.screenh = DisplayHeight(dpy, screen);
413}
414
415/* recursivelly calculate menu geometry and set window hints */
416static void
417calcmenu(struct Menu *menu)
418{
419 static XClassHint classh = {PROGNAME, PROGNAME};
420 XWindowChanges changes;
421 XSizeHints sizeh;
422 XGlyphInfo ext;
423 struct Item *item;
424 int labelwidth;
425
426 /* calculate items positions and menu width and height */
427 menu->w = geom.itemw;
428 for (item = menu->list; item != NULL; item = item->next) {
429 item->y = menu->h;
430 if (item->label == NULL) /* height for separator item */
431 menu->h += geom.separator;
432 else
433 menu->h += geom.itemh;
434
435 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
436 item->labellen, &ext);
437 labelwidth = ext.xOff + dc.font->height * 2;
438 menu->w = MAX(menu->w, labelwidth);
439 }
440
441 /* calculate menu's x and y positions */
442 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
443 if (screengeom.screenw - screengeom.cursx >= menu->w)
444 menu->x = screengeom.cursx;
445 else if (screengeom.cursx > menu->w)
446 menu->x = screengeom.cursx - menu->w;
447
448 if (screengeom.screenh - screengeom.cursy >= menu->h)
449 menu->y = screengeom.cursy;
450 else if (screengeom.screenh > menu->h)
451 menu->y = screengeom.screenh - menu->h;
452 } else { /* else, calculate in respect to parent menu */
453 if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w)
454 menu->x = menu->parent->x + menu->parent->w + geom.border;
455 else if (menu->parent->x > menu->w + geom.border)
456 menu->x = menu->parent->x - menu->w - geom.border;
457
458 if (screengeom.screenh - (menu->caller->y + menu->parent->y) > menu->h)
459 menu->y = menu->caller->y + menu->parent->y;
460 else if (screengeom.screenh - menu->parent->y > menu->h)
461 menu->y = menu->parent->y;
462 else if (screengeom.screenh > menu->h)
463 menu->y = screengeom.screenh - menu->h;
464 }
465
466 /* update menu geometry */
467 changes.height = menu->h;
468 changes.width = menu->w;
469 changes.x = menu->x;
470 changes.y = menu->y;
471 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
472
473 /* set window manager hints */
474 sizeh.flags = PMaxSize | PMinSize;
475 sizeh.min_width = sizeh.max_width = menu->w;
476 sizeh.min_height = sizeh.max_height = menu->h;
477 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
478 NULL, &classh);
479
480 /* create pixmap and XftDraw */
481 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
482 DefaultDepth(dpy, screen));
483 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
484
485 /* calculate positions of submenus */
486 for (item = menu->list; item != NULL; item = item->next) {
487 if (item->submenu != NULL)
488 calcmenu(item->submenu);
489 }
490}
491
492/* try to grab pointer, we may have to wait for another process to ungrab */
493static void
494grabpointer(void)
495{
496 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
497 int i;
498
499 for (i = 0; i < 1000; i++) {
500 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
501 GrabModeAsync, GrabModeAsync, None,
502 None, CurrentTime) == GrabSuccess)
503 return;
504 nanosleep(&ts, NULL);
505 }
506 errx(1, "cannot grab keyboard");
507}
508
509/* try to grab keyboard, we may have to wait for another process to ungrab */
510static void
511grabkeyboard(void)
512{
513 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
514 int i;
515
516 for (i = 0; i < 1000; i++) {
517 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
518 GrabModeAsync, CurrentTime) == GrabSuccess)
519 return;
520 nanosleep(&ts, NULL);
521 }
522 errx(1, "cannot grab keyboard");
523}
524
525/* get menu of given window */
526static struct Menu *
527getmenu(Window win)
528{
529 struct Menu *menu;
530
531 for (menu = currmenu; menu != NULL; menu = menu->parent)
532 if (menu->win == win)
533 return menu;
534
535 return NULL;
536}
537
538/* get item of given menu and position */
539static struct Item *
540getitem(struct Menu *menu, int y)
541{
542 struct Item *item;
543
544 if (menu == NULL)
545 return NULL;
546
547 for (item = menu->list; item != NULL; item = item->next)
548 if (y >= item->y && y <= item->y + item->h)
549 return item;
550
551 return NULL;
552}
553
554/* set currentmenu to menu, umap previous menus and map current menu and its parents */
555static void
556setcurrmenu(struct Menu *currmenu_new)
557{
558 struct Menu *menu, *menu_;
559 struct Item *item;
560 struct Menu *lcamenu; /* lowest common ancestor menu */
561 unsigned minlevel; /* level of the closest to root menu */
562 unsigned maxlevel; /* level of the closest to root menu */
563
564 /* do not update currmenu to itself */
565 if (currmenu_new == currmenu)
566 return;
567
568 /* if there was no currmenu, skip calculations */
569 if (currmenu == NULL) {
570 currmenu = currmenu_new;
571 XMapWindow(dpy, currmenu->win);
572 return;
573 }
574
575 /* find lowest common ancestor menu */
576 lcamenu = rootmenu;
577 minlevel = MIN(currmenu_new->level, currmenu->level);
578 maxlevel = MAX(currmenu_new->level, currmenu->level);
579 if (currmenu_new->level == maxlevel) {
580 menu = currmenu_new;
581 menu_ = currmenu;
582 } else {
583 menu = currmenu;
584 menu_ = currmenu_new;
585 }
586 while (menu->level > minlevel)
587 menu = menu->parent;
588 while (menu != menu_) {
589 menu = menu->parent;
590 menu_ = menu_->parent;
591 }
592 lcamenu = menu;
593
594 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
595 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
596 menu->selected = NULL;
597 XUnmapWindow(dpy, menu->win);
598 }
599
600 currmenu = currmenu_new;
601
602 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
603 item = NULL;
604 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
605 XMapWindow(dpy, menu->win);
606 }
607}
608
609/* draw separator item */
610static void
611drawseparator(struct Menu *menu, struct Item *item)
612{
613 int linex, liney, linew;
614
615 linex = dc.font->height;
616 liney = item->y + item->h/2;
617 linew = menu->w - dc.font->height;
618
619 XSetForeground(dpy, dc.gc, dc.separator.pixel);
620 XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
621}
622
623/* draw regular item */
624static void
625drawitem(struct Menu *menu, struct Item *item, XftColor *color)
626{
627 int x, y;
628
629 x = 0 + dc.font->height;
630 y = item->y + dc.font->height + geom.itemb / 2;
631 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
632 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
633 x, y, item->label, item->labellen);
634
635 /* draw triangle, if item contains a submenu */
636 if (item->submenu != NULL) {
637 x = menu->w - dc.font->height + geom.itemb - 1;
638 y = item->y + geom.itemh/2 - triangle_height/2 - 1;
639
640 XPoint triangle[] = {
641 {x, y},
642 {x + triangle_width, y + triangle_height/2},
643 {x, y + triangle_height},
644 {x, y}
645 };
646
647 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
648 Convex, CoordModeOrigin);
649 }
650}
651
652/* draw items of the current menu and of its ancestors */
653static void
654drawmenu(void)
655{
656 struct Menu *menu;
657 struct Item *item;
658
659 for (menu = currmenu; menu != NULL; menu = menu->parent) {
660 for (item = menu->list; item != NULL; item = item->next) {
661 XftColor *color;
662
663 /* determine item color */
664 if (item == menu->selected && item->label != NULL)
665 color = dc.selected;
666 else
667 color = dc.normal;
668
669 /* draw item box */
670 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
671 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
672 menu->w, item->h);
673
674 if (item->label == NULL) /* item is a separator */
675 drawseparator(menu, item);
676 else /* item is a regular item */
677 drawitem(menu, item, color);
678 }
679
680 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
681 menu->w, menu->h, 0, 0);
682 }
683}
684
685/* cycle through the items; non-zero direction is next, zero is prev */
686static struct Item *
687itemcycle(int direction)
688{
689 struct Item *item;
690 struct Item *lastitem;
691
692 item = NULL;
693
694 if (direction == ITEMNEXT) {
695 if (currmenu->selected == NULL)
696 item = currmenu->list;
697 else if (currmenu->selected->next != NULL)
698 item = currmenu->selected->next;
699
700 while (item != NULL && item->label == NULL)
701 item = item->next;
702
703 if (item == NULL)
704 item = currmenu->list;
705 } else {
706 for (lastitem = currmenu->list;
707 lastitem != NULL && lastitem->next != NULL;
708 lastitem = lastitem->next)
709 ;
710
711 if (currmenu->selected == NULL)
712 item = lastitem;
713 else if (currmenu->selected->prev != NULL)
714 item = currmenu->selected->prev;
715
716 while (item != NULL && item->label == NULL)
717 item = item->prev;
718
719 if (item == NULL)
720 item = lastitem;
721 }
722
723 return item;
724}
725
726/* run event loop */
727static void
728run(void)
729{
730 struct Menu *menu;
731 struct Item *item;
732 struct Item *previtem = NULL;
733 KeySym ksym;
734 XEvent ev;
735
736 setcurrmenu(rootmenu);
737
738 while (!XNextEvent(dpy, &ev)) {
739 switch(ev.type) {
740 case Expose:
741 if (ev.xexpose.count == 0)
742 drawmenu();
743 break;
744 case MotionNotify:
745 menu = getmenu(ev.xbutton.window);
746 item = getitem(menu, ev.xbutton.y);
747 if (menu == NULL || item == NULL)
748 break;
749 if (previtem != item) {
750 menu->selected = item;
751 if (item->submenu != NULL)
752 setcurrmenu(item->submenu);
753 else
754 setcurrmenu(menu);
755 previtem = item;
756 drawmenu();
757 }
758 break;
759 case ButtonRelease:
760 menu = getmenu(ev.xbutton.window);
761 item = getitem(menu, ev.xbutton.y);
762 if (menu == NULL || item == NULL)
763 break;
764selectitem:
765 if (item->label == NULL)
766 break; /* ignore separators */
767 if (item->submenu != NULL) {
768 setcurrmenu(item->submenu);
769 } else {
770 printf("%s\n", item->output);
771 return;
772 }
773 currmenu->selected = currmenu->list;
774 drawmenu();
775 break;
776 case ButtonPress:
777 menu = getmenu(ev.xbutton.window);
778 if (menu == NULL)
779 return;
780 break;
781 case KeyPress:
782 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
783
784 /* esc closes xmenu when current menu is the root menu */
785 if (ksym == XK_Escape && currmenu == rootmenu)
786 return;
787
788 /* Shift-Tab = ISO_Left_Tab */
789 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
790 ksym = XK_ISO_Left_Tab;
791
792 /* cycle through menu */
793 item = NULL;
794 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
795 item = itemcycle(ITEMPREV);
796 } else if (ksym == XK_Tab || ksym == XK_Down) {
797 item = itemcycle(ITEMNEXT);
798 } else if ((ksym == XK_Return || ksym == XK_Right) &&
799 currmenu->selected != NULL) {
800 item = currmenu->selected;
801 goto selectitem;
802 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
803 currmenu->parent != NULL) {
804 item = currmenu->parent->selected;
805 setcurrmenu(currmenu->parent);
806 } else
807 break;
808 currmenu->selected = item;
809 drawmenu();
810 break;
811 case LeaveNotify:
812 previtem = NULL;
813 currmenu->selected = NULL;
814 drawmenu();
815 break;
816 }
817 }
818}
819
820/* recursivelly free pixmaps and destroy windows */
821static void
822freewindow(struct Menu *menu)
823{
824 struct Item *item;
825
826 for (item = menu->list; item != NULL; item = item->next)
827 if (item->submenu != NULL)
828 freewindow(item->submenu);
829
830 XFreePixmap(dpy, menu->pixmap);
831 XftDrawDestroy(menu->draw);
832 XDestroyWindow(dpy, menu->win);
833}
834
835/* cleanup and exit */
836static void
837cleanup(void)
838{
839 freewindow(rootmenu);
840
841 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
842 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
843 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
844 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
845 XftColorFree(dpy, visual, colormap, &dc.separator);
846 XftColorFree(dpy, visual, colormap, &dc.border);
847
848 XFreeGC(dpy, dc.gc);
849 XCloseDisplay(dpy);
850}
851
852/* show usage */
853static void
854usage(void)
855{
856 (void)fprintf(stderr, "usage: xmenu title...\n");
857 exit(1);
858}