dmenu for lunch applications in dwm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

682 lines
17 KiB

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