2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
|
|
|
#include "lockutil.h"
|
|
|
|
|
|
|
|
SDL_bool controller_init(struct controller *controller, TCPsocket video_socket) {
|
|
|
|
if (!control_event_queue_init(&controller->queue)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(controller->mutex = SDL_CreateMutex())) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(controller->event_cond = SDL_CreateCond())) {
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->video_socket = video_socket;
|
|
|
|
controller->stopped = SDL_FALSE;
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_destroy(struct controller *controller) {
|
|
|
|
SDL_DestroyCond(controller->event_cond);
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
|
|
|
control_event_queue_destroy(&controller->queue);
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:12:22 +08:00
|
|
|
SDL_bool controller_push_event(struct controller *controller, const struct control_event *event) {
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_bool res;
|
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
SDL_bool was_empty = control_event_queue_is_empty(&controller->queue);
|
|
|
|
res = control_event_queue_push(&controller->queue, event);
|
|
|
|
if (was_empty) {
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
}
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:12:22 +08:00
|
|
|
static SDL_bool process_event(struct controller *controller, const struct control_event *event) {
|
2017-12-14 18:38:44 +08:00
|
|
|
unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE];
|
|
|
|
int length = control_event_serialize(event, serialized_event);
|
|
|
|
if (!length) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
int w = SDLNet_TCP_Send(controller->video_socket, serialized_event, length);
|
|
|
|
return w == length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_controller(void *data) {
|
|
|
|
struct controller *controller = data;
|
|
|
|
|
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
for (;;) {
|
|
|
|
while (!controller->stopped && control_event_queue_is_empty(&controller->queue)) {
|
|
|
|
cond_wait(controller->event_cond, controller->mutex);
|
|
|
|
}
|
|
|
|
if (controller->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
struct control_event event;
|
|
|
|
while (control_event_queue_take(&controller->queue, &event)) {
|
|
|
|
if (!process_event(controller, &event)) {
|
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Cannot write event to socket");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool controller_start(struct controller *controller) {
|
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Starting controller thread");
|
|
|
|
|
|
|
|
controller->thread = SDL_CreateThread(run_controller, "controller", controller);
|
|
|
|
if (!controller->thread) {
|
2018-02-12 23:06:53 +08:00
|
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Could not start controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_stop(struct controller *controller) {
|
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
controller->stopped = SDL_TRUE;
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_join(struct controller *controller) {
|
|
|
|
SDL_WaitThread(controller->thread, NULL);
|
|
|
|
}
|