dmenu.c (20905B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <ctype.h> 3 #include <locale.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <strings.h> 8 #include <time.h> 9 #include <unistd.h> 10 11 #include <X11/Xlib.h> 12 #include <X11/Xatom.h> 13 #include <X11/Xproto.h> 14 #include <X11/Xutil.h> 15 #ifdef XINERAMA 16 #include <X11/extensions/Xinerama.h> 17 #endif 18 #include <X11/Xft/Xft.h> 19 20 #include "drw.h" 21 #include "util.h" 22 23 /* macros */ 24 #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ 25 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) 26 #define LENGTH(X) (sizeof X / sizeof X[0]) 27 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 28 29 #define OPAQUE 0xffU 30 31 /* enums */ 32 enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 33 34 struct item { 35 char *text; 36 struct item *left, *right; 37 int out; 38 }; 39 40 static char text[BUFSIZ] = ""; 41 static char *embed; 42 static int bh, mw, mh; 43 static int inputw = 0, promptw; 44 static int lrpad; /* sum of left and right padding */ 45 static size_t cursor; 46 static struct item *items = NULL; 47 static struct item *matches, *matchend; 48 static struct item *prev, *curr, *next, *sel; 49 static int mon = -1, screen; 50 51 static Atom clip, utf8; 52 static Display *dpy; 53 static Window root, parentwin, win; 54 static XIC xic; 55 56 static Drw *drw; 57 static Clr *scheme[SchemeLast]; 58 59 static int useargb = 0; 60 static Visual *visual; 61 static int depth; 62 static Colormap cmap; 63 64 #include "config.h" 65 66 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 67 static char *(*fstrstr)(const char *, const char *) = strstr; 68 static void xinitvisual(); 69 70 static unsigned int 71 textw_clamp(const char *str, unsigned int n) 72 { 73 unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad; 74 return MIN(w, n); 75 } 76 77 static void 78 appenditem(struct item *item, struct item **list, struct item **last) 79 { 80 if (*last) 81 (*last)->right = item; 82 else 83 *list = item; 84 85 item->left = *last; 86 item->right = NULL; 87 *last = item; 88 } 89 90 static void 91 calcoffsets(void) 92 { 93 int i, n; 94 95 if (lines > 0) 96 n = lines * bh; 97 else 98 n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">")); 99 /* calculate which items will begin the next page and previous page */ 100 for (i = 0, next = curr; next; next = next->right) 101 if ((i += (lines > 0) ? bh : textw_clamp(next->text, n)) > n) 102 break; 103 for (i = 0, prev = curr; prev && prev->left; prev = prev->left) 104 if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n) 105 break; 106 } 107 108 static int 109 max_textw(void) 110 { 111 int len = 0; 112 for (struct item *item = items; item && item->text; item++) 113 len = MAX(TEXTW(item->text), len); 114 return len; 115 } 116 117 static void 118 cleanup(void) 119 { 120 size_t i; 121 122 XUngrabKey(dpy, AnyKey, AnyModifier, root); 123 for (i = 0; i < SchemeLast; i++) 124 free(scheme[i]); 125 for (i = 0; items && items[i].text; ++i) 126 free(items[i].text); 127 free(items); 128 drw_free(drw); 129 XSync(dpy, False); 130 XCloseDisplay(dpy); 131 } 132 133 static char * 134 cistrstr(const char *h, const char *n) 135 { 136 size_t i; 137 138 if (!n[0]) 139 return (char *)h; 140 141 for (; *h; ++h) { 142 for (i = 0; n[i] && tolower((unsigned char)n[i]) == 143 tolower((unsigned char)h[i]); ++i) 144 ; 145 if (n[i] == '\0') 146 return (char *)h; 147 } 148 return NULL; 149 } 150 151 static int 152 drawitem(struct item *item, int x, int y, int w) 153 { 154 if (item == sel) 155 drw_setscheme(drw, scheme[SchemeSel]); 156 else if (item->out) 157 drw_setscheme(drw, scheme[SchemeOut]); 158 else 159 drw_setscheme(drw, scheme[SchemeNorm]); 160 161 return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 162 } 163 164 static void 165 drawmenu(void) 166 { 167 unsigned int curpos; 168 struct item *item; 169 int x = 0, y = 0, w; 170 171 drw_setscheme(drw, scheme[SchemeNorm]); 172 drw_rect(drw, 0, 0, mw, mh, 1, 1); 173 174 if (prompt && *prompt) { 175 drw_setscheme(drw, scheme[SchemeSel]); 176 x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); 177 } 178 /* draw input field */ 179 w = (lines > 0 || !matches) ? mw - x : inputw; 180 drw_setscheme(drw, scheme[SchemeNorm]); 181 drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0); 182 183 curpos = TEXTW(text) - TEXTW(&text[cursor]); 184 if ((curpos += lrpad / 2 - 1) < w) { 185 drw_setscheme(drw, scheme[SchemeNorm]); 186 drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0); 187 } 188 189 if (lines > 0) { 190 /* draw vertical list */ 191 for (item = curr; item != next; item = item->right) 192 drawitem(item, x, y += bh, mw - x); 193 } else if (matches) { 194 /* draw horizontal list */ 195 x += inputw; 196 w = TEXTW("<"); 197 if (curr->left) { 198 drw_setscheme(drw, scheme[SchemeNorm]); 199 drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0); 200 } 201 x += w; 202 for (item = curr; item != next; item = item->right) 203 x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">"))); 204 if (next) { 205 w = TEXTW(">"); 206 drw_setscheme(drw, scheme[SchemeNorm]); 207 drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0); 208 } 209 } 210 drw_map(drw, win, 0, 0, mw, mh); 211 } 212 213 static void 214 grabfocus(void) 215 { 216 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; 217 Window focuswin; 218 int i, revertwin; 219 220 for (i = 0; i < 100; ++i) { 221 XGetInputFocus(dpy, &focuswin, &revertwin); 222 if (focuswin == win) 223 return; 224 XSetInputFocus(dpy, win, RevertToParent, CurrentTime); 225 nanosleep(&ts, NULL); 226 } 227 die("cannot grab focus"); 228 } 229 230 static void 231 grabkeyboard(void) 232 { 233 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; 234 int i; 235 236 if (embed) 237 return; 238 /* try to grab keyboard, we may have to wait for another process to ungrab */ 239 for (i = 0; i < 1000; i++) { 240 if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync, 241 GrabModeAsync, CurrentTime) == GrabSuccess) 242 return; 243 nanosleep(&ts, NULL); 244 } 245 die("cannot grab keyboard"); 246 } 247 248 static void 249 match(void) 250 { 251 static char **tokv = NULL; 252 static int tokn = 0; 253 254 char buf[sizeof text], *s; 255 int i, tokc = 0; 256 size_t len, textsize; 257 struct item *item, *lprefix, *prefixend; 258 259 strcpy(buf, text); 260 /* separate input text into tokens to be matched individually */ 261 for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " ")) 262 if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv))) 263 die("cannot realloc %zu bytes:", tokn * sizeof *tokv); 264 len = tokc ? strlen(tokv[0]) : 0; 265 266 matches = lprefix = matchend = prefixend = NULL; 267 textsize = strlen(text); 268 for (item = items; item && item->text; item++) { 269 for (i = 0; i < tokc; i++) 270 if (!fstrstr(item->text, tokv[i])) 271 break; 272 if (i != tokc) /* not all tokens match */ 273 continue; 274 /* exact matches go first, then prefixes, then substrings */ 275 if (!tokc || !fstrncmp(text, item->text, textsize)) 276 appenditem(item, &matches, &matchend); 277 else if (!fstrncmp(tokv[0], item->text, len)) 278 appenditem(item, &lprefix, &prefixend); 279 } 280 if (lprefix) { 281 if (matches) { 282 matchend->right = lprefix; 283 lprefix->left = matchend; 284 } else 285 matches = lprefix; 286 matchend = prefixend; 287 } 288 curr = sel = matches; 289 calcoffsets(); 290 } 291 292 static void 293 insert(const char *str, ssize_t n) 294 { 295 if (strlen(text) + n > sizeof text - 1) 296 return; 297 /* move existing text out of the way, insert new text, and update cursor */ 298 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); 299 if (n > 0) 300 memcpy(&text[cursor], str, n); 301 cursor += n; 302 match(); 303 } 304 305 static size_t 306 nextrune(int inc) 307 { 308 ssize_t n; 309 310 /* return location of next utf8 rune in the given direction (+1 or -1) */ 311 for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc) 312 ; 313 return n; 314 } 315 316 static void 317 movewordedge(int dir) 318 { 319 if (dir < 0) { /* move cursor to the start of the word*/ 320 while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) 321 cursor = nextrune(-1); 322 while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)])) 323 cursor = nextrune(-1); 324 } else { /* move cursor to the end of the word */ 325 while (text[cursor] && strchr(worddelimiters, text[cursor])) 326 cursor = nextrune(+1); 327 while (text[cursor] && !strchr(worddelimiters, text[cursor])) 328 cursor = nextrune(+1); 329 } 330 } 331 332 static void 333 keypress(XKeyEvent *ev) 334 { 335 char buf[64]; 336 int len; 337 struct item* item; 338 KeySym ksym; 339 Status status; 340 341 ksym = NoSymbol; 342 len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status); 343 switch (status) { 344 default: /* XLookupNone, XBufferOverflow */ 345 return; 346 case XLookupChars: /* composed string from input method */ 347 goto insert; 348 case XLookupKeySym: 349 case XLookupBoth: /* a KeySym and a string are returned: use keysym */ 350 break; 351 } 352 353 if (ev->state & ControlMask) { 354 switch(ksym) { 355 case XK_a: ksym = XK_Home; break; 356 case XK_b: ksym = XK_Left; break; 357 case XK_c: ksym = XK_Escape; break; 358 case XK_d: ksym = XK_Delete; break; 359 case XK_e: ksym = XK_End; break; 360 case XK_f: ksym = XK_Right; break; 361 case XK_g: ksym = XK_Escape; break; 362 case XK_h: ksym = XK_BackSpace; break; 363 case XK_i: ksym = XK_Tab; break; 364 case XK_j: /* fallthrough */ 365 case XK_J: /* fallthrough */ 366 case XK_m: /* fallthrough */ 367 case XK_M: ksym = XK_Return; ev->state &= ~ControlMask; break; 368 case XK_n: ksym = XK_Down; break; 369 case XK_p: ksym = XK_Up; break; 370 371 case XK_k: /* delete right */ 372 text[cursor] = '\0'; 373 match(); 374 break; 375 case XK_u: /* delete left */ 376 insert(NULL, 0 - cursor); 377 break; 378 case XK_w: /* delete word */ 379 while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) 380 insert(NULL, nextrune(-1) - cursor); 381 while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)])) 382 insert(NULL, nextrune(-1) - cursor); 383 break; 384 case XK_y: /* paste selection */ 385 case XK_Y: 386 XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, 387 utf8, utf8, win, CurrentTime); 388 return; 389 case XK_Left: 390 case XK_KP_Left: 391 movewordedge(-1); 392 goto draw; 393 case XK_Right: 394 case XK_KP_Right: 395 movewordedge(+1); 396 goto draw; 397 case XK_Return: 398 case XK_KP_Enter: 399 break; 400 case XK_bracketleft: 401 cleanup(); 402 exit(1); 403 default: 404 return; 405 } 406 } else if (ev->state & Mod1Mask) { 407 switch(ksym) { 408 case XK_b: 409 movewordedge(-1); 410 goto draw; 411 case XK_f: 412 movewordedge(+1); 413 goto draw; 414 case XK_g: ksym = XK_Home; break; 415 case XK_G: ksym = XK_End; break; 416 case XK_h: ksym = XK_Up; break; 417 case XK_j: ksym = XK_Next; break; 418 case XK_k: ksym = XK_Prior; break; 419 case XK_l: ksym = XK_Down; break; 420 default: 421 return; 422 } 423 } 424 425 switch(ksym) { 426 default: 427 insert: 428 if (!iscntrl((unsigned char)*buf)) 429 insert(buf, len); 430 break; 431 case XK_Delete: 432 case XK_KP_Delete: 433 if (text[cursor] == '\0') 434 return; 435 cursor = nextrune(+1); 436 /* fallthrough */ 437 case XK_BackSpace: 438 if (cursor == 0) 439 return; 440 insert(NULL, nextrune(-1) - cursor); 441 break; 442 case XK_End: 443 case XK_KP_End: 444 if (text[cursor] != '\0') { 445 cursor = strlen(text); 446 break; 447 } 448 if (next) { 449 /* jump to end of list and position items in reverse */ 450 curr = matchend; 451 calcoffsets(); 452 curr = prev; 453 calcoffsets(); 454 while (next && (curr = curr->right)) 455 calcoffsets(); 456 } 457 sel = matchend; 458 break; 459 case XK_Escape: 460 cleanup(); 461 exit(1); 462 case XK_Home: 463 case XK_KP_Home: 464 if (sel == matches) { 465 cursor = 0; 466 break; 467 } 468 sel = curr = matches; 469 calcoffsets(); 470 break; 471 case XK_Left: 472 case XK_KP_Left: 473 if (cursor > 0 && (!sel || !sel->left || lines > 0)) { 474 cursor = nextrune(-1); 475 break; 476 } 477 if (lines > 0) 478 return; 479 /* fallthrough */ 480 case XK_Up: 481 case XK_KP_Up: 482 if (sel && sel->left && (sel = sel->left)->right == curr) { 483 curr = prev; 484 calcoffsets(); 485 } 486 break; 487 case XK_Next: 488 case XK_KP_Next: 489 if (!next) 490 return; 491 sel = curr = next; 492 calcoffsets(); 493 break; 494 case XK_Prior: 495 case XK_KP_Prior: 496 if (!prev) 497 return; 498 sel = curr = prev; 499 calcoffsets(); 500 break; 501 case XK_Return: 502 case XK_KP_Enter: 503 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); 504 if (!(ev->state & ControlMask)) { 505 cleanup(); 506 exit(0); 507 } 508 if (sel) 509 sel->out = 1; 510 break; 511 case XK_Right: 512 case XK_KP_Right: 513 if (text[cursor] != '\0') { 514 cursor = nextrune(+1); 515 break; 516 } 517 if (lines > 0) 518 return; 519 /* fallthrough */ 520 case XK_Down: 521 case XK_KP_Down: 522 if (sel && sel->right && (sel = sel->right) == next) { 523 curr = next; 524 calcoffsets(); 525 } 526 break; 527 case XK_Tab: 528 if (!matches) 529 break; 530 strncpy(text, matches->text, sizeof text - 1); 531 text[sizeof(text) - 1] = '\0'; 532 len = cursor = strlen(text); /* length of longest common prefix */ 533 for (item = matches; item && item->text; item = item->right) { 534 for (cursor = 0 535 ;cursor < len && text[cursor] == item->text[cursor] 536 ;++cursor 537 ); 538 len = cursor; 539 } 540 memset(text + len, '\0', strlen(text) - len); 541 break; 542 } 543 544 draw: 545 drawmenu(); 546 } 547 548 static void 549 paste(void) 550 { 551 char *p, *q; 552 int di; 553 unsigned long dl; 554 Atom da; 555 556 /* we have been given the current selection, now insert it into input */ 557 if (XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False, 558 utf8, &da, &di, &dl, &dl, (unsigned char **)&p) 559 == Success && p) { 560 insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p)); 561 XFree(p); 562 } 563 drawmenu(); 564 } 565 566 static void 567 readstdin(void) 568 { 569 char *line = NULL; 570 size_t i, junk, itemsiz = 0; 571 ssize_t len; 572 573 /* read each line from stdin and add it to the item list */ 574 for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) { 575 if (i + 1 >= itemsiz) { 576 itemsiz += 256; 577 if (!(items = realloc(items, itemsiz * sizeof(*items)))) 578 die("cannot realloc %zu bytes:", itemsiz * sizeof(*items)); 579 } 580 if (line[len - 1] == '\n') 581 line[len - 1] = '\0'; 582 items[i].text = line; 583 items[i].out = 0; 584 line = NULL; /* next call of getline() allocates a new line */ 585 } 586 free(line); 587 if (items) 588 items[i].text = NULL; 589 lines = MIN(lines, i); 590 } 591 592 static void 593 run(void) 594 { 595 XEvent ev; 596 597 while (!XNextEvent(dpy, &ev)) { 598 if (XFilterEvent(&ev, win)) 599 continue; 600 switch(ev.type) { 601 case DestroyNotify: 602 if (ev.xdestroywindow.window != win) 603 break; 604 cleanup(); 605 exit(1); 606 case Expose: 607 if (ev.xexpose.count == 0) 608 drw_map(drw, win, 0, 0, mw, mh); 609 break; 610 case FocusIn: 611 /* regrab focus from parent window */ 612 if (ev.xfocus.window != win) 613 grabfocus(); 614 break; 615 case KeyPress: 616 keypress(&ev.xkey); 617 break; 618 case SelectionNotify: 619 if (ev.xselection.property == utf8) 620 paste(); 621 break; 622 case VisibilityNotify: 623 if (ev.xvisibility.state != VisibilityUnobscured) 624 XRaiseWindow(dpy, win); 625 break; 626 } 627 } 628 } 629 630 static void 631 setup(void) 632 { 633 int x, y, i, j; 634 unsigned int du; 635 XSetWindowAttributes swa; 636 XIM xim; 637 Window w, dw, *dws; 638 XWindowAttributes wa; 639 XClassHint ch = {"dmenu", "dmenu"}; 640 #ifdef XINERAMA 641 XineramaScreenInfo *info; 642 Window pw; 643 int a, di, n, area = 0; 644 #endif 645 /* init appearance */ 646 for (j = 0; j < SchemeLast; j++) 647 scheme[j] = drw_scm_create(drw, colors[j], alphas[j], 2); 648 649 clip = XInternAtom(dpy, "CLIPBOARD", False); 650 utf8 = XInternAtom(dpy, "UTF8_STRING", False); 651 652 /* calculate menu geometry */ 653 bh = drw->fonts->h + 2; 654 lines = MAX(lines, 0); 655 mh = (lines + 1) * bh; 656 promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 657 #ifdef XINERAMA 658 i = 0; 659 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 660 XGetInputFocus(dpy, &w, &di); 661 if (mon >= 0 && mon < n) 662 i = mon; 663 else if (w != root && w != PointerRoot && w != None) { 664 /* find top-level window containing current input focus */ 665 do { 666 if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws) 667 XFree(dws); 668 } while (w != root && w != pw); 669 /* find xinerama screen with which the window intersects most */ 670 if (XGetWindowAttributes(dpy, pw, &wa)) 671 for (j = 0; j < n; j++) 672 if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { 673 area = a; 674 i = j; 675 } 676 } 677 /* no focused window is on screen, so use pointer location instead */ 678 if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) 679 for (i = 0; i < n; i++) 680 if (INTERSECT(x, y, 1, 1, info[i]) != 0) 681 break; 682 683 mw = MIN(MAX(max_textw() + promptw, 100), info[i].width); 684 x = info[i].x_org + ((info[i].width - mw) / 2); 685 y = info[i].y_org + ((info[i].height - mh) / 2); 686 XFree(info); 687 } else 688 #endif 689 { 690 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 691 die("could not get embedding window attributes: 0x%lx", 692 parentwin); 693 mw = MIN(MAX(max_textw() + promptw, 100), wa.width); 694 x = (wa.width - mw) / 2; 695 y = (wa.height - mh) / 2; 696 } 697 inputw = mw / 3; /* input width: ~33% of monitor width */ 698 match(); 699 700 /* create menu window */ 701 swa.override_redirect = True; 702 swa.background_pixel = 0; 703 swa.border_pixel = 0; 704 swa.colormap = cmap; 705 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; 706 win = XCreateWindow(dpy, parentwin, x, y, mw, mh, bw, 707 depth, CopyFromParent, visual, 708 CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &swa); 709 XSetWindowBorder(dpy, win, scheme[SchemeSel][ColBg].pixel); 710 XSetClassHint(dpy, win, &ch); 711 712 /* input methods */ 713 if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL) 714 die("XOpenIM failed: could not open input device"); 715 716 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, 717 XNClientWindow, win, XNFocusWindow, win, NULL); 718 719 XMapRaised(dpy, win); 720 if (embed) { 721 XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask); 722 if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) { 723 for (i = 0; i < du && dws[i] != win; ++i) 724 XSelectInput(dpy, dws[i], FocusChangeMask); 725 XFree(dws); 726 } 727 grabfocus(); 728 } 729 drw_resize(drw, mw, mh); 730 drawmenu(); 731 } 732 733 static void 734 usage(void) 735 { 736 die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" 737 " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]"); 738 } 739 740 int 741 main(int argc, char *argv[]) 742 { 743 XWindowAttributes wa; 744 int i, fast = 0; 745 746 for (i = 1; i < argc; i++) 747 /* these options take no arguments */ 748 if (!strcmp(argv[i], "-v")) { /* prints version information */ 749 puts("dmenu-"VERSION); 750 exit(0); 751 } else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */ 752 topbar = 0; 753 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 754 fast = 1; 755 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 756 fstrncmp = strncasecmp; 757 fstrstr = cistrstr; 758 } else if (i + 1 == argc) 759 usage(); 760 /* these options take one argument */ 761 else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */ 762 lines = atoi(argv[++i]); 763 else if (!strcmp(argv[i], "-m")) 764 mon = atoi(argv[++i]); 765 else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ 766 prompt = argv[++i]; 767 else if (!strcmp(argv[i], "-fn")) /* font or font set */ 768 fonts[0] = argv[++i]; 769 else if (!strcmp(argv[i], "-nb")) /* normal background color */ 770 colors[SchemeNorm][ColBg] = argv[++i]; 771 else if (!strcmp(argv[i], "-nf")) /* normal foreground color */ 772 colors[SchemeNorm][ColFg] = argv[++i]; 773 else if (!strcmp(argv[i], "-sb")) /* selected background color */ 774 colors[SchemeSel][ColBg] = argv[++i]; 775 else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ 776 colors[SchemeSel][ColFg] = argv[++i]; 777 else if (!strcmp(argv[i], "-w")) /* embedding window id */ 778 embed = argv[++i]; 779 else 780 usage(); 781 782 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 783 fputs("warning: no locale support\n", stderr); 784 if (!(dpy = XOpenDisplay(NULL))) 785 die("cannot open display"); 786 screen = DefaultScreen(dpy); 787 root = RootWindow(dpy, screen); 788 if (!embed || !(parentwin = strtol(embed, NULL, 0))) 789 parentwin = root; 790 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 791 die("could not get embedding window attributes: 0x%lx", 792 parentwin); 793 xinitvisual(); 794 drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); 795 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 796 die("no fonts could be loaded."); 797 lrpad = drw->fonts->h; 798 799 #ifdef __OpenBSD__ 800 if (pledge("stdio rpath", NULL) == -1) 801 die("pledge"); 802 #endif 803 804 if (fast && !isatty(0)) { 805 grabkeyboard(); 806 readstdin(); 807 } else { 808 readstdin(); 809 grabkeyboard(); 810 } 811 setup(); 812 run(); 813 814 return 1; /* unreachable */ 815 } 816 817 void 818 xinitvisual() 819 { 820 XVisualInfo *infos; 821 XRenderPictFormat *fmt; 822 int nitems; 823 int i; 824 825 XVisualInfo tpl = { 826 .screen = screen, 827 .depth = 32, 828 .class = TrueColor 829 }; 830 long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; 831 832 infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); 833 visual = NULL; 834 for(i = 0; i < nitems; i ++) { 835 fmt = XRenderFindVisualFormat(dpy, infos[i].visual); 836 if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 837 visual = infos[i].visual; 838 depth = infos[i].depth; 839 cmap = XCreateColormap(dpy, root, visual, AllocNone); 840 useargb = 1; 841 break; 842 } 843 } 844 845 XFree(infos); 846 847 if (! visual) { 848 visual = DefaultVisual(dpy, screen); 849 depth = DefaultDepth(dpy, screen); 850 cmap = DefaultColormap(dpy, screen); 851 } 852 }