commit 397537761c5a6303bba45c273327786570fc4a2e
parent bd41463268c70fcbec5422aa30c68d126418d59a
Author: Anselm R Garbe <anselm@garbe.us>
Date: Sun, 16 Jun 2013 15:21:30 +0200
added new drw changes of dwm
Diffstat:
M | drw.c | | | 137 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
M | drw.h | | | 46 | +++++++++++++++++++++++++++------------------- |
2 files changed, 103 insertions(+), 80 deletions(-)
diff --git a/drw.c b/drw.c
@@ -8,17 +8,17 @@
#include "util.h"
Drw *
-drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
+drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
Drw *drw = (Drw *)calloc(1, sizeof(Drw));
if(!drw)
return NULL;
drw->dpy = dpy;
drw->screen = screen;
- drw->win = win;
+ drw->root = root;
drw->w = w;
drw->h = h;
- drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
- drw->gc = XCreateGC(dpy, win, 0, NULL);
+ drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
+ drw->gc = XCreateGC(dpy, root, 0, NULL);
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
return drw;
}
@@ -29,29 +29,28 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h) {
return;
drw->w = w;
drw->h = h;
- XFreePixmap(drw->dpy, drw->drwable);
- drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
+ if(drw->drawable != 0)
+ XFreePixmap(drw->dpy, drw->drawable);
+ drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
}
void
drw_free(Drw *drw) {
- XFreePixmap(drw->dpy, drw->drwable);
+ XFreePixmap(drw->dpy, drw->drawable);
XFreeGC(drw->dpy, drw->gc);
free(drw);
}
Fnt *
-drw_font_create(Drw *drw, const char *fontname) {
+drw_font_create(Display *dpy, const char *fontname) {
Fnt *font;
char *def, **missing;
int n;
- if(!drw)
- return NULL;
font = (Fnt *)calloc(1, sizeof(Fnt));
if(!font)
return NULL;
- font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
+ font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
if(missing) {
while(n--)
fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
@@ -69,11 +68,9 @@ drw_font_create(Drw *drw, const char *fontname) {
}
}
else {
- if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname))
- && !(font->xfont = XLoadQueryFont(drw->dpy, "fixed"))) {
- free(font);
- return NULL;
- }
+ if(!(font->xfont = XLoadQueryFont(dpy, fontname))
+ && !(font->xfont = XLoadQueryFont(dpy, "fixed")))
+ die("error, cannot load font: '%s'\n", fontname);
font->ascent = font->xfont->ascent;
font->descent = font->xfont->descent;
}
@@ -82,13 +79,13 @@ drw_font_create(Drw *drw, const char *fontname) {
}
void
-drw_font_free(Drw *drw, Fnt *font) {
- if(!drw || !font)
+drw_font_free(Display *dpy, Fnt *font) {
+ if(!font)
return;
if(font->set)
- XFreeFontSet(drw->dpy, font->set);
+ XFreeFontSet(dpy, font->set);
else
- XFreeFont(drw->dpy, font->xfont);
+ XFreeFont(dpy, font->xfont);
free(font);
}
@@ -111,9 +108,7 @@ drw_clr_create(Drw *drw, const char *clrname) {
}
void
-drw_clr_free(Drw *drw, Clr *clr) {
- if(!drw)
- return;
+drw_clr_free(Clr *clr) {
if(!clr)
return;
free(clr);
@@ -126,88 +121,108 @@ drw_setfont(Drw *drw, Fnt *font) {
}
void
-drw_setfg(Drw *drw, Clr *clr) {
- if(drw)
- drw->fg = clr;
-}
-
-void
-drw_setbg(Drw *drw, Clr *clr) {
- if(drw)
- drw->bg = clr;
+drw_settheme(Drw *drw, Theme *theme) {
+ if(!drw || !theme)
+ return;
+ drw->theme = theme;
}
void
-drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
+drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
int dx;
- if(!drw || !drw->font || !drw->fg || !drw->bg)
+ if(!drw || !drw->font || !drw->theme)
return;
- XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
+ XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
dx = (drw->font->ascent + drw->font->descent + 2) / 4;
if(filled)
- XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
+ XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
else if(empty)
- XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
+ XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx);
}
void
-drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
+drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
char buf[256];
- int i, tx, ty, len, olen;
+ int i, tx, ty, th, len, olen;
Extnts tex;
- if(!drw || !drw->fg || !drw->bg)
+ if(!drw || !drw->theme)
return;
- XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb);
- XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h);
+ XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->fg->rgb : drw->theme->bg->rgb);
+ XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
if(!text || !drw->font)
return;
olen = strlen(text);
- drw_getexts(drw, text, olen, &tex);
- ty = y + (h / 2) - tex.yOff;
- tx = x + tex.xOff;
+ drw_font_getexts(drw->font, text, olen, &tex);
+ th = drw->font->ascent + drw->font->descent;
+ ty = y + (h / 2) - (th / 2) + drw->font->ascent;
+ tx = x + (h / 2);
/* shorten text if necessary */
for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
- drw_getexts(drw, text, len, &tex);
+ drw_font_getexts(drw->font, text, len, &tex);
if(!len)
return;
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
- XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
+ XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
if(drw->font->set)
- XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
+ XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
else
- XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
+ XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
}
void
-drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
+drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
if(!drw)
return;
- XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
+ XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
XSync(drw->dpy, False);
}
void
-drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
+drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
XRectangle r;
- if(!drw || !drw->font || !text)
+ if(!font || !text)
return;
- if(drw->font->set) {
- XmbTextExtents(drw->font->set, text, len, NULL, &r);
- tex->xOff = r.x;
- tex->yOff = r.y;
+ if(font->set) {
+ XmbTextExtents(font->set, text, len, NULL, &r);
tex->w = r.width;
tex->h = r.height;
}
else {
- tex->h = drw->font->ascent + drw->font->descent;
- tex->w = XTextWidth(drw->font->xfont, text, len);
- tex->xOff = tex->h / 2;
- tex->yOff = (tex->h / 2) + drw->font->ascent;
+ tex->h = font->ascent + font->descent;
+ tex->w = XTextWidth(font->xfont, text, len);
}
}
+
+unsigned int
+drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
+ Extnts tex;
+
+ if(!font)
+ return -1;
+ drw_font_getexts(font, text, len, &tex);
+ return tex.w;
+}
+
+Cur *
+drw_cur_create(Drw *drw, int shape) {
+ Cur *cur = (Cur *)calloc(1, sizeof(Cur));
+
+ if(!drw || !cur)
+ return NULL;
+ cur->cursor = XCreateFontCursor(drw->dpy, shape);
+ return cur;
+}
+
+void
+drw_cur_free(Drw *drw, Cur *cursor) {
+ if(!drw || !cursor)
+ return;
+ XFreeCursor(drw->dpy, cursor->cursor);
+ free(cursor);
+}
diff --git a/drw.h b/drw.h
@@ -5,6 +5,10 @@ typedef struct {
} Clr;
typedef struct {
+ Cursor cursor;
+} Cur;
+
+typedef struct {
int ascent;
int descent;
unsigned int h;
@@ -13,22 +17,25 @@ typedef struct {
} Fnt;
typedef struct {
+ Clr *fg;
+ Clr *bg;
+ Clr *border;
+} Theme;
+
+typedef struct {
unsigned int w, h;
Display *dpy;
int screen;
- Window win;
- Drawable drwable;
+ Window root;
+ Drawable drawable;
GC gc;
- Clr *fg;
- Clr *bg;
+ Theme *theme;
Fnt *font;
} Drw;
typedef struct {
unsigned int w;
unsigned int h;
- int xOff;
- int yOff;
} Extnts;
/* Drawable abstraction */
@@ -37,25 +44,26 @@ void drw_resize(Drw *drw, unsigned int w, unsigned int h);
void drw_free(Drw *drw);
/* Fnt abstraction */
-Fnt *drw_font_create(Drw *drw, const char *fontname);
-void drw_font_free(Drw *drw, Fnt *font);
+Fnt *drw_font_create(Display *dpy, const char *fontname);
+void drw_font_free(Display *dpy, Fnt *font);
+void drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *extnts);
+unsigned int drw_font_getexts_width(Fnt *font, const char *text, unsigned int len);
-/* Clrour abstraction */
+/* Colour abstraction */
Clr *drw_clr_create(Drw *drw, const char *clrname);
-void drw_clr_free(Drw *drw, Clr *clr);
+void drw_clr_free(Clr *clr);
+
+/* Cursor abstraction */
+Cur *drw_cur_create(Drw *drw, int shape);
+void drw_cur_free(Drw *drw, Cur *cursor);
/* Drawing context manipulation */
void drw_setfont(Drw *drw, Fnt *font);
-void drw_setfg(Drw *drw, Clr *clr);
-void drw_setbg(Drw *drw, Clr *clr);
+void drw_settheme(Drw *drw, Theme *theme);
/* Drawing functions */
-void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
-void drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
+void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert);
+void drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert);
/* Map functions */
-void drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h);
-
-/* Text functions */
-void drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *extnts);
-
+void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);