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