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