Fixing calculation of menu position.
[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 freemenu(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 int width, height;
413
414 /* calculate items positions and menu width and height */
415 menu->w = geom.itemw;
416 for (item = menu->list; item != NULL; item = item->next) {
417 item->y = menu->h;
418 if (item->label == NULL) /* height for separator item */
419 menu->h += geom.separator;
420 else
421 menu->h += geom.itemh;
422
423 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
424 item->labellen, &ext);
425 labelwidth = ext.xOff + dc.font->height * 2;
426 menu->w = MAX(menu->w, labelwidth);
427 }
428
429 /* calculate menu's x and y positions */
430 width = menu->w + geom.border * 2;
431 height = menu->h + geom.border * 2;
432 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
433 if (geom.screenw - geom.cursx >= menu->w)
434 menu->x = geom.cursx;
435 else if (geom.cursx > width)
436 menu->x = geom.cursx - width;
437
438 if (geom.screenh - geom.cursy >= height)
439 menu->y = geom.cursy;
440 else if (geom.screenh > height)
441 menu->y = geom.screenh - height;
442 } else { /* else, calculate in respect to parent menu */
443 if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
444 menu->x = menu->parent->x + menu->parent->w + geom.border;
445 else if (menu->parent->x > menu->w + geom.border)
446 menu->x = menu->parent->x - menu->w - geom.border;
447
448 if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
449 menu->y = menu->caller->y + menu->parent->y;
450 else if (geom.screenh - menu->parent->y > height)
451 menu->y = menu->parent->y;
452 else if (geom.screenh > height)
453 menu->y = geom.screenh - height;
454 }
455
456 /* update menu geometry */
457 changes.height = menu->h;
458 changes.width = menu->w;
459 changes.x = menu->x;
460 changes.y = menu->y;
461 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
462
463 /* set window manager hints */
464 sizeh.flags = PMaxSize | PMinSize;
465 sizeh.min_width = sizeh.max_width = menu->w;
466 sizeh.min_height = sizeh.max_height = menu->h;
467 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
468 NULL, &classh);
469
470 /* create pixmap and XftDraw */
471 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
472 DefaultDepth(dpy, screen));
473 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
474
475 /* calculate positions of submenus */
476 for (item = menu->list; item != NULL; item = item->next) {
477 if (item->submenu != NULL)
478 calcmenu(item->submenu);
479 }
480}
481
482/* try to grab pointer, we may have to wait for another process to ungrab */
483static void
484grabpointer(void)
485{
486 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
487 int i;
488
489 for (i = 0; i < 1000; i++) {
490 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
491 GrabModeAsync, GrabModeAsync, None,
492 None, CurrentTime) == GrabSuccess)
493 return;
494 nanosleep(&ts, NULL);
495 }
496 errx(1, "cannot grab keyboard");
497}
498
499/* try to grab keyboard, we may have to wait for another process to ungrab */
500static void
501grabkeyboard(void)
502{
503 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
504 int i;
505
506 for (i = 0; i < 1000; i++) {
507 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
508 GrabModeAsync, CurrentTime) == GrabSuccess)
509 return;
510 nanosleep(&ts, NULL);
511 }
512 errx(1, "cannot grab keyboard");
513}
514
515/* get menu of given window */
516static struct Menu *
517getmenu(struct Menu *currmenu, Window win)
518{
519 struct Menu *menu;
520
521 for (menu = currmenu; menu != NULL; menu = menu->parent)
522 if (menu->win == win)
523 return menu;
524
525 return NULL;
526}
527
528/* get item of given menu and position */
529static struct Item *
530getitem(struct Menu *menu, int y)
531{
532 struct Item *item;
533
534 if (menu == NULL)
535 return NULL;
536
537 for (item = menu->list; item != NULL; item = item->next)
538 if (y >= item->y && y <= item->y + item->h)
539 return item;
540
541 return NULL;
542}
543
544/* umap previous menus and map current menu and its parents */
545static void
546mapmenu(struct Menu *currmenu)
547{
548 static struct Menu *prevmenu = NULL;
549 struct Menu *menu, *menu_;
550 struct Menu *lcamenu; /* lowest common ancestor menu */
551 unsigned minlevel; /* level of the closest to root menu */
552 unsigned maxlevel; /* level of the closest to root menu */
553
554 /* do not remap current menu if it wasn't updated*/
555 if (prevmenu == currmenu)
556 return;
557
558 /* if this is the first time mapping, skip calculations */
559 if (prevmenu == NULL) {
560 XMapWindow(dpy, currmenu->win);
561 prevmenu = currmenu;
562 return;
563 }
564
565 /* find lowest common ancestor menu */
566 minlevel = MIN(currmenu->level, prevmenu->level);
567 maxlevel = MAX(currmenu->level, prevmenu->level);
568 if (currmenu->level == maxlevel) {
569 menu = currmenu;
570 menu_ = prevmenu;
571 } else {
572 menu = prevmenu;
573 menu_ = currmenu;
574 }
575 while (menu->level > minlevel)
576 menu = menu->parent;
577 while (menu != menu_) {
578 menu = menu->parent;
579 menu_ = menu_->parent;
580 }
581 lcamenu = menu;
582
583 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
584 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
585 menu->selected = NULL;
586 XUnmapWindow(dpy, menu->win);
587 }
588
589 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
590 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
591 XMapWindow(dpy, menu->win);
592 }
593
594 prevmenu = currmenu;
595}
596
597/* draw separator item */
598static void
599drawseparator(struct Menu *menu, struct Item *item)
600{
601 int linex, liney, linew;
602
603 linex = dc.font->height;
604 liney = item->y + item->h/2;
605 linew = menu->w - dc.font->height;
606
607 XSetForeground(dpy, dc.gc, dc.separator.pixel);
608 XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
609}
610
611/* draw regular item */
612static void
613drawitem(struct Menu *menu, struct Item *item, XftColor *color)
614{
615 int x, y;
616
617 x = dc.font->height;
618 y = item->y + item->h/2 + dc.font->ascent/2 - 1;
619 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
620 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
621 x, y, item->label, item->labellen);
622
623 /* draw triangle, if item contains a submenu */
624 if (item->submenu != NULL) {
625 x = menu->w - dc.font->height/2 - triangle_width/2;
626 y = item->y + item->h/2 - triangle_height/2 - 1;
627
628 XPoint triangle[] = {
629 {x, y},
630 {x + triangle_width, y + triangle_height/2},
631 {x, y + triangle_height},
632 {x, y}
633 };
634
635 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
636 Convex, CoordModeOrigin);
637 }
638}
639
640/* draw items of the current menu and of its ancestors */
641static void
642drawmenu(struct Menu *currmenu)
643{
644 struct Menu *menu;
645 struct Item *item;
646
647 for (menu = currmenu; menu != NULL; menu = menu->parent) {
648 for (item = menu->list; item != NULL; item = item->next) {
649 XftColor *color;
650
651 /* determine item color */
652 if (item == menu->selected && item->label != NULL)
653 color = dc.selected;
654 else
655 color = dc.normal;
656
657 /* draw item box */
658 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
659 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
660 menu->w, item->h);
661
662 if (item->label == NULL) /* item is a separator */
663 drawseparator(menu, item);
664 else /* item is a regular item */
665 drawitem(menu, item, color);
666 }
667
668 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
669 menu->w, menu->h, 0, 0);
670 }
671}
672
673/* cycle through the items; non-zero direction is next, zero is prev */
674static struct Item *
675itemcycle(struct Menu *currmenu, int direction)
676{
677 struct Item *item;
678 struct Item *lastitem;
679
680 item = NULL;
681
682 if (direction == ITEMNEXT) {
683 if (currmenu->selected == NULL)
684 item = currmenu->list;
685 else if (currmenu->selected->next != NULL)
686 item = currmenu->selected->next;
687
688 while (item != NULL && item->label == NULL)
689 item = item->next;
690
691 if (item == NULL)
692 item = currmenu->list;
693 } else {
694 for (lastitem = currmenu->list;
695 lastitem != NULL && lastitem->next != NULL;
696 lastitem = lastitem->next)
697 ;
698
699 if (currmenu->selected == NULL)
700 item = lastitem;
701 else if (currmenu->selected->prev != NULL)
702 item = currmenu->selected->prev;
703
704 while (item != NULL && item->label == NULL)
705 item = item->prev;
706
707 if (item == NULL)
708 item = lastitem;
709 }
710
711 return item;
712}
713
714/* run event loop */
715static void
716run(struct Menu *currmenu)
717{
718 struct Menu *menu;
719 struct Item *item;
720 struct Item *previtem = NULL;
721 KeySym ksym;
722 XEvent ev;
723
724 mapmenu(currmenu);
725
726 while (!XNextEvent(dpy, &ev)) {
727 switch(ev.type) {
728 case Expose:
729 if (ev.xexpose.count == 0)
730 drawmenu(currmenu);
731 break;
732 case MotionNotify:
733 menu = getmenu(currmenu, ev.xbutton.window);
734 item = getitem(menu, ev.xbutton.y);
735 if (menu == NULL || item == NULL || previtem == item)
736 break;
737 previtem = item;
738 menu->selected = item;
739 if (item->submenu != NULL) {
740 currmenu = item->submenu;
741 currmenu->selected = NULL;
742 } else {
743 currmenu = menu;
744 }
745 mapmenu(currmenu);
746 drawmenu(currmenu);
747 break;
748 case ButtonRelease:
749 menu = getmenu(currmenu, ev.xbutton.window);
750 item = getitem(menu, ev.xbutton.y);
751 if (menu == NULL || item == NULL)
752 break;
753selectitem:
754 if (item->label == NULL)
755 break; /* ignore separators */
756 if (item->submenu != NULL) {
757 currmenu = item->submenu;
758 } else {
759 printf("%s\n", item->output);
760 return;
761 }
762 mapmenu(currmenu);
763 currmenu->selected = currmenu->list;
764 drawmenu(currmenu);
765 break;
766 case ButtonPress:
767 menu = getmenu(currmenu, ev.xbutton.window);
768 if (menu == NULL)
769 return;
770 break;
771 case KeyPress:
772 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
773
774 /* esc closes xmenu when current menu is the root menu */
775 if (ksym == XK_Escape && currmenu->parent == NULL)
776 return;
777
778 /* Shift-Tab = ISO_Left_Tab */
779 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
780 ksym = XK_ISO_Left_Tab;
781
782 /* cycle through menu */
783 item = NULL;
784 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
785 item = itemcycle(currmenu, ITEMPREV);
786 } else if (ksym == XK_Tab || ksym == XK_Down) {
787 item = itemcycle(currmenu, ITEMNEXT);
788 } else if ((ksym == XK_Return || ksym == XK_Right) &&
789 currmenu->selected != NULL) {
790 item = currmenu->selected;
791 goto selectitem;
792 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
793 currmenu->parent != NULL) {
794 item = currmenu->parent->selected;
795 currmenu = currmenu->parent;
796 mapmenu(currmenu);
797 } else
798 break;
799 currmenu->selected = item;
800 drawmenu(currmenu);
801 break;
802 case LeaveNotify:
803 previtem = NULL;
804 currmenu->selected = NULL;
805 drawmenu(currmenu);
806 break;
807 }
808 }
809}
810
811/* recursivelly free pixmaps and destroy windows */
812static void
813freemenu(struct Menu *menu)
814{
815 struct Item *item;
816 struct Item *tmp;
817
818 item = menu->list;
819 while (item != NULL) {
820 if (item->submenu != NULL)
821 freemenu(item->submenu);
822 tmp = item;
823 item = item->next;
824 free(tmp);
825 }
826
827 XFreePixmap(dpy, menu->pixmap);
828 XftDrawDestroy(menu->draw);
829 XDestroyWindow(dpy, menu->win);
830 free(menu);
831}
832
833/* cleanup and exit */
834static void
835cleanup(struct Menu *rootmenu)
836{
837 XUngrabPointer(dpy, CurrentTime);
838 XUngrabKeyboard(dpy, CurrentTime);
839
840 freemenu(rootmenu);
841
842 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
843 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
844 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
845 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
846 XftColorFree(dpy, visual, colormap, &dc.separator);
847 XftColorFree(dpy, visual, colormap, &dc.border);
848
849 XFreeGC(dpy, dc.gc);
850 XCloseDisplay(dpy);
851}
852
853/* show usage */
854static void
855usage(void)
856{
857 (void)fprintf(stderr, "usage: xmenu\n");
858 exit(1);
859}