2024-04-01 22:39:21 +00:00
|
|
|
#include <assert.h>
|
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,
|
|
|
|
WINDOW,
|
|
|
|
ESCAPE
|
|
|
|
};
|
|
|
|
|
2024-04-02 00:55:30 +00:00
|
|
|
struct CtrlRecord {
|
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 {
|
|
|
|
struct CtrlRecord 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
|
|
|
};
|
|
|
|
|
2024-04-02 00:55:30 +00:00
|
|
|
struct RecordBuffer NewInputBuffer()
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-02 00:55:30 +00:00
|
|
|
struct RecordBuffer buf;
|
2024-03-26 20:11:58 +00:00
|
|
|
buf.count = 0;
|
|
|
|
buf.mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
u8* data = (u8*)item;
|
|
|
|
|
|
|
|
u32 h = 98317;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
h ^= data[i];
|
|
|
|
h *= 0x5bd1e995;
|
|
|
|
h ^= h >> 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ident {
|
|
|
|
u16 id;
|
2024-04-01 22:39:21 +00:00
|
|
|
u8 type;
|
2024-03-25 05:34:59 +00:00
|
|
|
};
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
hashtype hash_ident(u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
struct ident id = { value, type };
|
|
|
|
return Hash(&id, sizeof(struct ident));
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
struct 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-01 22:39:21 +00:00
|
|
|
struct dict {
|
|
|
|
size_t capacity;
|
|
|
|
size_t filled;
|
|
|
|
struct bkt *bkts;
|
2024-03-26 20:11:58 +00:00
|
|
|
};
|
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
struct Ctrl {
|
2024-04-01 22:39:21 +00:00
|
|
|
struct dict codes;
|
|
|
|
struct dict binds;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
pthread_t thread;
|
2024-03-25 05:34:59 +00:00
|
|
|
pthread_mutex_t mutex;
|
|
|
|
};
|
|
|
|
typedef struct Ctrl Ctrl;
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
Ctrl NewCtrl(struct bkt *codes, struct Axis *axes, size_t c, struct bkt *binds, size_t b)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
memset(codes, 0, sizeof(struct bkt) * c);
|
|
|
|
memset(axes, 0, sizeof(struct Axis) * c);
|
|
|
|
memset(binds, 0, sizeof(struct bkt) * b);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < c; i++) {
|
|
|
|
codes[i].axis = &axes[i];
|
|
|
|
}
|
2024-03-26 20:11:58 +00:00
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
Ctrl ctrl;
|
2024-04-01 22:39:21 +00:00
|
|
|
|
|
|
|
ctrl.codes.capacity = c;
|
|
|
|
ctrl.codes.filled = 0;
|
2024-03-26 20:11:58 +00:00
|
|
|
ctrl.codes.bkts = codes;
|
2024-04-01 22:39:21 +00:00
|
|
|
|
|
|
|
ctrl.binds.capacity = b;
|
|
|
|
ctrl.binds.filled = 0;
|
2024-03-26 20:11:58 +00:00
|
|
|
ctrl.binds.bkts = binds;
|
2024-04-01 22:39:21 +00:00
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
ctrl.mutex = PTHREAD_MUTEX_INITIALIZER;
|
2024-03-26 20:11:58 +00:00
|
|
|
|
2024-03-25 05:34:59 +00:00
|
|
|
return ctrl;
|
|
|
|
}
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
struct bkt *get_bkt(struct dict *dict, size_t i)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
assert(i < dict->capacity);
|
|
|
|
return &dict->bkts[i];
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
void set_bkt(struct 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-01 22:39:21 +00:00
|
|
|
size_t wrap(size_t x, size_t wrap)
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
return x % (SIZE_MAX - wrap + 1);
|
2024-03-26 20:11:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
bool find_or_set(struct dict *dict, struct bkt **out, u16f value, u8f type)
|
2024-03-26 20:11:58 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
hashtype hash = hash_ident(value, type);
|
|
|
|
const size_t index = hash % dict->capacity;
|
|
|
|
|
2024-03-26 20:11:58 +00:00
|
|
|
size_t i = index;
|
2024-04-01 22:39:21 +00:00
|
|
|
while (i != wrap(index - 1, dict->capacity)) {
|
|
|
|
struct bkt *bkt = get_bkt(dict, i);
|
|
|
|
|
|
|
|
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-01 22:39:21 +00:00
|
|
|
struct bkt *find(struct dict *dict, u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
hashtype hash = hash_ident(value, type);
|
|
|
|
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;
|
|
|
|
while (i != wrap(index - 1, dict->capacity)) {
|
|
|
|
struct 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;
|
|
|
|
|
|
|
|
if (bkt->value == value and bkt->type == type) {
|
2024-03-26 20:11:58 +00:00
|
|
|
return bkt;
|
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-01 22:39:21 +00:00
|
|
|
struct Axis *find_axis(struct dict *dict, u16f value, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
struct bkt *bkt = find(dict, value, type);
|
|
|
|
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-01 22:39:21 +00:00
|
|
|
bool CtrlMap(Ctrl *ctrl, u16f code, u16f bind, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
assert(ctrl->codes.filled < ctrl->codes.capacity);
|
|
|
|
assert(ctrl->binds.filled < ctrl->binds.capacity);
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
struct bkt *code_bkt;
|
|
|
|
find_or_set(&ctrl->codes, &code_bkt, code, type);
|
|
|
|
assert(code_bkt != nullptr);
|
|
|
|
|
|
|
|
struct bkt *bind_bkt;
|
|
|
|
bool bind_existed = find_or_set(&ctrl->binds, &bind_bkt, bind, type);
|
|
|
|
assert(bind_bkt != nullptr);
|
|
|
|
|
|
|
|
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-03-26 20:11:58 +00:00
|
|
|
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
struct 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-02 00:55:30 +00:00
|
|
|
void update_key(struct Axis *axis, struct CtrlRecord *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-02 00:55:30 +00:00
|
|
|
void update_axis(struct Axis *axis, struct CtrlRecord *record)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
|
|
|
axis->data.axis.value = record->data.axis.value;
|
|
|
|
axis->last_pressed = record->timestamp;
|
|
|
|
}
|
|
|
|
|
2024-04-02 00:55:30 +00:00
|
|
|
void update_joystick(struct Axis *axis, struct CtrlRecord *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-02 00:55:30 +00:00
|
|
|
bool CtrlPoll(Ctrl *ctrl, struct RecordBuffer *buf)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-01 22:39:21 +00:00
|
|
|
pthread_mutex_lock(&buf->mutex);
|
|
|
|
pthread_mutex_lock(&ctrl->mutex);
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
for (size_t i = 0; i < buf->count; i++) {
|
2024-04-02 00:55:30 +00:00
|
|
|
struct CtrlRecord *rec = &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:
|
|
|
|
case WINDOW:
|
|
|
|
update_joystick(axis, rec);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-01 22:39:21 +00:00
|
|
|
buf->count = 0;
|
|
|
|
pthread_mutex_unlock(&buf->mutex);
|
|
|
|
pthread_mutex_unlock(&ctrl->mutex);
|
2024-03-25 05:34:59 +00:00
|
|
|
return true;
|
|
|
|
}
|