From 0cff8325ded7d6235e84496418e6a594613b230b Mon Sep 17 00:00:00 2001 From: 0scar Date: Sun, 18 Feb 2024 11:40:21 +0100 Subject: More reorganization --- src/ctrl/src/input.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'src/ctrl') 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 +#include + #include #include #include #include -#include 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]); + } +} -- cgit v1.3