My dmenu build
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.

dmenu.c 16 KiB

13 years ago
18 years ago
18 years ago
16 years ago
18 years ago
13 years ago
8 years ago
14 years ago
18 years ago
8 years ago
18 years ago
13 years ago
13 years ago
12 years ago
14 years ago
13 years ago
17 years ago
12 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
12 years ago
14 years ago
12 years ago
12 years ago
18 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
18 years ago
12 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
8 years ago
13 years ago
18 years ago
12 years ago
18 years ago
18 years ago
13 years ago
18 years ago
8 years ago
13 years ago
18 years ago
13 years ago
18 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 && text[nextrune(-1)] == ' ')
  281. insert(NULL, nextrune(-1) - cursor);
  282. while (cursor > 0 && text[nextrune(-1)] != ' ')
  283. insert(NULL, nextrune(-1) - cursor);
  284. break;
  285. case XK_y: /* paste selection */
  286. case XK_Y:
  287. XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY,
  288. utf8, utf8, win, CurrentTime);
  289. return;
  290. case XK_Return:
  291. case XK_KP_Enter:
  292. break;
  293. case XK_bracketleft:
  294. cleanup();
  295. exit(1);
  296. default:
  297. return;
  298. }
  299. else if (ev->state & Mod1Mask)
  300. switch(ksym) {
  301. case XK_g: ksym = XK_Home; break;
  302. case XK_G: ksym = XK_End; break;
  303. case XK_h: ksym = XK_Up; break;
  304. case XK_j: ksym = XK_Next; break;
  305. case XK_k: ksym = XK_Prior; break;
  306. case XK_l: ksym = XK_Down; break;
  307. default:
  308. return;
  309. }
  310. switch(ksym) {
  311. default:
  312. if (!iscntrl(*buf))
  313. insert(buf, len);
  314. break;
  315. case XK_Delete:
  316. if (text[cursor] == '\0')
  317. return;
  318. cursor = nextrune(+1);
  319. /* fallthrough */
  320. case XK_BackSpace:
  321. if (cursor == 0)
  322. return;
  323. insert(NULL, nextrune(-1) - cursor);
  324. break;
  325. case XK_End:
  326. if (text[cursor] != '\0') {
  327. cursor = strlen(text);
  328. break;
  329. }
  330. if (next) {
  331. /* jump to end of list and position items in reverse */
  332. curr = matchend;
  333. calcoffsets();
  334. curr = prev;
  335. calcoffsets();
  336. while (next && (curr = curr->right))
  337. calcoffsets();
  338. }
  339. sel = matchend;
  340. break;
  341. case XK_Escape:
  342. cleanup();
  343. exit(1);
  344. case XK_Home:
  345. if (sel == matches) {
  346. cursor = 0;
  347. break;
  348. }
  349. sel = curr = matches;
  350. calcoffsets();
  351. break;
  352. case XK_Left:
  353. if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
  354. cursor = nextrune(-1);
  355. break;
  356. }
  357. if (lines > 0)
  358. return;
  359. /* fallthrough */
  360. case XK_Up:
  361. if (sel && sel->left && (sel = sel->left)->right == curr) {
  362. curr = prev;
  363. calcoffsets();
  364. }
  365. break;
  366. case XK_Next:
  367. if (!next)
  368. return;
  369. sel = curr = next;
  370. calcoffsets();
  371. break;
  372. case XK_Prior:
  373. if (!prev)
  374. return;
  375. sel = curr = prev;
  376. calcoffsets();
  377. break;
  378. case XK_Return:
  379. case XK_KP_Enter:
  380. puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
  381. if (!(ev->state & ControlMask)) {
  382. cleanup();
  383. exit(0);
  384. }
  385. if (sel)
  386. sel->out = 1;
  387. break;
  388. case XK_Right:
  389. if (text[cursor] != '\0') {
  390. cursor = nextrune(+1);
  391. break;
  392. }
  393. if (lines > 0)
  394. return;
  395. /* fallthrough */
  396. case XK_Down:
  397. if (sel && sel->right && (sel = sel->right) == next) {
  398. curr = next;
  399. calcoffsets();
  400. }
  401. break;
  402. case XK_Tab:
  403. if (!sel)
  404. return;
  405. strncpy(text, sel->text, sizeof text - 1);
  406. text[sizeof text - 1] = '\0';
  407. cursor = strlen(text);
  408. match();
  409. break;
  410. }
  411. drawmenu();
  412. }
  413. static void
  414. paste(void)
  415. {
  416. char *p, *q;
  417. int di;
  418. unsigned long dl;
  419. Atom da;
  420. /* we have been given the current selection, now insert it into input */
  421. XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  422. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  423. insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p));
  424. XFree(p);
  425. drawmenu();
  426. }
  427. static void
  428. readstdin(void)
  429. {
  430. char buf[sizeof text], *p, *maxstr = NULL;
  431. size_t i, max = 0, size = 0;
  432. /* read each line from stdin and add it to the item list */
  433. for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
  434. if (i + 1 >= size / sizeof *items)
  435. if (!(items = realloc(items, (size += BUFSIZ))))
  436. die("cannot realloc %u bytes:", size);
  437. if ((p = strchr(buf, '\n')))
  438. *p = '\0';
  439. if (!(items[i].text = strdup(buf)))
  440. die("cannot strdup %u bytes:", strlen(buf) + 1);
  441. items[i].out = 0;
  442. if (strlen(items[i].text) > max)
  443. max = strlen(maxstr = items[i].text);
  444. }
  445. if (items)
  446. items[i].text = NULL;
  447. inputw = maxstr ? TEXTW(maxstr) : 0;
  448. lines = MIN(lines, i);
  449. }
  450. static void
  451. run(void)
  452. {
  453. XEvent ev;
  454. while (!XNextEvent(dpy, &ev)) {
  455. if (XFilterEvent(&ev, win))
  456. continue;
  457. switch(ev.type) {
  458. case Expose:
  459. if (ev.xexpose.count == 0)
  460. drw_map(drw, win, 0, 0, mw, mh);
  461. break;
  462. case KeyPress:
  463. keypress(&ev.xkey);
  464. break;
  465. case SelectionNotify:
  466. if (ev.xselection.property == utf8)
  467. paste();
  468. break;
  469. case VisibilityNotify:
  470. if (ev.xvisibility.state != VisibilityUnobscured)
  471. XRaiseWindow(dpy, win);
  472. break;
  473. }
  474. }
  475. }
  476. static void
  477. setup(void)
  478. {
  479. int x, y;
  480. XSetWindowAttributes swa;
  481. XIM xim;
  482. #ifdef XINERAMA
  483. XineramaScreenInfo *info;
  484. Window w, pw, dw, *dws;
  485. XWindowAttributes wa;
  486. int a, j, di, n, i = 0, area = 0;
  487. unsigned int du;
  488. #endif
  489. /* init appearance */
  490. scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
  491. scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
  492. scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
  493. scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
  494. scheme[SchemeOut].bg = drw_clr_create(drw, outbgcolor);
  495. scheme[SchemeOut].fg = drw_clr_create(drw, outfgcolor);
  496. clip = XInternAtom(dpy, "CLIPBOARD", False);
  497. utf8 = XInternAtom(dpy, "UTF8_STRING", False);
  498. /* calculate menu geometry */
  499. bh = drw->fonts[0]->h + 2;
  500. lines = MAX(lines, 0);
  501. mh = (lines + 1) * bh;
  502. #ifdef XINERAMA
  503. if ((info = XineramaQueryScreens(dpy, &n))) {
  504. XGetInputFocus(dpy, &w, &di);
  505. if (mon != -1 && mon < n)
  506. i = mon;
  507. else if (w != root && w != PointerRoot && w != None) {
  508. /* find top-level window containing current input focus */
  509. do {
  510. if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
  511. XFree(dws);
  512. } while (w != root && w != pw);
  513. /* find xinerama screen with which the window intersects most */
  514. if (XGetWindowAttributes(dpy, pw, &wa))
  515. for (j = 0; j < n; j++)
  516. if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
  517. area = a;
  518. i = j;
  519. }
  520. }
  521. /* no focused window is on screen, so use pointer location instead */
  522. if (mon == -1 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
  523. for (i = 0; i < n; i++)
  524. if (INTERSECT(x, y, 1, 1, info[i]))
  525. break;
  526. x = info[i].x_org;
  527. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  528. mw = info[i].width;
  529. XFree(info);
  530. } else
  531. #endif
  532. {
  533. x = 0;
  534. y = topbar ? 0 : sh - mh;
  535. mw = sw;
  536. }
  537. promptw = (prompt && *prompt) ? TEXTW(prompt) : 0;
  538. inputw = MIN(inputw, mw/3);
  539. match();
  540. /* create menu window */
  541. swa.override_redirect = True;
  542. swa.background_pixel = scheme[SchemeNorm].bg->pix;
  543. swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  544. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  545. DefaultDepth(dpy, screen), CopyFromParent,
  546. DefaultVisual(dpy, screen),
  547. CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
  548. /* open input methods */
  549. xim = XOpenIM(dpy, NULL, NULL, NULL);
  550. xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
  551. XNClientWindow, win, XNFocusWindow, win, NULL);
  552. XMapRaised(dpy, win);
  553. drw_resize(drw, mw, mh);
  554. drawmenu();
  555. }
  556. static void
  557. usage(void)
  558. {
  559. fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
  560. " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
  561. exit(1);
  562. }
  563. int
  564. main(int argc, char *argv[])
  565. {
  566. int i, fast = 0;
  567. for (i = 1; i < argc; i++)
  568. /* these options take no arguments */
  569. if (!strcmp(argv[i], "-v")) { /* prints version information */
  570. puts("dmenu-"VERSION);
  571. exit(0);
  572. } else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
  573. topbar = 0;
  574. else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
  575. fast = 1;
  576. else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
  577. fstrncmp = strncasecmp;
  578. fstrstr = cistrstr;
  579. } else if (i + 1 == argc)
  580. usage();
  581. /* these options take one argument */
  582. else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */
  583. lines = atoi(argv[++i]);
  584. else if (!strcmp(argv[i], "-m"))
  585. mon = atoi(argv[++i]);
  586. else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
  587. prompt = argv[++i];
  588. else if (!strcmp(argv[i], "-fn")) /* font or font set */
  589. fonts[0] = argv[++i];
  590. else if (!strcmp(argv[i], "-nb")) /* normal background color */
  591. normbgcolor = argv[++i];
  592. else if (!strcmp(argv[i], "-nf")) /* normal foreground color */
  593. normfgcolor = argv[++i];
  594. else if (!strcmp(argv[i], "-sb")) /* selected background color */
  595. selbgcolor = argv[++i];
  596. else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
  597. selfgcolor = argv[++i];
  598. else
  599. usage();
  600. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  601. fputs("warning: no locale support\n", stderr);
  602. if (!(dpy = XOpenDisplay(NULL)))
  603. die("cannot open display\n");
  604. screen = DefaultScreen(dpy);
  605. root = RootWindow(dpy, screen);
  606. sw = DisplayWidth(dpy, screen);
  607. sh = DisplayHeight(dpy, screen);
  608. drw = drw_create(dpy, screen, root, sw, sh);
  609. drw_load_fonts(drw, fonts, LENGTH(fonts));
  610. if (!drw->fontcount)
  611. die("no fonts could be loaded.\n");
  612. drw_setscheme(drw, &scheme[SchemeNorm]);
  613. if (fast) {
  614. grabkeyboard();
  615. readstdin();
  616. } else {
  617. readstdin();
  618. grabkeyboard();
  619. }
  620. setup();
  621. run();
  622. return 1; /* unreachable */
  623. }