| 1 | #include <ctype.h> |
| 2 | #include <err.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <limits.h> |
| 8 | #include <time.h> |
| 9 | #include <unistd.h> |
| 10 | #include <X11/Xlib.h> |
| 11 | #include <X11/Xatom.h> |
| 12 | #include <X11/Xutil.h> |
| 13 | #include <X11/Xresource.h> |
| 14 | #include <X11/XKBlib.h> |
| 15 | #include <X11/Xft/Xft.h> |
| 16 | #include <X11/extensions/Xinerama.h> |
| 17 | #include <Imlib2.h> |
| 18 | #include "xmenu.h" |
| 19 | |
| 20 | /* |
| 21 | * Function declarations |
| 22 | */ |
| 23 | |
| 24 | /* argument parser */ |
| 25 | static void parseposition(char *optarg); |
| 26 | |
| 27 | /* initializers, and their helper routines */ |
| 28 | static void parsefonts(const char *s); |
| 29 | static void ealloccolor(const char *s, XftColor *color); |
| 30 | static void initmonitor(void); |
| 31 | static void initresources(void); |
| 32 | static void initdc(void); |
| 33 | static void initiconsize(void); |
| 34 | static void initatoms(void); |
| 35 | |
| 36 | /* structure builders, and their helper routines */ |
| 37 | static struct Item *allocitem(const char *label, const char *output, char *file); |
| 38 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
| 39 | static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file); |
| 40 | static struct Menu *parsestdin(void); |
| 41 | |
| 42 | /* text drawer, and its helper routine */ |
| 43 | static FcChar32 getnextutf8char(const char *s, const char **end_ret); |
| 44 | static XftFont *getfontucode(FcChar32 ucode); |
| 45 | static int drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text); |
| 46 | |
| 47 | /* structure setters, and their helper routines */ |
| 48 | static void setupitems(struct Menu *menu); |
| 49 | static void setupmenupos(struct Menu *menu); |
| 50 | static void setupmenu(struct Menu *menu, XClassHint *classh); |
| 51 | |
| 52 | /* grabbers */ |
| 53 | static void grabpointer(void); |
| 54 | static void grabkeyboard(void); |
| 55 | |
| 56 | /* item drawer, and its helper routine */ |
| 57 | static Imlib_Image loadicon(const char *file); |
| 58 | static void drawitems(struct Menu *menu); |
| 59 | |
| 60 | /* menu drawers and mappers */ |
| 61 | static void drawmenus(struct Menu *currmenu); |
| 62 | static void mapmenu(struct Menu *currmenu); |
| 63 | |
| 64 | /* getters */ |
| 65 | static struct Menu *getmenu(struct Menu *currmenu, Window win); |
| 66 | static struct Item *getitem(struct Menu *menu, int y); |
| 67 | |
| 68 | /* cycle through items */ |
| 69 | static struct Item *itemcycle(struct Menu *currmenu, int direction); |
| 70 | |
| 71 | /* main event loop */ |
| 72 | static void run(struct Menu *currmenu); |
| 73 | |
| 74 | /* cleaners */ |
| 75 | static void cleanmenu(struct Menu *menu); |
| 76 | static void cleanup(void); |
| 77 | |
| 78 | /* show usage */ |
| 79 | static void usage(void); |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Variable declarations |
| 84 | */ |
| 85 | |
| 86 | /* X stuff */ |
| 87 | static Display *dpy; |
| 88 | static int screen; |
| 89 | static Visual *visual; |
| 90 | static Window rootwin; |
| 91 | static Colormap colormap; |
| 92 | static struct DC dc; |
| 93 | static struct Monitor mon; |
| 94 | static Atom utf8string; |
| 95 | static Atom wmdelete; |
| 96 | static Atom netatom[NetLast]; |
| 97 | |
| 98 | /* flags */ |
| 99 | static int iflag = 0; /* whether to disable icons */ |
| 100 | static int mflag = 0; /* whether the user specified a monitor with -p */ |
| 101 | static int pflag = 0; /* whether the user specified a position with -p */ |
| 102 | static int wflag = 0; /* whether to let the window manager control XMenu */ |
| 103 | |
| 104 | /* include config variable */ |
| 105 | #include "config.h" |
| 106 | |
| 107 | |
| 108 | /* |
| 109 | * Function implementations |
| 110 | */ |
| 111 | |
| 112 | /* xmenu: generate menu from stdin and print selected entry to stdout */ |
| 113 | int |
| 114 | main(int argc, char *argv[]) |
| 115 | { |
| 116 | struct Menu *rootmenu; |
| 117 | XClassHint classh; |
| 118 | int ch; |
| 119 | |
| 120 | while ((ch = getopt(argc, argv, "ip:w")) != -1) { |
| 121 | switch (ch) { |
| 122 | case 'i': |
| 123 | iflag = 1; |
| 124 | break; |
| 125 | case 'p': |
| 126 | pflag = 1; |
| 127 | parseposition(optarg); |
| 128 | break; |
| 129 | case 'w': |
| 130 | wflag = 1; |
| 131 | break; |
| 132 | default: |
| 133 | usage(); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | argc -= optind; |
| 138 | argv += optind; |
| 139 | |
| 140 | if (argc > 1) |
| 141 | usage(); |
| 142 | |
| 143 | /* open connection to server and set X variables */ |
| 144 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
| 145 | errx(1, "could not open display"); |
| 146 | screen = DefaultScreen(dpy); |
| 147 | visual = DefaultVisual(dpy, screen); |
| 148 | rootwin = RootWindow(dpy, screen); |
| 149 | colormap = DefaultColormap(dpy, screen); |
| 150 | |
| 151 | /* imlib2 stuff */ |
| 152 | if (!iflag) { |
| 153 | imlib_set_cache_size(2048 * 1024); |
| 154 | imlib_context_set_dither(1); |
| 155 | imlib_context_set_display(dpy); |
| 156 | imlib_context_set_visual(visual); |
| 157 | imlib_context_set_colormap(colormap); |
| 158 | } |
| 159 | |
| 160 | /* initializers */ |
| 161 | initmonitor(); |
| 162 | initresources(); |
| 163 | initdc(); |
| 164 | initiconsize(); |
| 165 | initatoms(); |
| 166 | |
| 167 | /* set window class */ |
| 168 | classh.res_class = PROGNAME; |
| 169 | if (argc == 1) |
| 170 | classh.res_name = *argv; |
| 171 | else |
| 172 | classh.res_name = PROGNAME; |
| 173 | |
| 174 | /* generate menus and set them up */ |
| 175 | rootmenu = parsestdin(); |
| 176 | if (rootmenu == NULL) |
| 177 | errx(1, "no menu generated"); |
| 178 | setupmenu(rootmenu, &classh); |
| 179 | |
| 180 | /* grab mouse and keyboard */ |
| 181 | if (!wflag) { |
| 182 | grabpointer(); |
| 183 | grabkeyboard(); |
| 184 | } |
| 185 | |
| 186 | /* run event loop */ |
| 187 | run(rootmenu); |
| 188 | |
| 189 | /* freeing stuff */ |
| 190 | cleanmenu(rootmenu); |
| 191 | cleanup(); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* parse position string from -p, |
| 197 | * put results on config.posx, config.posy, and config.monitor */ |
| 198 | static void |
| 199 | parseposition(char *optarg) |
| 200 | { |
| 201 | long n; |
| 202 | char *s = optarg; |
| 203 | char *endp; |
| 204 | |
| 205 | n = strtol(s, &endp, 10); |
| 206 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x') |
| 207 | goto error; |
| 208 | config.posx = n; |
| 209 | s = endp+1; |
| 210 | n = strtol(s, &endp, 10); |
| 211 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s) |
| 212 | goto error; |
| 213 | config.posy = n; |
| 214 | if (*endp == ':') { |
| 215 | s = endp+1; |
| 216 | mflag = 1; |
| 217 | if (strncasecmp(s, "CUR", 3) == 0) { |
| 218 | config.monitor = -1; |
| 219 | endp = s+3; |
| 220 | } else { |
| 221 | n = strtol(s, &endp, 10); |
| 222 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0') |
| 223 | goto error; |
| 224 | config.monitor = n; |
| 225 | } |
| 226 | } else if (*endp != '\0') { |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | return; |
| 231 | |
| 232 | error: |
| 233 | errx(1, "improper position: %s", optarg); |
| 234 | } |
| 235 | |
| 236 | /* parse color string */ |
| 237 | static void |
| 238 | parsefonts(const char *s) |
| 239 | { |
| 240 | const char *p; |
| 241 | char buf[1024]; |
| 242 | size_t nfont = 0; |
| 243 | |
| 244 | dc.nfonts = 1; |
| 245 | for (p = s; *p; p++) |
| 246 | if (*p == ',') |
| 247 | dc.nfonts++; |
| 248 | |
| 249 | if ((dc.fonts = calloc(dc.nfonts, sizeof *dc.fonts)) == NULL) |
| 250 | err(1, "calloc"); |
| 251 | |
| 252 | p = s; |
| 253 | while (*p != '\0') { |
| 254 | size_t i; |
| 255 | |
| 256 | i = 0; |
| 257 | while (isspace(*p)) |
| 258 | p++; |
| 259 | while (i < sizeof buf && *p != '\0' && *p != ',') |
| 260 | buf[i++] = *p++; |
| 261 | if (i >= sizeof buf) |
| 262 | errx(1, "font name too long"); |
| 263 | if (*p == ',') |
| 264 | p++; |
| 265 | buf[i] = '\0'; |
| 266 | if (nfont == 0) |
| 267 | if ((dc.pattern = FcNameParse((FcChar8 *)buf)) == NULL) |
| 268 | errx(1, "the first font in the cache must be loaded from a font string"); |
| 269 | if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL) |
| 270 | errx(1, "could not load font"); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* get color from color string */ |
| 275 | static void |
| 276 | ealloccolor(const char *s, XftColor *color) |
| 277 | { |
| 278 | if(!XftColorAllocName(dpy, visual, colormap, s, color)) |
| 279 | errx(1, "could not allocate color: %s", s); |
| 280 | } |
| 281 | |
| 282 | /* query monitor information and cursor position */ |
| 283 | static void |
| 284 | initmonitor(void) |
| 285 | { |
| 286 | XineramaScreenInfo *info = NULL; |
| 287 | Window dw; /* dummy variable */ |
| 288 | int di; /* dummy variable */ |
| 289 | unsigned du; /* dummy variable */ |
| 290 | int cursx, cursy; /* cursor position */ |
| 291 | int nmons; |
| 292 | int i; |
| 293 | |
| 294 | XQueryPointer(dpy, rootwin, &dw, &dw, &cursx, &cursy, &di, &di, &du); |
| 295 | |
| 296 | mon.x = mon.y = 0; |
| 297 | mon.w = DisplayWidth(dpy, screen); |
| 298 | mon.h = DisplayHeight(dpy, screen); |
| 299 | |
| 300 | if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) { |
| 301 | int selmon = 0; |
| 302 | |
| 303 | if (!mflag || config.monitor < 0 || config.monitor >= nmons) { |
| 304 | for (i = 0; i < nmons; i++) { |
| 305 | if (BETWEEN(cursx, info[i].x_org, info[i].x_org + info[i].width) && |
| 306 | BETWEEN(cursy, info[i].y_org, info[i].y_org + info[i].height)) { |
| 307 | selmon = i; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } else { |
| 312 | selmon = config.monitor; |
| 313 | } |
| 314 | |
| 315 | mon.x = info[selmon].x_org; |
| 316 | mon.y = info[selmon].y_org; |
| 317 | mon.w = info[selmon].width; |
| 318 | mon.h = info[selmon].height; |
| 319 | |
| 320 | XFree(info); |
| 321 | } |
| 322 | |
| 323 | if (!pflag) { |
| 324 | config.posx = cursx; |
| 325 | config.posy = cursy; |
| 326 | } else if (mflag) { |
| 327 | config.posx += mon.x; |
| 328 | config.posy += mon.y; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* read xrdb for configuration options */ |
| 333 | static void |
| 334 | initresources(void) |
| 335 | { |
| 336 | long n; |
| 337 | char *type; |
| 338 | char *xrm; |
| 339 | XrmDatabase xdb; |
| 340 | XrmValue xval; |
| 341 | |
| 342 | XrmInitialize(); |
| 343 | if ((xrm = XResourceManagerString(dpy)) == NULL) |
| 344 | return; |
| 345 | |
| 346 | xdb = XrmGetStringDatabase(xrm); |
| 347 | |
| 348 | if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True) |
| 349 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
| 350 | config.border_pixels = n; |
| 351 | if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True) |
| 352 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
| 353 | config.separator_pixels = n; |
| 354 | if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True) |
| 355 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
| 356 | config.height_pixels = n; |
| 357 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
| 358 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
| 359 | config.width_pixels = n; |
| 360 | if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True) |
| 361 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
| 362 | config.gap_pixels = n; |
| 363 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
| 364 | config.background_color = strdup(xval.addr); |
| 365 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
| 366 | config.foreground_color = strdup(xval.addr); |
| 367 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
| 368 | config.selbackground_color = strdup(xval.addr); |
| 369 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
| 370 | config.selforeground_color = strdup(xval.addr); |
| 371 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
| 372 | config.separator_color = strdup(xval.addr); |
| 373 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
| 374 | config.border_color = strdup(xval.addr); |
| 375 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
| 376 | config.font = strdup(xval.addr); |
| 377 | |
| 378 | XrmDestroyDatabase(xdb); |
| 379 | } |
| 380 | |
| 381 | /* init draw context */ |
| 382 | static void |
| 383 | initdc(void) |
| 384 | { |
| 385 | /* get color pixels */ |
| 386 | ealloccolor(config.background_color, &dc.normal[ColorBG]); |
| 387 | ealloccolor(config.foreground_color, &dc.normal[ColorFG]); |
| 388 | ealloccolor(config.selbackground_color, &dc.selected[ColorBG]); |
| 389 | ealloccolor(config.selforeground_color, &dc.selected[ColorFG]); |
| 390 | ealloccolor(config.separator_color, &dc.separator); |
| 391 | ealloccolor(config.border_color, &dc.border); |
| 392 | |
| 393 | /* parse fonts */ |
| 394 | parsefonts(config.font); |
| 395 | |
| 396 | /* create common GC */ |
| 397 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
| 398 | } |
| 399 | |
| 400 | /* calculate icon size */ |
| 401 | static void |
| 402 | initiconsize(void) |
| 403 | { |
| 404 | config.iconsize = config.height_pixels - config.iconpadding * 2; |
| 405 | } |
| 406 | |
| 407 | /* intern atoms */ |
| 408 | static void |
| 409 | initatoms(void) |
| 410 | { |
| 411 | utf8string = XInternAtom(dpy, "UTF8_STRING", False); |
| 412 | wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); |
| 413 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); |
| 414 | netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); |
| 415 | netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False); |
| 416 | } |
| 417 | |
| 418 | /* allocate an item */ |
| 419 | static struct Item * |
| 420 | allocitem(const char *label, const char *output, char *file) |
| 421 | { |
| 422 | struct Item *item; |
| 423 | |
| 424 | if ((item = malloc(sizeof *item)) == NULL) |
| 425 | err(1, "malloc"); |
| 426 | if (label == NULL) { |
| 427 | item->label = NULL; |
| 428 | item->output = NULL; |
| 429 | } else { |
| 430 | if ((item->label = strdup(label)) == NULL) |
| 431 | err(1, "strdup"); |
| 432 | if (label == output) { |
| 433 | item->output = item->label; |
| 434 | } else { |
| 435 | if ((item->output = strdup(output)) == NULL) |
| 436 | err(1, "strdup"); |
| 437 | } |
| 438 | } |
| 439 | if (file == NULL) { |
| 440 | item->file = NULL; |
| 441 | } else { |
| 442 | if ((item->file = strdup(file)) == NULL) |
| 443 | err(1, "strdup"); |
| 444 | } |
| 445 | item->y = 0; |
| 446 | item->h = 0; |
| 447 | item->next = NULL; |
| 448 | item->submenu = NULL; |
| 449 | item->icon = NULL; |
| 450 | |
| 451 | return item; |
| 452 | } |
| 453 | |
| 454 | /* allocate a menu and create its window */ |
| 455 | static struct Menu * |
| 456 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
| 457 | { |
| 458 | XSetWindowAttributes swa; |
| 459 | struct Menu *menu; |
| 460 | |
| 461 | if ((menu = malloc(sizeof *menu)) == NULL) |
| 462 | err(1, "malloc"); |
| 463 | menu->parent = parent; |
| 464 | menu->list = list; |
| 465 | menu->caller = NULL; |
| 466 | menu->selected = NULL; |
| 467 | menu->w = 0; /* recalculated by setupmenu() */ |
| 468 | menu->h = 0; /* recalculated by setupmenu() */ |
| 469 | menu->x = mon.x; /* recalculated by setupmenu() */ |
| 470 | menu->y = mon.y; /* recalculated by setupmenu() */ |
| 471 | menu->level = level; |
| 472 | menu->drawn = 0; |
| 473 | menu->hasicon = 0; |
| 474 | |
| 475 | swa.override_redirect = (wflag) ? False : True; |
| 476 | swa.background_pixel = dc.normal[ColorBG].pixel; |
| 477 | swa.border_pixel = dc.border.pixel; |
| 478 | swa.save_under = True; /* pop-up windows should save_under*/ |
| 479 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
| 480 | | PointerMotionMask | LeaveWindowMask; |
| 481 | if (wflag) |
| 482 | swa.event_mask |= StructureNotifyMask; |
| 483 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0, |
| 484 | CopyFromParent, CopyFromParent, CopyFromParent, |
| 485 | CWOverrideRedirect | CWBackPixel | |
| 486 | CWBorderPixel | CWEventMask | CWSaveUnder, |
| 487 | &swa); |
| 488 | |
| 489 | return menu; |
| 490 | } |
| 491 | |
| 492 | /* build the menu tree */ |
| 493 | static struct Menu * |
| 494 | buildmenutree(unsigned level, const char *label, const char *output, char *file) |
| 495 | { |
| 496 | static struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
| 497 | static struct Menu *rootmenu = NULL; /* menu to be returned */ |
| 498 | struct Item *curritem = NULL; /* item currently being read */ |
| 499 | struct Item *item; /* dummy item for loops */ |
| 500 | struct Menu *menu; /* dummy menu for loops */ |
| 501 | unsigned i; |
| 502 | |
| 503 | /* create the item */ |
| 504 | curritem = allocitem(label, output, file); |
| 505 | |
| 506 | /* put the item in the menu tree */ |
| 507 | if (prevmenu == NULL) { /* there is no menu yet */ |
| 508 | menu = allocmenu(NULL, curritem, level); |
| 509 | rootmenu = menu; |
| 510 | prevmenu = menu; |
| 511 | curritem->prev = NULL; |
| 512 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu */ |
| 513 | /* go up the menu tree until find the menu this item continues */ |
| 514 | for (menu = prevmenu, i = level; |
| 515 | menu != NULL && i != prevmenu->level; |
| 516 | menu = menu->parent, i++) |
| 517 | ; |
| 518 | if (menu == NULL) |
| 519 | errx(1, "improper indentation detected"); |
| 520 | |
| 521 | /* find last item in the new menu */ |
| 522 | for (item = menu->list; item->next != NULL; item = item->next) |
| 523 | ; |
| 524 | |
| 525 | prevmenu = menu; |
| 526 | item->next = curritem; |
| 527 | curritem->prev = item; |
| 528 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
| 529 | /* find last item in the previous menu */ |
| 530 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
| 531 | ; |
| 532 | |
| 533 | item->next = curritem; |
| 534 | curritem->prev = item; |
| 535 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
| 536 | menu = allocmenu(prevmenu, curritem, level); |
| 537 | |
| 538 | /* find last item in the previous menu */ |
| 539 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
| 540 | ; |
| 541 | |
| 542 | prevmenu = menu; |
| 543 | menu->caller = item; |
| 544 | item->submenu = menu; |
| 545 | curritem->prev = NULL; |
| 546 | } |
| 547 | |
| 548 | if (curritem->file) |
| 549 | prevmenu->hasicon = 1; |
| 550 | |
| 551 | return rootmenu; |
| 552 | } |
| 553 | |
| 554 | /* create menus and items from the stdin */ |
| 555 | static struct Menu * |
| 556 | parsestdin(void) |
| 557 | { |
| 558 | struct Menu *rootmenu; |
| 559 | char *s, buf[BUFSIZ]; |
| 560 | char *file, *label, *output; |
| 561 | unsigned level = 0; |
| 562 | |
| 563 | rootmenu = NULL; |
| 564 | |
| 565 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
| 566 | /* get the indentation level */ |
| 567 | level = strspn(buf, "\t"); |
| 568 | |
| 569 | /* get the label */ |
| 570 | s = level + buf; |
| 571 | label = strtok(s, "\t\n"); |
| 572 | |
| 573 | /* get the filename */ |
| 574 | file = NULL; |
| 575 | if (label != NULL && strncmp(label, "IMG:", 4) == 0) { |
| 576 | file = label + 4; |
| 577 | label = strtok(NULL, "\t\n"); |
| 578 | } |
| 579 | |
| 580 | /* get the output */ |
| 581 | output = strtok(NULL, "\n"); |
| 582 | if (output == NULL) { |
| 583 | output = label; |
| 584 | } else { |
| 585 | while (*output == '\t') |
| 586 | output++; |
| 587 | } |
| 588 | |
| 589 | rootmenu = buildmenutree(level, label, output, file); |
| 590 | } |
| 591 | |
| 592 | return rootmenu; |
| 593 | } |
| 594 | |
| 595 | /* get next utf8 char from s return its codepoint and set next_ret to pointer to end of character */ |
| 596 | static FcChar32 |
| 597 | getnextutf8char(const char *s, const char **next_ret) |
| 598 | { |
| 599 | static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0}; |
| 600 | static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; |
| 601 | static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000}; |
| 602 | static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; |
| 603 | /* 0xFFFD is the replacement character, used to represent unknown characters */ |
| 604 | static const FcChar32 unknown = 0xFFFD; |
| 605 | FcChar32 ucode; /* FcChar32 type holds 32 bits */ |
| 606 | size_t usize = 0; /* n' of bytes of the utf8 character */ |
| 607 | size_t i; |
| 608 | |
| 609 | *next_ret = s+1; |
| 610 | |
| 611 | /* get code of first byte of utf8 character */ |
| 612 | for (i = 0; i < sizeof utfmask; i++) { |
| 613 | if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) { |
| 614 | usize = i; |
| 615 | ucode = (unsigned char)*s & ~utfmask[i]; |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /* if first byte is a continuation byte or is not allowed, return unknown */ |
| 621 | if (i == sizeof utfmask || usize == 0) |
| 622 | return unknown; |
| 623 | |
| 624 | /* check the other usize-1 bytes */ |
| 625 | s++; |
| 626 | for (i = 1; i < usize; i++) { |
| 627 | *next_ret = s+1; |
| 628 | /* if byte is nul or is not a continuation byte, return unknown */ |
| 629 | if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0]) |
| 630 | return unknown; |
| 631 | /* 6 is the number of relevant bits in the continuation byte */ |
| 632 | ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]); |
| 633 | s++; |
| 634 | } |
| 635 | |
| 636 | /* check if ucode is invalid or in utf-16 surrogate halves */ |
| 637 | if (!BETWEEN(ucode, utfmin[usize], utfmax[usize]) |
| 638 | || BETWEEN (ucode, 0xD800, 0xDFFF)) |
| 639 | return unknown; |
| 640 | |
| 641 | return ucode; |
| 642 | } |
| 643 | |
| 644 | /* get which font contains a given code point */ |
| 645 | static XftFont * |
| 646 | getfontucode(FcChar32 ucode) |
| 647 | { |
| 648 | FcCharSet *fccharset = NULL; |
| 649 | FcPattern *fcpattern = NULL; |
| 650 | FcPattern *match = NULL; |
| 651 | XftFont *retfont = NULL; |
| 652 | XftResult result; |
| 653 | size_t i; |
| 654 | |
| 655 | for (i = 0; i < dc.nfonts; i++) |
| 656 | if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue) |
| 657 | return dc.fonts[i]; |
| 658 | |
| 659 | /* create a charset containing our code point */ |
| 660 | fccharset = FcCharSetCreate(); |
| 661 | FcCharSetAddChar(fccharset, ucode); |
| 662 | |
| 663 | /* create a pattern akin to the dc.pattern but containing our charset */ |
| 664 | if (fccharset) { |
| 665 | fcpattern = FcPatternDuplicate(dc.pattern); |
| 666 | FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); |
| 667 | } |
| 668 | |
| 669 | /* find pattern matching fcpattern */ |
| 670 | if (fcpattern) { |
| 671 | FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); |
| 672 | FcDefaultSubstitute(fcpattern); |
| 673 | match = XftFontMatch(dpy, screen, fcpattern, &result); |
| 674 | } |
| 675 | |
| 676 | /* if found a pattern, open its font */ |
| 677 | if (match) { |
| 678 | retfont = XftFontOpenPattern(dpy, match); |
| 679 | if (retfont && XftCharExists(dpy, retfont, ucode) == FcTrue) { |
| 680 | if ((dc.fonts = realloc(dc.fonts, dc.nfonts+1)) == NULL) |
| 681 | err(1, "realloc"); |
| 682 | dc.fonts[dc.nfonts] = retfont; |
| 683 | return dc.fonts[dc.nfonts++]; |
| 684 | } else { |
| 685 | XftFontClose(dpy, retfont); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* in case no fount was found, return the first one */ |
| 690 | return dc.fonts[0]; |
| 691 | } |
| 692 | |
| 693 | /* draw text into XftDraw, return width of text glyphs */ |
| 694 | static int |
| 695 | drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text) |
| 696 | { |
| 697 | int textwidth = 0; |
| 698 | |
| 699 | while (*text) { |
| 700 | XftFont *currfont; |
| 701 | XGlyphInfo ext; |
| 702 | FcChar32 ucode; |
| 703 | const char *next; |
| 704 | size_t len; |
| 705 | |
| 706 | ucode = getnextutf8char(text, &next); |
| 707 | currfont = getfontucode(ucode); |
| 708 | |
| 709 | len = next - text; |
| 710 | XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext); |
| 711 | textwidth += ext.xOff; |
| 712 | |
| 713 | if (draw) { |
| 714 | int texty; |
| 715 | |
| 716 | texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent; |
| 717 | XftDrawStringUtf8(draw, color, currfont, x, texty, (XftChar8 *)text, len); |
| 718 | x += ext.xOff; |
| 719 | } |
| 720 | |
| 721 | text = next; |
| 722 | } |
| 723 | |
| 724 | return textwidth; |
| 725 | } |
| 726 | |
| 727 | /* setup the height, width and icon of the items of a menu */ |
| 728 | static void |
| 729 | setupitems(struct Menu *menu) |
| 730 | { |
| 731 | struct Item *item; |
| 732 | |
| 733 | menu->w = config.width_pixels; |
| 734 | for (item = menu->list; item != NULL; item = item->next) { |
| 735 | int itemwidth; |
| 736 | int textwidth; |
| 737 | |
| 738 | item->y = menu->h; |
| 739 | |
| 740 | if (item->label == NULL) /* height for separator item */ |
| 741 | item->h = config.separator_pixels; |
| 742 | else |
| 743 | item->h = config.height_pixels; |
| 744 | menu->h += item->h; |
| 745 | |
| 746 | if (item->label) |
| 747 | textwidth = drawtext(NULL, NULL, 0, 0, 0, item->label); |
| 748 | else |
| 749 | textwidth = 0; |
| 750 | |
| 751 | /* |
| 752 | * set menu width |
| 753 | * |
| 754 | * the item width depends on the size of its label (textwidth), |
| 755 | * and it is only used to calculate the width of the menu (which |
| 756 | * is equal to the width of the largest item). |
| 757 | * |
| 758 | * the horizontal padding appears 4 times through the width of a |
| 759 | * item: before and after its icon, and before and after its triangle. |
| 760 | * if the iflag is set (icons are disabled) then the horizontal |
| 761 | * padding appears 3 times: before the label and around the triangle. |
| 762 | */ |
| 763 | itemwidth = textwidth + config.triangle_width + config.horzpadding * 3; |
| 764 | itemwidth += (iflag || !menu->hasicon) ? 0 : config.iconsize + config.horzpadding; |
| 765 | menu->w = MAX(menu->w, itemwidth); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* setup the position of a menu */ |
| 770 | static void |
| 771 | setupmenupos(struct Menu *menu) |
| 772 | { |
| 773 | int width, height; |
| 774 | |
| 775 | width = menu->w + config.border_pixels * 2; |
| 776 | height = menu->h + config.border_pixels * 2; |
| 777 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
| 778 | if (pflag || (config.posx > mon.x && mon.x + mon.w - config.posx >= width)) |
| 779 | menu->x = config.posx; |
| 780 | else if (config.posx > width) |
| 781 | menu->x = config.posx - width; |
| 782 | |
| 783 | if (pflag || (config.posy > mon.y && mon.y + mon.h - config.posy >= height)) |
| 784 | menu->y = config.posy; |
| 785 | else if (mon.y + mon.h > height) |
| 786 | menu->y = mon.y + mon.h - height; |
| 787 | } else { /* else, calculate in respect to parent menu */ |
| 788 | int parentwidth; |
| 789 | |
| 790 | parentwidth = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels; |
| 791 | |
| 792 | if (mon.x + mon.w - parentwidth >= width) |
| 793 | menu->x = parentwidth; |
| 794 | else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels) |
| 795 | menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels; |
| 796 | |
| 797 | if (mon.y + mon.h - (menu->caller->y + menu->parent->y) >= height) |
| 798 | menu->y = menu->caller->y + menu->parent->y; |
| 799 | else if (mon.y + mon.h > height) |
| 800 | menu->y = mon.y + mon.h - height; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* recursivelly setup menu configuration and its pixmap */ |
| 805 | static void |
| 806 | setupmenu(struct Menu *menu, XClassHint *classh) |
| 807 | { |
| 808 | char *title; |
| 809 | struct Item *item; |
| 810 | XWindowChanges changes; |
| 811 | XSizeHints sizeh; |
| 812 | XTextProperty wintitle; |
| 813 | |
| 814 | /* setup size and position of menus */ |
| 815 | setupitems(menu); |
| 816 | setupmenupos(menu); |
| 817 | |
| 818 | /* update menu geometry */ |
| 819 | changes.border_width = config.border_pixels; |
| 820 | changes.height = menu->h; |
| 821 | changes.width = menu->w; |
| 822 | changes.x = menu->x; |
| 823 | changes.y = menu->y; |
| 824 | XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes); |
| 825 | |
| 826 | /* set window title (used if wflag is on) */ |
| 827 | if (menu->parent == NULL) { |
| 828 | title = classh->res_name; |
| 829 | } else { |
| 830 | title = menu->caller->output; |
| 831 | } |
| 832 | XStringListToTextProperty(&title, 1, &wintitle); |
| 833 | |
| 834 | /* set window manager hints */ |
| 835 | sizeh.flags = USPosition | PMaxSize | PMinSize; |
| 836 | sizeh.min_width = sizeh.max_width = menu->w; |
| 837 | sizeh.min_height = sizeh.max_height = menu->h; |
| 838 | XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh); |
| 839 | |
| 840 | /* set WM protocols and ewmh window properties */ |
| 841 | XSetWMProtocols(dpy, menu->win, &wmdelete, 1); |
| 842 | XChangeProperty(dpy, menu->win, netatom[NetWMName], utf8string, 8, |
| 843 | PropModeReplace, (unsigned char *)title, strlen(title)); |
| 844 | XChangeProperty(dpy, menu->win, netatom[NetWMWindowType], XA_ATOM, 32, |
| 845 | PropModeReplace, |
| 846 | (unsigned char *)&netatom[NetWMWindowTypePopupMenu], 1); |
| 847 | |
| 848 | /* calculate positions of submenus */ |
| 849 | for (item = menu->list; item != NULL; item = item->next) { |
| 850 | if (item->submenu != NULL) |
| 851 | setupmenu(item->submenu, classh); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* try to grab pointer, we may have to wait for another process to ungrab */ |
| 856 | static void |
| 857 | grabpointer(void) |
| 858 | { |
| 859 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
| 860 | int i; |
| 861 | |
| 862 | for (i = 0; i < 1000; i++) { |
| 863 | if (XGrabPointer(dpy, rootwin, True, ButtonPressMask, |
| 864 | GrabModeAsync, GrabModeAsync, None, |
| 865 | None, CurrentTime) == GrabSuccess) |
| 866 | return; |
| 867 | nanosleep(&ts, NULL); |
| 868 | } |
| 869 | errx(1, "could not grab pointer"); |
| 870 | } |
| 871 | |
| 872 | /* try to grab keyboard, we may have to wait for another process to ungrab */ |
| 873 | static void |
| 874 | grabkeyboard(void) |
| 875 | { |
| 876 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
| 877 | int i; |
| 878 | |
| 879 | for (i = 0; i < 1000; i++) { |
| 880 | if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync, |
| 881 | GrabModeAsync, CurrentTime) == GrabSuccess) |
| 882 | return; |
| 883 | nanosleep(&ts, NULL); |
| 884 | } |
| 885 | errx(1, "could not grab keyboard"); |
| 886 | } |
| 887 | |
| 888 | /* load and scale icon */ |
| 889 | static Imlib_Image |
| 890 | loadicon(const char *file) |
| 891 | { |
| 892 | Imlib_Image icon; |
| 893 | Imlib_Load_Error errcode; |
| 894 | const char *errstr; |
| 895 | int width; |
| 896 | int height; |
| 897 | int imgsize; |
| 898 | |
| 899 | icon = imlib_load_image_with_error_return(file, &errcode); |
| 900 | if (*file == '\0') { |
| 901 | warnx("could not load icon (file name is blank)"); |
| 902 | return NULL; |
| 903 | } else if (icon == NULL) { |
| 904 | switch (errcode) { |
| 905 | case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: |
| 906 | errstr = "file does not exist"; |
| 907 | break; |
| 908 | case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: |
| 909 | errstr = "file is directory"; |
| 910 | break; |
| 911 | case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: |
| 912 | case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: |
| 913 | errstr = "permission denied"; |
| 914 | break; |
| 915 | case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: |
| 916 | errstr = "unknown file format"; |
| 917 | break; |
| 918 | case IMLIB_LOAD_ERROR_PATH_TOO_LONG: |
| 919 | errstr = "path too long"; |
| 920 | break; |
| 921 | case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: |
| 922 | case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: |
| 923 | case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: |
| 924 | errstr = "improper path"; |
| 925 | break; |
| 926 | case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: |
| 927 | errstr = "too many symbolic links"; |
| 928 | break; |
| 929 | case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: |
| 930 | errstr = "out of memory"; |
| 931 | break; |
| 932 | case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: |
| 933 | errstr = "out of file descriptors"; |
| 934 | break; |
| 935 | default: |
| 936 | errstr = "unknown error"; |
| 937 | break; |
| 938 | } |
| 939 | warnx("could not load icon (%s): %s", errstr, file); |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | imlib_context_set_image(icon); |
| 944 | |
| 945 | width = imlib_image_get_width(); |
| 946 | height = imlib_image_get_height(); |
| 947 | imgsize = MIN(width, height); |
| 948 | |
| 949 | icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize, |
| 950 | config.iconsize, |
| 951 | config.iconsize); |
| 952 | |
| 953 | return icon; |
| 954 | } |
| 955 | |
| 956 | /* draw pixmap for the selected and unselected version of each item on menu */ |
| 957 | static void |
| 958 | drawitems(struct Menu *menu) |
| 959 | { |
| 960 | struct Item *item; |
| 961 | |
| 962 | for (item = menu->list; item != NULL; item = item->next) { |
| 963 | XftDraw *dsel, *dunsel; |
| 964 | int x, y; |
| 965 | |
| 966 | item->unsel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
| 967 | DefaultDepth(dpy, screen)); |
| 968 | |
| 969 | XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel); |
| 970 | XFillRectangle(dpy, item->unsel, dc.gc, 0, 0, menu->w, item->h); |
| 971 | |
| 972 | if (item->label == NULL) { /* item is separator */ |
| 973 | y = item->h/2; |
| 974 | XSetForeground(dpy, dc.gc, dc.separator.pixel); |
| 975 | XDrawLine(dpy, item->unsel, dc.gc, config.horzpadding, y, |
| 976 | menu->w - config.horzpadding, y); |
| 977 | |
| 978 | item->sel = item->unsel; |
| 979 | } else { |
| 980 | |
| 981 | item->sel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
| 982 | DefaultDepth(dpy, screen)); |
| 983 | XSetForeground(dpy, dc.gc, dc.selected[ColorBG].pixel); |
| 984 | XFillRectangle(dpy, item->sel, dc.gc, 0, 0, menu->w, item->h); |
| 985 | |
| 986 | /* draw text */ |
| 987 | x = config.horzpadding; |
| 988 | x += (iflag || !menu->hasicon) ? 0 : config.horzpadding + config.iconsize; |
| 989 | dsel = XftDrawCreate(dpy, item->sel, visual, colormap); |
| 990 | dunsel = XftDrawCreate(dpy, item->unsel, visual, colormap); |
| 991 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
| 992 | drawtext(dsel, &dc.selected[ColorFG], x, 0, item->h, item->label); |
| 993 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
| 994 | drawtext(dunsel, &dc.normal[ColorFG], x, 0, item->h, item->label); |
| 995 | XftDrawDestroy(dsel); |
| 996 | XftDrawDestroy(dunsel); |
| 997 | |
| 998 | /* draw triangle */ |
| 999 | if (item->submenu != NULL) { |
| 1000 | x = menu->w - config.triangle_width - config.horzpadding; |
| 1001 | y = (item->h - config.triangle_height + 1) / 2; |
| 1002 | |
| 1003 | XPoint triangle[] = { |
| 1004 | {x, y}, |
| 1005 | {x + config.triangle_width, y + config.triangle_height/2}, |
| 1006 | {x, y + config.triangle_height}, |
| 1007 | {x, y} |
| 1008 | }; |
| 1009 | |
| 1010 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
| 1011 | XFillPolygon(dpy, item->sel, dc.gc, triangle, LEN(triangle), |
| 1012 | Convex, CoordModeOrigin); |
| 1013 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
| 1014 | XFillPolygon(dpy, item->unsel, dc.gc, triangle, LEN(triangle), |
| 1015 | Convex, CoordModeOrigin); |
| 1016 | } |
| 1017 | |
| 1018 | /* try to load icon */ |
| 1019 | if (item->file && !iflag) { |
| 1020 | item->icon = loadicon(item->file); |
| 1021 | free(item->file); |
| 1022 | } |
| 1023 | |
| 1024 | /* draw icon if properly loaded */ |
| 1025 | if (item->icon) { |
| 1026 | imlib_context_set_image(item->icon); |
| 1027 | imlib_context_set_drawable(item->sel); |
| 1028 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
| 1029 | imlib_context_set_drawable(item->unsel); |
| 1030 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
| 1031 | imlib_context_set_image(item->icon); |
| 1032 | imlib_free_image(); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* copy pixmaps of items of the current menu and of its ancestors into menu window */ |
| 1039 | static void |
| 1040 | drawmenus(struct Menu *currmenu) |
| 1041 | { |
| 1042 | struct Menu *menu; |
| 1043 | struct Item *item; |
| 1044 | |
| 1045 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
| 1046 | if (!menu->drawn) { |
| 1047 | drawitems(menu); |
| 1048 | menu->drawn = 1; |
| 1049 | } |
| 1050 | for (item = menu->list; item != NULL; item = item->next) { |
| 1051 | if (item == menu->selected) |
| 1052 | XCopyArea(dpy, item->sel, menu->win, dc.gc, 0, 0, |
| 1053 | menu->w, item->h, 0, item->y); |
| 1054 | else |
| 1055 | XCopyArea(dpy, item->unsel, menu->win, dc.gc, 0, 0, |
| 1056 | menu->w, item->h, 0, item->y); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /* umap previous menus and map current menu and its parents */ |
| 1062 | static void |
| 1063 | mapmenu(struct Menu *currmenu) |
| 1064 | { |
| 1065 | static struct Menu *prevmenu = NULL; |
| 1066 | struct Menu *menu, *menu_; |
| 1067 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
| 1068 | unsigned minlevel; /* level of the closest to root menu */ |
| 1069 | unsigned maxlevel; /* level of the closest to root menu */ |
| 1070 | |
| 1071 | /* do not remap current menu if it wasn't updated*/ |
| 1072 | if (prevmenu == currmenu) |
| 1073 | return; |
| 1074 | |
| 1075 | /* if this is the first time mapping, skip calculations */ |
| 1076 | if (prevmenu == NULL) { |
| 1077 | XMapWindow(dpy, currmenu->win); |
| 1078 | prevmenu = currmenu; |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | /* find lowest common ancestor menu */ |
| 1083 | minlevel = MIN(currmenu->level, prevmenu->level); |
| 1084 | maxlevel = MAX(currmenu->level, prevmenu->level); |
| 1085 | if (currmenu->level == maxlevel) { |
| 1086 | menu = currmenu; |
| 1087 | menu_ = prevmenu; |
| 1088 | } else { |
| 1089 | menu = prevmenu; |
| 1090 | menu_ = currmenu; |
| 1091 | } |
| 1092 | while (menu->level > minlevel) |
| 1093 | menu = menu->parent; |
| 1094 | while (menu != menu_) { |
| 1095 | menu = menu->parent; |
| 1096 | menu_ = menu_->parent; |
| 1097 | } |
| 1098 | lcamenu = menu; |
| 1099 | |
| 1100 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
| 1101 | for (menu = prevmenu; menu != lcamenu; menu = menu->parent) { |
| 1102 | menu->selected = NULL; |
| 1103 | XUnmapWindow(dpy, menu->win); |
| 1104 | } |
| 1105 | |
| 1106 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
| 1107 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
| 1108 | |
| 1109 | if (wflag) { |
| 1110 | setupmenupos(menu); |
| 1111 | XMoveWindow(dpy, menu->win, menu->x, menu->y); |
| 1112 | } |
| 1113 | |
| 1114 | XMapWindow(dpy, menu->win); |
| 1115 | } |
| 1116 | |
| 1117 | prevmenu = currmenu; |
| 1118 | } |
| 1119 | |
| 1120 | /* get menu of given window */ |
| 1121 | static struct Menu * |
| 1122 | getmenu(struct Menu *currmenu, Window win) |
| 1123 | { |
| 1124 | struct Menu *menu; |
| 1125 | |
| 1126 | for (menu = currmenu; menu != NULL; menu = menu->parent) |
| 1127 | if (menu->win == win) |
| 1128 | return menu; |
| 1129 | |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
| 1133 | /* get item of given menu and position */ |
| 1134 | static struct Item * |
| 1135 | getitem(struct Menu *menu, int y) |
| 1136 | { |
| 1137 | struct Item *item; |
| 1138 | |
| 1139 | if (menu == NULL) |
| 1140 | return NULL; |
| 1141 | |
| 1142 | for (item = menu->list; item != NULL; item = item->next) |
| 1143 | if (y >= item->y && y <= item->y + item->h) |
| 1144 | return item; |
| 1145 | |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | |
| 1149 | /* cycle through the items; non-zero direction is next, zero is prev */ |
| 1150 | static struct Item * |
| 1151 | itemcycle(struct Menu *currmenu, int direction) |
| 1152 | { |
| 1153 | struct Item *item = NULL; |
| 1154 | struct Item *lastitem; |
| 1155 | |
| 1156 | for (lastitem = currmenu->list; lastitem && lastitem->next; lastitem = lastitem->next) |
| 1157 | ; |
| 1158 | |
| 1159 | /* select item (either separator or labeled item) in given direction */ |
| 1160 | switch (direction) { |
| 1161 | case ITEMNEXT: |
| 1162 | if (currmenu->selected == NULL) |
| 1163 | item = currmenu->list; |
| 1164 | else if (currmenu->selected->next != NULL) |
| 1165 | item = currmenu->selected->next; |
| 1166 | break; |
| 1167 | case ITEMPREV: |
| 1168 | if (currmenu->selected == NULL) |
| 1169 | item = lastitem; |
| 1170 | else if (currmenu->selected->prev != NULL) |
| 1171 | item = currmenu->selected->prev; |
| 1172 | break; |
| 1173 | case ITEMFIRST: |
| 1174 | item = currmenu->list; |
| 1175 | break; |
| 1176 | case ITEMLAST: |
| 1177 | item = lastitem; |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * the selected item can be a separator |
| 1183 | * let's select the closest labeled item (ie., one that isn't a separator) |
| 1184 | */ |
| 1185 | switch (direction) { |
| 1186 | case ITEMNEXT: |
| 1187 | case ITEMFIRST: |
| 1188 | while (item != NULL && item->label == NULL) |
| 1189 | item = item->next; |
| 1190 | if (item == NULL) |
| 1191 | item = currmenu->list; |
| 1192 | break; |
| 1193 | case ITEMPREV: |
| 1194 | case ITEMLAST: |
| 1195 | while (item != NULL && item->label == NULL) |
| 1196 | item = item->prev; |
| 1197 | if (item == NULL) |
| 1198 | item = lastitem; |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | return item; |
| 1203 | } |
| 1204 | |
| 1205 | /* run event loop */ |
| 1206 | static void |
| 1207 | run(struct Menu *currmenu) |
| 1208 | { |
| 1209 | struct Menu *menu; |
| 1210 | struct Item *item; |
| 1211 | struct Item *previtem = NULL; |
| 1212 | KeySym ksym; |
| 1213 | XEvent ev; |
| 1214 | |
| 1215 | mapmenu(currmenu); |
| 1216 | |
| 1217 | while (!XNextEvent(dpy, &ev)) { |
| 1218 | switch(ev.type) { |
| 1219 | case Expose: |
| 1220 | if (ev.xexpose.count == 0) |
| 1221 | drawmenus(currmenu); |
| 1222 | break; |
| 1223 | case MotionNotify: |
| 1224 | menu = getmenu(currmenu, ev.xbutton.window); |
| 1225 | item = getitem(menu, ev.xbutton.y); |
| 1226 | if (menu == NULL || item == NULL || previtem == item) |
| 1227 | break; |
| 1228 | previtem = item; |
| 1229 | menu->selected = item; |
| 1230 | if (item->submenu != NULL) { |
| 1231 | currmenu = item->submenu; |
| 1232 | currmenu->selected = NULL; |
| 1233 | } else { |
| 1234 | currmenu = menu; |
| 1235 | } |
| 1236 | mapmenu(currmenu); |
| 1237 | drawmenus(currmenu); |
| 1238 | break; |
| 1239 | case ButtonRelease: |
| 1240 | menu = getmenu(currmenu, ev.xbutton.window); |
| 1241 | item = getitem(menu, ev.xbutton.y); |
| 1242 | if (menu == NULL || item == NULL) |
| 1243 | break; |
| 1244 | selectitem: |
| 1245 | if (item->label == NULL) |
| 1246 | break; /* ignore separators */ |
| 1247 | if (item->submenu != NULL) { |
| 1248 | currmenu = item->submenu; |
| 1249 | } else { |
| 1250 | printf("%s\n", item->output); |
| 1251 | return; |
| 1252 | } |
| 1253 | mapmenu(currmenu); |
| 1254 | currmenu->selected = currmenu->list; |
| 1255 | drawmenus(currmenu); |
| 1256 | break; |
| 1257 | case ButtonPress: |
| 1258 | menu = getmenu(currmenu, ev.xbutton.window); |
| 1259 | if (menu == NULL) |
| 1260 | return; |
| 1261 | break; |
| 1262 | case KeyPress: |
| 1263 | ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0); |
| 1264 | |
| 1265 | /* esc closes xmenu when current menu is the root menu */ |
| 1266 | if (ksym == XK_Escape && currmenu->parent == NULL) |
| 1267 | return; |
| 1268 | |
| 1269 | /* Shift-Tab = ISO_Left_Tab */ |
| 1270 | if (ksym == XK_Tab && (ev.xkey.state & ShiftMask)) |
| 1271 | ksym = XK_ISO_Left_Tab; |
| 1272 | |
| 1273 | /* cycle through menu */ |
| 1274 | item = NULL; |
| 1275 | if (ksym == XK_Home) { |
| 1276 | item = itemcycle(currmenu, ITEMFIRST); |
| 1277 | } else if (ksym == XK_End) { |
| 1278 | item = itemcycle(currmenu, ITEMLAST); |
| 1279 | } else if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) { |
| 1280 | item = itemcycle(currmenu, ITEMPREV); |
| 1281 | } else if (ksym == XK_Tab || ksym == XK_Down) { |
| 1282 | item = itemcycle(currmenu, ITEMNEXT); |
| 1283 | } else if ((ksym == XK_Return || ksym == XK_Right) && |
| 1284 | currmenu->selected != NULL) { |
| 1285 | item = currmenu->selected; |
| 1286 | goto selectitem; |
| 1287 | } else if ((ksym == XK_Escape || ksym == XK_Left) && |
| 1288 | currmenu->parent != NULL) { |
| 1289 | item = currmenu->parent->selected; |
| 1290 | currmenu = currmenu->parent; |
| 1291 | mapmenu(currmenu); |
| 1292 | } else |
| 1293 | break; |
| 1294 | currmenu->selected = item; |
| 1295 | drawmenus(currmenu); |
| 1296 | break; |
| 1297 | case LeaveNotify: |
| 1298 | previtem = NULL; |
| 1299 | currmenu->selected = NULL; |
| 1300 | drawmenus(currmenu); |
| 1301 | break; |
| 1302 | case ConfigureNotify: |
| 1303 | menu = getmenu(currmenu, ev.xconfigure.window); |
| 1304 | if (menu == NULL) |
| 1305 | break; |
| 1306 | menu->x = ev.xconfigure.x; |
| 1307 | menu->y = ev.xconfigure.y; |
| 1308 | break; |
| 1309 | case ClientMessage: |
| 1310 | if ((unsigned long) ev.xclient.data.l[0] != wmdelete) |
| 1311 | break; |
| 1312 | /* user closed window */ |
| 1313 | menu = getmenu(currmenu, ev.xclient.window); |
| 1314 | if (menu->parent == NULL) |
| 1315 | return; /* closing the root menu closes the program */ |
| 1316 | currmenu = menu->parent; |
| 1317 | mapmenu(currmenu); |
| 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /* recursivelly free pixmaps and destroy windows */ |
| 1324 | static void |
| 1325 | cleanmenu(struct Menu *menu) |
| 1326 | { |
| 1327 | struct Item *item; |
| 1328 | struct Item *tmp; |
| 1329 | |
| 1330 | item = menu->list; |
| 1331 | while (item != NULL) { |
| 1332 | if (item->submenu != NULL) |
| 1333 | cleanmenu(item->submenu); |
| 1334 | tmp = item; |
| 1335 | if (menu->drawn) { |
| 1336 | XFreePixmap(dpy, item->unsel); |
| 1337 | if (tmp->label != NULL) |
| 1338 | XFreePixmap(dpy, item->sel); |
| 1339 | } |
| 1340 | if (tmp->label != tmp->output) |
| 1341 | free(tmp->label); |
| 1342 | free(tmp->output); |
| 1343 | item = item->next; |
| 1344 | free(tmp); |
| 1345 | } |
| 1346 | |
| 1347 | XDestroyWindow(dpy, menu->win); |
| 1348 | free(menu); |
| 1349 | } |
| 1350 | |
| 1351 | /* cleanup X and exit */ |
| 1352 | static void |
| 1353 | cleanup(void) |
| 1354 | { |
| 1355 | size_t i; |
| 1356 | |
| 1357 | XUngrabPointer(dpy, CurrentTime); |
| 1358 | XUngrabKeyboard(dpy, CurrentTime); |
| 1359 | |
| 1360 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); |
| 1361 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); |
| 1362 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); |
| 1363 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]); |
| 1364 | XftColorFree(dpy, visual, colormap, &dc.separator); |
| 1365 | XftColorFree(dpy, visual, colormap, &dc.border); |
| 1366 | |
| 1367 | for (i = 0; i < dc.nfonts; i++) |
| 1368 | XftFontClose(dpy, dc.fonts[i]); |
| 1369 | |
| 1370 | XFreeGC(dpy, dc.gc); |
| 1371 | XCloseDisplay(dpy); |
| 1372 | } |
| 1373 | |
| 1374 | /* show usage */ |
| 1375 | static void |
| 1376 | usage(void) |
| 1377 | { |
| 1378 | (void)fprintf(stderr, "usage: xmenu [-iw] [-p position] [title]\n"); |
| 1379 | exit(1); |
| 1380 | } |