From 57b3623a9a694a38685d5db2f41b0d4eb350897f Mon Sep 17 00:00:00 2001 From: Omar Al Taki Date: Sat, 31 Oct 2026 02:21:49 +0400 Subject: [PATCH] fix(input): mouse right click no longer acts as Back in REDMAGIC cast mode REDMAGIC's cast mode replaces a mouse's right button with a Back key that names no device and lacks the virtual-hard-key flag that the navigation bar and back gesture set. During a session, a Back from a mouse, or that injected form while a mouse is connected, now presses the guest's right button. The navigation bar, back gesture and controller Back still act as Back, and so does a right click while a DroidDeck menu is open. A mouse's Back and Forward buttons no longer also send a left click; Android delivers them as key events. --- .../com/droiddeck/launcher/SessionActivity.kt | 43 ++++--- .../launcher/input/MouseBackInput.kt | 39 +++++++ .../launcher/input/MouseBackInputTest.kt | 107 ++++++++++++++++++ 3 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 1b4b59df1e..82e692f5f1 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -330,6 +330,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onCancel = { padBridge?.cancelBackQam() }, holds = { backHold }, ) + private val mouseBackInput = com.droiddeck.launcher.input.MouseBackInput( + sendRight = { down -> sendSceneButton(PointerGestures.BTN_RIGHT, down) }, + mouseAttached = ::mouseAttached, + ) private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN private var drawerDirectionDownTime = 0L private var drawerDirectionDeviceId = -1 @@ -2575,6 +2579,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return super.dispatchKeyEvent(event) } } + if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { + Log.i(TAG, String.format(java.util.Locale.ROOT, "back key device=%d (%s) source=0x%x flags=0x%x", + event.deviceId, event.device?.name, event.source, event.flags)) + } + val mouseToGuest = !pipUi && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && + !(::loading.isInitialized && loading.ended) + if (mouseBackInput.onKeyEvent(event, mouseToGuest)) return true val backToQam = !pipUi && !screenKeyTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen && !SessionState.suspended && padBridge?.canHoldQam() == true @@ -3797,14 +3808,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) - val button = when (event.actionButton) { - MotionEvent.BUTTON_SECONDARY -> PointerGestures.BTN_RIGHT - MotionEvent.BUTTON_TERTIARY -> PointerGestures.BTN_MIDDLE - MotionEvent.BUTTON_BACK -> BTN_SIDE - MotionEvent.BUTTON_FORWARD -> BTN_EXTRA - else -> PointerGestures.BTN_LEFT - } - sendSceneButton(button, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) + guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) @@ -3819,7 +3823,18 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun isHardwareMouse(d: InputDevice?): Boolean = d != null && !d.isVirtual && (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) - private fun mouseAttached(): Boolean = InputDevice.getDeviceIds().any { isHardwareMouse(InputDevice.getDevice(it)) } + /** 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) { + MotionEvent.BUTTON_SECONDARY -> PointerGestures.BTN_RIGHT + MotionEvent.BUTTON_TERTIARY -> PointerGestures.BTN_MIDDLE + MotionEvent.BUTTON_BACK, MotionEvent.BUTTON_FORWARD -> null + else -> PointerGestures.BTN_LEFT + } + + private fun mouseAttached(): Boolean = InputDevice.getDeviceIds().any { id -> + val d = InputDevice.getDevice(id) + isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true + } private fun relativeMouseAttached(): Boolean = !isInMultiWindowMode && !glassesOutputActive && InputDevice.getDeviceIds().any { id -> val d = InputDevice.getDevice(id) @@ -3917,14 +3932,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { moveCapturedMouse(dx, dy) } } - MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { - val button = when (event.actionButton) { - MotionEvent.BUTTON_SECONDARY -> PointerGestures.BTN_RIGHT - MotionEvent.BUTTON_TERTIARY -> PointerGestures.BTN_MIDDLE - else -> PointerGestures.BTN_LEFT - } - sendSceneButton(button, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) - } + MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> + guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) if (v != 0f) sendSceneWheel(-Math.round(v)) diff --git a/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt new file mode 100644 index 0000000000..4e2450f1c8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt @@ -0,0 +1,39 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.KeyCharacterMap +import android.view.KeyEvent + +/** + * Some firmware turns a mouse's right button into Back. Most report it from the mouse itself; REDMAGIC's + * cast mode injects it from no device and without the virtual-hard-key flag that the navigation bar and + * back gesture set, so that form only counts while a mouse is connected. + */ +class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouseAttached: () -> Boolean) { + private val held = mutableSetOf() + + fun onKeyEvent(event: KeyEvent, toGuest: Boolean): Boolean { + if (event.keyCode != KeyEvent.KEYCODE_BACK) return false + val device = event.deviceId + if (device in held) { + if (event.action == KeyEvent.ACTION_UP) { + held.remove(device) + sendRight(false) + } + return true + } + if (event.action != KeyEvent.ACTION_DOWN || event.repeatCount > 0 || !toGuest) return false + if (!isFromMouse(event) && !(isInjectedBack(event) && mouseAttached())) return false + held.add(device) + sendRight(true) + return true + } + + companion object { + fun isFromMouse(event: KeyEvent): Boolean = + event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE) + + fun isInjectedBack(event: KeyEvent): Boolean = + event.deviceId == KeyCharacterMap.VIRTUAL_KEYBOARD && (event.flags and KeyEvent.FLAG_VIRTUAL_HARD_KEY) == 0 + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt new file mode 100644 index 0000000000..a955b9db57 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.KeyCharacterMap +import android.view.KeyEvent +import org.junit.Assert.* +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 MouseBackInputTest { + private val presses = mutableListOf() + private var mouse = true + private val input = MouseBackInput(presses::add) { mouse } + + private fun key(action: Int, source: Int = InputDevice.SOURCE_MOUSE, device: Int = 7, repeat: Int = 0, + code: Int = KeyEvent.KEYCODE_BACK, flags: Int = 0) = + KeyEvent(0L, 0L, action, code, repeat, 0, device, 0, flags, source) + + /** What REDMAGIC's cast mode sends for a right click, as logged on an NX789J. */ + private fun injected(action: Int, repeat: Int = 0) = key(action, source = InputDevice.SOURCE_KEYBOARD, + device = KeyCharacterMap.VIRTUAL_KEYBOARD, repeat = repeat, flags = KeyEvent.FLAG_FROM_SYSTEM) + + /** What the navigation bar and back gesture send, as logged on the same phone. */ + private fun navigationBar(action: Int) = key(action, source = InputDevice.SOURCE_KEYBOARD, + device = KeyCharacterMap.VIRTUAL_KEYBOARD, flags = KeyEvent.FLAG_FROM_SYSTEM or KeyEvent.FLAG_VIRTUAL_HARD_KEY) + + @Test fun mouseBackBecomesTheRightButton() { + assertTrue(input.onKeyEvent(key(KeyEvent.ACTION_DOWN), toGuest = true)) + assertTrue(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = true)) + assertEquals(listOf(true, false), presses) + } + + @Test fun holdingTheButtonSwallowsRepeatsWithoutPressingAgain() { + input.onKeyEvent(key(KeyEvent.ACTION_DOWN), toGuest = true) + repeat(3) { assertTrue(input.onKeyEvent(key(KeyEvent.ACTION_DOWN, repeat = it + 1), toGuest = true)) } + input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = true) + assertEquals(listOf(true, false), presses) + } + + @Test fun relativeMouseBackAlsoBecomesTheRightButton() { + assertTrue(input.onKeyEvent(key(KeyEvent.ACTION_DOWN, source = InputDevice.SOURCE_MOUSE_RELATIVE), toGuest = true)) + assertEquals(listOf(true), presses) + } + + @Test fun injectedBackBecomesTheRightButtonWhileAMouseIsConnected() { + assertTrue(input.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertTrue(input.onKeyEvent(injected(KeyEvent.ACTION_DOWN, repeat = 1), toGuest = true)) + assertTrue(input.onKeyEvent(injected(KeyEvent.ACTION_UP), toGuest = true)) + assertEquals(listOf(true, false), presses) + } + + @Test fun injectedBackStaysBackWithoutAMouse() { + mouse = false + assertFalse(input.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertFalse(input.onKeyEvent(injected(KeyEvent.ACTION_UP), toGuest = true)) + assertTrue(presses.isEmpty()) + } + + @Test fun navigationBarBackStaysBackEvenWithAMouse() { + for (action in listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) { + assertFalse(input.onKeyEvent(navigationBar(action), toGuest = true)) + } + assertTrue(presses.isEmpty()) + } + + @Test fun controllerBackStaysBack() { + val source = InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_KEYBOARD + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_DOWN, source = source), toGuest = true)) + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_UP, source = source), toGuest = true)) + assertTrue(presses.isEmpty()) + } + + @Test fun otherMouseKeysAreLeftAlone() { + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_DOWN, code = KeyEvent.KEYCODE_FORWARD), toGuest = true)) + assertTrue(presses.isEmpty()) + } + + @Test fun mouseBackStaysBackWhileAMenuIsOpen() { + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_DOWN), toGuest = false)) + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_DOWN, repeat = 1), toGuest = false)) + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = false)) + assertTrue(presses.isEmpty()) + } + + @Test fun aMenuOpeningMidPressStillReleasesTheButton() { + input.onKeyEvent(key(KeyEvent.ACTION_DOWN), toGuest = true) + assertTrue(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = false)) + assertEquals(listOf(true, false), presses) + } + + @Test fun theMouseDisconnectingMidPressStillReleasesTheButton() { + input.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true) + mouse = false + assertTrue(input.onKeyEvent(injected(KeyEvent.ACTION_UP), toGuest = true)) + assertEquals(listOf(true, false), presses) + } + + @Test fun aMenuClosingMidPressLeavesTheReleaseToTheMenu() { + input.onKeyEvent(key(KeyEvent.ACTION_DOWN), toGuest = false) + assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = true)) + assertTrue(presses.isEmpty()) + } +}