Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

438 lines
10 KiB

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