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