My build of dmenu from suckless.org.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

810 lignes
19 KiB

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