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