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.

748 lines
15 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
16 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <X11/Xlib.h>
  10. #include <X11/Xutil.h>
  11. #include <X11/keysym.h>
  12. /* macros */
  13. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  14. /* enums */
  15. enum { ColFG, ColBG, ColLast };
  16. /* typedefs */
  17. typedef struct {
  18. int x, y, w, h;
  19. unsigned long norm[ColLast];
  20. unsigned long sel[ColLast];
  21. Drawable drawable;
  22. GC gc;
  23. struct {
  24. XFontStruct *xfont;
  25. XFontSet set;
  26. int ascent;
  27. int descent;
  28. int height;
  29. } font;
  30. } DC; /* draw context */
  31. typedef struct Item Item;
  32. struct Item {
  33. char *text;
  34. Item *next; /* traverses all items */
  35. Item *left, *right; /* traverses items matching current search pattern */
  36. };
  37. /* forward declarations */
  38. void appenditem(Item *i, Item **list, Item **last);
  39. void calcoffsets(void);
  40. char *cistrstr(const char *s, const char *sub);
  41. void cleanup(void);
  42. void drawmenu(void);
  43. void drawtext(const char *text, unsigned long col[ColLast]);
  44. void *emalloc(unsigned int size);
  45. void eprint(const char *errstr, ...);
  46. char *estrdup(const char *str);
  47. unsigned long getcolor(const char *colstr);
  48. Bool grabkeyboard(void);
  49. void initfont(const char *fontstr);
  50. void kpress(XKeyEvent * e);
  51. void match(char *pattern);
  52. void readstdin(void);
  53. void run(void);
  54. void setup(int x, int y, int w);
  55. unsigned int textnw(const char *text, unsigned int len);
  56. unsigned int textw(const char *text);
  57. #include "config.h"
  58. /* variables */
  59. char *font = FONT;
  60. char *maxname = NULL;
  61. char *normbg = NORMBGCOLOR;
  62. char *normfg = NORMFGCOLOR;
  63. char *prompt = NULL;
  64. char *selbg = SELBGCOLOR;
  65. char *selfg = SELFGCOLOR;
  66. char text[4096];
  67. int screen;
  68. int ret = 0;
  69. unsigned int cmdw = 0;
  70. unsigned int mw, mh;
  71. unsigned int promptw = 0;
  72. unsigned int numlockmask = 0;
  73. Bool running = True;
  74. Display *dpy;
  75. DC dc = {0};
  76. Item *allitems = NULL; /* first of all items */
  77. Item *item = NULL; /* first of pattern matching items */
  78. Item *sel = NULL;
  79. Item *next = NULL;
  80. Item *prev = NULL;
  81. Item *curr = NULL;
  82. Window root, win;
  83. int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
  84. char *(*fstrstr)(const char *, const char *) = strstr;
  85. void
  86. appenditem(Item *i, Item **list, Item **last) {
  87. if(!(*last))
  88. *list = i;
  89. else
  90. (*last)->right = i;
  91. i->left = *last;
  92. i->right = NULL;
  93. *last = i;
  94. }
  95. void
  96. calcoffsets(void) {
  97. unsigned int tw, w;
  98. if(!curr)
  99. return;
  100. w = promptw + cmdw + 2 * SPACE;
  101. for(next = curr; next; next=next->right) {
  102. tw = textw(next->text);
  103. if(tw > mw / 3)
  104. tw = mw / 3;
  105. w += tw;
  106. if(w > mw)
  107. break;
  108. }
  109. w = promptw + cmdw + 2 * SPACE;
  110. for(prev = curr; prev && prev->left; prev=prev->left) {
  111. tw = textw(prev->left->text);
  112. if(tw > mw / 3)
  113. tw = mw / 3;
  114. w += tw;
  115. if(w > mw)
  116. break;
  117. }
  118. }
  119. char *
  120. cistrstr(const char *s, const char *sub) {
  121. int c, csub;
  122. unsigned int len;
  123. if(!sub)
  124. return (char *)s;
  125. if((c = *sub++) != 0) {
  126. c = tolower(c);
  127. len = strlen(sub);
  128. do {
  129. do {
  130. if((csub = *s++) == 0)
  131. return (NULL);
  132. }
  133. while(tolower(csub) != c);
  134. }
  135. while(strncasecmp(s, sub, len) != 0);
  136. s--;
  137. }
  138. return (char *)s;
  139. }
  140. void
  141. cleanup(void) {
  142. Item *itm;
  143. while(allitems) {
  144. itm = allitems->next;
  145. free(allitems->text);
  146. free(allitems);
  147. allitems = itm;
  148. }
  149. if(dc.font.set)
  150. XFreeFontSet(dpy, dc.font.set);
  151. else
  152. XFreeFont(dpy, dc.font.xfont);
  153. XFreePixmap(dpy, dc.drawable);
  154. XFreeGC(dpy, dc.gc);
  155. XDestroyWindow(dpy, win);
  156. XUngrabKeyboard(dpy, CurrentTime);
  157. }
  158. void
  159. drawmenu(void) {
  160. Item *i;
  161. dc.x = 0;
  162. dc.y = 0;
  163. dc.w = mw;
  164. dc.h = mh;
  165. drawtext(NULL, dc.norm);
  166. /* print prompt? */
  167. if(promptw) {
  168. dc.w = promptw;
  169. drawtext(prompt, dc.sel);
  170. }
  171. dc.x += promptw;
  172. dc.w = mw - promptw;
  173. /* print command */
  174. if(cmdw && item)
  175. dc.w = cmdw;
  176. drawtext(text[0] ? text : NULL, dc.norm);
  177. dc.x += cmdw;
  178. if(curr) {
  179. dc.w = SPACE;
  180. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  181. dc.x += dc.w;
  182. /* determine maximum items */
  183. for(i = curr; i != next; i=i->right) {
  184. dc.w = textw(i->text);
  185. if(dc.w > mw / 3)
  186. dc.w = mw / 3;
  187. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  188. dc.x += dc.w;
  189. }
  190. dc.x = mw - SPACE;
  191. dc.w = SPACE;
  192. drawtext(next ? ">" : NULL, dc.norm);
  193. }
  194. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  195. XFlush(dpy);
  196. }
  197. void
  198. drawtext(const char *text, unsigned long col[ColLast]) {
  199. int x, y, w, h;
  200. static char buf[256];
  201. unsigned int len, olen;
  202. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  203. XSetForeground(dpy, dc.gc, col[ColBG]);
  204. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  205. if(!text)
  206. return;
  207. w = 0;
  208. olen = len = strlen(text);
  209. if(len >= sizeof buf)
  210. len = sizeof buf - 1;
  211. memcpy(buf, text, len);
  212. buf[len] = 0;
  213. h = dc.font.ascent + dc.font.descent;
  214. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  215. x = dc.x + (h / 2);
  216. /* shorten text if necessary */
  217. while(len && (w = textnw(buf, len)) > dc.w - h)
  218. buf[--len] = 0;
  219. if(len < olen) {
  220. if(len > 1)
  221. buf[len - 1] = '.';
  222. if(len > 2)
  223. buf[len - 2] = '.';
  224. if(len > 3)
  225. buf[len - 3] = '.';
  226. }
  227. if(w > dc.w)
  228. return; /* too long */
  229. XSetForeground(dpy, dc.gc, col[ColFG]);
  230. if(dc.font.set)
  231. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  232. else
  233. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  234. }
  235. void *
  236. emalloc(unsigned int size) {
  237. void *res = malloc(size);
  238. if(!res)
  239. eprint("fatal: could not malloc() %u bytes\n", size);
  240. return res;
  241. }
  242. void
  243. eprint(const char *errstr, ...) {
  244. va_list ap;
  245. va_start(ap, errstr);
  246. vfprintf(stderr, errstr, ap);
  247. va_end(ap);
  248. exit(EXIT_FAILURE);
  249. }
  250. char *
  251. estrdup(const char *str) {
  252. void *res = strdup(str);
  253. if(!res)
  254. eprint("fatal: could not malloc() %u bytes\n", strlen(str));
  255. return res;
  256. }
  257. unsigned long
  258. getcolor(const char *colstr) {
  259. Colormap cmap = DefaultColormap(dpy, screen);
  260. XColor color;
  261. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  262. eprint("error, cannot allocate color '%s'\n", colstr);
  263. return color.pixel;
  264. }
  265. Bool
  266. grabkeyboard(void) {
  267. unsigned int len;
  268. for(len = 1000; len; len--) {
  269. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  270. == GrabSuccess)
  271. break;
  272. usleep(1000);
  273. }
  274. return len > 0;
  275. }
  276. void
  277. initfont(const char *fontstr) {
  278. char *def, **missing;
  279. int i, n;
  280. if(!fontstr || fontstr[0] == '\0')
  281. eprint("error, cannot load font: '%s'\n", fontstr);
  282. missing = NULL;
  283. if(dc.font.set)
  284. XFreeFontSet(dpy, dc.font.set);
  285. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  286. if(missing)
  287. XFreeStringList(missing);
  288. if(dc.font.set) {
  289. XFontSetExtents *font_extents;
  290. XFontStruct **xfonts;
  291. char **font_names;
  292. dc.font.ascent = dc.font.descent = 0;
  293. font_extents = XExtentsOfFontSet(dc.font.set);
  294. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  295. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  296. if(dc.font.ascent < (*xfonts)->ascent)
  297. dc.font.ascent = (*xfonts)->ascent;
  298. if(dc.font.descent < (*xfonts)->descent)
  299. dc.font.descent = (*xfonts)->descent;
  300. xfonts++;
  301. }
  302. }
  303. else {
  304. if(dc.font.xfont)
  305. XFreeFont(dpy, dc.font.xfont);
  306. dc.font.xfont = NULL;
  307. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  308. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  309. eprint("error, cannot load font: '%s'\n", fontstr);
  310. dc.font.ascent = dc.font.xfont->ascent;
  311. dc.font.descent = dc.font.xfont->descent;
  312. }
  313. dc.font.height = dc.font.ascent + dc.font.descent;
  314. }
  315. void
  316. kpress(XKeyEvent * e) {
  317. char buf[32];
  318. int i, num;
  319. unsigned int len;
  320. KeySym ksym;
  321. len = strlen(text);
  322. buf[0] = 0;
  323. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  324. if(IsKeypadKey(ksym))
  325. if(ksym == XK_KP_Enter)
  326. ksym = XK_Return;
  327. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  328. ksym = (ksym - XK_KP_0) + XK_0;
  329. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  330. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  331. || IsPrivateKeypadKey(ksym))
  332. return;
  333. /* first check if a control mask is omitted */
  334. if(e->state & ControlMask) {
  335. switch (ksym) {
  336. default: /* ignore other control sequences */
  337. return;
  338. case XK_bracketleft:
  339. ksym = XK_Escape;
  340. break;
  341. case XK_h:
  342. case XK_H:
  343. ksym = XK_BackSpace;
  344. break;
  345. case XK_i:
  346. case XK_I:
  347. ksym = XK_Tab;
  348. break;
  349. case XK_j:
  350. case XK_J:
  351. ksym = XK_Return;
  352. break;
  353. case XK_u:
  354. case XK_U:
  355. text[0] = 0;
  356. match(text);
  357. drawmenu();
  358. return;
  359. case XK_w:
  360. case XK_W:
  361. if(len) {
  362. i = len - 1;
  363. while(i >= 0 && text[i] == ' ')
  364. text[i--] = 0;
  365. while(i >= 0 && text[i] != ' ')
  366. text[i--] = 0;
  367. match(text);
  368. drawmenu();
  369. }
  370. return;
  371. }
  372. }
  373. if(CLEANMASK(e->state) & Mod1Mask) {
  374. switch(ksym) {
  375. default: return;
  376. case XK_h:
  377. ksym = XK_Left;
  378. break;
  379. case XK_l:
  380. ksym = XK_Right;
  381. break;
  382. case XK_j:
  383. ksym = XK_Next;
  384. break;
  385. case XK_k:
  386. ksym = XK_Prior;
  387. break;
  388. case XK_g:
  389. ksym = XK_Home;
  390. break;
  391. case XK_G:
  392. ksym = XK_End;
  393. break;
  394. }
  395. }
  396. switch(ksym) {
  397. default:
  398. if(num && !iscntrl((int) buf[0])) {
  399. buf[num] = 0;
  400. if(len > 0)
  401. strncat(text, buf, sizeof text);
  402. else
  403. strncpy(text, buf, sizeof text);
  404. match(text);
  405. }
  406. break;
  407. case XK_BackSpace:
  408. if(len) {
  409. text[--len] = 0;
  410. match(text);
  411. }
  412. break;
  413. case XK_End:
  414. if(!item)
  415. return;
  416. while(next) {
  417. sel = curr = next;
  418. calcoffsets();
  419. }
  420. while(sel && sel->right)
  421. sel = sel->right;
  422. break;
  423. case XK_Escape:
  424. ret = 1;
  425. running = False;
  426. break;
  427. case XK_Home:
  428. if(!item)
  429. return;
  430. sel = curr = item;
  431. calcoffsets();
  432. break;
  433. case XK_Left:
  434. if(!(sel && sel->left))
  435. return;
  436. sel=sel->left;
  437. if(sel->right == curr) {
  438. curr = prev;
  439. calcoffsets();
  440. }
  441. break;
  442. case XK_Next:
  443. if(!next)
  444. return;
  445. sel = curr = next;
  446. calcoffsets();
  447. break;
  448. case XK_Prior:
  449. if(!prev)
  450. return;
  451. sel = curr = prev;
  452. calcoffsets();
  453. break;
  454. case XK_Return:
  455. if((e->state & ShiftMask) && text)
  456. fprintf(stdout, "%s", text);
  457. else if(sel)
  458. fprintf(stdout, "%s", sel->text);
  459. else if(text)
  460. fprintf(stdout, "%s", text);
  461. fflush(stdout);
  462. running = False;
  463. break;
  464. case XK_Right:
  465. if(!(sel && sel->right))
  466. return;
  467. sel=sel->right;
  468. if(sel == next) {
  469. curr = next;
  470. calcoffsets();
  471. }
  472. break;
  473. case XK_Tab:
  474. if(!sel)
  475. return;
  476. strncpy(text, sel->text, sizeof text);
  477. match(text);
  478. break;
  479. }
  480. drawmenu();
  481. }
  482. void
  483. match(char *pattern) {
  484. unsigned int plen;
  485. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  486. if(!pattern)
  487. return;
  488. plen = strlen(pattern);
  489. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  490. for(i = allitems; i; i = i->next)
  491. if(!fstrncmp(pattern, i->text, plen + 1))
  492. appenditem(i, &lexact, &exactend);
  493. else if(!fstrncmp(pattern, i->text, plen))
  494. appenditem(i, &lprefix, &prefixend);
  495. else if(fstrstr(i->text, pattern))
  496. appenditem(i, &lsubstr, &substrend);
  497. if(lexact) {
  498. item = lexact;
  499. itemend = exactend;
  500. }
  501. if(lprefix) {
  502. if(itemend) {
  503. itemend->right = lprefix;
  504. lprefix->left = itemend;
  505. }
  506. else
  507. item = lprefix;
  508. itemend = prefixend;
  509. }
  510. if(lsubstr) {
  511. if(itemend) {
  512. itemend->right = lsubstr;
  513. lsubstr->left = itemend;
  514. }
  515. else
  516. item = lsubstr;
  517. }
  518. curr = prev = next = sel = item;
  519. calcoffsets();
  520. }
  521. void
  522. readstdin(void) {
  523. char *p, buf[1024];
  524. unsigned int len = 0, max = 0;
  525. Item *i, *new;
  526. i = 0;
  527. while(fgets(buf, sizeof buf, stdin)) {
  528. len = strlen(buf);
  529. if (buf[len - 1] == '\n')
  530. buf[len - 1] = 0;
  531. p = estrdup(buf);
  532. if(max < len) {
  533. maxname = p;
  534. max = len;
  535. }
  536. new = emalloc(sizeof(Item));
  537. new->next = new->left = new->right = NULL;
  538. new->text = p;
  539. if(!i)
  540. allitems = new;
  541. else
  542. i->next = new;
  543. i = new;
  544. }
  545. }
  546. void
  547. run(void) {
  548. XEvent ev;
  549. /* main event loop */
  550. while(running && !XNextEvent(dpy, &ev))
  551. switch (ev.type) {
  552. default: /* ignore all crap */
  553. break;
  554. case KeyPress:
  555. kpress(&ev.xkey);
  556. break;
  557. case Expose:
  558. if(ev.xexpose.count == 0)
  559. drawmenu();
  560. break;
  561. }
  562. }
  563. void
  564. setup(int x, int y, int w) {
  565. unsigned int i, j;
  566. XModifierKeymap *modmap;
  567. XSetWindowAttributes wa;
  568. /* init modifier map */
  569. modmap = XGetModifierMapping(dpy);
  570. for(i = 0; i < 8; i++)
  571. for(j = 0; j < modmap->max_keypermod; j++) {
  572. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  573. == XKeysymToKeycode(dpy, XK_Num_Lock))
  574. numlockmask = (1 << i);
  575. }
  576. XFreeModifiermap(modmap);
  577. /* style */
  578. dc.norm[ColBG] = getcolor(normbg);
  579. dc.norm[ColFG] = getcolor(normfg);
  580. dc.sel[ColBG] = getcolor(selbg);
  581. dc.sel[ColFG] = getcolor(selfg);
  582. initfont(font);
  583. /* menu window */
  584. wa.override_redirect = 1;
  585. wa.background_pixmap = ParentRelative;
  586. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  587. mw = w ? w : DisplayWidth(dpy, screen);
  588. mh = dc.font.height + 2;
  589. if(y < 0) {
  590. if(y == ((~(unsigned int)0)/2)+1)
  591. y = DisplayHeight(dpy, screen) - mh;
  592. else
  593. y = (-1 * y) - mh;
  594. }
  595. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  596. DefaultDepth(dpy, screen), CopyFromParent,
  597. DefaultVisual(dpy, screen),
  598. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  599. /* pixmap */
  600. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  601. dc.gc = XCreateGC(dpy, root, 0, 0);
  602. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  603. if(!dc.font.set)
  604. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  605. if(maxname)
  606. cmdw = textw(maxname);
  607. if(cmdw > mw / 3)
  608. cmdw = mw / 3;
  609. if(prompt)
  610. promptw = textw(prompt);
  611. if(promptw > mw / 5)
  612. promptw = mw / 5;
  613. text[0] = 0;
  614. match(text);
  615. XMapRaised(dpy, win);
  616. }
  617. unsigned int
  618. textnw(const char *text, unsigned int len) {
  619. XRectangle r;
  620. if(dc.font.set) {
  621. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  622. return r.width;
  623. }
  624. return XTextWidth(dc.font.xfont, text, len);
  625. }
  626. unsigned int
  627. textw(const char *text) {
  628. return textnw(text, strlen(text)) + dc.font.height;
  629. }
  630. int
  631. main(int argc, char *argv[]) {
  632. int x = 0, y = 0, w = 0;
  633. unsigned int i;
  634. /* command line args */
  635. for(i = 1; i < argc; i++)
  636. if(!strcmp(argv[i], "-i")) {
  637. fstrncmp = strncasecmp;
  638. fstrstr = cistrstr;
  639. }
  640. else if(!strcmp(argv[i], "-fn")) {
  641. if(++i < argc) font = argv[i];
  642. }
  643. else if(!strcmp(argv[i], "-nb")) {
  644. if(++i < argc) normbg = argv[i];
  645. }
  646. else if(!strcmp(argv[i], "-nf")) {
  647. if(++i < argc) normfg = argv[i];
  648. }
  649. else if(!strcmp(argv[i], "-p")) {
  650. if(++i < argc) prompt = argv[i];
  651. }
  652. else if(!strcmp(argv[i], "-sb")) {
  653. if(++i < argc) selbg = argv[i];
  654. }
  655. else if(!strcmp(argv[i], "-sf")) {
  656. if(++i < argc) selfg = argv[i];
  657. }
  658. else if(!strcmp(argv[i], "-x")) {
  659. if(++i < argc) x = atoi(argv[i]);
  660. }
  661. else if(!strcmp(argv[i], "-y")) {
  662. if(++i < argc)
  663. if(!strcmp(argv[i], "-0"))
  664. y = ((~(unsigned int)0)/2)+1;
  665. else
  666. y = atoi(argv[i]);
  667. }
  668. else if(!strcmp(argv[i], "-w")) {
  669. if(++i < argc) w = atoi(argv[i]);
  670. }
  671. else if(!strcmp(argv[i], "-v"))
  672. eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
  673. else
  674. eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  675. " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
  676. " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
  677. setlocale(LC_CTYPE, "");
  678. dpy = XOpenDisplay(0);
  679. if(!dpy)
  680. eprint("dmenu: cannot open display\n");
  681. screen = DefaultScreen(dpy);
  682. root = RootWindow(dpy, screen);
  683. if(isatty(STDIN_FILENO)) {
  684. readstdin();
  685. running = grabkeyboard();
  686. }
  687. else { /* prevent keypress loss */
  688. running = grabkeyboard();
  689. readstdin();
  690. }
  691. setup(x, y, w);
  692. drawmenu();
  693. XSync(dpy, False);
  694. run();
  695. cleanup();
  696. XCloseDisplay(dpy);
  697. return ret;
  698. }