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