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.

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