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