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