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>
2024-04-01 22:39:21 +00:00
#include <time.h>
2024-03-25 05:34:59 +00:00
#include "fumotris.h"
2024-04-11 19:57:22 +00:00
#include "hash.h"
2024-03-25 05:34:59 +00:00
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-04-11 19:57:22 +00:00
BUTTON,
2024-03-25 05:34:59 +00:00
AXIS,
JOYSTICK,
ESCAPE
};
2024-04-10 14:03:57 +00:00
struct Button {
2024-04-11 19:57:22 +00:00
u64 value;
2024-04-10 14:03:57 +00:00
};
struct Axis {
i64 value;
};
struct Joystick {
i32 x;
i32 y;
};
2024-04-11 19:57:22 +00:00
union InputData {
struct Button input_but;
struct Axis input_axis;
struct Joystick input_js;
};
2024-04-10 14:03:57 +00:00
2024-04-09 22:10:30 +00:00
struct InputRecord {
2024-04-11 19:57:22 +00:00
u16f bind;
struct timespec timestamp;
2024-04-01 22:39:21 +00:00
u8 type;
2024-04-11 19:57:22 +00:00
u8 is_down;
u8 is_held;
u8 is_up;
2024-03-25 05:34:59 +00:00
union {
2024-04-10 14:03:57 +00:00
struct Button but;
struct Axis axis;
struct Joystick js;
2024-04-11 19:57:22 +00:00
union InputData data;
2024-04-09 22:10:30 +00:00
};
2024-03-25 05:34:59 +00:00
};
2024-04-09 22:10:30 +00:00
struct InputAxis {
2024-04-11 19:57:22 +00:00
struct timespec last_pressed;
struct timespec last_released;
u8 type;
u8 is_down;
u8 is_held;
u8 is_up;
2024-03-25 05:34:59 +00:00
union {
2024-04-10 14:03:57 +00:00
struct Button but;
struct Axis axis;
struct Joystick js;
2024-04-11 19:57:22 +00:00
union InputData data;
2024-04-09 22:10:30 +00:00
};
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
};
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-10 14:03:57 +00:00
struct ctrl_bkt {
hashtype hash;
u16 value;
u8 type;
struct InputAxis *axis;
} *bkts;
2024-04-01 22:39:21 +00:00
};
2024-03-25 05:34:59 +00:00
2024-04-11 19:57:22 +00:00
struct InputBuffer {
size_t len;
struct InputRecord records[IO_BUF_SIZE];
};
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;
2024-04-01 22:39:21 +00:00
2024-04-11 19:57:22 +00:00
struct InputBuffer input_buf;
2024-04-10 14:03:57 +00:00
struct {
size_t len;
2024-04-11 19:57:22 +00:00
struct InputAxis *axes[IO_BUF_SIZE];
2024-04-10 14:03:57 +00:00
} pending_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-04-09 07:00:48 +00:00
2024-04-09 22:10:30 +00:00
void FreeCtrl(struct Controller *ctrl);
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 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-09 22:10:30 +00:00
bool CtrlPoll(struct Controller *ctrl);