From 2898eeda8806776eaef6c8a998b2433868115287 Mon Sep 17 00:00:00 2001 From: QC Date: Sat, 10 Oct 2015 14:15:27 +0200 Subject: [PATCH] Cache colors. --- include/x11-helper.h | 1 + source/rofi.c | 2 + source/x11-helper.c | 97 +++++++++++++++++++++++++++++--------------- 3 files changed, 68 insertions(+), 32 deletions(-) diff --git a/include/x11-helper.h b/include/x11-helper.h index 53afe9c1..588a0196 100644 --- a/include/x11-helper.h +++ b/include/x11-helper.h @@ -151,6 +151,7 @@ unsigned int color_get ( Display *display, const char *const name, const char * void color_background ( Display *display, cairo_t *d ); void color_border ( Display *display, cairo_t *d ); void color_separator ( Display *display, cairo_t *d ); +void color_cache_reset ( void ); cairo_format_t get_format ( void ); diff --git a/source/rofi.c b/source/rofi.c index 359b736d..f37a49ee 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -1749,6 +1749,8 @@ static void print_global_keybindings () static void reload_configuration () { if ( find_arg ( "-no-config" ) < 0 ) { + // Reset the color cache + color_cache_reset (); // We need to open a new connection to X11, otherwise we get old // configuration Display *temp_display = XOpenDisplay ( display_str ); diff --git a/source/x11-helper.c b/source/x11-helper.c index 1a8c8dfe..346bb7ad 100644 --- a/source/x11-helper.c +++ b/source/x11-helper.c @@ -546,53 +546,86 @@ void x11_helper_set_cairo_rgba ( cairo_t *d, unsigned int pixel ) ( ( pixel & 0xFF000000 ) >> 24 ) / 256.0 ); } +/** + * Color cache. + * + * This stores the current color until + */ +enum +{ + BACKGROUND, + BORDER, + SEPARATOR +}; +struct +{ + unsigned int color; + unsigned int set; +} color_cache[3] = { + { 0, FALSE }, + { 0, FALSE }, + { 0, FALSE } +}; +void color_cache_reset ( void ) +{ + color_cache[BACKGROUND].set = FALSE; + color_cache[BORDER].set = FALSE; + color_cache[SEPARATOR].set = FALSE; +} void color_background ( Display *display, cairo_t *d ) { - unsigned int pixel = 0; - if ( !config.color_enabled ) { - pixel = color_get ( display, config.menu_bg, "black" ); - } - else { - gchar **vals = g_strsplit ( config.color_window, ",", 3 ); - if ( vals != NULL && vals[0] != NULL ) { - pixel = color_get ( display, vals[0], "black" ); + if ( !color_cache[BACKGROUND].set ) { + if ( !config.color_enabled ) { + color_cache[BACKGROUND].color = color_get ( display, config.menu_bg, "black" ); } - g_strfreev ( vals ); + else { + gchar **vals = g_strsplit ( config.color_window, ",", 3 ); + if ( vals != NULL && vals[0] != NULL ) { + color_cache[BACKGROUND].color = color_get ( display, vals[0], "black" ); + } + g_strfreev ( vals ); + } + color_cache[BACKGROUND].set = TRUE; } - x11_helper_set_cairo_rgba ( d, pixel ); + + x11_helper_set_cairo_rgba ( d, color_cache[BACKGROUND].color ); } void color_border ( Display *display, cairo_t *d ) { - unsigned int pixel = 0; - if ( !config.color_enabled ) { - pixel = color_get ( display, config.menu_bc, "white" ); - } - else { - gchar **vals = g_strsplit ( config.color_window, ",", 3 ); - if ( vals != NULL && vals[0] != NULL && vals[1] != NULL ) { - pixel = color_get ( display, vals[1], "white" ); + if ( !color_cache[BORDER].set ) { + if ( !config.color_enabled ) { + color_cache[BORDER].color = color_get ( display, config.menu_bc, "white" ); } - g_strfreev ( vals ); + else { + gchar **vals = g_strsplit ( config.color_window, ",", 3 ); + if ( vals != NULL && vals[0] != NULL && vals[1] != NULL ) { + color_cache[BORDER].color = color_get ( display, vals[1], "white" ); + } + g_strfreev ( vals ); + } + color_cache[BORDER].set = TRUE; } - x11_helper_set_cairo_rgba ( d, pixel ); + x11_helper_set_cairo_rgba ( d, color_cache[BORDER].color ); } void color_separator ( Display *display, cairo_t *d ) { - unsigned int pixel = 0; - if ( !config.color_enabled ) { - pixel = color_get ( display, config.menu_bc, "white" ); - } - else { - gchar **vals = g_strsplit ( config.color_window, ",", 3 ); - if ( vals != NULL && vals[0] != NULL && vals[1] != NULL && vals[2] != NULL ) { - pixel = color_get ( display, vals[2], "white" ); + if ( !color_cache[SEPARATOR].set ) { + if ( !config.color_enabled ) { + color_cache[SEPARATOR].color = color_get ( display, config.menu_bc, "white" ); } - else if ( vals != NULL && vals[0] != NULL && vals[1] != NULL ) { - pixel = color_get ( display, vals[1], "white" ); + else { + gchar **vals = g_strsplit ( config.color_window, ",", 3 ); + if ( vals != NULL && vals[0] != NULL && vals[1] != NULL && vals[2] != NULL ) { + color_cache[SEPARATOR].color = color_get ( display, vals[2], "white" ); + } + else if ( vals != NULL && vals[0] != NULL && vals[1] != NULL ) { + color_cache[SEPARATOR].color = color_get ( display, vals[1], "white" ); + } + g_strfreev ( vals ); } - g_strfreev ( vals ); + color_cache[SEPARATOR].set = TRUE; } - x11_helper_set_cairo_rgba ( d, pixel ); + x11_helper_set_cairo_rgba ( d, color_cache[SEPARATOR].color ); }