1624 Commits

Author SHA1 Message Date
Romain Vimont
b54b0a6958 Add UHID gamepad rumble WIP 2024-09-07 23:24:42 +02:00
Romain Vimont
e5c7840530 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-07 23:24:42 +02:00
Romain Vimont
ccc030625b 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-07 23:24:42 +02:00
Romain Vimont
b0b05909c0 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-07 23:24:42 +02:00
Romain Vimont
330d844231 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-07 23:24:42 +02:00
Romain Vimont
ab6732f32d Add gamepad support in OTG mode
Implement gamepad support for OTG.
2024-09-07 23:24:42 +02:00
Romain Vimont
32c73a5448 Add AOA gamepad support
Similar to AOA keyboard and mouse, but for gamepads.

Can be enabled with --gamepad=aoa.
2024-09-07 23:24:42 +02:00
Romain Vimont
c72c772c81 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-07 23:24:42 +02:00
Romain Vimont
d2e1d96fbd Add util functions to write in little-endian
This will be helpful for writing HID values.
2024-09-07 23:24:42 +02:00
Romain Vimont
07d5a6ca80 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.

It is a bit racy, because a device may be added after the controller
subsystem initialization, but before the call to list the joysticks. In
that case, a single device may in theory be detected as added twice.
This should be harmless though (the second initialization will just fail
without major impact).
2024-09-07 23:24:15 +02:00
Romain Vimont
2d929fa4e5 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-07 22:45:05 +02:00
Romain Vimont
623346b558 Fix HID comments
Fix typo and reference the latest version of "HID Usage Tables"
specifications.
2024-09-07 22:45:05 +02:00
Romain Vimont
5b52afd980 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-07 22:45:05 +02:00
Romain Vimont
4c5d43d41d 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-07 22:45:05 +02:00
Romain Vimont
2351eed46f Make HID logs uniform 2024-09-07 22:45:05 +02:00
Romain Vimont
2595bcfa75 Add AOA open/close verbose logs 2024-09-07 22:45:05 +02:00
Romain Vimont
93dad86634 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-07 22:45:05 +02:00
Romain Vimont
282e6503e1 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-07 22:45:05 +02:00
Romain Vimont
9c86cf74e6 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-07 22:45:05 +02:00
Romain Vimont
2caf593960 Reorder AOA functions
This will allow sc_aoa_setup_hid() to compile even when
sc_aoa_unregister_hid() will be made static.
2024-09-07 22:45:05 +02:00
Romain Vimont
1344b76f42 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-07 22:45:05 +02:00
Romain Vimont
32b1a8093a 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-07 22:45:05 +02:00
Romain Vimont
4416aa29f1 Fix HID mouse header guard 2024-09-07 22:45:05 +02:00
Romain Vimont
4b95a9674a Add missing SC_ prefix for HID mouse event size 2024-09-07 22:45:05 +02:00
Romain Vimont
c7c8d71de5 Remove duplicate definition SC_HID_MAX_SIZE
This constant is defined in hid_event.h.
2024-09-07 22:45:05 +02:00
Romain Vimont
de6ee25d09 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-07 22:45:05 +02:00
Romain Vimont
70a9bbbc06 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-07 22:45:05 +02:00
Romain Vimont
97e83fa3d6 Remove atomics from keyboard_uhid
The UHID output callback is now called from the same (main) thread as
the process_key() function.
2024-09-07 22:45:05 +02:00
Romain Vimont
0215693725 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-07 22:45:05 +02:00
Romain Vimont
220cad2e7a 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-07 22:45:05 +02:00
Romain Vimont
0b6119a594 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-07 22:44:51 +02:00
Romain Vimont
9cae029ef2 Expose main thread id
This will allow to assert that a function is called from the main
thread.
2024-09-07 22:05:18 +02:00
Romain Vimont
72d5bcc12a Extract sc_push_event()
Expose a convenience function to push an event without args to the main
thread.
2024-09-07 22:03:27 +02:00
Romain Vimont
d56b8a6a95 Store events numbers in an enum
This avoids to manually set an explicit value for each item.
2024-09-07 22:03:27 +02:00
Romain Vimont
580cba73c0 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-07 18:36:05 +02:00
Romain Vimont
523299743c Do not send uninitialized HID event
If the function returns false, then there is nothing to send.
2024-09-07 18:35:16 +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
cc41115625 Bump version to 2.6.1 2024-08-02 22:32:04 +02:00
Romain Vimont
52136268ef Bump version to 2.6 2024-08-01 18:15:59 +02:00
Romain Vimont
5d2441d198 Upgrade SDL (2.30.5) for Windows 2024-08-01 18:15:37 +02:00
Romain Vimont
071d459ad7 Fix --no-audio
By default, the audio source is initialized to SC_AUDIO_SOURCE_AUTO, and
is "resolved" only if audio is enabled.

