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