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