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> |
8 | |
9 | /* macros */ |
10 | #define LEN(x) (sizeof (x) / sizeof (x[0])) |
f1583285 |
11 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
d8a7caf2 |
12 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
a7732690 |
13 | |
14 | /* color enum */ |
15 | enum {ColorFG, ColorBG, ColorLast}; |
16 | |
17 | /* draw context structure */ |
18 | struct DC { |
19 | unsigned long unpressed[ColorLast]; |
20 | unsigned long pressed[ColorLast]; |
21 | unsigned long decoration[ColorLast]; |
22 | |
23 | Drawable d; |
24 | GC gc; |
25 | XFontStruct *font; |
26 | int fonth; |
27 | }; |
28 | |
29 | /* menu geometry structure */ |
30 | struct Geometry { |
31 | int itemb; /* item border */ |
32 | int itemw; /* item width */ |
33 | int itemh; /* item height */ |
a80fee22 |
34 | int border; /* window border width */ |
35 | int separator; /* menu separator width */ |
a7732690 |
36 | }; |
37 | |
38 | /* screen geometry structure */ |
39 | struct ScreenGeometry { |
40 | int cursx, cursy; /* cursor position */ |
41 | int screenw, screenh; /* screen width and height */ |
42 | }; |
43 | |
44 | /* menu item structure */ |
45 | struct Item { |
d8a7caf2 |
46 | char *label; /* string to be drawed on menu */ |
47 | char *output; /* string to be outputed when item is clicked */ |
48 | int y; /* item y position relative to menu */ |
49 | int h; /* item height */ |
50 | size_t labellen; /* strlen(label) */ |
51 | struct Item *next; /* next item */ |
52 | struct Menu *submenu; /* submenu spawned by clicking on item */ |
a7732690 |
53 | }; |
54 | |
55 | /* menu structure */ |
56 | struct Menu { |
d8a7caf2 |
57 | struct Menu *parent; /* parent menu */ |
58 | struct Item *caller; /* item that spawned the menu */ |
59 | struct Item *list; /* list of items contained by the menu */ |
60 | struct Item *selected; /* item currently selected in the menu */ |
61 | int x, y, w, h; /* menu geometry */ |
62 | unsigned level; /* menu level relative to root */ |
63 | Drawable pixmap; /* pixmap to draw the menu on */ |
64 | Window win; /* menu window to map on the screen */ |
a7732690 |
65 | }; |
66 | |
67 | /* function declarations */ |
68 | static unsigned long getcolor(const char *s); |
69 | static void setupdc(void); |
70 | static void setupgeom(void); |
71 | static void setupgrab(void); |
a80fee22 |
72 | static struct Item *allocitem(const char *label, const char *output); |
a7732690 |
73 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
f15fc339 |
74 | static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret); |
a7732690 |
75 | static void drawmenu(void); |
76 | static void calcscreengeom(void); |
77 | static void calcmenu(struct Menu *menu); |
78 | static void setcurrmenu(struct Menu *currmenu_new); |
79 | static void parsestdin(void); |
80 | static void run(void); |
f15fc339 |
81 | static void freewindow(struct Menu *menu); |
a7732690 |
82 | static void cleanupexit(void); |
83 | static void usage(void); |
84 | |
85 | /* X variables */ |
86 | static Colormap colormap; |
87 | static Display *dpy; |
88 | static Window rootwin; |
89 | static int screen; |
90 | static struct DC dc; |
91 | |
92 | /* menu variables */ |
93 | static struct Menu *rootmenu = NULL; |
94 | static struct Menu *currmenu = NULL; |
95 | |
96 | /* geometry variables */ |
97 | static struct Geometry geom; |
a80fee22 |
98 | static struct ScreenGeometry screengeom; |
a7732690 |
99 | |
100 | /* flag variables */ |
101 | static Bool override_redirect = True; |
102 | |
103 | #include "config.h" |
104 | |
105 | int |
106 | main(int argc, char *argv[]) |
107 | { |
108 | int ch; |
109 | |
110 | while ((ch = getopt(argc, argv, "w")) != -1) { |
111 | switch (ch) { |
112 | case 'w': |
113 | override_redirect = False; |
114 | break; |
115 | default: |
116 | usage(); |
117 | break; |
118 | } |
119 | } |
120 | argc -= optind; |
121 | argv += optind; |
122 | |
123 | /* open connection to server and set X variables */ |
124 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
125 | errx(1, "cannot open display"); |
126 | screen = DefaultScreen(dpy); |
127 | rootwin = RootWindow(dpy, screen); |
128 | colormap = DefaultColormap(dpy, screen); |
129 | |
130 | /* setup */ |
131 | setupdc(); |
132 | setupgeom(); |
133 | setupgrab(); |
134 | |
135 | /* generate menus and recalculate them */ |
136 | parsestdin(); |
137 | if (rootmenu == NULL) |
138 | errx(1, "no menu generated"); |
139 | calcscreengeom(); |
140 | calcmenu(rootmenu); |
141 | |
d8a7caf2 |
142 | /* map root menu */ |
143 | currmenu = rootmenu; |
144 | XMapWindow(dpy, rootmenu->win); |
145 | |
a7732690 |
146 | /* run event loop */ |
147 | run(); |
148 | |
149 | return 1; /* UNREACHABLE */ |
150 | } |
151 | |
152 | /* get color from color string */ |
153 | static unsigned long |
154 | getcolor(const char *s) |
155 | { |
156 | XColor color; |
157 | |
158 | if(!XAllocNamedColor(dpy, colormap, s, &color, &color)) |
159 | errx(1, "cannot allocate color: %s", s); |
160 | return color.pixel; |
161 | } |
162 | |
163 | /* init draw context */ |
164 | static void |
165 | setupdc(void) |
166 | { |
167 | /* get color pixels */ |
168 | dc.unpressed[ColorBG] = getcolor(UNPRESSEDBG); |
169 | dc.unpressed[ColorFG] = getcolor(UNPRESSEDFG); |
170 | dc.pressed[ColorBG] = getcolor(PRESSEDBG); |
171 | dc.pressed[ColorFG] = getcolor(PRESSEDFG); |
172 | dc.decoration[ColorBG] = getcolor(DECORATIONBG); |
173 | dc.decoration[ColorFG] = getcolor(DECORATIONFG); |
174 | |
175 | /* try to get font */ |
176 | if ((dc.font = XLoadQueryFont(dpy, FONT)) == NULL) |
177 | errx(1, "cannot load font"); |
178 | dc.fonth = dc.font->ascent + dc.font->descent; |
179 | |
180 | /* create GC and set its font */ |
181 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
182 | XSetFont(dpy, dc.gc, dc.font->fid); |
183 | } |
184 | |
185 | /* init menu geometry values */ |
186 | static void |
187 | setupgeom(void) |
188 | { |
189 | geom.itemb = ITEMB; |
190 | geom.itemh = dc.fonth + ITEMB * 2; |
191 | geom.itemw = ITEMW; |
192 | geom.border = BORDER; |
a80fee22 |
193 | geom.separator = SEPARATOR; |
a7732690 |
194 | } |
195 | |
196 | /* grab pointer */ |
197 | static void |
198 | setupgrab(void) |
199 | { |
200 | XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask, |
201 | GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
202 | } |
203 | |
204 | /* allocate an item */ |
205 | static struct Item * |
a80fee22 |
206 | allocitem(const char *label, const char *output) |
a7732690 |
207 | { |
208 | struct Item *item; |
209 | |
210 | if ((item = malloc(sizeof *item)) == NULL) |
211 | err(1, "malloc"); |
7fbd1c5e |
212 | if (*label == '\0') { |
213 | item->label = NULL; |
214 | item->output = NULL; |
215 | } else { |
216 | if ((item->label = strdup(label)) == NULL) |
217 | err(1, "strdup"); |
218 | if ((item->output = strdup(output)) == NULL) |
219 | err(1, "strdup"); |
220 | } |
a80fee22 |
221 | item->y = 0; |
7fbd1c5e |
222 | item->h = item->label ? geom.itemh : geom.separator; |
f1583285 |
223 | if (item->label == NULL) |
224 | item->labellen = 0; |
225 | else |
226 | item->labellen = strlen(item->label); |
a7732690 |
227 | item->next = NULL; |
228 | item->submenu = NULL; |
229 | |
230 | return item; |
231 | } |
232 | |
233 | /* allocate a menu */ |
234 | static struct Menu * |
235 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
236 | { |
237 | XSetWindowAttributes swa; |
238 | struct Menu *menu; |
239 | |
240 | if ((menu = malloc(sizeof *menu)) == NULL) |
241 | err(1, "malloc"); |
242 | menu->parent = parent; |
243 | menu->list = list; |
d888f2ca |
244 | menu->caller = NULL; |
a7732690 |
245 | menu->selected = NULL; |
a7732690 |
246 | menu->w = geom.itemw; |
a80fee22 |
247 | menu->h = 0; /* calculated by calcmenu() */ |
248 | menu->x = 0; /* calculated by calcmenu() */ |
249 | menu->y = 0; /* calculated by calcmenu() */ |
a7732690 |
250 | menu->level = level; |
a7732690 |
251 | |
252 | swa.override_redirect = override_redirect; |
253 | swa.background_pixel = dc.decoration[ColorBG]; |
254 | swa.border_pixel = dc.decoration[ColorFG]; |
255 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
256 | | PointerMotionMask | LeaveWindowMask; |
a7732690 |
257 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border, |
258 | CopyFromParent, CopyFromParent, CopyFromParent, |
259 | CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, |
260 | &swa); |
261 | |
262 | return menu; |
263 | } |
264 | |
265 | /* create menus and items from the stdin */ |
266 | static void |
267 | parsestdin(void) |
268 | { |
269 | char *s, buf[BUFSIZ]; |
270 | char *label, *output; |
271 | unsigned level = 0; |
272 | unsigned i; |
a80fee22 |
273 | struct Item *curritem = NULL; /* item currently being read */ |
274 | struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
275 | struct Item *item; /* dummy item for for loops */ |
276 | struct Menu *menu; /* dummy menu for for loops */ |
a7732690 |
277 | size_t count = 0; /* number of items in the current menu */ |
278 | |
279 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
280 | level = 0; |
281 | s = buf; |
282 | |
283 | while (*s == '\t') { |
284 | level++; |
285 | s++; |
286 | } |
287 | |
288 | label = output = s; |
289 | |
290 | while (*s != '\0' && *s != '\t' && *s != '\n') |
291 | s++; |
292 | |
293 | while (*s == '\t') |
294 | *s++ = '\0'; |
295 | |
296 | if (*s != '\0' && *s != '\n') |
297 | output = s; |
298 | |
299 | while (*s != '\0' && *s != '\n') |
300 | s++; |
301 | |
302 | if (*s == '\n') |
303 | *s = '\0'; |
304 | |
a80fee22 |
305 | curritem = allocitem(label, output); |
a7732690 |
306 | |
307 | if (prevmenu == NULL) { /* there is no menu yet */ |
a80fee22 |
308 | menu = allocmenu(NULL, curritem, level); |
a7732690 |
309 | rootmenu = menu; |
310 | prevmenu = menu; |
311 | count = 1; |
312 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/ |
313 | for (menu = prevmenu, i = level; |
314 | menu != NULL && i < prevmenu->level; |
315 | menu = menu->parent, i++) |
316 | ; |
317 | |
318 | if (menu == NULL) |
319 | errx(1, "reached NULL menu"); |
320 | |
a80fee22 |
321 | for (item = menu->list; item->next != NULL; item = item->next) |
a7732690 |
322 | ; |
323 | |
a80fee22 |
324 | item->next = curritem; |
a7732690 |
325 | prevmenu = menu; |
326 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
a80fee22 |
327 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
328 | ; |
a80fee22 |
329 | item->next = curritem; |
a7732690 |
330 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
a80fee22 |
331 | menu = allocmenu(prevmenu, curritem, level); |
a7732690 |
332 | |
a80fee22 |
333 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
334 | ; |
335 | |
a80fee22 |
336 | item->submenu = menu; |
d888f2ca |
337 | menu->caller = item; |
a7732690 |
338 | |
339 | prevmenu = menu; |
340 | } |
a80fee22 |
341 | count++; |
a7732690 |
342 | } |
343 | } |
344 | |
345 | /* calculate screen geometry */ |
346 | static void |
347 | calcscreengeom(void) |
348 | { |
349 | Window w1, w2; /* unused variables */ |
350 | int a, b; /* unused variables */ |
351 | unsigned mask; /* unused variable */ |
352 | |
a80fee22 |
353 | XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask); |
354 | screengeom.screenw = DisplayWidth(dpy, screen); |
355 | screengeom.screenh = DisplayHeight(dpy, screen); |
a7732690 |
356 | } |
357 | |
358 | /* recursivelly calculate height and position of the menus */ |
359 | static void |
360 | calcmenu(struct Menu *menu) |
361 | { |
362 | XWindowChanges changes; |
09c13122 |
363 | XSizeHints sizeh; |
a80fee22 |
364 | struct Item *item; |
f1583285 |
365 | int labelwidth; |
a7732690 |
366 | |
f1583285 |
367 | /* calculate items positions and menu width and height */ |
368 | menu->w = geom.itemw; |
a80fee22 |
369 | for (item = menu->list; item != NULL; item = item->next) { |
370 | item->y = menu->h; |
7fbd1c5e |
371 | if (item->label == NULL) /* height for separator item */ |
a80fee22 |
372 | menu->h += geom.separator; |
373 | else |
374 | menu->h += geom.itemh; |
f1583285 |
375 | |
376 | labelwidth = XTextWidth(dc.font, item->label, item->labellen) + dc.fonth * 2; |
377 | menu->w = MAX(menu->w, labelwidth); |
a80fee22 |
378 | } |
a7732690 |
379 | |
380 | /* calculate menu's x and y positions */ |
381 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
a80fee22 |
382 | if (screengeom.screenw - screengeom.cursx >= menu->w) |
383 | menu->x = screengeom.cursx; |
384 | else if (screengeom.cursx > menu->w) |
385 | menu->x = screengeom.cursx - menu->w; |
386 | |
387 | if (screengeom.screenh - screengeom.cursy >= menu->h) |
388 | menu->y = screengeom.cursy; |
389 | else if (screengeom.screenh > menu->h) |
390 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
391 | } else { /* else, calculate in respect to parent menu */ |
392 | |
393 | /* search for the item in parent menu that generates this menu */ |
a80fee22 |
394 | for (item = menu->parent->list; item->submenu != menu; item = item->next) |
a7732690 |
395 | ; |
396 | |
a80fee22 |
397 | if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w) |
a7732690 |
398 | menu->x = menu->parent->x + menu->parent->w; |
399 | else if (menu->parent->x > menu->w) |
400 | menu->x = menu->parent->x - menu->w; |
401 | |
a80fee22 |
402 | if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) |
403 | menu->y = item->y + menu->parent->y; |
404 | else if (screengeom.screenh - menu->parent->y > menu->h) |
a7732690 |
405 | menu->y = menu->parent->y; |
a80fee22 |
406 | else if (screengeom.screenh > menu->h) |
407 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
408 | } |
409 | |
410 | /* update menu geometry */ |
411 | changes.height = menu->h; |
f1583285 |
412 | changes.width = menu->w; |
a7732690 |
413 | changes.x = menu->x; |
414 | changes.y = menu->y; |
f1583285 |
415 | XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes); |
a7732690 |
416 | |
09c13122 |
417 | /* set window manager size hints */ |
418 | sizeh.flags = PMaxSize | PMinSize; |
419 | sizeh.min_width = sizeh.max_width = menu->w; |
420 | sizeh.min_height = sizeh.max_height = menu->h; |
421 | XSetWMNormalHints(dpy, menu->win, &sizeh); |
422 | |
f15fc339 |
423 | /* create pixmap */ |
424 | menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h, |
425 | DefaultDepth(dpy, screen)); |
426 | |
a80fee22 |
427 | /* calculate positions of submenus */ |
a7732690 |
428 | for (item = menu->list; item != NULL; item = item->next) { |
429 | if (item->submenu != NULL) |
430 | calcmenu(item->submenu); |
431 | } |
432 | } |
433 | |
434 | /* get menu and item of given window and position */ |
435 | static void |
a80fee22 |
436 | getmenuitem(Window win, int y, |
a7732690 |
437 | struct Menu **menu_ret, struct Item **item_ret) |
438 | { |
439 | struct Menu *menu = NULL; |
440 | struct Item *item = NULL; |
441 | |
442 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
443 | if (menu->win == win) { |
444 | for (item = menu->list; item != NULL; item = item->next) { |
7fbd1c5e |
445 | if (y >= item->y && y <= item->y + item->h) { |
a7732690 |
446 | goto done; |
447 | } |
448 | } |
449 | } |
450 | } |
451 | |
452 | |
453 | done: |
454 | *menu_ret = menu; |
455 | *item_ret = item; |
456 | } |
457 | |
458 | /* set currentmenu to menu, umap previous menus and map current menu and its parents */ |
459 | static void |
460 | setcurrmenu(struct Menu *currmenu_new) |
461 | { |
d8a7caf2 |
462 | struct Menu *menu, *menu_; |
d888f2ca |
463 | struct Item *item; |
d8a7caf2 |
464 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
465 | unsigned minlevel; /* level of the closest to root menu */ |
466 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
467 | |
468 | if (currmenu_new == currmenu) |
469 | return; |
470 | |
d8a7caf2 |
471 | /* find lowest common ancestor menu */ |
472 | lcamenu = rootmenu; |
473 | if (currmenu != NULL) { |
474 | minlevel = MIN(currmenu_new->level, currmenu->level); |
475 | maxlevel = MAX(currmenu_new->level, currmenu->level); |
476 | if (currmenu_new->level == maxlevel) { |
477 | menu = currmenu_new; |
478 | menu_ = currmenu; |
479 | } else { |
480 | menu = currmenu; |
481 | menu_ = currmenu_new; |
482 | } |
483 | while (menu->level > minlevel) |
484 | menu = menu->parent; |
485 | |
486 | while (menu != menu_) { |
487 | menu = menu->parent; |
488 | menu_ = menu_->parent; |
489 | } |
490 | lcamenu = menu; |
491 | } |
492 | |
493 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
494 | XUnmapWindow(dpy, menu->win); |
495 | } |
496 | |
497 | currmenu = currmenu_new; |
498 | |
d888f2ca |
499 | item = NULL; |
d8a7caf2 |
500 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
501 | XMapWindow(dpy, menu->win); |
d888f2ca |
502 | if (item != NULL) |
503 | menu->selected = item; |
504 | item = menu->caller; |
505 | } |
a7732690 |
506 | } |
507 | |
508 | /* draw items of the current menu and of its ancestors */ |
509 | static void |
510 | drawmenu(void) |
511 | { |
512 | struct Menu *menu; |
513 | struct Item *item; |
514 | |
515 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
516 | for (item = menu->list; item != NULL; item = item->next) { |
517 | unsigned long *color; |
a7732690 |
518 | int labelx, labely; |
a7732690 |
519 | |
520 | /* determine item color */ |
7fbd1c5e |
521 | if (item->label == NULL) |
522 | color = dc.decoration; |
523 | else if (item == menu->selected) |
a7732690 |
524 | color = dc.pressed; |
525 | else |
526 | color = dc.unpressed; |
527 | |
a7732690 |
528 | /* draw item box */ |
529 | XSetForeground(dpy, dc.gc, color[ColorBG]); |
d888f2ca |
530 | XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
531 | menu->w, item->h); |
f15fc339 |
532 | XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
f1583285 |
533 | menu->w, item->h); |
7fbd1c5e |
534 | |
535 | /* continue if item is a separator */ |
536 | if (item->label == NULL) |
537 | continue; |
a7732690 |
538 | |
539 | /* draw item label */ |
a7732690 |
540 | labelx = 0 + dc.fonth; |
7fbd1c5e |
541 | labely = item->y + dc.fonth + geom.itemb; |
a7732690 |
542 | XSetForeground(dpy, dc.gc, color[ColorFG]); |
f1583285 |
543 | XDrawString(dpy, menu->pixmap, dc.gc, labelx, labely, |
544 | item->label, item->labellen); |
a7732690 |
545 | |
546 | /* draw triangle, if item contains a submenu */ |
547 | if (item->submenu != NULL) { |
f1583285 |
548 | int trianglex = menu->w - dc.fonth + geom.itemb - 1; |
2b6968f9 |
549 | int triangley = item->y + (3 * item->h)/8 -1; |
a7732690 |
550 | |
551 | XPoint triangle[] = { |
552 | {trianglex, triangley}, |
2b6968f9 |
553 | {trianglex + item->h/8 + 1, item->y + item->h/2}, |
554 | {trianglex, triangley + item->h/4 + 2}, |
a7732690 |
555 | {trianglex, triangley} |
556 | }; |
557 | |
f15fc339 |
558 | XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle), |
a7732690 |
559 | Convex, CoordModeOrigin); |
560 | } |
f15fc339 |
561 | |
562 | XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y, |
563 | menu->w, item->h, 0, item->y); |
a7732690 |
564 | } |
565 | } |
566 | } |
567 | |
568 | /* run event loop */ |
569 | static void |
570 | run(void) |
571 | { |
572 | struct Menu *menu; |
573 | struct Item *item; |
574 | struct Item *previtem = NULL; |
575 | XEvent ev; |
576 | |
577 | setcurrmenu(rootmenu); |
578 | |
579 | while (!XNextEvent(dpy, &ev)) { |
580 | switch(ev.type) { |
581 | case Expose: |
582 | drawmenu(); |
583 | break; |
584 | case MotionNotify: |
a80fee22 |
585 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
586 | if (menu != NULL && item != NULL) { |
587 | if (previtem != item) { |
588 | if (item->submenu != NULL) |
589 | setcurrmenu(item->submenu); |
590 | else |
591 | setcurrmenu(menu); |
592 | previtem = item; |
d888f2ca |
593 | drawmenu(); |
594 | } else if (menu->selected != item) { |
a7732690 |
595 | menu->selected = item; |
d888f2ca |
596 | drawmenu(); |
597 | } |
a7732690 |
598 | } |
a7732690 |
599 | break; |
600 | case ButtonRelease: |
a80fee22 |
601 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
602 | if (menu != NULL && item != NULL) { |
7fbd1c5e |
603 | if (item->label == NULL) |
604 | break; /* ignore separators */ |
a7732690 |
605 | if (item->submenu != NULL) { |
606 | setcurrmenu(item->submenu); |
607 | } else { |
608 | printf("%s\n", item->output); |
609 | cleanupexit(); |
610 | } |
611 | drawmenu(); |
612 | } else { |
613 | cleanupexit(); |
614 | } |
615 | break; |
f15fc339 |
616 | case LeaveNotify: |
617 | currmenu->selected = NULL; |
618 | drawmenu(); |
619 | break; |
a7732690 |
620 | } |
621 | } |
622 | } |
623 | |
f15fc339 |
624 | /* recursivelly free a pixmap */ |
625 | static void |
626 | freewindow(struct Menu *menu) |
627 | { |
628 | struct Item *item; |
629 | |
630 | for (item = menu->list; item != NULL; item = item->next) |
631 | if (item->submenu != NULL) |
632 | freewindow(item->submenu); |
633 | |
634 | XFreePixmap(dpy, menu->pixmap); |
635 | XDestroyWindow(dpy, menu->win); |
636 | } |
637 | |
a7732690 |
638 | /* cleanup and exit */ |
639 | static void |
640 | cleanupexit(void) |
641 | { |
f15fc339 |
642 | freewindow(rootmenu); |
643 | XFreeFont(dpy, dc.font); |
644 | XFreeGC(dpy, dc.gc); |
a7732690 |
645 | XCloseDisplay(dpy); |
646 | exit(0); |
647 | } |
648 | |
649 | /* show usage */ |
650 | static void |
651 | usage(void) |
652 | { |
653 | (void)fprintf(stderr, "usage: xmenu [-w] menuname\n"); |
654 | exit(1); |
655 | } |