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.

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