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