Xinqi Bao's Git

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