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