Xinqi Bao's Git

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