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.

757 lines
18 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
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
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
18 years ago
9 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
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 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. movewordedge(int dir)
  255. {
  256. if (dir < 0) { /* move cursor to the start of the word*/
  257. while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
  258. cursor = nextrune(-1);
  259. while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
  260. cursor = nextrune(-1);
  261. } else { /* move cursor to the end of the word */
  262. while (text[cursor] && strchr(worddelimiters, text[cursor]))
  263. cursor = nextrune(+1);
  264. while (text[cursor] && !strchr(worddelimiters, text[cursor]))
  265. cursor = nextrune(+1);
  266. }
  267. }
  268. static void
  269. keypress(XKeyEvent *ev)
  270. {
  271. char buf[32];
  272. int len;
  273. KeySym ksym;
  274. Status status;
  275. len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
  276. switch (status) {
  277. default: /* XLookupNone, XBufferOverflow */
  278. return;
  279. case XLookupChars:
  280. goto insert;
  281. case XLookupKeySym:
  282. case XLookupBoth:
  283. break;
  284. }
  285. if (ev->state & ControlMask) {
  286. switch(ksym) {
  287. case XK_a: ksym = XK_Home; break;
  288. case XK_b: ksym = XK_Left; break;
  289. case XK_c: ksym = XK_Escape; break;
  290. case XK_d: ksym = XK_Delete; break;
  291. case XK_e: ksym = XK_End; break;
  292. case XK_f: ksym = XK_Right; break;
  293. case XK_g: ksym = XK_Escape; break;
  294. case XK_h: ksym = XK_BackSpace; break;
  295. case XK_i: ksym = XK_Tab; break;
  296. case XK_j: /* fallthrough */
  297. case XK_J: /* fallthrough */
  298. case XK_m: /* fallthrough */
  299. case XK_M: ksym = XK_Return; ev->state &= ~ControlMask; break;
  300. case XK_n: ksym = XK_Down; break;
  301. case XK_p: ksym = XK_Up; break;
  302. case XK_k: /* delete right */
  303. text[cursor] = '\0';
  304. match();
  305. break;
  306. case XK_u: /* delete left */
  307. insert(NULL, 0 - cursor);
  308. break;
  309. case XK_w: /* delete word */
  310. while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
  311. insert(NULL, nextrune(-1) - cursor);
  312. while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
  313. insert(NULL, nextrune(-1) - cursor);
  314. break;
  315. case XK_y: /* paste selection */
  316. case XK_Y:
  317. XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY,
  318. utf8, utf8, win, CurrentTime);
  319. return;
  320. case XK_Left:
  321. movewordedge(-1);
  322. goto draw;
  323. case XK_Right:
  324. movewordedge(+1);
  325. goto draw;
  326. case XK_Return:
  327. case XK_KP_Enter:
  328. break;
  329. case XK_bracketleft:
  330. cleanup();
  331. exit(1);
  332. default:
  333. return;
  334. }
  335. } else if (ev->state & Mod1Mask) {
  336. switch(ksym) {
  337. case XK_b:
  338. movewordedge(-1);
  339. goto draw;
  340. case XK_f:
  341. movewordedge(+1);
  342. goto draw;
  343. case XK_g: ksym = XK_Home; break;
  344. case XK_G: ksym = XK_End; break;
  345. case XK_h: ksym = XK_Up; break;
  346. case XK_j: ksym = XK_Next; break;
  347. case XK_k: ksym = XK_Prior; break;
  348. case XK_l: ksym = XK_Down; break;
  349. default:
  350. return;
  351. }
  352. }
  353. switch(ksym) {
  354. default:
  355. insert:
  356. if (!iscntrl(*buf))
  357. insert(buf, len);
  358. break;
  359. case XK_Delete:
  360. if (text[cursor] == '\0')
  361. return;
  362. cursor = nextrune(+1);
  363. /* fallthrough */
  364. case XK_BackSpace:
  365. if (cursor == 0)
  366. return;
  367. insert(NULL, nextrune(-1) - cursor);
  368. break;
  369. case XK_End:
  370. if (text[cursor] != '\0') {
  371. cursor = strlen(text);
  372. break;
  373. }
  374. if (next) {
  375. /* jump to end of list and position items in reverse */
  376. curr = matchend;
  377. calcoffsets();
  378. curr = prev;
  379. calcoffsets();
  380. while (next && (curr = curr->right))
  381. calcoffsets();
  382. }
  383. sel = matchend;
  384. break;
  385. case XK_Escape:
  386. cleanup();
  387. exit(1);
  388. case XK_Home:
  389. if (sel == matches) {
  390. cursor = 0;
  391. break;
  392. }
  393. sel = curr = matches;
  394. calcoffsets();
  395. break;
  396. case XK_Left:
  397. if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
  398. cursor = nextrune(-1);
  399. break;
  400. }
  401. if (lines > 0)
  402. return;
  403. /* fallthrough */
  404. case XK_Up:
  405. if (sel && sel->left && (sel = sel->left)->right == curr) {
  406. curr = prev;
  407. calcoffsets();
  408. }
  409. break;
  410. case XK_Next:
  411. if (!next)
  412. return;
  413. sel = curr = next;
  414. calcoffsets();
  415. break;
  416. case XK_Prior:
  417. if (!prev)
  418. return;
  419. sel = curr = prev;
  420. calcoffsets();
  421. break;
  422. case XK_Return:
  423. case XK_KP_Enter:
  424. puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
  425. if (!(ev->state & ControlMask)) {
  426. cleanup();
  427. exit(0);
  428. }
  429. if (sel)
  430. sel->out = 1;
  431. break;
  432. case XK_Right:
  433. if (text[cursor] != '\0') {
  434. cursor = nextrune(+1);
  435. break;
  436. }
  437. if (lines > 0)
  438. return;
  439. /* fallthrough */
  440. case XK_Down:
  441. if (sel && sel->right && (sel = sel->right) == next) {
  442. curr = next;
  443. calcoffsets();
  444. }
  445. break;
  446. case XK_Tab:
  447. if (!sel)
  448. return;
  449. strncpy(text, sel->text, sizeof text - 1);
  450. text[sizeof text - 1] = '\0';
  451. cursor = strlen(text);
  452. match();
  453. break;
  454. }
  455. draw:
  456. drawmenu();
  457. }
  458. static void
  459. paste(void)
  460. {
  461. char *p, *q;
  462. int di;
  463. unsigned long dl;
  464. Atom da;
  465. /* we have been given the current selection, now insert it into input */
  466. if (XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  467. utf8, &da, &di, &dl, &dl, (unsigned char **)&p)
  468. == Success && p) {
  469. insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p));
  470. XFree(p);
  471. }
  472. drawmenu();
  473. }
  474. static void
  475. readstdin(void)
  476. {
  477. char buf[sizeof text], *p;
  478. size_t i, imax = 0, size = 0;
  479. unsigned int tmpmax = 0;
  480. /* read each line from stdin and add it to the item list */
  481. for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
  482. if (i + 1 >= size / sizeof *items)
  483. if (!(items = realloc(items, (size += BUFSIZ))))
  484. die("cannot realloc %u bytes:", size);
  485. if ((p = strchr(buf, '\n')))
  486. *p = '\0';
  487. if (!(items[i].text = strdup(buf)))
  488. die("cannot strdup %u bytes:", strlen(buf) + 1);
  489. items[i].out = 0;
  490. drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
  491. if (tmpmax > inputw) {
  492. inputw = tmpmax;
  493. imax = i;
  494. }
  495. }
  496. if (items)
  497. items[i].text = NULL;
  498. inputw = items ? TEXTW(items[imax].text) : 0;
  499. lines = MIN(lines, i);
  500. }
  501. static void
  502. run(void)
  503. {
  504. XEvent ev;
  505. while (!XNextEvent(dpy, &ev)) {
  506. if (XFilterEvent(&ev, win))
  507. continue;
  508. switch(ev.type) {
  509. case Expose:
  510. if (ev.xexpose.count == 0)
  511. drw_map(drw, win, 0, 0, mw, mh);
  512. break;
  513. case FocusIn:
  514. /* regrab focus from parent window */
  515. if (ev.xfocus.window != win)
  516. grabfocus();
  517. break;
  518. case KeyPress:
  519. keypress(&ev.xkey);
  520. break;
  521. case SelectionNotify:
  522. if (ev.xselection.property == utf8)
  523. paste();
  524. break;
  525. case VisibilityNotify:
  526. if (ev.xvisibility.state != VisibilityUnobscured)
  527. XRaiseWindow(dpy, win);
  528. break;
  529. }
  530. }
  531. }
  532. static void
  533. setup(void)
  534. {
  535. int x, y, i, j;
  536. unsigned int du;
  537. XSetWindowAttributes swa;
  538. XIM xim;
  539. Window w, dw, *dws;
  540. XWindowAttributes wa;
  541. XClassHint ch = {"dmenu", "dmenu"};
  542. #ifdef XINERAMA
  543. XineramaScreenInfo *info;
  544. Window pw;
  545. int a, di, n, area = 0;
  546. #endif
  547. /* init appearance */
  548. for (j = 0; j < SchemeLast; j++)
  549. scheme[j] = drw_scm_create(drw, colors[j], 2);
  550. clip = XInternAtom(dpy, "CLIPBOARD", False);
  551. utf8 = XInternAtom(dpy, "UTF8_STRING", False);
  552. /* calculate menu geometry */
  553. bh = drw->fonts->h + 2;
  554. lines = MAX(lines, 0);
  555. mh = (lines + 1) * bh;
  556. #ifdef XINERAMA
  557. i = 0;
  558. if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
  559. XGetInputFocus(dpy, &w, &di);
  560. if (mon >= 0 && mon < n)
  561. i = mon;
  562. else if (w != root && w != PointerRoot && w != None) {
  563. /* find top-level window containing current input focus */
  564. do {
  565. if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
  566. XFree(dws);
  567. } while (w != root && w != pw);
  568. /* find xinerama screen with which the window intersects most */
  569. if (XGetWindowAttributes(dpy, pw, &wa))
  570. for (j = 0; j < n; j++)
  571. if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
  572. area = a;
  573. i = j;
  574. }
  575. }
  576. /* no focused window is on screen, so use pointer location instead */
  577. if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
  578. for (i = 0; i < n; i++)
  579. if (INTERSECT(x, y, 1, 1, info[i]))
  580. break;
  581. x = info[i].x_org;
  582. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  583. mw = info[i].width;
  584. XFree(info);
  585. } else
  586. #endif
  587. {
  588. if (!XGetWindowAttributes(dpy, parentwin, &wa))
  589. die("could not get embedding window attributes: 0x%lx",
  590. parentwin);
  591. x = 0;
  592. y = topbar ? 0 : wa.height - mh;
  593. mw = wa.width;
  594. }
  595. promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
  596. inputw = MIN(inputw, mw/3);
  597. match();
  598. /* create menu window */
  599. swa.override_redirect = True;
  600. swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  601. swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  602. win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
  603. CopyFromParent, CopyFromParent, CopyFromParent,
  604. CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
  605. XSetClassHint(dpy, win, &ch);
  606. /* open input methods */
  607. xim = XOpenIM(dpy, NULL, NULL, NULL);
  608. xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
  609. XNClientWindow, win, XNFocusWindow, win, NULL);
  610. XMapRaised(dpy, win);
  611. if (embed) {
  612. XSelectInput(dpy, parentwin, FocusChangeMask);
  613. if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
  614. for (i = 0; i < du && dws[i] != win; ++i)
  615. XSelectInput(dpy, dws[i], FocusChangeMask);
  616. XFree(dws);
  617. }
  618. grabfocus();
  619. }
  620. drw_resize(drw, mw, mh);
  621. drawmenu();
  622. }
  623. static void
  624. usage(void)
  625. {
  626. fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
  627. " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
  628. exit(1);
  629. }
  630. int
  631. main(int argc, char *argv[])
  632. {
  633. XWindowAttributes wa;
  634. int i, fast = 0;
  635. for (i = 1; i < argc; i++)
  636. /* these options take no arguments */
  637. if (!strcmp(argv[i], "-v")) { /* prints version information */
  638. puts("dmenu-"VERSION);
  639. exit(0);
  640. } else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
  641. topbar = 0;
  642. else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
  643. fast = 1;
  644. else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
  645. fstrncmp = strncasecmp;
  646. fstrstr = cistrstr;
  647. } else if (i + 1 == argc)
  648. usage();
  649. /* these options take one argument */
  650. else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */
  651. lines = atoi(argv[++i]);
  652. else if (!strcmp(argv[i], "-m"))
  653. mon = atoi(argv[++i]);
  654. else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
  655. prompt = argv[++i];
  656. else if (!strcmp(argv[i], "-fn")) /* font or font set */
  657. fonts[0] = argv[++i];
  658. else if (!strcmp(argv[i], "-nb")) /* normal background color */
  659. colors[SchemeNorm][ColBg] = argv[++i];
  660. else if (!strcmp(argv[i], "-nf")) /* normal foreground color */
  661. colors[SchemeNorm][ColFg] = argv[++i];
  662. else if (!strcmp(argv[i], "-sb")) /* selected background color */
  663. colors[SchemeSel][ColBg] = argv[++i];
  664. else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
  665. colors[SchemeSel][ColFg] = argv[++i];
  666. else if (!strcmp(argv[i], "-w")) /* embedding window id */
  667. embed = argv[++i];
  668. else
  669. usage();
  670. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  671. fputs("warning: no locale support\n", stderr);
  672. if (!(dpy = XOpenDisplay(NULL)))
  673. die("cannot open display");
  674. screen = DefaultScreen(dpy);
  675. root = RootWindow(dpy, screen);
  676. if (!embed || !(parentwin = strtol(embed, NULL, 0)))
  677. parentwin = root;
  678. if (!XGetWindowAttributes(dpy, parentwin, &wa))
  679. die("could not get embedding window attributes: 0x%lx",
  680. parentwin);
  681. drw = drw_create(dpy, screen, root, wa.width, wa.height);
  682. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  683. die("no fonts could be loaded.");
  684. lrpad = drw->fonts->h;
  685. if (fast) {
  686. grabkeyboard();
  687. readstdin();
  688. } else {
  689. readstdin();
  690. grabkeyboard();
  691. }
  692. setup();
  693. run();
  694. return 1; /* unreachable */
  695. }