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