aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2024-04-11 23:54:55 +0200
committerkdx <kikoodx@paranoici.org>2024-04-11 23:54:55 +0200
commita1d6b52259ccc2eb96883a7af39fdda60bcf8462 (patch)
tree079484e710b286043bdff863672ad0af834cf5a8
parentd19711c99ec4279292801edb86a39c24fff35c02 (diff)
downloadinput-a1d6b52259ccc2eb96883a7af39fdda60bcf8462.tar.gz
lock bind and clear action
-rw-r--r--input.c78
-rw-r--r--input.h3
2 files changed, 57 insertions, 24 deletions
diff --git a/input.c b/input.c
index d8a058a..a1914ff 100644
--- a/input.c
+++ b/input.c
@@ -16,6 +16,36 @@
static InputAction _actions = {0};
static InputAxis _axis = {0};
+static InputAction *
+_input_get_action(const char *tag)
+{
+ foreach (e, _actions.next)
+ if (strcmp(e->tag, tag) == 0)
+ return e;
+ plog("action '%s' not found", tag);
+ return NULL;
+}
+
+static InputBind *
+_input_new_bind(const char *tag, InputBindType ibt)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL) {
+ plog("_input_get_action failed");
+ return NULL;
+ }
+
+ InputBind *bind = malloc(sizeof(InputBind));
+ if (bind == NULL)
+ panic("walloc failed");
+ memset(bind, 0, sizeof(*bind));
+
+ bind->type = ibt;
+ bind->next = action->binds.next;
+ action->binds.next = bind;
+ return bind;
+}
+
void
input_deinit(void)
{
@@ -71,43 +101,43 @@ input_new_axis(const char *tag)
_axis.next = axis;
}
-InputAction *
-_input_get_action(const char *tag)
-{
- foreach (e, _actions.next)
- if (strcmp(e->tag, tag) == 0)
- return e;
- plog("action '%s' not found", tag);
- return NULL;
-}
-
-InputBind *
-_input_new_bind(const char *tag, InputBindType ibt)
+void
+input_clear_action(const char *tag)
{
InputAction *action = _input_get_action(tag);
- if (action == NULL) {
- plog("_input_get_action failed");
- return NULL;
+ if (action == NULL)
+ panic("_input_new_bind failed");
+ InputBind *prev = &action->binds;
+ InputBind *bind = action->binds.next;
+ while (bind) {
+ InputBind *queue = bind->next;
+ if (bind->lock) {
+ prev = bind;
+ } else {
+ prev->next = bind->next;
+ free(bind);
+ }
+ bind = queue;
}
+}
- InputBind *bind = malloc(sizeof(InputBind));
+void
+input_bind_action_sc(const char *tag, SDL_Scancode sc)
+{
+ InputBind *bind = _input_new_bind(tag, IBT_SCANCODE);
if (bind == NULL)
- panic("walloc failed");
- memset(bind, 0, sizeof(*bind));
-
- bind->type = ibt;
- bind->next = action->binds.next;
- action->binds.next = bind;
- return bind;
+ panic("_input_new_bind failed");
+ bind->sc = sc;
}
void
-input_bind_action_sc(const char *tag, SDL_Scancode sc)
+input_bind_action_sc_locked(const char *tag, SDL_Scancode sc)
{
InputBind *bind = _input_new_bind(tag, IBT_SCANCODE);
if (bind == NULL)
panic("_input_new_bind failed");
bind->sc = sc;
+ bind->lock = true;
}
void
diff --git a/input.h b/input.h
index e2a682a..08a65da 100644
--- a/input.h
+++ b/input.h
@@ -13,6 +13,7 @@ typedef enum InputBindType {
typedef struct InputBind InputBind;
struct InputBind {
InputBindType type;
+ bool lock;
union {
SDL_Scancode sc;
uint8_t cb;
@@ -56,7 +57,9 @@ struct InputAxis {
void input_deinit(void);
void input_new_action(const char *tag);
void input_new_axis(const char *tag);
+void input_clear_action(const char *tag);
void input_bind_action_sc(const char *tag, SDL_Scancode sc);
+void input_bind_action_sc_locked(const char *tag, SDL_Scancode sc);
void input_bind_action_cb(const char *tag, uint8_t cb);
void input_bind_action_mb(const char *tag, uint8_t mb);
void input_bind_action_tzr(const char *tag, TZR_Event e);