Configuration of dwm for Mac Computers
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.

421 lines
9.7 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <X11/Xlib.h>
  6. #include <X11/Xft/Xft.h>
  7. #include "drw.h"
  8. #include "util.h"
  9. #define UTF_INVALID 0xFFFD
  10. #define UTF_SIZ 4
  11. static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  12. static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  13. static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  14. static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  15. static long
  16. utf8decodebyte(const char c, size_t *i)
  17. {
  18. for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
  19. if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
  20. return (unsigned char)c & ~utfmask[*i];
  21. return 0;
  22. }
  23. static size_t
  24. utf8validate(long *u, size_t i)
  25. {
  26. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  27. *u = UTF_INVALID;
  28. for (i = 1; *u > utfmax[i]; ++i)
  29. ;
  30. return i;
  31. }
  32. static size_t
  33. utf8decode(const char *c, long *u, size_t clen)
  34. {
  35. size_t i, j, len, type;
  36. long udecoded;
  37. *u = UTF_INVALID;
  38. if (!clen)
  39. return 0;
  40. udecoded = utf8decodebyte(c[0], &len);
  41. if (!BETWEEN(len, 1, UTF_SIZ))
  42. return 1;
  43. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  44. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  45. if (type)
  46. return j;
  47. }
  48. if (j < len)
  49. return 0;
  50. *u = udecoded;
  51. utf8validate(u, len);
  52. return len;
  53. }
  54. Drw *
  55. drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
  56. {
  57. Drw *drw = ecalloc(1, sizeof(Drw));
  58. drw->dpy = dpy;
  59. drw->screen = screen;
  60. drw->root = root;
  61. drw->w = w;
  62. drw->h = h;
  63. drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  64. drw->gc = XCreateGC(dpy, root, 0, NULL);
  65. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  66. return drw;
  67. }
  68. void
  69. drw_resize(Drw *drw, unsigned int w, unsigned int h)
  70. {
  71. if (!drw)
  72. return;
  73. drw->w = w;
  74. drw->h = h;
  75. if (drw->drawable)
  76. XFreePixmap(drw->dpy, drw->drawable);
  77. drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
  78. }
  79. void
  80. drw_free(Drw *drw)
  81. {
  82. XFreePixmap(drw->dpy, drw->drawable);
  83. XFreeGC(drw->dpy, drw->gc);
  84. free(drw);
  85. }
  86. /* This function is an implementation detail. Library users should use
  87. * drw_fontset_create instead.
  88. */
  89. static Fnt *
  90. xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
  91. {
  92. Fnt *font;
  93. XftFont *xfont = NULL;
  94. FcPattern *pattern = NULL;
  95. if (fontname) {
  96. /* Using the pattern found at font->xfont->pattern does not yield the
  97. * same substitution results as using the pattern returned by
  98. * FcNameParse; using the latter results in the desired fallback
  99. * behaviour whereas the former just results in missing-character
  100. * rectangles being drawn, at least with some fonts. */
  101. if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  102. fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
  103. return NULL;
  104. }
  105. if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  106. fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
  107. XftFontClose(drw->dpy, xfont);
  108. return NULL;
  109. }
  110. } else if (fontpattern) {
  111. if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
  112. fprintf(stderr, "error, cannot load font from pattern.\n");
  113. return NULL;
  114. }
  115. } else {
  116. die("no font specified.\n");
  117. }
  118. font = ecalloc(1, sizeof(Fnt));
  119. font->xfont = xfont;
  120. font->pattern = pattern;
  121. font->h = xfont->ascent + xfont->descent;
  122. font->dpy = drw->dpy;
  123. return font;
  124. }
  125. static void
  126. xfont_free(Fnt *font)
  127. {
  128. if (!font)
  129. return;
  130. if (font->pattern)
  131. FcPatternDestroy(font->pattern);
  132. XftFontClose(font->dpy, font->xfont);
  133. free(font);
  134. }
  135. Fnt*
  136. drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  137. {
  138. Fnt *cur, *ret = NULL;
  139. size_t i;
  140. if (!drw || !fonts)
  141. return NULL;
  142. for (i = 1; i <= fontcount; i++) {
  143. if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
  144. cur->next = ret;
  145. ret = cur;
  146. }
  147. }
  148. return (drw->fonts = ret);
  149. }
  150. void
  151. drw_fontset_free(Fnt *font)
  152. {
  153. if (font) {
  154. drw_fontset_free(font->next);
  155. xfont_free(font);
  156. }
  157. }
  158. void
  159. drw_clr_create(Drw *drw, XftColor *dest, const char *clrname)
  160. {
  161. if (!drw || !dest || !clrname)
  162. return;
  163. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  164. DefaultColormap(drw->dpy, drw->screen),
  165. clrname, dest))
  166. die("error, cannot allocate color '%s'\n", clrname);
  167. }
  168. /* Wrapper to create color schemes. The caller has to call free(3) on the
  169. * returned color scheme when done using it. */
  170. Scm
  171. drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  172. {
  173. size_t i;
  174. Scm ret;
  175. /* need at least two colors for a scheme */
  176. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  177. return NULL;
  178. for (i = 0; i < clrcount; i++)
  179. drw_clr_create(drw, &ret[i], clrnames[i]);
  180. return ret;
  181. }
  182. void
  183. drw_setfontset(Drw *drw, Fnt *set)
  184. {
  185. if (drw)
  186. drw->fonts = set;
  187. }
  188. void
  189. drw_setscheme(Drw *drw, Scm scm)
  190. {
  191. if (drw)
  192. drw->scheme = scm;
  193. }
  194. void
  195. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  196. {
  197. if (!drw || !drw->scheme)
  198. return;
  199. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  200. if (filled)
  201. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  202. else
  203. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  204. }
  205. int
  206. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  207. {
  208. char buf[1024];
  209. int ty;
  210. unsigned int ew;
  211. XftDraw *d = NULL;
  212. Fnt *usedfont, *curfont, *nextfont;
  213. size_t i, len;
  214. int utf8strlen, utf8charlen, render = x || y || w || h;
  215. long utf8codepoint = 0;
  216. const char *utf8str;
  217. FcCharSet *fccharset;
  218. FcPattern *fcpattern;
  219. FcPattern *match;
  220. XftResult result;
  221. int charexists = 0;
  222. if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
  223. return 0;
  224. if (!render) {
  225. w = ~w;
  226. } else {
  227. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  228. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  229. d = XftDrawCreate(drw->dpy, drw->drawable,
  230. DefaultVisual(drw->dpy, drw->screen),
  231. DefaultColormap(drw->dpy, drw->screen));
  232. x += lpad;
  233. w -= lpad;
  234. }
  235. usedfont = drw->fonts;
  236. while (1) {
  237. utf8strlen = 0;
  238. utf8str = text;
  239. nextfont = NULL;
  240. while (*text) {
  241. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  242. for (curfont = drw->fonts; curfont; curfont = curfont->next) {
  243. charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
  244. if (charexists) {
  245. if (curfont == usedfont) {
  246. utf8strlen += utf8charlen;
  247. text += utf8charlen;
  248. } else {
  249. nextfont = curfont;
  250. }
  251. break;
  252. }
  253. }
  254. if (!charexists || nextfont)
  255. break;
  256. else
  257. charexists = 0;
  258. }
  259. if (utf8strlen) {
  260. drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
  261. /* shorten text if necessary */
  262. for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
  263. drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
  264. if (len) {
  265. memcpy(buf, utf8str, len);
  266. buf[len] = '\0';
  267. if (len < utf8strlen)
  268. for (i = len; i && i > len - 3; buf[--i] = '.')
  269. ; /* NOP */
  270. if (render) {
  271. ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
  272. XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
  273. usedfont->xfont, x, ty, (XftChar8 *)buf, len);
  274. }
  275. x += ew;
  276. w -= ew;
  277. }
  278. }
  279. if (!*text) {
  280. break;
  281. } else if (nextfont) {
  282. charexists = 0;
  283. usedfont = nextfont;
  284. } else {
  285. /* Regardless of whether or not a fallback font is found, the
  286. * character must be drawn. */
  287. charexists = 1;
  288. fccharset = FcCharSetCreate();
  289. FcCharSetAddChar(fccharset, utf8codepoint);
  290. if (!drw->fonts->pattern) {
  291. /* Refer to the comment in xfont_create for more information. */
  292. die("the first font in the cache must be loaded from a font string.\n");
  293. }
  294. fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  295. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  296. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  297. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  298. FcDefaultSubstitute(fcpattern);
  299. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  300. FcCharSetDestroy(fccharset);
  301. FcPatternDestroy(fcpattern);
  302. if (match) {
  303. usedfont = xfont_create(drw, NULL, match);
  304. if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  305. for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  306. ; /* NOP */
  307. curfont->next = usedfont;
  308. } else {
  309. xfont_free(usedfont);
  310. usedfont = drw->fonts;
  311. }
  312. }
  313. }
  314. }
  315. if (d)
  316. XftDrawDestroy(d);
  317. return x + (render ? w : 0);
  318. }
  319. void
  320. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  321. {
  322. if (!drw)
  323. return;
  324. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  325. XSync(drw->dpy, False);
  326. }
  327. unsigned int
  328. drw_fontset_getwidth(Drw *drw, const char *text)
  329. {
  330. if (!drw || !drw->fonts || !text)
  331. return 0;
  332. return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
  333. }
  334. void
  335. drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  336. {
  337. XGlyphInfo ext;
  338. if (!font || !text)
  339. return;
  340. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  341. if (w)
  342. *w = ext.xOff;
  343. if (h)
  344. *h = font->h;
  345. }
  346. Cur *
  347. drw_cur_create(Drw *drw, int shape)
  348. {
  349. Cur *cur;
  350. if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  351. return NULL;
  352. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  353. return cur;
  354. }
  355. void
  356. drw_cur_free(Drw *drw, Cur *cursor)
  357. {
  358. if (!cursor)
  359. return;
  360. XFreeCursor(drw->dpy, cursor->cursor);
  361. free(cursor);
  362. }