Configuration of dwm for Mac Computers
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.

226 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. free(clr);
  104. }
  105. void
  106. drw_setfont(Drw *drw, Fnt *font) {
  107. if(drw)
  108. drw->font = font;
  109. }
  110. void
  111. drw_setscheme(Drw *drw, ClrScheme *scheme) {
  112. if(drw && scheme)
  113. drw->scheme = scheme;
  114. }
  115. void
  116. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
  117. int dx;
  118. if(!drw || !drw->font || !drw->scheme)
  119. return;
  120. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
  121. dx = (drw->font->ascent + drw->font->descent + 2) / 4;
  122. if(filled)
  123. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
  124. else if(empty)
  125. XDrawRectangle(drw->dpy, drw->drawable, 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, int invert) {
  129. char buf[256];
  130. int i, tx, ty, th, len, olen;
  131. Extnts tex;
  132. if(!drw || !drw->scheme)
  133. return;
  134. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->rgb : drw->scheme->bg->rgb);
  135. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  136. if(!text || !drw->font)
  137. return;
  138. olen = strlen(text);
  139. drw_font_getexts(drw->font, text, olen, &tex);
  140. th = drw->font->ascent + drw->font->descent;
  141. ty = y + (h / 2) - (th / 2) + drw->font->ascent;
  142. tx = x + (h / 2);
  143. /* shorten text if necessary */
  144. for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
  145. drw_font_getexts(drw->font, text, len, &tex);
  146. if(!len)
  147. return;
  148. memcpy(buf, text, len);
  149. if(len < olen)
  150. for(i = len; i && i > len - 3; buf[--i] = '.');
  151. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
  152. if(drw->font->set)
  153. XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
  154. else
  155. XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
  156. }
  157. void
  158. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
  159. if(!drw)
  160. return;
  161. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  162. XSync(drw->dpy, False);
  163. }
  164. void
  165. drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
  166. XRectangle r;
  167. if(!font || !text)
  168. return;
  169. if(font->set) {
  170. XmbTextExtents(font->set, text, len, NULL, &r);
  171. tex->w = r.width;
  172. tex->h = r.height;
  173. }
  174. else {
  175. tex->h = font->ascent + font->descent;
  176. tex->w = XTextWidth(font->xfont, text, len);
  177. }
  178. }
  179. unsigned int
  180. drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
  181. Extnts tex;
  182. if(!font)
  183. return -1;
  184. drw_font_getexts(font, text, len, &tex);
  185. return tex.w;
  186. }
  187. Cur *
  188. drw_cur_create(Drw *drw, int shape) {
  189. Cur *cur = (Cur *)calloc(1, sizeof(Cur));
  190. if(!drw || !cur)
  191. return NULL;
  192. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  193. return cur;
  194. }
  195. void
  196. drw_cur_free(Drw *drw, Cur *cursor) {
  197. if(!drw || !cursor)
  198. return;
  199. XFreeCursor(drw->dpy, cursor->cursor);
  200. free(cursor);
  201. }