From 30020a08392c959505bd27388564f7436a04bace Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 24 Nov 2024 16:43:35 +0100 Subject: Change action syntax --- src/action.c | 56 +++++++++++++++++++++++++++++++++++++++----------------- src/action.h | 11 ++++++----- src/config.c | 34 ++++++++++++++++++++++++++-------- src/event.c | 1 + 4 files changed, 72 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/action.c b/src/action.c index 2ceae9b..81995e6 100644 --- a/src/action.c +++ b/src/action.c @@ -1,5 +1,6 @@ #include #include +#include #include "action.h" #include "block.h" @@ -7,36 +8,57 @@ void action_perform(action_t *action, block_t *block, config_t *config) { + block_t *target = block; + for (size_t i = 0; i < action->length; i++) { const char *key = action->parts[i].key; const char *value = action->parts[i].value; - if (strncmp(key, "block.", 6)) - abort(); + switch (action->parts[i].type) { + case ACTION_TARGET: { + if (strncmp(value, "block.", 6)) + log_panic("Invalid target %s", value); + + if (!strcmp(value + 6, "default")) { + target = block; + } else { + for (size_t i = 0; i < config->n_blocks; i++) { + if (!strcmp(value + 6, config->blocks[i]->label)) { + target = config->blocks[i]; + break; + } + } + } + break; + } - int sep = 6; - while (key[sep] != '.' && key[sep] != '\0') sep++; + case ACTION_SET_PAIR: { + assert(target != NULL); - if (key[sep] == '\0') abort(); + if (target->scheme->change_fn == NULL) { + log_warn("Block '%s' does not support changing values", target->label); + continue; + } - block_t *target = NULL; - for (size_t i = 0; i < config->n_blocks; i++) { - size_t length = strlen(config->blocks[i]->label); - if (length == (sep - 6) && !strncmp(key + 6, config->blocks[i]->label, length)) { - target = config->blocks[i]; + // Skip "set-" + block_change(target, key + 4, value); break; } - } - if (target == NULL) - abort(); + case ACTION_RUN_SCRIPT: { + log_panic("TODO"); + break; + } - if (target->scheme->change_fn == NULL) { - log_warn("Block '%s' does not support changing values", target->label); - continue; + default: + unreachable(); } - block_change(target, key, value); + + log_value_trace("Performed action step", + "i:type", action->parts[i].type, + "s:key", key, + "s:value", value); } log_debug("Performed action '%s'", action->label); diff --git a/src/action.h b/src/action.h index 02ca1b8..d1ca4b3 100644 --- a/src/action.h +++ b/src/action.h @@ -3,13 +3,14 @@ #include "config.h" -//typedef enum { -// ACTION_SET, -// ACTION_RUN, -//} action_type_t; +typedef enum { + ACTION_TARGET, + ACTION_SET_PAIR, + ACTION_RUN_SCRIPT, +} action_type_t; typedef struct { - //action_type_t type; + action_type_t type; char *key; char *value; } action_part_t; diff --git a/src/config.c b/src/config.c index 619d95c..f76065c 100644 --- a/src/config.c +++ b/src/config.c @@ -432,14 +432,25 @@ static block_t *config_alloc_block(config_t *config, const block_scheme_t *schem return block; } -static config_status_t config_read_action(action_t *action, const char **type, - const char *section, const char *key, const char *value) +static config_status_t config_read_action(action_t *action, const char *section, const char *key, const char *value) { + action_type_t type; + + if (!strcmp(key, "target")) + type = ACTION_TARGET; + else if (!strncmp(key, "set-", 4)) + type = ACTION_SET_PAIR; + else if (!strcmp(key, "run-script")) + type = ACTION_RUN_SCRIPT; + else + return CONFIG_UNKNOWN; + action->parts = realloc(action->parts, ++action->length * sizeof(action_part_t)); assert(action->parts != NULL); + action->parts[action->length - 1].type = type; action->parts[action->length - 1].key = strcopy(key); - action->parts[action->length - 1].value = strcopy(key); + action->parts[action->length - 1].value = strcopy(value); return CONFIG_SUCCESS; } @@ -484,10 +495,16 @@ int config_read(config_t *config, FILE *file) if (section != NULL) { if (!strncmp(section, "block.", 6)) { char *label = strcopy(section + 6); - if (label == NULL || *label == '\0' || !strcmp(label, "bar")) { + if (label == NULL || *label == '\0') { + ++errors; + log_value_error("Block section must have a non-empty label", + "s:section", section, + "i:line", ini.line); + } else if (!strcmp(label, "bar") || !strcmp(label, "default")) { ++errors; - log_value_error("Block section must have a valid label", + log_value_error("Block section must have a non-reserved label", "s:section", section, + "s:label", label, "i:line", ini.line); } else { for (size_t i = 0; i < config->n_blocks; i++) { @@ -495,6 +512,7 @@ int config_read(config_t *config, FILE *file) ++errors; log_value_error("Block section must have a unique label", "s:section", section, + "s:label", label, "i:line", ini.line); } } @@ -539,7 +557,7 @@ int config_read(config_t *config, FILE *file) if (label == NULL || *label == '\0') { ++errors; - log_value_error("Action section must have a valid label", + log_value_error("Action section must have a non-empty label", "s:section", section, "i:line", ini.line); } else { @@ -579,7 +597,7 @@ int config_read(config_t *config, FILE *file) if (!bar_section && block == NULL && action == NULL) goto skip_pair; - const char *type; + const char *type = "none"; config_status_t status; if (bar_section) { @@ -595,7 +613,7 @@ int config_read(config_t *config, FILE *file) } if (action != NULL) { - status = config_read_action(action, &type, section, key, value); + status = config_read_action(action, section, key, value); } switch (status) { diff --git a/src/event.c b/src/event.c index c226bea..5e8d7d5 100644 --- a/src/event.c +++ b/src/event.c @@ -250,6 +250,7 @@ void event_dispatch(event_state_t *state, window_t *window) .y = button->event_y, }; + assert(event_is_click(event) || event_is_scroll(event)); event_dispatch_mouse(state, state->layout, event); break; } -- cgit v1.2.3