diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-27 23:19:38 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-27 23:19:38 +0100 |
| commit | 6ab3b52fbde3ab2ac11e66e4d664f67e108f6aee (patch) | |
| tree | 838f5231fffc6cacdd65762443fd56bbe0f275f6 /src/blocks | |
| parent | 2582279bc1046954702311c300294c5fd9f3ae0c (diff) | |
Rework resolve functions
Diffstat (limited to 'src/blocks')
| -rw-r--r-- | src/blocks/group.c | 57 | ||||
| -rw-r--r-- | src/blocks/slider.c | 64 |
2 files changed, 104 insertions, 17 deletions
diff --git a/src/blocks/group.c b/src/blocks/group.c index eeac132..da9e6d6 100644 --- a/src/blocks/group.c +++ b/src/blocks/group.c @@ -1,3 +1,5 @@ +#include <assert.h> + #include "../block.h" #include "../any_log.h" @@ -12,6 +14,60 @@ static void block_group_clean(block_t *block) free(group->children); } +static bool block_group_resolve(block_t *block, config_t *config) +{ + block_group_t *group = (block_group_t *)block; + assert(block->type == BLOCK_GROUP); + + char **children = (char **)group->children; + group->children = NULL; + group->n_children = 0; + + if (children == NULL) + return true; + + while (children[group->n_children] != NULL) + group->n_children++; + + group->children = malloc(group->n_children * sizeof(block_t *)); + bool result = true; + + for (size_t i = 0; i < group->n_children; i++) { + for (size_t j = 0; j < config->n_blocks; j++) { + if (config->blocks[j] == block) + continue; + + if (!strcmp(children[i], config->blocks[j]->label)) { + if (config->blocks[j]->grouped) { + log_error("Block '%s' can only be in one group", children[i]); + result = false; + goto end; + } + + log_debug("Block '%s' is parent of '%s'", block->label, children[i]); + group->children[i] = config->blocks[j]; + group->children[i]->grouped = true; + + if (!block_resolve(group->children[i], config)) { + result = false; + goto end; + } + + goto next; + } + } + + log_error("Block '%s' not found (referenced by '%s')", children[i], block->label); + result = false; + goto end; +next: + } + +end: + strfreelist(children); + return result; +} + static config_status_t block_group_change(block_t *block, config_t *config, const char *key, const char *value) { if (!strcmp(key, "blocks")) { @@ -30,5 +86,6 @@ const block_scheme_t block_group_scheme = { .init_fn = block_group_init, .clean_fn = block_group_clean, .validate_fn = NULL, + .resolve_fn = block_group_resolve, .change_fn = block_group_change, }; diff --git a/src/blocks/slider.c b/src/blocks/slider.c index 34d6c35..1b5d4c3 100644 --- a/src/blocks/slider.c +++ b/src/blocks/slider.c @@ -34,6 +34,9 @@ typedef struct { gradient_t knob_color; gradient_t knob_line_color; double knob_rotation; + action_t *left_click; + action_t *middle_click; + action_t *right_click; } block_slider_t; static void block_slider_layout(block_t *block, layout_t *layout, layout_info_t info) @@ -242,6 +245,29 @@ static int block_slider_validate(block_t *block, config_t *config) return errors; } +static bool block_slider_resolve(block_t *block, config_t *config) +{ + block_slider_t *slider = (block_slider_t *)block; + + action_t **actions[] = { + &slider->left_click, + &slider->middle_click, + &slider->right_click, + }; + + for (size_t i = 0; i < 3; i++) { + char *label = (char *)*actions[i]; + if (label == NULL) continue; + + if (!config_resolve_action(config, label, actions[i])) + return false; + + free(label); + } + + return true; +} + static config_enum_t knob_shape_enum[] = { { "none", KNOB_NONE }, { "capsule", KNOB_CAPSULE }, @@ -252,23 +278,26 @@ static config_enum_t knob_shape_enum[] = { static const config_entry_t block_slider_entries[] = { // TODO: Ugly names - { "bar-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, bar_color) }, - { "bar-line-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, line_color) }, - { "bar-bg-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, bg_color) }, - { "bar-height", CONFIG_UINT, NULL, offsetof(block_slider_t, height) }, - { "bar-width", CONFIG_UINT, NULL, offsetof(block_slider_t, width) }, - { "bar-line-width", CONFIG_UINT, NULL, offsetof(block_slider_t, line_width) }, - { "bar-value", CONFIG_INT, NULL, offsetof(block_slider_t, value) }, - { "seekable", CONFIG_BOOL, NULL, offsetof(block_slider_t, seekable) }, - { "knob", CONFIG_ENUM, knob_shape_enum, offsetof(block_slider_t, knob) }, - { "knob-height", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_height) }, - { "knob-width", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_width) }, - { "knob-line-width", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_line_width) }, - { "knob-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, knob_color) }, - { "knob-line-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, knob_line_color) }, - { "knob-x-offset", CONFIG_INT, NULL, offsetof(block_slider_t, knob_x_offset) }, - { "knob-y-offset", CONFIG_INT, NULL, offsetof(block_slider_t, knob_y_offset) }, - { "knob-rotation", CONFIG_DOUBLE, NULL, offsetof(block_slider_t, knob_rotation) }, + { "bar-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, bar_color) }, + { "bar-line-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, line_color) }, + { "bar-bg-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, bg_color) }, + { "bar-height", CONFIG_UINT, NULL, offsetof(block_slider_t, height) }, + { "bar-width", CONFIG_UINT, NULL, offsetof(block_slider_t, width) }, + { "bar-line-width", CONFIG_UINT, NULL, offsetof(block_slider_t, line_width) }, + { "bar-value", CONFIG_INT, NULL, offsetof(block_slider_t, value) }, + { "seekable", CONFIG_BOOL, NULL, offsetof(block_slider_t, seekable) }, + { "knob", CONFIG_ENUM, knob_shape_enum, offsetof(block_slider_t, knob) }, + { "knob-height", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_height) }, + { "knob-width", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_width) }, + { "knob-line-width", CONFIG_UINT, NULL, offsetof(block_slider_t, knob_line_width) }, + { "knob-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, knob_color) }, + { "knob-line-color", CONFIG_GRADIENT, NULL, offsetof(block_slider_t, knob_line_color) }, + { "knob-x-offset", CONFIG_INT, NULL, offsetof(block_slider_t, knob_x_offset) }, + { "knob-y-offset", CONFIG_INT, NULL, offsetof(block_slider_t, knob_y_offset) }, + { "knob-rotation", CONFIG_DOUBLE, NULL, offsetof(block_slider_t, knob_rotation) }, + { "left-click-bar", CONFIG_STRING, NULL, offsetof(block_slider_t, left_click) }, + { "middle-click-bar", CONFIG_STRING, NULL, offsetof(block_slider_t, middle_click) }, + { "right-click-bar", CONFIG_STRING, NULL, offsetof(block_slider_t, right_click) }, { 0 }, }; @@ -285,5 +314,6 @@ const block_scheme_t block_slider_scheme = { .init_fn = block_slider_init, .clean_fn = block_slider_clean, .validate_fn = block_slider_validate, + .resolve_fn = block_slider_resolve, .change_fn = block_slider_change, }; |
