My dmenu build
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.
 
 
 
 
 
 

399 lines
9.3 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;
  58. drw = ecalloc(1, sizeof(Drw));
  59. drw->dpy = dpy;
  60. drw->screen = screen;
  61. drw->root = root;
  62. drw->w = w;
  63. drw->h = h;
  64. drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  65. drw->gc = XCreateGC(dpy, root, 0, NULL);
  66. drw->fontcount = 0;
  67. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  68. return drw;
  69. }
  70. void
  71. drw_resize(Drw *drw, unsigned int w, unsigned int h)
  72. {
  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. size_t i;
  83. for (i = 0; i < drw->fontcount; i++)
  84. drw_font_free(drw->fonts[i]);
  85. XFreePixmap(drw->dpy, drw->drawable);
  86. XFreeGC(drw->dpy, drw->gc);
  87. free(drw);
  88. }
  89. /* This function is an implementation detail. Library users should use
  90. * drw_font_create instead.
  91. */
  92. static Fnt *
  93. drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
  94. {
  95. Fnt *font;
  96. XftFont *xfont = NULL;
  97. FcPattern *pattern = NULL;
  98. if (fontname) {
  99. /* Using the pattern found at font->xfont->pattern does not yield same
  100. * the same substitution results as using the pattern returned by
  101. * FcNameParse; using the latter results in the desired fallback
  102. * behaviour whereas the former just results in
  103. * missing-character-rectangles being drawn, at least with some fonts.
  104. */
  105. if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  106. fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
  107. return NULL;
  108. }
  109. if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  110. fprintf(stderr, "error, cannot load font: '%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 pattern.\n");
  117. return NULL;
  118. }
  119. } else {
  120. die("no font specified.\n");
  121. }
  122. font = ecalloc(1, sizeof(Fnt));
  123. font->xfont = xfont;
  124. font->pattern = pattern;
  125. font->ascent = xfont->ascent;
  126. font->descent = xfont->descent;
  127. font->h = font->ascent + font->descent;
  128. font->dpy = drw->dpy;
  129. return font;
  130. }
  131. Fnt*
  132. drw_font_create(Drw *drw, const char *fontname)
  133. {
  134. return drw_font_xcreate(drw, fontname, NULL);
  135. }
  136. void
  137. drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
  138. {
  139. size_t i;
  140. Fnt *font;
  141. for (i = 0; i < fontcount; i++) {
  142. if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
  143. die("Font cache exhausted.\n");
  144. } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
  145. drw->fonts[drw->fontcount++] = font;
  146. }
  147. }
  148. }
  149. void
  150. drw_font_free(Fnt *font)
  151. {
  152. if (!font)
  153. return;
  154. if (font->pattern)
  155. FcPatternDestroy(font->pattern);
  156. XftFontClose(font->dpy, font->xfont);
  157. free(font);
  158. }
  159. Clr *
  160. drw_clr_create(Drw *drw, const char *clrname)
  161. {
  162. Clr *clr;
  163. clr = ecalloc(1, sizeof(Clr));
  164. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  165. DefaultColormap(drw->dpy, drw->screen),
  166. clrname, &clr->rgb))
  167. die("error, cannot allocate color '%s'\n", clrname);
  168. clr->pix = clr->rgb.pixel;
  169. return clr;
  170. }
  171. void
  172. drw_clr_free(Clr *clr)
  173. {
  174. free(clr);
  175. }
  176. void
  177. drw_setscheme(Drw *drw, ClrScheme *scheme)
  178. {
  179. drw->scheme = scheme;
  180. }
  181. void
  182. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
  183. {
  184. if (!drw->scheme)
  185. return;
  186. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
  187. if (filled)
  188. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
  189. else if (empty)
  190. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  191. }
  192. int
  193. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
  194. {
  195. char buf[1024];
  196. int tx, ty, th;
  197. Extnts tex;
  198. XftDraw *d = NULL;
  199. Fnt *curfont, *nextfont;
  200. size_t i, len;
  201. int utf8strlen, utf8charlen, render;
  202. long utf8codepoint = 0;
  203. const char *utf8str;
  204. FcCharSet *fccharset;
  205. FcPattern *fcpattern;
  206. FcPattern *match;
  207. XftResult result;
  208. int charexists = 0;
  209. if (!drw->scheme || !drw->fontcount)
  210. return 0;
  211. if (!(render = x || y || w || h)) {
  212. w = ~w;
  213. } else {
  214. XSetForeground(drw->dpy, drw->gc, invert ?
  215. drw->scheme->fg->pix : drw->scheme->bg->pix);
  216. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  217. d = XftDrawCreate(drw->dpy, drw->drawable,
  218. DefaultVisual(drw->dpy, drw->screen),
  219. DefaultColormap(drw->dpy, drw->screen));
  220. }
  221. curfont = drw->fonts[0];
  222. while (1) {
  223. utf8strlen = 0;
  224. utf8str = text;
  225. nextfont = NULL;
  226. while (*text) {
  227. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  228. for (i = 0; i < drw->fontcount; i++) {
  229. charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
  230. if (charexists) {
  231. if (drw->fonts[i] == curfont) {
  232. utf8strlen += utf8charlen;
  233. text += utf8charlen;
  234. } else {
  235. nextfont = drw->fonts[i];
  236. }
  237. break;
  238. }
  239. }
  240. if (!charexists || (nextfont && nextfont != curfont))
  241. break;
  242. else
  243. charexists = 0;
  244. }
  245. if (utf8strlen) {
  246. drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
  247. /* shorten text if necessary */
  248. for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
  249. drw_font_getexts(curfont, utf8str, len, &tex);
  250. if (len) {
  251. memcpy(buf, utf8str, len);
  252. buf[len] = '\0';
  253. if (len < utf8strlen)
  254. for (i = len; i && i > len - 3; buf[--i] = '.');
  255. if (render) {
  256. th = curfont->ascent + curfont->descent;
  257. ty = y + (h / 2) - (th / 2) + curfont->ascent;
  258. tx = x + (h / 2);
  259. XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
  260. }
  261. x += tex.w;
  262. w -= tex.w;
  263. }
  264. }
  265. if (!*text) {
  266. break;
  267. } else if (nextfont) {
  268. charexists = 0;
  269. curfont = nextfont;
  270. } else {
  271. /* Regardless of whether or not a fallback font is found, the
  272. * character must be drawn.
  273. */
  274. charexists = 1;
  275. if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
  276. continue;
  277. fccharset = FcCharSetCreate();
  278. FcCharSetAddChar(fccharset, utf8codepoint);
  279. if (!drw->fonts[0]->pattern) {
  280. /* Refer to the comment in drw_font_xcreate for more
  281. * information. */
  282. die("the first font in the cache must be loaded from a font string.\n");
  283. }
  284. fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
  285. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  286. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  287. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  288. FcDefaultSubstitute(fcpattern);
  289. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  290. FcCharSetDestroy(fccharset);
  291. FcPatternDestroy(fcpattern);
  292. if (match) {
  293. curfont = drw_font_xcreate(drw, NULL, match);
  294. if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
  295. drw->fonts[drw->fontcount++] = curfont;
  296. } else {
  297. drw_font_free(curfont);
  298. curfont = drw->fonts[0];
  299. }
  300. }
  301. }
  302. }
  303. if (d)
  304. XftDrawDestroy(d);
  305. return x;
  306. }
  307. void
  308. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  309. {
  310. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  311. XSync(drw->dpy, False);
  312. }
  313. void
  314. drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
  315. {
  316. XGlyphInfo ext;
  317. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  318. tex->h = font->h;
  319. tex->w = ext.xOff;
  320. }
  321. unsigned int
  322. drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
  323. {
  324. Extnts tex;
  325. drw_font_getexts(font, text, len, &tex);
  326. return tex.w;
  327. }
  328. Cur *
  329. drw_cur_create(Drw *drw, int shape)
  330. {
  331. Cur *cur;
  332. cur = ecalloc(1, sizeof(Cur));
  333. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  334. return cur;
  335. }
  336. void
  337. drw_cur_free(Drw *drw, Cur *cursor)
  338. {
  339. if (!cursor)
  340. return;
  341. XFreeCursor(drw->dpy, cursor->cursor);
  342. free(cursor);
  343. }