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