From 1d4eb2446e78847ce6ce63d0d65ad66c557417cb Mon Sep 17 00:00:00 2001 From: ReVanTis Date: Sun, 29 Nov 2026 05:42:01 +0800 Subject: [PATCH] fix(input): a captured touchpad drives the pointer (#2802) * fix(input): a captured touchpad drives the pointer Android reports a captured touchpad's events as SOURCE_TOUCHPAD with the finger positions on the pad and the movement in AXIS_RELATIVE_X/Y. The capture handler only accepted SOURCE_MOUSE_RELATIVE, so every captured touchpad event was dropped and the touchpad stopped working in a session. Read the relative axes for a captured touchpad, and count a touchpad as an attached pointer for the Auto relative mouse choice and for pointer capture. A touchpad that declares SOURCE_TOUCHPAD is not captured for the whole session, so Android keeps its cursor and gestures until a game holds the pointer. * fix(input): keep captured touchpad motion without relative axes Some builds do not provide AXIS_RELATIVE_X/Y for a captured touchpad (the AOSP include_relative_axis_values_for_captured_touchpads flag can leave them out). Fall back to the movement between the pointer's successive absolute positions, tracked per pointer id, and clear the positions when capture changes or the pointer ends. * fix(input): key captured touchpad positions by device too A pointer id is only unique within its device, so the fallback positions could collide between two touchpads or a replaced device. Key them by device id and pointer id, and cover it with a test. * fix(input): end only the touchpad that lifted ACTION_UP and ACTION_CANCEL cleared every device's fallback positions. Forget only the device that ended, so a second touchpad keeps its baseline; a capture change still resets all of them. * Give a captured touchpad taps, two-finger scroll and clickpad presses * Keep captured touchpad clicks apart from mouse buttons A tap's synthetic release shared the mouse's held-button owner, so it could release a mouse button that was still held. Captured touchpad presses now use their own owner, released when capture ends. A physical pad press also keeps a later second finger from turning into a right-click tap. * Keep physical touchpad buttons on the mouse owner A press made before capture was released under the captured owner and stayed held. Physical buttons now always use the mouse owner, and only gesture clicks use the touchpad owner. A physical press also releases a pending tap click first, so the two never merge. --------- Co-authored-by: maxjivi05 --- .../com/droiddeck/launcher/SessionActivity.kt | 85 +++++++++++++++---- .../launcher/input/PointerCapture.kt | 33 +++++++ .../launcher/input/TouchpadGestures.kt | 19 ++++- .../launcher/input/PointerCaptureTest.kt | 77 +++++++++++++++++ .../launcher/input/TouchpadGesturesTest.kt | 75 ++++++++++++++++ 5 files changed, 268 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/input/PointerCapture.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/input/PointerCaptureTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index d6c648c2c0..d332058fb3 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -904,6 +904,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde onCapturedPointer(event) || super.dispatchCapturedPointerEvent(event) override fun dispatchPointerCaptureChanged(hasCapture: Boolean) { super.dispatchPointerCaptureChanged(hasCapture) + capturedTouchpad.cancel() + heldButtons.release(64) Log.i(TAG, "pointer capture " + if (hasCapture) "on" else "off") if (hasCapture) { pointerCaptureRefused = false @@ -3836,7 +3838,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } else -> return super.dispatchGenericMotionEvent(event) } - if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) && !drawerOpen) { + if ((event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) || + event.isFromSource(android.view.InputDevice.SOURCE_TOUCHPAD)) && !drawerOpen) { if (appUiOwnsPointer()) { if (super.dispatchGenericMotionEvent(event)) return true if (touchEditorOpen) return false @@ -4020,7 +4023,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde steamSession = SessionState.mode == SessionService.MODE_STEAM, steamDesktopUi = SessionState.steamUi == "desktop", )) == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD - val mode = com.droiddeck.launcher.input.TouchGestureConfig.gamescopeRelativeMouse(wanted, mouseAttached(), touchpadRoute) + val mode = com.droiddeck.launcher.input.TouchGestureConfig.gamescopeRelativeMouse(wanted, pointerAttached(), touchpadRoute) if (appliedRelativeMouseMode == mode) return if (AgentGuest.setGamescopeConvar(this, "relative_mouse", mode)) appliedRelativeMouseMode = mode else if (relativeMouseRetries++ < RELATIVE_MOUSE_MAX_RETRIES) uiHandler.postDelayed({ requestRelativeMouse() }, 2_000) @@ -4151,6 +4154,27 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } } + private val capturedTouchpadListener: PointerGestures.Listener = object : PointerGestures.Listener by pointerListener { + override fun onMove(x: Float, y: Float) { + val rect = drawnRect() ?: return + val scale = sceneScale(rect) + capturedTouchpad.place(CAPTURED_PAD_CENTRE, CAPTURED_PAD_CENTRE) + onRelativeMouse((x - CAPTURED_PAD_CENTRE) * scale, (y - CAPTURED_PAD_CENTRE) * scale) + } + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { + heldButtons.set(64, button, pressed) + } + override fun onThreeFingerTap(): Boolean = false + } + + private val capturedTouchpad: TouchpadGestures by lazy { + TouchpadGestures(PointerGestures.slop(this), capturedTouchpadListener, holdDrags = false).apply { + bounds = android.graphics.RectF(0f, 0f, CAPTURED_PAD_CENTRE * 2f, CAPTURED_PAD_CENTRE * 2f) + place(CAPTURED_PAD_CENTRE, CAPTURED_PAD_CENTRE) + } + } + private val capturedPadMatrix = android.graphics.Matrix() + private val scrollListener = object : ScrollTouchGestures.Listener { override fun onMove(x: Float, y: Float) { val rect = pointerSurfaceRect() ?: return @@ -5327,7 +5351,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun handleTouch(event: MotionEvent): Boolean { val pen = PenStream.penIndex(event) val penRouted = if (pen >= 0) routePen(event, pen).takeIf { it != PenRouter.Route.TOUCH } else null - if (penRouted == null && event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event) + if (penRouted == null && (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) || + event.isFromSource(android.view.InputDevice.SOURCE_TOUCHPAD))) return onMouse(event) when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { sessionTouchDown = true @@ -5573,7 +5598,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun isHardwareMouse(d: InputDevice?): Boolean = d != null && !d.isVirtual && !d.supportsSource(InputDevice.SOURCE_STYLUS) && - (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) + (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE) || + d.supportsSource(InputDevice.SOURCE_TOUCHPAD)) + + private fun isTouchpad(d: InputDevice?): Boolean = + d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) == true /** Android also sends the Back and Forward buttons as key events, so they are handled as keys. */ private fun guestMouseButton(actionButton: Int): Int? = when (actionButton) { @@ -5583,9 +5612,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde else -> PointerGestures.BTN_LEFT } + private fun pointerAttached(): Boolean = usbMouse.active || InputDevice.getDeviceIds().any { id -> + isHardwareMouse(InputDevice.getDevice(id)) + } + private fun mouseAttached(): Boolean = usbMouse.active || InputDevice.getDeviceIds().any { id -> val d = InputDevice.getDevice(id) - isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true + isHardwareMouse(d) && !isTouchpad(d) } private fun externalMouseAttached(): Boolean { @@ -5603,7 +5636,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun updatePointerCapture() { if (!::surfaceView.isInitialized) return val view = window.decorView - val want = (pointerLocked || relativeMouseAttached()) && sessionOwnsInput() && hasWindowFocus() && mouseAttached() + val want = (pointerLocked || relativeMouseAttached()) && sessionOwnsInput() && hasWindowFocus() && pointerAttached() releaseDesktopMouseGrab() updateUsbMouse(want) if (want == view.hasPointerCapture()) return @@ -5656,7 +5689,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } private fun dropMouseCursor() { - if (!mouseOwnsCursor || mouseAttached()) return + if (!mouseOwnsCursor || pointerAttached()) return mouseOwnsCursor = false uiHandler.removeCallbacks(cursorResync) cursorVisible = false @@ -5707,20 +5740,22 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } private fun onCapturedPointer(event: MotionEvent): Boolean { - if (!event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)) return false + if (!com.droiddeck.launcher.input.PointerCapture.accepts(event)) return false notePress(event) + val pad = com.droiddeck.launcher.input.PointerCapture.isTouchpad(event) + when (event.actionMasked) { + MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { + if (pad && event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) capturedTouchpad.buttonPressed() + guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + } + else -> if (pad) return onCapturedTouchpad(event) + } when (event.actionMasked) { MotionEvent.ACTION_MOVE -> { - var dx = event.x - var dy = event.y - for (i in 0 until event.historySize) { - dx += event.getHistoricalX(i) - dy += event.getHistoricalY(i) - } - onRelativeMouse(dx, dy) + val delta = com.droiddeck.launcher.input.PointerCapture.delta(event) + onRelativeMouse(delta[0], delta[1]) } - MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> - guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> {} MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) if (v != 0f) sendSceneWheel(-Math.round(v)) @@ -5731,6 +5766,21 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde return true } + private fun onCapturedTouchpad(event: MotionEvent): Boolean { + if (event.actionMasked == MotionEvent.ACTION_DOWN) { + val metrics = resources.displayMetrics + val scale = com.droiddeck.launcher.input.PointerCapture.touchpadScale(event, metrics.xdpi / 25.4f, metrics.widthPixels) + capturedPadMatrix.setScale(scale, scale) + capturedTouchpad.desktop = SessionState.mode == SessionService.MODE_DESKTOP + if (::touchpad.isInitialized) capturedTouchpad.sensitivity = touchpad.sensitivity + } + val scaled = MotionEvent.obtain(event) + scaled.transform(capturedPadMatrix) + val handled = capturedTouchpad.onTouch(scaled) + scaled.recycle() + return handled + } + private fun logInputDevice(what: String, deviceId: Int) { val d = InputDevice.getDevice(deviceId) ?: return if (!PadBridge.isFromController(d)) return @@ -7162,6 +7212,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private const val PAD_SCROLL_POINTER_X = 0.985f private const val PAD_SCROLL_HEIGHTS_PER_S = 3f private const val AXIS_UNITS_PER_PIXEL = 3f + private const val CAPTURED_PAD_CENTRE = 1_000_000f private const val KEY_LEFTCTRL = 29 private const val KEY_RIGHTCTRL = 97 private const val KEY_A = 30 diff --git a/app/src/main/java/com/droiddeck/launcher/input/PointerCapture.kt b/app/src/main/java/com/droiddeck/launcher/input/PointerCapture.kt new file mode 100644 index 0000000000..d598a1bc05 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PointerCapture.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.MotionEvent + +object PointerCapture { + fun accepts(event: MotionEvent): Boolean = + event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE) || + event.isFromSource(InputDevice.SOURCE_TOUCHPAD) + + fun isTouchpad(event: MotionEvent): Boolean = event.isFromSource(InputDevice.SOURCE_TOUCHPAD) + + fun delta(event: MotionEvent): FloatArray { + var dx = event.x + var dy = event.y + for (i in 0 until event.historySize) { + dx += event.getHistoricalX(i) + dy += event.getHistoricalY(i) + } + return floatArrayOf(dx, dy) + } + + fun touchpadScale(unitsPerMm: Float, padWidth: Float, pixelsPerMm: Float, screenWidth: Int): Float = when { + unitsPerMm > 0f && pixelsPerMm > 0f -> pixelsPerMm / unitsPerMm + padWidth > 0f && screenWidth > 0 -> screenWidth / padWidth + else -> 1f + } + + fun touchpadScale(event: MotionEvent, pixelsPerMm: Float, screenWidth: Int): Float { + val range = event.device?.getMotionRange(MotionEvent.AXIS_X, InputDevice.SOURCE_TOUCHPAD) + return touchpadScale(range?.resolution ?: 0f, range?.range ?: 0f, pixelsPerMm, screenWidth) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt b/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt index 659b99c0ce..a044469f3b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt @@ -9,6 +9,7 @@ import kotlin.math.hypot class TouchpadGestures( private val slopPx: Float, private val listener: PointerGestures.Listener, + private val holdDrags: Boolean = true, ) { var x = 0f private set @@ -33,6 +34,7 @@ class TouchpadGestures( private var held = false private var secondDownAt = 0L private var secondWasTap = false + private var pressed = false private var twoFingers = false private var scrollAnchorX = 0f private var scrollAnchorY = 0f @@ -90,6 +92,15 @@ class TouchpadGestures( lastTapAt = 0L } + fun buttonPressed() { + pressed = true + handler.removeCallbacks(longPress) + releaseClick() + travelled = slopPx + 1f + secondWasTap = false + lastTapAt = 0L + } + fun place(px: Float, py: Float) { x = px.coerceIn(bounds.left, bounds.right) y = py.coerceIn(bounds.top, bounds.bottom) @@ -115,7 +126,7 @@ class TouchpadGestures( releaseClick() lastX = event.x; lastY = event.y downAt = event.eventTime - travelled = 0f; held = false; twoFingers = false; scrolled = false; secondWasTap = false + travelled = 0f; held = false; twoFingers = false; scrolled = false; secondWasTap = false; pressed = false fingers = 1; pinching = false anchorId = event.getPointerId(0); moverId = -1; holdDrag = false; scrollLocked = false if (clicks && event.eventTime - lastTapAt < TouchTiming.DOUBLE_TAP_MS) { @@ -123,7 +134,7 @@ class TouchpadGestures( press(PointerGestures.BTN_LEFT) } else { dragging = false - if (clicks) handler.postDelayed(longPress, TouchTiming.HOLD_MS) + if (clicks && holdDrags) handler.postDelayed(longPress, TouchTiming.HOLD_MS) } return true } @@ -142,7 +153,7 @@ class TouchpadGestures( if (holdDrag) return true twoFingers = true secondDownAt = event.eventTime - secondWasTap = true + secondWasTap = !pressed scrollAnchorX = (event.getX(0) + event.getX(1)) / 2f scrollAnchorY = (event.getY(0) + event.getY(1)) / 2f pinchSpan = span(event) @@ -153,7 +164,7 @@ class TouchpadGestures( val mi = if (moverId == -1) -1 else event.findPointerIndex(moverId) if (ai >= 0 && mi >= 0) { val mx = event.getX(mi); val my = event.getY(mi) - if (!holdDrag && !scrolled && !scrollLocked) { + if (holdDrags && !holdDrag && !scrolled && !scrollLocked) { val anchorMoved = abs(event.getX(ai) - anchorStartX) + abs(event.getY(ai) - anchorStartY) val moverMoved = abs(mx - moverStartX) + abs(my - moverStartY) if (anchorMoved >= slopPx) { diff --git a/app/src/test/java/com/droiddeck/launcher/input/PointerCaptureTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PointerCaptureTest.kt new file mode 100644 index 0000000000..3d85e8c259 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PointerCaptureTest.kt @@ -0,0 +1,77 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.MotionEvent +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class PointerCaptureTest { + @Test fun aCapturedMouseReportsItsPositionAsMovement() { + val event = event(MotionEvent.ACTION_MOVE, InputDevice.SOURCE_MOUSE_RELATIVE, x = 12f, y = -7f) + try { + assertTrue(PointerCapture.accepts(event)) + assertFalse(PointerCapture.isTouchpad(event)) + assertArrayEquals(floatArrayOf(12f, -7f), PointerCapture.delta(event), 0.001f) + } finally { + event.recycle() + } + } + + @Test fun batchedMouseSamplesAddUp() { + val event = event(MotionEvent.ACTION_MOVE, InputDevice.SOURCE_MOUSE_RELATIVE, x = 3f, y = -5f) + try { + event.addBatch(1L, arrayOf(MotionEvent.PointerCoords().apply { x = 2f; y = 4f }), 0) + assertArrayEquals(floatArrayOf(5f, -1f), PointerCapture.delta(event), 0.001f) + } finally { + event.recycle() + } + } + + @Test fun aCapturedTouchpadIsACapturedPointer() { + val event = event(MotionEvent.ACTION_DOWN, InputDevice.SOURCE_TOUCHPAD, x = 800f, y = 400f) + try { + assertTrue(PointerCapture.accepts(event)) + assertTrue(PointerCapture.isTouchpad(event)) + } finally { + event.recycle() + } + } + + @Test fun anUncapturedMouseIsNotACapturedPointer() { + val event = event(MotionEvent.ACTION_HOVER_MOVE, InputDevice.SOURCE_MOUSE, x = 12f, y = -7f) + try { + assertFalse(PointerCapture.accepts(event)) + } finally { + event.recycle() + } + } + + @Test fun touchpadTravelMatchesTheSameDistanceOnScreen() { + assertEquals(0.5f, PointerCapture.touchpadScale(unitsPerMm = 20f, padWidth = 2000f, pixelsPerMm = 10f, screenWidth = 1920), 0.001f) + } + + @Test fun aTouchpadWithoutAResolutionSpansTheScreen() { + assertEquals(1.92f, PointerCapture.touchpadScale(unitsPerMm = 0f, padWidth = 1000f, pixelsPerMm = 10f, screenWidth = 1920), 0.001f) + } + + @Test fun aTouchpadWithoutARangeKeepsItsUnits() { + assertEquals(1f, PointerCapture.touchpadScale(unitsPerMm = 0f, padWidth = 0f, pixelsPerMm = 10f, screenWidth = 1920), 0.001f) + } + + private fun event(action: Int, source: Int, x: Float, y: Float): MotionEvent { + val properties = arrayOf(MotionEvent.PointerProperties().apply { + id = 0 + toolType = MotionEvent.TOOL_TYPE_FINGER + }) + val coords = arrayOf(MotionEvent.PointerCoords().apply { this.x = x; this.y = y }) + return MotionEvent.obtain(0L, 0L, action, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, source, 0) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt index c967ce02fd..80df39e48c 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt @@ -331,6 +331,81 @@ class TouchpadGesturesTest { assertEquals(listOf("left down", "left up", "left down"), events) } + @Test fun aRestingFingerNeverDragsWhenHoldDragsAreOff() { + touchpad = TouchpadGestures(SLOP, listener, holdDrags = false) + touchpad.bounds = RectF(0f, 0f, 1920f, 1080f) + touchpad.place(960f, 540f) + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(TouchTiming.HOLD_MS + 100) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 300f), 2 to (620f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 300f), 2 to (640f to 300f)) + assertFalse(events.any { it.startsWith("left") || it == "long press" }) + } + + @Test fun tapsStillClickWhenHoldDragsAreOff() { + touchpad = TouchpadGestures(SLOP, listener, holdDrags = false) + tap() + assertEquals(listOf("left down"), events) + } + + @Test fun aPhysicalButtonPressIsNotAlsoATap() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + touchpad.buttonPressed() + advance(80) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + advance(TouchTiming.HOLD_MS + 100) + assertTrue(events.isEmpty()) + } + + @Test fun aPhysicalButtonPressDuringTwoFingersIsNotARightClick() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + touchpad.buttonPressed() + advance(40) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + assertTrue(events.isEmpty()) + } + + @Test fun aSecondFingerAfterAPhysicalButtonPressIsNotARightClick() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + touchpad.buttonPressed() + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(40) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + advance(TouchTiming.HOLD_MS + 100) + assertTrue(events.isEmpty()) + twoFingerTapStillRightClicks() + } + + @Test fun aPhysicalButtonPressReleasesAPendingTapClickFirst() { + tap() + assertEquals(listOf("left down"), events) + touchpad.buttonPressed() + assertEquals(listOf("left down", "left up"), events) + advance(TouchTiming.CLICK_MS + 100) + assertEquals(listOf("left down", "left up"), events) + } + + private fun twoFingerTapStillRightClicks() { + advance(1000) + events.clear() + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(40) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + advance(TouchTiming.HOLD_MS + 100) + assertTrue("right down" in events) + } + private fun tap() { touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) advance(80)