Fixing calculation of menu position.
[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);
ec4ed1ac 89static void freemenu(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;
ec4ed1ac 412 int width, height;
a7732690 413
f1583285 414 /* calculate items positions and menu width and height */
415 menu->w = geom.itemw;
a80fee22 416 for (item = menu->list; item != NULL; item = item->next) {
417 item->y = menu->h;
7fbd1c5e 418 if (item->label == NULL) /* height for separator item */
a80fee22 419 menu->h += geom.separator;
420 else
421 menu->h += geom.itemh;
f1583285 422
dbeb9940 423 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
424 item->labellen, &ext);
425 labelwidth = ext.xOff + dc.font->height * 2;
f1583285 426 menu->w = MAX(menu->w, labelwidth);
a80fee22 427 }
a7732690 428
429 /* calculate menu's x and y positions */
ec4ed1ac 430 width = menu->w + geom.border * 2;
431 height = menu->h + geom.border * 2;
a7732690 432 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
257e42cc 433 if (geom.screenw - geom.cursx >= menu->w)
434 menu->x = geom.cursx;
ec4ed1ac 435 else if (geom.cursx > width)
436 menu->x = geom.cursx - width;
257e42cc 437
ec4ed1ac 438 if (geom.screenh - geom.cursy >= height)
257e42cc 439 menu->y = geom.cursy;
ec4ed1ac 440 else if (geom.screenh > height)
441 menu->y = geom.screenh - height;
a7732690 442 } else { /* else, calculate in respect to parent menu */
ec4ed1ac 443 if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
f644b8bc 444 menu->x = menu->parent->x + menu->parent->w + geom.border;
445 else if (menu->parent->x > menu->w + geom.border)
446 menu->x = menu->parent->x - menu->w - geom.border;
a7732690 447
ec4ed1ac 448 if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
8455c369 449 menu->y = menu->caller->y + menu->parent->y;
ec4ed1ac 450 else if (geom.screenh - menu->parent->y > height)
a7732690 451 menu->y = menu->parent->y;
ec4ed1ac 452 else if (geom.screenh > height)
453 menu->y = geom.screenh - height;
a7732690 454 }
455
456 /* update menu geometry */
457 changes.height = menu->h;
f1583285 458 changes.width = menu->w;
a7732690 459 changes.x = menu->x;
460 changes.y = menu->y;
f1583285 461 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 462
d2435fcd 463 /* set window manager hints */
09c13122 464 sizeh.flags = PMaxSize | PMinSize;
465 sizeh.min_width = sizeh.max_width = menu->w;
466 sizeh.min_height = sizeh.max_height = menu->h;
0c1a7864 467 XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
d2435fcd 468 NULL, &classh);
09c13122 469
dbeb9940 470 /* create pixmap and XftDraw */
f15fc339 471 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
472 DefaultDepth(dpy, screen));
dbeb9940 473 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 474
a80fee22 475 /* calculate positions of submenus */
a7732690 476 for (item = menu->list; item != NULL; item = item->next) {
477 if (item->submenu != NULL)
478 calcmenu(item->submenu);
479 }
480}
481
85003546 482/* try to grab pointer, we may have to wait for another process to ungrab */
483static void
484grabpointer(void)
485{
486 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
487 int i;
488
489 for (i = 0; i < 1000; i++) {
490 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
491 GrabModeAsync, GrabModeAsync, None,
492 None, CurrentTime) == GrabSuccess)
493 return;
494 nanosleep(&ts, NULL);
495 }
496 errx(1, "cannot grab keyboard");
497}
498
499/* try to grab keyboard, we may have to wait for another process to ungrab */
500static void
501grabkeyboard(void)
502{
503 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
504 int i;
505
506 for (i = 0; i < 1000; i++) {
507 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
508 GrabModeAsync, CurrentTime) == GrabSuccess)
509 return;
510 nanosleep(&ts, NULL);
511 }
512 errx(1, "cannot grab keyboard");
513}
514
0c1a7864 515/* get menu of given window */
516static struct Menu *
257e42cc 517getmenu(struct Menu *currmenu, Window win)
a7732690 518{
0c1a7864 519 struct Menu *menu;
a7732690 520
0c1a7864 521 for (menu = currmenu; menu != NULL; menu = menu->parent)
522 if (menu->win == win)
523 return menu;
a7732690 524
0c1a7864 525 return NULL;
526}
527
528/* get item of given menu and position */
529static struct Item *
530getitem(struct Menu *menu, int y)
531{
532 struct Item *item;
a7732690 533
0c1a7864 534 if (menu == NULL)
535 return NULL;
536
537 for (item = menu->list; item != NULL; item = item->next)
538 if (y >= item->y && y <= item->y + item->h)
539 return item;
540
541 return NULL;
a7732690 542}
543
257e42cc 544/* umap previous menus and map current menu and its parents */
a7732690 545static void
257e42cc 546mapmenu(struct Menu *currmenu)
a7732690 547{
257e42cc 548 static struct Menu *prevmenu = NULL;
d8a7caf2 549 struct Menu *menu, *menu_;
d8a7caf2 550 struct Menu *lcamenu; /* lowest common ancestor menu */
551 unsigned minlevel; /* level of the closest to root menu */
552 unsigned maxlevel; /* level of the closest to root menu */
a7732690 553
257e42cc 554 /* do not remap current menu if it wasn't updated*/
555 if (prevmenu == currmenu)
a7732690 556 return;
557
257e42cc 558 /* if this is the first time mapping, skip calculations */
559 if (prevmenu == NULL) {
fd530f3f 560 XMapWindow(dpy, currmenu->win);
cdc4d51f 561 prevmenu = currmenu;
562 return;
27443900 563 }
564
d8a7caf2 565 /* find lowest common ancestor menu */
257e42cc 566 minlevel = MIN(currmenu->level, prevmenu->level);
567 maxlevel = MAX(currmenu->level, prevmenu->level);
568 if (currmenu->level == maxlevel) {
27443900 569 menu = currmenu;
257e42cc 570 menu_ = prevmenu;
571 } else {
572 menu = prevmenu;
573 menu_ = currmenu;
27443900 574 }
575 while (menu->level > minlevel)
576 menu = menu->parent;
577 while (menu != menu_) {
578 menu = menu->parent;
579 menu_ = menu_->parent;
d8a7caf2 580 }
27443900 581 lcamenu = menu;
d8a7caf2 582
873c080c 583 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
257e42cc 584 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
8455c369 585 menu->selected = NULL;
a7732690 586 XUnmapWindow(dpy, menu->win);
587 }
588
873c080c 589 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 590 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
a7732690 591 XMapWindow(dpy, menu->win);
fd530f3f 592 }
257e42cc 593
257e42cc 594 prevmenu = currmenu;
fd530f3f 595}
596
597/* draw separator item */
598static void
599drawseparator(struct Menu *menu, struct Item *item)
600{
601 int linex, liney, linew;
602
603 linex = dc.font->height;
604 liney = item->y + item->h/2;
605 linew = menu->w - dc.font->height;
606
607 XSetForeground(dpy, dc.gc, dc.separator.pixel);
608 XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
609}
610
611/* draw regular item */
612static void
613drawitem(struct Menu *menu, struct Item *item, XftColor *color)
614{
615 int x, y;
616
cdc4d51f 617 x = dc.font->height;
257e42cc 618 y = item->y + item->h/2 + dc.font->ascent/2 - 1;
fd530f3f 619 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
620 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
621 x, y, item->label, item->labellen);
622
623 /* draw triangle, if item contains a submenu */
624 if (item->submenu != NULL) {
257e42cc 625 x = menu->w - dc.font->height/2 - triangle_width/2;
626 y = item->y + item->h/2 - triangle_height/2 - 1;
fd530f3f 627
628 XPoint triangle[] = {
629 {x, y},
630 {x + triangle_width, y + triangle_height/2},
631 {x, y + triangle_height},
632 {x, y}
633 };
634
635 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
636 Convex, CoordModeOrigin);
d888f2ca 637 }
a7732690 638}
639
640/* draw items of the current menu and of its ancestors */
641static void
257e42cc 642drawmenu(struct Menu *currmenu)
a7732690 643{
644 struct Menu *menu;
645 struct Item *item;
646
647 for (menu = currmenu; menu != NULL; menu = menu->parent) {
a7732690 648 for (item = menu->list; item != NULL; item = item->next) {
dbeb9940 649 XftColor *color;
a7732690 650
651 /* determine item color */
fd530f3f 652 if (item == menu->selected && item->label != NULL)
f644b8bc 653 color = dc.selected;
a7732690 654 else
f644b8bc 655 color = dc.normal;
656
a7732690 657 /* draw item box */
dbeb9940 658 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
f15fc339 659 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
f1583285 660 menu->w, item->h);
7fbd1c5e 661
fd530f3f 662 if (item->label == NULL) /* item is a separator */
663 drawseparator(menu, item);
664 else /* item is a regular item */
665 drawitem(menu, item, color);
a7732690 666 }
fd530f3f 667
668 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
669 menu->w, menu->h, 0, 0);
a7732690 670 }
671}
672
858338d9 673/* cycle through the items; non-zero direction is next, zero is prev */
674static struct Item *
257e42cc 675itemcycle(struct Menu *currmenu, int direction)
858338d9 676{
677 struct Item *item;
678 struct Item *lastitem;
679
680 item = NULL;
681
682 if (direction == ITEMNEXT) {
683 if (currmenu->selected == NULL)
684 item = currmenu->list;
685 else if (currmenu->selected->next != NULL)
686 item = currmenu->selected->next;
687
688 while (item != NULL && item->label == NULL)
689 item = item->next;
690
691 if (item == NULL)
692 item = currmenu->list;
693 } else {
694 for (lastitem = currmenu->list;
695 lastitem != NULL && lastitem->next != NULL;
696 lastitem = lastitem->next)
697 ;
698
699 if (currmenu->selected == NULL)
700 item = lastitem;
701 else if (currmenu->selected->prev != NULL)
702 item = currmenu->selected->prev;
703
704 while (item != NULL && item->label == NULL)
705 item = item->prev;
706
707 if (item == NULL)
708 item = lastitem;
709 }
710
711 return item;
712}
713
a7732690 714/* run event loop */
715static void
257e42cc 716run(struct Menu *currmenu)
a7732690 717{
718 struct Menu *menu;
719 struct Item *item;
720 struct Item *previtem = NULL;
858338d9 721 KeySym ksym;
a7732690 722 XEvent ev;
723
257e42cc 724 mapmenu(currmenu);
fd530f3f 725
a7732690 726 while (!XNextEvent(dpy, &ev)) {
727 switch(ev.type) {
728 case Expose:
858338d9 729 if (ev.xexpose.count == 0)
257e42cc 730 drawmenu(currmenu);
a7732690 731 break;
732 case MotionNotify:
257e42cc 733 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 734 item = getitem(menu, ev.xbutton.y);
c6d9174e 735 if (menu == NULL || item == NULL || previtem == item)
fd530f3f 736 break;
c6d9174e 737 previtem = item;
738 menu->selected = item;
739 if (item->submenu != NULL) {
740 currmenu = item->submenu;
741 currmenu->selected = NULL;
742 } else {
743 currmenu = menu;
a7732690 744 }
c6d9174e 745 mapmenu(currmenu);
746 drawmenu(currmenu);
a7732690 747 break;
748 case ButtonRelease:
257e42cc 749 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 750 item = getitem(menu, ev.xbutton.y);
fd530f3f 751 if (menu == NULL || item == NULL)
858338d9 752 break;
fd530f3f 753selectitem:
754 if (item->label == NULL)
755 break; /* ignore separators */
756 if (item->submenu != NULL) {
257e42cc 757 currmenu = item->submenu;
a7732690 758 } else {
fd530f3f 759 printf("%s\n", item->output);
08f16589 760 return;
a7732690 761 }
257e42cc 762 mapmenu(currmenu);
fd530f3f 763 currmenu->selected = currmenu->list;
257e42cc 764 drawmenu(currmenu);
fd530f3f 765 break;
858338d9 766 case ButtonPress:
257e42cc 767 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 768 if (menu == NULL)
858338d9 769 return;
770 break;
771 case KeyPress:
772 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
773
0c1a7864 774 /* esc closes xmenu when current menu is the root menu */
257e42cc 775 if (ksym == XK_Escape && currmenu->parent == NULL)
858338d9 776 return;
777
778 /* Shift-Tab = ISO_Left_Tab */
779 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
780 ksym = XK_ISO_Left_Tab;
781
782 /* cycle through menu */
783 item = NULL;
784 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
257e42cc 785 item = itemcycle(currmenu, ITEMPREV);
858338d9 786 } else if (ksym == XK_Tab || ksym == XK_Down) {
257e42cc 787 item = itemcycle(currmenu, ITEMNEXT);
858338d9 788 } else if ((ksym == XK_Return || ksym == XK_Right) &&
789 currmenu->selected != NULL) {
790 item = currmenu->selected;
791 goto selectitem;
792 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
793 currmenu->parent != NULL) {
794 item = currmenu->parent->selected;
257e42cc 795 currmenu = currmenu->parent;
796 mapmenu(currmenu);
858338d9 797 } else
798 break;
799 currmenu->selected = item;
257e42cc 800 drawmenu(currmenu);
a7732690 801 break;
f15fc339 802 case LeaveNotify:
fd530f3f 803 previtem = NULL;
f15fc339 804 currmenu->selected = NULL;
257e42cc 805 drawmenu(currmenu);
f15fc339 806 break;
a7732690 807 }
808 }
809}
810
fd530f3f 811/* recursivelly free pixmaps and destroy windows */
f15fc339 812static void
ec4ed1ac 813freemenu(struct Menu *menu)
f15fc339 814{
815 struct Item *item;
ec4ed1ac 816 struct Item *tmp;
f15fc339 817
ec4ed1ac 818 item = menu->list;
819 while (item != NULL) {
f15fc339 820 if (item->submenu != NULL)
ec4ed1ac 821 freemenu(item->submenu);
822 tmp = item;
823 item = item->next;
824 free(tmp);
825 }
f15fc339 826
827 XFreePixmap(dpy, menu->pixmap);
dbeb9940 828 XftDrawDestroy(menu->draw);
f15fc339 829 XDestroyWindow(dpy, menu->win);
ec4ed1ac 830 free(menu);
f15fc339 831}
832
a7732690 833/* cleanup and exit */
834static void
257e42cc 835cleanup(struct Menu *rootmenu)
a7732690 836{
257e42cc 837 XUngrabPointer(dpy, CurrentTime);
838 XUngrabKeyboard(dpy, CurrentTime);
839
ec4ed1ac 840 freemenu(rootmenu);
dbeb9940 841
842 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
843 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
844 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
845 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 846 XftColorFree(dpy, visual, colormap, &dc.separator);
847 XftColorFree(dpy, visual, colormap, &dc.border);
dbeb9940 848
f15fc339 849 XFreeGC(dpy, dc.gc);
a7732690 850 XCloseDisplay(dpy);
a7732690 851}
852
853/* show usage */
854static void
855usage(void)
856{
c6d9174e 857 (void)fprintf(stderr, "usage: xmenu\n");
a7732690 858 exit(1);
859}