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