1
0

dmenu.c 19 KB

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