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