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.

228 lines
5.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 "drw.h"
  7. #include "util.h"
  8. Drw *
  9. drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
  10. Drw *drw = (Drw *)calloc(1, sizeof(Drw));
  11. if(!drw)
  12. return NULL;
  13. drw->dpy = dpy;
  14. drw->screen = screen;
  15. drw->root = root;
  16. drw->w = w;
  17. drw->h = h;
  18. drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  19. drw->gc = XCreateGC(dpy, root, 0, NULL);
  20. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  21. return drw;
  22. }
  23. void
  24. drw_resize(Drw *drw, unsigned int w, unsigned int h) {
  25. if(!drw)
  26. return;
  27. drw->w = w;
  28. drw->h = h;
  29. if(drw->drawable != 0)
  30. XFreePixmap(drw->dpy, drw->drawable);
  31. drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
  32. }
  33. void
  34. drw_free(Drw *drw) {
  35. XFreePixmap(drw->dpy, drw->drawable);
  36. XFreeGC(drw->dpy, drw->gc);
  37. free(drw);
  38. }
  39. Fnt *
  40. drw_font_create(Display *dpy, const char *fontname) {
  41. Fnt *font;
  42. char *def, **missing;
  43. int n;
  44. font = (Fnt *)calloc(1, sizeof(Fnt));
  45. if(!font)
  46. return NULL;
  47. font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
  48. if(missing) {
  49. while(n--)
  50. fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
  51. XFreeStringList(missing);
  52. }
  53. if(font->set) {
  54. XFontStruct **xfonts;
  55. char **font_names;
  56. XExtentsOfFontSet(font->set);
  57. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  58. while(n--) {
  59. font->ascent = MAX(font->ascent, (*xfonts)->ascent);
  60. font->descent = MAX(font->descent,(*xfonts)->descent);
  61. xfonts++;
  62. }
  63. }
  64. else {
  65. if(!(font->xfont = XLoadQueryFont(dpy, fontname))
  66. && !(font->xfont = XLoadQueryFont(dpy, "fixed")))
  67. die("error, cannot load font: '%s'\n", fontname);
  68. font->ascent = font->xfont->ascent;
  69. font->descent = font->xfont->descent;
  70. }
  71. font->h = font->ascent + font->descent;
  72. return font;
  73. }
  74. void
  75. drw_font_free(Display *dpy, Fnt *font) {
  76. if(!font)
  77. return;
  78. if(font->set)
  79. XFreeFontSet(dpy, font->set);
  80. else
  81. XFreeFont(dpy, font->xfont);
  82. free(font);
  83. }
  84. Clr *
  85. drw_clr_create(Drw *drw, const char *clrname) {
  86. Clr *clr;
  87. Colormap cmap;
  88. XColor color;
  89. if(!drw)
  90. return NULL;
  91. clr = (Clr *)calloc(1, sizeof(Clr));
  92. if(!clr)
  93. return NULL;
  94. cmap = DefaultColormap(drw->dpy, drw->screen);
  95. if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
  96. die("error, cannot allocate color '%s'\n", clrname);
  97. clr->rgb = color.pixel;
  98. return clr;
  99. }
  100. void
  101. drw_clr_free(Clr *clr) {
  102. if(!clr)
  103. return;
  104. free(clr);
  105. }
  106. void
  107. drw_setfont(Drw *drw, Fnt *font) {
  108. if(drw)
  109. drw->font = font;
  110. }
  111. void
  112. drw_settheme(Drw *drw, Theme *theme) {
  113. if(!drw || !theme)
  114. return;
  115. drw->theme = theme;
  116. }
  117. void
  118. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
  119. int dx;
  120. if(!drw || !drw->font || !drw->theme)
  121. return;
  122. XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
  123. dx = (drw->font->ascent + drw->font->descent + 2) / 4;
  124. if(filled)
  125. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
  126. else if(empty)
  127. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx);
  128. }
  129. void
  130. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
  131. char buf[256];
  132. int i, tx, ty, th, len, olen;
  133. Extnts tex;
  134. if(!drw || !drw->theme)
  135. return;
  136. XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->fg->rgb : drw->theme->bg->rgb);
  137. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  138. if(!text || !drw->font)
  139. return;
  140. olen = strlen(text);
  141. drw_font_getexts(drw->font, text, olen, &tex);
  142. th = drw->font->ascent + drw->font->descent;
  143. ty = y + (h / 2) - (th / 2) + drw->font->ascent;
  144. tx = x + (h / 2);
  145. /* shorten text if necessary */
  146. for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
  147. drw_font_getexts(drw->font, text, len, &tex);
  148. if(!len)
  149. return;
  150. memcpy(buf, text, len);
  151. if(len < olen)
  152. for(i = len; i && i > len - 3; buf[--i] = '.');
  153. XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
  154. if(drw->font->set)
  155. XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
  156. else
  157. XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
  158. }
  159. void
  160. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
  161. if(!drw)
  162. return;
  163. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  164. XSync(drw->dpy, False);
  165. }
  166. void
  167. drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
  168. XRectangle r;
  169. if(!font || !text)
  170. return;
  171. if(font->set) {
  172. XmbTextExtents(font->set, text, len, NULL, &r);
  173. tex->w = r.width;
  174. tex->h = r.height;
  175. }
  176. else {
  177. tex->h = font->ascent + font->descent;
  178. tex->w = XTextWidth(font->xfont, text, len);
  179. }
  180. }
  181. unsigned int
  182. drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
  183. Extnts tex;
  184. if(!font)
  185. return -1;
  186. drw_font_getexts(font, text, len, &tex);
  187. return tex.w;
  188. }
  189. Cur *
  190. drw_cur_create(Drw *drw, int shape) {
  191. Cur *cur = (Cur *)calloc(1, sizeof(Cur));
  192. if(!drw || !cur)
  193. return NULL;
  194. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  195. return cur;
  196. }
  197. void
  198. drw_cur_free(Drw *drw, Cur *cursor) {
  199. if(!drw || !cursor)
  200. return;
  201. XFreeCursor(drw->dpy, cursor->cursor);
  202. free(cursor);
  203. }