Removed the -w option (it was too buggy)
[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>
f644b8bc 8#include <X11/Xresource.h>
858338d9 9#include <X11/XKBlib.h>
dbeb9940 10#include <X11/Xft/Xft.h>
858338d9 11
d2435fcd 12#define PROGNAME "xmenu"
858338d9 13#define ITEMPREV 0
14#define ITEMNEXT 1
a7732690 15
16/* macros */
17#define LEN(x) (sizeof (x) / sizeof (x[0]))
f1583285 18#define MAX(x,y) ((x)>(y)?(x):(y))
d8a7caf2 19#define MIN(x,y) ((x)<(y)?(x):(y))
a7732690 20
21/* color enum */
22enum {ColorFG, ColorBG, ColorLast};
23
24/* draw context structure */
25struct DC {
dbeb9940 26 XftColor normal[ColorLast];
27 XftColor selected[ColorLast];
28 XftColor decoration[ColorLast];
a7732690 29
30 Drawable d;
31 GC gc;
dbeb9940 32 XftFont *font;
a7732690 33};
34
35/* menu geometry structure */
36struct Geometry {
37 int itemb; /* item border */
38 int itemw; /* item width */
39 int itemh; /* item height */
a80fee22 40 int border; /* window border width */
41 int separator; /* menu separator width */
a7732690 42};
43
44/* screen geometry structure */
45struct ScreenGeometry {
46 int cursx, cursy; /* cursor position */
47 int screenw, screenh; /* screen width and height */
48};
49
50/* menu item structure */
51struct Item {
d8a7caf2 52 char *label; /* string to be drawed on menu */
53 char *output; /* string to be outputed when item is clicked */
54 int y; /* item y position relative to menu */
55 int h; /* item height */
56 size_t labellen; /* strlen(label) */
858338d9 57 struct Item *prev; /* previous item */
d8a7caf2 58 struct Item *next; /* next item */
59 struct Menu *submenu; /* submenu spawned by clicking on item */
a7732690 60};
61
62/* menu structure */
63struct Menu {
d8a7caf2 64 struct Menu *parent; /* parent menu */
65 struct Item *caller; /* item that spawned the menu */
66 struct Item *list; /* list of items contained by the menu */
67 struct Item *selected; /* item currently selected in the menu */
68 int x, y, w, h; /* menu geometry */
69 unsigned level; /* menu level relative to root */
70 Drawable pixmap; /* pixmap to draw the menu on */
dbeb9940 71 XftDraw *draw;
d8a7caf2 72 Window win; /* menu window to map on the screen */
a7732690 73};
74
75/* function declarations */
dbeb9940 76static void getcolor(const char *s, XftColor *color);
f644b8bc 77static void getresources(void);
a7732690 78static void setupdc(void);
79static void setupgeom(void);
a80fee22 80static struct Item *allocitem(const char *label, const char *output);
a7732690 81static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
f15fc339 82static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret);
a7732690 83static void drawmenu(void);
84static void calcscreengeom(void);
85static void calcmenu(struct Menu *menu);
85003546 86static void grabpointer(void);
87static void grabkeyboard(void);
a7732690 88static void setcurrmenu(struct Menu *currmenu_new);
89static void parsestdin(void);
90static void run(void);
f15fc339 91static void freewindow(struct Menu *menu);
08f16589 92static void cleanup(void);
a7732690 93static void usage(void);
94
95/* X variables */
96static Colormap colormap;
97static Display *dpy;
dbeb9940 98static Visual *visual;
a7732690 99static Window rootwin;
100static int screen;
101static struct DC dc;
102
103/* menu variables */
104static struct Menu *rootmenu = NULL;
105static struct Menu *currmenu = NULL;
d2435fcd 106static char **menutitle;
107static int menutitlecount;
a7732690 108
109/* geometry variables */
110static struct Geometry geom;
a80fee22 111static struct ScreenGeometry screengeom;
a7732690 112
a7732690 113#include "config.h"
114
115int
116main(int argc, char *argv[])
117{
118 int ch;
119
27443900 120 while ((ch = getopt(argc, argv, "")) != -1) {
a7732690 121 switch (ch) {
a7732690 122 default:
123 usage();
124 break;
125 }
126 }
127 argc -= optind;
128 argv += optind;
129
d2435fcd 130 menutitle = argv;
131 menutitlecount = argc;
132
a7732690 133 /* open connection to server and set X variables */
134 if ((dpy = XOpenDisplay(NULL)) == NULL)
135 errx(1, "cannot open display");
136 screen = DefaultScreen(dpy);
dbeb9940 137 visual = DefaultVisual(dpy, screen);
a7732690 138 rootwin = RootWindow(dpy, screen);
139 colormap = DefaultColormap(dpy, screen);
21a9aaec 140
a7732690 141 /* setup */
f644b8bc 142 getresources();
a7732690 143 setupdc();
144 setupgeom();
a7732690 145
146 /* generate menus and recalculate them */
147 parsestdin();
148 if (rootmenu == NULL)
149 errx(1, "no menu generated");
150 calcscreengeom();
151 calcmenu(rootmenu);
152
465d07db 153 /* grab mouse and keyboard */
27443900 154 grabpointer();
155 grabkeyboard();
465d07db 156
d8a7caf2 157 /* map root menu */
27443900 158 setcurrmenu(rootmenu);
d8a7caf2 159 XMapWindow(dpy, rootmenu->win);
160
a7732690 161 /* run event loop */
162 run();
163
08f16589 164 cleanup();
21a9aaec 165 return 0;
166}
167
f644b8bc 168/* read xrdb for configuration options */
169static void
170getresources(void)
171{
172 char *xrm;
173 long n;
174
175 XrmInitialize();
176 if ((xrm = XResourceManagerString(dpy))) {
177 char *type;
178 XrmDatabase xdb;
179 XrmValue xval;
180
181 xdb = XrmGetStringDatabase(xrm);
182
183 if (XrmGetResource(xdb, "xmenu.menuborder", "*", &type, &xval) == True)
184 if ((n = strtol(xval.addr, NULL, 10)) > 0)
185 menuborder = n;
186 if (XrmGetResource(xdb, "xmenu.separatorsize", "*", &type, &xval) == True)
187 if ((n = strtol(xval.addr, NULL, 10)) > 0)
188 separatorsize = n;
189 if (XrmGetResource(xdb, "xmenu.itemborder", "*", &type, &xval) == True)
190 if ((n = strtol(xval.addr, NULL, 10)) > 0)
191 itemborder = n;
192 if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
193 if ((n = strtol(xval.addr, NULL, 10)) > 0)
194 width = n;
195 if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
196 background = strdup(xval.addr);
197 if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
198 foreground = strdup(xval.addr);
199 if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
200 selbackground = strdup(xval.addr);
201 if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
202 selforeground = strdup(xval.addr);
203 if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
204 separator = strdup(xval.addr);
205 if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
206 border = strdup(xval.addr);
207 if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
208 font = strdup(xval.addr);
209
210 XrmDestroyDatabase(xdb);
211 }
212}
213
a7732690 214/* get color from color string */
dbeb9940 215static void
216getcolor(const char *s, XftColor *color)
a7732690 217{
dbeb9940 218 if(!XftColorAllocName(dpy, visual, colormap, s, color))
a7732690 219 errx(1, "cannot allocate color: %s", s);
a7732690 220}
221
222/* init draw context */
223static void
224setupdc(void)
225{
226 /* get color pixels */
dbeb9940 227 getcolor(background, &dc.normal[ColorBG]);
228 getcolor(foreground, &dc.normal[ColorFG]);
229 getcolor(selbackground, &dc.selected[ColorBG]);
230 getcolor(selforeground, &dc.selected[ColorFG]);
231 getcolor(separator, &dc.decoration[ColorBG]);
232 getcolor(border, &dc.decoration[ColorFG]);
a7732690 233
234 /* try to get font */
dbeb9940 235 if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
a7732690 236 errx(1, "cannot load font");
a7732690 237
dbeb9940 238 /* create GC */
a7732690 239 dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
a7732690 240}
241
242/* init menu geometry values */
243static void
244setupgeom(void)
245{
f644b8bc 246 geom.itemb = itemborder;
dbeb9940 247 geom.itemh = dc.font->height + itemborder * 2;
f644b8bc 248 geom.itemw = width;
249 geom.border = menuborder;
250 geom.separator = separatorsize;
a7732690 251}
252
a7732690 253/* allocate an item */
254static struct Item *
a80fee22 255allocitem(const char *label, const char *output)
a7732690 256{
257 struct Item *item;
258
259 if ((item = malloc(sizeof *item)) == NULL)
260 err(1, "malloc");
7fbd1c5e 261 if (*label == '\0') {
262 item->label = NULL;
263 item->output = NULL;
264 } else {
265 if ((item->label = strdup(label)) == NULL)
266 err(1, "strdup");
267 if ((item->output = strdup(output)) == NULL)
268 err(1, "strdup");
269 }
a80fee22 270 item->y = 0;
7fbd1c5e 271 item->h = item->label ? geom.itemh : geom.separator;
f1583285 272 if (item->label == NULL)
273 item->labellen = 0;
274 else
275 item->labellen = strlen(item->label);
a7732690 276 item->next = NULL;
277 item->submenu = NULL;
278
279 return item;
280}
281
282/* allocate a menu */
283static struct Menu *
284allocmenu(struct Menu *parent, struct Item *list, unsigned level)
285{
286 XSetWindowAttributes swa;
287 struct Menu *menu;
288
289 if ((menu = malloc(sizeof *menu)) == NULL)
290 err(1, "malloc");
291 menu->parent = parent;
292 menu->list = list;
d888f2ca 293 menu->caller = NULL;
a7732690 294 menu->selected = NULL;
a7732690 295 menu->w = geom.itemw;
a80fee22 296 menu->h = 0; /* calculated by calcmenu() */
297 menu->x = 0; /* calculated by calcmenu() */
298 menu->y = 0; /* calculated by calcmenu() */
a7732690 299 menu->level = level;
a7732690 300
27443900 301 swa.override_redirect = True;
dbeb9940 302 swa.background_pixel = dc.decoration[ColorBG].pixel;
303 swa.border_pixel = dc.decoration[ColorFG].pixel;
a7732690 304 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
f15fc339 305 | PointerMotionMask | LeaveWindowMask;
a7732690 306 menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
307 CopyFromParent, CopyFromParent, CopyFromParent,
308 CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
309 &swa);
310
311 return menu;
312}
313
314/* create menus and items from the stdin */
315static void
316parsestdin(void)
317{
318 char *s, buf[BUFSIZ];
319 char *label, *output;
320 unsigned level = 0;
321 unsigned i;
a80fee22 322 struct Item *curritem = NULL; /* item currently being read */
323 struct Menu *prevmenu = NULL; /* menu the previous item was added to */
324 struct Item *item; /* dummy item for for loops */
325 struct Menu *menu; /* dummy menu for for loops */
a7732690 326 size_t count = 0; /* number of items in the current menu */
327
328 while (fgets(buf, BUFSIZ, stdin) != NULL) {
329 level = 0;
330 s = buf;
331
332 while (*s == '\t') {
333 level++;
334 s++;
335 }
336
337 label = output = s;
338
339 while (*s != '\0' && *s != '\t' && *s != '\n')
340 s++;
341
342 while (*s == '\t')
343 *s++ = '\0';
344
345 if (*s != '\0' && *s != '\n')
346 output = s;
347
348 while (*s != '\0' && *s != '\n')
349 s++;
350
351 if (*s == '\n')
352 *s = '\0';
353
a80fee22 354 curritem = allocitem(label, output);
a7732690 355
356 if (prevmenu == NULL) { /* there is no menu yet */
a80fee22 357 menu = allocmenu(NULL, curritem, level);
a7732690 358 rootmenu = menu;
359 prevmenu = menu;
360 count = 1;
858338d9 361 curritem->prev = NULL;
362 curritem->next = NULL;
a7732690 363 } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
364 for (menu = prevmenu, i = level;
365 menu != NULL && i < prevmenu->level;
366 menu = menu->parent, i++)
367 ;
368
369 if (menu == NULL)
370 errx(1, "reached NULL menu");
371
a80fee22 372 for (item = menu->list; item->next != NULL; item = item->next)
a7732690 373 ;
374
a80fee22 375 item->next = curritem;
858338d9 376
377 curritem->prev = item;
378 curritem->next = NULL;
379
a7732690 380 prevmenu = menu;
381 } else if (level == prevmenu->level) { /* item is a continuation of current menu */
a80fee22 382 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 383 ;
a80fee22 384 item->next = curritem;
858338d9 385
386 curritem->prev = item;
387 curritem->next = NULL;
388
a7732690 389 } else if (level > prevmenu->level) { /* item begins a new menu */
a80fee22 390 menu = allocmenu(prevmenu, curritem, level);
a7732690 391
a80fee22 392 for (item = prevmenu->list; item->next != NULL; item = item->next)
a7732690 393 ;
394
a80fee22 395 item->submenu = menu;
d888f2ca 396 menu->caller = item;
a7732690 397
858338d9 398 curritem->prev = NULL;
399 curritem->next = NULL;
400
a7732690 401 prevmenu = menu;
402 }
a80fee22 403 count++;
a7732690 404 }
405}
406
407/* calculate screen geometry */
408static void
409calcscreengeom(void)
410{
411 Window w1, w2; /* unused variables */
412 int a, b; /* unused variables */
413 unsigned mask; /* unused variable */
414
a80fee22 415 XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
416 screengeom.screenw = DisplayWidth(dpy, screen);
417 screengeom.screenh = DisplayHeight(dpy, screen);
a7732690 418}
419
21a9aaec 420/* recursivelly calculate menu geometry and set window hints */
a7732690 421static void
422calcmenu(struct Menu *menu)
423{
d2435fcd 424 static XClassHint classh = {PROGNAME, PROGNAME};
a7732690 425 XWindowChanges changes;
d2435fcd 426 XTextProperty textprop;
09c13122 427 XSizeHints sizeh;
dbeb9940 428 XGlyphInfo ext;
a80fee22 429 struct Item *item;
f1583285 430 int labelwidth;
a7732690 431
f1583285 432 /* calculate items positions and menu width and height */
433 menu->w = geom.itemw;
a80fee22 434 for (item = menu->list; item != NULL; item = item->next) {
435 item->y = menu->h;
7fbd1c5e 436 if (item->label == NULL) /* height for separator item */
a80fee22 437 menu->h += geom.separator;
438 else
439 menu->h += geom.itemh;
f1583285 440
dbeb9940 441 XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
442 item->labellen, &ext);
443 labelwidth = ext.xOff + dc.font->height * 2;
f1583285 444 menu->w = MAX(menu->w, labelwidth);
a80fee22 445 }
a7732690 446
447 /* calculate menu's x and y positions */
448 if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
a80fee22 449 if (screengeom.screenw - screengeom.cursx >= menu->w)
450 menu->x = screengeom.cursx;
451 else if (screengeom.cursx > menu->w)
452 menu->x = screengeom.cursx - menu->w;
453
454 if (screengeom.screenh - screengeom.cursy >= menu->h)
455 menu->y = screengeom.cursy;
456 else if (screengeom.screenh > menu->h)
457 menu->y = screengeom.screenh - menu->h;
d2435fcd 458
459 XStringListToTextProperty(menutitle, menutitlecount, &textprop);
a7732690 460 } else { /* else, calculate in respect to parent menu */
f644b8bc 461 if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w)
462 menu->x = menu->parent->x + menu->parent->w + geom.border;
463 else if (menu->parent->x > menu->w + geom.border)
464 menu->x = menu->parent->x - menu->w - geom.border;
a7732690 465
8455c369 466 if (screengeom.screenh - (menu->caller->y + menu->parent->y) > menu->h)
467 menu->y = menu->caller->y + menu->parent->y;
a80fee22 468 else if (screengeom.screenh - menu->parent->y > menu->h)
a7732690 469 menu->y = menu->parent->y;
a80fee22 470 else if (screengeom.screenh > menu->h)
471 menu->y = screengeom.screenh - menu->h;
d2435fcd 472
473 XStringListToTextProperty(&(menu->caller->output), 1, &textprop);
a7732690 474 }
475
476 /* update menu geometry */
477 changes.height = menu->h;
f1583285 478 changes.width = menu->w;
a7732690 479 changes.x = menu->x;
480 changes.y = menu->y;
f1583285 481 XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
a7732690 482
d2435fcd 483 /* set window manager hints */
09c13122 484 sizeh.flags = PMaxSize | PMinSize;
485 sizeh.min_width = sizeh.max_width = menu->w;
486 sizeh.min_height = sizeh.max_height = menu->h;
d2435fcd 487 XSetWMProperties(dpy, menu->win, &textprop, NULL, NULL, 0, &sizeh,
488 NULL, &classh);
09c13122 489
dbeb9940 490 /* create pixmap and XftDraw */
f15fc339 491 menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
492 DefaultDepth(dpy, screen));
dbeb9940 493 menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
f15fc339 494
a80fee22 495 /* calculate positions of submenus */
a7732690 496 for (item = menu->list; item != NULL; item = item->next) {
497 if (item->submenu != NULL)
498 calcmenu(item->submenu);
499 }
500}
501
85003546 502/* try to grab pointer, we may have to wait for another process to ungrab */
503static void
504grabpointer(void)
505{
506 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
507 int i;
508
509 for (i = 0; i < 1000; i++) {
510 if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
511 GrabModeAsync, GrabModeAsync, None,
512 None, CurrentTime) == GrabSuccess)
513 return;
514 nanosleep(&ts, NULL);
515 }
516 errx(1, "cannot grab keyboard");
517}
518
519/* try to grab keyboard, we may have to wait for another process to ungrab */
520static void
521grabkeyboard(void)
522{
523 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
524 int i;
525
526 for (i = 0; i < 1000; i++) {
527 if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
528 GrabModeAsync, CurrentTime) == GrabSuccess)
529 return;
530 nanosleep(&ts, NULL);
531 }
532 errx(1, "cannot grab keyboard");
533}
534
a7732690 535/* get menu and item of given window and position */
536static void
a80fee22 537getmenuitem(Window win, int y,
a7732690 538 struct Menu **menu_ret, struct Item **item_ret)
539{
540 struct Menu *menu = NULL;
541 struct Item *item = NULL;
542
543 for (menu = currmenu; menu != NULL; menu = menu->parent) {
544 if (menu->win == win) {
545 for (item = menu->list; item != NULL; item = item->next) {
7fbd1c5e 546 if (y >= item->y && y <= item->y + item->h) {
a7732690 547 goto done;
548 }
549 }
550 }
551 }
552
553
554done:
555 *menu_ret = menu;
556 *item_ret = item;
557}
558
559/* set currentmenu to menu, umap previous menus and map current menu and its parents */
560static void
561setcurrmenu(struct Menu *currmenu_new)
562{
d8a7caf2 563 struct Menu *menu, *menu_;
d888f2ca 564 struct Item *item;
d8a7caf2 565 struct Menu *lcamenu; /* lowest common ancestor menu */
566 unsigned minlevel; /* level of the closest to root menu */
567 unsigned maxlevel; /* level of the closest to root menu */
a7732690 568
27443900 569 /* do not update currmenu to itself */
a7732690 570 if (currmenu_new == currmenu)
571 return;
572
27443900 573 /* if there was no currmenu, skip calculations */
574 if (currmenu == NULL) {
575 currmenu = currmenu_new;
576 return;
577 }
578
d8a7caf2 579 /* find lowest common ancestor menu */
580 lcamenu = rootmenu;
27443900 581 minlevel = MIN(currmenu_new->level, currmenu->level);
582 maxlevel = MAX(currmenu_new->level, currmenu->level);
583 if (currmenu_new->level == maxlevel) {
584 menu = currmenu_new;
585 menu_ = currmenu;
586 } else {
587 menu = currmenu;
588 menu_ = currmenu_new;
589 }
590 while (menu->level > minlevel)
591 menu = menu->parent;
592 while (menu != menu_) {
593 menu = menu->parent;
594 menu_ = menu_->parent;
d8a7caf2 595 }
27443900 596 lcamenu = menu;
d8a7caf2 597
873c080c 598 /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
d8a7caf2 599 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
8455c369 600 menu->selected = NULL;
a7732690 601 XUnmapWindow(dpy, menu->win);
602 }
603
604 currmenu = currmenu_new;
605
873c080c 606 /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
d888f2ca 607 item = NULL;
d8a7caf2 608 for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
a7732690 609 XMapWindow(dpy, menu->win);
d888f2ca 610 if (item != NULL)
611 menu->selected = item;
612 item = menu->caller;
613 }
a7732690 614}
615
616/* draw items of the current menu and of its ancestors */
617static void
618drawmenu(void)
619{
620 struct Menu *menu;
621 struct Item *item;
622
623 for (menu = currmenu; menu != NULL; menu = menu->parent) {
a7732690 624 for (item = menu->list; item != NULL; item = item->next) {
dbeb9940 625 XftColor *color;
a7732690 626 int labelx, labely;
a7732690 627
628 /* determine item color */
f644b8bc 629 if (item == menu->selected)
630 color = dc.selected;
a7732690 631 else
f644b8bc 632 color = dc.normal;
633
634 /* continue if item is a separator */
635 if (item->label == NULL)
636 continue;
a7732690 637
a7732690 638 /* draw item box */
dbeb9940 639 XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
d888f2ca 640 XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
641 menu->w, item->h);
f15fc339 642 XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
f1583285 643 menu->w, item->h);
7fbd1c5e 644
a7732690 645 /* draw item label */
dbeb9940 646 labelx = 0 + dc.font->height;
647 labely = item->y + dc.font->height + geom.itemb / 2;
648 XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
649 XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
650 labelx, labely, item->label,
651 item->labellen);
a7732690 652
653 /* draw triangle, if item contains a submenu */
654 if (item->submenu != NULL) {
dbeb9940 655 int trianglex = menu->w - dc.font->height + geom.itemb - 1;
2b6968f9 656 int triangley = item->y + (3 * item->h)/8 -1;
a7732690 657
658 XPoint triangle[] = {
659 {trianglex, triangley},
2b6968f9 660 {trianglex + item->h/8 + 1, item->y + item->h/2},
661 {trianglex, triangley + item->h/4 + 2},
a7732690 662 {trianglex, triangley}
663 };
664
f15fc339 665 XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
a7732690 666 Convex, CoordModeOrigin);
667 }
f15fc339 668
669 XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y,
670 menu->w, item->h, 0, item->y);
a7732690 671 }
672 }
673}
674
858338d9 675/* cycle through the items; non-zero direction is next, zero is prev */
676static struct Item *
677itemcycle(int direction)
678{
679 struct Item *item;
680 struct Item *lastitem;
681
682 item = NULL;
683
684 if (direction == ITEMNEXT) {
685 if (currmenu->selected == NULL)
686 item = currmenu->list;
687 else if (currmenu->selected->next != NULL)
688 item = currmenu->selected->next;
689
690 while (item != NULL && item->label == NULL)
691 item = item->next;
692
693 if (item == NULL)
694 item = currmenu->list;
695 } else {
696 for (lastitem = currmenu->list;
697 lastitem != NULL && lastitem->next != NULL;
698 lastitem = lastitem->next)
699 ;
700
701 if (currmenu->selected == NULL)
702 item = lastitem;
703 else if (currmenu->selected->prev != NULL)
704 item = currmenu->selected->prev;
705
706 while (item != NULL && item->label == NULL)
707 item = item->prev;
708
709 if (item == NULL)
710 item = lastitem;
711 }
712
713 return item;
714}
715
a7732690 716/* run event loop */
717static void
718run(void)
719{
720 struct Menu *menu;
721 struct Item *item;
722 struct Item *previtem = NULL;
858338d9 723 KeySym ksym;
a7732690 724 XEvent ev;
725
a7732690 726 while (!XNextEvent(dpy, &ev)) {
727 switch(ev.type) {
728 case Expose:
858338d9 729 if (ev.xexpose.count == 0)
730 drawmenu();
a7732690 731 break;
732 case MotionNotify:
a80fee22 733 getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
a7732690 734 if (menu != NULL && item != NULL) {
735 if (previtem != item) {
736 if (item->submenu != NULL)
737 setcurrmenu(item->submenu);
738 else
739 setcurrmenu(menu);
740 previtem = item;
d888f2ca 741 drawmenu();
742 } else if (menu->selected != item) {
a7732690 743 menu->selected = item;
d888f2ca 744 drawmenu();
745 }
a7732690 746 }
a7732690 747 break;
748 case ButtonRelease:
a80fee22 749 getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
a7732690 750 if (menu != NULL && item != NULL) {
858338d9 751selectitem:
7fbd1c5e 752 if (item->label == NULL)
753 break; /* ignore separators */
a7732690 754 if (item->submenu != NULL) {
755 setcurrmenu(item->submenu);
756 } else {
757 printf("%s\n", item->output);
08f16589 758 return;
a7732690 759 }
858338d9 760 currmenu->selected = currmenu->list;
a7732690 761 drawmenu();
858338d9 762 break;
a7732690 763 } else {
08f16589 764 return;
a7732690 765 }
858338d9 766 case ButtonPress:
767 getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
768 if (menu == NULL || item == NULL)
769 return;
770 break;
771 case KeyPress:
772 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
773
774 if (ksym == XK_Escape && currmenu == rootmenu)
775 return;
776
777 /* Shift-Tab = ISO_Left_Tab */
778 if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
779 ksym = XK_ISO_Left_Tab;
780
781 /* cycle through menu */
782 item = NULL;
783 if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
784 item = itemcycle(ITEMPREV);
785 } else if (ksym == XK_Tab || ksym == XK_Down) {
786 item = itemcycle(ITEMNEXT);
787 } else if ((ksym == XK_Return || ksym == XK_Right) &&
788 currmenu->selected != NULL) {
789 item = currmenu->selected;
790 goto selectitem;
791 } else if ((ksym == XK_Escape || ksym == XK_Left) &&
792 currmenu->parent != NULL) {
793 item = currmenu->parent->selected;
794 setcurrmenu(currmenu->parent);
795 } else
796 break;
797 currmenu->selected = item;
798 drawmenu();
a7732690 799 break;
f15fc339 800 case LeaveNotify:
801 currmenu->selected = NULL;
802 drawmenu();
803 break;
a7732690 804 }
805 }
806}
807
f15fc339 808/* recursivelly free a pixmap */
809static void
810freewindow(struct Menu *menu)
811{
812 struct Item *item;
813
814 for (item = menu->list; item != NULL; item = item->next)
815 if (item->submenu != NULL)
816 freewindow(item->submenu);
817
818 XFreePixmap(dpy, menu->pixmap);
dbeb9940 819 XftDrawDestroy(menu->draw);
f15fc339 820 XDestroyWindow(dpy, menu->win);
821}
822
a7732690 823/* cleanup and exit */
824static void
08f16589 825cleanup(void)
a7732690 826{
f15fc339 827 freewindow(rootmenu);
dbeb9940 828
829 XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
830 XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
831 XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
832 XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
833 XftColorFree(dpy, visual, colormap, &dc.decoration[ColorBG]);
834 XftColorFree(dpy, visual, colormap, &dc.decoration[ColorFG]);
835
f15fc339 836 XFreeGC(dpy, dc.gc);
a7732690 837 XCloseDisplay(dpy);
a7732690 838}
839
840/* show usage */
841static void
842usage(void)
843{
27443900 844 (void)fprintf(stderr, "usage: xmenu title...\n");
a7732690 845 exit(1);
846}