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