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