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