But the server arguments were built assuming that the audio source was
never SC_AUDIO_SOURCE_AUTO (even with audio disabled), causing a crash.

Regression introduced by a10f8cd798023f858796b023cb846fa2184ad2c7.
2024-07-29 20:03:44 +02:00
Romain Vimont
127a271d34 Switch audio source if audio-dup is set
Automatically switch implicit audio source to "playback" if --audio-dup
is passed.

This allows to run:

    scrcpy --audio-dup

without specifying explicitly:

    scrcpy --audio-source=playback --audio-dup

PR #5102 <https://github.com/Genymobile/scrcpy/pull/5102>
2024-07-19 17:48:39 +02:00
Romain Vimont
31116a60d7 Add --audio-dup
Add an option to duplicate audio on the device, compatible with the new
audio playback capture (--audio-source=playback).

Fixes #3875 <https://github.com/Genymobile/scrcpy/issues/3875>
Fixes #4380 <https://github.com/Genymobile/scrcpy/issues/4380>
PR #5102 <https://github.com/Genymobile/scrcpy/pull/5102>

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2024-07-19 17:48:39 +02:00
Romain Vimont
a10f8cd798 Add audio playback capture method
Add a new method to capture audio playback.

It requires Android 13 (where the Shell app has MODIFY_AUDIO_ROUTING
permission).

The main benefit is that it supports keeping audio playing on the device
(implemented in a further commit).

Fixes #4380 <https://github.com/Genymobile/scrcpy/issues/4380>
PR #5102 <https://github.com/Genymobile/scrcpy/pull/5102>

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2024-07-19 17:48:39 +02:00
Romain Vimont
c57a0512ba Add assertions
Passing an unknown enum value to convert them to string would return
NULL without any error, possibly causing undefined behavior later.

Add assertions to catch such programming errors early.
2024-07-16 21:31:31 +02:00
Romain Vimont
9989668226 Add mouse secondary bindings
Add secondary bindings (Shift+click) for mouse buttons.

In addition to:

    --mouse-bind=xxxx

It is now possible to pass a sequence of secondary bindings:

    --mouse-bind=xxxx:xxxx
                 <--> <-->
             primary   secondary
            bindings   bindings

If the second sequence is omitted, then it is the same as the first one.

By default, for SDK mouse, primary bindings trigger shortcuts and
secondary bindings forward all clicks.

For AOA and UHID, the default bindings are reversed: all clicks are
forwarded by default, whereas pressing Shift+click trigger shortcuts.

    --mouse-bind=bhsn:++++  # default for SDK
    --mouse-bind=++++:bhsn  # default for AOA and UHID

Refs 035d60cf5d3f4c83d48735b4cb4cd108a5b5f413
Refs f5e6b8092afd82bab402e7c2c3d00b1719f9bb57
Fixes #5055 <https://github.com/Genymobile/scrcpy/issues/5055>
PR #5076 <https://github.com/Genymobile/scrcpy/pull/5076>
2024-07-11 12:04:09 +02:00
Romain Vimont
6baea57987 Track mouse buttons state manually
The buttons state was tracked by SDL_GetMouseState(), and scrcpy applied
a mask to ignore buttons used for shortcuts.

Instead, track the buttons actually pressed (ignoring shortcuts)
manually, to prepare the introduction of more dynamic mouse shortcuts.

PR #5076 <https://github.com/Genymobile/scrcpy/pull/5076>
2024-07-11 12:04:09 +02:00
Romain Vimont
86b8286217 Remove unused virtual mouse
PR #5076 <https://github.com/Genymobile/scrcpy/pull/5076>
2024-07-11 12:04:09 +02:00
Romain Vimont
51fee79bf5 Use finger source when a pointer is simulated
For pinch-to-zoom, rotation and tilt simulation, always use a finger
source (instead of a mouse) for both pointers (the real one and the
simulated one).

A "virtual" mouse does not work on all devices (e.g. on Pixel 8).

PR #5076 <https://github.com/Genymobile/scrcpy/pull/5076>
2024-07-11 12:04:09 +02:00
Romain Vimont
6808288823 Make pointer id independent of mouse bindings
The device source (MOUSE or FINGER) to use depended on whether a
secondary click was possible via mouse bindings.

As a first step, always use a mouse source to break this dependency.
Note that this change might cause regressions in some (unknown) cases
(refs f70359f14fb13f277c65b96f43ec83aba4722457), but hopefully not.

Further commits will restore a finger source in some specific use cases,
but independent of secondary clicks.

Refs #5055 <https://github.com/Genymobile/scrcpy/issues/5055>
Fixes #5067 <https://github.com/Genymobile/scrcpy/issues/5067>
PR #5076 <https://github.com/Genymobile/scrcpy/pull/5076>
2024-07-11 12:04:09 +02:00