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