My dmenu build
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dmenu.c 19 KiB

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