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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
#include <errno.h>
#include <string.h>
#undef GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <daw/daw.h>
#include <daw/dltools.h>
#include <daw/logging.h>
#include <daw/input.h>
extern input_callback_t* callbacks[128];
extern usize callbacks_len;
extern Instance* GLOBAL_PLATFORM;
/* Lazy binds, used internally. They are similar to BindAction and friends.
* The only difference is that we set callbacks and such to NULL, but populate
* the function name strings such that can be reloaded. */
#define BindActionLazy(key, altkey, action_str) \
(binding_t) { \
.action = (action_t){.action = \
{ \
.type = InputType_action, \
.callback = NULL, \
.callback_str = strdup(action_str), \
}}, \
.keycode = key, .keycode_alt = altkey, .since_last_activation = 0 \
}
#define BindStateLazy(key, altkey, _activate_str, _deactivate_str) \
(binding_t) { \
.action = (action_t){.state = \
{ \
.type = InputType_state, \
.activate = NULL, \
.deactivate = NULL, \
.activate_str = strdup(_activate_str), \
.deactivate_str = strdup(_deactivate_str), \
}}, \
.keycode = key, .keycode_alt = altkey, .since_last_activation = 0 \
}
void key_callback(void* window, int key, int scancode, int action, int mods) {
const i_ctx* bindings = *GLOBAL_PLATFORM->window->bindings;
const usize bindings_len = GLOBAL_PLATFORM->window->bindings_len;
const u64 now = get_time();
for (usize b = 0; b < bindings_len; b++) {
const action_t a = i_get_action(&bindings[b], now, key);
switch (action) {
case GLFW_PRESS:
switch (a.type) {
case InputType_action:
if (a.action.callback != NULL) {
callbacks[callbacks_len++] = a.action.callback;
}
break;
case InputType_state:
if (a.state.activate != NULL) {
callbacks[callbacks_len++] = a.state.activate;
}
break;
case InputType_range:
#ifdef _DEBUG
WARN("Range inputs not supported yet!");
#endif
break;
case InputType_error:
#ifdef _DEBUG
WARN("Unhandled keycode: %lu", key);
#endif
default:
break;
}
break;
case GLFW_RELEASE:
switch (a.type) {
case InputType_state:
if (a.state.deactivate != NULL) {
callbacks[callbacks_len++] = a.state.deactivate;
}
break;
default:break;
}
break;
case GLFW_REPEAT:
/* unhandled so far */
break;
default:
break;
}
}
}
void binding_t_free(binding_t* b) {
switch (b->action.type) {
case InputType_error:
ERROR("Cannot free binding of type InputType_error");
break;
case InputType_action:
free(b->action.action.callback_str);
return;
case InputType_state:
free(b->action.state.activate_str);
free(b->action.state.deactivate_str);
break;
case InputType_range:
ERROR("Cannot free binding of type InputType_rage");
break;
default:
ERROR("Unknown bindings type");
break;
}
exit(EXIT_FAILURE);
}
void i_ctx_t_free(i_ctx* c) {
for (usize i = 0; i < c->len; i++) {
binding_t_free(&c->bindings[i]);
}
}
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_keycode_cmp(binding_t* restrict a, binding_t* restrict b) {
return a->keycode == b->keycode || a->keycode_alt == b->keycode_alt;
}
bool binding_keycode_cmp_i(binding_t* restrict a, keycode_t keycode) {
return a->keycode == keycode || a->keycode_alt == keycode;
}
// If any binding in ctx contains action, replace that entry.
bool i_update_binding(i_ctx* ctx, binding_t* binding) {
usize 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].keycode = binding->keycode;
ctx->bindings[idx].keycode_alt = binding->keycode_alt;
return true;
}
return false;
}
// If any binding in ctx contains keycode, replace that action.
bool i_update_unique_binding(i_ctx* ctx, binding_t* binding) {
usize idx = 0;
if (ctx == NULL || binding == NULL) {
ERROR("i_update_unique_binding received nullptr!");
return false;
}
while (idx < ctx->len && !binding_keycode_cmp(&ctx->bindings[idx], binding))
idx++;
if (idx < ctx->len && binding_keycode_cmp(&ctx->bindings[idx], binding)) {
ctx->bindings[idx].action = binding->action;
return true;
}
return false;
}
/* Call binding callbacks of respective bindings */
void i_flush_bindings(u64 dt, usize numcalls, input_callback_t* c[], void* state_mem) {
for (usize i = 0; i < numcalls; i++) {
(c[i])(state_mem);
}
// reset callback len and be ready for more
callbacks_len = 0;
}
action_t i_get_action(const i_ctx* restrict ctx, u64 time,
keycode_t keycode) {
usize idx = 0;
if (ctx == NULL) {
ERROR("%s received nullptr!", __func__);
return (action_t){.type = InputType_error};
}
// Needed for glfw
switch (keycode) {
case GLFW_KEY_LEFT_SHIFT:
case GLFW_KEY_RIGHT_SHIFT:
keycode = MOD_SHIFT;
break;
case GLFW_KEY_LEFT_CONTROL:
case GLFW_KEY_RIGHT_CONTROL:
keycode = MOD_CONTROL;
break;
case GLFW_KEY_LEFT_ALT:
case GLFW_KEY_RIGHT_ALT:
keycode = MOD_ALT;
break;
case GLFW_KEY_LEFT_SUPER:
case GLFW_KEY_RIGHT_SUPER:
keycode = MOD_SUPER;
break;
default: break;
}
while (idx < ctx->len &&
!binding_keycode_cmp_i(&ctx->bindings[idx], keycode))
idx++;
if (idx < ctx->len && binding_keycode_cmp_i(&ctx->bindings[idx], keycode)) {
ctx->bindings[idx].since_last_activation = time;
return ctx->bindings[idx].action;
}
return (action_t){.type = InputType_error};
}
/* Make a lazy duplication of a binding. See comments on BindActionLazy and
* friends above. */
i_ctx* i_ctx_dup(i_ctx** ctx, usize ctx_len) {
usize num_binds = 0;
for (usize c = 0; c < ctx_len; c++) {
num_binds += ctx[c]->len;
}
binding_t* bb = calloc(num_binds, sizeof(binding_t));
i_ctx* ret = calloc(ctx_len, sizeof(i_ctx));
usize cumsum = 0;
for (usize c = 0; c < ctx_len; c++) {
binding_t* b = ctx[c]->bindings;
ret[c].len = ctx[c]->len;
ret[c].bindings = &bb[cumsum];
for (usize i = 0; i < ctx[c]->len; i++) {
switch (b[i].action.type) {
case InputType_error:
break;
case InputType_action:
bb[cumsum] = BindActionLazy(b[i].keycode, b[i].keycode_alt,
strdup(b[i].action.action.callback_str));
break;
case InputType_state:
bb[cumsum] = BindStateLazy(b[i].keycode, b[i].keycode_alt,
strdup(b[i].action.state.activate_str),
strdup(b[i].action.state.deactivate_str));
break;
case InputType_range:
default:
break;
}
cumsum++;
}
}
return ret;
}
/* Returns a pointer to a binding in c with .action == a.
* Returns NULL if not found. */
binding_t* get_action(i_ctx* c, action_t* a) {
for (usize i = 0; i < c->len; i++) {
if (c->bindings[i].action.type != a->type) continue;
switch (c->bindings[i].action.type) {
case InputType_error:
return NULL;
case InputType_action:
if (!strcmp(c->bindings[i].action.action.callback_str,
a->action.callback_str)) {
return &c->bindings[i];
}
break;
case InputType_state:
if (!strcmp(c->bindings[i].action.state.activate_str,
a->state.activate_str) &&
!strcmp(c->bindings[i].action.state.deactivate_str,
a->state.deactivate_str)) {
return &c->bindings[i];
}
break;
case InputType_range:
default:
return NULL;
}
}
return NULL;
}
void i_bind_ctx(i_ctx* c, keycode_t s, action_t* a) {
binding_t* b = get_action(c, a);
if (b == NULL) {
WARN("Failed to update keybinding");
return;
}
b->keycode = s;
}
void i_bind_ctx_alt(i_ctx* c, keycode_t s, action_t* a) {
binding_t* b = get_action(c, a);
if (b == NULL) {
WARN("Failed to update keybinding");
return;
}
b->keycode_alt = s;
}
void i_bind(binding_t* b, keycode_t s) {
if (b == NULL) {
WARN("Failed to update keybinding");
return;
}
b->keycode = s;
}
void i_bind_alt(binding_t* b, keycode_t s) {
if (b == NULL) {
WARN("Failed to update keybinding");
return;
}
b->keycode_alt = s;
}
/* Pushes an input context onto the input handling stack */
void i_ctx_push(i_ctx* ctx) {
if (GLOBAL_PLATFORM->window->bindings == NULL) {
GLOBAL_PLATFORM->window->bindings = calloc(8, sizeof(i_ctx*));
GLOBAL_PLATFORM->window->bindings_sz = 8;
}
if (GLOBAL_PLATFORM->window->bindings_len + 1 >= GLOBAL_PLATFORM->window->bindings_sz) {
void* m =
realloc(GLOBAL_PLATFORM->window->bindings, GLOBAL_PLATFORM->window->bindings_sz + 8);
if (m == NULL) {
ERROR("Failed to allocate 8 bytes (%d): %s", errno, strerror(errno));
exit(EXIT_FAILURE);
}
GLOBAL_PLATFORM->window->bindings_sz += 8;
}
LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->window->bindings_len);
for (usize 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->window->bindings[GLOBAL_PLATFORM->window->bindings_len++] = ctx;
}
/* Pops an input context from the input stack */
void i_ctx_pop(void) {
if (GLOBAL_PLATFORM->window->bindings == NULL || GLOBAL_PLATFORM->window->bindings_sz == 0)
return;
i_ctx_t_free(GLOBAL_PLATFORM->window->bindings[--GLOBAL_PLATFORM->window->bindings_len]);
}
/* Removes all input contexts from the input stack */
void i_ctx_reset(void) {
while (GLOBAL_PLATFORM->window->bindings_len > 0) {
i_ctx_t_free(GLOBAL_PLATFORM->window->bindings[--GLOBAL_PLATFORM->window->bindings_len]);
}
}
|