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.

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