From 2a7f3d70d71730b7a79a1b0ce626a5ea772c5cbe Mon Sep 17 00:00:00 2001 From: brunoais Date: Wed, 8 Jul 2020 21:49:34 +0100 Subject: [PATCH] Keep the screen off if it was off when powering on Fixes #1573 PR #1577 Signed-off-by: Romain Vimont --- README.md | 8 ++++-- .../com/genymobile/scrcpy/Controller.java | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d875fd99..4ef28ab2 100644 --- a/README.md +++ b/README.md @@ -440,8 +440,12 @@ scrcpy -S Or by pressing MOD+o at any time. -To turn it back on, press MOD+Shift+o (or -`POWER`, MOD+p). +To turn it back on, press MOD+Shift+o. + +On Android, the `POWER` button always turns the screen on. For convenience, if +`POWER` is sent via scrcpy (via right-click or Ctrl+p), it +will force to turn the screen off after a small delay (on a best effort basis). +The physical `POWER` button will still cause the screen to be turned on. It can be useful to also prevent the device to sleep: diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index f32e5ec0..8751a52a 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -8,6 +8,8 @@ import android.view.KeyEvent; import android.view.MotionEvent; import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; public class Controller { @@ -24,6 +26,9 @@ public class Controller { private final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[PointersState.MAX_POINTERS]; private final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[PointersState.MAX_POINTERS]; + private boolean screenStayOff; + private static final Timer keepScreenOffTimer = new Timer("KeepScreenOff", true); + public Controller(Device device, DesktopConnection connection) { this.device = device; this.connection = connection; @@ -117,6 +122,7 @@ public class Controller { int mode = msg.getAction(); boolean setPowerModeOk = Device.setScreenPowerMode(mode); if (setPowerModeOk) { + screenStayOff = mode == Device.POWER_MODE_OFF; Ln.i("Device screen turned " + (mode == Device.POWER_MODE_OFF ? "off" : "on")); } } @@ -130,6 +136,9 @@ public class Controller { } private boolean injectKeycode(int action, int keycode, int repeat, int metaState) { + if (screenStayOff && action == KeyEvent.ACTION_UP && (keycode == KeyEvent.KEYCODE_POWER || keycode == KeyEvent.KEYCODE_WAKEUP)) { + scheduleScreenOff(); + } return device.injectKeyEvent(action, keycode, repeat, metaState); } @@ -223,8 +232,24 @@ public class Controller { return device.injectEvent(event); } + /** + * Schedule a call to turn the screen off after a small delay. + */ + private static void scheduleScreenOff() { + keepScreenOffTimer.schedule(new TimerTask() { + @Override + public void run() { + Ln.i("Forcing screen off"); + Device.setScreenPowerMode(Device.POWER_MODE_OFF); + } + }, 200); + } + private boolean pressBackOrTurnScreenOn() { int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_WAKEUP; + if (screenStayOff && keycode == KeyEvent.KEYCODE_WAKEUP) { + scheduleScreenOff(); + } return device.injectKeycode(keycode); }