dmenu.c 17 KB

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