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> |
8 | |
9 | /* macros */ |
10 | #define LEN(x) (sizeof (x) / sizeof (x[0])) |
11 | |
12 | /* color enum */ |
13 | enum {ColorFG, ColorBG, ColorLast}; |
14 | |
15 | /* draw context structure */ |
16 | struct DC { |
17 | unsigned long unpressed[ColorLast]; |
18 | unsigned long pressed[ColorLast]; |
19 | unsigned long decoration[ColorLast]; |
20 | |
21 | Drawable d; |
22 | GC gc; |
23 | XFontStruct *font; |
24 | int fonth; |
25 | }; |
26 | |
27 | /* menu geometry structure */ |
28 | struct Geometry { |
29 | int itemb; /* item border */ |
30 | int itemw; /* item width */ |
31 | int itemh; /* item height */ |
a80fee22 |
32 | int border; /* window border width */ |
33 | int separator; /* menu separator width */ |
a7732690 |
34 | }; |
35 | |
36 | /* screen geometry structure */ |
37 | struct ScreenGeometry { |
38 | int cursx, cursy; /* cursor position */ |
39 | int screenw, screenh; /* screen width and height */ |
40 | }; |
41 | |
42 | /* menu item structure */ |
43 | struct Item { |
44 | char *label; |
45 | char *output; |
7fbd1c5e |
46 | int y; |
47 | int h; |
a7732690 |
48 | struct Item *next; |
49 | struct Menu *submenu; |
50 | }; |
51 | |
52 | /* menu structure */ |
53 | struct Menu { |
54 | struct Menu *parent; |
55 | struct Item *list; |
56 | struct Item *selected; |
57 | int x, y, w, h; |
58 | unsigned level; |
a7732690 |
59 | Window win; |
60 | }; |
61 | |
62 | /* function declarations */ |
63 | static unsigned long getcolor(const char *s); |
64 | static void setupdc(void); |
65 | static void setupgeom(void); |
66 | static void setupgrab(void); |
a80fee22 |
67 | static struct Item *allocitem(const char *label, const char *output); |
a7732690 |
68 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
a80fee22 |
69 | static void getmenuitem(Window win, int y, |
a7732690 |
70 | struct Menu **menu_ret, struct Item **item_ret); |
71 | static void drawmenu(void); |
72 | static void calcscreengeom(void); |
73 | static void calcmenu(struct Menu *menu); |
74 | static void setcurrmenu(struct Menu *currmenu_new); |
75 | static void parsestdin(void); |
76 | static void run(void); |
77 | static void cleanupexit(void); |
78 | static void usage(void); |
79 | |
80 | /* X variables */ |
81 | static Colormap colormap; |
82 | static Display *dpy; |
83 | static Window rootwin; |
84 | static int screen; |
85 | static struct DC dc; |
86 | |
87 | /* menu variables */ |
88 | static struct Menu *rootmenu = NULL; |
89 | static struct Menu *currmenu = NULL; |
90 | |
91 | /* geometry variables */ |
92 | static struct Geometry geom; |
a80fee22 |
93 | static struct ScreenGeometry screengeom; |
a7732690 |
94 | |
95 | /* flag variables */ |
96 | static Bool override_redirect = True; |
97 | |
98 | #include "config.h" |
99 | |
100 | int |
101 | main(int argc, char *argv[]) |
102 | { |
103 | int ch; |
104 | |
105 | while ((ch = getopt(argc, argv, "w")) != -1) { |
106 | switch (ch) { |
107 | case 'w': |
108 | override_redirect = False; |
109 | break; |
110 | default: |
111 | usage(); |
112 | break; |
113 | } |
114 | } |
115 | argc -= optind; |
116 | argv += optind; |
117 | |
118 | /* open connection to server and set X variables */ |
119 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
120 | errx(1, "cannot open display"); |
121 | screen = DefaultScreen(dpy); |
122 | rootwin = RootWindow(dpy, screen); |
123 | colormap = DefaultColormap(dpy, screen); |
124 | |
125 | /* setup */ |
126 | setupdc(); |
127 | setupgeom(); |
128 | setupgrab(); |
129 | |
130 | /* generate menus and recalculate them */ |
131 | parsestdin(); |
132 | if (rootmenu == NULL) |
133 | errx(1, "no menu generated"); |
134 | calcscreengeom(); |
135 | calcmenu(rootmenu); |
136 | |
137 | /* run event loop */ |
138 | run(); |
139 | |
140 | return 1; /* UNREACHABLE */ |
141 | } |
142 | |
143 | /* get color from color string */ |
144 | static unsigned long |
145 | getcolor(const char *s) |
146 | { |
147 | XColor color; |
148 | |
149 | if(!XAllocNamedColor(dpy, colormap, s, &color, &color)) |
150 | errx(1, "cannot allocate color: %s", s); |
151 | return color.pixel; |
152 | } |
153 | |
154 | /* init draw context */ |
155 | static void |
156 | setupdc(void) |
157 | { |
158 | /* get color pixels */ |
159 | dc.unpressed[ColorBG] = getcolor(UNPRESSEDBG); |
160 | dc.unpressed[ColorFG] = getcolor(UNPRESSEDFG); |
161 | dc.pressed[ColorBG] = getcolor(PRESSEDBG); |
162 | dc.pressed[ColorFG] = getcolor(PRESSEDFG); |
163 | dc.decoration[ColorBG] = getcolor(DECORATIONBG); |
164 | dc.decoration[ColorFG] = getcolor(DECORATIONFG); |
165 | |
166 | /* try to get font */ |
167 | if ((dc.font = XLoadQueryFont(dpy, FONT)) == NULL) |
168 | errx(1, "cannot load font"); |
169 | dc.fonth = dc.font->ascent + dc.font->descent; |
170 | |
171 | /* create GC and set its font */ |
172 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
173 | XSetFont(dpy, dc.gc, dc.font->fid); |
174 | } |
175 | |
176 | /* init menu geometry values */ |
177 | static void |
178 | setupgeom(void) |
179 | { |
180 | geom.itemb = ITEMB; |
181 | geom.itemh = dc.fonth + ITEMB * 2; |
182 | geom.itemw = ITEMW; |
183 | geom.border = BORDER; |
a80fee22 |
184 | geom.separator = SEPARATOR; |
a7732690 |
185 | } |
186 | |
187 | /* grab pointer */ |
188 | static void |
189 | setupgrab(void) |
190 | { |
191 | XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask, |
192 | GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
193 | } |
194 | |
195 | /* allocate an item */ |
196 | static struct Item * |
a80fee22 |
197 | allocitem(const char *label, const char *output) |
a7732690 |
198 | { |
199 | struct Item *item; |
200 | |
201 | if ((item = malloc(sizeof *item)) == NULL) |
202 | err(1, "malloc"); |
7fbd1c5e |
203 | if (*label == '\0') { |
204 | item->label = NULL; |
205 | item->output = NULL; |
206 | } else { |
207 | if ((item->label = strdup(label)) == NULL) |
208 | err(1, "strdup"); |
209 | if ((item->output = strdup(output)) == NULL) |
210 | err(1, "strdup"); |
211 | } |
a80fee22 |
212 | item->y = 0; |
7fbd1c5e |
213 | item->h = item->label ? geom.itemh : geom.separator; |
a7732690 |
214 | item->next = NULL; |
215 | item->submenu = NULL; |
216 | |
217 | return item; |
218 | } |
219 | |
220 | /* allocate a menu */ |
221 | static struct Menu * |
222 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
223 | { |
224 | XSetWindowAttributes swa; |
225 | struct Menu *menu; |
226 | |
227 | if ((menu = malloc(sizeof *menu)) == NULL) |
228 | err(1, "malloc"); |
229 | menu->parent = parent; |
230 | menu->list = list; |
231 | menu->selected = NULL; |
a7732690 |
232 | menu->w = geom.itemw; |
a80fee22 |
233 | menu->h = 0; /* calculated by calcmenu() */ |
234 | menu->x = 0; /* calculated by calcmenu() */ |
235 | menu->y = 0; /* calculated by calcmenu() */ |
a7732690 |
236 | menu->level = level; |
a7732690 |
237 | |
238 | swa.override_redirect = override_redirect; |
239 | swa.background_pixel = dc.decoration[ColorBG]; |
240 | swa.border_pixel = dc.decoration[ColorFG]; |
241 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
242 | | PointerMotionMask; |
243 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border, |
244 | CopyFromParent, CopyFromParent, CopyFromParent, |
245 | CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, |
246 | &swa); |
247 | |
248 | return menu; |
249 | } |
250 | |
251 | /* create menus and items from the stdin */ |
252 | static void |
253 | parsestdin(void) |
254 | { |
255 | char *s, buf[BUFSIZ]; |
256 | char *label, *output; |
257 | unsigned level = 0; |
258 | unsigned i; |
a80fee22 |
259 | struct Item *curritem = NULL; /* item currently being read */ |
260 | struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
261 | struct Item *item; /* dummy item for for loops */ |
262 | struct Menu *menu; /* dummy menu for for loops */ |
a7732690 |
263 | size_t count = 0; /* number of items in the current menu */ |
264 | |
265 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
266 | level = 0; |
267 | s = buf; |
268 | |
269 | while (*s == '\t') { |
270 | level++; |
271 | s++; |
272 | } |
273 | |
274 | label = output = s; |
275 | |
276 | while (*s != '\0' && *s != '\t' && *s != '\n') |
277 | s++; |
278 | |
279 | while (*s == '\t') |
280 | *s++ = '\0'; |
281 | |
282 | if (*s != '\0' && *s != '\n') |
283 | output = s; |
284 | |
285 | while (*s != '\0' && *s != '\n') |
286 | s++; |
287 | |
288 | if (*s == '\n') |
289 | *s = '\0'; |
290 | |
a80fee22 |
291 | curritem = allocitem(label, output); |
a7732690 |
292 | |
293 | if (prevmenu == NULL) { /* there is no menu yet */ |
a80fee22 |
294 | menu = allocmenu(NULL, curritem, level); |
a7732690 |
295 | rootmenu = menu; |
296 | prevmenu = menu; |
297 | count = 1; |
298 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/ |
299 | for (menu = prevmenu, i = level; |
300 | menu != NULL && i < prevmenu->level; |
301 | menu = menu->parent, i++) |
302 | ; |
303 | |
304 | if (menu == NULL) |
305 | errx(1, "reached NULL menu"); |
306 | |
a80fee22 |
307 | for (item = menu->list; item->next != NULL; item = item->next) |
a7732690 |
308 | ; |
309 | |
a80fee22 |
310 | item->next = curritem; |
a7732690 |
311 | prevmenu = menu; |
312 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
a80fee22 |
313 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
314 | ; |
a80fee22 |
315 | item->next = curritem; |
a7732690 |
316 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
a80fee22 |
317 | menu = allocmenu(prevmenu, curritem, level); |
a7732690 |
318 | |
a80fee22 |
319 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
320 | ; |
321 | |
a80fee22 |
322 | item->submenu = menu; |
a7732690 |
323 | |
324 | prevmenu = menu; |
325 | } |
a80fee22 |
326 | count++; |
a7732690 |
327 | } |
328 | } |
329 | |
330 | /* calculate screen geometry */ |
331 | static void |
332 | calcscreengeom(void) |
333 | { |
334 | Window w1, w2; /* unused variables */ |
335 | int a, b; /* unused variables */ |
336 | unsigned mask; /* unused variable */ |
337 | |
a80fee22 |
338 | XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask); |
339 | screengeom.screenw = DisplayWidth(dpy, screen); |
340 | screengeom.screenh = DisplayHeight(dpy, screen); |
a7732690 |
341 | } |
342 | |
343 | /* recursivelly calculate height and position of the menus */ |
344 | static void |
345 | calcmenu(struct Menu *menu) |
346 | { |
347 | XWindowChanges changes; |
a80fee22 |
348 | struct Item *item; |
a7732690 |
349 | |
a80fee22 |
350 | /* calculate items positions and menu height */ |
351 | for (item = menu->list; item != NULL; item = item->next) { |
352 | item->y = menu->h; |
7fbd1c5e |
353 | if (item->label == NULL) /* height for separator item */ |
a80fee22 |
354 | menu->h += geom.separator; |
355 | else |
356 | menu->h += geom.itemh; |
357 | } |
a7732690 |
358 | |
359 | /* calculate menu's x and y positions */ |
360 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
a80fee22 |
361 | if (screengeom.screenw - screengeom.cursx >= menu->w) |
362 | menu->x = screengeom.cursx; |
363 | else if (screengeom.cursx > menu->w) |
364 | menu->x = screengeom.cursx - menu->w; |
365 | |
366 | if (screengeom.screenh - screengeom.cursy >= menu->h) |
367 | menu->y = screengeom.cursy; |
368 | else if (screengeom.screenh > menu->h) |
369 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
370 | } else { /* else, calculate in respect to parent menu */ |
371 | |
372 | /* search for the item in parent menu that generates this menu */ |
a80fee22 |
373 | for (item = menu->parent->list; item->submenu != menu; item = item->next) |
a7732690 |
374 | ; |
375 | |
a80fee22 |
376 | if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w) |
a7732690 |
377 | menu->x = menu->parent->x + menu->parent->w; |
378 | else if (menu->parent->x > menu->w) |
379 | menu->x = menu->parent->x - menu->w; |
380 | |
a80fee22 |
381 | if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) |
382 | menu->y = item->y + menu->parent->y; |
383 | else if (screengeom.screenh - menu->parent->y > menu->h) |
a7732690 |
384 | menu->y = menu->parent->y; |
a80fee22 |
385 | else if (screengeom.screenh > menu->h) |
386 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
387 | } |
388 | |
389 | /* update menu geometry */ |
390 | changes.height = menu->h; |
391 | changes.x = menu->x; |
392 | changes.y = menu->y; |
393 | XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes); |
394 | |
a80fee22 |
395 | /* calculate positions of submenus */ |
a7732690 |
396 | for (item = menu->list; item != NULL; item = item->next) { |
397 | if (item->submenu != NULL) |
398 | calcmenu(item->submenu); |
399 | } |
400 | } |
401 | |
402 | /* get menu and item of given window and position */ |
403 | static void |
a80fee22 |
404 | getmenuitem(Window win, int y, |
a7732690 |
405 | struct Menu **menu_ret, struct Item **item_ret) |
406 | { |
407 | struct Menu *menu = NULL; |
408 | struct Item *item = NULL; |
409 | |
410 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
411 | if (menu->win == win) { |
412 | for (item = menu->list; item != NULL; item = item->next) { |
7fbd1c5e |
413 | if (y >= item->y && y <= item->y + item->h) { |
a7732690 |
414 | goto done; |
415 | } |
416 | } |
417 | } |
418 | } |
419 | |
420 | |
421 | done: |
422 | *menu_ret = menu; |
423 | *item_ret = item; |
424 | } |
425 | |
426 | /* set currentmenu to menu, umap previous menus and map current menu and its parents */ |
427 | static void |
428 | setcurrmenu(struct Menu *currmenu_new) |
429 | { |
430 | struct Menu *menu; |
431 | |
432 | if (currmenu_new == currmenu) |
433 | return; |
434 | |
435 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
436 | XUnmapWindow(dpy, menu->win); |
437 | } |
438 | |
439 | currmenu = currmenu_new; |
440 | |
441 | for (menu = currmenu; menu != NULL; menu = menu->parent) |
442 | XMapWindow(dpy, menu->win); |
443 | } |
444 | |
445 | /* draw items of the current menu and of its ancestors */ |
446 | static void |
447 | drawmenu(void) |
448 | { |
449 | struct Menu *menu; |
450 | struct Item *item; |
451 | |
452 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
453 | for (item = menu->list; item != NULL; item = item->next) { |
454 | unsigned long *color; |
455 | size_t labellen; |
456 | int labelx, labely; |
a7732690 |
457 | |
458 | /* determine item color */ |
7fbd1c5e |
459 | if (item->label == NULL) |
460 | color = dc.decoration; |
461 | else if (item == menu->selected) |
a7732690 |
462 | color = dc.pressed; |
463 | else |
464 | color = dc.unpressed; |
465 | |
a7732690 |
466 | /* draw item box */ |
467 | XSetForeground(dpy, dc.gc, color[ColorBG]); |
7fbd1c5e |
468 | XFillRectangle(dpy, menu->win, dc.gc, 0, item->y, |
469 | geom.itemw, item->h); |
470 | |
471 | /* continue if item is a separator */ |
472 | if (item->label == NULL) |
473 | continue; |
a7732690 |
474 | |
475 | /* draw item label */ |
476 | labellen = strlen(item->label); |
477 | labelx = 0 + dc.fonth; |
7fbd1c5e |
478 | labely = item->y + dc.fonth + geom.itemb; |
a7732690 |
479 | XSetForeground(dpy, dc.gc, color[ColorFG]); |
480 | XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen); |
481 | |
482 | /* draw triangle, if item contains a submenu */ |
483 | if (item->submenu != NULL) { |
484 | int trianglex = geom.itemw - (geom.itemb + dc.fonth); |
7fbd1c5e |
485 | int triangley = item->y + geom.itemb; |
a7732690 |
486 | |
487 | XPoint triangle[] = { |
488 | {trianglex, triangley}, |
489 | {trianglex + dc.fonth, triangley + dc.fonth/2}, |
490 | {trianglex, triangley + dc.fonth}, |
491 | {trianglex, triangley} |
492 | }; |
493 | |
494 | XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle), |
495 | Convex, CoordModeOrigin); |
496 | } |
a7732690 |
497 | } |
498 | } |
499 | } |
500 | |
501 | /* run event loop */ |
502 | static void |
503 | run(void) |
504 | { |
505 | struct Menu *menu; |
506 | struct Item *item; |
507 | struct Item *previtem = NULL; |
508 | XEvent ev; |
509 | |
510 | setcurrmenu(rootmenu); |
511 | |
512 | while (!XNextEvent(dpy, &ev)) { |
513 | switch(ev.type) { |
514 | case Expose: |
515 | drawmenu(); |
516 | break; |
517 | case MotionNotify: |
a80fee22 |
518 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
519 | if (menu != NULL && item != NULL) { |
520 | if (previtem != item) { |
521 | if (item->submenu != NULL) |
522 | setcurrmenu(item->submenu); |
523 | else |
524 | setcurrmenu(menu); |
525 | previtem = item; |
526 | } else if (menu->selected != item) |
527 | menu->selected = item; |
528 | } |
529 | drawmenu(); |
530 | break; |
531 | case ButtonRelease: |
a80fee22 |
532 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
533 | if (menu != NULL && item != NULL) { |
7fbd1c5e |
534 | if (item->label == NULL) |
535 | break; /* ignore separators */ |
a7732690 |
536 | if (item->submenu != NULL) { |
537 | setcurrmenu(item->submenu); |
538 | } else { |
539 | printf("%s\n", item->output); |
540 | cleanupexit(); |
541 | } |
542 | drawmenu(); |
543 | } else { |
544 | cleanupexit(); |
545 | } |
546 | break; |
547 | } |
548 | } |
549 | } |
550 | |
551 | /* cleanup and exit */ |
552 | static void |
553 | cleanupexit(void) |
554 | { |
555 | XCloseDisplay(dpy); |
556 | exit(0); |
557 | } |
558 | |
559 | /* show usage */ |
560 | static void |
561 | usage(void) |
562 | { |
563 | (void)fprintf(stderr, "usage: xmenu [-w] menuname\n"); |
564 | exit(1); |
565 | } |