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