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
16 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
18 years ago
18 years ago
18 years ago
16 years ago
18 years ago
17 years ago
16 years ago
17 years ago
17 years ago
16 years ago
18 years ago
18 years ago
16 years ago
18 years ago
18 years ago
16 years ago
18 years ago
18 years ago
17 years ago
16 years ago
16 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
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
16 years ago
16 years ago
18 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
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
16 years ago
18 years ago
18 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
16 years ago
17 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
16 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 <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <strings.h>
  9. #include <unistd.h>
  10. #include <X11/keysym.h>
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #ifdef XINERAMA
  14. #include <X11/extensions/Xinerama.h>
  15. #endif
  16. /* macros */
  17. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  18. /* enums */
  19. enum { ColFG, ColBG, ColLast };
  20. /* typedefs */
  21. typedef unsigned int uint;
  22. typedef unsigned long ulong;
  23. typedef struct {
  24. int x, y, w, h;
  25. ulong norm[ColLast];
  26. ulong sel[ColLast];
  27. Drawable drawable;
  28. GC gc;
  29. struct {
  30. XFontStruct *xfont;
  31. XFontSet set;
  32. int ascent;
  33. int descent;
  34. int height;
  35. } font;
  36. } DC; /* draw context */
  37. typedef struct Item Item;
  38. struct Item {
  39. char *text;
  40. Item *next; /* traverses all items */
  41. Item *left, *right; /* traverses items matching current search pattern */
  42. };
  43. /* forward declarations */
  44. static void appenditem(Item *i, Item **list, Item **last);
  45. static void calcoffsets(void);
  46. static char *cistrstr(const char *s, const char *sub);
  47. static void cleanup(void);
  48. static void drawmenu(void);
  49. static void drawtext(const char *text, ulong col[ColLast]);
  50. static void *emalloc(uint size);
  51. static void eprint(const char *errstr, ...);
  52. static ulong getcolor(const char *colstr);
  53. static Bool grabkeyboard(void);
  54. static void initfont(const char *fontstr);
  55. static void kpress(XKeyEvent * e);
  56. static void match(char *pattern);
  57. static void readstdin(void);
  58. static void run(void);
  59. static void setup(Bool topbar);
  60. static int textnw(const char *text, uint len);
  61. static int textw(const char *text);
  62. #include "config.h"
  63. /* variables */
  64. static char *font = FONT;
  65. static char *maxname = NULL;
  66. static char *normbg = NORMBGCOLOR;
  67. static char *normfg = NORMFGCOLOR;
  68. static char *prompt = NULL;
  69. static char *selbg = SELBGCOLOR;
  70. static char *selfg = SELFGCOLOR;
  71. static char text[4096];
  72. static int cmdw = 0;
  73. static int promptw = 0;
  74. static int ret = 0;
  75. static int screen;
  76. static uint mw, mh;
  77. static uint numlockmask = 0;
  78. static Bool running = True;
  79. static Display *dpy;
  80. static DC dc = {0};
  81. static Item *allitems = NULL; /* first of all items */
  82. static Item *item = NULL; /* first of pattern matching items */
  83. static Item *sel = NULL;
  84. static Item *next = NULL;
  85. static Item *prev = NULL;
  86. static Item *curr = NULL;
  87. static Window root, win;
  88. static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
  89. static char *(*fstrstr)(const char *, const char *) = strstr;
  90. void
  91. appenditem(Item *i, Item **list, Item **last) {
  92. if(!(*last))
  93. *list = i;
  94. else
  95. (*last)->right = i;
  96. i->left = *last;
  97. i->right = NULL;
  98. *last = i;
  99. }
  100. void
  101. calcoffsets(void) {
  102. int tw;
  103. uint w;
  104. if(!curr)
  105. return;
  106. w = promptw + cmdw + 2 * spaceitem;
  107. for(next = curr; next; next=next->right) {
  108. tw = textw(next->text);
  109. if(tw > mw / 3)
  110. tw = mw / 3;
  111. w += tw;
  112. if(w > mw)
  113. break;
  114. }
  115. w = promptw + cmdw + 2 * spaceitem;
  116. for(prev = curr; prev && prev->left; prev=prev->left) {
  117. tw = textw(prev->left->text);
  118. if(tw > mw / 3)
  119. tw = mw / 3;
  120. w += tw;
  121. if(w > mw)
  122. break;
  123. }
  124. }
  125. char *
  126. cistrstr(const char *s, const char *sub) {
  127. int c, csub;
  128. uint len;
  129. if(!sub)
  130. return (char *)s;
  131. if((c = *sub++) != 0) {
  132. c = tolower(c);
  133. len = strlen(sub);
  134. do {
  135. do {
  136. if((csub = *s++) == 0)
  137. return (NULL);
  138. }
  139. while(tolower(csub) != c);
  140. }
  141. while(strncasecmp(s, sub, len) != 0);
  142. s--;
  143. }
  144. return (char *)s;
  145. }
  146. void
  147. cleanup(void) {
  148. Item *itm;
  149. while(allitems) {
  150. itm = allitems->next;
  151. free(allitems->text);
  152. free(allitems);
  153. allitems = itm;
  154. }
  155. if(dc.font.set)
  156. XFreeFontSet(dpy, dc.font.set);
  157. else
  158. XFreeFont(dpy, dc.font.xfont);
  159. XFreePixmap(dpy, dc.drawable);
  160. XFreeGC(dpy, dc.gc);
  161. XDestroyWindow(dpy, win);
  162. XUngrabKeyboard(dpy, CurrentTime);
  163. }
  164. void
  165. drawmenu(void) {
  166. Item *i;
  167. dc.x = 0;
  168. dc.y = 0;
  169. dc.w = mw;
  170. dc.h = mh;
  171. drawtext(NULL, dc.norm);
  172. /* print prompt? */
  173. if(promptw) {
  174. dc.w = promptw;
  175. drawtext(prompt, dc.sel);
  176. }
  177. dc.x += promptw;
  178. dc.w = mw - promptw;
  179. /* print command */
  180. if(cmdw && item)
  181. dc.w = cmdw;
  182. drawtext(text[0] ? text : NULL, dc.norm);
  183. dc.x += cmdw;
  184. if(curr) {
  185. dc.w = spaceitem;
  186. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  187. dc.x += dc.w;
  188. /* determine maximum items */
  189. for(i = curr; i != next; i=i->right) {
  190. dc.w = textw(i->text);
  191. if(dc.w > mw / 3)
  192. dc.w = mw / 3;
  193. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  194. dc.x += dc.w;
  195. }
  196. dc.x = mw - spaceitem;
  197. dc.w = spaceitem;
  198. drawtext(next ? ">" : NULL, dc.norm);
  199. }
  200. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  201. XFlush(dpy);
  202. }
  203. void
  204. drawtext(const char *text, ulong col[ColLast]) {
  205. int x, y, w, h;
  206. static char buf[256];
  207. uint len, olen;
  208. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  209. XSetForeground(dpy, dc.gc, col[ColBG]);
  210. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  211. if(!text)
  212. return;
  213. w = 0;
  214. olen = len = strlen(text);
  215. if(len >= sizeof buf)
  216. len = sizeof buf - 1;
  217. memcpy(buf, text, len);
  218. buf[len] = 0;
  219. h = dc.font.ascent + dc.font.descent;
  220. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  221. x = dc.x + (h / 2);
  222. /* shorten text if necessary */
  223. while(len && (w = textnw(buf, len)) > dc.w - h)
  224. buf[--len] = 0;
  225. if(len < olen) {
  226. if(len > 1)
  227. buf[len - 1] = '.';
  228. if(len > 2)
  229. buf[len - 2] = '.';
  230. if(len > 3)
  231. buf[len - 3] = '.';
  232. }
  233. if(w > dc.w)
  234. return; /* too long */
  235. XSetForeground(dpy, dc.gc, col[ColFG]);
  236. if(dc.font.set)
  237. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  238. else
  239. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  240. }
  241. void *
  242. emalloc(uint size) {
  243. void *res = malloc(size);
  244. if(!res)
  245. eprint("fatal: could not malloc() %u bytes\n", size);
  246. return res;
  247. }
  248. void
  249. eprint(const char *errstr, ...) {
  250. va_list ap;
  251. va_start(ap, errstr);
  252. vfprintf(stderr, errstr, ap);
  253. va_end(ap);
  254. exit(EXIT_FAILURE);
  255. }
  256. ulong
  257. getcolor(const char *colstr) {
  258. Colormap cmap = DefaultColormap(dpy, screen);
  259. XColor color;
  260. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  261. eprint("error, cannot allocate color '%s'\n", colstr);
  262. return color.pixel;
  263. }
  264. Bool
  265. grabkeyboard(void) {
  266. uint len;
  267. for(len = 1000; len; len--) {
  268. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  269. == GrabSuccess)
  270. break;
  271. usleep(1000);
  272. }
  273. return len > 0;
  274. }
  275. void
  276. initfont(const char *fontstr) {
  277. char *def, **missing;
  278. int i, n;
  279. if(!fontstr || fontstr[0] == '\0')
  280. eprint("error, cannot load font: '%s'\n", fontstr);
  281. missing = NULL;
  282. if(dc.font.set)
  283. XFreeFontSet(dpy, dc.font.set);
  284. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  285. if(missing)
  286. XFreeStringList(missing);
  287. if(dc.font.set) {
  288. XFontSetExtents *font_extents;
  289. XFontStruct **xfonts;
  290. char **font_names;
  291. dc.font.ascent = dc.font.descent = 0;
  292. font_extents = XExtentsOfFontSet(dc.font.set);
  293. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  294. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  295. if(dc.font.ascent < (*xfonts)->ascent)
  296. dc.font.ascent = (*xfonts)->ascent;
  297. if(dc.font.descent < (*xfonts)->descent)
  298. dc.font.descent = (*xfonts)->descent;
  299. xfonts++;
  300. }
  301. }
  302. else {
  303. if(dc.font.xfont)
  304. XFreeFont(dpy, dc.font.xfont);
  305. dc.font.xfont = NULL;
  306. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  307. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  308. eprint("error, cannot load font: '%s'\n", fontstr);
  309. dc.font.ascent = dc.font.xfont->ascent;
  310. dc.font.descent = dc.font.xfont->descent;
  311. }
  312. dc.font.height = dc.font.ascent + dc.font.descent;
  313. }
  314. void
  315. kpress(XKeyEvent * e) {
  316. char buf[32];
  317. int i, num;
  318. uint len;
  319. KeySym ksym;
  320. len = strlen(text);
  321. buf[0] = 0;
  322. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  323. if(IsKeypadKey(ksym)) {
  324. if(ksym == XK_KP_Enter)
  325. ksym = XK_Return;
  326. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  327. ksym = (ksym - XK_KP_0) + XK_0;
  328. }
  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. uint 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. uint 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. if(!(p = strdup(buf)))
  532. eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
  533. if(max < len) {
  534. maxname = p;
  535. max = len;
  536. }
  537. new = emalloc(sizeof(Item));
  538. new->next = new->left = new->right = NULL;
  539. new->text = p;
  540. if(!i)
  541. allitems = new;
  542. else
  543. i->next = new;
  544. i = new;
  545. }
  546. }
  547. void
  548. run(void) {
  549. XEvent ev;
  550. /* main event loop */
  551. while(running && !XNextEvent(dpy, &ev))
  552. switch (ev.type) {
  553. default: /* ignore all crap */
  554. break;
  555. case KeyPress:
  556. kpress(&ev.xkey);
  557. break;
  558. case Expose:
  559. if(ev.xexpose.count == 0)
  560. drawmenu();
  561. break;
  562. }
  563. }
  564. void
  565. setup(Bool topbar) {
  566. int i, j, x, y;
  567. XModifierKeymap *modmap;
  568. XSetWindowAttributes wa;
  569. #if XINERAMA
  570. XineramaScreenInfo *info = NULL;
  571. #endif
  572. /* init modifier map */
  573. modmap = XGetModifierMapping(dpy);
  574. for(i = 0; i < 8; i++)
  575. for(j = 0; j < modmap->max_keypermod; j++) {
  576. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  577. == XKeysymToKeycode(dpy, XK_Num_Lock))
  578. numlockmask = (1 << i);
  579. }
  580. XFreeModifiermap(modmap);
  581. /* style */
  582. dc.norm[ColBG] = getcolor(normbg);
  583. dc.norm[ColFG] = getcolor(normfg);
  584. dc.sel[ColBG] = getcolor(selbg);
  585. dc.sel[ColFG] = getcolor(selfg);
  586. initfont(font);
  587. /* menu window */
  588. wa.override_redirect = 1;
  589. wa.background_pixmap = ParentRelative;
  590. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  591. /* menu window geometry */
  592. mh = dc.font.height + 2;
  593. #if XINERAMA
  594. if(XineramaIsActive(dpy)) {
  595. info = XineramaQueryScreens(dpy, &i);
  596. x = info[0].x_org;
  597. y = topbar ? info[0].y_org : info[0].y_org + info[0].height - mh;
  598. mw = info[0].width;
  599. XFree(info);
  600. }
  601. else
  602. #endif
  603. {
  604. x = 0;
  605. y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
  606. mw = DisplayWidth(dpy, screen);
  607. }
  608. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  609. DefaultDepth(dpy, screen), CopyFromParent,
  610. DefaultVisual(dpy, screen),
  611. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  612. /* pixmap */
  613. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  614. dc.gc = XCreateGC(dpy, root, 0, 0);
  615. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  616. if(!dc.font.set)
  617. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  618. if(maxname)
  619. cmdw = textw(maxname);
  620. if(cmdw > mw / 3)
  621. cmdw = mw / 3;
  622. if(prompt)
  623. promptw = textw(prompt);
  624. if(promptw > mw / 5)
  625. promptw = mw / 5;
  626. text[0] = 0;
  627. match(text);
  628. XMapRaised(dpy, win);
  629. }
  630. int
  631. textnw(const char *text, uint len) {
  632. XRectangle r;
  633. if(dc.font.set) {
  634. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  635. return r.width;
  636. }
  637. return XTextWidth(dc.font.xfont, text, len);
  638. }
  639. int
  640. textw(const char *text) {
  641. return textnw(text, strlen(text)) + dc.font.height;
  642. }
  643. int
  644. main(int argc, char *argv[]) {
  645. uint i;
  646. Bool topbar = True;
  647. /* command line args */
  648. for(i = 1; i < argc; i++)
  649. if(!strcmp(argv[i], "-i")) {
  650. fstrncmp = strncasecmp;
  651. fstrstr = cistrstr;
  652. }
  653. else if(!strcmp(argv[i], "-b"))
  654. topbar = False;
  655. else if(!strcmp(argv[i], "-fn")) {
  656. if(++i < argc) font = argv[i];
  657. }
  658. else if(!strcmp(argv[i], "-nb")) {
  659. if(++i < argc) normbg = argv[i];
  660. }
  661. else if(!strcmp(argv[i], "-nf")) {
  662. if(++i < argc) normfg = argv[i];
  663. }
  664. else if(!strcmp(argv[i], "-p")) {
  665. if(++i < argc) prompt = argv[i];
  666. }
  667. else if(!strcmp(argv[i], "-sb")) {
  668. if(++i < argc) selbg = argv[i];
  669. }
  670. else if(!strcmp(argv[i], "-sf")) {
  671. if(++i < argc) selfg = argv[i];
  672. }
  673. else if(!strcmp(argv[i], "-v"))
  674. eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
  675. else
  676. eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  677. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  678. setlocale(LC_CTYPE, "");
  679. if(!(dpy = XOpenDisplay(0)))
  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(topbar);
  692. drawmenu();
  693. XSync(dpy, False);
  694. run();
  695. cleanup();
  696. XCloseDisplay(dpy);
  697. return ret;
  698. }