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 6597b39..f592520 100644 --- a/config-verify.c +++ b/config-verify.c @@ -158,8 +158,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) { @@ -172,15 +170,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)); @@ -243,8 +241,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)); @@ -259,35 +257,6 @@ verify_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) { @@ -309,90 +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}, - - static const struct attr_info empty[] = { - COMMON_ATTRS - }; - - 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}, - {"default", false, &conf_verify_particle}, - COMMON_ATTRS - }; - - 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, &verify_list_items}, - COMMON_ATTRS - }; - - 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 { - 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])}, - {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, - {"string", string, sizeof(string) / sizeof(string[0])}, - }; - - 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, - particles[i].attrs, particles[i].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; } - LOG_ERR( - "%s: invalid particle name: %s", conf_err_prefix(chain, particle), particle_name); - return false; + if (!conf_verify_dict(chain_push(chain, particle_name), values, + info->attrs, info->attr_count)) + return false; + + chain_pop(chain); + return true; } bool @@ -401,7 +300,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..5bdc37c 100644 --- a/config-verify.h +++ b/config-verify.h @@ -42,3 +42,6 @@ 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); + +bool conf_verify_decoration(keychain_t *chain, const struct yml_node *node); diff --git a/config.c b/config.c index e18660c..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" @@ -47,10 +41,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 +69,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 +80,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 +88,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,150 +132,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, - 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_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, - 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, - 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_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) @@ -296,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); } @@ -321,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_config( - 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); - 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); + 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"); @@ -371,7 +218,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 @@ -410,7 +257,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 */ @@ -419,7 +266,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/modules/CMakeLists.txt b/modules/CMakeLists.txt index 9ea7116..189459e 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -1,5 +1,13 @@ 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}) + +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/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/particle.h b/particle.h index c12d7fe..a3fcf8c 100644 --- a/particle.h +++ b/particle.h @@ -3,13 +3,36 @@ #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, + 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 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} + +}; + struct particle { void *private; diff --git a/particles/CMakeLists.txt b/particles/CMakeLists.txt new file mode 100644 index 0000000..1c41e95 --- /dev/null +++ b/particles/CMakeLists.txt @@ -0,0 +1,37 @@ +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}) + +set(CMAKE_SHARED_MODULE_PREFIX particle_) + +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) + +install( + TARGETS + empty + list + map + progress-bar + ramp + string + + DESTINATION lib/f00bar) diff --git a/particles/empty.c b/particles/empty.c index ead1dfc..184ce78 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 plugin_info = { + .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; diff --git a/particles/list.c b/particles/list.c index 9a9a5e3..90ba4ac 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 plugin_info = { + .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; diff --git a/particles/map.c b/particles/map.c index 5094fb2..3e023a1 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 plugin_info = { + .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; diff --git a/particles/progress-bar.c b/particles/progress-bar.c index 88b4f2a..68b0d0f 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 plugin_info = { + .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; diff --git a/particles/ramp.c b/particles/ramp.c index 827cecc..2e51cb7 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 plugin_info = { + .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; diff --git a/particles/string.c b/particles/string.c index 303e50a..d47fb93 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,34 @@ 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 plugin_info = { + .from_conf = &from_conf, + .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}, + 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; diff --git a/plugin.c b/plugin.c index 7f59e55..31ce260 100644 --- a/plugin.c +++ b/plugin.c @@ -9,14 +9,19 @@ #include "config.h" #include "tllist.h" -struct plugin { - char *name; - void *lib; - const void *sym; -}; - 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) { @@ -25,7 +30,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); } @@ -36,42 +41,57 @@ fini(void) tll_free_and_free(plugins, free_plugin); } -const struct module_info * -plugin_load_module(const char *name) +const struct plugin * +plugin_load(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) { - LOG_DBG("%s already loaded: %p", name, plug->item.lib); + 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; } } + char path[128]; + 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); - 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; } - 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, "plugin_info"); 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; } - assert(plug->sym != NULL); - return plug->sym; + return plug; +} + +const struct module_info * +plugin_load_module(const char *name) +{ + 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) +{ + const struct plugin *plug = plugin_load(name, PLUGIN_PARTICLE); + return plug != NULL ? plug->sym : NULL; } diff --git a/plugin.h b/plugin.h index 6d829e2..aaa84c4 100644 --- a/plugin.h +++ b/plugin.h @@ -1,5 +1,19 @@ #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); + +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);