2
0
mirror of https://github.com/Genymobile/scrcpy synced 2025-08-31 14:25:56 +00:00

Implement computer-to-device clipboard copy

It was already possible to _paste_ (with Ctrl+v) the content of the
computer clipboard on the device. Technically, it injects a sequence of
events to generate the text.

Add a new feature (Ctrl+Shift+v) to copy to the device clipboard
instead, without injecting the content. Contrary to events injection,
this preserves the UTF-8 content exactly, so the text is not broken by
special characters.

<https://github.com/Genymobile/scrcpy/issues/413>
This commit is contained in:
Romain Vimont
2019-05-30 20:25:23 +02:00
parent 2322069656
commit c13a24389c
12 changed files with 138 additions and 6 deletions

View File

@@ -136,6 +136,29 @@ request_device_clipboard(struct controller *controller) {
}
}
static void
set_device_clipboard(struct controller *controller) {
char *text = SDL_GetClipboardText();
if (!text) {
LOGW("Cannot get clipboard text: %s", SDL_GetError());
return;
}
if (!*text) {
// empty text
SDL_free(text);
return;
}
struct control_event control_event;
control_event.type = CONTROL_EVENT_TYPE_SET_CLIPBOARD;
control_event.set_clipboard_event.text = text;
if (!controller_push_event(controller, &control_event)) {
SDL_free(text);
LOGW("Cannot send clipboard paste event");
}
}
static void
switch_fps_counter_state(struct video_buffer *vb) {
mutex_lock(vb->mutex);
@@ -267,9 +290,15 @@ input_manager_process_key(struct input_manager *input_manager,
}
return;
case SDLK_v:
if (control && ctrl && !meta && !shift && !repeat
if (control && ctrl && !meta && !repeat
&& event->type == SDL_KEYDOWN) {
clipboard_paste(input_manager->controller);
if (shift) {
// store the text in the device clipboard
set_device_clipboard(input_manager->controller);
} else {
// inject the text as input events
clipboard_paste(input_manager->controller);
}
}
return;
case SDLK_f: