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