Simplifying parsing and data structure building
[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
a7732690 31 GC gc;
dbeb9940 32 XftFont *font;
a7732690 33};
34
35/* menu geometry structure */
257e42cc 36struct Geometry {
37 int border; /* window border width */
38 int separator; /* menu separator width */
39 int itemw, itemh; /* item width and height */
a7732690 40 int cursx, cursy; /* cursor position */
41 int screenw, screenh; /* screen width and height */
42};
43
44/* menu item structure */
45struct Item {
d8a7caf2 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) */
858338d9 51 struct Item *prev; /* previous item */
d8a7caf2 52 struct Item *next; /* next item */
53 struct Menu *submenu; /* submenu spawned by clicking on item */
a7732690 54};
55
56/* menu structure */
57struct Menu {
d8a7caf2 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 */
dbeb9940 65 XftDraw *draw;
d8a7caf2 66 Window win; /* menu window to map on the screen */
a7732690 67};
68
257e42cc 69/* functions declarations */
f644b8bc 70static void getresources(void);
257e42cc 71static void getcolor(const char *s, XftColor *color);
a7732690 72static void setupdc(void);
257e42cc 73static void calcgeom(void);
a80fee22 74static struct Item *allocitem(const char *label, const char *output);
a7732690 75static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
a5ddd2cd 76static struct Menu * buildmenutree(unsigned level, const char *label, const char *output);
257e42cc 77static struct Menu *parsestdin(void);
a7732690 78static void calcmenu(struct Menu *menu);
85003546 79static void grabpointer(void);
80static void grabkeyboard(void);
257e42cc 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);
ec4ed1ac 89static void freemenu(struct Menu *menu);
15dfafdf 90static void cleanup(void);
a7732690 91static void usage(void);
92
257e42cc 93/* global variables (X stuff and geometries) */
a7732690 94static Display *dpy;
c6d9174e 95static int screen;
dbeb9940 96static Visual *visual;
a7732690 97static Window rootwin;
c6d9174e 98static Colormap colormap;
a7732690 99static struct DC dc;
257e42cc 100static struct Geometry geom;
a7732690 101
a7732690 102#include "config.h"
103
104int
105main(int argc, char *argv[])
106{
257e42cc 107 struct Menu *rootmenu;
a7732690 108 int ch;
109
27443900 110 while ((ch = getopt(argc, argv, "")) != -1) {
a7732690 111 switch (ch) {
a7732690 112 default:
113 usage();
114 break;
115 }
116 }
117 argc -= optind;
118 argv += optind;
119
c6d9174e 120 if (argc != 0)
121 usage();
122
a7732690 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);
dbeb9940 127 visual = DefaultVisual(dpy, screen);
a7732690 128 rootwin = RootWindow(dpy, screen);
129 colormap = DefaultColormap(dpy, screen);
21a9aaec 130
a7732690 131 /* setup */
f644b8bc 132 getresources();
a7732690 133 setupdc();
257e42cc 134 calcgeom();
a7732690 135
136 /* generate menus and recalculate them */
257e42cc 137 rootmenu = parsestdin();
a7732690 138 if (rootmenu == NULL)
139 errx(1, "no menu generated");
a7732690 140 calcmenu(rootmenu);
141
465d07db 142 /* grab mouse and keyboard */
27443900 143 grabpointer();
144 grabkeyboard();
465d07db 145
a7732690 146 /* run event loop */
257e42cc 147 run(rootmenu);
a7732690 148
15dfafdf 149 /* freeing stuff */
150 freemenu(rootmenu);
151 cleanup();
152
21a9aaec 153 return 0;
154}
155
f644b8bc 156/* read xrdb for configuration options */
157static void
158getresources(void)
159{
160 char *xrm;
161 long n;
fd530f3f 162 char *type;
163 XrmDatabase xdb;
164 XrmValue xval;
f644b8bc 165
166 XrmInitialize();
fd530f3f 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);
f644b8bc 200}
201
a7732690 202/* get color from color string */
dbeb9940 203static void
204getcolor(const char *s, XftColor *color)
a7732690 205{
dbeb9940 206 if(!XftColorAllocName(dpy, visual, colormap, s, color))
a7732690 207 errx(1, "cannot allocate color: %s", s);
a7732690 208}
209
210/* init draw context */
211static void
212setupdc(void)
213{
214 /* get color pixels */
fd530f3f 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);
a7732690 221
222 /* try to get font */
dbeb9940 223 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
a7732690 224 errx(1, "cannot load font");
a7732690 225
fd530f3f 226 /* create common GC */
a7732690 227 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 228}
229
257e42cc 230/* calculate menu and screen geometry */
a7732690 231static void
257e42cc 232calcgeom(void)
a7732690 233{
257e42cc 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);
fd530f3f 241 geom.itemh = dc.font->height + padding_pixels * 2;
242 geom.itemw = width_pixels;
243 geom.border = border_pixels;
244 geom.separator = separator_pixels;
a7732690 245}
246
a7732690 247/* allocate an item */
248static struct Item *
a80fee22 249allocitem(const char *label, const char *output)
a7732690 250{
251 struct Item *item;
252
253 if ((item = malloc(sizeof *item)) == NULL)
254 err(1, "malloc");
a5ddd2cd 255 if (label == NULL) {
7fbd1c5e 256 item->label = NULL;
257 item->output = NULL;
258 } else {
259 if ((item->label = strdup(label)) == NULL)
260 err(1, "strdup");
15dfafdf 261 if (label == output) {
262 item->output = item->label;
263 } else {
264 if ((item->output = strdup(output)) == NULL)
265 err(1, "strdup");
266 }
7fbd1c5e 267 }
a80fee22 268 item->y = 0;
7fbd1c5e 269 item->h = item->label ? geom.itemh : geom.separator;
f1583285 270 if (item->label == NULL)
271 item->labellen = 0;
272 else
273 item->labellen = strlen(item->label);
a7732690 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;
d888f2ca 291 menu->caller = NULL;
a7732690 292 menu->selected = NULL;
a7732690 293 menu->w = geom.itemw;
a80fee22 294 menu->h = 0; /* calculated by calcmenu() */
295 menu->x = 0; /* calculated by calcmenu() */
296 menu->y = 0; /* calculated by calcmenu() */
a7732690 297 menu->level = level;
a7732690 298
27443900 299 swa.override_redirect = True;
fd530f3f 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*/
a7732690 303 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 304 | PointerMotionMask | LeaveWindowMask;
a7732690 305 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
306 CopyFromParent, CopyFromParent, CopyFromParent,
fd530f3f 307 CWOverrideRedirect | CWBackPixel |
308 CWBorderPixel | CWEventMask | CWSaveUnder,
a7732690 309 &swa);
310
311 return menu;
312}
313
a5ddd2cd 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
a7732690 373/* create menus and items from the stdin */
257e42cc 374static struct Menu *
a7732690 375parsestdin(void)
376{
a5ddd2cd 377 struct Menu *rootmenu;
a7732690 378 char *s, buf[BUFSIZ];
379 char *label, *output;
380 unsigned level = 0;
257e42cc 381
a7732690 382 while (fgets(buf, BUFSIZ, stdin) != NULL) {
a5ddd2cd 383 /* get the indentation level */
384 level = strspn(buf, "\t");
a7732690 385
a5ddd2cd 386 /* get the label */
387 s = level + buf;
388 label = strtok(s, "\t\n");
a7732690 389
a5ddd2cd 390 /* get the output */
391 output = strtok(NULL, "\n");
392 if (output == NULL) {
393 output = label;
394 } else {
395 while (*output == '\t')
396 output++;
a7732690 397 }
a5ddd2cd 398
399 rootmenu = buildmenutree(level, label, output);
a7732690 400 }
a7732690 401
257e42cc 402 return rootmenu;
a7732690 403}
404
21a9aaec 405/* recursivelly calculate menu geometry and set window hints */
a7732690 406static void
407calcmenu(struct Menu *menu)
408{
d2435fcd 409 static XClassHint classh = {PROGNAME, PROGNAME};
a7732690 410 XWindowChanges changes;
09c13122 411 XSizeHints sizeh;
dbeb9940 412 XGlyphInfo ext;
a80fee22 413 struct Item *item;
f1583285 414 int labelwidth;
ec4ed1ac 415 int width, height;
a7732690 416
f1583285 417 /* calculate items positions and menu width and height */
418 menu->w = geom.itemw;
a80fee22 419 for (item = menu->list; item != NULL; item = item->next) {
420 item->y = menu->h;
7fbd1c5e 421 if (item->label == NULL) /* height for separator item */
a80fee22 422 menu->h += geom.separator;
423 else
424 menu->h += geom.itemh;
f1583285 425
dbeb9940 426 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
427 item->labellen, &ext);
428 labelwidth = ext.xOff + dc.font->height * 2;
f1583285 429 menu->w = MAX(menu->w, labelwidth);
a80fee22 430 }
a7732690 431
432 /* calculate menu's x and y positions */
ec4ed1ac 433 width = menu->w + geom.border * 2;
434 height = menu->h + geom.border * 2;
a7732690 435 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
257e42cc 436 if (geom.screenw - geom.cursx >= menu->w)
437 menu->x = geom.cursx;
ec4ed1ac 438 else if (geom.cursx > width)
439 menu->x = geom.cursx - width;
257e42cc 440
ec4ed1ac 441 if (geom.screenh - geom.cursy >= height)
257e42cc 442 menu->y = geom.cursy;
ec4ed1ac 443 else if (geom.screenh > height)
444 menu->y = geom.screenh - height;
a7732690 445 } else { /* else, calculate in respect to parent menu */
ec4ed1ac 446 if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
f644b8bc 447 menu->x = menu->parent->x + menu->parent->w + geom.border;
448 else if (menu->parent->x > menu->w + geom.border)
449 menu->x = menu->parent->x - menu->w - geom.border;
a7732690 450
ec4ed1ac 451 if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
8455c369 452 menu->y = menu->caller->y + menu->parent->y;
ec4ed1ac 453 else if (geom.screenh - menu->parent->y > height)
a7732690 454 menu->y = menu->parent->y;
ec4ed1ac 455 else if (geom.screenh > height)
456 menu->y = geom.screenh - height;
a7732690 457 }
458
459 /* update menu geometry */
460 changes.height = menu->h;
f1583285 461 changes.width = menu->w;
a7732690 462 changes.x = menu->x;
463 changes.y = menu->y;
f1583285 464 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 465
d2435fcd 466 /* set window manager hints */
09c13122 467 sizeh.flags = PMaxSize | PMinSize;
468 sizeh.min_width = sizeh.max_width = menu->w;
469 sizeh.min_height = sizeh.max_height = menu->h;
0c1a7864 470 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
d2435fcd 471 NULL, &classh);
09c13122 472
dbeb9940 473 /* create pixmap and XftDraw */
f15fc339 474 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
475 DefaultDepth(dpy, screen));
dbeb9940 476 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 477
a80fee22 478 /* calculate positions of submenus */
a7732690 479 for (item = menu->list; item != NULL; item = item->next) {
480 if (item->submenu != NULL)
481 calcmenu(item->submenu);
482 }
483}
484
85003546 485/* try to grab pointer, we may have to wait for another process to ungrab */
486static void
487grabpointer(void)
488{
489 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
490 int i;
491
492 for (i = 0; i < 1000; i++) {
493 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
494 GrabModeAsync, GrabModeAsync, None,
495 None, CurrentTime) == GrabSuccess)
496 return;
497 nanosleep(&ts, NULL);
498 }
499 errx(1, "cannot grab keyboard");
500}
501
502/* try to grab keyboard, we may have to wait for another process to ungrab */
503static void
504grabkeyboard(void)
505{
506 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
507 int i;
508
509 for (i = 0; i < 1000; i++) {
510 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
511 GrabModeAsync, CurrentTime) == GrabSuccess)
512 return;
513 nanosleep(&ts, NULL);
514 }
515 errx(1, "cannot grab keyboard");
516}
517
0c1a7864 518/* get menu of given window */
519static struct Menu *
257e42cc 520getmenu(struct Menu *currmenu, Window win)
a7732690 521{
0c1a7864 522 struct Menu *menu;
a7732690 523
0c1a7864 524 for (menu = currmenu; menu != NULL; menu = menu->parent)
525 if (menu->win == win)
526 return menu;
a7732690 527
0c1a7864 528 return NULL;
529}
530
531/* get item of given menu and position */
532static struct Item *
533getitem(struct Menu *menu, int y)
534{
535 struct Item *item;
a7732690 536
0c1a7864 537 if (menu == NULL)
538 return NULL;
539
540 for (item = menu->list; item != NULL; item = item->next)
541 if (y >= item->y && y <= item->y + item->h)
542 return item;
543
544 return NULL;
a7732690 545}
546
257e42cc 547/* umap previous menus and map current menu and its parents */
a7732690 548static void
257e42cc 549mapmenu(struct Menu *currmenu)
a7732690 550{
257e42cc 551 static struct Menu *prevmenu = NULL;
d8a7caf2 552 struct Menu *menu, *menu_;
d8a7caf2 553 struct Menu *lcamenu; /* lowest common ancestor menu */
554 unsigned minlevel; /* level of the closest to root menu */
555 unsigned maxlevel; /* level of the closest to root menu */
a7732690 556
257e42cc 557 /* do not remap current menu if it wasn't updated*/
558 if (prevmenu == currmenu)
a7732690 559 return;
560
257e42cc 561 /* if this is the first time mapping, skip calculations */
562 if (prevmenu == NULL) {
fd530f3f 563 XMapWindow(dpy, currmenu->win);
cdc4d51f 564 prevmenu = currmenu;
565 return;
27443900 566 }
567
d8a7caf2 568 /* find lowest common ancestor menu */
257e42cc 569 minlevel = MIN(currmenu->level, prevmenu->level);
570 maxlevel = MAX(currmenu->level, prevmenu->level);
571 if (currmenu->level == maxlevel) {
27443900 572 menu = currmenu;
257e42cc 573 menu_ = prevmenu;
574 } else {
575 menu = prevmenu;
576 menu_ = currmenu;
27443900 577 }
578 while (menu->level > minlevel)
579 menu = menu->parent;
580 while (menu != menu_) {
581 menu = menu->parent;
582 menu_ = menu_->parent;
d8a7caf2 583 }
27443900 584 lcamenu = menu;
d8a7caf2 585
873c080c 586 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
257e42cc 587 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
8455c369 588 menu->selected = NULL;
a7732690 589 XUnmapWindow(dpy, menu->win);
590 }
591
873c080c 592 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 593 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
a7732690 594 XMapWindow(dpy, menu->win);
fd530f3f 595 }
257e42cc 596
257e42cc 597 prevmenu = currmenu;
fd530f3f 598}
599
600/* draw separator item */
601static void
602drawseparator(struct Menu *menu, struct Item *item)
603{
6b5123e7 604 int y;
fd530f3f 605
6b5123e7 606 y = item->y + item->h/2;
fd530f3f 607
608 XSetForeground(dpy, dc.gc, dc.separator.pixel);
6b5123e7 609 XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
fd530f3f 610}
611
612/* draw regular item */
613static void
614drawitem(struct Menu *menu, struct Item *item, XftColor *color)
615{
616 int x, y;
617
cdc4d51f 618 x = dc.font->height;
257e42cc 619 y = item->y + item->h/2 + dc.font->ascent/2 - 1;
fd530f3f 620 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
621 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
622 x, y, item->label, item->labellen);
623
624 /* draw triangle, if item contains a submenu */
625 if (item->submenu != NULL) {
257e42cc 626 x = menu->w - dc.font->height/2 - triangle_width/2;
627 y = item->y + item->h/2 - triangle_height/2 - 1;
fd530f3f 628
629 XPoint triangle[] = {
630 {x, y},
631 {x + triangle_width, y + triangle_height/2},
632 {x, y + triangle_height},
633 {x, y}
634 };
635
636 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
637 Convex, CoordModeOrigin);
d888f2ca 638 }
a7732690 639}
640
641/* draw items of the current menu and of its ancestors */
642static void
257e42cc 643drawmenu(struct Menu *currmenu)
a7732690 644{
645 struct Menu *menu;
646 struct Item *item;
647
648 for (menu = currmenu; menu != NULL; menu = menu->parent) {
a7732690 649 for (item = menu->list; item != NULL; item = item->next) {
dbeb9940 650 XftColor *color;
a7732690 651
652 /* determine item color */
fd530f3f 653 if (item == menu->selected && item->label != NULL)
f644b8bc 654 color = dc.selected;
a7732690 655 else
f644b8bc 656 color = dc.normal;
657
a7732690 658 /* draw item box */
dbeb9940 659 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
f15fc339 660 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
f1583285 661 menu->w, item->h);
7fbd1c5e 662
fd530f3f 663 if (item->label == NULL) /* item is a separator */
664 drawseparator(menu, item);
665 else /* item is a regular item */
666 drawitem(menu, item, color);
a7732690 667 }
fd530f3f 668
669 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
670 menu->w, menu->h, 0, 0);
a7732690 671 }
672}
673
858338d9 674/* cycle through the items; non-zero direction is next, zero is prev */
675static struct Item *
257e42cc 676itemcycle(struct Menu *currmenu, int direction)
858338d9 677{
678 struct Item *item;
679 struct Item *lastitem;
680
681 item = NULL;
682
683 if (direction == ITEMNEXT) {
684 if (currmenu->selected == NULL)
685 item = currmenu->list;
686 else if (currmenu->selected->next != NULL)
687 item = currmenu->selected->next;
688
689 while (item != NULL && item->label == NULL)
690 item = item->next;
691
692 if (item == NULL)
693 item = currmenu->list;
694 } else {
695 for (lastitem = currmenu->list;
696 lastitem != NULL && lastitem->next != NULL;
697 lastitem = lastitem->next)
698 ;
699
700 if (currmenu->selected == NULL)
701 item = lastitem;
702 else if (currmenu->selected->prev != NULL)
703 item = currmenu->selected->prev;
704
705 while (item != NULL && item->label == NULL)
706 item = item->prev;
707
708 if (item == NULL)
709 item = lastitem;
710 }
711
712 return item;
713}
714
a7732690 715/* run event loop */
716static void
257e42cc 717run(struct Menu *currmenu)
a7732690 718{
719 struct Menu *menu;
720 struct Item *item;
721 struct Item *previtem = NULL;
858338d9 722 KeySym ksym;
a7732690 723 XEvent ev;
724
257e42cc 725 mapmenu(currmenu);
fd530f3f 726
a7732690 727 while (!XNextEvent(dpy, &ev)) {
728 switch(ev.type) {
729 case Expose:
858338d9 730 if (ev.xexpose.count == 0)
257e42cc 731 drawmenu(currmenu);
a7732690 732 break;
733 case MotionNotify:
257e42cc 734 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 735 item = getitem(menu, ev.xbutton.y);
c6d9174e 736 if (menu == NULL || item == NULL || previtem == item)
fd530f3f 737 break;
c6d9174e 738 previtem = item;
739 menu->selected = item;
740 if (item->submenu != NULL) {
741 currmenu = item->submenu;
742 currmenu->selected = NULL;
743 } else {
744 currmenu = menu;
a7732690 745 }
c6d9174e 746 mapmenu(currmenu);
747 drawmenu(currmenu);
a7732690 748 break;
749 case ButtonRelease:
257e42cc 750 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 751 item = getitem(menu, ev.xbutton.y);
fd530f3f 752 if (menu == NULL || item == NULL)
858338d9 753 break;
fd530f3f 754selectitem:
755 if (item->label == NULL)
756 break; /* ignore separators */
757 if (item->submenu != NULL) {
257e42cc 758 currmenu = item->submenu;
a7732690 759 } else {
fd530f3f 760 printf("%s\n", item->output);
08f16589 761 return;
a7732690 762 }
257e42cc 763 mapmenu(currmenu);
fd530f3f 764 currmenu->selected = currmenu->list;
257e42cc 765 drawmenu(currmenu);
fd530f3f 766 break;
858338d9 767 case ButtonPress:
257e42cc 768 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 769 if (menu == NULL)
858338d9 770 return;
771 break;
772 case KeyPress:
773 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
774
0c1a7864 775 /* esc closes xmenu when current menu is the root menu */
257e42cc 776 if (ksym == XK_Escape && currmenu->parent == NULL)
858338d9 777 return;
778
779 /* Shift-Tab = ISO_Left_Tab */
780 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
781 ksym = XK_ISO_Left_Tab;
782
783 /* cycle through menu */
784 item = NULL;
785 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
257e42cc 786 item = itemcycle(currmenu, ITEMPREV);
858338d9 787 } else if (ksym == XK_Tab || ksym == XK_Down) {
257e42cc 788 item = itemcycle(currmenu, ITEMNEXT);
858338d9 789 } else if ((ksym == XK_Return || ksym == XK_Right) &&
790 currmenu->selected != NULL) {
791 item = currmenu->selected;
792 goto selectitem;
793 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
794 currmenu->parent != NULL) {
795 item = currmenu->parent->selected;
257e42cc 796 currmenu = currmenu->parent;
797 mapmenu(currmenu);
858338d9 798 } else
799 break;
800 currmenu->selected = item;
257e42cc 801 drawmenu(currmenu);
a7732690 802 break;
f15fc339 803 case LeaveNotify:
fd530f3f 804 previtem = NULL;
f15fc339 805 currmenu->selected = NULL;
257e42cc 806 drawmenu(currmenu);
f15fc339 807 break;
a7732690 808 }
809 }
810}
811
fd530f3f 812/* recursivelly free pixmaps and destroy windows */
f15fc339 813static void
ec4ed1ac 814freemenu(struct Menu *menu)
f15fc339 815{
816 struct Item *item;
ec4ed1ac 817 struct Item *tmp;
f15fc339 818
ec4ed1ac 819 item = menu->list;
820 while (item != NULL) {
f15fc339 821 if (item->submenu != NULL)
ec4ed1ac 822 freemenu(item->submenu);
823 tmp = item;
824 item = item->next;
15dfafdf 825 if (tmp->label != tmp->output)
826 free(tmp->label);
6b5123e7 827 free(tmp->output);
ec4ed1ac 828 free(tmp);
829 }
f15fc339 830
831 XFreePixmap(dpy, menu->pixmap);
dbeb9940 832 XftDrawDestroy(menu->draw);
f15fc339 833 XDestroyWindow(dpy, menu->win);
ec4ed1ac 834 free(menu);
f15fc339 835}
836
a7732690 837/* cleanup and exit */
838static void
15dfafdf 839cleanup(void)
a7732690 840{
257e42cc 841 XUngrabPointer(dpy, CurrentTime);
842 XUngrabKeyboard(dpy, CurrentTime);
843
dbeb9940 844 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
845 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
846 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
847 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 848 XftColorFree(dpy, visual, colormap, &dc.separator);
849 XftColorFree(dpy, visual, colormap, &dc.border);
dbeb9940 850
f15fc339 851 XFreeGC(dpy, dc.gc);
a7732690 852 XCloseDisplay(dpy);
a7732690 853}
854
855/* show usage */
856static void
857usage(void)
858{
c6d9174e 859 (void)fprintf(stderr, "usage: xmenu\n");
a7732690 860 exit(1);
861}