2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2018-08-15 22:45:22 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
|
|
|
#include "config.h"
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "lock_util.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
SDL_bool
|
|
|
|
controller_init(struct controller *controller, socket_t video_socket) {
|
2017-12-14 18:38:44 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_destroy(struct controller *controller) {
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_DestroyCond(controller->event_cond);
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
|
|
|
control_event_queue_destroy(&controller->queue);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +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;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +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;
|
|
|
|
}
|
2018-02-16 06:55:52 +08:00
|
|
|
int w = net_send_all(controller->video_socket, serialized_event, length);
|
2017-12-14 18:38:44 +08:00
|
|
|
return w == length;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static int
|
|
|
|
run_controller(void *data) {
|
2017-12-14 18:38:44 +08:00
|
|
|
struct controller *controller = data;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2019-03-03 03:09:56 +08:00
|
|
|
while (!controller->stopped
|
|
|
|
&& control_event_queue_is_empty(&controller->queue)) {
|
2017-12-14 18:38:44 +08:00
|
|
|
cond_wait(controller->event_cond, controller->mutex);
|
|
|
|
}
|
|
|
|
if (controller->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_unlock(controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
struct control_event event;
|
2019-03-03 03:09:56 +08:00
|
|
|
SDL_bool non_empty = control_event_queue_take(&controller->queue,
|
|
|
|
&event);
|
2018-05-26 21:14:46 +08:00
|
|
|
SDL_assert(non_empty);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
|
|
|
|
SDL_bool ok = process_event(controller, &event);
|
|
|
|
control_event_destroy(&event);
|
|
|
|
if (!ok) {
|
|
|
|
LOGD("Cannot write event to socket");
|
|
|
|
break;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
SDL_bool
|
|
|
|
controller_start(struct controller *controller) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGD("Starting controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
controller->thread = SDL_CreateThread(run_controller, "controller",
|
|
|
|
controller);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!controller->thread) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGC("Could not start controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_stop(struct controller *controller) {
|
2017-12-14 18:38:44 +08:00
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
controller->stopped = SDL_TRUE;
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_join(struct controller *controller) {
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_WaitThread(controller->thread, NULL);
|
|
|
|
}
|