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