diff options
| author | Anselm R Garbe <anselm@garbe.us> | 2013-06-16 15:20:29 +0200 |
|---|---|---|
| committer | Anselm R Garbe <anselm@garbe.us> | 2013-06-16 15:20:29 +0200 |
| commit | 5364697914fd4272fc1a6494b4fc522d2935427a (patch) | |
| tree | e315c431f0c1b13907eb7e3513e73fd40fc6b433 /drw.c | |
| parent | include font argument for st by default (diff) | |
| download | dwm-5364697914fd4272fc1a6494b4fc522d2935427a.tar.gz dwm-5364697914fd4272fc1a6494b4fc522d2935427a.zip | |
finished libsl/drw integration
Diffstat (limited to '')
| -rw-r--r-- | drw.c | 150 |
1 files changed, 88 insertions, 62 deletions
| @@ -8,15 +8,17 @@ | |||
| 8 | #include "util.h" | 8 | #include "util.h" |
| 9 | 9 | ||
| 10 | Drw * | 10 | Drw * |
| 11 | drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { | 11 | drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) { |
| 12 | Drw *drw = (Drw *)calloc(1, sizeof(Drw)); | 12 | Drw *drw = (Drw *)calloc(1, sizeof(Drw)); |
| 13 | if(!drw) | ||
| 14 | return NULL; | ||
| 13 | drw->dpy = dpy; | 15 | drw->dpy = dpy; |
| 14 | drw->screen = screen; | 16 | drw->screen = screen; |
| 15 | drw->win = win; | 17 | drw->root = root; |
| 16 | drw->w = w; | 18 | drw->w = w; |
| 17 | drw->h = h; | 19 | drw->h = h; |
| 18 | drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen)); | 20 | drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); |
| 19 | drw->gc = XCreateGC(dpy, win, 0, NULL); | 21 | drw->gc = XCreateGC(dpy, root, 0, NULL); |
| 20 | XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); | 22 | XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); |
| 21 | return drw; | 23 | return drw; |
| 22 | } | 24 | } |
| @@ -27,27 +29,28 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h) { | |||
| 27 | return; | 29 | return; |
| 28 | drw->w = w; | 30 | drw->w = w; |
| 29 | drw->h = h; | 31 | drw->h = h; |
| 30 | XFreePixmap(drw->dpy, drw->drwable); | 32 | if(drw->drawable != 0) |
| 31 | drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen)); | 33 | XFreePixmap(drw->dpy, drw->drawable); |
| 34 | drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen)); | ||
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | void | 37 | void |
| 35 | drw_free(Drw *drw) { | 38 | drw_free(Drw *drw) { |
| 36 | XFreePixmap(drw->dpy, drw->drwable); | 39 | XFreePixmap(drw->dpy, drw->drawable); |
| 37 | XFreeGC(drw->dpy, drw->gc); | 40 | XFreeGC(drw->dpy, drw->gc); |
| 38 | free(drw); | 41 | free(drw); |
| 39 | } | 42 | } |
| 40 | 43 | ||
| 41 | Fnt * | 44 | Fnt * |
| 42 | drw_font_create(Drw *drw, const char *fontname) { | 45 | drw_font_create(Display *dpy, const char *fontname) { |
| 43 | Fnt *font; | 46 | Fnt *font; |
| 44 | char *def, **missing; | 47 | char *def, **missing; |
| 45 | int n; | 48 | int n; |
| 46 | 49 | ||
| 47 | if(!drw) | ||
| 48 | return NULL; | ||
| 49 | font = (Fnt *)calloc(1, sizeof(Fnt)); | 50 | font = (Fnt *)calloc(1, sizeof(Fnt)); |
| 50 | font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def); | 51 | if(!font) |
| 52 | return NULL; | ||
| 53 | font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def); | ||
| 51 | if(missing) { | 54 | if(missing) { |
| 52 | while(n--) | 55 | while(n--) |
| 53 | fprintf(stderr, "drw: missing fontset: %s\n", missing[n]); | 56 | fprintf(stderr, "drw: missing fontset: %s\n", missing[n]); |
| @@ -65,8 +68,8 @@ drw_font_create(Drw *drw, const char *fontname) { | |||
| 65 | } | 68 | } |
| 66 | } | 69 | } |
| 67 | else { | 70 | else { |
| 68 | if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname)) | 71 | if(!(font->xfont = XLoadQueryFont(dpy, fontname)) |
| 69 | && !(font->xfont = XLoadQueryFont(drw->dpy, "fixed"))) | 72 | && !(font->xfont = XLoadQueryFont(dpy, "fixed"))) |
| 70 | die("error, cannot load font: '%s'\n", fontname); | 73 | die("error, cannot load font: '%s'\n", fontname); |
| 71 | font->ascent = font->xfont->ascent; | 74 | font->ascent = font->xfont->ascent; |
| 72 | font->descent = font->xfont->descent; | 75 | font->descent = font->xfont->descent; |
| @@ -76,22 +79,28 @@ drw_font_create(Drw *drw, const char *fontname) { | |||
| 76 | } | 79 | } |
| 77 | 80 | ||
| 78 | void | 81 | void |
| 79 | drw_font_free(Drw *drw, Fnt *font) { | 82 | drw_font_free(Display *dpy, Fnt *font) { |
| 80 | if(!drw || !font) | 83 | if(!font) |
| 81 | return; | 84 | return; |
| 82 | if(font->set) | 85 | if(font->set) |
| 83 | XFreeFontSet(drw->dpy, font->set); | 86 | XFreeFontSet(dpy, font->set); |
| 84 | else | 87 | else |
| 85 | XFreeFont(drw->dpy, font->xfont); | 88 | XFreeFont(dpy, font->xfont); |
| 86 | free(font); | 89 | free(font); |
| 87 | } | 90 | } |
| 88 | 91 | ||
| 89 | Clr * | 92 | Clr * |
| 90 | drw_clr_create(Drw *drw, const char *clrname) { | 93 | drw_clr_create(Drw *drw, const char *clrname) { |
| 91 | Clr *clr = (Clr *)calloc(1, sizeof(Clr)); | 94 | Clr *clr; |
| 92 | Colormap cmap = DefaultColormap(drw->dpy, drw->screen); | 95 | Colormap cmap; |
| 93 | XColor color; | 96 | XColor color; |
| 94 | 97 | ||
| 98 | if(!drw) | ||
| 99 | return NULL; | ||
| 100 | clr = (Clr *)calloc(1, sizeof(Clr)); | ||
| 101 | if(!clr) | ||
| 102 | return NULL; | ||
| 103 | cmap = DefaultColormap(drw->dpy, drw->screen); | ||
| 95 | if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color)) | 104 | if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color)) |
| 96 | die("error, cannot allocate color '%s'\n", clrname); | 105 | die("error, cannot allocate color '%s'\n", clrname); |
| 97 | clr->rgb = color.pixel; | 106 | clr->rgb = color.pixel; |
| @@ -99,7 +108,7 @@ drw_clr_create(Drw *drw, const char *clrname) { | |||
| 99 | } | 108 | } |
| 100 | 109 | ||
| 101 | void | 110 | void |
| 102 | drw_clr_free(Drw *drw, Clr *clr) { | 111 | drw_clr_free(Clr *clr) { |
| 103 | if(!clr) | 112 | if(!clr) |
| 104 | return; | 113 | return; |
| 105 | free(clr); | 114 | free(clr); |
| @@ -107,96 +116,113 @@ drw_clr_free(Drw *drw, Clr *clr) { | |||
| 107 | 116 | ||
| 108 | void | 117 | void |
| 109 | drw_setfont(Drw *drw, Fnt *font) { | 118 | drw_setfont(Drw *drw, Fnt *font) { |
| 110 | if(!drw) | 119 | if(drw) |
| 111 | return; | 120 | drw->font = font; |
| 112 | drw->font = font; | ||
| 113 | } | 121 | } |
| 114 | 122 | ||
| 115 | void | 123 | void |
| 116 | drw_setfg(Drw *drw, Clr *clr) { | 124 | drw_settheme(Drw *drw, Theme *theme) { |
| 117 | if(!drw) | 125 | if(!drw || !theme) |
| 118 | return; | 126 | return; |
| 119 | drw->fg = clr; | 127 | drw->theme = theme; |
| 120 | } | 128 | } |
| 121 | 129 | ||
| 122 | void | 130 | void |
| 123 | drw_setbg(Drw *drw, Clr *clr) { | 131 | drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) { |
| 124 | if(!drw) | ||
| 125 | return; | ||
| 126 | drw->bg = clr; | ||
| 127 | } | ||
| 128 | |||
| 129 | void | ||
| 130 | drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) { | ||
| 131 | int dx; | 132 | int dx; |
| 132 | 133 | ||
| 133 | if(!drw || !drw->font || !drw->fg || !drw->bg) | 134 | if(!drw || !drw->font || !drw->theme) |
| 134 | return; | 135 | return; |
| 135 | XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb); | 136 | XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb); |
| 136 | dx = (drw->font->ascent + drw->font->descent + 2) / 4; | 137 | dx = (drw->font->ascent + drw->font->descent + 2) / 4; |
| 137 | if(filled) | 138 | if(filled) |
| 138 | XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1); | 139 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1); |
| 139 | else if(empty) | 140 | else if(empty) |
| 140 | XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx); | 141 | XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx); |
| 141 | } | 142 | } |
| 142 | 143 | ||
| 143 | void | 144 | void |
| 144 | drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) { | 145 | drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) { |
| 145 | char buf[256]; | 146 | char buf[256]; |
| 146 | int i, tx, ty, len, olen; | 147 | int i, tx, ty, th, len, olen; |
| 147 | Extnts tex; | 148 | Extnts tex; |
| 148 | 149 | ||
| 149 | if(!drw || !drw->fg || !drw->bg) | 150 | if(!drw || !drw->theme) |
| 150 | return; | 151 | return; |
| 151 | XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb); | 152 | XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->fg->rgb : drw->theme->bg->rgb); |
| 152 | XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h); | 153 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); |
| 153 | if(!text || !drw->font) | 154 | if(!text || !drw->font) |
| 154 | return; | 155 | return; |
| 155 | olen = strlen(text); | 156 | olen = strlen(text); |
| 156 | drw_getexts(drw, text, olen, &tex); | 157 | drw_font_getexts(drw->font, text, olen, &tex); |
| 157 | ty = y + (h / 2) - tex.yOff; | 158 | th = drw->font->ascent + drw->font->descent; |
| 158 | tx = x + tex.xOff; | 159 | ty = y + (h / 2) - (th / 2) + drw->font->ascent; |
| 160 | tx = x + (h / 2); | ||
| 159 | /* shorten text if necessary */ | 161 | /* shorten text if necessary */ |
| 160 | for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--) | 162 | for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--) |
| 161 | drw_getexts(drw, text, len, &tex); | 163 | drw_font_getexts(drw->font, text, len, &tex); |
| 162 | if(!len) | 164 | if(!len) |
| 163 | return; | 165 | return; |
| 164 | memcpy(buf, text, len); | 166 | memcpy(buf, text, len); |
| 165 | if(len < olen) | 167 | if(len < olen) |
| 166 | for(i = len; i && i > len - 3; buf[--i] = '.'); | 168 | for(i = len; i && i > len - 3; buf[--i] = '.'); |
| 167 | XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb); | 169 | XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb); |
| 168 | if(drw->font->set) | 170 | if(drw->font->set) |
| 169 | XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len); | 171 | XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len); |
| 170 | else | 172 | else |
| 171 | XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len); | 173 | XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len); |
| 172 | } | 174 | } |
| 173 | 175 | ||
| 174 | void | 176 | void |
| 175 | drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) { | 177 | drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) { |
| 176 | if(!drw) | 178 | if(!drw) |
| 177 | return; | 179 | return; |
| 178 | XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y); | 180 | XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); |
| 179 | XSync(drw->dpy, False); | 181 | XSync(drw->dpy, False); |
| 180 | } | 182 | } |
| 181 | 183 | ||
| 182 | 184 | ||
| 183 | void | 185 | void |
| 184 | drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) { | 186 | drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) { |
| 185 | XRectangle r; | 187 | XRectangle r; |
| 186 | 188 | ||
| 187 | if(!drw || !drw->font || !text) | 189 | if(!font || !text) |
| 188 | return; | 190 | return; |
| 189 | if(drw->font->set) { | 191 | if(font->set) { |
| 190 | XmbTextExtents(drw->font->set, text, len, NULL, &r); | 192 | XmbTextExtents(font->set, text, len, NULL, &r); |
| 191 | tex->xOff = r.x; | ||
| 192 | tex->yOff = r.y; | ||
| 193 | tex->w = r.width; | 193 | tex->w = r.width; |
| 194 | tex->h = r.height; | 194 | tex->h = r.height; |
| 195 | } | 195 | } |
| 196 | else { | 196 | else { |
| 197 | tex->h = drw->font->ascent + drw->font->descent; | 197 | tex->h = font->ascent + font->descent; |
| 198 | tex->w = XTextWidth(drw->font->xfont, text, len); | 198 | tex->w = XTextWidth(font->xfont, text, len); |
| 199 | tex->xOff = tex->h / 2; | ||
| 200 | tex->yOff = (tex->h / 2) + drw->font->ascent; | ||
| 201 | } | 199 | } |
| 202 | } | 200 | } |
| 201 | |||
| 202 | unsigned int | ||
| 203 | drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) { | ||
| 204 | Extnts tex; | ||
| 205 | |||
| 206 | if(!font) | ||
| 207 | return -1; | ||
| 208 | drw_font_getexts(font, text, len, &tex); | ||
| 209 | return tex.w; | ||
| 210 | } | ||
| 211 | |||
| 212 | Cur * | ||
| 213 | drw_cur_create(Drw *drw, int shape) { | ||
| 214 | Cur *cur = (Cur *)calloc(1, sizeof(Cur)); | ||
| 215 | |||
| 216 | if(!drw || !cur) | ||
| 217 | return NULL; | ||
| 218 | cur->cursor = XCreateFontCursor(drw->dpy, shape); | ||
| 219 | return cur; | ||
| 220 | } | ||
| 221 | |||
| 222 | void | ||
| 223 | drw_cur_free(Drw *drw, Cur *cursor) { | ||
| 224 | if(!drw || !cursor) | ||
| 225 | return; | ||
| 226 | XFreeCursor(drw->dpy, cursor->cursor); | ||
| 227 | free(cursor); | ||
| 228 | } | ||
