Fumofumotris/source/io/ctrl.h

114 lines
1.8 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>
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-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-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
};
2024-04-02 00:55:30 +00:00
enum CtrlCode {
2024-03-25 05:34:59 +00:00
LEFT,
RIGHT,
SOFT_DROP,
HARD_DROP,
ROTATE_CCW,
ROTATE_CW,
ROTATE_180,
SWAP,
2024-04-02 00:55:30 +00:00
ESC,
2024-03-25 05:34:59 +00:00
VSCROLL,
2024-04-02 00:55:30 +00:00
HSCROLL,
2024-03-25 05:34:59 +00:00
MOUSE
};
typedef u32 hashtype;
2024-04-03 23:31:47 +00:00
struct ctrl_bkt {
2024-04-01 22:39:21 +00:00
hashtype hash;
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-03-25 05:34:59 +00:00
size_t capacity;
size_t filled;
2024-04-03 23:31:47 +00:00
struct ctrl_bkt *bkts;
2024-04-01 22:39:21 +00:00
};
2024-03-25 05:34:59 +00:00
2024-04-01 22:39:21 +00:00
struct Ctrl {
2024-04-03 23:31:47 +00:00
struct ctrl_dict codes;
struct ctrl_dict binds;
2024-04-01 22:39:21 +00:00
pthread_t thread;
2024-03-25 05:34:59 +00:00
pthread_mutex_t mutex;
};
2024-04-03 23:31:47 +00:00
struct Ctrl NewCtrl(struct ctrl_dict *codes, struct ctrl_dict *binds, struct Axis *axes);
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-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
bool CtrlPoll(struct Ctrl *ctrl, struct RecordBuffer *buf);