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.
 
 
 
 
 
 

828 rivejä
18 KiB

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