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.

822 lines
17 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
16 years ago
18 years ago
17 years ago
16 years ago
16 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
16 years ago
18 years ago
17 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
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
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
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
17 years ago
18 years ago
17 years ago
17 years ago
18 years ago
  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)
  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. text[0] = 0;
  382. match(text);
  383. break;
  384. case XK_w:
  385. case XK_W:
  386. if(cursor > 0) {
  387. i = cursor;
  388. while(i-- > 0 && text[i] == ' ');
  389. while(i-- > 0 && text[i] != ' ');
  390. memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
  391. cursor = i + 1;
  392. match(text);
  393. }
  394. break;
  395. }
  396. }
  397. if(CLEANMASK(e->state) & Mod1Mask) {
  398. switch(ksym) {
  399. default: return;
  400. case XK_h:
  401. ksym = XK_Left;
  402. break;
  403. case XK_l:
  404. ksym = XK_Right;
  405. break;
  406. case XK_j:
  407. ksym = XK_Next;
  408. break;
  409. case XK_k:
  410. ksym = XK_Prior;
  411. break;
  412. case XK_g:
  413. ksym = XK_Home;
  414. break;
  415. case XK_G:
  416. ksym = XK_End;
  417. break;
  418. case XK_p:
  419. {
  420. FILE *fp;
  421. char *s;
  422. if(!(fp = (FILE*)popen("sselp", "r")))
  423. eprint("dmenu: Could not popen sselp\n");
  424. s = fgets(buf, sizeof buf, fp);
  425. pclose(fp);
  426. if(s == NULL)
  427. return;
  428. }
  429. num = strlen(buf);
  430. if(num && buf[num-1] == '\n')
  431. buf[--num] = '\0';
  432. break;
  433. }
  434. }
  435. switch(ksym) {
  436. default:
  437. num = MIN(num, sizeof text - cursor);
  438. if(num && !iscntrl((int) buf[0])) {
  439. memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
  440. memcpy(text + cursor, buf, num);
  441. cursor+=num;
  442. match(text);
  443. }
  444. break;
  445. case XK_BackSpace:
  446. if(cursor > 0) {
  447. memmove(text + cursor - 1, text + cursor, sizeof text - cursor + 1);
  448. cursor--;
  449. match(text);
  450. }
  451. break;
  452. case XK_Delete:
  453. memmove(text + cursor, text + cursor + 1, sizeof text - cursor);
  454. match(text);
  455. break;
  456. case XK_End:
  457. if(cursor < len) {
  458. cursor = len;
  459. break;
  460. }
  461. while(next) {
  462. sel = curr = next;
  463. calcoffsets();
  464. }
  465. while(sel && sel->right)
  466. sel = sel->right;
  467. break;
  468. case XK_Escape:
  469. ret = 1;
  470. running = False;
  471. break;
  472. case XK_Home:
  473. if(sel == item) {
  474. cursor = 0;
  475. break;
  476. }
  477. sel = curr = item;
  478. calcoffsets();
  479. break;
  480. case XK_Left:
  481. case XK_Up:
  482. if(sel && sel->left){
  483. sel=sel->left;
  484. if(sel->right == curr) {
  485. curr = prev;
  486. calcoffsets();
  487. }
  488. }
  489. else if(cursor > 0)
  490. cursor--;
  491. else
  492. return;
  493. break;
  494. case XK_Next:
  495. if(!next)
  496. return;
  497. sel = curr = next;
  498. calcoffsets();
  499. break;
  500. case XK_Prior:
  501. if(!prev)
  502. return;
  503. sel = curr = prev;
  504. calcoffsets();
  505. break;
  506. case XK_Return:
  507. if((e->state & ShiftMask) || !sel)
  508. fprintf(stdout, "%s", text);
  509. else
  510. fprintf(stdout, "%s", sel->text);
  511. fflush(stdout);
  512. running = False;
  513. break;
  514. case XK_Right:
  515. case XK_Down:
  516. if(cursor < len)
  517. cursor++;
  518. else if(sel && sel->right) {
  519. sel=sel->right;
  520. if(sel == next) {
  521. curr = next;
  522. calcoffsets();
  523. }
  524. }
  525. else
  526. return;
  527. break;
  528. case XK_Tab:
  529. if(!sel)
  530. return;
  531. strncpy(text, sel->text, sizeof text);
  532. cursor = strlen(text);
  533. match(text);
  534. break;
  535. }
  536. drawmenu();
  537. }
  538. void
  539. match(char *pattern) {
  540. unsigned int plen;
  541. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  542. if(!pattern)
  543. return;
  544. plen = strlen(pattern);
  545. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  546. for(i = allitems; i; i = i->next)
  547. if(!fstrncmp(pattern, i->text, plen + 1))
  548. appenditem(i, &lexact, &exactend);
  549. else if(!fstrncmp(pattern, i->text, plen))
  550. appenditem(i, &lprefix, &prefixend);
  551. else if(fstrstr(i->text, pattern))
  552. appenditem(i, &lsubstr, &substrend);
  553. if(lexact) {
  554. item = lexact;
  555. itemend = exactend;
  556. }
  557. if(lprefix) {
  558. if(itemend) {
  559. itemend->right = lprefix;
  560. lprefix->left = itemend;
  561. }
  562. else
  563. item = lprefix;
  564. itemend = prefixend;
  565. }
  566. if(lsubstr) {
  567. if(itemend) {
  568. itemend->right = lsubstr;
  569. lsubstr->left = itemend;
  570. }
  571. else
  572. item = lsubstr;
  573. }
  574. curr = prev = next = sel = item;
  575. calcoffsets();
  576. }
  577. void
  578. readstdin(void) {
  579. char *p, buf[sizeof text];
  580. unsigned int len = 0, max = 0;
  581. Item *i, *new;
  582. i = NULL;
  583. while(fgets(buf, sizeof buf, stdin)) {
  584. len = strlen(buf);
  585. if(buf[len-1] == '\n')
  586. buf[--len] = '\0';
  587. if(!(p = strdup(buf)))
  588. eprint("fatal: could not strdup() %u bytes\n", len);
  589. if(max < len) {
  590. maxname = p;
  591. max = len;
  592. }
  593. if(!(new = (Item *)malloc(sizeof(Item))))
  594. eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
  595. new->next = new->left = new->right = NULL;
  596. new->text = p;
  597. if(!i)
  598. allitems = new;
  599. else
  600. i->next = new;
  601. i = new;
  602. }
  603. }
  604. void
  605. run(void) {
  606. XEvent ev;
  607. /* main event loop */
  608. while(running && !XNextEvent(dpy, &ev))
  609. switch (ev.type) {
  610. default: /* ignore all crap */
  611. break;
  612. case KeyPress:
  613. kpress(&ev.xkey);
  614. break;
  615. case Expose:
  616. if(ev.xexpose.count == 0)
  617. drawmenu();
  618. break;
  619. }
  620. }
  621. void
  622. setup(Bool topbar) {
  623. int i, j, x, y;
  624. #if XINERAMA
  625. int n;
  626. XineramaScreenInfo *info = NULL;
  627. #endif
  628. XModifierKeymap *modmap;
  629. XSetWindowAttributes wa;
  630. /* init modifier map */
  631. modmap = XGetModifierMapping(dpy);
  632. for(i = 0; i < 8; i++)
  633. for(j = 0; j < modmap->max_keypermod; j++) {
  634. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  635. == XKeysymToKeycode(dpy, XK_Num_Lock))
  636. numlockmask = (1 << i);
  637. }
  638. XFreeModifiermap(modmap);
  639. /* style */
  640. dc.norm[ColBG] = getcolor(normbgcolor);
  641. dc.norm[ColFG] = getcolor(normfgcolor);
  642. dc.sel[ColBG] = getcolor(selbgcolor);
  643. dc.sel[ColFG] = getcolor(selfgcolor);
  644. initfont(font);
  645. /* menu window */
  646. wa.override_redirect = True;
  647. wa.background_pixmap = ParentRelative;
  648. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  649. /* menu window geometry */
  650. mh = dc.font.height + 2;
  651. mh = vlist ? mh * (lines+1) : mh;
  652. #if XINERAMA
  653. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  654. i = 0;
  655. if(n > 1) {
  656. int di;
  657. unsigned int dui;
  658. Window dummy;
  659. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  660. for(i = 0; i < n; i++)
  661. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  662. break;
  663. }
  664. x = info[i].x_org;
  665. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  666. mw = info[i].width;
  667. XFree(info);
  668. }
  669. else
  670. #endif
  671. {
  672. x = 0;
  673. y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
  674. mw = DisplayWidth(dpy, screen);
  675. }
  676. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  677. DefaultDepth(dpy, screen), CopyFromParent,
  678. DefaultVisual(dpy, screen),
  679. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  680. /* pixmap */
  681. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  682. dc.gc = XCreateGC(dpy, root, 0, NULL);
  683. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  684. if(!dc.font.set)
  685. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  686. if(maxname)
  687. cmdw = MIN(textw(maxname), mw / 3);
  688. if(prompt)
  689. promptw = MIN(textw(prompt), mw / 5);
  690. text[0] = 0;
  691. match(text);
  692. XMapRaised(dpy, win);
  693. }
  694. int
  695. textnw(const char *text, unsigned int len) {
  696. XRectangle r;
  697. if(dc.font.set) {
  698. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  699. return r.width;
  700. }
  701. return XTextWidth(dc.font.xfont, text, len);
  702. }
  703. int
  704. textw(const char *text) {
  705. return textnw(text, strlen(text)) + dc.font.height;
  706. }
  707. int
  708. main(int argc, char *argv[]) {
  709. unsigned int i;
  710. Bool topbar = True;
  711. /* command line args */
  712. for(i = 1; i < argc; i++)
  713. if(!strcmp(argv[i], "-i")) {
  714. fstrncmp = strncasecmp;
  715. fstrstr = cistrstr;
  716. }
  717. else if(!strcmp(argv[i], "-b"))
  718. topbar = False;
  719. else if(!strcmp(argv[i], "-e")) {
  720. if(++i < argc) root = atoi(argv[i]);
  721. }
  722. else if(!strcmp(argv[i], "-l")) {
  723. vlist = True;
  724. calcoffsets = calcoffsetsv;
  725. if(++i < argc) lines = atoi(argv[i]);
  726. }
  727. else if(!strcmp(argv[i], "-fn")) {
  728. if(++i < argc) font = argv[i];
  729. }
  730. else if(!strcmp(argv[i], "-nb")) {
  731. if(++i < argc) normbgcolor = argv[i];
  732. }
  733. else if(!strcmp(argv[i], "-nf")) {
  734. if(++i < argc) normfgcolor = argv[i];
  735. }
  736. else if(!strcmp(argv[i], "-p")) {
  737. if(++i < argc) prompt = argv[i];
  738. }
  739. else if(!strcmp(argv[i], "-sb")) {
  740. if(++i < argc) selbgcolor = argv[i];
  741. }
  742. else if(!strcmp(argv[i], "-sf")) {
  743. if(++i < argc) selfgcolor = argv[i];
  744. }
  745. else if(!strcmp(argv[i], "-v"))
  746. eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  747. else
  748. eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  749. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  750. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  751. fprintf(stderr, "warning: no locale support\n");
  752. if(!(dpy = XOpenDisplay(NULL)))
  753. eprint("dmenu: cannot open display\n");
  754. screen = DefaultScreen(dpy);
  755. if(!root)
  756. root = RootWindow(dpy, screen);
  757. readstdin();
  758. running = grabkeyboard();
  759. setup(topbar);
  760. drawmenu();
  761. XSync(dpy, False);
  762. run();
  763. cleanup();
  764. XCloseDisplay(dpy);
  765. return ret;
  766. }