Updating README
[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#define IMGPADDING 8
17
18/* macros */
19#define LEN(x) (sizeof (x) / sizeof (x[0]))
20#define MAX(x,y) ((x)>(y)?(x):(y))
21#define MIN(x,y) ((x)<(y)?(x):(y))
22
23/* color enum */
24enum {ColorFG, ColorBG, ColorLast};
25
26/* draw context structure */
27struct DC {
28 XftColor normal[ColorLast];
29 XftColor selected[ColorLast];
30 XftColor border;
31 XftColor separator;
32
33 GC gc;
34 XftFont *font;
35};
36
37/* menu geometry structure */
38struct Geometry {
39 int border; /* window border width */
40 int separator; /* menu separator width */
41 int itemw, itemh; /* item width and height */
42 int cursx, cursy; /* cursor position */
43 int screenw, screenh; /* screen width and height */
44};
45
46/* menu item structure */
47struct Item {
48 char *label; /* string to be drawed on menu */
49 char *output; /* string to be outputed when item is clicked */
50 char *file; /* filename of the icon */
51 int y; /* item y position relative to menu */
52 int h; /* item height */
53 size_t labellen; /* strlen(label) */
54 struct Item *prev; /* previous item */
55 struct Item *next; /* next item */
56 struct Menu *submenu; /* submenu spawned by clicking on item */
57 Imlib_Image icon;
58};
59
60/* menu structure */
61struct Menu {
62 struct Menu *parent; /* parent menu */
63 struct Item *caller; /* item that spawned the menu */
64 struct Item *list; /* list of items contained by the menu */
65 struct Item *selected; /* item currently selected in the menu */
66 int x, y, w, h; /* menu geometry */
67 unsigned level; /* menu level relative to root */
68 Drawable pixmap; /* pixmap to draw the menu on */
69 XftDraw *draw;
70 Window win; /* menu window to map on the screen */
71};
72
73/* functions declarations */
74static void getresources(void);
75static void getcolor(const char *s, XftColor *color);
76static void setupdc(void);
77static void calcgeom(struct Geometry *geom);
78static struct Item *allocitem(const char *label, const char *output, char *file);
79static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
80static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file);
81static struct Menu *parsestdin(void);
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.padding", "*", &type, &xval) == True)
211 if ((n = strtol(xval.addr, NULL, 10)) > 0)
212 padding_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 = dc.font->height + padding_pixels * 2;
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 labelwidth = ext.xOff + dc.font->height * 2 + IMGPADDING * 2;
503 menu->w = MAX(menu->w, labelwidth);
504
505 /* create icon */
506 if (item->file != NULL)
507 item->icon = loadicon(item->file, dc.font->height);
508 }
509}
510
511/* setup the position of a menu */
512static void
513setupmenupos(struct Geometry *g, struct Menu *menu)
514{
515 static struct Geometry *geom = NULL;
516 int width, height;
517
518 /*
519 * Save the geometry so functions can call setupmenupos() without
520 * having to know the geometry.
521 */
522 if (g != NULL)
523 geom = g;
524
525 width = menu->w + geom->border * 2;
526 height = menu->h + geom->border * 2;
527 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
528 if (geom->screenw - geom->cursx >= menu->w)
529 menu->x = geom->cursx;
530 else if (geom->cursx > width)
531 menu->x = geom->cursx - width;
532
533 if (geom->screenh - geom->cursy >= height)
534 menu->y = geom->cursy;
535 else if (geom->screenh > height)
536 menu->y = geom->screenh - height;
537 } else { /* else, calculate in respect to parent menu */
538 if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width)
539 menu->x = menu->parent->x + menu->parent->w + geom->border;
540 else if (menu->parent->x > menu->w + geom->border)
541 menu->x = menu->parent->x - menu->w - geom->border;
542
543 if (geom->screenh - (menu->caller->y + menu->parent->y) > height)
544 menu->y = menu->caller->y + menu->parent->y;
545 else if (geom->screenh - menu->parent->y > height)
546 menu->y = menu->parent->y;
547 else if (geom->screenh > height)
548 menu->y = geom->screenh - height;
549 }
550}
551
552/* recursivelly setup menu configuration and its pixmap */
553static void
554setupmenu(struct Geometry *geom, struct Menu *menu, XClassHint *classh)
555{
556 struct Item *item;
557 XWindowChanges changes;
558 XSizeHints sizeh;
559 XTextProperty wintitle;
560
561 /* setup size and position of menus */
562 setupmenusize(geom, menu);
563 setupmenupos(geom, menu);
564
565 /* update menu geometry */
566 changes.border_width = geom->border;
567 changes.height = menu->h;
568 changes.width = menu->w;
569 changes.x = menu->x;
570 changes.y = menu->y;
571 XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
572
573 /* set window title (used if wflag is on) */
574 if (menu->parent == NULL) {
575 XStringListToTextProperty(&classh->res_name, 1, &wintitle);
576 } else {
577 XStringListToTextProperty(&menu->caller->output, 1, &wintitle);
578 }
579
580 /* set window manager hints */
581 sizeh.flags = PMaxSize | PMinSize;
582 sizeh.min_width = sizeh.max_width = menu->w;
583 sizeh.min_height = sizeh.max_height = menu->h;
584 XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh);
585
586 /* create pixmap and XftDraw */
587 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
588 DefaultDepth(dpy, screen));
589 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
590
591 /* calculate positions of submenus */
592 for (item = menu->list; item != NULL; item = item->next) {
593 if (item->submenu != NULL)
594 setupmenu(geom, item->submenu, classh);
595 }
596}
597
598/* try to grab pointer, we may have to wait for another process to ungrab */
599static void
600grabpointer(void)
601{
602 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
603 int i;
604
605 for (i = 0; i < 1000; i++) {
606 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
607 GrabModeAsync, GrabModeAsync, None,
608 None, CurrentTime) == GrabSuccess)
609 return;
610 nanosleep(&ts, NULL);
611 }
612 errx(1, "cannot grab keyboard");
613}
614
615/* try to grab keyboard, we may have to wait for another process to ungrab */
616static void
617grabkeyboard(void)
618{
619 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
620 int i;
621
622 for (i = 0; i < 1000; i++) {
623 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
624 GrabModeAsync, CurrentTime) == GrabSuccess)
625 return;
626 nanosleep(&ts, NULL);
627 }
628 errx(1, "cannot grab keyboard");
629}
630
631/* get menu of given window */
632static struct Menu *
633getmenu(struct Menu *currmenu, Window win)
634{
635 struct Menu *menu;
636
637 for (menu = currmenu; menu != NULL; menu = menu->parent)
638 if (menu->win == win)
639 return menu;
640
641 return NULL;
642}
643
644/* get item of given menu and position */
645static struct Item *
646getitem(struct Menu *menu, int y)
647{
648 struct Item *item;
649
650 if (menu == NULL)
651 return NULL;
652
653 for (item = menu->list; item != NULL; item = item->next)
654 if (y >= item->y && y <= item->y + item->h)
655 return item;
656
657 return NULL;
658}
659
660/* umap previous menus and map current menu and its parents */
661static void
662mapmenu(struct Menu *currmenu)
663{
664 static struct Menu *prevmenu = NULL;
665 struct Menu *menu, *menu_;
666 struct Menu *lcamenu; /* lowest common ancestor menu */
667 unsigned minlevel; /* level of the closest to root menu */
668 unsigned maxlevel; /* level of the closest to root menu */
669
670 /* do not remap current menu if it wasn't updated*/
671 if (prevmenu == currmenu)
672 return;
673
674 /* if this is the first time mapping, skip calculations */
675 if (prevmenu == NULL) {
676 XMapWindow(dpy, currmenu->win);
677 prevmenu = currmenu;
678 return;
679 }
680
681 /* find lowest common ancestor menu */
682 minlevel = MIN(currmenu->level, prevmenu->level);
683 maxlevel = MAX(currmenu->level, prevmenu->level);
684 if (currmenu->level == maxlevel) {
685 menu = currmenu;
686 menu_ = prevmenu;
687 } else {
688 menu = prevmenu;
689 menu_ = currmenu;
690 }
691 while (menu->level > minlevel)
692 menu = menu->parent;
693 while (menu != menu_) {
694 menu = menu->parent;
695 menu_ = menu_->parent;
696 }
697 lcamenu = menu;
698
699 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
700 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
701 menu->selected = NULL;
702 XUnmapWindow(dpy, menu->win);
703 }
704
705 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
706 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
707
708 if (wflag) {
709 setupmenupos(NULL, menu);
710 XMoveWindow(dpy, menu->win, menu->x, menu->y);
711 }
712
713 XMapWindow(dpy, menu->win);
714 }
715
716 prevmenu = currmenu;
717}
718
719/* draw separator item */
720static void
721drawseparator(struct Menu *menu, struct Item *item)
722{
723 int y;
724
725 y = item->y + item->h/2;
726
727 XSetForeground(dpy, dc.gc, dc.separator.pixel);
728 XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
729}
730
731/* draw regular item */
732static void
733drawitem(struct Menu *menu, struct Item *item, XftColor *color)
734{
735 int x, y;
736
737 x = dc.font->height + IMGPADDING;
738 y = item->y + item->h/2 + dc.font->ascent/2 - 1;
739 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
740 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
741 x, y, item->label, item->labellen);
742
743 /* draw triangle, if item contains a submenu */
744 if (item->submenu != NULL) {
745 x = menu->w - dc.font->height/2 - IMGPADDING/2 - triangle_width/2 - 1;
746 y = item->y + item->h/2 - triangle_height/2 - 1;
747
748 XPoint triangle[] = {
749 {x, y},
750 {x + triangle_width, y + triangle_height/2},
751 {x, y + triangle_height},
752 {x, y}
753 };
754
755 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
756 Convex, CoordModeOrigin);
757 }
758
759 /* draw icon */
760 if (item->file != NULL) {
761 x = IMGPADDING / 2;
762 y = item->y + (item->h - dc.font->height) / 2;
763 imlib_context_set_drawable(menu->pixmap);
764 imlib_context_set_image(item->icon);
765 imlib_render_image_on_drawable(x, y);
766 }
767}
768
769/* draw items of the current menu and of its ancestors */
770static void
771drawmenu(struct Menu *currmenu)
772{
773 struct Menu *menu;
774 struct Item *item;
775
776 for (menu = currmenu; menu != NULL; menu = menu->parent) {
777 for (item = menu->list; item != NULL; item = item->next) {
778 XftColor *color;
779
780 /* determine item color */
781 if (item == menu->selected && item->label != NULL)
782 color = dc.selected;
783 else
784 color = dc.normal;
785
786 /* draw item box */
787 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
788 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
789 menu->w, item->h);
790
791 if (item->label == NULL) /* item is a separator */
792 drawseparator(menu, item);
793 else /* item is a regular item */
794 drawitem(menu, item, color);
795 }
796
797 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
798 menu->w, menu->h, 0, 0);
799 }
800}
801
802/* cycle through the items; non-zero direction is next, zero is prev */
803static struct Item *
804itemcycle(struct Menu *currmenu, int direction)
805{
806 struct Item *item;
807 struct Item *lastitem;
808
809 item = NULL;
810
811 if (direction == ITEMNEXT) {
812 if (currmenu->selected == NULL)
813 item = currmenu->list;
814 else if (currmenu->selected->next != NULL)
815 item = currmenu->selected->next;
816
817 while (item != NULL && item->label == NULL)
818 item = item->next;
819
820 if (item == NULL)
821 item = currmenu->list;
822 } else {
823 for (lastitem = currmenu->list;
824 lastitem != NULL && lastitem->next != NULL;
825 lastitem = lastitem->next)
826 ;
827
828 if (currmenu->selected == NULL)
829 item = lastitem;
830 else if (currmenu->selected->prev != NULL)
831 item = currmenu->selected->prev;
832
833 while (item != NULL && item->label == NULL)
834 item = item->prev;
835
836 if (item == NULL)
837 item = lastitem;
838 }
839
840 return item;
841}
842
843/* run event loop */
844static void
845run(struct Menu *currmenu)
846{
847 struct Menu *menu;
848 struct Item *item;
849 struct Item *previtem = NULL;
850 KeySym ksym;
851 XEvent ev;
852
853 mapmenu(currmenu);
854
855 while (!XNextEvent(dpy, &ev)) {
856 switch(ev.type) {
857 case Expose:
858 if (ev.xexpose.count == 0)
859 drawmenu(currmenu);
860 break;
861 case MotionNotify:
862 menu = getmenu(currmenu, ev.xbutton.window);
863 item = getitem(menu, ev.xbutton.y);
864 if (menu == NULL || item == NULL || previtem == item)
865 break;
866 previtem = item;
867 menu->selected = item;
868 if (item->submenu != NULL) {
869 currmenu = item->submenu;
870 currmenu->selected = NULL;
871 } else {
872 currmenu = menu;
873 }
874 mapmenu(currmenu);
875 drawmenu(currmenu);
876 break;
877 case ButtonRelease:
878 menu = getmenu(currmenu, ev.xbutton.window);
879 item = getitem(menu, ev.xbutton.y);
880 if (menu == NULL || item == NULL)
881 break;
882selectitem:
883 if (item->label == NULL)
884 break; /* ignore separators */
885 if (item->submenu != NULL) {
886 currmenu = item->submenu;
887 } else {
888 printf("%s\n", item->output);
889 return;
890 }
891 mapmenu(currmenu);
892 currmenu->selected = currmenu->list;
893 drawmenu(currmenu);
894 break;
895 case ButtonPress:
896 menu = getmenu(currmenu, ev.xbutton.window);
897 if (menu == NULL)
898 return;
899 break;
900 case KeyPress:
901 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
902
903 /* esc closes xmenu when current menu is the root menu */
904 if (ksym == XK_Escape && currmenu->parent == NULL)
905 return;
906
907 /* Shift-Tab = ISO_Left_Tab */
908 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
909 ksym = XK_ISO_Left_Tab;
910
911 /* cycle through menu */
912 item = NULL;
913 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
914 item = itemcycle(currmenu, ITEMPREV);
915 } else if (ksym == XK_Tab || ksym == XK_Down) {
916 item = itemcycle(currmenu, ITEMNEXT);
917 } else if ((ksym == XK_Return || ksym == XK_Right) &&
918 currmenu->selected != NULL) {
919 item = currmenu->selected;
920 goto selectitem;
921 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
922 currmenu->parent != NULL) {
923 item = currmenu->parent->selected;
924 currmenu = currmenu->parent;
925 mapmenu(currmenu);
926 } else
927 break;
928 currmenu->selected = item;
929 drawmenu(currmenu);
930 break;
931 case LeaveNotify:
932 previtem = NULL;
933 currmenu->selected = NULL;
934 drawmenu(currmenu);
935 break;
936 case ConfigureNotify:
937 menu = getmenu(currmenu, ev.xconfigure.window);
938 if (menu == NULL)
939 break;
940 menu->x = ev.xconfigure.x;
941 menu->y = ev.xconfigure.y;
942 break;
943 case ClientMessage:
944 /* user closed window */
945 menu = getmenu(currmenu, ev.xclient.window);
946 if (menu->parent == NULL)
947 return; /* closing the root menu closes the program */
948 currmenu = menu->parent;
949 mapmenu(currmenu);
950 break;
951 }
952 }
953}
954
955/* recursivelly free pixmaps and destroy windows */
956static void
957freemenu(struct Menu *menu)
958{
959 struct Item *item;
960 struct Item *tmp;
961
962 item = menu->list;
963 while (item != NULL) {
964 if (item->submenu != NULL)
965 freemenu(item->submenu);
966 tmp = item;
967 if (tmp->label != tmp->output)
968 free(tmp->label);
969 free(tmp->output);
970 if (tmp->file != NULL) {
971 free(tmp->file);
972 if (tmp->icon != NULL) {
973 imlib_context_set_image(tmp->icon);
974 imlib_free_image();
975 }
976 }
977 item = item->next;
978 free(tmp);
979 }
980
981 XFreePixmap(dpy, menu->pixmap);
982 XftDrawDestroy(menu->draw);
983 XDestroyWindow(dpy, menu->win);
984 free(menu);
985}
986
987/* cleanup and exit */
988static void
989cleanup(void)
990{
991 XUngrabPointer(dpy, CurrentTime);
992 XUngrabKeyboard(dpy, CurrentTime);
993
994 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
995 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
996 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
997 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
998 XftColorFree(dpy, visual, colormap, &dc.separator);
999 XftColorFree(dpy, visual, colormap, &dc.border);
1000
1001 XFreeGC(dpy, dc.gc);
1002 XCloseDisplay(dpy);
1003}
1004
1005/* show usage */
1006static void
1007usage(void)
1008{
1009 (void)fprintf(stderr, "usage: xmenu [-w] [title]\n");
1010 exit(1);
1011}