Compare commits

..

59 Commits

Author SHA1 Message Date
Romain Vimont
4cc4abdcc8 Mention issue with AOA and multiple gamepads
Android does not support multiple HID gamepads properly over AOA.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 18:58:53 +02:00
Romain Vimont
befc0fac5b Mention UHID permission errors
UHID may not work on old Android versions due to permission errors.

Mention it in UHID mouse and gamepad documentation (it was already
mentioned for UHID keyboard).

Refs #4473 comment <https://github.com/Genymobile/scrcpy/pull/4473#issuecomment-1975133226>
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 18:58:44 +02:00
Romain Vimont
f01a622ead Enable joystick events in background
Capture the gamepads even when the window is not focused.

Note: In theory, with this flag set, we could capture gamepad events
even without a window (--no-window). In practice, scrcpy still requires
a window, because --no-window implies --no-control, and the input
manager is owned by the sc_screen instance, which does not exist if
there is no window. Supporting this use case would require a lot of
refactors.

Refs <https://github.com/Genymobile/scrcpy/pull/5270#issuecomment-2339360460>
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>

Suggested-by: Luiz Henrique Laurini <luizhenriquelaurini@gmail.com>
2024-09-15 11:21:56 +02:00
Romain Vimont
0ba430a462 Add gamepad user documentation
Mainly copied and adapted from HID keyboard and mouse documentation.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
91d40c7548 Fix link in OTG documentation
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
9f3d51106d Remove fragile assert()
The sc_uhid_devices instance is initialized only when there is a UHID
keyboard.

The device message receiver assumed that it could not receive HID output
reports without a sc_uhid_devices instance (i.e. without a UHID
keyboard), but in practice, a UHID driver implementation on the device
may decide to send UHID output reports for mouse or for gamepads (and we
must just ignore them).

So remove the assert().

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
bf2b679e70 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 cannot
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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
7f250dd669 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
68e27c7357 Reorder function parameters for consistency
Make the local function write_string() accept the output buffer as a
first parameter, like the other similar functions.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
c4febd55eb 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
f9d1a333a0 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).

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
64a25f6e9d Add UHID_DESTROY control message
This message will be sent on gamepad disconnection.

Contrary to keyboard and mouse devices, which are registered once and
unregistered when scrcpy exists, each physical gamepad is mapped with
its own HID id, and they can be plugged and unplugged dynamically.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
5fe884276b Add gamepad support in OTG mode
Implement gamepad support for OTG.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
3e68244dd3 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 be trigged automatically
until SDL events are "pumped" (SDL_PumpEvents/SDL_WaitEvent).

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
a34a62ca4b Add AOA gamepad support
Similar to AOA keyboard and mouse, but for gamepads.

Can be enabled with --gamepad=aoa.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
a59c6df4b7 Implement HID gamepad
Implement the HID protocol for gamepads, that will be used in further
commits by the AOA and UHID gamepad processor implementations.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
f4d1e49ad9 Add util functions to write in little-endian
This will be helpful for writing HID values.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
4565f36ee6 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>

Co-authored-by: Luiz Henrique Laurini <luizhenriquelaurini@gmail.com>
2024-09-15 11:21:56 +02:00
Romain Vimont
c8479fe8bf Discard unknown SDL events
Mouse and keyboard events with unknown button/keycode/scancode cannot be
handled properly. Discard them without forwarding them to the
keyboard or mouse processors.

This can happen for example if a more recent version of SDL introduces
new enum values.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
de8455400c Fix HID comments
Fix typo and reference the latest version of "HID Usage Tables"
specifications.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
1f5be743b4 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
222916eebe 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
6c707ad8a3 Make HID logs uniform
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
d748ac75e6 Add AOA open/close verbose logs
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
6f0c9eba9b Introduce hid_open and hid_close events
This allows to handle HID open/close reports at the same place as HID
input reports (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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
f6219d2640 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
6e9b0d7d4c Make AOA open and close asynchronous
For AOA keyboard and mouse, only input reports were asynchronous.
Register/unregister were called from the main thread.

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

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
3e9c89c535 Reorder AOA functions
This will allow sc_aoa_setup_hid() to compile even when
sc_aoa_unregister_hid() will be made static.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
9af3bacdd6 Refactor AOA handling
Extract event processing to a separate function.

This will make the code more readable when more event types will be
added.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
2dd02ebb80 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
dad04bf138 Fix HID mouse header guard
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
1afc8ca368 Add missing SC_ prefix for HID mouse event size
PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
785099b74d Remove duplicate definition SC_HID_MAX_SIZE
This constant is defined in hid_event.h.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
08da2e068e Fail on AOA keyboard/mouse initialization error
If the AOA keyboard or the AOA mouse fails to be initialized, this is a
fatal error.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
49c8ca34fd 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 all further UHID_INPUT messages invalid.
Therefore, mark these messages as non-droppable.

A non-droppable event is queued anyway (resizing the queue if
necessary, unless the allocation fails).

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
a84b0dfd0c Remove atomics from keyboard_uhid
The UHID output callback is now called from the same (main) thread as
the process_key() function.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
cbf5db85c1 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.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
72ee195693 Set clipboard from the main thread
The clipboard changes from the device are received from a separate
thread, but they must be handled from the main thread.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
8620d06741 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).

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:55 +02:00
Romain Vimont
e9240f6804 Expose main thread id
This will allow to assert that a function is called from the main
thread.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-14 21:24:15 +02:00
Romain Vimont
e9b32d8a52 Extract sc_push_event()
Expose a convenience function to push an event without args to the main
thread.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-14 21:24:15 +02:00
Romain Vimont
ce4e1fc420 Store events numbers in an enum
This avoids to manually set an explicit value for each item.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-14 21:24:15 +02:00
Romain Vimont
e8f02685e9 Fix deprecated references 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-14 21:24:15 +02:00
Romain Vimont
4a6b335f7d Do not send uninitialized HID event
If the function returns false, then there is nothing to send.
2024-09-14 21:24:15 +02:00
Romain Vimont
90ee0062cb Fix compilation with -Dusb=false
UHID does not depend on USB support, so the struct sc_uhid_devices must
always be defined.
2024-09-14 21:24:15 +02:00
Romain Vimont
e03888d587 Reject arguments containing new line characters
Refs bec3321fff
2024-09-14 21:23:44 +02:00
Romain Vimont
8453e3ba7d Enable TCP_NODELAY for the control socket
It is better to disable Nagle's algorithm to avoid unnecessary latency
for control messages. (I'm not sure this has any impact for a local TCP
socket though.)
2024-09-14 19:46:55 +02:00
Romain Vimont
145a9468fd Fix ifdef _WIN32
We use _WIN32 across the code base, not __WIN32.
2024-09-14 14:42:00 +02:00
Romain Vimont
1d713d7598 Do not parse --max-fps float in the client
Many parsing and formatting C functions like strtof() and asprintf() are
locale-dependent. Forcing a C locale just for the conversions in a way
that works on all platforms is a mess.

In practice, this is not a problem, scrcpy always uses the C locale,
because it never calls:

    setlocale(LC_ALL, "");

But the max-fps option should not depend on the locale configuration
anyway.

Since the value is parsed by the client in Java anyway, just forward the
string value as is.
2024-09-14 14:37:30 +02:00
Romain Vimont
265a15e0b1 Accept float values for --max-fps
Android accepts a float value, there is no reason to limit the option
to be an integer.

In particular, it allows to capture at a rate lower than 1 fps. For
example, to capture 1 frame every 5 seconds:

    scrcpy --video-source=camera --max-fps=0.2

It was already possible to pass a float manually:

    scrcpy --video-source=camera \
        --video-codec-options=max-fps-to-encoder:float=0.2

But accepting a float directly for --max-fps is more convenient.

Refs <https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_FPS_TO_ENCODER>
2024-09-13 22:02:25 +02:00
Romain Vimont
6451ad271a Ignore minBufferSize on error
A negative return value from AudioRecord.getMinBufferSize() represents
an error. Only consider positive values (0 would be invalid).

Refs #5228 <https://github.com/Genymobile/scrcpy/issues/5228>
2024-09-13 20:03:17 +02:00
Romain Vimont
bec3321fff Validate server arguments
Some command line arguments are passed as is to "adb shell". Therefore,
they must not contain special shell characters.
2024-09-13 19:53:05 +02:00
Romain Vimont
dea1fe3386 Validate crop and video size
A video width or height of 0 triggered an assert.

Fail explicitly instead: the server may actually send this size in
practice (for example on cropping with small dimensions, even if the
requested crop size is not 0).
2024-09-13 19:48:55 +02:00
Romain Vimont
a7cae59578 Improve delay buffer startup
The delay buffer clock estimates the clock offset between the PTS and
the frame decoded date using an "Exponentially Weighted Moving Average"
(EWMA).

But for the first frames, the clock have less than SC_CLOCK_RANGE
points to average. Since the timing for the first frames are typically
the worst ones, give more weight to the last point for the estimation.

Once SC_CLOCK_RANGE points are available (i.e. when SC_CLOCK_RANGE ==
clock->range), the new estimation is equivalent to the previous version.
2024-09-12 11:06:13 +02:00
Romain Vimont
f089ea67e1 Add missing flag initialization
The delay buffer `stopped` field was not initialized.

Since it practice the unique instance of sc_delay_buffer is initialized
in static memory, the flag was initialized to false as a side effect.

But with commit fd0f432e87, in debug mode
only, the delay buffer was broken.
2024-09-11 15:41:49 +02:00
Romain Vimont
63ced79842 Reverse NDEBUG conditions
By default, these specific debug logs are disabled.

Make the ifdef condition less confusing.
2024-09-11 15:41:29 +02:00
Romain Vimont
33a8c39beb Fix local NDEBUG define
The struct definition and the implementation did not use the same NDEBUG
constant.
2024-09-11 11:26:07 +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
59 changed files with 806 additions and 610 deletions

View File

