Skip to content

Commit 2a7f3d7

Browse files
brunoaisrom1v
authored andcommitted
Keep the screen off if it was off when powering on
Fixes #1573 <#1573> PR #1577 <#1577> Signed-off-by: Romain Vimont <[email protected]>
1 parent 94269cc commit 2a7f3d7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,12 @@ scrcpy -S
440440

441441
Or by pressing <kbd>MOD</kbd>+<kbd>o</kbd> at any time.
442442

443-
To turn it back on, press <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd> (or
444-
`POWER`, <kbd>MOD</kbd>+<kbd>p</kbd>).
443+
To turn it back on, press <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>.
444+
445+
On Android, the `POWER` button always turns the screen on. For convenience, if
446+
`POWER` is sent via scrcpy (via right-click or <kbd>Ctrl</kbd>+<kbd>p</kbd>), it
447+
will force to turn the screen off after a small delay (on a best effort basis).
448+
The physical `POWER` button will still cause the screen to be turned on.
445449

446450
It can be useful to also prevent the device to sleep:
447451

server/src/main/java/com/genymobile/scrcpy/Controller.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.view.MotionEvent;
99

1010
import java.io.IOException;
11+
import java.util.Timer;
12+
import java.util.TimerTask;
1113

1214
public class Controller {
1315

@@ -24,6 +26,9 @@ public class Controller {
2426
private final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[PointersState.MAX_POINTERS];
2527
private final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[PointersState.MAX_POINTERS];
2628

29+
private boolean screenStayOff;
30+
private static final Timer keepScreenOffTimer = new Timer("KeepScreenOff", true);
31+
2732
public Controller(Device device, DesktopConnection connection) {
2833
this.device = device;
2934
this.connection = connection;
@@ -117,6 +122,7 @@ private void handleEvent() throws IOException {
117122
int mode = msg.getAction();
118123
boolean setPowerModeOk = Device.setScreenPowerMode(mode);
119124
if (setPowerModeOk) {
125+
screenStayOff = mode == Device.POWER_MODE_OFF;
120126
Ln.i("Device screen turned " + (mode == Device.POWER_MODE_OFF ? "off" : "on"));
121127
}
122128
}
@@ -130,6 +136,9 @@ private void handleEvent() throws IOException {
130136
}
131137

132138
private boolean injectKeycode(int action, int keycode, int repeat, int metaState) {
139+
if (screenStayOff && action == KeyEvent.ACTION_UP && (keycode == KeyEvent.KEYCODE_POWER || keycode == KeyEvent.KEYCODE_WAKEUP)) {
140+
scheduleScreenOff();
141+
}
133142
return device.injectKeyEvent(action, keycode, repeat, metaState);
134143
}
135144

@@ -223,8 +232,24 @@ private boolean injectScroll(Position position, int hScroll, int vScroll) {
223232
return device.injectEvent(event);
224233
}
225234

235+
/**
236+
* Schedule a call to turn the screen off after a small delay.
237+
*/
238+
private static void scheduleScreenOff() {
239+
keepScreenOffTimer.schedule(new TimerTask() {
240+
@Override
241+
public void run() {
242+
Ln.i("Forcing screen off");
243+
Device.setScreenPowerMode(Device.POWER_MODE_OFF);
244+
}
245+
}, 200);
246+
}
247+
226248
private boolean pressBackOrTurnScreenOn() {
227249
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_WAKEUP;
250+
if (screenStayOff && keycode == KeyEvent.KEYCODE_WAKEUP) {
251+
scheduleScreenOff();
252+
}
228253
return device.injectKeycode(keycode);
229254
}
230255

0 commit comments

Comments
 (0)