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.

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