Press COPY on "get clipboard" request if possible

If the device runs at least Android 7, just press COPY on the device
(the clipboard content will be sent via the clipboard listener).

<https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_COPY>
This commit is contained in:
Romain Vimont
2020-05-29 22:46:08 +02:00
parent 31cee2c49f
commit 9badd2bdf0
11 changed files with 65 additions and 19 deletions

View File

@@ -277,7 +277,7 @@ Collapse notification panel
.TP
.B Meta+c
Copy device clipboard to computer
Press COPY (Android >= 7), then copy device clipboard to computer
.TP
.B Meta+v

View File

@@ -245,7 +245,8 @@ scrcpy_print_usage(const char *arg0) {
" Collapse notification panel\n"
"\n"
" " MOD "+c\n"
" Copy device clipboard to computer\n"
" Press COPY (Android >= 7), then copy device clipboard to\n"
" computer\n"
"\n"
" " MOD "+v\n"
" Paste computer clipboard to device\n"

View File

@@ -66,6 +66,9 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
buffer_write32be(&buf[17],
(uint32_t) msg->inject_scroll_event.vscroll);
return 21;
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
buf[1] = msg->get_clipboard.copy;
return 2;
case CONTROL_MSG_TYPE_SET_CLIPBOARD: {
buf[1] = !!msg->set_clipboard.paste;
size_t len = write_string(msg->set_clipboard.text,
@@ -79,7 +82,6 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_COLLAPSE_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
// no additional data
return 1;

View File

@@ -60,6 +60,9 @@ struct control_msg {
int32_t hscroll;
int32_t vscroll;
} inject_scroll_event;
struct {
bool copy;
} get_clipboard;
struct {
char *text; // owned, to be freed by SDL_free()
bool paste;

View File

@@ -102,9 +102,10 @@ collapse_notification_panel(struct controller *controller) {
}
static void
request_device_clipboard(struct controller *controller) {
request_device_clipboard(struct controller *controller, bool copy) {
struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_GET_CLIPBOARD;
msg.get_clipboard.copy = copy;
if (!controller_push_msg(controller, &msg)) {
LOGW("Could not request device clipboard");
@@ -341,7 +342,8 @@ input_manager_process_key(struct input_manager *im,
return;
case SDLK_c:
if (control && !shift && !repeat && down) {
request_device_clipboard(controller);
// Press COPY, then get the clipboard content
request_device_clipboard(controller, true);
}
return;
case SDLK_v:

View File

@@ -185,14 +185,18 @@ static void test_serialize_collapse_notification_panel(void) {
static void test_serialize_get_clipboard(void) {
struct control_msg msg = {
.type = CONTROL_MSG_TYPE_GET_CLIPBOARD,
.get_clipboard = {
.copy = true,
},
};
unsigned char buf[CONTROL_MSG_SERIALIZED_MAX_SIZE];
int size = control_msg_serialize(&msg, buf);
assert(size == 1);
assert(size == 2);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_GET_CLIPBOARD,
1, // copy
};
assert(!memcmp(buf, expected, sizeof(expected)));
}