diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2024-02-18 10:40:21 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2024-02-18 10:43:41 +0000 |
| commit | 0cff8325ded7d6235e84496418e6a594613b230b (patch) | |
| tree | bd35186759570a00cc829d3a5ad4556cf6fcdfa4 /src/ctrl | |
| parent | d11abae4b6560b6574a84f41d7a34156204cdc5b (diff) | |
More reorganization
Diffstat (limited to 'src/ctrl')
| -rw-r--r-- | src/ctrl/src/input.c | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/src/ctrl/src/input.c b/src/ctrl/src/input.c index 5fe643e..cbfd576 100644 --- a/src/ctrl/src/input.c +++ b/src/ctrl/src/input.c @@ -1,8 +1,10 @@ +#include <errno.h> +#include <string.h> + #include <engine/core/dltools.h> #include <engine/core/logging.h> #include <engine/core/platform.h> #include <engine/ctrl/input.h> -#include <string.h> extern input_callback_t* callbacks[128]; extern usize callbacks_len; @@ -330,3 +332,59 @@ void i_bind_alt(binding_t* b, scancode_t s) { b->scancode_alt = s; } + +/* Pushes an input context onto the input handling stack */ +void i_ctx_push(i_ctx* ctx) { + if (GLOBAL_PLATFORM->bindings == NULL) { + GLOBAL_PLATFORM->bindings = calloc(8, sizeof(i_ctx*)); + GLOBAL_PLATFORM->bindings_sz = 8; + } + + if (GLOBAL_PLATFORM->bindings_len + 1 >= GLOBAL_PLATFORM->bindings_sz) { + void* m = + realloc(GLOBAL_PLATFORM->bindings, GLOBAL_PLATFORM->bindings_sz + 8); + if (m == NULL) { + ERROR("Failed to allocate 8 bytes (%d): %s", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + GLOBAL_PLATFORM->bindings_sz += 8; + } + + LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->bindings_len); + for (isize i = 0; i < ctx->len; i++) { + switch (ctx->bindings[i].action.type) { + case InputType_error: + LOG("(error)"); + break; + + case InputType_action: + LOG("(action) %s", ctx->bindings[i].action.action.callback_str); + break; + + case InputType_state: + LOG("(+state) %s", ctx->bindings[i].action.state.activate_str); + LOG("(-state) %s", ctx->bindings[i].action.state.deactivate_str); + break; + + case InputType_range: + LOG("(range) --unhandled--"); + break; + } + } + + GLOBAL_PLATFORM->bindings[GLOBAL_PLATFORM->bindings_len++] = ctx; +} + +/* Pops an input context from the input stack */ +void i_ctx_pop(void) { + if (GLOBAL_PLATFORM->bindings == NULL || GLOBAL_PLATFORM->bindings_sz == 0) + return; + i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); +} + +/* Removes all input contexts from the input stack */ +void i_ctx_reset(void) { + while (GLOBAL_PLATFORM->bindings_len > 0) { + i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); + } +} |
