add displayId setting to InputEvent

This commit is contained in:
evigurskiy 2020-03-02 14:48:39 +03:00
commit 1a122fcce9
3 changed files with 20 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package com.genymobile.scrcpy;
import com.genymobile.scrcpy.wrappers.InputManager; import com.genymobile.scrcpy.wrappers.InputManager;
import android.os.Build;
import android.os.SystemClock; import android.os.SystemClock;
import android.view.InputDevice; import android.view.InputDevice;
import android.view.InputEvent; import android.view.InputEvent;
@ -10,6 +11,8 @@ import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Controller { public class Controller {
@ -178,7 +181,6 @@ public class Controller {
action = MotionEvent.ACTION_POINTER_DOWN | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT); action = MotionEvent.ACTION_POINTER_DOWN | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
} }
} }
MotionEvent event = MotionEvent MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0, .obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0); InputDevice.SOURCE_TOUCHSCREEN, 0);
@ -220,6 +222,9 @@ public class Controller {
} }
private boolean injectEvent(InputEvent event) { private boolean injectEvent(InputEvent event) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
InputManager.setDisplayId(event, device.getDisplayId());
}
return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
} }

View file

@ -22,10 +22,12 @@ public final class Device {
private final ServiceManager serviceManager = new ServiceManager(); private final ServiceManager serviceManager = new ServiceManager();
private final int displayId;
private ScreenInfo screenInfo; private ScreenInfo screenInfo;
private RotationListener rotationListener; private RotationListener rotationListener;
public Device(Options options) { public Device(Options options) {
displayId = options.getDisplayId();
screenInfo = computeScreenInfo(options.getCrop(), options.getMaxSize(), options.getDisplayId()); screenInfo = computeScreenInfo(options.getCrop(), options.getMaxSize(), options.getDisplayId());
registerRotationWatcher(new IRotationWatcher.Stub() { registerRotationWatcher(new IRotationWatcher.Stub() {
@Override @Override
@ -121,6 +123,10 @@ public final class Device {
return Build.MODEL; return Build.MODEL;
} }
public int getDisplayId() {
return displayId;
}
public boolean injectInputEvent(InputEvent inputEvent, int mode) { public boolean injectInputEvent(InputEvent inputEvent, int mode) {
return serviceManager.getInputManager().injectInputEvent(inputEvent, mode); return serviceManager.getInputManager().injectInputEvent(inputEvent, mode);
} }

View file

@ -37,4 +37,12 @@ public final class InputManager {
return false; return false;
} }
} }
public static void setDisplayId(InputEvent inputEvent, int displayId) {
try {
inputEvent.getClass().getMethod("setDisplayId", int.class).invoke(inputEvent, displayId);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e);
}
}
} }