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