Files
DroidDeck/app
OmarAlTaki1 a8aff1c1a3 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.
2026-11-15 10:21:15 -05:00
..