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 17 KiB

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