Minor change to `Makefile` to set FreeBSD paths as default.
[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>
f472bfac 8#include <locale.h>
8902c43b 9#include <time.h>
a7732690 10#include <unistd.h>
11#include <X11/Xlib.h>
8902c43b 12#include <X11/Xatom.h>
a7732690 13#include <X11/Xutil.h>
f644b8bc 14#include <X11/Xresource.h>
858338d9 15#include <X11/XKBlib.h>
dbeb9940 16#include <X11/Xft/Xft.h>
237da982 17#include <X11/extensions/Xinerama.h>
33376f54 18#include <Imlib2.h>
45b2e22f 19
20/* macros */
523b3d5e 21#define MAXPATHS 128 /* maximal number of paths to look for icons */
22#define ICONPATH "ICONPATH" /* environment variable name */
45b2e22f 23#define CLASS "XMenu"
24#define LEN(x) (sizeof (x) / sizeof (x[0]))
45b2e22f 25#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
26#define GETNUM(n, s) { \
27 unsigned long __TMP__; \
28 if ((__TMP__ = strtoul((s), NULL, 10)) < INT_MAX) \
29 (n) = __TMP__; \
30 }
31
32/* Actions for the main loop */
33enum {
34 ACTION_NOP = 0,
35 ACTION_CLEAR = 1<<0, /* clear text */
36 ACTION_SELECT = 1<<1, /* select item */
37 ACTION_MAP = 1<<2, /* remap menu windows */
38 ACTION_DRAW = 1<<3, /* redraw menu windows */
39 ACTION_WARP = 1<<4, /* warp the pointer */
40};
41
42/* enum for keyboard menu navigation */
43enum { ITEMPREV, ITEMNEXT, ITEMFIRST, ITEMLAST };
44
45/* enum for text alignment */
46enum {LeftAlignment, CenterAlignment, RightAlignment};
47
48/* color enum */
49enum {ColorFG, ColorBG, ColorLast};
50
51/* EWMH atoms */
52enum {NetWMName, NetWMWindowType, NetWMWindowTypePopupMenu, NetLast};
53
54/* configuration structure */
55struct Config {
56 /* the values below are set by config.h */
57 const char *font;
58 const char *background_color;
59 const char *foreground_color;
60 const char *selbackground_color;
61 const char *selforeground_color;
62 const char *separator_color;
63 const char *border_color;
64 int width_pixels;
65 int height_pixels;
66 int border_pixels;
523b3d5e 67 int max_items;
45b2e22f 68 int separator_pixels;
69 int gap_pixels;
70 int triangle_width;
71 int triangle_height;
72 int iconpadding;
73 int horzpadding;
74 int alignment;
75
76 /* the values below are set by options */
77 int monitor;
78 int posx, posy; /* rootmenu position */
79
80 /* the value below is computed by xmenu */
81 int iconsize;
82};
83
84/* draw context structure */
85struct DC {
86 XftColor normal[ColorLast];
87 XftColor selected[ColorLast];
88 XftColor border;
89 XftColor separator;
90
91 GC gc;
92
93 FcPattern *pattern;
94 XftFont **fonts;
95 size_t nfonts;
96};
97
98/* menu item structure */
99struct Item {
100 char *label; /* string to be drawed on menu */
101 char *output; /* string to be outputed when item is clicked */
102 char *file; /* filename of the icon */
103 int y; /* item y position relative to menu */
104 int h; /* item height */
105 int textw; /* text width */
106 struct Item *prev; /* previous item */
107 struct Item *next; /* next item */
108 struct Menu *submenu; /* submenu spawned by clicking on item */
109 Drawable sel, unsel; /* pixmap for selected and unselected item */
110 Imlib_Image icon;
111};
112
113/* monitor geometry structure */
114struct Monitor {
115 int x, y, w, h; /* monitor geometry */
116};
117
118/* menu structure */
119struct Menu {
120 struct Menu *parent; /* parent menu */
121 struct Item *caller; /* item that spawned the menu */
122 struct Item *list; /* list of items contained by the menu */
523b3d5e 123 struct Item *first; /* first item displayed on the menu */
45b2e22f 124 struct Item *selected; /* item currently selected in the menu */
125 int x, y, w, h; /* menu geometry */
126 int hasicon; /* whether the menu has item with icons */
127 int drawn; /* whether the menu was already drawn */
128 int maxtextw; /* maximum text width */
523b3d5e 129 int level; /* menu level relative to root */
130 int overflow; /* whether the menu is higher than the monitor */
45b2e22f 131 Window win; /* menu window to map on the screen */
132 XIC xic; /* input context */
133};
858338d9 134
738ed9d5 135/* X stuff */
a7732690 136static Display *dpy;
dbeb9940 137static Visual *visual;
a7732690 138static Window rootwin;
c6d9174e 139static Colormap colormap;
6abae763 140static XrmDatabase xdb;
45b2e22f 141static XClassHint classh;
6abae763 142static char *xrm;
a7732690 143static struct DC dc;
8902c43b 144static Atom utf8string;
3bec05ea 145static Atom wmdelete;
8902c43b 146static Atom netatom[NetLast];
523b3d5e 147static int screen;
148static int depth;
f472bfac 149static XIM xim;
3bec05ea 150
151/* flags */
45b2e22f 152static int iflag = 0; /* whether to disable icons */
153static int rflag = 0; /* whether to disable right-click */
154static int mflag = 0; /* whether the user specified a monitor with -p */
155static int pflag = 0; /* whether the user specified a position with -p */
156static int wflag = 0; /* whether to let the window manager control XMenu */
157static int rootmodeflag = 0; /* wheter to run in root mode */
158static int passclickflag = 0; /* whether to pass click to root window */
159static int firsttime = 1; /* set to 0 after first run */
160
161/* arguments */
162static unsigned int button = 0; /* button to trigger pmenu in root mode */
163static unsigned int modifier = 0; /* modifier to trigger pmenu */
a7732690 164
523b3d5e 165/* icons paths */
166static char *iconstring = NULL; /* string read from getenv */
167static char *iconpaths[MAXPATHS]; /* paths to icon directories */
168static int niconpaths = 0; /* number of paths to icon directories */
169
8902c43b 170/* include config variable */
a7732690 171#include "config.h"
172
73a2a23d 173/* show usage */
174static void
175usage(void)
a7732690 176{
288a35e5
LS
177
178 (void)fprintf(stderr, "usage: xmenu [-irw] [-p position] [(-x|-X) [modifier-]button] [title]\n");
73a2a23d 179 exit(1);
21a9aaec 180}
181
523b3d5e 182/* maximum int */
183static int
184max(int x, int y)
185{
186 return x > y ? x : y;
187}
188
189/* minimum int */
190static int
191min(int x, int y)
192{
193 return x < y ? x : y;
194}
195
196/* call malloc checking for error */
197static void *
198emalloc(size_t size)
199{
200 void *p;
201
202 if ((p = malloc(size)) == NULL)
203 err(1, "malloc");
204 return p;
205}
206
207/* call strdup checking for error */
208static char *
209estrdup(const char *s)
210{
211 char *t;
212
213 if ((t = strdup(s)) == NULL)
214 err(1, "strdup");
215 return t;
216}
217
218/* parse position string from -p, put results on config.posx, config.posy, and config.monitor */
05cfe1a0 219static void
237da982 220parseposition(char *optarg)
05cfe1a0 221{
222 long n;
237da982 223 char *s = optarg;
05cfe1a0 224 char *endp;
225
226 n = strtol(s, &endp, 10);
227 if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x')
228 goto error;
229 config.posx = n;
230 s = endp+1;
231 n = strtol(s, &endp, 10);
237da982 232 if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s)
05cfe1a0 233 goto error;
234 config.posy = n;
237da982 235 if (*endp == ':') {
236 s = endp+1;
237 mflag = 1;
238 if (strncasecmp(s, "CUR", 3) == 0) {
239 config.monitor = -1;
240 endp = s+3;
241 } else {
242 n = strtol(s, &endp, 10);
243 if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0')
244 goto error;
245 config.monitor = n;
246 }
247 } else if (*endp != '\0') {
248 goto error;
249 }
05cfe1a0 250
251 return;
252
253error:
254 errx(1, "improper position: %s", optarg);
255}
256
6abae763 257/* get configuration from X resources */
258static void
259getresources(void)
260{
261 char *type;
262 XrmValue xval;
263
264 if (xrm == NULL || xdb == NULL)
265 return;
266
267 if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
268 GETNUM(config.border_pixels, xval.addr)
269 if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
270 GETNUM(config.separator_pixels, xval.addr)
271 if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True)
272 GETNUM(config.height_pixels, xval.addr)
273 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
274 GETNUM(config.width_pixels, xval.addr)
275 if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True)
276 GETNUM(config.gap_pixels, xval.addr)
523b3d5e 277 if (XrmGetResource(xdb, "xmenu.maxItems", "*", &type, &xval) == True)
278 GETNUM(config.max_items, xval.addr)
6abae763 279 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
280 config.background_color = xval.addr;
281 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
282 config.foreground_color = xval.addr;
283 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
284 config.selbackground_color = xval.addr;
285 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
286 config.selforeground_color = xval.addr;
287 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
288 config.separator_color = xval.addr;
289 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
290 config.border_color = xval.addr;
291 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
292 config.font = xval.addr;
293 if (XrmGetResource(xdb, "xmenu.alignment", "*", &type, &xval) == True) {
294 if (strcasecmp(xval.addr, "center") == 0)
295 config.alignment = CenterAlignment;
296 else if (strcasecmp(xval.addr, "left") == 0)
297 config.alignment = LeftAlignment;
298 else if (strcasecmp(xval.addr, "right") == 0)
299 config.alignment = RightAlignment;
300 }
301}
302
45b2e22f 303/* set button global variable */
304static void
305setbutton(char *s)
306{
307 size_t len;
308
309 if ((len = strlen(s)) < 1)
310 return;
311 switch (s[len-1]) {
312 case '1': button = Button1; break;
313 case '2': button = Button2; break;
314 case '3': button = Button3; break;
315 default: button = atoi(&s[len-1]); break;
316 }
317}
318
319/* set modifier global variable */
320static void
321setmodifier(char *s)
322{
323 size_t len;
324
325 if ((len = strlen(s)) < 1)
326 return;
327 switch (s[len-1]) {
328 case '1': modifier = Mod1Mask; break;
329 case '2': modifier = Mod2Mask; break;
330 case '3': modifier = Mod3Mask; break;
331 case '4': modifier = Mod4Mask; break;
332 case '5': modifier = Mod5Mask; break;
333 default:
334 if (strcasecmp(s, "Alt") == 0) {
335 modifier = Mod1Mask;
336 } else if (strcasecmp(s, "Super") == 0) {
337 modifier = Mod4Mask;
338 }
339 break;
340 }
341}
342
523b3d5e 343/* parse icon path string */
344static void
345parseiconpaths(char *s)
346{
347 if (s == NULL)
348 return;
349 free(iconstring);
350 iconstring = estrdup(s);
351 niconpaths = 0;
352 for (s = strtok(iconstring, ":"); s != NULL; s = strtok(NULL, ":")) {
353 if (niconpaths < MAXPATHS) {
354 iconpaths[niconpaths++] = s;
355 }
356 }
357}
358
6abae763 359/* get configuration from command-line options */
45b2e22f 360static void
6abae763 361getoptions(int argc, char *argv[])
362{
363 int ch;
45b2e22f 364 char *s, *t;
6abae763 365
45b2e22f 366 classh.res_class = CLASS;
367 classh.res_name = argv[0];
368 if ((s = strrchr(argv[0], '/')) != NULL)
369 classh.res_name = s + 1;
523b3d5e 370 parseiconpaths(getenv(ICONPATH));
45b2e22f 371 while ((ch = getopt(argc, argv, "ip:rwx:X:")) != -1) {
6abae763 372 switch (ch) {
373 case 'i':
374 iflag = 1;
375 break;
376 case 'p':
377 pflag = 1;
378 parseposition(optarg);
379 break;
380 case 'r':
381 rflag = 1;
382 break;
383 case 'w':
384 wflag = 1;
385 break;
45b2e22f 386 case 'X':
387 passclickflag = 1;
388 /* PASSTHROUGH */
389 case 'x':
390 rootmodeflag = 1;
391 s = optarg;
392 setbutton(s);
393 if ((t = strchr(s, '-')) == NULL)
394 return;
395 *t = '\0';
396 setmodifier(s);
397 break;
6abae763 398 default:
399 usage();
400 break;
401 }
402 }
523b3d5e 403 if (rootmodeflag)
404 wflag = 0;
6abae763 405 argc -= optind;
406 argv += optind;
45b2e22f 407 if (argc != 0)
6abae763 408 usage();
45b2e22f 409 return;
6abae763 410}
411
112e23c3 412/* parse font string */
cdeaefaa 413static void
414parsefonts(const char *s)
415{
416 const char *p;
417 char buf[1024];
418 size_t nfont = 0;
419
420 dc.nfonts = 1;
421 for (p = s; *p; p++)
422 if (*p == ',')
423 dc.nfonts++;
424
425 if ((dc.fonts = calloc(dc.nfonts, sizeof *dc.fonts)) == NULL)
426 err(1, "calloc");
427
428 p = s;
429 while (*p != '\0') {
430 size_t i;
431
432 i = 0;
433 while (isspace(*p))
434 p++;
70063f16 435 while (i < sizeof buf && *p != '\0' && *p != ',')
cdeaefaa 436 buf[i++] = *p++;
6d56f279 437 if (i >= sizeof buf)
438 errx(1, "font name too long");
cdeaefaa 439 if (*p == ',')
440 p++;
441 buf[i] = '\0';
6d56f279 442 if (nfont == 0)
443 if ((dc.pattern = FcNameParse((FcChar8 *)buf)) == NULL)
444 errx(1, "the first font in the cache must be loaded from a font string");
cdeaefaa 445 if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL)
8a7bfdf8 446 errx(1, "could not load font");
cdeaefaa 447 }
448}
449
8902c43b 450/* get color from color string */
451static void
452ealloccolor(const char *s, XftColor *color)
453{
454 if(!XftColorAllocName(dpy, visual, colormap, s, color))
8a7bfdf8 455 errx(1, "could not allocate color: %s", s);
8902c43b 456}
457
237da982 458/* query monitor information and cursor position */
459static void
45b2e22f 460getmonitor(struct Monitor *mon)
237da982 461{
462 XineramaScreenInfo *info = NULL;
463 Window dw; /* dummy variable */
464 int di; /* dummy variable */
465 unsigned du; /* dummy variable */
466 int cursx, cursy; /* cursor position */
467 int nmons;
468 int i;
469
470 XQueryPointer(dpy, rootwin, &dw, &dw, &cursx, &cursy, &di, &di, &du);
471
45b2e22f 472 mon->x = mon->y = 0;
473 mon->w = DisplayWidth(dpy, screen);
474 mon->h = DisplayHeight(dpy, screen);
237da982 475
476 if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) {
477 int selmon = 0;
478
70063f16 479 if (!mflag || config.monitor < 0 || config.monitor >= nmons) {
237da982 480 for (i = 0; i < nmons; i++) {
d2304ecf 481 if (BETWEEN(cursx, info[i].x_org, info[i].x_org + info[i].width) &&
482 BETWEEN(cursy, info[i].y_org, info[i].y_org + info[i].height)) {
237da982 483 selmon = i;
484 break;
485 }
486 }
487 } else {
488 selmon = config.monitor;
489 }
490
45b2e22f 491 mon->x = info[selmon].x_org;
492 mon->y = info[selmon].y_org;
493 mon->w = info[selmon].width;
494 mon->h = info[selmon].height;
883ec17f 495
496 XFree(info);
237da982 497 }
498
499 if (!pflag) {
500 config.posx = cursx;
501 config.posy = cursy;
502 } else if (mflag) {
45b2e22f 503 config.posx += mon->x;
504 config.posy += mon->y;
237da982 505 }
506}
507
a7732690 508/* init draw context */
509static void
8902c43b 510initdc(void)
a7732690 511{
512 /* get color pixels */
8902c43b 513 ealloccolor(config.background_color, &dc.normal[ColorBG]);
514 ealloccolor(config.foreground_color, &dc.normal[ColorFG]);
515 ealloccolor(config.selbackground_color, &dc.selected[ColorBG]);
516 ealloccolor(config.selforeground_color, &dc.selected[ColorFG]);
517 ealloccolor(config.separator_color, &dc.separator);
518 ealloccolor(config.border_color, &dc.border);
a7732690 519
cdeaefaa 520 /* parse fonts */
521 parsefonts(config.font);
a7732690 522
fd530f3f 523 /* create common GC */
a7732690 524 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 525}
526
b6cf4847 527/* calculate icon size */
a7732690 528static void
b6cf4847 529initiconsize(void)
a7732690 530{
71b4db92 531 config.iconsize = config.height_pixels - config.iconpadding * 2;
8902c43b 532}
533
534/* intern atoms */
535static void
536initatoms(void)
537{
538 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
539 wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
540 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
541 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
542 netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
a7732690 543}
544
a7732690 545/* allocate an item */
546static struct Item *
33376f54 547allocitem(const char *label, const char *output, char *file)
a7732690 548{
549 struct Item *item;
550
523b3d5e 551 item = emalloc(sizeof *item);
a5ddd2cd 552 if (label == NULL) {
7fbd1c5e 553 item->label = NULL;
554 item->output = NULL;
555 } else {
523b3d5e 556 item->label = estrdup(label);
15dfafdf 557 if (label == output) {
558 item->output = item->label;
559 } else {
523b3d5e 560 item->output = estrdup(output);
15dfafdf 561 }
7fbd1c5e 562 }
33376f54 563 if (file == NULL) {
564 item->file = NULL;
565 } else {
523b3d5e 566 item->file = estrdup(file);
33376f54 567 }
a80fee22 568 item->y = 0;
beae67a6 569 item->h = 0;
a7732690 570 item->next = NULL;
571 item->submenu = NULL;
3bec05ea 572 item->icon = NULL;
a7732690 573
574 return item;
575}
576
a3a73837 577/* allocate a menu and create its window */
a7732690 578static struct Menu *
523b3d5e 579allocmenu(struct Menu *parent, struct Item *list, int level)
a7732690 580{
581 XSetWindowAttributes swa;
582 struct Menu *menu;
583
523b3d5e 584 menu = emalloc(sizeof *menu);
585 *menu = (struct Menu){
586 .parent = parent,
587 .list = list,
588 .level = level,
589 .first = NULL,
590 };
a7732690 591
3bec05ea 592 swa.override_redirect = (wflag) ? False : True;
fd530f3f 593 swa.background_pixel = dc.normal[ColorBG].pixel;
594 swa.border_pixel = dc.border.pixel;
595 swa.save_under = True; /* pop-up windows should save_under*/
a7732690 596 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 597 | PointerMotionMask | LeaveWindowMask;
3bec05ea 598 if (wflag)
599 swa.event_mask |= StructureNotifyMask;
45b2e22f 600 menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, config.border_pixels,
a7732690 601 CopyFromParent, CopyFromParent, CopyFromParent,
fd530f3f 602 CWOverrideRedirect | CWBackPixel |
603 CWBorderPixel | CWEventMask | CWSaveUnder,
a7732690 604 &swa);
605
f472bfac 606 menu->xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
607 XNClientWindow, menu->win, XNFocusWindow, menu->win, NULL);
608 if (menu->xic == NULL)
609 errx(1, "XCreateIC: could not obtain input method");
610
a7732690 611 return menu;
612}
613
a5ddd2cd 614/* build the menu tree */
615static struct Menu *
523b3d5e 616buildmenutree(int level, const char *label, const char *output, char *file)
a5ddd2cd 617{
618 static struct Menu *prevmenu = NULL; /* menu the previous item was added to */
619 static struct Menu *rootmenu = NULL; /* menu to be returned */
620 struct Item *curritem = NULL; /* item currently being read */
621 struct Item *item; /* dummy item for loops */
622 struct Menu *menu; /* dummy menu for loops */
523b3d5e 623 int i;
a5ddd2cd 624
625 /* create the item */
33376f54 626 curritem = allocitem(label, output, file);
a5ddd2cd 627
628 /* put the item in the menu tree */
629 if (prevmenu == NULL) { /* there is no menu yet */
685ca30d 630 menu = allocmenu(NULL, curritem, level);
631 rootmenu = menu;
632 prevmenu = menu;
633 curritem->prev = NULL;
a5ddd2cd 634 } else if (level < prevmenu->level) { /* item is continuation of a parent menu */
635 /* go up the menu tree until find the menu this item continues */
636 for (menu = prevmenu, i = level;
637 menu != NULL && i != prevmenu->level;
638 menu = menu->parent, i++)
639 ;
640 if (menu == NULL)
70f5db0f 641 errx(1, "improper indentation detected");
a5ddd2cd 642
643 /* find last item in the new menu */
644 for (item = menu->list; item->next != NULL; item = item->next)
645 ;
646
647 prevmenu = menu;
648 item->next = curritem;
649 curritem->prev = item;
650 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
651 /* find last item in the previous menu */
652 for (item = prevmenu->list; item->next != NULL; item = item->next)
653 ;
654
655 item->next = curritem;
656 curritem->prev = item;
657 } else if (level > prevmenu->level) { /* item begins a new menu */
658 menu = allocmenu(prevmenu, curritem, level);
659
660 /* find last item in the previous menu */
661 for (item = prevmenu->list; item->next != NULL; item = item->next)
662 ;
663
78cdb02b
TH
664 /* a separator is no valid root for a submenu */
665 if (!item->label)
666 errx(1, "a separator is no valid root for a submenu");
667
a5ddd2cd 668 prevmenu = menu;
669 menu->caller = item;
670 item->submenu = menu;
671 curritem->prev = NULL;
672 }
673
a2ff706d 674 if (curritem->file)
675 prevmenu->hasicon = 1;
676
a5ddd2cd 677 return rootmenu;
678}
679
a7732690 680/* create menus and items from the stdin */
257e42cc 681static struct Menu *
a7732690 682parsestdin(void)
683{
a5ddd2cd 684 struct Menu *rootmenu;
a7732690 685 char *s, buf[BUFSIZ];
33376f54 686 char *file, *label, *output;
523b3d5e 687 int level = 0;
257e42cc 688
ddb15acf 689 rootmenu = NULL;
690
a7732690 691 while (fgets(buf, BUFSIZ, stdin) != NULL) {
a5ddd2cd 692 /* get the indentation level */
693 level = strspn(buf, "\t");
a7732690 694
a5ddd2cd 695 /* get the label */
696 s = level + buf;
697 label = strtok(s, "\t\n");
a7732690 698
33376f54 699 /* get the filename */
700 file = NULL;
701 if (label != NULL && strncmp(label, "IMG:", 4) == 0) {
702 file = label + 4;
703 label = strtok(NULL, "\t\n");
704 }
705
a5ddd2cd 706 /* get the output */
707 output = strtok(NULL, "\n");
708 if (output == NULL) {
709 output = label;
710 } else {
711 while (*output == '\t')
712 output++;
a7732690 713 }
a5ddd2cd 714
33376f54 715 rootmenu = buildmenutree(level, label, output, file);
a7732690 716 }
a7732690 717
257e42cc 718 return rootmenu;
a7732690 719}
720
3d853664 721/* get next utf8 char from s return its codepoint and set next_ret to pointer to end of character */
cdeaefaa 722static FcChar32
723getnextutf8char(const char *s, const char **next_ret)
724{
cdeaefaa 725 static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0};
cdeaefaa 726 static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
cdeaefaa 727 static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000};
728 static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
3d853664 729 /* 0xFFFD is the replacement character, used to represent unknown characters */
cdeaefaa 730 static const FcChar32 unknown = 0xFFFD;
731 FcChar32 ucode; /* FcChar32 type holds 32 bits */
732 size_t usize = 0; /* n' of bytes of the utf8 character */
733 size_t i;
734
735 *next_ret = s+1;
736
737 /* get code of first byte of utf8 character */
738 for (i = 0; i < sizeof utfmask; i++) {
739 if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) {
740 usize = i;
741 ucode = (unsigned char)*s & ~utfmask[i];
742 break;
743 }
744 }
745
746 /* if first byte is a continuation byte or is not allowed, return unknown */
747 if (i == sizeof utfmask || usize == 0)
748 return unknown;
749
750 /* check the other usize-1 bytes */
751 s++;
752 for (i = 1; i < usize; i++) {
753 *next_ret = s+1;
237da982 754 /* if byte is nul or is not a continuation byte, return unknown */
cdeaefaa 755 if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0])
756 return unknown;
757 /* 6 is the number of relevant bits in the continuation byte */
758 ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]);
759 s++;
760 }
761
762 /* check if ucode is invalid or in utf-16 surrogate halves */
763 if (!BETWEEN(ucode, utfmin[usize], utfmax[usize])
764 || BETWEEN (ucode, 0xD800, 0xDFFF))
765 return unknown;
766
767 return ucode;
768}
769
f8d55995 770/* get which font contains a given code point */
771static XftFont *
772getfontucode(FcChar32 ucode)
773{
a48473fd 774 FcCharSet *fccharset = NULL;
775 FcPattern *fcpattern = NULL;
776 FcPattern *match = NULL;
777 XftFont *retfont = NULL;
6d56f279 778 XftResult result;
f8d55995 779 size_t i;
780
781 for (i = 0; i < dc.nfonts; i++)
782 if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue)
783 return dc.fonts[i];
6d56f279 784
785 /* create a charset containing our code point */
786 fccharset = FcCharSetCreate();
787 FcCharSetAddChar(fccharset, ucode);
788
a48473fd 789 /* create a pattern akin to the dc.pattern but containing our charset */
790 if (fccharset) {
791 fcpattern = FcPatternDuplicate(dc.pattern);
792 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
793 }
6d56f279 794
795 /* find pattern matching fcpattern */
a48473fd 796 if (fcpattern) {
797 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
798 FcDefaultSubstitute(fcpattern);
799 match = XftFontMatch(dpy, screen, fcpattern, &result);
800 }
6d56f279 801
802 /* if found a pattern, open its font */
803 if (match) {
804 retfont = XftFontOpenPattern(dpy, match);
805 if (retfont && XftCharExists(dpy, retfont, ucode) == FcTrue) {
806 if ((dc.fonts = realloc(dc.fonts, dc.nfonts+1)) == NULL)
807 err(1, "realloc");
a48473fd 808 dc.fonts[dc.nfonts] = retfont;
809 return dc.fonts[dc.nfonts++];
6d56f279 810 } else {
811 XftFontClose(dpy, retfont);
812 }
813 }
814
815 /* in case no fount was found, return the first one */
816 return dc.fonts[0];
f8d55995 817}
818
819/* draw text into XftDraw, return width of text glyphs */
cdeaefaa 820static int
821drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text)
822{
f8d55995 823 int textwidth = 0;
cdeaefaa 824
f8d55995 825 while (*text) {
826 XftFont *currfont;
cdeaefaa 827 XGlyphInfo ext;
f8d55995 828 FcChar32 ucode;
829 const char *next;
cdeaefaa 830 size_t len;
cdeaefaa 831
f8d55995 832 ucode = getnextutf8char(text, &next);
6d56f279 833 currfont = getfontucode(ucode);
cdeaefaa 834
f8d55995 835 len = next - text;
f8d55995 836 XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext);
837 textwidth += ext.xOff;
cdeaefaa 838
839 if (draw) {
a48473fd 840 int texty;
841
842 texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent;
f8d55995 843 XftDrawStringUtf8(draw, color, currfont, x, texty, (XftChar8 *)text, len);
cdeaefaa 844 x += ext.xOff;
845 }
846
f8d55995 847 text = next;
cdeaefaa 848 }
849
f8d55995 850 return textwidth;
cdeaefaa 851}
852
71b4db92 853/* setup the height, width and icon of the items of a menu */
a7732690 854static void
523b3d5e 855setupitems(struct Menu *menu, struct Monitor *mon)
a7732690 856{
523b3d5e 857 Pixmap pix;
a80fee22 858 struct Item *item;
27c03246 859 int itemwidth;
523b3d5e 860 int menuh;
861 int maxh;
a7732690 862
523b3d5e 863 menu->first = menu->list;
8902c43b 864 menu->w = config.width_pixels;
27c03246 865 menu->maxtextw = 0;
523b3d5e 866 menuh = 0;
867 maxh = config.max_items > 3 ? (2 + config.max_items) * config.height_pixels : mon->h;
a80fee22 868 for (item = menu->list; item != NULL; item = item->next) {
523b3d5e 869 item->y = menuh;
7fbd1c5e 870 if (item->label == NULL) /* height for separator item */
8902c43b 871 item->h = config.separator_pixels;
a80fee22 872 else
8902c43b 873 item->h = config.height_pixels;
523b3d5e 874 menuh += item->h;
875 if (!menu->overflow) {
876 if (menu->h + config.height_pixels * 2 < maxh - config.border_pixels * 2) {
877 menu->h = menuh;
878 } else {
879 menu->overflow = 1;
880 menu->h += config.height_pixels * 2;
881 }
882 }
15362de4 883 if (item->label)
27c03246 884 item->textw = drawtext(NULL, NULL, 0, 0, 0, item->label);
15362de4 885 else
27c03246 886 item->textw = 0;
685ca30d 887
71b4db92 888 /*
889 * set menu width
890 *
27c03246 891 * the item width depends on the size of its label (item->textw),
71b4db92 892 * and it is only used to calculate the width of the menu (which
893 * is equal to the width of the largest item).
894 *
895 * the horizontal padding appears 4 times through the width of a
15362de4 896 * item: before and after its icon, and before and after its triangle.
71b4db92 897 * if the iflag is set (icons are disabled) then the horizontal
15362de4 898 * padding appears 3 times: before the label and around the triangle.
71b4db92 899 */
27c03246 900 itemwidth = item->textw + config.triangle_width + config.horzpadding * 3;
a2ff706d 901 itemwidth += (iflag || !menu->hasicon) ? 0 : config.iconsize + config.horzpadding;
523b3d5e 902 menu->w = max(menu->w, itemwidth);
903 menu->maxtextw = max(menu->maxtextw, item->textw);
904 }
905 if (!menu->overflow) {
906 XSetWindowBackground(dpy, menu->win, dc.normal[ColorBG].pixel);
907 } else {
908 pix = XCreatePixmap(dpy, menu->win, menu->w, menu->h, depth);
909 XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel);
910 XFillRectangle(dpy, pix, dc.gc, 0, 0, menu->w, menu->h);
911 XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel);
912 XFillPolygon(
913 dpy, pix, dc.gc,
914 (XPoint []){
915 {menu->w / 2 - 3, config.height_pixels / 2 + 2},
916 {3, -4},
917 {3, 4},
918 {-6, 0},
919 },
920 4, Convex, CoordModePrevious
921 );
922 XFillPolygon(
923 dpy, pix, dc.gc,
924 (XPoint []){
925 {menu->w / 2 - 3, menu->h - config.height_pixels / 2 - 2},
926 {3, 4},
927 {3, -4},
928 {-6, 0},
929 },
930 4, Convex, CoordModePrevious
931 );
932 XSetWindowBackgroundPixmap(dpy, menu->win, pix);
933 XFreePixmap(dpy, pix);
a80fee22 934 }
f8ffe0b2 935}
936
937/* setup the position of a menu */
938static void
45b2e22f 939setupmenupos(struct Menu *menu, struct Monitor *mon)
f8ffe0b2 940{
941 int width, height;
a7732690 942
8902c43b 943 width = menu->w + config.border_pixels * 2;
944 height = menu->h + config.border_pixels * 2;
a7732690 945 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
45b2e22f 946 if (pflag || (config.posx >= mon->x && mon->x + mon->w - config.posx >= width))
05cfe1a0 947 menu->x = config.posx;
948 else if (config.posx > width)
949 menu->x = config.posx - width;
8902c43b 950
45b2e22f 951 if (pflag || (config.posy >= mon->y && mon->y + mon->h - config.posy >= height))
05cfe1a0 952 menu->y = config.posy;
45b2e22f 953 else if (mon->y + mon->h > height)
954 menu->y = mon->y + mon->h - height;
a7732690 955 } else { /* else, calculate in respect to parent menu */
237da982 956 int parentwidth;
957
958 parentwidth = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
959
45b2e22f 960 if (mon->x + mon->w - parentwidth >= width)
237da982 961 menu->x = parentwidth;
8902c43b 962 else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
963 menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
a7732690 964
45b2e22f 965 if (mon->y + mon->h - (menu->caller->y + menu->parent->y) >= height)
8455c369 966 menu->y = menu->caller->y + menu->parent->y;
45b2e22f 967 else if (mon->y + mon->h > height)
968 menu->y = mon->y + mon->h - height;
a7732690 969 }
f8ffe0b2 970}
971
972/* recursivelly setup menu configuration and its pixmap */
973static void
45b2e22f 974setupmenu(struct Menu *menu, struct Monitor *mon)
f8ffe0b2 975{
8902c43b 976 char *title;
f8ffe0b2 977 struct Item *item;
f8ffe0b2 978 XWindowChanges changes;
979 XSizeHints sizeh;
3bec05ea 980 XTextProperty wintitle;
f8ffe0b2 981
982 /* setup size and position of menus */
523b3d5e 983 setupitems(menu, mon);
45b2e22f 984 setupmenupos(menu, mon);
a7732690 985
986 /* update menu geometry */
987 changes.height = menu->h;
f1583285 988 changes.width = menu->w;
a7732690 989 changes.x = menu->x;
990 changes.y = menu->y;
45b2e22f 991 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
992
993 if (firsttime) {
994 /* set window title (used if wflag is on) */
995 if (menu->parent == NULL) {
996 title = classh.res_name;
997 } else if (menu->caller->output) {
998 title = menu->caller->output;
999 } else {
1000 title = "\0";
1001 }
1002 XStringListToTextProperty(&title, 1, &wintitle);
1003
1004 /* set window manager hints */
1005 sizeh.flags = USPosition | PMaxSize | PMinSize;
1006 sizeh.min_width = sizeh.max_width = menu->w;
1007 sizeh.min_height = sizeh.max_height = menu->h;
1008 XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, &classh);
1009
1010 /* set WM protocols and ewmh window properties */
1011 XSetWMProtocols(dpy, menu->win, &wmdelete, 1);
1012 XChangeProperty(dpy, menu->win, netatom[NetWMName], utf8string, 8,
1013 PropModeReplace, (unsigned char *)title, strlen(title));
1014 XChangeProperty(dpy, menu->win, netatom[NetWMWindowType], XA_ATOM, 32,
1015 PropModeReplace,
1016 (unsigned char *)&netatom[NetWMWindowTypePopupMenu], 1);
3bec05ea 1017 }
8902c43b 1018
a80fee22 1019 /* calculate positions of submenus */
a7732690 1020 for (item = menu->list; item != NULL; item = item->next) {
1021 if (item->submenu != NULL)
45b2e22f 1022 setupmenu(item->submenu, mon);
a7732690 1023 }
1024}
1025
85003546 1026/* try to grab pointer, we may have to wait for another process to ungrab */
1027static void
1028grabpointer(void)
1029{
1030 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
1031 int i;
1032
1033 for (i = 0; i < 1000; i++) {
1034 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
1035 GrabModeAsync, GrabModeAsync, None,
1036 None, CurrentTime) == GrabSuccess)
1037 return;
1038 nanosleep(&ts, NULL);
1039 }
70f5db0f 1040 errx(1, "could not grab pointer");
85003546 1041}
1042
1043/* try to grab keyboard, we may have to wait for another process to ungrab */
1044static void
1045grabkeyboard(void)
1046{
1047 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
1048 int i;
1049
1050 for (i = 0; i < 1000; i++) {
1051 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
1052 GrabModeAsync, CurrentTime) == GrabSuccess)
1053 return;
1054 nanosleep(&ts, NULL);
1055 }
8a7bfdf8 1056 errx(1, "could not grab keyboard");
85003546 1057}
1058
f472bfac 1059/* try to grab focus, we may have to wait for another process to ungrab */
1060static void
1061grabfocus(Window win)
1062{
1063 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
1064 Window focuswin;
1065 int i, revertwin;
1066
1067 for (i = 0; i < 100; ++i) {
1068 XGetInputFocus(dpy, &focuswin, &revertwin);
1069 if (focuswin == win)
1070 return;
1071 XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
1072 nanosleep(&ts, NULL);
1073 }
1074 errx(1, "cannot grab focus");
1075}
1076
6abae763 1077/* ungrab pointer and keyboard */
1078static void
1079ungrab(void)
1080{
1081 XUngrabPointer(dpy, CurrentTime);
1082 XUngrabKeyboard(dpy, CurrentTime);
1083}
1084
523b3d5e 1085/* check if path is absolute or relative to current directory */
1086static int
1087isabsolute(const char *s)
1088{
1089 return s[0] == '/' || (s[0] == '.' && (s[1] == '/' || (s[1] == '.' && s[2] == '/')));
1090}
1091
3d853664 1092/* load and scale icon */
1093static Imlib_Image
1094loadicon(const char *file)
1095{
1096 Imlib_Image icon;
ed1650a7 1097 Imlib_Load_Error errcode;
523b3d5e 1098 char path[PATH_MAX];
ed1650a7 1099 const char *errstr;
3d853664 1100 int width;
1101 int height;
1102 int imgsize;
523b3d5e 1103 int i;
3d853664 1104
f66f4c18 1105 if (*file == '\0') {
1106 warnx("could not load icon (file name is blank)");
1107 return NULL;
523b3d5e 1108 }
1109 if (isabsolute(file))
1110 icon = imlib_load_image_with_error_return(file, &errcode);
1111 else {
1112 for (i = 0; i < niconpaths; i++) {
1113 snprintf(path, sizeof(path), "%s/%s", iconpaths[i], file);
1114 icon = imlib_load_image_with_error_return(path, &errcode);
1115 if (icon != NULL || errcode != IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST) {
1116 break;
1117 }
1118 }
1119 }
1120 if (icon == NULL) {
ed1650a7 1121 switch (errcode) {
1122 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
1123 errstr = "file does not exist";
1124 break;
1125 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
1126 errstr = "file is directory";
1127 break;
1128 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
1129 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
1130 errstr = "permission denied";
1131 break;
1132 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
1133 errstr = "unknown file format";
1134 break;
1135 case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
1136 errstr = "path too long";
1137 break;
1138 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
1139 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
1140 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
1141 errstr = "improper path";
1142 break;
1143 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
1144 errstr = "too many symbolic links";
1145 break;
1146 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
1147 errstr = "out of memory";
1148 break;
1149 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
1150 errstr = "out of file descriptors";
1151 break;
1152 default:
1153 errstr = "unknown error";
1154 break;
1155 }
f66f4c18 1156 warnx("could not load icon (%s): %s", errstr, file);
1157 return NULL;
ed1650a7 1158 }
3d853664 1159
1160 imlib_context_set_image(icon);
1161
1162 width = imlib_image_get_width();
1163 height = imlib_image_get_height();
523b3d5e 1164 imgsize = min(width, height);
3d853664 1165
1166 icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize,
1167 config.iconsize,
1168 config.iconsize);
1169
1170 return icon;
1171}
1172
1173/* draw pixmap for the selected and unselected version of each item on menu */
1174static void
1175drawitems(struct Menu *menu)
1176{
27c03246 1177 XftDraw *dsel, *dunsel;
3d853664 1178 struct Item *item;
27c03246 1179 int textx;
1180 int x, y;
3d853664 1181
1182 for (item = menu->list; item != NULL; item = item->next) {
523b3d5e 1183 item->unsel = XCreatePixmap(dpy, menu->win, menu->w, item->h, depth);
3d853664 1184
1185 XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel);
1186 XFillRectangle(dpy, item->unsel, dc.gc, 0, 0, menu->w, item->h);
1187
1188 if (item->label == NULL) { /* item is separator */
523b3d5e 1189 y = item->h / 2;
3d853664 1190 XSetForeground(dpy, dc.gc, dc.separator.pixel);
1191 XDrawLine(dpy, item->unsel, dc.gc, config.horzpadding, y,
1192 menu->w - config.horzpadding, y);
1193
1194 item->sel = item->unsel;
1195 } else {
523b3d5e 1196 item->sel = XCreatePixmap(dpy, menu->win, menu->w, item->h, depth);
3d853664 1197 XSetForeground(dpy, dc.gc, dc.selected[ColorBG].pixel);
1198 XFillRectangle(dpy, item->sel, dc.gc, 0, 0, menu->w, item->h);
1199
1200 /* draw text */
27c03246 1201 textx = config.horzpadding;
1202 textx += (iflag || !menu->hasicon) ? 0 : config.horzpadding + config.iconsize;
1203 switch (config.alignment) {
1204 case CenterAlignment:
1205 textx += (menu->maxtextw - item->textw) / 2;
1206 break;
1207 case RightAlignment:
1208 textx += menu->maxtextw - item->textw;
1209 break;
1210 default:
1211 break;
1212 }
3d853664 1213 dsel = XftDrawCreate(dpy, item->sel, visual, colormap);
1214 dunsel = XftDrawCreate(dpy, item->unsel, visual, colormap);
1215 XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel);
27c03246 1216 drawtext(dsel, &dc.selected[ColorFG], textx, 0, item->h, item->label);
3d853664 1217 XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel);
27c03246 1218 drawtext(dunsel, &dc.normal[ColorFG], textx, 0, item->h, item->label);
3d853664 1219 XftDrawDestroy(dsel);
1220 XftDrawDestroy(dunsel);
1221
1222 /* draw triangle */
1223 if (item->submenu != NULL) {
1224 x = menu->w - config.triangle_width - config.horzpadding;
1225 y = (item->h - config.triangle_height + 1) / 2;
1226
1227 XPoint triangle[] = {
1228 {x, y},
1229 {x + config.triangle_width, y + config.triangle_height/2},
1230 {x, y + config.triangle_height},
1231 {x, y}
1232 };
1233
1234 XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel);
1235 XFillPolygon(dpy, item->sel, dc.gc, triangle, LEN(triangle),
1236 Convex, CoordModeOrigin);
1237 XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel);
1238 XFillPolygon(dpy, item->unsel, dc.gc, triangle, LEN(triangle),
1239 Convex, CoordModeOrigin);
1240 }
1241
d3c21312 1242 /* try to load icon */
1243 if (item->file && !iflag) {
3d853664 1244 item->icon = loadicon(item->file);
d3c21312 1245 free(item->file);
1246 }
3d853664 1247
d3c21312 1248 /* draw icon if properly loaded */
f66f4c18 1249 if (item->icon) {
3d853664 1250 imlib_context_set_image(item->icon);
237da982 1251 imlib_context_set_drawable(item->sel);
3d853664 1252 imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
1253 imlib_context_set_drawable(item->unsel);
1254 imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
d3c21312 1255 imlib_context_set_image(item->icon);
1256 imlib_free_image();
3d853664 1257 }
1258 }
1259 }
1260}
1261
1262/* copy pixmaps of items of the current menu and of its ancestors into menu window */
1263static void
1264drawmenus(struct Menu *currmenu)
1265{
1266 struct Menu *menu;
1267 struct Item *item;
523b3d5e 1268 int y0, y;
1269 int maxh;
3d853664 1270
1271 for (menu = currmenu; menu != NULL; menu = menu->parent) {
1272 if (!menu->drawn) {
1273 drawitems(menu);
1274 menu->drawn = 1;
1275 }
523b3d5e 1276 if (menu->overflow && menu->selected != NULL) {
1277 maxh = menu->h - config.height_pixels * 2;
1278 while (menu->first->next != NULL &&
1279 menu->selected->y >= menu->first->y + maxh) {
1280 menu->first = menu->first->next;
1281 }
1282 while (menu->first->prev != NULL &&
1283 menu->selected->y < menu->first->y) {
1284 menu->first = menu->first->prev;
1285 }
1286 }
1287 y = menu->first->y;
1288 y0 = menu->overflow ? config.height_pixels : 0;
1289 for (item = menu->first; item != NULL; item = item->next) {
1290 if (menu->overflow && item->y - y + item->h > menu->h - config.height_pixels * 2)
1291 break;
1292 if (item == menu->selected) {
1293 XCopyArea(dpy, item->sel, menu->win, dc.gc, 0, 0, menu->w, item->h, 0, y0 + item->y - y);
1294 } else {
1295 XCopyArea(dpy, item->unsel, menu->win, dc.gc, 0, 0, menu->w, item->h, 0, y0 + item->y - y);
1296 }
3d853664 1297 }
1298 }
1299}
1300
45b2e22f 1301/* unmap current menu and its parents */
a7732690 1302static void
45b2e22f 1303unmapmenu(struct Menu *currmenu)
1304{
1305 struct Menu *menu;
1306
1307 for (menu = currmenu; menu != NULL; menu = menu->parent) {
1308 menu->selected = NULL;
1309 XUnmapWindow(dpy, menu->win);
1310 }
1311}
1312
1313/* umap previous menus and map current menu and its parents */
1314static struct Menu *
1315mapmenu(struct Menu *currmenu, struct Menu *prevmenu, struct Monitor *mon)
a7732690 1316{
d8a7caf2 1317 struct Menu *menu, *menu_;
d8a7caf2 1318 struct Menu *lcamenu; /* lowest common ancestor menu */
523b3d5e 1319 int minlevel; /* level of the closest to root menu */
1320 int maxlevel; /* level of the closest to root menu */
a7732690 1321
257e42cc 1322 /* do not remap current menu if it wasn't updated*/
1323 if (prevmenu == currmenu)
45b2e22f 1324 goto done;
a7732690 1325
257e42cc 1326 /* if this is the first time mapping, skip calculations */
1327 if (prevmenu == NULL) {
fd530f3f 1328 XMapWindow(dpy, currmenu->win);
45b2e22f 1329 goto done;
27443900 1330 }
1331
d8a7caf2 1332 /* find lowest common ancestor menu */
523b3d5e 1333 minlevel = min(currmenu->level, prevmenu->level);
1334 maxlevel = max(currmenu->level, prevmenu->level);
257e42cc 1335 if (currmenu->level == maxlevel) {
27443900 1336 menu = currmenu;
257e42cc 1337 menu_ = prevmenu;
1338 } else {
1339 menu = prevmenu;
1340 menu_ = currmenu;
27443900 1341 }
1342 while (menu->level > minlevel)
1343 menu = menu->parent;
1344 while (menu != menu_) {
1345 menu = menu->parent;
1346 menu_ = menu_->parent;
d8a7caf2 1347 }
27443900 1348 lcamenu = menu;
d8a7caf2 1349
873c080c 1350 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
257e42cc 1351 for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
8455c369 1352 menu->selected = NULL;
a7732690 1353 XUnmapWindow(dpy, menu->win);
1354 }
1355
873c080c 1356 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 1357 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
3bec05ea 1358 if (wflag) {
45b2e22f 1359 setupmenupos(menu, mon);
3bec05ea 1360 XMoveWindow(dpy, menu->win, menu->x, menu->y);
1361 }
a7732690 1362 XMapWindow(dpy, menu->win);
fd530f3f 1363 }
257e42cc 1364
f472bfac 1365 grabfocus(currmenu->win);
45b2e22f 1366done:
1367 return currmenu;
fd530f3f 1368}
1369
8902c43b 1370/* get menu of given window */
1371static struct Menu *
1372getmenu(struct Menu *currmenu, Window win)
1373{
1374 struct Menu *menu;
1375
1376 for (menu = currmenu; menu != NULL; menu = menu->parent)
1377 if (menu->win == win)
1378 return menu;
1379
1380 return NULL;
1381}
1382
523b3d5e 1383/* get in *ret the item in given menu and position; return 1 if position is on a scroll triangle */
1384static int
1385getitem(struct Menu *menu, struct Item **ret, int y)
8902c43b 1386{
1387 struct Item *item;
523b3d5e 1388 int y0;
8902c43b 1389
523b3d5e 1390 *ret = NULL;
8902c43b 1391 if (menu == NULL)
523b3d5e 1392 return 0;
1393 if (menu->overflow) {
1394 if (y < config.height_pixels) {
1395 *ret = menu->first->prev;
1396 return 1;
1397 } else if (y > menu->h - config.height_pixels) {
1398 y0 = menu->overflow ? config.height_pixels : 0;
1399 y = menu->h - y0 + menu->first->y;
1400 for (item = menu->first; item != NULL; item = item->next)
1401 if (y >= item->y && y <= item->y + item->h)
1402 break;
1403 if (item != NULL)
1404 *ret = menu->first->next;
1405 return 1;
1406 }
1407 }
1408 y0 = menu->overflow ? config.height_pixels : 0;
1409 y -= y0 - menu->first->y;
1410 for (item = menu->first; item != NULL; item = item->next) {
1411 if (y >= item->y && y <= item->y + item->h) {
1412 *ret = item;
1413 break;
1414 }
1415 }
1416 return 0;
8902c43b 1417}
1418
858338d9 1419/* cycle through the items; non-zero direction is next, zero is prev */
1420static struct Item *
257e42cc 1421itemcycle(struct Menu *currmenu, int direction)
858338d9 1422{
006c94ce 1423 struct Item *item = NULL;
858338d9 1424 struct Item *lastitem;
1425
006c94ce 1426 for (lastitem = currmenu->list; lastitem && lastitem->next; lastitem = lastitem->next)
1427 ;
858338d9 1428
006c94ce 1429 /* select item (either separator or labeled item) in given direction */
1430 switch (direction) {
1431 case ITEMNEXT:
858338d9 1432 if (currmenu->selected == NULL)
1433 item = currmenu->list;
1434 else if (currmenu->selected->next != NULL)
1435 item = currmenu->selected->next;
006c94ce 1436 break;
1437 case ITEMPREV:
858338d9 1438 if (currmenu->selected == NULL)
1439 item = lastitem;
1440 else if (currmenu->selected->prev != NULL)
1441 item = currmenu->selected->prev;
006c94ce 1442 break;
1443 case ITEMFIRST:
1444 item = currmenu->list;
1445 break;
1446 case ITEMLAST:
1447 item = lastitem;
1448 break;
1449 }
858338d9 1450
006c94ce 1451 /*
1452 * the selected item can be a separator
1453 * let's select the closest labeled item (ie., one that isn't a separator)
1454 */
1455 switch (direction) {
1456 case ITEMNEXT:
1457 case ITEMFIRST:
1458 while (item != NULL && item->label == NULL)
1459 item = item->next;
1460 if (item == NULL)
1461 item = currmenu->list;
1462 break;
1463 case ITEMPREV:
1464 case ITEMLAST:
858338d9 1465 while (item != NULL && item->label == NULL)
1466 item = item->prev;
858338d9 1467 if (item == NULL)
1468 item = lastitem;
006c94ce 1469 break;
858338d9 1470 }
1471
1472 return item;
1473}
1474
60ddf397 1475/* check if button is used to scroll */
1476static int
1477isscrollbutton(unsigned int button)
1478{
1479 if (button == Button4 || button == Button5)
1480 return 1;
1481 return 0;
1482}
1483
1146fd81 1484/* check if button is used to open a item on click */
1485static int
1486isclickbutton(unsigned int button)
1487{
60ddf397 1488 if (button == Button1 || button == Button2)
1146fd81 1489 return 1;
1490 if (!rflag && button == Button3)
1491 return 1;
1492 return 0;
1493}
1494
60ddf397 1495/* warp pointer to center of selected item */
1496static void
1497warppointer(struct Menu *menu, struct Item *item)
1498{
1499 if (menu == NULL || item == NULL)
1500 return;
1501 if (menu->selected) {
1502 XWarpPointer(dpy, None, menu->win, 0, 0, 0, 0, menu->w / 2, item->y + item->h / 2);
1503 }
1504}
1505
b33886d7 1506/* append buf into text */
1507static int
1508append(char *text, char *buf, size_t textsize, size_t buflen)
1509{
1510 size_t textlen;
1511
1512 textlen = strlen(text);
1513 if (iscntrl(*buf))
1514 return 0;
1515 if (textlen + buflen > textsize - 1)
1516 return 0;
1517 if (buflen < 1)
1518 return 0;
1519 memcpy(text + textlen, buf, buflen);
1520 text[textlen + buflen] = '\0';
1521 return 1;
1522}
1523
693735f7 1524/* get item in menu matching text from given direction (or from beginning, if dir = 0) */
b33886d7 1525static struct Item *
693735f7 1526matchitem(struct Menu *menu, char *text, int dir)
b33886d7 1527{
693735f7 1528 struct Item *item, *lastitem;
b33886d7 1529 char *s;
1530 size_t textlen;
1531
693735f7 1532 for (lastitem = menu->list; lastitem && lastitem->next; lastitem = lastitem->next)
1533 ;
b33886d7 1534 textlen = strlen(text);
693735f7 1535 if (dir < 0) {
1536 if (menu->selected && menu->selected->prev)
1537 item = menu->selected->prev;
1538 else
1539 item = lastitem;
1540 } else if (dir > 0) {
1541 if (menu->selected && menu->selected->next)
1542 item = menu->selected->next;
1543 else
1544 item = menu->list;
1545 } else {
1546 item = menu->list;
1547 }
1548 /* find next item from selected item */
1549 for ( ; item; item = (dir < 0) ? item->prev : item->next)
b33886d7 1550 for (s = item->label; s && *s; s++)
1551 if (strncasecmp(s, text, textlen) == 0)
1552 return item;
693735f7 1553 /* if not found, try to find from the beginning/end of list */
1554 if (dir > 0) {
1555 for (item = menu->list ; item; item = item->next) {
1556 for (s = item->label; s && *s; s++) {
1557 if (strncasecmp(s, text, textlen) == 0) {
1558 return item;
1559 }
1560 }
1561 }
1562 } else {
1563 for (item = lastitem ; item; item = item->prev) {
1564 for (s = item->label; s && *s; s++) {
1565 if (strncasecmp(s, text, textlen) == 0) {
1566 return item;
1567 }
1568 }
1569 }
1570 }
b33886d7 1571 return NULL;
1572}
1573
8239f1f1 1574/* check keysyms defined on config.h */
1575static KeySym
1576normalizeksym(KeySym ksym)
1577{
1578 if (ksym == KSYMFIRST)
1579 return XK_Home;
1580 if (ksym == KSYMLAST)
1581 return XK_End;
1582 if (ksym == KSYMUP)
1583 return XK_Up;
1584 if (ksym == KSYMDOWN)
1585 return XK_Down;
1586 if (ksym == KSYMLEFT)
1587 return XK_Left;
1588 if (ksym == KSYMRIGHT)
1589 return XK_Right;
1590 return ksym;
1591}
1592
a7732690 1593/* run event loop */
1594static void
45b2e22f 1595run(struct Menu *currmenu, struct Monitor *mon)
a7732690 1596{
b33886d7 1597 char text[BUFSIZ];
1598 char buf[32];
45b2e22f 1599 struct Menu *menu, *prevmenu;
a7732690 1600 struct Item *item;
1601 struct Item *previtem = NULL;
b33886d7 1602 struct Item *lastitem, *select;
858338d9 1603 KeySym ksym;
b33886d7 1604 Status status;
a7732690 1605 XEvent ev;
60ddf397 1606 int warped = 0;
6bbc0e45 1607 int action;
b33886d7 1608 int len;
8239f1f1 1609 int i;
a7732690 1610
b33886d7 1611 text[0] = '\0';
45b2e22f 1612 prevmenu = currmenu;
a7732690 1613 while (!XNextEvent(dpy, &ev)) {
b33886d7 1614 if (XFilterEvent(&ev, None))
1615 continue;
6bbc0e45 1616 action = ACTION_NOP;
a7732690 1617 switch(ev.type) {
1618 case Expose:
858338d9 1619 if (ev.xexpose.count == 0)
6bbc0e45 1620 action = ACTION_DRAW;
a7732690 1621 break;
1622 case MotionNotify:
60ddf397 1623 if (!warped) {
1624 menu = getmenu(currmenu, ev.xbutton.window);
523b3d5e 1625 if (getitem(menu, &item, ev.xbutton.y))
1626 break;
60ddf397 1627 if (menu == NULL || item == NULL || previtem == item)
1628 break;
1629 previtem = item;
1630 select = menu->selected = item;
1631 if (item->submenu != NULL) {
1632 currmenu = item->submenu;
1633 select = NULL;
1634 } else {
1635 currmenu = menu;
1636 }
1637 action = ACTION_CLEAR | ACTION_SELECT | ACTION_MAP | ACTION_DRAW;
a7732690 1638 }
60ddf397 1639 warped = 0;
a7732690 1640 break;
1641 case ButtonRelease:
60ddf397 1642 if (isscrollbutton(ev.xbutton.button)) {
1643 if (ev.xbutton.button == Button4)
1644 select = itemcycle(currmenu, ITEMPREV);
1645 else
1646 select = itemcycle(currmenu, ITEMNEXT);
1647 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW | ACTION_WARP;
1648 } else if (isclickbutton(ev.xbutton.button)) {
1649 menu = getmenu(currmenu, ev.xbutton.window);
523b3d5e 1650 if (getitem(menu, &item, ev.xbutton.y) && item != NULL) {
1651 select = NULL;
1652 menu->first = item;
1653 action = ACTION_CLEAR | ACTION_SELECT | ACTION_MAP | ACTION_DRAW;
1654 break;
1655 }
60ddf397 1656 if (menu == NULL || item == NULL)
1657 break;
b33886d7 1658enteritem:
60ddf397 1659 if (item->label == NULL)
1660 break; /* ignore separators */
1661 if (item->submenu != NULL) {
1662 currmenu = item->submenu;
1663 } else {
1664 printf("%s\n", item->output);
45b2e22f 1665 goto done;
60ddf397 1666 }
1667 select = currmenu->list;
1668 action = ACTION_CLEAR | ACTION_SELECT | ACTION_MAP | ACTION_DRAW;
1669 if (ev.xbutton.button == Button2) {
1670 action |= ACTION_WARP;
1671 }
a7732690 1672 }
fd530f3f 1673 break;
858338d9 1674 case ButtonPress:
257e42cc 1675 menu = getmenu(currmenu, ev.xbutton.window);
0c1a7864 1676 if (menu == NULL)
45b2e22f 1677 goto done;
858338d9 1678 break;
1679 case KeyPress:
b33886d7 1680 len = XmbLookupString(currmenu->xic, &ev.xkey, buf, sizeof buf, &ksym, &status);
1681 switch(status) {
1682 default: /* XLookupNone, XBufferOverflow */
1683 continue;
1684 case XLookupChars:
1685 goto append;
1686 case XLookupKeySym: /* FALLTHROUGH */
1687 case XLookupBoth:
1688 break;
1689 }
858338d9 1690
0c1a7864 1691 /* esc closes xmenu when current menu is the root menu */
257e42cc 1692 if (ksym == XK_Escape && currmenu->parent == NULL)
45b2e22f 1693 goto done;
858338d9 1694
1695 /* Shift-Tab = ISO_Left_Tab */
1696 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
1697 ksym = XK_ISO_Left_Tab;
1698
1699 /* cycle through menu */
06d6acdb 1700 select = NULL;
8239f1f1 1701 ksym = normalizeksym(ksym);
1702 switch (ksym) {
1703 case XK_Home:
06d6acdb 1704 select = itemcycle(currmenu, ITEMFIRST);
0129fde4 1705 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1706 break;
1707 case XK_End:
06d6acdb 1708 select = itemcycle(currmenu, ITEMLAST);
0129fde4 1709 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1710 break;
1711 case XK_ISO_Left_Tab:
693735f7 1712 if (*text) {
06d6acdb 1713 select = matchitem(currmenu, text, -1);
e14d88b0 1714 action = ACTION_SELECT | ACTION_DRAW;
8239f1f1 1715 break;
693735f7 1716 }
8239f1f1 1717 /* FALLTHROUGH */
1718 case XK_Up:
06d6acdb 1719 select = itemcycle(currmenu, ITEMPREV);
0129fde4 1720 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1721 break;
1722 case XK_Tab:
693735f7 1723 if (*text) {
06d6acdb 1724 select = matchitem(currmenu, text, 1);
e14d88b0 1725 action = ACTION_SELECT | ACTION_DRAW;
8239f1f1 1726 break;
693735f7 1727 }
8239f1f1 1728 /* FALLTHROUGH */
1729 case XK_Down:
06d6acdb 1730 select = itemcycle(currmenu, ITEMNEXT);
0129fde4 1731 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1732 break;
1733 case XK_1: case XK_2: case XK_3: case XK_4: case XK_5: case XK_6: case XK_7: case XK_8: case XK_9:
02511d09 1734 item = itemcycle(currmenu, ITEMFIRST);
c2959cf4 1735 lastitem = itemcycle(currmenu, ITEMLAST);
1736 for (int i = ksym - XK_1; i > 0 && item != lastitem; i--) {
02511d09
R
1737 currmenu->selected = item;
1738 item = itemcycle(currmenu, ITEMNEXT);
02511d09 1739 }
06d6acdb 1740 select = item;
0129fde4 1741 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1742 break;
1743 case XK_Return: case XK_Right:
1744 if (currmenu->selected) {
1745 item = currmenu->selected;
1746 goto enteritem;
1747 }
1748 break;
1749 case XK_Escape: case XK_Left:
1750 if (currmenu->parent) {
06d6acdb 1751 select = currmenu->parent->selected;
8239f1f1 1752 currmenu = currmenu->parent;
06d6acdb 1753 action = ACTION_CLEAR | ACTION_MAP | ACTION_SELECT | ACTION_DRAW;
8239f1f1 1754 }
1755 break;
1756 case XK_BackSpace: case XK_Clear: case XK_Delete:
0129fde4 1757 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
693735f7 1758 break;
8239f1f1 1759 default:
b33886d7 1760append:
0129fde4 1761 if (*buf == '\0' || iscntrl(*buf))
1762 break;
8239f1f1 1763 for (i = 0; i < 2; i++) {
1764 append(text, buf, sizeof text, len);
06d6acdb 1765 if ((select = matchitem(currmenu, text, 0)))
8239f1f1 1766 break;
1767 text[0] = '\0';
b33886d7 1768 }
289de21f 1769 action = ACTION_SELECT | ACTION_DRAW;
8239f1f1 1770 break;
b33886d7 1771 }
a7732690 1772 break;
f15fc339 1773 case LeaveNotify:
fd530f3f 1774 previtem = NULL;
b33886d7 1775 select = NULL;
693735f7 1776 action = ACTION_CLEAR | ACTION_SELECT | ACTION_DRAW;
f15fc339 1777 break;
3bec05ea 1778 case ConfigureNotify:
1779 menu = getmenu(currmenu, ev.xconfigure.window);
1780 if (menu == NULL)
1781 break;
1782 menu->x = ev.xconfigure.x;
1783 menu->y = ev.xconfigure.y;
1784 break;
1785 case ClientMessage:
523b3d5e 1786 if ((unsigned long)ev.xclient.data.l[0] != wmdelete)
8902c43b 1787 break;
3bec05ea 1788 /* user closed window */
1789 menu = getmenu(currmenu, ev.xclient.window);
1790 if (menu->parent == NULL)
45b2e22f 1791 goto done; /* closing the root menu closes the program */
3bec05ea 1792 currmenu = menu->parent;
6bbc0e45 1793 action = ACTION_MAP;
3bec05ea 1794 break;
a7732690 1795 }
693735f7 1796 if (action & ACTION_CLEAR)
b33886d7 1797 text[0] = '\0';
693735f7 1798 if (action & ACTION_SELECT)
1799 currmenu->selected = select;
6bbc0e45 1800 if (action & ACTION_MAP)
45b2e22f 1801 prevmenu = mapmenu(currmenu, prevmenu, mon);
6bbc0e45 1802 if (action & ACTION_DRAW)
1803 drawmenus(currmenu);
60ddf397 1804 if (action & ACTION_WARP) {
1805 warppointer(currmenu, select);
1806 warped = 1;
1807 }
a7732690 1808 }
45b2e22f 1809done:
1810 unmapmenu(currmenu);
1811 ungrab();
a7732690 1812}
1813
fd530f3f 1814/* recursivelly free pixmaps and destroy windows */
f15fc339 1815static void
8902c43b 1816cleanmenu(struct Menu *menu)
f15fc339 1817{
1818 struct Item *item;
ec4ed1ac 1819 struct Item *tmp;
f15fc339 1820
ec4ed1ac 1821 item = menu->list;
1822 while (item != NULL) {
f15fc339 1823 if (item->submenu != NULL)
8902c43b 1824 cleanmenu(item->submenu);
ec4ed1ac 1825 tmp = item;
3d853664 1826 if (menu->drawn) {
1827 XFreePixmap(dpy, item->unsel);
1828 if (tmp->label != NULL)
1829 XFreePixmap(dpy, item->sel);
1830 }
15dfafdf 1831 if (tmp->label != tmp->output)
1832 free(tmp->label);
6b5123e7 1833 free(tmp->output);
33376f54 1834 item = item->next;
ec4ed1ac 1835 free(tmp);
1836 }
f15fc339 1837
f15fc339 1838 XDestroyWindow(dpy, menu->win);
ec4ed1ac 1839 free(menu);
f15fc339 1840}
1841
6abae763 1842/* cleanup draw context */
a7732690 1843static void
6abae763 1844cleandc(void)
a7732690 1845{
7539247b 1846 size_t i;
1847
dbeb9940 1848 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
1849 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
1850 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
1851 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
fd530f3f 1852 XftColorFree(dpy, visual, colormap, &dc.separator);
1853 XftColorFree(dpy, visual, colormap, &dc.border);
7539247b 1854 for (i = 0; i < dc.nfonts; i++)
1855 XftFontClose(dpy, dc.fonts[i]);
f15fc339 1856 XFreeGC(dpy, dc.gc);
a7732690 1857}
1858
73a2a23d 1859/* xmenu: generate menu from stdin and print selected entry to stdout */
1860int
1861main(int argc, char *argv[])
a7732690 1862{
45b2e22f 1863 struct Monitor mon;
73a2a23d 1864 struct Menu *rootmenu;
45b2e22f 1865 XEvent ev;
73a2a23d 1866
1867 /* open connection to server and set X variables */
f472bfac 1868 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1869 warnx("warning: no locale support");
73a2a23d 1870 if ((dpy = XOpenDisplay(NULL)) == NULL)
1871 errx(1, "could not open display");
1872 screen = DefaultScreen(dpy);
1873 visual = DefaultVisual(dpy, screen);
1874 rootwin = RootWindow(dpy, screen);
1875 colormap = DefaultColormap(dpy, screen);
523b3d5e 1876 depth = DefaultDepth(dpy, screen);
6abae763 1877 XrmInitialize();
1878 if ((xrm = XResourceManagerString(dpy)) != NULL)
1879 xdb = XrmGetStringDatabase(xrm);
f472bfac 1880 if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
1881 errx(1, "XOpenIM: could not open input device");
6abae763 1882
78fb523f 1883 /* process configuration and window class */
6abae763 1884 getresources();
45b2e22f 1885 getoptions(argc, argv);
73a2a23d 1886
1887 /* imlib2 stuff */
1888 if (!iflag) {
1889 imlib_set_cache_size(2048 * 1024);
1890 imlib_context_set_dither(1);
1891 imlib_context_set_display(dpy);
1892 imlib_context_set_visual(visual);
1893 imlib_context_set_colormap(colormap);
1894 }
1895
1896 /* initializers */
73a2a23d 1897 initdc();
1898 initiconsize();
1899 initatoms();
1900
45b2e22f 1901 /* if running in root mode, get button presses from root window */
1902 if (rootmodeflag)
1903 XGrabButton(dpy, button, AnyModifier, rootwin, False, ButtonPressMask, GrabModeSync, GrabModeSync, None, None);
1904
1905 /* generate menus */
73a2a23d 1906 rootmenu = parsestdin();
1907 if (rootmenu == NULL)
1908 errx(1, "no menu generated");
73a2a23d 1909
1910 /* run event loop */
45b2e22f 1911 do {
1912 if (rootmodeflag)
1913 XNextEvent(dpy, &ev);
1914 if (!rootmodeflag ||
1915 (ev.type == ButtonPress &&
15fadd88 1916 ((modifier != 0 && (ev.xbutton.state & modifier)) ||
45b2e22f 1917 (ev.xbutton.subwindow == None)))) {
1918 if (rootmodeflag && passclickflag) {
1919 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1920 }
1921 getmonitor(&mon);
1922 if (!wflag) {
1923 grabpointer();
1924 grabkeyboard();
1925 }
1926 setupmenu(rootmenu, &mon);
1927 mapmenu(rootmenu, NULL, &mon);
1928 XFlush(dpy);
1929 run(rootmenu, &mon);
1930 firsttime = 0;
1931 } else {
1932 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1933 }
1934 } while (rootmodeflag);
73a2a23d 1935
6abae763 1936 /* clean stuff */
73a2a23d 1937 cleanmenu(rootmenu);
6abae763 1938 cleandc();
1939 XrmDestroyDatabase(xdb);
1940 XCloseDisplay(dpy);
73a2a23d 1941
1942 return 0;
a7732690 1943}