From a425378576a685c822e7290afafd85d9818e8f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 14:24:44 +0100 Subject: [PATCH] config: allow font/foreground attributes on modules too Previously we allowed it on the bar, and on all particles. Now we also allow it on all modules. This allows us to specify a "default" font/foreground on a per-module basis, having it applied to all the modules particles. --- config.c | 34 +++++++++++++++++++++++++++++++--- module.h | 5 +++++ modules/alsa.c | 2 +- modules/backlight.c | 2 +- modules/battery.c | 2 +- modules/clock.c | 2 +- modules/i3.c | 2 +- modules/label.c | 2 +- modules/mpd.c | 2 +- modules/network.c | 2 +- modules/removables.c | 2 +- modules/xkb.c | 2 +- modules/xwindow.c | 2 +- 13 files changed, 47 insertions(+), 14 deletions(-) diff --git a/config.c b/config.c index 3981e83..a2d759e 100644 --- a/config.c +++ b/config.c @@ -191,18 +191,29 @@ conf_to_particle(const struct yml_node *node, struct conf_inherit inherited) const char *on_click_template = on_click != NULL ? yml_value_as_string(on_click) : NULL; + struct deco *deco = deco_node != NULL ? deco_from_config(deco_node) : NULL; + + /* + * Font and foreground are inheritable attributes. Each particle + * may specify its own font/foreground values, which will then be + * used by itself, and all its sub-particles. If *not* specified, + * we inherit the values from our parent. Note that since + * particles actually *use* the font/foreground values, we must + * clone the font, since each particle takes ownership of its own + * font. + */ struct font *font = font_node != NULL ? conf_to_font(font_node) : font_clone(inherited.font); struct rgba foreground = foreground_node != NULL ? conf_to_color(foreground_node) : inherited.foreground; - struct deco *deco = deco_node != NULL ? deco_from_config(deco_node) : NULL; + /* Instantiate base/common particle */ struct particle *common = particle_common_new( left, right, on_click_template, font, foreground, deco); const struct particle_info *info = plugin_load_particle(type); - assert(info != NULL); + assert(info != NULL); return info->from_conf(pair.value, common); } @@ -312,8 +323,25 @@ conf_to_bar(const struct yml_node *bar) struct yml_dict_iter m = yml_dict_iter(it.node); const char *mod_name = yml_value_as_string(m.key); + /* + * These aren't used by the modules, but passed down + * to particles. This allows us to specify a default + * font and foreground for each module, and having it + * applied to all its particles. + */ + const struct yml_node *mod_font = yml_get_value(m.value, "font"); + const struct yml_node *mod_foreground = yml_get_value( + m.value, "foreground"); + + struct conf_inherit mod_inherit = { + .font = mod_font != NULL + ? conf_to_font(mod_font) : inherited.font, + .foreground = mod_foreground != NULL + ? conf_to_color(mod_foreground) : inherited.foreground, + }; + const struct module_info *info = plugin_load_module(mod_name); - mods[idx] = info->from_conf(m.value, inherited); + mods[idx] = info->from_conf(m.value, mod_inherit); } if (i == 0) { diff --git a/module.h b/module.h index db0c6d6..74fd5cf 100644 --- a/module.h +++ b/module.h @@ -15,6 +15,11 @@ struct module_info { bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); struct module *(*from_conf)(const struct yml_node *node, struct conf_inherit inherited); + +#define MODULE_COMMON_ATTRS \ + {"font", false, &conf_verify_font}, \ + {"foreground", false, &conf_verify_color}, \ + {NULL, false, NULL} }; struct module_run_context { diff --git a/modules/alsa.c b/modules/alsa.c index 01abd45..d528196 100644 --- a/modules/alsa.c +++ b/modules/alsa.c @@ -288,7 +288,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"mixer", true, &conf_verify_string}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/backlight.c b/modules/backlight.c index 6582557..dff2e1a 100644 --- a/modules/backlight.c +++ b/modules/backlight.c @@ -231,7 +231,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"name", true, &conf_verify_string}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/battery.c b/modules/battery.c index 7810ce6..6d6c52b 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -364,7 +364,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"poll-interval", false, &conf_verify_int}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/clock.c b/modules/clock.c index d056c09..edb9005 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -115,7 +115,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"time-format", false, &conf_verify_string}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/i3.c b/modules/i3.c index 3d55361..70040f6 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -710,7 +710,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"right-spacing", false, &conf_verify_int}, {"content", true, &verify_content}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/label.c b/modules/label.c index 669443c..71d6455 100644 --- a/modules/label.c +++ b/modules/label.c @@ -60,7 +60,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) static const struct attr_info attrs[] = { {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/mpd.c b/modules/mpd.c index ae0986a..985b953 100644 --- a/modules/mpd.c +++ b/modules/mpd.c @@ -499,7 +499,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"port", false, &conf_verify_int}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/network.c b/modules/network.c index 7a526f3..7354ccb 100644 --- a/modules/network.c +++ b/modules/network.c @@ -550,7 +550,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"name", true, &conf_verify_string}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/removables.c b/modules/removables.c index c251264..889b55c 100644 --- a/modules/removables.c +++ b/modules/removables.c @@ -585,7 +585,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"right-spacing", false, &conf_verify_int}, {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/xkb.c b/modules/xkb.c index ee7edf5..f91e51d 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -465,7 +465,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) static const struct attr_info attrs[] = { {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs); diff --git a/modules/xwindow.c b/modules/xwindow.c index 3dc2cd8..e571647 100644 --- a/modules/xwindow.c +++ b/modules/xwindow.c @@ -327,7 +327,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) static const struct attr_info attrs[] = { {"content", true, &conf_verify_particle}, {"anchors", false, NULL}, - {NULL, false, NULL} + MODULE_COMMON_ATTRS, }; return conf_verify_dict(chain, node, attrs);