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