Fumofumotris/source/io/ctrl.c

323 lines
6.6 KiB
C
Raw Normal View History

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-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-03-25 05:34:59 +00:00
KEY,
AXIS,
JOYSTICK,
ESCAPE
};
2024-04-09 22:10:30 +00:00
struct Button {
u32 value;
bool is_down;
bool is_held;
bool is_up;
};
struct Axis {
i64 value;
};
struct Joystick {
i32 x;
i32 y;
};
struct InputRecord {
u16 bind;
2024-04-01 22:39:21 +00:00
u8 type;
2024-03-25 05:34:59 +00:00
union {
2024-04-09 22:10:30 +00:00
struct Button but;
struct Axis axis;
struct Joystick js;
};
2024-03-25 05:34:59 +00:00
2024-04-01 22:39:21 +00:00
struct timespec timestamp;
2024-03-25 05:34:59 +00:00
};
2024-04-09 22:10:30 +00:00
struct InputAxis {
2024-03-25 05:34:59 +00:00
union {
2024-04-09 22:10:30 +00:00
struct Button but;
struct Axis axis;
struct Joystick js;
};
2024-03-25 05:34:59 +00:00
2024-04-01 22:39:21 +00:00
struct timespec last_pressed;
struct timespec last_released;
2024-03-25 05:34:59 +00:00
};
typedef u32 hashtype;
hashtype Hash(void *item, size_t size)
{
2024-04-09 07:00:48 +00:00
u8 *data = (u8 *)item;
2024-03-25 05:34:59 +00:00
u32 h = 98317;
for (size_t i = 0; i < size; i++) {
h ^= data[i];
h *= 0x5bd1e995;
h ^= h >> 15;
}
return h;
}
2024-04-09 07:00:48 +00:00
hashtype hash_id(u16f value, u8f type)
2024-03-25 05:34:59 +00:00
{
2024-04-09 22:10:30 +00:00
struct { u16 id; u8 type; } id = { value, type };
return Hash(&id, sizeof(id));
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-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;
struct {
struct InputRecord records[IO_BUF_SIZE];
size_t len;
} input_buf;
2024-03-25 05:34:59 +00:00
2024-04-09 22:10:30 +00:00
struct {
size_t indexes[IO_BUF_SIZE];
size_t len;
} pending_state_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-09 22:10:30 +00:00
.input_buf = {
.len = 0,
},
.pending_state_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-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;
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-09 22:10:30 +00:00
void update_key(struct InputAxis *axis, struct InputRecord *rec)
2024-03-25 05:34:59 +00:00
{
2024-04-09 22:10:30 +00:00
if (rec->but.is_down) {
axis->last_pressed = rec->timestamp;
axis->but.is_held = true;
} else {
axis->last_released = rec->timestamp;
axis->but.is_held = false;
}
axis->but.is_down |= rec->but.is_down;
axis->but.is_up |= rec->but.is_up;
}
2024-03-25 05:34:59 +00:00
2024-04-09 22:10:30 +00:00
void update_axis(struct InputAxis *axis, struct InputRecord *rec)
{
axis->axis.value = rec->axis.value;
axis->last_pressed = rec->timestamp;
2024-03-25 05:34:59 +00:00
}
2024-04-09 22:10:30 +00:00
void update_joystick(struct InputAxis *axis, struct InputRecord *rec)
2024-03-25 05:34:59 +00:00
{
2024-04-09 22:10:30 +00:00
axis->js.x = rec->js.x;
axis->js.y = rec->js.y;
axis->last_pressed = rec->timestamp;
2024-03-25 05:34:59 +00:00
}
2024-04-09 22:10:30 +00:00
bool dispatch_update(struct InputAxis *axis, struct InputRecord *rec)
2024-03-25 05:34:59 +00:00
{
2024-04-09 22:10:30 +00:00
switch (rec->type) {
case KEY:
update_key(axis, rec);
printf("axis:%hu\n", axis->but.is_down);
return true;
case AXIS:
update_axis(axis, rec);
return true;
case JOYSTICK:
update_joystick(axis, rec);
return true;
}
return false;
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-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];
printf("i:%hu\n", rec->bind);
2024-04-01 22:39:21 +00:00
2024-04-09 22:10:30 +00:00
struct InputAxis *axis = find_axis(&ctrl->binds, rec->bind, rec->type);
2024-04-01 22:39:21 +00:00
if (axis == nullptr)
continue;
2024-04-09 22:10:30 +00:00
if (!dispatch_update(axis, rec))
2024-04-01 22:39:21 +00:00
return false;
}
2024-03-25 05:34:59 +00:00
2024-04-09 22:10:30 +00:00
ctrl->input_buf.len = 0;
2024-03-25 05:34:59 +00:00
return true;
}