diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 09ae78546a..5f39d03c6c 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -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 diff --git a/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt index 261c88a74b..9f8240640f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt @@ -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() @@ -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) diff --git a/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt index 625f93e7cf..ba7e666c6c 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt @@ -8,6 +8,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder @RunWith(RobolectricTestRunner::class) @Config(sdk = [34], manifest = Config.NONE) @@ -151,4 +152,47 @@ class MouseBackInputTest { assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = true)) assertTrue(presses.isEmpty()) } + + private fun device(name: String, sources: Int, external: Boolean = true, virtual: Boolean = false) = + InputDeviceBuilder.newBuilder().setId(if (virtual) -1 else 11).setName(name).setDescriptor(name) + .setExternal(external).setSources(sources).build() + + @Test fun anExternalMouseCounts() { + assertTrue(MouseBackInput.isExternalMouse(device("USB mouse", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Bluetooth mouse", InputDevice.SOURCE_MOUSE_RELATIVE))) + } + + @Test fun theLoggedControllerDeviceDoesNotCountAsAnExternalMouse() { + val odinController = device("Odin Controller", 0x1000511) + assertTrue(PadBridge.isFromController(odinController)) + assertFalse(MouseBackInput.isExternalMouse(odinController)) + } + + @Test fun virtualTouchpadTouchscreenUinputAndNonMouseDevicesDoNotCount() { + assertFalse(MouseBackInput.isExternalMouse(device("virtual mouse", InputDevice.SOURCE_MOUSE, virtual = true))) + assertFalse(MouseBackInput.isExternalMouse(device("touchpad", InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_TOUCHPAD))) + assertFalse(MouseBackInput.isExternalMouse(device("touchscreen", InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_TOUCHSCREEN))) + assertFalse(MouseBackInput.isExternalMouse(device("uinput-mouse", InputDevice.SOURCE_MOUSE))) + assertFalse(MouseBackInput.isExternalMouse(device("gamepad", InputDevice.SOURCE_GAMEPAD))) + assertFalse(MouseBackInput.isExternalMouse(null)) + } + + @Test fun aMouseThatNamesItselfVirtualDoesNotCount() { + assertFalse(MouseBackInput.isExternalMouse(device("ODIN Station Virtual Mouse", 0x20004))) + assertFalse(MouseBackInput.isExternalMouse(device("VIRTUAL pointer", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Virtuality Pro Mouse", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Logitech MX Master 3", InputDevice.SOURCE_MOUSE))) + } + + @Test fun injectedBackIsLeftAloneWithoutAnExternalMouse() { + val odinController = device("Odin Controller", 0x1000511) + val controllerOnly = MouseBackInput(presses::add) { MouseBackInput.isExternalMouse(odinController) } + assertFalse(controllerOnly.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertTrue(presses.isEmpty()) + val external = MouseBackInput(presses::add) { + MouseBackInput.isExternalMouse(device("USB mouse", InputDevice.SOURCE_MOUSE)) + } + assertTrue(external.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertEquals(listOf(true), presses) + } }