Skip to content

Commit

Permalink
Add option to disable clipboard autosync
Browse files Browse the repository at this point in the history
By default, scrcpy automatically synchronizes the computer clipboard to
the device clipboard before injecting Ctrl+v, and the device clipboard
to the computer clipboard whenever it changes.

This new option --no-clipboard-autosync disables this automatic
synchronization.

Fixes #2228 <#2228>
  • Loading branch information
rom1v committed Nov 22, 2021
1 parent df5b5e8 commit 35b7b4a
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,9 @@ of <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>MOD</kbd>+<kbd>v</kbd> so that they
also inject the computer clipboard text as a sequence of key events (the same
way as <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).

To disable automatic clipboard synchronization, use
`--no-clipboard-autosync`.

#### Pinch-to-zoom

To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
Expand Down
6 changes: 6 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Limit both the width and height of the video to \fIvalue\fR. The other dimension

Default is 0 (unlimited).

.TP
.B \-\-no\-clipboard\-autosync
By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.

This option disables this automatic synchronization.

.TP
.B \-n, \-\-no\-control
Disable device control (mirror the device in read\-only).
Expand Down
13 changes: 13 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#define OPT_V4L2_BUFFER 1029
#define OPT_TUNNEL_HOST 1030
#define OPT_TUNNEL_PORT 1031
#define OPT_NO_CLIPBOARD_AUTOSYNC 1032

struct sc_option {
char shortopt;
Expand Down Expand Up @@ -208,6 +209,15 @@ static const struct sc_option options[] = {
"is preserved.\n"
"Default is 0 (unlimited).",
},
{
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
.longopt = "no-clipboard-autosync",
.text = "By default, scrcpy automatically synchronizes the computer "
"clipboard to the device clipboard before injecting Ctrl+v, "
"and the device clipboard to the computer clipboard whenever "
"it changes.\n"
"This option disables this automatic synchronization."
},
{
.shortopt = 'n',
.longopt = "no-control",
Expand Down Expand Up @@ -1364,6 +1374,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case OPT_NO_CLIPBOARD_AUTOSYNC:
opts->clipboard_autosync = false;
break;
#ifdef HAVE_V4L2
case OPT_V4L2_SINK:
opts->v4l2_device = optarg;
Expand Down
3 changes: 2 additions & 1 deletion app/src/input_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ input_manager_init(struct input_manager *im, struct controller *controller,
im->control = options->control;
im->forward_all_clicks = options->forward_all_clicks;
im->legacy_paste = options->legacy_paste;
im->clipboard_autosync = options->clipboard_autosync;

const struct sc_shortcut_mods *shortcut_mods = &options->shortcut_mods;
assert(shortcut_mods->count);
Expand Down Expand Up @@ -517,7 +518,7 @@ input_manager_process_key(struct input_manager *im,

uint64_t ack_to_wait = SC_SEQUENCE_INVALID;
bool is_ctrl_v = ctrl && !shift && keycode == SDLK_v && down && !repeat;
if (is_ctrl_v) {
if (im->clipboard_autosync && is_ctrl_v) {
if (im->legacy_paste) {
// inject the text as input events
clipboard_paste(controller);
Expand Down
1 change: 1 addition & 0 deletions app/src/input_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct input_manager {
bool control;
bool forward_all_clicks;
bool legacy_paste;
bool clipboard_autosync;

struct {
unsigned data[SC_MAX_SHORTCUT_MODS];
Expand Down
1 change: 1 addition & 0 deletions app/src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ const struct scrcpy_options scrcpy_options_default = {
.forward_all_clicks = false,
.legacy_paste = false,
.power_off_on_close = false,
.clipboard_autosync = true,
};
1 change: 1 addition & 0 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct scrcpy_options {
bool forward_all_clicks;
bool legacy_paste;
bool power_off_on_close;
bool clipboard_autosync;
};

extern const struct scrcpy_options scrcpy_options_default;
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ scrcpy(struct scrcpy_options *options) {
.encoder_name = options->encoder_name,
.force_adb_forward = options->force_adb_forward,
.power_off_on_close = options->power_off_on_close,
.clipboard_autosync = options->clipboard_autosync,
};

static const struct sc_server_callbacks cbs = {
Expand Down
1 change: 1 addition & 0 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ execute_server(struct sc_server *server,
params->codec_options ? params->codec_options : "-",
params->encoder_name ? params->encoder_name : "-",
params->power_off_on_close ? "true" : "false",
params->clipboard_autosync ? "true" : "false",
};
#ifdef SERVER_DEBUGGER
LOGI("Server debugger waiting for a client on device port "
Expand Down
1 change: 1 addition & 0 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct sc_server_params {
bool stay_awake;
bool force_adb_forward;
bool power_off_on_close;
bool clipboard_autosync;
};

struct sc_server {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void onRotationChanged(int rotation) {
}
}, displayId);

if (options.getControl()) {
// If control is enabled, synchronize Android clipboard to the computer automatically
if (options.getControl() && options.getClipboardAutosync()) {
// If control and autosync are enabled, synchronize Android clipboard to the computer automatically
ClipboardManager clipboardManager = SERVICE_MANAGER.getClipboardManager();
if (clipboardManager != null) {
clipboardManager.addPrimaryClipChangedListener(new IOnPrimaryClipChangedListener.Stub() {
Expand Down
9 changes: 9 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Options {
private String codecOptions;
private String encoderName;
private boolean powerOffScreenOnClose;
private boolean clipboardAutosync;

public Ln.Level getLogLevel() {
return logLevel;
Expand Down Expand Up @@ -138,4 +139,12 @@ public void setPowerOffScreenOnClose(boolean powerOffScreenOnClose) {
public boolean getPowerOffScreenOnClose() {
return this.powerOffScreenOnClose;
}

public boolean getClipboardAutosync() {
return clipboardAutosync;
}

public void setClipboardAutosync(boolean clipboardAutosync) {
this.clipboardAutosync = clipboardAutosync;
}
}
5 changes: 4 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static Options createOptions(String... args) {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
}

final int expectedParameters = 16;
final int expectedParameters = 17;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Expand Down Expand Up @@ -213,6 +213,9 @@ private static Options createOptions(String... args) {
boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]);
options.setPowerOffScreenOnClose(powerOffScreenOnClose);

boolean clipboardAutosync = Boolean.parseBoolean(args[16]);
options.setClipboardAutosync(clipboardAutosync);

return options;
}

Expand Down

0 comments on commit 35b7b4a

Please sign in to comment.