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