Commit graph

2547 commits

Author SHA1 Message Date
Romain Vimont
aaa9284e58 Add gamepad user documentation
Mainly copied and adapted from HID keyboard and mouse documentation.
2024-09-09 18:25:50 +02:00
Romain Vimont
94a234ca43 Fix link in OTG documentation 2024-09-09 18:24:29 +02:00
Romain Vimont
125625ecd1 Add UHID gamepad rumble WIP 2024-09-09 12:35:54 +02:00
Romain Vimont
0483874a55 Simplify UHID outputs routing
There was a registration mechanism to listen to HID outputs with a
specific HID id.

However, the UHID gamepad processor handles several ids, so it will not
work. We could complexify the registration mechanism, but instead,
directly dispatch to the expected processor based on the UHID id.

Concretely, instead of passing a sc_uhid_devices instance to construct a
sc_keyboard_uhid, so that it can register itself, construct the
sc_uhid_devices with all the UHID instances (currently only
sc_keyboard_uhid) so that it can dispatch HID outputs directly.
2024-09-09 12:35:54 +02:00
Romain Vimont
32091f0c39 Mention physical gamepad names for UHID devices
Initialize UHID devices with a custom name:
 - "scrcpy: $GAMEPAD_NAME" for gamepads
 - "scrcpy" for keyboard and mouse (or if no gamepad name is available)

The name may appear in Android apps.
2024-09-09 12:35:54 +02:00
Romain Vimont
7f6adc015f Reorder function parameters for consistency
Make the local function write_string() accept the output buffer as a
first parameter, like the other similar functions.
2024-09-09 12:35:54 +02:00
Romain Vimont
f2e179c447 Make -K -M and -G use AOA in OTG mode
For convenience, short options were added to select UHID input modes:
 - -K for --keyboard=uhid
 - -M for --mouse=uhid
 - -G for --gamepad=uhid

In OTG mode, UHID is not available, so the short options should select
AOA instead.
2024-09-09 12:35:54 +02:00
Romain Vimont
0fe208d3f3 Add UHID gamepad support
Similar to UHID keyboard and mouse, but for gamepads.

Can be enabled with --gamepad=uhid or -G.

It is not enabled by default because not all devices support UHID
(there is a permission error on old Android versions).
2024-09-09 12:35:54 +02:00
Romain Vimont
9afc497e20 Add UHID_DESTROY control message
This message will be sent on gamepad disconnection.

Contrary to keyboard and mouse, which are registered once and are
unregistered when scrcpy exists, each gamepad is mapped with its own HID
id, and they can be plugged/unplugged dynamically.
2024-09-09 12:35:54 +02:00
Romain Vimont
477cbbbfd5 Add gamepad support in OTG mode
Implement gamepad support for OTG.
2024-09-09 12:35:54 +02:00
Romain Vimont
b964e7f3cb Add AOA gamepad support
Similar to AOA keyboard and mouse, but for gamepads.

Can be enabled with --gamepad=aoa.
2024-09-09 12:35:54 +02:00
Romain Vimont
33344968d7 Implement HID gamepad
Implement the HID protocol for gamepads, that will be used in further
commits by the AOA and UHID gamepad processor implementations.
2024-09-09 12:35:54 +02:00
Romain Vimont
c4b6bb312c Add util functions to write in little-endian
This will be helpful for writing HID values.
2024-09-09 08:26:24 +02:00
Romain Vimont
abec04e8e7 Add connected gamepads on start
Trigger SDL_CONTROLLERDEVICEADDED for all gamepads already connected
when scrcpy starts. We want to handle both the gamepads initially
connected and the gamepads connected while scrcpy is running.

This is not racy, because this event may not trigged automatically until
SDL events are "pumped" (SDL_PumpEvents/SDL_WaitEvent).
2024-09-09 08:26:24 +02:00
Romain Vimont
0ea20f90be Handle SDL gamepad events
Introduce a gamepad processor trait, similar to the keyboard processor
and mouse processor traits.

Handle gamepad events received from SDL, convert them to scrcpy-specific
gamepad events, and forward them to the gamepad processor.

Further commits will provide AOA and UHID implementations of the gamepad
processor trait.

