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