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.

826 lines
18 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
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
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
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
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. /* 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 parent, win;
  88. static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
  89. static char *(*fstrstr)(const char *, const char *) = strstr;
  90. static unsigned int lines = 0;
  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 = MIN(textw(next->text), mw / 3);
  111. w += tw;
  112. if(w > mw)
  113. break;
  114. }
  115. w = promptw + cmdw + 2 * spaceitem;
  116. for(prev = curr; prev && prev->left; prev=prev->left) {
  117. tw = MIN(textw(prev->left->text), mw / 3);
  118. w += tw;
  119. if(w > mw)
  120. break;
  121. }
  122. }
  123. void
  124. calcoffsetsv(void) {
  125. static unsigned int h;
  126. if(!curr)
  127. return;
  128. h = (dc.font.height + 2) * (lines + 1);
  129. for(next = curr; next; next=next->right) {
  130. h -= dc.font.height + 2;
  131. if(h <= 0)
  132. break;
  133. }
  134. h = (dc.font.height + 2) * (lines + 1);
  135. for(prev = curr; prev && prev->left; prev=prev->left) {
  136. h -= dc.font.height + 2;
  137. if(h <= 0)
  138. break;
  139. }
  140. }
  141. char *
  142. cistrstr(const char *s, const char *sub) {
  143. int c, csub;
  144. unsigned int len;
  145. if(!sub)
  146. return (char *)s;
  147. if((c = *sub++) != '\0') {
  148. c = tolower(c);
  149. len = strlen(sub);
  150. do {
  151. do {
  152. if((csub = *s++) == '\0')
  153. return NULL;
  154. }
  155. while(tolower(csub) != c);
  156. }
  157. while(strncasecmp(s, sub, len) != 0);
  158. s--;
  159. }
  160. return (char *)s;
  161. }
  162. void
  163. cleanup(void) {
  164. Item *itm;
  165. while(allitems) {
  166. itm = allitems->next;
  167. free(allitems->text);
  168. free(allitems);
  169. allitems = itm;
  170. }
  171. if(dc.font.set)
  172. XFreeFontSet(dpy, dc.font.set);
  173. else
  174. XFreeFont(dpy, dc.font.xfont);
  175. XFreePixmap(dpy, dc.drawable);
  176. XFreeGC(dpy, dc.gc);
  177. XDestroyWindow(dpy, win);
  178. XUngrabKeyboard(dpy, CurrentTime);
  179. }
  180. void
  181. drawcursor(void) {
  182. XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
  183. r.x += textnw(text, cursor) + dc.font.height / 2;
  184. XSetForeground(dpy, dc.gc, dc.norm[ColFG]);
  185. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  186. }
  187. void
  188. drawmenu(void) {
  189. dc.x = 0;
  190. dc.y = 0;
  191. dc.w = mw;
  192. dc.h = mh;
  193. drawtext(NULL, dc.norm);
  194. /* print prompt? */
  195. if(promptw) {
  196. dc.w = promptw;
  197. drawtext(prompt, dc.sel);
  198. }
  199. dc.x += promptw;
  200. dc.w = mw - promptw;
  201. /* print command */
  202. if(cmdw && item && lines == 0)
  203. dc.w = cmdw;
  204. drawtext(text[0] ? text : NULL, dc.norm);
  205. drawcursor();
  206. if(curr) {
  207. if(lines > 0)
  208. drawmenuv();
  209. else
  210. drawmenuh();
  211. }
  212. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  213. XFlush(dpy);
  214. }
  215. void
  216. drawmenuh(void) {
  217. Item *i;
  218. dc.x += cmdw;
  219. dc.w = spaceitem;
  220. drawtext(curr->left ? "<" : NULL, dc.norm);
  221. dc.x += dc.w;
  222. /* determine maximum items */
  223. for(i = curr; i != next; i=i->right) {
  224. dc.w = MIN(textw(i->text), mw / 3);
  225. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  226. dc.x += dc.w;
  227. }
  228. dc.x = mw - spaceitem;
  229. dc.w = spaceitem;
  230. drawtext(next ? ">" : NULL, dc.norm);
  231. }
  232. void
  233. drawmenuv(void) {
  234. Item *i;
  235. dc.x = 0;
  236. dc.w = mw;
  237. dc.y += dc.font.height + 2;
  238. /* determine maximum items */
  239. for(i = curr; i != next; i=i->right) {
  240. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  241. dc.y += dc.font.height + 2;
  242. }
  243. drawtext(NULL, dc.norm);
  244. }
  245. void
  246. drawtext(const char *text, unsigned long col[ColLast]) {
  247. char buf[256];
  248. int i, x, y, h, len, olen;
  249. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  250. XSetForeground(dpy, dc.gc, col[ColBG]);
  251. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  252. if(!text)
  253. return;
  254. olen = strlen(text);
  255. h = dc.font.height;
  256. y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
  257. x = dc.x + (h / 2);
  258. /* shorten text if necessary */
  259. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  260. if(!len)
  261. return;
  262. memcpy(buf, text, len);
  263. if(len < olen)
  264. for(i = len; i && i > len - 3; buf[--i] = '.');
  265. XSetForeground(dpy, dc.gc, col[ColFG]);
  266. if(dc.font.set)
  267. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  268. else
  269. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  270. }
  271. void
  272. eprint(const char *errstr, ...) {
  273. va_list ap;
  274. va_start(ap, errstr);
  275. vfprintf(stderr, errstr, ap);
  276. va_end(ap);
  277. exit(EXIT_FAILURE);
  278. }
  279. unsigned long
  280. getcolor(const char *colstr) {
  281. Colormap cmap = DefaultColormap(dpy, screen);
  282. XColor color;
  283. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  284. eprint("error, cannot allocate color '%s'\n", colstr);
  285. return color.pixel;
  286. }
  287. Bool
  288. grabkeyboard(void) {
  289. unsigned int len;
  290. for(len = 1000; len; len--) {
  291. if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  292. == GrabSuccess)
  293. break;
  294. usleep(1000);
  295. }
  296. return len > 0;
  297. }
  298. void
  299. initfont(const char *fontstr) {
  300. char *def, **missing;
  301. int i, n;
  302. if(!fontstr || fontstr[0] == '\0')
  303. eprint("error, cannot load font: '%s'\n", fontstr);
  304. missing = NULL;
  305. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  306. if(missing)
  307. XFreeStringList(missing);
  308. if(dc.font.set) {
  309. XFontSetExtents *font_extents;
  310. XFontStruct **xfonts;
  311. char **font_names;
  312. dc.font.ascent = dc.font.descent = 0;
  313. font_extents = XExtentsOfFontSet(dc.font.set);
  314. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  315. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  316. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  317. dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
  318. xfonts++;
  319. }
  320. }
  321. else {
  322. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  323. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  324. eprint("error, cannot load font: '%s'\n", fontstr);
  325. dc.font.ascent = dc.font.xfont->ascent;
  326. dc.font.descent = dc.font.xfont->descent;
  327. }
  328. dc.font.height = dc.font.ascent + dc.font.descent;
  329. }
  330. void
  331. kpress(XKeyEvent * e) {
  332. char buf[sizeof text];
  333. int i, num;
  334. unsigned int len;
  335. KeySym ksym;
  336. len = strlen(text);
  337. buf[0] = '\0';
  338. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  339. if(IsKeypadKey(ksym)) {
  340. if(ksym == XK_KP_Enter)
  341. ksym = XK_Return;
  342. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  343. ksym = (ksym - XK_KP_0) + XK_0;
  344. }
  345. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  346. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  347. || IsPrivateKeypadKey(ksym))
  348. return;
  349. /* first check if a control mask is omitted */
  350. if(e->state & ControlMask) {
  351. switch (ksym) {
  352. default: /* ignore other control sequences */
  353. return;
  354. case XK_a:
  355. case XK_A:
  356. ksym = XK_Home;
  357. break;
  358. case XK_c:
  359. case XK_C:
  360. ksym = XK_Escape;
  361. break;
  362. case XK_e:
  363. case XK_E:
  364. ksym = XK_End;
  365. break;
  366. case XK_h:
  367. case XK_H:
  368. ksym = XK_BackSpace;
  369. break;
  370. case XK_i:
  371. case XK_I:
  372. ksym = XK_Tab;
  373. break;
  374. case XK_j:
  375. case XK_J:
  376. ksym = XK_Return;
  377. break;
  378. case XK_u:
  379. case XK_U:
  380. memmove(text, text + cursor, sizeof text - cursor + 1);
  381. cursor = 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 || !maxname) {
  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. case VisibilityNotify:
  620. if (ev.xvisibility.state != VisibilityUnobscured)
  621. XRaiseWindow(dpy, win);
  622. break;
  623. }
  624. }
  625. void
  626. setup(Bool topbar) {
  627. int i, j, x, y;
  628. #if XINERAMA
  629. int n;
  630. XineramaScreenInfo *info = NULL;
  631. #endif
  632. XModifierKeymap *modmap;
  633. XSetWindowAttributes wa;
  634. XWindowAttributes pwa;
  635. /* init modifier map */
  636. modmap = XGetModifierMapping(dpy);
  637. for(i = 0; i < 8; i++)
  638. for(j = 0; j < modmap->max_keypermod; j++) {
  639. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  640. == XKeysymToKeycode(dpy, XK_Num_Lock))
  641. numlockmask = (1 << i);
  642. }
  643. XFreeModifiermap(modmap);
  644. /* style */
  645. dc.norm[ColBG] = getcolor(normbgcolor);
  646. dc.norm[ColFG] = getcolor(normfgcolor);
  647. dc.sel[ColBG] = getcolor(selbgcolor);
  648. dc.sel[ColFG] = getcolor(selfgcolor);
  649. initfont(font);
  650. /* menu window */
  651. wa.override_redirect = True;
  652. wa.background_pixmap = ParentRelative;
  653. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
  654. /* menu window geometry */
  655. mh = (dc.font.height + 2) * (lines + 1);
  656. #if XINERAMA
  657. if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  658. i = 0;
  659. if(n > 1) {
  660. int di;
  661. unsigned int dui;
  662. Window dummy;
  663. if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
  664. for(i = 0; i < n; i++)
  665. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  666. break;
  667. }
  668. x = info[i].x_org;
  669. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  670. mw = info[i].width;
  671. XFree(info);
  672. }
  673. else
  674. #endif
  675. {
  676. XGetWindowAttributes(dpy, parent, &pwa);
  677. x = 0;
  678. y = topbar ? 0 : pwa.height - mh;
  679. mw = pwa.width;
  680. }
  681. win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
  682. DefaultDepth(dpy, screen), CopyFromParent,
  683. DefaultVisual(dpy, screen),
  684. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  685. /* pixmap */
  686. dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
  687. dc.gc = XCreateGC(dpy, parent, 0, NULL);
  688. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  689. if(!dc.font.set)
  690. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  691. if(maxname)
  692. cmdw = MIN(textw(maxname), mw / 3);
  693. if(prompt)
  694. promptw = MIN(textw(prompt), mw / 5);
  695. text[0] = '\0';
  696. match(text);
  697. XMapRaised(dpy, win);
  698. }
  699. int
  700. textnw(const char *text, unsigned int len) {
  701. XRectangle r;
  702. if(dc.font.set) {
  703. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  704. return r.width;
  705. }
  706. return XTextWidth(dc.font.xfont, text, len);
  707. }
  708. int
  709. textw(const char *text) {
  710. return textnw(text, strlen(text)) + dc.font.height;
  711. }
  712. int
  713. main(int argc, char *argv[]) {
  714. unsigned int i;
  715. Bool topbar = True;
  716. /* command line args */
  717. for(i = 1; i < argc; i++)
  718. if(!strcmp(argv[i], "-i")) {
  719. fstrncmp = strncasecmp;
  720. fstrstr = cistrstr;
  721. }
  722. else if(!strcmp(argv[i], "-b"))
  723. topbar = False;
  724. else if(!strcmp(argv[i], "-e")) {
  725. if(++i < argc) parent = atoi(argv[i]);
  726. }
  727. else if(!strcmp(argv[i], "-l")) {
  728. calcoffsets = calcoffsetsv;
  729. if(++i < argc) lines = atoi(argv[i]);
  730. }
  731. else if(!strcmp(argv[i], "-fn")) {
  732. if(++i < argc) font = argv[i];
  733. }
  734. else if(!strcmp(argv[i], "-nb")) {
  735. if(++i < argc) normbgcolor = argv[i];
  736. }
  737. else if(!strcmp(argv[i], "-nf")) {
  738. if(++i < argc) normfgcolor = argv[i];
  739. }
  740. else if(!strcmp(argv[i], "-p")) {
  741. if(++i < argc) prompt = argv[i];
  742. }
  743. else if(!strcmp(argv[i], "-sb")) {
  744. if(++i < argc) selbgcolor = argv[i];
  745. }
  746. else if(!strcmp(argv[i], "-sf")) {
  747. if(++i < argc) selfgcolor = argv[i];
  748. }
  749. else if(!strcmp(argv[i], "-v"))
  750. eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  751. else
  752. eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  753. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  754. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  755. fprintf(stderr, "warning: no locale support\n");
  756. if(!(dpy = XOpenDisplay(NULL)))
  757. eprint("dmenu: cannot open display\n");
  758. screen = DefaultScreen(dpy);
  759. if(!parent)
  760. parent = RootWindow(dpy, screen);
  761. readstdin();
  762. running = grabkeyboard();
  763. setup(topbar);
  764. drawmenu();
  765. XSync(dpy, False);
  766. run();
  767. cleanup();
  768. XCloseDisplay(dpy);
  769. return ret;
  770. }