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 --- comet.conf | 7 +++++++ src/action.c | 41 ++++++++++++++++++++++++++++++++++++++--- src/action.h | 1 + src/config.c | 2 ++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/comet.conf b/comet.conf index b3cef7d..fbf0bc8 100644 --- a/comet.conf +++ b/comet.conf @@ -93,6 +93,8 @@ sus = 1 x-padding = 5 y-padding = 5 + trigger = tried + [block.slide] type = slider bar-value = 88 @@ -136,3 +138,8 @@ sus = 1 ;target = block.mus ;set-blocks = bogus + target = block.next + trigger = ciao + +[action.tried] + run-sync = echo ciao 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(); } diff --git a/src/action.h b/src/action.h index eb144a5..946b629 100644 --- a/src/action.h +++ b/src/action.h @@ -10,6 +10,7 @@ typedef enum { ACTION_SET_PAIR, ACTION_RUN_SYNC, ACTION_RUN_ASYNC, + ACTION_TRIGGER, } action_type_t; typedef struct { diff --git a/src/config.c b/src/config.c index 6b8fc58..86d5ac2 100644 --- a/src/config.c +++ b/src/config.c @@ -446,6 +446,8 @@ static config_status_t config_read_action(action_t *action, const char *key, con type = ACTION_RUN_SYNC; else if (!strcmp(key, "run-async")) type = ACTION_RUN_ASYNC; + else if (!strcmp(key, "trigger")) + type = ACTION_TRIGGER; else return CONFIG_UNKNOWN; -- cgit v1.2.3