From fd2ef765de733fbbbb15e28c91df255da36d5338 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sat, 7 Dec 2024 00:41:01 +0100 Subject: Add trigger action --- src/action.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/action.c') diff --git a/src/action.c b/src/action.c index 6b48c15..df6ad3a 100644 --- a/src/action.c +++ b/src/action.c @@ -51,9 +51,11 @@ bool action_perform(action_t *action, block_t *block, config_t *config) case ACTION_SET_PAIR: { assert(target != NULL); - if (!warned && target->scheme->change_fn == NULL) { - log_warn("Block '%s' does not support 'set' actions", target->label); - warned = true; + if (target->scheme->change_fn == NULL) { + if (!warned) { + log_warn("Block '%s' does not support 'set' actions", target->label); + warned = true; + } continue; } @@ -96,6 +98,39 @@ bool action_perform(action_t *action, block_t *block, config_t *config) break; } + case ACTION_TRIGGER: { + event_t event; + event_init_trigger(&event, action->label, value); + + action_t *action = target->actions[EVENT_TRIGGER]; + block_event_t event_fn = target->type == BLOCK_SPEC + ? ((block_spec_t *)target)->event_fn + : NULL; + + log_value_debug("Block received a trigger", + "s:target", target->label, + "s:origin", event.trigger.origin, + "s:payload", event.trigger.payload, + "b:action", action != NULL, + "b:callback", event_fn != NULL); + + if (event_fn != NULL) { + // FIXME + layout_t tmp = { + .block = target, + }; + + event_fn(&tmp, config, event); + log_trace("Completed event callback"); + } else if (action != NULL) { + action_perform(action, target, config); + } else { + log_warn("Block '%s' was triggered but had nothing to respond", target->label); + } + + break; + } + default: unreachable(); } -- cgit v1.2.3