dmenu.c 19 KB

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