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