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