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
4.8 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 win, unsigned int w, unsigned int h) {
  10. Drw *drw = (Drw *)calloc(1, sizeof(Drw));
  11. drw->dpy = dpy;
  12. drw->screen = screen;
  13. drw->win = win;
  14. drw->w = w;
  15. drw->h = h;
  16. drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
  17. drw->gc = XCreateGC(dpy, win, 0, NULL);
  18. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  19. return drw;
  20. }
  21. void
  22. drw_resize(Drw *drw, unsigned int w, unsigned int h) {
  23. if(!drw)
  24. return;
  25. drw->w = w;
  26. drw->h = h;
  27. XFreePixmap(drw->dpy, drw->drwable);
  28. drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
  29. }
  30. void
  31. drw_free(Drw *drw) {
  32. XFreePixmap(drw->dpy, drw->drwable);
  33. XFreeGC(drw->dpy, drw->gc);
  34. free(drw);
  35. }
  36. Fnt *
  37. drw_font_create(Drw *drw, const char *fontname) {
  38. Fnt *font;
  39. char *def, **missing;
  40. int n;
  41. if(!drw)
  42. return NULL;
  43. font = (Fnt *)calloc(1, sizeof(Fnt));
  44. font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
  45. if(missing) {
  46. while(n--)
  47. fprintf(stderr, "drw: 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(drw->dpy, fontname))
  63. && !(font->xfont = XLoadQueryFont(drw->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. drw_font_free(Drw *drw, Fnt *font) {
  73. if(!drw || !font)
  74. return;
  75. if(font->set)
  76. XFreeFontSet(drw->dpy, font->set);
  77. else
  78. XFreeFont(drw->dpy, font->xfont);
  79. free(font);
  80. }
  81. Clr *
  82. drw_clr_create(Drw *drw, const char *clrname) {
  83. Clr *clr = (Clr *)calloc(1, sizeof(Clr));
  84. Colormap cmap = DefaultColormap(drw->dpy, drw->screen);
  85. XColor color;
  86. if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
  87. die("error, cannot allocate color '%s'\n", clrname);
  88. clr->rgb = color.pixel;
  89. return clr;
  90. }
  91. void
  92. drw_clr_free(Drw *drw, Clr *clr) {
  93. if(!clr)
  94. return;
  95. free(clr);
  96. }
  97. void
  98. drw_setfont(Drw *drw, Fnt *font) {
  99. if(!drw)
  100. return;
  101. drw->font = font;
  102. }
  103. void
  104. drw_setfg(Drw *drw, Clr *clr) {
  105. if(!drw)
  106. return;
  107. drw->fg = clr;
  108. }
  109. void
  110. drw_setbg(Drw *drw, Clr *clr) {
  111. if(!drw)
  112. return;
  113. drw->bg = clr;
  114. }
  115. void
  116. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
  117. int dx;
  118. if(!drw || !drw->font || !drw->fg || !drw->bg)
  119. return;
  120. XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
  121. dx = (drw->font->ascent + drw->font->descent + 2) / 4;
  122. if(filled)
  123. XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
  124. else if(empty)
  125. XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
  126. }
  127. void
  128. drw_text(Drw *drw, 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. Extnts tex;
  132. if(!drw || !drw->fg || !drw->bg)
  133. return;
  134. XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb);
  135. XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h);
  136. if(!text || !drw->font)
  137. return;
  138. olen = strlen(text);
  139. drw_getexts(drw, 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. drw_getexts(drw, 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(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
  151. if(drw->font->set)
  152. XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
  153. else
  154. XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
  155. }
  156. void
  157. drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
  158. if(!drw)
  159. return;
  160. XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
  161. XSync(drw->dpy, False);
  162. }
  163. void
  164. drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
  165. XRectangle r;
  166. if(!drw || !drw->font || !text)
  167. return;
  168. if(drw->font->set) {
  169. XmbTextExtents(drw->font->set, text, len, NULL, &r);
  170. tex->xOff = r.x;
  171. tex->yOff = r.y;
  172. tex->w = r.width;
  173. tex->h = r.height;
  174. }
  175. else {
  176. tex->h = drw->font->ascent + drw->font->descent;
  177. tex->w = XTextWidth(drw->font->xfont, text, len);
  178. tex->xOff = tex->h / 2;
  179. tex->yOff = (tex->h / 2) + drw->font->ascent;
  180. }
  181. }