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