diff --git a/config-verify.c b/config-verify.c index 6dd8d7c..97ea8cf 100644 --- a/config-verify.c +++ b/config-verify.c @@ -416,6 +416,7 @@ conf_verify_bar(const struct yml_node *bar) {"border", false, &verify_bar_border}, {"font", false, &conf_verify_font}, + {"foreground", false, &conf_verify_color}, {"left", false, &verify_module_list}, {"center", false, &verify_module_list}, diff --git a/config.c b/config.c index c38f8d6..3981e83 100644 --- a/config.c +++ b/config.c @@ -14,6 +14,7 @@ #include "decorations/stack.h" #include "decorations/underline.h" +#include "bar.h" #include "module.h" #include "config-verify.h" #include "plugin.h" @@ -134,7 +135,7 @@ deco_from_config(const struct yml_node *node) static struct particle * particle_simple_list_from_config(const struct yml_node *node, - const struct font *parent_font) + struct conf_inherit inherited) { size_t count = yml_list_length(node); struct particle *parts[count]; @@ -144,7 +145,7 @@ particle_simple_list_from_config(const struct yml_node *node, it.node != NULL; yml_list_next(&it), idx++) { - parts[idx] = conf_to_particle(it.node, parent_font); + parts[idx] = conf_to_particle(it.node, inherited); } /* Lazy-loaded function pointer to particle_list_new() */ @@ -161,17 +162,16 @@ particle_simple_list_from_config(const struct yml_node *node, } struct particle *common = particle_common_new( - 0, 0, NULL, font_clone(parent_font), - (struct rgba){1.0, 1.0, 1.0, 1.0}, NULL); + 0, 0, NULL, font_clone(inherited.font), inherited.foreground, NULL); return particle_list_new(common, parts, count, 0, 2); } struct particle * -conf_to_particle(const struct yml_node *node, const struct font *parent_font) +conf_to_particle(const struct yml_node *node, struct conf_inherit inherited) { if (yml_is_list(node)) - return particle_simple_list_from_config(node, parent_font); + return particle_simple_list_from_config(node, inherited); struct yml_dict_iter pair = yml_dict_iter(node); const char *type = yml_value_as_string(pair.key); @@ -192,9 +192,9 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) const char *on_click_template = on_click != NULL ? yml_value_as_string(on_click) : NULL; struct font *font = font_node != NULL - ? conf_to_font(font_node) : font_clone(parent_font); + ? conf_to_font(font_node) : font_clone(inherited.font); struct rgba foreground = foreground_node != NULL - ? conf_to_color(foreground_node) : (struct rgba){1.0, 1.0, 1.0, 1.0}; + ? conf_to_color(foreground_node) : inherited.foreground; struct deco *deco = deco_node != NULL ? deco_from_config(deco_node) : NULL; struct particle *common = particle_common_new( @@ -268,8 +268,15 @@ conf_to_bar(const struct yml_node *bar) conf.border.color = conf_to_color(color); } - /* Create a default font */ + /* + * Create a default font and foreground + * + * These aren't used by the bar itself, but passed down to modules + * and particles. This allows us to specify a default font and + * foreground color at top-level. + */ struct font *font = font_new("sans"); + struct rgba foreground = (struct rgba){1.0, 1.0, 1.0, 1.0}; /* White */ const struct yml_node *font_node = yml_get_value(bar, "font"); if (font_node != NULL) { @@ -277,6 +284,15 @@ conf_to_bar(const struct yml_node *bar) font = conf_to_font(font_node); } + const struct yml_node *foreground_node = yml_get_value(bar, "foreground"); + if (foreground_node != NULL) + foreground = conf_to_color(foreground_node); + + struct conf_inherit inherited = { + .font = font, + .foreground = foreground, + }; + const struct yml_node *left = yml_get_value(bar, "left"); const struct yml_node *center = yml_get_value(bar, "center"); const struct yml_node *right = yml_get_value(bar, "right"); @@ -297,7 +313,7 @@ conf_to_bar(const struct yml_node *bar) const char *mod_name = yml_value_as_string(m.key); const struct module_info *info = plugin_load_module(mod_name); - mods[idx] = info->from_conf(m.value, font); + mods[idx] = info->from_conf(m.value, inherited); } if (i == 0) { diff --git a/config.h b/config.h index 0bdbc09..3579877 100644 --- a/config.h +++ b/config.h @@ -1,10 +1,11 @@ #pragma once -#include "bar.h" #include "font.h" -#include "particle.h" #include "yml.h" +struct bar; +struct particle; + bool conf_verify_bar(const struct yml_node *bar); struct bar *conf_to_bar(const struct yml_node *bar); @@ -15,5 +16,10 @@ struct bar *conf_to_bar(const struct yml_node *bar); struct rgba conf_to_color(const struct yml_node *node); struct font *conf_to_font(const struct yml_node *node); +struct conf_inherit { + const struct font *font; + struct rgba foreground; +}; + struct particle * conf_to_particle( - const struct yml_node *node, const struct font *parent_font); + const struct yml_node *node, struct conf_inherit inherited); diff --git a/module.h b/module.h index 38bd48f..db0c6d6 100644 --- a/module.h +++ b/module.h @@ -3,7 +3,7 @@ #include #include -#include "config-verify.h" +#include "config.h" #include "particle.h" #include "tag.h" #include "yml.h" @@ -14,7 +14,7 @@ struct module; struct module_info { bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); struct module *(*from_conf)(const struct yml_node *node, - const struct font *parent_font); + struct conf_inherit inherited); }; struct module_run_context { diff --git a/modules/alsa.c b/modules/alsa.c index ebb4343..01abd45 100644 --- a/modules/alsa.c +++ b/modules/alsa.c @@ -268,7 +268,7 @@ alsa_new(const char *card, const char *mixer, struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *card = yml_get_value(node, "card"); const struct yml_node *mixer = yml_get_value(node, "mixer"); @@ -277,7 +277,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return alsa_new( yml_value_as_string(card), yml_value_as_string(mixer), - conf_to_particle(content, parent_font)); + conf_to_particle(content, inherited)); } static bool diff --git a/modules/backlight.c b/modules/backlight.c index 2df38ea..6582557 100644 --- a/modules/backlight.c +++ b/modules/backlight.c @@ -215,13 +215,13 @@ backlight_new(const char *device, struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *name = yml_get_value(node, "name"); const struct yml_node *c = yml_get_value(node, "content"); return backlight_new( - yml_value_as_string(name), conf_to_particle(c, parent_font)); + yml_value_as_string(name), conf_to_particle(c, inherited)); } static bool diff --git a/modules/battery.c b/modules/battery.c index 600122c..7810ce6 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -344,7 +344,7 @@ battery_new(const char *battery, struct particle *label, int poll_interval_secs) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *name = yml_get_value(node, "name"); @@ -352,7 +352,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return battery_new( yml_value_as_string(name), - conf_to_particle(c, parent_font), + conf_to_particle(c, inherited), poll_interval != NULL ? yml_value_as_int(poll_interval) : 60); } diff --git a/modules/clock.c b/modules/clock.c index c538b5a..d056c09 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -95,14 +95,14 @@ clock_new(struct particle *label, const char *date_format, const char *time_form } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *date_format = yml_get_value(node, "date-format"); const struct yml_node *time_format = yml_get_value(node, "time-format"); return clock_new( - conf_to_particle(c, parent_font), + conf_to_particle(c, inherited), date_format != NULL ? yml_value_as_string(date_format) : "%x", time_format != NULL ? yml_value_as_string(time_format) : "%H:%M"); } diff --git a/modules/i3.c b/modules/i3.c index e71828c..3d55361 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -645,7 +645,7 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count, } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *spacing = yml_get_value(node, "spacing"); @@ -665,7 +665,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) yml_dict_next(&it), idx++) { workspaces[idx].name = yml_value_as_string(it.key); - workspaces[idx].content = conf_to_particle(it.value, parent_font); + workspaces[idx].content = conf_to_particle(it.value, inherited); } return i3_new(workspaces, yml_dict_length(c), left, right); diff --git a/modules/label.c b/modules/label.c index 1f5a46a..669443c 100644 --- a/modules/label.c +++ b/modules/label.c @@ -4,6 +4,7 @@ #include #include "../config.h" +#include "../module.h" struct private { struct particle *label; @@ -47,10 +48,10 @@ label_new(struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); - return label_new(conf_to_particle(c, parent_font)); + return label_new(conf_to_particle(c, inherited)); } static bool diff --git a/modules/mpd.c b/modules/mpd.c index c6472de..ae0986a 100644 --- a/modules/mpd.c +++ b/modules/mpd.c @@ -479,7 +479,7 @@ mpd_new(const char *host, uint16_t port, struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *host = yml_get_value(node, "host"); const struct yml_node *port = yml_get_value(node, "port"); @@ -488,7 +488,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return mpd_new( yml_value_as_string(host), port != NULL ? yml_value_as_int(port) : 0, - conf_to_particle(c, parent_font)); + conf_to_particle(c, inherited)); } static bool diff --git a/modules/network.c b/modules/network.c index 7df8233..7a526f3 100644 --- a/modules/network.c +++ b/modules/network.c @@ -534,13 +534,13 @@ network_new(const char *iface, struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *name = yml_get_value(node, "name"); const struct yml_node *content = yml_get_value(node, "content"); return network_new( - yml_value_as_string(name), conf_to_particle(content, parent_font)); + yml_value_as_string(name), conf_to_particle(content, inherited)); } static bool diff --git a/modules/removables.c b/modules/removables.c index a788541..c251264 100644 --- a/modules/removables.c +++ b/modules/removables.c @@ -561,7 +561,7 @@ removables_new(struct particle *label, int left_spacing, int right_spacing) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *content = yml_get_value(node, "content"); const struct yml_node *spacing = yml_get_value(node, "spacing"); @@ -573,8 +573,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) int right = spacing != NULL ? yml_value_as_int(spacing) : right_spacing != NULL ? yml_value_as_int(right_spacing) : 0; - return removables_new( - conf_to_particle(content, parent_font), left, right); + return removables_new(conf_to_particle(content, inherited), left, right); } static bool diff --git a/modules/xkb.c b/modules/xkb.c index a373801..ee7edf5 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -453,10 +453,10 @@ xkb_new(struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); - return xkb_new(conf_to_particle(c, parent_font)); + return xkb_new(conf_to_particle(c, inherited)); } static bool diff --git a/modules/xwindow.c b/modules/xwindow.c index c91908d..3dc2cd8 100644 --- a/modules/xwindow.c +++ b/modules/xwindow.c @@ -315,10 +315,10 @@ xwindow_new(struct particle *label) } static struct module * -from_conf(const struct yml_node *node, const struct font *parent_font) +from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); - return xwindow_new(conf_to_particle(c, parent_font)); + return xwindow_new(conf_to_particle(c, inherited)); } static bool diff --git a/particle.h b/particle.h index 7518374..0d46b88 100644 --- a/particle.h +++ b/particle.h @@ -14,7 +14,8 @@ struct exposable; struct particle_info { bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); - struct particle *(*from_conf)(const struct yml_node *node, struct particle *common); + struct particle *(*from_conf)( + const struct yml_node *node, struct particle *common); #define PARTICLE_COMMON_ATTRS_COUNT 5 #define PARTICLE_COMMON_ATTRS \ diff --git a/particles/list.c b/particles/list.c index 10da4b4..f0980b7 100644 --- a/particles/list.c +++ b/particles/list.c @@ -183,7 +183,8 @@ from_conf(const struct yml_node *node, struct particle *common) it.node != NULL; yml_list_next(&it), idx++) { - parts[idx] = conf_to_particle(it.node, common->font); + parts[idx] = conf_to_particle( + it.node, (struct conf_inherit){common->font, common->foreground}); } return particle_list_new(common, parts, count, left_spacing, right_spacing); diff --git a/particles/map.c b/particles/map.c index e9bf03b..0b4c0bf 100644 --- a/particles/map.c +++ b/particles/map.c @@ -205,17 +205,22 @@ from_conf(const struct yml_node *node, struct particle *common) struct particle_map particle_map[yml_dict_length(values)]; + struct conf_inherit inherited = { + .font = common->font, + .foreground = common->foreground + }; + size_t idx = 0; for (struct yml_dict_iter it = yml_dict_iter(values); it.key != NULL; yml_dict_next(&it), idx++) { particle_map[idx].tag_value = yml_value_as_string(it.key); - particle_map[idx].particle = conf_to_particle(it.value, common->font); + particle_map[idx].particle = conf_to_particle(it.value, inherited); } struct particle *default_particle = def != NULL - ? conf_to_particle(def, common->font) : NULL; + ? conf_to_particle(def, inherited) : NULL; return map_new( common, yml_value_as_string(tag), particle_map, yml_dict_length(values), diff --git a/particles/progress-bar.c b/particles/progress-bar.c index 169592a..e69a1c4 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -239,15 +239,20 @@ from_conf(const struct yml_node *node, struct particle *common) const struct yml_node *empty = yml_get_value(node, "empty"); const struct yml_node *indicator = yml_get_value(node, "indicator"); + struct conf_inherit inherited = { + .font = common->font, + .foreground = common->foreground, + }; + return progress_bar_new( common, yml_value_as_string(tag), yml_value_as_int(length), - conf_to_particle(start, common->font), - conf_to_particle(end, common->font), - conf_to_particle(fill, common->font), - conf_to_particle(empty, common->font), - conf_to_particle(indicator, common->font)); + conf_to_particle(start, inherited), + conf_to_particle(end, inherited), + conf_to_particle(fill, inherited), + conf_to_particle(empty, inherited), + conf_to_particle(indicator, inherited)); } static bool diff --git a/particles/ramp.c b/particles/ramp.c index 3d912ed..a17f716 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -167,7 +167,8 @@ from_conf(const struct yml_node *node, struct particle *common) it.node != NULL; yml_list_next(&it), idx++) { - parts[idx] = conf_to_particle(it.node, common->font); + parts[idx] = conf_to_particle( + it.node, (struct conf_inherit){common->font, common->foreground}); } return ramp_new(common, yml_value_as_string(tag), parts, count);