config: pass a struct with inheritable values

For now, font and foreground color
This commit is contained in:
Daniel Eklöf 2019-01-13 14:13:14 +01:00
parent 7776135454
commit 8dc278aaf2
20 changed files with 84 additions and 48 deletions

View file

@ -416,6 +416,7 @@ conf_verify_bar(const struct yml_node *bar)
{"border", false, &verify_bar_border}, {"border", false, &verify_bar_border},
{"font", false, &conf_verify_font}, {"font", false, &conf_verify_font},
{"foreground", false, &conf_verify_color},
{"left", false, &verify_module_list}, {"left", false, &verify_module_list},
{"center", false, &verify_module_list}, {"center", false, &verify_module_list},

View file

@ -14,6 +14,7 @@
#include "decorations/stack.h" #include "decorations/stack.h"
#include "decorations/underline.h" #include "decorations/underline.h"
#include "bar.h"
#include "module.h" #include "module.h"
#include "config-verify.h" #include "config-verify.h"
#include "plugin.h" #include "plugin.h"
@ -134,7 +135,7 @@ deco_from_config(const struct yml_node *node)
static struct particle * static struct particle *
particle_simple_list_from_config(const struct yml_node *node, 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); size_t count = yml_list_length(node);
struct particle *parts[count]; struct particle *parts[count];
@ -144,7 +145,7 @@ particle_simple_list_from_config(const struct yml_node *node,
it.node != NULL; it.node != NULL;
yml_list_next(&it), idx++) 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() */ /* 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( struct particle *common = particle_common_new(
0, 0, NULL, font_clone(parent_font), 0, 0, NULL, font_clone(inherited.font), inherited.foreground, NULL);
(struct rgba){1.0, 1.0, 1.0, 1.0}, NULL);
return particle_list_new(common, parts, count, 0, 2); return particle_list_new(common, parts, count, 0, 2);
} }
struct particle * 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)) 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); struct yml_dict_iter pair = yml_dict_iter(node);
const char *type = yml_value_as_string(pair.key); 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 const char *on_click_template
= on_click != NULL ? yml_value_as_string(on_click) : NULL; = on_click != NULL ? yml_value_as_string(on_click) : NULL;
struct font *font = font_node != 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 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 deco *deco = deco_node != NULL ? deco_from_config(deco_node) : NULL;
struct particle *common = particle_common_new( 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); 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 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"); const struct yml_node *font_node = yml_get_value(bar, "font");
if (font_node != NULL) { if (font_node != NULL) {
@ -277,6 +284,15 @@ conf_to_bar(const struct yml_node *bar)
font = conf_to_font(font_node); 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 *left = yml_get_value(bar, "left");
const struct yml_node *center = yml_get_value(bar, "center"); const struct yml_node *center = yml_get_value(bar, "center");
const struct yml_node *right = yml_get_value(bar, "right"); 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 char *mod_name = yml_value_as_string(m.key);
const struct module_info *info = plugin_load_module(mod_name); 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) { if (i == 0) {

View file

@ -1,10 +1,11 @@
#pragma once #pragma once
#include "bar.h"
#include "font.h" #include "font.h"
#include "particle.h"
#include "yml.h" #include "yml.h"
struct bar;
struct particle;
bool conf_verify_bar(const struct yml_node *bar); bool conf_verify_bar(const struct yml_node *bar);
struct bar *conf_to_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 rgba conf_to_color(const struct yml_node *node);
struct font *conf_to_font(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( struct particle * conf_to_particle(
const struct yml_node *node, const struct font *parent_font); const struct yml_node *node, struct conf_inherit inherited);

View file

@ -3,7 +3,7 @@
#include <threads.h> #include <threads.h>
#include <cairo.h> #include <cairo.h>
#include "config-verify.h" #include "config.h"
#include "particle.h" #include "particle.h"
#include "tag.h" #include "tag.h"
#include "yml.h" #include "yml.h"
@ -14,7 +14,7 @@ struct module;
struct module_info { struct module_info {
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); bool (*verify_conf)(keychain_t *chain, const struct yml_node *node);
struct module *(*from_conf)(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 { struct module_run_context {

View file

@ -268,7 +268,7 @@ alsa_new(const char *card, const char *mixer, struct particle *label)
} }
static struct module * 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 *card = yml_get_value(node, "card");
const struct yml_node *mixer = yml_get_value(node, "mixer"); 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( return alsa_new(
yml_value_as_string(card), yml_value_as_string(card),
yml_value_as_string(mixer), yml_value_as_string(mixer),
conf_to_particle(content, parent_font)); conf_to_particle(content, inherited));
} }
static bool static bool

View file

@ -215,13 +215,13 @@ backlight_new(const char *device, struct particle *label)
} }
static struct module * 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 *name = yml_get_value(node, "name");
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
return backlight_new( 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 static bool

View file

@ -344,7 +344,7 @@ battery_new(const char *battery, struct particle *label, int poll_interval_secs)
} }
static struct module * 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 *c = yml_get_value(node, "content");
const struct yml_node *name = yml_get_value(node, "name"); 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( return battery_new(
yml_value_as_string(name), 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); poll_interval != NULL ? yml_value_as_int(poll_interval) : 60);
} }

View file

@ -95,14 +95,14 @@ clock_new(struct particle *label, const char *date_format, const char *time_form
} }
static struct module * 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 *c = yml_get_value(node, "content");
const struct yml_node *date_format = yml_get_value(node, "date-format"); const struct yml_node *date_format = yml_get_value(node, "date-format");
const struct yml_node *time_format = yml_get_value(node, "time-format"); const struct yml_node *time_format = yml_get_value(node, "time-format");
return clock_new( return clock_new(
conf_to_particle(c, parent_font), conf_to_particle(c, inherited),
date_format != NULL ? yml_value_as_string(date_format) : "%x", date_format != NULL ? yml_value_as_string(date_format) : "%x",
time_format != NULL ? yml_value_as_string(time_format) : "%H:%M"); time_format != NULL ? yml_value_as_string(time_format) : "%H:%M");
} }

View file

@ -645,7 +645,7 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
} }
static struct module * 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 *c = yml_get_value(node, "content");
const struct yml_node *spacing = yml_get_value(node, "spacing"); 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++) yml_dict_next(&it), idx++)
{ {
workspaces[idx].name = yml_value_as_string(it.key); 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); return i3_new(workspaces, yml_dict_length(c), left, right);

View file

@ -4,6 +4,7 @@
#include <poll.h> #include <poll.h>
#include "../config.h" #include "../config.h"
#include "../module.h"
struct private { struct private {
struct particle *label; struct particle *label;
@ -47,10 +48,10 @@ label_new(struct particle *label)
} }
static struct module * 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 *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 static bool

View file

@ -479,7 +479,7 @@ mpd_new(const char *host, uint16_t port, struct particle *label)
} }
static struct module * 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 *host = yml_get_value(node, "host");
const struct yml_node *port = yml_get_value(node, "port"); 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( return mpd_new(
yml_value_as_string(host), yml_value_as_string(host),
port != NULL ? yml_value_as_int(port) : 0, port != NULL ? yml_value_as_int(port) : 0,
conf_to_particle(c, parent_font)); conf_to_particle(c, inherited));
} }
static bool static bool

View file

@ -534,13 +534,13 @@ network_new(const char *iface, struct particle *label)
} }
static struct module * 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 *name = yml_get_value(node, "name");
const struct yml_node *content = yml_get_value(node, "content"); const struct yml_node *content = yml_get_value(node, "content");
return network_new( 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 static bool

View file

@ -561,7 +561,7 @@ removables_new(struct particle *label, int left_spacing, int right_spacing)
} }
static struct module * 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 *content = yml_get_value(node, "content");
const struct yml_node *spacing = yml_get_value(node, "spacing"); 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) : int right = spacing != NULL ? yml_value_as_int(spacing) :
right_spacing != NULL ? yml_value_as_int(right_spacing) : 0; right_spacing != NULL ? yml_value_as_int(right_spacing) : 0;
return removables_new( return removables_new(conf_to_particle(content, inherited), left, right);
conf_to_particle(content, parent_font), left, right);
} }
static bool static bool

View file

@ -453,10 +453,10 @@ xkb_new(struct particle *label)
} }
static struct module * 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 *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 static bool

View file

@ -315,10 +315,10 @@ xwindow_new(struct particle *label)
} }
static struct module * 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 *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 static bool

View file

@ -14,7 +14,8 @@ struct exposable;
struct particle_info { struct particle_info {
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); 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_COUNT 5
#define PARTICLE_COMMON_ATTRS \ #define PARTICLE_COMMON_ATTRS \

View file

@ -183,7 +183,8 @@ from_conf(const struct yml_node *node, struct particle *common)
it.node != NULL; it.node != NULL;
yml_list_next(&it), idx++) 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); return particle_list_new(common, parts, count, left_spacing, right_spacing);

View file

@ -205,17 +205,22 @@ from_conf(const struct yml_node *node, struct particle *common)
struct particle_map particle_map[yml_dict_length(values)]; struct particle_map particle_map[yml_dict_length(values)];
struct conf_inherit inherited = {
.font = common->font,
.foreground = common->foreground
};
size_t idx = 0; size_t idx = 0;
for (struct yml_dict_iter it = yml_dict_iter(values); for (struct yml_dict_iter it = yml_dict_iter(values);
it.key != NULL; it.key != NULL;
yml_dict_next(&it), idx++) yml_dict_next(&it), idx++)
{ {
particle_map[idx].tag_value = yml_value_as_string(it.key); 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 struct particle *default_particle = def != NULL
? conf_to_particle(def, common->font) : NULL; ? conf_to_particle(def, inherited) : NULL;
return map_new( return map_new(
common, yml_value_as_string(tag), particle_map, yml_dict_length(values), common, yml_value_as_string(tag), particle_map, yml_dict_length(values),

View file

@ -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 *empty = yml_get_value(node, "empty");
const struct yml_node *indicator = yml_get_value(node, "indicator"); const struct yml_node *indicator = yml_get_value(node, "indicator");
struct conf_inherit inherited = {
.font = common->font,
.foreground = common->foreground,
};
return progress_bar_new( return progress_bar_new(
common, common,
yml_value_as_string(tag), yml_value_as_string(tag),
yml_value_as_int(length), yml_value_as_int(length),
conf_to_particle(start, common->font), conf_to_particle(start, inherited),
conf_to_particle(end, common->font), conf_to_particle(end, inherited),
conf_to_particle(fill, common->font), conf_to_particle(fill, inherited),
conf_to_particle(empty, common->font), conf_to_particle(empty, inherited),
conf_to_particle(indicator, common->font)); conf_to_particle(indicator, inherited));
} }
static bool static bool

View file

@ -167,7 +167,8 @@ from_conf(const struct yml_node *node, struct particle *common)
it.node != NULL; it.node != NULL;
yml_list_next(&it), idx++) 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); return ramp_new(common, yml_value_as_string(tag), parts, count);