Skip to content

Commit

Permalink
Export static method to power off screen in Device
Browse files Browse the repository at this point in the history
PR #824 <#824>

Signed-off-by: Yu-Chen Lin <[email protected]>
Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
npes87184 authored and rom1v committed Mar 16, 2021
1 parent dd453ad commit fb0bcae
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ public static String getDeviceName() {
return Build.MODEL;
}

public static boolean supportsInputEvents(int displayId) {
return displayId == 0 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}

public boolean supportsInputEvents() {
return supportsInputEvents;
}

public boolean injectEvent(InputEvent inputEvent, int mode) {
if (!supportsInputEvents()) {
throw new AssertionError("Could not inject input event if !supportsInputEvents()");
public static boolean injectEvent(InputEvent inputEvent, int mode, int displayId) {
if (!supportsInputEvents(displayId)) {
return false;
}

if (displayId != 0 && !InputManager.setDisplayId(inputEvent, displayId)) {
Expand All @@ -169,17 +173,40 @@ public boolean injectEvent(InputEvent inputEvent, int mode) {
return SERVICE_MANAGER.getInputManager().injectInputEvent(inputEvent, mode);
}

public boolean injectEvent(InputEvent inputEvent, int mode) {
if (!supportsInputEvents()) {
throw new AssertionError("Could not inject input event if !supportsInputEvents()");
}

return injectEvent(inputEvent, mode, displayId);
}

public static boolean injectEventOnDisplay(InputEvent event, int displayId) {
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, displayId);
}

public boolean injectEvent(InputEvent event) {
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}

public static boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int displayId) {
long now = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
InputDevice.SOURCE_KEYBOARD);
return injectEventOnDisplay(event, displayId);
}

public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
long now = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
InputDevice.SOURCE_KEYBOARD);
return injectEvent(event);
}

public static boolean injectKeycode(int keyCode, int displayId) {
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0, displayId) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId);
}

public boolean injectKeycode(int keyCode) {
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0);
}
Expand Down Expand Up @@ -249,6 +276,13 @@ public static boolean setScreenPowerMode(int mode) {
return SurfaceControl.setDisplayPowerMode(d, mode);
}

public static boolean powerOffScreen(int displayId) {
if (!isScreenOn()) {
return true;
}
return injectKeycode(KeyEvent.KEYCODE_POWER, displayId);
}

/**
* Disable auto-rotation (if enabled), set the screen rotation and re-enable auto-rotation (if it was enabled).
*/
Expand Down

0 comments on commit fb0bcae

Please sign in to comment.