From f34a341c337d72319abc5c2ac1586ed7329dd131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:36:26 +0100 Subject: [PATCH 01/15] particle: add struct particle_info definition --- particle.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/particle.h b/particle.h index c12d7fe..8bbaa48 100644 --- a/particle.h +++ b/particle.h @@ -3,13 +3,23 @@ #include #include "color.h" +#include "config-verify.h" #include "decoration.h" #include "font.h" #include "tag.h" +#include "yml.h" struct bar; +struct particle; struct exposable; +struct particle_info { + struct particle *(*from_conf)(const struct yml_node *node, + const struct font *parent_font); + size_t attr_count; /* TODO: reomve, NULL-terminate attr list instead */ + const struct attr_info attrs[]; +}; + struct particle { void *private; From 0fa7906e996be06601ae5b99f57db6774eb06d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:36:45 +0100 Subject: [PATCH 02/15] plugin: add plugin_load_particle() function --- plugin.c | 33 +++++++++++++++++++++++++-------- plugin.h | 2 ++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugin.c b/plugin.c index 7f59e55..3c3c3bc 100644 --- a/plugin.c +++ b/plugin.c @@ -9,8 +9,12 @@ #include "config.h" #include "tllist.h" +enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE }; + struct plugin { char *name; + enum plugin_type type; + void *lib; const void *sym; }; @@ -36,21 +40,21 @@ fini(void) tll_free_and_free(plugins, free_plugin); } -const struct module_info * -plugin_load_module(const char *name) +static const void * +_load_plugin(const char *name, enum plugin_type type) { - char path[128]; - snprintf(path, sizeof(path), "lib%s.so", name); - /* Have we already loaded it? */ tll_foreach(plugins, plug) { - if (strcmp(plug->item.name, name) == 0) { + if (plug->item.type == type && strcmp(plug->item.name, name) == 0) { LOG_DBG("%s already loaded: %p", name, plug->item.lib); assert(plug->item.sym != NULL); return plug->item.sym; } } + char path[128]; + snprintf(path, sizeof(path), "lib%s.so", name); + /* Not loaded - do it now */ void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); LOG_DBG("%s: dlopened to %p", name, lib); @@ -60,11 +64,12 @@ plugin_load_module(const char *name) return NULL; } - tll_push_back(plugins, ((struct plugin){strdup(name), lib})); + tll_push_back(plugins, ((struct plugin){strdup(name), type, lib, NULL})); struct plugin *plug = &tll_back(plugins); + /* TODO: rename to plugin_info or so, in both modules and particles */ dlerror(); /* Clear previous error */ - plug->sym = dlsym(lib, "module_info"); + plug->sym = dlsym(lib, type == PLUGIN_MODULE ? "module_info" : "particle_info"); const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { @@ -75,3 +80,15 @@ plugin_load_module(const char *name) assert(plug->sym != NULL); return plug->sym; } + +const struct module_info * +plugin_load_module(const char *name) +{ + return _load_plugin(name, PLUGIN_MODULE); +} + +const struct particle_info * +plugin_load_particle(const char *name) +{ + return _load_plugin(name, PLUGIN_PARTICLE); +} diff --git a/plugin.h b/plugin.h index 6d829e2..58e4421 100644 --- a/plugin.h +++ b/plugin.h @@ -1,5 +1,7 @@ #pragma once #include "module.h" +#include "particle.h" const struct module_info *plugin_load_module(const char *name); +const struct particle_info *plugin_load_particle(const char *name); From 72edcf608c09cbf4529468c186c17b7a5d0e1403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:47:36 +0100 Subject: [PATCH 03/15] plugin: include plugin type in log messages --- plugin.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/plugin.c b/plugin.c index 3c3c3bc..0ed13e4 100644 --- a/plugin.c +++ b/plugin.c @@ -21,6 +21,17 @@ struct plugin { static tll(struct plugin) plugins = tll_init(); +static const char * +type2str(enum plugin_type type) +{ + switch (type) { + case PLUGIN_MODULE: return "module"; + case PLUGIN_PARTICLE: return "particle"; + } + + return NULL; +} + static void free_plugin(struct plugin plug) { @@ -29,7 +40,7 @@ free_plugin(struct plugin plug) const char *dl_error = dlerror(); if (dl_error != NULL) - LOG_ERR("%s: dlclose(): %s", plug.name, dl_error); + LOG_ERR("%s: %s: dlclose(): %s", type2str(plug.type), plug.name, dl_error); free(plug.name); } @@ -46,7 +57,7 @@ _load_plugin(const char *name, enum plugin_type type) /* Have we already loaded it? */ tll_foreach(plugins, plug) { if (plug->item.type == type && strcmp(plug->item.name, name) == 0) { - LOG_DBG("%s already loaded: %p", name, plug->item.lib); + LOG_DBG("%s: %s already loaded: %p", type2str(type), name, plug->item.lib); assert(plug->item.sym != NULL); return plug->item.sym; } @@ -57,10 +68,10 @@ _load_plugin(const char *name, enum plugin_type type) /* Not loaded - do it now */ void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); - LOG_DBG("%s: dlopened to %p", name, lib); + LOG_DBG("%s: %s: dlopened to %p", type2str(type), name, lib); if (lib == NULL) { - LOG_ERR("%s: dlopen: %s", name, dlerror()); + LOG_ERR("%s: %s: dlopen: %s", type2str(type), name, dlerror()); return NULL; } @@ -73,7 +84,7 @@ _load_plugin(const char *name, enum plugin_type type) const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { - LOG_ERR("%s: dlsym: %s", name, dlsym_error); + LOG_ERR("%s: %s: dlsym: %s", type2str(type), name, dlsym_error); return NULL; } From c2db518a4f086c4eb068c5552724b53b11c09be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:47:46 +0100 Subject: [PATCH 04/15] particle: fix from_conf() function pointer prototype and add macro for common attributes (shared by all particles) --- particle.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/particle.h b/particle.h index 8bbaa48..6bf1621 100644 --- a/particle.h +++ b/particle.h @@ -15,9 +15,20 @@ struct exposable; struct particle_info { struct particle *(*from_conf)(const struct yml_node *node, - const struct font *parent_font); + const struct font *parent_font, + int left_margin, int right_margin, + const char *on_click_template); + size_t attr_count; /* TODO: reomve, NULL-terminate attr list instead */ const struct attr_info attrs[]; + +#define PARTICLE_COMMON_ATTRS_COUNT 4 +#define PARTICLE_COMMON_ATTRS \ + {"margin", false, &conf_verify_int}, \ + {"left-margin", false, &conf_verify_int}, \ + {"right-margin", false, &conf_verify_int}, \ + {"on-click", false, &conf_verify_string} + }; struct particle { From 0f8f21510a282db03f53b40078335fd2d8bc2424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:48:23 +0100 Subject: [PATCH 05/15] particle/empty: expose info through the new struct particle_info struct --- config-verify.c | 29 ++++++++++++++++++++++++----- config.c | 11 ++--------- particles/empty.c | 22 +++++++++++++++++++--- particles/empty.h | 3 +-- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/config-verify.c b/config-verify.c index 6597b39..69735b5 100644 --- a/config-verify.c +++ b/config-verify.c @@ -9,6 +9,8 @@ #include "plugin.h" #include "tllist.h" +#include "particles/empty.h" + const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) { @@ -315,10 +317,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 empty[] = { - COMMON_ATTRS - }; - static const struct attr_info list[] = { {"items", true, &verify_list_items}, {"spacing", false, &conf_verify_int}, @@ -363,12 +361,18 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) #undef COMMON_ATTRS + static const struct { + const char *name; + const struct particle_info *info; + } particles_v2[] = { + {"empty", &particle_empty}, + }; + static const struct { const char *name; const struct attr_info *attrs; size_t count; } particles[] = { - {"empty", empty, sizeof(empty) / sizeof(empty[0])}, {"list", list, sizeof(list) / sizeof(list[0])}, {"map", map, sizeof(map) / sizeof(map[0])}, {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, @@ -376,6 +380,21 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"string", string, sizeof(string) / sizeof(string[0])}, }; + for (size_t i = 0; i < sizeof(particles_v2) / sizeof(particles_v2[0]); i++) { + if (strcmp(particles_v2[i].name, particle_name) != 0) + continue; + + if (!conf_verify_dict(chain_push(chain, particle_name), values, + particles_v2[i].info->attrs, + particles_v2[i].info->attr_count)) + { + return false; + } + + chain_pop(chain); + return true; + } + for (size_t i = 0; i < sizeof(particles) / sizeof(particles[0]); i++) { if (strcmp(particles[i].name, particle_name) != 0) continue; diff --git a/config.c b/config.c index e18660c..74f970a 100644 --- a/config.c +++ b/config.c @@ -137,14 +137,6 @@ deco_from_config(const struct yml_node *node) return NULL; } -static struct particle * -particle_empty_from_config(const struct yml_node *node, - const struct font *parent_font, - int left_margin, int right_margin, - const char *on_click_template) -{ - return particle_empty_new(left_margin, right_margin, on_click_template); -} static struct particle * particle_string_from_config(const struct yml_node *node, @@ -323,8 +315,9 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) struct particle *ret = NULL; if (strcmp(type, "empty") == 0) - ret = particle_empty_from_config( + ret = particle_empty.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "string") == 0) ret = particle_string_from_config( pair.value, parent_font, left, right, on_click_template); diff --git a/particles/empty.c b/particles/empty.c index ead1dfc..0ab63a2 100644 --- a/particles/empty.c +++ b/particles/empty.c @@ -2,6 +2,8 @@ #include +#include "../config.h" + static int begin_expose(struct exposable *exposable) { @@ -29,9 +31,8 @@ instantiate(const struct particle *particle, const struct tag_set *tags) return exposable; } -struct particle * -particle_empty_new(int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +empty_new(int left_margin, int right_margin, const char *on_click_template) { struct particle *particle = particle_common_new( left_margin, right_margin, on_click_template); @@ -39,3 +40,18 @@ particle_empty_new(int left_margin, int right_margin, particle->instantiate = &instantiate; 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) +{ + return empty_new(left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_empty = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 0, + .attrs = { + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/empty.h b/particles/empty.h index b353f3e..5e2f567 100644 --- a/particles/empty.h +++ b/particles/empty.h @@ -1,5 +1,4 @@ #pragma once #include "../particle.h" -struct particle *particle_empty_new( - int left_margin, int right_margin, const char *on_click_template); +extern const struct particle_info particle_empty; From 6379b1939f9edd41e9ff4b8f3de176cb3e91c0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:56:00 +0100 Subject: [PATCH 06/15] particle/list: expose info through the new struct particle_info struct --- config-verify.c | 19 ++++++------------- config-verify.h | 1 + config.c | 38 +++----------------------------------- particles/list.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ particles/list.h | 2 ++ 5 files changed, 56 insertions(+), 48 deletions(-) diff --git a/config-verify.c b/config-verify.c index 69735b5..b8659fa 100644 --- a/config-verify.c +++ b/config-verify.c @@ -10,6 +10,7 @@ #include "tllist.h" #include "particles/empty.h" +#include "particles/list.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -245,8 +246,8 @@ verify_decoration(keychain_t *chain, const struct yml_node *node) return false; } -static bool -verify_list_items(keychain_t *chain, const struct yml_node *node) +bool +conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node) { assert(yml_is_list(node)); @@ -317,14 +318,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 list[] = { - {"items", true, &verify_list_items}, - {"spacing", false, &conf_verify_int}, - {"left-spacing", false, &conf_verify_int}, - {"right-spacing", false, &conf_verify_int}, - COMMON_ATTRS - }; - static const struct attr_info map[] = { {"tag", true, &conf_verify_string}, {"values", true, &verify_map_values}, @@ -346,7 +339,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) static const struct attr_info ramp[] = { {"tag", true, &conf_verify_string}, - {"items", true, &verify_list_items}, + {"items", true, &conf_verify_particle_list_items}, COMMON_ATTRS }; @@ -366,6 +359,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct particle_info *info; } particles_v2[] = { {"empty", &particle_empty}, + {"list", &particle_list}, }; static const struct { @@ -373,7 +367,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"list", list, sizeof(list) / sizeof(list[0])}, {"map", map, sizeof(map) / sizeof(map[0])}, {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, @@ -420,7 +413,7 @@ conf_verify_particle(keychain_t *chain, const struct yml_node *node) if (yml_is_dict(node)) return conf_verify_particle_dictionary(chain, node); else if (yml_is_list(node)) - return verify_list_items(chain, node); + return conf_verify_particle_list_items(chain, node); else { LOG_ERR("%s: particle must be either a dictionary or a list", conf_err_prefix(chain, node)); diff --git a/config-verify.h b/config-verify.h index a6f4660..18c7900 100644 --- a/config-verify.h +++ b/config-verify.h @@ -42,3 +42,4 @@ bool conf_verify_color(keychain_t *chain, const struct yml_node *node); 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); diff --git a/config.c b/config.c index 74f970a..3ddfca3 100644 --- a/config.c +++ b/config.c @@ -160,38 +160,6 @@ particle_string_from_config(const struct yml_node *node, fg_color, left_margin, right_margin, on_click_template); } -static struct particle * -particle_list_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 *items = yml_get_value(node, "items"); - - const struct yml_node *spacing = yml_get_value(node, "spacing"); - const struct yml_node *_left_spacing = yml_get_value(node, "left-spacing"); - const struct yml_node *_right_spacing = yml_get_value(node, "right-spacing"); - - int left_spacing = spacing != NULL ? yml_value_as_int(spacing) : - _left_spacing != NULL ? yml_value_as_int(_left_spacing) : 0; - int right_spacing = spacing != NULL ? yml_value_as_int(spacing) : - _right_spacing != NULL ? yml_value_as_int(_right_spacing) : 2; - - size_t count = yml_list_length(items); - struct particle *parts[count]; - - size_t idx = 0; - for (struct yml_list_iter it = yml_list_iter(items); - it.node != NULL; - yml_list_next(&it), idx++) - { - parts[idx] = conf_to_particle(it.node, parent_font); - } - - return particle_list_new( - parts, count, left_spacing, right_spacing, left_margin, right_margin, - on_click_template); -} static struct particle * particle_map_from_config(const struct yml_node *node, @@ -317,13 +285,13 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) if (strcmp(type, "empty") == 0) ret = particle_empty.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "list") == 0) + ret = particle_list.from_conf( + pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "string") == 0) ret = particle_string_from_config( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "list") == 0) - ret = particle_list_from_config( - pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "map") == 0) ret = particle_map_from_config( pair.value, parent_font, left, right, on_click_template); diff --git a/particles/list.c b/particles/list.c index 9a9a5e3..e427c0c 100644 --- a/particles/list.c +++ b/particles/list.c @@ -4,6 +4,7 @@ #define LOG_MODULE "list" #define LOG_ENABLE_DBG 1 #include "../log.h" +#include "../config.h" struct private { struct particle **particles; @@ -165,3 +166,46 @@ particle_list_new( 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 *items = yml_get_value(node, "items"); + + const struct yml_node *spacing = yml_get_value(node, "spacing"); + const struct yml_node *_left_spacing = yml_get_value(node, "left-spacing"); + const struct yml_node *_right_spacing = yml_get_value(node, "right-spacing"); + + int left_spacing = spacing != NULL ? yml_value_as_int(spacing) : + _left_spacing != NULL ? yml_value_as_int(_left_spacing) : 0; + int right_spacing = spacing != NULL ? yml_value_as_int(spacing) : + _right_spacing != NULL ? yml_value_as_int(_right_spacing) : 2; + + size_t count = yml_list_length(items); + struct particle *parts[count]; + + size_t idx = 0; + for (struct yml_list_iter it = yml_list_iter(items); + it.node != NULL; + yml_list_next(&it), idx++) + { + parts[idx] = conf_to_particle(it.node, parent_font); + } + + return particle_list_new( + parts, count, left_spacing, right_spacing, left_margin, right_margin, + on_click_template); +} + +const struct particle_info particle_list = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 4, + .attrs = { + {"items", true, &conf_verify_particle_list_items}, + {"spacing", false, &conf_verify_int}, + {"left-spacing", false, &conf_verify_int}, + {"right-spacing", false, &conf_verify_int}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/list.h b/particles/list.h index f6d1d42..01dabb4 100644 --- a/particles/list.h +++ b/particles/list.h @@ -5,3 +5,5 @@ struct particle *particle_list_new( struct particle *particles[], size_t count, int left_spacing, int right_spacing, int left_margin, int right_margin, const char *on_click_template); + +extern const struct particle_info particle_list; From 73b8bf13463bc6641925ccb72ea5a43179df9b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:34:15 +0100 Subject: [PATCH 07/15] particle/map: expose info through the new struct particle_info struct --- config-verify.c | 39 ++-------------------- config.c | 36 ++------------------ particles/map.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++--- particles/map.h | 10 +----- 4 files changed, 88 insertions(+), 84 deletions(-) diff --git a/config-verify.c b/config-verify.c index b8659fa..1a99d9f 100644 --- a/config-verify.c +++ b/config-verify.c @@ -11,6 +11,7 @@ #include "particles/empty.h" #include "particles/list.h" +#include "particles/map.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -262,35 +263,6 @@ conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node) return true; } -static bool -verify_map_values(keychain_t *chain, const struct yml_node *node) -{ - if (!yml_is_dict(node)) { - LOG_ERR( - "%s: must be a dictionary of workspace-name: particle mappings", - conf_err_prefix(chain, node)); - return false; - } - - for (struct yml_dict_iter it = yml_dict_iter(node); - it.key != NULL; - yml_dict_next(&it)) - { - const char *key = yml_value_as_string(it.key); - if (key == NULL) { - LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key)); - return false; - } - - if (!conf_verify_particle(chain_push(chain, key), it.value)) - return false; - - chain_pop(chain); - } - - return true; -} - static bool conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) { @@ -318,13 +290,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 map[] = { - {"tag", true, &conf_verify_string}, - {"values", true, &verify_map_values}, - {"default", false, &conf_verify_particle}, - COMMON_ATTRS - }; - static const struct attr_info progress_bar[] = { {"tag", true, &conf_verify_string}, {"length", true, &conf_verify_int}, @@ -360,6 +325,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) } particles_v2[] = { {"empty", &particle_empty}, {"list", &particle_list}, + {"map", &particle_map}, }; static const struct { @@ -367,7 +333,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"map", map, sizeof(map) / sizeof(map[0])}, {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, {"string", string, sizeof(string) / sizeof(string[0])}, diff --git a/config.c b/config.c index 3ddfca3..c2845fc 100644 --- a/config.c +++ b/config.c @@ -161,36 +161,6 @@ particle_string_from_config(const struct yml_node *node, } -static struct particle * -particle_map_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 *tag = yml_get_value(node, "tag"); - const struct yml_node *values = yml_get_value(node, "values"); - const struct yml_node *def = yml_get_value(node, "default"); - - struct particle_map particle_map[yml_dict_length(values)]; - - 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, parent_font); - } - - struct particle *default_particle = def != NULL - ? conf_to_particle(def, parent_font) - : NULL; - - return particle_map_new( - yml_value_as_string(tag), particle_map, yml_dict_length(values), - default_particle, left_margin, right_margin, on_click_template); -} - static struct particle * particle_ramp_from_config(const struct yml_node *node, const struct font *parent_font, @@ -288,13 +258,13 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "list") == 0) ret = particle_list.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "map") == 0) + ret = particle_map.from_conf( + pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "string") == 0) ret = particle_string_from_config( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "map") == 0) - ret = particle_map_from_config( - pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "ramp") == 0) ret = particle_ramp_from_config( pair.value, parent_font, left, right, on_click_template); diff --git a/particles/map.c b/particles/map.c index 5094fb2..fee606b 100644 --- a/particles/map.c +++ b/particles/map.c @@ -4,6 +4,15 @@ #include #include +#define LOG_MODULE "map" +#include "../log.h" +#include "../config.h" + +struct particle_map { + const char *tag_value; + struct particle *particle; +}; + struct private { char *tag; struct particle *default_particle; @@ -136,11 +145,11 @@ particle_destroy(struct particle *particle) particle_default_destroy(particle); } -struct particle * -particle_map_new(const char *tag, const struct particle_map *particle_map, - size_t count, struct particle *default_particle, - int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +map_new(const char *tag, const struct particle_map *particle_map, + size_t count, struct particle *default_particle, + int left_margin, int right_margin, + const char *on_click_template) { struct particle *particle = particle_common_new( left_margin, right_margin, on_click_template); @@ -161,3 +170,71 @@ particle_map_new(const char *tag, const struct particle_map *particle_map, particle->private = priv; return particle; } + +static bool +verify_map_values(keychain_t *chain, const struct yml_node *node) +{ + if (!yml_is_dict(node)) { + LOG_ERR( + "%s: must be a dictionary of workspace-name: particle mappings", + conf_err_prefix(chain, node)); + return false; + } + + for (struct yml_dict_iter it = yml_dict_iter(node); + it.key != NULL; + yml_dict_next(&it)) + { + const char *key = yml_value_as_string(it.key); + if (key == NULL) { + LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key)); + return false; + } + + if (!conf_verify_particle(chain_push(chain, key), it.value)) + return false; + + chain_pop(chain); + } + + return true; +} + +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 *tag = yml_get_value(node, "tag"); + const struct yml_node *values = yml_get_value(node, "values"); + const struct yml_node *def = yml_get_value(node, "default"); + + struct particle_map particle_map[yml_dict_length(values)]; + + 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, parent_font); + } + + struct particle *default_particle = def != NULL + ? conf_to_particle(def, parent_font) + : NULL; + + return map_new( + yml_value_as_string(tag), particle_map, yml_dict_length(values), + default_particle, left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_map = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 3, + .attrs = { + {"tag", true, &conf_verify_string}, + {"values", true, &verify_map_values}, + {"default", false, &conf_verify_particle}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/map.h b/particles/map.h index 77dcee2..c9ccb13 100644 --- a/particles/map.h +++ b/particles/map.h @@ -1,12 +1,4 @@ #pragma once #include "../particle.h" -struct particle_map { - const char *tag_value; - struct particle *particle; -}; - -struct particle *particle_map_new( - const char *tag, const struct particle_map *particle_map, size_t count, - struct particle *default_particle, int left_margin, int right_margin, - const char *on_click_template); +extern const struct particle_info particle_map; From 21e28315e3476229af9e4482857cfdb9f4228804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:38:25 +0100 Subject: [PATCH 08/15] particle/progress-bar: expose info through the new struct particle_info struct --- config-verify.c | 15 ++--------- config.c | 31 +++------------------- particle.h | 3 ++- particles/progress-bar.c | 56 ++++++++++++++++++++++++++++++++++------ particles/progress-bar.h | 6 +---- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/config-verify.c b/config-verify.c index 1a99d9f..4074f35 100644 --- a/config-verify.c +++ b/config-verify.c @@ -12,6 +12,7 @@ #include "particles/empty.h" #include "particles/list.h" #include "particles/map.h" +#include "particles/progress-bar.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -290,18 +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 progress_bar[] = { - {"tag", true, &conf_verify_string}, - {"length", true, &conf_verify_int}, - /* TODO: make these optional? Default to empty */ - {"start", true, &conf_verify_particle}, - {"end", true, &conf_verify_particle}, - {"fill", true, &conf_verify_particle}, - {"empty", true, &conf_verify_particle}, - {"indicator", true, &conf_verify_particle}, - COMMON_ATTRS - }; - static const struct attr_info ramp[] = { {"tag", true, &conf_verify_string}, {"items", true, &conf_verify_particle_list_items}, @@ -326,6 +315,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"empty", &particle_empty}, {"list", &particle_list}, {"map", &particle_map}, + {"progress-bar", &particle_progress_bar}, }; static const struct { @@ -333,7 +323,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, {"string", string, sizeof(string) / sizeof(string[0])}, }; diff --git a/config.c b/config.c index c2845fc..bee4a2f 100644 --- a/config.c +++ b/config.c @@ -186,31 +186,6 @@ particle_ramp_from_config(const struct yml_node *node, on_click_template); } -static struct particle * -particle_progress_bar_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 *tag = yml_get_value(node, "tag"); - const struct yml_node *length = yml_get_value(node, "length"); - const struct yml_node *start = yml_get_value(node, "start"); - const struct yml_node *end = yml_get_value(node, "end"); - const struct yml_node *fill = yml_get_value(node, "fill"); - const struct yml_node *empty = yml_get_value(node, "empty"); - const struct yml_node *indicator = yml_get_value(node, "indicator"); - - return particle_progress_bar_new( - yml_value_as_string(tag), - yml_value_as_int(length), - conf_to_particle(start, parent_font), - conf_to_particle(end, parent_font), - conf_to_particle(fill, parent_font), - conf_to_particle(empty, parent_font), - conf_to_particle(indicator, parent_font), - 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) @@ -261,6 +236,9 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "map") == 0) ret = particle_map.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "progress-bar") == 0) + ret = particle_progress_bar.from_conf( + pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "string") == 0) ret = particle_string_from_config( @@ -268,9 +246,6 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "ramp") == 0) ret = particle_ramp_from_config( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "progress-bar") == 0) - ret = particle_progress_bar_from_config( - pair.value, parent_font, left, right, on_click_template); else assert(false); diff --git a/particle.h b/particle.h index 6bf1621..5b8644d 100644 --- a/particle.h +++ b/particle.h @@ -27,7 +27,8 @@ struct particle_info { {"margin", false, &conf_verify_int}, \ {"left-margin", false, &conf_verify_int}, \ {"right-margin", false, &conf_verify_int}, \ - {"on-click", false, &conf_verify_string} + {"on-click", false, &conf_verify_string}, \ + {NULL, false, NULL} }; diff --git a/particles/progress-bar.c b/particles/progress-bar.c index 88b4f2a..d90a8a6 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -7,6 +7,7 @@ #define LOG_MODULE "progress_bar" #define LOG_ENABLE_DBG 0 #include "../log.h" +#include "../config.h" struct private { char *tag; @@ -206,14 +207,14 @@ instantiate(const struct particle *particle, const struct tag_set *tags) return exposable; } -struct particle * -particle_progress_bar_new(const char *tag, int width, - struct particle *start_marker, - struct particle *end_marker, - struct particle *fill, struct particle *empty, - struct particle *indicator, - int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +progress_bar_new(const char *tag, int width, + struct particle *start_marker, + struct particle *end_marker, + struct particle *fill, struct particle *empty, + struct particle *indicator, + int left_margin, int right_margin, + const char *on_click_template) { struct private *priv = malloc(sizeof(*priv)); priv->tag = strdup(tag); @@ -232,3 +233,42 @@ particle_progress_bar_new(const char *tag, int width, 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 *tag = yml_get_value(node, "tag"); + const struct yml_node *length = yml_get_value(node, "length"); + const struct yml_node *start = yml_get_value(node, "start"); + const struct yml_node *end = yml_get_value(node, "end"); + const struct yml_node *fill = yml_get_value(node, "fill"); + const struct yml_node *empty = yml_get_value(node, "empty"); + const struct yml_node *indicator = yml_get_value(node, "indicator"); + + return progress_bar_new( + yml_value_as_string(tag), + yml_value_as_int(length), + conf_to_particle(start, parent_font), + conf_to_particle(end, parent_font), + conf_to_particle(fill, parent_font), + conf_to_particle(empty, parent_font), + conf_to_particle(indicator, parent_font), + left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_progress_bar = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 7, + .attrs = { + {"tag", true, &conf_verify_string}, + {"length", true, &conf_verify_int}, + /* TODO: make these optional? Default to empty */ + {"start", true, &conf_verify_particle}, + {"end", true, &conf_verify_particle}, + {"fill", true, &conf_verify_particle}, + {"empty", true, &conf_verify_particle}, + {"indicator", true, &conf_verify_particle}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/progress-bar.h b/particles/progress-bar.h index a9f94a5..4095cef 100644 --- a/particles/progress-bar.h +++ b/particles/progress-bar.h @@ -1,8 +1,4 @@ #pragma once #include "../particle.h" -struct particle * particle_progress_bar_new( - const char *tag, int width, - struct particle *start_marker, struct particle *end_marker, - struct particle *fill, struct particle *empty, struct particle *indicator, - int left_margin, int right_margin, const char *on_click_template); +extern const struct particle_info particle_progress_bar; From 9f8000b0479c46b6a668e86027de56303b9458db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:40:47 +0100 Subject: [PATCH 09/15] particle/ramp: expose info through the new struct particle_info struct --- config-verify.c | 9 ++------- config.c | 31 +++---------------------------- particles/ramp.c | 42 ++++++++++++++++++++++++++++++++++++++---- particles/ramp.h | 4 +--- 4 files changed, 44 insertions(+), 42 deletions(-) diff --git a/config-verify.c b/config-verify.c index 4074f35..73d1b47 100644 --- a/config-verify.c +++ b/config-verify.c @@ -13,6 +13,7 @@ #include "particles/list.h" #include "particles/map.h" #include "particles/progress-bar.h" +#include "particles/ramp.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -291,12 +292,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 ramp[] = { - {"tag", true, &conf_verify_string}, - {"items", true, &conf_verify_particle_list_items}, - COMMON_ATTRS - }; - static const struct attr_info string[] = { {"text", true, &conf_verify_string}, {"max", false, &conf_verify_int}, @@ -316,6 +311,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"list", &particle_list}, {"map", &particle_map}, {"progress-bar", &particle_progress_bar}, + {"ramp", &particle_ramp}, }; static const struct { @@ -323,7 +319,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, {"string", string, sizeof(string) / sizeof(string[0])}, }; diff --git a/config.c b/config.c index bee4a2f..cb9f5c6 100644 --- a/config.c +++ b/config.c @@ -161,31 +161,6 @@ particle_string_from_config(const struct yml_node *node, } -static struct particle * -particle_ramp_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 *tag = yml_get_value(node, "tag"); - const struct yml_node *items = yml_get_value(node, "items"); - - size_t count = yml_list_length(items); - struct particle *parts[count]; - - size_t idx = 0; - for (struct yml_list_iter it = yml_list_iter(items); - it.node != NULL; - yml_list_next(&it), idx++) - { - parts[idx] = conf_to_particle(it.node, parent_font); - } - - return particle_ramp_new( - yml_value_as_string(tag), parts, count, 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) @@ -239,13 +214,13 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "progress-bar") == 0) ret = particle_progress_bar.from_conf( pair.value, parent_font, left, right, on_click_template); + 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( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "ramp") == 0) - ret = particle_ramp_from_config( - pair.value, parent_font, left, right, on_click_template); else assert(false); diff --git a/particles/ramp.c b/particles/ramp.c index 827cecc..60f6b11 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -6,6 +6,8 @@ #include +#include "../config.h" + struct private { char *tag; struct particle **particles; @@ -132,10 +134,9 @@ instantiate(const struct particle *particle, const struct tag_set *tags) return exposable; } -struct particle * -particle_ramp_new(const char *tag, struct particle *particles[], size_t count, - int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +ramp_new(const char *tag, struct particle *particles[], size_t count, + int left_margin, int right_margin, const char *on_click_template) { struct particle *particle = particle_common_new( left_margin, right_margin, on_click_template); @@ -153,3 +154,36 @@ particle_ramp_new(const char *tag, struct particle *particles[], size_t count, particle->private = priv; 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 *tag = yml_get_value(node, "tag"); + const struct yml_node *items = yml_get_value(node, "items"); + + size_t count = yml_list_length(items); + struct particle *parts[count]; + + size_t idx = 0; + for (struct yml_list_iter it = yml_list_iter(items); + it.node != NULL; + yml_list_next(&it), idx++) + { + parts[idx] = conf_to_particle(it.node, parent_font); + } + + return ramp_new( + yml_value_as_string(tag), parts, count, left_margin, right_margin, + on_click_template); +} + +const struct particle_info particle_ramp = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 2, + .attrs = { + {"tag", true, &conf_verify_string}, + {"items", true, &conf_verify_particle_list_items}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/ramp.h b/particles/ramp.h index 0deb20b..6605fd3 100644 --- a/particles/ramp.h +++ b/particles/ramp.h @@ -1,6 +1,4 @@ #pragma once #include "../particle.h" -struct particle *particle_ramp_new( - const char *tag, struct particle *particles[], size_t count, - int left_margin, int right_margin, const char *on_click_template); +extern const struct particle_info particle_ramp; From 7b98ea2b7cd360e17a1c28eb14e2c6162f300f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:49:10 +0100 Subject: [PATCH 10/15] particle/string: expose info through the new struct particle_info struct --- config-verify.c | 20 +++++------------- config-verify.h | 2 ++ config.c | 52 +++++++++++++--------------------------------- config.h | 3 +++ particles/string.c | 41 ++++++++++++++++++++++++++++++++---- particles/string.h | 4 +--- 6 files changed, 62 insertions(+), 60 deletions(-) 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; From 47018104da1982b63fd8881eab5796df42289979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 11:13:47 +0100 Subject: [PATCH 11/15] particles: compile as shared libraries (plugins) --- CMakeLists.txt | 15 ++------ config-verify.c | 74 ++++++---------------------------------- config.c | 48 +++++++++++--------------- modules/CMakeLists.txt | 6 ++++ particles/CMakeLists.txt | 24 +++++++++++++ particles/empty.c | 2 +- particles/list.c | 2 +- particles/map.c | 2 +- particles/progress-bar.c | 2 +- particles/ramp.c | 2 +- particles/string.c | 2 +- plugin.c | 28 +++++---------- plugin.h | 12 +++++++ 13 files changed, 89 insertions(+), 130 deletions(-) create mode 100644 particles/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b9ed27..9bf63d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,12 +40,6 @@ add_executable(f00bar decorations/underline.c decorations/underline.h particles/dynlist.c particles/dynlist.h - particles/empty.c particles/empty.h - particles/list.c particles/list.h - particles/map.c particles/map.h - particles/progress-bar.c particles/progress-bar.h - particles/ramp.c particles/ramp.h - particles/string.c particles/string.h ) # TODO: directory global @@ -78,15 +72,10 @@ target_link_libraries(f00bar ${YAML_LIBRARIES} ) -add_library(module-sdk INTERFACE) -target_compile_definitions(module-sdk INTERFACE _GNU_SOURCE) -target_compile_options(module-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) -target_include_directories(module-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) -target_link_libraries(module-sdk INTERFACE ${CMAKE_THREAD_LIBS_INIT}) - set_property(TARGET f00bar PROPERTY INSTALL_RPATH \$ORIGIN/../lib/f00bar) -set_property(TARGET f00bar PROPERTY BUILD_RPATH \$ORIGIN/modules) +set_property(TARGET f00bar PROPERTY BUILD_RPATH "\$ORIGIN/modules;\$ORIGIN/particles") install(TARGETS f00bar DESTINATION bin) +add_subdirectory(particles) add_subdirectory(modules) diff --git a/config-verify.c b/config-verify.c index 64efae4..f592520 100644 --- a/config-verify.c +++ b/config-verify.c @@ -9,13 +9,6 @@ #include "plugin.h" #include "tllist.h" -#include "particles/empty.h" -#include "particles/list.h" -#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) { @@ -285,65 +278,20 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) return false; } -#define COMMON_ATTRS \ - {"margin", false, &conf_verify_int}, \ - {"left-margin", false, &conf_verify_int}, \ - {"right-margin", false, &conf_verify_int}, \ - {"on-click", false, &conf_verify_string}, - -#undef COMMON_ATTRS - - static const struct { - const char *name; - const struct particle_info *info; - } particles_v2[] = { - {"empty", &particle_empty}, - {"list", &particle_list}, - {"map", &particle_map}, - {"progress-bar", &particle_progress_bar}, - {"ramp", &particle_ramp}, - {"string", &particle_string}, - }; - - static const struct { - const char *name; - const struct attr_info *attrs; - size_t count; - } particles[] = { - }; - - for (size_t i = 0; i < sizeof(particles_v2) / sizeof(particles_v2[0]); i++) { - if (strcmp(particles_v2[i].name, particle_name) != 0) - continue; - - if (!conf_verify_dict(chain_push(chain, particle_name), values, - particles_v2[i].info->attrs, - particles_v2[i].info->attr_count)) - { - return false; - } - - chain_pop(chain); - return true; + const struct particle_info *info = plugin_load_particle(particle_name); + if (info == NULL) { + LOG_ERR( + "%s: invalid particle name: %s", + conf_err_prefix(chain, particle), particle_name); + return false; } - for (size_t i = 0; i < sizeof(particles) / sizeof(particles[0]); i++) { - if (strcmp(particles[i].name, particle_name) != 0) - continue; + if (!conf_verify_dict(chain_push(chain, particle_name), values, + info->attrs, info->attr_count)) + return false; - if (!conf_verify_dict(chain_push(chain, particle_name), values, - particles[i].attrs, particles[i].count)) - { - return false; - } - - chain_pop(chain); - return true; - } - - LOG_ERR( - "%s: invalid particle name: %s", conf_err_prefix(chain, particle), particle_name); - return false; + chain_pop(chain); + return true; } bool diff --git a/config.c b/config.c index e6441ef..1ccc026 100644 --- a/config.c +++ b/config.c @@ -5,6 +5,8 @@ #include #include +#include + #include "color.h" #include "decoration.h" @@ -12,14 +14,6 @@ #include "decorations/stack.h" #include "decorations/underline.h" -#include "particle.h" -#include "particles/empty.h" -#include "particles/list.h" -#include "particles/map.h" -#include "particles/progress-bar.h" -#include "particles/ramp.h" -#include "particles/string.h" - #include "module.h" #include "config-verify.h" #include "plugin.h" @@ -153,6 +147,18 @@ particle_simple_list_from_config(const struct yml_node *node, parts[idx] = conf_to_particle(it.node, parent_font); } + /* Lazy-loaded function pointer to particle_list_new() */ + static struct particle *(*particle_list_new)( + struct particle *particles[], size_t count, + int left_spacing, int right_spacing, int left_margin, int right_margin, + const char *on_click_template) = NULL; + + if (particle_list_new == NULL) { + const struct plugin *plug = plugin_load("list", PLUGIN_PARTICLE); + particle_list_new = dlsym(plug->lib, "particle_list_new"); + assert(particle_list_new != NULL); + } + return particle_list_new(parts, count, 0, 2, 0, 0, NULL); } @@ -178,27 +184,11 @@ 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 particle *ret = NULL; - if (strcmp(type, "empty") == 0) - ret = particle_empty.from_conf( - pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "list") == 0) - ret = particle_list.from_conf( - pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "map") == 0) - ret = particle_map.from_conf( - pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "progress-bar") == 0) - ret = particle_progress_bar.from_conf( - pair.value, parent_font, left, right, on_click_template); - 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_conf( - pair.value, parent_font, left, right, on_click_template); - else - assert(false); + const struct particle_info *info = plugin_load_particle(type); + assert(info != NULL); + + struct particle *ret = info->from_conf( + pair.value, parent_font, left, right, on_click_template); const struct yml_node *deco_node = yml_get_value(pair.value, "deco"); diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 9ea7116..f700181 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -1,5 +1,11 @@ cmake_minimum_required(VERSION 3.13) +add_library(module-sdk INTERFACE) +target_compile_definitions(module-sdk INTERFACE _GNU_SOURCE) +target_compile_options(module-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) +target_include_directories(module-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) +target_link_libraries(module-sdk INTERFACE ${CMAKE_THREAD_LIBS_INIT}) + pkg_check_modules(ALSA REQUIRED alsa) add_library(alsa MODULE alsa.c) target_compile_options(alsa PRIVATE ${ALSA_CFLAGS_OTHER}) diff --git a/particles/CMakeLists.txt b/particles/CMakeLists.txt new file mode 100644 index 0000000..3a5f348 --- /dev/null +++ b/particles/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.13) + +add_library(particle-sdk INTERFACE) +target_compile_definitions(particle-sdk INTERFACE _GNU_SOURCE) +target_compile_options(particle-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) +target_include_directories(particle-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) + +add_library(empty MODULE empty.c empty.h) +target_link_libraries(empty particle-sdk) + +add_library(list MODULE list.c list.h) +target_link_libraries(list particle-sdk) + +add_library(map MODULE map.c map.h) +target_link_libraries(map particle-sdk) + +add_library(progress-bar MODULE progress-bar.c progress-bar.h) +target_link_libraries(progress-bar particle-sdk) + +add_library(ramp MODULE ramp.c ramp.h) +target_link_libraries(ramp particle-sdk) + +add_library(string MODULE string.c string.h) +target_link_libraries(string particle-sdk) diff --git a/particles/empty.c b/particles/empty.c index 0ab63a2..184ce78 100644 --- a/particles/empty.c +++ b/particles/empty.c @@ -48,7 +48,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, return empty_new(left_margin, right_margin, on_click_template); } -const struct particle_info particle_empty = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 0, .attrs = { diff --git a/particles/list.c b/particles/list.c index e427c0c..90ba4ac 100644 --- a/particles/list.c +++ b/particles/list.c @@ -198,7 +198,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, on_click_template); } -const struct particle_info particle_list = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 4, .attrs = { diff --git a/particles/map.c b/particles/map.c index fee606b..3e023a1 100644 --- a/particles/map.c +++ b/particles/map.c @@ -228,7 +228,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, default_particle, left_margin, right_margin, on_click_template); } -const struct particle_info particle_map = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 3, .attrs = { diff --git a/particles/progress-bar.c b/particles/progress-bar.c index d90a8a6..68b0d0f 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -257,7 +257,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, left_margin, right_margin, on_click_template); } -const struct particle_info particle_progress_bar = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 7, .attrs = { diff --git a/particles/ramp.c b/particles/ramp.c index 60f6b11..2e51cb7 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -178,7 +178,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, on_click_template); } -const struct particle_info particle_ramp = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 2, .attrs = { diff --git a/particles/string.c b/particles/string.c index 5731692..eef9d6b 100644 --- a/particles/string.c +++ b/particles/string.c @@ -171,7 +171,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font, fg_color, left_margin, right_margin, on_click_template); } -const struct particle_info particle_string = { +const struct particle_info plugin_info = { .from_conf = &from_conf, .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 5, .attrs = { diff --git a/plugin.c b/plugin.c index 0ed13e4..38bc0e2 100644 --- a/plugin.c +++ b/plugin.c @@ -9,16 +9,6 @@ #include "config.h" #include "tllist.h" -enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE }; - -struct plugin { - char *name; - enum plugin_type type; - - void *lib; - const void *sym; -}; - static tll(struct plugin) plugins = tll_init(); static const char * @@ -51,15 +41,14 @@ fini(void) tll_free_and_free(plugins, free_plugin); } -static const void * -_load_plugin(const char *name, enum plugin_type type) +const struct plugin * +plugin_load(const char *name, enum plugin_type type) { - /* Have we already loaded it? */ tll_foreach(plugins, plug) { if (plug->item.type == type && strcmp(plug->item.name, name) == 0) { LOG_DBG("%s: %s already loaded: %p", type2str(type), name, plug->item.lib); assert(plug->item.sym != NULL); - return plug->item.sym; + return &plug->item; } } @@ -80,7 +69,7 @@ _load_plugin(const char *name, enum plugin_type type) /* TODO: rename to plugin_info or so, in both modules and particles */ dlerror(); /* Clear previous error */ - plug->sym = dlsym(lib, type == PLUGIN_MODULE ? "module_info" : "particle_info"); + plug->sym = dlsym(lib, type == PLUGIN_MODULE ? "module_info" : "plugin_info"); const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { @@ -88,18 +77,19 @@ _load_plugin(const char *name, enum plugin_type type) return NULL; } - assert(plug->sym != NULL); - return plug->sym; + return plug; } const struct module_info * plugin_load_module(const char *name) { - return _load_plugin(name, PLUGIN_MODULE); + const struct plugin *plug = plugin_load(name, PLUGIN_MODULE); + return plug != NULL ? plug->sym : NULL; } const struct particle_info * plugin_load_particle(const char *name) { - return _load_plugin(name, PLUGIN_PARTICLE); + const struct plugin *plug = plugin_load(name, PLUGIN_PARTICLE); + return plug != NULL ? plug->sym : NULL; } diff --git a/plugin.h b/plugin.h index 58e4421..aaa84c4 100644 --- a/plugin.h +++ b/plugin.h @@ -5,3 +5,15 @@ const struct module_info *plugin_load_module(const char *name); const struct particle_info *plugin_load_particle(const char *name); + +enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE }; + +struct plugin { + char *name; + enum plugin_type type; + + void *lib; + const void *sym; +}; + +const struct plugin *plugin_load(const char *name, enum plugin_type type); From 311193751c622fed814bb3f60bd85b8cebaa6c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 11:14:57 +0100 Subject: [PATCH 12/15] particles: install target --- particles/CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/particles/CMakeLists.txt b/particles/CMakeLists.txt index 3a5f348..f3a0c5d 100644 --- a/particles/CMakeLists.txt +++ b/particles/CMakeLists.txt @@ -22,3 +22,14 @@ target_link_libraries(ramp particle-sdk) add_library(string MODULE string.c string.h) target_link_libraries(string particle-sdk) + +install( + TARGETS + empty + list + map + progress-bar + ramp + string + + DESTINATION lib/f00bar) From 307a1f5ec841eeebda9a6b08a73606e47132fd49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 11:16:52 +0100 Subject: [PATCH 13/15] particles: all particles can have a decoration --- particle.h | 3 ++- particles/string.c | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/particle.h b/particle.h index 5b8644d..a3fcf8c 100644 --- a/particle.h +++ b/particle.h @@ -22,12 +22,13 @@ struct particle_info { size_t attr_count; /* TODO: reomve, NULL-terminate attr list instead */ const struct attr_info attrs[]; -#define PARTICLE_COMMON_ATTRS_COUNT 4 +#define PARTICLE_COMMON_ATTRS_COUNT 5 #define PARTICLE_COMMON_ATTRS \ {"margin", false, &conf_verify_int}, \ {"left-margin", false, &conf_verify_int}, \ {"right-margin", false, &conf_verify_int}, \ {"on-click", false, &conf_verify_string}, \ + {"deco", false, &conf_verify_decoration}, \ {NULL, false, NULL} }; diff --git a/particles/string.c b/particles/string.c index eef9d6b..d47fb93 100644 --- a/particles/string.c +++ b/particles/string.c @@ -173,13 +173,12 @@ from_conf(const struct yml_node *node, const struct font *parent_font, const struct particle_info plugin_info = { .from_conf = &from_conf, - .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 5, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 4, .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, }, }; From ec4a47e5db37f2ecf0bb64eb76970a868b6264f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 11:18:32 +0100 Subject: [PATCH 14/15] modules: rename module_info -> plugin_info This is the same name used by particles. --- 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 +- plugin.c | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/alsa.c b/modules/alsa.c index 968f777..c8f1562 100644 --- a/modules/alsa.c +++ b/modules/alsa.c @@ -280,7 +280,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) conf_to_particle(content, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 4, .attrs = { diff --git a/modules/backlight.c b/modules/backlight.c index 8c06851..5848640 100644 --- a/modules/backlight.c +++ b/modules/backlight.c @@ -224,7 +224,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) yml_value_as_string(name), conf_to_particle(c, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 3, .attrs = { diff --git a/modules/battery.c b/modules/battery.c index 3c42a16..b4812f0 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -356,7 +356,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) poll_interval != NULL ? yml_value_as_int(poll_interval) : 60); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 4, .attrs = { diff --git a/modules/clock.c b/modules/clock.c index 6a9841e..8834571 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -107,7 +107,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) time_format != NULL ? yml_value_as_string(time_format) : "%H:%M"); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 4, .attrs = { diff --git a/modules/i3.c b/modules/i3.c index 9b6bb2d..924fedb 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -701,7 +701,7 @@ verify_content(keychain_t *chain, const struct yml_node *node) return true; } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 5, .attrs = { diff --git a/modules/label.c b/modules/label.c index ab7b2d6..a0553ac 100644 --- a/modules/label.c +++ b/modules/label.c @@ -53,7 +53,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return label_new(conf_to_particle(c, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 2, .attrs = { diff --git a/modules/mpd.c b/modules/mpd.c index c833e5d..7d72be1 100644 --- a/modules/mpd.c +++ b/modules/mpd.c @@ -491,7 +491,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) conf_to_particle(c, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 4, .attrs = { diff --git a/modules/network.c b/modules/network.c index 598a3eb..c918291 100644 --- a/modules/network.c +++ b/modules/network.c @@ -543,7 +543,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) yml_value_as_string(name), conf_to_particle(content, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 3, .attrs = { diff --git a/modules/removables.c b/modules/removables.c index caa6100..a76e001 100644 --- a/modules/removables.c +++ b/modules/removables.c @@ -577,7 +577,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) conf_to_particle(content, parent_font), left, right); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 5, .attrs = { diff --git a/modules/xkb.c b/modules/xkb.c index 3b1d2d7..2e20aa0 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -459,7 +459,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return xkb_new(conf_to_particle(c, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 2, .attrs = { diff --git a/modules/xwindow.c b/modules/xwindow.c index 167d1fa..7d95023 100644 --- a/modules/xwindow.c +++ b/modules/xwindow.c @@ -321,7 +321,7 @@ from_conf(const struct yml_node *node, const struct font *parent_font) return xwindow_new(conf_to_particle(c, parent_font)); } -const struct module_info module_info = { +const struct module_info plugin_info = { .from_conf = &from_conf, .attr_count = 2, .attrs = { diff --git a/plugin.c b/plugin.c index 38bc0e2..3e665de 100644 --- a/plugin.c +++ b/plugin.c @@ -69,7 +69,7 @@ plugin_load(const char *name, enum plugin_type type) /* TODO: rename to plugin_info or so, in both modules and particles */ dlerror(); /* Clear previous error */ - plug->sym = dlsym(lib, type == PLUGIN_MODULE ? "module_info" : "plugin_info"); + plug->sym = dlsym(lib, "plugin_info"); const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { From 45280416ff23b6efe09356e9e26d420d52eaad43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 11:26:31 +0100 Subject: [PATCH 15/15] modules, particles: type-specific filename prefix Instead of naming the shared libraries libfoo.so, add a type-specific prefix: module_foo.so, or particle_foo.so --- modules/CMakeLists.txt | 2 ++ particles/CMakeLists.txt | 2 ++ plugin.c | 4 +++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index f700181..189459e 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -6,6 +6,8 @@ target_compile_options(module-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) target_include_directories(module-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) target_link_libraries(module-sdk INTERFACE ${CMAKE_THREAD_LIBS_INIT}) +set(CMAKE_SHARED_MODULE_PREFIX module_) + pkg_check_modules(ALSA REQUIRED alsa) add_library(alsa MODULE alsa.c) target_compile_options(alsa PRIVATE ${ALSA_CFLAGS_OTHER}) diff --git a/particles/CMakeLists.txt b/particles/CMakeLists.txt index f3a0c5d..1c41e95 100644 --- a/particles/CMakeLists.txt +++ b/particles/CMakeLists.txt @@ -5,6 +5,8 @@ target_compile_definitions(particle-sdk INTERFACE _GNU_SOURCE) target_compile_options(particle-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) target_include_directories(particle-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) +set(CMAKE_SHARED_MODULE_PREFIX particle_) + add_library(empty MODULE empty.c empty.h) target_link_libraries(empty particle-sdk) diff --git a/plugin.c b/plugin.c index 3e665de..31ce260 100644 --- a/plugin.c +++ b/plugin.c @@ -53,7 +53,9 @@ plugin_load(const char *name, enum plugin_type type) } char path[128]; - snprintf(path, sizeof(path), "lib%s.so", name); + snprintf( + path, sizeof(path), "%s_%s.so", + type == PLUGIN_MODULE ? "module" : "particle", name); /* Not loaded - do it now */ void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW);