From a8aff1c1a30e3e5b75c4746059c2bd4c686130bc Mon Sep 17 00:00:00 2001 From: OmarAlTaki1 <144649942+OmarAlTaki1@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:21:15 +0400 Subject: [PATCH] fix(input): read a USB mouse directly where Android won't capture it, as in REDMAGIC cast mode (#2298) * fix(input): read a USB mouse directly where Android won't capture it, as in REDMAGIC cast mode REDMAGIC's cast mode shows the session on a virtual display while its own mirror activity keeps focus on the phone's display. Android only grants pointer capture on the focused display, so the session got a cursor that stopped at the screen edge and the camera stopped turning in mouse-look games. When capture is wanted but not granted within 500 ms, the session claims the mouse's USB HID boot interface instead, reads its reports with the layout from its report descriptor (boot protocol if that can't be read), and sends motion, five buttons and the wheel the way captured motion goes. The mouse goes back to Android whenever capture is no longer wanted. * feat(input): hold the middle button still to open the session menu while the mouse is read over USB While the session reads the mouse over USB, Android has no cursor, so the mouse can't reach REDMAGIC's cast-mode panel or anything else outside the session. Holding the middle button for two seconds without moving opens the session menu, which hands the mouse back to Android until the menu closes. Quick middle clicks and middle-button drags still reach the game. Relative motion sent to a locked pointer now hides the session's own cursor, as pad pointer motion already does. A desktop keeps its grab while the menu is open, so a click made with Android's cursor on the way back showed the session cursor, which then stayed where the click was. * fix(input): give a USB mouse back to Android when the app starts after being killed while reading it A process killed while it reads the mouse, by a crash or an update, leaves the mouse's interface with no driver, so Android has no mouse until it is replugged. At startup, any permitted USB boot mouse that can be claimed without force has no driver, and releasing it reattaches Android's. * fix(input): only sessions shown off the phone's screen fall back to reading the USB mouse Android grants pointer capture on the focused display, which the phone's own screen is, so a session there never needs the USB mouse. Checking the display keeps a slow capture grant from ever asking for the mouse's USB access there. * fix(input): reject out-of-range HID report sizes and counts, and release USB buttons when the mouse is lost A report descriptor can give any 32-bit report count or size, which drove the field loop for billions of iterations and could overflow bit offsets into a negative index on the reader thread. Items outside 32 bits and 4096 fields are rejected, so such a mouse falls back to the boot protocol. A mouse that stops responding while it is read over USB now releases the buttons it held and cancels a pending menu hold, through a separate onLost callback, since the session's own release path only runs while the grab is still active. * test(input): an oversized report count is rejected even when X and Y follow it The earlier case had no X or Y fields, so it returned null without the count check too. A 4097-field item before valid X and Y is now rejected, and a 4096-field one still parses. * fix(input): Auto relative mouse sees a mouse read over USB, and re-checks when it is lost The Auto relative-mouse decision from #2305 reads mouseAttached() on the relative-mouse thread, which counts a mouse read over USB, so the grab's state is now volatile. Losing that mouse re-runs the decision, as removing an Android mouse does. * fix(input): set up the USB mouse on its reader thread, select report protocol, and retry a failed permission request Opening the receiver and its descriptor and protocol transfers now run on the reader thread, so an unresponsive receiver can't stall the session for up to two seconds. A release during setup lets the thread clean up as it already did. A mouse that fails setup isn't tried again until it is replugged, since releasing it re-adds Android's mouse, which would start another attempt. A parsed report descriptor now selects report protocol first: HID 1.11 7.2.6 says a host must not assume the protocol a device is in. A permission request that throws no longer marks the mouse as asked. * fix(input): a cancelled or overlapping USB reader can't claim the mouse or mark it failed A new reader waits for the previous one to let go of the interface, so a quick release and grab, like toggling the session menu, no longer fails the claim. A reader released before or during setup doesn't claim the interface or keeps going. Failing to open or claim leaves the mouse with Android without marking it failed, since nothing re-adds it to start another attempt; only a failure after claiming, or a lost mouse, marks it, and only for the current reader. If SET_PROTOCOL(report) fails, setup tries boot protocol before giving up, so the decoder always matches the protocol the mouse was set to. * fix(input): release can't slip between the USB reader's cancellation check and its claim The reader checked `running` and then claimed the interface, so a release in between still let it claim and start setup. The check and claim now share a lock with release, so the reader either never claims or claims first and sees the release before setup. * fix(input): hand the mouse back to Android while the WoW chat bar is on top appUiOwnsPointer() already shows Android's pointer for the on-top chat bar, but pointer capture, and now the USB mouse, stayed on, so that pointer couldn't reach the bar. The bar on top now ends capture and USB reading like the session menus do, and its changes re-run the capture decision. --- .../main/java/com/droiddeck/launcher/App.kt | 1 + .../com/droiddeck/launcher/SessionActivity.kt | 82 +++++- .../launcher/input/HidMouseReport.kt | 158 +++++++++++ .../droiddeck/launcher/input/MouseMenuHold.kt | 46 +++ .../com/droiddeck/launcher/input/UsbMouse.kt | 262 ++++++++++++++++++ .../launcher/input/HidMouseReportTest.kt | 65 +++++ .../launcher/input/MouseMenuHoldTest.kt | 54 ++++ 7 files changed, 655 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/input/HidMouseReport.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/input/MouseMenuHold.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/input/UsbMouse.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/input/HidMouseReportTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/input/MouseMenuHoldTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/App.kt b/app/src/main/java/com/droiddeck/launcher/App.kt index 4b18fa9710..22c89a48d1 100644 --- a/app/src/main/java/com/droiddeck/launcher/App.kt +++ b/app/src/main/java/com/droiddeck/launcher/App.kt @@ -56,6 +56,7 @@ class App : Application() { com.droiddeck.launcher.core.DownloadSource.init(this) prefetchAddonPacks() GpuClockPin.clearLeftover(this) + com.droiddeck.launcher.input.UsbMouse.returnLeftover(this) com.droiddeck.launcher.glasses.Glasses.init(this) com.droiddeck.launcher.glasses.GlassesMenuMirror.initialize(this) BwrapSpawner.start(this) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 3775b229b2..b28607f7d8 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -407,6 +407,38 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde mouseAttached = ::externalMouseAttached, ) private var lastExternalMouse: InputDevice? = null + private val usbMenuHold = com.droiddeck.launcher.input.MouseMenuHold( + schedule = { runnable, ms -> uiHandler.postDelayed(runnable, ms) }, + cancel = { uiHandler.removeCallbacks(it) }, + onMenu = { releasePads(); drawerOpen = true }, + ) + private val usbMouse = com.droiddeck.launcher.input.UsbMouse(this, object : com.droiddeck.launcher.input.UsbMouse.Listener { + override fun onMove(dx: Int, dy: Int) { + usbMenuHold.onMove(dx, dy) + onRelativeMouse(dx.toFloat(), dy.toFloat()) + } + override fun onButton(index: Int, down: Boolean) { + if (down) lastUserPressMs = android.os.SystemClock.uptimeMillis() + heldButtons.set(8, USB_MOUSE_BUTTONS[index], down) + usbMenuHold.onButton(index, down) + } + override fun onWheel(steps: Int) = sendSceneWheel(-steps) + override fun onChanged() = updatePointerCapture() + override fun onLost() { + usbMenuHold.reset() + heldButtons.release(8) + updatePointerCapture() + requestRelativeMouse() + } + }) + private var pointerCaptureRefused = false + // Android grants capture on the focused display, which the phone's own screen is; only sessions shown on + // another display, like REDMAGIC's cast-mode virtual display, are refused, so only they ask for the USB mouse. + private val usbMouseFallback = Runnable { + if (window.decorView.hasPointerCapture() || window.decorView.display?.displayId == android.view.Display.DEFAULT_DISPLAY) return@Runnable + pointerCaptureRefused = true + if (usbMouse.grab()) Log.i(TAG, "pointer capture refused; reading the USB mouse directly") + } private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN private var drawerDirectionDownTime = 0L private var drawerDirectionDeviceId = -1 @@ -776,7 +808,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun dispatchPointerCaptureChanged(hasCapture: Boolean) { super.dispatchPointerCaptureChanged(hasCapture) Log.i(TAG, "pointer capture " + if (hasCapture) "on" else "off") - if (!hasCapture) { + if (hasCapture) { + pointerCaptureRefused = false + if (usbMouse.active) updatePointerCapture() + } else { releaseHeldGuestInput() uiHandler.postDelayed(::updatePointerCapture, 250) } @@ -1021,7 +1056,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde androidx.compose.runtime.DisposableEffect(Unit) { onDispose { com.droiddeck.launcher.glasses.GlassesMenuMirror.setSessionMenuVisible(this@SessionActivity, false) } } - androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, inputSettingsOpen, pipUi, bottomOutputActive) { updatePointerCapture() } + androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, inputSettingsOpen, pipUi, bottomOutputActive, wowChatOnTop) { updatePointerCapture() } androidx.compose.runtime.LaunchedEffect(bottomOutputActive) { hud.setHidden(pipUi || bottomOutputActive) } androidx.compose.runtime.LaunchedEffect(pipUi) { hud.setHidden(pipUi || bottomOutputActive) @@ -4902,7 +4937,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde else -> PointerGestures.BTN_LEFT } - private fun mouseAttached(): Boolean = InputDevice.getDeviceIds().any { id -> + private fun mouseAttached(): Boolean = usbMouse.active || InputDevice.getDeviceIds().any { id -> val d = InputDevice.getDevice(id) isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true } @@ -4913,20 +4948,31 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde 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 - } + private fun relativeMouseAttached(): Boolean = !isInMultiWindowMode && !glassesOutputActive && mouseAttached() private fun updatePointerCapture() { if (!::surfaceView.isInitialized) return val view = window.decorView - val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && mouseAttached() + val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && !wowChatOnTop && mouseAttached() releaseDesktopMouseGrab() + updateUsbMouse(want) if (want == view.hasPointerCapture()) return if (want) view.requestPointerCapture() else view.releasePointerCapture() } + private fun updateUsbMouse(want: Boolean) { + uiHandler.removeCallbacks(usbMouseFallback) + if (!want || window.decorView.hasPointerCapture()) { + if (usbMouse.active) { + usbMouse.release() + usbMenuHold.reset() + heldButtons.release(8) + } + return + } + if (!usbMouse.active) uiHandler.postDelayed(usbMouseFallback, if (pointerCaptureRefused) 0L else POINTER_CAPTURE_GRANT_MS) + } + private fun programLocksPointer(): Boolean = pointerLocked && SessionState.mode != SessionService.MODE_DESKTOP @@ -4995,6 +5041,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde surfaceView.pointerIcon = icon } + private fun onRelativeMouse(dx: Float, dy: Float) { + if (pointerLocked) { + sendSceneDelta(dx, dy) + cursorVisible = false + } else { + grabDesktopMouse() + moveCapturedMouse(dx, dy) + } + } + private fun onCapturedPointer(event: MotionEvent): Boolean { if (!event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)) return false notePress(event) @@ -5006,11 +5062,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde dx += event.getHistoricalX(i) dy += event.getHistoricalY(i) } - if (pointerLocked) sendSceneDelta(dx, dy) - else { - grabDesktopMouse() - moveCapturedMouse(dx, dy) - } + onRelativeMouse(dx, dy) } MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } @@ -6165,6 +6217,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde steamVrRecovery.close() uiHandler.removeCallbacks(steamTouchRetry) uiHandler.removeCallbacks(cursorResync) + uiHandler.removeCallbacks(usbMouseFallback) + usbMouse.close() releaseEvdevMouse() if (runtimeRemovalBlocked) { super.onDestroy(); return } watching = false @@ -6294,6 +6348,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private const val FIELD_SETTLE_MS = 200L private const val BTN_SIDE = 0x113 private const val BTN_EXTRA = 0x114 + private val USB_MOUSE_BUTTONS = intArrayOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE, BTN_SIDE, BTN_EXTRA) + private const val POINTER_CAPTURE_GRANT_MS = 500L private const val DESKTOP_GRAB_TAP_MS = 500L private const val DESKTOP_GRAB_MAX_TAPS = 8 private const val DESKTOP_GRAB_RETRY_MS = 30_000L diff --git a/app/src/main/java/com/droiddeck/launcher/input/HidMouseReport.kt b/app/src/main/java/com/droiddeck/launcher/input/HidMouseReport.kt new file mode 100644 index 0000000000..f562db1615 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/HidMouseReport.kt @@ -0,0 +1,158 @@ +package com.droiddeck.launcher.input + +/** Where a USB mouse's input report keeps its buttons, motion and wheel, as its HID report descriptor lays them out. */ +class HidMouseReport( + val reportId: Int, + private val buttons: List, + private val x: Field, + private val y: Field, + private val wheel: Field?, +) { + class Field(private val bit: Int, private val size: Int, private val signed: Boolean) { + fun read(report: ByteArray, length: Int, base: Int): Int { + var value = 0 + for (i in 0 until minOf(size, 32)) { + val at = base + bit + i + if (at / 8 >= length) break + if ((report[at / 8].toInt() shr (at % 8)) and 1 != 0) value = value or (1 shl i) + } + return if (signed && size in 1..31 && value and (1 shl (size - 1)) != 0) value - (1 shl size) else value + } + } + + /** [buttons] has bit 0 set for the first button. */ + data class State(val buttons: Int, val dx: Int, val dy: Int, val wheel: Int) + + /** Returns null for reports with another ID, such as a receiver's media keys. */ + fun decode(report: ByteArray, length: Int): State? { + val base = if (reportId == 0) 0 else { + if (length < 1 || (report[0].toInt() and 0xff) != reportId) return null + 8 + } + var pressed = 0 + buttons.forEachIndexed { i, field -> if (field != null && field.read(report, length, base) != 0) pressed = pressed or (1 shl i) } + return State(pressed, x.read(report, length, base), y.read(report, length, base), wheel?.read(report, length, base) ?: 0) + } + + companion object { + /** The report every boot mouse sends after SET_PROTOCOL(boot): three buttons, then X and Y as signed bytes. */ + val BOOT = HidMouseReport(0, List(3) { Field(it, 1, false) }, Field(8, 8, true), Field(16, 8, true), null) + + private const val BUTTON_PAGE = 0x09 + private const val MOUSE = 0x00010002 + private const val X = 0x00010030 + private const val Y = 0x00010031 + private const val WHEEL = 0x00010038 + private const val MAX_BUTTONS = 8 + private const val MAX_FIELD_BITS = 32 + private const val MAX_REPORT_COUNT = 4096 + + private class Global(var usagePage: Int = 0, var logicalMin: Int = 0, var reportSize: Int = 0, + var reportCount: Int = 0, var reportId: Int = 0) { + fun copy() = Global(usagePage, logicalMin, reportSize, reportCount, reportId) + } + + private class Placed(val reportId: Int, val field: Field) + + /** Returns null if [descriptor] has no mouse collection with X and Y in one report. */ + fun parse(descriptor: ByteArray): HidMouseReport? { + var global = Global() + val pushed = ArrayDeque() + val usages = mutableListOf() + var usageMin = -1 + var usageMax = -1 + val offsets = HashMap() + var depth = 0 + var mouseDepth = -1 + var mouseDone = false + val placed = HashMap() + + fun usage(index: Int): Int = when { + usages.isNotEmpty() -> usages.getOrElse(index) { usages.last() } + usageMin >= 0 && usageMax >= usageMin -> minOf(usageMin + index, usageMax) + else -> -1 + } + + var i = 0 + while (i < descriptor.size) { + val prefix = descriptor[i].toInt() and 0xff + if (prefix == 0xfe) { + if (i + 1 >= descriptor.size) break + i += 3 + (descriptor[i + 1].toInt() and 0xff) + continue + } + val size = intArrayOf(0, 1, 2, 4)[prefix and 3] + if (i + 1 + size > descriptor.size) break + var data = 0 + for (b in 0 until size) data = data or ((descriptor[i + 1 + b].toInt() and 0xff) shl (8 * b)) + val signedData = when (size) { + 1 -> data.toByte().toInt() + 2 -> data.toShort().toInt() + else -> data + } + val tag = prefix shr 4 + when ((prefix shr 2) and 3) { + 0 -> { + when (tag) { + 0x8 -> { + if (global.reportSize !in 0..MAX_FIELD_BITS || global.reportCount !in 0..MAX_REPORT_COUNT) return null + val start = offsets[global.reportId] ?: 0 + val isData = data and 1 == 0 + val isVariable = data and 2 != 0 + if (mouseDepth >= 0 && !mouseDone && isData && isVariable) { + for (n in 0 until global.reportCount) { + val u = usage(n) + if (u < 0) break + val field = Placed(global.reportId, Field(start + n * global.reportSize, global.reportSize, global.logicalMin < 0)) + val button = if (u ushr 16 == BUTTON_PAGE) u and 0xffff else 0 + when { + u == X || u == Y || u == WHEEL -> placed.putIfAbsent(u, field) + button in 1..MAX_BUTTONS -> placed.putIfAbsent(u, field) + } + } + } + offsets[global.reportId] = start + global.reportSize * global.reportCount + } + 0xa -> { + depth++ + if (mouseDepth < 0 && data == 1 && usage(0) == MOUSE) mouseDepth = depth + } + 0xc -> { + if (depth == mouseDepth) mouseDone = true + depth-- + } + } + usages.clear() + usageMin = -1 + usageMax = -1 + } + 1 -> when (tag) { + 0x0 -> global.usagePage = data + 0x1 -> global.logicalMin = signedData + 0x7 -> global.reportSize = data + 0x8 -> global.reportId = data + 0x9 -> global.reportCount = data + 0xa -> pushed.addLast(global.copy()) + 0xb -> global = pushed.removeLastOrNull() ?: global + } + 2 -> { + val full = if (size == 4) data else (global.usagePage shl 16) or data + when (tag) { + 0x0 -> usages += full + 0x1 -> usageMin = full + 0x2 -> usageMax = full + } + } + } + i += 1 + size + } + + val x = placed[X] ?: return null + val y = placed[Y]?.takeIf { it.reportId == x.reportId } ?: return null + val id = x.reportId + val buttons = (1..MAX_BUTTONS).map { n -> placed[(BUTTON_PAGE shl 16) or n]?.takeIf { it.reportId == id }?.field } + return HidMouseReport(id, buttons.dropLastWhile { it == null }, x.field, y.field, + placed[WHEEL]?.takeIf { it.reportId == id }?.field) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/MouseMenuHold.kt b/app/src/main/java/com/droiddeck/launcher/input/MouseMenuHold.kt new file mode 100644 index 0000000000..92bb16f65d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/MouseMenuHold.kt @@ -0,0 +1,46 @@ +package com.droiddeck.launcher.input + +import kotlin.math.abs + +/** + * Holding the middle button still opens the session menu. While the session reads the mouse over USB, Android has + * no cursor, so this is how a mouse reaches the menu and, through it, the system again. + */ +class MouseMenuHold( + private val schedule: (Runnable, Long) -> Unit, + private val cancel: (Runnable) -> Unit, + private val onMenu: () -> Unit, +) { + private var holding = false + private var travel = 0 + private val fire = Runnable { + if (!holding) return@Runnable + holding = false + onMenu() + } + + fun onButton(index: Int, down: Boolean) { + if (index != MIDDLE) return + cancel(fire) + holding = down + travel = 0 + if (down) schedule(fire, HOLD_MS) + } + + fun onMove(dx: Int, dy: Int) { + if (!holding) return + travel += abs(dx) + abs(dy) + if (travel > SLOP) reset() + } + + fun reset() { + holding = false + cancel(fire) + } + + companion object { + const val MIDDLE = 2 + const val HOLD_MS = 2000L + const val SLOP = 48 + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/UsbMouse.kt b/app/src/main/java/com/droiddeck/launcher/input/UsbMouse.kt new file mode 100644 index 0000000000..ffc2960c9a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/UsbMouse.kt @@ -0,0 +1,262 @@ +package com.droiddeck.launcher.input + +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.hardware.usb.UsbConstants +import android.hardware.usb.UsbDevice +import android.hardware.usb.UsbDeviceConnection +import android.hardware.usb.UsbEndpoint +import android.hardware.usb.UsbInterface +import android.hardware.usb.UsbManager +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.util.Log +import android.view.InputDevice + +/** + * Reads a USB mouse from its HID interface, detaching it from Android meanwhile. Android only grants pointer + * capture on the focused display, so an app shown elsewhere, as in REDMAGIC's cast mode, gets a cursor that + * stops at the screen edge. + */ +class UsbMouse(private val context: Context, private val listener: Listener) { + interface Listener { + fun onMove(dx: Int, dy: Int) + fun onButton(index: Int, down: Boolean) + fun onWheel(steps: Int) + fun onChanged() + /** The mouse stopped responding while read here, so buttons held then never got their releases. */ + fun onLost() + } + + private val usb by lazy { context.getSystemService(Context.USB_SERVICE) as UsbManager } + private val main = Handler(Looper.getMainLooper()) + private val permissionAction by lazy { context.packageName + ".USB_MOUSE_PERMISSION" } + private val asked = mutableSetOf() + private val failed = mutableSetOf() + private var receiverRegistered = false + @Volatile private var grab: Grab? = null + private var lastReader: Thread? = null + + val active: Boolean get() = grab != null + + private val receiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) + Log.i(TAG, "USB mouse access ${if (granted) "granted" else "denied"}") + if (granted) listener.onChanged() + } + } + + /** + * Returns whether the mouse is now read here, setting it up on the reader thread; asks for access once per + * plugged-in mouse. A mouse that fails setup after being claimed isn't tried again until it is replugged, since + * releasing it re-adds Android's mouse, which would start another attempt. + */ + fun grab(): Boolean { + if (grab != null) return true + val (device, mouse) = findMouse() ?: return false + if (device.deviceName in failed) return false + if (!usb.hasPermission(device)) { + if (asked.add(device.deviceName)) askPermission(device) + return false + } + val endpoint = (0 until mouse.endpointCount).map(mouse::getEndpoint).firstOrNull { + it.direction == UsbConstants.USB_DIR_IN && it.type == UsbConstants.USB_ENDPOINT_XFER_INT + } ?: return false + grab = Grab(device, mouse, endpoint, lastReader).also { + lastReader = it.thread + it.thread.start() + } + return true + } + + /** Hands the mouse back to Android. */ + fun release() { + val current = grab ?: return + grab = null + current.cancel() + } + + fun close() { + release() + if (receiverRegistered) runCatching { context.unregisterReceiver(receiver) } + receiverRegistered = false + } + + private fun findMouse(): Pair? { + val androidMice = InputDevice.getDeviceIds().asList().mapNotNull { InputDevice.getDevice(it) } + .filter { !it.isVirtual && it.supportsSource(InputDevice.SOURCE_MOUSE) } + .map { it.vendorId to it.productId }.toSet() + return usb.deviceList.values.filter { (it.vendorId to it.productId) in androidMice }.firstNotNullOfOrNull { device -> + (0 until device.interfaceCount).map(device::getInterface).firstOrNull(::isBootMouse)?.let { device to it } + } + } + + private fun askPermission(device: UsbDevice) { + if (!receiverRegistered) { + val filter = IntentFilter(permissionAction) + if (Build.VERSION.SDK_INT >= 33) context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED) + else context.registerReceiver(receiver, filter) + receiverRegistered = true + } + val flags = if (Build.VERSION.SDK_INT >= 31) PendingIntent.FLAG_MUTABLE else 0 + val intent = PendingIntent.getBroadcast(context, 0, Intent(permissionAction).setPackage(context.packageName), flags) + runCatching { usb.requestPermission(device, intent) }.onFailure { + asked.remove(device.deviceName) + Log.w(TAG, "requestPermission", it) + } + } + + private inner class Grab( + private val device: UsbDevice, + private val mouse: UsbInterface, + private val endpoint: UsbEndpoint, + private val previous: Thread?, + ) { + @Volatile private var running = true + private val claimLock = Any() + val thread = Thread(::run, "usb-mouse") + + /** Once this returns, the reader either never claims the interface or sees the cancellation after claiming. */ + fun cancel() = synchronized(claimLock) { running = false } + + // The reader owns the connection: closing it while a transfer is pending would free it under the transfer. + // A previous reader may still hold the interface, which would make this claim fail. + private fun run() { + runCatching { previous?.join() } + val connection = if (running) usb.openDevice(device) else null + if (connection == null) { + main.post { ended(markFailed = false) } + return + } + var claimed = false + var boot = false + try { + claimed = synchronized(claimLock) { running && connection.claimInterface(mouse, true) } + if (!claimed) { + main.post { ended(markFailed = false) } + return + } + if (!running) return + val format = setUp(connection) + if (format == null) { + main.post { ended(markFailed = true) } + return + } + boot = format === HidMouseReport.BOOT + if (!running) return + Log.i(TAG, String.format(java.util.Locale.ROOT, "reading USB mouse %04x:%04x \"%s\" directly (%s)", + device.vendorId, device.productId, device.productName, if (boot) "boot protocol" else "report ${format.reportId}")) + read(connection, format) + } finally { + if (claimed) { + if (boot) connection.controlTransfer(0x21, SET_PROTOCOL, HID_REPORT_PROTOCOL, mouse.id, null, 0, CONTROL_TIMEOUT_MS) + connection.releaseInterface(mouse) + } + connection.close() + } + } + + /** HID 1.11 7.2.6: a host must not assume the protocol a device is in, so it is set either way. */ + private fun setUp(connection: UsbDeviceConnection): HidMouseReport? { + val descriptor = ByteArray(1024) + val length = connection.controlTransfer(0x81, GET_DESCRIPTOR, HID_REPORT_DESCRIPTOR shl 8, mouse.id, + descriptor, descriptor.size, CONTROL_TIMEOUT_MS) + val parsed = if (length > 0) HidMouseReport.parse(descriptor.copyOf(length)) else null + if (parsed != null && running && + connection.controlTransfer(0x21, SET_PROTOCOL, HID_REPORT_PROTOCOL, mouse.id, null, 0, CONTROL_TIMEOUT_MS) >= 0) return parsed + val boot = running && + connection.controlTransfer(0x21, SET_PROTOCOL, HID_BOOT_PROTOCOL, mouse.id, null, 0, CONTROL_TIMEOUT_MS) >= 0 + return HidMouseReport.BOOT.takeIf { boot } + } + + private fun read(connection: UsbDeviceConnection, format: HidMouseReport) { + val report = ByteArray(maxOf(endpoint.maxPacketSize, 8)) + var buttons = 0 + var failures = 0 + while (running) { + val started = SystemClock.uptimeMillis() + val length = connection.bulkTransfer(endpoint, report, report.size, READ_TIMEOUT_MS) + if (length <= 0) { + if (SystemClock.uptimeMillis() - started < READ_TIMEOUT_MS / 2 && ++failures >= MAX_FAILURES) { + main.post { lost() } + return + } + continue + } + failures = 0 + val state = format.decode(report, length) ?: continue + val changed = buttons xor state.buttons + buttons = state.buttons + main.post { deliver(state, changed) } + } + } + + private fun ended(markFailed: Boolean) { + if (grab !== this) return + if (markFailed) failed += device.deviceName + Log.i(TAG, "USB mouse \"${device.productName}\" could not be set up") + grab = null + } + + private fun deliver(state: HidMouseReport.State, changed: Int) { + if (grab !== this) return + if (state.dx != 0 || state.dy != 0) listener.onMove(state.dx, state.dy) + for (i in 0 until BUTTONS) if (changed and (1 shl i) != 0) listener.onButton(i, state.buttons and (1 shl i) != 0) + if (state.wheel != 0) listener.onWheel(state.wheel) + } + + private fun lost() { + if (grab !== this) return + failed += device.deviceName + Log.i(TAG, "USB mouse stopped responding") + grab = null + listener.onLost() + } + } + + companion object { + private const val TAG = "UsbMouse" + private const val GET_DESCRIPTOR = 0x06 + private const val SET_PROTOCOL = 0x0b + private const val HID_BOOT_PROTOCOL = 0 + private const val HID_REPORT_PROTOCOL = 1 + private const val HID_REPORT_DESCRIPTOR = 0x22 + private const val SUBCLASS_BOOT = 1 + private const val PROTOCOL_MOUSE = 2 + private const val CONTROL_TIMEOUT_MS = 1000 + private const val READ_TIMEOUT_MS = 100 + private const val MAX_FAILURES = 5 + private const val BUTTONS = 5 + + private fun isBootMouse(iface: UsbInterface) = iface.interfaceClass == UsbConstants.USB_CLASS_HID && + iface.interfaceSubclass == SUBCLASS_BOOT && iface.interfaceProtocol == PROTOCOL_MOUSE + + /** + * A process killed while reading a mouse leaves it with no driver until it is replugged. Claiming without + * force only succeeds when no driver has the interface, and releasing it reattaches Android's. + */ + fun returnLeftover(context: Context) { + Thread({ + runCatching { + val usb = context.getSystemService(Context.USB_SERVICE) as UsbManager + for (device in usb.deviceList.values) { + if (!usb.hasPermission(device)) continue + val mouse = (0 until device.interfaceCount).map(device::getInterface).firstOrNull(::isBootMouse) ?: continue + val connection = usb.openDevice(device) ?: continue + if (connection.claimInterface(mouse, false)) { + connection.releaseInterface(mouse) + Log.i(TAG, "returned USB mouse \"${device.productName}\" to Android") + } + connection.close() + } + }.onFailure { Log.w(TAG, "returning a USB mouse to Android failed", it) } + }, "usb-mouse-return").start() + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/HidMouseReportTest.kt b/app/src/test/java/com/droiddeck/launcher/input/HidMouseReportTest.kt new file mode 100644 index 0000000000..7e62907cb3 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/HidMouseReportTest.kt @@ -0,0 +1,65 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Test + +class HidMouseReportTest { + private fun bytes(hex: String) = hex.split(' ').filter { it.isNotEmpty() }.map { it.toInt(16).toByte() }.toByteArray() + + private fun HidMouseReport.decode(hex: String) = bytes(hex).let { decode(it, it.size) } + + /** A MOSART 2.4G receiver (062a:4c01), as read on a REDMAGIC 10 Pro. */ + private val receiver = HidMouseReport.parse(bytes( + "05 01 09 02 a1 01 85 01 09 01 a1 00 05 09 19 01 29 05 15 00 25 01 75 01 95 05 81 02 75 03 95 01 81 01 " + + "05 01 09 30 09 31 15 81 25 7f 75 08 95 02 81 06 09 38 15 81 25 7f 75 08 95 01 81 06 c0 05 0c 15 81 25 7f " + + "75 08 95 01 0a 38 02 81 06 c0 05 0c 09 01 a1 01 85 03 19 00 2a 80 03 15 00 26 80 03 75 10 95 01 81 00 c0 " + + "05 01 09 80 a1 01 85 02 19 81 29 83 25 01 75 01 95 03 81 02 95 05 81 01 c0 06 f2 f1 0a f2 f1 a1 01 85 05 " + + "09 00 95 06 75 08 15 00 26 ff 00 91 02 09 00 95 05 75 08 15 00 26 ff 00 81 02 c0"))!! + + @Test fun readsTheReceiversMouseReport() { + assertEquals(1, receiver.reportId) + assertEquals(HidMouseReport.State(0, 5, -7, 0), receiver.decode("01 00 05 f9 00 00")) + assertEquals(HidMouseReport.State(0, -30, -2, 0), receiver.decode("01 00 e2 fe 00 00")) + } + + @Test fun readsButtonsAndWheel() { + assertEquals(HidMouseReport.State(0b10, 0, 0, 0), receiver.decode("01 02 00 00 00 00")) + assertEquals(HidMouseReport.State(0b11111, 0, 0, 0), receiver.decode("01 1f 00 00 00 00")) + assertEquals(HidMouseReport.State(0, 0, 0, -1), receiver.decode("01 00 00 00 ff 00")) + } + + @Test fun ignoresTheReceiversOtherReports() { + assertNull(receiver.decode("03 e9 00")) + assertNull(receiver.decode("02 01")) + } + + @Test fun readsSixteenBitMotionWithoutReportIds() { + val mouse = HidMouseReport.parse(bytes( + "05 01 09 02 a1 01 09 01 a1 00 05 09 19 01 29 05 15 00 25 01 95 05 75 01 81 02 95 01 75 03 81 01 " + + "05 01 16 01 80 26 ff 7f 75 10 95 02 09 30 09 31 81 06 15 81 25 7f 75 08 95 01 09 38 81 06 c0 c0")) + assertNotNull(mouse) + assertEquals(0, mouse!!.reportId) + assertEquals(HidMouseReport.State(1, 300, -100, 1), mouse.decode("01 2c 01 9c ff 01")) + } + + @Test fun findsNoMouseInAKeyboard() { + assertNull(HidMouseReport.parse(bytes("05 01 09 06 a1 01 05 07 19 e0 29 e7 15 00 25 01 75 01 95 08 81 02 c0"))) + } + + @Test fun rejectsOutOfRangeReportCountsAndSizes() { + assertNull(HidMouseReport.parse(bytes( + "05 01 09 02 a1 01 05 09 19 01 29 03 15 00 25 01 75 01 96 01 10 81 02 " + + "05 01 09 30 09 31 15 81 25 7f 75 08 95 02 81 06 c0"))) + assertNotNull(HidMouseReport.parse(bytes( + "05 01 09 02 a1 01 05 09 19 01 29 03 15 00 25 01 75 01 96 00 10 81 02 " + + "05 01 09 30 09 31 15 81 25 7f 75 08 95 02 81 06 c0"))) + assertNull(HidMouseReport.parse(bytes( + "05 01 09 02 a1 01 05 01 09 30 09 31 15 81 25 7f 77 ff ff ff 7f 95 02 81 06 c0"))) + } + + @Test fun readsTheBootReport() { + assertEquals(HidMouseReport.State(0b11, -6, 2, 0), HidMouseReport.BOOT.decode("03 fa 02")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/MouseMenuHoldTest.kt b/app/src/test/java/com/droiddeck/launcher/input/MouseMenuHoldTest.kt new file mode 100644 index 0000000000..77377ccc6e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/MouseMenuHoldTest.kt @@ -0,0 +1,54 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class MouseMenuHoldTest { + private var pending: Runnable? = null + private var delay = 0L + private var menus = 0 + private val hold = MouseMenuHold( + schedule = { runnable, ms -> pending = runnable; delay = ms }, + cancel = { if (pending === it) pending = null }, + onMenu = { menus++ }, + ) + + private fun elapse() = pending?.also { pending = null }?.run() + + @Test fun holdingTheMiddleButtonStillOpensTheMenu() { + hold.onButton(MouseMenuHold.MIDDLE, true) + assertEquals(MouseMenuHold.HOLD_MS, delay) + hold.onMove(3, -2) + elapse() + assertEquals(1, menus) + } + + @Test fun aQuickMiddleClickDoesNothing() { + hold.onButton(MouseMenuHold.MIDDLE, true) + hold.onButton(MouseMenuHold.MIDDLE, false) + assertNull(pending) + assertEquals(0, menus) + } + + @Test fun draggingWithTheMiddleButtonDoesNothing() { + hold.onButton(MouseMenuHold.MIDDLE, true) + hold.onMove(30, 0) + hold.onMove(0, -30) + assertNull(pending) + assertEquals(0, menus) + } + + @Test fun otherButtonsDoNothing() { + hold.onButton(0, true) + hold.onButton(1, true) + assertNull(pending) + } + + @Test fun resetCancelsAHold() { + hold.onButton(MouseMenuHold.MIDDLE, true) + hold.reset() + assertNull(pending) + assertEquals(0, menus) + } +}