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.

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