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