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.

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