@@ -37,6 +37,7 @@ Its features include:
- [camera mirroring](doc/camera.md) (Android 12+)
- [mirroring as a webcam (V4L2)](doc/v4l2.md) (Linux-only)
- physical [keyboard][hid-keyboard] and [mouse][hid-mouse] simulation (HID)
- [gamepad](doc/gamepad.md) support
- [OTG mode](doc/otg.md)
- and more…
@@ -111,6 +112,13 @@ Here are just some common examples.
scrcpy --otg
```
- Control the device using gamepad controllers plugged into the computer:
```bash
scrcpy --gamepad=uhid
scrcpy -G # short version
```
## User documentation
The application provides a lot of features and configuration options. They are
@@ -122,6 +130,7 @@ documented in the following pages:
- [Control](doc/control.md)
- [Keyboard](doc/keyboard.md)
- [Mouse](doc/mouse.md)
- [Gamepad](doc/gamepad.md)
- [Device](doc/device.md)
- [Window](doc/window.md)
- [Recording](doc/recording.md)

View File

@@ -26,6 +26,8 @@ _scrcpy() {
-e --select-tcpip
-f --fullscreen
--force-adb-forward
-G
--gamepad=
-h --help
-K
--keyboard=
@@ -127,6 +129,10 @@ _scrcpy() {
COMPREPLY=($(compgen -W 'disabled sdk uhid aoa' -- "$cur"))
return
;;
--gamepad)
COMPREPLY=($(compgen -W 'disabled uhid aoa' -- "$cur"))
return
;;
--orientation|--display-orientation)
COMPREPLY=($(compgen -W '0 90 180 270 flip0 flip90 flip180 flip270' -- "$cur"))
return

View File

@@ -33,8 +33,10 @@ arguments=(
{-e,--select-tcpip}'[Use TCP/IP device]'
{-f,--fullscreen}'[Start in fullscreen]'
'--force-adb-forward[Do not attempt to use \"adb reverse\" to connect to the device]'
'-G[Use UHID/AOA gamepad (same as --gamepad=uhid or --gamepad=aoa, depending on OTG mode)]'
'--gamepad=[Set the gamepad input mode]:mode:(disabled uhid aoa)'
{-h,--help}'[Print the help]'
'-K[Use UHID keyboard (same as --keyboard=uhid)]'
'-K[Use UHID/AOA keyboard (same as --keyboard=uhid or --keyboard=aoa, depending on OTG mode)]'
'--keyboard=[Set the keyboard input mode]:mode:(disabled sdk uhid aoa)'
'--kill-adb-on-close[Kill adb when scrcpy terminates]'
'--legacy-paste[Inject computer clipboard text as a sequence of key events on Ctrl+v]'
@@ -44,7 +46,7 @@ arguments=(
'--list-encoders[List video and audio encoders available on the device]'
'--lock-video-orientation=[Lock video orientation]:orientation:(unlocked initial 0 90 180 270)'
{-m,--max-size=}'[Limit both the width and height of the video to value]'
'-M[Use UHID mouse (same as --mouse=uhid)]'
'-M[Use UHID/AOA mouse (same as --mouse=uhid or --mouse=aoa, depending on OTG mode)]'
'--max-fps=[Limit the frame rate of screen capture]'
'--mouse=[Set the mouse input mode]:mode:(disabled sdk uhid aoa)'
'--mouse-bind=[Configure bindings of secondary clicks]'

View File

@@ -175,15 +175,19 @@ Start in fullscreen.
.B \-\-force\-adb\-forward
Do not attempt to use "adb reverse" to connect to the device.
.TP
.B \-G
Same as \fB\-\-gamepad=uhid\fR, or \fB\-\-keyboard=aoa\fR if \fB\-\-otg\fR is set.
.TP
.BI "\-\-gamepad " mode
Select how to send gamepad inputs to the device.
Possible values are "disabled", "uhid" and "aoa":
- "disabled" does not send keyboard inputs to the device.
- "uhid" simulates a physical HID gamepad using the Linux HID kernel module on the device.
- "aoa" simulates a physical HID gamepad using the AOAv2 protocol. It may only work over USB.
- "disabled" does not send gamepad inputs to the device.
- "uhid" simulates physical HID gamepads using the Linux HID kernel module on the device.
- "aoa" simulates physical HID gamepads using the AOAv2 protocol. It may only work over USB.
Also see \fB\-\-keyboard\f and R\fB\-\-mouse\fR.
.TP
@@ -192,7 +196,7 @@ Print this help.
.TP
.B \-K
Same as \fB\-\-keyboard=uhid\fR.
Same as \fB\-\-keyboard=uhid\fR, or \fB\-\-keyboard=aoa\fR if \fB\-\-otg\fR is set.
.TP
.BI "\-\-keyboard " mode
@@ -257,7 +261,7 @@ Default is 0 (unlimited).
.TP
.B \-M
Same as \fB\-\-mouse=uhid\fR.
Same as \fB\-\-mouse=uhid\fR, or \fB\-\-mouse=aoa\fR if \fB\-\-otg\fR is set.
.TP
.BI "\-\-max\-fps " value

View File

@@ -5,7 +5,7 @@
#include "util/log.h"
#define SC_AUDIO_PLAYER_NDEBUG // comment to debug
//#define SC_AUDIO_PLAYER_DEBUG // uncomment to debug
/**
* Real-time audio player with configurable latency
@@ -72,7 +72,7 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) {
size_t len = len_int;
uint32_t count = TO_SAMPLES(len);
#ifndef SC_AUDIO_PLAYER_NDEBUG
#ifdef SC_AUDIO_PLAYER_DEBUG
LOGD("[Audio] SDL callback requests %" PRIu32 " samples", count);
#endif
@@ -162,7 +162,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
// swr_convert() returns the number of samples which would have been
// written if the buffer was big enough.
uint32_t samples = MIN(ret, dst_nb_samples);
#ifndef SC_AUDIO_PLAYER_NDEBUG
#ifdef SC_AUDIO_PLAYER_DEBUG
LOGD("[Audio] %" PRIu32 " samples written to buffer", samples);
#endif
@@ -244,7 +244,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
if (played) {
LOGD("[Audio] Buffering threshold exceeded, skipping %" PRIu32
" samples", skip_samples);
#ifndef SC_AUDIO_PLAYER_NDEBUG
#ifdef SC_AUDIO_PLAYER_DEBUG
} else {
LOGD("[Audio] Playback not started, skipping %" PRIu32
" samples", skip_samples);
@@ -282,7 +282,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
// However, the buffering level must be smoothed
sc_average_push(&ap->avg_buffering, can_read);
#ifndef SC_AUDIO_PLAYER_NDEBUG
#ifdef SC_AUDIO_PLAYER_DEBUG
LOGD("[Audio] can_read=%" PRIu32 " avg_buffering=%f",
can_read, sc_average_get(&ap->avg_buffering));
#endif

View File

@@ -375,7 +375,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'G',
.text = "Same as --gamepad=uhid.",
.text = "Same as --gamepad=uhid, or --gamepad=aoa if --otg is set.",
},
{
.longopt_id = OPT_GAMEPAD,
@@ -384,10 +384,10 @@ static const struct sc_option options[] = {
.text = "Select how to send gamepad inputs to the device.\n"
"Possible values are \"disabled\", \"uhid\" and \"aoa\".\n"
"\"disabled\" does not send gamepad inputs to the device.\n"
"\"uhid\" simulates a physical HID gamepad using the Linux "
"UHID kernel module on the device.\n"
"\"aoa\" simulates a physical gamepad using the AOAv2 "
"protocol. It may only work over USB.\n"
"\"uhid\" simulates physical HID gamepads using the Linux UHID "
"kernel module on the device.\n"
"\"aoa\" simulates physical gamepads using the AOAv2 protocol."
"It may only work over USB.\n"
"Also see --keyboard and --mouse.",
},
{
@@ -397,7 +397,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'K',
.text = "Same as --keyboard=uhid.",
.text = "Same as --keyboard=uhid, or --keyboard=aoa if --otg is set.",
},
{
.longopt_id = OPT_KEYBOARD,
@@ -493,7 +493,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'M',
.text = "Same as --mouse=uhid.",
.text = "Same as --mouse=uhid, or --mouse=aoa if --otg is set.",
},
{
.longopt_id = OPT_MAX_FPS,
@@ -1491,18 +1491,6 @@ parse_max_size(const char *s, uint16_t *max_size) {
return true;
}
static bool
parse_max_fps(const char *s, uint16_t *max_fps) {
long value;
bool ok = parse_integer_arg(s, &value, false, 0, 0xFFFF, "max fps");
if (!ok) {
return false;
}
*max_fps = (uint16_t) value;
return true;
}
static bool
parse_buffering_time(const char *s, sc_tick *tick) {
long value;
@@ -2264,7 +2252,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
args->help = true;
break;
case 'K':
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_UHID;
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_KEYBOARD:
if (!parse_keyboard(optarg, &opts->keyboard_input_mode)) {
@@ -2276,9 +2264,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
"--keyboard=uhid instead.");
return false;
case OPT_MAX_FPS:
if (!parse_max_fps(optarg, &opts->max_fps)) {
return false;
}
opts->max_fps = optarg;
break;
case 'm':
if (!parse_max_size(optarg, &opts->max_size)) {
@@ -2286,7 +2272,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
break;
case 'M':
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_UHID;
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_MOUSE:
if (!parse_mouse(optarg, &opts->mouse_input_mode)) {
@@ -2671,7 +2657,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
opts->audio_dup = true;
break;
case 'G':
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_UHID;
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_GAMEPAD:
if (!parse_gamepad(optarg, &opts->gamepad_input_mode)) {
@@ -2795,7 +2781,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
if (opts->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_AUTO) {
opts->keyboard_input_mode = otg ? SC_KEYBOARD_INPUT_MODE_AOA
: SC_KEYBOARD_INPUT_MODE_SDK;
} else if (opts->keyboard_input_mode
== SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA) {
opts->keyboard_input_mode = otg ? SC_KEYBOARD_INPUT_MODE_AOA
: SC_KEYBOARD_INPUT_MODE_UHID;
}
if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_AUTO) {
if (otg) {
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_AOA;
@@ -2805,19 +2796,17 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
} else {
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_SDK;
}
} else if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_UHID_OR_AOA) {
opts->mouse_input_mode = otg ? SC_MOUSE_INPUT_MODE_AOA
: SC_MOUSE_INPUT_MODE_UHID;
} else if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_SDK
&& !opts->video_playback) {
LOGE("SDK mouse mode requires video playback. Try --mouse=uhid.");
return false;
}
if (opts->gamepad_input_mode == SC_GAMEPAD_INPUT_MODE_AUTO) {
if (otg) {
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_AOA;
} else {
// UHID does not work on all devices (with old Android
// versions), so it cannot be enabled by default
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_DISABLED;
}
if (opts->gamepad_input_mode == SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA) {
opts->gamepad_input_mode = otg ? SC_GAMEPAD_INPUT_MODE_AOA
: SC_GAMEPAD_INPUT_MODE_UHID;
}
}
@@ -2875,9 +2864,17 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
enum sc_gamepad_input_mode gmode = opts->gamepad_input_mode;
if (gmode != SC_GAMEPAD_INPUT_MODE_AOA
&& gmode != SC_GAMEPAD_INPUT_MODE_DISABLED) {
LOGE("In OTG mode, --gamepad only supports aoa or disabled.");
return false;
}
if (kmode == SC_KEYBOARD_INPUT_MODE_DISABLED
&& mmode == SC_MOUSE_INPUT_MODE_DISABLED) {
LOGE("Could not disable both keyboard and mouse in OTG mode.");
&& mmode == SC_MOUSE_INPUT_MODE_DISABLED
&& gmode == SC_GAMEPAD_INPUT_MODE_DISABLED) {
LOGE("Cannot not disable all inputs in OTG mode.");
return false;
}
}
@@ -2918,18 +2915,18 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
if (opts->camera_id && opts->camera_facing != SC_CAMERA_FACING_ANY) {
LOGE("Could not specify both --camera-id and --camera-facing");
LOGE("Cannot specify both --camera-id and --camera-facing");
return false;
}
if (opts->camera_size) {
if (opts->max_size) {
LOGE("Could not specify both --camera-size and -m/--max-size");
LOGE("Cannot specify both --camera-size and -m/--max-size");
return false;
}
if (opts->camera_ar) {
LOGE("Could not specify both --camera-size and --camera-ar");
LOGE("Cannot specify both --camera-size and --camera-ar");
return false;
}
}
@@ -3070,19 +3067,19 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
if (!opts->control) {
if (opts->turn_screen_off) {
LOGE("Could not request to turn screen off if control is disabled");
LOGE("Cannot request to turn screen off if control is disabled");
return false;
}
if (opts->stay_awake) {
LOGE("Could not request to stay awake if control is disabled");
LOGE("Cannot request to stay awake if control is disabled");
return false;
}
if (opts->show_touches) {
LOGE("Could not request to show touches if control is disabled");
LOGE("Cannot request to show touches if control is disabled");
return false;
}
if (opts->power_off_on_close) {
LOGE("Could not request power off on close if control is disabled");
LOGE("Cannot request power off on close if control is disabled");
return false;
}
}
@@ -3107,7 +3104,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
// OTG mode is compatible with only very few options.
// Only report obvious errors.
if (opts->record_filename) {
LOGE("OTG mode: could not record");
LOGE("OTG mode: cannot record");
return false;
}
if (opts->turn_screen_off) {

View File

@@ -4,7 +4,7 @@
#include "util/log.h"
#define SC_CLOCK_NDEBUG // comment to debug
//#define SC_CLOCK_DEBUG // uncomment to debug
#define SC_CLOCK_RANGE 32
@@ -21,10 +21,12 @@ sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
}
sc_tick offset = system - stream;
clock->offset = ((clock->range - 1) * clock->offset + offset)
/ clock->range;
unsigned clock_weight = clock->range - 1;
unsigned value_weight = SC_CLOCK_RANGE - clock->range + 1;
clock->offset = (clock->offset * clock_weight + offset * value_weight)
/ SC_CLOCK_RANGE;
#ifndef SC_CLOCK_NDEBUG
#ifdef SC_CLOCK_DEBUG
LOGD("Clock estimation: pts + %" PRItick, clock->offset);
#endif
}

View File

@@ -8,7 +8,7 @@
#include <libavutil/version.h>
#include <SDL2/SDL_version.h>
#ifndef __WIN32
#ifndef _WIN32
# define PRIu64_ PRIu64
# define SC_PRIsizet "zu"
#else

View File

@@ -83,15 +83,34 @@ write_position(uint8_t *buf, const struct sc_position *position) {
sc_write16be(&buf[10], position->screen_size.height);
}
// write length (4 bytes) + string (non null-terminated)
// Write truncated string, and return the size
static size_t
write_string(const char *utf8, size_t max_len, uint8_t *buf) {
write_string_payload(uint8_t *payload, const char *utf8, size_t max_len) {
if (!utf8) {
return 0;
}
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
memcpy(payload, utf8, len);
return len;
}
// Write length (4 bytes) + string (non null-terminated)
static size_t
write_string(uint8_t *buf, const char *utf8, size_t max_len) {
size_t len = write_string_payload(buf + 4, utf8, max_len);
sc_write32be(buf, len);
memcpy(&buf[4], utf8, len);
return 4 + len;
}
// Write length (1 byte) + string (non null-terminated)
static size_t
write_string_tiny(uint8_t *buf, const char *utf8, size_t max_len) {
assert(max_len <= 0xFF);
size_t len = write_string_payload(buf + 1, utf8, max_len);
buf[0] = len;
return 1 + len;
}
size_t
sc_control_msg_serialize(const struct sc_control_msg *msg, uint8_t *buf) {
buf[0] = msg->type;
@@ -103,9 +122,8 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, uint8_t *buf) {
sc_write32be(&buf[10], msg->inject_keycode.metastate);
return 14;
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
size_t len =
write_string(msg->inject_text.text,
SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH, &buf[1]);
size_t len = write_string(&buf[1], msg->inject_text.text,
SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
return 1 + len;
}
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
@@ -137,19 +155,26 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, uint8_t *buf) {
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
sc_write64be(&buf[1], msg->set_clipboard.sequence);
buf[9] = !!msg->set_clipboard.paste;
size_t len = write_string(msg->set_clipboard.text,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
&buf[10]);
size_t len = write_string(&buf[10], msg->set_clipboard.text,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
return 10 + len;
case SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
buf[1] = msg->set_screen_power_mode.mode;
return 2;
case SC_CONTROL_MSG_TYPE_UHID_CREATE:
sc_write16be(&buf[1], msg->uhid_create.id);
sc_write16be(&buf[3], msg->uhid_create.report_desc_size);
memcpy(&buf[5], msg->uhid_create.report_desc,
msg->uhid_create.report_desc_size);
return 5 + msg->uhid_create.report_desc_size;
size_t index = 3;
index += write_string_tiny(&buf[index], msg->uhid_create.name, 127);
sc_write16be(&buf[index], msg->uhid_create.report_desc_size);
index += 2;
memcpy(&buf[index], msg->uhid_create.report_desc,
msg->uhid_create.report_desc_size);
index += msg->uhid_create.report_desc_size;
return index;
case SC_CONTROL_MSG_TYPE_UHID_INPUT:
sc_write16be(&buf[1], msg->uhid_input.id);
sc_write16be(&buf[3], msg->uhid_input.size);
@@ -255,10 +280,15 @@ sc_control_msg_log(const struct sc_control_msg *msg) {
case SC_CONTROL_MSG_TYPE_ROTATE_DEVICE:
LOG_CMSG("rotate device");
break;
case SC_CONTROL_MSG_TYPE_UHID_CREATE:
LOG_CMSG("UHID create [%" PRIu16 "] report_desc_size=%" PRIu16,
msg->uhid_create.id, msg->uhid_create.report_desc_size);
case SC_CONTROL_MSG_TYPE_UHID_CREATE: {
// Quote only if name is not null
const char *name = msg->uhid_create.name;
const char *quote = name ? "\"" : "";
LOG_CMSG("UHID create [%" PRIu16 "] name=%s%s%s "
"report_desc_size=%" PRIu16, msg->uhid_create.id,
quote, name, quote, msg->uhid_create.report_desc_size);
break;
}
case SC_CONTROL_MSG_TYPE_UHID_INPUT: {
char *hex = sc_str_to_hex_string(msg->uhid_input.data,
msg->uhid_input.size);

View File

@@ -98,6 +98,7 @@ struct sc_control_msg {
} set_screen_power_mode;
struct {
uint16_t id;
const char *name; // pointer to static data
uint16_t report_desc_size;
const uint8_t *report_desc; // pointer to static data
} uhid_create;

View File

@@ -116,7 +116,7 @@ sc_controller_push_msg(struct sc_controller *controller,
LOG_OOM();
}
}
// Otherwise (if the queue is full), the msg is discarded
// Otherwise, the msg is discarded
sc_mutex_unlock(&controller->mutex);

View File

@@ -8,8 +8,6 @@
#include "util/log.h"
#define SC_BUFFERING_NDEBUG // comment to debug
/** Downcast frame_sink to sc_delay_buffer */
#define DOWNCAST(SINK) container_of(SINK, struct sc_delay_buffer, frame_sink)
@@ -80,7 +78,7 @@ run_buffering(void *data) {
goto stopped;
}
#ifndef SC_BUFFERING_NDEBUG
#ifdef SC_BUFFERING_DEBUG
LOGD("Buffering: %" PRItick ";%" PRItick ";%" PRItick,
pts, dframe.push_date, sc_tick_now());
#endif
@@ -134,6 +132,7 @@ sc_delay_buffer_frame_sink_open(struct sc_frame_sink *sink,
sc_clock_init(&db->clock);
sc_vecdeque_init(&db->queue);
db->stopped = false;
if (!sc_frame_source_sinks_open(&db->frame_source, ctx)) {
goto error_destroy_wait_cond;
@@ -206,7 +205,7 @@ sc_delay_buffer_frame_sink_push(struct sc_frame_sink *sink,
return false;
}
#ifndef SC_BUFFERING_NDEBUG
#ifdef SC_BUFFERING_DEBUG
dframe.push_date = sc_tick_now();
#endif

View File

@@ -12,12 +12,14 @@
#include "util/tick.h"
#include "util/vecdeque.h"
//#define SC_BUFFERING_DEBUG // uncomment to debug
// forward declarations
typedef struct AVFrame AVFrame;
struct sc_delayed_frame {
AVFrame *frame;
#ifndef NDEBUG
#ifdef SC_BUFFERING_DEBUG
sc_tick push_date;
#endif
};

View File

@@ -1,6 +1,7 @@
#include "events.h"
#include "util/log.h"
#include "util/thread.h"
bool
sc_push_event_impl(uint32_t type, const char *name) {
@@ -32,9 +33,34 @@ sc_post_to_main_thread(sc_runnable_fn run, void *userdata) {
// ret == 0: event was filtered
// ret == 1: success
if (ret != 1) {
LOGW("Coud not post to main thread: %s", SDL_GetError());
if (ret == 0) {
// if ret == 0, this is expected on exit, log in debug mode
LOGD("Could not post runnable to main thread (filtered)");
} else {
assert(ret < 0);
LOGW("Could not post runnable to main thread: %s", SDL_GetError());
}
return false;
}
return true;
}
static int SDLCALL
task_event_filter(void *userdata, SDL_Event *event) {
(void) userdata;
if (event->type == SC_EVENT_RUN_ON_MAIN_THREAD) {
// Reject this event type from now on
return 0;
}
return 1;
}
void
sc_reject_new_runnables(void) {
assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
SDL_SetEventFilter(task_event_filter, NULL);
}

View File

@@ -1,5 +1,5 @@
#ifndef SC_EVENTS
#define SC_EVENTS
#ifndef SC_EVENTS_H
#define SC_EVENTS_H
#include "common.h"
@@ -32,4 +32,7 @@ typedef void (*sc_runnable_fn)(void *userdata);
bool
sc_post_to_main_thread(sc_runnable_fn run, void *userdata);
void
sc_reject_new_runnables(void);
#endif

View File

@@ -15,6 +15,7 @@ struct sc_hid_input {
struct sc_hid_open {
uint16_t hid_id;
const char *name; // pointer to static memory
const uint8_t *report_desc; // pointer to static memory
size_t report_desc_size;
};

View File

@@ -6,17 +6,17 @@
#include "util/binary.h"
#include "util/log.h"
// 2 bytes for left stick (X, Y)
// 2 bytes for right stick (Z, Rz)
// 2 bytes for L2/R2 triggers
// 2x2 bytes for left stick (X, Y)
// 2x2 bytes for right stick (Z, Rz)
// 2x2 bytes for L2/R2 triggers
// 2 bytes for buttons + padding,
// 1 byte for hat switch (dpad) + padding
#define SC_HID_GAMEPAD_EVENT_SIZE 15
// The ->buttons field stores the state for all buttons, but only some of them
// (the 16 LSB) must be transmitted "as is". The DPAD (hat switch) are stored
// locally in the MSB, but not transmitted as is: they are transformed to
// generate another specific byte.
// (the 16 LSB) must be transmitted "as is". The DPAD (hat switch) buttons are
// stored locally in the MSB of this field, but not transmitted as is: they are
// transformed to generate another specific byte.
#define SC_HID_BUTTONS_MASK 0xFFFF
// outside SC_HID_BUTTONS_MASK
@@ -26,7 +26,7 @@
#define SC_GAMEPAD_BUTTONS_BIT_DPAD_RIGHT UINT32_C(0x80000)
/**
* Gamepad descriptor manually crafted to transmit the events.
* Gamepad descriptor manually crafted to transmit the input reports.
*
* The HID specification is available here:
* <https://www.usb.org/document-library/device-class-definition-hid-111>
@@ -76,8 +76,8 @@ static const uint8_t SC_HID_GAMEPAD_REPORT_DESC[] = {
0x09, 0xC4,
// Logical Minimum (0)
0x15, 0x00,
// Logical Maximum (65535)
0x27, 0xFF, 0xFF, 0x00, 0x00,
// Logical Maximum (32767)
0x26, 0xFF, 0x7F,
// Report Size (16)
0x75, 0x10,
// Report Count (2)
@@ -89,26 +89,19 @@ static const uint8_t SC_HID_GAMEPAD_REPORT_DESC[] = {
0x05, 0x09,
// Usage Minimum (1)
0x19, 0x01,
// Usage Maximum (15)
0x29, 0x0E,
// Usage Maximum (16)
0x29, 0x10,
// Logical Minimum (0)
0x15, 0x00,
// Logical Maximum (1)
0x25, 0x01,
// Report Count (15)
0x95, 0x0F,
// Report Count (16)
0x95, 0x10,
// Report Size (1)
0x75, 0x01,
// Input (Data, Variable, Absolute): 15 buttons bits
// Input (Data, Variable, Absolute): 16 buttons bits
0x81, 0x02,
// Report Count (1)
0x95, 0x01,
// Report Size (1)
0x75, 0x01,
// Input (Constant): 1-bit padding
0x81, 0x01,
// Usage Page (Generic Desktop)
0x05, 0x01,
// Usage (Hat switch)
@@ -124,7 +117,6 @@ static const uint8_t SC_HID_GAMEPAD_REPORT_DESC[] = {
// Input (Data, Variable, Null State): 4-bit value
0x81, 0x42,
// End Collection
0xC0,
@@ -133,7 +125,7 @@ static const uint8_t SC_HID_GAMEPAD_REPORT_DESC[] = {
};
/**
* A gamepad HID event is 15 bytes long:
* A gamepad HID input report is 15 bytes long:
* - bytes 0-3: left stick state
* - bytes 4-7: right stick state
* - bytes 8-11: L2/R2 triggers state
@@ -142,28 +134,28 @@ static const uint8_t SC_HID_GAMEPAD_REPORT_DESC[] = {
*
* +---------------+
* byte 0: |. . . . . . . .|
* | | left stick x state (0-65535)
* | | left stick x (0-65535, little-endian)
* byte 1: |. . . . . . . .|
* +---------------+
* byte 2: |. . . . . . . .|
* | | left stick y state (0-65535)
* | | left stick y (0-65535, little-endian)
* byte 3: |. . . . . . . .|
* +---------------+
* byte 4: |. . . . . . . .|
* | | right stick x state (0-65535)
* | | right stick x (0-65535, little-endian)
* byte 5: |. . . . . . . .|
* +---------------+
* byte 6: |. . . . . . . .|
* | | right stick y state (0-65535)
* | | right stick y (0-65535, little-endian)
* byte 7: |. . . . . . . .|
* +---------------+
* byte 8: |. . . . . . . .|
* | | L2 trigger state (0-65535)
* byte 9: |. . . . . . . .|
* | | L2 trigger (0-32767, little-endian)
* byte 9: |0 . . . . . . .|
* +---------------+
* byte 10: |. . . . . . . .|
* | | R2 trigger state (0-65535)
* byte 11: |. . . . . . . .|
* | | R2 trigger (0-32767, little-endian)
* byte 11: |0 . . . . . . .|
* +---------------+
*
* ,--------------- SC_GAMEPAD_BUTTON_RIGHT_SHOULDER
@@ -251,8 +243,14 @@ sc_hid_gamepad_generate_open(struct sc_hid_gamepad *hid,
sc_hid_gamepad_slot_init(&hid->slots[slot_idx], gamepad_id);
SDL_GameController* game_controller =
SDL_GameControllerFromInstanceID(gamepad_id);
assert(game_controller);
const char *name = SDL_GameControllerName(game_controller);
uint16_t hid_id = sc_hid_gamepad_slot_get_id(slot_idx);
hid_open->hid_id = hid_id;
hid_open->name = name;
hid_open->report_desc = SC_HID_GAMEPAD_REPORT_DESC;
hid_open->report_desc_size = sizeof(SC_HID_GAMEPAD_REPORT_DESC);
@@ -320,14 +318,13 @@ sc_hid_gamepad_event_from_slot(uint16_t hid_id,
hid_input->size = SC_HID_GAMEPAD_EVENT_SIZE;
uint8_t *data = hid_input->data;
// Buttons are individual bits in the report descriptor, so the resulting
// u16 must be written in little-endian
sc_write16be(data, slot->axis_left_x);
sc_write16be(data + 2, slot->axis_left_y);
sc_write16be(data + 4, slot->axis_right_x);
sc_write16be(data + 6, slot->axis_right_y);
sc_write16be(data + 8, slot->axis_left_trigger);
sc_write16be(data + 10, slot->axis_right_trigger);
// Values must be written in little-endian
sc_write16le(data, slot->axis_left_x);
sc_write16le(data + 2, slot->axis_left_y);
sc_write16le(data + 4, slot->axis_right_x);
sc_write16le(data + 6, slot->axis_right_y);
sc_write16le(data + 8, slot->axis_left_trigger);
sc_write16le(data + 10, slot->axis_right_trigger);
sc_write16le(data + 12, slot->buttons & SC_HID_BUTTONS_MASK);
data[14] = sc_hid_gamepad_get_dpad_value(slot->buttons);
}
@@ -406,6 +403,7 @@ sc_hid_gamepad_generate_input_from_button(struct sc_hid_gamepad *hid,
uint16_t hid_id = sc_hid_gamepad_slot_get_id(slot_idx);
sc_hid_gamepad_event_from_slot(hid_id, slot, hid_input);
return true;
}
@@ -425,26 +423,28 @@ sc_hid_gamepad_generate_input_from_axis(struct sc_hid_gamepad *hid,
struct sc_hid_gamepad_slot *slot = &hid->slots[slot_idx];
// [-32768 to 32767] -> [0 to 65535]
uint16_t value = ((int32_t) event->value) + 0x8000;
// [-32768 to 32767] -> [0 to 65535]
#define AXIS_RESCALE(V) (uint16_t) (((int32_t) V) + 0x8000)
switch (event->axis) {
case SC_GAMEPAD_AXIS_LEFTX:
slot->axis_left_x = value;
slot->axis_left_x = AXIS_RESCALE(event->value);
break;
case SC_GAMEPAD_AXIS_LEFTY:
slot->axis_left_y = value;
slot->axis_left_y = AXIS_RESCALE(event->value);
break;
case SC_GAMEPAD_AXIS_RIGHTX:
slot->axis_right_x = value;
slot->axis_right_x = AXIS_RESCALE(event->value);
break;
case SC_GAMEPAD_AXIS_RIGHTY:
slot->axis_right_y = value;
slot->axis_right_y = AXIS_RESCALE(event->value);
break;
case SC_GAMEPAD_AXIS_LEFT_TRIGGER:
slot->axis_left_trigger = value;
// Trigger is always positive between 0 and 32767
slot->axis_left_trigger = MAX(0, event->value);
break;
case SC_GAMEPAD_AXIS_RIGHT_TRIGGER:
slot->axis_right_trigger = value;
// Trigger is always positive between 0 and 32767
slot->axis_right_trigger = MAX(0, event->value);
break;
default:
return false;
@@ -452,5 +452,6 @@ sc_hid_gamepad_generate_input_from_axis(struct sc_hid_gamepad *hid,
uint16_t hid_id = sc_hid_gamepad_slot_get_id(slot_idx);
sc_hid_gamepad_event_from_slot(hid_id, slot, hid_input);
return true;
}

View File

@@ -9,7 +9,7 @@
#include "input_events.h"
#define SC_MAX_GAMEPADS 8
#define SC_HID_ID_GAMEPAD_FIRST 2
#define SC_HID_ID_GAMEPAD_FIRST 3
#define SC_HID_ID_GAMEPAD_LAST (SC_HID_ID_GAMEPAD_FIRST + SC_MAX_GAMEPADS - 1)
struct sc_hid_gamepad_slot {

View File

@@ -125,7 +125,7 @@ static const uint8_t SC_HID_KEYBOARD_REPORT_DESC[] = {
};
/**
* A keyboard HID event is 8 bytes long:
* A keyboard HID input report is 8 bytes long:
*
* - byte 0: modifiers (1 flag per modifier key, 8 possible modifier keys)
* - byte 1: reserved (always 0)
@@ -335,6 +335,7 @@ sc_hid_keyboard_generate_input_from_mods(struct sc_hid_input *hid_input,
void sc_hid_keyboard_generate_open(struct sc_hid_open *hid_open) {
hid_open->hid_id = SC_HID_ID_KEYBOARD;
hid_open->name = NULL; // No name specified after "scrcpy"
hid_open->report_desc = SC_HID_KEYBOARD_REPORT_DESC;
hid_open->report_desc_size = sizeof(SC_HID_KEYBOARD_REPORT_DESC);
}

View File

@@ -14,7 +14,7 @@
* <https://www.usb.org/document-library/hid-usage-tables-15>
* §4 Generic Desktop Page (0x01) (p32)
*/
const uint8_t SC_HID_MOUSE_REPORT_DESC[] = {
static const uint8_t SC_HID_MOUSE_REPORT_DESC[] = {
// Usage Page (Generic Desktop)
0x05, 0x01,
// Usage (Mouse)
@@ -81,7 +81,7 @@ const uint8_t SC_HID_MOUSE_REPORT_DESC[] = {
};
/**
* A mouse HID event is 4 bytes long:
* A mouse HID input report is 4 bytes long:
*
* - byte 0: buttons state
* - byte 1: relative x motion (signed byte from -127 to 127)
@@ -190,6 +190,7 @@ sc_hid_mouse_generate_input_from_scroll(struct sc_hid_input *hid_input,
void sc_hid_mouse_generate_open(struct sc_hid_open *hid_open) {
hid_open->hid_id = SC_HID_ID_MOUSE;
hid_open->name = NULL; // No name specified after "scrcpy"
hid_open->report_desc = SC_HID_MOUSE_REPORT_DESC;
hid_open->report_desc_size = sizeof(SC_HID_MOUSE_REPORT_DESC);
}

View File

@@ -516,7 +516,7 @@ sc_gamepad_device_event_type_from_sdl_type(uint32_t type) {
static inline enum sc_gamepad_axis
sc_gamepad_axis_from_sdl(uint8_t axis) {
if (axis <= SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
// SDL_GAMEPAD_AXIS_* constants are initialized from
// SC_GAMEPAD_AXIS_* constants are initialized from
// SDL_CONTROLLER_AXIS_*
return axis;
}

View File

@@ -402,7 +402,7 @@ sc_input_manager_process_key(struct sc_input_manager *im,
bool paused = im->screen->paused;
bool video = im->screen->video;
SDL_Keycode keycode = event->keysym.sym;
SDL_Keycode sdl_keycode = event->keysym.sym;
uint16_t mod = event->keysym.mod;
bool down = event->type == SDL_KEYDOWN;
bool ctrl = event->keysym.mod & KMOD_CTRL;
@@ -414,21 +414,21 @@ sc_input_manager_process_key(struct sc_input_manager *im,
// The second condition is necessary to ignore the release of the modifier
// key (because in this case mod is 0).
bool is_shortcut = is_shortcut_mod(im, mod)
|| is_shortcut_key(im, keycode);
|| is_shortcut_key(im, sdl_keycode);
if (down && !repeat) {
if (keycode == im->last_keycode && mod == im->last_mod) {
if (sdl_keycode == im->last_keycode && mod == im->last_mod) {
++im->key_repeat;
} else {
im->key_repeat = 0;
im->last_keycode = keycode;
im->last_keycode = sdl_keycode;
im->last_mod = mod;
}
}
if (is_shortcut) {
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
switch (keycode) {
switch (sdl_keycode) {
case SDLK_h:
if (im->kp && !shift && !repeat && !paused) {
action_home(im, action);
@@ -587,7 +587,7 @@ sc_input_manager_process_key(struct sc_input_manager *im,
}
uint64_t ack_to_wait = SC_SEQUENCE_INVALID;
bool is_ctrl_v = ctrl && !shift && keycode == SDLK_v && down && !repeat;
bool is_ctrl_v = ctrl && !shift && sdl_keycode == SDLK_v && down && !repeat;
if (im->clipboard_autosync && is_ctrl_v) {
if (im->legacy_paste) {
// inject the text as input events
@@ -615,10 +615,20 @@ sc_input_manager_process_key(struct sc_input_manager *im,
}
}
enum sc_keycode keycode = sc_keycode_from_sdl(sdl_keycode);
if (keycode == SC_KEYCODE_UNKNOWN) {
return;
}
enum sc_scancode scancode = sc_scancode_from_sdl(event->keysym.scancode);
if (scancode == SC_SCANCODE_UNKNOWN) {
return;
}
struct sc_key_event evt = {
.action = sc_action_from_sdl_keyboard_type(event->type),
.keycode = sc_keycode_from_sdl(event->keysym.sym),
.scancode = sc_scancode_from_sdl(event->keysym.scancode),
.keycode = keycode,
.scancode = scancode,
.repeat = event->repeat,
.mods_state = sc_mods_state_from_sdl(event->keysym.mod),
};
@@ -741,6 +751,10 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
bool down = event->type == SDL_MOUSEBUTTONDOWN;
enum sc_mouse_button button = sc_mouse_button_from_sdl(event->button);
if (button == SC_MOUSE_BUTTON_UNKNOWN) {
return;
}
if (!down) {
// Mark the button as released
im->mouse_buttons_state &= ~button;
@@ -829,7 +843,7 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
struct sc_mouse_click_event evt = {
.position = sc_input_manager_get_position(im, event->x, event->y),
.action = sc_action_from_sdl_mousebutton_type(event->type),
.button = sc_mouse_button_from_sdl(event->button),
.button = button,
.pointer_id = use_finger ? SC_POINTER_ID_GENERIC_FINGER
: SC_POINTER_ID_MOUSE,
.buttons_state = im->mouse_buttons_state,
@@ -950,10 +964,15 @@ sc_input_manager_process_gamepad_device(struct sc_input_manager *im,
static void
sc_input_manager_process_gamepad_axis(struct sc_input_manager *im,
const SDL_ControllerAxisEvent *event) {
const SDL_ControllerAxisEvent *event) {
enum sc_gamepad_axis axis = sc_gamepad_axis_from_sdl(event->axis);
if (axis == SC_GAMEPAD_AXIS_UNKNOWN) {
return;
}
struct sc_gamepad_axis_event evt = {
.gamepad_id = event->which,
.axis = sc_gamepad_axis_from_sdl(event->axis),
.axis = axis,
.value = event->value,
};
im->gp->ops->process_gamepad_axis(im->gp, &evt);
@@ -962,10 +981,15 @@ sc_input_manager_process_gamepad_axis(struct sc_input_manager *im,
static void
sc_input_manager_process_gamepad_button(struct sc_input_manager *im,
const SDL_ControllerButtonEvent *event) {
enum sc_gamepad_button button = sc_gamepad_button_from_sdl(event->button);
if (button == SC_GAMEPAD_BUTTON_UNKNOWN) {
return;
}
struct sc_gamepad_button_event evt = {
.gamepad_id = event->which,
.action = sc_action_from_sdl_controllerbutton_type(event->type),
.button = sc_gamepad_button_from_sdl(event->button),
.button = button,
};
im->gp->ops->process_gamepad_button(im->gp, &evt);
}

View File

@@ -23,7 +23,7 @@ const struct scrcpy_options scrcpy_options_default = {
.record_format = SC_RECORD_FORMAT_AUTO,
.keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_AUTO,
.mouse_input_mode = SC_MOUSE_INPUT_MODE_AUTO,
.gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_AUTO,
.gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_DISABLED,
.mouse_bindings = {
.pri = {
.right_click = SC_MOUSE_BINDING_AUTO,
@@ -49,7 +49,7 @@ const struct scrcpy_options scrcpy_options_default = {
.max_size = 0,
.video_bit_rate = 0,
.audio_bit_rate = 0,
.max_fps = 0,
.max_fps = NULL,
.lock_video_orientation = SC_LOCK_VIDEO_ORIENTATION_UNLOCKED,
.display_orientation = SC_ORIENTATION_0,
.record_orientation = SC_ORIENTATION_0,

View File

@@ -142,6 +142,7 @@ enum sc_lock_video_orientation {
enum sc_keyboard_input_mode {
SC_KEYBOARD_INPUT_MODE_AUTO,
SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_KEYBOARD_INPUT_MODE_DISABLED,
SC_KEYBOARD_INPUT_MODE_SDK,
SC_KEYBOARD_INPUT_MODE_UHID,
@@ -150,6 +151,7 @@ enum sc_keyboard_input_mode {
enum sc_mouse_input_mode {
SC_MOUSE_INPUT_MODE_AUTO,
SC_MOUSE_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_MOUSE_INPUT_MODE_DISABLED,
SC_MOUSE_INPUT_MODE_SDK,
SC_MOUSE_INPUT_MODE_UHID,
@@ -157,8 +159,8 @@ enum sc_mouse_input_mode {
};
enum sc_gamepad_input_mode {
SC_GAMEPAD_INPUT_MODE_AUTO,
SC_GAMEPAD_INPUT_MODE_DISABLED,
SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_GAMEPAD_INPUT_MODE_UHID,
SC_GAMEPAD_INPUT_MODE_AOA,
};
@@ -248,7 +250,7 @@ struct scrcpy_options {
uint16_t max_size;
uint32_t video_bit_rate;
uint32_t audio_bit_rate;
uint16_t max_fps;
const char *max_fps; // float to be parsed by the server
enum sc_lock_video_orientation lock_video_orientation;
enum sc_orientation display_orientation;
enum sc_orientation record_orientation;

View File

@@ -9,6 +9,7 @@
#include "events.h"
#include "util/log.h"
#include "util/str.h"
#include "util/thread.h"
struct sc_uhid_output_task_data {
struct sc_uhid_devices *uhid_devices;
@@ -43,6 +44,8 @@ sc_receiver_destroy(struct sc_receiver *receiver) {
static void
task_set_clipboard(void *userdata) {
assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
char *text = userdata;
char *current = SDL_GetClipboardText();
@@ -50,17 +53,18 @@ task_set_clipboard(void *userdata) {
SDL_free(current);
if (same) {
LOGD("Computer clipboard unchanged");
free(text);
return;
} else {
LOGI("Device clipboard copied");
SDL_SetClipboardText(text);
}
LOGI("Device clipboard copied");
SDL_SetClipboardText(text);
free(text);
}
static void
task_uhid_output(void *userdata) {
assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
struct sc_uhid_output_task_data *data = userdata;
sc_uhid_devices_process_hid_output(data->uhid_devices, data->id, data->data,
@@ -117,11 +121,6 @@ process_msg(struct sc_receiver *receiver, struct sc_device_msg *msg) {
}
}
// This is a programming error to receive this message if there is
// no uhid_devices instance
assert(receiver->uhid_devices);
// Also check at runtime (do not trust the server)
if (!receiver->uhid_devices) {
LOGE("Received unexpected HID output message");
sc_device_msg_destroy(msg);

View File

@@ -65,8 +65,8 @@ struct scrcpy {
struct sc_aoa aoa;
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
struct sc_acksync acksync;
struct sc_uhid_devices uhid_devices;
#endif
struct sc_uhid_devices uhid_devices;
union {
struct sc_keyboard_sdk keyboard_sdk;
struct sc_keyboard_uhid keyboard_uhid;
@@ -136,6 +136,10 @@ sdl_set_hints(const char *render_driver) {
if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
LOGW("Could not disable minimize on focus loss");
}
if (!SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1")) {
LOGW("Could not allow joystick background events");
}
}
static void
@@ -185,11 +189,12 @@ event_loop(struct scrcpy *s) {
case SDL_QUIT:
LOGD("User requested to quit");
return SCRCPY_EXIT_SUCCESS;
case SC_EVENT_RUN_ON_MAIN_THREAD:
case SC_EVENT_RUN_ON_MAIN_THREAD: {
sc_runnable_fn run = event.user.data1;
void *userdata = event.user.data2;
run(userdata);
break;
}
default:
if (!sc_screen_handle_event(&s->screen, &event)) {
return SCRCPY_EXIT_FAILURE;
@@ -200,6 +205,21 @@ event_loop(struct scrcpy *s) {
return SCRCPY_EXIT_FAILURE;
}
static void
terminate_event_loop(void) {
sc_reject_new_runnables();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SC_EVENT_RUN_ON_MAIN_THREAD) {
// Make sure all posted runnables are run, to avoid memory leaks
sc_runnable_fn run = event.user.data1;
void *userdata = event.user.data2;
run(userdata);
}
}
}
// Return true on success, false on error
static bool
await_for_server(bool *connected) {
@@ -709,7 +729,6 @@ aoa_complete:
#endif
struct sc_keyboard_uhid *uhid_keyboard = NULL;
struct sc_gamepad_uhid *uhid_gamepad = NULL;
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_SDK) {
sc_keyboard_sdk_init(&s->keyboard_sdk, &s->controller,
@@ -744,8 +763,8 @@ aoa_complete:
}
struct sc_uhid_devices *uhid_devices = NULL;
if (uhid_keyboard || uhid_gamepad) {
sc_uhid_devices_init(&s->uhid_devices, uhid_keyboard, uhid_gamepad);
if (uhid_keyboard) {
sc_uhid_devices_init(&s->uhid_devices, uhid_keyboard);
uhid_devices = &s->uhid_devices;
}
@@ -882,12 +901,13 @@ aoa_complete:
timeout_started = true;
}
bool use_gamepads = true;
if (use_gamepads) {
if (options->control
&& options->gamepad_input_mode != SC_GAMEPAD_INPUT_MODE_DISABLED) {
init_sdl_gamepads();
}
ret = event_loop(s);
terminate_event_loop();
LOGD("quit...");
if (options->video_playback) {

View File

@@ -299,6 +299,12 @@ sc_screen_frame_sink_open(struct sc_frame_sink *sink,
struct sc_screen *screen = DOWNCAST(sink);
if (ctx->width <= 0 || ctx->width > 0xFFFF
|| ctx->height <= 0 || ctx->height > 0xFFFF) {
LOGE("Invalid video size: %dx%d", ctx->width, ctx->height);
return false;
}
assert(ctx->width > 0 && ctx->width <= 0xFFFF);
assert(ctx->height > 0 && ctx->height <= 0xFFFF);
// screen->frame_size is never used before the event is pushed, and the
@@ -309,7 +315,6 @@ sc_screen_frame_sink_open(struct sc_frame_sink *sink,
// Post the event on the UI thread (the texture must be created from there)
bool ok = sc_push_event(SC_EVENT_SCREEN_INIT_SIZE);
if (!ok) {
LOGW("Could not post init size event: %s", SDL_GetError());
return false;
}
@@ -351,7 +356,6 @@ sc_screen_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
// Post the event on the UI thread
bool ok = sc_push_event(SC_EVENT_NEW_FRAME);
if (!ok) {
LOGW("Could not post new frame event: %s", SDL_GetError());
return false;
}
}

View File

@@ -218,6 +218,21 @@ sc_server_get_audio_source_name(enum sc_audio_source audio_source) {
}
}
static bool
validate_string(const char *s) {
// The parameters values are passed as command line arguments to adb, so
// they must either be properly escaped, or they must not contain any
// special shell characters.
// Since they are not properly escaped on Windows anyway (see
// sys/win/process.c), just forbid special shell characters.
if (strpbrk(s, " ;'\"*$?&`#\\|<>[]{}()!~\r\n")) {
LOGE("Invalid server param: [%s]", s);
return false;
}
return true;
}
static sc_pid
execute_server(struct sc_server *server,
const struct sc_server_params *params) {
@@ -260,6 +275,11 @@ execute_server(struct sc_server *server,
} \
cmd[count++] = p; \
} while(0)
#define VALIDATE_STRING(s) do { \
if (!validate_string(s)) { \
goto end; \
} \
} while(0)
ADD_PARAM("scid=%08x", params->scid);
ADD_PARAM("log_level=%s", log_level_to_server_string(params->log_level));
@@ -301,7 +321,8 @@ execute_server(struct sc_server *server,
ADD_PARAM("max_size=%" PRIu16, params->max_size);
}
if (params->max_fps) {
ADD_PARAM("max_fps=%" PRIu16, params->max_fps);
VALIDATE_STRING(params->max_fps);
ADD_PARAM("max_fps=%s", params->max_fps);
}
if (params->lock_video_orientation != SC_LOCK_VIDEO_ORIENTATION_UNLOCKED) {
ADD_PARAM("lock_video_orientation=%" PRIi8,
@@ -311,6 +332,7 @@ execute_server(struct sc_server *server,
ADD_PARAM("tunnel_forward=true");
}
if (params->crop) {
VALIDATE_STRING(params->crop);
ADD_PARAM("crop=%s", params->crop);
}
if (!params->control) {
@@ -321,9 +343,11 @@ execute_server(struct sc_server *server,
ADD_PARAM("display_id=%" PRIu32, params->display_id);
}
if (params->camera_id) {
VALIDATE_STRING(params->camera_id);
ADD_PARAM("camera_id=%s", params->camera_id);
}
if (params->camera_size) {
VALIDATE_STRING(params->camera_size);
ADD_PARAM("camera_size=%s", params->camera_size);
}
if (params->camera_facing != SC_CAMERA_FACING_ANY) {
@@ -331,6 +355,7 @@ execute_server(struct sc_server *server,
sc_server_get_camera_facing_name(params->camera_facing));
}
if (params->camera_ar) {
VALIDATE_STRING(params->camera_ar);
ADD_PARAM("camera_ar=%s", params->camera_ar);
}
if (params->camera_fps) {
@@ -346,15 +371,19 @@ execute_server(struct sc_server *server,
ADD_PARAM("stay_awake=true");
}
if (params->video_codec_options) {
VALIDATE_STRING(params->video_codec_options);
ADD_PARAM("video_codec_options=%s", params->video_codec_options);
}
if (params->audio_codec_options) {
VALIDATE_STRING(params->audio_codec_options);
ADD_PARAM("audio_codec_options=%s", params->audio_codec_options);
}
if (params->video_encoder) {
VALIDATE_STRING(params->video_encoder);
ADD_PARAM("video_encoder=%s", params->video_encoder);
}
if (params->audio_encoder) {
VALIDATE_STRING(params->audio_encoder);
ADD_PARAM("audio_encoder=%s", params->audio_encoder);
}
if (params->power_off_on_close) {
@@ -630,6 +659,14 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
}
}
if (control_socket != SC_SOCKET_NONE) {
// Disable Nagle's algorithm for the control socket
// (it only impacts the sending side, so it is useless to set it
// for the other sockets)
bool ok = net_set_tcp_nodelay(control_socket, true);
(void) ok; // error already logged
}
// we don't need the adb tunnel anymore
sc_adb_tunnel_close(tunnel, &server->intr, serial,
server->device_socket_name);

View File

@@ -44,7 +44,7 @@ struct sc_server_params {
uint16_t max_size;
uint32_t video_bit_rate;
uint32_t audio_bit_rate;
uint16_t max_fps;
const char *max_fps; // float to be parsed by the server
int8_t lock_video_orientation;
bool control;
uint32_t display_id;

View File

@@ -3,7 +3,6 @@
#include "hid/hid_gamepad.h"
#include "input_events.h"
#include "util/log.h"
#include "util/str.h"
/** Downcast gamepad processor to sc_gamepad_uhid */
#define DOWNCAST(GP) container_of(GP, struct sc_gamepad_uhid, gamepad_processor)
@@ -31,6 +30,7 @@ sc_gamepad_uhid_send_open(struct sc_gamepad_uhid *gamepad,
struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE;
msg.uhid_create.id = hid_open->hid_id;
msg.uhid_create.name = hid_open->name;
msg.uhid_create.report_desc = hid_open->report_desc;
msg.uhid_create.report_desc_size = hid_open->report_desc_size;
@@ -83,7 +83,10 @@ sc_gamepad_processor_process_gamepad_axis(struct sc_gamepad_processor *gp,
struct sc_gamepad_uhid *gamepad = DOWNCAST(gp);
struct sc_hid_input hid_input;
sc_hid_gamepad_generate_input_from_axis(&gamepad->hid, &hid_input, event);
if (!sc_hid_gamepad_generate_input_from_axis(&gamepad->hid, &hid_input,
event)) {
return;
}
sc_gamepad_uhid_send_input(gamepad, &hid_input, "gamepad axis");
}
@@ -94,28 +97,15 @@ sc_gamepad_processor_process_gamepad_button(struct sc_gamepad_processor *gp,
struct sc_gamepad_uhid *gamepad = DOWNCAST(gp);
struct sc_hid_input hid_input;
sc_hid_gamepad_generate_input_from_button(&gamepad->hid, &hid_input, event);
if (!sc_hid_gamepad_generate_input_from_button(&gamepad->hid, &hid_input,
event)) {
return;
}
sc_gamepad_uhid_send_input(gamepad, &hid_input, "gamepad button");
}
void
sc_gamepad_uhid_process_hid_output(struct sc_gamepad_uhid *gamepad,
uint16_t hid_id, const uint8_t *data,
size_t size) {
(void) gamepad;
char *hex = sc_str_to_hex_string(data, size);
if (hex) {
LOGI("==== HID output [%" PRIu16 "] %s", hid_id, hex);
free(hex);
} else {
LOGI("==== HID output [%" PRIu16 "]", hid_id);
}
// TODO
}
void
sc_gamepad_uhid_init(struct sc_gamepad_uhid *gamepad,
struct sc_controller *controller) {

View File

@@ -20,9 +20,4 @@ void
sc_gamepad_uhid_init(struct sc_gamepad_uhid *mouse,
struct sc_controller *controller);
void
sc_gamepad_uhid_process_hid_output(struct sc_gamepad_uhid *gamepad,
uint16_t hid_id, const uint8_t *data,
size_t size);
#endif

View File

@@ -38,7 +38,9 @@ sc_keyboard_uhid_synchronize_mod(struct sc_keyboard_uhid *kb) {
kb->device_mod = mod;
struct sc_hid_input hid_input;
sc_hid_keyboard_generate_input_from_mods(&hid_input, diff);
if (!sc_hid_keyboard_generate_input_from_mods(&hid_input, diff)) {
return;
}
LOGV("HID keyboard state synchronized");
@@ -139,6 +141,7 @@ sc_keyboard_uhid_init(struct sc_keyboard_uhid *kb,
struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE;
msg.uhid_create.id = SC_HID_ID_KEYBOARD;
msg.uhid_create.name = hid_open.name;
msg.uhid_create.report_desc = hid_open.report_desc;
msg.uhid_create.report_desc_size = hid_open.report_desc_size;
if (!sc_controller_push_msg(controller, &msg)) {

View File

@@ -81,6 +81,7 @@ sc_mouse_uhid_init(struct sc_mouse_uhid *mouse,
struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE;
msg.uhid_create.id = SC_HID_ID_MOUSE;
msg.uhid_create.name = hid_open.name;
msg.uhid_create.report_desc = hid_open.report_desc;
msg.uhid_create.report_desc_size = hid_open.report_desc_size;
if (!sc_controller_push_msg(controller, &msg)) {

View File

@@ -4,15 +4,12 @@
#include <inttypes.h>
#include "uhid/keyboard_uhid.h"
#include "uhid/gamepad_uhid.h"
#include "util/log.h"
void
sc_uhid_devices_init(struct sc_uhid_devices *devices,
struct sc_keyboard_uhid *keyboard,
struct sc_gamepad_uhid *gamepad) {
struct sc_keyboard_uhid *keyboard) {
devices->keyboard = keyboard;
devices->gamepad = gamepad;
}
void
@@ -24,13 +21,6 @@ sc_uhid_devices_process_hid_output(struct sc_uhid_devices *devices, uint16_t id,
} else {
LOGW("Unexpected keyboard HID output without UHID keyboard");
}
} else if (id >= SC_HID_ID_GAMEPAD_FIRST && id <= SC_HID_ID_GAMEPAD_LAST) {
if (devices->gamepad) {
sc_gamepad_uhid_process_hid_output(devices->gamepad, id, data,
size);
} else {
LOGW("Unexpected gamepad HID output without UHID gamepad");
}
} else {
LOGW("HID output ignored for id %" PRIu16, id);
}

View File

@@ -14,13 +14,11 @@
struct sc_uhid_devices {
struct sc_keyboard_uhid *keyboard;
struct sc_gamepad_uhid *gamepad;
};
void
sc_uhid_devices_init(struct sc_uhid_devices *devices,
struct sc_keyboard_uhid *keyboard,
struct sc_gamepad_uhid *gamepad);
struct sc_keyboard_uhid *keyboard);
void
sc_uhid_devices_process_hid_output(struct sc_uhid_devices *devices, uint16_t id,

View File

@@ -51,7 +51,7 @@ sc_hid_open_log(const struct sc_hid_open *hid_open) {
static void
sc_hid_close_log(const struct sc_hid_close *hid_close) {
// HID close: [00]
LOGV("HD close: [%" PRIu16 "]", hid_close->hid_id);
LOGV("HID close: [%" PRIu16 "]", hid_close->hid_id);
}
bool
@@ -227,8 +227,11 @@ sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,
}
sc_mutex_lock(&aoa->mutex);
bool full = sc_vecdeque_is_full(&aoa->queue);
if (!full) {
bool pushed = false;
size_t size = sc_vecdeque_size(&aoa->queue);
if (size < SC_AOA_EVENT_QUEUE_LIMIT) {
bool was_empty = sc_vecdeque_is_empty(&aoa->queue);
struct sc_aoa_event *aoa_event =
@@ -236,16 +239,17 @@ sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,
aoa_event->type = SC_AOA_EVENT_TYPE_INPUT;
aoa_event->input.hid = *hid_input;
aoa_event->input.ack_to_wait = ack_to_wait;
pushed = true;
if (was_empty) {
sc_cond_signal(&aoa->event_cond);
}
}
// Otherwise (if the queue is full), the event is discarded
// Otherwise, the event is discarded
sc_mutex_unlock(&aoa->mutex);
return !full;
return pushed;
}
bool
@@ -289,7 +293,7 @@ sc_aoa_push_close(struct sc_aoa *aoa, const struct sc_hid_close *hid_close) {
sc_mutex_lock(&aoa->mutex);
bool was_empty = sc_vecdeque_is_empty(&aoa->queue);
// an OPEN event is non-droppable, so push it to the queue even above the
// a CLOSE event is non-droppable, so push it to the queue even above the
// SC_AOA_EVENT_QUEUE_LIMIT
struct sc_aoa_event *aoa_event = sc_vecdeque_push_hole(&aoa->queue);
if (!aoa_event) {

View File

@@ -22,10 +22,7 @@ sc_usb_on_disconnected(struct sc_usb *usb, void *userdata) {
(void) usb;
(void) userdata;
bool ok = sc_push_event(SC_EVENT_USB_DEVICE_DISCONNECTED);
if (!ok) {
LOGE("Could not post USB disconnection event: %s", SDL_GetError());
}
sc_push_event(SC_EVENT_USB_DEVICE_DISCONNECTED);
}
static enum scrcpy_exit_code
@@ -42,11 +39,6 @@ event_loop(struct scrcpy_otg *s) {
case SDL_QUIT:
LOGD("User requested to quit");
return SCRCPY_EXIT_SUCCESS;
case SC_EVENT_RUN_ON_MAIN_THREAD:
sc_runnable_fn run = event.user.data1;
void *userdata = event.user.data2;
run(userdata);
break;
default:
sc_screen_otg_handle_event(&s->screen_otg, &event);
break;
@@ -66,6 +58,10 @@ scrcpy_otg(struct scrcpy_options *options) {
LOGW("Could not enable linear filtering");
}
if (!SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1")) {
LOGW("Could not allow joystick background events");
}
// Minimal SDL initialization
if (SDL_Init(SDL_INIT_EVENTS)) {
LOGE("Could not initialize SDL: %s", SDL_GetError());

View File

@@ -264,9 +264,14 @@ sc_screen_otg_process_gamepad_axis(struct sc_screen_otg *screen,
assert(screen->gamepad);
struct sc_gamepad_processor *gp = &screen->gamepad->gamepad_processor;
enum sc_gamepad_axis axis = sc_gamepad_axis_from_sdl(event->axis);
if (axis == SC_GAMEPAD_AXIS_UNKNOWN) {
return;
}
struct sc_gamepad_axis_event evt = {
.gamepad_id = event->which,
.axis = sc_gamepad_axis_from_sdl(event->axis),
.axis = axis,
.value = event->value,
};
gp->ops->process_gamepad_axis(gp, &evt);
@@ -278,10 +283,15 @@ sc_screen_otg_process_gamepad_button(struct sc_screen_otg *screen,
assert(screen->gamepad);
struct sc_gamepad_processor *gp = &screen->gamepad->gamepad_processor;
enum sc_gamepad_button button = sc_gamepad_button_from_sdl(event->button);
if (button == SC_GAMEPAD_BUTTON_UNKNOWN) {
return;
}
struct sc_gamepad_button_event evt = {
.gamepad_id = event->which,
.action = sc_action_from_sdl_controllerbutton_type(event->type),
.button = sc_gamepad_button_from_sdl(event->button),
.button = button,
};
gp->ops->process_gamepad_button(gp, &evt);
}

View File

@@ -44,7 +44,7 @@ sc_write64be(uint8_t *buf, uint64_t value) {
static inline void
sc_write64le(uint8_t *buf, uint64_t value) {
sc_write32le(buf, (uint32_t) value);
sc_write32le(buf, value >> 32);
sc_write32le(&buf[4], value >> 32);
}
static inline uint16_t

View File

@@ -15,6 +15,7 @@
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <unistd.h>
# include <fcntl.h>
@@ -273,6 +274,22 @@ net_close(sc_socket socket) {
#endif
}
bool
net_set_tcp_nodelay(sc_socket socket, bool tcp_nodelay) {
sc_raw_socket raw_sock = unwrap(socket);
int value = tcp_nodelay ? 1 : 0;
int ret = setsockopt(raw_sock, IPPROTO_TCP, TCP_NODELAY,
(const void *) &value, sizeof(value));
if (ret == -1) {
net_perror("setsockopt(TCP_NODELAY)");
return false;
}
assert(ret == 0);
return true;
}
bool
net_parse_ipv4(const char *s, uint32_t *ipv4) {
struct in_addr addr;

View File

@@ -67,6 +67,10 @@ net_interrupt(sc_socket socket);
bool
net_close(sc_socket socket);
// Disable Nagle's algorithm (if tcp_nodelay is true)
bool
net_set_tcp_nodelay(sc_socket socket, bool tcp_nodelay);
/**
* Parse `ip` "xxx.xxx.xxx.xxx" to an IPv4 host representation
*/

View File

@@ -42,6 +42,44 @@ static void test_write64be(void) {
assert(buf[7] == 0xEF);
}
static void test_write16le(void) {
uint16_t val = 0xABCD;
uint8_t buf[2];
sc_write16le(buf, val);
assert(buf[0] == 0xCD);
assert(buf[1] == 0xAB);
}
static void test_write32le(void) {
uint32_t val = 0xABCD1234;
uint8_t buf[4];
sc_write32le(buf, val);
assert(buf[0] == 0x34);
assert(buf[1] == 0x12);
assert(buf[2] == 0xCD);
assert(buf[3] == 0xAB);
}
static void test_write64le(void) {
uint64_t val = 0xABCD1234567890EF;
uint8_t buf[8];
sc_write64le(buf, val);
assert(buf[0] == 0xEF);
assert(buf[1] == 0x90);
assert(buf[2] == 0x78);
assert(buf[3] == 0x56);
assert(buf[4] == 0x34);
assert(buf[5] == 0x12);
assert(buf[6] == 0xCD);
assert(buf[7] == 0xAB);
}
static void test_read16be(void) {
uint8_t buf[2] = {0xAB, 0xCD};
@@ -108,6 +146,10 @@ int main(int argc, char *argv[]) {
test_read32be();
test_read64be();
test_write16le();
test_write32le();
test_write64le();
test_float_to_u16fp();
test_float_to_i16fp();
return 0;

View File

@@ -78,7 +78,7 @@ static void test_options(void) {
assert(opts->video_bit_rate == 5000000);
assert(!strcmp(opts->crop, "100:200:300:400"));
assert(opts->fullscreen);
assert(opts->max_fps == 30);
assert(!strcmp(opts->max_fps, "30"));
assert(opts->max_size == 1024);
assert(opts->lock_video_orientation == 2);
assert(opts->port_range.first == 1234);

View File

@@ -329,6 +329,7 @@ static void test_serialize_uhid_create(void) {
.type = SC_CONTROL_MSG_TYPE_UHID_CREATE,
.uhid_create = {
.id = 42,
.name = "ABC",
.report_desc_size = sizeof(report_desc),
.report_desc = report_desc,
},
@@ -336,12 +337,14 @@ static void test_serialize_uhid_create(void) {
uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 16);
assert(size == 20);
const uint8_t expected[] = {
SC_CONTROL_MSG_TYPE_UHID_CREATE,
0, 42, // id
0, 11, // size
3, // name size
65, 66, 67, // "ABC"
0, 11, // report desc size
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
assert(!memcmp(buf, expected, sizeof(expected)));

58
doc/gamepad.md Normal file
View File

@@ -0,0 +1,58 @@
# Gamepad
Several gamepad input modes are available:
- `--gamepad=disabled` (default)
- `--gamepad=uhid` (or `-G`): simulates physical HID gamepads using the UHID
kernel module on the device
- `--gamepad=aoa`: simulates physical HID gamepads using the AOAv2 protocol
## Physical gamepad simulation
Two modes allow to simulate physical HID gamepads on the device, one for each
physical gamepad plugged into the computer.
### UHID
This mode simulates physical HID gamepads using the [UHID] kernel module on the
device.
[UHID]: https://kernel.org/doc/Documentation/hid/uhid.txt
To enable UHID gamepads, use:
```bash
scrcpy --gamepad=uhid
scrcpy -G # short version
```
Note: UHID may not work on old Android versions due to permission errors.
### AOA
This mode simulates physical HID gamepads using the [AOAv2] protocol.
[AOAv2]: https://source.android.com/devices/accessories/aoa2#hid-support
To enable AOA gamepads, use:
```bash
scrcpy --gamepad=aoa
```
Contrary to the other mode, it works at the USB level directly (so it only works
over USB).
It does not use the scrcpy server, and does not require `adb` (USB debugging).
Therefore, it is possible to control the device (but not mirror) even with USB
debugging disabled (see [OTG](otg.md)).
Note: For some reason, in this mode, Android detects multiple physical gamepads
as a single misbehaving one. Use UHID if you need multiple gamepads.
Note: On Windows, it may only work in [OTG mode](otg.md), not while mirroring
(it is not possible to open a USB device if it is already open by another
process like the _adb daemon_).

View File

@@ -53,6 +53,8 @@ scrcpy --mouse=uhid
scrcpy -M # short version
```
Note: UHID may not work on old Android versions due to permission errors.
### AOA

View File

@@ -6,16 +6,18 @@ was a [physical keyboard] and/or a [physical mouse] connected to the Android
device (see [keyboard](keyboard.md) and [mouse](mouse.md)).
[physical keyboard]: keyboard.md#physical-keyboard-simulation
[physical mouse]: physical-keyboard-simulation
[physical mouse]: mouse.md#physical-mouse-simulation
A special mode (OTG) allows to control the device using AOA
[keyboard](keyboard.md#aoa) and [mouse](mouse.md#aoa), without using _adb_ at
all (so USB debugging is not necessary). In this mode, video and audio are
disabled, and `--keyboard=aoa and `--mouse=aoa` are implicitly set.
[keyboard](keyboard.md#aoa), [mouse](mouse.md#aoa) and
[gamepad](gamepad.md#aoa), without using _adb_ at all (so USB debugging is not
necessary). In this mode, video and audio are disabled, and `--keyboard=aoa` and
`--mouse=aoa` are implicitly set. However, gamepads are disabled by default, so
`--gamepad=aoa` (or `-G` in OTG mode) must be explicitly set.
Therefore, it is possible to run _scrcpy_ with only physical keyboard and mouse
simulation, as if the computer keyboard and mouse were plugged directly to the
device via an OTG cable.
Therefore, it is possible to run _scrcpy_ with only physical keyboard, mouse and
gamepad simulation, as if the computer keyboard, mouse and gamepads were plugged
directly to the device via an OTG cable.
To enable OTG mode:
@@ -32,6 +34,13 @@ scrcpy --otg --keyboard=disabled
scrcpy --otg --mouse=disabled
```
and to enable gamepads:
```bash
scrcpy --otg --gamepad=aoa
scrcpy --otg -G # short version
```
It only works if the device is connected over USB.
## OTG issues on Windows
@@ -50,9 +59,9 @@ is enabled, then OTG mode is not necessary.
Instead, disable video and audio, and select UHID (or AOA):
```bash
scrcpy --no-video --no-audio --keyboard=uhid --mouse=uhid
scrcpy --no-video --no-audio -KM # short version
scrcpy --no-video --no-audio --keyboard=aoa --mouse=aoa
scrcpy --no-video --no-audio --keyboard=uhid --mouse=uhid --gamepad=uhid
scrcpy --no-video --no-audio -KMG # short version
scrcpy --no-video --no-audio --keyboard=aoa --mouse=aoa --gamepad=aoa
```
One benefit of UHID is that it also works wirelessly.

View File

@@ -29,7 +29,7 @@ public class Options {
private boolean audioDup;
private int videoBitRate = 8000000;
private int audioBitRate = 128000;
private int maxFps;
private float maxFps;
private int lockVideoOrientation = -1;
private boolean tunnelForward;
private Rect crop;
@@ -113,7 +113,7 @@ public class Options {
return audioBitRate;
}
public int getMaxFps() {
public float getMaxFps() {
return maxFps;
}
@@ -321,7 +321,7 @@ public class Options {
options.audioBitRate = Integer.parseInt(value);
break;
case "max_fps":
options.maxFps = Integer.parseInt(value);
options.maxFps = parseFloat("max_fps", value);
break;
case "lock_video_orientation":
options.lockVideoOrientation = Integer.parseInt(value);
@@ -456,8 +456,14 @@ public class Options {
}
int width = Integer.parseInt(tokens[0]);
int height = Integer.parseInt(tokens[1]);
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("Invalid crop size: " + width + "x" + height);
}
int x = Integer.parseInt(tokens[2]);
int y = Integer.parseInt(tokens[3]);
if (x < 0 || y < 0) {
throw new IllegalArgumentException("Invalid crop offset: " + x + ":" + y);
}
return new Rect(x, y, x + width, y + height);
}
@@ -487,4 +493,12 @@ public class Options {
float floatAr = Float.parseFloat(tokens[0]);
return CameraAspectRatio.fromFloat(floatAr);
}
private static float parseFloat(String key, String value) {
try {
return Float.parseFloat(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid float value for " + key + ": \"" + value + "\"");
}
}
}

View File

@@ -56,8 +56,11 @@ public class AudioDirectCapture implements AudioCapture {
builder.setAudioSource(audioSource);
builder.setAudioFormat(AudioConfig.createAudioFormat());
int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, ENCODING);
// This buffer size does not impact latency
builder.setBufferSizeInBytes(8 * minBufferSize);
if (minBufferSize > 0) {
// This buffer size does not impact latency
builder.setBufferSizeInBytes(8 * minBufferSize);
}
return builder.build();
}

View File

@@ -3,31 +3,22 @@ package com.genymobile.scrcpy.control;
import android.net.LocalSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public final class ControlChannel {
private final InputStream inputStream;
private final OutputStream outputStream;
private final ControlMessageReader reader = new ControlMessageReader();
private final DeviceMessageWriter writer = new DeviceMessageWriter();
private final ControlMessageReader reader;
private final DeviceMessageWriter writer;
public ControlChannel(LocalSocket controlSocket) throws IOException {
this.inputStream = controlSocket.getInputStream();
this.outputStream = controlSocket.getOutputStream();
reader = new ControlMessageReader(controlSocket.getInputStream());
writer = new DeviceMessageWriter(controlSocket.getOutputStream());
}
public ControlMessage recv() throws IOException {
ControlMessage msg = reader.next();
while (msg == null) {
reader.readFrom(inputStream);
msg = reader.next();
}
return msg;
return reader.read();
}
public void send(DeviceMessage msg) throws IOException {
writer.writeTo(msg, outputStream);
writer.write(msg);
}
}

View File

@@ -131,10 +131,11 @@ public final class ControlMessage {
return msg;
}
public static ControlMessage createUhidCreate(int id, byte[] reportDesc) {
public static ControlMessage createUhidCreate(int id, String name, byte[] reportDesc) {
ControlMessage msg = new ControlMessage();
msg.type = TYPE_UHID_CREATE;
msg.id = id;
msg.text = name;
msg.data = reportDesc;
return msg;
}

View File

@@ -1,271 +1,165 @@
package com.genymobile.scrcpy.control;
import com.genymobile.scrcpy.util.Binary;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.device.Position;
import java.io.EOFException;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class ControlMessageReader {
static final int INJECT_KEYCODE_PAYLOAD_LENGTH = 13;
static final int INJECT_TOUCH_EVENT_PAYLOAD_LENGTH = 31;
static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
static final int BACK_OR_SCREEN_ON_LENGTH = 1;
static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
static final int GET_CLIPBOARD_LENGTH = 1;
static final int SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH = 9;
static final int UHID_CREATE_FIXED_PAYLOAD_LENGTH = 4;
static final int UHID_INPUT_FIXED_PAYLOAD_LENGTH = 4;
static final int UHID_DESTROY_PAYLOAD_LENGTH = 2;
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 14; // type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
public static final int INJECT_TEXT_MAX_LENGTH = 300;
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
private final DataInputStream dis;
public ControlMessageReader() {
// invariant: the buffer is always in "get" mode
buffer.limit(0);
public ControlMessageReader(InputStream rawInputStream) {
dis = new DataInputStream(new BufferedInputStream(rawInputStream));
}
public boolean isFull() {
return buffer.remaining() == rawBuffer.length;
}
public void readFrom(InputStream input) throws IOException {
if (isFull()) {
throw new IllegalStateException("Buffer full, call next() to consume");
}
buffer.compact();
int head = buffer.position();
int r = input.read(rawBuffer, head, rawBuffer.length - head);
if (r == -1) {
throw new EOFException("Controller socket closed");
}
buffer.position(head + r);
buffer.flip();
}
public ControlMessage next() {
if (!buffer.hasRemaining()) {
return null;
}
int savedPosition = buffer.position();
int type = buffer.get();
ControlMessage msg;
public ControlMessage read() throws IOException {
int type = dis.readUnsignedByte();
switch (type) {
case ControlMessage.TYPE_INJECT_KEYCODE:
msg = parseInjectKeycode();
break;
return parseInjectKeycode();
case ControlMessage.TYPE_INJECT_TEXT:
msg = parseInjectText();
break;
return parseInjectText();
case ControlMessage.TYPE_INJECT_TOUCH_EVENT:
msg = parseInjectTouchEvent();
break;
return parseInjectTouchEvent();
case ControlMessage.TYPE_INJECT_SCROLL_EVENT:
msg = parseInjectScrollEvent();
break;
return parseInjectScrollEvent();
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
msg = parseBackOrScreenOnEvent();
break;
return parseBackOrScreenOnEvent();
case ControlMessage.TYPE_GET_CLIPBOARD:
msg = parseGetClipboard();
break;
return parseGetClipboard();
case ControlMessage.TYPE_SET_CLIPBOARD:
msg = parseSetClipboard();
break;
return parseSetClipboard();
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
msg = parseSetScreenPowerMode();
break;
return parseSetScreenPowerMode();
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
case ControlMessage.TYPE_COLLAPSE_PANELS:
case ControlMessage.TYPE_ROTATE_DEVICE:
case ControlMessage.TYPE_OPEN_HARD_KEYBOARD_SETTINGS:
msg = ControlMessage.createEmpty(type);
break;
return ControlMessage.createEmpty(type);
case ControlMessage.TYPE_UHID_CREATE:
msg = parseUhidCreate();
break;
return parseUhidCreate();
case ControlMessage.TYPE_UHID_INPUT:
msg = parseUhidInput();
break;
return parseUhidInput();
case ControlMessage.TYPE_UHID_DESTROY:
msg = parseUhidDestroy();
break;
return parseUhidDestroy();
default:
Ln.w("Unknown event type: " + type);
msg = null;
break;
throw new ControlProtocolException("Unknown event type: " + type);
}
if (msg == null) {
// failure, reset savedPosition
buffer.position(savedPosition);
}
return msg;
}
private ControlMessage parseInjectKeycode() {
if (buffer.remaining() < INJECT_KEYCODE_PAYLOAD_LENGTH) {
return null;
}
int action = Binary.toUnsigned(buffer.get());
int keycode = buffer.getInt();
int repeat = buffer.getInt();
int metaState = buffer.getInt();
private ControlMessage parseInjectKeycode() throws IOException {
int action = dis.readUnsignedByte();
int keycode = dis.readInt();
int repeat = dis.readInt();
int metaState = dis.readInt();
return ControlMessage.createInjectKeycode(action, keycode, repeat, metaState);
}
private int parseBufferLength(int sizeBytes) {
private int parseBufferLength(int sizeBytes) throws IOException {
assert sizeBytes > 0 && sizeBytes <= 4;
if (buffer.remaining() < sizeBytes) {
return -1;
}
int value = 0;
for (int i = 0; i < sizeBytes; ++i) {
value = (value << 8) | (buffer.get() & 0xFF);
value = (value << 8) | dis.readUnsignedByte();
}
return value;
}
private String parseString() {
int len = parseBufferLength(4);
if (len == -1 || buffer.remaining() < len) {
return null;
}
int position = buffer.position();
// Move the buffer position to consume the text
buffer.position(position + len);
return new String(rawBuffer, position, len, StandardCharsets.UTF_8);
private String parseString(int sizeBytes) throws IOException {
assert sizeBytes > 0 && sizeBytes <= 4;
byte[] data = parseByteArray(sizeBytes);
return new String(data, StandardCharsets.UTF_8);
}
private byte[] parseByteArray(int sizeBytes) {
private String parseString() throws IOException {
return parseString(4);
}
private byte[] parseByteArray(int sizeBytes) throws IOException {
int len = parseBufferLength(sizeBytes);
if (len == -1 || buffer.remaining() < len) {
return null;
}
byte[] data = new byte[len];
buffer.get(data);
dis.readFully(data);
return data;
}
private ControlMessage parseInjectText() {
private ControlMessage parseInjectText() throws IOException {
String text = parseString();
if (text == null) {
return null;
}
return ControlMessage.createInjectText(text);
}
private ControlMessage parseInjectTouchEvent() {
if (buffer.remaining() < INJECT_TOUCH_EVENT_PAYLOAD_LENGTH) {
return null;
}
int action = Binary.toUnsigned(buffer.get());
long pointerId = buffer.getLong();
Position position = readPosition(buffer);
float pressure = Binary.u16FixedPointToFloat(buffer.getShort());
int actionButton = buffer.getInt();
int buttons = buffer.getInt();
private ControlMessage parseInjectTouchEvent() throws IOException {
int action = dis.readUnsignedByte();
long pointerId = dis.readLong();
Position position = parsePosition();
float pressure = Binary.u16FixedPointToFloat(dis.readShort());
int actionButton = dis.readInt();
int buttons = dis.readInt();
return ControlMessage.createInjectTouchEvent(action, pointerId, position, pressure, actionButton, buttons);
}
private ControlMessage parseInjectScrollEvent() {
if (buffer.remaining() < INJECT_SCROLL_EVENT_PAYLOAD_LENGTH) {
return null;
}
Position position = readPosition(buffer);
float hScroll = Binary.i16FixedPointToFloat(buffer.getShort());
float vScroll = Binary.i16FixedPointToFloat(buffer.getShort());
int buttons = buffer.getInt();
private ControlMessage parseInjectScrollEvent() throws IOException {
Position position = parsePosition();
float hScroll = Binary.i16FixedPointToFloat(dis.readShort());
float vScroll = Binary.i16FixedPointToFloat(dis.readShort());
int buttons = dis.readInt();
return ControlMessage.createInjectScrollEvent(position, hScroll, vScroll, buttons);
}
private ControlMessage parseBackOrScreenOnEvent() {
if (buffer.remaining() < BACK_OR_SCREEN_ON_LENGTH) {
return null;
}
int action = Binary.toUnsigned(buffer.get());
private ControlMessage parseBackOrScreenOnEvent() throws IOException {
int action = dis.readUnsignedByte();
return ControlMessage.createBackOrScreenOn(action);
}
private ControlMessage parseGetClipboard() {
if (buffer.remaining() < GET_CLIPBOARD_LENGTH) {
return null;
}
int copyKey = Binary.toUnsigned(buffer.get());
private ControlMessage parseGetClipboard() throws IOException {
int copyKey = dis.readUnsignedByte();
return ControlMessage.createGetClipboard(copyKey);
}
private ControlMessage parseSetClipboard() {
if (buffer.remaining() < SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH) {
return null;
}
long sequence = buffer.getLong();
boolean paste = buffer.get() != 0;
private ControlMessage parseSetClipboard() throws IOException {
long sequence = dis.readLong();
boolean paste = dis.readByte() != 0;
String text = parseString();
if (text == null) {
return null;
}
return ControlMessage.createSetClipboard(sequence, text, paste);
}
private ControlMessage parseSetScreenPowerMode() {
if (buffer.remaining() < SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH) {
return null;
}
int mode = buffer.get();
private ControlMessage parseSetScreenPowerMode() throws IOException {
int mode = dis.readUnsignedByte();
return ControlMessage.createSetScreenPowerMode(mode);
}
private ControlMessage parseUhidCreate() {
if (buffer.remaining() < UHID_CREATE_FIXED_PAYLOAD_LENGTH) {
return null;
}
int id = buffer.getShort();
private ControlMessage parseUhidCreate() throws IOException {
int id = dis.readUnsignedShort();
String name = parseString(1);
byte[] data = parseByteArray(2);
if (data == null) {
return null;
}
return ControlMessage.createUhidCreate(id, data);
return ControlMessage.createUhidCreate(id, name, data);
}
private ControlMessage parseUhidInput() {
if (buffer.remaining() < UHID_INPUT_FIXED_PAYLOAD_LENGTH) {
return null;
}
int id = buffer.getShort();
private ControlMessage parseUhidInput() throws IOException {
int id = dis.readUnsignedShort();
byte[] data = parseByteArray(2);
if (data == null) {
return null;
}
return ControlMessage.createUhidInput(id, data);
}
private ControlMessage parseUhidDestroy() {
if (buffer.remaining() < UHID_DESTROY_PAYLOAD_LENGTH) {
return null;
}
int id = buffer.getShort();
private ControlMessage parseUhidDestroy() throws IOException {
int id = dis.readUnsignedShort();
return ControlMessage.createUhidDestroy(id);
}
private static Position readPosition(ByteBuffer buffer) {
int x = buffer.getInt();
int y = buffer.getInt();
int screenWidth = Binary.toUnsigned(buffer.getShort());
int screenHeight = Binary.toUnsigned(buffer.getShort());
private Position parsePosition() throws IOException {
int x = dis.readInt();
int y = dis.readInt();
int screenWidth = dis.readUnsignedShort();
int screenHeight = dis.readUnsignedShort();
return new Position(x, y, screenWidth, screenHeight);
}
}

View File

@@ -0,0 +1,9 @@
package com.genymobile.scrcpy.control;
import java.io.IOException;
public class ControlProtocolException extends IOException {
public ControlProtocolException(String message) {
super(message);
}
}

View File

@@ -210,7 +210,7 @@ public class Controller implements AsyncProcessor {
device.rotateDevice();
break;
case ControlMessage.TYPE_UHID_CREATE:
getUhidManager().open(msg.getId(), msg.getData());
getUhidManager().open(msg.getId(), msg.getText(), msg.getData());
break;
case ControlMessage.TYPE_UHID_INPUT:
getUhidManager().writeInput(msg.getId(), msg.getData());

View File

@@ -1,11 +1,11 @@
package com.genymobile.scrcpy.control;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.StringUtils;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class DeviceMessageWriter {
@@ -13,35 +13,35 @@ public class DeviceMessageWriter {
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 5; // type: 1 byte; length: 4 bytes
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
private final DataOutputStream dos;
public void writeTo(DeviceMessage msg, OutputStream output) throws IOException {
buffer.clear();
buffer.put((byte) msg.getType());
switch (msg.getType()) {
public DeviceMessageWriter(OutputStream rawOutputStream) {
dos = new DataOutputStream(new BufferedOutputStream(rawOutputStream));
}
public void write(DeviceMessage msg) throws IOException {
int type = msg.getType();
dos.writeByte(type);
switch (type) {
case DeviceMessage.TYPE_CLIPBOARD:
String text = msg.getText();
byte[] raw = text.getBytes(StandardCharsets.UTF_8);
int len = StringUtils.getUtf8TruncationIndex(raw, CLIPBOARD_TEXT_MAX_LENGTH);
buffer.putInt(len);
buffer.put(raw, 0, len);
output.write(rawBuffer, 0, buffer.position());
dos.writeInt(len);
dos.write(raw, 0, len);
break;
case DeviceMessage.TYPE_ACK_CLIPBOARD:
buffer.putLong(msg.getSequence());
output.write(rawBuffer, 0, buffer.position());
dos.writeLong(msg.getSequence());
break;
case DeviceMessage.TYPE_UHID_OUTPUT:
buffer.putShort((short) msg.getId());
dos.writeShort(msg.getId());
byte[] data = msg.getData();
buffer.putShort((short) data.length);
buffer.put(data);
output.write(rawBuffer, 0, buffer.position());
dos.writeShort(data.length);
dos.write(data);
break;
default:
Ln.w("Unknown device message: " + msg.getType());
break;
throw new ControlProtocolException("Unknown event type: " + type);
}
dos.flush();
}
}

View File

@@ -1,6 +1,7 @@
package com.genymobile.scrcpy.control;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.StringUtils;
import android.os.Build;
import android.os.HandlerThread;
@@ -46,7 +47,7 @@ public final class UhidManager {
}
}
public void open(int id, byte[] reportDesc) throws IOException {
public void open(int id, String name, byte[] reportDesc) throws IOException {
try {
FileDescriptor fd = Os.open("/dev/uhid", OsConstants.O_RDWR, 0);
try {
@@ -56,7 +57,7 @@ public final class UhidManager {
close(old);
}
byte[] req = buildUhidCreate2Req(reportDesc);
byte[] req = buildUhidCreate2Req(name, reportDesc);
Os.write(fd, req, 0, req.length);
registerUhidListener(id, fd);
@@ -146,7 +147,7 @@ public final class UhidManager {
}
}
private static byte[] buildUhidCreate2Req(byte[] reportDesc) {
private static byte[] buildUhidCreate2Req(String name, byte[] reportDesc) {
/*
* struct uhid_event {
* uint32_t type;
@@ -171,8 +172,14 @@ public final class UhidManager {
byte[] empty = new byte[256];
ByteBuffer buf = ByteBuffer.allocate(280 + reportDesc.length).order(ByteOrder.nativeOrder());
buf.putInt(UHID_CREATE2);
buf.put("scrcpy".getBytes(StandardCharsets.US_ASCII));
buf.put(empty, 0, 256 - "scrcpy".length());
String actualName = name.isEmpty() ? "scrcpy" : "scrcpy: " + name;
byte[] utf8Name = actualName.getBytes(StandardCharsets.UTF_8);
int len = StringUtils.getUtf8TruncationIndex(utf8Name, 127);
assert len <= 127;
buf.put(utf8Name, 0, len);
buf.put(empty, 0, 256 - len);
buf.putShort((short) reportDesc.length);
buf.putShort(BUS_VIRTUAL);
buf.putInt(0); // vendor id

View File

@@ -39,7 +39,7 @@ public class SurfaceEncoder implements AsyncProcessor {
private final String encoderName;
private final List<CodecOption> codecOptions;
private final int videoBitRate;
private final int maxFps;
private final float maxFps;
private final boolean downsizeOnError;
private boolean firstFrameSent;
@@ -48,8 +48,8 @@ public class SurfaceEncoder implements AsyncProcessor {
private Thread thread;
private final AtomicBoolean stopped = new AtomicBoolean();
public SurfaceEncoder(SurfaceCapture capture, Streamer streamer, int videoBitRate, int maxFps, List<CodecOption> codecOptions, String encoderName,
boolean downsizeOnError) {
public SurfaceEncoder(SurfaceCapture capture, Streamer streamer, int videoBitRate, float maxFps, List<CodecOption> codecOptions,
String encoderName, boolean downsizeOnError) {
this.capture = capture;
this.streamer = streamer;
this.videoBitRate = videoBitRate;
@@ -225,7 +225,7 @@ public class SurfaceEncoder implements AsyncProcessor {
}
}
private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions) {
private static MediaFormat createFormat(String videoMimeType, int bitRate, float maxFps, List<CodecOption> codecOptions) {
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, videoMimeType);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);

View File

@@ -10,6 +10,7 @@ import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@@ -18,8 +19,6 @@ public class ControlMessageReaderTest {
@Test
public void testParseKeycodeEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_INJECT_KEYCODE);
@@ -29,23 +28,21 @@ public class ControlMessageReaderTest {
dos.writeInt(KeyEvent.META_CTRL_ON);
byte[] packet = bos.toByteArray();
// The message type (1 byte) does not count
Assert.assertEquals(ControlMessageReader.INJECT_KEYCODE_PAYLOAD_LENGTH, packet.length - 1);
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_KEYCODE, event.getType());
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
Assert.assertEquals(5, event.getRepeat());
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseTextEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_INJECT_TEXT);
@@ -54,17 +51,18 @@ public class ControlMessageReaderTest {
dos.write(text);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_TEXT, event.getType());
Assert.assertEquals("testé", event.getText());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseLongTextEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_INJECT_TEXT);
@@ -74,17 +72,18 @@ public class ControlMessageReaderTest {
dos.write(text);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_TEXT, event.getType());
Assert.assertEquals(new String(text, StandardCharsets.US_ASCII), event.getText());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseTouchEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_INJECT_TOUCH_EVENT);
@@ -100,12 +99,10 @@ public class ControlMessageReaderTest {
byte[] packet = bos.toByteArray();
// The message type (1 byte) does not count
Assert.assertEquals(ControlMessageReader.INJECT_TOUCH_EVENT_PAYLOAD_LENGTH, packet.length - 1);
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_TOUCH_EVENT, event.getType());
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
Assert.assertEquals(-42, event.getPointerId());
@@ -116,12 +113,12 @@ public class ControlMessageReaderTest {
Assert.assertEquals(1f, event.getPressure(), 0f); // must be exact
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getActionButton());
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getButtons());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseScrollEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_INJECT_SCROLL_EVENT);
@@ -132,15 +129,12 @@ public class ControlMessageReaderTest {
dos.writeShort(0); // 0.0f encoded as i16
dos.writeShort(0x8000); // -1.0f encoded as i16
dos.writeInt(1);
byte[] packet = bos.toByteArray();
// The message type (1 byte) does not count
Assert.assertEquals(ControlMessageReader.INJECT_SCROLL_EVENT_PAYLOAD_LENGTH, packet.length - 1);
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_SCROLL_EVENT, event.getType());
Assert.assertEquals(260, event.getPosition().getPoint().getX());
Assert.assertEquals(1026, event.getPosition().getPoint().getY());
@@ -149,96 +143,96 @@ public class ControlMessageReaderTest {
Assert.assertEquals(0f, event.getHScroll(), 0f);
Assert.assertEquals(-1f, event.getVScroll(), 0f);
Assert.assertEquals(1, event.getButtons());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseBackOrScreenOnEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_BACK_OR_SCREEN_ON);
dos.writeByte(KeyEvent.ACTION_UP);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_BACK_OR_SCREEN_ON, event.getType());
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseExpandNotificationPanelEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL, event.getType());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseExpandSettingsPanelEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_EXPAND_SETTINGS_PANEL);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_EXPAND_SETTINGS_PANEL, event.getType());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseCollapsePanelsEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_COLLAPSE_PANELS);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_COLLAPSE_PANELS, event.getType());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseGetClipboardEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_GET_CLIPBOARD);
dos.writeByte(ControlMessage.COPY_KEY_COPY);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_GET_CLIPBOARD, event.getType());
Assert.assertEquals(ControlMessage.COPY_KEY_COPY, event.getCopyKey());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseSetClipboardEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_SET_CLIPBOARD);
@@ -247,22 +241,22 @@ public class ControlMessageReaderTest {
byte[] text = "testé".getBytes(StandardCharsets.UTF_8);
dos.writeInt(text.length);
dos.write(text);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_SET_CLIPBOARD, event.getType());
Assert.assertEquals(0x0102030405060708L, event.getSequence());
Assert.assertEquals("testé", event.getText());
Assert.assertTrue(event.getPaste());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseBigSetClipboardEvent() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_SET_CLIPBOARD);
@@ -278,78 +272,79 @@ public class ControlMessageReaderTest {
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_SET_CLIPBOARD, event.getType());
Assert.assertEquals(0x0807060504030201L, event.getSequence());
Assert.assertEquals(text, event.getText());
Assert.assertTrue(event.getPaste());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseSetScreenPowerMode() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_SET_SCREEN_POWER_MODE);
dos.writeByte(Device.POWER_MODE_NORMAL);
byte[] packet = bos.toByteArray();
// The message type (1 byte) does not count
Assert.assertEquals(ControlMessageReader.SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH, packet.length - 1);
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_SET_SCREEN_POWER_MODE, event.getType());
Assert.assertEquals(Device.POWER_MODE_NORMAL, event.getAction());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseRotateDevice() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_ROTATE_DEVICE);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_ROTATE_DEVICE, event.getType());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseUhidCreate() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_UHID_CREATE);
dos.writeShort(42); // id
dos.writeByte(3); // name size
dos.write("ABC".getBytes(StandardCharsets.US_ASCII));
byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
dos.writeShort(data.length); // size
dos.writeShort(data.length); // report desc size
dos.write(data);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_UHID_CREATE, event.getType());
Assert.assertEquals(42, event.getId());
Assert.assertEquals("ABC", event.getText());
Assert.assertArrayEquals(data, event.getData());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseUhidInput() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_UHID_INPUT);
@@ -357,55 +352,55 @@ public class ControlMessageReaderTest {
byte[] data = {1, 2, 3, 4, 5};
dos.writeShort(data.length); // size
dos.write(data);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_UHID_INPUT, event.getType());
Assert.assertEquals(42, event.getId());
Assert.assertArrayEquals(data, event.getData());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseUhidDestroy() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_UHID_DESTROY);
dos.writeShort(42); // id
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_UHID_DESTROY, event.getType());
Assert.assertEquals(42, event.getId());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseOpenHardKeyboardSettings() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_OPEN_HARD_KEYBOARD_SETTINGS);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_OPEN_HARD_KEYBOARD_SETTINGS, event.getType());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testMultiEvents() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
@@ -422,27 +417,29 @@ public class ControlMessageReaderTest {
dos.writeInt(KeyEvent.META_CTRL_ON);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_KEYCODE, event.getType());
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
Assert.assertEquals(0, event.getRepeat());
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
event = reader.next();
event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_KEYCODE, event.getType());
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getKeycode());
Assert.assertEquals(1, event.getRepeat());
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testPartialEvents() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
@@ -456,31 +453,21 @@ public class ControlMessageReaderTest {
dos.writeByte(MotionEvent.ACTION_DOWN);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.next();
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_INJECT_KEYCODE, event.getType());
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
Assert.assertEquals(4, event.getRepeat());
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
event = reader.next();
Assert.assertNull(event); // the event is not complete
bos.reset();
dos.writeInt(MotionEvent.BUTTON_PRIMARY);
dos.writeInt(5); // repeat
dos.writeInt(KeyEvent.META_CTRL_ON);
packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
// the event is now complete
event = reader.next();
Assert.assertEquals(ControlMessage.TYPE_INJECT_KEYCODE, event.getType());
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getKeycode());
Assert.assertEquals(5, event.getRepeat());
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
try {
event = reader.read();
Assert.fail("Reader did not reach EOF");
} catch (EOFException e) {
// expected
}
}
}

View File

@@ -12,8 +12,6 @@ public class DeviceMessageWriterTest {
@Test
public void testSerializeClipboard() throws IOException {
DeviceMessageWriter writer = new DeviceMessageWriter();
String text = "aéûoç";
byte[] data = text.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -21,12 +19,13 @@ public class DeviceMessageWriterTest {
dos.writeByte(DeviceMessage.TYPE_CLIPBOARD);
dos.writeInt(data.length);
dos.write(data);
byte[] expected = bos.toByteArray();
DeviceMessage msg = DeviceMessage.createClipboard(text);
bos = new ByteArrayOutputStream();
writer.writeTo(msg, bos);
DeviceMessageWriter writer = new DeviceMessageWriter(bos);
DeviceMessage msg = DeviceMessage.createClipboard(text);
writer.write(msg);
byte[] actual = bos.toByteArray();
@@ -35,18 +34,17 @@ public class DeviceMessageWriterTest {
@Test
public void testSerializeAckSetClipboard() throws IOException {
DeviceMessageWriter writer = new DeviceMessageWriter();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(DeviceMessage.TYPE_ACK_CLIPBOARD);
dos.writeLong(0x0102030405060708L);
byte[] expected = bos.toByteArray();
DeviceMessage msg = DeviceMessage.createAckClipboard(0x0102030405060708L);
bos = new ByteArrayOutputStream();
writer.writeTo(msg, bos);
DeviceMessageWriter writer = new DeviceMessageWriter(bos);
DeviceMessage msg = DeviceMessage.createAckClipboard(0x0102030405060708L);
writer.write(msg);
byte[] actual = bos.toByteArray();
@@ -55,8 +53,6 @@ public class DeviceMessageWriterTest {
@Test
public void testSerializeUhidOutput() throws IOException {
DeviceMessageWriter writer = new DeviceMessageWriter();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(DeviceMessage.TYPE_UHID_OUTPUT);
@@ -64,12 +60,13 @@ public class DeviceMessageWriterTest {
byte[] data = {1, 2, 3, 4, 5};
dos.writeShort(data.length);
dos.write(data);
byte[] expected = bos.toByteArray();
DeviceMessage msg = DeviceMessage.createUhidOutput(42, data);
bos = new ByteArrayOutputStream();
writer.writeTo(msg, bos);
DeviceMessageWriter writer = new DeviceMessageWriter(bos);
DeviceMessage msg = DeviceMessage.createUhidOutput(42, data);
writer.write(msg);
byte[] actual = bos.toByteArray();