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