Configuring height directly by setting the height.
[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>
33376f54 11#include <Imlib2.h>
858338d9 12
d2435fcd 13#define PROGNAME "xmenu"
858338d9 14#define ITEMPREV 0
15#define ITEMNEXT 1
a7732690 16
17/* macros */
18#define LEN(x) (sizeof (x) / sizeof (x[0]))
f1583285 19#define MAX(x,y) ((x)>(y)?(x):(y))
d8a7caf2 20#define MIN(x,y) ((x)<(y)?(x):(y))
a7732690 21
22/* color enum */
23enum {ColorFG, ColorBG, ColorLast};
24
25/* draw context structure */
26struct DC {
dbeb9940 27 XftColor normal[ColorLast];
28 XftColor selected[ColorLast];
fd530f3f 29 XftColor border;
30 XftColor separator;
a7732690 31
a7732690 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 */
3bec05ea 49 char *file; /* filename of the icon */
d8a7caf2 50 int y; /* item y position relative to menu */
51 int h; /* item height */
52 size_t labellen; /* strlen(label) */
858338d9 53 struct Item *prev; /* previous item */
d8a7caf2 54 struct Item *next; /* next item */
55 struct Menu *submenu; /* submenu spawned by clicking on item */
3bec05ea 56 Imlib_Image icon;
a7732690 57};
58
59/* menu structure */
60struct Menu {
d8a7caf2 61 struct Menu *parent; /* parent menu */
62 struct Item *caller; /* item that spawned the menu */
63 struct Item *list; /* list of items contained by the menu */
64 struct Item *selected; /* item currently selected in the menu */
65 int x, y, w, h; /* menu geometry */
66 unsigned level; /* menu level relative to root */
67 Drawable pixmap; /* pixmap to draw the menu on */
dbeb9940 68 XftDraw *draw;
d8a7caf2 69 Window win; /* menu window to map on the screen */
a7732690 70};
71
257e42cc 72/* functions declarations */
f644b8bc 73static void getresources(void);
257e42cc 74static void getcolor(const char *s, XftColor *color);
a7732690 75static void setupdc(void);
beae67a6 76static void calcgeom(struct Geometry *geom);
33376f54 77static struct Item *allocitem(const char *label, const char *output, char *file);
a7732690 78static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
33376f54 79static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file);
257e42cc 80static struct Menu *parsestdin(void);
685ca30d 81static Imlib_Image loadicon(const char *file, int size);
f8ffe0b2 82static void setupmenusize(struct Geometry *geom, struct Menu *menu);
83static void setupmenupos(struct Geometry *geom, struct Menu *menu);
3bec05ea 84static void setupmenu(struct Geometry *geom, struct Menu *menu, XClassHint *classh);
85003546 85static void grabpointer(void);
86static void grabkeyboard(void);
257e42cc 87static struct Menu *getmenu(struct Menu *currmenu, Window win);
88static struct Item *getitem(struct Menu *menu, int y);
89static void mapmenu(struct Menu *currmenu);
90static void drawseparator(struct Menu *menu, struct Item *item);
91static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
92static void drawmenu(struct Menu *currmenu);
93static struct Item *itemcycle(struct Menu *currmenu, int direction);
94static void run(struct Menu *currmenu);
ec4ed1ac 95static void freemenu(struct Menu *menu);
15dfafdf 96static void cleanup(void);
a7732690 97static void usage(void);
98
738ed9d5 99/* X stuff */
a7732690 100static Display *dpy;
c6d9174e 101static int screen;
dbeb9940 102static Visual *visual;
a7732690 103static Window rootwin;
c6d9174e 104static Colormap colormap;
a7732690 105static struct DC dc;
3bec05ea 106static Atom wmdelete;
107
108/* flags */
109static int wflag = 0; /* whether to let the window manager control XMenu */
a7732690 110
a7732690 111#include "config.h"
112
beae67a6 113/* xmenu: generate menu from stdin and print selected entry to stdout */
a7732690 114int
115main(int argc, char *argv[])
116{
257e42cc 117 struct Menu *rootmenu;
beae67a6 118 struct Geometry geom;
3bec05ea 119 XClassHint classh;
a7732690 120 int ch;
121
3bec05ea 122 while ((ch = getopt(argc, argv, "w")) != -1) {
a7732690 123 switch (ch) {
3bec05ea 124 case 'w':
125 wflag = 1;
126 break;
a7732690 127 default:
128 usage();
129 break;
130 }
131 }
132 argc -= optind;
133 argv += optind;
134
3bec05ea 135 if (argc > 1)
c6d9174e 136 usage();
137
a7732690 138 /* open connection to server and set X variables */
139 if ((dpy = XOpenDisplay(NULL)) == NULL)
140 errx(1, "cannot open display");
141 screen = DefaultScreen(dpy);
dbeb9940 142 visual = DefaultVisual(dpy, screen);
a7732690 143 rootwin = RootWindow(dpy, screen);
144 colormap = DefaultColormap(dpy, screen);
3bec05ea 145 wmdelete=XInternAtom(dpy, "WM_DELETE_WINDOW", True);
21a9aaec 146
33376f54 147 /* imlib2 stuff */
148 imlib_set_cache_size(2048 * 1024);
149 imlib_context_set_dither(1);
150 imlib_context_set_display(dpy);
151 imlib_context_set_visual(visual);
152 imlib_context_set_colormap(colormap);
153
a7732690 154 /* setup */
f644b8bc 155 getresources();
a7732690 156 setupdc();
beae67a6 157 calcgeom(&geom);
a7732690 158
3bec05ea 159 /* set window class */
160 classh.res_class = PROGNAME;
161 if (argc == 1)
162 classh.res_name = *argv;
163 else
164 classh.res_name = PROGNAME;
165
f8ffe0b2 166 /* generate menus and set them up */
257e42cc 167 rootmenu = parsestdin();
a7732690 168 if (rootmenu == NULL)
169 errx(1, "no menu generated");
3bec05ea 170 setupmenu(&geom, rootmenu, &classh);
a7732690 171
465d07db 172 /* grab mouse and keyboard */
3bec05ea 173 if (!wflag) {
174 grabpointer();
175 grabkeyboard();
176 }
465d07db 177
a7732690 178 /* run event loop */
257e42cc 179 run(rootmenu);
a7732690 180
15dfafdf 181 /* freeing stuff */
182 freemenu(rootmenu);
183 cleanup();
184
21a9aaec 185 return 0;
186}
187
f644b8bc 188/* read xrdb for configuration options */
189static void
190getresources(void)
191{
192 char *xrm;
193 long n;
fd530f3f 194 char *type;
195 XrmDatabase xdb;
196 XrmValue xval;
f644b8bc 197
198 XrmInitialize();
fd530f3f 199 if ((xrm = XResourceManagerString(dpy)) == NULL)
200 return;
201
202 xdb = XrmGetStringDatabase(xrm);
203
204 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
205 if ((n = strtol(xval.addr, NULL, 10)) > 0)
206 border_pixels = n;
207 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
208 if ((n = strtol(xval.addr, NULL, 10)) > 0)
209 separator_pixels = n;
685ca30d 210 if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True)
fd530f3f 211 if ((n = strtol(xval.addr, NULL, 10)) > 0)
685ca30d 212 height_pixels = n;
fd530f3f 213 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
214 if ((n = strtol(xval.addr, NULL, 10)) > 0)
215 width_pixels = n;
216 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
217 background_color = strdup(xval.addr);
218 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
219 foreground_color = strdup(xval.addr);
220 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
221 selbackground_color = strdup(xval.addr);
222 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
223 selforeground_color = strdup(xval.addr);
224 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
225 separator_color = strdup(xval.addr);
226 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
227 border_color = strdup(xval.addr);
228 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
229 font = strdup(xval.addr);
230
231 XrmDestroyDatabase(xdb);
f644b8bc 232}
233
a7732690 234/* get color from color string */
dbeb9940 235static void
236getcolor(const char *s, XftColor *color)
a7732690 237{
dbeb9940 238 if(!XftColorAllocName(dpy, visual, colormap, s, color))
a7732690 239 errx(1, "cannot allocate color: %s", s);
a7732690 240}
241
242/* init draw context */
243static void
244setupdc(void)
245{
246 /* get color pixels */
fd530f3f 247 getcolor(background_color, &dc.normal[ColorBG]);
248 getcolor(foreground_color, &dc.normal[ColorFG]);
249 getcolor(selbackground_color, &dc.selected[ColorBG]);
250 getcolor(selforeground_color, &dc.selected[ColorFG]);
251 getcolor(separator_color, &dc.separator);
252 getcolor(border_color, &dc.border);
a7732690 253
254 /* try to get font */
dbeb9940 255 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
a7732690 256 errx(1, "cannot load font");
a7732690 257
fd530f3f 258 /* create common GC */
a7732690 259 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 260}
261
257e42cc 262/* calculate menu and screen geometry */
a7732690 263static void
beae67a6 264calcgeom(struct Geometry *geom)
a7732690 265{
257e42cc 266 Window w1, w2; /* unused variables */
267 int a, b; /* unused variables */
268 unsigned mask; /* unused variable */
269
beae67a6 270 XQueryPointer(dpy, rootwin, &w1, &w2, &geom->cursx, &geom->cursy, &a, &b, &mask);
271 geom->screenw = DisplayWidth(dpy, screen);
272 geom->screenh = DisplayHeight(dpy, screen);
685ca30d 273 geom->itemh = height_pixels;
beae67a6 274 geom->itemw = width_pixels;
275 geom->border = border_pixels;
276 geom->separator = separator_pixels;
a7732690 277}
278
a7732690 279/* allocate an item */
280static struct Item *
33376f54 281allocitem(const char *label, const char *output, char *file)
a7732690 282{
283 struct Item *item;
284
285 if ((item = malloc(sizeof *item)) == NULL)
286 err(1, "malloc");
a5ddd2cd 287 if (label == NULL) {
7fbd1c5e 288 item->label = NULL;
289 item->output = NULL;
290 } else {
291 if ((item->label = strdup(label)) == NULL)
292 err(1, "strdup");
15dfafdf 293 if (label == output) {
294 item->output = item->label;
295 } else {
296 if ((item->output = strdup(output)) == NULL)
297 err(1, "strdup");
298 }
7fbd1c5e 299 }
33376f54 300 if (file == NULL) {
301 item->file = NULL;
302 } else {
303 if ((item->file = strdup(file)) == NULL)
304 err(1, "strdup");
305 }
a80fee22 306 item->y = 0;
beae67a6 307 item->h = 0;
f1583285 308 if (item->label == NULL)
309 item->labellen = 0;
310 else
311 item->labellen = strlen(item->label);
a7732690 312 item->next = NULL;
313 item->submenu = NULL;
3bec05ea 314 item->icon = NULL;
a7732690 315
316 return item;
317}
318
319/* allocate a menu */
320static struct Menu *
321allocmenu(struct Menu *parent, struct Item *list, unsigned level)
322{
323 XSetWindowAttributes swa;
324 struct Menu *menu;
325
326 if ((menu = malloc(sizeof *menu)) == NULL)
327 err(1, "malloc");
328 menu->parent = parent;
329 menu->list = list;
d888f2ca 330 menu->caller = NULL;
a7732690 331 menu->selected = NULL;
f8ffe0b2 332 menu->w = 0; /* calculated by setupmenu() */
333 menu->h = 0; /* calculated by setupmenu() */
334 menu->x = 0; /* calculated by setupmenu() */
335 menu->y = 0; /* calculated by setupmenu() */
a7732690 336 menu->level = level;
a7732690 337
3bec05ea 338 swa.override_redirect = (wflag) ? False : True;
fd530f3f 339 swa.background_pixel = dc.normal[ColorBG].pixel;
340 swa.border_pixel = dc.border.pixel;
341 swa.save_under = True; /* pop-up windows should save_under*/
a7732690 342 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 343 | PointerMotionMask | LeaveWindowMask;
3bec05ea 344 if (wflag)
345 swa.event_mask |= StructureNotifyMask;
beae67a6 346 menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0,
a7732690 347 CopyFromParent, CopyFromParent, CopyFromParent,
fd530f3f 348 CWOverrideRedirect | CWBackPixel |
349 CWBorderPixel | CWEventMask | CWSaveUnder,
a7732690 350 &swa);
351
3bec05ea 352 XSetWMProtocols(dpy, menu->win, &wmdelete, 1);
353
a7732690 354 return menu;
355}
356
a5ddd2cd 357/* build the menu tree */
358static struct Menu *
33376f54 359buildmenutree(unsigned level, const char *label, const char *output, char *file)
a5ddd2cd 360{
361 static struct Menu *prevmenu = NULL; /* menu the previous item was added to */
362 static struct Menu *rootmenu = NULL; /* menu to be returned */
363 struct Item *curritem = NULL; /* item currently being read */
364 struct Item *item; /* dummy item for loops */
365 struct Menu *menu; /* dummy menu for loops */
366 unsigned i;
367
368 /* create the item */
33376f54 369 curritem = allocitem(label, output, file);
a5ddd2cd 370
371 /* put the item in the menu tree */
372 if (prevmenu == NULL) { /* there is no menu yet */
685ca30d 373 menu = allocmenu(NULL, curritem, level);
374 rootmenu = menu;
375 prevmenu = menu;
376 curritem->prev = NULL;
a5ddd2cd 377 } else if (level < prevmenu->level) { /* item is continuation of a parent menu */
378 /* go up the menu tree until find the menu this item continues */
379 for (menu = prevmenu, i = level;
380 menu != NULL && i != prevmenu->level;
381 menu = menu->parent, i++)
382 ;
383 if (menu == NULL)
384 errx(1, "reached NULL menu");
385
386 /* find last item in the new menu */
387 for (item = menu->list; item->next != NULL; item = item->next)
388 ;
389
390 prevmenu = menu;
391 item->next = curritem;
392 curritem->prev = item;
393 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
394 /* find last item in the previous menu */
395 for (item = prevmenu->list; item->next != NULL; item = item->next)
396 ;
397
398 item->next = curritem;
399 curritem->prev = item;
400 } else if (level > prevmenu->level) { /* item begins a new menu */
401 menu = allocmenu(prevmenu, curritem, level);
402
403 /* find last item in the previous menu */
404 for (item = prevmenu->list; item->next != NULL; item = item->next)
405 ;
406
407 prevmenu = menu;
408 menu->caller = item;
409 item->submenu = menu;
410 curritem->prev = NULL;
411 }
412
413 return rootmenu;
414}
415
a7732690 416/* create menus and items from the stdin */
257e42cc 417static struct Menu *
a7732690 418parsestdin(void)
419{
a5ddd2cd 420 struct Menu *rootmenu;
a7732690 421 char *s, buf[BUFSIZ];
33376f54 422 char *file, *label, *output;
a7732690 423 unsigned level = 0;
257e42cc 424
ddb15acf 425 rootmenu = NULL;
426
a7732690 427 while (fgets(buf, BUFSIZ, stdin) != NULL) {
a5ddd2cd 428 /* get the indentation level */
429 level = strspn(buf, "\t");
a7732690 430
a5ddd2cd 431 /* get the label */
432 s = level + buf;
433 label = strtok(s, "\t\n");
a7732690 434
33376f54 435 /* get the filename */
436 file = NULL;
437 if (label != NULL && strncmp(label, "IMG:", 4) == 0) {
438 file = label + 4;
439 label = strtok(NULL, "\t\n");
440 }
441
a5ddd2cd 442 /* get the output */
443 output = strtok(NULL, "\n");
444 if (output == NULL) {
445 output = label;
446 } else {
447 while (*output == '\t')
448 output++;
a7732690 449 }
a5ddd2cd 450
33376f54 451 rootmenu = buildmenutree(level, label, output, file);
a7732690 452 }
a7732690 453
257e42cc 454 return rootmenu;
a7732690 455}
456
3bec05ea 457/* load and scale icon */
33376f54 458static Imlib_Image
3bec05ea 459loadicon(const char *file, int size)
33376f54 460{
3bec05ea 461 Imlib_Image icon;
33376f54 462 int width;
463 int height;
464 int imgsize;
465
3bec05ea 466 icon = imlib_load_image(file);
467 if (icon == NULL)
468 errx(1, "cannot load icon %s", file);
33376f54 469
3bec05ea 470 imlib_context_set_image(icon);
33376f54 471
472 width = imlib_image_get_width();
473 height = imlib_image_get_height();
474 imgsize = MIN(width, height);
475
3bec05ea 476 icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize, size, size);
33376f54 477
3bec05ea 478 return icon;
33376f54 479}
480
f8ffe0b2 481/* setup the size of a menu and the position of its items */
a7732690 482static void
f8ffe0b2 483setupmenusize(struct Geometry *geom, struct Menu *menu)
a7732690 484{
dbeb9940 485 XGlyphInfo ext;
a80fee22 486 struct Item *item;
f1583285 487 int labelwidth;
a7732690 488
beae67a6 489 menu->w = geom->itemw;
a80fee22 490 for (item = menu->list; item != NULL; item = item->next) {
491 item->y = menu->h;
beae67a6 492
7fbd1c5e 493 if (item->label == NULL) /* height for separator item */
beae67a6 494 item->h = geom->separator;
a80fee22 495 else
beae67a6 496 item->h = geom->itemh;
497 menu->h += item->h;
f1583285 498
f8ffe0b2 499 /* get length of item->label rendered in the font */
dbeb9940 500 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
501 item->labellen, &ext);
685ca30d 502
503 /* set menu width */
504 labelwidth = ext.xOff + item->h * 2;
f1583285 505 menu->w = MAX(menu->w, labelwidth);
33376f54 506
3bec05ea 507 /* create icon */
33376f54 508 if (item->file != NULL)
685ca30d 509 item->icon = loadicon(item->file, item->h - iconpadding * 2);
a80fee22 510 }
f8ffe0b2 511}
512
513/* setup the position of a menu */
514static void
3bec05ea 515setupmenupos(struct Geometry *g, struct Menu *menu)
f8ffe0b2 516{
3bec05ea 517 static struct Geometry *geom = NULL;
f8ffe0b2 518 int width, height;
a7732690 519
3bec05ea 520 /*
521 * Save the geometry so functions can call setupmenupos() without
522 * having to know the geometry.
523 */
524 if (g != NULL)
525 geom = g;
526
beae67a6 527 width = menu->w + geom->border * 2;
528 height = menu->h + geom->border * 2;
a7732690 529 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
beae67a6 530 if (geom->screenw - geom->cursx >= menu->w)
531 menu->x = geom->cursx;
532 else if (geom->cursx > width)
533 menu->x = geom->cursx - width;
534
535 if (geom->screenh - geom->cursy >= height)
536 menu->y = geom->cursy;
537 else if (geom->screenh > height)
538 menu->y = geom->screenh - height;
a7732690 539 } else { /* else, calculate in respect to parent menu */
beae67a6 540 if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width)
541 menu->x = menu->parent->x + menu->parent->w + geom->border;
542 else if (menu->parent->x > menu->w + geom->border)
543 menu->x = menu->parent->x - menu->w - geom->border;
a7732690 544
beae67a6 545 if (geom->screenh - (menu->caller->y + menu->parent->y) > height)
8455c369 546 menu->y = menu->caller->y + menu->parent->y;
beae67a6 547 else if (geom->screenh - menu->parent->y > height)
a7732690 548 menu->y = menu->parent->y;
beae67a6 549 else if (geom->screenh > height)
550 menu->y = geom->screenh - height;
a7732690 551 }
f8ffe0b2 552}
553
554/* recursivelly setup menu configuration and its pixmap */
555static void
3bec05ea 556setupmenu(struct Geometry *geom, struct Menu *menu, XClassHint *classh)
f8ffe0b2 557{
558 struct Item *item;
f8ffe0b2 559 XWindowChanges changes;
560 XSizeHints sizeh;
3bec05ea 561 XTextProperty wintitle;
f8ffe0b2 562
563 /* setup size and position of menus */
564 setupmenusize(geom, menu);
565 setupmenupos(geom, menu);
a7732690 566
567 /* update menu geometry */
beae67a6 568 changes.border_width = geom->border;
a7732690 569 changes.height = menu->h;
f1583285 570 changes.width = menu->w;
a7732690 571 changes.x = menu->x;
572 changes.y = menu->y;
beae67a6 573 XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 574
3bec05ea 575 /* set window title (used if wflag is on) */
576 if (menu->parent == NULL) {
577 XStringListToTextProperty(&classh->res_name, 1, &wintitle);
578 } else {
579 XStringListToTextProperty(&menu->caller->output, 1, &wintitle);
580 }
581
d2435fcd 582 /* set window manager hints */
09c13122 583 sizeh.flags = PMaxSize | PMinSize;
584 sizeh.min_width = sizeh.max_width = menu->w;
585 sizeh.min_height = sizeh.max_height = menu->h;
3bec05ea 586 XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh);
09c13122 587
dbeb9940 588 /* create pixmap and XftDraw */
f15fc339 589 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
590 DefaultDepth(dpy, screen));
dbeb9940 591 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 592
a80fee22 593 /* calculate positions of submenus */
a7732690 594 for (item = menu->list; item != NULL; item = item->next) {
595 if (item->submenu != NULL)
3bec05ea 596 setupmenu(geom, item->submenu, classh);
a7732690 597 }
598}
599
85003546 600/* try to grab pointer, we may have to wait for another process to ungrab */
601static void
602grabpointer(void)
603{
604 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
605 int i;
606
607 for (i = 0; i < 1000; i++) {
608 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
609 GrabModeAsync, GrabModeAsync, None,
610 None, CurrentTime) == GrabSuccess)
611 return;
612 nanosleep(&ts, NULL);
613 }
614 errx(1, "cannot grab keyboard");
615}
616
617/* try to grab keyboard, we may have to wait for another process to ungrab */
618static void
619grabkeyboard(void)
620{
621 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
622 int i;
623
624 for (i = 0; i < 1000; i++) {
625 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
626 GrabModeAsync, CurrentTime) == GrabSuccess)
627 return;
628 nanosleep(&ts, NULL);
629 }
630 errx(1, "cannot grab keyboard");
631}
632
0c1a7864 633/* get menu of given window */
634static struct Menu *
257e42cc 635getmenu(struct Menu *currmenu, Window win)
a7732690 636{
0c1a7864 637 struct Menu *menu;
a7732690 638
0c1a7864 639 for (menu = currmenu; menu != NULL; menu = menu->parent)
640 if (menu->win == win)
641 return menu;
a7732690 642
0c1a7864 643 return NULL;
644}
645
646/* get item of given menu and position */
647static struct Item *
648getitem(struct Menu *menu, int y)
649{
650 struct Item *item;
a7732690 651
0c1a7864 652 if (menu == NULL)
653 return NULL;
654
655 for (item = menu->list; item != NULL; item = item->next)
656 if (y >= item->y && y <= item->y + item->h)
657 return item;
658
659 return NULL;
a7732690 660}
661
257e42cc 662/* umap previous menus and map current menu and its parents */
a7732690 663static void
257e42cc 664mapmenu(struct Menu *currmenu)
a7732690 665{
257e42cc 666 static struct Menu *prevmenu = NULL;
d8a7caf2 667 struct Menu *menu, *menu_;
d8a7caf2 668 struct Menu *lcamenu; /* lowest common ancestor menu */
669 unsigned minlevel; /* level of the closest to root menu */
670 unsigned maxlevel; /* level of the closest to root menu */
a7732690 671
257e42cc 672 /* do not remap current menu if it wasn't updated*/
673 if (prevmenu == currmenu)
a7732690 674 return;
675
257e42cc 676 /* if this is the first time mapping, skip calculations */
677 if (prevmenu == NULL) {
fd530f3f 678 XMapWindow(dpy, currmenu->win);
cdc4d51f 679 prevmenu = currmenu;
680 return;
27443900 681 }
682
d8a7caf2 683 /* find lowest common ancestor menu */
257e42cc 684 minlevel = MIN(currmenu->level, prevmenu->level);
685 maxlevel = MAX(currmenu->level, prevmenu->level);
686 if (currmenu->level == maxlevel) {
27443900 687 menu = currmenu;
257e42cc 688 menu_ = prevmenu;
689 } else {
690 menu = prevmenu;
691 menu_ = currmenu;
27443900 692 }
693 while (menu->level > minlevel)
694 menu = menu->parent;
695 while (menu != menu_) {
696 menu = menu->parent;
697 menu_ = menu_->parent;
d8a7caf2 698 }
27443900 699 lcamenu = menu;
d8a7caf2 700
873c080c 701 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
257e42cc 702 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
8455c369 703 menu->selected = NULL;
a7732690 704 XUnmapWindow(dpy, menu->win);
705 }
706
873c080c 707 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 708 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
3bec05ea 709
710 if (wflag) {
711 setupmenupos(NULL, menu);
712 XMoveWindow(dpy, menu->win, menu->x, menu->y);
713 }
714
a7732690 715 XMapWindow(dpy, menu->win);
fd530f3f 716 }
257e42cc 717
257e42cc 718 prevmenu = currmenu;
fd530f3f 719}
720
721/* draw separator item */
722static void
723drawseparator(struct Menu *menu, struct Item *item)
724{
6b5123e7 725 int y;
fd530f3f 726
6b5123e7 727 y = item->y + item->h/2;
fd530f3f 728
729 XSetForeground(dpy, dc.gc, dc.separator.pixel);
6b5123e7 730 XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
fd530f3f 731}
732
733/* draw regular item */
734static void
735drawitem(struct Menu *menu, struct Item *item, XftColor *color)
736{
737 int x, y;
738
685ca30d 739 x = item->h;
740 y = item->y + (item->h + dc.font->ascent) / 2;
fd530f3f 741 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
742 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
743 x, y, item->label, item->labellen);
744
745 /* draw triangle, if item contains a submenu */
746 if (item->submenu != NULL) {
685ca30d 747 x = menu->w - (item->h + triangle_width + 1) / 2;
748 y = item->y + (item->h - triangle_height + 1) / 2;
fd530f3f 749
750 XPoint triangle[] = {
751 {x, y},
752 {x + triangle_width, y + triangle_height/2},
753 {x, y + triangle_height},
754 {x, y}
755 };
756
757 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
758 Convex, CoordModeOrigin);
d888f2ca 759 }
33376f54 760
3bec05ea 761 /* draw icon */
33376f54 762 if (item->file != NULL) {
685ca30d 763 x = iconpadding;
764 y = item->y + iconpadding;
33376f54 765 imlib_context_set_drawable(menu->pixmap);
3bec05ea 766 imlib_context_set_image(item->icon);
33376f54 767 imlib_render_image_on_drawable(x, y);
768 }
a7732690 769}
770
771/* draw items of the current menu and of its ancestors */
772static void
257e42cc 773drawmenu(struct Menu *currmenu)
a7732690 774{
775 struct Menu *menu;
776 struct Item *item;
777
778 for (menu = currmenu; menu != NULL; menu = menu->parent) {
a7732690 779 for (item = menu->list; item != NULL; item = item->next) {
dbeb9940 780 XftColor *color;
a7732690 781
782 /* determine item color */
fd530f3f 783 if (item == menu->selected && item->label != NULL)
f644b8bc 784 color = dc.selected;
a7732690 785 else
f644b8bc 786 color = dc.normal;
787
a7732690 788 /* draw item box */
dbeb9940 789 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
f15fc339 790 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
f1583285 791 menu->w, item->h);
7fbd1c5e 792
fd530f3f 793 if (item->label == NULL) /* item is a separator */
794 drawseparator(menu, item);
795 else /* item is a regular item */
796 drawitem(menu, item, color);
a7732690 797 }
fd530f3f 798
799 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
800 menu->w, menu->h, 0, 0);
a7732690 801 }
802}
803
858338d9 804/* cycle through the items; non-zero direction is next, zero is prev */
805static struct Item *
257e42cc 806itemcycle(struct Menu *currmenu, int direction)
858338d9 807{
808 struct Item *item;
809 struct Item *lastitem;
810
811 item = NULL;
812
813 if (direction == ITEMNEXT) {
814 if (currmenu->selected == NULL)
815 item = currmenu->list;
816 else if (currmenu->selected->next != NULL)
817 item = currmenu->selected->next;
818
819 while (item != NULL && item->label == NULL)
820 item = item->next;
821
822 if (item == NULL)
823 item = currmenu->list;
824 } else {
825 for (lastitem = currmenu->list;
826 lastitem != NULL && lastitem->next != NULL;
827 lastitem = lastitem->next)
828 ;
829
830 if (currmenu->selected == NULL)
831 item = lastitem;
832 else if (currmenu->selected->prev != NULL)
833 item = currmenu->selected->prev;
834
835 while (item != NULL && item->label == NULL)
836 item = item->prev;
837
838 if (item == NULL)
839 item = lastitem;
840 }
841
842 return item;
843}
844
a7732690 845/* run event loop */
846static void
257e42cc 847run(struct Menu *currmenu)
a7732690 848{
849 struct Menu *menu;
850 struct Item *item;
851 struct Item *previtem = NULL;
858338d9 852 KeySym ksym;
a7732690 853 XEvent ev;
854
257e42cc 855 mapmenu(currmenu);
fd530f3f 856
a7732690 857 while (!XNextEvent(dpy, &ev)) {
858 switch(ev.type) {
859 case Expose:
858338d9 860 if (ev.xexpose.count == 0)
257e42cc 861 drawmenu(currmenu);
a7732690 862 break;
863 case MotionNotify:
257e42cc 864 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 865 item = getitem(menu, ev.xbutton.y);
c6d9174e 866 if (menu == NULL || item == NULL || previtem == item)
fd530f3f 867 break;
c6d9174e 868 previtem = item;
869 menu->selected = item;
870 if (item->submenu != NULL) {
871 currmenu = item->submenu;
872 currmenu->selected = NULL;
873 } else {
874 currmenu = menu;
a7732690 875 }
c6d9174e 876 mapmenu(currmenu);
877 drawmenu(currmenu);
a7732690 878 break;
879 case ButtonRelease:
257e42cc 880 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 881 item = getitem(menu, ev.xbutton.y);
fd530f3f 882 if (menu == NULL || item == NULL)
858338d9 883 break;
fd530f3f 884selectitem:
885 if (item->label == NULL)
886 break; /* ignore separators */
887 if (item->submenu != NULL) {
257e42cc 888 currmenu = item->submenu;
a7732690 889 } else {
fd530f3f 890 printf("%s\n", item->output);
08f16589 891 return;
a7732690 892 }
257e42cc 893 mapmenu(currmenu);
fd530f3f 894 currmenu->selected = currmenu->list;
257e42cc 895 drawmenu(currmenu);
fd530f3f 896 break;
858338d9 897 case ButtonPress:
257e42cc 898 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 899 if (menu == NULL)
858338d9 900 return;
901 break;
902 case KeyPress:
903 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
904
0c1a7864 905 /* esc closes xmenu when current menu is the root menu */
257e42cc 906 if (ksym == XK_Escape && currmenu->parent == NULL)
858338d9 907 return;
908
909 /* Shift-Tab = ISO_Left_Tab */
910 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
911 ksym = XK_ISO_Left_Tab;
912
913 /* cycle through menu */
914 item = NULL;
915 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
257e42cc 916 item = itemcycle(currmenu, ITEMPREV);
858338d9 917 } else if (ksym == XK_Tab || ksym == XK_Down) {
257e42cc 918 item = itemcycle(currmenu, ITEMNEXT);
858338d9 919 } else if ((ksym == XK_Return || ksym == XK_Right) &&
920 currmenu->selected != NULL) {
921 item = currmenu->selected;
922 goto selectitem;
923 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
924 currmenu->parent != NULL) {
925 item = currmenu->parent->selected;
257e42cc 926 currmenu = currmenu->parent;
927 mapmenu(currmenu);
858338d9 928 } else
929 break;
930 currmenu->selected = item;
257e42cc 931 drawmenu(currmenu);
a7732690 932 break;
f15fc339 933 case LeaveNotify:
fd530f3f 934 previtem = NULL;
f15fc339 935 currmenu->selected = NULL;
257e42cc 936 drawmenu(currmenu);
f15fc339 937 break;
3bec05ea 938 case ConfigureNotify:
939 menu = getmenu(currmenu, ev.xconfigure.window);
940 if (menu == NULL)
941 break;
942 menu->x = ev.xconfigure.x;
943 menu->y = ev.xconfigure.y;
944 break;
945 case ClientMessage:
946 /* user closed window */
947 menu = getmenu(currmenu, ev.xclient.window);
948 if (menu->parent == NULL)
949 return; /* closing the root menu closes the program */
950 currmenu = menu->parent;
951 mapmenu(currmenu);
952 break;
a7732690 953 }
954 }
955}
956
fd530f3f 957/* recursivelly free pixmaps and destroy windows */
f15fc339 958static void
ec4ed1ac 959freemenu(struct Menu *menu)
f15fc339 960{
961 struct Item *item;
ec4ed1ac 962 struct Item *tmp;
f15fc339 963
ec4ed1ac 964 item = menu->list;
965 while (item != NULL) {
f15fc339 966 if (item->submenu != NULL)
ec4ed1ac 967 freemenu(item->submenu);
968 tmp = item;
15dfafdf 969 if (tmp->label != tmp->output)
970 free(tmp->label);
6b5123e7 971 free(tmp->output);
33376f54 972 if (tmp->file != NULL) {
973 free(tmp->file);
3bec05ea 974 if (tmp->icon != NULL) {
975 imlib_context_set_image(tmp->icon);
33376f54 976 imlib_free_image();
977 }
978 }
979 item = item->next;
ec4ed1ac 980 free(tmp);
981 }
f15fc339 982
983 XFreePixmap(dpy, menu->pixmap);
dbeb9940 984 XftDrawDestroy(menu->draw);
f15fc339 985 XDestroyWindow(dpy, menu->win);
ec4ed1ac 986 free(menu);
f15fc339 987}
988
a7732690 989/* cleanup and exit */
990static void
15dfafdf 991cleanup(void)
a7732690 992{
257e42cc 993 XUngrabPointer(dpy, CurrentTime);
994 XUngrabKeyboard(dpy, CurrentTime);
995
dbeb9940 996 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
997 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
998 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
999 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 1000 XftColorFree(dpy, visual, colormap, &dc.separator);
1001 XftColorFree(dpy, visual, colormap, &dc.border);
dbeb9940 1002
f15fc339 1003 XFreeGC(dpy, dc.gc);
a7732690 1004 XCloseDisplay(dpy);
a7732690 1005}
1006
1007/* show usage */
1008static void
1009usage(void)
1010{
3bec05ea 1011 (void)fprintf(stderr, "usage: xmenu [-w] [title]\n");
a7732690 1012 exit(1);
1013}