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.

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