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 "fumotris.h"
|
2024-04-16 22:14:53 +00:00
|
|
|
#include "platform.h"
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-16 22:14:53 +00:00
|
|
|
#define IO_BUF_SIZE 16
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-04-16 22:14:53 +00:00
|
|
|
enum InputType {
|
|
|
|
BUTTON,
|
|
|
|
AXIS,
|
|
|
|
JOYSTICK,
|
|
|
|
ESCAPE
|
|
|
|
};
|
2024-04-10 20:05:56 +00:00
|
|
|
|
2024-04-16 22:14:53 +00:00
|
|
|
struct Button {
|
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
struct Axis {
|
|
|
|
i64 value;
|
|
|
|
};
|
|
|
|
struct Joystick {
|
|
|
|
i32 x;
|
|
|
|
i32 y;
|
|
|
|
};
|
|
|
|
union InputData {
|
|
|
|
struct Button input_but;
|
|
|
|
struct Axis input_axis;
|
|
|
|
struct Joystick input_js;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputRecord {
|
|
|
|
u16f bind;
|
|
|
|
struct timespec timestamp;
|
|
|
|
|
|
|
|
u8 type;
|
|
|
|
u8 is_down;
|
|
|
|
u8 is_held;
|
|
|
|
u8 is_up;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct Button but;
|
|
|
|
struct Axis axis;
|
|
|
|
struct Joystick js;
|
|
|
|
union InputData data;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputBuffer {
|
|
|
|
size_t len;
|
|
|
|
size_t start;
|
|
|
|
struct InputRecord records[IO_BUF_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputRecord *in_buf_get(struct InputBuffer *buf, size_t i)
|
|
|
|
{
|
|
|
|
return buf->records + (buf->start + 1) % IO_BUF_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest)
|
|
|
|
{
|
|
|
|
size_t n = IO_BUF_SIZE - (tmp->len > dest->len ? tmp->len : dest->len);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
|
|
|
*in_buf_get(dest, dest->len + i) = *in_buf_get(tmp, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < tmp->len)
|
|
|
|
tmp->start += n;
|
|
|
|
|
|
|
|
tmp->len -= n;
|
|
|
|
dest->len += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src)
|
|
|
|
{
|
|
|
|
buf->records[(buf->start + buf->len) % IO_BUF_SIZE] = *src;
|
|
|
|
buf->len += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct input_args {
|
|
|
|
struct InputBuffer *buf;
|
|
|
|
pthread_mutex_t *mutex;
|
|
|
|
};
|
|
|
|
|
2024-04-17 21:05:18 +00:00
|
|
|
void *block_input(struct input_args *args)
|
2024-04-16 22:14:53 +00:00
|
|
|
{
|
2024-04-18 05:04:44 +00:00
|
|
|
struct InputBuffer *buf = args->buf;
|
|
|
|
pthread_mutex_t *mutex = args->mutex;
|
|
|
|
free(args);
|
2024-04-16 22:14:53 +00:00
|
|
|
|
|
|
|
struct InputBuffer tmp_buf = { .len = 0, .start = 0 };
|
2024-04-01 22:39:21 +00:00
|
|
|
|
|
|
|
while (true) {
|
2024-04-16 22:14:53 +00:00
|
|
|
if (!PlatformReadInput(&tmp_buf))
|
2024-04-10 20:05:56 +00:00
|
|
|
return false;
|
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
pthread_mutex_lock(mutex);
|
2024-04-15 19:29:51 +00:00
|
|
|
{
|
2024-04-18 05:04:44 +00:00
|
|
|
InputBufferTransfer(&tmp_buf, buf);
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
2024-04-18 05:04:44 +00:00
|
|
|
pthread_mutex_unlock(mutex);
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:14:53 +00:00
|
|
|
bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-18 05:04:44 +00:00
|
|
|
struct input_args *args = malloc(sizeof(struct input_args));
|
|
|
|
*args = (struct input_args) {
|
2024-04-16 22:14:53 +00:00
|
|
|
.buf = buf,
|
|
|
|
.mutex = mutex,
|
|
|
|
};
|
|
|
|
|
2024-04-18 05:04:44 +00:00
|
|
|
pthread_t thread;
|
|
|
|
return pthread_create(&thread, nullptr, block_input, args) == 0;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|