diff --git a/client.c b/client.c index c1ef9896c..c1e56298a 100644 --- a/client.c +++ b/client.c @@ -1005,8 +1005,11 @@ luaA_client_border_set(lua_State *L) if(colorstr && xcolor_new(globalconf.connection, (*c)->phys_screen, colorstr, &color)) + { xcb_change_window_attributes(globalconf.connection, (*c)->win, XCB_CW_BORDER_PIXEL, &color.pixel); + xcolor_wipe(&color); + } return 0; } diff --git a/common/draw.c b/common/draw.c index c5faef3de..c0658d852 100644 --- a/common/draw.c +++ b/common/draw.c @@ -1138,7 +1138,7 @@ xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t color->red = hexa_color->red; color->green = hexa_color->green; color->blue = hexa_color->blue; - + color->name = a_strdup(colstr); p_delete(&hexa_color); return true; } @@ -1159,6 +1159,7 @@ xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t color->green = named_color->visual_green; color->blue = named_color->visual_blue; color->alpha = 0xffff; + color->name = a_strdup(colstr); p_delete(&named_color); return true; diff --git a/common/draw.h b/common/draw.h index 874b0f71c..e3fc50c3f 100644 --- a/common/draw.h +++ b/common/draw.h @@ -33,6 +33,8 @@ typedef struct { + /** Color name */ + char *name; uint32_t pixel; uint16_t red; uint16_t green; @@ -195,6 +197,17 @@ void draw_image_from_argb_data(draw_context_t *, int, int, int, int, int, unsign void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int); area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_parser_data_t *); alignment_t draw_align_fromstr(const char *, ssize_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); +} + bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *); void area_array_remove(area_array_t *, area_t); diff --git a/lua.c b/lua.c index a18376e9d..52ba678d0 100644 --- a/lua.c +++ b/lua.c @@ -394,14 +394,25 @@ luaA_font_set(lua_State *L) static int luaA_colors_set(lua_State *L) { - const char *fg, *bg; + const char *buf; + xcolor_t color; + luaA_checktable(L, 1); - if((fg = luaA_getopt_string(L, 1, "fg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - fg, &globalconf.colors.fg); - if((bg = luaA_getopt_string(L, 1, "bg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - bg, &globalconf.colors.bg); + + 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; + } + return 0; } diff --git a/statusbar.c b/statusbar.c index a893e2d0c..c92ee230c 100644 --- a/statusbar.c +++ b/statusbar.c @@ -350,13 +350,15 @@ luaA_statusbar_colors_set(lua_State *L) { statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); const char *buf; + xcolor_t color; luaA_checktable(L, 2); - if ((buf = luaA_getopt_string(L, 2, "fg", NULL))) + if((buf = luaA_getopt_string(L, 2, "fg", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) { - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &(*sb)->colors.fg); + xcolor_wipe(&(*sb)->colors.fg); + (*sb)->colors.fg = color; if((*sb)->ctx) (*sb)->ctx->fg = (*sb)->colors.fg; @@ -364,10 +366,11 @@ luaA_statusbar_colors_set(lua_State *L) (*sb)->need_update = true; } - if ((buf = luaA_getopt_string(L, 2, "bg", NULL))) + if((buf = luaA_getopt_string(L, 2, "bg", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) { - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &(*sb)->colors.bg); + xcolor_wipe(&(*sb)->colors.bg); + (*sb)->colors.bg = color; if((*sb)->ctx) (*sb)->ctx->bg = (*sb)->colors.bg; @@ -541,16 +544,14 @@ luaA_statusbar_new(lua_State *L) sb->name = luaA_name_init(L); - if((buf = luaA_getopt_string(L, 1, "fg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &sb->colors.fg); - else + if(!(buf = luaA_getopt_string(L, 1, "fg", NULL)) + || !xcolor_new(globalconf.connection, globalconf.default_screen, + buf, &sb->colors.fg)) sb->colors.fg = globalconf.colors.fg; - if((buf = luaA_getopt_string(L, 1, "bg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &sb->colors.bg); - else + if(!(buf = luaA_getopt_string(L, 1, "bg", NULL)) + || !xcolor_new(globalconf.connection, globalconf.default_screen, + buf, &sb->colors.bg)) sb->colors.bg = globalconf.colors.bg; buf = luaA_getopt_lstring(L, 1, "align", "left", &len); diff --git a/titlebar.c b/titlebar.c index 182a7b4a5..c9c6cdb5f 100644 --- a/titlebar.c +++ b/titlebar.c @@ -314,21 +314,20 @@ luaA_titlebar_new(lua_State *L) buf = luaA_getopt_lstring(L, 1, "position", "top", &len); tb->position = position_fromstr(buf, len); - if((buf = luaA_getopt_string(L, -1, "fg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->colors.fg); - else + if(!(buf = luaA_getopt_string(L, -1, "fg", NULL)) + || !xcolor_new(globalconf.connection, globalconf.default_screen, + buf, &tb->colors.fg)) tb->colors.fg = globalconf.colors.fg; - if((buf = luaA_getopt_string(L, 1, "bg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->colors.bg); - else + if(!(buf = luaA_getopt_string(L, -1, "bg", NULL)) + || !xcolor_new(globalconf.connection, globalconf.default_screen, + buf, &tb->colors.bg)) tb->colors.bg = globalconf.colors.bg; - if((buf = luaA_getopt_string(L, 1, "border_color", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, - buf, &tb->border.color); + if(!(buf = luaA_getopt_string(L, -1, "border_color", NULL)) + || !xcolor_new(globalconf.connection, globalconf.default_screen, + buf, &tb->border.color)) + tb->border.color = globalconf.colors.fg; tb->border.width = luaA_getopt_number(L, 1, "border_width", 0); diff --git a/widgets/graph.c b/widgets/graph.c index f55f46088..0a8a2d654 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -110,22 +110,6 @@ typedef struct plot_t *plots; } graph_data_t; -static void -plot_pcolor_set(xcolor_t **ppcolor, const char *new_color) -{ - bool flag = false; - if(!*ppcolor) - { - flag = true; /* p_delete && restore to NULL, if xcolor_new unsuccessful */ - *ppcolor = p_new(xcolor_t, 1); - } - if(!(xcolor_new(globalconf.connection, - globalconf.default_screen, - new_color, *ppcolor)) - && flag) - p_delete(ppcolor); -} - /** Add a plot to a graph. * \param d The graph private data. * \param title The plot title. @@ -302,6 +286,7 @@ 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); @@ -313,12 +298,26 @@ 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, &plot->color_start); - if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) - plot_pcolor_set(&plot->pcolor_center, buf); - if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) - plot_pcolor_set(&plot->pcolor_end, buf); + if((buf = luaA_getopt_string(L, 3, "fg", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(&plot->color_start); + plot->color_start = color; + } + + if((buf = luaA_getopt_string(L, 3, "fg_center", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(plot->pcolor_center); + plot->pcolor_center = p_dup(&color, 1); + } + + if((buf = luaA_getopt_string(L, 3, "fg_end", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(plot->pcolor_end); + plot->pcolor_end = p_dup(&color, 1); + } plot->vertical_gradient = luaA_getopt_boolean(L, 3, "vertical_gradient", plot->vertical_gradient); plot->scale = luaA_getopt_boolean(L, 3, "scale", plot->scale); @@ -483,6 +482,7 @@ luaA_graph_newindex(lua_State *L) int width; plot_t *plot; position_t pos; + xcolor_t color; switch(a_tokenize(attr, len)) { @@ -510,10 +510,22 @@ luaA_graph_newindex(lua_State *L) return 0; break; case A_TK_BG: - xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &d->bg); + if(xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &color)) + { + xcolor_wipe(&d->bg); + d->bg = color; + } + else + return 0; break; case A_TK_BORDER_COLOR: - xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &d->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 + return 0; break; case A_TK_GROW: buf = luaL_checklstring(L, 3, &len); diff --git a/widgets/progressbar.c b/widgets/progressbar.c index a57134720..1e707dd23 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -51,7 +51,7 @@ struct bar_t /** Background color */ xcolor_t bg; /** Border color */ - xcolor_t bordercolor; + xcolor_t border_color; /** The next and previous bar in the list */ bar_t *next, *prev; }; @@ -93,22 +93,6 @@ typedef struct bar_t *bars; } progressbar_data_t; -static void -progressbar_pcolor_set(xcolor_t **ppcolor, const char *new_color) -{ - bool flag = false; - if(!*ppcolor) - { - flag = true; /* p_delete && restore to NULL, if xcolor_new unsuccessful */ - *ppcolor = p_new(xcolor_t, 1); - } - if(!(xcolor_new(globalconf.connection, - globalconf.default_screen, - new_color, *ppcolor)) - && flag) - p_delete(ppcolor); -} - /** Add a new bar to the progressbar private data structure. * \param d The private data structure. * \param title The graph title. @@ -122,7 +106,7 @@ progressbar_bar_add(progressbar_data_t *d, const char *title) bar->fg = globalconf.colors.fg; bar->fg_off = globalconf.colors.bg; bar->bg = globalconf.colors.bg; - bar->bordercolor = globalconf.colors.fg; + bar->border_color = globalconf.colors.fg; bar->max_value = 100.0; /* append the bar in the list */ @@ -245,7 +229,7 @@ progressbar_draw(draw_context_t *ctx, if(d->border_padding) draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); - draw_rectangle(ctx, rectangle, d->border_width, false, bar->bordercolor); + draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color); } pattern_rect.x = pb_x; @@ -345,7 +329,7 @@ progressbar_draw(draw_context_t *ctx, if(d->border_padding) draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); - draw_rectangle(ctx, rectangle, d->border_width, false, bar->bordercolor); + draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color); } pattern_rect.y = pb_y; @@ -462,6 +446,7 @@ 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); @@ -474,18 +459,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, &bar->fg); - if((buf = luaA_getopt_string(L, 3, "bg", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->bg); - if((buf = luaA_getopt_string(L, 3, "fg_off", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->fg_off); - if((buf = luaA_getopt_string(L, 3, "border_color", NULL))) - xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->bordercolor); - if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) - progressbar_pcolor_set(&bar->pfg_center, buf); - if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) - progressbar_pcolor_set(&bar->pfg_end, buf); + if((buf = luaA_getopt_string(L, 3, "fg", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(&bar->fg); + bar->fg = color; + } + + if((buf = luaA_getopt_string(L, 3, "bg", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(&bar->bg); + bar->bg = color; + } + + if((buf = luaA_getopt_string(L, 3, "border_color", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(&bar->border_color); + bar->border_color = color; + } + + if((buf = luaA_getopt_string(L, 3, "fg_center", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(bar->pfg_center); + bar->pfg_end = p_dup(&color, 1);; + } + + if((buf = luaA_getopt_string(L, 3, "fg_end", NULL)) + && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color)) + { + xcolor_wipe(bar->pfg_end); + bar->pfg_end = p_dup(&color, 1);; + } bar->min_value = luaA_getopt_number(L, 3, "min_value", bar->min_value); /* hack to prevent max_value beeing less than min_value diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 3899a05e6..81556ac22 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -94,6 +94,7 @@ tasklist_markup_on_elem(markup_parser_data_t *p, const char *elem, 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); break; } }