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