diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-02-21 00:59:16 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-02-21 01:32:59 -0600 |
commit | 8e7f8c02a1c6f1a916376f43cbce1271225c2ec7 (patch) | |
tree | 6b86ca3e365991327af78054370976c4903488ba | |
parent | 2077fd91caaa678de78e7b599def61f89306f2b8 (diff) | |
download | dzen-8e7f8c02a1c6f1a916376f43cbce1271225c2ec7.tar.gz dzen-8e7f8c02a1c6f1a916376f43cbce1271225c2ec7.zip |
cache return values of XAllocNamedColor
round trip to the server on every color lookup is pretty bad
-rw-r--r-- | draw.c | 16 | ||||
-rw-r--r-- | dzen.h | 3 | ||||
-rw-r--r-- | util.c | 31 |
3 files changed, 45 insertions, 5 deletions
@@ -103,13 +103,19 @@ drawtext(const char *text, int reverse, int line, int align) { long getcolor(const char *colstr) { - Colormap cmap = DefaultColormap(dzen.dpy, dzen.screen); - XColor color; + long color; + if ((color = colorcache_get(colstr)) != -1) + return color; + else { + Colormap cmap = DefaultColormap(dzen.dpy, dzen.screen); + XColor color; - if(!XAllocNamedColor(dzen.dpy, cmap, colstr, &color, &color)) - return -1; + if(!XAllocNamedColor(dzen.dpy, cmap, colstr, &color, &color)) + return -1; - return color.pixel; + colorcache_set(colstr, color.pixel); + return color.pixel; + } } void @@ -178,3 +178,6 @@ extern void *emalloc(unsigned int size); /* allocates memory, exits on error */ extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ extern char *estrdup(const char *str); /* duplicates str, exits on allocation error */ extern void spawn(const char *arg); /* execute arg */ + +extern long colorcache_get(const char *name); /* gets a cached color */ +extern void colorcache_set(const char *name, long value); /* sets a cached color */ @@ -13,6 +13,11 @@ #include <sys/wait.h> #include <unistd.h> +#define COLORCACHE_MAX 256 + +static char *colorcache_names[COLORCACHE_MAX]; +static long colorcache_values[COLORCACHE_MAX]; + #define ONEMASK ((size_t)(-1) / 0xFF) void * @@ -64,3 +69,29 @@ spawn(const char *arg) { wait(0); } +long colorcache_get(const char *name) { + int i; + + for (i = 0; i < COLORCACHE_MAX; ++i) { + if (!colorcache_names[i]) + break; + if (!strcmp(colorcache_names[i], name)) + return colorcache_values[i]; + } + + return -1; +} + +void colorcache_set(const char *name, long value) { + int i; + + for (i = 0; i < COLORCACHE_MAX; ++i) + if (!colorcache_names[i]) + break; + + if (i >= COLORCACHE_MAX) + return; + + colorcache_names[i] = strdup(name); + colorcache_values[i] = value; +} |