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