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