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