Co-authored-by: Luiz Henrique Laurini <luizhenriquelaurini@gmail.com>
2024-09-09 08:26:24 +02:00
Romain Vimont
a5ffe5b060 Fix HID comments
Fix typo and reference the latest version of "HID Usage Tables"
specifications.
2024-09-09 08:26:24 +02:00
Romain Vimont
6f1d79ba17 Make AOA keyboard/mouse open error fatal
Now that the AOA open/close are asynchronous, an open error did not make
scrcpy exit anymore.

Add a mechanism to exit if the AOA device could not be opened
asynchronously.
2024-09-09 08:26:24 +02:00
Romain Vimont
44e29989ee Unregister all AOA devices automatically on exit
Pushing a close event from the keyboard_aoa or mouse_aoa implementation
was racy, because the AOA thread might be stopped before these events
were processed.

Instead, keep the list of open AOA devices to close them automatically
from the AOA thread before exiting.
2024-09-09 08:26:24 +02:00
Romain Vimont
00786942be Make HID logs uniform 2024-09-09 08:26:24 +02:00
Romain Vimont
e6017cdc5d Add AOA open/close verbose logs 2024-09-09 08:26:24 +02:00
Romain Vimont
8fb87b5e6b Introduce hid_open and hid_close events
This allows to handle HID open/close at the same place as HID input
events (in the HID layer).

This will be especially useful to manage HID gamepads, to avoid
implementing one part in the HID layer and another part in the gamepad
processor implementation.
2024-09-09 08:26:24 +02:00
Romain Vimont
24f7ea5894 Rename hid_event to hid_input
The sc_hid_event structure represents HID input data. Rename it so that
we can add other hid event structs without confusion.
2024-09-09 08:26:24 +02:00
Romain Vimont
dbdfd9c8bf Make AOA open and close asynchronous
For AOA keyboard and mouse, only input events were asynchronous.
Register/unregister were called from the main thread.

This had the benefit to fail immediately if the AOA registration failed,
but to support gamepads we want to open/close AOA devices dynamically.

Also, it is better to avoid USB I/O from the main thread.
2024-09-09 08:26:24 +02:00
Romain Vimont
d58eb616f0 Reorder AOA functions
This will allow sc_aoa_setup_hid() to compile even when
sc_aoa_unregister_hid() will be made static.
2024-09-09 08:26:24 +02:00
Romain Vimont
230e6b4079 Refactor AOA handling
Extract event processing to a separate function.

This will make the code more readable when more event types will be
added.
2024-09-09 08:26:24 +02:00
Romain Vimont
3bd07aa8ff Move HID ids to common HID code
The HID ids (accessory ids or UHID ids) were defined by the keyboard and
mouse implementations.

Instead, define them in the common HID part, and make that id part of
the sc_hid_event.

This prepares the introduction of gamepad support, which will handle
several gamepads (and ids) in the common HID gamepad code.
2024-09-09 08:26:24 +02:00
Romain Vimont
28c91ecba2 Fix HID mouse header guard 2024-09-09 08:26:24 +02:00
Romain Vimont
9ebb836b20 Add missing SC_ prefix for HID mouse event size 2024-09-09 08:26:24 +02:00
Romain Vimont
1559940cee Remove duplicate definition SC_HID_MAX_SIZE
This constant is defined in hid_event.h.
2024-09-09 08:26:24 +02:00
Romain Vimont
0cc1a855dc Fail on AOA keyboard/mouse initialization error
If the AOA keyboard or the AOA mouse fails to be initialized, this is a
fatal error.
2024-09-09 08:26:24 +02:00
Romain Vimont
b49064064c Introduce non-droppable control messages
Control messages are queued from the main thread and sent to the device
from a separate thread.

When the queue is full, messages are just dropped. This avoids to
accumulate too much delay between the client and the device in case of
network issue.

However, some messages should not be dropped: for example, dropping a
UHID_CREATE message would make invalid all further UHID_INPUT messages.
Therefore, mark these messages as non-droppable.

