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 "ctrl.h"
|
|
|
|
#include "fumotris.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "win.h"
|
|
|
|
#endif
|
|
|
|
|
2024-04-10 20:05:56 +00:00
|
|
|
struct Input {
|
|
|
|
struct Controller ctrl;
|
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_mutex_t access_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InputRecord *get_at(struct InputBuffer *buf, size_t i)
|
|
|
|
{
|
|
|
|
return &buf->records[(buf->start + i) % IO_BUF_SIZE];
|
|
|
|
}
|
|
|
|
|
2024-03-26 20:11:58 +00:00
|
|
|
void *block_input(void *args_ptr)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-10 20:05:56 +00:00
|
|
|
struct Input *in = args_ptr;
|
|
|
|
struct InputBuffer tmp_buf = { .len = 0 };
|
2024-04-01 22:39:21 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
#ifdef _WIN32
|
2024-04-10 20:05:56 +00:00
|
|
|
if (!WindowsReadInputBuffer(&tmp_buf))
|
|
|
|
return false;
|
2024-04-01 22:39:21 +00:00
|
|
|
#endif
|
2024-04-10 20:05:56 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&in->access_mutex);
|
|
|
|
for (size_t i = 0; i < tmp_buf.len; i++) {
|
|
|
|
*get_at(&in->ctrl.input_buf, i) = *get_at();
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
2024-04-10 20:05:56 +00:00
|
|
|
pthread_mutex_unlock(&in->access_mutex);
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-10 20:05:56 +00:00
|
|
|
bool StartInput(struct Input *in)
|
2024-04-01 22:39:21 +00:00
|
|
|
{
|
2024-04-10 20:05:56 +00:00
|
|
|
return pthread_create(&in->thread, nullptr, block_input, in) == 0;
|
2024-04-01 22:39:21 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 20:05:56 +00:00
|
|
|
bool JoinInput(struct Input *in)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
2024-04-10 20:05:56 +00:00
|
|
|
return pthread_join(in->thread, nullptr) == 0;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|