Getting rid of some global variables
[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 Colormap colormap;
95static Display *dpy;
96static Visual *visual;
97static Window rootwin;
98static int screen;
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 /* open connection to server and set X variables */
121 if ((dpy = XOpenDisplay(NULL)) == NULL)
122 errx(1, "cannot open display");
123 screen = DefaultScreen(dpy);
124 visual = DefaultVisual(dpy, screen);
125 rootwin = RootWindow(dpy, screen);
126 colormap = DefaultColormap(dpy, screen);
127
128 /* setup */
129 getresources();
130 setupdc();
131 calcgeom();
132
133 /* generate menus and recalculate them */
134 rootmenu = parsestdin();
135 if (rootmenu == NULL)
136 errx(1, "no menu generated");
137 calcmenu(rootmenu);
138
139 /* grab mouse and keyboard */
140 grabpointer();
141 grabkeyboard();
142
143 /* run event loop */
144 run(rootmenu);
145
146 cleanup(rootmenu);
147 return 0;
148}
149
150/* read xrdb for configuration options */
151static void
152getresources(void)
153{
154 char *xrm;
155 long n;
156 char *type;
157 XrmDatabase xdb;
158 XrmValue xval;
159
160 XrmInitialize();
161 if ((xrm = XResourceManagerString(dpy)) == NULL)
162 return;
163
164 xdb = XrmGetStringDatabase(xrm);
165
166 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
167 if ((n = strtol(xval.addr, NULL, 10)) > 0)
168 border_pixels = n;
169 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
170 if ((n = strtol(xval.addr, NULL, 10)) > 0)
171 separator_pixels = n;
172 if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True)
173 if ((n = strtol(xval.addr, NULL, 10)) > 0)
174 padding_pixels = n;
175 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
176 if ((n = strtol(xval.addr, NULL, 10)) > 0)
177 width_pixels = n;
178 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
179 background_color = strdup(xval.addr);
180 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
181 foreground_color = strdup(xval.addr);
182 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
183 selbackground_color = strdup(xval.addr);
184 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
185 selforeground_color = strdup(xval.addr);
186 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
187 separator_color = strdup(xval.addr);
188 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
189 border_color = strdup(xval.addr);
190 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
191 font = strdup(xval.addr);
192
193 XrmDestroyDatabase(xdb);
194}
195
196/* get color from color string */
197static void
198getcolor(const char *s, XftColor *color)
199{
200 if(!XftColorAllocName(dpy, visual, colormap, s, color))
201 errx(1, "cannot allocate color: %s", s);
202}
203
204/* init draw context */
205static void
206setupdc(void)
207{
208 /* get color pixels */
209 getcolor(background_color, &dc.normal[ColorBG]);
210 getcolor(foreground_color, &dc.normal[ColorFG]);
211 getcolor(selbackground_color, &dc.selected[ColorBG]);
212 getcolor(selforeground_color, &dc.selected[ColorFG]);
213 getcolor(separator_color, &dc.separator);
214 getcolor(border_color, &dc.border);
215
216 /* try to get font */
217 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
218 errx(1, "cannot load font");
219
220 /* create common GC */
221 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
222}
223
224/* calculate menu and screen geometry */
225static void
226calcgeom(void)
227{
228 Window w1, w2; /* unused variables */
229 int a, b; /* unused variables */
230 unsigned mask; /* unused variable */
231
232 XQueryPointer(dpy, rootwin, &w1, &w2, &geom.cursx, &geom.cursy, &a, &b, &mask);
233 geom.screenw = DisplayWidth(dpy, screen);
234 geom.screenh = DisplayHeight(dpy, screen);
235 geom.itemh = dc.font->height + padding_pixels * 2;
236 geom.itemw = width_pixels;
237 geom.border = border_pixels;
238 geom.separator = separator_pixels;
239}
240
241/* allocate an item */
242static struct Item *
243allocitem(const char *label, const char *output)
244{
245 struct Item *item;
246
247 if ((item = malloc(sizeof *item)) == NULL)
248 err(1, "malloc");
249 if (*label == '\0') {
250 item->label = NULL;
251 item->output = NULL;
252 } else {
253 if ((item->label = strdup(label)) == NULL)
254 err(1, "strdup");
255 if ((item->output = strdup(output)) == NULL)
256 err(1, "strdup");
257 }
258 item->y = 0;
259 item->h = item->label ? geom.itemh : geom.separator;
260 if (item->label == NULL)
261 item->labellen = 0;
262 else
263 item->labellen = strlen(item->label);
264 item->next = NULL;
265 item->submenu = NULL;
266
267 return item;
268}
269
270/* allocate a menu */
271static struct Menu *
272allocmenu(struct Menu *parent, struct Item *list, unsigned level)
273{
274 XSetWindowAttributes swa;
275 struct Menu *menu;
276
277 if ((menu = malloc(sizeof *menu)) == NULL)
278 err(1, "malloc");
279 menu->parent = parent;
280 menu->list = list;
281 menu->caller = NULL;
282 menu->selected = NULL;
283 menu->w = geom.itemw;
284 menu->h = 0; /* calculated by calcmenu() */
285 menu->x = 0; /* calculated by calcmenu() */
286 menu->y = 0; /* calculated by calcmenu() */
287 menu->level = level;
288
289 swa.override_redirect = True;
290 swa.background_pixel = dc.normal[ColorBG].pixel;
291 swa.border_pixel = dc.border.pixel;
292 swa.save_under = True; /* pop-up windows should save_under*/
293 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
294 | PointerMotionMask | LeaveWindowMask;
295 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
296 CopyFromParent, CopyFromParent, CopyFromParent,
297 CWOverrideRedirect | CWBackPixel |
298 CWBorderPixel | CWEventMask | CWSaveUnder,
299 &swa);
300
301 return menu;
302}
303
304/* create menus and items from the stdin */
305static struct Menu *
306parsestdin(void)
307{
308 struct Menu *rootmenu;
309 char *s, buf[BUFSIZ];
310 char *label, *output;
311 unsigned level = 0;
312 unsigned i;
313 struct Item *curritem = NULL; /* item currently being read */
314 struct Menu *prevmenu = NULL; /* menu the previous item was added to */
315 struct Item *item; /* dummy item for for loops */
316 struct Menu *menu; /* dummy menu for for loops */
317 size_t count = 0; /* number of items in the current menu */
318
319 rootmenu = NULL;
320
321 while (fgets(buf, BUFSIZ, stdin) != NULL) {
322 level = 0;
323 s = buf;
324
325 while (*s == '\t') {
326 level++;
327 s++;
328 }
329
330 label = output = s;
331
332 while (*s != '\0' && *s != '\t' && *s != '\n')
333 s++;
334
335 while (*s == '\t')
336 *s++ = '\0';
337
338 if (*s != '\0' && *s != '\n')
339 output = s;
340
341 while (*s != '\0' && *s != '\n')
342 s++;
343
344 if (*s == '\n')
345 *s = '\0';
346
347 curritem = allocitem(label, output);
348
349 if (prevmenu == NULL) { /* there is no menu yet */
350 menu = allocmenu(NULL, curritem, level);
351 rootmenu = menu;
352 prevmenu = menu;
353 count = 1;
354 curritem->prev = NULL;
355 curritem->next = NULL;
356 } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
357 for (menu = prevmenu, i = level;
358 menu != NULL && i < prevmenu->level;
359 menu = menu->parent, i++)
360 ;
361
362 if (menu == NULL)
363 errx(1, "reached NULL menu");
364
365 for (item = menu->list; item->next != NULL; item = item->next)
366 ;
367
368 item->next = curritem;
369
370 curritem->prev = item;
371 curritem->next = NULL;
372
373 prevmenu = menu;
374 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
375 for (item = prevmenu->list; item->next != NULL; item = item->next)
376 ;
377 item->next = curritem;
378
379 curritem->prev = item;
380 curritem->next = NULL;
381
382 } else if (level > prevmenu->level) { /* item begins a new menu */
383 menu = allocmenu(prevmenu, curritem, level);
384
385 for (item = prevmenu->list; item->next != NULL; item = item->next)
386 ;
387
388 item->submenu = menu;
389 menu->caller = item;
390
391 curritem->prev = NULL;
392 curritem->next = NULL;
393
394 prevmenu = menu;
395 }
396 count++;
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 goto done;
559 }
560
561 /* find lowest common ancestor menu */
562 minlevel = MIN(currmenu->level, prevmenu->level);
563 maxlevel = MAX(currmenu->level, prevmenu->level);
564 if (currmenu->level == maxlevel) {
565 menu = currmenu;
566 menu_ = prevmenu;
567 } else {
568 menu = prevmenu;
569 menu_ = currmenu;
570 }
571 while (menu->level > minlevel)
572 menu = menu->parent;
573 while (menu != menu_) {
574 menu = menu->parent;
575 menu_ = menu_->parent;
576 }
577 lcamenu = menu;
578
579 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
580 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
581 menu->selected = NULL;
582 XUnmapWindow(dpy, menu->win);
583 }
584
585 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
586 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
587 XMapWindow(dpy, menu->win);
588 }
589
590done:
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 = 0 + 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)
733 break;
734 if (previtem != item) {
735 previtem = item;
736 menu->selected = item;
737 if (item->submenu != NULL) {
738 currmenu = item->submenu;
739 currmenu->selected = NULL;
740 } else {
741 currmenu = menu;
742 }
743 mapmenu(currmenu);
744 drawmenu(currmenu);
745 }
746 break;
747 case ButtonRelease:
748 menu = getmenu(currmenu, ev.xbutton.window);
749 item = getitem(menu, ev.xbutton.y);
750 if (menu == NULL || item == NULL)
751 break;
752selectitem:
753 if (item->label == NULL)
754 break; /* ignore separators */
755 if (item->submenu != NULL) {
756 currmenu = item->submenu;
757 } else {
758 printf("%s\n", item->output);
759 return;
760 }
761 mapmenu(currmenu);
762 currmenu->selected = currmenu->list;
763 drawmenu(currmenu);
764 break;
765 case ButtonPress:
766 menu = getmenu(currmenu, ev.xbutton.window);
767 if (menu == NULL)
768 return;
769 break;
770 case KeyPress:
771 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
772
773 /* esc closes xmenu when current menu is the root menu */
774 if (ksym == XK_Escape && currmenu->parent == NULL)
775 return;
776
777 /* Shift-Tab = ISO_Left_Tab */
778 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
779 ksym = XK_ISO_Left_Tab;
780
781 /* cycle through menu */
782 item = NULL;
783 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
784 item = itemcycle(currmenu, ITEMPREV);
785 } else if (ksym == XK_Tab || ksym == XK_Down) {
786 item = itemcycle(currmenu, ITEMNEXT);
787 } else if ((ksym == XK_Return || ksym == XK_Right) &&
788 currmenu->selected != NULL) {
789 item = currmenu->selected;
790 goto selectitem;
791 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
792 currmenu->parent != NULL) {
793 item = currmenu->parent->selected;
794 currmenu = currmenu->parent;
795 mapmenu(currmenu);
796 } else
797 break;
798 currmenu->selected = item;
799 drawmenu(currmenu);
800 break;
801 case LeaveNotify:
802 previtem = NULL;
803 currmenu->selected = NULL;
804 drawmenu(currmenu);
805 break;
806 }
807 }
808}
809
810/* recursivelly free pixmaps and destroy windows */
811static void
812freewindow(struct Menu *menu)
813{
814 struct Item *item;
815
816 for (item = menu->list; item != NULL; item = item->next)
817 if (item->submenu != NULL)
818 freewindow(item->submenu);
819
820 XFreePixmap(dpy, menu->pixmap);
821 XftDrawDestroy(menu->draw);
822 XDestroyWindow(dpy, menu->win);
823}
824
825/* cleanup and exit */
826static void
827cleanup(struct Menu *rootmenu)
828{
829 XUngrabPointer(dpy, CurrentTime);
830 XUngrabKeyboard(dpy, CurrentTime);
831
832 freewindow(rootmenu);
833
834 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
835 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
836 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
837 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
838 XftColorFree(dpy, visual, colormap, &dc.separator);
839 XftColorFree(dpy, visual, colormap, &dc.border);
840
841 XFreeGC(dpy, dc.gc);
842 XCloseDisplay(dpy);
843}
844
845/* show usage */
846static void
847usage(void)
848{
849 (void)fprintf(stderr, "usage: xmenu title...\n");
850 exit(1);
851}