From edc81b4a1c68454ce4b0c4cd3eb246755b264bff Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 31 Mar 2026 19:54:13 +0200 Subject: [PATCH] Do not fallback to main display If the virtual display is not initialized yet, do not send events to the main display. --- .../genymobile/scrcpy/control/Controller.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 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 968663a0..98aaf56f 100644 --- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java @@ -349,7 +349,10 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { } return true; case ControlMessage.TYPE_ROTATE_DEVICE: - Device.rotateDevice(getActionDisplayId()); + int actionDisplayId = getActionDisplayId(); + if (actionDisplayId != Device.DISPLAY_ID_NONE) { + Device.rotateDevice(actionDisplayId); + } return true; case ControlMessage.TYPE_UHID_CREATE: getUhidManager().open(msg.getId(), msg.getVendorId(), msg.getProductId(), msg.getText(), msg.getData()); @@ -407,9 +410,11 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { } int actionDisplayId = getActionDisplayId(); - for (KeyEvent event : events) { - if (!Device.injectEvent(event, actionDisplayId, Device.INJECT_MODE_ASYNC)) { - return false; + if (actionDisplayId != Device.DISPLAY_ID_NONE) { + for (KeyEvent event : events) { + if (!Device.injectEvent(event, actionDisplayId, Device.INJECT_MODE_ASYNC)) { + return false; + } } } return true; @@ -670,11 +675,19 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { } private boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int injectMode) { - return Device.injectKeyEvent(action, keyCode, repeat, metaState, getActionDisplayId(), injectMode); + int actionDisplayId = getActionDisplayId(); + if (actionDisplayId == Device.DISPLAY_ID_NONE) { + return false; + } + return Device.injectKeyEvent(action, keyCode, repeat, metaState, actionDisplayId, injectMode); } private boolean pressReleaseKeycode(int keyCode, int injectMode) { - return Device.pressReleaseKeycode(keyCode, getActionDisplayId(), injectMode); + int actionDisplayId = getActionDisplayId(); + if (actionDisplayId == Device.DISPLAY_ID_NONE) { + return false; + } + return Device.pressReleaseKeycode(keyCode, actionDisplayId, injectMode); } private int getActionDisplayId() { @@ -686,8 +699,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { // Virtual display created by --new-display, use the virtualDisplayId DisplayData data = displayData.get(); if (data == null) { - // If no virtual display id is initialized yet, use the main display id - return 0; + return Device.DISPLAY_ID_NONE; } return data.virtualDisplayId;