Skip to content

Commit

Permalink
Turn screen off when closing scrcpy
Browse files Browse the repository at this point in the history
Signed-off-by: Yu-Chen Lin <[email protected]>
  • Loading branch information
npes87184 committed Sep 28, 2019
1 parent 795d103 commit 7ec0c29
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/control_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_COLLAPSE_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
case CONTROL_MSG_TYPE_TURN_SCREEN_OFF:
// no additional data
return 1;
default:
Expand Down
1 change: 1 addition & 0 deletions app/src/control_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum control_msg_type {
CONTROL_MSG_TYPE_GET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
CONTROL_MSG_TYPE_TURN_SCREEN_OFF,
};

enum screen_power_mode {
Expand Down
12 changes: 12 additions & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ wait_show_touches(process_t process) {
process_check_success(process, "show_touches");
}

static void
turn_device_screen_off(struct controller *controller) {
struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_TURN_SCREEN_OFF;

if (!controller_push_msg(controller, &msg)) {
LOGW("Could not request 'turn screen off'");
}
}

static SDL_LogPriority
sdl_priority_from_av_level(int level) {
switch (level) {
Expand Down Expand Up @@ -411,6 +421,8 @@ scrcpy(const struct scrcpy_options *options) {
ret = event_loop(options->display, options->control);
LOGD("quit...");

turn_device_screen_off(&controller);

screen_destroy(&screen);

end:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class ControlMessage {
public static final int TYPE_GET_CLIPBOARD = 7;
public static final int TYPE_SET_CLIPBOARD = 8;
public static final int TYPE_SET_SCREEN_POWER_MODE = 9;
public static final int TYPE_TURN_SCREEN_OFF = 10;

private int type;
private String text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public ControlMessage next() {
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
case ControlMessage.TYPE_COLLAPSE_NOTIFICATION_PANEL:
case ControlMessage.TYPE_GET_CLIPBOARD:
case ControlMessage.TYPE_TURN_SCREEN_OFF:
msg = ControlMessage.createEmpty(type);
break;
default:
Expand Down
11 changes: 11 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ private void handleEvent() throws IOException {
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
pressBackOrTurnScreenOn();
break;
case ControlMessage.TYPE_TURN_SCREEN_OFF:
turnScreenOff();
break;
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
device.expandNotificationPanel();
break;
Expand Down Expand Up @@ -198,4 +201,12 @@ private boolean pressBackOrTurnScreenOn() {
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
}

private boolean turnScreenOff() {
if (!device.isScreenOn()) {
return true;
}
int keycode = KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
}
}

0 comments on commit 7ec0c29

Please sign in to comment.