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.

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