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