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; |
09c13122 |
348 | XSizeHints sizeh; |
a80fee22 |
349 | struct Item *item; |
a7732690 |
350 | |
a80fee22 |
351 | /* calculate items positions and menu height */ |
352 | for (item = menu->list; item != NULL; item = item->next) { |
353 | item->y = menu->h; |
7fbd1c5e |
354 | if (item->label == NULL) /* height for separator item */ |
a80fee22 |
355 | menu->h += geom.separator; |
356 | else |
357 | menu->h += geom.itemh; |
358 | } |
a7732690 |
359 | |
360 | /* calculate menu's x and y positions */ |
361 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
a80fee22 |
362 | if (screengeom.screenw - screengeom.cursx >= menu->w) |
363 | menu->x = screengeom.cursx; |
364 | else if (screengeom.cursx > menu->w) |
365 | menu->x = screengeom.cursx - menu->w; |
366 | |
367 | if (screengeom.screenh - screengeom.cursy >= menu->h) |
368 | menu->y = screengeom.cursy; |
369 | else if (screengeom.screenh > menu->h) |
370 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
371 | } else { /* else, calculate in respect to parent menu */ |
372 | |
373 | /* search for the item in parent menu that generates this menu */ |
a80fee22 |
374 | for (item = menu->parent->list; item->submenu != menu; item = item->next) |
a7732690 |
375 | ; |
376 | |
a80fee22 |
377 | if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w) |
a7732690 |
378 | menu->x = menu->parent->x + menu->parent->w; |
379 | else if (menu->parent->x > menu->w) |
380 | menu->x = menu->parent->x - menu->w; |
381 | |
a80fee22 |
382 | if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) |
383 | menu->y = item->y + menu->parent->y; |
384 | else if (screengeom.screenh - menu->parent->y > menu->h) |
a7732690 |
385 | menu->y = menu->parent->y; |
a80fee22 |
386 | else if (screengeom.screenh > menu->h) |
387 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
388 | } |
389 | |
390 | /* update menu geometry */ |
391 | changes.height = menu->h; |
392 | changes.x = menu->x; |
393 | changes.y = menu->y; |
394 | XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes); |
395 | |
09c13122 |
396 | /* set window manager size hints */ |
397 | sizeh.flags = PMaxSize | PMinSize; |
398 | sizeh.min_width = sizeh.max_width = menu->w; |
399 | sizeh.min_height = sizeh.max_height = menu->h; |
400 | XSetWMNormalHints(dpy, menu->win, &sizeh); |
401 | |
a80fee22 |
402 | /* calculate positions of submenus */ |
a7732690 |
403 | for (item = menu->list; item != NULL; item = item->next) { |
404 | if (item->submenu != NULL) |
405 | calcmenu(item->submenu); |
406 | } |
407 | } |
408 | |
409 | /* get menu and item of given window and position */ |
410 | static void |
a80fee22 |
411 | getmenuitem(Window win, int y, |
a7732690 |
412 | struct Menu **menu_ret, struct Item **item_ret) |
413 | { |
414 | struct Menu *menu = NULL; |
415 | struct Item *item = NULL; |
416 | |
417 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
418 | if (menu->win == win) { |
419 | for (item = menu->list; item != NULL; item = item->next) { |
7fbd1c5e |
420 | if (y >= item->y && y <= item->y + item->h) { |
a7732690 |
421 | goto done; |
422 | } |
423 | } |
424 | } |
425 | } |
426 | |
427 | |
428 | done: |
429 | *menu_ret = menu; |
430 | *item_ret = item; |
431 | } |
432 | |
433 | /* set currentmenu to menu, umap previous menus and map current menu and its parents */ |
434 | static void |
435 | setcurrmenu(struct Menu *currmenu_new) |
436 | { |
437 | struct Menu *menu; |
438 | |
439 | if (currmenu_new == currmenu) |
440 | return; |
441 | |
442 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
443 | XUnmapWindow(dpy, menu->win); |
444 | } |
445 | |
446 | currmenu = currmenu_new; |
447 | |
448 | for (menu = currmenu; menu != NULL; menu = menu->parent) |
449 | XMapWindow(dpy, menu->win); |
450 | } |
451 | |
452 | /* draw items of the current menu and of its ancestors */ |
453 | static void |
454 | drawmenu(void) |
455 | { |
456 | struct Menu *menu; |
457 | struct Item *item; |
458 | |
459 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
460 | for (item = menu->list; item != NULL; item = item->next) { |
461 | unsigned long *color; |
462 | size_t labellen; |
463 | int labelx, labely; |
a7732690 |
464 | |
465 | /* determine item color */ |
7fbd1c5e |
466 | if (item->label == NULL) |
467 | color = dc.decoration; |
468 | else if (item == menu->selected) |
a7732690 |
469 | color = dc.pressed; |
470 | else |
471 | color = dc.unpressed; |
472 | |
a7732690 |
473 | /* draw item box */ |
474 | XSetForeground(dpy, dc.gc, color[ColorBG]); |
7fbd1c5e |
475 | XFillRectangle(dpy, menu->win, dc.gc, 0, item->y, |
476 | geom.itemw, item->h); |
477 | |
478 | /* continue if item is a separator */ |
479 | if (item->label == NULL) |
480 | continue; |
a7732690 |
481 | |
482 | /* draw item label */ |
483 | labellen = strlen(item->label); |
484 | labelx = 0 + dc.fonth; |
7fbd1c5e |
485 | labely = item->y + dc.fonth + geom.itemb; |
a7732690 |
486 | XSetForeground(dpy, dc.gc, color[ColorFG]); |
487 | XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen); |
488 | |
489 | /* draw triangle, if item contains a submenu */ |
490 | if (item->submenu != NULL) { |
491 | int trianglex = geom.itemw - (geom.itemb + dc.fonth); |
7fbd1c5e |
492 | int triangley = item->y + geom.itemb; |
a7732690 |
493 | |
494 | XPoint triangle[] = { |
495 | {trianglex, triangley}, |
496 | {trianglex + dc.fonth, triangley + dc.fonth/2}, |
497 | {trianglex, triangley + dc.fonth}, |
498 | {trianglex, triangley} |
499 | }; |
500 | |
501 | XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle), |
502 | Convex, CoordModeOrigin); |
503 | } |
a7732690 |
504 | } |
505 | } |
506 | } |
507 | |
508 | /* run event loop */ |
509 | static void |
510 | run(void) |
511 | { |
512 | struct Menu *menu; |
513 | struct Item *item; |
514 | struct Item *previtem = NULL; |
515 | XEvent ev; |
516 | |
517 | setcurrmenu(rootmenu); |
518 | |
519 | while (!XNextEvent(dpy, &ev)) { |
520 | switch(ev.type) { |
521 | case Expose: |
522 | drawmenu(); |
523 | break; |
524 | case MotionNotify: |
a80fee22 |
525 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
526 | if (menu != NULL && item != NULL) { |
527 | if (previtem != item) { |
528 | if (item->submenu != NULL) |
529 | setcurrmenu(item->submenu); |
530 | else |
531 | setcurrmenu(menu); |
532 | previtem = item; |
533 | } else if (menu->selected != item) |
534 | menu->selected = item; |
535 | } |
536 | drawmenu(); |
537 | break; |
538 | case ButtonRelease: |
a80fee22 |
539 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
540 | if (menu != NULL && item != NULL) { |
7fbd1c5e |
541 | if (item->label == NULL) |
542 | break; /* ignore separators */ |
a7732690 |
543 | if (item->submenu != NULL) { |
544 | setcurrmenu(item->submenu); |
545 | } else { |
546 | printf("%s\n", item->output); |
547 | cleanupexit(); |
548 | } |
549 | drawmenu(); |
550 | } else { |
551 | cleanupexit(); |
552 | } |
553 | break; |
554 | } |
555 | } |
556 | } |
557 | |
558 | /* cleanup and exit */ |
559 | static void |
560 | cleanupexit(void) |
561 | { |
562 | XCloseDisplay(dpy); |
563 | exit(0); |
564 | } |
565 | |
566 | /* show usage */ |
567 | static void |
568 | usage(void) |
569 | { |
570 | (void)fprintf(stderr, "usage: xmenu [-w] menuname\n"); |
571 | exit(1); |
572 | } |