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.

448 lines
10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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(
  173. Drw *drw,
  174. Clr *dest,
  175. const char *clrname
  176. ) {
  177. if (!drw || !dest || !clrname)
  178. return;
  179. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  180. DefaultColormap(drw->dpy, drw->screen),
  181. clrname, dest))
  182. die("error, cannot allocate color '%s'", clrname);
  183. dest->pixel |= 0xff << 24;
  184. }
  185. /* Wrapper to create color schemes. The caller has to call free(3) on the
  186. * returned color scheme when done using it. */
  187. Clr *
  188. drw_scm_create(
  189. Drw *drw,
  190. char *clrnames[],
  191. size_t clrcount
  192. ) {
  193. size_t i;
  194. Clr *ret;
  195. /* need at least two colors for a scheme */
  196. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  197. return NULL;
  198. for (i = 0; i < clrcount; i++)
  199. drw_clr_create(drw, &ret[i], clrnames[i]);
  200. return ret;
  201. }
  202. void
  203. drw_setfontset(Drw *drw, Fnt *set)
  204. {
  205. if (drw)
  206. drw->fonts = set;
  207. }
  208. void
  209. drw_setscheme(Drw *drw, Clr *scm)
  210. {
  211. if (drw)
  212. drw->scheme = scm;
  213. }
  214. void
  215. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  216. {
  217. if (!drw || !drw->scheme)
  218. return;
  219. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  220. if (filled)
  221. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  222. else
  223. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  224. }
  225. int
  226. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool ignored)
  227. {
  228. char buf[1024];
  229. int ty;
  230. unsigned int ew;
  231. XftDraw *d = NULL;
  232. Fnt *usedfont, *curfont, *nextfont;
  233. size_t i, len;
  234. int utf8strlen, utf8charlen, render = x || y || w || h;
  235. long utf8codepoint = 0;
  236. const char *utf8str;
  237. FcCharSet *fccharset;
  238. FcPattern *fcpattern;
  239. FcPattern *match;
  240. XftResult result;
  241. int charexists = 0;
  242. if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
  243. return 0;
  244. if (!render) {
  245. w = ~w;
  246. } else {
  247. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  248. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  249. d = XftDrawCreate(drw->dpy, drw->drawable,
  250. DefaultVisual(drw->dpy, drw->screen),
  251. DefaultColormap(drw->dpy, drw->screen));
  252. x += lpad;
  253. w -= lpad;
  254. }
  255. usedfont = drw->fonts;
  256. while (1) {
  257. utf8strlen = 0;
  258. utf8str = text;
  259. nextfont = NULL;
  260. while (*text) {
  261. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  262. for (curfont = drw->fonts; curfont; curfont = curfont->next) {
  263. charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
  264. if (charexists) {
  265. if (curfont == usedfont) {
  266. utf8strlen += utf8charlen;
  267. text += utf8charlen;
  268. } else {
  269. nextfont = curfont;
  270. }
  271. break;
  272. }
  273. }
  274. if (!charexists || nextfont)
  275. break;
  276. else
  277. charexists = 0;
  278. }
  279. if (utf8strlen) {
  280. drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
  281. /* shorten text if necessary */
  282. for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
  283. drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
  284. if (len) {
  285. memcpy(buf, utf8str, len);
  286. buf[len] = '\0';
  287. if (len < utf8strlen)
  288. for (i = len; i && i > len - 3; buf[--i] = '.')
  289. ; /* NOP */
  290. if (render) {
  291. ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
  292. XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
  293. usedfont->xfont, x, ty, (XftChar8 *)buf, len);
  294. }
  295. x += ew;
  296. w -= ew;
  297. }
  298. }
  299. if (!*text) {
  300. break;
  301. } else if (nextfont) {
  302. charexists = 0;
  303. usedfont = nextfont;
  304. } else {
  305. /* Regardless of whether or not a fallback font is found, the
  306. * character must be drawn. */
  307. charexists = 1;
  308. fccharset = FcCharSetCreate();
  309. FcCharSetAddChar(fccharset, utf8codepoint);
  310. if (!drw->fonts->pattern) {
  311. /* Refer to the comment in xfont_create for more information. */
  312. die("the first font in the cache must be loaded from a font string.");
  313. }
  314. fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  315. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  316. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  317. FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
  318. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  319. FcDefaultSubstitute(fcpattern);
  320. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  321. FcCharSetDestroy(fccharset);
  322. FcPatternDestroy(fcpattern);
  323. if (match) {
  324. usedfont = xfont_create(drw, NULL, match);
  325. if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  326. for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  327. ; /* NOP */
  328. curfont->next = usedfont;
  329. } else {
  330. xfont_free(usedfont);
  331. usedfont = drw->fonts;
  332. }
  333. }
  334. }
  335. }
  336. if (d)
  337. XftDrawDestroy(d);
  338. return x + (render ? w : 0);
  339. }
  340. void
  341. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  342. {
  343. if (!drw)
  344. return;
  345. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  346. XSync(drw->dpy, False);
  347. }
  348. unsigned int
  349. drw_fontset_getwidth(Drw *drw, const char *text, Bool markup)
  350. {
  351. if (!drw || !drw->fonts || !text)
  352. return 0;
  353. return drw_text(drw, 0, 0, 0, 0, 0, text, 0, markup);
  354. }
  355. void
  356. drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  357. {
  358. XGlyphInfo ext;
  359. if (!font || !text)
  360. return;
  361. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  362. if (w)
  363. *w = ext.xOff;
  364. if (h)
  365. *h = font->h;
  366. }
  367. Cur *
  368. drw_cur_create(Drw *drw, int shape)
  369. {
  370. Cur *cur;
  371. if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  372. return NULL;
  373. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  374. return cur;
  375. }
  376. void
  377. drw_cur_free(Drw *drw, Cur *cursor)
  378. {
  379. if (!cursor)
  380. return;
  381. XFreeCursor(drw->dpy, cursor->cursor);
  382. free(cursor);
  383. }