Improved README
[xmenu] / xmenu.c
CommitLineData
a7732690 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>
f644b8bc 8#include <X11/Xresource.h>
858338d9 9#include <X11/XKBlib.h>
dbeb9940 10#include <X11/Xft/Xft.h>
858338d9 11
d2435fcd 12#define PROGNAME "xmenu"
858338d9 13#define ITEMPREV 0
14#define ITEMNEXT 1
a7732690 15
16/* macros */
17#define LEN(x) (sizeof (x) / sizeof (x[0]))
f1583285 18#define MAX(x,y) ((x)>(y)?(x):(y))
d8a7caf2 19#define MIN(x,y) ((x)<(y)?(x):(y))
a7732690 20
21/* color enum */
22enum {ColorFG, ColorBG, ColorLast};
23
24/* draw context structure */
25struct DC {
dbeb9940 26 XftColor normal[ColorLast];
27 XftColor selected[ColorLast];
fd530f3f 28 XftColor border;
29 XftColor separator;
a7732690 30
31 Drawable d;
32 GC gc;
dbeb9940 33 XftFont *font;
a7732690 34};
35
36/* menu geometry structure */
fd530f3f 37struct MenuGeometry {
a7732690 38 int itemb; /* item border */
39 int itemw; /* item width */
40 int itemh; /* item height */
a80fee22 41 int border; /* window border width */
42 int separator; /* menu separator width */
a7732690 43};
44
45/* screen geometry structure */
46struct ScreenGeometry {
47 int cursx, cursy; /* cursor position */
48 int screenw, screenh; /* screen width and height */
49};
50
51/* menu item structure */
52struct Item {
d8a7caf2 53 char *label; /* string to be drawed on menu */
54 char *output; /* string to be outputed when item is clicked */
55 int y; /* item y position relative to menu */
56 int h; /* item height */
57 size_t labellen; /* strlen(label) */
858338d9 58 struct Item *prev; /* previous item */
d8a7caf2 59 struct Item *next; /* next item */
60 struct Menu *submenu; /* submenu spawned by clicking on item */
a7732690 61};
62
63/* menu structure */
64struct Menu {
d8a7caf2 65 struct Menu *parent; /* parent menu */
66 struct Item *caller; /* item that spawned the menu */
67 struct Item *list; /* list of items contained by the menu */
68 struct Item *selected; /* item currently selected in the menu */
69 int x, y, w, h; /* menu geometry */
70 unsigned level; /* menu level relative to root */
71 Drawable pixmap; /* pixmap to draw the menu on */
dbeb9940 72 XftDraw *draw;
d8a7caf2 73 Window win; /* menu window to map on the screen */
a7732690 74};
75
76/* function declarations */
dbeb9940 77static void getcolor(const char *s, XftColor *color);
f644b8bc 78static void getresources(void);
a7732690 79static void setupdc(void);
80static void setupgeom(void);
a80fee22 81static struct Item *allocitem(const char *label, const char *output);
a7732690 82static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
0c1a7864 83static struct Menu *getmenu(Window win);
84static struct Item *getitem(struct Menu *menu, int y);
fd530f3f 85static void drawseparator(struct Menu *menu, struct Item *item);
86static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
a7732690 87static void drawmenu(void);
88static void calcscreengeom(void);
89static void calcmenu(struct Menu *menu);
85003546 90static void grabpointer(void);
91static void grabkeyboard(void);
a7732690 92static void setcurrmenu(struct Menu *currmenu_new);
93static void parsestdin(void);
94static void run(void);
f15fc339 95static void freewindow(struct Menu *menu);
08f16589 96static void cleanup(void);
a7732690 97static void usage(void);
98
99/* X variables */
100static Colormap colormap;
101static Display *dpy;
dbeb9940 102static Visual *visual;
a7732690 103static Window rootwin;
104static int screen;
105static struct DC dc;
fd530f3f 106static struct ScreenGeometry screengeom;
a7732690 107
108/* menu variables */
109static struct Menu *rootmenu = NULL;
110static struct Menu *currmenu = NULL;
fd530f3f 111static struct MenuGeometry geom;
a7732690 112
a7732690 113#include "config.h"
114
115int
116main(int argc, char *argv[])
117{
118 int ch;
119
27443900 120 while ((ch = getopt(argc, argv, "")) != -1) {
a7732690 121 switch (ch) {
a7732690 122 default:
123 usage();
124 break;
125 }
126 }
127 argc -= optind;
128 argv += optind;
129
130 /* open connection to server and set X variables */
131 if ((dpy = XOpenDisplay(NULL)) == NULL)
132 errx(1, "cannot open display");
133 screen = DefaultScreen(dpy);
dbeb9940 134 visual = DefaultVisual(dpy, screen);
a7732690 135 rootwin = RootWindow(dpy, screen);
136 colormap = DefaultColormap(dpy, screen);
21a9aaec 137
a7732690 138 /* setup */
f644b8bc 139 getresources();
a7732690 140 setupdc();
141 setupgeom();
a7732690 142
143 /* generate menus and recalculate them */
144 parsestdin();
145 if (rootmenu == NULL)
146 errx(1, "no menu generated");
147 calcscreengeom();
148 calcmenu(rootmenu);
149
465d07db 150 /* grab mouse and keyboard */
27443900 151 grabpointer();
152 grabkeyboard();
465d07db 153
a7732690 154 /* run event loop */
155 run();
156
08f16589 157 cleanup();
21a9aaec 158 return 0;
159}
160
f644b8bc 161/* read xrdb for configuration options */
162static void
163getresources(void)
164{
165 char *xrm;
166 long n;
fd530f3f 167 char *type;
168 XrmDatabase xdb;
169 XrmValue xval;
f644b8bc 170
171 XrmInitialize();
fd530f3f 172 if ((xrm = XResourceManagerString(dpy)) == NULL)
173 return;
174
175 xdb = XrmGetStringDatabase(xrm);
176
177 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
178 if ((n = strtol(xval.addr, NULL, 10)) > 0)
179 border_pixels = n;
180 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
181 if ((n = strtol(xval.addr, NULL, 10)) > 0)
182 separator_pixels = n;
183 if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True)
184 if ((n = strtol(xval.addr, NULL, 10)) > 0)
185 padding_pixels = n;
186 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
187 if ((n = strtol(xval.addr, NULL, 10)) > 0)
188 width_pixels = n;
189 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
190 background_color = strdup(xval.addr);
191 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
192 foreground_color = strdup(xval.addr);
193 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
194 selbackground_color = strdup(xval.addr);
195 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
196 selforeground_color = strdup(xval.addr);
197 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
198 separator_color = strdup(xval.addr);
199 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
200 border_color = strdup(xval.addr);
201 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
202 font = strdup(xval.addr);
203
204 XrmDestroyDatabase(xdb);
f644b8bc 205}
206
a7732690 207/* get color from color string */
dbeb9940 208static void
209getcolor(const char *s, XftColor *color)
a7732690 210{
dbeb9940 211 if(!XftColorAllocName(dpy, visual, colormap, s, color))
a7732690 212 errx(1, "cannot allocate color: %s", s);
a7732690 213}
214
215/* init draw context */
216static void
217setupdc(void)
218{
219 /* get color pixels */
fd530f3f 220 getcolor(background_color, &dc.normal[ColorBG]);
221 getcolor(foreground_color, &dc.normal[ColorFG]);
222 getcolor(selbackground_color, &dc.selected[ColorBG]);
223 getcolor(selforeground_color, &dc.selected[ColorFG]);
224 getcolor(separator_color, &dc.separator);
225 getcolor(border_color, &dc.border);
a7732690 226
227 /* try to get font */
dbeb9940 228 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
a7732690 229 errx(1, "cannot load font");
a7732690 230
fd530f3f 231 /* create common GC */
a7732690 232 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 233}
234
235/* init menu geometry values */
236static void
237setupgeom(void)
238{
fd530f3f 239 geom.itemb = padding_pixels;
240 geom.itemh = dc.font->height + padding_pixels * 2;
241 geom.itemw = width_pixels;
242 geom.border = border_pixels;
243 geom.separator = separator_pixels;
a7732690 244}
245
a7732690 246/* allocate an item */
247static struct Item *
a80fee22 248allocitem(const char *label, const char *output)
a7732690 249{
250 struct Item *item;
251
252 if ((item = malloc(sizeof *item)) == NULL)
253 err(1, "malloc");
7fbd1c5e 254 if (*label == '\0') {
255 item->label = NULL;
256 item->output = NULL;
257 } else {
258 if ((item->label = strdup(label)) == NULL)
259 err(1, "strdup");
260 if ((item->output = strdup(output)) == NULL)
261 err(1, "strdup");
262 }
a80fee22 263 item->y = 0;
7fbd1c5e 264 item->h = item->label ? geom.itemh : geom.separator;
f1583285 265 if (item->label == NULL)
266 item->labellen = 0;
267 else
268 item->labellen = strlen(item->label);
a7732690 269 item->next = NULL;
270 item->submenu = NULL;
271
272 return item;
273}
274
275/* allocate a menu */
276static struct Menu *
277allocmenu(struct Menu *parent, struct Item *list, unsigned level)
278{
279 XSetWindowAttributes swa;
280 struct Menu *menu;
281
282 if ((menu = malloc(sizeof *menu)) == NULL)
283 err(1, "malloc");
284 menu->parent = parent;
285 menu->list = list;
d888f2ca 286 menu->caller = NULL;
a7732690 287 menu->selected = NULL;
a7732690 288 menu->w = geom.itemw;
a80fee22 289 menu->h = 0; /* calculated by calcmenu() */
290 menu->x = 0; /* calculated by calcmenu() */
291 menu->y = 0; /* calculated by calcmenu() */
a7732690 292 menu->level = level;
a7732690 293
27443900 294 swa.override_redirect = True;
fd530f3f 295 swa.background_pixel = dc.normal[ColorBG].pixel;
296 swa.border_pixel = dc.border.pixel;
297 swa.save_under = True; /* pop-up windows should save_under*/
a7732690 298 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 299 | PointerMotionMask | LeaveWindowMask;
a7732690 300 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
301 CopyFromParent, CopyFromParent, CopyFromParent,
fd530f3f 302 CWOverrideRedirect | CWBackPixel |
303 CWBorderPixel | CWEventMask | CWSaveUnder,
a7732690 304 &swa);
305
306 return menu;
307}
308
309/* create menus and items from the stdin */
310static void
311parsestdin(void)
312{
313 char *s, buf[BUFSIZ];
314 char *label, *output;
315 unsigned level = 0;
316 unsigned i;
a80fee22 317 struct Item *curritem = NULL; /* item currently being read */
318 struct Menu *prevmenu = NULL; /* menu the previous item was added to */
319 struct Item *item; /* dummy item for for loops */
320 struct Menu *menu; /* dummy menu for for loops */
a7732690 321 size_t count = 0; /* number of items in the current menu */
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
a80fee22 349 curritem = allocitem(label, output);
a7732690 350
351 if (prevmenu == NULL) { /* there is no menu yet */
a80fee22 352 menu = allocmenu(NULL, curritem, level);
a7732690 353 rootmenu = menu;
354 prevmenu = menu;
355 count = 1;
858338d9 356 curritem->prev = NULL;
357 curritem->next = NULL;
a7732690 358 } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
359 for (menu = prevmenu, i = level;
360 menu != NULL && i < prevmenu->level;
361 menu = menu->parent, i++)
362 ;
363
364 if (menu == NULL)
365 errx(1, "reached NULL menu");
366
a80fee22 367 for (item = menu->list; item->next != NULL; item = item->next)
a7732690 368 ;
369
a80fee22 370 item->next = curritem;
858338d9 371
372 curritem->prev = item;
373 curritem->next = NULL;
374
a7732690 375 prevmenu = menu;
376 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
a80fee22 377 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 378 ;
a80fee22 379 item->next = curritem;
858338d9 380
381 curritem->prev = item;
382 curritem->next = NULL;
383
a7732690 384 } else if (level > prevmenu->level) { /* item begins a new menu */
a80fee22 385 menu = allocmenu(prevmenu, curritem, level);
a7732690 386
a80fee22 387 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 388 ;
389
a80fee22 390 item->submenu = menu;
d888f2ca 391 menu->caller = item;
a7732690 392
858338d9 393 curritem->prev = NULL;
394 curritem->next = NULL;
395
a7732690 396 prevmenu = menu;
397 }
a80fee22 398 count++;
a7732690 399 }
400}
401
402/* calculate screen geometry */
403static void
404calcscreengeom(void)
405{
406 Window w1, w2; /* unused variables */
407 int a, b; /* unused variables */
408 unsigned mask; /* unused variable */
409
a80fee22 410 XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
411 screengeom.screenw = DisplayWidth(dpy, screen);
412 screengeom.screenh = DisplayHeight(dpy, screen);
a7732690 413}
414
21a9aaec 415/* recursivelly calculate menu geometry and set window hints */
a7732690 416static void
417calcmenu(struct Menu *menu)
418{
d2435fcd 419 static XClassHint classh = {PROGNAME, PROGNAME};
a7732690 420 XWindowChanges changes;
09c13122 421 XSizeHints sizeh;
dbeb9940 422 XGlyphInfo ext;
a80fee22 423 struct Item *item;
f1583285 424 int labelwidth;
a7732690 425
f1583285 426 /* calculate items positions and menu width and height */
427 menu->w = geom.itemw;
a80fee22 428 for (item = menu->list; item != NULL; item = item->next) {
429 item->y = menu->h;
7fbd1c5e 430 if (item->label == NULL) /* height for separator item */
a80fee22 431 menu->h += geom.separator;
432 else
433 menu->h += geom.itemh;
f1583285 434
dbeb9940 435 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
436 item->labellen, &ext);
437 labelwidth = ext.xOff + dc.font->height * 2;
f1583285 438 menu->w = MAX(menu->w, labelwidth);
a80fee22 439 }
a7732690 440
441 /* calculate menu's x and y positions */
442 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
a80fee22 443 if (screengeom.screenw - screengeom.cursx >= menu->w)
444 menu->x = screengeom.cursx;
445 else if (screengeom.cursx > menu->w)
446 menu->x = screengeom.cursx - menu->w;
447
448 if (screengeom.screenh - screengeom.cursy >= menu->h)
449 menu->y = screengeom.cursy;
450 else if (screengeom.screenh > menu->h)
451 menu->y = screengeom.screenh - menu->h;
a7732690 452 } else { /* else, calculate in respect to parent menu */
f644b8bc 453 if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w)
454 menu->x = menu->parent->x + menu->parent->w + geom.border;
455 else if (menu->parent->x > menu->w + geom.border)
456 menu->x = menu->parent->x - menu->w - geom.border;
a7732690 457
8455c369 458 if (screengeom.screenh - (menu->caller->y + menu->parent->y) > menu->h)
459 menu->y = menu->caller->y + menu->parent->y;
a80fee22 460 else if (screengeom.screenh - menu->parent->y > menu->h)
a7732690 461 menu->y = menu->parent->y;
a80fee22 462 else if (screengeom.screenh > menu->h)
463 menu->y = screengeom.screenh - menu->h;
a7732690 464 }
465
466 /* update menu geometry */
467 changes.height = menu->h;
f1583285 468 changes.width = menu->w;
a7732690 469 changes.x = menu->x;
470 changes.y = menu->y;
f1583285 471 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 472
d2435fcd 473 /* set window manager hints */
09c13122 474 sizeh.flags = PMaxSize | PMinSize;
475 sizeh.min_width = sizeh.max_width = menu->w;
476 sizeh.min_height = sizeh.max_height = menu->h;
0c1a7864 477 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
d2435fcd 478 NULL, &classh);
09c13122 479
dbeb9940 480 /* create pixmap and XftDraw */
f15fc339 481 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
482 DefaultDepth(dpy, screen));
dbeb9940 483 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 484
a80fee22 485 /* calculate positions of submenus */
a7732690 486 for (item = menu->list; item != NULL; item = item->next) {
487 if (item->submenu != NULL)
488 calcmenu(item->submenu);
489 }
490}
491
85003546 492/* try to grab pointer, we may have to wait for another process to ungrab */
493static void
494grabpointer(void)
495{
496 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
497 int i;
498
499 for (i = 0; i < 1000; i++) {
500 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
501 GrabModeAsync, GrabModeAsync, None,
502 None, CurrentTime) == GrabSuccess)
503 return;
504 nanosleep(&ts, NULL);
505 }
506 errx(1, "cannot grab keyboard");
507}
508
509/* try to grab keyboard, we may have to wait for another process to ungrab */
510static void
511grabkeyboard(void)
512{
513 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
514 int i;
515
516 for (i = 0; i < 1000; i++) {
517 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
518 GrabModeAsync, CurrentTime) == GrabSuccess)
519 return;
520 nanosleep(&ts, NULL);
521 }
522 errx(1, "cannot grab keyboard");
523}
524
0c1a7864 525/* get menu of given window */
526static struct Menu *
527getmenu(Window win)
a7732690 528{
0c1a7864 529 struct Menu *menu;
a7732690 530
0c1a7864 531 for (menu = currmenu; menu != NULL; menu = menu->parent)
532 if (menu->win == win)
533 return menu;
a7732690 534
0c1a7864 535 return NULL;
536}
537
538/* get item of given menu and position */
539static struct Item *
540getitem(struct Menu *menu, int y)
541{
542 struct Item *item;
a7732690 543
0c1a7864 544 if (menu == NULL)
545 return NULL;
546
547 for (item = menu->list; item != NULL; item = item->next)
548 if (y >= item->y && y <= item->y + item->h)
549 return item;
550
551 return NULL;
a7732690 552}
553
554/* set currentmenu to menu, umap previous menus and map current menu and its parents */
555static void
556setcurrmenu(struct Menu *currmenu_new)
557{
d8a7caf2 558 struct Menu *menu, *menu_;
d888f2ca 559 struct Item *item;
d8a7caf2 560 struct Menu *lcamenu; /* lowest common ancestor menu */
561 unsigned minlevel; /* level of the closest to root menu */
562 unsigned maxlevel; /* level of the closest to root menu */
a7732690 563
27443900 564 /* do not update currmenu to itself */
a7732690 565 if (currmenu_new == currmenu)
566 return;
567
27443900 568 /* if there was no currmenu, skip calculations */
569 if (currmenu == NULL) {
570 currmenu = currmenu_new;
fd530f3f 571 XMapWindow(dpy, currmenu->win);
27443900 572 return;
573 }
574
d8a7caf2 575 /* find lowest common ancestor menu */
576 lcamenu = rootmenu;
27443900 577 minlevel = MIN(currmenu_new->level, currmenu->level);
578 maxlevel = MAX(currmenu_new->level, currmenu->level);
579 if (currmenu_new->level == maxlevel) {
580 menu = currmenu_new;
581 menu_ = currmenu;
582 } else {
583 menu = currmenu;
584 menu_ = currmenu_new;
585 }
586 while (menu->level > minlevel)
587 menu = menu->parent;
588 while (menu != menu_) {
589 menu = menu->parent;
590 menu_ = menu_->parent;
d8a7caf2 591 }
27443900 592 lcamenu = menu;
d8a7caf2 593
873c080c 594 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 595 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
8455c369 596 menu->selected = NULL;
a7732690 597 XUnmapWindow(dpy, menu->win);
598 }
599
600 currmenu = currmenu_new;
601
873c080c 602 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d888f2ca 603 item = NULL;
d8a7caf2 604 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
a7732690 605 XMapWindow(dpy, menu->win);
fd530f3f 606 }
607}
608
609/* draw separator item */
610static void
611drawseparator(struct Menu *menu, struct Item *item)
612{
613 int linex, liney, linew;
614
615 linex = dc.font->height;
616 liney = item->y + item->h/2;
617 linew = menu->w - dc.font->height;
618
619 XSetForeground(dpy, dc.gc, dc.separator.pixel);
620 XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
621}
622
623/* draw regular item */
624static void
625drawitem(struct Menu *menu, struct Item *item, XftColor *color)
626{
627 int x, y;
628
629 x = 0 + dc.font->height;
630 y = item->y + dc.font->height + geom.itemb / 2;
631 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
632 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
633 x, y, item->label, item->labellen);
634
635 /* draw triangle, if item contains a submenu */
636 if (item->submenu != NULL) {
637 x = menu->w - dc.font->height + geom.itemb - 1;
638 y = item->y + geom.itemh/2 - triangle_height/2 - 1;
639
640 XPoint triangle[] = {
641 {x, y},
642 {x + triangle_width, y + triangle_height/2},
643 {x, y + triangle_height},
644 {x, y}
645 };
646
647 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
648 Convex, CoordModeOrigin);
d888f2ca 649 }
a7732690 650}
651
652/* draw items of the current menu and of its ancestors */
653static void
654drawmenu(void)
655{
656 struct Menu *menu;
657 struct Item *item;
658
659 for (menu = currmenu; menu != NULL; menu = menu->parent) {
a7732690 660 for (item = menu->list; item != NULL; item = item->next) {
dbeb9940 661 XftColor *color;
a7732690 662
663 /* determine item color */
fd530f3f 664 if (item == menu->selected && item->label != NULL)
f644b8bc 665 color = dc.selected;
a7732690 666 else
f644b8bc 667 color = dc.normal;
668
a7732690 669 /* draw item box */
dbeb9940 670 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
f15fc339 671 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
f1583285 672 menu->w, item->h);
7fbd1c5e 673
fd530f3f 674 if (item->label == NULL) /* item is a separator */
675 drawseparator(menu, item);
676 else /* item is a regular item */
677 drawitem(menu, item, color);
a7732690 678 }
fd530f3f 679
680 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
681 menu->w, menu->h, 0, 0);
a7732690 682 }
683}
684
858338d9 685/* cycle through the items; non-zero direction is next, zero is prev */
686static struct Item *
687itemcycle(int direction)
688{
689 struct Item *item;
690 struct Item *lastitem;
691
692 item = NULL;
693
694 if (direction == ITEMNEXT) {
695 if (currmenu->selected == NULL)
696 item = currmenu->list;
697 else if (currmenu->selected->next != NULL)
698 item = currmenu->selected->next;
699
700 while (item != NULL && item->label == NULL)
701 item = item->next;
702
703 if (item == NULL)
704 item = currmenu->list;
705 } else {
706 for (lastitem = currmenu->list;
707 lastitem != NULL && lastitem->next != NULL;
708 lastitem = lastitem->next)
709 ;
710
711 if (currmenu->selected == NULL)
712 item = lastitem;
713 else if (currmenu->selected->prev != NULL)
714 item = currmenu->selected->prev;
715
716 while (item != NULL && item->label == NULL)
717 item = item->prev;
718
719 if (item == NULL)
720 item = lastitem;
721 }
722
723 return item;
724}
725
a7732690 726/* run event loop */
727static void
728run(void)
729{
730 struct Menu *menu;
731 struct Item *item;
732 struct Item *previtem = NULL;
858338d9 733 KeySym ksym;
a7732690 734 XEvent ev;
735
fd530f3f 736 setcurrmenu(rootmenu);
737
a7732690 738 while (!XNextEvent(dpy, &ev)) {
739 switch(ev.type) {
740 case Expose:
858338d9 741 if (ev.xexpose.count == 0)
742 drawmenu();
a7732690 743 break;
744 case MotionNotify:
0c1a7864 745 menu = getmenu(ev.xbutton.window);
746 item = getitem(menu, ev.xbutton.y);
fd530f3f 747 if (menu == NULL || item == NULL)
748 break;
749 if (previtem != item) {
750 menu->selected = item;
751 if (item->submenu != NULL)
752 setcurrmenu(item->submenu);
753 else
754 setcurrmenu(menu);
755 previtem = item;
756 drawmenu();
a7732690 757 }
a7732690 758 break;
759 case ButtonRelease:
0c1a7864 760 menu = getmenu(ev.xbutton.window);
761 item = getitem(menu, ev.xbutton.y);
fd530f3f 762 if (menu == NULL || item == NULL)
858338d9 763 break;
fd530f3f 764selectitem:
765 if (item->label == NULL)
766 break; /* ignore separators */
767 if (item->submenu != NULL) {
768 setcurrmenu(item->submenu);
a7732690 769 } else {
fd530f3f 770 printf("%s\n", item->output);
08f16589 771 return;
a7732690 772 }
fd530f3f 773 currmenu->selected = currmenu->list;
774 drawmenu();
775 break;
858338d9 776 case ButtonPress:
0c1a7864 777 menu = getmenu(ev.xbutton.window);
778 if (menu == NULL)
858338d9 779 return;
780 break;
781 case KeyPress:
782 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
783
0c1a7864 784 /* esc closes xmenu when current menu is the root menu */
858338d9 785 if (ksym == XK_Escape && currmenu == rootmenu)
786 return;
787
788 /* Shift-Tab = ISO_Left_Tab */
789 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
790 ksym = XK_ISO_Left_Tab;
791
792 /* cycle through menu */
793 item = NULL;
794 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
795 item = itemcycle(ITEMPREV);
796 } else if (ksym == XK_Tab || ksym == XK_Down) {
797 item = itemcycle(ITEMNEXT);
798 } else if ((ksym == XK_Return || ksym == XK_Right) &&
799 currmenu->selected != NULL) {
800 item = currmenu->selected;
801 goto selectitem;
802 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
803 currmenu->parent != NULL) {
804 item = currmenu->parent->selected;
805 setcurrmenu(currmenu->parent);
806 } else
807 break;
808 currmenu->selected = item;
809 drawmenu();
a7732690 810 break;
f15fc339 811 case LeaveNotify:
fd530f3f 812 previtem = NULL;
f15fc339 813 currmenu->selected = NULL;
814 drawmenu();
815 break;
a7732690 816 }
817 }
818}
819
fd530f3f 820/* recursivelly free pixmaps and destroy windows */
f15fc339 821static void
822freewindow(struct Menu *menu)
823{
824 struct Item *item;
825
826 for (item = menu->list; item != NULL; item = item->next)
827 if (item->submenu != NULL)
828 freewindow(item->submenu);
829
830 XFreePixmap(dpy, menu->pixmap);
dbeb9940 831 XftDrawDestroy(menu->draw);
f15fc339 832 XDestroyWindow(dpy, menu->win);
833}
834
a7732690 835/* cleanup and exit */
836static void
08f16589 837cleanup(void)
a7732690 838{
f15fc339 839 freewindow(rootmenu);
dbeb9940 840
841 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
842 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
843 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
844 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 845 XftColorFree(dpy, visual, colormap, &dc.separator);
846 XftColorFree(dpy, visual, colormap, &dc.border);
dbeb9940 847
f15fc339 848 XFreeGC(dpy, dc.gc);
a7732690 849 XCloseDisplay(dpy);
a7732690 850}
851
852/* show usage */
853static void
854usage(void)
855{
27443900 856 (void)fprintf(stderr, "usage: xmenu title...\n");
a7732690 857 exit(1);
858}