2024-04-18 21:20:44 +00:00
|
|
|
#include "ctrl.h"
|
2024-04-29 20:01:48 +00:00
|
|
|
#include "ringbuffer.h"
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
#define INIT_SIZE 16
|
|
|
|
|
2024-04-30 21:41:31 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
DictT BIND_T = DICT_T(struct InputAxis *);
|
2024-05-08 22:07:31 +00:00
|
|
|
|
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
bool CreateController(struct Controller *ctrl)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputAxis *axes = calloc(16, sizeof(struct InputAxis));
|
|
|
|
|
|
|
|
if (axes == nullptr)
|
2024-05-08 22:07:31 +00:00
|
|
|
return false;
|
2024-04-09 07:00:48 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
if (!CreateDictionary(BIND_T, &ctrl->binds))
|
2024-04-09 07:00:48 +00:00
|
|
|
return false;
|
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
*ctrl = (struct Controller) {
|
|
|
|
.pending_len = 0,
|
|
|
|
.axes = axes,
|
|
|
|
.axes_len = 0
|
|
|
|
};
|
2024-05-08 22:07:31 +00:00
|
|
|
|
2024-04-09 07:00:48 +00:00
|
|
|
return true;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
void FreeController(struct Controller *ctrl)
|
2024-04-09 07:00:48 +00:00
|
|
|
{
|
2024-05-09 05:32:58 +00:00
|
|
|
free(ctrl->axes);
|
2024-05-08 22:07:31 +00:00
|
|
|
FreeDictionary(&ctrl->binds);
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
u32 hash_bind(u16f bind, u16f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-05-09 05:32:58 +00:00
|
|
|
return bind + (type << 16);
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputAxis *ControllerMap(
|
2024-05-08 22:07:31 +00:00
|
|
|
struct Controller *ctrl,
|
|
|
|
struct ControlMapping *map
|
|
|
|
) {
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputAxis *axis = &ctrl->axes[map->code];
|
|
|
|
|
|
|
|
u32 hash = hash_bind(map->bind, map->type);
|
|
|
|
struct InputAxis **bind = DictionarySet(BIND_T, &ctrl->binds, hash, axis);
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
if (bind == nullptr) {
|
|
|
|
printf("whar");
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-05-08 12:47:46 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
*bind = axis;
|
|
|
|
axis->type = map->type;
|
|
|
|
|
|
|
|
return axis;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
bool ControllerMapMulti(
|
|
|
|
struct Controller *ctrl,
|
2024-05-08 12:47:46 +00:00
|
|
|
usize n,
|
2024-05-08 22:07:31 +00:00
|
|
|
struct ControlMapping *maps,
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputAxis **binds
|
2024-05-07 22:10:15 +00:00
|
|
|
) {
|
2024-05-08 12:47:46 +00:00
|
|
|
for (usize i = 0; i < n; i++) {
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputAxis *axis = ControllerMap(ctrl, maps + i);
|
2024-05-08 12:47:46 +00:00
|
|
|
|
2024-05-08 22:07:31 +00:00
|
|
|
if (axis == nullptr)
|
2024-05-07 22:10:15 +00:00
|
|
|
return false;
|
2024-05-08 22:07:31 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
binds[i] = axis;
|
2024-05-07 22:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
void dispatch_update(struct InputAxis *axis, struct InputRecord *rec)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-11 19:57:22 +00:00
|
|
|
if (rec->is_down and !axis->is_held) {
|
|
|
|
axis->is_down = true;
|
|
|
|
axis->is_held = true;
|
2024-04-18 21:20:44 +00:00
|
|
|
axis->last_pressed = rec->time;
|
2024-04-11 19:57:22 +00:00
|
|
|
} else if (rec->is_up) {
|
|
|
|
axis->is_up = true;
|
|
|
|
axis->is_held = false;
|
2024-04-18 21:20:44 +00:00
|
|
|
axis->last_released = rec->time;
|
2024-04-09 22:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
axis->data = rec->data;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-05-09 05:32:58 +00:00
|
|
|
for (usize i = 0; i < ctrl->pending_len; i++) {
|
|
|
|
struct InputAxis *axis = ctrl->pending[i];
|
2024-04-10 14:03:57 +00:00
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
axis->is_up = false;
|
|
|
|
axis->is_down = false;
|
2024-04-10 14:03:57 +00:00
|
|
|
}
|
2024-05-06 05:52:30 +00:00
|
|
|
|
2024-05-09 05:32:58 +00:00
|
|
|
ctrl->pending_len = 0;
|
2024-05-02 22:17:37 +00:00
|
|
|
|
2024-05-08 12:47:46 +00:00
|
|
|
for (usize i = 0; i < recs->head.len; i++) {
|
2024-05-09 05:32:58 +00:00
|
|
|
struct InputRecord *rec = recs->buf + i;
|
|
|
|
|
|
|
|
u32 hash = hash_bind(rec->bind, rec->type);
|
|
|
|
struct InputAxis *axis = DictionaryFind(BIND_T, &ctrl->binds, hash);
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
if (axis == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
dispatch_update(axis, rec);
|
2024-05-09 05:32:58 +00:00
|
|
|
ctrl->pending[ctrl->pending_len++] = axis;
|
2024-04-11 19:57:22 +00:00
|
|
|
}
|
2024-04-22 22:13:13 +00:00
|
|
|
|
2024-04-30 21:41:31 +00:00
|
|
|
recs->head.len = 0;
|
2024-04-11 19:57:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
/*int main()
|
2024-04-11 19:57:22 +00:00
|
|
|
{
|
|
|
|
struct Controller ctrl;
|
2024-05-02 22:17:37 +00:00
|
|
|
if (!CreateController(&ctrl))
|
2024-04-11 19:57:22 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
ControllerMap(&ctrl, 123, 111, BUTTON);
|
|
|
|
ControllerMap(&ctrl, 0, 8, BUTTON);
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
struct RecordBuffer recs = { .head.len = 0, .head.start = 0 };
|
|
|
|
|
|
|
|
recs.buf[recs.head.len++] = (struct InputRecord) {
|
|
|
|
.but.value = 69,
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
.is_down = true,
|
2024-05-02 22:17:37 +00:00
|
|
|
|
|
|
|
.id.bind = 111,
|
|
|
|
.id.type = BUTTON
|
2024-04-11 19:57:22 +00:00
|
|
|
};
|
2024-05-02 22:17:37 +00:00
|
|
|
recs.buf[recs.head.len++] = (struct InputRecord) {
|
|
|
|
.but.value = 1000,
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
.is_down = true,
|
2024-05-02 22:17:37 +00:00
|
|
|
|
|
|
|
.id.bind = 8,
|
|
|
|
.id.type = BUTTON
|
2024-04-18 21:20:44 +00:00
|
|
|
};
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
ControllerPoll(&ctrl, &recs);
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
struct InputAxis *a = ControllerGet(&ctrl, 123, BUTTON);
|
2024-04-11 19:57:22 +00:00
|
|
|
printf("%u\n", a->but.value);
|
2024-04-18 21:20:44 +00:00
|
|
|
|
2024-05-02 22:17:37 +00:00
|
|
|
struct InputAxis *b = ControllerGet(&ctrl, 0, BUTTON);
|
2024-04-18 21:20:44 +00:00
|
|
|
printf("%u\n", b->but.value);
|
2024-04-11 19:57:22 +00:00
|
|
|
|
|
|
|
printf("success");
|
|
|
|
return 0;
|
2024-04-19 20:23:11 +00:00
|
|
|
}*/
|