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.

435 lines
10 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.");
  117. }
  118. /* Do not allow using color fonts. This is a workaround for a BadLength
  119. * error from Xft with color glyphs. Modelled on the Xterm workaround. See
  120. * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
  121. * https://lists.suckless.org/dev/1701/30932.html
  122. * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
  123. * and lots more all over the internet.
  124. */
  125. FcBool iscol;
  126. if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
  127. XftFontClose(drw->dpy, xfont);
  128. return NULL;
  129. }
  130. font = ecalloc(1, sizeof(Fnt));
  131. font->xfont = xfont;
  132. font->pattern = pattern;
  133. font->h = xfont->ascent + xfont->descent;
  134. font->dpy = drw->dpy;
  135. return font;
  136. }
  137. static void
  138. xfont_free(Fnt *font)
  139. {
  140. if (!font)
  141. return;
  142. if (font->pattern)
  143. FcPatternDestroy(font->pattern);
  144. XftFontClose(font->dpy, font->xfont);
  145. free(font);
  146. }
  147. Fnt*
  148. drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  149. {
  150. Fnt *cur, *ret = NULL;
  151. size_t i;
  152. if (!drw || !fonts)
  153. return NULL;
  154. for (i = 1; i <= fontcount; i++) {
  155. if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
  156. cur->next = ret;
  157. ret = cur;
  158. }
  159. }
  160. return (drw->fonts = ret);
  161. }
  162. void
  163. drw_fontset_free(Fnt *font)
  164. {
  165. if (font) {
  166. drw_fontset_free(font->next);
  167. xfont_free(font);
  168. }
  169. }
  170. void
  171. drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
  172. {
  173. if (!drw || !dest || !clrname)
  174. return;
  175. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  176. DefaultColormap(drw->dpy, drw->screen),
  177. clrname, dest))
  178. die("error, cannot allocate color '%s'", clrname);
  179. }
  180. /* Wrapper to create color schemes. The caller has to call free(3) on the
  181. * returned color scheme when done using it. */
  182. Clr *
  183. drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  184. {
  185. size_t i;
  186. Clr *ret;
  187. /* need at least two colors for a scheme */
  188. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  189. return NULL;
  190. for (i = 0; i < clrcount; i++)
  191. drw_clr_create(drw, &ret[i], clrnames[i]);
  192. return ret;
  193. }
  194. void
  195. drw_setfontset(Drw *drw, Fnt *set)
  196. {
  197. if (drw)
  198. drw->fonts = set;
  199. }
  200. void
  201. drw_setscheme(Drw *drw, Clr *scm)
  202. {
  203. if (drw)
  204. drw->scheme = scm;
  205. }
  206. void
  207. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  208. {
  209. if (!drw || !drw->scheme)
  210. return;
  211. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  212. if (filled)
  213. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  214. else
  215. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  216. }
  217. int
  218. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  219. {
  220. char buf[1024];
  221. int ty;
  222. unsigned int ew;
  223. XftDraw *d = NULL;
  224. Fnt *usedfont, *curfont, *nextfont;
  225. size_t i, len;
  226. int utf8strlen, utf8charlen, render = x || y || w || h;
  227. long utf8codepoint = 0;
  228. const char *utf8str;
  229. FcCharSet *fccharset;
  230. FcPattern *fcpattern;
  231. FcPattern *match;
  232. XftResult result;
  233. int charexists = 0;
  234. if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
  235. return 0;
  236. if (!render) {
  237. w = ~w;
  238. } else {
  239. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  240. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  241. d = XftDrawCreate(drw->dpy, drw->drawable,
  242. DefaultVisual(drw->dpy, drw->screen),
  243. DefaultColormap(drw->dpy, drw->screen));
  244. x += lpad;
  245. w -= lpad;
  246. }
  247. usedfont = drw->fonts;
  248. while (1) {
  249. utf8strlen = 0;
  250. utf8str = text;
  251. nextfont = NULL;
  252. while (*text) {
  253. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  254. for (curfont = drw->fonts; curfont; curfont = curfont->next) {
  255. charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
  256. if (charexists) {
  257. if (curfont == usedfont) {
  258. utf8strlen += utf8charlen;
  259. text += utf8charlen;
  260. } else {
  261. nextfont = curfont;
  262. }
  263. break;
  264. }
  265. }
  266. if (!charexists || nextfont)
  267. break;
  268. else
  269. charexists = 0;
  270. }
  271. if (utf8strlen) {
  272. drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
  273. /* shorten text if necessary */
  274. for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
  275. drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
  276. if (len) {
  277. memcpy(buf, utf8str, len);
  278. buf[len] = '\0';
  279. if (len < utf8strlen)
  280. for (i = len; i && i > len - 3; buf[--i] = '.')
  281. ; /* NOP */
  282. if (render) {
  283. ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
  284. XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
  285. usedfont->xfont, x, ty, (XftChar8 *)buf, len);
  286. }
  287. x += ew;
  288. w -= ew;
  289. }
  290. }
  291. if (!*text) {
  292. break;
  293. } else if (nextfont) {
  294. charexists = 0;
  295. usedfont = nextfont;
  296. } else {
  297. /* Regardless of whether or not a fallback font is found, the
  298. * character must be drawn. */
  299. charexists = 1;
  300. fccharset = FcCharSetCreate();
  301. FcCharSetAddChar(fccharset, utf8codepoint);
  302. if (!drw->fonts->pattern) {
  303. /* Refer to the comment in xfont_create for more information. */
  304. die("the first font in the cache must be loaded from a font string.");
  305. }
  306. fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  307. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  308. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  309. FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
  310. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  311. FcDefaultSubstitute(fcpattern);
  312. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  313. FcCharSetDestroy(fccharset);
  314. FcPatternDestroy(fcpattern);
  315. if (match) {
  316. usedfont = xfont_create(drw, NULL, match);
  317. if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  318. for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  319. ; /* NOP */
  320. curfont->next = usedfont;
  321. } else {
  322. xfont_free(usedfont);
  323. usedfont = drw->fonts;
  324. }
  325. }
  326. }
  327. }
  328. if (d)
  329. XftDrawDestroy(d);
  330. return x + (render ? w : 0);
  331. }
  332. void
  333. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  334. {
  335. if (!drw)
  336. return;
  337. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  338. XSync(drw->dpy, False);
  339. }
  340. unsigned int
  341. drw_fontset_getwidth(Drw *drw, const char *text)
  342. {
  343. if (!drw || !drw->fonts || !text)
  344. return 0;
  345. return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
  346. }
  347. void
  348. drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  349. {
  350. XGlyphInfo ext;
  351. if (!font || !text)
  352. return;
  353. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  354. if (w)
  355. *w = ext.xOff;
  356. if (h)
  357. *h = font->h;
  358. }
  359. Cur *
  360. drw_cur_create(Drw *drw, int shape)
  361. {
  362. Cur *cur;
  363. if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  364. return NULL;
  365. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  366. return cur;
  367. }
  368. void
  369. drw_cur_free(Drw *drw, Cur *cursor)
  370. {
  371. if (!cursor)
  372. return;
  373. XFreeCursor(drw->dpy, cursor->cursor);
  374. free(cursor);
  375. }