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