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>
|
2019-03-03 06:52:22 +08:00
|
|
|
|
2018-08-15 22:45:22 +08:00
|
|
|
#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 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_init(struct controller *controller, socket_t video_socket) {
|
2019-05-30 03:24:30 +08:00
|
|
|
cbuf_init(&controller->queue);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
|
|
|
if (!(controller->mutex = SDL_CreateMutex())) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(controller->event_cond = SDL_CreateCond())) {
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
controller->video_socket = video_socket;
|
2019-03-03 06:52:22 +08:00
|
|
|
controller->stopped = false;
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2019-05-30 03:24:30 +08:00
|
|
|
|
|
|
|
struct control_event event;
|
|
|
|
while (cbuf_take(&controller->queue, &event)) {
|
|
|
|
control_event_destroy(&event);
|
|
|
|
}
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_push_event(struct controller *controller,
|
|
|
|
const struct control_event *event) {
|
2017-12-14 18:38:44 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2019-05-30 03:24:30 +08:00
|
|
|
bool was_empty = cbuf_is_empty(&controller->queue);
|
|
|
|
bool res = cbuf_push(&controller->queue, *event);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (was_empty) {
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
}
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
process_event(struct controller *controller,
|
|
|
|
const struct control_event *event) {
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char serialized_event[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2017-12-14 18:38:44 +08:00
|
|
|
int length = control_event_serialize(event, serialized_event);
|
|
|
|
if (!length) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
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-05-30 03:24:30 +08:00
|
|
|
while (!controller->stopped && cbuf_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-05-30 03:24:30 +08:00
|
|
|
bool non_empty = cbuf_take(&controller->queue, &event);
|
2018-05-26 21:14:46 +08:00
|
|
|
SDL_assert(non_empty);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool ok = process_event(controller, &event);
|
2018-05-26 21:14:46 +08:00
|
|
|
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 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
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");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2019-03-03 06:52:22 +08:00
|
|
|
controller->stopped = true;
|
2017-12-14 18:38:44 +08:00
|
|
|
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);
|
|
|
|
}
|