dmenu for lunch applications in dwm
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.

408 lines
9.4 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. if (!(fontname || fontpattern))
  97. die("No font specified.\n");
  98. if (!(font = calloc(1, sizeof(Fnt))))
  99. return NULL;
  100. if (fontname) {
  101. /* Using the pattern found at font->xfont->pattern does not yield same
  102. * the same substitution results as using the pattern returned by
  103. * FcNameParse; using the latter results in the desired fallback
  104. * behaviour whereas the former just results in
  105. * missing-character-rectangles being drawn, at least with some fonts.
  106. */
  107. if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) ||
  108. !(font->pattern = FcNameParse((FcChar8 *) fontname))) {
  109. if (font->xfont) {
  110. XftFontClose(drw->dpy, font->xfont);
  111. font->xfont = NULL;
  112. }
  113. fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
  114. }
  115. } else if (fontpattern) {
  116. if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern)))
  117. fprintf(stderr, "error, cannot load font pattern.\n");
  118. else
  119. font->pattern = NULL;
  120. }
  121. if (!font->xfont) {
  122. free(font);
  123. return NULL;
  124. }
  125. font->ascent = font->xfont->ascent;
  126. font->descent = font->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. Colormap cmap;
  199. Visual *vis;
  200. XftDraw *d;
  201. Fnt *curfont, *nextfont;
  202. size_t i, len;
  203. int utf8strlen, utf8charlen, render;
  204. long utf8codepoint = 0;
  205. const char *utf8str;
  206. FcCharSet *fccharset;
  207. FcPattern *fcpattern;
  208. FcPattern *match;
  209. XftResult result;
  210. int charexists = 0;
  211. if (!(render = x || y || w || h))
  212. w = ~w;
  213. if (!drw || !drw->scheme) {
  214. return 0;
  215. } else if (render) {
  216. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->pix : drw->scheme->bg->pix);
  217. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  218. }
  219. if (!text || !drw->fontcount) {
  220. return 0;
  221. } else if (render) {
  222. cmap = DefaultColormap(drw->dpy, drw->screen);
  223. vis = DefaultVisual(drw->dpy, drw->screen);
  224. d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap);
  225. }
  226. curfont = drw->fonts[0];
  227. while (1) {
  228. utf8strlen = 0;
  229. utf8str = text;
  230. nextfont = NULL;
  231. while (*text) {
  232. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  233. for (i = 0; i < drw->fontcount; i++) {
  234. charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
  235. if (charexists) {
  236. if (drw->fonts[i] == curfont) {
  237. utf8strlen += utf8charlen;
  238. text += utf8charlen;
  239. } else {
  240. nextfont = drw->fonts[i];
  241. }
  242. break;
  243. }
  244. }
  245. if (!charexists || (nextfont && nextfont != curfont))
  246. break;
  247. else
  248. charexists = 0;
  249. }
  250. if (utf8strlen) {
  251. drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
  252. /* shorten text if necessary */
  253. for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
  254. drw_font_getexts(curfont, utf8str, len, &tex);
  255. if (len) {
  256. memcpy(buf, utf8str, len);
  257. buf[len] = '\0';
  258. if (len < utf8strlen)
  259. for (i = len; i && i > len - 3; buf[--i] = '.');
  260. if (render) {
  261. th = curfont->ascent + curfont->descent;
  262. ty = y + (h / 2) - (th / 2) + curfont->ascent;
  263. tx = x + (h / 2);
  264. XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
  265. }
  266. x += tex.w;
  267. w -= tex.w;
  268. }
  269. }
  270. if (!*text) {
  271. break;
  272. } else if (nextfont) {
  273. charexists = 0;
  274. curfont = nextfont;
  275. } else {
  276. /* Regardless of whether or not a fallback font is found, the
  277. * character must be drawn.
  278. */
  279. charexists = 1;
  280. if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
  281. continue;
  282. fccharset = FcCharSetCreate();
  283. FcCharSetAddChar(fccharset, utf8codepoint);
  284. if (!drw->fonts[0]->pattern) {
  285. /* Refer to the comment in drw_font_xcreate for more
  286. * information. */
  287. die("The first font in the cache must be loaded from a font string.\n");
  288. }
  289. fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
  290. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  291. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  292. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  293. FcDefaultSubstitute(fcpattern);
  294. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  295. FcCharSetDestroy(fccharset);
  296. FcPatternDestroy(fcpattern);
  297. if (match) {
  298. curfont = drw_font_xcreate(drw, NULL, match);
  299. if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
  300. drw->fonts[drw->fontcount++] = curfont;
  301. } else {
  302. if (curfont)
  303. drw_font_free(curfont);
  304. curfont = drw->fonts[0];
  305. }
  306. }
  307. }
  308. }
  309. if (render)
  310. XftDrawDestroy(d);
  311. return x;
  312. }
  313. void
  314. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  315. {
  316. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  317. XSync(drw->dpy, False);
  318. }
  319. void
  320. drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
  321. {
  322. XGlyphInfo ext;
  323. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  324. tex->h = font->h;
  325. tex->w = ext.xOff;
  326. }
  327. unsigned int
  328. drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
  329. {
  330. Extnts tex;
  331. drw_font_getexts(font, text, len, &tex);
  332. return tex.w;
  333. }
  334. Cur *
  335. drw_cur_create(Drw *drw, int shape)
  336. {
  337. Cur *cur;
  338. cur = ecalloc(1, sizeof(Cur));
  339. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  340. return cur;
  341. }
  342. void
  343. drw_cur_free(Drw *drw, Cur *cursor)
  344. {
  345. if (!cursor)
  346. return;
  347. XFreeCursor(drw->dpy, cursor->cursor);
  348. free(cursor);
  349. }