-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
/
controller.c
107 lines (90 loc) · 3.1 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "controller.h"
#include "lockutil.h"
#include "log.h"
SDL_bool controller_init(struct controller *controller, socket_t 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);
}
SDL_bool controller_push_event(struct controller *controller, const struct control_event *event) {
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;
}
static SDL_bool process_event(struct controller *controller, const struct control_event *event) {
unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE];
int length = control_event_serialize(event, serialized_event);
if (!length) {
return SDL_FALSE;
}
int w = net_send_all(controller->video_socket, serialized_event, length);
return w == length;
}
static int run_controller(void *data) {
struct controller *controller = data;
for (;;) {
mutex_lock(controller->mutex);
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
mutex_unlock(controller->mutex);
break;
}
struct control_event event;
#ifdef BUILD_DEBUG
bool non_empty = control_event_queue_take(&controller->queue, &event);
SDL_assert(non_empty);
#else
control_event_queue_take(&controller->queue, &event);
#endif
mutex_unlock(controller->mutex);
SDL_bool ok = process_event(controller, &event);
control_event_destroy(&event);
if (!ok) {
LOGD("Cannot write event to socket");
break;
}
}
return 0;
}
SDL_bool controller_start(struct controller *controller) {
LOGD("Starting controller thread");
controller->thread = SDL_CreateThread(run_controller, "controller", controller);
if (!controller->thread) {
LOGC("Could not start controller thread");
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);
}