dmenu for lunch applications in dwm
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.

685 lines
17 KiB

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