2024-03-25 05:34:59 +00:00
|
|
|
#pragma once
|
|
|
|
#include <iso646.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-04-19 20:23:11 +00:00
|
|
|
#include <pthread.h>
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
#include "fumotris.h"
|
2024-04-18 21:20:44 +00:00
|
|
|
#include "gametime.h"
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
#define IO_BUF_SIZE 16
|
2024-04-23 20:33:32 +00:00
|
|
|
#define STR_BUF_SIZE (IO_BUF_SIZE * 4)
|
2024-03-26 20:11:58 +00:00
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
enum InputType {
|
|
|
|
BUTTON,
|
|
|
|
AXIS,
|
|
|
|
JOYSTICK,
|
|
|
|
ESCAPE
|
2024-04-10 20:05:56 +00:00
|
|
|
};
|
|
|
|
|
2024-04-18 21:20:44 +00:00
|
|
|
union InputID {
|
|
|
|
struct {
|
2024-04-19 20:23:11 +00:00
|
|
|
union {
|
|
|
|
u16 code;
|
|
|
|
u16 bind;
|
|
|
|
u16 value;
|
|
|
|
};
|
2024-04-18 21:20:44 +00:00
|
|
|
u16 type;
|
|
|
|
};
|
|
|
|
u32 hash;
|
|
|
|
};
|
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
struct Button {
|
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
struct Axis {
|
|
|
|
i64 value;
|
|
|
|
};
|
|
|
|
struct Joystick {
|
|
|
|
i32 x;
|
|
|
|
i32 y;
|
|
|
|
};
|
2024-04-19 06:38:45 +00:00
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
union InputData {
|
|
|
|
struct Button input_but;
|
|
|
|
struct Axis input_axis;
|
|
|
|
struct Joystick input_js;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputRecord {
|
2024-04-22 22:13:13 +00:00
|
|
|
Time time;
|
2024-04-18 05:04:44 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct Button but;
|
|
|
|
struct Axis axis;
|
|
|
|
struct Joystick js;
|
|
|
|
union InputData data;
|
|
|
|
};
|
2024-04-18 21:20:44 +00:00
|
|
|
|
2024-04-23 20:33:32 +00:00
|
|
|
union InputID id;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u8f is_down : 1;
|
|
|
|
u8f is_held : 1;
|
|
|
|
u8f is_up : 1;
|
|
|
|
};
|
|
|
|
};
|
2024-04-18 21:20:44 +00:00
|
|
|
|
2024-04-25 07:04:43 +00:00
|
|
|
struct RecordBuffer {
|
2024-04-23 20:33:32 +00:00
|
|
|
struct InputRecord buf[IO_BUF_SIZE];
|
|
|
|
u8f len;
|
2024-04-24 23:37:47 +00:00
|
|
|
u8f start;
|
2024-04-23 20:33:32 +00:00
|
|
|
};
|
|
|
|
|
2024-04-25 07:04:43 +00:00
|
|
|
struct StringBuffer {
|
2024-04-23 20:33:32 +00:00
|
|
|
char buf[STR_BUF_SIZE];
|
2024-04-18 21:20:44 +00:00
|
|
|
u8f len;
|
2024-04-24 23:37:47 +00:00
|
|
|
u8f start;
|
2024-04-18 05:04:44 +00:00
|
|
|
};
|
|
|
|
|
2024-04-19 20:23:11 +00:00
|
|
|
struct InputThreadHandle {
|
2024-04-25 07:04:43 +00:00
|
|
|
struct RecordBuffer *in;
|
|
|
|
struct StringBuffer *str;
|
2024-04-19 20:23:11 +00:00
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_mutex_t mutex;
|
2024-04-25 07:04:43 +00:00
|
|
|
pthread_cond_t consume;
|
2024-04-19 20:23:11 +00:00
|
|
|
|
|
|
|
int err;
|
|
|
|
bool is_terminating;
|
|
|
|
};
|
|
|
|
|
2024-04-25 07:04:43 +00:00
|
|
|
void InputBufferTransfer(struct RecordBuffer *tmp, struct RecordBuffer *dest);
|
2024-04-18 05:04:44 +00:00
|
|
|
|
2024-04-25 07:04:43 +00:00
|
|
|
bool BeginInputThread(
|
|
|
|
struct InputThreadHandle *hand,
|
|
|
|
struct RecordBuffer *in,
|
|
|
|
struct StringBuffer *str
|
|
|
|
);
|
2024-04-19 20:23:11 +00:00
|
|
|
|
|
|
|
bool EndInputThread(struct InputThreadHandle *hand);
|