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.

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