Fumofumotris/source/io/input.c

68 lines
1.6 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-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-25 20:08:24 +00:00
struct InputRecordBuf tmp_in = { .head.len = 0, .head.start = 0 };
struct InputStringBuf tmp_str = { .head.len = 0, .head.start = 0 };
2024-04-01 22:39:21 +00:00
2024-04-19 20:23:11 +00:00
while (!hand->is_terminating) {
if (!PlatformReadInput(&tmp_in, &tmp_str))
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-19 20:23:11 +00:00
if (hand->err = pthread_mutex_lock(&hand->mutex))
2024-04-22 05:19:54 +00:00
return nullptr;
2024-04-19 20:23:11 +00:00
2024-04-25 20:08:24 +00:00
while (tmp_in.head.len == IO_BUF_SIZE) {
if (hand->err = pthread_cond_wait(&hand->consume, &hand->mutex))
2024-04-22 22:13:13 +00:00
return nullptr;
}
2024-04-25 20:08:24 +00:00
RingBufferTransfer(IO_BUF_T, hand->in, &tmp_in);
RingBufferTransfer(STR_BUF_T, hand->str, &tmp_str);
2024-04-19 20:23:11 +00:00
if (hand->err = pthread_mutex_unlock(&hand->mutex))
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
bool BeginInputThread(
struct InputThreadHandle *hand,
2024-04-25 20:08:24 +00:00
struct InputRecordBuf *in,
struct InputStringBuf *str
) {
*hand = (struct InputThreadHandle) {
.in = in,
.str = str,
.mutex = PTHREAD_MUTEX_INITIALIZER,
.consume = PTHREAD_COND_INITIALIZER,
.err = 0,
.is_terminating = false,
};
2024-04-22 22:13:13 +00:00
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
}