diff --git a/config-verify.c b/config-verify.c index 73d1b47..64efae4 100644 --- a/config-verify.c +++ b/config-verify.c @@ -14,6 +14,7 @@ #include "particles/map.h" #include "particles/progress-bar.h" #include "particles/ramp.h" +#include "particles/string.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -164,8 +165,6 @@ conf_verify_font(keychain_t *chain, const struct yml_node *node) return conf_verify_dict(chain, node, attrs, sizeof(attrs) / sizeof(attrs[0])); } -static bool verify_decoration(keychain_t *chain, const struct yml_node *node); - static bool verify_decoration_stack(keychain_t *chain, const struct yml_node *node) { @@ -178,15 +177,15 @@ verify_decoration_stack(keychain_t *chain, const struct yml_node *node) it.node != NULL; yml_list_next(&it)) { - if (!verify_decoration(chain, it.node)) + if (!conf_verify_decoration(chain, it.node)) return false; } return true; } -static bool -verify_decoration(keychain_t *chain, const struct yml_node *node) +bool +conf_verify_decoration(keychain_t *chain, const struct yml_node *node) { assert(yml_is_dict(node)); @@ -292,15 +291,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"right-margin", false, &conf_verify_int}, \ {"on-click", false, &conf_verify_string}, - static const struct attr_info string[] = { - {"text", true, &conf_verify_string}, - {"max", false, &conf_verify_int}, - {"font", false, &conf_verify_font}, - {"foreground", false, &conf_verify_color}, - {"deco", false, &verify_decoration}, - COMMON_ATTRS - }; - #undef COMMON_ATTRS static const struct { @@ -312,6 +302,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"map", &particle_map}, {"progress-bar", &particle_progress_bar}, {"ramp", &particle_ramp}, + {"string", &particle_string}, }; static const struct { @@ -319,7 +310,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"string", string, sizeof(string) / sizeof(string[0])}, }; for (size_t i = 0; i < sizeof(particles_v2) / sizeof(particles_v2[0]); i++) { diff --git a/config-verify.h b/config-verify.h index 18c7900..5bdc37c 100644 --- a/config-verify.h +++ b/config-verify.h @@ -43,3 +43,5 @@ bool conf_verify_font(keychain_t *chain, const struct yml_node *node); bool conf_verify_particle(keychain_t *chain, const struct yml_node *node); bool conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node); + +bool conf_verify_decoration(keychain_t *chain, const struct yml_node *node); diff --git a/config.c b/config.c index cb9f5c6..e6441ef 100644 --- a/config.c +++ b/config.c @@ -47,10 +47,14 @@ hex_byte(const char hex[2]) return upper << 4 | lower; } -static struct rgba -color_from_hexstr(const char *hex) +struct rgba +conf_to_color(const struct yml_node *node) { + const char *hex = yml_value_as_string(node); + + assert(hex != NULL); assert(strlen(hex) == 8); + uint8_t red = hex_byte(&hex[0]); uint8_t green = hex_byte(&hex[2]); uint8_t blue = hex_byte(&hex[4]); @@ -71,8 +75,8 @@ color_from_hexstr(const char *hex) return rgba; } -static struct font * -font_from_config(const struct yml_node *node) +struct font * +conf_to_font(const struct yml_node *node) { const struct yml_node *family = yml_get_value(node, "family"); return font_new(family != NULL ? yml_value_as_string(family) : "monospace"); @@ -82,7 +86,7 @@ static struct deco * deco_background_from_config(const struct yml_node *node) { const struct yml_node *color = yml_get_value(node, "color"); - return deco_background(color_from_hexstr(yml_value_as_string(color))); + return deco_background(conf_to_color(color)); } static struct deco * @@ -90,10 +94,7 @@ deco_underline_from_config(const struct yml_node *node) { const struct yml_node *size = yml_get_value(node, "size"); const struct yml_node *color = yml_get_value(node, "color"); - - return deco_underline( - yml_value_as_int(size), - color_from_hexstr(yml_value_as_string(color))); + return deco_underline(yml_value_as_int(size), conf_to_color(color)); } static struct deco *deco_from_config(const struct yml_node *node); @@ -137,30 +138,6 @@ deco_from_config(const struct yml_node *node) return NULL; } - -static struct particle * -particle_string_from_config(const struct yml_node *node, - const struct font *parent_font, - int left_margin, int right_margin, - const char *on_click_template) -{ - const struct yml_node *text = yml_get_value(node, "text"); - const struct yml_node *max = yml_get_value(node, "max"); - const struct yml_node *font = yml_get_value(node, "font"); - const struct yml_node *foreground = yml_get_value(node, "foreground"); - - struct rgba fg_color = foreground != NULL - ? color_from_hexstr(yml_value_as_string(foreground)) : - (struct rgba){1.0, 1.0, 1.0, 1.0}; - - return particle_string_new( - yml_value_as_string(text), - max != NULL ? yml_value_as_int(max) : 0, - font != NULL ? font_from_config(font) : font_clone(parent_font), - fg_color, left_margin, right_margin, on_click_template); -} - - static struct particle * particle_simple_list_from_config(const struct yml_node *node, const struct font *parent_font) @@ -217,9 +194,8 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "ramp") == 0) ret = particle_ramp.from_conf( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "string") == 0) - ret = particle_string_from_config( + ret = particle_string.from_conf( pair.value, parent_font, left, right, on_click_template); else assert(false); @@ -252,7 +228,7 @@ conf_to_bar(const struct yml_node *bar) ? BAR_TOP : BAR_BOTTOM; const struct yml_node *background = yml_get_value(bar, "background"); - conf.background = color_from_hexstr(yml_value_as_string(background)); + conf.background = conf_to_color(background); /* * Optional attributes @@ -291,7 +267,7 @@ conf_to_bar(const struct yml_node *bar) conf.border.width = yml_value_as_int(width); if (color != NULL) - conf.border.color = color_from_hexstr(yml_value_as_string(color)); + conf.border.color = conf_to_color(color); } /* Create a default font */ @@ -300,7 +276,7 @@ conf_to_bar(const struct yml_node *bar) const struct yml_node *font_node = yml_get_value(bar, "font"); if (font_node != NULL) { font_destroy(font); - font = font_from_config(font_node); + font = conf_to_font(font_node); } const struct yml_node *left = yml_get_value(bar, "left"); diff --git a/config.h b/config.h index c659e0c..0bdbc09 100644 --- a/config.h +++ b/config.h @@ -12,5 +12,8 @@ struct bar *conf_to_bar(const struct yml_node *bar); * Utility functions, for e.g. modules */ +struct rgba conf_to_color(const struct yml_node *node); +struct font *conf_to_font(const struct yml_node *node); + struct particle * conf_to_particle( const struct yml_node *node, const struct font *parent_font); diff --git a/particles/string.c b/particles/string.c index 303e50a..5731692 100644 --- a/particles/string.c +++ b/particles/string.c @@ -7,6 +7,7 @@ #define LOG_MODULE "string" #define LOG_ENABLE_DBG 1 #include "../log.h" +#include "../config.h" struct private { char *text; @@ -130,10 +131,10 @@ particle_destroy(struct particle *particle) particle_default_destroy(particle); } -struct particle * -particle_string_new(const char *text, size_t max_len, struct font *font, - struct rgba foreground, int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +string_new(const char *text, size_t max_len, struct font *font, + struct rgba foreground, int left_margin, int right_margin, + const char *on_click_template) { struct private *p = malloc(sizeof(*p)); p->text = strdup(text); @@ -150,3 +151,35 @@ particle_string_new(const char *text, size_t max_len, struct font *font, return particle; } + +static struct particle * +from_conf(const struct yml_node *node, const struct font *parent_font, + int left_margin, int right_margin, const char *on_click_template) +{ + const struct yml_node *text = yml_get_value(node, "text"); + const struct yml_node *max = yml_get_value(node, "max"); + const struct yml_node *font = yml_get_value(node, "font"); + const struct yml_node *foreground = yml_get_value(node, "foreground"); + + struct rgba fg_color = foreground != NULL + ? conf_to_color(foreground) : (struct rgba){1.0, 1.0, 1.0, 1.0}; + + return string_new( + yml_value_as_string(text), + max != NULL ? yml_value_as_int(max) : 0, + font != NULL ? conf_to_font(font) : font_clone(parent_font), + fg_color, left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_string = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 5, + .attrs = { + {"text", true, &conf_verify_string}, + {"max", false, &conf_verify_int}, + {"font", false, &conf_verify_font}, + {"foreground", false, &conf_verify_color}, + {"deco", false, &conf_verify_decoration}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/string.h b/particles/string.h index 2f51ccb..ca09106 100644 --- a/particles/string.h +++ b/particles/string.h @@ -1,6 +1,4 @@ #pragma once #include "../particle.h" -struct particle *particle_string_new( - const char *text, size_t max_len, struct font *font, struct rgba foreground, - int left_margin, int right_margin, const char *on_click_template); +extern const struct particle_info particle_string;