|
17 | 17 | import java.lang.reflect.InvocationTargetException; |
18 | 18 | import java.lang.reflect.Method; |
19 | 19 |
|
| 20 | +import xtr.keymapper.server.event.MouseEventHandler; |
| 21 | + |
20 | 22 | public class Input { |
21 | 23 |
|
22 | 24 | static Method injectInputEventMethod; |
23 | 25 | static Object inputManager; |
24 | 26 | static Method setDisplayIdMethod; |
| 27 | + static Method setActionButtonMethod; |
25 | 28 |
|
26 | 29 | private final PointersState pointersState = new PointersState(); |
27 | 30 | private final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[PointersState.MAX_POINTERS]; |
@@ -102,6 +105,114 @@ public void injectTouch(int action, int pointerId, float pressure, float x, floa |
102 | 105 | injectInputEvent(motionEvent); |
103 | 106 | } |
104 | 107 |
|
| 108 | + void injectRightClick(long pointerId, float x, float y, boolean pressed) { |
| 109 | + long now = SystemClock.uptimeMillis(); |
| 110 | + |
| 111 | + final float pressure = 1.0f; |
| 112 | + final int actionButton = MotionEvent.BUTTON_SECONDARY; |
| 113 | + int action = pressed ? MotionEvent.ACTION_DOWN : MotionEvent.ACTION_UP; |
| 114 | + int buttons = pressed ? MotionEvent.BUTTON_SECONDARY : 0; |
| 115 | + |
| 116 | + Point point = new Point(x, y); |
| 117 | + |
| 118 | + int pointerIndex = pointersState.getPointerIndex(pointerId); |
| 119 | + if (pointerIndex == -1) { |
| 120 | + Log.e(RemoteService.TAG, "Too many pointers for touch event"); |
| 121 | + } |
| 122 | + |
| 123 | + Pointer pointer = pointersState.get(pointerIndex); |
| 124 | + pointer.setPoint(point); |
| 125 | + pointer.setPressure(pressure); |
| 126 | + |
| 127 | + int source; |
| 128 | + if (pointerId == MouseEventHandler.pointerId) { |
| 129 | + // real mouse event, or event incompatible with a finger |
| 130 | + pointerProperties[pointerIndex].toolType = MotionEvent.TOOL_TYPE_MOUSE; |
| 131 | + source = InputDevice.SOURCE_MOUSE; |
| 132 | + pointer.setUp(buttons == 0); |
| 133 | + } else { |
| 134 | + // POINTER_ID_GENERIC_FINGER, POINTER_ID_VIRTUAL_FINGER or real touch from device |
| 135 | + pointerProperties[pointerIndex].toolType = MotionEvent.TOOL_TYPE_FINGER; |
| 136 | + source = InputDevice.SOURCE_TOUCHSCREEN; |
| 137 | + // Buttons must not be set for touch events |
| 138 | + buttons = 0; |
| 139 | + pointer.setUp(action == MotionEvent.ACTION_UP); |
| 140 | + } |
| 141 | + |
| 142 | + int pointerCount = pointersState.update(pointerProperties, pointerCoords); |
| 143 | + if (pointerCount == 1) { |
| 144 | + if (action == MotionEvent.ACTION_DOWN) { |
| 145 | + lastTouchDown = now; |
| 146 | + } |
| 147 | + } else { |
| 148 | + // secondary pointers must use ACTION_POINTER_* ORed with the pointerIndex |
| 149 | + if (action == MotionEvent.ACTION_UP) { |
| 150 | + action = MotionEvent.ACTION_POINTER_UP | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT); |
| 151 | + } else { |
| 152 | + action = MotionEvent.ACTION_POINTER_DOWN | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /* If the input device is a mouse (on API >= 23): |
| 157 | + * - the first button pressed must first generate ACTION_DOWN; |
| 158 | + * - all button pressed (including the first one) must generate ACTION_BUTTON_PRESS; |
| 159 | + * - all button released (including the last one) must generate ACTION_BUTTON_RELEASE; |
| 160 | + * - the last button released must in addition generate ACTION_UP. |
| 161 | + * |
| 162 | + * Otherwise, Chrome does not work properly: <https://github.com/Genymobile/scrcpy/issues/3635> |
| 163 | + */ |
| 164 | + if (source == InputDevice.SOURCE_MOUSE) { |
| 165 | + if (action == MotionEvent.ACTION_DOWN) { |
| 166 | + // First button pressed: ACTION_DOWN |
| 167 | + MotionEvent downEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_DOWN, pointerCount, pointerProperties, |
| 168 | + pointerCoords, 0, buttons, 1f, 1f, 0, 0, source, 0); |
| 169 | + injectInputEvent(downEvent); |
| 170 | + |
| 171 | + // Any button pressed: ACTION_BUTTON_PRESS |
| 172 | + MotionEvent pressEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_BUTTON_PRESS, pointerCount, pointerProperties, |
| 173 | + pointerCoords, 0, buttons, 1f, 1f, 0, 0, source, 0); |
| 174 | + try { |
| 175 | + if (setActionButtonMethod != null) { |
| 176 | + setActionButtonMethod.invoke(pressEvent, actionButton); |
| 177 | + } |
| 178 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 179 | + Log.e(RemoteService.TAG, e.getMessage(), e); |
| 180 | + } |
| 181 | + |
| 182 | + injectInputEvent(pressEvent); |
| 183 | + |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + if (action == MotionEvent.ACTION_UP) { |
| 188 | + // Any button released: ACTION_BUTTON_RELEASE |
| 189 | + MotionEvent releaseEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_BUTTON_RELEASE, pointerCount, pointerProperties, |
| 190 | + pointerCoords, 0, buttons, 1f, 1f, 0, 0, source, 0); |
| 191 | + try { |
| 192 | + if (setActionButtonMethod != null) { |
| 193 | + setActionButtonMethod.invoke(releaseEvent, actionButton); |
| 194 | + } |
| 195 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 196 | + Log.e(RemoteService.TAG, e.getMessage(), e); |
| 197 | + } |
| 198 | + |
| 199 | + injectInputEvent(releaseEvent); |
| 200 | + |
| 201 | + // Last button released: ACTION_UP |
| 202 | + MotionEvent upEvent = MotionEvent.obtain(lastTouchDown, now, MotionEvent.ACTION_UP, pointerCount, pointerProperties, |
| 203 | + pointerCoords, 0, buttons, 1f, 1f, 0, 0, source, 0); |
| 204 | + injectInputEvent(upEvent); |
| 205 | + |
| 206 | + return; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, |
| 211 | + 0, 0, source, 0); |
| 212 | + injectInputEvent(event); |
| 213 | + } |
| 214 | + |
| 215 | + |
105 | 216 | public void onScrollEvent(float x, float y, float value){ |
106 | 217 | mScrollThread.onScrollEvent(x, y, value); |
107 | 218 | } |
@@ -239,7 +350,8 @@ private void next() { |
239 | 350 | setDisplayIdMethod = MotionEvent.class.getDeclaredMethod(methodName, Integer.TYPE); |
240 | 351 | setDisplayIdMethod.setAccessible(true); |
241 | 352 | } |
242 | | - |
| 353 | + setActionButtonMethod = MotionEvent.class.getDeclaredMethod("setActionButton", int.class); |
| 354 | + setActionButtonMethod.setAccessible(true); |
243 | 355 | } catch (Exception e) { |
244 | 356 | Log.e(RemoteService.TAG, e.getMessage(), e); |
245 | 357 | } |
|
0 commit comments