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
|
|
|
|
|
|
|
|
bool CreateCtrl(struct Controller *ctrl)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-19 20:23:11 +00:00
|
|
|
struct ctrl_bkt *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt));
|
|
|
|
struct ctrl_bkt *bind_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt));
|
|
|
|
struct InputAxis *axes = calloc(INIT_SIZE, sizeof(struct InputAxis));
|
2024-04-09 07:00:48 +00:00
|
|
|
|
|
|
|
if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
*ctrl = (struct Controller) {
|
2024-04-29 20:01:48 +00:00
|
|
|
.recs.head = RINGBUF_HEAD_INIT,
|
2024-04-18 21:20:44 +00:00
|
|
|
.pending_buf.len = 0,
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
.axis_vec = (struct ctrl_axis_vec) {
|
|
|
|
.axes = axes,
|
|
|
|
.size = INIT_SIZE,
|
|
|
|
.len = 0,
|
|
|
|
},
|
2024-04-03 23:31:47 +00:00
|
|
|
.codes = (struct ctrl_dict) {
|
2024-04-09 07:00:48 +00:00
|
|
|
.bkts = code_bkts,
|
2024-04-19 20:23:11 +00:00
|
|
|
.capacity = INIT_SIZE,
|
2024-04-18 21:20:44 +00:00
|
|
|
.filled = 0,
|
2024-04-03 23:31:47 +00:00
|
|
|
},
|
|
|
|
.binds = (struct ctrl_dict) {
|
2024-04-09 07:00:48 +00:00
|
|
|
.bkts = bind_bkts,
|
2024-04-19 20:23:11 +00:00
|
|
|
.capacity = INIT_SIZE,
|
2024-04-18 21:20:44 +00:00
|
|
|
.filled = 0,
|
2024-04-09 07:00:48 +00:00
|
|
|
},
|
2024-04-03 23:31:47 +00:00
|
|
|
};
|
2024-04-09 07:00:48 +00:00
|
|
|
return true;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
void FreeCtrl(struct Controller *ctrl)
|
2024-04-09 07:00:48 +00:00
|
|
|
{
|
|
|
|
free(ctrl->codes.bkts);
|
|
|
|
free(ctrl->binds.bkts);
|
2024-04-18 21:20:44 +00:00
|
|
|
free(ctrl->axis_vec.axes);
|
2024-04-03 23:31:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i) {
|
2024-04-01 22:39:21 +00:00
|
|
|
return &dict->bkts[i];
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
size_t wrap_index(size_t i, size_t max) {
|
2024-04-09 07:00:48 +00:00
|
|
|
return i % (SIZE_MAX - max + 1);
|
2024-03-26 20:11:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 20:01:48 +00:00
|
|
|
struct ctrl_bkt *find_set(struct ctrl_dict *dict, union InputID id)
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-19 20:23:11 +00:00
|
|
|
size_t i = id.hash % dict->capacity;
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
size_t last = wrap_index(i - 1, dict->capacity);
|
|
|
|
while (i != last) {
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
if (bkt->axis == nullptr) {
|
|
|
|
bkt->id.hash = id.hash;
|
2024-04-01 22:39:21 +00:00
|
|
|
dict->filled += 1;
|
2024-04-18 21:20:44 +00:00
|
|
|
|
|
|
|
return bkt;
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
if (bkt->id.hash == id.hash)
|
2024-04-18 21:20:44 +00:00
|
|
|
return bkt;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
i = (i + 1) % dict->capacity;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
return nullptr;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
struct ctrl_bkt *find(struct ctrl_dict *dict, union InputID id)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-19 20:23:11 +00:00
|
|
|
size_t i = id.hash % dict->capacity;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
size_t last = wrap_index(i - 1, dict->capacity);
|
|
|
|
while (i != last) {
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
if (bkt->id.hash == id.hash)
|
2024-03-26 20:11:58 +00:00
|
|
|
return bkt;
|
2024-04-09 07:00:48 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
i = (i + 1) % dict->capacity;
|
|
|
|
};
|
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
struct InputAxis *find_axis(struct ctrl_dict *dict, union InputID id)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-18 21:20:44 +00:00
|
|
|
struct ctrl_bkt *bkt = find(dict, id);
|
2024-04-01 22:39:21 +00:00
|
|
|
if (bkt == nullptr)
|
|
|
|
return nullptr;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
return bkt->axis;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 20:01:48 +00:00
|
|
|
union InputID as_id(u16f value, u16f type) {
|
2024-04-18 21:20:44 +00:00
|
|
|
return (union InputID) { .value = value, .type = type };
|
|
|
|
}
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind)
|
|
|
|
{
|
2024-04-29 20:01:48 +00:00
|
|
|
struct ctrl_bkt *code_bkt = find_set(&ctrl->codes, as_id(code, type));
|
|
|
|
struct ctrl_bkt *bind_bkt = find_set(&ctrl->binds, as_id(bind, type));
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
if (code_bkt->axis == nullptr)
|
|
|
|
code_bkt->axis = &ctrl->axis_vec.axes[ctrl->axis_vec.len++];
|
|
|
|
else if (code_bkt->axis == bind_bkt->axis)
|
2024-04-01 22:39:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bind_bkt->axis = code_bkt->axis;
|
2024-04-18 21:20:44 +00:00
|
|
|
code_bkt->axis->id.type = type;
|
2024-04-01 22:39:21 +00:00
|
|
|
return true;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-29 20:01:48 +00:00
|
|
|
struct ctrl_bkt *code_bkt = find(&ctrl->codes, as_id(code, type));
|
2024-03-25 05:34:59 +00:00
|
|
|
if (code_bkt == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
return code_bkt->axis;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 21:20:44 +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-04-25 07:04:43 +00:00
|
|
|
void CtrlPoll(struct Controller *ctrl)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-10 14:03:57 +00:00
|
|
|
for (size_t i = 0; i < ctrl->pending_buf.len; i++) {
|
2024-04-18 21:20:44 +00:00
|
|
|
struct InputAxis *axis = ctrl->pending_buf.axes[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-04-22 22:13:13 +00:00
|
|
|
ctrl->pending_buf.len = 0;
|
2024-04-23 06:35:19 +00:00
|
|
|
|
2024-04-29 20:01:48 +00:00
|
|
|
for (size_t i = 0; i < ctrl->recs.head.len; i++) {
|
2024-04-25 07:04:43 +00:00
|
|
|
struct InputRecord *rec = &ctrl->recs.buf[i];
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-04-23 20:33:32 +00:00
|
|
|
struct InputAxis *axis = find_axis(&ctrl->binds, rec->id);
|
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-04-22 22:13:13 +00:00
|
|
|
ctrl->pending_buf.axes[ctrl->pending_buf.len++] = axis;
|
2024-04-11 19:57:22 +00:00
|
|
|
}
|
2024-04-22 22:13:13 +00:00
|
|
|
|
2024-04-29 20:01:48 +00:00
|
|
|
ctrl->recs.head.len = 0;
|
2024-04-22 22:13:13 +00:00
|
|
|
}
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-04-25 07:04:43 +00:00
|
|
|
size_t CtrlInputString(struct Controller *ctrl, size_t n, char *buf)
|
2024-04-22 22:13:13 +00:00
|
|
|
{
|
2024-04-29 20:01:48 +00:00
|
|
|
return RingBufferOut(&STR_BUF_T, n, buf, &ctrl->str.head);
|
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-04-19 20:23:11 +00:00
|
|
|
if (!CreateCtrl(&ctrl))
|
2024-04-11 19:57:22 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
CtrlMap(&ctrl, 123, BUTTON, 111);
|
|
|
|
CtrlMap(&ctrl, 0, BUTTON, 8);
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
ctrl.buf.recs[ctrl.buf.len++] = (struct InputRecord) {
|
2024-04-11 19:57:22 +00:00
|
|
|
.bind = 111,
|
|
|
|
.type = BUTTON,
|
|
|
|
.is_down = true,
|
|
|
|
.but.value = 69
|
|
|
|
};
|
2024-04-18 21:20:44 +00:00
|
|
|
ctrl.buf.recs[ctrl.buf.len++] = (struct InputRecord) {
|
|
|
|
.bind = 8,
|
|
|
|
.type = BUTTON,
|
|
|
|
.is_down = true,
|
|
|
|
.but.value = 1000
|
|
|
|
};
|
2024-04-11 19:57:22 +00:00
|
|
|
|
|
|
|
CtrlPoll(&ctrl);
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
struct InputAxis *a = CtrlGet(&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
|
|
|
|
|
|
|
struct InputAxis *b = CtrlGet(&ctrl, 0, BUTTON);
|
|
|
|
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
|
|
|
}*/
|