From 44e06e7b7e3790b6e1cecbb5eee9161a22627ada Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 23 Apr 2022 15:08:30 +0200 Subject: [PATCH] Add option to prevent power on on start By default, on start, the device is powered on. To prevent this behavior, add a new option --no-power-on. Fixes #3148 PR #3210 --- README.md | 10 ++++++++++ app/data/bash-completion/scrcpy | 1 + app/data/zsh-completion/_scrcpy | 1 + app/scrcpy.1 | 4 ++++ app/src/cli.c | 9 +++++++++ app/src/options.c | 1 + app/src/options.h | 1 + app/src/scrcpy.c | 1 + app/src/server.c | 4 ++++ app/src/server.h | 1 + .../main/java/com/genymobile/scrcpy/Controller.java | 6 ++++-- .../src/main/java/com/genymobile/scrcpy/Options.java | 9 +++++++++ server/src/main/java/com/genymobile/scrcpy/Server.java | 6 +++++- 13 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 614d1ed435..846b18acd2 100644 --- a/README.md +++ b/README.md @@ -721,6 +721,16 @@ To turn the device screen off when closing scrcpy: scrcpy --power-off-on-close ``` +#### Power on on start + +By default, on start, the device is powered on. + +To prevent this behavior: + +```bash +scrcpy --no-power-on +``` + #### Show touches diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy index 464bc532f4..3e75cbb018 100644 --- a/app/data/bash-completion/scrcpy +++ b/app/data/bash-completion/scrcpy @@ -29,6 +29,7 @@ _scrcpy() { -N --no-display --no-key-repeat --no-mipmaps + --no-power-on --otg -p --port= --power-off-on-close diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy index 77e6027a40..097c80d68b 100644 --- a/app/data/zsh-completion/_scrcpy +++ b/app/data/zsh-completion/_scrcpy @@ -35,6 +35,7 @@ arguments=( {-N,--no-display}'[Do not display device \(during screen recording or when V4L2 sink is enabled\)]' '--no-key-repeat[Do not forward repeated key events when a key is held down]' '--no-mipmaps[Disable the generation of mipmaps]' + '--no-power-on[Do not power on the device on start]' '--otg[Run in OTG mode \(simulating physical keyboard and mouse\)]' {-p,--port=}'[\[port\[\:port\]\] Set the TCP port \(range\) used by the client to listen]' '--power-off-on-close[Turn the device screen off when closing scrcpy]' diff --git a/app/scrcpy.1 b/app/scrcpy.1 index eb16447502..7cb893b7f0 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -180,6 +180,10 @@ Do not forward repeated key events when a key is held down. .B \-\-no\-mipmaps If the renderer is OpenGL 3.0+ or OpenGL ES 2.0+, then mipmaps are automatically generated to improve downscaling quality. This option disables the generation of mipmaps. +.TP +.B \-\-no\-power\-on +Do not power on the device on start. + .TP .B \-\-otg Run in OTG mode: simulate physical keyboard and mouse, as if the computer keyboard and mouse were plugged directly to the device via an OTG cable. diff --git a/app/src/cli.c b/app/src/cli.c index 5dda86e590..a96b9d2ef7 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -56,6 +56,7 @@ #define OPT_OTG 1036 #define OPT_NO_CLEANUP 1037 #define OPT_PRINT_FPS 1038 +#define OPT_NO_POWER_ON 1039 struct sc_option { char shortopt; @@ -302,6 +303,11 @@ static const struct sc_option options[] = { "mipmaps are automatically generated to improve downscaling " "quality. This option disables the generation of mipmaps.", }, + { + .longopt_id = OPT_NO_POWER_ON, + .longopt = "no-power-on", + .text = "Do not power on the device on start.", + }, { .longopt_id = OPT_OTG, .longopt = "otg", @@ -1598,6 +1604,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[], case OPT_NO_CLEANUP: opts->cleanup = false; break; + case OPT_NO_POWER_ON: + opts->power_on = false; + break; case OPT_PRINT_FPS: opts->start_fps_counter = true; break; diff --git a/app/src/options.c b/app/src/options.c index 6651020f3e..8b2624d992 100644 --- a/app/src/options.c +++ b/app/src/options.c @@ -64,4 +64,5 @@ const struct scrcpy_options scrcpy_options_default = { .select_usb = false, .cleanup = true, .start_fps_counter = false, + .power_on = true, }; diff --git a/app/src/options.h b/app/src/options.h index f63e5c429c..7e542c06a8 100644 --- a/app/src/options.h +++ b/app/src/options.h @@ -139,6 +139,7 @@ struct scrcpy_options { bool select_tcpip; bool cleanup; bool start_fps_counter; + bool power_on; }; extern const struct scrcpy_options scrcpy_options_default; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 8fbfe394c6..3588e9aef0 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -324,6 +324,7 @@ scrcpy(struct scrcpy_options *options) { .tcpip = options->tcpip, .tcpip_dst = options->tcpip_dst, .cleanup = options->cleanup, + .power_on = options->power_on, }; static const struct sc_server_callbacks cbs = { diff --git a/app/src/server.c b/app/src/server.c index c02390a4ed..663ef18bb4 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -248,6 +248,10 @@ execute_server(struct sc_server *server, // By default, cleanup is true ADD_PARAM("cleanup=false"); } + if (!params->power_on) { + // By default, power_on is true + ADD_PARAM("power_on=false"); + } #undef ADD_PARAM diff --git a/app/src/server.h b/app/src/server.h index 5f630ca8f1..49ba83c1f8 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -47,6 +47,7 @@ struct sc_server_params { bool select_usb; bool select_tcpip; bool cleanup; + bool power_on; }; struct sc_server { diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index 481c512fc4..913371ee06 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -22,6 +22,7 @@ public class Controller { private final DesktopConnection connection; private final DeviceMessageSender sender; private final boolean clipboardAutosync; + private final boolean powerOn; private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); @@ -32,10 +33,11 @@ public class Controller { private boolean keepPowerModeOff; - public Controller(Device device, DesktopConnection connection, boolean clipboardAutosync) { + public Controller(Device device, DesktopConnection connection, boolean clipboardAutosync, boolean powerOn) { this.device = device; this.connection = connection; this.clipboardAutosync = clipboardAutosync; + this.powerOn = powerOn; initPointers(); sender = new DeviceMessageSender(connection); } @@ -56,7 +58,7 @@ private void initPointers() { public void control() throws IOException { // on start, power on the device - if (!Device.isScreenOn()) { + if (powerOn && !Device.isScreenOn()) { device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC); // dirty hack diff --git a/server/src/main/java/com/genymobile/scrcpy/Options.java b/server/src/main/java/com/genymobile/scrcpy/Options.java index 4842b63557..d1607c200c 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Options.java +++ b/server/src/main/java/com/genymobile/scrcpy/Options.java @@ -22,6 +22,7 @@ public class Options { private boolean clipboardAutosync = true; private boolean downsizeOnError = true; private boolean cleanup = true; + private boolean powerOn = true; // Options not used by the scrcpy client, but useful to use scrcpy-server directly private boolean sendDeviceMeta = true; // send device name and size @@ -164,6 +165,14 @@ public void setCleanup(boolean cleanup) { this.cleanup = cleanup; } + public boolean getPowerOn() { + return powerOn; + } + + public void setPowerOn(boolean powerOn) { + this.powerOn = powerOn; + } + public boolean getSendDeviceMeta() { return sendDeviceMeta; } diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index 60f485d81a..1df9155207 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -82,7 +82,7 @@ private static void scrcpy(Options options) throws IOException { Thread controllerThread = null; Thread deviceMessageSenderThread = null; if (control) { - final Controller controller = new Controller(device, connection, options.getClipboardAutosync()); + final Controller controller = new Controller(device, connection, options.getClipboardAutosync(), options.getPowerOn()); // asynchronous controllerThread = startController(controller); @@ -248,6 +248,10 @@ private static Options createOptions(String... args) { boolean cleanup = Boolean.parseBoolean(value); options.setCleanup(cleanup); break; + case "power_on": + boolean powerOn = Boolean.parseBoolean(value); + options.setPowerOn(powerOn); + break; case "send_device_meta": boolean sendDeviceMeta = Boolean.parseBoolean(value); options.setSendDeviceMeta(sendDeviceMeta);