add injectmode option

This commit is contained in:
Bart Saint Germain 2025-04-04 05:51:29 +02:00
commit 378f0247a1
No known key found for this signature in database
2 changed files with 22 additions and 12 deletions

View file

@ -40,6 +40,7 @@ public class Options {
private Rect crop; private Rect crop;
private boolean control = true; private boolean control = true;
private int displayId; private int displayId;
private int injectMode = Device.INJECT_MODE_ASYNC;
private String cameraId; private String cameraId;
private Size cameraSize; private Size cameraSize;
private CameraFacing cameraFacing; private CameraFacing cameraFacing;
@ -152,6 +153,10 @@ public class Options {
return displayId; return displayId;
} }
public int getInjectMode() {
return injectMode;
}
public String getCameraId() { public String getCameraId() {
return cameraId; return cameraId;
} }
@ -387,6 +392,9 @@ public class Options {
case "display_id": case "display_id":
options.displayId = Integer.parseInt(value); options.displayId = Integer.parseInt(value);
break; break;
case "inject_mode":
options.injectMode = Integer.parseInt(value);
break;
case "show_touches": case "show_touches":
options.showTouches = Boolean.parseBoolean(value); options.showTouches = Boolean.parseBoolean(value);
break; break;

View file

@ -82,6 +82,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
private final DeviceMessageSender sender; private final DeviceMessageSender sender;
private final boolean clipboardAutosync; private final boolean clipboardAutosync;
private final boolean powerOn; private final boolean powerOn;
private int injectMode = Device.INJECT_MODE_ASYNC;
private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
@ -102,6 +103,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
public Controller(ControlChannel controlChannel, CleanUp cleanUp, Options options) { public Controller(ControlChannel controlChannel, CleanUp cleanUp, Options options) {
this.displayId = options.getDisplayId(); this.displayId = options.getDisplayId();
this.injectMode = options.getInjectMode();
this.controlChannel = controlChannel; this.controlChannel = controlChannel;
this.cleanUp = cleanUp; this.cleanUp = cleanUp;
this.clipboardAutosync = options.getClipboardAutosync(); this.clipboardAutosync = options.getClipboardAutosync();
@ -178,7 +180,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
private void control() throws IOException { private void control() throws IOException {
// on start, power on the device // on start, power on the device
if (powerOn && displayId == 0 && !Device.isScreenOn(displayId)) { if (powerOn && displayId == 0 && !Device.isScreenOn(displayId)) {
Device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC); Device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, this.injectMode);
// dirty hack // dirty hack
// After POWER is injected, the device is powered on asynchronously. // After POWER is injected, the device is powered on asynchronously.
@ -319,7 +321,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
assert displayId != Device.DISPLAY_ID_NONE; assert displayId != Device.DISPLAY_ID_NONE;
scheduleDisplayPowerOff(displayId); scheduleDisplayPowerOff(displayId);
} }
return injectKeyEvent(action, keycode, repeat, metaState, Device.INJECT_MODE_ASYNC); return injectKeyEvent(action, keycode, repeat, metaState, this.injectMode);
} }
private boolean injectChar(char c) { private boolean injectChar(char c) {
@ -332,7 +334,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
int actionDisplayId = getActionDisplayId(); int actionDisplayId = getActionDisplayId();
for (KeyEvent event : events) { for (KeyEvent event : events) {
if (!Device.injectEvent(event, actionDisplayId, Device.INJECT_MODE_ASYNC)) { if (!Device.injectEvent(event, actionDisplayId, this.injectMode)) {
return false; return false;
} }
} }
@ -445,7 +447,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
// First button pressed: ACTION_DOWN // First button pressed: ACTION_DOWN
MotionEvent downEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_DOWN, pointerCount, pointerProperties, MotionEvent downEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_DOWN, pointerCount, pointerProperties,
pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0); pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0);
if (!Device.injectEvent(downEvent, targetDisplayId, Device.INJECT_MODE_ASYNC)) { if (!Device.injectEvent(downEvent, targetDisplayId, this.injectMode)) {
return false; return false;
} }
} }
@ -456,7 +458,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
if (!InputManager.setActionButton(pressEvent, actionButton)) { if (!InputManager.setActionButton(pressEvent, actionButton)) {
return false; return false;
} }
if (!Device.injectEvent(pressEvent, targetDisplayId, Device.INJECT_MODE_ASYNC)) { if (!Device.injectEvent(pressEvent, targetDisplayId, this.injectMode)) {
return false; return false;
} }
@ -470,7 +472,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
if (!InputManager.setActionButton(releaseEvent, actionButton)) { if (!InputManager.setActionButton(releaseEvent, actionButton)) {
return false; return false;
} }
if (!Device.injectEvent(releaseEvent, targetDisplayId, Device.INJECT_MODE_ASYNC)) { if (!Device.injectEvent(releaseEvent, targetDisplayId, this.injectMode)) {
return false; return false;
} }
@ -478,7 +480,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
// Last button released: ACTION_UP // Last button released: ACTION_UP
MotionEvent upEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_UP, pointerCount, pointerProperties, MotionEvent upEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_UP, pointerCount, pointerProperties,
pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0); pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, 0);
if (!Device.injectEvent(upEvent, targetDisplayId, Device.INJECT_MODE_ASYNC)) { if (!Device.injectEvent(upEvent, targetDisplayId, this.injectMode)) {
return false; return false;
} }
} }
@ -489,7 +491,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f,
DEFAULT_DEVICE_ID, 0, source, 0); DEFAULT_DEVICE_ID, 0, source, 0);
return Device.injectEvent(event, targetDisplayId, Device.INJECT_MODE_ASYNC); return Device.injectEvent(event, targetDisplayId, this.injectMode);
} }
private boolean injectScroll(Position position, float hScroll, float vScroll, int buttons) { private boolean injectScroll(Position position, float hScroll, float vScroll, int buttons) {
@ -514,7 +516,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
MotionEvent event = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, MotionEvent event = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f,
DEFAULT_DEVICE_ID, 0, InputDevice.SOURCE_MOUSE, 0); DEFAULT_DEVICE_ID, 0, InputDevice.SOURCE_MOUSE, 0);
return Device.injectEvent(event, targetDisplayId, Device.INJECT_MODE_ASYNC); return Device.injectEvent(event, targetDisplayId, this.injectMode);
} }
/** /**
@ -529,7 +531,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
private boolean pressBackOrTurnScreenOn(int action) { private boolean pressBackOrTurnScreenOn(int action) {
if (displayId == Device.DISPLAY_ID_NONE || Device.isScreenOn(displayId)) { if (displayId == Device.DISPLAY_ID_NONE || Device.isScreenOn(displayId)) {
return injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_ASYNC); return injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, this.injectMode);
} }
// Screen is off // Screen is off
@ -543,7 +545,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
assert displayId != Device.DISPLAY_ID_NONE; assert displayId != Device.DISPLAY_ID_NONE;
scheduleDisplayPowerOff(displayId); scheduleDisplayPowerOff(displayId);
} }
return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC); return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, this.injectMode);
} }
private void getClipboard(int copyKey) { private void getClipboard(int copyKey) {
@ -576,7 +578,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
// On Android >= 7, also press the PASTE key if requested // On Android >= 7, also press the PASTE key if requested
if (paste && Build.VERSION.SDK_INT >= AndroidVersions.API_24_ANDROID_7_0 && supportsInputEvents) { if (paste && Build.VERSION.SDK_INT >= AndroidVersions.API_24_ANDROID_7_0 && supportsInputEvents) {
pressReleaseKeycode(KeyEvent.KEYCODE_PASTE, Device.INJECT_MODE_ASYNC); pressReleaseKeycode(KeyEvent.KEYCODE_PASTE, this.injectMode);
} }
if (sequence != ControlMessage.SEQUENCE_INVALID) { if (sequence != ControlMessage.SEQUENCE_INVALID) {