2024-03-25 05:34:59 +00:00
|
|
|
#include <iso646.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-04-01 22:39:21 +00:00
|
|
|
#include <time.h>
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
#include "fumotris.h"
|
2024-04-10 20:05:56 +00:00
|
|
|
#include "hash.h"
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
#define IO_BUF_SIZE 16
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
enum InputType {
|
2024-04-11 19:57:22 +00:00
|
|
|
BUTTON,
|
2024-03-25 05:34:59 +00:00
|
|
|
AXIS,
|
|
|
|
JOYSTICK,
|
|
|
|
ESCAPE
|
|
|
|
};
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
struct Button {
|
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
struct Axis {
|
|
|
|
i64 value;
|
|
|
|
};
|
|
|
|
struct Joystick {
|
|
|
|
i32 x;
|
|
|
|
i32 y;
|
|
|
|
};
|
|
|
|
union InputData {
|
|
|
|
struct Button input_but;
|
|
|
|
struct Axis input_axis;
|
|
|
|
struct Joystick input_js;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputRecord {
|
|
|
|
u16f bind;
|
|
|
|
struct timespec timestamp;
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
u8 type;
|
2024-04-10 20:05:56 +00:00
|
|
|
u8 is_down;
|
|
|
|
u8 is_held;
|
|
|
|
u8 is_up;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
union {
|
2024-04-11 19:57:22 +00:00
|
|
|
struct Button but;
|
|
|
|
struct Axis axis;
|
|
|
|
struct Joystick js;
|
|
|
|
union InputData data;
|
2024-04-09 22:10:30 +00:00
|
|
|
};
|
2024-04-11 05:17:14 +00:00
|
|
|
};
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
struct InputAxis {
|
2024-04-01 22:39:21 +00:00
|
|
|
struct timespec last_pressed;
|
|
|
|
struct timespec last_released;
|
2024-04-11 19:57:22 +00:00
|
|
|
|
|
|
|
u8 type;
|
|
|
|
u8 is_down;
|
|
|
|
u8 is_held;
|
|
|
|
u8 is_up;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct Button but;
|
|
|
|
struct Axis axis;
|
|
|
|
struct Joystick js;
|
|
|
|
union InputData data;
|
|
|
|
};
|
2024-03-25 05:34:59 +00:00
|
|
|
};
|
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_dict {
|
2024-04-01 22:39:21 +00:00
|
|
|
size_t capacity;
|
|
|
|
size_t filled;
|
2024-04-09 22:10:30 +00:00
|
|
|
|
|
|
|
struct ctrl_bkt {
|
|
|
|
hashtype hash;
|
|
|
|
u16 value;
|
|
|
|
u8 type;
|
|
|
|
|
|
|
|
struct InputAxis *axis;
|
|
|
|
} *bkts;
|
2024-03-26 20:11:58 +00:00
|
|
|
};
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
struct InputBuffer {
|
|
|
|
size_t len;
|
|
|
|
struct InputRecord records[IO_BUF_SIZE];
|
|
|
|
};
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
struct Controller {
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_dict codes;
|
|
|
|
struct ctrl_dict binds;
|
2024-04-09 22:10:30 +00:00
|
|
|
struct InputAxis *axes;
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
struct InputBuffer input_buf;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
struct {
|
|
|
|
size_t len;
|
2024-04-11 19:57:22 +00:00
|
|
|
struct InputAxis *axes[IO_BUF_SIZE];
|
2024-04-10 14:03:57 +00:00
|
|
|
} pending_buf;
|
2024-03-25 05:34:59 +00:00
|
|
|
};
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
bool NewCtrl(struct Controller *ctrl, size_t code_cap, size_t bind_cap)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-09 07:00:48 +00:00
|
|
|
struct ctrl_bkt *code_bkts = calloc(code_cap, sizeof(struct ctrl_bkt));
|
|
|
|
struct ctrl_bkt *bind_bkts = calloc(bind_cap, sizeof(struct ctrl_bkt));
|
2024-04-09 22:10:30 +00:00
|
|
|
struct InputAxis *axes = calloc(code_cap, sizeof(struct InputAxis));
|
2024-04-09 07:00:48 +00:00
|
|
|
|
|
|
|
if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < code_cap; i++) {
|
|
|
|
code_bkts[i].axis = axes + i;
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
*ctrl = (struct Controller) {
|
2024-04-03 23:31:47 +00:00
|
|
|
.codes = (struct ctrl_dict) {
|
2024-04-09 07:00:48 +00:00
|
|
|
.capacity = code_cap,
|
2024-04-03 23:31:47 +00:00
|
|
|
.filled = 0,
|
2024-04-09 07:00:48 +00:00
|
|
|
.bkts = code_bkts,
|
2024-04-03 23:31:47 +00:00
|
|
|
},
|
|
|
|
.binds = (struct ctrl_dict) {
|
2024-04-09 07:00:48 +00:00
|
|
|
.capacity = bind_cap,
|
2024-04-03 23:31:47 +00:00
|
|
|
.filled = 0,
|
2024-04-09 07:00:48 +00:00
|
|
|
.bkts = bind_bkts,
|
|
|
|
},
|
2024-04-09 22:10:30 +00:00
|
|
|
.axes = axes,
|
2024-04-09 07:00:48 +00:00
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
.input_buf.len = 0,
|
|
|
|
.pending_buf.len = 0,
|
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-09 22:10:30 +00:00
|
|
|
free(ctrl->axes);
|
2024-04-03 23:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
return &dict->bkts[i];
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
void set_bkt(struct ctrl_bkt *bkt, hashtype hash, u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
bkt->hash = hash;
|
|
|
|
bkt->value = value;
|
|
|
|
bkt->type = type;
|
2024-03-26 20:11:58 +00:00
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-09 07:00:48 +00:00
|
|
|
size_t wrap_index(size_t i, size_t max)
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-09 07:00:48 +00:00
|
|
|
return i % (SIZE_MAX - max + 1);
|
2024-03-26 20:11:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 20:05:56 +00:00
|
|
|
hashtype hash_id(u16f value, u8f type)
|
|
|
|
{
|
|
|
|
struct { u16 id; u8 type; } id = { value, type };
|
|
|
|
return Hash(&id, sizeof(id));
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
bool find_or_set(struct ctrl_dict *dict, struct ctrl_bkt **out, u16f value, u8f type)
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-09 07:00:48 +00:00
|
|
|
hashtype hash = hash_id(value, type);
|
2024-04-01 22:39:21 +00:00
|
|
|
const size_t index = hash % dict->capacity;
|
|
|
|
|
2024-03-26 20:11:58 +00:00
|
|
|
size_t i = index;
|
2024-04-09 07:00:48 +00:00
|
|
|
while (i != wrap_index(index - 1, dict->capacity)) {
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
2024-04-01 22:39:21 +00:00
|
|
|
|
|
|
|
if (bkt->hash == 0) {
|
|
|
|
set_bkt(bkt, hash, value, type);
|
|
|
|
dict->filled += 1;
|
|
|
|
*out = bkt;
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
if (bkt->value == value and bkt->type == type) {
|
|
|
|
*out = bkt;
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
*out = nullptr;
|
|
|
|
return false;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *find(struct ctrl_dict *dict, u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-09 07:00:48 +00:00
|
|
|
hashtype hash = hash_id(value, type);
|
2024-04-01 22:39:21 +00:00
|
|
|
const size_t index = hash % dict->capacity;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
size_t i = index;
|
2024-04-09 07:00:48 +00:00
|
|
|
while (i != wrap_index(index - 1, dict->capacity)) {
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
2024-03-26 20:11:58 +00:00
|
|
|
if (bkt->hash == 0)
|
2024-04-01 22:39:21 +00:00
|
|
|
goto next;
|
|
|
|
|
2024-04-09 07:00:48 +00:00
|
|
|
if (bkt->value == value and bkt->type == type)
|
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
|
|
|
next:
|
|
|
|
i = (i + 1) % dict->capacity;
|
|
|
|
};
|
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
struct InputAxis *find_axis(struct ctrl_dict *dict, u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bkt = find(dict, value, type);
|
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-09 22:10:30 +00:00
|
|
|
bool CtrlMap(struct Controller *ctrl, u16f code, u16f bind, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-09 07:00:48 +00:00
|
|
|
if (ctrl->codes.filled >= ctrl->codes.capacity or ctrl->binds.filled >= ctrl->binds.capacity) {
|
|
|
|
printf("fatal error");
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *code_bkt;
|
2024-04-01 22:39:21 +00:00
|
|
|
find_or_set(&ctrl->codes, &code_bkt, code, type);
|
|
|
|
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *bind_bkt;
|
2024-04-01 22:39:21 +00:00
|
|
|
bool bind_existed = find_or_set(&ctrl->binds, &bind_bkt, bind, type);
|
|
|
|
|
|
|
|
if(bind_existed and bind_bkt->axis == code_bkt->axis)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bind_bkt->axis = code_bkt->axis;
|
2024-04-10 14:03:57 +00:00
|
|
|
code_bkt->axis->type = type;
|
2024-04-01 22:39:21 +00:00
|
|
|
return true;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-03 23:31:47 +00:00
|
|
|
struct ctrl_bkt *code_bkt = find(&ctrl->codes, 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-11 19:57:22 +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-09 22:10:30 +00:00
|
|
|
axis->last_pressed = rec->timestamp;
|
2024-04-11 19:57:22 +00:00
|
|
|
} else if (rec->is_up) {
|
|
|
|
axis->is_up = true;
|
|
|
|
axis->is_held = false;
|
2024-04-09 22:10:30 +00:00
|
|
|
axis->last_released = rec->timestamp;
|
|
|
|
}
|
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
axis->data = rec->data;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
bool 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-11 19:57:22 +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-11 19:57:22 +00:00
|
|
|
ctrl->pending_buf.len = ctrl->input_buf.len;
|
2024-04-10 14:03:57 +00:00
|
|
|
|
2024-04-09 22:10:30 +00:00
|
|
|
for (size_t i = 0; i < ctrl->input_buf.len; i++) {
|
|
|
|
struct InputRecord *rec = &ctrl->input_buf.records[i];
|
|
|
|
struct InputAxis *axis = find_axis(&ctrl->binds, rec->bind, rec->type);
|
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-03-25 05:34:59 +00:00
|
|
|
|
2024-04-11 19:57:22 +00:00
|
|
|
ctrl->pending_buf.axes[i] = axis;
|
|
|
|
}
|
2024-04-09 22:10:30 +00:00
|
|
|
ctrl->input_buf.len = 0;
|
2024-04-11 19:57:22 +00:00
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
return true;
|
2024-04-11 19:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
struct Controller ctrl;
|
|
|
|
if (!NewCtrl(&ctrl, 3, 3))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
CtrlMap(&ctrl, 123, 111, BUTTON);
|
|
|
|
|
|
|
|
ctrl.input_buf.records[ctrl.input_buf.len++] = (struct InputRecord) {
|
|
|
|
.bind = 111,
|
|
|
|
.type = BUTTON,
|
|
|
|
.is_down = true,
|
|
|
|
.but.value = 69
|
|
|
|
};
|
|
|
|
|
|
|
|
CtrlPoll(&ctrl);
|
|
|
|
|
|
|
|
struct InputAxis *a = CtrlGet(&ctrl, 123, BUTTON);
|
|
|
|
printf("%u\n", a->but.value);
|
|
|
|
|
|
|
|
printf("success");
|
|
|
|
return 0;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|