Skip to content

Commit

Permalink
Support power off on close
Browse files Browse the repository at this point in the history
Signed-off-by: Yu-Chen Lin <[email protected]>
  • Loading branch information
npes87184 committed Feb 21, 2021
1 parent 9cb8c92 commit f399cb6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ guess_record_format(const char *filename) {
#define OPT_FORWARD_ALL_CLICKS 1023
#define OPT_LEGACY_PASTE 1024
#define OPT_ENCODER_NAME 1025
#define OPT_POWER_OFF_ON_CLOSE 1026

bool
scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
Expand Down Expand Up @@ -717,6 +718,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"window-height", required_argument, NULL, OPT_WINDOW_HEIGHT},
{"window-borderless", no_argument, NULL,
OPT_WINDOW_BORDERLESS},
{"power-off-on-close", no_argument, NULL,
OPT_POWER_OFF_ON_CLOSE},
{NULL, 0, NULL, 0 },
};

Expand Down Expand Up @@ -885,6 +888,9 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
case OPT_LEGACY_PASTE:
opts->legacy_paste = true;
break;
case OPT_POWER_OFF_ON_CLOSE:
opts->power_off_on_close = true;
break;
default:
// getopt prints the error message on stderr
return false;
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ scrcpy(const struct scrcpy_options *options) {
.codec_options = options->codec_options,
.encoder_name = options->encoder_name,
.force_adb_forward = options->force_adb_forward,
.power_off_on_close = options->power_off_on_close,
};
if (!server_start(&server, options->serial, &params)) {
goto end;
Expand Down
2 changes: 2 additions & 0 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct scrcpy_options {
bool forward_key_repeat;
bool forward_all_clicks;
bool legacy_paste;
bool power_off_on_close;
};

#define SCRCPY_OPTIONS_DEFAULT { \
Expand Down Expand Up @@ -129,6 +130,7 @@ struct scrcpy_options {
.forward_key_repeat = true, \
.forward_all_clicks = false, \
.legacy_paste = false, \
.power_off_on_close = false, \
}

bool
Expand Down
1 change: 1 addition & 0 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ execute_server(struct server *server, const struct server_params *params) {
params->stay_awake ? "true" : "false",
params->codec_options ? params->codec_options : "-",
params->encoder_name ? params->encoder_name : "-",
params->power_off_on_close ? "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 @@ -46,6 +46,7 @@ struct server_params {
bool show_touches;
bool stay_awake;
bool force_adb_forward;
bool power_off_on_close;
};

// init default values
Expand Down
23 changes: 18 additions & 5 deletions server/src/main/java/com/genymobile/scrcpy/CleanUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ private CleanUp() {
// not instantiable
}

public static void configure(boolean disableShowTouches, int restoreStayOn, boolean restoreNormalPowerMode) throws IOException {
boolean needProcess = disableShowTouches || restoreStayOn != -1 || restoreNormalPowerMode;
public static void configure(boolean disableShowTouches,
int restoreStayOn,
boolean restoreNormalPowerMode,
boolean powerOffScreen, int displayId) throws IOException {
boolean needProcess = disableShowTouches || restoreStayOn != -1 || restoreNormalPowerMode || powerOffScreen;
if (needProcess) {
startProcess(disableShowTouches, restoreStayOn, restoreNormalPowerMode);
startProcess(disableShowTouches, restoreStayOn, restoreNormalPowerMode, powerOffScreen, displayId);
} else {
// There is no additional clean up to do when scrcpy dies
unlinkSelf();
}
}

private static void startProcess(boolean disableShowTouches, int restoreStayOn, boolean restoreNormalPowerMode) throws IOException {
private static void startProcess(boolean disableShowTouches,
int restoreStayOn,
boolean restoreNormalPowerMode,
boolean powerOffScreen, int displayId) throws IOException {
String[] cmd = {"app_process", "/", CleanUp.class.getName(), String.valueOf(disableShowTouches), String.valueOf(
restoreStayOn), String.valueOf(restoreNormalPowerMode)};
restoreStayOn), String.valueOf(restoreNormalPowerMode), String.valueOf(powerOffScreen), String.valueOf(displayId)};

ProcessBuilder builder = new ProcessBuilder(cmd);
builder.environment().put("CLASSPATH", SERVER_PATH);
Expand Down Expand Up @@ -61,6 +67,8 @@ public static void main(String... args) {
boolean disableShowTouches = Boolean.parseBoolean(args[0]);
int restoreStayOn = Integer.parseInt(args[1]);
boolean restoreNormalPowerMode = Boolean.parseBoolean(args[2]);
boolean powerOffScreen = Boolean.parseBoolean(args[3]);
int displayId = Integer.parseInt(args[4]);

if (disableShowTouches || restoreStayOn != -1) {
ServiceManager serviceManager = new ServiceManager();
Expand All @@ -82,5 +90,10 @@ public static void main(String... args) {
Device.setScreenPowerMode(Device.POWER_MODE_NORMAL);
}
}

if (powerOffScreen) {
Ln.i("Power off screen");
Device.powerOffScreen(displayId);
}
}
}
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 @@ -17,6 +17,7 @@ public class Options {
private boolean stayAwake;
private String codecOptions;
private String encoderName;
private boolean powerOffScreenOnClose;

public Ln.Level getLogLevel() {
return logLevel;
Expand Down Expand Up @@ -129,4 +130,12 @@ public String getEncoderName() {
public void setEncoderName(String encoderName) {
this.encoderName = encoderName;
}

public void setPowerOffScreenOnClose(boolean powerOffScreenOnClose) {
this.powerOffScreenOnClose = powerOffScreenOnClose;
}

public boolean getPowerOffScreenOnClose() {
return this.powerOffScreenOnClose;
}
}
7 changes: 5 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void scrcpy(Options options) throws IOException {
}
}

CleanUp.configure(mustDisableShowTouchesOnCleanUp, restoreStayOn, true);
CleanUp.configure(mustDisableShowTouchesOnCleanUp, restoreStayOn, true, options.getPowerOffScreenOnClose(), options.getDisplayId());

boolean tunnelForward = options.isTunnelForward();

Expand Down Expand Up @@ -135,7 +135,7 @@ private static Options createOptions(String... args) {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
}

final int expectedParameters = 15;
final int expectedParameters = 16;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Expand Down Expand Up @@ -185,6 +185,9 @@ private static Options createOptions(String... args) {
String encoderName = "-".equals(args[14]) ? null : args[14];
options.setEncoderName(encoderName);

boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]);
options.setPowerOffScreenOnClose(powerOffScreenOnClose);

return options;
}

Expand Down

0 comments on commit f399cb6

Please sign in to comment.