fix(input): system Back only turns into a right-click with an external mouse (#2323)

* fix(input): injected Back is a right-click only while an external mouse is attached

* input: order imports

* fix(input): reject built-in devices for injected Back

* fix(input): log matched mouse for injected Back

* fix(input): ignore mice that name themselves virtual for injected Back
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-10 11:55:38 -05:00
1 parent 40242fa082
commit 9bf638a3c4
3 files changed
+85 -3

No files matched your search

@@ -391,8 +391,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
)
private val mouseBackInput = com.droiddeck.launcher.input.MouseBackInput(
sendRight = { down -> heldButtons.set(32, PointerGestures.BTN_RIGHT, down) },
mouseAttached = ::mouseAttached,
mouseAttached = ::externalMouseAttached,
)
private var lastExternalMouse: InputDevice? = null
private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN
private var drawerDirectionDownTime = 0L
private var drawerDirectionDeviceId = -1
@@ -2683,7 +2684,22 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
val mouseToGuest = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen &&
!SessionState.suspended && !(::loading.isInitialized && (loading.visible || loading.ended))
if (mouseBackInput.onKeyEvent(event, mouseToGuest)) return true
if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0 &&
com.droiddeck.launcher.input.MouseBackInput.isInjectedBack(event)) lastExternalMouse = null
val mouseBackHandled = mouseBackInput.onKeyEvent(event, mouseToGuest)
if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0 &&
com.droiddeck.launcher.input.MouseBackInput.isInjectedBack(event) && mouseToGuest) {
val device = lastExternalMouse
if (mouseBackHandled && device != null) {
Log.i(TAG, String.format(java.util.Locale.ROOT,
"injected Back right-click mouse=\"%s\" (%04x:%04x, id %d, sources 0x%x, external=%s, virtual=%s)",
device.name, device.vendorId, device.productId, device.id, device.sources,
Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || device.isExternal, device.isVirtual))
} else if (!mouseBackHandled) {
Log.i(TAG, "injected Back no external mouse found")
}
}
if (mouseBackHandled) return true
val backToQam = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen &&
!inputSettingsOpen && !gestureSettingsOpen &&
SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen &&
@@ -4163,6 +4179,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true
}
private fun externalMouseAttached(): Boolean {
lastExternalMouse = InputDevice.getDeviceIds().map { InputDevice.getDevice(it) }
.firstOrNull { device -> device != null && com.droiddeck.launcher.input.MouseBackInput.isExternalMouse(device) }
return lastExternalMouse != null
}
private fun relativeMouseAttached(): Boolean = !isInMultiWindowMode && !glassesOutputActive && InputDevice.getDeviceIds().any { id ->
val d = InputDevice.getDevice(id)
isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true
@@ -1,5 +1,6 @@
package com.droiddeck.launcher.input
import android.os.Build
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
@@ -7,7 +8,7 @@ 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.
* back gesture set, so that form only counts while an external mouse is connected.
*/
class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouseAttached: () -> Boolean) {
private val held = mutableSetOf<Int>()
@@ -45,6 +46,21 @@ class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouse
}
companion object {
fun isExternalMouse(device: InputDevice?): Boolean {
if (device == null || device.isVirtual || PadBridge.isSystemVirtualInputName(device.name) ||
declaresVirtual(device.name) ||
PadBridge.isFromController(device) ||
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !device.isExternal)) return false
return (device.supportsSource(InputDevice.SOURCE_MOUSE) ||
device.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) &&
!device.supportsSource(InputDevice.SOURCE_TOUCHPAD) &&
!device.supportsSource(InputDevice.SOURCE_TOUCHSCREEN)
}
private val VIRTUAL_NAME = Regex("\\bvirtual\\b", RegexOption.IGNORE_CASE)
fun declaresVirtual(name: String?): Boolean = name != null && VIRTUAL_NAME.containsMatchIn(name)
fun isFromMouse(event: KeyEvent): Boolean =
event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)