Initial commit
[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 */
32 int border; /* window border */
33};
34
35/* screen geometry structure */
36struct ScreenGeometry {
37 int cursx, cursy; /* cursor position */
38 int screenw, screenh; /* screen width and height */
39};
40
41/* menu item structure */
42struct Item {
43 char *label;
44 char *output;
45 int x, y;
46 struct Item *next;
47 struct Menu *submenu;
48};
49
50/* menu structure */
51struct Menu {
52 struct Menu *parent;
53 struct Item *list;
54 struct Item *selected;
55 int x, y, w, h;
56 unsigned level;
57 unsigned nitems;
58 Window win;
59};
60
61/* function declarations */
62static unsigned long getcolor(const char *s);
63static void setupdc(void);
64static void setupgeom(void);
65static void setupgrab(void);
66static struct Item *allocitem(size_t count, const char *label, const char *output);
67static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
68static void getmenuitem(Window win, int x, int y,
69 struct Menu **menu_ret, struct Item **item_ret);
70static void drawmenu(void);
71static void calcscreengeom(void);
72static void calcmenu(struct Menu *menu);
73static void setcurrmenu(struct Menu *currmenu_new);
74static void parsestdin(void);
75static void run(void);
76static void cleanupexit(void);
77static void usage(void);
78
79/* X variables */
80static Colormap colormap;
81static Display *dpy;
82static Window rootwin;
83static int screen;
84static struct DC dc;
85
86/* menu variables */
87static struct Menu *rootmenu = NULL;
88static struct Menu *currmenu = NULL;
89
90/* geometry variables */
91static struct Geometry geom;
92static struct ScreenGeometry sgeom;
93
94/* flag variables */
95static Bool override_redirect = True;
96
97#include "config.h"
98
99int
100main(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 */
143static unsigned long
144getcolor(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 */
154static void
155setupdc(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 */
176static void
177setupgeom(void)
178{
179 geom.itemb = ITEMB;
180 geom.itemh = dc.fonth + ITEMB * 2;
181 geom.itemw = ITEMW;
182 geom.border = BORDER;
183}
184
185/* grab pointer */
186static void
187setupgrab(void)
188{
189 XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask,
190 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
191}
192
193/* allocate an item */
194static struct Item *
195allocitem(size_t count, const char *label, const char *output)
196{
197 struct Item *item;
198
199 if ((item = malloc(sizeof *item)) == NULL)
200 err(1, "malloc");
201 if ((item->label = strdup(label)) == NULL)
202 err(1, "strdup");
203 if ((item->output = strdup(output)) == NULL)
204 err(1, "strdup");
205 item->x = 0;
206 item->y = count * geom.itemh;
207 item->next = NULL;
208 item->submenu = NULL;
209
210 return item;
211}
212
213/* allocate a menu */
214static struct Menu *
215allocmenu(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;
225 menu->x = 0;
226 menu->y = 0;
227 menu->w = geom.itemw;
228 menu->h = geom.itemh;
229 menu->level = level;
230 menu->nitems = 0;
231
232 swa.override_redirect = override_redirect;
233 swa.background_pixel = dc.decoration[ColorBG];
234 swa.border_pixel = dc.decoration[ColorFG];
235 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
236 | PointerMotionMask;
237 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
238 CopyFromParent, CopyFromParent, CopyFromParent,
239 CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
240 &swa);
241
242 return menu;
243}
244
245/* create menus and items from the stdin */
246static void
247parsestdin(void)
248{
249 char *s, buf[BUFSIZ];
250 char *label, *output;
251 unsigned level = 0;
252 unsigned i;
253 struct Item *item, *p;
254 struct Menu *menu;
255 struct Menu *prevmenu = NULL;
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
284 item = allocitem(count, label, output);
285
286 if (prevmenu == NULL) { /* there is no menu yet */
287 menu = allocmenu(NULL, item, level);
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
300 for (p = menu->list; p->next != NULL; p = p->next)
301 ;
302
303 p->next = item;
304 prevmenu = menu;
305 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
306 for (p = prevmenu->list; p->next != NULL; p = p->next)
307 ;
308 p->next = item;
309 } else if (level > prevmenu->level) { /* item begins a new menu */
310 menu = allocmenu(prevmenu, item, level);
311
312 for (p = prevmenu->list; p->next != NULL; p = p->next)
313 ;
314
315 p->submenu = menu;
316
317 prevmenu = menu;
318 }
319 }
320}
321
322/* calculate screen geometry */
323static void
324calcscreengeom(void)
325{
326 Window w1, w2; /* unused variables */
327 int a, b; /* unused variables */
328 unsigned mask; /* unused variable */
329
330 XQueryPointer(dpy, rootwin, &w1, &w2, &sgeom.cursx, &sgeom.cursy, &a, &b, &mask);
331 sgeom.screenw = DisplayWidth(dpy, screen);
332 sgeom.screenh = DisplayHeight(dpy, screen);
333}
334
335/* recursivelly calculate height and position of the menus */
336static void
337calcmenu(struct Menu *menu)
338{
339 XWindowChanges changes;
340 struct Item *item, *p;
341 size_t i;
342
343 /* calculate number of items */
344 i = 0;
345 for (item = menu->list; item != NULL; item = item->next)
346 i++;
347 menu->nitems = i;
348 menu->h = geom.itemh * i;
349
350 /* calculate menu's x and y positions */
351 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
352 if (sgeom.screenw - sgeom.cursx >= menu->w)
353 menu->x = sgeom.cursx;
354 else if (sgeom.cursx > menu->w)
355 menu->x = sgeom.cursx - menu->w;
356
357 if (sgeom.screenh - sgeom.cursy >= menu->h)
358 menu->y = sgeom.cursy;
359 else if (sgeom.screenh > menu->h)
360 menu->y = sgeom.screenh - menu->h;
361 } else { /* else, calculate in respect to parent menu */
362
363 /* search for the item in parent menu that generates this menu */
364 for (p = menu->parent->list; p->submenu != menu; p = p->next)
365 ;
366
367 if (sgeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w)
368 menu->x = menu->parent->x + menu->parent->w;
369 else if (menu->parent->x > menu->w)
370 menu->x = menu->parent->x - menu->w;
371
372 if (sgeom.screenh - p->y > menu->h)
373 menu->y = p->y;
374 else if (sgeom.screenh - menu->parent->y > menu->h)
375 menu->y = menu->parent->y;
376 else if (sgeom.screenh > menu->h)
377 menu->y = sgeom.screenh - menu->h;
378 }
379
380 /* calculate position of each item in the menu */
381 for (i = 0, item = menu->list; item != NULL; item = item->next, i++) {
382 item->x = menu->x;
383 item->y = menu->y + i * geom.itemh;
384 }
385
386 /* update menu geometry */
387 changes.height = menu->h;
388 changes.x = menu->x;
389 changes.y = menu->y;
390 XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes);
391
392 for (item = menu->list; item != NULL; item = item->next) {
393 if (item->submenu != NULL)
394 calcmenu(item->submenu);
395 }
396}
397
398/* get menu and item of given window and position */
399static void
400getmenuitem(Window win, int x, int y,
401 struct Menu **menu_ret, struct Item **item_ret)
402{
403 struct Menu *menu = NULL;
404 struct Item *item = NULL;
405
406 for (menu = currmenu; menu != NULL; menu = menu->parent) {
407 if (menu->win == win) {
408 for (item = menu->list; item != NULL; item = item->next) {
409 if (x >= item->x && x <= item->x + geom.itemw &&
410 y >= item->y && y <= item->y + geom.itemh) {
411 goto done;
412 }
413 }
414 }
415 }
416
417
418done:
419 *menu_ret = menu;
420 *item_ret = item;
421}
422
423/* set currentmenu to menu, umap previous menus and map current menu and its parents */
424static void
425setcurrmenu(struct Menu *currmenu_new)
426{
427 struct Menu *menu;
428
429 if (currmenu_new == currmenu)
430 return;
431
432 for (menu = currmenu; menu != NULL; menu = menu->parent) {
433 XUnmapWindow(dpy, menu->win);
434 }
435
436 currmenu = currmenu_new;
437
438 for (menu = currmenu; menu != NULL; menu = menu->parent)
439 XMapWindow(dpy, menu->win);
440}
441
442/* draw items of the current menu and of its ancestors */
443static void
444drawmenu(void)
445{
446 struct Menu *menu;
447 struct Item *item;
448
449 for (menu = currmenu; menu != NULL; menu = menu->parent) {
450 size_t nitems; /* number of items before current item */
451
452 nitems = 0;
453 for (item = menu->list; item != NULL; item = item->next) {
454 unsigned long *color;
455 size_t labellen;
456 int labelx, labely;
457 int y;
458
459 /* determine item color */
460 if (item == menu->selected)
461 color = dc.pressed;
462 else
463 color = dc.unpressed;
464
465 /* calculate item's y position */
466 y = nitems * geom.itemh;
467
468 /* draw item box */
469 XSetForeground(dpy, dc.gc, color[ColorBG]);
470 XFillRectangle(dpy, menu->win, dc.gc, 0, y,
471 geom.itemw, geom.itemh);
472
473 /* draw item label */
474 labellen = strlen(item->label);
475 labelx = 0 + dc.fonth;
476 labely = y + dc.fonth + geom.itemb;
477 XSetForeground(dpy, dc.gc, color[ColorFG]);
478 XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen);
479
480 /* draw triangle, if item contains a submenu */
481 if (item->submenu != NULL) {
482 int trianglex = geom.itemw - (geom.itemb + dc.fonth);
483 int triangley = y + geom.itemb;
484
485 XPoint triangle[] = {
486 {trianglex, triangley},
487 {trianglex + dc.fonth, triangley + dc.fonth/2},
488 {trianglex, triangley + dc.fonth},
489 {trianglex, triangley}
490 };
491
492 XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle),
493 Convex, CoordModeOrigin);
494 }
495
496 nitems++;
497 }
498 }
499}
500
501/* run event loop */
502static void
503run(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:
518 getmenuitem(ev.xbutton.window, ev.xbutton.x_root, ev.xbutton.y_root,
519 &menu, &item);
520 if (menu != NULL && item != NULL) {
521 if (previtem != item) {
522 if (item->submenu != NULL)
523 setcurrmenu(item->submenu);
524 else
525 setcurrmenu(menu);
526 previtem = item;
527 } else if (menu->selected != item)
528 menu->selected = item;
529 }
530 drawmenu();
531 break;
532 case ButtonRelease:
533 getmenuitem(ev.xbutton.window, ev.xbutton.x_root, ev.xbutton.y_root,
534 &menu, &item);
535 if (menu != NULL && item != NULL) {
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 */
552static void
553cleanupexit(void)
554{
555 XCloseDisplay(dpy);
556 exit(0);
557}
558
559/* show usage */
560static void
561usage(void)
562{
563 (void)fprintf(stderr, "usage: xmenu [-w] menuname\n");
564 exit(1);
565}