[PATCH 1/5] drw: simplify color schemes

From: Markus Teich <markus.teich_AT_stusta.mhn.de>
Date: Fri, 10 Apr 2015 23:12:50 +0200

- Use simple XftColor typedef instead of nesting struct => No need for
  drw_clr_free function.
- Replace ClrScheme struct with simple array of Clr items.
- Add Enum with ColFg and ColBg for indexing Clr scheme type array variables.
- Remove border color, since drw.c does never use it. dwm can still use it by
  creating a 3-element Clr array with drw_scm_create().
---
 drw.c | 47 ++++++++++++++++++++++++++++-------------------
 drw.h | 22 +++++++---------------
 2 files changed, 35 insertions(+), 34 deletions(-)
diff --git a/drw.c b/drw.c
index ac27cdc..82361a0 100644
--- a/drw.c
+++ b/drw.c
_AT_@ -179,31 +179,40 @@ drw_font_free(Fnt *font)
 	free(font);
 }
 
-Clr *
-drw_clr_create(Drw *drw, const char *clrname)
+void
+drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
 {
-	Clr *clr;
+	if (!drw || !dest || !clrname)
+		return;
 
-	clr = ecalloc(1, sizeof(Clr));
 	if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
 	                       DefaultColormap(drw->dpy, drw->screen),
-	                       clrname, &clr->rgb))
+	                       clrname, dest))
 		die("error, cannot allocate color '%s'\n", clrname);
-	clr->pix = clr->rgb.pixel;
-
-	return clr;
 }
 
-void
-drw_clr_free(Clr *clr)
+/* Wrapper to create color schemes. The caller has to call free(3) on the
+ * returned color scheme when done using it. */
+Clr *
+drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
 {
-	free(clr);
+	size_t i;
+	Clr *ret;
+
+	/* need at least two colors for a scheme */
+	if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
+		return NULL;
+
+	for (i = 0; i < clrcount; i++)
+		drw_clr_create(drw, &ret[i], clrnames[i]);
+	return ret;
 }
 
 void
-drw_setscheme(Drw *drw, ClrScheme *scheme)
+drw_setscheme(Drw *drw, Clr *scm)
 {
-	drw->scheme = scheme;
+	if (drw)
+		drw->scheme = scm;
 }
 
 void
_AT_@ -211,7 +220,7 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
 {
 	if (!drw->scheme)
 		return;
-	XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
+	XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
 	if (filled)
 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
 	else if (empty)
_AT_@ -227,7 +236,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 	XftDraw *d = NULL;
 	Fnt *curfont, *nextfont;
 	size_t i, len;
-	int utf8strlen, utf8charlen, render;
+	int utf8strlen, utf8charlen, render = x || y || w || h;
 	long utf8codepoint = 0;
 	const char *utf8str;
 	FcCharSet *fccharset;
_AT_@ -236,14 +245,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 	XftResult result;
 	int charexists = 0;
 
-	if (!drw->scheme || !drw->fontcount)
+	if ((render && !drw->scheme) || !drw->fontcount)
 		return 0;
 
-	if (!(render = x || y || w || h)) {
+	if (!render) {
 		w = ~w;
 	} else {
 		XSetForeground(drw->dpy, drw->gc, invert ?
-		               drw->scheme->fg->pix : drw->scheme->bg->pix);
+		               drw->scheme[ColFg].pixel : drw->scheme[ColBg].pixel);
 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
 		d = XftDrawCreate(drw->dpy, drw->drawable,
 		                  DefaultVisual(drw->dpy, drw->screen),
_AT_@ -292,7 +301,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
 					th = curfont->ascent + curfont->descent;
 					ty = y + (h / 2) - (th / 2) + curfont->ascent;
 					tx = x + (h / 2);
-					XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
+					XftDrawStringUtf8(d, invert ? &drw->scheme[ColBg] : &drw->scheme[ColFg], curfont->xfont, tx, ty, (XftChar8 *)buf, len);
 				}
 				x += ew;
 				w -= ew;
diff --git a/drw.h b/drw.h
index 28f7c61..f8012af 100644
--- a/drw.h
+++ b/drw.h
_AT_@ -2,11 +2,6 @@
 #define DRW_FONT_CACHE_SIZE 32
 
 typedef struct {
-	unsigned long pix;
-	XftColor rgb;
-} Clr;
-
-typedef struct {
 	Cursor cursor;
 } Cur;
 
_AT_@ -19,11 +14,8 @@ typedef struct {
 	FcPattern *pattern;
 } Fnt;
 
-typedef struct {
-	Clr *fg;
-	Clr *bg;
-	Clr *border;
-} ClrScheme;
+enum { ColFg, ColBg }; /* Clr scheme index */
+typedef XftColor Clr;
 
 typedef struct {
 	unsigned int w, h;
_AT_@ -32,7 +24,7 @@ typedef struct {
 	Window root;
 	Drawable drawable;
 	GC gc;
-	ClrScheme *scheme;
+	Clr *scheme;
 	size_t fontcount;
 	Fnt *fonts[DRW_FONT_CACHE_SIZE];
 } Drw;
_AT_@ -48,9 +40,9 @@ void drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount);
 void drw_font_free(Fnt *font);
 void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
 
-/* Colour abstraction */
-Clr *drw_clr_create(Drw *drw, const char *clrname);
-void drw_clr_free(Clr *clr);
+/* Colorscheme abstraction */
+void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
+Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
 
 /* Cursor abstraction */
 Cur *drw_cur_create(Drw *drw, int shape);
_AT_@ -58,7 +50,7 @@ void drw_cur_free(Drw *drw, Cur *cursor);
 
 /* Drawing context manipulation */
 void drw_setfont(Drw *drw, Fnt *font);
-void drw_setscheme(Drw *drw, ClrScheme *scheme);
+void drw_setscheme(Drw *drw, Clr *scm);
 
 /* Drawing functions */
 void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert);
-- 
2.7.3
--mYCpIKhGyMATD0i+
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment; filename="libsl5.patch"
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue May 24 2016 - 21:00:13 CEST