Fumofumotris/source/io/ctrl.c

306 lines
6.4 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-02 00:55:30 +00:00
enum CtrlType {
2024-03-25 05:34:59 +00:00
KEY,
AXIS,
JOYSTICK,
ESCAPE
};
2024-04-04 13:27:54 +00:00
struct Record {
2024-03-25 05:34:59 +00:00
u16 id;
2024-04-01 22:39:21 +00:00
u8 type;
2024-03-25 05:34:59 +00:00
union {
struct {
bool is_down;
2024-03-26 20:11:58 +00:00
} key;
struct {
u64 value;
2024-03-25 05:34:59 +00:00
} axis;
struct {
u32 x;
u32 y;
} joystick;
} data;
2024-04-01 22:39:21 +00:00
struct timespec timestamp;
2024-03-25 05:34:59 +00:00
};
2024-04-02 00:55:30 +00:00
struct RecordBuffer {
2024-04-04 13:27:54 +00:00
struct Record records[IO_BUF_SIZE];
2024-03-25 05:34:59 +00:00
size_t count;
2024-03-26 20:11:58 +00:00
pthread_mutex_t mutex;
2024-03-25 05:34:59 +00:00
};
struct Axis {
union {
struct {
u32 value;
2024-03-26 20:11:58 +00:00
u32 is_down : 1;
u32 is_up : 1;
} key;
struct {
u64 value;
2024-03-25 05:34:59 +00:00
} axis;
struct {
u32 x;
u32 y;
} joystick;
} data;
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-03 23:31:47 +00:00
struct id {
u16 id;
u8 type;
};
struct id id = { value, type };
return Hash(&id, sizeof(struct id));
2024-03-25 05:34:59 +00:00
}
2024-04-03 23:31:47 +00:00
struct ctrl_bkt {
2024-03-26 20:11:58 +00:00
hashtype hash;
2024-04-01 22:39:21 +00:00
u16 value;
u8 type;
struct Axis *axis;
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-03 23:31:47 +00:00
struct ctrl_bkt *bkts;
2024-03-26 20:11:58 +00:00
};
2024-03-25 05:34:59 +00:00
struct Ctrl {
2024-04-03 23:31:47 +00:00
struct ctrl_dict codes;
struct ctrl_dict binds;
2024-03-25 05:34:59 +00:00
2024-04-09 07:00:48 +00:00
struct RecordBuffer buf;
2024-04-01 22:39:21 +00:00
pthread_t thread;
2024-03-25 05:34:59 +00:00
};
2024-04-09 07:00:48 +00:00
bool NewCtrl(struct Ctrl *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));
struct Axis *axes = calloc(code_cap, sizeof(struct Axis));
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;
}
*ctrl = (struct Ctrl) {
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,
},
.buf = (struct RecordBuffer) {
.count = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER,
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 07:00:48 +00:00
void FreeCtrl(struct Ctrl *ctrl)
{
free(ctrl->codes.bkts);
free(ctrl->binds.bkts);
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-03 23:31:47 +00:00
struct Axis *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-03 23:31:47 +00:00
bool CtrlMap(struct Ctrl *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-03 23:31:47 +00:00
struct Axis *CtrlGet(struct Ctrl *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-04 13:27:54 +00:00
void update_key(struct Axis *axis, struct Record *record)
2024-03-25 05:34:59 +00:00
{
2024-04-01 22:39:21 +00:00
if (record->data.key.is_down)
2024-03-25 05:34:59 +00:00
axis->last_pressed = record->timestamp;
2024-04-01 22:39:21 +00:00
else
2024-03-25 05:34:59 +00:00
axis->last_released = record->timestamp;
2024-04-01 22:39:21 +00:00
axis->data.key.is_down = record->data.key.is_down;
2024-03-25 05:34:59 +00:00
}
2024-04-04 13:27:54 +00:00
void update_axis(struct Axis *axis, struct Record *record)
2024-03-25 05:34:59 +00:00
{
axis->data.axis.value = record->data.axis.value;
axis->last_pressed = record->timestamp;
}
2024-04-04 13:27:54 +00:00
void update_joystick(struct Axis *axis, struct Record *record)
2024-03-25 05:34:59 +00:00
{
axis->data.joystick.x = record->data.joystick.x;
axis->data.joystick.y = record->data.joystick.y;
axis->last_pressed = record->timestamp;
}
2024-04-09 07:00:48 +00:00
bool CtrlPoll(struct Ctrl *ctrl)
2024-03-25 05:34:59 +00:00
{
2024-04-09 07:00:48 +00:00
for (size_t i = 0; i < ctrl->buf.count; i++) {
struct Record *rec = &ctrl->buf.records[i];
2024-04-01 22:39:21 +00:00
struct Axis *axis = find_axis(&ctrl->binds, rec->id, rec->type);
if (axis == nullptr)
continue;
switch (rec->type) {
case KEY:
update_key(axis, rec);
break;
case AXIS:
update_axis(axis, rec);
break;
case JOYSTICK:
update_joystick(axis, rec);
break;
default:
return false;
}
}
2024-03-25 05:34:59 +00:00
2024-04-09 07:00:48 +00:00
ctrl->buf.count = 0;
2024-03-25 05:34:59 +00:00
return true;
}