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