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.

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