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