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.

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