Fumofumotris/source/io/ctrl.h

127 lines
2 KiB
C
Raw Normal View History

2024-03-25 05:34:59 +00:00
#pragma once
#include <iso646.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fumotris.h"
#define IO_BUF_SIZE 8
enum InputType {
KEY,
AXIS,
JOYSTICK,
WINDOW,
ESCAPE
};
struct InputRecord {
enum InputType type;
u16 id;
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;
double timestamp;
};
2024-03-26 20:11:58 +00:00
struct InputBuffer {
struct InputRecord 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-03-26 20:11:58 +00:00
struct InputBuffer NewInputBuffer();
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;
double last_pressed;
double last_released;
};
enum KeyCode {
LEFT,
RIGHT,
SOFT_DROP,
HARD_DROP,
ROTATE_CCW,
ROTATE_CW,
ROTATE_180,
SWAP,
ESC
};
enum AxisCode {
VSCROLL,
HSCROLL
};
enum JoystickCode {
MOUSE
};
typedef u32 hashtype;
struct ctrl_bkt {
hashtype bind_hash;
2024-03-26 20:11:58 +00:00
u16 bind;
2024-03-25 05:34:59 +00:00
size_t index;
2024-03-26 20:11:58 +00:00
2024-03-25 05:34:59 +00:00
hashtype code_hash;
2024-03-26 20:11:58 +00:00
u16 code;
2024-03-25 05:34:59 +00:00
struct Axis axis;
};
struct Ctrl {
size_t capacity;
size_t filled;
struct ctrl_bkt *bkts;
pthread_mutex_t mutex;
};
typedef struct Ctrl Ctrl;
Ctrl NewCtrl(struct ctrl_bkt *bkts_prealloc, size_t capacity);
bool CtrlMap(Ctrl *ctrl, u16f bind, u16f code, enum InputType type);
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, enum InputType type);
bool CtrlUpdateKey(Ctrl *ctrl, struct InputRecord *record);
bool CtrlUpdateAxis(Ctrl *ctrl, struct InputRecord *record);
bool CtrlUpdateJoystick(Ctrl *ctrl, struct InputRecord *record);
bool CtrlUpdateWindow(Ctrl *ctrl, struct InputRecord *record);