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