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.

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