2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2019-03-03 06:52:22 +08:00
|
|
|
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2023-03-02 05:39:11 +08:00
|
|
|
#define SC_CONTROL_MSG_QUEUE_MAX 64
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_init(struct sc_controller *controller, sc_socket control_socket,
|
|
|
|
struct sc_acksync *acksync) {
|
2023-03-02 05:39:11 +08:00
|
|
|
sc_vecdeque_init(&controller->queue);
|
|
|
|
|
|
|
|
bool ok = sc_vecdeque_reserve(&controller->queue, SC_CONTROL_MSG_QUEUE_MAX);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2023-03-02 05:39:11 +08:00
|
|
|
ok = sc_receiver_init(&controller->receiver, control_socket, acksync);
|
2021-02-01 01:24:35 +08:00
|
|
|
if (!ok) {
|
2023-03-02 05:39:11 +08:00
|
|
|
sc_vecdeque_destroy(&controller->queue);
|
2019-05-30 06:25:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
ok = sc_mutex_init(&controller->mutex);
|
|
|
|
if (!ok) {
|
2023-02-18 16:37:13 +08:00
|
|
|
sc_receiver_destroy(&controller->receiver);
|
2023-03-02 05:39:11 +08:00
|
|
|
sc_vecdeque_destroy(&controller->queue);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
ok = sc_cond_init(&controller->msg_cond);
|
|
|
|
if (!ok) {
|
2023-02-18 16:37:13 +08:00
|
|
|
sc_receiver_destroy(&controller->receiver);
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_destroy(&controller->mutex);
|
2023-03-02 05:39:11 +08:00
|
|
|
sc_vecdeque_destroy(&controller->queue);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 03:03:54 +08:00
|
|
|
controller->control_socket = control_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
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_destroy(struct sc_controller *controller) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_destroy(&controller->msg_cond);
|
|
|
|
sc_mutex_destroy(&controller->mutex);
|
2019-05-30 03:24:30 +08:00
|
|
|
|
2023-03-02 05:39:11 +08:00
|
|
|
while (!sc_vecdeque_is_empty(&controller->queue)) {
|
|
|
|
struct sc_control_msg *msg = sc_vecdeque_popref(&controller->queue);
|
|
|
|
assert(msg);
|
|
|
|
sc_control_msg_destroy(msg);
|
2019-05-30 03:24:30 +08:00
|
|
|
}
|
2023-03-02 05:39:11 +08:00
|
|
|
sc_vecdeque_destroy(&controller->queue);
|
2019-05-30 06:25:37 +08:00
|
|
|
|
2023-02-18 16:37:13 +08:00
|
|
|
sc_receiver_destroy(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_push_msg(struct sc_controller *controller,
|
2022-01-15 05:17:30 +08:00
|
|
|
const struct sc_control_msg *msg) {
|
2021-06-20 18:54:09 +08:00
|
|
|
if (sc_get_log_level() <= SC_LOG_LEVEL_VERBOSE) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_control_msg_log(msg);
|
2021-06-20 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&controller->mutex);
|
2023-03-02 05:39:11 +08:00
|
|
|
bool full = sc_vecdeque_is_full(&controller->queue);
|
|
|
|
if (!full) {
|
|
|
|
bool was_empty = sc_vecdeque_is_empty(&controller->queue);
|
|
|
|
sc_vecdeque_push_noresize(&controller->queue, *msg);
|
|
|
|
if (was_empty) {
|
|
|
|
sc_cond_signal(&controller->msg_cond);
|
|
|
|
}
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
2023-03-02 05:39:11 +08:00
|
|
|
// Otherwise (if the queue is full), the msg is discarded
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&controller->mutex);
|
2023-03-02 05:39:11 +08:00
|
|
|
|
|
|
|
return !full;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2022-01-15 05:17:30 +08:00
|
|
|
process_msg(struct sc_controller *controller,
|
|
|
|
const struct sc_control_msg *msg) {
|
2022-01-27 04:31:30 +08:00
|
|
|
static unsigned char serialized_msg[SC_CONTROL_MSG_MAX_SIZE];
|
2022-01-15 05:17:30 +08:00
|
|
|
size_t length = sc_control_msg_serialize(msg, serialized_msg);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!length) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
2021-10-22 00:36:16 +08:00
|
|
|
ssize_t w =
|
|
|
|
net_send_all(controller->control_socket, serialized_msg, length);
|
2021-01-17 21:22:23 +08:00
|
|
|
return (size_t) w == length;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static int
|
|
|
|
run_controller(void *data) {
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_controller *controller = data;
|
2017-12-14 18:38:44 +08:00
|
|
|
|
|
|
|
for (;;) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&controller->mutex);
|
2023-03-02 05:39:11 +08:00
|
|
|
while (!controller->stopped
|
|
|
|
&& sc_vecdeque_is_empty(&controller->queue)) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_wait(&controller->msg_cond, &controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
if (controller->stopped) {
|
2019-05-31 20:55:11 +08:00
|
|
|
// stop immediately, do not process further msgs
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2023-03-02 05:39:11 +08:00
|
|
|
|
|
|
|
assert(!sc_vecdeque_is_empty(&controller->queue));
|
|
|
|
struct sc_control_msg msg = sc_vecdeque_pop(&controller->queue);
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&controller->mutex);
|
2018-05-26 21:14:46 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
bool ok = process_msg(controller, &msg);
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_control_msg_destroy(&msg);
|
2018-05-26 21:14:46 +08:00
|
|
|
if (!ok) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGD("Could not write msg to socket");
|
2018-05-26 21:14:46 +08:00
|
|
|
break;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_start(struct sc_controller *controller) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGD("Starting controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool ok = sc_thread_create(&controller->thread, run_controller,
|
2021-12-10 04:32:11 +08:00
|
|
|
"scrcpy-ctl", controller);
|
2021-02-01 01:24:35 +08:00
|
|
|
if (!ok) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not start controller thread");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 16:37:13 +08:00
|
|
|
if (!sc_receiver_start(&controller->receiver)) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_stop(controller);
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_thread_join(&controller->thread, NULL);
|
2019-05-30 06:25:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_stop(struct sc_controller *controller) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&controller->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
controller->stopped = true;
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&controller->msg_cond);
|
|
|
|
sc_mutex_unlock(&controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_join(struct sc_controller *controller) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_thread_join(&controller->thread, NULL);
|
2023-02-18 16:37:13 +08:00
|
|
|
sc_receiver_join(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|