Xinqi Bao's Git

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