diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-28 01:09:40 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-28 01:09:40 +0100 |
| commit | 737167955e8347ce06718e7a8324c9d2ac89dfba (patch) | |
| tree | d187ac026739c01eb5df22a3954b95eb7bc63a7b /src | |
| parent | 6ab3b52fbde3ab2ac11e66e4d664f67e108f6aee (diff) | |
Refactor actions
Diffstat (limited to 'src')
| -rw-r--r-- | src/action.c | 7 | ||||
| -rw-r--r-- | src/action.h | 2 | ||||
| -rw-r--r-- | src/block.c | 27 | ||||
| -rw-r--r-- | src/block.h | 4 | ||||
| -rw-r--r-- | src/blocks/slider.c | 92 | ||||
| -rw-r--r-- | src/config.c | 9 | ||||
| -rw-r--r-- | src/event.c | 7 | ||||
| -rw-r--r-- | src/util.c | 2 | ||||
| -rw-r--r-- | src/util.h | 2 |
9 files changed, 97 insertions, 55 deletions
diff --git a/src/action.c b/src/action.c index 3a358c3..e5293e4 100644 --- a/src/action.c +++ b/src/action.c @@ -10,6 +10,9 @@ bool action_perform(action_t *action, block_t *block, config_t *config) { + if (action == NULL) + return true; + block_t *target = block; bool warned = false; bool notify = false; @@ -134,8 +137,10 @@ bool action_resolve(action_t *action, config_t *config) return true; } -void action_free(action_t *action) +void action_clean(action_t *action) { + assert(action != NULL); + for (size_t i = 0; i < action->length; i++) { free(action->parts[i].key); free(action->parts[i].value); diff --git a/src/action.h b/src/action.h index 795a79c..eb144a5 100644 --- a/src/action.h +++ b/src/action.h @@ -34,6 +34,6 @@ int action_validate(action_t *action, config_t *config); bool action_resolve(action_t *action, config_t *config); -void action_free(action_t *action); +void action_clean(action_t *action); #endif diff --git a/src/block.c b/src/block.c index 04ad902..c397b5a 100644 --- a/src/block.c +++ b/src/block.c @@ -51,6 +51,22 @@ void block_update(block_t *block) } } +bool block_resolve_action(block_t *block, config_t *config, action_t **action) +{ + if (action == NULL || *action == NULL) + return true; + + char *label = (char *)*action; + + bool result = config_resolve_action(config, label, action); + if (!result) { + log_error("Action '%s' not found (referenced by block '%s')", label, block->label); + } + + free(label); + return result; +} + bool block_resolve(block_t *block, config_t *config) { if (block->resolved) @@ -59,13 +75,8 @@ bool block_resolve(block_t *block, config_t *config) block->resolved = true; for (size_t i = 0; i < EVENT_MAX; i++) { - char *action = (char *)block->actions[i]; - if (action == NULL) continue; - - if (!config_resolve_action(config, action, &block->actions[i])) + if (!block_resolve_action(block, config, &block->actions[i])) return false; - - free(action); } return block->scheme->resolve_fn == NULL @@ -92,8 +103,8 @@ void block_free(block_t *block) if (block->scheme->clean_fn != NULL) block->scheme->clean_fn(block); - gradient_free(&block->bg_color); - gradient_free(&block->line_color); + gradient_clean(&block->bg_color); + gradient_clean(&block->line_color); free(block->label); free(block); diff --git a/src/block.h b/src/block.h index 940231a..927fd1a 100644 --- a/src/block.h +++ b/src/block.h @@ -44,7 +44,7 @@ typedef void (*block_render_t)(layout_t *layout, cairo_t *cr); // Triggered when an event is directed towards the block // -typedef void (*block_event_t)(layout_t *layout, event_t event); +typedef void (*block_event_t)(layout_t *layout, config_t *config, event_t event); // The block struct // @@ -129,6 +129,8 @@ extern const block_scheme_t *block_schemes[]; void block_update(block_t *block); +bool block_resolve_action(block_t *block, config_t *config, action_t **action); + bool block_resolve(block_t *block, config_t *config); bool block_change(block_t *block, config_t *config, const char *key, const char *value); diff --git a/src/blocks/slider.c b/src/blocks/slider.c index 1b5d4c3..32ec3e6 100644 --- a/src/blocks/slider.c +++ b/src/blocks/slider.c @@ -4,6 +4,7 @@ #include <stdlib.h> #include <math.h> +#include "../action.h" #include "../block.h" #include "../format.h" #include "../any_log.h" @@ -78,7 +79,7 @@ static void block_slider_render(layout_t *layout, cairo_t *cr) pattern = slider->line_color.pattern; if (pattern != NULL) { - int line_radius = radius - slider->line_width / 2; + int line_radius = radius + slider->line_width / 2; render_capsule_fast(cr, bar_x, bar_y, slider->width, radius, line_radius); cairo_pattern_set_matrix(pattern, &matrix); @@ -172,35 +173,65 @@ static void block_slider_render(layout_t *layout, cairo_t *cr) } } -static void block_slider_event(layout_t *layout, event_t event) +static void block_slider_event(layout_t *layout, config_t *config, event_t event) { block_slider_t *slider = (block_slider_t *)layout->block; - int value = slider->value; - if (layout->block->hidden) return; - if (!slider->seekable) return; + int value = slider->value; if (event_is_click(event)) { - int bar_x = layout->x + (layout->width - slider->width) / 2; - int bar_y = layout->y + (layout->height - slider->height) / 2; + int bar_x = layout->x + (layout->width - slider->width) / 2 - slider->line_width; + int bar_y = layout->y + (layout->height - slider->height) / 2 - slider->line_width; + + int bar_width = slider->width + 2 * slider->line_width; + int bar_height = slider->height + 2 * slider->line_width; + + bool clicked = check_capsule(event.x, event.y, bar_x, bar_y, bar_width, bar_height); + + action_t *action = layout->block->actions[event.type]; + switch (event.type) { + case EVENT_LEFT_CLICK: + if (slider->left_click != NULL && clicked) + action = slider->left_click; + break; + + case EVENT_MIDDLE_CLICK: + if (slider->middle_click != NULL && clicked) + action = slider->middle_click; + break; - if (check_capsule(event.x, event.y, bar_x, bar_y, slider->width, slider->height)) { - int value = 100 * (event.x - bar_x) / (double)(slider->width - 1); - slider->value = value; + case EVENT_RIGHT_CLICK: + if (slider->right_click != NULL && clicked) + action = slider->right_click; + break; + + default: + unreachable(); + } + + action_perform(action, layout->block, config); + + if (slider->seekable && clicked) + slider->value = 100 * (event.x - bar_x) / (double)(slider->width - 1); + } else { + action_perform(layout->block->actions[event.type], layout->block, config); + + if (slider->seekable && event_is_scroll(event)) { + int step = event.type == EVENT_SCROLL_DOWN ? -1 : 1; + slider->value += step; + + if (slider->value > 100) slider->value = 100; + if (slider->value < 0) slider->value = 0; } - } else if (event_is_scroll(event)) { - int step = event.type == EVENT_SCROLL_DOWN ? -1 : 1; - slider->value += step; - - if (slider->value > 100) slider->value = 100; - if (slider->value < 0) slider->value = 0; - } else return; - - log_value_debug("Updated slider value", - "s:label", layout->block->label, - "i:old_value", value, - "i:new_value", slider->value); + } + + if (slider->value != value) { + log_value_debug("Updated slider value", + "s:label", layout->block->label, + "i:old_value", value, + "i:new_value", slider->value); + } } static void block_slider_init(block_t *block) @@ -215,11 +246,11 @@ static void block_slider_init(block_t *block) static void block_slider_clean(block_t *block) { block_slider_t *slider = (block_slider_t *)block; - gradient_free(&slider->bar_color); - gradient_free(&slider->line_color); - gradient_free(&slider->bg_color); - gradient_free(&slider->knob_color); - gradient_free(&slider->knob_line_color); + gradient_clean(&slider->bar_color); + gradient_clean(&slider->line_color); + gradient_clean(&slider->bg_color); + gradient_clean(&slider->knob_color); + gradient_clean(&slider->knob_line_color); } static int block_slider_validate(block_t *block, config_t *config) @@ -256,13 +287,8 @@ static bool block_slider_resolve(block_t *block, config_t *config) }; 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])) + if (!block_resolve_action(block, config, actions[i])) return false; - - free(label); } return true; diff --git a/src/config.c b/src/config.c index e7c47d7..ea59761 100644 --- a/src/config.c +++ b/src/config.c @@ -49,7 +49,7 @@ const config_entry_t block_entries[] = { { "min-width", CONFIG_UINT, NULL, offsetof(block_t, min_width) }, { "max-width", CONFIG_UINT, NULL, offsetof(block_t, max_width) }, { "interval", CONFIG_TIME, NULL, offsetof(block_t, update_interval) }, - //{ "trigger", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_TRIGGER]) }, + { "trigger", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_TRIGGER]) }, { "left-click", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_LEFT_CLICK]) }, { "middle-click", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_MIDDLE_CLICK]) }, { "right-click", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_RIGHT_CLICK]) }, @@ -205,7 +205,7 @@ static bool config_read_color(const char *value, color_t *result) static bool config_read_gradient(const char *value, gradient_t *result) { - gradient_free(result); + gradient_clean(result); memset(result, 0, sizeof(gradient_t)); size_t count = 0; @@ -762,7 +762,6 @@ bool config_resolve_action(config_t *config, const char *label, action_t **actio } } - log_error("Action '%s' not found", label); return false; } @@ -798,11 +797,11 @@ void config_free(config_t *config) free(config->blocks); for (size_t i = 0; i < config->n_actions; i++) - action_free(&config->actions[i]); + action_clean(&config->actions[i]); free(config->actions); - gradient_free(&config->background); + gradient_clean(&config->background); free(config->font); free(config->monitor); free(config->wm_name); diff --git a/src/event.c b/src/event.c index 45ec809..8b7d56c 100644 --- a/src/event.c +++ b/src/event.c @@ -81,13 +81,12 @@ static void event_dispatch_callback(event_state_t *state, layout_t *layout, even "b:action", layout->block->actions != NULL, "b:callback", event_fn != NULL); - if (layout->block->actions[event.type] != NULL) { - action_perform(layout->block->actions[event.type], layout->block, state->config); - } if (event_fn != NULL) { - event_fn(layout, event); + event_fn(layout, state->config, event); log_trace("Completed event callback"); + } else if (layout->block->actions[event.type] != NULL) { + action_perform(layout->block->actions[event.type], layout->block, state->config); } } @@ -63,7 +63,7 @@ void gradient_print(FILE *stream, gradient_t *gradient) } } -void gradient_free(gradient_t *gradient) +void gradient_clean(gradient_t *gradient) { free(gradient->colors); if (gradient->pattern != NULL) @@ -46,7 +46,7 @@ char *gradient_to_string(gradient_t *gradient); void gradient_print(FILE *stream, gradient_t *gradient); -void gradient_free(gradient_t *gradient); +void gradient_clean(gradient_t *gradient); const struct timespec timespec_from_ms(long ms); |
