diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-24 23:00:33 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-24 23:00:33 +0100 |
| commit | 7ed39aca6d07b8ca69c0373f5623ffd6a5564412 (patch) | |
| tree | b640cf38ade176b6c1ad22608fed87dc83b18c63 | |
| parent | b5c06f76b9a7cbc64590ec247778704459efb3ea (diff) | |
Add other actions to config
| -rw-r--r-- | comet.conf | 5 | ||||
| -rw-r--r-- | src/action.c | 45 | ||||
| -rw-r--r-- | src/action.h | 7 | ||||
| -rw-r--r-- | src/block.h | 2 | ||||
| -rw-r--r-- | src/config.c | 72 | ||||
| -rw-r--r-- | src/event.c | 8 | ||||
| -rw-r--r-- | src/event.h | 2 |
7 files changed, 86 insertions, 55 deletions
@@ -112,7 +112,10 @@ knob-y-offset = 0 knob-rotation = 90 - action = just + ;action-left-click = just + ;on-left-click = just + ;when-left-click = just + left-click = just [action.just] ;block.slide.color = #18baf2 diff --git a/src/action.c b/src/action.c index 590eac8..1ea7d7f 100644 --- a/src/action.c +++ b/src/action.c @@ -8,7 +8,7 @@ #include "block.h" #include "any_log.h" -void action_perform(action_t *action, block_t *block, config_t *config) +bool action_perform(action_t *action, block_t *block, config_t *config) { block_t *target = block; bool warned = false; @@ -38,10 +38,8 @@ void action_perform(action_t *action, block_t *block, config_t *config) assert(target->scheme->validate_fn != NULL); int errors = target->scheme->validate_fn(target, config); - if (config->action_failfast && errors) { - log_debug("Aborted action '%s' for %d errors", action->label, errors); - return; - } + if (config->action_failfast && errors) + goto error; } notify = false; @@ -60,24 +58,21 @@ void action_perform(action_t *action, block_t *block, config_t *config) // Skip "set-" in key bool success = block_change(target, config, key + 4, value); - if (config->action_failfast && !success) { - log_debug("Aborted action '%s'", action->label); - return; - } + if (config->action_failfast && !success) + goto error; notify = true; break; } - case ACTION_RUN_SCRIPT: { + case ACTION_RUN_SYNC: { // TODO: Pass information about the action/block to the script - // Also allow sync/async scripts pid_t pid = fork(); if (pid == -1) { if (config->action_failfast) { - log_debug("Aborted action '%s'", action->label); - return; + log_trace("Failed to fork"); + goto error; } } else if (pid == 0) { execl("/bin/sh", "sh", "-c", value, (char *)NULL); @@ -88,15 +83,18 @@ void action_perform(action_t *action, block_t *block, config_t *config) bool success = !WIFEXITED(status) || WEXITSTATUS(status) != 0; if (config->action_strict_run && !success) { - if (config->action_failfast) { - log_debug("Aborted action '%s'", action->label); - return; - } + if (config->action_failfast) + goto error; } } break; } + case ACTION_RUN_ASYNC: { + log_panic("Async not implemented"); + break; + } + default: unreachable(); } @@ -110,15 +108,18 @@ void action_perform(action_t *action, block_t *block, config_t *config) if (notify && target->scheme->validate_change) { assert(target->scheme->validate_fn != NULL); - int errors = target->scheme->validate_fn(target, config); - if (config->action_failfast && errors) { - log_debug("Aborted action '%s' for %d errors", action->label, errors); - return; - } + int errors = target->scheme->validate_fn(target, config); + if (config->action_failfast && errors != 0) + goto error; } log_debug("Performed action '%s'", action->label); + return true; + +error: + log_debug("Aborted action '%s'", action->label); + return false; } int action_validate(action_t *action, config_t *config) diff --git a/src/action.h b/src/action.h index d1ca4b3..712bf81 100644 --- a/src/action.h +++ b/src/action.h @@ -1,12 +1,15 @@ #ifndef COMET_ACTION_H #define COMET_ACTION_H +#include <stdbool.h> + #include "config.h" typedef enum { ACTION_TARGET, ACTION_SET_PAIR, - ACTION_RUN_SCRIPT, + ACTION_RUN_SYNC, + ACTION_RUN_ASYNC, } action_type_t; typedef struct { @@ -23,7 +26,7 @@ struct action { typedef struct action action_t; -void action_perform(action_t *action, block_t *block, config_t *config); +bool action_perform(action_t *action, block_t *block, config_t *config); int action_validate(action_t *action, config_t *config); diff --git a/src/block.h b/src/block.h index b240343..be62d20 100644 --- a/src/block.h +++ b/src/block.h @@ -63,7 +63,7 @@ struct block { unsigned int line_width; unsigned int x_padding, y_padding; unsigned int min_width, max_width; - action_t *actions; //[EVENT_LENGTH]; + action_t *actions[EVENT_MAX]; }; typedef struct { diff --git a/src/config.c b/src/config.c index ffceba9..3bbce6d 100644 --- a/src/config.c +++ b/src/config.c @@ -36,16 +36,24 @@ const config_entry_t bar_entries[] = { }; const config_entry_t block_entries[] = { - { "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) }, - { "color", CONFIG_GRADIENT, NULL, offsetof(block_t, bg_color) }, - { "line-color", CONFIG_GRADIENT, NULL, offsetof(block_t, line_color) }, - { "line-width", CONFIG_UINT, NULL, offsetof(block_t, line_width) }, - { "x-padding", CONFIG_UINT, NULL, offsetof(block_t, x_padding) }, - { "y-padding", CONFIG_UINT, NULL, offsetof(block_t, y_padding) }, - { "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) }, - { "action", CONFIG_STRING, NULL, offsetof(block_t, actions) }, + { "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) }, + { "color", CONFIG_GRADIENT, NULL, offsetof(block_t, bg_color) }, + { "line-color", CONFIG_GRADIENT, NULL, offsetof(block_t, line_color) }, + { "line-width", CONFIG_UINT, NULL, offsetof(block_t, line_width) }, + { "x-padding", CONFIG_UINT, NULL, offsetof(block_t, x_padding) }, + { "y-padding", CONFIG_UINT, NULL, offsetof(block_t, y_padding) }, + { "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]) }, + { "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]) }, + { "scroll-up", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_SCROLL_UP]) }, + { "scroll-down", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_SCROLL_DOWN]) }, + { "hover-start", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_HOVER_START]) }, + { "hover-move", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_HOVER_MOVE]) }, + { "hover-stop", CONFIG_STRING, NULL, offsetof(block_t, actions[EVENT_HOVER_STOP]) }, { 0 }, }; @@ -277,6 +285,9 @@ static bool config_read_time(const char *value, struct timespec *result) // TODO: Handle quotation marks static bool config_read_list(const char *value, char ***result) { + // TODO: Ensure that this is correct + strfree(*result); + size_t count = 0; for (size_t i = 0; value[i] != '\0'; ++i) count += value[i] == ','; @@ -298,7 +309,8 @@ static bool config_read_list(const char *value, char ***result) if (!config_read_string(token, &list[n++])) { free(copy); - free(list); + list[n] = NULL; + strfree(list); return false; } } @@ -357,8 +369,10 @@ static config_status_t config_read_action(action_t *action, const char *key, con type = ACTION_TARGET; else if (!strncmp(key, "set-", 4)) type = ACTION_SET_PAIR; - else if (!strcmp(key, "run-script")) - type = ACTION_RUN_SCRIPT; + else if (!strcmp(key, "run-sync")) + type = ACTION_RUN_SYNC; + else if (!strcmp(key, "run-async")) + type = ACTION_RUN_ASYNC; else return CONFIG_UNKNOWN; @@ -732,21 +746,31 @@ int config_validate(config_t *config) bool config_resolve_action(config_t *config, block_t *block) { - char *label = (char *)block->actions; + action_t *actions[EVENT_MAX] = { 0 }; - if (label == NULL) return true; + for (size_t i = 0; i < EVENT_MAX; i++) { + char *label = (char *)block->actions[i]; - for (size_t j = 0; j < config->n_actions; j++) { - if (!strcmp(label, config->actions[j].label)) { - log_debug("Block '%s' uses action '%s'", block->label, label); - free(label); - block->actions = &config->actions[j]; - return true; + if (label == NULL) + continue; + + for (size_t j = 0; j < config->n_actions; j++) { + if (!strcmp(label, config->actions[j].label)) { + log_debug("Block '%s' uses action '%s' for %s", block->label, label, event_type_to_string(i)); + + actions[i] = &config->actions[j]; + free(label); + goto next; + } } + + log_error("Action '%s' not found (referenced by '%s')", label, block->label); + return false; +next: } - log_error("Action '%s' not found", label); - return false; + memcpy(block->actions, actions, sizeof(actions)); + return true; } bool config_resolve_children(config_t *config, block_t *block) @@ -794,7 +818,7 @@ bool config_resolve_children(config_t *config, block_t *block) } } - log_error("Block '%s' not found", children[i]); + log_error("Block '%s' not found (referenced by '%s')", children[i], block->label); goto error; next: } diff --git a/src/event.c b/src/event.c index 0e7efe3..45ec809 100644 --- a/src/event.c +++ b/src/event.c @@ -9,7 +9,7 @@ const char *event_type_to_string(event_type_t type) { - const char *types[EVENT_LENGTH] = { + const char *types[EVENT_MAX] = { "trigger", "left_click", "middle_click", @@ -21,7 +21,7 @@ const char *event_type_to_string(event_type_t type) "hover_stop", }; - assert(type >= EVENT_TRIGGER && type < EVENT_LENGTH); + assert(type >= EVENT_TRIGGER && type < EVENT_MAX); return types[type]; } @@ -81,8 +81,8 @@ 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 (event_is_click(event) && layout->block->actions != NULL) { - action_perform(layout->block->actions, layout->block, state->config); + if (layout->block->actions[event.type] != NULL) { + action_perform(layout->block->actions[event.type], layout->block, state->config); } if (event_fn != NULL) { diff --git a/src/event.h b/src/event.h index 3ebc718..2f00c49 100644 --- a/src/event.h +++ b/src/event.h @@ -16,7 +16,7 @@ typedef enum { EVENT_HOVER_START, EVENT_HOVER_MOVE, EVENT_HOVER_STOP, - EVENT_LENGTH, + EVENT_MAX, } event_type_t; typedef struct { |
