summaryrefslogtreecommitdiff
path: root/src/input.c
blob: d53e7ed995ae75f28f25a168db7d4257a3a225d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <string.h>
#include <engine/input.h>
#include <engine/logging.h>


bool binding_action_cmp(binding_t *restrict a, binding_t *restrict b) {
	InputType t = a->action.type;
	if (t != b->action.type) return false;
	switch (t) {
		case InputType_action:
			return !strcmp(a->action.action.callback_str, b->action.action.callback_str);

		case InputType_state:
			return (!strcmp(a->action.state.activate_str,   b->action.state.activate_str))
			    && (!strcmp(a->action.state.deactivate_str, b->action.state.deactivate_str));

		case InputType_range: // fallthrough
		default:
			return false; // not implemented
	}
	return false;
}

bool binding_scancode_cmp(binding_t *restrict a, binding_t *restrict b) {
	return a->scancode == b->scancode
	    || a->scancode_alt == b->scancode_alt;
}

bool binding_scancode_cmp_i(binding_t *restrict a, scancode_t scancode) {
	return a->scancode == scancode
	    || a->scancode_alt == scancode;
}

// If any binding in ctx contains action, replace that entry.
bool i_update_binding(i_ctx *ctx, binding_t* binding) {
	isize idx = 0;
	if (ctx == NULL || binding == NULL) {
		ERROR("i_update_binding received nullptr!");
		return false;
	}

	while (idx < ctx->len && !binding_action_cmp(&ctx->bindings[idx], binding)) idx++;

	if (idx < ctx->len && binding_action_cmp(&ctx->bindings[idx], binding)) {
		ctx->bindings[idx].scancode = binding->scancode;
		ctx->bindings[idx].scancode_alt = binding->scancode_alt;
		return true;
	}

	return false;
}

// If any binding in ctx contains scancode, replace that action.
bool i_update_unique_binding(i_ctx *ctx, binding_t* binding) {
	isize idx = 0;
	if (ctx == NULL || binding == NULL) {
		ERROR("i_update_unique_binding received nullptr!");
		return false;
	}

	while (idx < ctx->len && !binding_scancode_cmp(&ctx->bindings[idx], binding)) idx++;

	if (idx < ctx->len && binding_scancode_cmp(&ctx->bindings[idx], binding)) {
		ctx->bindings[idx].action = binding->action;
		return true;
	}

	return false;
}

void i_flush_bindings(usize numcalls, void* state_mem, input_callback_t* c[]) {
	for (usize i = 0; i < numcalls; i++) {
		(c[i])(state_mem);
	}
}

action_t i_get_action(const i_ctx *restrict ctx, u32 time, scancode_t scancode) {
	isize idx = 0;

	if (ctx == NULL) {
		ERROR("%s received nullptr!", __func__);
		return (action_t){.type = InputType_error};
	}

	while (idx < ctx->len && !binding_scancode_cmp_i(&ctx->bindings[idx], scancode)) idx++;

	if (idx < ctx->len
		  && binding_scancode_cmp_i(&ctx->bindings[idx], scancode)) {
		ctx->bindings[idx].since_last_activation = time;
		return ctx->bindings[idx].action;
	}

	return (action_t){.type = InputType_error};
}