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