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 | |
40 | /* image loader */ |
71b4db92 |
41 | static Imlib_Image loadicon(const char *file); |
8902c43b |
42 | |
cdeaefaa |
43 | /* utf8 utils */ |
44 | static FcChar32 getnextutf8char(const char *s, const char **end_ret); |
45 | |
46 | /* pixmap drawers */ |
47 | static int drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text); |
48 | static void drawitems(struct Menu *menu); |
49 | |
8902c43b |
50 | /* structure setters, and their helper routines */ |
71b4db92 |
51 | static void setupitems(struct Menu *menu); |
8902c43b |
52 | static void setupmenupos(struct Menu *menu); |
53 | static void setupmenu(struct Menu *menu, XClassHint *classh); |
54 | |
55 | /* grabbers */ |
85003546 |
56 | static void grabpointer(void); |
57 | static void grabkeyboard(void); |
8902c43b |
58 | |
59 | /* window drawers and mappers */ |
257e42cc |
60 | static void mapmenu(struct Menu *currmenu); |
cdeaefaa |
61 | static void copypixmaps(struct Menu *currmenu); |
8902c43b |
62 | |
cdeaefaa |
63 | /* getters */ |
8902c43b |
64 | static struct Menu *getmenu(struct Menu *currmenu, Window win); |
65 | static struct Item *getitem(struct Menu *menu, int y); |
cdeaefaa |
66 | |
67 | /* cycle through items */ |
257e42cc |
68 | static struct Item *itemcycle(struct Menu *currmenu, int direction); |
cdeaefaa |
69 | |
70 | /* main event loop */ |
257e42cc |
71 | static void run(struct Menu *currmenu); |
8902c43b |
72 | |
73 | /* cleaners */ |
74 | static void cleanmenu(struct Menu *menu); |
15dfafdf |
75 | static void cleanup(void); |
8902c43b |
76 | |
77 | /* show usage */ |
a7732690 |
78 | static void usage(void); |
79 | |
8902c43b |
80 | |
81 | /* |
82 | * Variable declarations |
83 | */ |
84 | |
738ed9d5 |
85 | /* X stuff */ |
a7732690 |
86 | static Display *dpy; |
c6d9174e |
87 | static int screen; |
dbeb9940 |
88 | static Visual *visual; |
a7732690 |
89 | static Window rootwin; |
c6d9174e |
90 | static Colormap colormap; |
a7732690 |
91 | static struct DC dc; |
8902c43b |
92 | static Atom utf8string; |
3bec05ea |
93 | static Atom wmdelete; |
8902c43b |
94 | static Atom netatom[NetLast]; |
3bec05ea |
95 | |
96 | /* flags */ |
71b4db92 |
97 | static int iflag = 0; /* whether to disable icons */ |
05cfe1a0 |
98 | static int pflag = 0; /* whether the user specified a position */ |
99 | static int wflag = 0; /* whether to let the window manager control XMenu */ |
a7732690 |
100 | |
8902c43b |
101 | /* include config variable */ |
a7732690 |
102 | #include "config.h" |
103 | |
8902c43b |
104 | |
105 | /* |
106 | * Function implementations |
107 | */ |
108 | |
beae67a6 |
109 | /* xmenu: generate menu from stdin and print selected entry to stdout */ |
a7732690 |
110 | int |
111 | main(int argc, char *argv[]) |
112 | { |
257e42cc |
113 | struct Menu *rootmenu; |
3bec05ea |
114 | XClassHint classh; |
a7732690 |
115 | int ch; |
116 | |
05cfe1a0 |
117 | while ((ch = getopt(argc, argv, "ip:w")) != -1) { |
a7732690 |
118 | switch (ch) { |
71b4db92 |
119 | case 'i': |
120 | iflag = 1; |
121 | break; |
05cfe1a0 |
122 | case 'p': |
123 | pflag = 1; |
124 | parseposition(optarg); |
05cfe1a0 |
125 | break; |
3bec05ea |
126 | case 'w': |
127 | wflag = 1; |
128 | break; |
a7732690 |
129 | default: |
130 | usage(); |
131 | break; |
132 | } |
133 | } |
134 | argc -= optind; |
135 | argv += optind; |
136 | |
3bec05ea |
137 | if (argc > 1) |
c6d9174e |
138 | usage(); |
139 | |
a7732690 |
140 | /* open connection to server and set X variables */ |
141 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
142 | errx(1, "cannot open display"); |
143 | screen = DefaultScreen(dpy); |
dbeb9940 |
144 | visual = DefaultVisual(dpy, screen); |
a7732690 |
145 | rootwin = RootWindow(dpy, screen); |
146 | colormap = DefaultColormap(dpy, screen); |
21a9aaec |
147 | |
33376f54 |
148 | /* imlib2 stuff */ |
71b4db92 |
149 | if (!iflag) { |
150 | imlib_set_cache_size(2048 * 1024); |
151 | imlib_context_set_dither(1); |
152 | imlib_context_set_display(dpy); |
153 | imlib_context_set_visual(visual); |
154 | imlib_context_set_colormap(colormap); |
155 | } |
33376f54 |
156 | |
8902c43b |
157 | /* initializers */ |
158 | initresources(); |
159 | initdc(); |
71b4db92 |
160 | initconfig(); |
8902c43b |
161 | initatoms(); |
a7732690 |
162 | |
3bec05ea |
163 | /* set window class */ |
164 | classh.res_class = PROGNAME; |
165 | if (argc == 1) |
166 | classh.res_name = *argv; |
167 | else |
168 | classh.res_name = PROGNAME; |
169 | |
f8ffe0b2 |
170 | /* generate menus and set them up */ |
257e42cc |
171 | rootmenu = parsestdin(); |
a7732690 |
172 | if (rootmenu == NULL) |
173 | errx(1, "no menu generated"); |
8902c43b |
174 | setupmenu(rootmenu, &classh); |
a7732690 |
175 | |
465d07db |
176 | /* grab mouse and keyboard */ |
3bec05ea |
177 | if (!wflag) { |
178 | grabpointer(); |
179 | grabkeyboard(); |
180 | } |
465d07db |
181 | |
a7732690 |
182 | /* run event loop */ |
257e42cc |
183 | run(rootmenu); |
a7732690 |
184 | |
15dfafdf |
185 | /* freeing stuff */ |
8902c43b |
186 | cleanmenu(rootmenu); |
15dfafdf |
187 | cleanup(); |
188 | |
21a9aaec |
189 | return 0; |
190 | } |
191 | |
05cfe1a0 |
192 | /* parse position string from -p, put results on config.x and config.y */ |
193 | static void |
194 | parseposition(const char *optarg) |
195 | { |
196 | long n; |
197 | const char *s = optarg; |
198 | char *endp; |
199 | |
200 | n = strtol(s, &endp, 10); |
201 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x') |
202 | goto error; |
203 | config.posx = n; |
204 | s = endp+1; |
205 | n = strtol(s, &endp, 10); |
206 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0') |
207 | goto error; |
208 | config.posy = n; |
209 | |
210 | return; |
211 | |
212 | error: |
213 | errx(1, "improper position: %s", optarg); |
214 | } |
215 | |
cdeaefaa |
216 | /* parse color string */ |
217 | static void |
218 | parsefonts(const char *s) |
219 | { |
220 | const char *p; |
221 | char buf[1024]; |
222 | size_t nfont = 0; |
223 | |
224 | dc.nfonts = 1; |
225 | for (p = s; *p; p++) |
226 | if (*p == ',') |
227 | dc.nfonts++; |
228 | |
229 | if ((dc.fonts = calloc(dc.nfonts, sizeof *dc.fonts)) == NULL) |
230 | err(1, "calloc"); |
231 | |
232 | p = s; |
233 | while (*p != '\0') { |
234 | size_t i; |
235 | |
236 | i = 0; |
237 | while (isspace(*p)) |
238 | p++; |
239 | while (*p != '\0' && *p != ',') { |
240 | buf[i++] = *p++; |
241 | } |
242 | if (*p == ',') |
243 | p++; |
244 | buf[i] = '\0'; |
245 | if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL) |
246 | errx(1, "cannot load font"); |
247 | } |
248 | } |
249 | |
8902c43b |
250 | /* get color from color string */ |
251 | static void |
252 | ealloccolor(const char *s, XftColor *color) |
253 | { |
254 | if(!XftColorAllocName(dpy, visual, colormap, s, color)) |
255 | errx(1, "cannot allocate color: %s", s); |
256 | } |
257 | |
f644b8bc |
258 | /* read xrdb for configuration options */ |
259 | static void |
8902c43b |
260 | initresources(void) |
f644b8bc |
261 | { |
262 | char *xrm; |
263 | long n; |
fd530f3f |
264 | char *type; |
265 | XrmDatabase xdb; |
266 | XrmValue xval; |
f644b8bc |
267 | |
268 | XrmInitialize(); |
fd530f3f |
269 | if ((xrm = XResourceManagerString(dpy)) == NULL) |
270 | return; |
271 | |
272 | xdb = XrmGetStringDatabase(xrm); |
273 | |
274 | if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True) |
275 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
276 | config.border_pixels = n; |
fd530f3f |
277 | if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True) |
278 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
279 | config.separator_pixels = n; |
685ca30d |
280 | if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True) |
fd530f3f |
281 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
282 | config.height_pixels = n; |
fd530f3f |
283 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
284 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
285 | config.width_pixels = n; |
eecf9b25 |
286 | if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True) |
287 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
288 | config.gap_pixels = n; |
fd530f3f |
289 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
8902c43b |
290 | config.background_color = strdup(xval.addr); |
fd530f3f |
291 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
8902c43b |
292 | config.foreground_color = strdup(xval.addr); |
fd530f3f |
293 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
8902c43b |
294 | config.selbackground_color = strdup(xval.addr); |
fd530f3f |
295 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
8902c43b |
296 | config.selforeground_color = strdup(xval.addr); |
fd530f3f |
297 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
8902c43b |
298 | config.separator_color = strdup(xval.addr); |
fd530f3f |
299 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
8902c43b |
300 | config.border_color = strdup(xval.addr); |
fd530f3f |
301 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
8902c43b |
302 | config.font = strdup(xval.addr); |
fd530f3f |
303 | |
304 | XrmDestroyDatabase(xdb); |
f644b8bc |
305 | } |
306 | |
a7732690 |
307 | /* init draw context */ |
308 | static void |
8902c43b |
309 | initdc(void) |
a7732690 |
310 | { |
311 | /* get color pixels */ |
8902c43b |
312 | ealloccolor(config.background_color, &dc.normal[ColorBG]); |
313 | ealloccolor(config.foreground_color, &dc.normal[ColorFG]); |
314 | ealloccolor(config.selbackground_color, &dc.selected[ColorBG]); |
315 | ealloccolor(config.selforeground_color, &dc.selected[ColorFG]); |
316 | ealloccolor(config.separator_color, &dc.separator); |
317 | ealloccolor(config.border_color, &dc.border); |
a7732690 |
318 | |
cdeaefaa |
319 | /* parse fonts */ |
320 | parsefonts(config.font); |
a7732690 |
321 | |
fd530f3f |
322 | /* create common GC */ |
a7732690 |
323 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
a7732690 |
324 | } |
325 | |
71b4db92 |
326 | /* calculate configuration values that are not set manually */ |
a7732690 |
327 | static void |
71b4db92 |
328 | initconfig(void) |
a7732690 |
329 | { |
8902c43b |
330 | Window dw; /* dummy variable */ |
331 | int di; /* dummy variable */ |
332 | unsigned du; /* dummy variable */ |
333 | |
05cfe1a0 |
334 | if (!pflag) /* if the user haven't specified a position, use cursor position*/ |
335 | XQueryPointer(dpy, rootwin, &dw, &dw, &config.posx, &config.posy, &di, &di, &du); |
8902c43b |
336 | config.screenw = DisplayWidth(dpy, screen); |
337 | config.screenh = DisplayHeight(dpy, screen); |
71b4db92 |
338 | config.iconsize = config.height_pixels - config.iconpadding * 2; |
8902c43b |
339 | } |
340 | |
341 | /* intern atoms */ |
342 | static void |
343 | initatoms(void) |
344 | { |
345 | utf8string = XInternAtom(dpy, "UTF8_STRING", False); |
346 | wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); |
347 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); |
348 | netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); |
349 | netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False); |
a7732690 |
350 | } |
351 | |
a7732690 |
352 | /* allocate an item */ |
353 | static struct Item * |
33376f54 |
354 | allocitem(const char *label, const char *output, char *file) |
a7732690 |
355 | { |
356 | struct Item *item; |
357 | |
358 | if ((item = malloc(sizeof *item)) == NULL) |
359 | err(1, "malloc"); |
a5ddd2cd |
360 | if (label == NULL) { |
7fbd1c5e |
361 | item->label = NULL; |
362 | item->output = NULL; |
363 | } else { |
364 | if ((item->label = strdup(label)) == NULL) |
365 | err(1, "strdup"); |
15dfafdf |
366 | if (label == output) { |
367 | item->output = item->label; |
368 | } else { |
369 | if ((item->output = strdup(output)) == NULL) |
370 | err(1, "strdup"); |
371 | } |
7fbd1c5e |
372 | } |
33376f54 |
373 | if (file == NULL) { |
374 | item->file = NULL; |
375 | } else { |
376 | if ((item->file = strdup(file)) == NULL) |
377 | err(1, "strdup"); |
378 | } |
a80fee22 |
379 | item->y = 0; |
beae67a6 |
380 | item->h = 0; |
f1583285 |
381 | if (item->label == NULL) |
382 | item->labellen = 0; |
383 | else |
384 | item->labellen = strlen(item->label); |
a7732690 |
385 | item->next = NULL; |
386 | item->submenu = NULL; |
3bec05ea |
387 | item->icon = NULL; |
a7732690 |
388 | |
389 | return item; |
390 | } |
391 | |
a3a73837 |
392 | /* allocate a menu and create its window */ |
a7732690 |
393 | static struct Menu * |
394 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
395 | { |
396 | XSetWindowAttributes swa; |
397 | struct Menu *menu; |
398 | |
399 | if ((menu = malloc(sizeof *menu)) == NULL) |
400 | err(1, "malloc"); |
401 | menu->parent = parent; |
402 | menu->list = list; |
d888f2ca |
403 | menu->caller = NULL; |
a7732690 |
404 | menu->selected = NULL; |
f8ffe0b2 |
405 | menu->w = 0; /* calculated by setupmenu() */ |
406 | menu->h = 0; /* calculated by setupmenu() */ |
407 | menu->x = 0; /* calculated by setupmenu() */ |
408 | menu->y = 0; /* calculated by setupmenu() */ |
a7732690 |
409 | menu->level = level; |
a7732690 |
410 | |
3bec05ea |
411 | swa.override_redirect = (wflag) ? False : True; |
fd530f3f |
412 | swa.background_pixel = dc.normal[ColorBG].pixel; |
413 | swa.border_pixel = dc.border.pixel; |
414 | swa.save_under = True; /* pop-up windows should save_under*/ |
a7732690 |
415 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
416 | | PointerMotionMask | LeaveWindowMask; |
3bec05ea |
417 | if (wflag) |
418 | swa.event_mask |= StructureNotifyMask; |
beae67a6 |
419 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0, |
a7732690 |
420 | CopyFromParent, CopyFromParent, CopyFromParent, |
fd530f3f |
421 | CWOverrideRedirect | CWBackPixel | |
422 | CWBorderPixel | CWEventMask | CWSaveUnder, |
a7732690 |
423 | &swa); |
424 | |
425 | return menu; |
426 | } |
427 | |
a5ddd2cd |
428 | /* build the menu tree */ |
429 | static struct Menu * |
33376f54 |
430 | buildmenutree(unsigned level, const char *label, const char *output, char *file) |
a5ddd2cd |
431 | { |
432 | static struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
433 | static struct Menu *rootmenu = NULL; /* menu to be returned */ |
434 | struct Item *curritem = NULL; /* item currently being read */ |
435 | struct Item *item; /* dummy item for loops */ |
436 | struct Menu *menu; /* dummy menu for loops */ |
437 | unsigned i; |
438 | |
439 | /* create the item */ |
33376f54 |
440 | curritem = allocitem(label, output, file); |
a5ddd2cd |
441 | |
442 | /* put the item in the menu tree */ |
443 | if (prevmenu == NULL) { /* there is no menu yet */ |
685ca30d |
444 | menu = allocmenu(NULL, curritem, level); |
445 | rootmenu = menu; |
446 | prevmenu = menu; |
447 | curritem->prev = NULL; |
a5ddd2cd |
448 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu */ |
449 | /* go up the menu tree until find the menu this item continues */ |
450 | for (menu = prevmenu, i = level; |
451 | menu != NULL && i != prevmenu->level; |
452 | menu = menu->parent, i++) |
453 | ; |
454 | if (menu == NULL) |
455 | errx(1, "reached NULL menu"); |
456 | |
457 | /* find last item in the new menu */ |
458 | for (item = menu->list; item->next != NULL; item = item->next) |
459 | ; |
460 | |
461 | prevmenu = menu; |
462 | item->next = curritem; |
463 | curritem->prev = item; |
464 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
465 | /* find last item in the previous menu */ |
466 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
467 | ; |
468 | |
469 | item->next = curritem; |
470 | curritem->prev = item; |
471 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
472 | menu = allocmenu(prevmenu, curritem, level); |
473 | |
474 | /* find last item in the previous menu */ |
475 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
476 | ; |
477 | |
478 | prevmenu = menu; |
479 | menu->caller = item; |
480 | item->submenu = menu; |
481 | curritem->prev = NULL; |
482 | } |
483 | |
484 | return rootmenu; |
485 | } |
486 | |
a7732690 |
487 | /* create menus and items from the stdin */ |
257e42cc |
488 | static struct Menu * |
a7732690 |
489 | parsestdin(void) |
490 | { |
a5ddd2cd |
491 | struct Menu *rootmenu; |
a7732690 |
492 | char *s, buf[BUFSIZ]; |
33376f54 |
493 | char *file, *label, *output; |
a7732690 |
494 | unsigned level = 0; |
257e42cc |
495 | |
ddb15acf |
496 | rootmenu = NULL; |
497 | |
a7732690 |
498 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
a5ddd2cd |
499 | /* get the indentation level */ |
500 | level = strspn(buf, "\t"); |
a7732690 |
501 | |
a5ddd2cd |
502 | /* get the label */ |
503 | s = level + buf; |
504 | label = strtok(s, "\t\n"); |
a7732690 |
505 | |
33376f54 |
506 | /* get the filename */ |
507 | file = NULL; |
508 | if (label != NULL && strncmp(label, "IMG:", 4) == 0) { |
509 | file = label + 4; |
510 | label = strtok(NULL, "\t\n"); |
511 | } |
512 | |
a5ddd2cd |
513 | /* get the output */ |
514 | output = strtok(NULL, "\n"); |
515 | if (output == NULL) { |
516 | output = label; |
517 | } else { |
518 | while (*output == '\t') |
519 | output++; |
a7732690 |
520 | } |
a5ddd2cd |
521 | |
33376f54 |
522 | rootmenu = buildmenutree(level, label, output, file); |
a7732690 |
523 | } |
a7732690 |
524 | |
257e42cc |
525 | return rootmenu; |
a7732690 |
526 | } |
527 | |
3bec05ea |
528 | /* load and scale icon */ |
33376f54 |
529 | static Imlib_Image |
71b4db92 |
530 | loadicon(const char *file) |
33376f54 |
531 | { |
3bec05ea |
532 | Imlib_Image icon; |
33376f54 |
533 | int width; |
534 | int height; |
535 | int imgsize; |
536 | |
3bec05ea |
537 | icon = imlib_load_image(file); |
538 | if (icon == NULL) |
539 | errx(1, "cannot load icon %s", file); |
33376f54 |
540 | |
3bec05ea |
541 | imlib_context_set_image(icon); |
33376f54 |
542 | |
543 | width = imlib_image_get_width(); |
544 | height = imlib_image_get_height(); |
545 | imgsize = MIN(width, height); |
546 | |
71b4db92 |
547 | icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize, |
548 | config.iconsize, |
549 | config.iconsize); |
33376f54 |
550 | |
3bec05ea |
551 | return icon; |
33376f54 |
552 | } |
553 | |
cdeaefaa |
554 | /* get next utf8 char from s return its codepoint and set next_ret to pointer to next character */ |
555 | static FcChar32 |
556 | getnextutf8char(const char *s, const char **next_ret) |
557 | { |
558 | /* */ |
559 | static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0}; |
560 | /* */ |
561 | static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; |
562 | /* 0xFFFD is the replacement character, used to represent unknown characters */ |
563 | static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000}; |
564 | static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; |
565 | static const FcChar32 unknown = 0xFFFD; |
566 | FcChar32 ucode; /* FcChar32 type holds 32 bits */ |
567 | size_t usize = 0; /* n' of bytes of the utf8 character */ |
568 | size_t i; |
569 | |
570 | *next_ret = s+1; |
571 | |
572 | /* get code of first byte of utf8 character */ |
573 | for (i = 0; i < sizeof utfmask; i++) { |
574 | if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) { |
575 | usize = i; |
576 | ucode = (unsigned char)*s & ~utfmask[i]; |
577 | break; |
578 | } |
579 | } |
580 | |
581 | /* if first byte is a continuation byte or is not allowed, return unknown */ |
582 | if (i == sizeof utfmask || usize == 0) |
583 | return unknown; |
584 | |
585 | /* check the other usize-1 bytes */ |
586 | s++; |
587 | for (i = 1; i < usize; i++) { |
588 | *next_ret = s+1; |
589 | /* if byte is EOS or is not a continuation byte, return unknown */ |
590 | if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0]) |
591 | return unknown; |
592 | /* 6 is the number of relevant bits in the continuation byte */ |
593 | ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]); |
594 | s++; |
595 | } |
596 | |
597 | /* check if ucode is invalid or in utf-16 surrogate halves */ |
598 | if (!BETWEEN(ucode, utfmin[usize], utfmax[usize]) |
599 | || BETWEEN (ucode, 0xD800, 0xDFFF)) |
600 | return unknown; |
601 | |
602 | return ucode; |
603 | } |
604 | |
605 | /* draw text into XftDraw */ |
606 | static int |
607 | drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text) |
608 | { |
609 | const char *s, *nexts; |
610 | FcChar32 ucode; |
611 | XftFont *currfont; |
612 | int textlen = 0; |
613 | |
614 | s = text; |
615 | while (*s) { |
616 | XGlyphInfo ext; |
617 | int charexists; |
618 | size_t len; |
619 | size_t i; |
620 | |
621 | charexists = 0; |
622 | ucode = getnextutf8char(s, &nexts); |
623 | for (i = 0; i < dc.nfonts; i++) { |
624 | charexists = XftCharExists(dpy, dc.fonts[i], ucode); |
625 | if (charexists) |
626 | break; |
627 | } |
628 | if (charexists) |
629 | currfont = dc.fonts[i]; |
630 | |
631 | len = nexts - s; |
632 | |
633 | XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)s, |
634 | len, &ext); |
635 | textlen += ext.xOff; |
636 | |
637 | if (draw) { |
638 | int texty; |
639 | |
640 | texty = y + (h + currfont->ascent) / 2; |
641 | XftDrawStringUtf8(draw, color, currfont, x, texty, |
642 | (XftChar8 *)s, len); |
643 | x += ext.xOff; |
644 | } |
645 | |
646 | s = nexts; |
647 | } |
648 | |
649 | return textlen; |
650 | } |
651 | |
652 | /* draw pixmap for the selected and unselected version of each item on menu */ |
653 | static void |
654 | drawitems(struct Menu *menu) |
655 | { |
656 | struct Item *item; |
657 | |
658 | for (item = menu->list; item != NULL; item = item->next) { |
659 | XftDraw *dsel, *dunsel; |
660 | int x, y; |
661 | |
662 | item->unsel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
663 | DefaultDepth(dpy, screen)); |
664 | |
665 | XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel); |
666 | XFillRectangle(dpy, item->unsel, dc.gc, 0, 0, menu->w, item->h); |
667 | |
668 | if (item->label == NULL) { /* item is separator */ |
669 | y = item->h/2; |
670 | XSetForeground(dpy, dc.gc, dc.separator.pixel); |
671 | XDrawLine(dpy, item->unsel, dc.gc, config.horzpadding, y, |
672 | menu->w - config.horzpadding, y); |
673 | |
674 | item->sel = item->unsel; |
675 | } else { |
676 | |
677 | item->sel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
678 | DefaultDepth(dpy, screen)); |
679 | XSetForeground(dpy, dc.gc, dc.selected[ColorBG].pixel); |
680 | XFillRectangle(dpy, item->sel, dc.gc, 0, 0, menu->w, item->h); |
681 | |
682 | /* draw text */ |
683 | x = config.horzpadding; |
684 | x += (iflag) ? 0 : config.horzpadding + config.iconsize; |
685 | dsel = XftDrawCreate(dpy, item->sel, visual, colormap); |
686 | dunsel = XftDrawCreate(dpy, item->unsel, visual, colormap); |
687 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
688 | drawtext(dsel, &dc.selected[ColorFG], x, 0, item->h, item->label); |
689 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
690 | drawtext(dunsel, &dc.normal[ColorFG], x, 0, item->h, item->label); |
691 | XftDrawDestroy(dsel); |
692 | XftDrawDestroy(dunsel); |
693 | |
694 | /* draw triangle */ |
695 | if (item->submenu != NULL) { |
696 | x = menu->w - config.triangle_width - config.horzpadding; |
697 | y = (item->h - config.triangle_height + 1) / 2; |
698 | |
699 | XPoint triangle[] = { |
700 | {x, y}, |
701 | {x + config.triangle_width, y + config.triangle_height/2}, |
702 | {x, y + config.triangle_height}, |
703 | {x, y} |
704 | }; |
705 | |
706 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
707 | XFillPolygon(dpy, item->sel, dc.gc, triangle, LEN(triangle), |
708 | Convex, CoordModeOrigin); |
709 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
710 | XFillPolygon(dpy, item->unsel, dc.gc, triangle, LEN(triangle), |
711 | Convex, CoordModeOrigin); |
712 | } |
713 | |
714 | /* draw icon */ |
715 | if (item->file != NULL && !iflag) { |
716 | item->icon = loadicon(item->file); |
717 | |
718 | imlib_context_set_drawable(item->sel); |
719 | imlib_context_set_image(item->icon); |
720 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
721 | |
722 | imlib_context_set_drawable(item->unsel); |
723 | imlib_context_set_image(item->icon); |
724 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
725 | } |
726 | } |
727 | } |
728 | } |
729 | |
71b4db92 |
730 | /* setup the height, width and icon of the items of a menu */ |
a7732690 |
731 | static void |
71b4db92 |
732 | setupitems(struct Menu *menu) |
a7732690 |
733 | { |
dbeb9940 |
734 | XGlyphInfo ext; |
a80fee22 |
735 | struct Item *item; |
71b4db92 |
736 | int itemwidth; |
a7732690 |
737 | |
8902c43b |
738 | menu->w = config.width_pixels; |
a80fee22 |
739 | for (item = menu->list; item != NULL; item = item->next) { |
740 | item->y = menu->h; |
beae67a6 |
741 | |
7fbd1c5e |
742 | if (item->label == NULL) /* height for separator item */ |
8902c43b |
743 | item->h = config.separator_pixels; |
a80fee22 |
744 | else |
8902c43b |
745 | item->h = config.height_pixels; |
beae67a6 |
746 | menu->h += item->h; |
f1583285 |
747 | |
f8ffe0b2 |
748 | /* get length of item->label rendered in the font */ |
cdeaefaa |
749 | XftTextExtentsUtf8(dpy, dc.fonts[0], (XftChar8 *)item->label, |
dbeb9940 |
750 | item->labellen, &ext); |
685ca30d |
751 | |
71b4db92 |
752 | /* |
753 | * set menu width |
754 | * |
755 | * the item width depends on the size of its label (ext.xOff), |
756 | * and it is only used to calculate the width of the menu (which |
757 | * is equal to the width of the largest item). |
758 | * |
759 | * the horizontal padding appears 4 times through the width of a |
760 | * item: before and after its icon, and before and after its triangle |
761 | * if the iflag is set (icons are disabled) then the horizontal |
762 | * padding appears before the label and around the triangle. |
763 | */ |
764 | itemwidth = ext.xOff + config.triangle_width + config.horzpadding * 3; |
765 | itemwidth += (iflag) ? 0 : config.iconsize + config.horzpadding; |
766 | menu->w = MAX(menu->w, itemwidth); |
a80fee22 |
767 | } |
f8ffe0b2 |
768 | } |
769 | |
770 | /* setup the position of a menu */ |
771 | static void |
8902c43b |
772 | setupmenupos(struct Menu *menu) |
f8ffe0b2 |
773 | { |
774 | int width, height; |
a7732690 |
775 | |
8902c43b |
776 | width = menu->w + config.border_pixels * 2; |
777 | height = menu->h + config.border_pixels * 2; |
a7732690 |
778 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
05cfe1a0 |
779 | if (pflag || config.screenw - config.posx >= menu->w) |
780 | menu->x = config.posx; |
781 | else if (config.posx > width) |
782 | menu->x = config.posx - width; |
8902c43b |
783 | |
05cfe1a0 |
784 | if (pflag || config.screenh - config.posy >= height) |
785 | menu->y = config.posy; |
8902c43b |
786 | else if (config.screenh > height) |
787 | menu->y = config.screenh - height; |
a7732690 |
788 | } else { /* else, calculate in respect to parent menu */ |
8902c43b |
789 | if (config.screenw - (menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels) >= width) |
790 | menu->x = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels; |
791 | else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels) |
792 | menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels; |
a7732690 |
793 | |
8902c43b |
794 | if (config.screenh - (menu->caller->y + menu->parent->y) > height) |
8455c369 |
795 | menu->y = menu->caller->y + menu->parent->y; |
8902c43b |
796 | else if (config.screenh - menu->parent->y > height) |
a7732690 |
797 | menu->y = menu->parent->y; |
8902c43b |
798 | else if (config.screenh > height) |
799 | menu->y = config.screenh - height; |
a7732690 |
800 | } |
f8ffe0b2 |
801 | } |
802 | |
803 | /* recursivelly setup menu configuration and its pixmap */ |
804 | static void |
8902c43b |
805 | setupmenu(struct Menu *menu, XClassHint *classh) |
f8ffe0b2 |
806 | { |
8902c43b |
807 | char *title; |
f8ffe0b2 |
808 | struct Item *item; |
f8ffe0b2 |
809 | XWindowChanges changes; |
810 | XSizeHints sizeh; |
3bec05ea |
811 | XTextProperty wintitle; |
f8ffe0b2 |
812 | |
813 | /* setup size and position of menus */ |
71b4db92 |
814 | setupitems(menu); |
cdeaefaa |
815 | drawitems(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 */ |
09c13122 |
835 | sizeh.flags = PMaxSize | PMinSize; |
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 | } |
869 | errx(1, "cannot grab keyboard"); |
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 | } |
885 | errx(1, "cannot grab keyboard"); |
886 | } |
887 | |
257e42cc |
888 | /* umap previous menus and map current menu and its parents */ |
a7732690 |
889 | static void |
257e42cc |
890 | mapmenu(struct Menu *currmenu) |
a7732690 |
891 | { |
257e42cc |
892 | static struct Menu *prevmenu = NULL; |
d8a7caf2 |
893 | struct Menu *menu, *menu_; |
d8a7caf2 |
894 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
895 | unsigned minlevel; /* level of the closest to root menu */ |
896 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
897 | |
257e42cc |
898 | /* do not remap current menu if it wasn't updated*/ |
899 | if (prevmenu == currmenu) |
a7732690 |
900 | return; |
901 | |
257e42cc |
902 | /* if this is the first time mapping, skip calculations */ |
903 | if (prevmenu == NULL) { |
fd530f3f |
904 | XMapWindow(dpy, currmenu->win); |
cdc4d51f |
905 | prevmenu = currmenu; |
906 | return; |
27443900 |
907 | } |
908 | |
d8a7caf2 |
909 | /* find lowest common ancestor menu */ |
257e42cc |
910 | minlevel = MIN(currmenu->level, prevmenu->level); |
911 | maxlevel = MAX(currmenu->level, prevmenu->level); |
912 | if (currmenu->level == maxlevel) { |
27443900 |
913 | menu = currmenu; |
257e42cc |
914 | menu_ = prevmenu; |
915 | } else { |
916 | menu = prevmenu; |
917 | menu_ = currmenu; |
27443900 |
918 | } |
919 | while (menu->level > minlevel) |
920 | menu = menu->parent; |
921 | while (menu != menu_) { |
922 | menu = menu->parent; |
923 | menu_ = menu_->parent; |
d8a7caf2 |
924 | } |
27443900 |
925 | lcamenu = menu; |
d8a7caf2 |
926 | |
873c080c |
927 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
257e42cc |
928 | for (menu = prevmenu; menu != lcamenu; menu = menu->parent) { |
8455c369 |
929 | menu->selected = NULL; |
a7732690 |
930 | XUnmapWindow(dpy, menu->win); |
931 | } |
932 | |
873c080c |
933 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d8a7caf2 |
934 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
3bec05ea |
935 | |
936 | if (wflag) { |
8902c43b |
937 | setupmenupos(menu); |
3bec05ea |
938 | XMoveWindow(dpy, menu->win, menu->x, menu->y); |
939 | } |
940 | |
a7732690 |
941 | XMapWindow(dpy, menu->win); |
fd530f3f |
942 | } |
257e42cc |
943 | |
257e42cc |
944 | prevmenu = currmenu; |
fd530f3f |
945 | } |
946 | |
cdeaefaa |
947 | /* copy pixmaps of items of the current menu and of its ancestors into menu window */ |
a7732690 |
948 | static void |
cdeaefaa |
949 | copypixmaps(struct Menu *currmenu) |
a7732690 |
950 | { |
951 | struct Menu *menu; |
952 | struct Item *item; |
953 | |
954 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
955 | for (item = menu->list; item != NULL; item = item->next) { |
cdeaefaa |
956 | if (item == menu->selected) |
957 | XCopyArea(dpy, item->sel, menu->win, dc.gc, 0, 0, |
958 | menu->w, item->h, 0, item->y); |
a7732690 |
959 | else |
cdeaefaa |
960 | XCopyArea(dpy, item->unsel, menu->win, dc.gc, 0, 0, |
961 | menu->w, item->h, 0, item->y); |
a7732690 |
962 | } |
963 | } |
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) |
cdeaefaa |
1052 | copypixmaps(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); |
cdeaefaa |
1068 | copypixmaps(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; |
cdeaefaa |
1086 | copypixmaps(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; |
cdeaefaa |
1122 | copypixmaps(currmenu); |
a7732690 |
1123 | break; |
f15fc339 |
1124 | case LeaveNotify: |
fd530f3f |
1125 | previtem = NULL; |
f15fc339 |
1126 | currmenu->selected = NULL; |
cdeaefaa |
1127 | copypixmaps(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; |
cdeaefaa |
1162 | XFreePixmap(dpy, item->unsel); |
1163 | if (tmp->label != NULL) |
1164 | XFreePixmap(dpy, item->sel); |
15dfafdf |
1165 | if (tmp->label != tmp->output) |
1166 | free(tmp->label); |
6b5123e7 |
1167 | free(tmp->output); |
33376f54 |
1168 | if (tmp->file != NULL) { |
1169 | free(tmp->file); |
3bec05ea |
1170 | if (tmp->icon != NULL) { |
1171 | imlib_context_set_image(tmp->icon); |
33376f54 |
1172 | imlib_free_image(); |
1173 | } |
1174 | } |
1175 | item = item->next; |
ec4ed1ac |
1176 | free(tmp); |
1177 | } |
f15fc339 |
1178 | |
f15fc339 |
1179 | XDestroyWindow(dpy, menu->win); |
ec4ed1ac |
1180 | free(menu); |
f15fc339 |
1181 | } |
1182 | |
8902c43b |
1183 | /* cleanup X and exit */ |
a7732690 |
1184 | static void |
15dfafdf |
1185 | cleanup(void) |
a7732690 |
1186 | { |
257e42cc |
1187 | XUngrabPointer(dpy, CurrentTime); |
1188 | XUngrabKeyboard(dpy, CurrentTime); |
1189 | |
dbeb9940 |
1190 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); |
1191 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); |
1192 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); |
1193 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]); |
fd530f3f |
1194 | XftColorFree(dpy, visual, colormap, &dc.separator); |
1195 | XftColorFree(dpy, visual, colormap, &dc.border); |
dbeb9940 |
1196 | |
f15fc339 |
1197 | XFreeGC(dpy, dc.gc); |
a7732690 |
1198 | XCloseDisplay(dpy); |
a7732690 |
1199 | } |
1200 | |
1201 | /* show usage */ |
1202 | static void |
1203 | usage(void) |
1204 | { |
05cfe1a0 |
1205 | (void)fprintf(stderr, "usage: xmenu [-iw] [-p position] [title]\n"); |
a7732690 |
1206 | exit(1); |
1207 | } |