Add --no-cleanup option

It might be useful not to cleanup on exit, for example to leave the
screen turned off, or keep the server binary on the device (via the
server option "cleanup=false").

Fixes #1764 <https://github.com/Genymobile/scrcpy/issues/1764>
PR #3020 <https://github.com/Genymobile/scrcpy/pull/3020>
This commit is contained in:
Romain Vimont 2022-02-13 17:17:01 +01:00
parent bb991f829c
commit ccbe370cc5
9 changed files with 46 additions and 5 deletions

View file

@ -146,6 +146,12 @@ It may only work over USB, and is currently only supported on Linux.
Also see \fB\-\-hid\-keyboard\fR. Also see \fB\-\-hid\-keyboard\fR.
.TP
.B \-\-no\-cleanup
By default, scrcpy removes the server binary from the device and restores the device state (show touches, stay awake and power mode) on exit.
This option disables this cleanup.
.TP .TP
.B \-\-no\-clipboard\-autosync .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. 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.

View file

@ -54,6 +54,7 @@
#define OPT_RAW_KEY_EVENTS 1034 #define OPT_RAW_KEY_EVENTS 1034
#define OPT_NO_DOWNSIZE_ON_ERROR 1035 #define OPT_NO_DOWNSIZE_ON_ERROR 1035
#define OPT_OTG 1036 #define OPT_OTG 1036
#define OPT_NO_CLEANUP 1037
struct sc_option { struct sc_option {
char shortopt; char shortopt;
@ -250,6 +251,14 @@ static const struct sc_option options[] = {
"is preserved.\n" "is preserved.\n"
"Default is 0 (unlimited).", "Default is 0 (unlimited).",
}, },
{
.longopt_id = OPT_NO_CLEANUP,
.longopt = "no-cleanup",
.text = "By default, scrcpy removes the server binary from the device "
"and restores the device state (show touches, stay awake and "
"power mode) on exit.\n"
"This option disables this cleanup."
},
{ {
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC, .longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
.longopt = "no-clipboard-autosync", .longopt = "no-clipboard-autosync",
@ -1535,6 +1544,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
case OPT_NO_DOWNSIZE_ON_ERROR: case OPT_NO_DOWNSIZE_ON_ERROR:
opts->downsize_on_error = false; opts->downsize_on_error = false;
break; break;
case OPT_NO_CLEANUP:
opts->cleanup = false;
break;
case OPT_OTG: case OPT_OTG:
#ifdef HAVE_USB #ifdef HAVE_USB
opts->otg = true; opts->otg = true;

View file

@ -62,4 +62,5 @@ const struct scrcpy_options scrcpy_options_default = {
.tcpip_dst = NULL, .tcpip_dst = NULL,
.select_tcpip = false, .select_tcpip = false,
.select_usb = false, .select_usb = false,
.cleanup = true,
}; };

View file

@ -137,6 +137,7 @@ struct scrcpy_options {
const char *tcpip_dst; const char *tcpip_dst;
bool select_usb; bool select_usb;
bool select_tcpip; bool select_tcpip;
bool cleanup;
}; };
extern const struct scrcpy_options scrcpy_options_default; extern const struct scrcpy_options scrcpy_options_default;

View file

@ -320,6 +320,7 @@ scrcpy(struct scrcpy_options *options) {
.downsize_on_error = options->downsize_on_error, .downsize_on_error = options->downsize_on_error,
.tcpip = options->tcpip, .tcpip = options->tcpip,
.tcpip_dst = options->tcpip_dst, .tcpip_dst = options->tcpip_dst,
.cleanup = options->cleanup,
}; };
static const struct sc_server_callbacks cbs = { static const struct sc_server_callbacks cbs = {

View file

@ -244,6 +244,10 @@ execute_server(struct sc_server *server,
// By default, downsize_on_error is true // By default, downsize_on_error is true
ADD_PARAM("downsize_on_error=false"); ADD_PARAM("downsize_on_error=false");
} }
if (!params->cleanup) {
// By default, cleanup is true
ADD_PARAM("cleanup=false");
}
#undef ADD_PARAM #undef ADD_PARAM

View file

@ -46,6 +46,7 @@ struct sc_server_params {
const char *tcpip_dst; const char *tcpip_dst;
bool select_usb; bool select_usb;
bool select_tcpip; bool select_tcpip;
bool cleanup;
}; };
struct sc_server { struct sc_server {

View file

@ -21,6 +21,7 @@ public class Options {
private boolean powerOffScreenOnClose; private boolean powerOffScreenOnClose;
private boolean clipboardAutosync = true; private boolean clipboardAutosync = true;
private boolean downsizeOnError = true; private boolean downsizeOnError = true;
private boolean cleanup = true;
// Options not used by the scrcpy client, but useful to use scrcpy-server directly // Options not used by the scrcpy client, but useful to use scrcpy-server directly
private boolean sendDeviceMeta = true; // send device name and size private boolean sendDeviceMeta = true; // send device name and size
@ -155,6 +156,14 @@ public class Options {
this.downsizeOnError = downsizeOnError; this.downsizeOnError = downsizeOnError;
} }
public boolean getCleanup() {
return cleanup;
}
public void setCleanup(boolean cleanup) {
this.cleanup = cleanup;
}
public boolean getSendDeviceMeta() { public boolean getSendDeviceMeta() {
return sendDeviceMeta; return sendDeviceMeta;
} }

View file

@ -51,11 +51,13 @@ public final class Server {
} }
} }
try { if (options.getCleanup()) {
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode, try {
options.getPowerOffScreenOnClose()); CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode,
} catch (IOException e) { options.getPowerOffScreenOnClose());
Ln.e("Could not configure cleanup", e); } catch (IOException e) {
Ln.e("Could not configure cleanup", e);
}
} }
} }
@ -243,6 +245,10 @@ public final class Server {
boolean downsizeOnError = Boolean.parseBoolean(value); boolean downsizeOnError = Boolean.parseBoolean(value);
options.setDownsizeOnError(downsizeOnError); options.setDownsizeOnError(downsizeOnError);
break; break;
case "cleanup":
boolean cleanup = Boolean.parseBoolean(value);
options.setCleanup(cleanup);
break;
case "send_device_meta": case "send_device_meta":
boolean sendDeviceMeta = Boolean.parseBoolean(value); boolean sendDeviceMeta = Boolean.parseBoolean(value);
options.setSendDeviceMeta(sendDeviceMeta); options.setSendDeviceMeta(sendDeviceMeta);