patched suckless dmenu
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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