Skip to content

Commit

Permalink
Add option to prevent power on on start
Browse files Browse the repository at this point in the history
By default, on start, the device is powered on. To prevent this
behavior, add a new option --no-power-on.

Fixes #3148 <#3148>
PR #3210 <#3210>
  • Loading branch information
rom1v committed Apr 23, 2022
1 parent 6a4a4a2 commit 44e06e7
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/data/bash-completion/scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _scrcpy() {
-N --no-display
--no-key-repeat
--no-mipmaps
--no-power-on
--otg
-p --port=
--power-off-on-close
Expand Down
1 change: 1 addition & 0 deletions app/data/zsh-completion/_scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
4 changes: 4 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ const struct scrcpy_options scrcpy_options_default = {
.select_usb = false,
.cleanup = true,
.start_fps_counter = false,
.power_on = true,
};
1 change: 1 addition & 0 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct sc_server_params {
bool select_usb;
bool select_tcpip;
bool cleanup;
bool power_on;
};

struct sc_server {
Expand Down
6 changes: 4 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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
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 @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 44e06e7

Please sign in to comment.