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