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