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.

695 lines
17 KiB

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