From 924078c898ccfc6d6569b0f27449a7e51b4057b6 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 2 Jul 2008 10:14:42 +0200 Subject: [PATCH] Streamline xcolor_t. Do not have a ->name char * field, but a char[32] instead. This isn't a big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and if it becomes a problem one day, one could just strip the name and generate an hexadecimal representation on the fly instead. But allocating the name is asking for a lot of trouble. Since we do not allocate anything anymore, just don't allocate anything anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of sleeping bugs (p_dup were used e.g., which is wrong). Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add consts at some places. xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t as a first argument (OO-style, this is self), and doesn't touch the structure at all if it returns false, which allow us to skip a lot of intermediates values. Signed-off-by: Pierre Habouzit Signed-off-by: Julien Danjou --- awesome.c | 4 +- client.c | 8 +-- common/draw.c | 140 ++++++++++++++++++++---------------------- common/draw.h | 47 ++++---------- common/swindow.h | 2 +- lua.c | 20 ++---- mouse.c | 6 +- statusbar.c | 37 +++++------ titlebar.c | 48 +++++++-------- widget.c | 2 +- widgets/graph.c | 61 +++++++----------- widgets/progressbar.c | 85 ++++++++++++------------- widgets/taglist.c | 2 +- widgets/tasklist.c | 7 +-- 14 files changed, 202 insertions(+), 267 deletions(-) diff --git a/awesome.c b/awesome.c index 2f3c870c7..d128b20c0 100644 --- a/awesome.c +++ b/awesome.c @@ -401,8 +401,8 @@ main(int argc, char **argv) /* init default font and colors */ globalconf.font = draw_font_new(globalconf.connection, globalconf.default_screen, "sans 8"); - xcolor_new(globalconf.connection, globalconf.default_screen, "black", &globalconf.colors.fg); - xcolor_new(globalconf.connection, globalconf.default_screen, "white", &globalconf.colors.bg); + xcolor_init(&globalconf.colors.fg, globalconf.connection, globalconf.default_screen, "black"); + xcolor_init(&globalconf.colors.bg, globalconf.connection, globalconf.default_screen, "white"); /* init cursors */ globalconf.cursor[CurNormal] = xutil_cursor_new(globalconf.connection, CURSOR_LEFT_PTR); diff --git a/client.c b/client.c index 3c929b77e..1c767bba2 100644 --- a/client.c +++ b/client.c @@ -1127,7 +1127,6 @@ luaA_client_newindex(lua_State *L) bool b; double d; int i; - xcolor_t color; titlebar_t **t = NULL; switch(a_tokenize(buf, len)) @@ -1180,11 +1179,10 @@ luaA_client_newindex(lua_State *L) break; case A_TK_BORDER_COLOR: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, (*c)->phys_screen, buf, &color)) + && xcolor_init(&(*c)->border_color, globalconf.connection, (*c)->phys_screen, buf)) { - xcolor_wipe(&(*c)->border_color); - (*c)->border_color = color; - xcb_change_window_attributes(globalconf.connection, (*c)->win, XCB_CW_BORDER_PIXEL, &color.pixel); + xcb_change_window_attributes(globalconf.connection, (*c)->win, + XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel); } break; case A_TK_COORDS: diff --git a/common/draw.c b/common/draw.c index 874cfbb14..712d211fa 100644 --- a/common/draw.c +++ b/common/draw.c @@ -133,7 +133,7 @@ draw_screen_default_visual(xcb_screen_t *s) draw_context_t * draw_context_new(xcb_connection_t *conn, int phys_screen, int width, int height, xcb_pixmap_t px, - xcolor_t fg, xcolor_t bg) + const xcolor_t *fg, const xcolor_t *bg) { draw_context_t *d = p_new(draw_context_t, 1); xcb_screen_t *s = xutil_screen_get(conn, phys_screen); @@ -148,8 +148,8 @@ draw_context_new(xcb_connection_t *conn, int phys_screen, d->surface = cairo_xcb_surface_create(conn, px, d->visual, width, height); d->cr = cairo_create(d->surface); d->layout = pango_cairo_create_layout(d->cr); - d->fg = fg; - d->bg = bg; + d->fg = *fg; + d->bg = *bg; return d; }; @@ -220,8 +220,7 @@ draw_markup_on_element(markup_parser_data_t *p, const char *elem, switch(a_tokenize(*names, -1)) { case A_TK_COLOR: - data->has_bg_color = xcolor_new(data->connection, data->phys_screen, - *values, &data->bg_color); + data->has_bg_color = xcolor_init(&data->bg_color, data->connection, data->phys_screen, *values); break; case A_TK_IMAGE: if(data->bg_image) @@ -244,8 +243,8 @@ draw_markup_on_element(markup_parser_data_t *p, const char *elem, data->align = draw_align_fromstr(*values, -1); break; case A_TK_SHADOW: - xcolor_new(data->connection, data->phys_screen, *values, - &data->shadow.color); + xcolor_init(&data->shadow.color, data->connection, + data->phys_screen, *values); break; case A_TK_SHADOW_OFFSET: data->shadow.offset = atoi(*values); @@ -340,7 +339,7 @@ draw_text(draw_context_t *ctx, font_t *font, olen = len; if(pdata->has_bg_color) - draw_rectangle(ctx, area, 1.0, true, pdata->bg_color); + draw_rectangle(ctx, area, 1.0, true, &pdata->bg_color); if(pdata->bg_image) { @@ -424,13 +423,15 @@ draw_text(draw_context_t *ctx, font_t *font, */ static cairo_pattern_t * draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect, - xcolor_t *pcolor, xcolor_t *pcolor_center, - xcolor_t *pcolor_end) + const xcolor_t *pcolor, const xcolor_t *pcolor_center, + const xcolor_t *pcolor_end) { cairo_pattern_t *pat = NULL; + bool has_center = pcolor_center->name[0] != '\0'; + bool has_end = pcolor_end->name[0] != '\0'; /* no need for a real pattern: */ - if(!pcolor_end && !pcolor_center) + if(!has_end && !has_center) cairo_set_source_rgba(ctx->cr, pcolor->red / 65535.0, pcolor->green / 65535.0, @@ -447,14 +448,14 @@ draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect, pcolor->blue / 65535.0, pcolor->alpha / 65535.0); - if(pcolor_center) + if(has_center) cairo_pattern_add_color_stop_rgba(pat, 0.5, pcolor_center->red / 65535.0, pcolor_center->green / 65535.0, pcolor_center->blue / 65535.0, pcolor_center->alpha / 65535.0); - if(pcolor_end) + if(has_end) cairo_pattern_add_color_stop_rgba(pat, 1.0, pcolor_end->red / 65535.0, pcolor_end->green / 65535.0, @@ -479,17 +480,18 @@ draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect, * \param color color to use */ void -draw_rectangle(draw_context_t *ctx, area_t geometry, float line_width, bool filled, xcolor_t color) +draw_rectangle(draw_context_t *ctx, area_t geometry, + float line_width, bool filled, const xcolor_t *color) { cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE); cairo_set_line_width(ctx->cr, line_width); cairo_set_miter_limit(ctx->cr, 10.0); cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER); cairo_set_source_rgba(ctx->cr, - color.red / 65535.0, - color.green / 65535.0, - color.blue / 65535.0, - color.alpha / 65535.0); + color->red / 65535.0, + color->green / 65535.0, + color->blue / 65535.0, + color->alpha / 65535.0); if(filled) { cairo_rectangle(ctx->cr, geometry.x, geometry.y, @@ -516,8 +518,8 @@ draw_rectangle(draw_context_t *ctx, area_t geometry, float line_width, bool fill */ void draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled, - area_t pattern_rect, xcolor_t *pcolor, - xcolor_t *pcolor_center, xcolor_t *pcolor_end) + area_t pattern_rect, const xcolor_t *pcolor, + const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) { cairo_pattern_t *pat; @@ -571,8 +573,8 @@ draw_graph_setup(draw_context_t *ctx) */ void draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index, - position_t grow, area_t patt_rect, - xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end) + position_t grow, area_t patt_rect, const xcolor_t *pcolor, + const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) { int i = -1; float x = rect.x + 0.5; /* middle of a pixel */ @@ -624,8 +626,8 @@ draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index, */ void draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index, - position_t grow, area_t patt_rect, - xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end) + position_t grow, area_t patt_rect, const xcolor_t *pcolor, + const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) { int i, w; float x, y; @@ -697,14 +699,14 @@ draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index, * \param color Color to use. */ void -draw_circle(draw_context_t *ctx, int x, int y, int r, bool filled, xcolor_t color) +draw_circle(draw_context_t *ctx, int x, int y, int r, bool filled, const xcolor_t *color) { cairo_set_line_width(ctx->cr, 1.0); cairo_set_source_rgba(ctx->cr, - color.red / 65535.0, - color.green / 65535.0, - color.blue / 65535.0, - color.alpha / 65535.0); + color->red / 65535.0, + color->green / 65535.0, + color->blue / 65535.0, + color->alpha / 65535.0); cairo_new_sub_path(ctx->cr); /* don't draw from the old reference point to.. */ @@ -1087,22 +1089,20 @@ draw_align_tostr(alignment_t a) #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255) /** Initialize an X color. + * \param color xcolor_t struct to store color into. * \param conn Connection ref. * \param phys_screen Physical screen number. * \param colstr Color specification. - * \param color xcolor_t struct to store color to. * \return True if color allocation was successfull. */ bool -xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t *color) +xcolor_init(xcolor_t *color, xcb_connection_t *conn, int phys_screen, + const char *colstr) { xcb_screen_t *s = xutil_screen_get(conn, phys_screen); - xcb_alloc_color_reply_t *hexa_color = NULL; - xcb_alloc_named_color_reply_t *named_color = NULL; unsigned long colnum; - uint16_t red, green, blue; + uint16_t red, green, blue, alpha = 0xffff; ssize_t len; - char *buf; if(!(len = a_strlen(colstr))) return false; @@ -1110,76 +1110,70 @@ xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t /* The color is given in RGB value */ if(colstr[0] == '#') { - errno = 0; + xcb_alloc_color_cookie_t cookie; + xcb_alloc_color_reply_t *hexa_color; + char *p; + if(len == 7) { - colnum = strtoul(&colstr[1], NULL, 16); - color->alpha = 0xffff; + colnum = strtoul(colstr + 1, &p, 16); + if(p - colstr != 7) + goto invalid; } /* we have alpha */ else if(len == 9) { - buf = a_strndup(colstr + 1, 6); - colnum = strtoul(buf, NULL, 16); - p_delete(&buf); - color->alpha = RGB_COLOR_8_TO_16(strtoul(&colstr[7], NULL, 16)); - if(errno != 0) - { - warn("awesome: error, invalid color '%s'", colstr); - return false; - } + colnum = strtoul(colstr + 1, &p, 16); + if(p - colstr != 9) + goto invalid; + alpha = RGB_COLOR_8_TO_16(colnum); + colnum >>= 8; } else { + invalid: warn("awesome: error, invalid color '%s'", colstr); return false; } - if(errno != 0) - { - warn("awesome: error, invalid color '%s'", colstr); - return false; - } - - red = RGB_COLOR_8_TO_16(colnum >> 16); + red = RGB_COLOR_8_TO_16(colnum >> 16); green = RGB_COLOR_8_TO_16(colnum >> 8); - blue = RGB_COLOR_8_TO_16(colnum); + blue = RGB_COLOR_8_TO_16(colnum); - hexa_color = xcb_alloc_color_reply(conn, - xcb_alloc_color_unchecked(conn, - s->default_colormap, - red, green, blue), - NULL); + cookie = xcb_alloc_color_unchecked(conn, s->default_colormap, + red, green, blue), + hexa_color = xcb_alloc_color_reply(conn, cookie, NULL); if(hexa_color) { color->pixel = hexa_color->pixel; - color->red = hexa_color->red; + color->red = hexa_color->red; color->green = hexa_color->green; - color->blue = hexa_color->blue; - color->name = a_strdup(colstr); + color->blue = hexa_color->blue; + color->alpha = alpha; + a_strcpy(color->name, sizeof(color->name), colstr); p_delete(&hexa_color); return true; } } else { - named_color = xcb_alloc_named_color_reply(conn, - xcb_alloc_named_color_unchecked(conn, - s->default_colormap, - len, - colstr), - NULL); + xcb_alloc_named_color_reply_t *named_color = NULL; + xcb_alloc_named_color_cookie_t cookie; + + cookie = xcb_alloc_named_color_unchecked(conn, s->default_colormap, len, + colstr), + named_color = xcb_alloc_named_color_reply(conn, cookie, NULL); if(named_color) { color->pixel = named_color->pixel; - color->red = named_color->visual_red; + color->red = named_color->visual_red; color->green = named_color->visual_green; - color->blue = named_color->visual_blue; + color->blue = named_color->visual_blue; color->alpha = 0xffff; - color->name = a_strdup(colstr); - + color->alpha = alpha; + a_strcpy(color->name, sizeof(color->name), colstr); p_delete(&named_color); return true; } diff --git a/common/draw.h b/common/draw.h index e236e0091..802f016e7 100644 --- a/common/draw.h +++ b/common/draw.h @@ -34,7 +34,7 @@ typedef struct { /** Color name */ - char *name; + char name[32]; uint32_t pixel; uint16_t red; uint16_t green; @@ -119,7 +119,9 @@ typedef struct size_t height; } draw_image_t; -draw_context_t *draw_context_new(xcb_connection_t *, int, int, int, xcb_drawable_t, xcolor_t, xcolor_t); +draw_context_t * +draw_context_new(xcb_connection_t *, int, int, int, xcb_drawable_t, + const xcolor_t *, const xcolor_t*); /** Delete a draw context. * \param ctx The draw_context_t to delete. @@ -183,13 +185,16 @@ void draw_parser_data_init(draw_parser_data_t *); void draw_parser_data_wipe(draw_parser_data_t *); void draw_text(draw_context_t *, font_t *, area_t, const char *, draw_parser_data_t *); -void draw_rectangle(draw_context_t *, area_t, float, bool, xcolor_t); -void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, area_t, xcolor_t *, xcolor_t *, xcolor_t *); +void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *); +void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, area_t, + const xcolor_t *, const xcolor_t *, const xcolor_t *); void draw_graph_setup(draw_context_t *); -void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, area_t, xcolor_t *, xcolor_t *, xcolor_t *); -void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, area_t, xcolor_t *, xcolor_t *, xcolor_t *); -void draw_circle(draw_context_t *, int, int, int, bool, xcolor_t); +void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, area_t, + const xcolor_t *, const xcolor_t *, const xcolor_t *); +void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, area_t, + const xcolor_t *, const xcolor_t *, const xcolor_t *); +void draw_circle(draw_context_t *, int, int, int, bool, const xcolor_t *); draw_image_t *draw_image_new(const char *); void draw_image_delete(draw_image_t **); void draw_image(draw_context_t *, int, int, int, draw_image_t *); @@ -199,33 +204,7 @@ area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_p alignment_t draw_align_fromstr(const char *, ssize_t); const char *draw_align_tostr(alignment_t); -/** Wipe a color resources. - * \param color The color to wipe out. - */ -static inline void -xcolor_wipe(xcolor_t *color) -{ - if(color) - { - p_delete(&color->name); - p_clear(color, 1); - } -} - -static inline xcolor_t -xcolor_copy(xcolor_t *color) -{ - xcolor_t c; - - assert(color); - - c = *color; - c.name = a_strdup(color->name); - - return c; -} - -bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *); +bool xcolor_init(xcolor_t *c, xcb_connection_t *, int, const char *); void area_array_remove(area_array_t *, area_t); diff --git a/common/swindow.h b/common/swindow.h index a3f4154d2..99a045186 100644 --- a/common/swindow.h +++ b/common/swindow.h @@ -93,7 +93,7 @@ simplewindow_border_width_set(simple_window_t *sw, uint32_t border_width) * \param color The border color. */ static inline void -simplewindow_border_color_set(simple_window_t *sw, xcolor_t *color) +simplewindow_border_color_set(simple_window_t *sw, const xcolor_t *color) { xcb_change_window_attributes(sw->connection, sw->window, XCB_CW_BORDER_PIXEL, &color->pixel); diff --git a/lua.c b/lua.c index 0bc4067ae..f235dd6fe 100644 --- a/lua.c +++ b/lua.c @@ -394,24 +394,16 @@ static int luaA_colors_set(lua_State *L) { const char *buf; - xcolor_t color; luaA_checktable(L, 1); - if((buf = luaA_getopt_string(L, 1, "fg", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) - { - xcolor_wipe(&globalconf.colors.fg); - globalconf.colors.fg = color; - } - - if((buf = luaA_getopt_string(L, 1, "bg", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) - { - xcolor_wipe(&globalconf.colors.bg); - globalconf.colors.bg = color; - } + if((buf = luaA_getopt_string(L, 1, "fg", NULL))) + xcolor_init(&globalconf.colors.fg, globalconf.connection, + globalconf.default_screen, buf); + if((buf = luaA_getopt_string(L, 1, "bg", NULL))) + xcolor_init(&globalconf.colors.bg, globalconf.connection, + globalconf.default_screen, buf); return 0; } diff --git a/mouse.c b/mouse.c index 94d480b7e..ef0a8f6d7 100644 --- a/mouse.c +++ b/mouse.c @@ -244,7 +244,7 @@ mouse_infobox_draw(draw_context_t *ctx, snprintf(size, sizeof(size), "%dx%d+%d+%d", geometry.width, geometry.height, geometry.x, geometry.y); - draw_rectangle(ctx, draw_geometry, 1.0, true, globalconf.colors.bg); + draw_rectangle(ctx, draw_geometry, 1.0, true, &globalconf.colors.bg); draw_text(ctx, globalconf.font, draw_geometry, size, NULL); simplewindow_move(sw, geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2, @@ -283,8 +283,8 @@ mouse_infobox_new(int phys_screen, int border, area_t geometry, *ctx = draw_context_new(globalconf.connection, sw->phys_screen, sw->geometry.width, sw->geometry.height, sw->pixmap, - globalconf.colors.fg, - globalconf.colors.bg); + &globalconf.colors.fg, + &globalconf.colors.bg); xcb_map_window(globalconf.connection, sw->window); mouse_infobox_draw(*ctx, sw, geometry, border); diff --git a/statusbar.c b/statusbar.c index cead090d9..c956b6bd0 100644 --- a/statusbar.c +++ b/statusbar.c @@ -180,8 +180,8 @@ statusbar_position_update(statusbar_t *statusbar, position_t position) statusbar->width, statusbar->height, dw, - statusbar->colors.fg, - statusbar->colors.bg); + &statusbar->colors.fg, + &statusbar->colors.bg); break; default: if(!statusbar->width_user) @@ -194,8 +194,8 @@ statusbar_position_update(statusbar_t *statusbar, position_t position) statusbar->width, statusbar->height, statusbar->sw->pixmap, - statusbar->colors.fg, - statusbar->colors.bg); + &statusbar->colors.fg, + &statusbar->colors.bg); break; } @@ -441,14 +441,17 @@ luaA_statusbar_new(lua_State *L) sb->name = a_strdup(buf); if(!(buf = luaA_getopt_string(L, 2, "fg", NULL)) - || !xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &sb->colors.fg)) - sb->colors.fg = xcolor_copy(&globalconf.colors.fg); + || !xcolor_init(&sb->colors.fg, globalconf.connection, globalconf.default_screen, buf)) + { + sb->colors.fg = globalconf.colors.fg; + } if(!(buf = luaA_getopt_string(L, 2, "bg", NULL)) - || !xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &sb->colors.bg)) - sb->colors.bg = xcolor_copy(&globalconf.colors.bg); + || !xcolor_init(&sb->colors.bg, globalconf.connection, + globalconf.default_screen, buf)) + { + sb->colors.bg = globalconf.colors.bg; + } buf = luaA_getopt_lstring(L, 2, "align", "left", &len); sb->align = draw_align_fromstr(buf, len); @@ -538,7 +541,6 @@ luaA_statusbar_newindex(lua_State *L) size_t len; statusbar_t *s, **statusbar = luaA_checkudata(L, 1, "statusbar"); const char *buf, *attr = luaL_checklstring(L, 2, &len); - xcolor_t color; position_t p; switch(a_tokenize(attr, len)) @@ -550,24 +552,19 @@ luaA_statusbar_newindex(lua_State *L) break; case A_TK_FG: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + && xcolor_init(&(*statusbar)->colors.fg, globalconf.connection, + globalconf.default_screen, buf)) { - xcolor_wipe(&(*statusbar)->colors.fg); - (*statusbar)->colors.fg = color; - if((*statusbar)->ctx) (*statusbar)->ctx->fg = (*statusbar)->colors.fg; - (*statusbar)->need_update = true; } break; case A_TK_BG: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + && xcolor_init(&(*statusbar)->colors.bg, globalconf.connection, + globalconf.default_screen, buf)) { - xcolor_wipe(&(*statusbar)->colors.bg); - (*statusbar)->colors.bg = color; - if((*statusbar)->ctx) (*statusbar)->ctx->bg = (*statusbar)->colors.bg; diff --git a/titlebar.c b/titlebar.c index 4d228abb7..eb038942c 100644 --- a/titlebar.c +++ b/titlebar.c @@ -98,16 +98,16 @@ titlebar_draw(client_t *c) c->titlebar->sw->geometry.height, c->titlebar->sw->geometry.width, dw, - c->titlebar->colors.fg, - c->titlebar->colors.bg); + &c->titlebar->colors.fg, + &c->titlebar->colors.bg); break; default: ctx = draw_context_new(globalconf.connection, c->titlebar->sw->phys_screen, c->titlebar->sw->geometry.width, c->titlebar->sw->geometry.height, c->titlebar->sw->pixmap, - c->titlebar->colors.fg, - c->titlebar->colors.bg); + &c->titlebar->colors.fg, + &c->titlebar->colors.bg); break; } @@ -315,19 +315,25 @@ luaA_titlebar_new(lua_State *L) tb->position = position_fromstr(buf, len); if(!(buf = luaA_getopt_string(L, 2, "fg", NULL)) - || !xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->colors.fg)) - tb->colors.fg = xcolor_copy(&globalconf.colors.fg); + || !xcolor_init(&tb->colors.fg, globalconf.connection, + globalconf.default_screen, buf)) + { + tb->colors.fg = globalconf.colors.fg; + } if(!(buf = luaA_getopt_string(L, 2, "bg", NULL)) - || !xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->colors.bg)) - tb->colors.bg = xcolor_copy(&globalconf.colors.bg); + || !xcolor_init(&tb->colors.bg, globalconf.connection, + globalconf.default_screen, buf)) + { + tb->colors.bg = globalconf.colors.bg; + } if(!(buf = luaA_getopt_string(L, 2, "border_color", NULL)) - || !xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->border.color)) - tb->border.color = xcolor_copy(&globalconf.colors.fg); + || !xcolor_init(&tb->border.color, globalconf.connection, + globalconf.default_screen, buf)) + { + tb->border.color = globalconf.colors.fg; + } tb->border.width = luaA_getopt_number(L, 2, "border_width", 0); @@ -457,7 +463,6 @@ luaA_titlebar_newindex(lua_State *L) titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar"); const char *buf, *attr = luaL_checklstring(L, 2, &len); client_t *c; - xcolor_t color; int i; switch(a_tokenize(attr, len)) @@ -476,11 +481,9 @@ luaA_titlebar_newindex(lua_State *L) break; case A_TK_BORDER_COLOR: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + && xcolor_init(&(*titlebar)->border.color, globalconf.connection, + globalconf.default_screen, buf)) { - xcolor_wipe(&(*titlebar)->border.color); - (*titlebar)->border.color = color; - if((*titlebar)->sw) xcb_change_window_attributes(globalconf.connection, (*titlebar)->sw->window, XCB_CW_BORDER_PIXEL, &(*titlebar)->border.color.pixel); @@ -488,19 +491,16 @@ luaA_titlebar_newindex(lua_State *L) return 0; case A_TK_FG: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + && xcolor_init(&(*titlebar)->colors.fg, globalconf.connection, + globalconf.default_screen, buf)) { - xcolor_wipe(&(*titlebar)->colors.fg); - (*titlebar)->colors.fg = color; titlebar_draw(client_getbytitlebar(*titlebar)); } return 0; case A_TK_BG: if((buf = luaL_checkstring(L, 3)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + && xcolor_init(&(*titlebar)->colors.bg, globalconf.connection, globalconf.default_screen, buf)) { - xcolor_wipe(&(*titlebar)->colors.bg); - (*titlebar)->colors.bg = color; titlebar_draw(client_getbytitlebar(*titlebar)); } return 0; diff --git a/widget.c b/widget.c index 018f59685..3d5464705 100644 --- a/widget.c +++ b/widget.c @@ -149,7 +149,7 @@ widget_render(widget_node_t *wnode, draw_context_t *ctx, xcb_gcontext_t gc, xcb_ } } - draw_rectangle(ctx, rectangle, 1.0, true, ctx->bg); + draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg); for(w = wnode; w; w = w->next) if(w->widget->isvisible && w->widget->align == AlignLeft) diff --git a/widgets/graph.c b/widgets/graph.c index 5c80ce3ec..ba104eabc 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -63,9 +63,9 @@ struct plot_t /** Color of them */ xcolor_t color_start; /** Color at middle of graph */ - xcolor_t *pcolor_center; + xcolor_t pcolor_center; /** Color at end of graph */ - xcolor_t *pcolor_end; + xcolor_t pcolor_end; /** Create a vertical color gradient */ bool vertical_gradient; /** Next and previous graph */ @@ -78,8 +78,6 @@ plot_delete(plot_t **g) p_delete(&(*g)->title); p_delete(&(*g)->lines); p_delete(&(*g)->values); - p_delete(&(*g)->pcolor_center); - p_delete(&(*g)->pcolor_end); p_delete(g); } @@ -124,7 +122,7 @@ graph_plot_add(graph_data_t *d, const char *title) plot->values = p_new(float, d->size); plot->lines = p_new(int, d->size); plot->max_value = 100.0; - plot->color_start = xcolor_copy(&globalconf.colors.fg); + plot->color_start = globalconf.colors.fg; plot->vertical_gradient = true; plot_list_append(&d->plots, plot); @@ -172,7 +170,7 @@ graph_draw(draw_context_t *ctx, rectangle.y = margin_top + 1; rectangle.width = d->size; rectangle.height = d->box_height; - draw_rectangle(ctx, rectangle, 1.0, true, d->bg); + draw_rectangle(ctx, rectangle, 1.0, true, &d->bg); /* for plot drawing */ rectangle.y = margin_top + d->box_height + 1; /* bottom left corner as starting point */ @@ -213,7 +211,7 @@ graph_draw(draw_context_t *ctx, d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */ } draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, pattern_area, - &plot->color_start, plot->pcolor_center, plot->pcolor_end); + &plot->color_start, &plot->pcolor_center, &plot->pcolor_end); break; case Bottom_Style: pattern_area.y = rectangle.y; @@ -234,7 +232,7 @@ graph_draw(draw_context_t *ctx, p_clear(d->draw_from, d->size); draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, pattern_area, - &plot->color_start, plot->pcolor_center, plot->pcolor_end); + &plot->color_start, &plot->pcolor_center, &plot->pcolor_end); break; case Line_Style: pattern_area.y = rectangle.y; @@ -253,7 +251,7 @@ graph_draw(draw_context_t *ctx, } draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, pattern_area, - &plot->color_start, plot->pcolor_center, plot->pcolor_end); + &plot->color_start, &plot->pcolor_center, &plot->pcolor_end); break; } @@ -262,7 +260,7 @@ graph_draw(draw_context_t *ctx, rectangle.y = margin_top; rectangle.width = d->size + 2; rectangle.height = d->box_height + 2; - draw_rectangle(ctx, rectangle, 1.0, false, d->border_color); + draw_rectangle(ctx, rectangle, 1.0, false, &d->border_color); w->area.width = d->width; w->area.height = ctx->height; @@ -286,7 +284,6 @@ luaA_graph_plot_properties_set(lua_State *L) const char *title, *buf; size_t len; plot_t *plot; - xcolor_t color; title = luaL_checkstring(L, 2); luaA_checktable(L, 3); @@ -298,25 +295,22 @@ luaA_graph_plot_properties_set(lua_State *L) if(!plot) plot = graph_plot_add(d, title); - if((buf = luaA_getopt_string(L, 3, "fg", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg", NULL))) { - xcolor_wipe(&plot->color_start); - plot->color_start = color; + xcolor_init(&plot->color_start, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "fg_center", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) { - xcolor_wipe(plot->pcolor_center); - plot->pcolor_center = p_dup(&color, 1); + xcolor_init(&plot->pcolor_center, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "fg_end", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) { - xcolor_wipe(plot->pcolor_end); - plot->pcolor_end = p_dup(&color, 1); + xcolor_init(&plot->pcolor_end, globalconf.connection, + globalconf.default_screen, buf); } plot->vertical_gradient = luaA_getopt_boolean(L, 3, "vertical_gradient", plot->vertical_gradient); @@ -489,7 +483,6 @@ luaA_graph_newindex(lua_State *L, awesome_token_t token) int width; plot_t *plot; position_t pos; - xcolor_t color; switch(token) { @@ -517,21 +510,13 @@ luaA_graph_newindex(lua_State *L, awesome_token_t token) return 0; break; case A_TK_BG: - if(xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &color)) - { - xcolor_wipe(&d->bg); - d->bg = color; - } - else + if (!xcolor_init(&d->bg, globalconf.connection, globalconf.default_screen, + luaL_checkstring(L, 3))) return 0; break; case A_TK_BORDER_COLOR: - if(xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &color)) - { - xcolor_wipe(&d->border_color); - d->border_color = color; - } - else + if (!xcolor_init(&d->border_color, globalconf.connection, + globalconf.default_screen, luaL_checkstring(L, 3))) return 0; break; case A_TK_GROW: @@ -596,8 +581,8 @@ graph_new(alignment_t align) d->draw_from = p_new(int, d->size); d->draw_to = p_new(int, d->size); - d->bg = xcolor_copy(&globalconf.colors.bg); - d->border_color = xcolor_copy(&globalconf.colors.fg); + d->bg = globalconf.colors.bg; + d->border_color = globalconf.colors.fg; return w; } diff --git a/widgets/progressbar.c b/widgets/progressbar.c index f9ed29719..b4cc8e85f 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -45,9 +45,9 @@ struct bar_t /** Foreground color of turned-off ticks */ xcolor_t fg_off; /** Foreground color when bar is half-full */ - xcolor_t *pfg_center; + xcolor_t pfg_center; /** Foreground color when bar is full */ - xcolor_t *pfg_end; + xcolor_t pfg_end; /** Background color */ xcolor_t bg; /** Border color */ @@ -63,8 +63,6 @@ static void bar_delete(bar_t **bar) { p_delete(&(*bar)->title); - p_delete(&(*bar)->pfg_center); - p_delete(&(*bar)->pfg_end); p_delete(bar); } @@ -103,10 +101,10 @@ progressbar_bar_add(progressbar_data_t *d, const char *title) bar_t *bar = p_new(bar_t, 1); bar->title = a_strdup(title); - bar->fg = xcolor_copy(&globalconf.colors.fg); - bar->fg_off = xcolor_copy(&globalconf.colors.bg); - bar->bg = xcolor_copy(&globalconf.colors.bg); - bar->border_color = xcolor_copy(&globalconf.colors.fg); + bar->fg = globalconf.colors.fg; + bar->fg_off = globalconf.colors.bg; + bar->bg = globalconf.colors.bg; + bar->border_color = globalconf.colors.fg; bar->max_value = 100.0; /* append the bar in the list */ @@ -228,8 +226,8 @@ progressbar_draw(draw_context_t *ctx, rectangle.height = pb_height + 2 * (d->border_padding + d->border_width); if(d->border_padding) - draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); - draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg); + draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color); } pattern_rect.x = pb_x; @@ -260,10 +258,10 @@ progressbar_draw(draw_context_t *ctx, /* fg color */ if(bar->reverse) - draw_rectangle(ctx, rectangle, 1.0, true, bar->fg_off); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); else draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, - &bar->fg, bar->pfg_center, bar->pfg_end); + &bar->fg, &bar->pfg_center, &bar->pfg_end); } /* top part */ @@ -277,9 +275,9 @@ progressbar_draw(draw_context_t *ctx, /* bg color */ if(bar->reverse) draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, - &bar->fg, bar->pfg_center, bar->pfg_end); + &bar->fg, &bar->pfg_center, &bar->pfg_end); else - draw_rectangle(ctx, rectangle, 1.0, true, bar->fg_off); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); } /* draw gaps TODO: improve e.g all in one */ if(d->ticks_count && d->ticks_gap) @@ -290,7 +288,7 @@ progressbar_draw(draw_context_t *ctx, for(rectangle.y = pb_y + (unit - d->ticks_gap); pb_y + pb_height - d->ticks_gap >= rectangle.y; rectangle.y += unit) - draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg); } pb_offset += pb_width + d->gap + 2 * (d->border_width + d->border_padding); } @@ -328,8 +326,8 @@ progressbar_draw(draw_context_t *ctx, rectangle.height = pb_height + 2 * (d->border_padding + d->border_width); if(d->border_padding) - draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); - draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg); + draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color); } pattern_rect.y = pb_y; @@ -358,10 +356,10 @@ progressbar_draw(draw_context_t *ctx, /* fg color */ if(bar->reverse) - draw_rectangle(ctx, rectangle, 1.0, true, bar->fg_off); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); else draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, - &bar->fg, bar->pfg_center, bar->pfg_end); + &bar->fg, &bar->pfg_center, &bar->pfg_end); } /* right part */ @@ -375,9 +373,9 @@ progressbar_draw(draw_context_t *ctx, /* bg color */ if(bar->reverse) draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, - &bar->fg, bar->pfg_center, bar->pfg_end); + &bar->fg, &bar->pfg_center, &bar->pfg_end); else - draw_rectangle(ctx, rectangle, 1.0, true, bar->fg_off); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); } /* draw gaps TODO: improve e.g all in one */ if(d->ticks_count && d->ticks_gap) @@ -388,7 +386,7 @@ progressbar_draw(draw_context_t *ctx, for(rectangle.x = pb_x + (unit - d->ticks_gap); pb_x + pb_width - d->ticks_gap >= rectangle.x; rectangle.x += unit) - draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); + draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg); } pb_offset += pb_height + d->gap + 2 * (d->border_width + d->border_padding); @@ -414,7 +412,6 @@ luaA_progressbar_bar_properties_set(lua_State *L) const char *buf, *title = luaL_checkstring(L, 2); bar_t *bar; progressbar_data_t *d = (*widget)->data; - xcolor_t color; luaA_checktable(L, 3); @@ -427,46 +424,40 @@ luaA_progressbar_bar_properties_set(lua_State *L) if(!bar) bar = progressbar_bar_add(d, title); - if((buf = luaA_getopt_string(L, 3, "fg", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg", NULL))) { - xcolor_wipe(&bar->fg); - bar->fg = color; + xcolor_init(&bar->fg, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "fg_off", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg_off", NULL))) { - xcolor_wipe(&bar->fg_off); - bar->fg_off = color; + xcolor_init(&bar->fg_off, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "bg", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "bg", NULL))) { - xcolor_wipe(&bar->bg); - bar->bg = color; + xcolor_init(&bar->bg, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "border_color", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "border_color", NULL))) { - xcolor_wipe(&bar->border_color); - bar->border_color = color; + xcolor_init(&bar->border_color, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "fg_center", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) { - xcolor_wipe(bar->pfg_center); - bar->pfg_center = p_dup(&color, 1);; + xcolor_init(&bar->pfg_center, globalconf.connection, + globalconf.default_screen, buf); } - if((buf = luaA_getopt_string(L, 3, "fg_end", NULL)) - && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) { - xcolor_wipe(bar->pfg_end); - bar->pfg_end = p_dup(&color, 1);; + xcolor_init(&bar->pfg_end, globalconf.connection, + globalconf.default_screen, buf); } bar->min_value = luaA_getopt_number(L, 3, "min_value", bar->min_value); diff --git a/widgets/taglist.c b/widgets/taglist.c index 1bdbc4074..39f5f0f24 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -241,7 +241,7 @@ taglist_draw(draw_context_t *ctx, int screen, widget_node_t *w, rectangle.x = r->x; rectangle.y = r->y; draw_rectangle(ctx, rectangle, 1.0, - sel && is_client_tagged(sel, tag), ctx->fg); + sel && is_client_tagged(sel, tag), &ctx->fg); } } diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 43a2f2f1f..c4b4b986e 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -92,9 +92,8 @@ tasklist_markup_on_elem(markup_parser_data_t *p, const char *elem, if(!a_strcmp(*names, "color")) { xcolor_t bg_color; - xcolor_new(ctx->connection, ctx->phys_screen, *values, &bg_color); - draw_rectangle(ctx, *data->area, 1.0, true, bg_color); - xcolor_wipe(&bg_color); + xcolor_init(&bg_color, ctx->connection, ctx->phys_screen, *values); + draw_rectangle(ctx, *data->area, 1.0, true, &bg_color); break; } } @@ -215,7 +214,7 @@ tasklist_draw(draw_context_t *ctx, int screen, draw_circle(ctx, w->area.x + icon_width + box_width * i, w->area.y, (globalconf.font->height + 2) / 4, - c->ismax, ctx->fg); + c->ismax, &ctx->fg); i++; }