From 6ddcc98663f001d22917f14edcf6864abc8a94ec Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 6 Oct 2024 18:39:15 +0200 Subject: [PATCH] vdevents --- .../genymobile/scrcpy/control/Controller.java | 14 +++++++------- .../com/genymobile/scrcpy/device/Device.java | 19 +++++++++++++++++-- .../scrcpy/video/ScreenCapture.java | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java index b445427d..aef249e6 100644 --- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java @@ -243,7 +243,7 @@ public class Controller implements AsyncProcessor { return false; } for (KeyEvent event : events) { - if (!device.injectEvent(event, Device.INJECT_MODE_ASYNC)) { + if (!device.injectMainDisplayEvent(event, Device.INJECT_MODE_ASYNC)) { return false; } } @@ -324,7 +324,7 @@ public class Controller implements AsyncProcessor { // First button pressed: ACTION_DOWN MotionEvent downEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_DOWN, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0); - if (!device.injectEvent(downEvent, Device.INJECT_MODE_ASYNC)) { + if (!device.injectVirtualDisplayEvent(downEvent, Device.INJECT_MODE_ASYNC)) { return false; } } @@ -335,7 +335,7 @@ public class Controller implements AsyncProcessor { if (!InputManager.setActionButton(pressEvent, actionButton)) { return false; } - if (!device.injectEvent(pressEvent, Device.INJECT_MODE_ASYNC)) { + if (!device.injectVirtualDisplayEvent(pressEvent, Device.INJECT_MODE_ASYNC)) { return false; } @@ -349,7 +349,7 @@ public class Controller implements AsyncProcessor { if (!InputManager.setActionButton(releaseEvent, actionButton)) { return false; } - if (!device.injectEvent(releaseEvent, Device.INJECT_MODE_ASYNC)) { + if (!device.injectVirtualDisplayEvent(releaseEvent, Device.INJECT_MODE_ASYNC)) { return false; } @@ -357,7 +357,7 @@ public class Controller implements AsyncProcessor { // Last button released: ACTION_UP MotionEvent upEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_UP, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0); - if (!device.injectEvent(upEvent, Device.INJECT_MODE_ASYNC)) { + if (!device.injectVirtualDisplayEvent(upEvent, Device.INJECT_MODE_ASYNC)) { return false; } } @@ -368,7 +368,7 @@ public class Controller implements AsyncProcessor { MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0); - return device.injectEvent(event, Device.INJECT_MODE_ASYNC); + return device.injectVirtualDisplayEvent(event, Device.INJECT_MODE_ASYNC); } private boolean injectScroll(Position position, float hScroll, float vScroll, int buttons) { @@ -390,7 +390,7 @@ public class Controller implements AsyncProcessor { MotionEvent event = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, InputDevice.SOURCE_MOUSE, 0); - return device.injectEvent(event, Device.INJECT_MODE_ASYNC); + return device.injectVirtualDisplayEvent(event, Device.INJECT_MODE_ASYNC); } /** diff --git a/server/src/main/java/com/genymobile/scrcpy/device/Device.java b/server/src/main/java/com/genymobile/scrcpy/device/Device.java index 8452bf0e..cd672135 100644 --- a/server/src/main/java/com/genymobile/scrcpy/device/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/device/Device.java @@ -48,10 +48,13 @@ public final class Device { private final boolean supportsInputEvents; - private ScreenInfo screenInfo; // set by the ScreenCapture instance + // set by the ScreenCapture instance + private ScreenInfo screenInfo; + private int virtualDisplayId; public Device(Options options) { displayId = options.getDisplayId(); + virtualDisplayId = displayId; // by default if (options.getControl() && options.getClipboardAutosync()) { // If control and autosync are enabled, synchronize Android clipboard to the computer automatically @@ -134,6 +137,14 @@ public final class Device { this.screenInfo = screenInfo; } + private synchronized int getVirtualDisplayId() { + return virtualDisplayId; + } + + public synchronized void setVirtualDisplayId(int virtualDisplayId) { + this.virtualDisplayId = virtualDisplayId; + } + public static boolean injectEvent(InputEvent inputEvent, int displayId, int injectMode) { if (!supportsInputEvents(displayId)) { throw new AssertionError("Could not inject input event if !supportsInputEvents()"); @@ -146,10 +157,14 @@ public final class Device { return ServiceManager.getInputManager().injectInputEvent(inputEvent, injectMode); } - public boolean injectEvent(InputEvent event, int injectMode) { + public boolean injectMainDisplayEvent(InputEvent event, int injectMode) { return injectEvent(event, displayId, injectMode); } + public boolean injectVirtualDisplayEvent(InputEvent event, int injectMode) { + return injectEvent(event, virtualDisplayId, injectMode); + } + public static boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int displayId, int injectMode) { long now = SystemClock.uptimeMillis(); KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, diff --git a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java index 21629f11..67d325f0 100644 --- a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java +++ b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java @@ -121,6 +121,7 @@ public class ScreenCapture extends SurfaceCapture { Rect videoRect = screenInfo.getVideoSize().toRect(); virtualDisplay = ServiceManager.getDisplayManager() .createVirtualDisplay("scrcpy", videoRect.width(), videoRect.height(), displayId, surface); + device.setVirtualDisplayId(virtualDisplay.getDisplay().getDisplayId()); Ln.d("Display: using DisplayManager API"); } catch (Exception displayManagerException) { try {