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