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> |
858338d9 |
11 | |
d2435fcd |
12 | #define PROGNAME "xmenu" |
858338d9 |
13 | #define ITEMPREV 0 |
14 | #define ITEMNEXT 1 |
a7732690 |
15 | |
16 | /* macros */ |
17 | #define LEN(x) (sizeof (x) / sizeof (x[0])) |
f1583285 |
18 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
d8a7caf2 |
19 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
a7732690 |
20 | |
21 | /* color enum */ |
22 | enum {ColorFG, ColorBG, ColorLast}; |
23 | |
24 | /* draw context structure */ |
25 | struct DC { |
dbeb9940 |
26 | XftColor normal[ColorLast]; |
27 | XftColor selected[ColorLast]; |
28 | XftColor decoration[ColorLast]; |
a7732690 |
29 | |
30 | Drawable d; |
31 | GC gc; |
dbeb9940 |
32 | XftFont *font; |
a7732690 |
33 | }; |
34 | |
35 | /* menu geometry structure */ |
36 | struct Geometry { |
37 | int itemb; /* item border */ |
38 | int itemw; /* item width */ |
39 | int itemh; /* item height */ |
a80fee22 |
40 | int border; /* window border width */ |
41 | int separator; /* menu separator width */ |
a7732690 |
42 | }; |
43 | |
44 | /* screen geometry structure */ |
45 | struct ScreenGeometry { |
46 | int cursx, cursy; /* cursor position */ |
47 | int screenw, screenh; /* screen width and height */ |
48 | }; |
49 | |
50 | /* menu item structure */ |
51 | struct Item { |
d8a7caf2 |
52 | char *label; /* string to be drawed on menu */ |
53 | char *output; /* string to be outputed when item is clicked */ |
54 | int y; /* item y position relative to menu */ |
55 | int h; /* item height */ |
56 | size_t labellen; /* strlen(label) */ |
858338d9 |
57 | struct Item *prev; /* previous item */ |
d8a7caf2 |
58 | struct Item *next; /* next item */ |
59 | struct Menu *submenu; /* submenu spawned by clicking on item */ |
a7732690 |
60 | }; |
61 | |
62 | /* menu structure */ |
63 | struct Menu { |
d8a7caf2 |
64 | struct Menu *parent; /* parent menu */ |
65 | struct Item *caller; /* item that spawned the menu */ |
66 | struct Item *list; /* list of items contained by the menu */ |
67 | struct Item *selected; /* item currently selected in the menu */ |
68 | int x, y, w, h; /* menu geometry */ |
69 | unsigned level; /* menu level relative to root */ |
70 | Drawable pixmap; /* pixmap to draw the menu on */ |
dbeb9940 |
71 | XftDraw *draw; |
d8a7caf2 |
72 | Window win; /* menu window to map on the screen */ |
a7732690 |
73 | }; |
74 | |
75 | /* function declarations */ |
dbeb9940 |
76 | static void getcolor(const char *s, XftColor *color); |
f644b8bc |
77 | static void getresources(void); |
a7732690 |
78 | static void setupdc(void); |
79 | static void setupgeom(void); |
80 | static void setupgrab(void); |
a80fee22 |
81 | static struct Item *allocitem(const char *label, const char *output); |
a7732690 |
82 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
f15fc339 |
83 | static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret); |
a7732690 |
84 | static void drawmenu(void); |
85 | static void calcscreengeom(void); |
86 | static void calcmenu(struct Menu *menu); |
87 | static void setcurrmenu(struct Menu *currmenu_new); |
88 | static void parsestdin(void); |
89 | static void run(void); |
f15fc339 |
90 | static void freewindow(struct Menu *menu); |
08f16589 |
91 | static void cleanup(void); |
a7732690 |
92 | static void usage(void); |
93 | |
94 | /* X variables */ |
95 | static Colormap colormap; |
96 | static Display *dpy; |
dbeb9940 |
97 | static Visual *visual; |
a7732690 |
98 | static Window rootwin; |
99 | static int screen; |
100 | static struct DC dc; |
101 | |
102 | /* menu variables */ |
103 | static struct Menu *rootmenu = NULL; |
104 | static struct Menu *currmenu = NULL; |
d2435fcd |
105 | static char **menutitle; |
106 | static int menutitlecount; |
a7732690 |
107 | |
108 | /* geometry variables */ |
109 | static struct Geometry geom; |
a80fee22 |
110 | static struct ScreenGeometry screengeom; |
a7732690 |
111 | |
112 | /* flag variables */ |
113 | static Bool override_redirect = True; |
114 | |
115 | #include "config.h" |
116 | |
117 | int |
118 | main(int argc, char *argv[]) |
119 | { |
120 | int ch; |
121 | |
122 | while ((ch = getopt(argc, argv, "w")) != -1) { |
123 | switch (ch) { |
124 | case 'w': |
125 | override_redirect = False; |
126 | break; |
127 | default: |
128 | usage(); |
129 | break; |
130 | } |
131 | } |
132 | argc -= optind; |
133 | argv += optind; |
134 | |
d2435fcd |
135 | menutitle = argv; |
136 | menutitlecount = argc; |
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); |
145 | |
146 | /* setup */ |
f644b8bc |
147 | getresources(); |
a7732690 |
148 | setupdc(); |
149 | setupgeom(); |
a7732690 |
150 | |
151 | /* generate menus and recalculate them */ |
152 | parsestdin(); |
153 | if (rootmenu == NULL) |
154 | errx(1, "no menu generated"); |
155 | calcscreengeom(); |
156 | calcmenu(rootmenu); |
157 | |
465d07db |
158 | /* grab mouse and keyboard */ |
159 | if (override_redirect) |
160 | setupgrab(); |
161 | |
d8a7caf2 |
162 | /* map root menu */ |
163 | currmenu = rootmenu; |
164 | XMapWindow(dpy, rootmenu->win); |
165 | |
a7732690 |
166 | /* run event loop */ |
167 | run(); |
168 | |
08f16589 |
169 | cleanup(); |
170 | |
171 | return 0; |
a7732690 |
172 | } |
173 | |
f644b8bc |
174 | /* read xrdb for configuration options */ |
175 | static void |
176 | getresources(void) |
177 | { |
178 | char *xrm; |
179 | long n; |
180 | |
181 | XrmInitialize(); |
182 | if ((xrm = XResourceManagerString(dpy))) { |
183 | char *type; |
184 | XrmDatabase xdb; |
185 | XrmValue xval; |
186 | |
187 | xdb = XrmGetStringDatabase(xrm); |
188 | |
189 | if (XrmGetResource(xdb, "xmenu.menuborder", "*", &type, &xval) == True) |
190 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
191 | menuborder = n; |
192 | if (XrmGetResource(xdb, "xmenu.separatorsize", "*", &type, &xval) == True) |
193 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
194 | separatorsize = n; |
195 | if (XrmGetResource(xdb, "xmenu.itemborder", "*", &type, &xval) == True) |
196 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
197 | itemborder = n; |
198 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
199 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
200 | width = n; |
201 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
202 | background = strdup(xval.addr); |
203 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
204 | foreground = strdup(xval.addr); |
205 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
206 | selbackground = strdup(xval.addr); |
207 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
208 | selforeground = strdup(xval.addr); |
209 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
210 | separator = strdup(xval.addr); |
211 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
212 | border = strdup(xval.addr); |
213 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
214 | font = strdup(xval.addr); |
215 | |
216 | XrmDestroyDatabase(xdb); |
217 | } |
218 | } |
219 | |
a7732690 |
220 | /* get color from color string */ |
dbeb9940 |
221 | static void |
222 | getcolor(const char *s, XftColor *color) |
a7732690 |
223 | { |
dbeb9940 |
224 | if(!XftColorAllocName(dpy, visual, colormap, s, color)) |
a7732690 |
225 | errx(1, "cannot allocate color: %s", s); |
a7732690 |
226 | } |
227 | |
228 | /* init draw context */ |
229 | static void |
230 | setupdc(void) |
231 | { |
232 | /* get color pixels */ |
dbeb9940 |
233 | getcolor(background, &dc.normal[ColorBG]); |
234 | getcolor(foreground, &dc.normal[ColorFG]); |
235 | getcolor(selbackground, &dc.selected[ColorBG]); |
236 | getcolor(selforeground, &dc.selected[ColorFG]); |
237 | getcolor(separator, &dc.decoration[ColorBG]); |
238 | getcolor(border, &dc.decoration[ColorFG]); |
a7732690 |
239 | |
240 | /* try to get font */ |
dbeb9940 |
241 | if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL) |
a7732690 |
242 | errx(1, "cannot load font"); |
a7732690 |
243 | |
dbeb9940 |
244 | /* create GC */ |
a7732690 |
245 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
a7732690 |
246 | } |
247 | |
248 | /* init menu geometry values */ |
249 | static void |
250 | setupgeom(void) |
251 | { |
f644b8bc |
252 | geom.itemb = itemborder; |
dbeb9940 |
253 | geom.itemh = dc.font->height + itemborder * 2; |
f644b8bc |
254 | geom.itemw = width; |
255 | geom.border = menuborder; |
256 | geom.separator = separatorsize; |
a7732690 |
257 | } |
258 | |
259 | /* grab pointer */ |
260 | static void |
261 | setupgrab(void) |
262 | { |
858338d9 |
263 | if (XGrabPointer(dpy, rootwin, True, ButtonPressMask, |
264 | GrabModeAsync, GrabModeAsync, None, |
265 | None, CurrentTime) != GrabSuccess) |
266 | errx(1, "cannot grab pointer"); |
267 | if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync, |
268 | GrabModeAsync, CurrentTime) != GrabSuccess) |
269 | errx(1, "cannot grab keyboard"); |
a7732690 |
270 | } |
271 | |
272 | /* allocate an item */ |
273 | static struct Item * |
a80fee22 |
274 | allocitem(const char *label, const char *output) |
a7732690 |
275 | { |
276 | struct Item *item; |
277 | |
278 | if ((item = malloc(sizeof *item)) == NULL) |
279 | err(1, "malloc"); |
7fbd1c5e |
280 | if (*label == '\0') { |
281 | item->label = NULL; |
282 | item->output = NULL; |
283 | } else { |
284 | if ((item->label = strdup(label)) == NULL) |
285 | err(1, "strdup"); |
286 | if ((item->output = strdup(output)) == NULL) |
287 | err(1, "strdup"); |
288 | } |
a80fee22 |
289 | item->y = 0; |
7fbd1c5e |
290 | item->h = item->label ? geom.itemh : geom.separator; |
f1583285 |
291 | if (item->label == NULL) |
292 | item->labellen = 0; |
293 | else |
294 | item->labellen = strlen(item->label); |
a7732690 |
295 | item->next = NULL; |
296 | item->submenu = NULL; |
297 | |
298 | return item; |
299 | } |
300 | |
301 | /* allocate a menu */ |
302 | static struct Menu * |
303 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
304 | { |
305 | XSetWindowAttributes swa; |
306 | struct Menu *menu; |
307 | |
308 | if ((menu = malloc(sizeof *menu)) == NULL) |
309 | err(1, "malloc"); |
310 | menu->parent = parent; |
311 | menu->list = list; |
d888f2ca |
312 | menu->caller = NULL; |
a7732690 |
313 | menu->selected = NULL; |
a7732690 |
314 | menu->w = geom.itemw; |
a80fee22 |
315 | menu->h = 0; /* calculated by calcmenu() */ |
316 | menu->x = 0; /* calculated by calcmenu() */ |
317 | menu->y = 0; /* calculated by calcmenu() */ |
a7732690 |
318 | menu->level = level; |
a7732690 |
319 | |
320 | swa.override_redirect = override_redirect; |
dbeb9940 |
321 | swa.background_pixel = dc.decoration[ColorBG].pixel; |
322 | swa.border_pixel = dc.decoration[ColorFG].pixel; |
a7732690 |
323 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
324 | | PointerMotionMask | LeaveWindowMask; |
a7732690 |
325 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border, |
326 | CopyFromParent, CopyFromParent, CopyFromParent, |
327 | CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, |
328 | &swa); |
329 | |
330 | return menu; |
331 | } |
332 | |
333 | /* create menus and items from the stdin */ |
334 | static void |
335 | parsestdin(void) |
336 | { |
337 | char *s, buf[BUFSIZ]; |
338 | char *label, *output; |
339 | unsigned level = 0; |
340 | unsigned i; |
a80fee22 |
341 | struct Item *curritem = NULL; /* item currently being read */ |
342 | struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
343 | struct Item *item; /* dummy item for for loops */ |
344 | struct Menu *menu; /* dummy menu for for loops */ |
a7732690 |
345 | size_t count = 0; /* number of items in the current menu */ |
346 | |
347 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
348 | level = 0; |
349 | s = buf; |
350 | |
351 | while (*s == '\t') { |
352 | level++; |
353 | s++; |
354 | } |
355 | |
356 | label = output = s; |
357 | |
358 | while (*s != '\0' && *s != '\t' && *s != '\n') |
359 | s++; |
360 | |
361 | while (*s == '\t') |
362 | *s++ = '\0'; |
363 | |
364 | if (*s != '\0' && *s != '\n') |
365 | output = s; |
366 | |
367 | while (*s != '\0' && *s != '\n') |
368 | s++; |
369 | |
370 | if (*s == '\n') |
371 | *s = '\0'; |
372 | |
a80fee22 |
373 | curritem = allocitem(label, output); |
a7732690 |
374 | |
375 | if (prevmenu == NULL) { /* there is no menu yet */ |
a80fee22 |
376 | menu = allocmenu(NULL, curritem, level); |
a7732690 |
377 | rootmenu = menu; |
378 | prevmenu = menu; |
379 | count = 1; |
858338d9 |
380 | curritem->prev = NULL; |
381 | curritem->next = NULL; |
a7732690 |
382 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/ |
383 | for (menu = prevmenu, i = level; |
384 | menu != NULL && i < prevmenu->level; |
385 | menu = menu->parent, i++) |
386 | ; |
387 | |
388 | if (menu == NULL) |
389 | errx(1, "reached NULL menu"); |
390 | |
a80fee22 |
391 | for (item = menu->list; item->next != NULL; item = item->next) |
a7732690 |
392 | ; |
393 | |
a80fee22 |
394 | item->next = curritem; |
858338d9 |
395 | |
396 | curritem->prev = item; |
397 | curritem->next = NULL; |
398 | |
a7732690 |
399 | prevmenu = menu; |
400 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
a80fee22 |
401 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
402 | ; |
a80fee22 |
403 | item->next = curritem; |
858338d9 |
404 | |
405 | curritem->prev = item; |
406 | curritem->next = NULL; |
407 | |
a7732690 |
408 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
a80fee22 |
409 | menu = allocmenu(prevmenu, curritem, level); |
a7732690 |
410 | |
a80fee22 |
411 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
412 | ; |
413 | |
a80fee22 |
414 | item->submenu = menu; |
d888f2ca |
415 | menu->caller = item; |
a7732690 |
416 | |
858338d9 |
417 | curritem->prev = NULL; |
418 | curritem->next = NULL; |
419 | |
a7732690 |
420 | prevmenu = menu; |
421 | } |
a80fee22 |
422 | count++; |
a7732690 |
423 | } |
424 | } |
425 | |
426 | /* calculate screen geometry */ |
427 | static void |
428 | calcscreengeom(void) |
429 | { |
430 | Window w1, w2; /* unused variables */ |
431 | int a, b; /* unused variables */ |
432 | unsigned mask; /* unused variable */ |
433 | |
a80fee22 |
434 | XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask); |
435 | screengeom.screenw = DisplayWidth(dpy, screen); |
436 | screengeom.screenh = DisplayHeight(dpy, screen); |
a7732690 |
437 | } |
438 | |
439 | /* recursivelly calculate height and position of the menus */ |
440 | static void |
441 | calcmenu(struct Menu *menu) |
442 | { |
d2435fcd |
443 | static XClassHint classh = {PROGNAME, PROGNAME}; |
a7732690 |
444 | XWindowChanges changes; |
d2435fcd |
445 | XTextProperty textprop; |
09c13122 |
446 | XSizeHints sizeh; |
dbeb9940 |
447 | XGlyphInfo ext; |
a80fee22 |
448 | struct Item *item; |
f1583285 |
449 | int labelwidth; |
a7732690 |
450 | |
f1583285 |
451 | /* calculate items positions and menu width and height */ |
452 | menu->w = geom.itemw; |
a80fee22 |
453 | for (item = menu->list; item != NULL; item = item->next) { |
454 | item->y = menu->h; |
7fbd1c5e |
455 | if (item->label == NULL) /* height for separator item */ |
a80fee22 |
456 | menu->h += geom.separator; |
457 | else |
458 | menu->h += geom.itemh; |
f1583285 |
459 | |
dbeb9940 |
460 | XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label, |
461 | item->labellen, &ext); |
462 | labelwidth = ext.xOff + dc.font->height * 2; |
f1583285 |
463 | menu->w = MAX(menu->w, labelwidth); |
a80fee22 |
464 | } |
a7732690 |
465 | |
466 | /* calculate menu's x and y positions */ |
467 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
a80fee22 |
468 | if (screengeom.screenw - screengeom.cursx >= menu->w) |
469 | menu->x = screengeom.cursx; |
470 | else if (screengeom.cursx > menu->w) |
471 | menu->x = screengeom.cursx - menu->w; |
472 | |
473 | if (screengeom.screenh - screengeom.cursy >= menu->h) |
474 | menu->y = screengeom.cursy; |
475 | else if (screengeom.screenh > menu->h) |
476 | menu->y = screengeom.screenh - menu->h; |
d2435fcd |
477 | |
478 | XStringListToTextProperty(menutitle, menutitlecount, &textprop); |
a7732690 |
479 | } else { /* else, calculate in respect to parent menu */ |
480 | |
481 | /* search for the item in parent menu that generates this menu */ |
a80fee22 |
482 | for (item = menu->parent->list; item->submenu != menu; item = item->next) |
a7732690 |
483 | ; |
484 | |
f644b8bc |
485 | if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w) |
486 | menu->x = menu->parent->x + menu->parent->w + geom.border; |
487 | else if (menu->parent->x > menu->w + geom.border) |
488 | menu->x = menu->parent->x - menu->w - geom.border; |
a7732690 |
489 | |
a80fee22 |
490 | if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) |
491 | menu->y = item->y + menu->parent->y; |
492 | else if (screengeom.screenh - menu->parent->y > menu->h) |
a7732690 |
493 | menu->y = menu->parent->y; |
a80fee22 |
494 | else if (screengeom.screenh > menu->h) |
495 | menu->y = screengeom.screenh - menu->h; |
d2435fcd |
496 | |
497 | XStringListToTextProperty(&(menu->caller->output), 1, &textprop); |
a7732690 |
498 | } |
499 | |
500 | /* update menu geometry */ |
501 | changes.height = menu->h; |
f1583285 |
502 | changes.width = menu->w; |
a7732690 |
503 | changes.x = menu->x; |
504 | changes.y = menu->y; |
f1583285 |
505 | XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes); |
a7732690 |
506 | |
d2435fcd |
507 | /* set window manager hints */ |
09c13122 |
508 | sizeh.flags = PMaxSize | PMinSize; |
509 | sizeh.min_width = sizeh.max_width = menu->w; |
510 | sizeh.min_height = sizeh.max_height = menu->h; |
d2435fcd |
511 | XSetWMProperties(dpy, menu->win, &textprop, NULL, NULL, 0, &sizeh, |
512 | NULL, &classh); |
09c13122 |
513 | |
dbeb9940 |
514 | /* create pixmap and XftDraw */ |
f15fc339 |
515 | menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h, |
516 | DefaultDepth(dpy, screen)); |
dbeb9940 |
517 | menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap); |
f15fc339 |
518 | |
a80fee22 |
519 | /* calculate positions of submenus */ |
a7732690 |
520 | for (item = menu->list; item != NULL; item = item->next) { |
521 | if (item->submenu != NULL) |
522 | calcmenu(item->submenu); |
523 | } |
524 | } |
525 | |
526 | /* get menu and item of given window and position */ |
527 | static void |
a80fee22 |
528 | getmenuitem(Window win, int y, |
a7732690 |
529 | struct Menu **menu_ret, struct Item **item_ret) |
530 | { |
531 | struct Menu *menu = NULL; |
532 | struct Item *item = NULL; |
533 | |
534 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
535 | if (menu->win == win) { |
536 | for (item = menu->list; item != NULL; item = item->next) { |
7fbd1c5e |
537 | if (y >= item->y && y <= item->y + item->h) { |
a7732690 |
538 | goto done; |
539 | } |
540 | } |
541 | } |
542 | } |
543 | |
544 | |
545 | done: |
546 | *menu_ret = menu; |
547 | *item_ret = item; |
548 | } |
549 | |
550 | /* set currentmenu to menu, umap previous menus and map current menu and its parents */ |
551 | static void |
552 | setcurrmenu(struct Menu *currmenu_new) |
553 | { |
d8a7caf2 |
554 | struct Menu *menu, *menu_; |
d888f2ca |
555 | struct Item *item; |
d8a7caf2 |
556 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
557 | unsigned minlevel; /* level of the closest to root menu */ |
558 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
559 | |
560 | if (currmenu_new == currmenu) |
561 | return; |
562 | |
d8a7caf2 |
563 | /* find lowest common ancestor menu */ |
564 | lcamenu = rootmenu; |
565 | if (currmenu != NULL) { |
566 | minlevel = MIN(currmenu_new->level, currmenu->level); |
567 | maxlevel = MAX(currmenu_new->level, currmenu->level); |
568 | if (currmenu_new->level == maxlevel) { |
569 | menu = currmenu_new; |
570 | menu_ = currmenu; |
571 | } else { |
572 | menu = currmenu; |
573 | menu_ = currmenu_new; |
574 | } |
575 | while (menu->level > minlevel) |
576 | menu = menu->parent; |
577 | |
578 | while (menu != menu_) { |
579 | menu = menu->parent; |
580 | menu_ = menu_->parent; |
581 | } |
582 | lcamenu = menu; |
583 | } |
584 | |
873c080c |
585 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d8a7caf2 |
586 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
587 | XUnmapWindow(dpy, menu->win); |
588 | } |
589 | |
590 | currmenu = currmenu_new; |
591 | |
873c080c |
592 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d888f2ca |
593 | item = NULL; |
d8a7caf2 |
594 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
595 | XMapWindow(dpy, menu->win); |
d888f2ca |
596 | if (item != NULL) |
597 | menu->selected = item; |
598 | item = menu->caller; |
599 | } |
a7732690 |
600 | } |
601 | |
602 | /* draw items of the current menu and of its ancestors */ |
603 | static void |
604 | drawmenu(void) |
605 | { |
606 | struct Menu *menu; |
607 | struct Item *item; |
608 | |
609 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
610 | for (item = menu->list; item != NULL; item = item->next) { |
dbeb9940 |
611 | XftColor *color; |
a7732690 |
612 | int labelx, labely; |
a7732690 |
613 | |
614 | /* determine item color */ |
f644b8bc |
615 | if (item == menu->selected) |
616 | color = dc.selected; |
a7732690 |
617 | else |
f644b8bc |
618 | color = dc.normal; |
619 | |
620 | /* continue if item is a separator */ |
621 | if (item->label == NULL) |
622 | continue; |
a7732690 |
623 | |
a7732690 |
624 | /* draw item box */ |
dbeb9940 |
625 | XSetForeground(dpy, dc.gc, color[ColorBG].pixel); |
d888f2ca |
626 | XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
627 | menu->w, item->h); |
f15fc339 |
628 | XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
f1583285 |
629 | menu->w, item->h); |
7fbd1c5e |
630 | |
a7732690 |
631 | /* draw item label */ |
dbeb9940 |
632 | labelx = 0 + dc.font->height; |
633 | labely = item->y + dc.font->height + geom.itemb / 2; |
634 | XSetForeground(dpy, dc.gc, color[ColorFG].pixel); |
635 | XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font, |
636 | labelx, labely, item->label, |
637 | item->labellen); |
a7732690 |
638 | |
639 | /* draw triangle, if item contains a submenu */ |
640 | if (item->submenu != NULL) { |
dbeb9940 |
641 | int trianglex = menu->w - dc.font->height + geom.itemb - 1; |
2b6968f9 |
642 | int triangley = item->y + (3 * item->h)/8 -1; |
a7732690 |
643 | |
644 | XPoint triangle[] = { |
645 | {trianglex, triangley}, |
2b6968f9 |
646 | {trianglex + item->h/8 + 1, item->y + item->h/2}, |
647 | {trianglex, triangley + item->h/4 + 2}, |
a7732690 |
648 | {trianglex, triangley} |
649 | }; |
650 | |
f15fc339 |
651 | XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle), |
a7732690 |
652 | Convex, CoordModeOrigin); |
653 | } |
f15fc339 |
654 | |
655 | XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y, |
656 | menu->w, item->h, 0, item->y); |
a7732690 |
657 | } |
658 | } |
659 | } |
660 | |
858338d9 |
661 | /* cycle through the items; non-zero direction is next, zero is prev */ |
662 | static struct Item * |
663 | itemcycle(int direction) |
664 | { |
665 | struct Item *item; |
666 | struct Item *lastitem; |
667 | |
668 | item = NULL; |
669 | |
670 | if (direction == ITEMNEXT) { |
671 | if (currmenu->selected == NULL) |
672 | item = currmenu->list; |
673 | else if (currmenu->selected->next != NULL) |
674 | item = currmenu->selected->next; |
675 | |
676 | while (item != NULL && item->label == NULL) |
677 | item = item->next; |
678 | |
679 | if (item == NULL) |
680 | item = currmenu->list; |
681 | } else { |
682 | for (lastitem = currmenu->list; |
683 | lastitem != NULL && lastitem->next != NULL; |
684 | lastitem = lastitem->next) |
685 | ; |
686 | |
687 | if (currmenu->selected == NULL) |
688 | item = lastitem; |
689 | else if (currmenu->selected->prev != NULL) |
690 | item = currmenu->selected->prev; |
691 | |
692 | while (item != NULL && item->label == NULL) |
693 | item = item->prev; |
694 | |
695 | if (item == NULL) |
696 | item = lastitem; |
697 | } |
698 | |
699 | return item; |
700 | } |
701 | |
a7732690 |
702 | /* run event loop */ |
703 | static void |
704 | run(void) |
705 | { |
706 | struct Menu *menu; |
707 | struct Item *item; |
708 | struct Item *previtem = NULL; |
858338d9 |
709 | KeySym ksym; |
a7732690 |
710 | XEvent ev; |
711 | |
a7732690 |
712 | while (!XNextEvent(dpy, &ev)) { |
713 | switch(ev.type) { |
714 | case Expose: |
858338d9 |
715 | if (ev.xexpose.count == 0) |
716 | drawmenu(); |
a7732690 |
717 | break; |
718 | case MotionNotify: |
a80fee22 |
719 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
720 | if (menu != NULL && item != NULL) { |
721 | if (previtem != item) { |
722 | if (item->submenu != NULL) |
723 | setcurrmenu(item->submenu); |
724 | else |
725 | setcurrmenu(menu); |
726 | previtem = item; |
d888f2ca |
727 | drawmenu(); |
728 | } else if (menu->selected != item) { |
a7732690 |
729 | menu->selected = item; |
d888f2ca |
730 | drawmenu(); |
731 | } |
a7732690 |
732 | } |
a7732690 |
733 | break; |
734 | case ButtonRelease: |
a80fee22 |
735 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
736 | if (menu != NULL && item != NULL) { |
858338d9 |
737 | selectitem: |
7fbd1c5e |
738 | if (item->label == NULL) |
739 | break; /* ignore separators */ |
a7732690 |
740 | if (item->submenu != NULL) { |
741 | setcurrmenu(item->submenu); |
742 | } else { |
743 | printf("%s\n", item->output); |
08f16589 |
744 | return; |
a7732690 |
745 | } |
858338d9 |
746 | currmenu->selected = currmenu->list; |
a7732690 |
747 | drawmenu(); |
858338d9 |
748 | break; |
a7732690 |
749 | } else { |
08f16589 |
750 | return; |
a7732690 |
751 | } |
858338d9 |
752 | case ButtonPress: |
753 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
754 | if (menu == NULL || item == NULL) |
755 | return; |
756 | break; |
757 | case KeyPress: |
758 | ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0); |
759 | |
760 | if (ksym == XK_Escape && currmenu == rootmenu) |
761 | return; |
762 | |
763 | /* Shift-Tab = ISO_Left_Tab */ |
764 | if (ksym == XK_Tab && (ev.xkey.state & ShiftMask)) |
765 | ksym = XK_ISO_Left_Tab; |
766 | |
767 | /* cycle through menu */ |
768 | item = NULL; |
769 | if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) { |
770 | item = itemcycle(ITEMPREV); |
771 | } else if (ksym == XK_Tab || ksym == XK_Down) { |
772 | item = itemcycle(ITEMNEXT); |
773 | } else if ((ksym == XK_Return || ksym == XK_Right) && |
774 | currmenu->selected != NULL) { |
775 | item = currmenu->selected; |
776 | goto selectitem; |
777 | } else if ((ksym == XK_Escape || ksym == XK_Left) && |
778 | currmenu->parent != NULL) { |
779 | item = currmenu->parent->selected; |
780 | setcurrmenu(currmenu->parent); |
781 | } else |
782 | break; |
783 | currmenu->selected = item; |
784 | drawmenu(); |
a7732690 |
785 | break; |
f15fc339 |
786 | case LeaveNotify: |
787 | currmenu->selected = NULL; |
788 | drawmenu(); |
789 | break; |
a7732690 |
790 | } |
791 | } |
792 | } |
793 | |
f15fc339 |
794 | /* recursivelly free a pixmap */ |
795 | static void |
796 | freewindow(struct Menu *menu) |
797 | { |
798 | struct Item *item; |
799 | |
800 | for (item = menu->list; item != NULL; item = item->next) |
801 | if (item->submenu != NULL) |
802 | freewindow(item->submenu); |
803 | |
804 | XFreePixmap(dpy, menu->pixmap); |
dbeb9940 |
805 | XftDrawDestroy(menu->draw); |
f15fc339 |
806 | XDestroyWindow(dpy, menu->win); |
807 | } |
808 | |
a7732690 |
809 | /* cleanup and exit */ |
810 | static void |
08f16589 |
811 | cleanup(void) |
a7732690 |
812 | { |
f15fc339 |
813 | freewindow(rootmenu); |
dbeb9940 |
814 | |
815 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); |
816 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); |
817 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); |
818 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]); |
819 | XftColorFree(dpy, visual, colormap, &dc.decoration[ColorBG]); |
820 | XftColorFree(dpy, visual, colormap, &dc.decoration[ColorFG]); |
821 | |
f15fc339 |
822 | XFreeGC(dpy, dc.gc); |
a7732690 |
823 | XCloseDisplay(dpy); |
a7732690 |
824 | } |
825 | |
826 | /* show usage */ |
827 | static void |
828 | usage(void) |
829 | { |
c0cff00d |
830 | (void)fprintf(stderr, "usage: xmenu [-w] title...\n"); |
a7732690 |
831 | exit(1); |
832 | } |