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.

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