A non-droppable event is queued anyway (resizing the queue if
necessary, unless the allocation fails).
2024-09-09 08:26:24 +02:00
Romain Vimont
ba342c398d Remove atomics from keyboard_uhid
The UHID output callback is now called from the same (main) thread as
the process_key() function.
2024-09-09 08:26:24 +02:00
Romain Vimont
d7fe119c8e Process UHID outputs events from the main thread
This will guarantee that the callbacks of UHID devices implementations
will always be called from the same (main) thread.
2024-09-09 08:26:24 +02:00
Romain Vimont
c5ccae5538 Set clipboard from the main thread
The clipboard changes from the device are received from a separate
thread, but it must be handled from the main thread.
2024-09-09 08:26:24 +02:00
Romain Vimont
87d9d68c07 Add mechanism to execute code on the main thread
This allows to schedule a runnable to be executed on the main thread,
until the event loop is explicitly terminated.

It is guaranteed that all accepted runnables will be executed (this
avoids possible memory leaks if a runnable owns resources).
2024-09-09 08:26:24 +02:00
Romain Vimont
684e2b632e Expose main thread id
This will allow to assert that a function is called from the main
thread.
2024-09-09 08:26:24 +02:00
Romain Vimont
8598e7d7a8 Extract sc_push_event()
Expose a convenience function to push an event without args to the main
thread.
2024-09-09 08:26:24 +02:00
Romain Vimont
8b372ae809 Store events numbers in an enum
This avoids to manually set an explicit value for each item.
2024-09-09 08:26:24 +02:00
Romain Vimont
28512d3872 Fix deprecated reference in scrcpy manpage
The options --hid-keyboard and --hid-mouse do not exist anymore. They
have been replaced by --keyboard=XXX and --mouse=XXX.
2024-09-09 08:26:24 +02:00
Romain Vimont
c11f07c1e8 Do not send uninitialized HID event
If the function returns false, then there is nothing to send.
2024-09-09 08:26:24 +02:00
Romain Vimont
903a5aaaf5 Replace "could not" by "cannot" where appropriate
"Could not" implies that the system tried to disable the option but
encountered an issue or failure.

"Cannot" indicates a rule or restriction, meaning it's not possible to
perform the action at all.
2024-09-09 08:24:52 +02:00
Romain Vimont
21b412cd98 Simplify messages reader/writer
In Java, control messages were parsed using manual buffering, which was
convoluted and error-prone.

Instead, read the socket directly through a DataInputStream and a
BufferedInputStream. Symmetrically, use a DataOutputStream and a
BufferedOutputStream to write messages.
2024-09-07 14:31:54 +02:00
Romain Vimont
3b241af3f6 Allow to pass an explicit version name on release
To build with a specific version name:

    VERSION=pr1234 ./release.sh

If not set, it will use the result of "git describe" (as before).
2024-09-06 23:07:15 +02:00
Romain Vimont
0c95794463 Do not apply all workarounds for ONYX devices
Calling fillAppInfo() breaks video mirroring on ONYX devices.

Fixes #5182 <https://github.com/Genymobile/scrcpy/issues/5182>
Refs 2b6089cbfc
2024-08-10 14:29:26 +02:00
Romain Vimont
523f939532 Do not create UHID thread if not used
The HandlerThread is used only via the looper queue.
2024-08-08 20:32:35 +02:00
yangfl
dd47cefa47 Fix typos
Refs <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1077968#27>
PR #5171 <https://github.com/Genymobile/scrcpy/pull/5171>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2024-08-05 16:13:02 +02:00
Romain Vimont
44b3fd82b1 Update links to 2.6.1 2024-08-02 22:58:09 +02:00
Romain Vimont
cc41115625 Bump version to 2.6.1 2024-08-02 22:32:04 +02:00
Romain Vimont
773c23fda2 Inject finger input whenever possible
Even if the pointer is a mouse, inject it as a finger unless it is
required to be a mouse, that is:
 - when it is a HOVER_MOUSE event, or
 - when a secondary button is pressed.

Some apps/games only accept events from a finger/touchscreen, so using a
mouse by default does not work for them.

For simplicity, make this change on the server side just before
event injection (so that the client does not need to know about this
hacky behavior).

Refs 6808288823
Refs c7b1d0ea9a
Fixes #5162 <https://github.com/Genymobile/scrcpy/issues/5162>
Fixes #5163 <https://github.com/Genymobile/scrcpy/issues/5163>
2024-08-02 22:24:31 +02:00
Romain Vimont
992b4922fe Document INJECT_EVENTS permission issue on Xiaomi
Make explicit in the prerequisites the exact error message when "USB
debugging (Security Settings)" is not set.
2024-08-02 18:44:42 +02:00