Configuration file for DWM on MacBook Air
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

  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, Visual *visual, unsigned int depth, Colormap cmap)
  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->visual = visual;
  64. drw->depth = depth;
  65. drw->cmap = cmap;
  66. drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
  67. drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
  68. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  69. return drw;
  70. }
  71. void
  72. drw_resize(Drw *drw, unsigned int w, unsigned int h)
  73. {
  74. if (!drw)
  75. return;
  76. drw->w = w;
  77. drw->h = h;
  78. if (drw->drawable)
  79. XFreePixmap(drw->dpy, drw->drawable);
  80. drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
  81. }
  82. void
  83. drw_free(Drw *drw)
  84. {
  85. XFreePixmap(drw->dpy, drw->drawable);
  86. XFreeGC(drw->dpy, drw->gc);
  87. drw_fontset_free(drw->fonts);
  88. free(drw);
  89. }
  90. /* This function is an implementation detail. Library users should use
  91. * drw_fontset_create instead.
  92. */
  93. static Fnt *
  94. xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
  95. {
  96. Fnt *font;
  97. XftFont *xfont = NULL;
  98. FcPattern *pattern = NULL;
  99. if (fontname) {
  100. /* Using the pattern found at font->xfont->pattern does not yield the
  101. * same substitution results as using the pattern returned by
  102. * FcNameParse; using the latter results in the desired fallback
  103. * behaviour whereas the former just results in missing-character
  104. * rectangles being drawn, at least with some fonts. */
  105. if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  106. fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
  107. return NULL;
  108. }
  109. if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  110. fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
  111. XftFontClose(drw->dpy, xfont);
  112. return NULL;
  113. }
  114. } else if (fontpattern) {
  115. if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
  116. fprintf(stderr, "error, cannot load font from pattern.\n");
  117. return NULL;
  118. }
  119. } else {
  120. die("no font specified.");
  121. }
  122. /* Do not allow using color fonts. This is a workaround for a BadLength
  123. * error from Xft with color glyphs. Modelled on the Xterm workaround. See
  124. * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
  125. * https://lists.suckless.org/dev/1701/30932.html
  126. * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
  127. * and lots more all over the internet.
  128. */
  129. FcBool iscol;
  130. if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
  131. XftFontClose(drw->dpy, xfont);
  132. return NULL;
  133. }
  134. font = ecalloc(1, sizeof(Fnt));
  135. font->xfont = xfont;
  136. font->pattern = pattern;
  137. font->h = xfont->ascent + xfont->descent;
  138. font->dpy = drw->dpy;
  139. return font;
  140. }
  141. static void
  142. xfont_free(Fnt *font)
  143. {
  144. if (!font)
  145. return;
  146. if (font->pattern)
  147. FcPatternDestroy(font->pattern);
  148. XftFontClose(font->dpy, font->xfont);
  149. free(font);
  150. }
  151. Fnt*
  152. drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  153. {
  154. Fnt *cur, *ret = NULL;
  155. size_t i;
  156. if (!drw || !fonts)
  157. return NULL;
  158. for (i = 1; i <= fontcount; i++) {
  159. if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
  160. cur->next = ret;
  161. ret = cur;
  162. }
  163. }
  164. return (drw->fonts = ret);
  165. }
  166. void
  167. drw_fontset_free(Fnt *font)
  168. {
  169. if (font) {
  170. drw_fontset_free(font->next);
  171. xfont_free(font);
  172. }
  173. }
  174. void
  175. drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha)
  176. {
  177. if (!drw || !dest || !clrname)
  178. return;
  179. if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
  180. clrname, dest))
  181. die("error, cannot allocate color '%s'", clrname);
  182. dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
  183. }
  184. /* Wrapper to create color schemes. The caller has to call free(3) on the
  185. * returned color scheme when done using it. */
  186. Clr *
  187. drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount)
  188. {
  189. size_t i;
  190. Clr *ret;
  191. /* need at least two colors for a scheme */
  192. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  193. return NULL;
  194. for (i = 0; i < clrcount; i++)
  195. drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
  196. return ret;
  197. }
  198. void
  199. drw_setfontset(Drw *drw, Fnt *set)
  200. {
  201. if (drw)
  202. drw->fonts = set;
  203. }
  204. void
  205. drw_setscheme(Drw *drw, Clr *scm)
  206. {
  207. if (drw)
  208. drw->scheme = scm;
  209. }
  210. void
  211. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  212. {
  213. if (!drw || !drw->scheme)
  214. return;
  215. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  216. if (filled)
  217. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  218. else
  219. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  220. }
  221. int
  222. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  223. {
  224. char buf[1024];
  225. int ty;
  226. unsigned int ew;
  227. XftDraw *d = NULL;
  228. Fnt *usedfont, *curfont, *nextfont;
  229. size_t i, len;
  230. int utf8strlen, utf8charlen, render = x || y || w || h;
  231. long utf8codepoint = 0;
  232. const char *utf8str;
  233. FcCharSet *fccharset;
  234. FcPattern *fcpattern;
  235. FcPattern *match;
  236. XftResult result;
  237. int charexists = 0;
  238. if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
  239. return 0;
  240. if (!render) {
  241. w = ~w;
  242. } else {
  243. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  244. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  245. d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
  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. }