My dmenu build
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.

dmenu.c 15 KiB

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