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.

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