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