Fumofumotris/source/io/input.c

100 lines
2.5 KiB
C
Raw Normal View History

#include "input.h"
2024-04-23 20:33:32 +00:00
#include <string.h>
2024-03-25 05:34:59 +00:00
2024-04-16 22:14:53 +00:00
#include "platform.h"
2024-03-25 05:34:59 +00:00
2024-04-23 20:33:32 +00:00
inline size_t min_size(size_t a, size_t b) {
return a < b ? a : b;
2024-04-19 06:38:45 +00:00
}
void InputBufferTransfer(struct InputBuffer *dest, struct InputBuffer *src)
2024-04-16 22:14:53 +00:00
{
2024-04-24 23:37:47 +00:00
size_t copy_amt = min_size(IO_BUF_SIZE - dest->len, src->len);
2024-04-16 22:14:53 +00:00
2024-04-24 23:37:47 +00:00
for (size_t i = 0; i < dest->start; i++) {
size_t dest_i = (dest->start + dest->len + i) % IO_BUF_SIZE;
size_t src_i = (src->start + i) % IO_BUF_SIZE;
2024-04-23 20:33:32 +00:00
2024-04-24 23:37:47 +00:00
dest->buf[dest_i] = src->buf[src_i];
}
2024-04-23 20:33:32 +00:00
2024-04-24 23:37:47 +00:00
dest->start += copy_amt;
if (copy_amt < src->len)
src->len -= copy_amt;
2024-04-23 20:33:32 +00:00
}
void InputStringTransfer(struct InputString *dest, struct InputString *src)
{
2024-04-24 23:37:47 +00:00
size_t copy_amt = min_size(STR_BUF_SIZE - dest->len, src->len);
2024-04-23 20:33:32 +00:00
for (size_t i = 0; i < copy_amt; i++) {
2024-04-24 23:37:47 +00:00
size_t dest_i = (dest->start + dest->len + i) % STR_BUF_SIZE;
size_t src_i = (src->start + i) % STR_BUF_SIZE;
dest->buf[dest_i] = src->buf[src_i];
2024-04-23 20:33:32 +00:00
}
2024-04-24 23:37:47 +00:00
dest->start += copy_amt;
if (copy_amt < src->len)
src->len -= copy_amt;
2024-04-16 22:14:53 +00:00
}
2024-04-22 05:19:54 +00:00
void *input_thread_loop(void *arg)
2024-04-16 22:14:53 +00:00
{
2024-04-22 05:19:54 +00:00
struct InputThreadHandle *hand = arg;
2024-04-23 20:33:32 +00:00
2024-04-24 23:37:47 +00:00
struct InputBuffer tmp_in = { .len = 0 };
struct InputString tmp_str = { .len = 0 };
2024-04-01 22:39:21 +00:00
2024-04-19 20:23:11 +00:00
while (!hand->is_terminating) {
2024-04-23 20:33:32 +00:00
if (!PlatformReadInput(&tmp_in, &tmp_str)) {
2024-04-19 20:23:11 +00:00
hand->err = true;
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-01 22:39:21 +00:00
}
2024-04-19 20:23:11 +00:00
hand->err = pthread_mutex_lock(&hand->mutex);
if (hand->err)
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-19 20:23:11 +00:00
2024-04-23 20:33:32 +00:00
while (tmp_in.len == IO_BUF_SIZE) {
2024-04-22 22:13:13 +00:00
hand->err = pthread_cond_wait(&hand->buf_read, &hand->mutex);
if (hand->err)
return nullptr;
}
2024-04-23 20:33:32 +00:00
InputBufferTransfer(hand->in, &tmp_in);
InputStringTransfer(hand->str, &tmp_str);
2024-04-19 20:23:11 +00:00
hand->err = pthread_mutex_unlock(&hand->mutex);
if (hand->err)
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-01 22:39:21 +00:00
}
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-19 20:23:11 +00:00
}
2024-03-25 05:34:59 +00:00
2024-04-19 20:23:11 +00:00
bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf)
{
2024-04-23 20:33:32 +00:00
hand->in = buf;
2024-04-19 20:23:11 +00:00
hand->mutex = PTHREAD_MUTEX_INITIALIZER;
2024-04-22 22:13:13 +00:00
hand->buf_read = PTHREAD_COND_INITIALIZER;
hand->err = 0;
2024-04-19 20:23:11 +00:00
return pthread_create(&hand->thread, nullptr, input_thread_loop, hand) == 0;
2024-03-25 05:34:59 +00:00
}
2024-04-19 20:23:11 +00:00
bool EndInputThread(struct InputThreadHandle *hand)
2024-03-25 05:34:59 +00:00
{
2024-04-19 20:23:11 +00:00
hand->is_terminating = true;
if (!PlatformStopInput())
return false;
if (!pthread_mutex_destroy(&hand->mutex))
return false;
if (!pthread_join(hand->thread, nullptr))
return false;
return true;
2024-03-25 05:34:59 +00:00
}