Updating install commands in Makefile
[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 */
257e42cc 37struct Geometry {
38 int border; /* window border width */
39 int separator; /* menu separator width */
40 int itemw, itemh; /* item width and height */
a7732690 41 int cursx, cursy; /* cursor position */
42 int screenw, screenh; /* screen width and height */
43};
44
45/* menu item structure */
46struct Item {
d8a7caf2 47 char *label; /* string to be drawed on menu */
48 char *output; /* string to be outputed when item is clicked */
49 int y; /* item y position relative to menu */
50 int h; /* item height */
51 size_t labellen; /* strlen(label) */
858338d9 52 struct Item *prev; /* previous item */
d8a7caf2 53 struct Item *next; /* next item */
54 struct Menu *submenu; /* submenu spawned by clicking on item */
a7732690 55};
56
57/* menu structure */
58struct Menu {
d8a7caf2 59 struct Menu *parent; /* parent menu */
60 struct Item *caller; /* item that spawned the menu */
61 struct Item *list; /* list of items contained by the menu */
62 struct Item *selected; /* item currently selected in the menu */
63 int x, y, w, h; /* menu geometry */
64 unsigned level; /* menu level relative to root */
65 Drawable pixmap; /* pixmap to draw the menu on */
dbeb9940 66 XftDraw *draw;
d8a7caf2 67 Window win; /* menu window to map on the screen */
a7732690 68};
69
257e42cc 70/* functions declarations */
f644b8bc 71static void getresources(void);
257e42cc 72static void getcolor(const char *s, XftColor *color);
a7732690 73static void setupdc(void);
257e42cc 74static void calcgeom(void);
a80fee22 75static struct Item *allocitem(const char *label, const char *output);
a7732690 76static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
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);
f15fc339 89static void freewindow(struct Menu *menu);
257e42cc 90static void cleanup(struct Menu *rootmenu);
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
257e42cc 149 cleanup(rootmenu);
21a9aaec 150 return 0;
151}
152
f644b8bc 153/* read xrdb for configuration options */
154static void
155getresources(void)
156{
157 char *xrm;
158 long n;
fd530f3f 159 char *type;
160 XrmDatabase xdb;
161 XrmValue xval;
f644b8bc 162
163 XrmInitialize();
fd530f3f 164 if ((xrm = XResourceManagerString(dpy)) == NULL)
165 return;
166
167 xdb = XrmGetStringDatabase(xrm);
168
169 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
170 if ((n = strtol(xval.addr, NULL, 10)) > 0)
171 border_pixels = n;
172 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
173 if ((n = strtol(xval.addr, NULL, 10)) > 0)
174 separator_pixels = n;
175 if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True)
176 if ((n = strtol(xval.addr, NULL, 10)) > 0)
177 padding_pixels = n;
178 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
179 if ((n = strtol(xval.addr, NULL, 10)) > 0)
180 width_pixels = n;
181 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
182 background_color = strdup(xval.addr);
183 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
184 foreground_color = strdup(xval.addr);
185 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
186 selbackground_color = strdup(xval.addr);
187 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
188 selforeground_color = strdup(xval.addr);
189 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
190 separator_color = strdup(xval.addr);
191 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
192 border_color = strdup(xval.addr);
193 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
194 font = strdup(xval.addr);
195
196 XrmDestroyDatabase(xdb);
f644b8bc 197}
198
a7732690 199/* get color from color string */
dbeb9940 200static void
201getcolor(const char *s, XftColor *color)
a7732690 202{
dbeb9940 203 if(!XftColorAllocName(dpy, visual, colormap, s, color))
a7732690 204 errx(1, "cannot allocate color: %s", s);
a7732690 205}
206
207/* init draw context */
208static void
209setupdc(void)
210{
211 /* get color pixels */
fd530f3f 212 getcolor(background_color, &dc.normal[ColorBG]);
213 getcolor(foreground_color, &dc.normal[ColorFG]);
214 getcolor(selbackground_color, &dc.selected[ColorBG]);
215 getcolor(selforeground_color, &dc.selected[ColorFG]);
216 getcolor(separator_color, &dc.separator);
217 getcolor(border_color, &dc.border);
a7732690 218
219 /* try to get font */
dbeb9940 220 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
a7732690 221 errx(1, "cannot load font");
a7732690 222
fd530f3f 223 /* create common GC */
a7732690 224 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 225}
226
257e42cc 227/* calculate menu and screen geometry */
a7732690 228static void
257e42cc 229calcgeom(void)
a7732690 230{
257e42cc 231 Window w1, w2; /* unused variables */
232 int a, b; /* unused variables */
233 unsigned mask; /* unused variable */
234
235 XQueryPointer(dpy, rootwin, &w1, &w2, &geom.cursx, &geom.cursy, &a, &b, &mask);
236 geom.screenw = DisplayWidth(dpy, screen);
237 geom.screenh = DisplayHeight(dpy, screen);
fd530f3f 238 geom.itemh = dc.font->height + padding_pixels * 2;
239 geom.itemw = width_pixels;
240 geom.border = border_pixels;
241 geom.separator = separator_pixels;
a7732690 242}
243
a7732690 244/* allocate an item */
245static struct Item *
a80fee22 246allocitem(const char *label, const char *output)
a7732690 247{
248 struct Item *item;
249
250 if ((item = malloc(sizeof *item)) == NULL)
251 err(1, "malloc");
7fbd1c5e 252 if (*label == '\0') {
253 item->label = NULL;
254 item->output = NULL;
255 } else {
256 if ((item->label = strdup(label)) == NULL)
257 err(1, "strdup");
258 if ((item->output = strdup(output)) == NULL)
259 err(1, "strdup");
260 }
a80fee22 261 item->y = 0;
7fbd1c5e 262 item->h = item->label ? geom.itemh : geom.separator;
f1583285 263 if (item->label == NULL)
264 item->labellen = 0;
265 else
266 item->labellen = strlen(item->label);
a7732690 267 item->next = NULL;
268 item->submenu = NULL;
269
270 return item;
271}
272
273/* allocate a menu */
274static struct Menu *
275allocmenu(struct Menu *parent, struct Item *list, unsigned level)
276{
277 XSetWindowAttributes swa;
278 struct Menu *menu;
279
280 if ((menu = malloc(sizeof *menu)) == NULL)
281 err(1, "malloc");
282 menu->parent = parent;
283 menu->list = list;
d888f2ca 284 menu->caller = NULL;
a7732690 285 menu->selected = NULL;
a7732690 286 menu->w = geom.itemw;
a80fee22 287 menu->h = 0; /* calculated by calcmenu() */
288 menu->x = 0; /* calculated by calcmenu() */
289 menu->y = 0; /* calculated by calcmenu() */
a7732690 290 menu->level = level;
a7732690 291
27443900 292 swa.override_redirect = True;
fd530f3f 293 swa.background_pixel = dc.normal[ColorBG].pixel;
294 swa.border_pixel = dc.border.pixel;
295 swa.save_under = True; /* pop-up windows should save_under*/
a7732690 296 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 297 | PointerMotionMask | LeaveWindowMask;
a7732690 298 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
299 CopyFromParent, CopyFromParent, CopyFromParent,
fd530f3f 300 CWOverrideRedirect | CWBackPixel |
301 CWBorderPixel | CWEventMask | CWSaveUnder,
a7732690 302 &swa);
303
304 return menu;
305}
306
307/* create menus and items from the stdin */
257e42cc 308static struct Menu *
a7732690 309parsestdin(void)
310{
311 char *s, buf[BUFSIZ];
312 char *label, *output;
313 unsigned level = 0;
314 unsigned i;
a80fee22 315 struct Item *curritem = NULL; /* item currently being read */
316 struct Menu *prevmenu = NULL; /* menu the previous item was added to */
c6d9174e 317 struct Item *item; /* dummy item for loops */
318 struct Menu *menu; /* dummy menu for loops */
319 struct Menu *rootmenu; /* menu to be returned */
a7732690 320
257e42cc 321 rootmenu = NULL;
322
a7732690 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;
858338d9 355 curritem->prev = NULL;
356 curritem->next = NULL;
a7732690 357 } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
358 for (menu = prevmenu, i = level;
359 menu != NULL && i < prevmenu->level;
360 menu = menu->parent, i++)
361 ;
362
363 if (menu == NULL)
364 errx(1, "reached NULL menu");
365
a80fee22 366 for (item = menu->list; item->next != NULL; item = item->next)
a7732690 367 ;
368
a80fee22 369 item->next = curritem;
858338d9 370
371 curritem->prev = item;
372 curritem->next = NULL;
373
a7732690 374 prevmenu = menu;
375 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
a80fee22 376 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 377 ;
a80fee22 378 item->next = curritem;
858338d9 379
380 curritem->prev = item;
381 curritem->next = NULL;
382
a7732690 383 } else if (level > prevmenu->level) { /* item begins a new menu */
a80fee22 384 menu = allocmenu(prevmenu, curritem, level);
a7732690 385
a80fee22 386 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 387 ;
388
a80fee22 389 item->submenu = menu;
d888f2ca 390 menu->caller = item;
a7732690 391
858338d9 392 curritem->prev = NULL;
393 curritem->next = NULL;
394
a7732690 395 prevmenu = menu;
396 }
397 }
a7732690 398
257e42cc 399 return rootmenu;
a7732690 400}
401
21a9aaec 402/* recursivelly calculate menu geometry and set window hints */
a7732690 403static void
404calcmenu(struct Menu *menu)
405{
d2435fcd 406 static XClassHint classh = {PROGNAME, PROGNAME};
a7732690 407 XWindowChanges changes;
09c13122 408 XSizeHints sizeh;
dbeb9940 409 XGlyphInfo ext;
a80fee22 410 struct Item *item;
f1583285 411 int labelwidth;
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 */
429 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
257e42cc 430 if (geom.screenw - geom.cursx >= menu->w)
431 menu->x = geom.cursx;
432 else if (geom.cursx > menu->w)
433 menu->x = geom.cursx - menu->w;
434
435 if (geom.screenh - geom.cursy >= menu->h)
436 menu->y = geom.cursy;
437 else if (geom.screenh > menu->h)
438 menu->y = geom.screenh - menu->h;
a7732690 439 } else { /* else, calculate in respect to parent menu */
257e42cc 440 if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w)
f644b8bc 441 menu->x = menu->parent->x + menu->parent->w + geom.border;
442 else if (menu->parent->x > menu->w + geom.border)
443 menu->x = menu->parent->x - menu->w - geom.border;
a7732690 444
257e42cc 445 if (geom.screenh - (menu->caller->y + menu->parent->y) > menu->h)
8455c369 446 menu->y = menu->caller->y + menu->parent->y;
257e42cc 447 else if (geom.screenh - menu->parent->y > menu->h)
a7732690 448 menu->y = menu->parent->y;
257e42cc 449 else if (geom.screenh > menu->h)
450 menu->y = geom.screenh - menu->h;
a7732690 451 }
452
453 /* update menu geometry */
454 changes.height = menu->h;
f1583285 455 changes.width = menu->w;
a7732690 456 changes.x = menu->x;
457 changes.y = menu->y;
f1583285 458 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 459
d2435fcd 460 /* set window manager hints */
09c13122 461 sizeh.flags = PMaxSize | PMinSize;
462 sizeh.min_width = sizeh.max_width = menu->w;
463 sizeh.min_height = sizeh.max_height = menu->h;
0c1a7864 464 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
d2435fcd 465 NULL, &classh);
09c13122 466
dbeb9940 467 /* create pixmap and XftDraw */
f15fc339 468 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
469 DefaultDepth(dpy, screen));
dbeb9940 470 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 471
a80fee22 472 /* calculate positions of submenus */
a7732690 473 for (item = menu->list; item != NULL; item = item->next) {
474 if (item->submenu != NULL)
475 calcmenu(item->submenu);
476 }
477}
478
85003546 479/* try to grab pointer, we may have to wait for another process to ungrab */
480static void
481grabpointer(void)
482{
483 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
484 int i;
485
486 for (i = 0; i < 1000; i++) {
487 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
488 GrabModeAsync, GrabModeAsync, None,
489 None, CurrentTime) == GrabSuccess)
490 return;
491 nanosleep(&ts, NULL);
492 }
493 errx(1, "cannot grab keyboard");
494}
495
496/* try to grab keyboard, we may have to wait for another process to ungrab */
497static void
498grabkeyboard(void)
499{
500 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
501 int i;
502
503 for (i = 0; i < 1000; i++) {
504 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
505 GrabModeAsync, CurrentTime) == GrabSuccess)
506 return;
507 nanosleep(&ts, NULL);
508 }
509 errx(1, "cannot grab keyboard");
510}
511
0c1a7864 512/* get menu of given window */
513static struct Menu *
257e42cc 514getmenu(struct Menu *currmenu, Window win)
a7732690 515{
0c1a7864 516 struct Menu *menu;
a7732690 517
0c1a7864 518 for (menu = currmenu; menu != NULL; menu = menu->parent)
519 if (menu->win == win)
520 return menu;
a7732690 521
0c1a7864 522 return NULL;
523}
524
525/* get item of given menu and position */
526static struct Item *
527getitem(struct Menu *menu, int y)
528{
529 struct Item *item;
a7732690 530
0c1a7864 531 if (menu == NULL)
532 return NULL;
533
534 for (item = menu->list; item != NULL; item = item->next)
535 if (y >= item->y && y <= item->y + item->h)
536 return item;
537
538 return NULL;
a7732690 539}
540
257e42cc 541/* umap previous menus and map current menu and its parents */
a7732690 542static void
257e42cc 543mapmenu(struct Menu *currmenu)
a7732690 544{
257e42cc 545 static struct Menu *prevmenu = NULL;
d8a7caf2 546 struct Menu *menu, *menu_;
d8a7caf2 547 struct Menu *lcamenu; /* lowest common ancestor menu */
548 unsigned minlevel; /* level of the closest to root menu */
549 unsigned maxlevel; /* level of the closest to root menu */
a7732690 550
257e42cc 551 /* do not remap current menu if it wasn't updated*/
552 if (prevmenu == currmenu)
a7732690 553 return;
554
257e42cc 555 /* if this is the first time mapping, skip calculations */
556 if (prevmenu == NULL) {
fd530f3f 557 XMapWindow(dpy, currmenu->win);
cdc4d51f 558 prevmenu = currmenu;
559 return;
27443900 560 }
561
d8a7caf2 562 /* find lowest common ancestor menu */
257e42cc 563 minlevel = MIN(currmenu->level, prevmenu->level);
564 maxlevel = MAX(currmenu->level, prevmenu->level);
565 if (currmenu->level == maxlevel) {
27443900 566 menu = currmenu;
257e42cc 567 menu_ = prevmenu;
568 } else {
569 menu = prevmenu;
570 menu_ = currmenu;
27443900 571 }
572 while (menu->level > minlevel)
573 menu = menu->parent;
574 while (menu != menu_) {
575 menu = menu->parent;
576 menu_ = menu_->parent;
d8a7caf2 577 }
27443900 578 lcamenu = menu;
d8a7caf2 579
873c080c 580 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
257e42cc 581 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
8455c369 582 menu->selected = NULL;
a7732690 583 XUnmapWindow(dpy, menu->win);
584 }
585
873c080c 586 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 587 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
a7732690 588 XMapWindow(dpy, menu->win);
fd530f3f 589 }
257e42cc 590
257e42cc 591 prevmenu = currmenu;
fd530f3f 592}
593
594/* draw separator item */
595static void
596drawseparator(struct Menu *menu, struct Item *item)
597{
598 int linex, liney, linew;
599
600 linex = dc.font->height;
601 liney = item->y + item->h/2;
602 linew = menu->w - dc.font->height;
603
604 XSetForeground(dpy, dc.gc, dc.separator.pixel);
605 XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
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
810freewindow(struct Menu *menu)
811{
812 struct Item *item;
813
814 for (item = menu->list; item != NULL; item = item->next)
815 if (item->submenu != NULL)
816 freewindow(item->submenu);
817
818 XFreePixmap(dpy, menu->pixmap);
dbeb9940 819 XftDrawDestroy(menu->draw);
f15fc339 820 XDestroyWindow(dpy, menu->win);
821}
822
a7732690 823/* cleanup and exit */
824static void
257e42cc 825cleanup(struct Menu *rootmenu)
a7732690 826{
257e42cc 827 XUngrabPointer(dpy, CurrentTime);
828 XUngrabKeyboard(dpy, CurrentTime);
829
f15fc339 830 freewindow(rootmenu);
dbeb9940 831
832 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
833 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
834 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
835 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 836 XftColorFree(dpy, visual, colormap, &dc.separator);
837 XftColorFree(dpy, visual, colormap, &dc.border);
dbeb9940 838
f15fc339 839 XFreeGC(dpy, dc.gc);
a7732690 840 XCloseDisplay(dpy);
a7732690 841}
842
843/* show usage */
844static void
845usage(void)
846{
c6d9174e 847 (void)fprintf(stderr, "usage: xmenu\n");
a7732690 848 exit(1);
849}