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
|
|
|
|
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) {
|
2019-05-30 03:24:30 +08:00
|
|
|
cbuf_init(&controller->queue);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
bool ok = receiver_init(&controller->receiver, control_socket, acksync);
|
2021-02-01 01:24:35 +08:00
|
|
|
if (!ok) {
|
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) {
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_destroy(&controller->receiver);
|
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) {
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_destroy(&controller->receiver);
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_destroy(&controller->mutex);
|
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
|
|
|
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_control_msg msg;
|
2019-05-31 20:55:11 +08:00
|
|
|
while (cbuf_take(&controller->queue, &msg)) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_control_msg_destroy(&msg);
|
2019-05-30 03:24:30 +08:00
|
|
|
}
|
2019-05-30 06:25:37 +08:00
|
|
|
|
|
|
|
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);
|
2019-05-30 03:24:30 +08:00
|
|
|
bool was_empty = cbuf_is_empty(&controller->queue);
|
2019-05-31 20:55:11 +08:00
|
|
|
bool res = cbuf_push(&controller->queue, *msg);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (was_empty) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&controller->msg_cond);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-05-30 03:24:30 +08:00
|
|
|
while (!controller->stopped && cbuf_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;
|
|
|
|
}
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_control_msg msg;
|
2019-05-31 20:55:11 +08:00
|
|
|
bool non_empty = cbuf_take(&controller->queue, &msg);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(non_empty);
|
|
|
|
(void) non_empty;
|
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
|
|
|
}
|
|
|
|
|
2019-05-30 06:25:37 +08:00
|
|
|
if (!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);
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_join(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|