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