From 7ec0c293608a89ee1320757e20b382efaa08f830 Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sat, 28 Sep 2019 11:44:33 +0800 Subject: [PATCH] Turn screen off when closing scrcpy Signed-off-by: Yu-Chen Lin --- app/src/control_msg.c | 1 + app/src/control_msg.h | 1 + app/src/scrcpy.c | 12 ++++++++++++ .../java/com/genymobile/scrcpy/ControlMessage.java | 1 + .../com/genymobile/scrcpy/ControlMessageReader.java | 1 + .../main/java/com/genymobile/scrcpy/Controller.java | 11 +++++++++++ 6 files changed, 27 insertions(+) diff --git a/app/src/control_msg.c b/app/src/control_msg.c index 9c3d9849fa..25127522f0 100644 --- a/app/src/control_msg.c +++ b/app/src/control_msg.c @@ -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: diff --git a/app/src/control_msg.h b/app/src/control_msg.h index e7fdfc4cb5..eed8a8d9e6 100644 --- a/app/src/control_msg.h +++ b/app/src/control_msg.h @@ -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 { diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index ed9887789b..594ad7d2e8 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -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) { @@ -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: diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlMessage.java b/server/src/main/java/com/genymobile/scrcpy/ControlMessage.java index a1cd873ab2..f4079b01b5 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlMessage.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlMessage.java @@ -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; diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java index 8ced049da2..d672627c9f 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java @@ -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: diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index 263fc2fc37..6fa7084e25 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -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; @@ -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); + } }