dmenu.c.orig 20 KB

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