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.

751 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
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
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
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. }
  330. }
  331. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  332. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  333. || IsPrivateKeypadKey(ksym))
  334. return;
  335. /* first check if a control mask is omitted */
  336. if(e->state & ControlMask) {
  337. switch (ksym) {
  338. default: /* ignore other control sequences */
  339. return;
  340. case XK_bracketleft:
  341. ksym = XK_Escape;
  342. break;
  343. case XK_h:
  344. case XK_H:
  345. ksym = XK_BackSpace;
  346. break;
  347. case XK_i:
  348. case XK_I:
  349. ksym = XK_Tab;
  350. break;
  351. case XK_j:
  352. case XK_J:
  353. ksym = XK_Return;
  354. break;
  355. case XK_u:
  356. case XK_U:
  357. text[0] = 0;
  358. match(text);
  359. drawmenu();
  360. return;
  361. case XK_w:
  362. case XK_W:
  363. if(len) {
  364. i = len - 1;
  365. while(i >= 0 && text[i] == ' ')
  366. text[i--] = 0;
  367. while(i >= 0 && text[i] != ' ')
  368. text[i--] = 0;
  369. match(text);
  370. drawmenu();
  371. }
  372. return;
  373. }
  374. }
  375. if(CLEANMASK(e->state) & Mod1Mask) {
  376. switch(ksym) {
  377. default: return;
  378. case XK_h:
  379. ksym = XK_Left;
  380. break;
  381. case XK_l:
  382. ksym = XK_Right;
  383. break;
  384. case XK_j:
  385. ksym = XK_Next;
  386. break;
  387. case XK_k:
  388. ksym = XK_Prior;
  389. break;
  390. case XK_g:
  391. ksym = XK_Home;
  392. break;
  393. case XK_G:
  394. ksym = XK_End;
  395. break;
  396. }
  397. }
  398. switch(ksym) {
  399. default:
  400. if(num && !iscntrl((int) buf[0])) {
  401. buf[num] = 0;
  402. if(len > 0)
  403. strncat(text, buf, sizeof text);
  404. else
  405. strncpy(text, buf, sizeof text);
  406. match(text);
  407. }
  408. break;
  409. case XK_BackSpace:
  410. if(len) {
  411. text[--len] = 0;
  412. match(text);
  413. }
  414. break;
  415. case XK_End:
  416. if(!item)
  417. return;
  418. while(next) {
  419. sel = curr = next;
  420. calcoffsets();
  421. }
  422. while(sel && sel->right)
  423. sel = sel->right;
  424. break;
  425. case XK_Escape:
  426. ret = 1;
  427. running = False;
  428. break;
  429. case XK_Home:
  430. if(!item)
  431. return;
  432. sel = curr = item;
  433. calcoffsets();
  434. break;
  435. case XK_Left:
  436. if(!(sel && sel->left))
  437. return;
  438. sel=sel->left;
  439. if(sel->right == curr) {
  440. curr = prev;
  441. calcoffsets();
  442. }
  443. break;
  444. case XK_Next:
  445. if(!next)
  446. return;
  447. sel = curr = next;
  448. calcoffsets();
  449. break;
  450. case XK_Prior:
  451. if(!prev)
  452. return;
  453. sel = curr = prev;
  454. calcoffsets();
  455. break;
  456. case XK_Return:
  457. if((e->state & ShiftMask) && text)
  458. fprintf(stdout, "%s", text);
  459. else if(sel)
  460. fprintf(stdout, "%s", sel->text);
  461. else if(text)
  462. fprintf(stdout, "%s", text);
  463. fflush(stdout);
  464. running = False;
  465. break;
  466. case XK_Right:
  467. if(!(sel && sel->right))
  468. return;
  469. sel=sel->right;
  470. if(sel == next) {
  471. curr = next;
  472. calcoffsets();
  473. }
  474. break;
  475. case XK_Tab:
  476. if(!sel)
  477. return;
  478. strncpy(text, sel->text, sizeof text);
  479. match(text);
  480. break;
  481. }
  482. drawmenu();
  483. }
  484. void
  485. match(char *pattern) {
  486. unsigned int plen;
  487. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  488. if(!pattern)
  489. return;
  490. plen = strlen(pattern);
  491. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  492. for(i = allitems; i; i = i->next)
  493. if(!fstrncmp(pattern, i->text, plen + 1))
  494. appenditem(i, &lexact, &exactend);
  495. else if(!fstrncmp(pattern, i->text, plen))
  496. appenditem(i, &lprefix, &prefixend);
  497. else if(fstrstr(i->text, pattern))
  498. appenditem(i, &lsubstr, &substrend);
  499. if(lexact) {
  500. item = lexact;
  501. itemend = exactend;
  502. }
  503. if(lprefix) {
  504. if(itemend) {
  505. itemend->right - lprefix;
  506. lprefix->left = itemend;
  507. }
  508. else
  509. item = lprefix;
  510. itemend = prefixend;
  511. }
  512. if(lsubstr) {
  513. if(itemend) {
  514. itemend->right = lsubstr;
  515. lsubstr->left = itemend;
  516. }
  517. else
  518. item = lsubstr;
  519. }
  520. curr = prev = next = sel = item;
  521. calcoffsets();
  522. }
  523. void
  524. readstdin(void) {
  525. char *p, buf[1024];
  526. unsigned int len = 0, max = 0;
  527. Item *i, *new;
  528. i = 0;
  529. while(fgets(buf, sizeof buf, stdin)) {
  530. len = strlen(buf);
  531. if (buf[len - 1] == '\n')
  532. buf[len - 1] = 0;
  533. p = estrdup(buf);
  534. if(max < len) {
  535. maxname = p;
  536. max = len;
  537. }
  538. new = emalloc(sizeof(Item));
  539. new->next = new->left = new->right = NULL;
  540. new->text = p;
  541. if(!i)
  542. allitems = new;
  543. else
  544. i->next = new;
  545. i = new;
  546. }
  547. }
  548. void
  549. run(void) {
  550. XEvent ev;
  551. /* main event loop */
  552. while(running && !XNextEvent(dpy, &ev))
  553. switch (ev.type) {
  554. default: /* ignore all crap */
  555. break;
  556. case KeyPress:
  557. kpress(&ev.xkey);
  558. break;
  559. case Expose:
  560. if(ev.xexpose.count == 0)
  561. drawmenu();
  562. break;
  563. }
  564. }
  565. void
  566. setup(int x, int y, int w) {
  567. unsigned int i, j;
  568. XModifierKeymap *modmap;
  569. XSetWindowAttributes wa;
  570. /* init modifier map */
  571. modmap = XGetModifierMapping(dpy);
  572. for(i = 0; i < 8; i++)
  573. for(j = 0; j < modmap->max_keypermod; j++) {
  574. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  575. == XKeysymToKeycode(dpy, XK_Num_Lock))
  576. numlockmask = (1 << i);
  577. }
  578. XFreeModifiermap(modmap);
  579. /* style */
  580. dc.norm[ColBG] = getcolor(normbg);
  581. dc.norm[ColFG] = getcolor(normfg);
  582. dc.sel[ColBG] = getcolor(selbg);
  583. dc.sel[ColFG] = getcolor(selfg);
  584. initfont(font);
  585. /* menu window */
  586. wa.override_redirect = 1;
  587. wa.background_pixmap = ParentRelative;
  588. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  589. mw = w ? w : DisplayWidth(dpy, screen);
  590. mh = dc.font.height + 2;
  591. if(y < 0) {
  592. if(y == (int)(unsigned int)-1)
  593. y = DisplayHeight(dpy, screen) - mh;
  594. else
  595. y = (-1 * y) - mh;
  596. }
  597. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  598. DefaultDepth(dpy, screen), CopyFromParent,
  599. DefaultVisual(dpy, screen),
  600. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  601. /* pixmap */
  602. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  603. dc.gc = XCreateGC(dpy, root, 0, 0);
  604. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  605. if(!dc.font.set)
  606. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  607. if(maxname)
  608. cmdw = textw(maxname);
  609. if(cmdw > mw / 3)
  610. cmdw = mw / 3;
  611. if(prompt)
  612. promptw = textw(prompt);
  613. if(promptw > mw / 5)
  614. promptw = mw / 5;
  615. text[0] = 0;
  616. match(text);
  617. XMapRaised(dpy, win);
  618. }
  619. unsigned int
  620. textnw(const char *text, unsigned int len) {
  621. XRectangle r;
  622. if(dc.font.set) {
  623. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  624. return r.width;
  625. }
  626. return XTextWidth(dc.font.xfont, text, len);
  627. }
  628. unsigned int
  629. textw(const char *text) {
  630. return textnw(text, strlen(text)) + dc.font.height;
  631. }
  632. int
  633. main(int argc, char *argv[]) {
  634. int x = 0, y = 0, w = 0;
  635. unsigned int i;
  636. /* command line args */
  637. for(i = 1; i < argc; i++)
  638. if(!strcmp(argv[i], "-i")) {
  639. fstrncmp = strncasecmp;
  640. fstrstr = cistrstr;
  641. }
  642. else if(!strcmp(argv[i], "-fn")) {
  643. if(++i < argc) font = argv[i];
  644. }
  645. else if(!strcmp(argv[i], "-nb")) {
  646. if(++i < argc) normbg = argv[i];
  647. }
  648. else if(!strcmp(argv[i], "-nf")) {
  649. if(++i < argc) normfg = argv[i];
  650. }
  651. else if(!strcmp(argv[i], "-p")) {
  652. if(++i < argc) prompt = argv[i];
  653. }
  654. else if(!strcmp(argv[i], "-sb")) {
  655. if(++i < argc) selbg = argv[i];
  656. }
  657. else if(!strcmp(argv[i], "-sf")) {
  658. if(++i < argc) selfg = argv[i];
  659. }
  660. else if(!strcmp(argv[i], "-x")) {
  661. if(++i < argc) x = atoi(argv[i]);
  662. }
  663. else if(!strcmp(argv[i], "-y")) {
  664. if(++i < argc) {
  665. if(!strcmp(argv[i], "-0"))
  666. y = (int)(unsigned int)-1;
  667. else
  668. y = atoi(argv[i]);
  669. }
  670. }
  671. else if(!strcmp(argv[i], "-w")) {
  672. if(++i < argc) w = atoi(argv[i]);
  673. }
  674. else if(!strcmp(argv[i], "-v"))
  675. eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
  676. else
  677. eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  678. " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
  679. " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
  680. setlocale(LC_CTYPE, "");
  681. dpy = XOpenDisplay(0);
  682. if(!dpy)
  683. eprint("dmenu: cannot open display\n");
  684. screen = DefaultScreen(dpy);
  685. root = RootWindow(dpy, screen);
  686. if(isatty(STDIN_FILENO)) {
  687. readstdin();
  688. running = grabkeyboard();
  689. }
  690. else { /* prevent keypress loss */
  691. running = grabkeyboard();
  692. readstdin();
  693. }
  694. setup(x, y, w);
  695. drawmenu();
  696. XSync(dpy, False);
  697. run();
  698. cleanup();
  699. XCloseDisplay(dpy);
  700. return ret;
  701. }