Compare commits

...

173 Commits

Author SHA1 Message Date
b3f5dfe1de Add specific exit code for device disconnection
Modify the return logic such that exit code 1 is used when the initial
connection fails, but if a session is established, and then the device
disconnects, exit code 2 is emitted.

Fixes #3083 <https://github.com/Genymobile/scrcpy/issues/3083>
PR #3085 <https://github.com/Genymobile/scrcpy/pull/3085>
Signed-off-by: martin f. krafft <madduck@madduck.net>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-03-06 22:19:46 +01:00
1f4c801f3c Report server connection state
We must distinguish 3 cases for await_for_server():
 - an error occurred
 - no error occurred, the device is connected
 - no error occurred, the device is not connected (user requested to
   quit)

For this purpose, use an additional output parameter to indicate if the
device is connected (only set when no error occurs).

Refs #3085 <https://github.com/Genymobile/scrcpy/pull/3085>
2022-03-06 22:16:13 +01:00
8d91cda4f6 Improve HID event push error message
On HID event push failure, add the event type in the error message.
2022-02-24 23:28:20 +01:00
59656fe649 Fix typo in error message 2022-02-24 23:26:12 +01:00
e4bb2b8728 Add libusb error log
Log libusb_get_string_descriptor_ascii() errors.

Refs #3050 <https://github.com/Genymobile/scrcpy/issues/3050>
2022-02-24 23:25:02 +01:00
adbe7908c6 Fix icon path in README
The data/ directory was moved to app/data/.

Refs 36c75e15b8
2022-02-23 01:21:18 +01:00
49434da36e Update links to v1.23 2022-02-22 23:48:00 +01:00
7deccef1c2 Bump version to 1.23 2022-02-22 21:01:55 +01:00
977735f916 Merge branch 'master' into dev 2022-02-22 21:01:43 +01:00
71ef5cc0a9 Add missing include for vector
Include stdlib.h for realloc().
2022-02-22 21:00:43 +01:00
4ab4548769 Add contact links to the README
Add Reddit and Twitter links (and an additional link to the GitHub
issues).
2022-02-22 19:36:22 +01:00
3ce6f8ca91 Add Bash completion script
Fixes #2930 <https://github.com/Genymobile/scrcpy/issues/2930>
Refs #3012 <https://github.com/Genymobile/scrcpy/pull/3012>
2022-02-22 19:22:12 +01:00
26953784d9 Add ZSH completion script
PR #3012 <https://github.com/Genymobile/scrcpy/pull/3012>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 19:15:27 +01:00
2716385887 Move "Device unauthorized" in FAQ
Put "Device not detected" first.
2022-02-22 19:12:02 +01:00
1f951f1a3a Update FAQ to match the latest version 2022-02-22 19:08:15 +01:00
6a9b2f2c36 Remove spurious empty line 2022-02-22 18:31:37 +01:00
ff8a69d8ec Mention adb wireless option for Android 11+
Add a paragraph about toggling an option to bypass having to physically
connect the device to the user's computer.

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

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
1693797277 Make step more explicit in wireless section
PR #1303 <https://github.com/Genymobile/scrcpy/pull/1303>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
8610e9a454 Add troubleshooting in wireless section
Add a small troubleshooting section since wireless might add some
complexity, and to lessen incoming relevant issue posts.

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

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
528275d501 Improve phrasing in wireless section
PR #1303 <https://github.com/Genymobile/scrcpy/pull/1303>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
78b18b7cee Renumber steps in wireless section
The actual numbers are ignored by markdown, but start at 1 for
consistency.

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

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
90816291d4 Add an explicit first step in wireless section
Mention that the device must be plugged via USB before configuring
TCP/IP connections.

It wasn't obvious that the device should be first plugged before running
scrcpy wirelessly, especially to those who aren't very familiar with
adb.

Note from committer: add this new step with index 0 to make the diff
readable, the next commit will renumber all the steps.

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

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-22 18:27:25 +01:00
3e0df6ad05 Update HID/OTG features in README
HID/OTG features are not limited to Linux anymore.

Refs 82a99f69ec
2022-02-22 18:27:13 +01:00
79ed83ab68 Reorder --tcpip option in cli
To keep options in alphabetic order.
2022-02-22 18:10:30 +01:00
71b41d846f Also retry on IllegalArgumentException
MediaCodec.configure() may throw an IllegalArgumentException if it does
not support the requested size. Also retry on this exception.

Fixes #2993 <https://github.com/Genymobile/scrcpy/issues/2993>
Refs #2947 <https://github.com/Genymobile/scrcpy/pull/2947>
Refs #2990 <https://github.com/Genymobile/scrcpy/pull/2990>
PR #3043 <https://github.com/Genymobile/scrcpy/pull/3043>
2022-02-22 10:27:11 +01:00
e2e76c5d48 Increase adb devices -l max output size
For simplicity, the parsing of `adb devices -l` output is performed in a
single pass on the whole output.

This output was limited to 4096 bytes. Since there are about 100 chars
per device line, this limited the number of connected devices to ~40.

Increase to 65536 bytes to avoid a limitation in practice.

PR #3035 <https://github.com/Genymobile/scrcpy/pull/3035>
2022-02-21 00:03:45 +01:00
4b8cb042c4 Use vector for listing ADB devices
This avoids the hardcoded maximum number of ADB devices detected (16).

Refs #3029 <https://github.com/Genymobile/scrcpy/pull/3029>
PR #3035 <https://github.com/Genymobile/scrcpy/pull/3035>

Co-authored-by: Daniel Ansorregui <d.ansorregui@samsung.com>
2022-02-20 23:59:35 +01:00
1790e88278 Use vector for listing USB devices
This avoids the hardcoded maximum number of USB devices detected (16).

Refs #3029 <https://github.com/Genymobile/scrcpy/pull/3029>
PR #3035 <https://github.com/Genymobile/scrcpy/pull/3035>
2022-02-20 23:59:35 +01:00
c070723bc8 Add sc_vector
Adapt vlc_vector [1], that I initially wrote while implementing the VLC
playlist [2].

Change the implementation to use "statement expressions" [3], which are
forbidden in VLC because "non-standard", but:
 - they are supported by gcc and clang;
 - they are already used in the scrcpy codebase;
 - they avoid implementation hacks (VLC_VECTOR_FAILFLAG_);
 - they allow a better API (sc_vector_index_of() may return the result
   without an output parameter).

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

[1]: 0857947aba/include/vlc_vector.h
[2]: https://blog.rom1v.com/2019/05/a-new-core-playlist-for-vlc-4
[3]: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
2022-02-20 23:59:35 +01:00
36c75e15b8 Move data/ to app/
The files in data/ are specific to the client app (not the server).

This also avoids to reference the parent directory (../) from
app/meson.build.

Refs 8d583d36e2
2022-02-20 17:56:50 +01:00
d9bc5082ab Disable USB features for win32
Currently, there is an issue with the libusb prebuilt dll.

Refs libusb/#1049 <https://github.com/libusb/libusb/issues/1049>
PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:52 +01:00
73a5311ac6 Forbid HID input without OTG on Windows
On Windows, if the adb daemon is running, opening the USB device will
necessarily fail, so HID input is not possible.

Refs #2773 <https://github.com/Genymobile/scrcpy/issues/2773>
PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:42 +01:00
25296ae167 Kill adb daemon in OTG mode on Windows
On Windows, it is not possible to open a USB device from several
process, so HID events may only work if no adb daemon is running.

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:39 +01:00
3bb24b3926 Make intr optional for adb commands
All adb commands are executed with an "interruptor", so that they can be
interrupted on Ctrl+C.

Make this interruptor optional, so that we could call "adb kill-server"
in OTG mode. This command always returns almost immediately anyway.

Ideally, we should make all blocking calls interruptible (including
libusb calls, by using the asynchronous API), but it's a lot of work,
and in practice it works well enough.

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:36 +01:00
6ee75c0cff Remove obsolete text in error message
The HID/OTG features are now available on all platforms.

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:30 +01:00
6b65cd405a Build for Windows with libusb support
Fixes #2773 <https://github.com/Genymobile/scrcpy/issues/2773>
PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:12 +01:00
ff3cb31cb4 Fix libusb callback for Windows
Add LIBUSB_CALL so that the callback has the correct signature on
Windows (including __attribute__((stdcall))).

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:09 +01:00
06243e7c3c Avoid PRIx16 printf format on Windows
Convert uint16_t to unsigned to avoid using PRIx16, which may not exist
on Windows.

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:39:04 +01:00
b9b2879789 Remove USB hotplug callback error log
If it fails, the error is already logged by sc_usb_register_callback().

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:38:51 +01:00
be1936bb85 Report USB device disconnection when detected
USB device disconnection is detected via a hotplug callback when it is
supported.

In addition, report disconnection on libusb calls returning
LIBUSB_ERROR_NO_DEVICE or LIBUSB_ERROR_NOT_FOUND. This allows to detect
disconnection after a libusb call when hotplug is not available.

PR #3011 <https://github.com/Genymobile/scrcpy/pull/3011>
2022-02-20 17:38:14 +01:00
3ee3f8dc02 Work around mouse capture SDL bug on macOS
On macOS, SDL relative mouse mode does not work correctly when the
cursor is outside the window.

As a workaround, move the cursor inside the window before setting the
relative mouse mode.

Refs SDL/#5340 <https://github.com/libsdl-org/SDL/issues/5340>
PR #3031 <https://github.com/Genymobile/scrcpy/pull/3031>
2022-02-20 17:28:02 +01:00
9db42341e4 Pass screen instance to mouse capture functions
Using the screen instance or not in these functions is an implementation
detail. Further changes will require the screen instance.

Refs 7848a387c8
PR #3031 <https://github.com/Genymobile/scrcpy/pull/3031>
2022-02-20 17:27:53 +01:00
82a99f69ec Remove "linux-only" mentions for HID/OTG features
HID/OTG features are not limited to Linux anymore.

PR #3031 <https://github.com/Genymobile/scrcpy/pull/3031>
2022-02-20 17:27:43 +01:00
33202491e1 Build on macOS with libusb support
Fixes #2774 <https://github.com/Genymobile/scrcpy/issues/2774>
PR #3031 <https://github.com/Genymobile/scrcpy/pull/3031>
2022-02-20 17:27:22 +01:00
b4fd882ece Fix typo 2022-02-20 17:21:25 +01:00
c4ab65eb79 Remove useless '\n' in log 2022-02-18 21:18:36 +01:00
6edf50d447 Remove fprintf() in tests
It was committed by mistake.
2022-02-18 19:34:54 +01:00
4b018be789 Add --print-fps to enable FPS counter on start
The FPS counter could be enabled/disabled via MOD+i.

Add a command line option to enable it on start. This is consistent with
other features like --turn-screen-off or --fullscreen.

Fixes #468 <https://github.com/Genymobile/scrcpy/issues/468>
PR #3030 <https://github.com/Genymobile/scrcpy/pull/3030>
2022-02-18 18:16:50 +01:00
fa93c8a91b Move FPS counter start/stop logs
This will allow to print the same logs for every start/stop call.

PR #3030 <https://github.com/Genymobile/scrcpy/pull/3030>
2022-02-18 18:16:12 +01:00
0e22032710 Update FAQ about Windows scaling behavior
Recommend to update to v1.22 before suggesting manual configuration.

Fixes #3028 <https://github.com/Genymobile/scrcpy/issues/3028>
PR #3032 <https://github.com/Genymobile/scrcpy/pull/3032>
2022-02-18 18:13:35 +01:00
03705b828b Use sc_prefix for fps counter 2022-02-17 19:55:24 +01:00
7a138c6929 Fix links in German README
There were three links that weren't displayed correctly due to incorrect
references:
 - the Windows release link to `README.md#windows`
 - 2 links that expected a German reference but got an English reference

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

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-16 20:44:09 +01:00
85edba20e7 Enforce deadline reached on timeout
The value of sc_tick_now() has microsecond precision, but
sc_cond_timedwait() has only millisecond precision.

To guarantee that sc_tick_now() >= deadline when sc_cond_timedwait()
returns due to timeout, round up to the next millisecond.

This avoids to call a non-blocking sc_cond_timedwait() in a loop for no
reason until a target deadline during up to 1 millisecond.

Refs 682a691173
2022-02-16 18:29:30 +01:00
2a872c3865 Fix fps_counter tick type
The type uint32_t is not sufficient to store the result of
sc_tick_now().

As a consequence, the FPS counter entered a live loop and caused a lock
starvation (deadlock in practice).

Refs ec871dd3f5
Refs 682a691173
2022-02-16 18:12:57 +01:00
b58b566fa5 Add German translation of README.md
PR #3023 <https://github.com/Genymobile/scrcpy/pull/3023>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-15 21:52:08 +01:00
ccbe370cc5 Add --no-cleanup option
It might be useful not to cleanup on exit, for example to leave the
screen turned off, or keep the server binary on the device (via the
server option "cleanup=false").

Fixes #1764 <https://github.com/Genymobile/scrcpy/issues/1764>
PR #3020 <https://github.com/Genymobile/scrcpy/pull/3020>
2022-02-15 19:25:57 +01:00
bb991f829c Fix order of options
In alphabetic order, "no-clipboard-autosync" is before
"no-downsize-on-error".
2022-02-13 17:26:34 +01:00
ca9e1a0514 Add compilation flag for USB features
This allows to disable HID/OTG features on Linux to build without
libusb.
2022-02-12 14:15:07 +01:00
cc27771dd1 Add compilation flag for V4L2 feature
This allows to disable V4L2 support on Linux to build without
libavdevice.
2022-02-12 14:15:07 +01:00
d0ab8c0e7b Fix double adb tunnel closing
On error, close the adb tunnel only if it has not already been closed
beforehand.
2022-02-12 14:15:02 +01:00
5c62f3419d Rename buffer util functions with sc_ prefix 2022-02-12 09:12:46 +01:00
044acc2259 Rename HEADER_SIZE to SC_PACKET_HEADER_SIZE
Prefix the constant for consistency.
2022-02-11 21:34:58 +01:00
6fc388f369 Remove unused BUFSIZE 2022-02-11 21:34:12 +01:00
1c02b58412 Remove sc_demuxer_parse()
Now that the key frame flag is known, parsing the packet is useless.
2022-02-11 21:33:09 +01:00
67068e4e3d Pass key frame flag from the device
MediaCodec indicates when a packet is a key frame. Transmit it to the
client.
2022-02-11 21:32:55 +01:00
e3c2398aa2 Store packet flags in PTS most significant bits
A special PTS value was used to encode a config packet.

To prepare for adding more flags, use the most significant bits of the
PTS field to store flags.
2022-02-11 21:32:11 +01:00
29c163959c Indent ifdef for clarity
Make it explicit that the ifdef is an inner block.
2022-02-10 09:24:19 +01:00
4a95c08d56 Improve error message for unsupported usb hotplug 2022-02-10 08:54:43 +01:00
7848a387c8 Do not duplicate relative mouse mode state
The relative mouse mode is tracked by SDL, and accessible via
SDL_GetRelativeMouseMode().

This is more robust in case SDL changes the relative mouse mode on its
own.
2022-02-10 08:50:18 +01:00
43ae418752 Fix USB device leak on connection error
If sc_usb_connect() failed, then the sc_usb_device was never destroyed.

The assignment was mistakenly removed by commit
61969aeb80.
2022-02-10 08:47:39 +01:00
8d583d36e2 Move prebuilt-deps/ to app/
The prebuilt dependencies are specific to the client app (not the
server).

This also avoids to reference the parent directory (../) from
app/meson.build.
2022-02-09 23:18:34 +01:00
8498a2e8a6 Reorder release.mk recipes
Group prepare-deps for win32 and win64.
2022-02-09 23:01:49 +01:00
c00a31f1b0 Pass --buildtype=release as a single meson arg
For consistency with the other arguments
2022-02-09 18:16:34 +01:00
f86df817f9 Print libusb version on --version 2022-02-09 10:15:19 +01:00
9a546ef1af Print both compiled and linked versions of libs
On --version, print both the version scrcpy had been compiled against,
and the version linked at runtime.
2022-02-09 10:15:19 +01:00
9477594f80 Move version handling to a separate file
This will avoid to include all dependencies headers from main.c.
2022-02-09 10:15:07 +01:00
29828aa330 Log device opening errors during listing
Without this log, the user would have no way to know that a USB device
is rejected because it could not be opened (typically due to
insufficient permissions).
2022-02-09 10:04:09 +01:00
dc5276b0e1 Mention --select-usb and --select-tcpip in README
PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 10:00:25 +01:00
582161607e Add option to select USB or TCP/IP devices
If several devices are connected (as listed by `adb devices`), it was
necessary to provide the explicit serial via -s/--serial.

If only one device is connected via USB (respectively, via TCP/IP), it
might be convenient to select it automatically. For this purpose, two
new options are introduced:
 - -d/--select-usb: select the single device connected over USB
 - -e/--select-tcpip: select the single device connected over TCP/IP

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 10:00:25 +01:00
146f65d7b2 Introduce adb device selector
Currently, a device is selected either from a specific serial, or if it
is the only one connected.

In order to support selecting the only device connected via USB or via
TCP/IP separately, introduce a new selection structure.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 10:00:25 +01:00
5ed13ef477 Execute adb start-server
This does nothing if the adb daemon is already started, but allows to
print any output/errors to the console.

Otherwise, the daemon starting would occur during `adb devices`, which
does not output to the console because the result is parsed.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 10:00:14 +01:00
9c545e8c29 Remove sc_adb_get_serialno()
The device serial is now retrieved from `adb devices -l`, `adb
get-serialno` is not called anymore.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:56:26 +01:00
0a619dc9ef Allow selecting a device from IP without port
Since the previous commit, if a serial is given via -s/--serial (either
a real USB serial or an IP:port), a device is selected if its serial
matches exactly.

In addition, if the user pass an IP without a port, then select any
device with this IP, regardless of the port (so that "192.168.1.1"
matches any "192.168.1.1:port"). This is also the default behavior of
adb.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:56:26 +01:00
4692d13179 Expose simple API to select a single adb device
Select an adb device from the output of `adb device -l`.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:56:26 +01:00
02d46b2262 Expose function to test if a serial is TCP/IP
In practice, it just tests if the serial contains a ':', which is
sufficient to distinguish ip:port from a real USB serial.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:56:25 +01:00
4389de1c23 Add adb devices parser
Add a parser of `adb device -l` output, to extract a list of devices
with their serial, state and model.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:56:25 +01:00
85ff70fc95 Refactor device configuration
Depending on the parameters passed to scrcpy, either the initial device
serial is necessary or not. Reorganize to simplify the logic.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:55:25 +01:00
700503df6c List and select USB devices separately
List all USB devices in a first step, then select the matching one(s).

This allows to report a user-friendly log message containing the list of
devices, with the matching one(s) highlighted.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:55:25 +01:00
61969aeb80 Expose simple API to select a single USB device
The caller just wants a single device. Handle all cases and error
messages internally.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:55:25 +01:00
b88c4aa75e Add move-function for sc_usb_device
Add a function to "move" a sc_usb_device into another instance.

This will avoid unnecessary copies.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:55:23 +01:00
8c50342fb2 Move SC_PRIsizet to compat.h
Define the printf format macro for size_t in compat.h so that it can be
used from anywhere.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:53:19 +01:00
0eadf95a3e Rename function to destroy a list of USB devices
Rename from "usb_device_" to "usb_devices_".

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:53:19 +01:00
6df2205cf3 Add generic LOG() macro with level parameter
One log macro was provided for each log level (LOGV(), LOGD(), LOGI(),
LOGW(), LOGE()).

Add a generic macro LOG(LEVEL, ...) accepting a log level as parameter,
so that it is possible to write logging wrappers.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
2022-02-09 09:53:11 +01:00
61b6324ee9 Remove LOGC()
It is not clear when to use LOGC() rather than LOGE(). Always use
LOGE().

Moreover, enum sc_log_level has no "critical" log level.
2022-02-09 09:52:15 +01:00
f20137d2ac Improve USB device open log
For consistency with "List USB devices", log "Open USB device".
2022-02-07 13:10:53 +01:00
b60809a4da Inline USB device opening
Such a separate function was useless.
2022-02-07 13:03:23 +01:00
b0e04aa327 Remove log_libusb_error()
This helper did not help a lot, and prevented the client to choose the
log level and the prefix error message.
2022-02-07 12:56:25 +01:00
137d2c9791 Remove confusing sc_str_truncate()
This util function was error-prone:
 - it accepted a buffer as parameter (not necessarily a NUL-terminated
   string) and its length (including the NUL char, if any);
 - it wrote '\0' over the last character of the buffer, so the last
   character was lost if the buffer was not a NUL-terminated string, and
   even worse, it caused undefined behavior if the length was empty;
 - it returned the length of the resulting NUL-terminated string,
   which was inconsistent with the input buffer length.

In addition, it was not necessarily optimal:
 - it wrote '\0' twice;
 - it required to know the buffer length, that is the input string
   length + 1, in advance.

Remove this function, and let the client use strcspn() manually.
2022-02-06 14:39:51 +01:00
6d41c53b61 Fix adb connect parsing
The function assumed that the raw output of "adb connect" was a
NUL-terminated string, but it is not the case.

It this output did not end with a space or a new line character, then
sc_str_truncate() would write '\0' over the last character. Even worse,
if the output was empty, then sc_str_truncate() would write
out-of-bounds.

Avoid the error-prone sc_str_truncate() util function.
2022-02-06 14:31:32 +01:00
8d540e83c7 Fix adb get-serialno parsing
The function assumed that the raw output of "adb get-serialno" was a
NUL-terminated string, but it is not the case.

It this output did not end with a space or a new line character, then
sc_str_truncate() would write '\0' over the last character. Even worse,
if the output was empty, then sc_str_truncate() would write
out-of-bounds.

Avoid the error-prone sc_str_truncate() util function.
2022-02-06 14:30:19 +01:00
2ea12f73db Fix adb getprop parsing
The function assumed that the raw output of "adb getprop" was a
NUL-terminated string, but it is not the case.

It this output did not end with a space or a new line character, then
sc_str_truncate() would write '\0' over the last character. Even worse,
if the output was empty, then sc_str_truncate() would write
out-of-bounds.

Avoid the error-prone sc_str_truncate() util function.
2022-02-06 14:30:13 +01:00
5d6bd8f9cd Fix adb device ip parsing
The parser assumed that its input was a NUL-terminated string, but it
was not the case: it is just the raw output of "adb devices ip route".

In practice, it was harmless, since the output always ended with '\n'
(which was replaced by '\0' on truncation), but it was incorrect
nonetheless.

Always write a '\0' at the end of the buffer, and explicitly parse as a
NUL-terminated string. For that purpose, avoid the error-prone
sc_str_truncate() util function.
2022-02-06 14:30:07 +01:00
5b3ae2cb2f Store actual serial in sc_server
Before starting the server, the actual device serial (possibly its
ip:port if it's over TCP/IP) must be known.

A serial might be requested via -s/--serial (stored in the
sc_server_params), but the actual serial may change afterwards:
 - if none is provided, then it is retrieved with "adb get-serialno";
 - if --tcpip is requested, then the final serial will be the target
   ip:port.

The requested serial was overwritten by the actual serial in the
sc_server_params struct, which was a bit hacky.

Instead, store a separate serial field in sc_server (and rename the one
from sc_server_params to "req_serial" to avoid confusion).
2022-02-05 11:17:45 +01:00
08f16a9dde Simplify switch to TCPIP function
Do not use an output parameter to return the value. Instead, return the
actual ip:port string on success or NULL on error.
2022-02-05 10:56:58 +01:00
386cf7d7ac Build adb argv statically
Now that providing a serial is mandatory for adb commands where it is
relevant, the whole argv array may be built statically, without
allocations at runtime.
2022-02-05 10:56:58 +01:00
5e2bfccab4 Expose adb executable path publicly
This will allow the caller to build the argv array directly.
2022-02-05 10:56:58 +01:00
ba30ca5c1e Rename adb_command to adb_executable
Semantically, a "command" refers to the whole command line argv (adb and
its arguments).
2022-02-05 10:56:58 +01:00
028e7afe32 Assert non-NULL serial
If no serial is passed, then the command would work if there is exactly
one device connected, but will fail with multiple devices.

To avoid such cases, ensure that a serial is always provided.
2022-02-05 10:56:58 +01:00
6ca9825c0f Assert "adb disconnect" is called with an argument
Calling "adb disconnect" without argument would disconnect every TCP/IP
device. We must make sure scrcpy never does that.
2022-02-05 10:56:21 +01:00
f807131c0a Remove useless undef
The #define was removed in 1c71bd16be.
2022-02-05 10:09:24 +01:00
21106bd70a Remove screensaver log
If --disable-screensaver is passed, then screensaver is disabled,
otherwise it is enabled. No need for a log.
2022-02-04 09:12:28 +01:00
bd3c93ae3d Remove platform-tools installation suggestion
On Windows, adb is provided in the release archive. Most missing adb
issues come from users setting the ADB environment variable to an
incorrect value (on all platforms).

Suggesting to install platform-tools to solve the problem will just make
things worse (there will be one more adb in yet another location).
2022-02-04 08:39:10 +01:00
9e3902f30c Use sc_ prefix for adb 2022-02-04 08:39:10 +01:00
7810ca61b0 Move ADB code to adb/ 2022-02-04 08:39:10 +01:00
c460243ce2 Simplify demuxer
Call the same push_packet_to_sinks() in all cases, and make
sc_demuxer_parse() return void.
2022-02-02 21:03:55 +01:00
7dec225ceb Rename stream to sc_demuxer
For consistency with recorder and decoder, name the component which
demuxes a "demuxer".

And add the missing sc_ prefix.
2022-02-02 21:03:55 +01:00
4ee62abe1d Use sc_ prefix for recorder 2022-02-02 21:03:55 +01:00
0080d0b0ff Use sc_ prefix for decoder 2022-02-02 21:03:55 +01:00
0bf7e4ddc4 Add missing spaces in help
PR #2994 <https://github.com/Genymobile/scrcpy/pull/2994>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-02-02 12:32:39 +01:00
c0a75ca746 Downscale and retry also on early MediaCodec error
The new retry mechanism with a lower definition only worked if the error
occurred during encode(). For example:

    java.lang.IllegalStateException
        at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
        at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3452)
        at com.genymobile.scrcpy.ScreenEncoder.encode(ScreenEncoder.java:114)
        at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:95)
        at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:61)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:80)
        at com.genymobile.scrcpy.Server.main(Server.java:255)

However, MediaCodec may also fail before encoding, during configure() or
start(). For example:

    android.media.MediaCodec$CodecException: Error 0xfffffc0e
        at android.media.MediaCodec.native_configure(Native Method)
        at android.media.MediaCodec.configure(MediaCodec.java:1956)
        at android.media.MediaCodec.configure(MediaCodec.java:1885)
        at com.genymobile.scrcpy.ScreenEncoder.configure(ScreenEncoder.java:158)
        at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:68)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:28)
        at com.genymobile.scrcpy.Server.main(Server.java:110)

Also downscale and retry in these cases.

Refs #2947 <https://github.com/Genymobile/scrcpy/pull/2947>
Refs #2988 <https://github.com/Genymobile/scrcpy/issues/2988>
PR #2990 <https://github.com/Genymobile/scrcpy/pull/2990>
2022-02-02 08:22:17 +01:00
f02f2135cd Fix include for standard library header 2022-02-01 21:40:15 +01:00
9b4360b6b8 Add warning in function documentation
The function parsing "ip route" output modifies the input buffer to
tokenize in place. This must be mentioned in the function documentation.
2022-02-01 21:39:14 +01:00
c8d0f5cdeb Fix sc_str_truncate() documentation
The function was initially implemented to truncate lines, but was later
generalized to accept custom delimiters. The whole documentation has not
been updated accordingly.

Refs 9619ade706
2022-02-01 21:38:50 +01:00
22d9f0faf4 Fix comment typo 2022-02-01 21:09:02 +01:00
a1967b4dfd Update FAQ.zh-Hans.md to v1.22
PR #2989 <https://github.com/Genymobile/scrcpy/pull/2989>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-01-30 16:56:18 +01:00
a86deab3d4 Update README.zh-Hans.md to v1.22
PR #2989 <https://github.com/Genymobile/scrcpy/pull/2989>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-01-30 16:55:45 +01:00
f4c7044b46 Update links to v1.22 2022-01-29 16:16:41 +01:00
64a09513ae Bump version to 1.22 2022-01-29 15:44:52 +01:00
28054cd471 Merge branch 'master' into dev 2022-01-29 15:40:48 +01:00
334f46995a Update README.zh-Hans.md to v1.21
PR #2978 <https://github.com/Genymobile/scrcpy/pull/2978>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-01-29 15:10:05 +01:00
38cdcdda50 Improve prebuilt system
This aims to fix two issues with the previous implementation:
 1. the whole content of downloaded archives were extracted, while only
    few files are necessary;
 2. the archives were extracted in the prebuild-deps/ directory as is.

As a consequence of (2), the actual directory name relied on the root
directory of the archive. For adb, this root directory was always
"platform-tools", so when bumping the adb version, the target directory
already existed and the dependency was not upgraded (the old one had to
be removed manually).

Expose common function to download a file and check its checksum, but
let the custom script for each dependency extract only the needed files
and reorganize the content if necessary.
2022-01-29 14:40:21 +01:00
eaba613633 Revert "Upgrade platform-tools (32.0.0) for Windows"
This reverts commit c0de365f67.

The new adb.exe crashes.

Refs #2981 comment <https://github.com/Genymobile/scrcpy/issues/2981#issuecomment-1024569778>
2022-01-29 11:46:39 +01:00
b8d7f36ba3 Fix SC_EXIT_CODE_NONE value
The exit code on windows is stored in a DWORD, an unsigned long:
<https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/262627d8-3418-4627-9218-4ffe110850b2>

Use the max value of this type for SC_EXIT_CODE_NONE.
2022-01-29 08:05:59 +01:00
80bec70852 Add helper to log Windows system errors
It will help to log errors returned by GetLastError() or
WSAGetLastError():
 - <https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror>
 - <https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsagetlasterror>
 - <https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes>

Always log the errors in English to be able to read them in bug reports.
2022-01-28 09:15:18 +01:00
5508c635cb Enable mouse focus clickthrough in OTG mode
A single click on the window must both give focus and capture the mouse.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:30 +01:00
ea68a003a2 Make HID keyboard and mouse optional in OTG mode
Allow to only enable HID keyboard or HID mouse:

    scrcpy --otg -K   # keyboard only
    scrcpy --otg -M   # mouse only
    scrcpy --otg -KM  # keyboard and mouse
    scrcpy --otg      # keyboard and mouse

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:27 +01:00
c5be0d6438 Document OTG mode in README
PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:25 +01:00
91418c79ab Add OTG mode
Add an option --otg to run scrcpy with only physical keyboard and mouse
simulation (HID over AOA), without mirroring and without requiring adb.

To avoid adding complexity into the scrcpy initialization and screen
implementation, OTG mode is implemented totally separately, with a
separate window.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:23 +01:00
36aaf70279 Move input event helpers
Input events helpers to convert from SDL events to scrcpy events were
implemented in input_manager. To reuse them for OTG mode, move them to
input_events.h.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:21 +01:00
1a03206e36 Detect USB device disconnection
The device disconnection is detected when the video socket closes.

In order to introduce an OTG mode (HID events) without mirroring (and
without server), we must be able to detect USB device disconnection.

This feature will only be used in OTG mode.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:18 +01:00
37987b822e Make acksync optional for AOA initialization
Acksync is used to delay HID events until some request (in practice,
device clipboard synchronization) is acknowledged by the device.

This mechanism will not be necessary for OTG mode.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:16 +01:00
8fc9dca8cb Make serial optional to find USB devices
If no serial is provided, then list all available USB devices (which can
be open and having a serial).

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:15 +01:00
1c17f57c10 Find a list of devices instead of a single one
Several devices may match the requested serial, but above all, this
paves the way to list all devices (when no serial is provided).

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:13 +01:00
d8b37fe189 Wrap libusb_device
Introduce a structure to wrap a libusb_device and expose its descriptor
data read during discovery.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:11 +01:00
0ee9e2ff51 Expose function to find a USB device
The device was automatically found by sc_usb_connect(). Instead, expose
a function to find a device from a serial, and let the caller connect to
the device found (if any).

This will allow to list all devices first, then select one device to
connect to.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:08 +01:00
1ab3692f3d Add util function to read USB descriptor string
Use it from accept_device() to simplify (at the cost an additional
allocation for each serial, but it is not important).

It will also be useful in other functions in further commits.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:06 +01:00
bbef426a4b Split USB initialization and connection
This will allow to execute other USB calls (retrieving the device list
for example) before connecting to the selected device.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:04 +01:00
2114f48185 Find device with USB context
An explicit context was used everywhere except for listing the devices.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:02 +01:00
b779eca8d3 Remove libusb_device field
It is possible to retrieve the device instance from the handle via
libusb_get_device(), so we don't need to reference the device one more
time.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:36:00 +01:00
adda47b0f7 Move sc_usb out of sc_aoa
This will allow to initialize a USB device separately and pass it to
sc_aoa.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:59 +01:00
48e3ff284f Make serial mandatory for sc_usb
In practice, it is already mandatory.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:57 +01:00
1d6f9952ee Extract USB handling from AOA
The AOA code handled both USB initialization and AOA commands/events.
Extract USB-related code to a separate file and structure.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:55 +01:00
d48d191262 Rename HAVE_AOA_HID to HAVE_USB
The condition actually determines whether scrcpy can use libusb or not.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:48 +01:00
2762f5d183 Move AOA/HID code to usb/
PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:34 +01:00
c996a6d462 Fix socket close race condition
The server needs to interrupt the sockets on stop, but it must not close
them while other threads may attempt to read from or write to them.

In particular, the video_socket is read by the stream thread, and the
control_socket is written by the controller and read by receiver.

Therefore, close the socket only on sc_server_destroy(), which is called
after all other threads are joined.

Reported by TSAN on close:

    WARNING: ThreadSanitizer: data race (pid=3287612)
      Write of size 8 at 0x7ba000000080 by thread T1:
        #0 close ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1690 (libtsan.so.0+0x359d8)
        #1 net_close ../app/src/util/net.c:280 (scrcpy+0x23643)
        #2 run_server ../app/src/server.c:772 (scrcpy+0x20047)
        #3 <null> <null> (libSDL2-2.0.so.0+0x905a0)

      Previous read of size 8 at 0x7ba000000080 by thread T16:
        #0 recv ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:6603 (libtsan.so.0+0x4f4a6)
        #1 net_recv_all ../app/src/util/net.c:228 (scrcpy+0x234a9)
        #2 stream_recv_packet ../app/src/stream.c:33 (scrcpy+0x2045c)
        #3 run_stream ../app/src/stream.c:228 (scrcpy+0x21169)
        #4 <null> <null> (libSDL2-2.0.so.0+0x905a0)

Refs ddb9396743
2022-01-27 23:26:46 +01:00
8ea6fb1f0f Print version on stdout
Refs b25404ee4b
2022-01-27 21:26:56 +01:00
b546c33eff Do not print scrcpy version twice on --version
Refs 6da6d905c2
2022-01-27 21:12:46 +01:00
8615813005 Update README.sp.md to v1.21
PR #2962 <https://github.com/Genymobile/scrcpy/pull/2962>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-01-27 19:14:07 +01:00
f51c53e913 Mention "screen copy" in README
Help users make sense of the app name :)
2022-01-27 19:05:33 +01:00
c0de365f67 Upgrade platform-tools (32.0.0) for Windows
Include the latest version of adb in Windows releases.
2022-01-27 19:02:07 +01:00
34e19dcc57 Upgrade SDL (2.0.20) for Windows
Include the latest version of SDL in Windows releases.
2022-01-27 19:02:07 +01:00
4817cadd09 Fix code style
Align function parameters.
2022-01-27 19:02:07 +01:00
02b5e87802 Slightly reduce lock usage
Locking the frame_buffer mutex to reference the input frame into the
tmp_frame is unnecessary.

This also fixes the missing unlock on error.
2022-01-27 19:01:43 +01:00
8e4d3beb01 Fix return value on adb commands error 2022-01-27 16:47:51 +01:00
50f4f1639c Add a shorcut "open a terminal here" on Windows
On Windows, the file explorer does not provide any "open a terminal
here" shortcut.

Add a bat file which just runs `cmd` to easily open a terminal directly
in the scrcpy directory (so there is no need to `cd C:\path\…`).

PR #2970 <https://github.com/Genymobile/scrcpy/pull/2970>
2022-01-27 16:47:51 +01:00
c8dc1917f4 Do not restore power mode if --no-control
This totally disables deferred cleanup when --no-control is passed.

Refs f289d206ea
2022-01-27 16:47:51 +01:00
9d2e00697e Use sc_ prefix for control_msg enums
Refs afa4a1b728
2022-01-27 16:47:51 +01:00
2faf9715be Add server option raw_video_stream
For convenience, this new option forces the 3 following options:
 - send_device_meta=false
 - send_frame_meta=false
 - send_dummy_byte=false

This allows to send a raw H.264 stream on the video socket.

Concretely:

    adb push scrcpy-server /data/local/tmp/scrcpy-server.jar
    adb forward tcp:1234 localabstract:scrcpy
    adb shell CLASSPATH=/data/local/tmp/scrcpy-server.jar \
        app_process / com.genymobile.scrcpy.Server 1.21 \
        raw_video_stream=true tunnel_forward=true control=false

As soon as a client connects via TCP to localhost:1234, it will receive
the raw H.264 stream.

Refs #1419 comment <https://github.com/Genymobile/scrcpy/pull/1419#issuecomment-1013964650>
PR #2971 <https://github.com/Genymobile/scrcpy/pull/2971>
2022-01-27 16:47:51 +01:00
ae8fdda09e Improve FAQ explanations 2022-01-24 08:55:27 +01:00
1ff69e21c2 Update error messages in FAQ
With the recent versions, scrcpy first executes "adb get-serialno", so
the adb error messages are not exactly the same.
2022-01-24 08:54:09 +01:00
479abc8c77 Reference Windows USB driver for Google devices
PR #2945 <https://github.com/Genymobile/scrcpy/pull/2945>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2022-01-16 17:46:43 +01:00
25a4135935 Mention react-native menu shortcut in README
PR #2879 <https://github.com/Genymobile/scrcpy/pull/2879>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2021-12-18 16:48:44 +01:00
5704ec6967 AUR to official Arch Repository
PR #2844 <https://github.com/Genymobile/scrcpy/pull/2844>

Signed-off-by: Romain Vimont <rom@rom1v.com>
2021-12-02 08:48:47 +01:00
115 changed files with 7068 additions and 2189 deletions

View File

@ -161,7 +161,8 @@ install the required packages:
```bash
# runtime dependencies
pacman -S mingw-w64-x86_64-SDL2 \
mingw-w64-x86_64-ffmpeg
mingw-w64-x86_64-ffmpeg \
mingw-w64-x86_64-libusb
# client build dependencies
pacman -S mingw-w64-x86_64-make \
@ -175,7 +176,8 @@ For a 32 bits version, replace `x86_64` by `i686`:
```bash
# runtime dependencies
pacman -S mingw-w64-i686-SDL2 \
mingw-w64-i686-ffmpeg
mingw-w64-i686-ffmpeg \
mingw-w64-i686-libusb
# client build dependencies
pacman -S mingw-w64-i686-make \
@ -199,7 +201,7 @@ Install the packages with [Homebrew]:
```bash
# runtime dependencies
brew install sdl2 ffmpeg
brew install sdl2 ffmpeg libusb
# client build dependencies
brew install pkg-config meson
@ -258,7 +260,7 @@ set ANDROID_SDK_ROOT=%LOCALAPPDATA%\Android\sdk
Then, build:
```bash
meson x --buildtype release --strip -Db_lto=true
meson x --buildtype=release --strip -Db_lto=true
ninja -Cx # DO NOT RUN AS ROOT
```
@ -270,16 +272,16 @@ install` must be run as root)._
#### Option 2: Use prebuilt server
- [`scrcpy-server-v1.21`][direct-scrcpy-server]
_(SHA-256: dbcccab523ee26796e55ea33652649e4b7af498edae9aa75e4d4d7869c0ab848)_
- [`scrcpy-server-v1.23`][direct-scrcpy-server]
_(SHA-256: 2a913fd47478c0b306fca507cb0beb625e49a19ff9fc7ab904e36ef5b9fe7e68)_
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-server-v1.21
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.23/scrcpy-server-v1.23
Download the prebuilt server somewhere, and specify its path during the Meson
configuration:
```bash
meson x --buildtype release --strip -Db_lto=true \
meson x --buildtype=release --strip -Db_lto=true \
-Dprebuilt_server=/path/to/scrcpy-server
ninja -Cx # DO NOT RUN AS ROOT
```

95
FAQ.md
View File

@ -4,23 +4,16 @@
Here are the common reported problems and their status.
If you encounter any error, the first step is to upgrade to the latest version.
## `adb` issues
`scrcpy` execute `adb` commands to initialize the connection with the device. If
`adb` fails, then scrcpy will not work.
In that case, it will print this error:
> ERROR: "adb push" returned with value 1
This is typically not a bug in _scrcpy_, but a problem in your environment.
To find out the cause, execute:
```bash
adb devices
```
### `adb` not found
@ -30,38 +23,63 @@ On Windows, the current directory is in your `PATH`, and `adb.exe` is included
in the release, so it should work out-of-the-box.
### Device unauthorized
Check [stackoverflow][device-unauthorized].
[device-unauthorized]: https://stackoverflow.com/questions/23081263/adb-android-device-unauthorized
### Device not detected
> adb: error: failed to get feature set: no devices/emulators found
> ERROR: Could not find any ADB device
Check that you correctly enabled [adb debugging][enable-adb].
If your device is not detected, you may need some [drivers] (on Windows).
Your device must be detected by `adb`:
```
adb devices
```
If your device is not detected, you may need some [drivers] (on Windows). There is a separate [USB driver for Google devices][google-usb-driver].
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
[drivers]: https://developer.android.com/studio/run/oem-usb.html
[google-usb-driver]: https://developer.android.com/studio/run/win-usb
### Device unauthorized
> ERROR: Device is unauthorized:
> ERROR: --> (usb) 0123456789abcdef unauthorized
> ERROR: A popup should open on the device to request authorization.
When connecting, a popup should open on the device. You must authorize USB
debugging.
If it does not open, check [stackoverflow][device-unauthorized].
[device-unauthorized]: https://stackoverflow.com/questions/23081263/adb-android-device-unauthorized
### Several devices connected
If several devices are connected, you will encounter this error:
> adb: error: failed to get feature set: more than one device/emulator
ERROR: Multiple (2) ADB devices:
ERROR: --> (usb) 0123456789abcdef device Nexus_5
ERROR: --> (tcpip) 192.168.1.5:5555 device GM1913
ERROR: Select a device via -s (--serial), -d (--select-usb) or -e (--select-tcpip)
the identifier of the device you want to mirror must be provided:
In that case, you can either provide the identifier of the device you want to
mirror:
```bash
scrcpy -s 01234567890abcdef
scrcpy -s 0123456789abcdef
```
Note that if your device is connected over TCP/IP, you'll get this message:
Or request the single USB (or TCP/IP) device:
```bash
scrcpy -d # USB device
scrcpy -e # TCP/IP device
```
Note that if your device is connected over TCP/IP, you might get this message:
> adb: error: more than one device/emulator
> ERROR: "adb reverse" returned with value 1
@ -140,22 +158,24 @@ screen, then you might get poor quality, especially visible on text (see [#40]).
[#40]: https://github.com/Genymobile/scrcpy/issues/40
To improve downscaling quality, trilinear filtering is enabled automatically
if the renderer is OpenGL and if it supports mipmapping.
This problem should be fixed in scrcpy v1.22: **update to the latest version**.
On Windows, you might want to force OpenGL:
```
scrcpy --render-driver=opengl
```
You may also need to configure the [scaling behavior]:
On older versions, you must configure the [scaling behavior]:
> `scrcpy.exe` > Properties > Compatibility > Change high DPI settings >
> Override high DPI scaling behavior > Scaling performed by: _Application_.
[scaling behavior]: https://github.com/Genymobile/scrcpy/issues/40#issuecomment-424466723
Also, to improve downscaling quality, trilinear filtering is enabled
automatically if the renderer is OpenGL and if it supports mipmapping.
On Windows, you might want to force OpenGL to enable mipmapping:
```
scrcpy --render-driver=opengl
```
### Issue with Wayland
@ -248,8 +268,15 @@ Caused by: java.lang.IllegalArgumentException: displayToken must not be null
## Command line on Windows
Some Windows users are not familiar with the command line. Here is how to open a
terminal and run `scrcpy` with arguments:
Since v1.22, a "shortcut" has been added to directly open a terminal in the
scrcpy directory. Double-click on `open_a_terminal_here.bat`, then type your
command. For example:
```
scrcpy --record file.mkv
```
You could also open a terminal and go to the scrcpy folder manually:
1. Press <kbd>Windows</kbd>+<kbd>r</kbd>, this opens a dialog box.
2. Type `cmd` and press <kbd>Enter</kbd>, this opens a terminal.
@ -288,4 +315,4 @@ This FAQ is available in other languages:
- [Italiano (Italiano, `it`) - v1.19](FAQ.it.md)
- [한국어 (Korean, `ko`) - v1.11](FAQ.ko.md)
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.18](FAQ.zh-Hans.md)
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.22](FAQ.zh-Hans.md)

View File

@ -1,7 +1,12 @@
只有原版的[FAQ](FAQ.md)会保持更新。
本文根据[d6aaa5]翻译。
_Only the original [FAQ.md](FAQ.md) is guaranteed to be up-to-date._
[d6aaa5]:https://github.com/Genymobile/scrcpy/blob/d6aaa5bf9aa3710660c683b6e3e0ed971ee44af5/FAQ.md
_只有原版的 [FAQ.md](FAQ.md)是保证最新的。_
Current version is based on [28054cd]
本文根据[28054cd]进行翻译。
[28054cd]: https://github.com/Genymobile/scrcpy/blob/28054cd471f848733e11372c9d745cd5d71e6ce7/FAQ.md
# 常见问题
@ -9,11 +14,11 @@
## `adb` 相关问题
`scrcpy` 执行 `adb` 命令来初始化和设备之间的连接。如果`adb` 执行失败了, scrcpy 就无法工作。
`scrcpy` 执行 `adb` 命令来初始化和设备之间的连接。如果 `adb` 执行失败了, scrcpy 就无法工作。
在这种情况中,将会输出这个错误:
> ERROR: "adb push" returned with value 1
> ERROR: "adb get-serialno" returned with value 1
这通常不是 _scrcpy_ 的bug而是你的环境的问题。
@ -33,28 +38,37 @@ adb devices
### 设备未授权
参见这里 [stackoverflow][device-unauthorized].
> error: device unauthorized.
> This adb server's $ADB_VENDOR_KEYS is not set
> Try 'adb kill-server' if that seems wrong.
> Otherwise check for a confirmation dialog on your device.
连接时,在设备上应该会打开一个弹出窗口。 您必须授权 USB 调试。
如果没有打开,参见[stackoverflow][device-unauthorized].
[device-unauthorized]: https://stackoverflow.com/questions/23081263/adb-android-device-unauthorized
### 未检测到设备
> adb: error: failed to get feature set: no devices/emulators found
> error: no devices/emulators found
确认已经正确启用 [adb debugging][enable-adb].
如果你的设备没有被检测到,你可能需要一些[驱动][drivers] (在 Windows上).
如果你的设备没有被检测到,你可能需要一些[驱动][drivers] (在 Windows上)。这里有一个单独的 [适用于Google设备的USB驱动][google-usb-driver].
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
[drivers]: https://developer.android.com/studio/run/oem-usb.html
[google-usb-driver]: https://developer.android.com/studio/run/win-usb
### 已连接多个设备
如果连接了多个设备,您将遇到以下错误:
> adb: error: failed to get feature set: more than one device/emulator
> error: more than one device/emulator
必须提供要镜像的设备的标识符:
@ -90,19 +104,19 @@ scrcpy
### 设备断开连接
如果 _scrcpy_ 在警告“设备连接断开”的情况下自动中止,那就意味着`adb`连接已经断开了。
请尝试使用另一条USB线或者电脑上的另一个USB接口。
请参看 [#281] 和 [#283]。
请尝试使用另一条USB线或者电脑上的另一个USB接口。请参看 [#281] 和 [#283]。
[#281]: https://github.com/Genymobile/scrcpy/issues/281
[#283]: https://github.com/Genymobile/scrcpy/issues/283
## 控制相关问题
### 鼠标和键盘不起作用
在某些设备上,您可能需要启用一个选项以允许 [模拟输入][simulating input]。
在开发者选项中,打开:
> **USB调试 (安全设置)**
@ -115,10 +129,12 @@ scrcpy
可输入的文本[被限制为ASCII字符][text-input]。也可以用一些小技巧输入一些[带重音符号的字符][accented-characters],但是仅此而已。参见[#37]。
自 Linux 上的 scrcpy v1.20 之后,可以模拟[物理键盘][hid] (HID)。
[text-input]: https://github.com/Genymobile/scrcpy/issues?q=is%3Aopen+is%3Aissue+label%3Aunicode
[accented-characters]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-accented-characters
[#37]: https://github.com/Genymobile/scrcpy/issues/37
[hid]: README.md#physical-keyboard-simulation-hid
## 客户端相关问题
@ -129,7 +145,6 @@ scrcpy
[#40]: https://github.com/Genymobile/scrcpy/issues/40
为了提升降尺度的质量如果渲染器是OpenGL并且支持mip映射就会自动开启三线性过滤。
在Windows上你可能希望强制使用OpenGL
@ -177,6 +192,7 @@ scrcpy
## 崩溃
### 异常
可能有很多原因。一个常见的原因是您的设备无法按给定清晰度进行编码:
> ```
@ -204,12 +220,40 @@ scrcpy -m 1024
scrcpy -m 800
```
自 scrcpy v1.22以来scrcpy 会自动在失败前以更低的分辨率重试。这种行为可以用`--no-downsize-on-error`关闭。
你也可以尝试另一种 [编码器](README.md#encoder)。
如果您在 Android 12 上遇到此异常,则只需升级到 scrcpy >= 1.18 (见 [#2129])
```
> ERROR: Exception on thread Thread[main,5,main]
java.lang.AssertionError: java.lang.reflect.InvocationTargetException
at com.genymobile.scrcpy.wrappers.SurfaceControl.setDisplaySurface(SurfaceControl.java:75)
...
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.genymobile.scrcpy.wrappers.SurfaceControl.setDisplaySurface(SurfaceControl.java:73)
... 7 more
Caused by: java.lang.IllegalArgumentException: displayToken must not be null
at android.view.SurfaceControl$Transaction.setDisplaySurface(SurfaceControl.java:3067)
at android.view.SurfaceControl.setDisplaySurface(SurfaceControl.java:2147)
... 9 more
```
[#2129]: https://github.com/Genymobile/scrcpy/issues/2129
## Windows命令行
一些Windows用户不熟悉命令行。以下是如何打开终端并带参数执行`scrcpy`
从 v1.22 开始,增加了一个“快捷方式”,可以直接在 scrcpy 目录打开一个终端。双击`open_a_terminal_here.bat`,然后输入你的命令。 例如
```
scrcpy --record file.mkv
```
您也可以打开终端并手动转到 scrcpy 文件夹:
1. 按下 <kbd>Windows</kbd>+<kbd>r</kbd>,打开一个对话框。
2. 输入 `cmd` 并按 <kbd>Enter</kbd>,这样就打开了一个终端。
@ -233,7 +277,7 @@ scrcpy -m 800
scrcpy --prefer-text --turn-screen-off --stay-awake
```
然后双击刚刚创建的文件。
然后只需双击刚刚创建的文件。
你也可以编辑 `scrcpy-console.bat` 或者 `scrcpy-noconsole.vbs`(的副本)来添加参数。

1016
README.de.md Normal file

File diff suppressed because it is too large Load Diff

148
README.md
View File

@ -1,6 +1,8 @@
# scrcpy (v1.21)
# scrcpy (v1.23)
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
<img src="app/data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
_pronounced "**scr**een **c**o**py**"_
[Read in another language](#translations)
@ -30,9 +32,8 @@ Its features include:
- [configurable quality](#capture-configuration)
- device screen [as a webcam (V4L2)](#v4l2loopback) (Linux-only)
- [physical keyboard simulation (HID)](#physical-keyboard-simulation-hid)
(Linux-only)
- [physical mouse simulation (HID)](#physical-mouse-simulation-hid)
(Linux-only)
- [OTG mode](#otg)
- and more…
## Requirements
@ -73,6 +74,12 @@ On Debian and Ubuntu:
apt install scrcpy
```
On Arch Linux:
```
pacman -S scrcpy
```
A [Snap] package is available: [`scrcpy`][snap-link].
[snap-link]: https://snapstats.org/snaps/scrcpy
@ -84,10 +91,6 @@ For Fedora, a [COPR] package is available: [`scrcpy`][copr-link].
[COPR]: https://fedoraproject.org/wiki/Category:Copr
[copr-link]: https://copr.fedorainfracloud.org/coprs/zeno/scrcpy/
For Arch Linux, an [AUR] package is available: [`scrcpy`][aur-link].
[AUR]: https://wiki.archlinux.org/index.php/Arch_User_Repository
[aur-link]: https://aur.archlinux.org/packages/scrcpy/
For Gentoo, an [Ebuild] is available: [`scrcpy/`][ebuild-link].
@ -103,10 +106,10 @@ process][BUILD_simple]).
For Windows, for simplicity, a prebuilt archive with all the dependencies
(including `adb`) is available:
- [`scrcpy-win64-v1.21.zip`][direct-win64]
_(SHA-256: fdab0c1421353b592a9bbcebd6e252675eadccca65cca8105686feaa9c1ded53)_
- [`scrcpy-win64-v1.23.zip`][direct-win64]
_(SHA-256: d2f601b1d0157faf65153d8a093d827fd65aec5d5842d677ac86fb2b5b7704cc)_
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-win64-v1.21.zip
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.23/scrcpy-win64-v1.23.zip
It is also available in [Chocolatey]:
@ -210,6 +213,15 @@ scrcpy --max-fps 15
This is officially supported since Android 10, but may work on earlier versions.
The actual capture framerate may be printed to the console:
```
scrcpy --print-fps
```
It may also be enabled or disabled at any time with <kbd>MOD</kbd>+<kbd>i</kbd>.
#### Crop
The device screen may be cropped to mirror only part of the screen.
@ -392,18 +404,30 @@ connect to the device before starting.
Alternatively, it is possible to enable the TCP/IP connection manually using
`adb`:
1. Connect the device to the same Wi-Fi as your computer.
2. Get your device IP address, in Settings → About phone → Status, or by
1. Plug the device into a USB port on your computer.
2. Connect the device to the same Wi-Fi network as your computer.
3. Get your device IP address, in Settings → About phone → Status, or by
executing this command:
```bash
adb shell ip route | awk '{print $9}'
```
3. Enable adb over TCP/IP on your device: `adb tcpip 5555`.
4. Unplug your device.
5. Connect to your device: `adb connect DEVICE_IP:5555` _(replace `DEVICE_IP`)_.
6. Run `scrcpy` as usual.
4. Enable adb over TCP/IP on your device: `adb tcpip 5555`.
5. Unplug your device.
6. Connect to your device: `adb connect DEVICE_IP:5555` _(replace `DEVICE_IP`
with the device IP address you found)_.
7. Run `scrcpy` as usual.
Since Android 11, a [Wireless debugging option][adb-wireless] allows to bypass
having to physically connect your device directly to your computer.
[adb-wireless]: https://developer.android.com/studio/command-line/adb#connect-to-a-device-over-wi-fi-android-11+
If the connection randomly drops, run your `scrcpy` command to reconnect. If it
says there are no devices/emulators found, try running `adb connect
DEVICE_IP:5555` again, and then `scrcpy` as usual. If it still says there are
none found, try running `adb disconnect` and then run those two commands again.
It may be useful to decrease the bit-rate and the definition:
@ -417,7 +441,7 @@ scrcpy -b2M -m800 # short version
#### Multi-devices
If several devices are listed in `adb devices`, you must specify the _serial_:
If several devices are listed in `adb devices`, you can specify the _serial_:
```bash
scrcpy --serial 0123456789abcdef
@ -431,6 +455,19 @@ scrcpy --serial 192.168.0.1:5555
scrcpy -s 192.168.0.1:5555 # short version
```
If only one device is connected via either USB or TCP/IP, it is possible to
select it automatically:
```bash
# Select the only device connected via USB
scrcpy -d # like adb -d
scrcpy --select-usb # long version
# Select the only device connected via TCP/IP
scrcpy -e # like adb -e
scrcpy --select-tcpip # long version
```
You can start several instances of _scrcpy_ for several devices.
#### Autostart on device connection
@ -780,14 +817,17 @@ a location inverted through the center of the screen.
By default, scrcpy uses Android key or text injection: it works everywhere, but
is limited to ASCII.
On Linux, scrcpy can simulate a physical USB keyboard on Android to provide a
better input experience (using [USB HID over AOAv2][hid-aoav2]): the virtual
Alternatively, scrcpy can simulate a physical USB keyboard on Android to provide
a better input experience (using [USB HID over AOAv2][hid-aoav2]): the virtual
keyboard is disabled and it works for all characters and IME.
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
However, it only works if the device is connected by USB, and is currently only
supported on Linux.
However, it only works if the device is connected by USB.
Note: On Windows, it may only work in [OTG mode](#otg), not while mirroring (it
is not possible to open a USB device if it is already open by another process
like the adb daemon).
To enable this mode:
@ -820,8 +860,7 @@ a physical keyboard is connected).
#### Physical mouse simulation (HID)
Similarly to the physical keyboard simulation, it is possible to simulate a
physical mouse. Likewise, it only works if the device is connected by USB, and
is currently only supported on Linux.
physical mouse. Likewise, it only works if the device is connected by USB.
By default, scrcpy uses Android mouse events injection, using absolute
coordinates. By simulating a physical mouse, a mouse pointer appears on the
@ -847,6 +886,36 @@ Special capture keys, either <kbd>Alt</kbd> or <kbd>Super</kbd>, toggle
the mouse back to the computer.
#### OTG
It is possible to run _scrcpy_ with only physical keyboard and mouse simulation
(HID), as if the computer keyboard and mouse were plugged directly to the device
via an OTG cable.
In this mode, _adb_ (USB debugging) is not necessary, and mirroring is disabled.
To enable OTG mode:
```bash
scrcpy --otg
# Pass the serial if several USB devices are available
scrcpy --otg -s 0123456789abcdef
```
It is possible to enable only HID keyboard or HID mouse:
```bash
scrcpy --otg --hid-keyboard # keyboard only
scrcpy --otg --hid-mouse # mouse only
scrcpy --otg --hid-keyboard --hid-mouse # keyboard and mouse
# for convenience, enable both by default
scrcpy --otg # keyboard and mouse
```
Like `--hid-keyboard` and `--hid-mouse`, it only works if the device is
connected by USB.
#### Text injection preference
There are two kinds of [events][textevents] generated when typing text:
@ -967,7 +1036,7 @@ _<kbd>[Super]</kbd> is typically the <kbd>Windows</kbd> or <kbd>Cmd</kbd> key._
| Click on `HOME` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _Middle-click_
| Click on `BACK` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _Right-click²_
| Click on `APP_SWITCH` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _4th-click³_
| Click on `MENU` (unlock screen) | <kbd>MOD</kbd>+<kbd>m</kbd>
| Click on `MENU` (unlock screen) | <kbd>MOD</kbd>+<kbd>m</kbd>
| Click on `VOLUME_UP` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(up)_
| Click on `VOLUME_DOWN` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(down)_
| Click on `POWER` | <kbd>MOD</kbd>+<kbd>p</kbd>
@ -978,9 +1047,9 @@ _<kbd>[Super]</kbd> is typically the <kbd>Windows</kbd> or <kbd>Cmd</kbd> key._
| Expand notification panel | <kbd>MOD</kbd>+<kbd>n</kbd> \| _5th-click³_
| Expand settings panel | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Double-5th-click³_
| Collapse panels | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
| Copy to clipboard | <kbd>MOD</kbd>+<kbd>c</kbd>
| Cut to clipboard | <kbd>MOD</kbd>+<kbd>x</kbd>
| Synchronize clipboards and paste | <kbd>MOD</kbd>+<kbd>v</kbd>
| Copy to clipboard | <kbd>MOD</kbd>+<kbd>c</kbd>
| Cut to clipboard | <kbd>MOD</kbd>+<kbd>x</kbd>
| Synchronize clipboards and paste | <kbd>MOD</kbd>+<kbd>v</kbd>
| Inject computer clipboard text | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
| Enable/disable FPS counter (on stdout) | <kbd>MOD</kbd>+<kbd>i</kbd>
| Pinch-to-zoom | <kbd>Ctrl</kbd>+_click-and-move_
@ -990,7 +1059,8 @@ _<kbd>[Super]</kbd> is typically the <kbd>Windows</kbd> or <kbd>Cmd</kbd> key._
_¹Double-click on black borders to remove them._
_²Right-click turns the screen on if it was off, presses BACK otherwise._
_³4th and 5th mouse buttons, if your mouse has them._
_⁴Only on Android >= 7._
_⁴For react-native apps in development, `MENU` triggers development menu._
_⁵Only on Android >= 7._
Shortcuts with repeated keys are executted by releasing and pressing the key a
second time. For example, to execute "Expand settings panel":
@ -1035,7 +1105,9 @@ See [BUILD].
## Common issues
See the [FAQ](FAQ.md).
See the [FAQ].md).
[FAQ]: FAQ.md
## Developers
@ -1070,17 +1142,29 @@ Read the [developers page].
[article-intro]: https://blog.rom1v.com/2018/03/introducing-scrcpy/
[article-tcpip]: https://www.genymotion.com/blog/open-source-project-scrcpy-now-works-wirelessly/
## Contact
If you encounter a bug, please read the [FAQ] first, then open an [issue].
[issue]: https://github.com/Genymobile/scrcpy/issues
For general questions or discussions, you could also use:
- Reddit: [`r/scrcpy`](https://www.reddit.com/r/scrcpy)
- Twitter: [`@scrcpy_app`](https://twitter.com/scrcpy_app)
## Translations
This README is available in other languages:
- [Deutsch (German, `de`) - v1.22](README.de.md)
- [Indonesian (Indonesia, `id`) - v1.16](README.id.md)
- [Italiano (Italiano, `it`) - v1.19](README.it.md)
- [日本語 (Japanese, `jp`) - v1.19](README.jp.md)
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
- [Español (Spanish, `sp`) - v1.17](README.sp.md)
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.20](README.zh-Hans.md)
- [Español (Spanish, `sp`) - v1.21](README.sp.md)
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.22](README.zh-Hans.md)
- [繁體中文 (Traditional Chinese, `zh-Hant`) - v1.15](README.zh-Hant.md)
- [Turkish (Turkish, `tr`) - v1.18](README.tr.md)

View File

@ -1,24 +1,36 @@
Solo se garantiza que el archivo [README](README.md) original esté actualizado.
# scrcpy (v1.17)
# scrcpy (v1.21)
Esta aplicación proporciona imagen y control de un dispositivo Android conectado
por USB (o [por TCP/IP][article-tcpip]). No requiere acceso _root_.
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
Esta aplicación proporciona control e imagen de un dispositivo Android conectado
por USB (o [por TCP/IP](#conexión)). No requiere acceso _root_.
Compatible con _GNU/Linux_, _Windows_ y _macOS_.
![screenshot](assets/screenshot-debian-600.jpg)
Sus características principales son:
- **ligero** (nativo, solo muestra la imagen del dispositivo)
- **desempeño** (30~60fps)
- **calidad** (1920×1080 o superior)
- **baja latencia** ([35~70ms][lowlatency])
- **corto tiempo de inicio** (~1 segundo para mostrar la primera imagen)
- **no intrusivo** (no se deja nada instalado en el dispositivo)
Se enfoca en:
- **ser ligera**: aplicación nativa, solo muestra la imagen del dispositivo
- **rendimiento**: 30~120fps, dependiendo del dispositivo
- **calidad**: 1920×1080 o superior
- **baja latencia**: [35~70ms][lowlatency]
- **inicio rápido**: ~1 segundo para mostrar la primera imagen
- **no intrusivo**: no deja nada instalado en el dispositivo
- **beneficios**: sin cuentas, sin anuncios, no requiere acceso a internet
- **libertad**: software gratis y de código abierto
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
Con la aplicación puede:
- [grabar la pantalla](#capturas-y-grabaciones)
- duplicar la imagen con [la pantalla apagada](#apagar-la-pantalla)
- [copiar y pegar](#copiar-y-pegar) en ambos sentidos
- [configurar la calidad](#configuración-de-captura)
- usar la pantalla del dispositivo [como webcam (V4L2)](#v4l2loopback) (solo en Linux)
- [emular un teclado físico (HID)](#emular-teclado-físico-hid)
(solo en Linux)
- y mucho más…
## Requisitos
@ -51,7 +63,7 @@ Construir desde la fuente: [BUILD] ([proceso simplificado][BUILD_simple])
### Linux
En Debian (_test_ y _sid_ por ahora) y Ubuntu (20.04):
En Debian y Ubuntu:
```
apt install scrcpy
@ -125,7 +137,7 @@ Necesitarás `adb`, accesible desde `PATH`. Si aún no lo tienes:
brew install android-platform-tools
```
También está disponible en [MacPorts], que configura el adb automáticamente:
También está disponible en [MacPorts], que configura el adb automáticamente:
```bash
sudo port install scrcpy
@ -153,7 +165,7 @@ scrcpy --help
## Características
### Capturar configuración
### Configuración de captura
#### Reducir la definición
@ -208,10 +220,11 @@ Si `--max-size` también está especificado, el cambio de tamaño es aplicado de
Para fijar la rotación de la transmisión:
```bash
scrcpy --lock-video-orientation 0 # orientación normal
scrcpy --lock-video-orientation 1 # 90° contrarreloj
scrcpy --lock-video-orientation 2 # 180°
scrcpy --lock-video-orientation 3 # 90° sentido de las agujas del reloj
scrcpy --lock-video-orientation # orientación inicial
scrcpy --lock-video-orientation=0 # orientación normal
scrcpy --lock-video-orientation=1 # 90° contrarreloj
scrcpy --lock-video-orientation=2 # 180°
scrcpy --lock-video-orientation=3 # 90° sentido de las agujas del reloj
```
Esto afecta la rotación de la grabación.
@ -233,7 +246,10 @@ Para listar los codificadores disponibles, puedes pasar un nombre de codificador
scrcpy --encoder _
```
### Grabación
### Capturas y grabaciones
#### Grabación
Es posible grabar la pantalla mientras se transmite:
@ -250,17 +266,117 @@ scrcpy -Nr file.mkv
# interrumpe la grabación con Ctrl+C
```
"Skipped frames" son grabados, incluso si no son mostrados en tiempo real (por razones de desempeño). Los frames tienen _marcas de tiempo_ en el dispositivo, por lo que el "[packet delay
Los "skipped frames" son grabados, incluso si no se mostrados en tiempo real (por razones de desempeño). Los frames tienen _marcas de tiempo_ en el dispositivo, por lo que el "[packet delay
variation]" no impacta el archivo grabado.
[packet delay variation]: https://en.wikipedia.org/wiki/Packet_delay_variation
#### v4l2loopback
En Linux se puede mandar el stream del video a un dispositivo loopback v4l2, por
lo que se puede abrir el dispositivo Android como una webcam con cualquier
programa compatible con v4l2.
Se debe instalar el modulo `v4l2loopback`:
```bash
sudo apt install v4l2loopback-dkms
```
Para crear un dispositivo v4l2:
```bash
sudo modprobe v4l2loopback
```
Esto va a crear un nuevo dispositivo de video en `/dev/videoN`, donde `N` es un número
(hay más [opciones](https://github.com/umlaeute/v4l2loopback#options) disponibles
para crear múltiples dispositivos o usar un ID en específico).
Para ver los dispositivos disponibles:
```bash
# requiere el paquete v4l-utils
v4l2-ctl --list-devices
# simple pero generalmente suficiente
ls /dev/video*
```
Para iniciar scrcpy usando una fuente v4l2:
```bash
scrcpy --v4l2-sink=/dev/videoN
scrcpy --v4l2-sink=/dev/videoN --no-display # deshabilita la transmisión de imagen
scrcpy --v4l2-sink=/dev/videoN -N # más corto
```
(reemplace `N` con el ID del dispositivo, compruebe con `ls /dev/video*`)
Una vez habilitado, podés abrir el stream del video con una herramienta compatible con v4l2:
```bash
ffplay -i /dev/videoN
vlc v4l2:///dev/videoN # VLC puede agregar un delay por buffering
```
Por ejemplo, podrías capturar el video usando [OBS].
[OBS]: https://obsproject.com/
#### Buffering
Es posible agregar buffering al video. Esto reduce el ruido en la imagen ("jitter")
pero aumenta la latencia (vea [#2464]).
[#2464]: https://github.com/Genymobile/scrcpy/issues/2464
La opción de buffering está disponible para la transmisión de imagen:
```bash
scrcpy --display-buffer=50 # agrega 50 ms de buffering a la imagen
```
y las fuentes V4L2:
```bash
scrcpy --v4l2-buffer=500 # agrega 500 ms de buffering a la fuente v4l2
```
### Conexión
#### Inalámbrica
#### TCP/IP (Inalámbrica)
_Scrcpy_ usa `adb` para comunicarse con el dispositivo, y `adb` puede [conectarse] vía TCP/IP:
_Scrcpy_ usa `adb` para comunicarse con el dispositivo, y `adb` puede [conectarse] vía TCP/IP.
El dispositivo debe estar conectado a la misma red que la computadora:
##### Automático
La opción `--tcpip` permite configurar la conexión automáticamente. Hay 2 variables.
Si el dispositivo (accesible en 192.168.1.1 para este ejemplo) ya está escuchando
en un puerto (generalmente 5555) esperando una conexión adb entrante, entonces corré:
```bash
scrcpy --tcpip=192.168.1.1 # el puerto default es 5555
scrcpy --tcpip=192.168.1.1:5555
```
Si el dispositivo no tiene habilitado el modo adb TCP/IP (o si no sabés la dirección IP),
entonces conectá el dispositivo por USB y corré:
```bash
scrcpy --tcpip # sin argumentos
```
El programa buscará automáticamente la IP del dispositivo, habilitará el modo TCP/IP, y
se conectará al dispositivo antes de comenzar a transmitir la imagen.
##### Manual
Como alternativa, se puede habilitar la conexión TCP/IP manualmente usando `adb`:
1. Conecta el dispositivo al mismo Wi-Fi que tu computadora.
2. Obtén la dirección IP del dispositivo, en Ajustes → Acerca del dispositivo → Estado, o ejecutando este comando:
@ -302,7 +418,7 @@ scrcpy -s 192.168.0.1:5555 # versión breve
Puedes iniciar múltiples instancias de _scrcpy_ para múltiples dispositivos.
#### Autoiniciar al detectar dispositivo
#### Iniciar automáticamente al detectar dispositivo
Puedes utilizar [AutoAdb]:
@ -312,37 +428,82 @@ autoadb scrcpy -s '{}'
[AutoAdb]: https://github.com/rom1v/autoadb
#### Túnel SSH
#### Túneles
Para conectarse a un dispositivo remoto, es posible conectar un cliente local de `adb` a un servidor remoto `adb` (siempre y cuando utilicen la misma versión de protocolos _adb_):
Para conectarse a un dispositivo remoto, es posible conectar un cliente local `adb` a un servidor remoto `adb` (siempre y cuando utilicen la misma versión de protocolos _adb_).
##### Servidor ADB remoto
Para conectarse a un servidor ADB remoto, haz que el servidor escuche en todas las interfaces:
```bash
adb kill-server # cierra el servidor local adb en 5037
ssh -CN -L5037:localhost:5037 -R27183:localhost:27183 your_remote_computer
adb kill-server
adb -a nodaemon server start
# conserva este servidor abierto
```
Desde otra terminal:
**Advertencia: todas las comunicaciones entre los clientes y el servidor ADB están desencriptadas.**
Supondremos que este servidor se puede acceder desde 192.168.1.2. Entonces, desde otra
terminal, corré scrcpy:
```bash
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
scrcpy --tunnel-host=192.168.1.2
```
Por default, scrcpy usa el puerto local que se usó para establecer el tunel
`adb forward` (típicamente `27183`, vea `--port`). También es posible forzar un
puerto diferente (puede resultar útil en situaciones más complejas, donde haya
múltiples redirecciones):
```
scrcpy --tunnel-port=1234
```
##### Túnel SSH
Para comunicarse con un servidor ADB remoto de forma segura, es preferible usar un túnel SSH.
Primero, asegurate que el servidor ADB está corriendo en la computadora remota:
```bash
adb start-server
```
Después, establecé el túnel SSH:
```bash
# local 5038 --> remoto 5037
# local 27183 <-- remoto 27183
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
# conserva este servidor abierto
```
Desde otra terminal, corré scrcpy:
```bash
export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy
```
Para evitar habilitar "remote port forwarding", puedes forzar una "forward connection" (nótese el argumento `-L` en vez de `-R`):
```bash
adb kill-server # cierra el servidor local adb en 5037
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
# local 5038 --> remoto 5037
# local 27183 --> remoto 27183
ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
# conserva este servidor abierto
```
Desde otra terminal:
Desde otra terminal, corré scrcpy:
```bash
export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy --force-adb-forward
```
Al igual que las conexiones inalámbricas, puede resultar útil reducir la calidad:
```
@ -402,7 +563,7 @@ Se puede rotar la ventana:
scrcpy --rotation 1
```
Los valores posibles son:
Los posibles valores son:
- `0`: sin rotación
- `1`: 90 grados contrarreloj
- `2`: 180 grados
@ -416,7 +577,7 @@ Nótese que _scrcpy_ maneja 3 diferentes rotaciones:
- `--rotation` (o <kbd>MOD</kbd>+<kbd>←</kbd>/<kbd>MOD</kbd>+<kbd>→</kbd>) rota solo el contenido de la imagen. Esto solo afecta a la imagen mostrada, no a la grabación.
### Otras opciones menores
### Otras opciones
#### Solo lectura ("Read-only")
@ -479,14 +640,12 @@ scrcpy -Sw # versión breve
```
#### Renderizar frames vencidos
#### Apagar al cerrar la aplicación
Por defecto, para minimizar la latencia, _scrcpy_ siempre renderiza el último frame disponible decodificado, e ignora cualquier frame anterior.
Para forzar el renderizado de todos los frames (a costo de posible aumento de latencia), use:
Para apagar la pantalla del dispositivo al cerrar scrcpy:
```bash
scrcpy --render-expired-frames
scrcpy --power-off-on-close
```
#### Mostrar clicks
@ -548,6 +707,8 @@ Además, <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> permite inyectar el texto
Algunos dispositivos no se comportan como es esperado al establecer el portapapeles programáticamente. La opción `--legacy-paste` está disponible para cambiar el comportamiento de <kbd>Ctrl</kbd>+<kbd>v</kbd> y <kbd>MOD</kbd>+<kbd>v</kbd> para que también inyecten el texto del portapapeles de la computadora como una secuencia de teclas (de la misma forma que <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).
Para deshabilitar la auto-sincronización del portapapeles, use `--no-clipboard-autosync`.
#### Pellizcar para zoom
Para simular "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-y-mover_.
@ -556,6 +717,48 @@ Más precisamente, mantén <kbd>Ctrl</kbd> mientras presionas botón izquierdo.
Concretamente, scrcpy genera clicks adicionales con un "dedo virtual" en la posición invertida respecto al centro de la pantalla.
#### Emular teclado físico (HID)
Por default, scrcpy usa el sistema de Android para la injección de teclas o texto:
funciona en todas partes, pero está limitado a ASCII.
En Linux, scrcpy puede emular un teclado USB físico en Android para proveer
una mejor experiencia al enviar _inputs_ (usando [USB HID vía AOAv2][hid-aoav2]):
deshabilita el teclado virtual y funciona para todos los caracteres y IME.
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
Sin embargo, solo funciona si el dispositivo está conectado por USB, y por ahora
solo funciona en Linux.
Para habilitar este modo:
```bash
scrcpy --hid-keyboard
scrcpy -K # más corto
```
Si por alguna razón falla (por ejemplo si el dispositivo no está conectado vía
USB), automáticamente vuelve al modo default (un mensaje se escribirá en la consola).
Se puede usar los mismos argumentos en la línea de comandos tanto si se conecta con
USB o vía TCP/IP.
En este modo, los _raw key events_ (_scancodes_) se envían al dispositivo, independientemente
del mapeo del teclado en el host. Por eso, si el diseño de tu teclado no concuerda, debe ser
configurado en el dispositivo Android, en Ajustes → Sistema → Idioma y Entrada de Texto
→ [Teclado Físico].
Se puede iniciar automáticamente en esta página de ajustes:
```bash
adb shell am start -a android.settings.HARD_KEYBOARD_SETTINGS
```
Sin embargo, la opción solo está disponible cuando el teclado HID está activo
(o cuando se conecta un teclado físico).
[Teclado Físico]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
#### Preferencias de inyección de texto
@ -573,13 +776,23 @@ scrcpy --prefer-text
(Pero esto romperá el comportamiento del teclado en los juegos)
Por el contrario, se puede forzar scrcpy para siempre injectar _raw key events_:
```bash
scrcpy --raw-key-events
```
Estas opciones no tienen efecto en los teclados HID (todos los _key events_ son enviados como
_scancodes_ en este modo).
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
#### Repetir tecla
Por defecto, mantener una tecla presionada genera múltiples _key events_. Esto puede causar problemas de desempeño en algunos juegos, donde estos eventos no tienen sentido de todos modos.
Por defecto, mantener una tecla presionada genera múltiples _key events_. Esto puede
causar problemas de desempeño en algunos juegos, donde estos eventos no tienen sentido de todos modos.
Para evitar enviar _key events_ repetidos:
@ -587,6 +800,9 @@ Para evitar enviar _key events_ repetidos:
scrcpy --no-key-repeat
```
Estas opciones no tienen efecto en los teclados HID (Android maneja directamente
las repeticiones de teclas en este modo)
#### Botón derecho y botón del medio
@ -608,14 +824,15 @@ No hay respuesta visual, un mensaje se escribirá en la consola.
#### Enviar archivos al dispositivo
Para enviar un archivo a `/sdcard/` en el dispositivo, arrastre y suelte un archivo (no APK) a la ventana de _scrcpy_.
Para enviar un archivo a `/sdcard/Download/` en el dispositivo, arrastre y suelte
un archivo (no APK) a la ventana de _scrcpy_.
No hay respuesta visual, un mensaje se escribirá en la consola.
No hay ninguna respuesta visual, un mensaje se escribirá en la consola.
El directorio de destino puede ser modificado al iniciar:
```bash
scrcpy --push-target=/sdcard/Download/
scrcpy --push-target=/sdcard/Movies/
```
@ -653,30 +870,42 @@ _<kbd>[Super]</kbd> es generalmente la tecla <kbd>Windows</kbd> o <kbd>Cmd</kbd>
| Rotar pantalla hacia la izquierda | <kbd>MOD</kbd>+<kbd>←</kbd> _(izquierda)_
| Rotar pantalla hacia la derecha | <kbd>MOD</kbd>+<kbd>→</kbd> _(derecha)_
| Ajustar ventana a 1:1 ("pixel-perfect") | <kbd>MOD</kbd>+<kbd>g</kbd>
| Ajustar ventana para quitar los bordes negros| <kbd>MOD</kbd>+<kbd>w</kbd> \| _Doble click¹_
| Click en `INICIO` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _Botón del medio_
| Click en `RETROCEDER` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _Botón derecho²_
| Click en `CAMBIAR APLICACIÓN` | <kbd>MOD</kbd>+<kbd>s</kbd>
| Click en `MENÚ` (desbloquear pantalla) | <kbd>MOD</kbd>+<kbd>m</kbd>
| Ajustar ventana para quitar los bordes negros| <kbd>MOD</kbd>+<kbd>w</kbd> \| _Doble click izquierdo¹_
| Click en `INICIO` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _Click medio_
| Click en `RETROCEDER` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _Click derecho²_
| Click en `CAMBIAR APLICACIÓN` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _Cuarto botón³_
| Click en `MENÚ` (desbloquear pantalla) | <kbd>MOD</kbd>+<kbd>m</kbd>
| Click en `SUBIR VOLUMEN` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(arriba)_
| Click en `BAJAR VOLUME` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(abajo)_
| Click en `ENCENDIDO` | <kbd>MOD</kbd>+<kbd>p</kbd>
| Encendido | _Botón derecho²_
| Apagar pantalla (manteniendo la transmisión)| <kbd>MOD</kbd>+<kbd>o</kbd>
| Apagar pantalla (manteniendo la transmisión) | <kbd>MOD</kbd>+<kbd>o</kbd>
| Encender pantalla | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>
| Rotar pantalla del dispositivo | <kbd>MOD</kbd>+<kbd>r</kbd>
| Abrir panel de notificaciones | <kbd>MOD</kbd>+<kbd>n</kbd>
| Cerrar panel de notificaciones | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
| Copiar al portapapeles³ | <kbd>MOD</kbd>+<kbd>c</kbd>
| Cortar al portapapeles³ | <kbd>MOD</kbd>+<kbd>x</kbd>
| Synchronizar portapapeles y pegar³ | <kbd>MOD</kbd>+<kbd>v</kbd>
| inyectar texto del portapapeles de la PC | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
| Abrir panel de notificaciones | <kbd>MOD</kbd>+<kbd>n</kbd> \| _Quinto botón³_
| Abrir panel de configuración | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Doble quinto botón³_
| Cerrar paneles | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
| Copiar al portapapeles | <kbd>MOD</kbd>+<kbd>c</kbd>
| Cortar al portapapeles | <kbd>MOD</kbd>+<kbd>x</kbd>
| Synchronizar portapapeles y pegar⁵ | <kbd>MOD</kbd>+<kbd>v</kbd>
| Inyectar texto del portapapeles de la PC | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
| Habilitar/Deshabilitar contador de FPS (en stdout) | <kbd>MOD</kbd>+<kbd>i</kbd>
| Pellizcar para zoom | <kbd>Ctrl</kbd>+_click-y-mover_
| Arrastrar y soltar un archivo (APK) | Instalar APK desde la computadora
| Arrastrar y soltar un archivo (no APK) | [Mover archivo al dispositivo](#enviar-archivos-al-dispositivo)
_¹Doble click en los bordes negros para eliminarlos._
_²Botón derecho enciende la pantalla si estaba apagada, sino ejecuta RETROCEDER._
Solo en Android >= 7._
Cuarto y quinto botón del mouse, si tu mouse los tiene._
_⁴Para las apps react-native en desarrollo, `MENU` activa el menú de desarrollo._
_⁵Solo en Android >= 7._
Los shortcuts con teclas repetidas se ejecutan soltando y volviendo a apretar la tecla
por segunda vez. Por ejemplo, para ejecutar "Abrir panel de configuración":
1. Apretá y mantené apretado <kbd>MOD</kbd>.
2. Después apretá dos veces la tecla <kbd>n</kbd>.
3. Por último, soltá la tecla <kbd>MOD</kbd>.
Todos los atajos <kbd>Ctrl</kbd>+_tecla_ son enviados al dispositivo para que sean manejados por la aplicación activa.
@ -691,6 +920,8 @@ ADB=/path/to/adb scrcpy
Para sobreescribir el path del archivo `scrcpy-server`, configure el path en `SCRCPY_SERVER_PATH`.
Para sobreescribir el ícono, configure el path en `SCRCPY_ICON_PATH`.
## ¿Por qué _scrcpy_?

View File

@ -1,17 +1,19 @@
_Only the original [README](README.md) is guaranteed to be up-to-date._
只有原版的[README](README.md)会保持最新。
_只有原版的 [README](README.md)是保证最新的。_
Current version is based on [65b023a]
Current version is based on [f4c7044]
本文根据[65b023a]进行翻译。
本文根据[f4c7044]进行翻译。
[65b023a]: https://github.com/Genymobile/scrcpy/blob/65b023ac6d586593193fd5290f65e25603b68e02/README.md
[f4c7044]: https://github.com/Genymobile/scrcpy/blob/f4c7044b46ae28eb64cb5e1a15c9649a44023c70/README.md
# scrcpy (v1.20)
# scrcpy (v1.22)
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
_发音为 "**scr**een **c**o**py**"_
本应用程序可以显示并控制通过 USB (或 [TCP/IP][article-tcpip]) 连接的安卓设备,且不需要任何 _root_ 权限。本程序支持 _GNU/Linux_, _Windows__macOS_
![screenshot](assets/screenshot-debian-600.jpg)
@ -36,6 +38,8 @@ Current version is based on [65b023a]
- [可配置显示质量](#采集设置)
- 以设备屏幕[作为摄像头(V4L2)](#v4l2loopback) (仅限 Linux)
- [模拟物理键盘 (HID)](#物理键盘模拟-hid) (仅限 Linux)
- [物理鼠标模拟 (HID)](#物理鼠标模拟-hid) (仅限 Linux)
- [OTG模式](#otg) (仅限 Linux)
- 更多 ……
## 系统要求
@ -68,12 +72,18 @@ Current version is based on [65b023a]
### Linux
在 Debian (目前仅支持 _testing__sid_ 分支) 和Ubuntu (20.04) 上:
在 Debian 和 Ubuntu 上:
```
apt install scrcpy
```
在 Arch Linux 上:
```
pacman -S scrcpy
```
我们也提供 [Snap] 包: [`scrcpy`][snap-link]。
[snap-link]: https://snapstats.org/snaps/scrcpy
@ -85,11 +95,6 @@ apt install scrcpy
[COPR]: https://fedoraproject.org/wiki/Category:Copr
[copr-link]: https://copr.fedorainfracloud.org/coprs/zeno/scrcpy/
对 Arch Linux 我们提供 [AUR] 包: [`scrcpy`][aur-link]。
[AUR]: https://wiki.archlinux.org/index.php/Arch_User_Repository
[aur-link]: https://aur.archlinux.org/packages/scrcpy/
对 Gentoo 我们提供 [Ebuild] 包:[`scrcpy/`][ebuild-link]。
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
@ -343,9 +348,32 @@ scrcpy --v4l2-buffer=500 # 为 v4l2 漏增加 500 毫秒的缓冲
### 连接
#### 无线
#### TCP/IP 无线
_Scrcpy_ 使用 `adb` 与设备通信,并且 `adb` 支持通过 TCP/IP [连接]到设备:
_Scrcpy_ 使用 `adb` 与设备通信,并且 `adb` 支持通过 TCP/IP [连接]到设备(设备必须连接与电脑相同的网络)。
##### 自动配置
参数 `--tcpip` 允许自动配置连接。这里有两种方式。
对于传入的 adb 连接如果设备在这个例子中以192.168.1.1为可用地址已经监听了一个端口通常是5555运行
```bash
scrcpy --tcpip=192.168.1.1 # 默认端口是5555
scrcpy --tcpip=192.168.1.1:5555
```
如果adb TCP/IP无线 模式在某些设备上不被启用或者你不知道IP地址用USB连接设备然后运行
```bash
scrcpy --tcpip # 无需其他参数
```
这将会自动寻找设备IP地址启用TCP/IP模式然后在启动之前连接到设备。
##### 手动配置
或者,可以通过 `adb` 使用手动启用 TCP/IP 连接:
1. 将设备和电脑连接至同一 Wi-Fi。
2. 打开 设置 → 关于手机 → 状态信息,获取设备的 IP 地址,也可以执行以下的命令:
@ -354,12 +382,12 @@ _Scrcpy_ 使用 `adb` 与设备通信,并且 `adb` 支持通过 TCP/IP [连接
adb shell ip route | awk '{print $9}'
```
3. 启用设备的网络 adb 功能: `adb tcpip 5555`。
3. 启用设备的网络 adb 功能:`adb tcpip 5555`。
4. 断开设备的 USB 连接。
5. 连接到您的设备:`adb connect DEVICE_IP:5555` _(将 `DEVICE_IP` 替换为设备 IP)_。
6. 正常运行 `scrcpy`。
可能降低码率和分辨率会更好一些
降低比特率和分辨率可能很有用
```bash
scrcpy --bit-rate 2M --max-size 800
@ -397,33 +425,75 @@ autoadb scrcpy -s '{}'
[AutoAdb]: https://github.com/rom1v/autoadb
#### SSH 隧道
#### 隧道
要远程连接到设备,可以将本地的 adb 客户端连接到远程的 adb 服务端 (需要两端的 _adb_ 协议版本相同)
要远程连接到设备,可以将本地的 adb 客户端连接到远程的 adb 服务端 (需要两端的 _adb_ 协议版本相同)
##### 远程ADB服务器
要连接到一个远程ADB服务器让服务器在所有接口上监听
```bash
adb kill-server # 关闭本地 5037 端口上的 adb 服务端
ssh -CN -L5037:localhost:5037 -R27183:localhost:27183 your_remote_computer
adb kill-server
adb -a nodaemon server start
# 保持该窗口开启
```
在另一个终端:
**警告所有客户端与ADB服务器的交流都是未加密的。**
假设此服务器可在 192.168.1.2 访问。 然后,从另一个终端,运行 scrcpy
```bash
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
scrcpy --tunnel-host=192.168.1.2
```
默认情况下scrcpy使用用于 `adb forward` 隧道建立的本地端口(通常是 `27183`,见 `--port` )。它也可以强制使用一个不同的隧道端口(当涉及更多的重定向时,这在更复杂的情况下可能很有用):
```
scrcpy --tunnel-port=1234
```
##### SSH 隧道
为了安全地与远程ADB服务器通信最好使用SSH隧道。
首先确保ADB服务器正在远程计算机上运行
```bash
adb start-server
```
然后建立一个SSH隧道
```bash
# 本地 5038 --> 远程 5037
# 本地 27183 <-- 远程 27183
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
# 保持该窗口开启
```
在另一个终端上运行scrcpy
```bash
export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy
```
若要不使用远程端口转发,可以强制使用正向连接 (注意 `-L` `-R` 的区别)
若要不使用远程端口转发,可以强制使用正向连接注意 `-L` 而不是 `-R`
```bash
adb kill-server # 关闭本地 5037 端口上的 adb 服务端
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
# 本地 5038 --> 远程 5037
# 本地 27183 <-- 远程 27183
ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
# 保持该窗口开启
```
在另一个终端:
在另一个终端运行scrcpy
```bash
export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy --force-adb-forward
```
@ -441,7 +511,7 @@ scrcpy -b2M -m800 --max-fps 15
窗口的标题默认为设备型号。可以通过如下命令修改:
```bash
scrcpy --window-title 'My device'
scrcpy --window-title "我的设备"
```
#### 位置和大小
@ -630,6 +700,8 @@ scrcpy --disable-screensaver
一些设备不支持通过程序设置剪贴板。通过 `--legacy-paste` 选项可以修改 <kbd>Ctrl</kbd>+<kbd>v</kbd> 和 <kbd>MOD</kbd>+<kbd>v</kbd> 的工作方式,使它们通过按键事件 (同 <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>) 来注入电脑剪贴板内容。
要禁用自动剪贴板同步功能,使用`--no-clipboard-autosync`。
#### 双指缩放
模拟“双指缩放”:<kbd>Ctrl</kbd>+_按住并移动鼠标_。
@ -659,11 +731,60 @@ scrcpy -K # 简写
在这种模式下,原始按键事件 (扫描码) 被发送给设备,而与宿主机按键映射无关。因此,若键盘布局不匹配,需要在 Android 设备上进行配置,具体为 设置 → 系统 → 语言和输入法 → [实体键盘]。
[Physical keyboard]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
[实体键盘]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
#### 物理鼠标模拟 (HID)
与物理键盘模拟类似,可以模拟一个物理鼠标。 同样,它仅在设备通过 USB 连接时才有效,并且目前仅在 Linux 上受支持。
默认情况下scrcpy 使用 Android 鼠标事件注入,使用绝对坐标。 通过模拟物理鼠标在Android设备上出现鼠标指针并注入鼠标相对运动、点击和滚动。
启用此模式:
```bash
scrcpy --hid-mouse
scrcpy -M # 简写
```
您还可以将 `--forward-all-clicks` 添加到 [转发所有点击][forward_all_clicks].
[forward_all_clicks]: #右键和中键
启用此模式后,计算机鼠标将被“捕获”(鼠标指针从计算机上消失并出现在 Android 设备上)。
特殊的捕获键,<kbd>Alt</kbd> 或 <kbd>Super</kbd>,切换(禁用或启用)鼠标捕获。 使用其中之一将鼠标的控制权交还给计算机。
#### OTG
可以仅使用物理键盘和鼠标模拟 (HID) 运行 _scrcpy_就好像计算机键盘和鼠标通过 OTG 线直接插入设备一样。
在这个模式下_adb_ (USB 调试)是不必要的,且镜像被禁用。
启用 OTG 模式:
```bash
scrcpy --otg
# 如果有多个 USB 设备可用,则通过序列号选择
scrcpy --otg -s 0123456789abcdef
```
只开启 HID 键盘 或 HID 鼠标 是可行的:
```bash
scrcpy --otg --hid-keyboard # 只开启 HID 键盘
scrcpy --otg --hid-mouse # 只开启 HID 鼠标
scrcpy --otg --hid-keyboard --hid-mouse # 开启 HID 键盘 和 HID 鼠标
# 为了方便,默认两者都开启
scrcpy --otg # 开启 HID 键盘 和 HID 鼠标
```
像 `--hid-keyboard` 和 `--hid-mouse` 一样,它只在设备通过 USB 连接时才有效,且目前仅在 Linux 上支持。
#### 文本注入偏好
字的时候,系统会产生两种[事件][textevents]
输入文字的时候,系统会产生两种[事件][textevents]
- _按键事件_ ,代表一个按键被按下或松开。
- _文本事件_ ,代表一个字符被输入。
@ -675,7 +796,13 @@ scrcpy -K # 简写
scrcpy --prefer-text
```
(这会导致键盘在游戏中工作不正常)
(这会导致键盘在游戏中工作不正常)
相反,您可以强制始终注入原始按键事件:
```bash
scrcpy --raw-key-events
```
该选项不影响 HID 键盘 (该模式下,所有按键都发送为扫描码)。
@ -765,7 +892,7 @@ _<kbd>[Super]</kbd> 键通常是指 <kbd>Windows</kbd> 或 <kbd>Cmd</kbd> 键。
| 点按 `主屏幕` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _中键_
| 点按 `返回` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _右键²_
| 点按 `切换应用` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _第4键³_
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd>
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd>
| 点按 `音量+` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上箭头)_
| 点按 `音量-` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下箭头)_
| 点按 `电源` | <kbd>MOD</kbd>+<kbd>p</kbd>
@ -776,9 +903,9 @@ _<kbd>[Super]</kbd> 键通常是指 <kbd>Windows</kbd> 或 <kbd>Cmd</kbd> 键。
| 展开通知面板 | <kbd>MOD</kbd>+<kbd>n</kbd> \| _第5键³_
| 展开设置面板 | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _双击第5键³_
| 收起通知面板 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
| 复制到剪贴板 | <kbd>MOD</kbd>+<kbd>c</kbd>
| 剪切到剪贴板 | <kbd>MOD</kbd>+<kbd>x</kbd>
| 同步剪贴板并粘贴 | <kbd>MOD</kbd>+<kbd>v</kbd>
| 复制到剪贴板 | <kbd>MOD</kbd>+<kbd>c</kbd>
| 剪切到剪贴板 | <kbd>MOD</kbd>+<kbd>x</kbd>
| 同步剪贴板并粘贴 | <kbd>MOD</kbd>+<kbd>v</kbd>
| 注入电脑剪贴板文本 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
| 打开/关闭FPS显示 (至标准输出) | <kbd>MOD</kbd>+<kbd>i</kbd>
| 捏拉缩放 | <kbd>Ctrl</kbd>+_按住并移动鼠标_
@ -788,7 +915,8 @@ _<kbd>[Super]</kbd> 键通常是指 <kbd>Windows</kbd> 或 <kbd>Cmd</kbd> 键。
_¹双击黑边可以去除黑边。_
_²点击鼠标右键将在屏幕熄灭时点亮屏幕其余情况则视为按下返回键 。_
_³鼠标的第4键和第5键。_
_⁴需要安卓版本 Android >= 7。_
_⁴对于开发中的 react-native 应用程序,`MENU` 触发开发菜单。_
_⁵需要安卓版本 Android >= 7。_
有重复按键的快捷键通过松开再按下一个按键来进行,如“展开设置面板”:
@ -816,7 +944,7 @@ ADB=/path/to/adb scrcpy
一个同事让我找出一个和 [gnirehtet] 一样难以发音的名字。
[`strcpy`] 复制一个 **str**ing (字符串); `scrcpy` 复制一个 **scr**een (屏幕)。
[`strcpy`] 源于 **str**ing (字符串); `scrcpy` 源于 **scr**een (屏幕)。
[gnirehtet]: https://github.com/Genymobile/gnirehtet
[`strcpy`]: http://man7.org/linux/man-pages/man3/strcpy.3.html

View File

@ -0,0 +1,121 @@
_scrcpy() {
local cur prev words cword
local opts="
--always-on-top
-b --bit-rate=
--codec-options=
--crop=
-d --select-usb
--disable-screensaver
--display=
--display-buffer=
-e --select-tcpip
--encoder=
--force-adb-forward
--forward-all-clicks
-f --fullscreen
-K --hid-keyboard
-h --help
--legacy-paste
--lock-video-orientation
--lock-video-orientation=
--max-fps=
-M --hid-mouse
-m --max-size=
--no-cleanup
--no-clipboard-on-error
--no-downsize-on-error
-n --no-control
-N --no-display
--no-key-repeat
--no-mipmaps
--otg
-p --port=
--power-off-on-close
--prefer-text
--print-fps
--push-target=
--raw-key-events
-r --record=
--record-format=
--render-driver=
--rotation=
-s --serial=
--shortcut-mod=
-S --turn-screen-off
-t --show-touches
--tcpip
--tcpip=
--tunnel-host=
--tunnel-port=
--v4l2-buffer=
--v4l2-sink=
-V --verbosity=
-v --version
-w --stay-awake
--window-borderless
--window-title=
--window-x=
--window-y=
--window-width=
--window-height="
_init_completion -s || return
case "$prev" in
--lock-video-orientation)
COMPREPLY=($(compgen -W 'unlocked initial 0 1 2 3' -- "$cur"))
return
;;
-r|--record)
COMPREPLY=($(compgen -f -- "$cur"))
return
;;
--record-format)
COMPREPLY=($(compgen -W 'mkv mp4' -- "$cur"))
return
;;
--render-driver)
COMPREPLY=($(compgen -W 'direct3d opengl opengles2 opengles metal software' -- "$cur"))
return
;;
--rotation)
COMPREPLY=($(compgen -W '0 1 2 3' -- "$cur"))
return
;;
--shortcut-mod)
# Only auto-complete a single key
COMPREPLY=($(compgen -W 'lctrl rctrl lalt ralt lsuper rsuper' -- "$cur"))
return
;;
-V|--verbosity)
COMPREPLY=($(compgen -W 'verbose debug info warn error' -- "$cur"))
return
;;
-b|--bitrate \
|--codec-options \
|--crop \
|--display \
|--display-buffer \
|--encoder \
|--max-fps \
|-m|--max-size \
|-p|--port \
|--push-target \
|-s|--serial \
|--tunnel-host \
|--tunnel-port \
|--v4l2-buffer \
|--v4l2-sink \
|--tcpip \
|--window-*)
# Option accepting an argument, but nothing to auto-complete
return
;;
esac
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
}
complete -F _scrcpy scrcpy

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -0,0 +1 @@
@cmd

View File

@ -0,0 +1,69 @@
#compdef -N scrcpy -N scrcpy.exe
#
# name: scrcpy
# auth: hltdev [hltdev8642@gmail.com]
# desc: completion file for scrcpy (all OSes)
#
local arguments
arguments=(
'--always-on-top[Make scrcpy window always on top \(above other windows\)]'
{-b,--bit-rate=}'[Encode the video at the given bit-rate]'
'--codec-options=[Set a list of comma-separated key\:type=value options for the device encoder]'
'--crop=[\[width\:height\:x\:y\] Crop the device screen on the server]'
{-d,--select-usb}'[Use USB device]'
'--disable-screensaver[Disable screensaver while scrcpy is running]'
'--display=[Specify the display id to mirror]'
'--display-buffer=[Add a buffering delay \(in milliseconds\) before displaying]'
{-e,--select-tcpip}'[Use TCP/IP device]'
'--encoder=[Use a specific MediaCodec encoder \(must be a H.264 encoder\)]'
'--force-adb-forward[Do not attempt to use \"adb reverse\" to connect to the device]'
'--forward-all-clicks[Forward clicks to device]'
{-f,--fullscreen}'[Start in fullscreen]'
{-K,--hid-keyboard}'[Simulate a physical keyboard by using HID over AOAv2]'
{-h,--help}'[Print the help]'
'--legacy-paste[Inject computer clipboard text as a sequence of key events on Ctrl+v]'
'--lock-video-orientation=[Lock video orientation]:orientation:(unlocked initial 0 1 2 3)'
'--max-fps=[Limit the frame rate of screen capture]'
{-M,--hid-mouse}'[Simulate a physical mouse by using HID over AOAv2]'
{-m,--max-size=}'[Limit both the width and height of the video to value]'
'--no-cleanup[Disable device cleanup actions on exit]'
'--no-clipboard-autosync[Disable automatic clipboard synchronization]'
'--no-downsize-on-error[Disable lowering definition on MediaCodec error]'
{-n,--no-control}'[Disable device control \(mirror the device in read only\)]'
{-N,--no-display}'[Do not display device \(during screen recording or when V4L2 sink is enabled\)]'
'--no-key-repeat[Do not forward repeated key events when a key is held down]'
'--no-mipmaps[Disable the generation of mipmaps]'
'--otg[Run in OTG mode \(simulating physical keyboard and mouse\)]'
{-p,--port=}'[\[port\[\:port\]\] Set the TCP port \(range\) used by the client to listen]'
'--power-off-on-close[Turn the device screen off when closing scrcpy]'
'--prefer-text[Inject alpha characters and space as text events instead of key events]'
'--print-fps[Start FPS counter, to print frame logs to the console]'
'--push-target=[Set the target directory for pushing files to the device by drag and drop]'
'--raw-key-events[Inject key events for all input keys, and ignore text events]'
{-r,--record=}'[Record screen to file]:record file:_files'
'--record-format=[Force recording format]:format:(mp4 mkv)'
'--render-driver=[Request SDL to use the given render driver]:driver name:(direct3d opengl opengles2 opengles metal software)'
'--rotation=[Set the initial display rotation]:rotation values:(0 1 2 3)'
{-s,--serial=}'[The device serial number \(mandatory for multiple devices only\)]'
'--shortcut-mod=[\[key1,key2+key3,...\] Specify the modifiers to use for scrcpy shortcuts]:shortcut mod:(lctrl rctrl lalt ralt lsuper rsuper)'
{-S,--turn-screen-off}'[Turn the device screen off immediately]'
{-t,--show-touches}'[Show physical touches]'
'--tcpip[\(optional \[ip\:port\]\) Configure and connect the device over TCP/IP]'
'--tunnel-host=[Set the IP address of the adb tunnel to reach the scrcpy server]'
'--tunnel-port=[Set the TCP port of the adb tunnel to reach the scrcpy server]'
'--v4l2-buffer=[Add a buffering delay \(in milliseconds\) before pushing frames]'
'--v4l2-sink=[\[\/dev\/videoN\] Output to v4l2loopback device]'
{-V,--verbosity=}'[Set the log level]:verbosity:(verbose debug info warn error)'
{-v,--version}'[Print the version of scrcpy]'
{-w,--stay-awake}'[Keep the device on while scrcpy is running, when the device is plugged in]'
'--window-borderless[Disable window decorations \(display borderless window\)]'
'--window-title=[Set a custom window title]'
'--window-x=[Set the initial window horizontal position]'
'--window-y=[Set the initial window vertical position]'
'--window-width=[Set the initial window width]'
'--window-height=[Set the initial window height]'
)
_arguments -s $arguments

View File

@ -1,14 +1,16 @@
src = [
'src/main.c',
'src/adb.c',
'src/adb_parser.c',
'src/adb_tunnel.c',
'src/adb/adb.c',
'src/adb/adb_device.c',
'src/adb/adb_parser.c',
'src/adb/adb_tunnel.c',
'src/cli.c',
'src/clock.c',
'src/compat.c',
'src/control_msg.c',
'src/controller.c',
'src/decoder.c',
'src/demuxer.c',
'src/device_msg.c',
'src/icon.c',
'src/file_pusher.c',
@ -24,7 +26,7 @@ src = [
'src/scrcpy.c',
'src/screen.c',
'src/server.c',
'src/stream.c',
'src/version.c',
'src/video_buffer.c',
'src/util/acksync.c',
'src/util/file.c',
@ -67,17 +69,20 @@ else
endif
endif
v4l2_support = host_machine.system() == 'linux'
v4l2_support = get_option('v4l2') and host_machine.system() == 'linux'
if v4l2_support
src += [ 'src/v4l2_sink.c' ]
endif
aoa_hid_support = host_machine.system() == 'linux'
if aoa_hid_support
usb_support = get_option('usb')
if usb_support
src += [
'src/aoa_hid.c',
'src/hid_keyboard.c',
'src/hid_mouse.c',
'src/usb/aoa_hid.c',
'src/usb/hid_keyboard.c',
'src/usb/hid_mouse.c',
'src/usb/scrcpy_otg.c',
'src/usb/screen_otg.c',
'src/usb/usb.c',
]
endif
@ -99,16 +104,16 @@ if not crossbuild_windows
dependencies += dependency('libavdevice')
endif
if aoa_hid_support
if usb_support
dependencies += dependency('libusb-1.0')
endif
else
# cross-compile mingw32 build (from Linux to Windows)
prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2')
sdl2_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_sdl2 + '/bin'
sdl2_lib_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_sdl2 + '/lib'
sdl2_include_dir = '../prebuilt-deps/' + prebuilt_sdl2 + '/include'
sdl2_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_sdl2 + '/bin'
sdl2_lib_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_sdl2 + '/lib'
sdl2_include_dir = 'prebuilt-deps/data/' + prebuilt_sdl2 + '/include'
sdl2 = declare_dependency(
dependencies: [
@ -119,8 +124,8 @@ else
)
prebuilt_ffmpeg = meson.get_cross_property('prebuilt_ffmpeg')
ffmpeg_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_ffmpeg + '/bin'
ffmpeg_include_dir = '../prebuilt-deps/' + prebuilt_ffmpeg + '/include'
ffmpeg_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_ffmpeg + '/bin'
ffmpeg_include_dir = 'prebuilt-deps/data/' + prebuilt_ffmpeg + '/include'
# ffmpeg versions are different for win32 and win64 builds
ffmpeg_avcodec = meson.get_cross_property('ffmpeg_avcodec')
@ -136,9 +141,22 @@ else
include_directories: include_directories(ffmpeg_include_dir)
)
prebuilt_libusb = meson.get_cross_property('prebuilt_libusb')
prebuilt_libusb_root = meson.get_cross_property('prebuilt_libusb_root')
libusb_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_libusb + '/dll'
libusb_include_dir = 'prebuilt-deps/data/' + prebuilt_libusb_root + '/include'
libusb = declare_dependency(
dependencies: [
cc.find_library('libusb-1.0', dirs: libusb_bin_dir),
],
include_directories: include_directories(libusb_include_dir)
)
dependencies = [
ffmpeg,
sdl2,
libusb,
cc.find_library('mingw32')
]
@ -193,7 +211,7 @@ conf.set('SERVER_DEBUGGER_METHOD_NEW', get_option('server_debugger_method') == '
conf.set('HAVE_V4L2', v4l2_support)
# enable HID over AOA support (linux only)
conf.set('HAVE_AOA_HID', aoa_hid_support)
conf.set('HAVE_USB', usb_support)
configure_file(configuration: conf, output: 'config.h')
@ -206,9 +224,13 @@ executable('scrcpy', src,
c_args: [])
install_man('scrcpy.1')
install_data('../data/icon.png',
install_data('data/icon.png',
rename: 'scrcpy.png',
install_dir: 'share/icons/hicolor/256x256/apps')
install_data('data/zsh-completion/_scrcpy',
install_dir: 'share/zsh/site-functions')
install_data('data/bash-completion/scrcpy',
install_dir: 'share/bash-completion/completions')
### TESTS
@ -218,7 +240,8 @@ if get_option('buildtype') == 'debug'
tests = [
['test_adb_parser', [
'tests/test_adb_parser.c',
'src/adb_parser.c',
'src/adb/adb_device.c',
'src/adb/adb_parser.c',
'src/util/str.c',
'src/util/strbuf.c',
]],
@ -263,6 +286,9 @@ if get_option('buildtype') == 'debug'
'src/util/str.c',
'src/util/strbuf.c',
]],
['test_vector', [
'tests/test_vector.c',
]],
]
foreach t : tests

1
app/prebuilt-deps/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/data

22
app/prebuilt-deps/common Executable file
View File

@ -0,0 +1,22 @@
PREBUILT_DATA_DIR=data
checksum() {
local file="$1"
local sum="$2"
echo "$file: verifying checksum..."
echo "$sum $file" | sha256sum -c
}
get_file() {
local url="$1"
local file="$2"
local sum="$3"
if [[ -f "$file" ]]
then
echo "$file: found"
else
echo "$file: not found, downloading..."
wget "$url" -O "$file"
fi
checksum "$file" "$sum"
}

View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=platform-tools-31.0.3
FILENAME=platform-tools_r31.0.3-windows.zip
SHA256SUM=0f4b8fdd26af2c3733539d6eebb3c2ed499ea1d4bb1f4e0ecc2d6016961a6e24
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://dl.google.com/android/repository/$FILENAME" \
"$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX=platform-tools
unzip "../$FILENAME" \
"$ZIP_PREFIX"/AdbWinApi.dll \
"$ZIP_PREFIX"/AdbWinUsbApi.dll \
"$ZIP_PREFIX"/adb.exe
mv "$ZIP_PREFIX"/* .
rmdir "$ZIP_PREFIX"

View File

@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=ffmpeg-win32-4.3.1
FILENAME_SHARED=ffmpeg-4.3.1-win32-shared.zip
SHA256SUM_SHARED=357af9901a456f4dcbacd107e83a934d344c9cb07ddad8aaf80612eeab7d26d2
FILENAME_DEV=ffmpeg-4.3.1-win32-dev.zip
SHA256SUM_DEV=230efb08e9bcf225bd474da29676c70e591fc94d8790a740ca801408fddcb78b
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/Genymobile/scrcpy/releases/download/v1.16/$FILENAME_SHARED" \
"$FILENAME_SHARED" "$SHA256SUM_SHARED"
get_file "https://github.com/Genymobile/scrcpy/releases/download/v1.16/$FILENAME_DEV" \
"$FILENAME_DEV" "$SHA256SUM_DEV"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX_SHARED=ffmpeg-4.3.1-win32-shared
unzip "../$FILENAME_SHARED" \
"$ZIP_PREFIX_SHARED"/bin/avutil-56.dll \
"$ZIP_PREFIX_SHARED"/bin/avcodec-58.dll \
"$ZIP_PREFIX_SHARED"/bin/avformat-58.dll \
"$ZIP_PREFIX_SHARED"/bin/swresample-3.dll \
"$ZIP_PREFIX_SHARED"/bin/swscale-5.dll
ZIP_PREFIX_DEV=ffmpeg-4.3.1-win32-dev
unzip "../$FILENAME_DEV" \
"$ZIP_PREFIX_DEV/include/*"
mv "$ZIP_PREFIX_SHARED"/* .
mv "$ZIP_PREFIX_DEV"/* .
rmdir "$ZIP_PREFIX_SHARED" "$ZIP_PREFIX_DEV"

View File

@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=ffmpeg-win64-5.0
FILENAME=ffmpeg-5.0-full_build-shared.7z
SHA256SUM=e5900f6cecd4c438d398bd2fc308736c10b857cd8dd61c11bcfb05bff5d1211a
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/GyanD/codexffmpeg/releases/download/5.0/$FILENAME" \
"$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX=ffmpeg-5.0-full_build-shared
7z x "../$FILENAME" \
"$ZIP_PREFIX"/bin/avutil-57.dll \
"$ZIP_PREFIX"/bin/avcodec-59.dll \
"$ZIP_PREFIX"/bin/avformat-59.dll \
"$ZIP_PREFIX"/bin/swresample-4.dll \
"$ZIP_PREFIX"/bin/swscale-6.dll \
"$ZIP_PREFIX"/include
mv "$ZIP_PREFIX"/* .
rmdir "$ZIP_PREFIX"

View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=libusb-1.0.25
FILENAME=libusb-1.0.25.7z
SHA256SUM=3d1c98416f454026034b2b5d67f8a294053898cb70a8b489874e75b136c6674d
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/libusb/libusb/releases/download/v1.0.25/$FILENAME" "$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
7z x "../$FILENAME" \
MinGW32/dll/libusb-1.0.dll \
MinGW64/dll/libusb-1.0.dll \
include /

View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=SDL2-2.0.20
FILENAME=SDL2-devel-2.0.20-mingw.tar.gz
SHA256SUM=38094d82a857d6c62352e5c5cdec74948c5b4d25c59cbd298d6d233568976bd1
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://libsdl.org/release/$FILENAME" "$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
TAR_PREFIX="$DEP_DIR" # root directory inside the tar has the same name
tar xf "../$FILENAME" --strip-components=1 \
"$TAR_PREFIX"/i686-w64-mingw32/bin/SDL2.dll \
"$TAR_PREFIX"/i686-w64-mingw32/include/ \
"$TAR_PREFIX"/i686-w64-mingw32/lib/ \
"$TAR_PREFIX"/x86_64-w64-mingw32/bin/SDL2.dll \
"$TAR_PREFIX"/x86_64-w64-mingw32/include/ \
"$TAR_PREFIX"/x86_64-w64-mingw32/lib/ \

View File

@ -1,6 +1,6 @@
#include <winuser.h>
0 ICON "../data/icon.ico"
0 ICON "data/icon.ico"
1 RT_MANIFEST "scrcpy-windows.manifest"
2 VERSIONINFO
BEGIN
@ -13,7 +13,7 @@ BEGIN
VALUE "LegalCopyright", "Romain Vimont, Genymobile"
VALUE "OriginalFilename", "scrcpy.exe"
VALUE "ProductName", "scrcpy"
VALUE "ProductVersion", "1.21"
VALUE "ProductVersion", "1.23"
END
END
BLOCK "VarFileInfo"

View File

@ -43,6 +43,12 @@ The values are expressed in the device natural orientation (typically, portrait
.B \-\-max\-size
value is computed on the cropped size.
.TP
.B \-d, \-\-select\-usb
Use USB device (if there is exactly one, like adb -d).
Also see \fB\-e\fR (\fB\-\-select\-tcpip\fR).
.TP
.BI "\-\-disable-screensaver"
Disable screensaver while scrcpy is running.
@ -62,6 +68,12 @@ Add a buffering delay (in milliseconds) before displaying. This increases latenc
Default is 0 (no buffering).
.TP
.B \-e, \-\-select\-tcpip
Use TCP/IP device (if there is exactly one, like adb -e).
Also see \fB\-d\fR (\fB\-\-select\-usb\fR).
.TP
.BI "\-\-encoder " name
Use a specific MediaCodec encoder (must be a H.264 encoder).
@ -88,7 +100,7 @@ Simulate a physical keyboard by using HID over AOAv2.
This provides a better experience for IME users, and allows to generate non-ASCII characters, contrary to the default injection method.
It may only work over USB, and is currently only supported on Linux.
It may only work over USB.
The keyboard layout must be configured (once and for all) on the device, via Settings -> System -> Languages and input -> Physical keyboard. This settings page can be started directly:
@ -130,10 +142,16 @@ In this mode, the computer mouse is captured to control the device directly (rel
LAlt, LSuper or RSuper toggle the capture mode, to give control of the mouse back to the computer.
It may only work over USB, and is currently only supported on Linux.
It may only work over USB.
Also see \fB\-\-hid\-keyboard\fR.
.TP
.B \-\-no\-cleanup
By default, scrcpy removes the server binary from the device and restores the device state (show touches, stay awake and power mode) on exit.
This option disables this cleanup.
.TP
.B \-\-no\-clipboard\-autosync
By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.
@ -162,6 +180,20 @@ Do not forward repeated key events when a key is held down.
.B \-\-no\-mipmaps
If the renderer is OpenGL 3.0+ or OpenGL ES 2.0+, then mipmaps are automatically generated to improve downscaling quality. This option disables the generation of mipmaps.
.TP
.B \-\-otg
Run in OTG mode: simulate physical keyboard and mouse, as if the computer keyboard and mouse were plugged directly to the device via an OTG cable.
In this mode, adb (USB debugging) is not necessary, and mirroring is disabled.
LAlt, LSuper or RSuper toggle the mouse capture mode, to give control of the mouse back to the computer.
If any of \fB\-\-hid\-keyboard\fR or \fB\-\-hid\-mouse\fR is set, only enable keyboard or mouse respectively, otherwise enable both.
It may only work over USB.
See \fB\-\-hid\-keyboard\fR and \fB\-\-hid\-mouse\fR.
.TP
.BI "\-p, \-\-port " port[:port]
Set the TCP port (range) used by the client to listen.
@ -179,6 +211,10 @@ Inject alpha characters and space as text events instead of key events.
This avoids issues when combining multiple keys to enter special characters,
but breaks the expected behavior of alpha keys in games (typically WASD).
.TP
.B "\-\-print\-fps
Start FPS counter, to print framerate logs to the console. It can be started or stopped at any time with MOD+i.
.TP
.BI "\-\-push\-target " path
Set the target directory for pushing files to the device by drag & drop. It is passed as\-is to "adb push".
@ -319,6 +355,12 @@ Set the initial window height.
Default is 0 (automatic).
.SH EXIT STATUS
.B scrcpy
will exit with code 0 on normal program termination. If an initial
connection cannot be established, the exit code 1 will be returned. If the
device disconnects while a session is active, exit code 2 will be returned.
.SH SHORTCUTS
In the following list, MOD is the shortcut modifier. By default, it's (left)

View File

@ -1,471 +0,0 @@
#include "adb.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "adb_parser.h"
#include "util/file.h"
#include "util/log.h"
#include "util/process_intr.h"
#include "util/str.h"
static const char *adb_command;
static inline const char *
get_adb_command(void) {
if (!adb_command) {
adb_command = getenv("ADB");
if (!adb_command)
adb_command = "adb";
}
return adb_command;
}
// serialize argv to string "[arg1], [arg2], [arg3]"
static size_t
argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
size_t idx = 0;
bool first = true;
while (*argv) {
const char *arg = *argv;
size_t len = strlen(arg);
// count space for "[], ...\0"
if (idx + len + 8 >= bufsize) {
// not enough space, truncate
assert(idx < bufsize - 4);
memcpy(&buf[idx], "...", 3);
idx += 3;
break;
}
if (first) {
first = false;
} else {
buf[idx++] = ',';
buf[idx++] = ' ';
}
buf[idx++] = '[';
memcpy(&buf[idx], arg, len);
idx += len;
buf[idx++] = ']';
argv++;
}
assert(idx < bufsize);
buf[idx] = '\0';
return idx;
}
static void
show_adb_installation_msg() {
#ifndef __WINDOWS__
static const struct {
const char *binary;
const char *command;
} pkg_managers[] = {
{"apt", "apt install adb"},
{"apt-get", "apt-get install adb"},
{"brew", "brew cask install android-platform-tools"},
{"dnf", "dnf install android-tools"},
{"emerge", "emerge dev-util/android-tools"},
{"pacman", "pacman -S android-tools"},
};
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
if (sc_file_executable_exists(pkg_managers[i].binary)) {
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
return;
}
}
#endif
LOGI("You may download and install 'adb' from "
"https://developer.android.com/studio/releases/platform-tools");
}
static void
show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
#define MAX_COMMAND_STRING_LEN 1024
char *buf = malloc(MAX_COMMAND_STRING_LEN);
if (!buf) {
LOG_OOM();
LOGE("Failed to execute");
return;
}
switch (err) {
case SC_PROCESS_ERROR_GENERIC:
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
LOGE("Failed to execute: %s", buf);
break;
case SC_PROCESS_ERROR_MISSING_BINARY:
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)");
show_adb_installation_msg();
break;
case SC_PROCESS_SUCCESS:
// do nothing
break;
}
free(buf);
}
static bool
process_check_success_internal(sc_pid pid, const char *name, bool close,
unsigned flags) {
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
if (pid == SC_PROCESS_NONE) {
if (log_errors) {
LOGE("Could not execute \"%s\"", name);
}
return false;
}
sc_exit_code exit_code = sc_process_wait(pid, close);
if (exit_code) {
if (log_errors) {
if (exit_code != SC_EXIT_CODE_NONE) {
LOGE("\"%s\" returned with value %" SC_PRIexitcode, name,
exit_code);
} else {
LOGE("\"%s\" exited unexpectedly", name);
}
}
return false;
}
return true;
}
static bool
process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name,
unsigned flags) {
if (!sc_intr_set_process(intr, pid)) {
// Already interrupted
return false;
}
// Always pass close=false, interrupting would be racy otherwise
bool ret = process_check_success_internal(pid, name, false, flags);
sc_intr_set_process(intr, SC_PROCESS_NONE);
// Close separately
sc_process_close(pid);
return ret;
}
static const char **
adb_create_argv(const char *serial, const char *const adb_cmd[], size_t len) {
const char **argv = malloc((len + 4) * sizeof(*argv));
if (!argv) {
LOG_OOM();
return NULL;
}
argv[0] = get_adb_command();
int i;
if (serial) {
argv[1] = "-s";
argv[2] = serial;
i = 3;
} else {
i = 1;
}
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
argv[len + i] = NULL;
return argv;
}
static sc_pid
adb_execute_p(const char *serial, const char *const adb_cmd[], size_t len,
unsigned flags, sc_pipe *pout) {
const char **argv = adb_create_argv(serial, adb_cmd, len);
if (!argv) {
return SC_PROCESS_NONE;
}
unsigned process_flags = 0;
if (flags & SC_ADB_NO_STDOUT) {
process_flags |= SC_PROCESS_NO_STDOUT;
}
if (flags & SC_ADB_NO_STDERR) {
process_flags |= SC_PROCESS_NO_STDERR;
}
sc_pid pid;
enum sc_process_result r =
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
if (r != SC_PROCESS_SUCCESS) {
// If the execution itself failed (not the command exit code), log the
// error in all cases
show_adb_err_msg(r, argv);
pid = SC_PROCESS_NONE;
}
free(argv);
return pid;
}
sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
unsigned flags) {
return adb_execute_p(serial, adb_cmd, len, flags, NULL);
}
bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"forward", local, remote};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
return process_check_success_intr(intr, pid, "adb forward", flags);
}
bool
adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
sprintf(local, "tcp:%" PRIu16, local_port);
const char *const adb_cmd[] = {"forward", "--remove", local};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
}
bool
adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port,
unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", remote, local};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
return process_check_success_intr(intr, pid, "adb reverse", flags);
}
bool
adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned flags) {
char remote[108 + 14 + 1]; // localabstract:NAME
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", "--remove", remote};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
}
bool
adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned flags) {
#ifdef __WINDOWS__
// Windows will parse the string, so the paths must be quoted
// (see sys/win/command.c)
local = sc_str_quote(local);
if (!local) {
return SC_PROCESS_NONE;
}
remote = sc_str_quote(remote);
if (!remote) {
free((void *) local);
return SC_PROCESS_NONE;
}
#endif
const char *const adb_cmd[] = {"push", local, remote};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
#ifdef __WINDOWS__
free((void *) remote);
free((void *) local);
#endif
return process_check_success_intr(intr, pid, "adb push", flags);
}
bool
adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned flags) {
#ifdef __WINDOWS__
// Windows will parse the string, so the local name must be quoted
// (see sys/win/command.c)
local = sc_str_quote(local);
if (!local) {
return SC_PROCESS_NONE;
}
#endif
const char *const adb_cmd[] = {"install", "-r", local};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
#ifdef __WINDOWS__
free((void *) local);
#endif
return process_check_success_intr(intr, pid, "adb install", flags);
}
bool
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
unsigned flags) {
char port_string[5 + 1];
sprintf(port_string, "%" PRIu16, port);
const char *const adb_cmd[] = {"tcpip", port_string};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
return process_check_success_intr(intr, pid, "adb tcpip", flags);
}
bool
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
const char *const adb_cmd[] = {"connect", ip_port};
sc_pipe pout;
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb connect\"");
return false;
}
// "adb connect" always returns successfully (with exit code 0), even in
// case of failure. As a workaround, check if its output starts with
// "connected".
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
if (!ok) {
return false;
}
if (r == -1) {
return false;
}
ok = !strncmp("connected", buf, sizeof("connected") - 1);
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
// "adb connect" also prints errors to stdout. Since we capture it,
// re-print the error to stderr.
sc_str_truncate(buf, r, "\r\n");
fprintf(stderr, "%s\n", buf);
}
return ok;
}
bool
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
const char *const adb_cmd[] = {"disconnect", ip_port};
size_t len = ip_port ? ARRAY_LEN(adb_cmd)
: ARRAY_LEN(adb_cmd) - 1;
sc_pid pid = adb_execute(NULL, adb_cmd, len, flags);
return process_check_success_intr(intr, pid, "adb disconnect", flags);
}
char *
adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
unsigned flags) {
const char *const adb_cmd[] = {"shell", "getprop", prop};
sc_pipe pout;
sc_pid pid =
adb_execute_p(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb getprop\"");
return NULL;
}
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb getprop", flags);
if (!ok) {
return NULL;
}
if (r == -1) {
return NULL;
}
sc_str_truncate(buf, r, " \r\n");
return strdup(buf);
}
char *
adb_get_serialno(struct sc_intr *intr, unsigned flags) {
const char *const adb_cmd[] = {"get-serialno"};
sc_pipe pout;
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb get-serialno\"");
return NULL;
}
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
if (!ok) {
return NULL;
}
if (r == -1) {
return false;
}
sc_str_truncate(buf, r, " \r\n");
return strdup(buf);
}
char *
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
const char *const cmd[] = {"shell", "ip", "route"};
sc_pipe pout;
sc_pid pid = adb_execute_p(serial, cmd, ARRAY_LEN(cmd), flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGD("Could not execute \"ip route\"");
return NULL;
}
// "adb shell ip route" output should contain only a few lines
char buf[1024];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
if (!ok) {
return NULL;
}
if (r == -1) {
return false;
}
assert((size_t) r <= sizeof(buf));
if (r == sizeof(buf) && buf[sizeof(buf) - 1] != '\0') {
// The implementation assumes that the output of "ip route" fits in the
// buffer in a single pass
LOGW("Result of \"ip route\" does not fit in 1Kb. "
"Please report an issue.\n");
return NULL;
}
return sc_adb_parse_device_ip_from_output(buf, r);
}

View File

@ -1,94 +0,0 @@
#ifndef SC_ADB_H
#define SC_ADB_H
#include "common.h"
#include <stdbool.h>
#include <inttypes.h>
#include "util/intr.h"
#define SC_ADB_NO_STDOUT (1 << 0)
#define SC_ADB_NO_STDERR (1 << 1)
#define SC_ADB_NO_LOGERR (1 << 2)
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
unsigned flags);
bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned flags);
bool
adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned flags);
bool
adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port,
unsigned flags);
bool
adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned flags);
bool
adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned flags);
bool
adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned flags);
/**
* Execute `adb tcpip <port>`
*/
bool
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
unsigned flags);
/**
* Execute `adb connect <ip_port>`
*
* `ip_port` may not be NULL.
*/
bool
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags);
/**
* Execute `adb disconnect [<ip_port>]`
*
* If `ip_port` is NULL, execute `adb disconnect`.
* Otherwise, execute `adb disconnect <ip_port>`.
*/
bool
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags);
/**
* Execute `adb getprop <prop>`
*/
char *
adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
unsigned flags);
/**
* Execute `adb get-serialno`
*
* Return the result, to be freed by the caller, or NULL on error.
*/
char *
adb_get_serialno(struct sc_intr *intr, unsigned flags);
/**
* Attempt to retrieve the device IP
*
* Return the IP as a string of the form "xxx.xxx.xxx.xxx", to be freed by the
* caller, or NULL on error.
*/
char *
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags);
#endif

712
app/src/adb/adb.c Normal file
View File

@ -0,0 +1,712 @@
#include "adb.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "adb_device.h"
#include "adb_parser.h"
#include "util/file.h"
#include "util/log.h"
#include "util/process_intr.h"
#include "util/str.h"
/* Convenience macro to expand:
*
* const char *const argv[] =
* SC_ADB_COMMAND("shell", "echo", "hello");
*
* to:
*
* const char *const argv[] =
* { sc_adb_get_executable(), "shell", "echo", "hello", NULL };
*/
#define SC_ADB_COMMAND(...) { sc_adb_get_executable(), __VA_ARGS__, NULL }
static const char *adb_executable;
const char *
sc_adb_get_executable(void) {
if (!adb_executable) {
adb_executable = getenv("ADB");
if (!adb_executable)
adb_executable = "adb";
}
return adb_executable;
}
// serialize argv to string "[arg1], [arg2], [arg3]"
static size_t
argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
size_t idx = 0;
bool first = true;
while (*argv) {
const char *arg = *argv;
size_t len = strlen(arg);
// count space for "[], ...\0"
if (idx + len + 8 >= bufsize) {
// not enough space, truncate
assert(idx < bufsize - 4);
memcpy(&buf[idx], "...", 3);
idx += 3;
break;
}
if (first) {
first = false;
} else {
buf[idx++] = ',';
buf[idx++] = ' ';
}
buf[idx++] = '[';
memcpy(&buf[idx], arg, len);
idx += len;
buf[idx++] = ']';
argv++;
}
assert(idx < bufsize);
buf[idx] = '\0';
return idx;
}
static void
show_adb_installation_msg() {
#ifndef __WINDOWS__
static const struct {
const char *binary;
const char *command;
} pkg_managers[] = {
{"apt", "apt install adb"},
{"apt-get", "apt-get install adb"},
{"brew", "brew cask install android-platform-tools"},
{"dnf", "dnf install android-tools"},
{"emerge", "emerge dev-util/android-tools"},
{"pacman", "pacman -S android-tools"},
};
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
if (sc_file_executable_exists(pkg_managers[i].binary)) {
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
return;
}
}
#endif
}
static void
show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
#define MAX_COMMAND_STRING_LEN 1024
char *buf = malloc(MAX_COMMAND_STRING_LEN);
if (!buf) {
LOG_OOM();
LOGE("Failed to execute");
return;
}
switch (err) {
case SC_PROCESS_ERROR_GENERIC:
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
LOGE("Failed to execute: %s", buf);
break;
case SC_PROCESS_ERROR_MISSING_BINARY:
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)");
show_adb_installation_msg();
break;
case SC_PROCESS_SUCCESS:
// do nothing
break;
}
free(buf);
}
static bool
process_check_success_internal(sc_pid pid, const char *name, bool close,
unsigned flags) {
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
if (pid == SC_PROCESS_NONE) {
if (log_errors) {
LOGE("Could not execute \"%s\"", name);
}
return false;
}
sc_exit_code exit_code = sc_process_wait(pid, close);
if (exit_code) {
if (log_errors) {
if (exit_code != SC_EXIT_CODE_NONE) {
LOGE("\"%s\" returned with value %" SC_PRIexitcode, name,
exit_code);
} else {
LOGE("\"%s\" exited unexpectedly", name);
}
}
return false;
}
return true;
}
static bool
process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name,
unsigned flags) {
if (intr && !sc_intr_set_process(intr, pid)) {
// Already interrupted
return false;
}
// Always pass close=false, interrupting would be racy otherwise
bool ret = process_check_success_internal(pid, name, false, flags);
if (intr) {
sc_intr_set_process(intr, SC_PROCESS_NONE);
}
// Close separately
sc_process_close(pid);
return ret;
}
static sc_pid
sc_adb_execute_p(const char *const argv[], unsigned flags, sc_pipe *pout) {
unsigned process_flags = 0;
if (flags & SC_ADB_NO_STDOUT) {
process_flags |= SC_PROCESS_NO_STDOUT;
}
if (flags & SC_ADB_NO_STDERR) {
process_flags |= SC_PROCESS_NO_STDERR;
}
sc_pid pid;
enum sc_process_result r =
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
if (r != SC_PROCESS_SUCCESS) {
// If the execution itself failed (not the command exit code), log the
// error in all cases
show_adb_err_msg(r, argv);
pid = SC_PROCESS_NONE;
}
return pid;
}
sc_pid
sc_adb_execute(const char *const argv[], unsigned flags) {
return sc_adb_execute_p(argv, flags, NULL);
}
bool
sc_adb_start_server(struct sc_intr *intr, unsigned flags) {
const char *const argv[] = SC_ADB_COMMAND("start-server");
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb start-server", flags);
}
bool
sc_adb_kill_server(struct sc_intr *intr, unsigned flags) {
const char *const argv[] = SC_ADB_COMMAND("kill-server");
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb kill-server", flags);
}
bool
sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "forward", local, remote);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb forward", flags);
}
bool
sc_adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
sprintf(local, "tcp:%" PRIu16, local_port);
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "forward", "--remove", local);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
}
bool
sc_adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port,
unsigned flags) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "reverse", remote, local);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb reverse", flags);
}
bool
sc_adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned flags) {
char remote[108 + 14 + 1]; // localabstract:NAME
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "reverse", "--remove", remote);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
}
bool
sc_adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned flags) {
#ifdef __WINDOWS__
// Windows will parse the string, so the paths must be quoted
// (see sys/win/command.c)
local = sc_str_quote(local);
if (!local) {
return SC_PROCESS_NONE;
}
remote = sc_str_quote(remote);
if (!remote) {
free((void *) local);
return SC_PROCESS_NONE;
}
#endif
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "push", local, remote);
sc_pid pid = sc_adb_execute(argv, flags);
#ifdef __WINDOWS__
free((void *) remote);
free((void *) local);
#endif
return process_check_success_intr(intr, pid, "adb push", flags);
}
bool
sc_adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned flags) {
#ifdef __WINDOWS__
// Windows will parse the string, so the local name must be quoted
// (see sys/win/command.c)
local = sc_str_quote(local);
if (!local) {
return SC_PROCESS_NONE;
}
#endif
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "install", "-r", local);
sc_pid pid = sc_adb_execute(argv, flags);
#ifdef __WINDOWS__
free((void *) local);
#endif
return process_check_success_intr(intr, pid, "adb install", flags);
}
bool
sc_adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
unsigned flags) {
char port_string[5 + 1];
sprintf(port_string, "%" PRIu16, port);
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "tcpip", port_string);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb tcpip", flags);
}
bool
sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
const char *const argv[] = SC_ADB_COMMAND("connect", ip_port);
sc_pipe pout;
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb connect\"");
return false;
}
// "adb connect" always returns successfully (with exit code 0), even in
// case of failure. As a workaround, check if its output starts with
// "connected".
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
if (!ok) {
return false;
}
if (r == -1) {
return false;
}
assert((size_t) r < sizeof(buf));
buf[r] = '\0';
ok = !strncmp("connected", buf, sizeof("connected") - 1);
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
// "adb connect" also prints errors to stdout. Since we capture it,
// re-print the error to stderr.
size_t len = strcspn(buf, "\r\n");
buf[len] = '\0';
fprintf(stderr, "%s\n", buf);
}
return ok;
}
bool
sc_adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
assert(ip_port);
const char *const argv[] = SC_ADB_COMMAND("disconnect", ip_port);
sc_pid pid = sc_adb_execute(argv, flags);
return process_check_success_intr(intr, pid, "adb disconnect", flags);
}
static bool
sc_adb_list_devices(struct sc_intr *intr, unsigned flags,
struct sc_vec_adb_devices *out_vec) {
const char *const argv[] = SC_ADB_COMMAND("devices", "-l");
#define BUFSIZE 65536
char *buf = malloc(BUFSIZE);
if (!buf) {
return false;
}
sc_pipe pout;
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb devices -l\"");
free(buf);
return false;
}
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, BUFSIZE - 1);
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb devices -l", flags);
if (!ok) {
free(buf);
return false;
}
if (r == -1) {
free(buf);
return false;
}
assert((size_t) r < BUFSIZE);
if (r == BUFSIZE - 1) {
// The implementation assumes that the output of "adb devices -l" fits
// in the buffer in a single pass
LOGW("Result of \"adb devices -l\" does not fit in 64Kb. "
"Please report an issue.");
return false;
}
// It is parsed as a NUL-terminated string
buf[r] = '\0';
// List all devices to the output list directly
ok = sc_adb_parse_devices(buf, out_vec);
free(buf);
return ok;
}
static bool
sc_adb_accept_device(const struct sc_adb_device *device,
const struct sc_adb_device_selector *selector) {
switch (selector->type) {
case SC_ADB_DEVICE_SELECT_ALL:
return true;
case SC_ADB_DEVICE_SELECT_SERIAL:
assert(selector->serial);
char *device_serial_colon = strchr(device->serial, ':');
if (device_serial_colon) {
// The device serial is an IP:port...
char *serial_colon = strchr(selector->serial, ':');
if (!serial_colon) {
// But the requested serial has no ':', so only consider
// the IP part of the device serial. This allows to use
// "192.168.1.1" to match any "192.168.1.1:port".
size_t serial_len = strlen(selector->serial);
size_t device_ip_len = device_serial_colon - device->serial;
if (serial_len != device_ip_len) {
// They are not equal, they don't even have the same
// length
return false;
}
return !strncmp(selector->serial, device->serial,
device_ip_len);
}
}
return !strcmp(selector->serial, device->serial);
case SC_ADB_DEVICE_SELECT_USB:
return !sc_adb_is_serial_tcpip(device->serial);
case SC_ADB_DEVICE_SELECT_TCPIP:
return sc_adb_is_serial_tcpip(device->serial);
default:
assert(!"Missing SC_ADB_DEVICE_SELECT_* handling");
break;
}
return false;
}
static size_t
sc_adb_devices_select(struct sc_adb_device *devices, size_t len,
const struct sc_adb_device_selector *selector,
size_t *idx_out) {
size_t count = 0;
for (size_t i = 0; i < len; ++i) {
struct sc_adb_device *device = &devices[i];
device->selected = sc_adb_accept_device(device, selector);
if (device->selected) {
if (idx_out && !count) {
*idx_out = i;
}
++count;
}
}
return count;
}
static void
sc_adb_devices_log(enum sc_log_level level, struct sc_adb_device *devices,
size_t count) {
for (size_t i = 0; i < count; ++i) {
struct sc_adb_device *d = &devices[i];
const char *selection = d->selected ? "-->" : " ";
const char *type = sc_adb_is_serial_tcpip(d->serial) ? "(tcpip)"
: " (usb)";
LOG(level, " %s %s %-20s %16s %s",
selection, type, d->serial, d->state, d->model ? d->model : "");
}
}
static bool
sc_adb_device_check_state(struct sc_adb_device *device,
struct sc_adb_device *devices, size_t count) {
const char *state = device->state;
if (!strcmp("device", state)) {
return true;
}
if (!strcmp("unauthorized", state)) {
LOGE("Device is unauthorized:");
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, devices, count);
LOGE("A popup should open on the device to request authorization.");
LOGE("Check the FAQ: "
"<https://github.com/Genymobile/scrcpy/blob/master/FAQ.md>");
}
return false;
}
bool
sc_adb_select_device(struct sc_intr *intr,
const struct sc_adb_device_selector *selector,
unsigned flags, struct sc_adb_device *out_device) {
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_list_devices(intr, flags, &vec);
if (!ok) {
LOGE("Could not list ADB devices");
return false;
}
if (vec.size == 0) {
LOGE("Could not find any ADB device");
return false;
}
size_t sel_idx; // index of the single matching device if sel_count == 1
size_t sel_count =
sc_adb_devices_select(vec.data, vec.size, selector, &sel_idx);
if (sel_count == 0) {
// if count > 0 && sel_count == 0, then necessarily a selection is
// requested
assert(selector->type != SC_ADB_DEVICE_SELECT_ALL);
switch (selector->type) {
case SC_ADB_DEVICE_SELECT_SERIAL:
assert(selector->serial);
LOGE("Could not find ADB device %s:", selector->serial);
break;
case SC_ADB_DEVICE_SELECT_USB:
LOGE("Could not find any ADB device over USB:");
break;
case SC_ADB_DEVICE_SELECT_TCPIP:
LOGE("Could not find any ADB device over TCP/IP:");
break;
default:
assert(!"Unexpected selector type");
break;
}
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
sc_adb_devices_destroy(&vec);
return false;
}
if (sel_count > 1) {
switch (selector->type) {
case SC_ADB_DEVICE_SELECT_ALL:
LOGE("Multiple (%" SC_PRIsizet ") ADB devices:", sel_count);
break;
case SC_ADB_DEVICE_SELECT_SERIAL:
assert(selector->serial);
LOGE("Multiple (%" SC_PRIsizet ") ADB devices with serial %s:",
sel_count, selector->serial);
break;
case SC_ADB_DEVICE_SELECT_USB:
LOGE("Multiple (%" SC_PRIsizet ") ADB devices over USB:",
sel_count);
break;
case SC_ADB_DEVICE_SELECT_TCPIP:
LOGE("Multiple (%" SC_PRIsizet ") ADB devices over TCP/IP:",
sel_count);
break;
default:
assert(!"Unexpected selector type");
break;
}
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
LOGE("Select a device via -s (--serial), -d (--select-usb) or -e "
"(--select-tcpip)");
sc_adb_devices_destroy(&vec);
return false;
}
assert(sel_count == 1); // sel_idx is valid only if sel_count == 1
struct sc_adb_device *device = &vec.data[sel_idx];
ok = sc_adb_device_check_state(device, vec.data, vec.size);
if (!ok) {
sc_adb_devices_destroy(&vec);
return false;
}
LOGD("ADB device found:");
sc_adb_devices_log(SC_LOG_LEVEL_DEBUG, vec.data, vec.size);
// Move devics into out_device (do not destroy device)
sc_adb_device_move(out_device, device);
sc_adb_devices_destroy(&vec);
return true;
}
char *
sc_adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
unsigned flags) {
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "shell", "getprop", prop);
sc_pipe pout;
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb getprop\"");
return NULL;
}
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "adb getprop", flags);
if (!ok) {
return NULL;
}
if (r == -1) {
return NULL;
}
assert((size_t) r < sizeof(buf));
buf[r] = '\0';
size_t len = strcspn(buf, " \r\n");
buf[len] = '\0';
return strdup(buf);
}
char *
sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
assert(serial);
const char *const argv[] =
SC_ADB_COMMAND("-s", serial, "shell", "ip", "route");
sc_pipe pout;
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
if (pid == SC_PROCESS_NONE) {
LOGD("Could not execute \"ip route\"");
return NULL;
}
// "adb shell ip route" output should contain only a few lines
char buf[1024];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
sc_pipe_close(pout);
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
if (!ok) {
return NULL;
}
if (r == -1) {
return NULL;
}
assert((size_t) r < sizeof(buf));
if (r == sizeof(buf) - 1) {
// The implementation assumes that the output of "ip route" fits in the
// buffer in a single pass
LOGW("Result of \"ip route\" does not fit in 1Kb. "
"Please report an issue.");
return NULL;
}
// It is parsed as a NUL-terminated string
buf[r] = '\0';
return sc_adb_parse_device_ip_from_output(buf);
}
bool
sc_adb_is_serial_tcpip(const char *serial) {
return strchr(serial, ':');
}

126
app/src/adb/adb.h Normal file
View File

@ -0,0 +1,126 @@
#ifndef SC_ADB_H
#define SC_ADB_H
#include "common.h"
#include <stdbool.h>
#include <inttypes.h>
#include "adb_device.h"
#include "util/intr.h"
#define SC_ADB_NO_STDOUT (1 << 0)
#define SC_ADB_NO_STDERR (1 << 1)
#define SC_ADB_NO_LOGERR (1 << 2)
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
const char *
sc_adb_get_executable(void);
enum sc_adb_device_selector_type {
SC_ADB_DEVICE_SELECT_ALL,
SC_ADB_DEVICE_SELECT_SERIAL,
SC_ADB_DEVICE_SELECT_USB,
SC_ADB_DEVICE_SELECT_TCPIP,
};
struct sc_adb_device_selector {
enum sc_adb_device_selector_type type;
const char *serial;
};
sc_pid
sc_adb_execute(const char *const argv[], unsigned flags);
bool
sc_adb_start_server(struct sc_intr *intr, unsigned flags);
bool
sc_adb_kill_server(struct sc_intr *intr, unsigned flags);
bool
sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned flags);
bool
sc_adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned flags);
bool
sc_adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port,
unsigned flags);
bool
sc_adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned flags);
bool
sc_adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned flags);
bool
sc_adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned flags);
/**
* Execute `adb tcpip <port>`
*/
bool
sc_adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
unsigned flags);
/**
* Execute `adb connect <ip_port>`
*
* `ip_port` may not be NULL.
*/
bool
sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags);
/**
* Execute `adb disconnect [<ip_port>]`
*
* If `ip_port` is NULL, execute `adb disconnect`.
* Otherwise, execute `adb disconnect <ip_port>`.
*/
bool
sc_adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags);
/**
* Execute `adb devices` and parse the result to select a device
*
* Return true if a single matching device is found, and write it to out_device.
*/
bool
sc_adb_select_device(struct sc_intr *intr,
const struct sc_adb_device_selector *selector,
unsigned flags, struct sc_adb_device *out_device);
/**
* Execute `adb getprop <prop>`
*/
char *
sc_adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
unsigned flags);
/**
* Attempt to retrieve the device IP
*
* Return the IP as a string of the form "xxx.xxx.xxx.xxx", to be freed by the
* caller, or NULL on error.
*/
char *
sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags);
/**
* Indicate if the serial represents an IP address
*
* In practice, it just returns true if and only if it contains a ':', which is
* sufficient to distinguish an ip:port from a real USB serial.
*/
bool
sc_adb_is_serial_tcpip(const char *serial);
#endif

27
app/src/adb/adb_device.c Normal file
View File

@ -0,0 +1,27 @@
#include "adb_device.h"
#include <stdlib.h>
void
sc_adb_device_destroy(struct sc_adb_device *device) {
free(device->serial);
free(device->state);
free(device->model);
}
void
sc_adb_device_move(struct sc_adb_device *dst, struct sc_adb_device *src) {
*dst = *src;
src->serial = NULL;
src->state = NULL;
src->model = NULL;
}
void
sc_adb_devices_destroy(struct sc_vec_adb_devices *devices) {
for (size_t i = 0; i < devices->size; ++i) {
sc_adb_device_destroy(&devices->data[i]);
}
sc_vector_destroy(devices);
}

38
app/src/adb/adb_device.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef SC_ADB_DEVICE_H
#define SC_ADB_DEVICE_H
#include "common.h"
#include <stdbool.h>
#include <stddef.h>
#include "util/vector.h"
struct sc_adb_device {
char *serial;
char *state;
char *model;
bool selected;
};
struct sc_vec_adb_devices SC_VECTOR(struct sc_adb_device);
void
sc_adb_device_destroy(struct sc_adb_device *device);
/**
* Move src to dst
*
* After this call, the content of src is undefined, except that
* sc_adb_device_destroy() can be called.
*
* This is useful to take a device from a list that will be destroyed, without
* making unnecessary copies.
*/
void
sc_adb_device_move(struct sc_adb_device *dst, struct sc_adb_device *src);
void
sc_adb_devices_destroy(struct sc_vec_adb_devices *devices);
#endif

227
app/src/adb/adb_parser.c Normal file
View File

@ -0,0 +1,227 @@
#include "adb_parser.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "util/log.h"
#include "util/str.h"
bool
sc_adb_parse_device(char *line, struct sc_adb_device *device) {
// One device line looks like:
// "0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
// "device:MyDevice transport_id:1"
if (line[0] == '*') {
// Garbage lines printed by adb daemon while starting start with a '*'
return false;
}
if (!strncmp("adb server", line, sizeof("adb server") - 1)) {
// Ignore lines starting with "adb server":
// adb server version (41) doesn't match this client (39); killing...
return false;
}
char *s = line; // cursor in the line
// After the serial:
// - "adb devices" writes a single '\t'
// - "adb devices -l" writes multiple spaces
// For flexibility, accept both.
size_t serial_len = strcspn(s, " \t");
if (!serial_len) {
// empty serial
return false;
}
bool eol = s[serial_len] == '\0';
if (eol) {
// serial alone is unexpected
return false;
}
s[serial_len] = '\0';
char *serial = s;
s += serial_len + 1;
// After the serial, there might be several spaces
s += strspn(s, " \t"); // consume all separators
size_t state_len = strcspn(s, " ");
if (!state_len) {
// empty state
return false;
}
eol = s[state_len] == '\0';
s[state_len] = '\0';
char *state = s;
char *model = NULL;
if (!eol) {
s += state_len + 1;
// Iterate over all properties "key:value key:value ..."
for (;;) {
size_t token_len = strcspn(s, " ");
if (!token_len) {
break;
}
eol = s[token_len] == '\0';
s[token_len] = '\0';
char *token = s;
if (!strncmp("model:", token, sizeof("model:") - 1)) {
model = &token[sizeof("model:") - 1];
// We only need the model
break;
}
if (eol) {
break;
} else {
s+= token_len + 1;
}
}
}
device->serial = strdup(serial);
if (!device->serial) {
return false;
}
device->state = strdup(state);
if (!device->state) {
free(device->serial);
return false;
}
if (model) {
device->model = strdup(model);
if (!device->model) {
LOG_OOM();
// model is optional, do not fail
}
} else {
device->model = NULL;
}
device->selected = false;
return true;
}
bool
sc_adb_parse_devices(char *str, struct sc_vec_adb_devices *out_vec) {
#define HEADER "List of devices attached"
#define HEADER_LEN (sizeof(HEADER) - 1)
bool header_found = false;
size_t idx_line = 0;
while (str[idx_line] != '\0') {
char *line = &str[idx_line];
size_t len = strcspn(line, "\n");
// The next line starts after the '\n' (replaced by `\0`)
idx_line += len;
if (str[idx_line] != '\0') {
// The next line starts after the '\n'
++idx_line;
}
if (!header_found) {
if (!strncmp(line, HEADER, HEADER_LEN)) {
header_found = true;
}
// Skip everything until the header, there might be garbage lines
// related to daemon starting before
continue;
}
// The line, but without any trailing '\r'
size_t line_len = sc_str_remove_trailing_cr(line, len);
line[line_len] = '\0';
struct sc_adb_device device;
bool ok = sc_adb_parse_device(line, &device);
if (!ok) {
continue;
}
ok = sc_vector_push(out_vec, device);
if (!ok) {
LOG_OOM();
LOGE("Could not push adb_device to vector");
sc_adb_device_destroy(&device);
// continue anyway
continue;
}
}
assert(header_found || out_vec->size == 0);
return header_found;
}
static char *
sc_adb_parse_device_ip_from_line(char *line) {
// One line from "ip route" looks like:
// "192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.x"
// Get the location of the device name (index of "wlan0" in the example)
ssize_t idx_dev_name = sc_str_index_of_column(line, 2, " ");
if (idx_dev_name == -1) {
return NULL;
}
// Get the location of the ip address (column 8, but column 6 if we start
// from column 2). Must be computed before truncating individual columns.
ssize_t idx_ip = sc_str_index_of_column(&line[idx_dev_name], 6, " ");
if (idx_ip == -1) {
return NULL;
}
// idx_ip is searched from &line[idx_dev_name]
idx_ip += idx_dev_name;
char *dev_name = &line[idx_dev_name];
size_t dev_name_len = strcspn(dev_name, " \t");
dev_name[dev_name_len] = '\0';
char *ip = &line[idx_ip];
size_t ip_len = strcspn(ip, " \t");
ip[ip_len] = '\0';
// Only consider lines where the device name starts with "wlan"
if (strncmp(dev_name, "wlan", sizeof("wlan") - 1)) {
LOGD("Device ip lookup: ignoring %s (%s)", ip, dev_name);
return NULL;
}
return strdup(ip);
}
char *
sc_adb_parse_device_ip_from_output(char *str) {
size_t idx_line = 0;
while (str[idx_line] != '\0') {
char *line = &str[idx_line];
size_t len = strcspn(line, "\n");
// The same, but without any trailing '\r'
size_t line_len = sc_str_remove_trailing_cr(line, len);
line[line_len] = '\0';
char *ip = sc_adb_parse_device_ip_from_line(line);
if (ip) {
// Found
return ip;
}
idx_line += len;
if (str[idx_line] != '\0') {
// The next line starts after the '\n'
++idx_line;
}
}
return NULL;
}

30
app/src/adb/adb_parser.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef SC_ADB_PARSER_H
#define SC_ADB_PARSER_H
#include "common.h"
#include <stddef.h>
#include "adb_device.h"
/**
* Parse the available devices from the output of `adb devices`
*
* The parameter must be a NUL-terminated string.
*
* Warning: this function modifies the buffer for optimization purposes.
*/
bool
sc_adb_parse_devices(char *str, struct sc_vec_adb_devices *out_vec);
/**
* Parse the ip from the output of `adb shell ip route`
*
* The parameter must be a NUL-terminated string.
*
* Warning: this function modifies the buffer for optimization purposes.
*/
char *
sc_adb_parse_device_ip_from_output(char *str);
#endif

View File

@ -20,7 +20,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
struct sc_port_range port_range) {
uint16_t port = port_range.first;
for (;;) {
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port,
if (!sc_adb_reverse(intr, serial, SC_SOCKET_NAME, port,
SC_ADB_NO_STDOUT)) {
// the command itself failed, it will fail on any port
return false;
@ -52,7 +52,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
}
// failure, disable tunnel and try another port
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
if (!sc_adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
SC_ADB_NO_STDOUT)) {
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
}
@ -83,7 +83,8 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
uint16_t port = port_range.first;
for (;;) {
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_ADB_NO_STDOUT)) {
if (sc_adb_forward(intr, serial, port, SC_SOCKET_NAME,
SC_ADB_NO_STDOUT)) {
// success
tunnel->local_port = port;
tunnel->enabled = true;
@ -148,10 +149,10 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
bool ret;
if (tunnel->forward) {
ret = adb_forward_remove(intr, serial, tunnel->local_port,
ret = sc_adb_forward_remove(intr, serial, tunnel->local_port,
SC_ADB_NO_STDOUT);
} else {
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
ret = sc_adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
SC_ADB_NO_STDOUT);
assert(tunnel->server_socket != SC_SOCKET_NONE);

View File

@ -1,65 +0,0 @@
#include "adb_parser.h"
#include <assert.h>
#include <string.h>
#include "util/log.h"
#include "util/str.h"
static char *
sc_adb_parse_device_ip_from_line(char *line, size_t len) {
// One line from "ip route" looks lile:
// "192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.x"
// Get the location of the device name (index of "wlan0" in the example)
ssize_t idx_dev_name = sc_str_index_of_column(line, 2, " ");
if (idx_dev_name == -1) {
return NULL;
}
// Get the location of the ip address (column 8, but column 6 if we start
// from column 2). Must be computed before truncating individual columns.
ssize_t idx_ip = sc_str_index_of_column(&line[idx_dev_name], 6, " ");
if (idx_ip == -1) {
return NULL;
}
// idx_ip is searched from &line[idx_dev_name]
idx_ip += idx_dev_name;
char *dev_name = &line[idx_dev_name];
sc_str_truncate(dev_name, len - idx_dev_name + 1, " \t");
char *ip = &line[idx_ip];
sc_str_truncate(ip, len - idx_ip + 1, " \t");
// Only consider lines where the device name starts with "wlan"
if (strncmp(dev_name, "wlan", sizeof("wlan") - 1)) {
LOGD("Device ip lookup: ignoring %s (%s)", ip, dev_name);
return NULL;
}
return strdup(ip);
}
char *
sc_adb_parse_device_ip_from_output(char *buf, size_t buf_len) {
size_t idx_line = 0;
while (idx_line < buf_len && buf[idx_line] != '\0') {
char *line = &buf[idx_line];
size_t len = sc_str_truncate(line, buf_len - idx_line, "\n");
// The same, but without any trailing '\r'
size_t line_len = sc_str_remove_trailing_cr(line, len);
char *ip = sc_adb_parse_device_ip_from_line(line, line_len);
if (ip) {
// Found
return ip;
}
// The next line starts after the '\n' (replaced by `\0`)
idx_line += len + 1;
}
return NULL;
}

View File

@ -1,14 +0,0 @@
#ifndef SC_ADB_PARSER_H
#define SC_ADB_PARSER_H
#include "common.h"
#include "stddef.h"
/**
* Parse the ip from the output of `adb shell ip route`
*/
char *
sc_adb_parse_device_ip_from_output(char *buf, size_t buf_len);
#endif

View File

@ -53,6 +53,9 @@
#define OPT_TCPIP 1033
#define OPT_RAW_KEY_EVENTS 1034
#define OPT_NO_DOWNSIZE_ON_ERROR 1035
#define OPT_OTG 1036
#define OPT_NO_CLEANUP 1037
#define OPT_PRINT_FPS 1038
struct sc_option {
char shortopt;
@ -115,7 +118,13 @@ static const struct sc_option options[] = {
.text = "Crop the device screen on the server.\n"
"The values are expressed in the device natural orientation "
"(typically, portrait for a phone, landscape for a tablet). "
"Any --max-size value is cmoputed on the cropped size.",
"Any --max-size value is computed on the cropped size.",
},
{
.shortopt = 'd',
.longopt = "select-usb",
.text = "Use USB device (if there is exactly one, like adb -d).\n"
"Also see -e (--select-tcpip).",
},
{
.longopt_id = OPT_DISABLE_SCREENSAVER,
@ -140,6 +149,12 @@ static const struct sc_option options[] = {
"This increases latency to compensate for jitter.\n"
"Default is 0 (no buffering).",
},
{
.shortopt = 'e',
.longopt = "select-tcpip",
.text = "Use TCP/IP device (if there is exactly one, like adb -e).\n"
"Also see -d (--select-usb).",
},
{
.longopt_id = OPT_ENCODER_NAME,
.longopt = "encoder",
@ -171,8 +186,7 @@ static const struct sc_option options[] = {
"It provides a better experience for IME users, and allows to "
"generate non-ASCII characters, contrary to the default "
"injection method.\n"
"It may only work over USB, and is currently only supported "
"on Linux.\n"
"It may only work over USB.\n"
"The keyboard layout must be configured (once and for all) on "
"the device, via Settings -> System -> Languages and input -> "
"Physical keyboard. This settings page can be started "
@ -224,8 +238,7 @@ static const struct sc_option options[] = {
"device directly (relative mouse mode).\n"
"LAlt, LSuper or RSuper toggle the capture mode, to give "
"control of the mouse back to the computer.\n"
"It may only work over USB, and is currently only supported "
"on Linux.\n"
"It may only work over USB.\n"
"Also see --hid-keyboard.",
},
{
@ -238,11 +251,12 @@ static const struct sc_option options[] = {
"Default is 0 (unlimited).",
},
{
.longopt_id = OPT_NO_DOWNSIZE_ON_ERROR,
.longopt = "no-downsize-on-error",
.text = "By default, on MediaCodec error, scrcpy automatically tries "
"again with a lower definition.\n"
"This option disables this behavior.",
.longopt_id = OPT_NO_CLEANUP,
.longopt = "no-cleanup",
.text = "By default, scrcpy removes the server binary from the device "
"and restores the device state (show touches, stay awake and "
"power mode) on exit.\n"
"This option disables this cleanup."
},
{
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
@ -253,6 +267,13 @@ static const struct sc_option options[] = {
"it changes.\n"
"This option disables this automatic synchronization."
},
{
.longopt_id = OPT_NO_DOWNSIZE_ON_ERROR,
.longopt = "no-downsize-on-error",
.text = "By default, on MediaCodec error, scrcpy automatically tries "
"again with a lower definition.\n"
"This option disables this behavior.",
},
{
.shortopt = 'n',
.longopt = "no-control",
@ -276,6 +297,21 @@ static const struct sc_option options[] = {
"mipmaps are automatically generated to improve downscaling "
"quality. This option disables the generation of mipmaps.",
},
{
.longopt_id = OPT_OTG,
.longopt = "otg",
.text = "Run in OTG mode: simulate physical keyboard and mouse, "
"as if the computer keyboard and mouse were plugged directly "
"to the device via an OTG cable.\n"
"In this mode, adb (USB debugging) is not necessary, and "
"mirroring is disabled.\n"
"LAlt, LSuper or RSuper toggle the mouse capture mode, to give "
"control of the mouse back to the computer.\n"
"If any of --hid-keyboard or --hid-mouse is set, only enable "
"keyboard or mouse respectively, otherwise enable both.\n"
"It may only work over USB.\n"
"See --hid-keyboard and --hid-mouse.",
},
{
.shortopt = 'p',
.longopt = "port",
@ -292,12 +328,18 @@ static const struct sc_option options[] = {
{
.longopt_id = OPT_PREFER_TEXT,
.longopt = "prefer-text",
.text = "Inject alpha characters and space as text events instead of"
.text = "Inject alpha characters and space as text events instead of "
"key events.\n"
"This avoids issues when combining multiple keys to enter a "
"special character, but breaks the expected behavior of alpha "
"keys in games (typically WASD).",
},
{
.longopt_id = OPT_PRINT_FPS,
.longopt = "print-fps",
.text = "Start FPS counter, to print framerate logs to the console. "
"It can be started or stopped at any time with MOD+i.",
},
{
.longopt_id = OPT_PUSH_TARGET,
.longopt = "push-target",
@ -380,6 +422,20 @@ static const struct sc_option options[] = {
"on exit.\n"
"It only shows physical touches (not clicks from scrcpy).",
},
{
.longopt_id = OPT_TCPIP,
.longopt = "tcpip",
.argdesc = "ip[:port]",
.optional_arg = true,
.text = "Configure and reconnect the device over TCP/IP.\n"
"If a destination address is provided, then scrcpy connects to "
"this address before starting. The device must listen on the "
"given TCP port (default is 5555).\n"
"If no destination address is provided, then scrcpy attempts "
"to find the IP address of the current device (typically "
"connected over USB), enables TCP/IP mode, then connects to "
"this address before starting.",
},
{
.longopt_id = OPT_TUNNEL_HOST,
.longopt = "tunnel-host",
@ -441,20 +497,6 @@ static const struct sc_option options[] = {
.text = "Keep the device on while scrcpy is running, when the device "
"is plugged in.",
},
{
.longopt_id = OPT_TCPIP,
.longopt = "tcpip",
.argdesc = "ip[:port]",
.optional_arg = true,
.text = "Configure and reconnect the device over TCP/IP.\n"
"If a destination address is provided, then scrcpy connects to "
"this address before starting. The device must listen on the "
"given TCP port (default is 5555).\n"
"If no destination address is provided, then scrcpy attempts "
"to find the IP address of the current device (typically "
"connected over USB), enables TCP/IP mode, then connects to "
"this address before starting.",
},
{
.longopt_id = OPT_WINDOW_BORDERLESS,
.longopt = "window-borderless",
@ -1303,6 +1345,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case 'd':
opts->select_usb = true;
break;
case 'e':
opts->select_tcpip = true;
break;
case 'f':
opts->fullscreen = true;
break;
@ -1318,12 +1366,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
args->help = true;
break;
case 'K':
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_HID;
break;
#else
LOGE("HID over AOA (-K/--hid-keyboard) is not supported on "
"this platform. It is only available on Linux.");
LOGE("HID over AOA (-K/--hid-keyboard) is disabled.");
return false;
#endif
case OPT_MAX_FPS:
@ -1337,12 +1384,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
break;
case 'M':
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_HID;
break;
#else
LOGE("HID over AOA (-M/--hid-mouse) is not supported on this"
"platform. It is only available on Linux.");
LOGE("HID over AOA (-M/--hid-mouse) is disabled.");
return false;
#endif
case OPT_LOCK_VIDEO_ORIENTATION:
@ -1500,12 +1546,27 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
case OPT_NO_DOWNSIZE_ON_ERROR:
opts->downsize_on_error = false;
break;
case OPT_NO_CLEANUP:
opts->cleanup = false;
break;
case OPT_PRINT_FPS:
opts->start_fps_counter = true;
break;
case OPT_OTG:
#ifdef HAVE_USB
opts->otg = true;
break;
#else
LOGE("OTG mode (--otg) is disabled.");
return false;
#endif
case OPT_V4L2_SINK:
#ifdef HAVE_V4L2
opts->v4l2_device = optarg;
break;
#else
LOGE("V4L2 (--v4l2-sink) is only available on Linux.");
LOGE("V4L2 (--v4l2-sink) is disabled (or unsupported on this "
"platform).");
return false;
#endif
case OPT_V4L2_BUFFER:
@ -1533,8 +1594,16 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
// If a TCP/IP address is provided, then tcpip must be enabled
assert(opts->tcpip || !opts->tcpip_dst);
if (opts->serial && opts->tcpip_dst) {
LOGE("Incompatible options: -s/--serial and --tcpip with an argument");
unsigned selectors = !!opts->serial
+ !!opts->tcpip_dst
+ opts->select_tcpip
+ opts->select_usb;
if (selectors > 1) {
LOGE("At most one device selector option may be passed, among:\n"
" --serial (-s)\n"
" --select-usb (-d)\n"
" --select-tcpip (-e)\n"
" --tcpip=<addr> (with an argument)");
return false;
}
@ -1610,6 +1679,55 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
}
#ifdef HAVE_USB
# ifdef _WIN32
if (!opts->otg && (opts->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID
|| opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_HID)) {
LOGE("On Windows, it is not possible to open a USB device already open "
"by another process (like adb).");
LOGE("Therefore, -K/--hid-keyboard and -M/--hid-mouse may only work in "
"OTG mode (--otg).");
return false;
}
# endif
if (opts->otg) {
// OTG mode is compatible with only very few options.
// Only report obvious errors.
if (opts->record_filename) {
LOGE("OTG mode: could not record");
return false;
}
if (opts->turn_screen_off) {
LOGE("OTG mode: could not turn screen off");
return false;
}
if (opts->stay_awake) {
LOGE("OTG mode: could not stay awake");
return false;
}
if (opts->show_touches) {
LOGE("OTG mode: could not request to show touches");
return false;
}
if (opts->power_off_on_close) {
LOGE("OTG mode: could not request power off on close");
return false;
}
if (opts->display_id) {
LOGE("OTG mode: could not select display");
return false;
}
# ifdef HAVE_V4L2
if (opts->v4l2_device) {
LOGE("OTG mode: could not sink to V4L2 device");
return false;
}
# endif
}
#endif
return true;
}

View File

@ -8,8 +8,10 @@
#ifndef __WIN32
# define PRIu64_ PRIu64
# define SC_PRIsizet "zu"
#else
# define PRIu64_ "I64u" // Windows...
# define SC_PRIsizet "Iu"
#endif
// In ffmpeg/doc/APIchanges:

View File

@ -63,17 +63,17 @@ static const char *const copy_key_labels[] = {
static void
write_position(uint8_t *buf, const struct sc_position *position) {
buffer_write32be(&buf[0], position->point.x);
buffer_write32be(&buf[4], position->point.y);
buffer_write16be(&buf[8], position->screen_size.width);
buffer_write16be(&buf[10], position->screen_size.height);
sc_write32be(&buf[0], position->point.x);
sc_write32be(&buf[4], position->point.y);
sc_write16be(&buf[8], position->screen_size.width);
sc_write16be(&buf[10], position->screen_size.height);
}
// write length (4 bytes) + string (non null-terminated)
static size_t
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
buffer_write32be(buf, len);
sc_write32be(buf, len);
memcpy(&buf[4], utf8, len);
return 4 + len;
}
@ -92,56 +92,55 @@ size_t
sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
buf[0] = msg->type;
switch (msg->type) {
case CONTROL_MSG_TYPE_INJECT_KEYCODE:
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
buf[1] = msg->inject_keycode.action;
buffer_write32be(&buf[2], msg->inject_keycode.keycode);
buffer_write32be(&buf[6], msg->inject_keycode.repeat);
buffer_write32be(&buf[10], msg->inject_keycode.metastate);
sc_write32be(&buf[2], msg->inject_keycode.keycode);
sc_write32be(&buf[6], msg->inject_keycode.repeat);
sc_write32be(&buf[10], msg->inject_keycode.metastate);
return 14;
case CONTROL_MSG_TYPE_INJECT_TEXT: {
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
size_t len =
write_string(msg->inject_text.text,
CONTROL_MSG_INJECT_TEXT_MAX_LENGTH, &buf[1]);
SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH, &buf[1]);
return 1 + len;
}
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
buf[1] = msg->inject_touch_event.action;
buffer_write64be(&buf[2], msg->inject_touch_event.pointer_id);
sc_write64be(&buf[2], msg->inject_touch_event.pointer_id);
write_position(&buf[10], &msg->inject_touch_event.position);
uint16_t pressure =
to_fixed_point_16(msg->inject_touch_event.pressure);
buffer_write16be(&buf[22], pressure);
buffer_write32be(&buf[24], msg->inject_touch_event.buttons);
sc_write16be(&buf[22], pressure);
sc_write32be(&buf[24], msg->inject_touch_event.buttons);
return 28;
case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
write_position(&buf[1], &msg->inject_scroll_event.position);
buffer_write32be(&buf[13],
sc_write32be(&buf[13],
(uint32_t) msg->inject_scroll_event.hscroll);
buffer_write32be(&buf[17],
sc_write32be(&buf[17],
(uint32_t) msg->inject_scroll_event.vscroll);
buffer_write32be(&buf[21], msg->inject_scroll_event.buttons);
sc_write32be(&buf[21], msg->inject_scroll_event.buttons);
return 25;
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
buf[1] = msg->inject_keycode.action;
return 2;
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
case SC_CONTROL_MSG_TYPE_GET_CLIPBOARD:
buf[1] = msg->get_clipboard.copy_key;
return 2;
case CONTROL_MSG_TYPE_SET_CLIPBOARD: {
buffer_write64be(&buf[1], msg->set_clipboard.sequence);
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,
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
&buf[10]);
return 10 + len;
}
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
case SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
buf[1] = msg->set_screen_power_mode.mode;
return 2;
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
case SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
case SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS:
case SC_CONTROL_MSG_TYPE_ROTATE_DEVICE:
// no additional data
return 1;
default:
@ -154,17 +153,17 @@ void
sc_control_msg_log(const struct sc_control_msg *msg) {
#define LOG_CMSG(fmt, ...) LOGV("input: " fmt, ## __VA_ARGS__)
switch (msg->type) {
case CONTROL_MSG_TYPE_INJECT_KEYCODE:
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
LOG_CMSG("key %-4s code=%d repeat=%" PRIu32 " meta=%06lx",
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action),
(int) msg->inject_keycode.keycode,
msg->inject_keycode.repeat,
(long) msg->inject_keycode.metastate);
break;
case CONTROL_MSG_TYPE_INJECT_TEXT:
case SC_CONTROL_MSG_TYPE_INJECT_TEXT:
LOG_CMSG("text \"%s\"", msg->inject_text.text);
break;
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: {
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: {
int action = msg->inject_touch_event.action
& AMOTION_EVENT_ACTION_MASK;
uint64_t id = msg->inject_touch_event.pointer_id;
@ -191,7 +190,7 @@ sc_control_msg_log(const struct sc_control_msg *msg) {
}
break;
}
case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
LOG_CMSG("scroll position=%" PRIi32 ",%" PRIi32 " hscroll=%" PRIi32
" vscroll=%" PRIi32 " buttons=%06lx",
msg->inject_scroll_event.position.point.x,
@ -200,34 +199,34 @@ sc_control_msg_log(const struct sc_control_msg *msg) {
msg->inject_scroll_event.vscroll,
(long) msg->inject_scroll_event.buttons);
break;
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
LOG_CMSG("back-or-screen-on %s",
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
break;
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
case SC_CONTROL_MSG_TYPE_GET_CLIPBOARD:
LOG_CMSG("get clipboard copy_key=%s",
copy_key_labels[msg->get_clipboard.copy_key]);
break;
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
LOG_CMSG("clipboard %" PRIu64_ " %s \"%s\"",
msg->set_clipboard.sequence,
msg->set_clipboard.paste ? "paste" : "nopaste",
msg->set_clipboard.text);
break;
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
case SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
LOG_CMSG("power mode %s",
SCREEN_POWER_MODE_LABEL(msg->set_screen_power_mode.mode));
break;
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
LOG_CMSG("expand notification panel");
break;
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
case SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
LOG_CMSG("expand settings panel");
break;
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
case SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS:
LOG_CMSG("collapse panels");
break;
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
case SC_CONTROL_MSG_TYPE_ROTATE_DEVICE:
LOG_CMSG("rotate device");
break;
default:
@ -239,10 +238,10 @@ sc_control_msg_log(const struct sc_control_msg *msg) {
void
sc_control_msg_destroy(struct sc_control_msg *msg) {
switch (msg->type) {
case CONTROL_MSG_TYPE_INJECT_TEXT:
case SC_CONTROL_MSG_TYPE_INJECT_TEXT:
free(msg->inject_text.text);
break;
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
free(msg->set_clipboard.text);
break;
default:

View File

@ -11,40 +11,40 @@
#include "android/keycodes.h"
#include "coords.h"
#define CONTROL_MSG_MAX_SIZE (1 << 18) // 256k
#define SC_CONTROL_MSG_MAX_SIZE (1 << 18) // 256k
#define CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300
#define SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300
// type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (CONTROL_MSG_MAX_SIZE - 14)
#define SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (SC_CONTROL_MSG_MAX_SIZE - 14)
#define POINTER_ID_MOUSE UINT64_C(-1)
#define POINTER_ID_VIRTUAL_FINGER UINT64_C(-2)
enum sc_control_msg_type {
CONTROL_MSG_TYPE_INJECT_KEYCODE,
CONTROL_MSG_TYPE_INJECT_TEXT,
CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
CONTROL_MSG_TYPE_COLLAPSE_PANELS,
CONTROL_MSG_TYPE_GET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
CONTROL_MSG_TYPE_ROTATE_DEVICE,
SC_CONTROL_MSG_TYPE_INJECT_KEYCODE,
SC_CONTROL_MSG_TYPE_INJECT_TEXT,
SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS,
SC_CONTROL_MSG_TYPE_GET_CLIPBOARD,
SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
SC_CONTROL_MSG_TYPE_ROTATE_DEVICE,
};
enum screen_power_mode {
enum sc_screen_power_mode {
// see <https://android.googlesource.com/platform/frameworks/base.git/+/pie-release-2/core/java/android/view/SurfaceControl.java#305>
SCREEN_POWER_MODE_OFF = 0,
SCREEN_POWER_MODE_NORMAL = 2,
SC_SCREEN_POWER_MODE_OFF = 0,
SC_SCREEN_POWER_MODE_NORMAL = 2,
};
enum get_clipboard_copy_key {
GET_CLIPBOARD_COPY_KEY_NONE,
GET_CLIPBOARD_COPY_KEY_COPY,
GET_CLIPBOARD_COPY_KEY_CUT,
enum sc_copy_key {
SC_COPY_KEY_NONE,
SC_COPY_KEY_COPY,
SC_COPY_KEY_CUT,
};
struct sc_control_msg {
@ -77,7 +77,7 @@ struct sc_control_msg {
// screen may only be turned on on ACTION_DOWN
} back_or_screen_on;
struct {
enum get_clipboard_copy_key copy_key;
enum sc_copy_key copy_key;
} get_clipboard;
struct {
uint64_t sequence;
@ -85,7 +85,7 @@ struct sc_control_msg {
bool paste;
} set_clipboard;
struct {
enum screen_power_mode mode;
enum sc_screen_power_mode mode;
} set_screen_power_mode;
};
};

View File

@ -66,7 +66,7 @@ sc_controller_push_msg(struct sc_controller *controller,
static bool
process_msg(struct sc_controller *controller,
const struct sc_control_msg *msg) {
static unsigned char serialized_msg[CONTROL_MSG_MAX_SIZE];
static unsigned char serialized_msg[SC_CONTROL_MSG_MAX_SIZE];
size_t length = sc_control_msg_serialize(msg, serialized_msg);
if (!length) {
return false;
@ -113,7 +113,7 @@ sc_controller_start(struct sc_controller *controller) {
bool ok = sc_thread_create(&controller->thread, run_controller,
"scrcpy-ctl", controller);
if (!ok) {
LOGC("Could not start controller thread");
LOGE("Could not start controller thread");
return false;
}

View File

@ -9,10 +9,10 @@
#include "util/log.h"
/** Downcast packet_sink to decoder */
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
#define DOWNCAST(SINK) container_of(SINK, struct sc_decoder, packet_sink)
static void
decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
sc_decoder_close_first_sinks(struct sc_decoder *decoder, unsigned count) {
while (count) {
struct sc_frame_sink *sink = decoder->sinks[--count];
sink->ops->close(sink);
@ -20,17 +20,17 @@ decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
}
static inline void
decoder_close_sinks(struct decoder *decoder) {
decoder_close_first_sinks(decoder, decoder->sink_count);
sc_decoder_close_sinks(struct sc_decoder *decoder) {
sc_decoder_close_first_sinks(decoder, decoder->sink_count);
}
static bool
decoder_open_sinks(struct decoder *decoder) {
sc_decoder_open_sinks(struct sc_decoder *decoder) {
for (unsigned i = 0; i < decoder->sink_count; ++i) {
struct sc_frame_sink *sink = decoder->sinks[i];
if (!sink->ops->open(sink)) {
LOGE("Could not open frame sink %d", i);
decoder_close_first_sinks(decoder, i);
sc_decoder_close_first_sinks(decoder, i);
return false;
}
}
@ -39,7 +39,7 @@ decoder_open_sinks(struct decoder *decoder) {
}
static bool
decoder_open(struct decoder *decoder, const AVCodec *codec) {
sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx = avcodec_alloc_context3(codec);
if (!decoder->codec_ctx) {
LOG_OOM();
@ -62,7 +62,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
return false;
}
if (!decoder_open_sinks(decoder)) {
if (!sc_decoder_open_sinks(decoder)) {
LOGE("Could not open decoder sinks");
av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx);
@ -74,15 +74,15 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
}
static void
decoder_close(struct decoder *decoder) {
decoder_close_sinks(decoder);
sc_decoder_close(struct sc_decoder *decoder) {
sc_decoder_close_sinks(decoder);
av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx);
avcodec_free_context(&decoder->codec_ctx);
}
static bool
push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
push_frame_to_sinks(struct sc_decoder *decoder, const AVFrame *frame) {
for (unsigned i = 0; i < decoder->sink_count; ++i) {
struct sc_frame_sink *sink = decoder->sinks[i];
if (!sink->ops->push(sink, frame)) {
@ -95,7 +95,7 @@ push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
}
static bool
decoder_push(struct decoder *decoder, const AVPacket *packet) {
sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
bool is_config = packet->pts == AV_NOPTS_VALUE;
if (is_config) {
// nothing to do
@ -124,39 +124,40 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
}
static bool
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct decoder *decoder = DOWNCAST(sink);
return decoder_open(decoder, codec);
sc_decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct sc_decoder *decoder = DOWNCAST(sink);
return sc_decoder_open(decoder, codec);
}
static void
decoder_packet_sink_close(struct sc_packet_sink *sink) {
struct decoder *decoder = DOWNCAST(sink);
decoder_close(decoder);
sc_decoder_packet_sink_close(struct sc_packet_sink *sink) {
struct sc_decoder *decoder = DOWNCAST(sink);
sc_decoder_close(decoder);
}
static bool
decoder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
struct decoder *decoder = DOWNCAST(sink);
return decoder_push(decoder, packet);
sc_decoder_packet_sink_push(struct sc_packet_sink *sink,
const AVPacket *packet) {
struct sc_decoder *decoder = DOWNCAST(sink);
return sc_decoder_push(decoder, packet);
}
void
decoder_init(struct decoder *decoder) {
sc_decoder_init(struct sc_decoder *decoder) {
decoder->sink_count = 0;
static const struct sc_packet_sink_ops ops = {
.open = decoder_packet_sink_open,
.close = decoder_packet_sink_close,
.push = decoder_packet_sink_push,
.open = sc_decoder_packet_sink_open,
.close = sc_decoder_packet_sink_close,
.push = sc_decoder_packet_sink_push,
};
decoder->packet_sink.ops = &ops;
}
void
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink) {
assert(decoder->sink_count < DECODER_MAX_SINKS);
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink) {
assert(decoder->sink_count < SC_DECODER_MAX_SINKS);
assert(sink);
assert(sink->ops);
decoder->sinks[decoder->sink_count++] = sink;

View File

@ -1,5 +1,5 @@
#ifndef DECODER_H
#define DECODER_H
#ifndef SC_DECODER_H
#define SC_DECODER_H
#include "common.h"
@ -9,12 +9,12 @@
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#define DECODER_MAX_SINKS 2
#define SC_DECODER_MAX_SINKS 2
struct decoder {
struct sc_decoder {
struct sc_packet_sink packet_sink; // packet sink trait
struct sc_frame_sink *sinks[DECODER_MAX_SINKS];
struct sc_frame_sink *sinks[SC_DECODER_MAX_SINKS];
unsigned sink_count;
AVCodecContext *codec_ctx;
@ -22,9 +22,9 @@ struct decoder {
};
void
decoder_init(struct decoder *decoder);
sc_decoder_init(struct sc_decoder *decoder);
void
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink);
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink);
#endif

280
app/src/demuxer.c Normal file
View File

@ -0,0 +1,280 @@
#include "demuxer.h"
#include <assert.h>
#include <libavutil/time.h>
#include <unistd.h>
#include "decoder.h"
#include "events.h"
#include "recorder.h"
#include "util/buffer_util.h"
#include "util/log.h"
#define SC_PACKET_HEADER_SIZE 12
#define SC_PACKET_FLAG_CONFIG (UINT64_C(1) << 63)
#define SC_PACKET_FLAG_KEY_FRAME (UINT64_C(1) << 62)
#define SC_PACKET_PTS_MASK (SC_PACKET_FLAG_KEY_FRAME - 1)
static bool
sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
// The video stream contains raw packets, without time information. When we
// record, we retrieve the timestamps separately, from a "meta" header
// added by the server before each raw packet.
//
// The "meta" header length is 12 bytes:
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
// <-------------> <-----> <-----------------------------...
// PTS packet raw packet
// size
//
// It is followed by <packet_size> bytes containing the packet/frame.
//
// The most significant bits of the PTS are used for packet flags:
//
// byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
// CK...... ........ ........ ........ ........ ........ ........ ........
// ^^<------------------------------------------------------------------->
// || PTS
// | `- config packet
// `-- key frame
uint8_t header[SC_PACKET_HEADER_SIZE];
ssize_t r = net_recv_all(demuxer->socket, header, SC_PACKET_HEADER_SIZE);
if (r < SC_PACKET_HEADER_SIZE) {
return false;
}
uint64_t pts_flags = sc_read64be(header);
uint32_t len = sc_read32be(&header[8]);
assert(len);
if (av_new_packet(packet, len)) {
LOG_OOM();
return false;
}
r = net_recv_all(demuxer->socket, packet->data, len);
if (r < 0 || ((uint32_t) r) < len) {
av_packet_unref(packet);
return false;
}
if (pts_flags & SC_PACKET_FLAG_CONFIG) {
packet->pts = AV_NOPTS_VALUE;
} else {
packet->pts = pts_flags & SC_PACKET_PTS_MASK;
}
if (pts_flags & SC_PACKET_FLAG_KEY_FRAME) {
packet->flags |= AV_PKT_FLAG_KEY;
}
packet->dts = packet->pts;
return true;
}
static bool
push_packet_to_sinks(struct sc_demuxer *demuxer, const AVPacket *packet) {
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
struct sc_packet_sink *sink = demuxer->sinks[i];
if (!sink->ops->push(sink, packet)) {
LOGE("Could not send config packet to sink %d", i);
return false;
}
}
return true;
}
static bool
sc_demuxer_push_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
bool is_config = packet->pts == AV_NOPTS_VALUE;
// A config packet must not be decoded immediately (it contains no
// frame); instead, it must be concatenated with the future data packet.
if (demuxer->pending || is_config) {
size_t offset;
if (demuxer->pending) {
offset = demuxer->pending->size;
if (av_grow_packet(demuxer->pending, packet->size)) {
LOG_OOM();
return false;
}
} else {
offset = 0;
demuxer->pending = av_packet_alloc();
if (!demuxer->pending) {
LOG_OOM();
return false;
}
if (av_new_packet(demuxer->pending, packet->size)) {
LOG_OOM();
av_packet_free(&demuxer->pending);
return false;
}
}
memcpy(demuxer->pending->data + offset, packet->data, packet->size);
if (!is_config) {
// prepare the concat packet to send to the decoder
demuxer->pending->pts = packet->pts;
demuxer->pending->dts = packet->dts;
demuxer->pending->flags = packet->flags;
packet = demuxer->pending;
}
}
bool ok = push_packet_to_sinks(demuxer, packet);
if (!is_config && demuxer->pending) {
// the pending packet must be discarded (consumed or error)
av_packet_free(&demuxer->pending);
}
if (!ok) {
LOGE("Could not process packet");
return false;
}
return true;
}
static void
sc_demuxer_close_first_sinks(struct sc_demuxer *demuxer, unsigned count) {
while (count) {
struct sc_packet_sink *sink = demuxer->sinks[--count];
sink->ops->close(sink);
}
}
static inline void
sc_demuxer_close_sinks(struct sc_demuxer *demuxer) {
sc_demuxer_close_first_sinks(demuxer, demuxer->sink_count);
}
static bool
sc_demuxer_open_sinks(struct sc_demuxer *demuxer, const AVCodec *codec) {
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
struct sc_packet_sink *sink = demuxer->sinks[i];
if (!sink->ops->open(sink, codec)) {
LOGE("Could not open packet sink %d", i);
sc_demuxer_close_first_sinks(demuxer, i);
return false;
}
}
return true;
}
static int
run_demuxer(void *data) {
struct sc_demuxer *demuxer = data;
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
LOGE("H.264 decoder not found");
goto end;
}
demuxer->codec_ctx = avcodec_alloc_context3(codec);
if (!demuxer->codec_ctx) {
LOG_OOM();
goto end;
}
if (!sc_demuxer_open_sinks(demuxer, codec)) {
LOGE("Could not open demuxer sinks");
goto finally_free_codec_ctx;
}
demuxer->parser = av_parser_init(AV_CODEC_ID_H264);
if (!demuxer->parser) {
LOGE("Could not initialize parser");
goto finally_close_sinks;
}
// We must only pass complete frames to av_parser_parse2()!
// It's more complicated, but this allows to reduce the latency by 1 frame!
demuxer->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
AVPacket *packet = av_packet_alloc();
if (!packet) {
LOG_OOM();
goto finally_close_parser;
}
for (;;) {
bool ok = sc_demuxer_recv_packet(demuxer, packet);
if (!ok) {
// end of stream
break;
}
ok = sc_demuxer_push_packet(demuxer, packet);
av_packet_unref(packet);
if (!ok) {
// cannot process packet (error already logged)
break;
}
}
LOGD("End of frames");
if (demuxer->pending) {
av_packet_free(&demuxer->pending);
}
av_packet_free(&packet);
finally_close_parser:
av_parser_close(demuxer->parser);
finally_close_sinks:
sc_demuxer_close_sinks(demuxer);
finally_free_codec_ctx:
avcodec_free_context(&demuxer->codec_ctx);
end:
demuxer->cbs->on_eos(demuxer, demuxer->cbs_userdata);
return 0;
}
void
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata) {
demuxer->socket = socket;
demuxer->pending = NULL;
demuxer->sink_count = 0;
assert(cbs && cbs->on_eos);
demuxer->cbs = cbs;
demuxer->cbs_userdata = cbs_userdata;
}
void
sc_demuxer_add_sink(struct sc_demuxer *demuxer, struct sc_packet_sink *sink) {
assert(demuxer->sink_count < SC_DEMUXER_MAX_SINKS);
assert(sink);
assert(sink->ops);
demuxer->sinks[demuxer->sink_count++] = sink;
}
bool
sc_demuxer_start(struct sc_demuxer *demuxer) {
LOGD("Starting demuxer thread");
bool ok = sc_thread_create(&demuxer->thread, run_demuxer, "scrcpy-demuxer",
demuxer);
if (!ok) {
LOGE("Could not start demuxer thread");
return false;
}
return true;
}
void
sc_demuxer_join(struct sc_demuxer *demuxer) {
sc_thread_join(&demuxer->thread, NULL);
}

51
app/src/demuxer.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef SC_DEMUXER_H
#define SC_DEMUXER_H
#include "common.h"
#include <stdbool.h>
#include <stdint.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "trait/packet_sink.h"
#include "util/net.h"
#include "util/thread.h"
#define SC_DEMUXER_MAX_SINKS 2
struct sc_demuxer {
sc_socket socket;
sc_thread thread;
struct sc_packet_sink *sinks[SC_DEMUXER_MAX_SINKS];
unsigned sink_count;
AVCodecContext *codec_ctx;
AVCodecParserContext *parser;
// successive packets may need to be concatenated, until a non-config
// packet is available
AVPacket *pending;
const struct sc_demuxer_callbacks *cbs;
void *cbs_userdata;
};
struct sc_demuxer_callbacks {
void (*on_eos)(struct sc_demuxer *demuxer, void *userdata);
};
void
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata);
void
sc_demuxer_add_sink(struct sc_demuxer *demuxer, struct sc_packet_sink *sink);
bool
sc_demuxer_start(struct sc_demuxer *demuxer);
void
sc_demuxer_join(struct sc_demuxer *demuxer);
#endif

View File

@ -18,7 +18,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
msg->type = buf[0];
switch (msg->type) {
case DEVICE_MSG_TYPE_CLIPBOARD: {
size_t clipboard_len = buffer_read32be(&buf[1]);
size_t clipboard_len = sc_read32be(&buf[1]);
if (clipboard_len > len - 5) {
return 0; // not available
}
@ -36,7 +36,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
return 5 + clipboard_len;
}
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: {
uint64_t sequence = buffer_read64be(&buf[1]);
uint64_t sequence = sc_read64be(&buf[1]);
msg->ack_clipboard.sequence = sequence;
return 9;
}

View File

@ -2,3 +2,4 @@
#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1)
#define EVENT_SERVER_CONNECTION_FAILED (SDL_USEREVENT + 2)
#define EVENT_SERVER_CONNECTED (SDL_USEREVENT + 3)
#define EVENT_USB_DEVICE_DISCONNECTED (SDL_USEREVENT + 4)

View File

@ -3,7 +3,7 @@
#include <assert.h>
#include <string.h>
#include "adb.h"
#include "adb/adb.h"
#include "util/log.h"
#include "util/process_intr.h"
@ -129,7 +129,7 @@ run_file_pusher(void *data) {
if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) {
LOGI("Installing %s...", req.file);
bool ok = adb_install(intr, serial, req.file, 0);
bool ok = sc_adb_install(intr, serial, req.file, 0);
if (ok) {
LOGI("%s successfully installed", req.file);
} else {
@ -137,7 +137,7 @@ run_file_pusher(void *data) {
}
} else {
LOGI("Pushing %s...", req.file);
bool ok = adb_push(intr, serial, req.file, push_target, 0);
bool ok = sc_adb_push(intr, serial, req.file, push_target, 0);
if (ok) {
LOGI("%s successfully pushed to %s", req.file, push_target);
} else {
@ -156,7 +156,7 @@ sc_file_pusher_start(struct sc_file_pusher *fp) {
bool ok = sc_thread_create(&fp->thread, run_file_pusher, "scrcpy-file", fp);
if (!ok) {
LOGC("Could not start file_pusher thread");
LOGE("Could not start file_pusher thread");
return false;
}

View File

@ -5,7 +5,6 @@
#include <stdbool.h>
#include "adb.h"
#include "util/cbuf.h"
#include "util/thread.h"
#include "util/intr.h"

View File

@ -4,10 +4,10 @@
#include "util/log.h"
#define FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
#define SC_FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
bool
fps_counter_init(struct fps_counter *counter) {
sc_fps_counter_init(struct sc_fps_counter *counter) {
bool ok = sc_mutex_init(&counter->mutex);
if (!ok) {
return false;
@ -27,26 +27,26 @@ fps_counter_init(struct fps_counter *counter) {
}
void
fps_counter_destroy(struct fps_counter *counter) {
sc_fps_counter_destroy(struct sc_fps_counter *counter) {
sc_cond_destroy(&counter->state_cond);
sc_mutex_destroy(&counter->mutex);
}
static inline bool
is_started(struct fps_counter *counter) {
is_started(struct sc_fps_counter *counter) {
return atomic_load_explicit(&counter->started, memory_order_acquire);
}
static inline void
set_started(struct fps_counter *counter, bool started) {
set_started(struct sc_fps_counter *counter, bool started) {
atomic_store_explicit(&counter->started, started, memory_order_release);
}
// must be called with mutex locked
static void
display_fps(struct fps_counter *counter) {
display_fps(struct sc_fps_counter *counter) {
unsigned rendered_per_second =
counter->nr_rendered * SC_TICK_FREQ / FPS_COUNTER_INTERVAL;
counter->nr_rendered * SC_TICK_FREQ / SC_FPS_COUNTER_INTERVAL;
if (counter->nr_skipped) {
LOGI("%u fps (+%u frames skipped)", rendered_per_second,
counter->nr_skipped);
@ -57,7 +57,7 @@ display_fps(struct fps_counter *counter) {
// must be called with mutex locked
static void
check_interval_expired(struct fps_counter *counter, uint32_t now) {
check_interval_expired(struct sc_fps_counter *counter, sc_tick now) {
if (now < counter->next_timestamp) {
return;
}
@ -67,13 +67,13 @@ check_interval_expired(struct fps_counter *counter, uint32_t now) {
counter->nr_skipped = 0;
// add a multiple of the interval
uint32_t elapsed_slices =
(now - counter->next_timestamp) / FPS_COUNTER_INTERVAL + 1;
counter->next_timestamp += FPS_COUNTER_INTERVAL * elapsed_slices;
(now - counter->next_timestamp) / SC_FPS_COUNTER_INTERVAL + 1;
counter->next_timestamp += SC_FPS_COUNTER_INTERVAL * elapsed_slices;
}
static int
run_fps_counter(void *data) {
struct fps_counter *counter = data;
struct sc_fps_counter *counter = data;
sc_mutex_lock(&counter->mutex);
while (!counter->interrupted) {
@ -94,9 +94,9 @@ run_fps_counter(void *data) {
}
bool
fps_counter_start(struct fps_counter *counter) {
sc_fps_counter_start(struct sc_fps_counter *counter) {
sc_mutex_lock(&counter->mutex);
counter->next_timestamp = sc_tick_now() + FPS_COUNTER_INTERVAL;
counter->next_timestamp = sc_tick_now() + SC_FPS_COUNTER_INTERVAL;
counter->nr_rendered = 0;
counter->nr_skipped = 0;
sc_mutex_unlock(&counter->mutex);
@ -117,22 +117,24 @@ fps_counter_start(struct fps_counter *counter) {
counter->thread_started = true;
}
LOGI("FPS counter started");
return true;
}
void
fps_counter_stop(struct fps_counter *counter) {
sc_fps_counter_stop(struct sc_fps_counter *counter) {
set_started(counter, false);
sc_cond_signal(&counter->state_cond);
LOGI("FPS counter stopped");
}
bool
fps_counter_is_started(struct fps_counter *counter) {
sc_fps_counter_is_started(struct sc_fps_counter *counter) {
return is_started(counter);
}
void
fps_counter_interrupt(struct fps_counter *counter) {
sc_fps_counter_interrupt(struct sc_fps_counter *counter) {
if (!counter->thread_started) {
return;
}
@ -145,7 +147,7 @@ fps_counter_interrupt(struct fps_counter *counter) {
}
void
fps_counter_join(struct fps_counter *counter) {
sc_fps_counter_join(struct sc_fps_counter *counter) {
if (counter->thread_started) {
// interrupted must be set by the thread calling join(), so no need to
// lock for the assertion
@ -156,7 +158,7 @@ fps_counter_join(struct fps_counter *counter) {
}
void
fps_counter_add_rendered_frame(struct fps_counter *counter) {
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter) {
if (!is_started(counter)) {
return;
}
@ -169,7 +171,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
}
void
fps_counter_add_skipped_frame(struct fps_counter *counter) {
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter) {
if (!is_started(counter)) {
return;
}

View File

@ -9,7 +9,7 @@
#include "util/thread.h"
struct fps_counter {
struct sc_fps_counter {
sc_thread thread;
sc_mutex mutex;
sc_cond state_cond;
@ -28,32 +28,32 @@ struct fps_counter {
};
bool
fps_counter_init(struct fps_counter *counter);
sc_fps_counter_init(struct sc_fps_counter *counter);
void
fps_counter_destroy(struct fps_counter *counter);
sc_fps_counter_destroy(struct sc_fps_counter *counter);
bool
fps_counter_start(struct fps_counter *counter);
sc_fps_counter_start(struct sc_fps_counter *counter);
void
fps_counter_stop(struct fps_counter *counter);
sc_fps_counter_stop(struct sc_fps_counter *counter);
bool
fps_counter_is_started(struct fps_counter *counter);
sc_fps_counter_is_started(struct sc_fps_counter *counter);
// request to stop the thread (on quit)
// must be called before fps_counter_join()
// must be called before sc_fps_counter_join()
void
fps_counter_interrupt(struct fps_counter *counter);
sc_fps_counter_interrupt(struct sc_fps_counter *counter);
void
fps_counter_join(struct fps_counter *counter);
sc_fps_counter_join(struct sc_fps_counter *counter);
void
fps_counter_add_rendered_frame(struct fps_counter *counter);
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter);
void
fps_counter_add_skipped_frame(struct fps_counter *counter);
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter);
#endif

View File

@ -51,8 +51,6 @@ swap_frames(AVFrame **lhs, AVFrame **rhs) {
bool
sc_frame_buffer_push(struct sc_frame_buffer *fb, const AVFrame *frame,
bool *previous_frame_skipped) {
sc_mutex_lock(&fb->mutex);
// Use a temporary frame to preserve pending_frame in case of error.
// tmp_frame is an empty frame, no need to call av_frame_unref() beforehand.
int r = av_frame_ref(fb->tmp_frame, frame);
@ -61,6 +59,8 @@ sc_frame_buffer_push(struct sc_frame_buffer *fb, const AVFrame *frame,
return false;
}
sc_mutex_lock(&fb->mutex);
// Now that av_frame_ref() succeeded, we can replace the previous
// pending_frame
swap_frames(&fb->pending_frame, &fb->tmp_frame);

View File

@ -377,4 +377,76 @@ struct sc_touch_event {
float pressure;
};
static inline uint16_t
sc_mods_state_from_sdl(uint16_t mods_state) {
return mods_state;
}
static inline enum sc_keycode
sc_keycode_from_sdl(SDL_Keycode keycode) {
return (enum sc_keycode) keycode;
}
static inline enum sc_scancode
sc_scancode_from_sdl(SDL_Scancode scancode) {
return (enum sc_scancode) scancode;
}
static inline enum sc_action
sc_action_from_sdl_keyboard_type(uint32_t type) {
assert(type == SDL_KEYDOWN || type == SDL_KEYUP);
if (type == SDL_KEYDOWN) {
return SC_ACTION_DOWN;
}
return SC_ACTION_UP;
}
static inline enum sc_action
sc_action_from_sdl_mousebutton_type(uint32_t type) {
assert(type == SDL_MOUSEBUTTONDOWN || type == SDL_MOUSEBUTTONUP);
if (type == SDL_MOUSEBUTTONDOWN) {
return SC_ACTION_DOWN;
}
return SC_ACTION_UP;
}
static inline enum sc_touch_action
sc_touch_action_from_sdl(uint32_t type) {
assert(type == SDL_FINGERMOTION || type == SDL_FINGERDOWN ||
type == SDL_FINGERUP);
if (type == SDL_FINGERMOTION) {
return SC_TOUCH_ACTION_MOVE;
}
if (type == SDL_FINGERDOWN) {
return SC_TOUCH_ACTION_DOWN;
}
return SC_TOUCH_ACTION_UP;
}
static inline enum sc_mouse_button
sc_mouse_button_from_sdl(uint8_t button) {
if (button >= SDL_BUTTON_LEFT && button <= SDL_BUTTON_X2) {
// SC_MOUSE_BUTTON_* constants are initialized from SDL_BUTTON(index)
return SDL_BUTTON(button);
}
return SC_MOUSE_BUTTON_UNKNOWN;
}
static inline uint8_t
sc_mouse_buttons_state_from_sdl(uint32_t buttons_state,
bool forward_all_clicks) {
assert(buttons_state < 0x100); // fits in uint8_t
uint8_t mask = SC_MOUSE_BUTTON_LEFT;
if (forward_all_clicks) {
mask |= SC_MOUSE_BUTTON_RIGHT
| SC_MOUSE_BUTTON_MIDDLE
| SC_MOUSE_BUTTON_X1
| SC_MOUSE_BUTTON_X2;
}
return buttons_state & mask;
}
#endif

View File

@ -7,78 +7,6 @@
#include "screen.h"
#include "util/log.h"
static inline uint16_t
sc_mods_state_from_sdl(uint16_t mods_state) {
return mods_state;
}
static inline enum sc_keycode
sc_keycode_from_sdl(SDL_Keycode keycode) {
return (enum sc_keycode) keycode;
}
static inline enum sc_scancode
sc_scancode_from_sdl(SDL_Scancode scancode) {
return (enum sc_scancode) scancode;
}
static inline enum sc_action
sc_action_from_sdl_keyboard_type(uint32_t type) {
assert(type == SDL_KEYDOWN || type == SDL_KEYUP);
if (type == SDL_KEYDOWN) {
return SC_ACTION_DOWN;
}
return SC_ACTION_UP;
}
static inline enum sc_action
sc_action_from_sdl_mousebutton_type(uint32_t type) {
assert(type == SDL_MOUSEBUTTONDOWN || type == SDL_MOUSEBUTTONUP);
if (type == SDL_MOUSEBUTTONDOWN) {
return SC_ACTION_DOWN;
}
return SC_ACTION_UP;
}
static inline enum sc_touch_action
sc_touch_action_from_sdl(uint32_t type) {
assert(type == SDL_FINGERMOTION || type == SDL_FINGERDOWN ||
type == SDL_FINGERUP);
if (type == SDL_FINGERMOTION) {
return SC_TOUCH_ACTION_MOVE;
}
if (type == SDL_FINGERDOWN) {
return SC_TOUCH_ACTION_DOWN;
}
return SC_TOUCH_ACTION_UP;
}
static inline enum sc_mouse_button
sc_mouse_button_from_sdl(uint8_t button) {
if (button >= SDL_BUTTON_LEFT && button <= SDL_BUTTON_X2) {
// SC_MOUSE_BUTTON_* constants are initialized from SDL_BUTTON(index)
return SDL_BUTTON(button);
}
return SC_MOUSE_BUTTON_UNKNOWN;
}
static inline uint8_t
sc_mouse_buttons_state_from_sdl(uint32_t buttons_state,
bool forward_all_clicks) {
assert(buttons_state < 0x100); // fits in uint8_t
uint8_t mask = SC_MOUSE_BUTTON_LEFT;
if (forward_all_clicks) {
mask |= SC_MOUSE_BUTTON_RIGHT
| SC_MOUSE_BUTTON_MIDDLE
| SC_MOUSE_BUTTON_X1
| SC_MOUSE_BUTTON_X2;
}
return buttons_state & mask;
}
#define SC_SDL_SHORTCUT_MODS_MASK (KMOD_CTRL | KMOD_ALT | KMOD_GUI)
static inline uint16_t
@ -161,7 +89,7 @@ send_keycode(struct sc_controller *controller, enum android_keycode keycode,
enum sc_action action, const char *name) {
// send DOWN event
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
msg.type = SC_CONTROL_MSG_TYPE_INJECT_KEYCODE;
msg.inject_keycode.action = action == SC_ACTION_DOWN
? AKEY_EVENT_ACTION_DOWN
: AKEY_EVENT_ACTION_UP;
@ -215,7 +143,7 @@ static void
press_back_or_turn_screen_on(struct sc_controller *controller,
enum sc_action action) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
msg.type = SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
msg.back_or_screen_on.action = action == SC_ACTION_DOWN
? AKEY_EVENT_ACTION_DOWN
: AKEY_EVENT_ACTION_UP;
@ -228,7 +156,7 @@ press_back_or_turn_screen_on(struct sc_controller *controller,
static void
expand_notification_panel(struct sc_controller *controller) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL;
msg.type = SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL;
if (!sc_controller_push_msg(controller, &msg)) {
LOGW("Could not request 'expand notification panel'");
@ -238,7 +166,7 @@ expand_notification_panel(struct sc_controller *controller) {
static void
expand_settings_panel(struct sc_controller *controller) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL;
msg.type = SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL;
if (!sc_controller_push_msg(controller, &msg)) {
LOGW("Could not request 'expand settings panel'");
@ -248,7 +176,7 @@ expand_settings_panel(struct sc_controller *controller) {
static void
collapse_panels(struct sc_controller *controller) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_COLLAPSE_PANELS;
msg.type = SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS;
if (!sc_controller_push_msg(controller, &msg)) {
LOGW("Could not request 'collapse notification panel'");
@ -257,9 +185,9 @@ collapse_panels(struct sc_controller *controller) {
static bool
get_device_clipboard(struct sc_controller *controller,
enum get_clipboard_copy_key copy_key) {
enum sc_copy_key copy_key) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_GET_CLIPBOARD;
msg.type = SC_CONTROL_MSG_TYPE_GET_CLIPBOARD;
msg.get_clipboard.copy_key = copy_key;
if (!sc_controller_push_msg(controller, &msg)) {
@ -287,7 +215,7 @@ set_device_clipboard(struct sc_controller *controller, bool paste,
}
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
msg.type = SC_CONTROL_MSG_TYPE_SET_CLIPBOARD;
msg.set_clipboard.sequence = sequence;
msg.set_clipboard.text = text_dup;
msg.set_clipboard.paste = paste;
@ -303,9 +231,9 @@ set_device_clipboard(struct sc_controller *controller, bool paste,
static void
set_screen_power_mode(struct sc_controller *controller,
enum screen_power_mode mode) {
enum sc_screen_power_mode mode) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
msg.type = SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
msg.set_screen_power_mode.mode = mode;
if (!sc_controller_push_msg(controller, &msg)) {
@ -314,18 +242,14 @@ set_screen_power_mode(struct sc_controller *controller,
}
static void
switch_fps_counter_state(struct fps_counter *fps_counter) {
switch_fps_counter_state(struct sc_fps_counter *fps_counter) {
// the started state can only be written from the current thread, so there
// is no ToCToU issue
if (fps_counter_is_started(fps_counter)) {
fps_counter_stop(fps_counter);
LOGI("FPS counter stopped");
if (sc_fps_counter_is_started(fps_counter)) {
sc_fps_counter_stop(fps_counter);
} else {
if (fps_counter_start(fps_counter)) {
LOGI("FPS counter started");
} else {
LOGE("FPS counter starting failed");
}
sc_fps_counter_start(fps_counter);
// Any error is already logged
}
}
@ -350,7 +274,7 @@ clipboard_paste(struct sc_controller *controller) {
}
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
msg.type = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
msg.inject_text.text = text_dup;
if (!sc_controller_push_msg(controller, &msg)) {
free(text_dup);
@ -361,7 +285,7 @@ clipboard_paste(struct sc_controller *controller) {
static void
rotate_device(struct sc_controller *controller) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_ROTATE_DEVICE;
msg.type = SC_CONTROL_MSG_TYPE_ROTATE_DEVICE;
if (!sc_controller_push_msg(controller, &msg)) {
LOGW("Could not request device rotation");
@ -407,7 +331,7 @@ simulate_virtual_finger(struct sc_input_manager *im,
bool up = action == AMOTION_EVENT_ACTION_UP;
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
msg.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
msg.inject_touch_event.action = action;
msg.inject_touch_event.position.screen_size = im->screen->frame_size;
msg.inject_touch_event.position.point = point;
@ -487,9 +411,9 @@ sc_input_manager_process_key(struct sc_input_manager *im,
return;
case SDLK_o:
if (controller && !repeat && down) {
enum screen_power_mode mode = shift
? SCREEN_POWER_MODE_NORMAL
: SCREEN_POWER_MODE_OFF;
enum sc_screen_power_mode mode = shift
? SC_SCREEN_POWER_MODE_NORMAL
: SC_SCREEN_POWER_MODE_OFF;
set_screen_power_mode(controller, mode);
}
return;
@ -517,14 +441,12 @@ sc_input_manager_process_key(struct sc_input_manager *im,
return;
case SDLK_c:
if (controller && !shift && !repeat && down) {
get_device_clipboard(controller,
GET_CLIPBOARD_COPY_KEY_COPY);
get_device_clipboard(controller, SC_COPY_KEY_COPY);
}
return;
case SDLK_x:
if (controller && !shift && !repeat && down) {
get_device_clipboard(controller,
GET_CLIPBOARD_COPY_KEY_CUT);
get_device_clipboard(controller, SC_COPY_KEY_CUT);
}
return;
case SDLK_v:

View File

@ -248,7 +248,7 @@ convert_meta_state(uint16_t mod) {
static bool
convert_input_key(const struct sc_key_event *event, struct sc_control_msg *msg,
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
msg->type = SC_CONTROL_MSG_TYPE_INJECT_KEYCODE;
if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
event->mods_state, key_inject_mode)) {
@ -310,7 +310,7 @@ sc_key_processor_process_text(struct sc_key_processor *kp,
}
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
msg.type = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
msg.inject_text.text = strdup(event->text);
if (!msg.inject_text.text) {
LOGW("Could not strdup input text");

View File

@ -13,30 +13,9 @@
#include "cli.h"
#include "options.h"
#include "scrcpy.h"
#include "usb/scrcpy_otg.h"
#include "util/log.h"
static void
print_version(void) {
fprintf(stderr, "scrcpy %s\n\n", SCRCPY_VERSION);
fprintf(stderr, "dependencies:\n");
fprintf(stderr, " - SDL %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION,
SDL_PATCHLEVEL);
fprintf(stderr, " - libavcodec %d.%d.%d\n", LIBAVCODEC_VERSION_MAJOR,
LIBAVCODEC_VERSION_MINOR,
LIBAVCODEC_VERSION_MICRO);
fprintf(stderr, " - libavformat %d.%d.%d\n", LIBAVFORMAT_VERSION_MAJOR,
LIBAVFORMAT_VERSION_MINOR,
LIBAVFORMAT_VERSION_MICRO);
fprintf(stderr, " - libavutil %d.%d.%d\n", LIBAVUTIL_VERSION_MAJOR,
LIBAVUTIL_VERSION_MINOR,
LIBAVUTIL_VERSION_MICRO);
#ifdef HAVE_V4L2
fprintf(stderr, " - libavdevice %d.%d.%d\n", LIBAVDEVICE_VERSION_MAJOR,
LIBAVDEVICE_VERSION_MINOR,
LIBAVDEVICE_VERSION_MICRO);
#endif
}
#include "version.h"
int
main(int argc, char *argv[]) {
@ -61,19 +40,19 @@ main(int argc, char *argv[]) {
#endif
if (!scrcpy_parse_args(&args, argc, argv)) {
return 1;
return SCRCPY_EXIT_FAILURE;
}
sc_set_log_level(args.opts.log_level);
if (args.help) {
scrcpy_print_usage(argv[0]);
return 0;
return SCRCPY_EXIT_SUCCESS;
}
if (args.version) {
print_version();
return 0;
scrcpy_print_version();
return SCRCPY_EXIT_SUCCESS;
}
#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL
@ -87,12 +66,17 @@ main(int argc, char *argv[]) {
#endif
if (avformat_network_init()) {
return 1;
return SCRCPY_EXIT_FAILURE;
}
int res = scrcpy(&args.opts) ? 0 : 1;
#ifdef HAVE_USB
enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts);
#else
enum scrcpy_exit_code ret = scrcpy(&args.opts);
#endif
avformat_network_deinit(); // ignore failure
return res;
return ret;
}

View File

@ -66,7 +66,7 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.inject_touch_event = {
.action = AMOTION_EVENT_ACTION_MOVE,
.pointer_id = POINTER_ID_MOUSE,
@ -87,7 +87,7 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.inject_touch_event = {
.action = convert_mouse_action(event->action),
.pointer_id = POINTER_ID_MOUSE,
@ -108,7 +108,7 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
.inject_scroll_event = {
.position = event->position,
.hscroll = event->hscroll,
@ -128,7 +128,7 @@ sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.inject_touch_event = {
.action = convert_touch_action(event->action),
.pointer_id = event->pointer_id,

View File

@ -37,6 +37,9 @@ const struct scrcpy_options scrcpy_options_default = {
.display_id = 0,
.display_buffer = 0,
.v4l2_buffer = 0,
#ifdef HAVE_USB
.otg = false,
#endif
.show_touches = false,
.fullscreen = false,
.always_on_top = false,
@ -57,4 +60,8 @@ const struct scrcpy_options scrcpy_options_default = {
.downsize_on_error = true,
.tcpip = false,
.tcpip_dst = NULL,
.select_tcpip = false,
.select_usb = false,
.cleanup = true,
.start_fps_counter = false,
};

View File

@ -112,6 +112,9 @@ struct scrcpy_options {
uint32_t display_id;
sc_tick display_buffer;
sc_tick v4l2_buffer;
#ifdef HAVE_USB
bool otg;
#endif
bool show_touches;
bool fullscreen;
bool always_on_top;
@ -132,6 +135,10 @@ struct scrcpy_options {
bool downsize_on_error;
bool tcpip;
const char *tcpip_dst;
bool select_usb;
bool select_tcpip;
bool cleanup;
bool start_fps_counter;
};
extern const struct scrcpy_options scrcpy_options_default;

View File

@ -114,7 +114,7 @@ receiver_start(struct receiver *receiver) {
bool ok = sc_thread_create(&receiver->thread, run_receiver,
"scrcpy-receiver", receiver);
if (!ok) {
LOGC("Could not start receiver thread");
LOGE("Could not start receiver thread");
return false;
}

View File

@ -9,7 +9,7 @@
#include "util/str.h"
/** Downcast packet_sink to recorder */
#define DOWNCAST(SINK) container_of(SINK, struct recorder, packet_sink)
#define DOWNCAST(SINK) container_of(SINK, struct sc_recorder, packet_sink)
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
@ -30,9 +30,9 @@ find_muxer(const char *name) {
return oformat;
}
static struct record_packet *
record_packet_new(const AVPacket *packet) {
struct record_packet *rec = malloc(sizeof(*rec));
static struct sc_record_packet *
sc_record_packet_new(const AVPacket *packet) {
struct sc_record_packet *rec = malloc(sizeof(*rec));
if (!rec) {
LOG_OOM();
return NULL;
@ -54,22 +54,22 @@ record_packet_new(const AVPacket *packet) {
}
static void
record_packet_delete(struct record_packet *rec) {
sc_record_packet_delete(struct sc_record_packet *rec) {
av_packet_free(&rec->packet);
free(rec);
}
static void
recorder_queue_clear(struct recorder_queue *queue) {
sc_recorder_queue_clear(struct sc_recorder_queue *queue) {
while (!sc_queue_is_empty(queue)) {
struct record_packet *rec;
struct sc_record_packet *rec;
sc_queue_take(queue, next, &rec);
record_packet_delete(rec);
sc_record_packet_delete(rec);
}
}
static const char *
recorder_get_format_name(enum sc_record_format format) {
sc_recorder_get_format_name(enum sc_record_format format) {
switch (format) {
case SC_RECORD_FORMAT_MP4: return "mp4";
case SC_RECORD_FORMAT_MKV: return "matroska";
@ -78,7 +78,7 @@ recorder_get_format_name(enum sc_record_format format) {
}
static bool
recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
sc_recorder_write_header(struct sc_recorder *recorder, const AVPacket *packet) {
AVStream *ostream = recorder->ctx->streams[0];
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
@ -103,19 +103,19 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
}
static void
recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) {
sc_recorder_rescale_packet(struct sc_recorder *recorder, AVPacket *packet) {
AVStream *ostream = recorder->ctx->streams[0];
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
}
static bool
recorder_write(struct recorder *recorder, AVPacket *packet) {
sc_recorder_write(struct sc_recorder *recorder, AVPacket *packet) {
if (!recorder->header_written) {
if (packet->pts != AV_NOPTS_VALUE) {
LOGE("The first packet is not a config packet");
return false;
}
bool ok = recorder_write_header(recorder, packet);
bool ok = sc_recorder_write_header(recorder, packet);
if (!ok) {
return false;
}
@ -128,13 +128,13 @@ recorder_write(struct recorder *recorder, AVPacket *packet) {
return true;
}
recorder_rescale_packet(recorder, packet);
sc_recorder_rescale_packet(recorder, packet);
return av_write_frame(recorder->ctx, packet) >= 0;
}
static int
run_recorder(void *data) {
struct recorder *recorder = data;
struct sc_recorder *recorder = data;
for (;;) {
sc_mutex_lock(&recorder->mutex);
@ -148,29 +148,29 @@ run_recorder(void *data) {
if (recorder->stopped && sc_queue_is_empty(&recorder->queue)) {
sc_mutex_unlock(&recorder->mutex);
struct record_packet *last = recorder->previous;
struct sc_record_packet *last = recorder->previous;
if (last) {
// assign an arbitrary duration to the last packet
last->packet->duration = 100000;
bool ok = recorder_write(recorder, last->packet);
bool ok = sc_recorder_write(recorder, last->packet);
if (!ok) {
// failing to write the last frame is not very serious, no
// future frame may depend on it, so the resulting file
// will still be valid
LOGW("Could not record last packet");
}
record_packet_delete(last);
sc_record_packet_delete(last);
}
break;
}
struct record_packet *rec;
struct sc_record_packet *rec;
sc_queue_take(&recorder->queue, next, &rec);
sc_mutex_unlock(&recorder->mutex);
// recorder->previous is only written from this thread, no need to lock
struct record_packet *previous = recorder->previous;
struct sc_record_packet *previous = recorder->previous;
recorder->previous = rec;
if (!previous) {
@ -186,15 +186,15 @@ run_recorder(void *data) {
rec->packet->pts - previous->packet->pts;
}
bool ok = recorder_write(recorder, previous->packet);
record_packet_delete(previous);
bool ok = sc_recorder_write(recorder, previous->packet);
sc_record_packet_delete(previous);
if (!ok) {
LOGE("Could not record packet");
sc_mutex_lock(&recorder->mutex);
recorder->failed = true;
// discard pending packets
recorder_queue_clear(&recorder->queue);
sc_recorder_queue_clear(&recorder->queue);
sc_mutex_unlock(&recorder->mutex);
break;
}
@ -216,7 +216,7 @@ run_recorder(void *data) {
if (recorder->failed) {
LOGE("Recording failed to %s", recorder->filename);
} else {
const char *format_name = recorder_get_format_name(recorder->format);
const char *format_name = sc_recorder_get_format_name(recorder->format);
LOGI("Recording complete to %s file: %s", format_name,
recorder->filename);
}
@ -227,7 +227,7 @@ run_recorder(void *data) {
}
static bool
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
sc_recorder_open(struct sc_recorder *recorder, const AVCodec *input_codec) {
bool ok = sc_mutex_init(&recorder->mutex);
if (!ok) {
return false;
@ -244,7 +244,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
recorder->header_written = false;
recorder->previous = NULL;
const char *format_name = recorder_get_format_name(recorder->format);
const char *format_name = sc_recorder_get_format_name(recorder->format);
assert(format_name);
const AVOutputFormat *format = find_muxer(format_name);
if (!format) {
@ -290,7 +290,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
ok = sc_thread_create(&recorder->thread, run_recorder, "scrcpy-recorder",
recorder);
if (!ok) {
LOGC("Could not start recorder thread");
LOGE("Could not start recorder thread");
goto error_avio_close;
}
@ -311,7 +311,7 @@ error_mutex_destroy:
}
static void
recorder_close(struct recorder *recorder) {
sc_recorder_close(struct sc_recorder *recorder) {
sc_mutex_lock(&recorder->mutex);
recorder->stopped = true;
sc_cond_signal(&recorder->queue_cond);
@ -326,7 +326,7 @@ recorder_close(struct recorder *recorder) {
}
static bool
recorder_push(struct recorder *recorder, const AVPacket *packet) {
sc_recorder_push(struct sc_recorder *recorder, const AVPacket *packet) {
sc_mutex_lock(&recorder->mutex);
assert(!recorder->stopped);
@ -336,7 +336,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
return false;
}
struct record_packet *rec = record_packet_new(packet);
struct sc_record_packet *rec = sc_record_packet_new(packet);
if (!rec) {
LOG_OOM();
sc_mutex_unlock(&recorder->mutex);
@ -351,25 +351,27 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
}
static bool
recorder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct recorder *recorder = DOWNCAST(sink);
return recorder_open(recorder, codec);
sc_recorder_packet_sink_open(struct sc_packet_sink *sink,
const AVCodec *codec) {
struct sc_recorder *recorder = DOWNCAST(sink);
return sc_recorder_open(recorder, codec);
}
static void
recorder_packet_sink_close(struct sc_packet_sink *sink) {
struct recorder *recorder = DOWNCAST(sink);
recorder_close(recorder);
sc_recorder_packet_sink_close(struct sc_packet_sink *sink) {
struct sc_recorder *recorder = DOWNCAST(sink);
sc_recorder_close(recorder);
}
static bool
recorder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
struct recorder *recorder = DOWNCAST(sink);
return recorder_push(recorder, packet);
sc_recorder_packet_sink_push(struct sc_packet_sink *sink,
const AVPacket *packet) {
struct sc_recorder *recorder = DOWNCAST(sink);
return sc_recorder_push(recorder, packet);
}
bool
recorder_init(struct recorder *recorder,
sc_recorder_init(struct sc_recorder *recorder,
const char *filename,
enum sc_record_format format,
struct sc_size declared_frame_size) {
@ -383,9 +385,9 @@ recorder_init(struct recorder *recorder,
recorder->declared_frame_size = declared_frame_size;
static const struct sc_packet_sink_ops ops = {
.open = recorder_packet_sink_open,
.close = recorder_packet_sink_close,
.push = recorder_packet_sink_push,
.open = sc_recorder_packet_sink_open,
.close = sc_recorder_packet_sink_close,
.push = sc_recorder_packet_sink_push,
};
recorder->packet_sink.ops = &ops;
@ -394,6 +396,6 @@ recorder_init(struct recorder *recorder,
}
void
recorder_destroy(struct recorder *recorder) {
sc_recorder_destroy(struct sc_recorder *recorder) {
free(recorder->filename);
}

View File

@ -1,5 +1,5 @@
#ifndef RECORDER_H
#define RECORDER_H
#ifndef SC_RECORDER_H
#define SC_RECORDER_H
#include "common.h"
@ -12,14 +12,14 @@
#include "util/queue.h"
#include "util/thread.h"
struct record_packet {
struct sc_record_packet {
AVPacket *packet;
struct record_packet *next;
struct sc_record_packet *next;
};
struct recorder_queue SC_QUEUE(struct record_packet);
struct sc_recorder_queue SC_QUEUE(struct sc_record_packet);
struct recorder {
struct sc_recorder {
struct sc_packet_sink packet_sink; // packet sink trait
char *filename;
@ -33,20 +33,21 @@ struct recorder {
sc_cond queue_cond;
bool stopped; // set on recorder_close()
bool failed; // set on packet write failure
struct recorder_queue queue;
struct sc_recorder_queue queue;
// we can write a packet only once we received the next one so that we can
// set its duration (next_pts - current_pts)
// "previous" is only accessed from the recorder thread, so it does not
// need to be protected by the mutex
struct record_packet *previous;
struct sc_record_packet *previous;
};
bool
recorder_init(struct recorder *recorder, const char *filename,
enum sc_record_format format, struct sc_size declared_frame_size);
sc_recorder_init(struct sc_recorder *recorder, const char *filename,
enum sc_record_format format,
struct sc_size declared_frame_size);
void
recorder_destroy(struct recorder *recorder);
sc_recorder_destroy(struct sc_recorder *recorder);
#endif

View File

@ -15,18 +15,20 @@
#include "controller.h"
#include "decoder.h"
#include "demuxer.h"
#include "events.h"
#include "file_pusher.h"
#ifdef HAVE_AOA_HID
# include "hid_keyboard.h"
# include "hid_mouse.h"
#endif
#include "keyboard_inject.h"
#include "mouse_inject.h"
#include "recorder.h"
#include "screen.h"
#include "server.h"
#include "stream.h"
#ifdef HAVE_USB
# include "usb/aoa_hid.h"
# include "usb/hid_keyboard.h"
# include "usb/hid_mouse.h"
# include "usb/usb.h"
#endif
#include "util/acksync.h"
#include "util/log.h"
#include "util/net.h"
@ -37,28 +39,29 @@
struct scrcpy {
struct sc_server server;
struct sc_screen screen;
struct stream stream;
struct decoder decoder;
struct recorder recorder;
struct sc_demuxer demuxer;
struct sc_decoder decoder;
struct sc_recorder recorder;
#ifdef HAVE_V4L2
struct sc_v4l2_sink v4l2_sink;
#endif
struct sc_controller controller;
struct sc_file_pusher file_pusher;
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
struct sc_usb usb;
struct sc_aoa aoa;
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
struct sc_acksync acksync;
#endif
union {
struct sc_keyboard_inject keyboard_inject;
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
struct sc_hid_keyboard keyboard_hid;
#endif
};
union {
struct sc_mouse_inject mouse_inject;
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
struct sc_hid_mouse mouse_hid;
#endif
};
@ -140,46 +143,47 @@ sdl_configure(bool display, bool disable_screensaver) {
}
if (disable_screensaver) {
LOGD("Screensaver disabled");
SDL_DisableScreenSaver();
} else {
LOGD("Screensaver enabled");
SDL_EnableScreenSaver();
}
}
static bool
static enum scrcpy_exit_code
event_loop(struct scrcpy *s) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case EVENT_STREAM_STOPPED:
LOGW("Device disconnected");
return false;
return SCRCPY_EXIT_DISCONNECTED;
case SDL_QUIT:
LOGD("User requested to quit");
return true;
return SCRCPY_EXIT_SUCCESS;
default:
sc_screen_handle_event(&s->screen, &event);
break;
}
}
return false;
return SCRCPY_EXIT_FAILURE;
}
// Return true on success, false on error
static bool
await_for_server(void) {
await_for_server(bool *connected) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
LOGD("User requested to quit");
return false;
*connected = false;
return true;
case EVENT_SERVER_CONNECTION_FAILED:
LOGE("Server connection failed");
return false;
case EVENT_SERVER_CONNECTED:
LOGD("Server connected");
*connected = true;
return true;
default:
break;
@ -228,8 +232,8 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
}
static void
stream_on_eos(struct stream *stream, void *userdata) {
(void) stream;
sc_demuxer_on_eos(struct sc_demuxer *demuxer, void *userdata) {
(void) demuxer;
(void) userdata;
PUSH_EVENT(EVENT_STREAM_STOPPED);
@ -261,20 +265,20 @@ sc_server_on_disconnected(struct sc_server *server, void *userdata) {
// event
}
bool
enum scrcpy_exit_code
scrcpy(struct scrcpy_options *options) {
static struct scrcpy scrcpy;
struct scrcpy *s = &scrcpy;
// Minimal SDL initialization
if (SDL_Init(SDL_INIT_EVENTS)) {
LOGC("Could not initialize SDL: %s", SDL_GetError());
return false;
LOGE("Could not initialize SDL: %s", SDL_GetError());
return SCRCPY_EXIT_FAILURE;
}
atexit(SDL_Quit);
bool ret = false;
enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;
bool server_started = false;
bool file_pusher_initialized = false;
@ -282,8 +286,8 @@ scrcpy(struct scrcpy_options *options) {
#ifdef HAVE_V4L2
bool v4l2_sink_initialized = false;
#endif
bool stream_started = false;
#ifdef HAVE_AOA_HID
bool demuxer_started = false;
#ifdef HAVE_USB
bool aoa_hid_initialized = false;
bool hid_keyboard_initialized = false;
bool hid_mouse_initialized = false;
@ -295,7 +299,9 @@ scrcpy(struct scrcpy_options *options) {
struct sc_acksync *acksync = NULL;
struct sc_server_params params = {
.serial = options->serial,
.req_serial = options->serial,
.select_usb = options->select_usb,
.select_tcpip = options->select_tcpip,
.log_level = options->log_level,
.crop = options->crop,
.port_range = options->port_range,
@ -317,6 +323,7 @@ scrcpy(struct scrcpy_options *options) {
.downsize_on_error = options->downsize_on_error,
.tcpip = options->tcpip,
.tcpip_dst = options->tcpip_dst,
.cleanup = options->cleanup,
};
static const struct sc_server_callbacks cbs = {
@ -325,7 +332,7 @@ scrcpy(struct scrcpy_options *options) {
.on_disconnected = sc_server_on_disconnected,
};
if (!sc_server_init(&s->server, &params, &cbs, NULL)) {
return false;
return SCRCPY_EXIT_FAILURE;
}
if (!sc_server_start(&s->server)) {
@ -340,21 +347,28 @@ scrcpy(struct scrcpy_options *options) {
// Initialize SDL video in addition if display is enabled
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
LOGC("Could not initialize SDL: %s", SDL_GetError());
LOGE("Could not initialize SDL: %s", SDL_GetError());
goto end;
}
sdl_configure(options->display, options->disable_screensaver);
// Await for server without blocking Ctrl+C handling
if (!await_for_server()) {
bool connected;
if (!await_for_server(&connected)) {
goto end;
}
if (!connected) {
// This is not an error, user requested to quit
ret = SCRCPY_EXIT_SUCCESS;
goto end;
}
// It is necessarily initialized here, since the device is connected
struct sc_server_info *info = &s->server.info;
const char *serial = s->server.params.serial;
const char *serial = s->server.serial;
assert(serial);
struct sc_file_pusher *fp = NULL;
@ -368,19 +382,19 @@ scrcpy(struct scrcpy_options *options) {
file_pusher_initialized = true;
}
struct decoder *dec = NULL;
struct sc_decoder *dec = NULL;
bool needs_decoder = options->display;
#ifdef HAVE_V4L2
needs_decoder |= !!options->v4l2_device;
#endif
if (needs_decoder) {
decoder_init(&s->decoder);
sc_decoder_init(&s->decoder);
dec = &s->decoder;
}
struct recorder *rec = NULL;
struct sc_recorder *rec = NULL;
if (options->record_filename) {
if (!recorder_init(&s->recorder,
if (!sc_recorder_init(&s->recorder,
options->record_filename,
options->record_format,
info->frame_size)) {
@ -392,17 +406,17 @@ scrcpy(struct scrcpy_options *options) {
av_log_set_callback(av_log_callback);
static const struct stream_callbacks stream_cbs = {
.on_eos = stream_on_eos,
static const struct sc_demuxer_callbacks demuxer_cbs = {
.on_eos = sc_demuxer_on_eos,
};
stream_init(&s->stream, s->server.video_socket, &stream_cbs, NULL);
sc_demuxer_init(&s->demuxer, s->server.video_socket, &demuxer_cbs, NULL);
if (dec) {
stream_add_sink(&s->stream, &dec->packet_sink);
sc_demuxer_add_sink(&s->demuxer, &dec->packet_sink);
}
if (rec) {
stream_add_sink(&s->stream, &rec->packet_sink);
sc_demuxer_add_sink(&s->demuxer, &rec->packet_sink);
}
struct sc_controller *controller = NULL;
@ -410,7 +424,7 @@ scrcpy(struct scrcpy_options *options) {
struct sc_mouse_processor *mp = NULL;
if (options->control) {
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
bool use_hid_keyboard =
options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID;
bool use_hid_mouse =
@ -421,9 +435,39 @@ scrcpy(struct scrcpy_options *options) {
goto end;
}
ok = sc_aoa_init(&s->aoa, serial, &s->acksync);
ok = sc_usb_init(&s->usb);
if (!ok) {
LOGE("Failed to initialize USB");
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
assert(serial);
struct sc_usb_device usb_device;
ok = sc_usb_select_device(&s->usb, serial, &usb_device);
if (!ok) {
sc_usb_destroy(&s->usb);
goto aoa_hid_end;
}
LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
usb_device.serial, usb_device.vid, usb_device.pid,
usb_device.manufacturer, usb_device.product);
ok = sc_usb_connect(&s->usb, usb_device.device, NULL, NULL);
sc_usb_device_destroy(&usb_device);
if (!ok) {
LOGE("Failed to connect to USB device %s", serial);
sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync);
if (!ok) {
LOGE("Failed to enable HID over AOA");
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
@ -450,6 +494,8 @@ scrcpy(struct scrcpy_options *options) {
if (!need_aoa || !sc_aoa_start(&s->aoa)) {
sc_acksync_destroy(&s->acksync);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
sc_aoa_destroy(&s->aoa);
goto aoa_hid_end;
}
@ -515,8 +561,8 @@ aoa_hid_end:
if (options->turn_screen_off) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
msg.set_screen_power_mode.mode = SCREEN_POWER_MODE_OFF;
msg.type = SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
msg.set_screen_power_mode.mode = SC_SCREEN_POWER_MODE_OFF;
if (!sc_controller_push_msg(&s->controller, &msg)) {
LOGW("Could not request 'set screen power mode'");
@ -552,6 +598,7 @@ aoa_hid_end:
.rotation = options->rotation,
.mipmaps = options->mipmaps,
.fullscreen = options->fullscreen,
.start_fps_counter = options->start_fps_counter,
.buffering_time = options->display_buffer,
};
@ -560,7 +607,7 @@ aoa_hid_end:
}
screen_initialized = true;
decoder_add_sink(&s->decoder, &s->screen.frame_sink);
sc_decoder_add_sink(&s->decoder, &s->screen.frame_sink);
}
#ifdef HAVE_V4L2
@ -570,30 +617,30 @@ aoa_hid_end:
goto end;
}
decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
sc_decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
v4l2_sink_initialized = true;
}
#endif
// now we consumed the header values, the socket receives the video stream
// start the stream
if (!stream_start(&s->stream)) {
// start the demuxer
if (!sc_demuxer_start(&s->demuxer)) {
goto end;
}
stream_started = true;
demuxer_started = true;
ret = event_loop(s);
LOGD("quit...");
// Close the window immediately on closing, because screen_destroy() may
// only be called once the stream thread is joined (it may take time)
// only be called once the demuxer thread is joined (it may take time)
sc_screen_hide_window(&s->screen);
end:
// The stream is not stopped explicitly, because it will stop by itself on
// The demuxer is not stopped explicitly, because it will stop by itself on
// end-of-stream
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
if (aoa_hid_initialized) {
if (hid_keyboard_initialized) {
sc_hid_keyboard_destroy(&s->keyboard_hid);
@ -602,6 +649,7 @@ end:
sc_hid_mouse_destroy(&s->mouse_hid);
}
sc_aoa_stop(&s->aoa);
sc_usb_stop(&s->usb);
}
if (acksync) {
sc_acksync_destroy(acksync);
@ -622,10 +670,10 @@ end:
sc_server_stop(&s->server);
}
// now that the sockets are shutdown, the stream and controller are
// now that the sockets are shutdown, the demuxer and controller are
// interrupted, we can join them
if (stream_started) {
stream_join(&s->stream);
if (demuxer_started) {
sc_demuxer_join(&s->demuxer);
}
#ifdef HAVE_V4L2
@ -634,14 +682,17 @@ end:
}
#endif
#ifdef HAVE_AOA_HID
#ifdef HAVE_USB
if (aoa_hid_initialized) {
sc_aoa_join(&s->aoa);
sc_aoa_destroy(&s->aoa);
sc_usb_join(&s->usb);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
}
#endif
// Destroy the screen only after the stream is guaranteed to be finished,
// Destroy the screen only after the demuxer is guaranteed to be finished,
// because otherwise the screen could receive new frames after destruction
if (screen_initialized) {
sc_screen_join(&s->screen);
@ -656,7 +707,7 @@ end:
}
if (recorder_initialized) {
recorder_destroy(&s->recorder);
sc_recorder_destroy(&s->recorder);
}
if (file_pusher_initialized) {

View File

@ -6,7 +6,18 @@
#include <stdbool.h>
#include "options.h"
bool
enum scrcpy_exit_code {
// Normal program termination
SCRCPY_EXIT_SUCCESS,
// No connection could be established
SCRCPY_EXIT_FAILURE,
// Device was disconnected while running
SCRCPY_EXIT_DISCONNECTED,
};
enum scrcpy_exit_code
scrcpy(struct scrcpy_options *options);
#endif

View File

@ -163,14 +163,44 @@ sc_screen_is_relative_mode(struct sc_screen *screen) {
}
static void
sc_screen_capture_mouse(struct sc_screen *screen, bool capture) {
sc_screen_set_mouse_capture(struct sc_screen *screen, bool capture) {
#ifdef __APPLE__
// Workaround for SDL bug on macOS:
// <https://github.com/libsdl-org/SDL/issues/5340>
if (capture) {
int mouse_x, mouse_y;
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
int x, y, w, h;
SDL_GetWindowPosition(screen->window, &x, &y);
SDL_GetWindowSize(screen->window, &w, &h);
bool outside_window = mouse_x < x || mouse_x >= x + w
|| mouse_y < y || mouse_y >= y + h;
if (outside_window) {
SDL_WarpMouseInWindow(screen->window, w / 2, h / 2);
}
}
#else
(void) screen;
#endif
if (SDL_SetRelativeMouseMode(capture)) {
LOGE("Could not set relative mouse mode to %s: %s",
capture ? "true" : "false", SDL_GetError());
return;
}
}
screen->mouse_captured = capture;
static inline bool
sc_screen_get_mouse_capture(struct sc_screen *screen) {
(void) screen;
return SDL_GetRelativeMouseMode();
}
static inline void
sc_screen_toggle_mouse_capture(struct sc_screen *screen) {
(void) screen;
bool new_value = !sc_screen_get_mouse_capture(screen);
sc_screen_set_mouse_capture(screen, new_value);
}
static void
@ -340,7 +370,7 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
bool need_new_event;
if (previous_skipped) {
fps_counter_add_skipped_frame(&screen->fps_counter);
sc_fps_counter_add_skipped_frame(&screen->fps_counter);
// The EVENT_NEW_FRAME triggered for the previous frame will consume
// this new frame instead, unless the previous event failed
need_new_event = screen->event_failed;
@ -372,7 +402,6 @@ sc_screen_init(struct sc_screen *screen,
screen->fullscreen = false;
screen->maximized = false;
screen->event_failed = false;
screen->mouse_captured = false;
screen->mouse_capture_key_pressed = 0;
screen->req.x = params->window_x;
@ -380,6 +409,7 @@ sc_screen_init(struct sc_screen *screen,
screen->req.width = params->window_width;
screen->req.height = params->window_height;
screen->req.fullscreen = params->fullscreen;
screen->req.start_fps_counter = params->start_fps_counter;
static const struct sc_video_buffer_callbacks cbs = {
.on_new_frame = sc_video_buffer_on_new_frame,
@ -396,7 +426,7 @@ sc_screen_init(struct sc_screen *screen,
goto error_destroy_video_buffer;
}
if (!fps_counter_init(&screen->fps_counter)) {
if (!sc_fps_counter_init(&screen->fps_counter)) {
goto error_stop_and_join_video_buffer;
}
@ -423,14 +453,14 @@ sc_screen_init(struct sc_screen *screen,
screen->window =
SDL_CreateWindow(params->window_title, 0, 0, 0, 0, window_flags);
if (!screen->window) {
LOGC("Could not create window: %s", SDL_GetError());
LOGE("Could not create window: %s", SDL_GetError());
goto error_destroy_fps_counter;
}
screen->renderer = SDL_CreateRenderer(screen->window, -1,
SDL_RENDERER_ACCELERATED);
if (!screen->renderer) {
LOGC("Could not create renderer: %s", SDL_GetError());
LOGE("Could not create renderer: %s", SDL_GetError());
goto error_destroy_window;
}
@ -479,7 +509,7 @@ sc_screen_init(struct sc_screen *screen,
params->frame_size.height);
screen->texture = create_texture(screen);
if (!screen->texture) {
LOGC("Could not create texture: %s", SDL_GetError());
LOGE("Could not create texture: %s", SDL_GetError());
goto error_destroy_renderer;
}
@ -528,7 +558,7 @@ error_destroy_renderer:
error_destroy_window:
SDL_DestroyWindow(screen->window);
error_destroy_fps_counter:
fps_counter_destroy(&screen->fps_counter);
sc_fps_counter_destroy(&screen->fps_counter);
error_stop_and_join_video_buffer:
sc_video_buffer_stop(&screen->vb);
sc_video_buffer_join(&screen->vb);
@ -556,6 +586,10 @@ sc_screen_show_initial_window(struct sc_screen *screen) {
sc_screen_switch_fullscreen(screen);
}
if (screen->req.start_fps_counter) {
sc_fps_counter_start(&screen->fps_counter);
}
SDL_ShowWindow(screen->window);
}
@ -567,13 +601,13 @@ sc_screen_hide_window(struct sc_screen *screen) {
void
sc_screen_interrupt(struct sc_screen *screen) {
sc_video_buffer_stop(&screen->vb);
fps_counter_interrupt(&screen->fps_counter);
sc_fps_counter_interrupt(&screen->fps_counter);
}
void
sc_screen_join(struct sc_screen *screen) {
sc_video_buffer_join(&screen->vb);
fps_counter_join(&screen->fps_counter);
sc_fps_counter_join(&screen->fps_counter);
}
void
@ -585,7 +619,7 @@ sc_screen_destroy(struct sc_screen *screen) {
SDL_DestroyTexture(screen->texture);
SDL_DestroyRenderer(screen->renderer);
SDL_DestroyWindow(screen->window);
fps_counter_destroy(&screen->fps_counter);
sc_fps_counter_destroy(&screen->fps_counter);
sc_video_buffer_destroy(&screen->vb);
}
@ -666,7 +700,7 @@ prepare_for_frame(struct sc_screen *screen, struct sc_size new_frame_size) {
screen->frame_size.width, screen->frame_size.height);
screen->texture = create_texture(screen);
if (!screen->texture) {
LOGC("Could not create texture: %s", SDL_GetError());
LOGE("Could not create texture: %s", SDL_GetError());
return false;
}
}
@ -695,7 +729,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
sc_video_buffer_consume(&screen->vb, screen->frame);
AVFrame *frame = screen->frame;
fps_counter_add_rendered_frame(&screen->fps_counter);
sc_fps_counter_add_rendered_frame(&screen->fps_counter);
struct sc_size new_frame_size = {frame->width, frame->height};
if (!prepare_for_frame(screen, new_frame_size)) {
@ -710,7 +744,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
if (sc_screen_is_relative_mode(screen)) {
// Capture mouse on start
sc_screen_capture_mouse(screen, true);
sc_screen_set_mouse_capture(screen, true);
}
}
@ -823,7 +857,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (relative_mode) {
sc_screen_capture_mouse(screen, false);
sc_screen_set_mouse_capture(screen, false);
}
break;
}
@ -853,8 +887,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
if (key == cap) {
// A mouse capture key has been pressed then released:
// toggle the capture mouse mode
sc_screen_capture_mouse(screen,
!screen->mouse_captured);
sc_screen_toggle_mouse_capture(screen);
}
// Mouse capture keys are never forwarded to the device
return;
@ -864,7 +897,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
case SDL_MOUSEWHEEL:
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
if (relative_mode && !screen->mouse_captured) {
if (relative_mode && !sc_screen_get_mouse_capture(screen)) {
// Do not forward to input manager, the mouse will be captured
// on SDL_MOUSEBUTTONUP
return;
@ -880,8 +913,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
}
break;
case SDL_MOUSEBUTTONUP:
if (relative_mode && !screen->mouse_captured) {
sc_screen_capture_mouse(screen, true);
if (relative_mode && !sc_screen_get_mouse_capture(screen)) {
sc_screen_set_mouse_capture(screen, true);
return;
}
break;

View File

@ -26,7 +26,7 @@ struct sc_screen {
struct sc_input_manager im;
struct sc_video_buffer vb;
struct fps_counter fps_counter;
struct sc_fps_counter fps_counter;
// The initial requested window properties
struct {
@ -35,6 +35,7 @@ struct sc_screen {
uint16_t width;
uint16_t height;
bool fullscreen;
bool start_fps_counter;
} req;
SDL_Window *window;
@ -60,7 +61,6 @@ struct sc_screen {
bool event_failed; // in case SDL_PushEvent() returned an error
bool mouse_captured; // only relevant in relative mouse mode
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
// RGUI) must be pressed. This variable tracks the pressed capture key.
SDL_Keycode mouse_capture_key_pressed;
@ -94,6 +94,7 @@ struct sc_screen_params {
bool mipmaps;
bool fullscreen;
bool start_fps_counter;
sc_tick buffering_time;
};

View File

@ -7,7 +7,7 @@
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_platform.h>
#include "adb.h"
#include "adb/adb.h"
#include "util/file.h"
#include "util/log.h"
#include "util/net_intr.h"
@ -65,7 +65,7 @@ get_server_path(void) {
static void
sc_server_params_destroy(struct sc_server_params *params) {
// The server stores a copy of the params provided by the user
free((char *) params->serial);
free((char *) params->req_serial);
free((char *) params->crop);
free((char *) params->codec_options);
free((char *) params->encoder_name);
@ -89,7 +89,7 @@ sc_server_params_copy(struct sc_server_params *dst,
} \
}
COPY(serial);
COPY(req_serial);
COPY(crop);
COPY(codec_options);
COPY(encoder_name);
@ -114,7 +114,7 @@ push_server(struct sc_intr *intr, const char *serial) {
free(server_path);
return false;
}
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);
bool ok = sc_adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);
free(server_path);
return ok;
}
@ -157,8 +157,14 @@ execute_server(struct sc_server *server,
const struct sc_server_params *params) {
sc_pid pid = SC_PROCESS_NONE;
const char *serial = server->serial;
assert(serial);
const char *cmd[128];
unsigned count = 0;
cmd[count++] = sc_adb_get_executable();
cmd[count++] = "-s";
cmd[count++] = serial;
cmd[count++] = "shell";
cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
cmd[count++] = "app_process";
@ -238,9 +244,14 @@ execute_server(struct sc_server *server,
// By default, downsize_on_error is true
ADD_PARAM("downsize_on_error=false");
}
if (!params->cleanup) {
// By default, cleanup is true
ADD_PARAM("cleanup=false");
}
#undef ADD_PARAM
#undef STRBOOL
cmd[count++] = NULL;
#ifdef SERVER_DEBUGGER
LOGI("Server debugger waiting for a client on device port "
@ -254,7 +265,7 @@ execute_server(struct sc_server *server,
// Then click on "Debug"
#endif
// Inherit both stdout and stderr (all server logs are printed to stdout)
pid = adb_execute(params->serial, cmd, count, 0);
pid = sc_adb_execute(cmd, 0);
end:
for (unsigned i = dyn_idx; i < count; ++i) {
@ -346,6 +357,7 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
return false;
}
server->serial = NULL;
server->stopped = false;
server->video_socket = SC_SOCKET_NONE;
@ -390,7 +402,9 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
assert(tunnel->enabled);
const char *serial = server->params.serial;
const char *serial = server->serial;
assert(serial);
bool control = server->params.control;
sc_socket video_socket = SC_SOCKET_NONE;
@ -472,8 +486,10 @@ fail:
}
}
if (tunnel->enabled) {
// Always leave this function with tunnel disabled
sc_adb_tunnel_close(tunnel, &server->intr, serial);
}
return false;
}
@ -494,32 +510,11 @@ sc_server_on_terminated(void *userdata) {
}
static bool
sc_server_fill_serial(struct sc_server *server) {
// Retrieve the actual device immediately if not provided, so that all
// future adb commands are executed for this specific device, even if other
// devices are connected afterwards (without "more than one
// device/emulator" error)
if (!server->params.serial) {
// The serial is owned by sc_server_params, and will be freed on destroy
server->params.serial = adb_get_serialno(&server->intr, 0);
if (!server->params.serial) {
LOGE("Could not get device serial");
return false;
}
LOGD("Device serial: %s", server->params.serial);
}
return true;
}
static bool
is_tcpip_mode_enabled(struct sc_server *server) {
is_tcpip_mode_enabled(struct sc_server *server, const char *serial) {
struct sc_intr *intr = &server->intr;
const char *serial = server->params.serial;
char *current_port =
adb_getprop(intr, serial, "service.adb.tcp.port", SC_ADB_SILENT);
sc_adb_getprop(intr, serial, "service.adb.tcp.port", SC_ADB_SILENT);
if (!current_port) {
return false;
}
@ -531,9 +526,9 @@ is_tcpip_mode_enabled(struct sc_server *server) {
}
static bool
wait_tcpip_mode_enabled(struct sc_server *server, unsigned attempts,
sc_tick delay) {
if (is_tcpip_mode_enabled(server)) {
wait_tcpip_mode_enabled(struct sc_server *server, const char *serial,
unsigned attempts, sc_tick delay) {
if (is_tcpip_mode_enabled(server, serial)) {
LOGI("TCP/IP mode enabled");
return true;
}
@ -548,7 +543,7 @@ wait_tcpip_mode_enabled(struct sc_server *server, unsigned attempts,
return false;
}
if (is_tcpip_mode_enabled(server)) {
if (is_tcpip_mode_enabled(server, serial)) {
LOGI("TCP/IP mode enabled");
return true;
}
@ -573,29 +568,30 @@ append_port_5555(const char *ip) {
return ip_port;
}
static bool
sc_server_switch_to_tcpip(struct sc_server *server, char **out_ip_port) {
const char *serial = server->params.serial;
static char *
sc_server_switch_to_tcpip(struct sc_server *server, const char *serial) {
assert(serial);
struct sc_intr *intr = &server->intr;
char *ip = adb_get_device_ip(intr, serial, 0);
LOGI("Switching device %s to TCP/IP...", serial);
char *ip = sc_adb_get_device_ip(intr, serial, 0);
if (!ip) {
LOGE("Device IP not found");
return false;
return NULL;
}
char *ip_port = append_port_5555(ip);
free(ip);
if (!ip_port) {
return false;
return NULL;
}
bool tcp_mode = is_tcpip_mode_enabled(server);
bool tcp_mode = is_tcpip_mode_enabled(server, serial);
if (!tcp_mode) {
bool ok = adb_tcpip(intr, serial, 5555, SC_ADB_NO_STDOUT);
bool ok = sc_adb_tcpip(intr, serial, 5555, SC_ADB_NO_STDOUT);
if (!ok) {
LOGE("Could not restart adbd in TCP/IP mode");
goto error;
@ -603,19 +599,17 @@ sc_server_switch_to_tcpip(struct sc_server *server, char **out_ip_port) {
unsigned attempts = 40;
sc_tick delay = SC_TICK_FROM_MS(250);
ok = wait_tcpip_mode_enabled(server, attempts, delay);
ok = wait_tcpip_mode_enabled(server, serial, attempts, delay);
if (!ok) {
goto error;
}
}
*out_ip_port = ip_port;
return true;
return ip_port;
error:
free(ip_port);
return false;
return NULL;
}
static bool
@ -623,73 +617,52 @@ sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
struct sc_intr *intr = &server->intr;
// Error expected if not connected, do not report any error
adb_disconnect(intr, ip_port, SC_ADB_SILENT);
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
bool ok = adb_connect(intr, ip_port, 0);
LOGI("Connecting to %s...", ip_port);
bool ok = sc_adb_connect(intr, ip_port, 0);
if (!ok) {
LOGE("Could not connect to %s", ip_port);
return false;
}
// Override the serial, owned by the sc_server_params
free((void *) server->params.serial);
server->params.serial = strdup(ip_port);
if (!server->params.serial) {
LOG_OOM();
return false;
}
LOGI("Connected to %s", ip_port);
return true;
}
static bool
sc_server_configure_tcpip(struct sc_server *server) {
char *ip_port;
const struct sc_server_params *params = &server->params;
// If tcpip parameter is given, then it must connect to this address.
// Therefore, the device is unknown, so serial is meaningless at this point.
assert(!params->serial || !params->tcpip_dst);
if (params->tcpip_dst) {
sc_server_configure_tcpip_known_address(struct sc_server *server,
const char *addr) {
// Append ":5555" if no port is present
bool contains_port = strchr(params->tcpip_dst, ':');
ip_port = contains_port ? strdup(params->tcpip_dst)
: append_port_5555(params->tcpip_dst);
bool contains_port = strchr(addr, ':');
char *ip_port = contains_port ? strdup(addr) : append_port_5555(addr);
if (!ip_port) {
LOG_OOM();
return false;
}
} else {
// The device IP address must be retrieved from the current
// connected device
if (!sc_server_fill_serial(server)) {
return false;
}
// The serial is either the real serial when connected via USB, or
// the IP:PORT when connected over TCP/IP. Only the latter contains
// a colon.
bool is_already_tcpip = strchr(params->serial, ':');
server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port);
}
static bool
sc_server_configure_tcpip_unknown_address(struct sc_server *server,
const char *serial) {
bool is_already_tcpip = sc_adb_is_serial_tcpip(serial);
if (is_already_tcpip) {
// Nothing to do
LOGI("Device already connected via TCP/IP: %s", params->serial);
LOGI("Device already connected via TCP/IP: %s", serial);
return true;
}
bool ok = sc_server_switch_to_tcpip(server, &ip_port);
if (!ok) {
char *ip_port = sc_server_switch_to_tcpip(server, serial);
if (!ip_port) {
return false;
}
}
// On success, this call changes params->serial
bool ok = sc_server_connect_to_tcpip(server, ip_port);
free(ip_port);
return ok;
server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port);
}
static int
@ -698,30 +671,83 @@ run_server(void *data) {
const struct sc_server_params *params = &server->params;
if (params->serial) {
LOGD("Device serial: %s", params->serial);
// Execute "adb start-server" before "adb devices" so that daemon starting
// output/errors is correctly printed in the console ("adb devices" output
// is parsed, so it is not output)
bool ok = sc_adb_start_server(&server->intr, 0);
if (!ok) {
LOGE("Could not start adb daemon");
goto error_connection_failed;
}
// params->tcpip_dst implies params->tcpip
assert(!params->tcpip_dst || params->tcpip);
// If tcpip_dst parameter is given, then it must connect to this address.
// Therefore, the device is unknown, so serial is meaningless at this point.
assert(!params->req_serial || !params->tcpip_dst);
// A device must be selected via a serial in all cases except when --tcpip=
// is called with a parameter (in that case, the device may initially not
// exist, and scrcpy will execute "adb connect").
bool need_initial_serial = !params->tcpip_dst;
if (need_initial_serial) {
// At most one of the 3 following parameters may be set
assert(!!params->req_serial
+ params->select_usb
+ params->select_tcpip <= 1);
struct sc_adb_device_selector selector;
if (params->req_serial) {
selector.type = SC_ADB_DEVICE_SELECT_SERIAL;
selector.serial = params->req_serial;
} else if (params->select_usb) {
selector.type = SC_ADB_DEVICE_SELECT_USB;
} else if (params->select_tcpip) {
selector.type = SC_ADB_DEVICE_SELECT_TCPIP;
} else {
selector.type = SC_ADB_DEVICE_SELECT_ALL;
}
struct sc_adb_device device;
ok = sc_adb_select_device(&server->intr, &selector, 0, &device);
if (!ok) {
goto error_connection_failed;
}
if (params->tcpip) {
// params->serial may be changed after this call
bool ok = sc_server_configure_tcpip(server);
assert(!params->tcpip_dst);
ok = sc_server_configure_tcpip_unknown_address(server,
device.serial);
sc_adb_device_destroy(&device);
if (!ok) {
goto error_connection_failed;
}
assert(server->serial);
} else {
// "move" the device.serial without copy
server->serial = device.serial;
// the serial must not be freed by the destructor
device.serial = NULL;
sc_adb_device_destroy(&device);
}
} else {
ok = sc_server_configure_tcpip_known_address(server, params->tcpip_dst);
if (!ok) {
goto error_connection_failed;
}
}
// It is ok to call this function even if the device serial has been
// changed by switching over TCP/IP
if (!sc_server_fill_serial(server)) {
goto error_connection_failed;
}
const char *serial = server->serial;
assert(serial);
LOGD("Device serial: %s", serial);
bool ok = push_server(&server->intr, params->serial);
ok = push_server(&server->intr, serial);
if (!ok) {
goto error_connection_failed;
}
ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, params->serial,
ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, serial,
params->port_range, params->force_adb_forward);
if (!ok) {
goto error_connection_failed;
@ -730,7 +756,7 @@ run_server(void *data) {
// server will connect to our server socket
sc_pid pid = execute_server(server, params);
if (pid == SC_PROCESS_NONE) {
sc_adb_tunnel_close(&server->tunnel, &server->intr, params->serial);
sc_adb_tunnel_close(&server->tunnel, &server->intr, serial);
goto error_connection_failed;
}
@ -742,7 +768,7 @@ run_server(void *data) {
if (!ok) {
sc_process_terminate(pid);
sc_process_wait(pid, true); // ignore exit code
sc_adb_tunnel_close(&server->tunnel, &server->intr, params->serial);
sc_adb_tunnel_close(&server->tunnel, &server->intr, serial);
goto error_connection_failed;
}
@ -769,12 +795,10 @@ run_server(void *data) {
// Interrupt sockets to wake up socket blocking calls on the server
assert(server->video_socket != SC_SOCKET_NONE);
net_interrupt(server->video_socket);
net_close(server->video_socket);
if (server->control_socket != SC_SOCKET_NONE) {
// There is no control_socket if --no-control is set
net_interrupt(server->control_socket);
net_close(server->control_socket);
}
// Give some delay for the server to terminate properly
@ -830,6 +854,14 @@ sc_server_stop(struct sc_server *server) {
void
sc_server_destroy(struct sc_server *server) {
if (server->video_socket != SC_SOCKET_NONE) {
net_close(server->video_socket);
}
if (server->control_socket != SC_SOCKET_NONE) {
net_close(server->control_socket);
}
free(server->serial);
sc_server_params_destroy(&server->params);
sc_intr_destroy(&server->intr);
sc_cond_destroy(&server->cond_stopped);

View File

@ -7,8 +7,7 @@
#include <stdbool.h>
#include <stdint.h>
#include "adb.h"
#include "adb_tunnel.h"
#include "adb/adb_tunnel.h"
#include "coords.h"
#include "options.h"
#include "util/intr.h"
@ -23,7 +22,7 @@ struct sc_server_info {
};
struct sc_server_params {
const char *serial;
const char *req_serial;
enum sc_log_level log_level;
const char *crop;
const char *codec_options;
@ -45,11 +44,15 @@ struct sc_server_params {
bool downsize_on_error;
bool tcpip;
const char *tcpip_dst;
bool select_usb;
bool select_tcpip;
bool cleanup;
};
struct sc_server {
// The internal allocated strings are copies owned by the server
struct sc_server_params params;
char *serial;
sc_thread thread;
struct sc_server_info info; // initialized once connected

View File

@ -1,298 +0,0 @@
#include "stream.h"
#include <assert.h>
#include <libavutil/time.h>
#include <unistd.h>
#include "decoder.h"
#include "events.h"
#include "recorder.h"
#include "util/buffer_util.h"
#include "util/log.h"
#define BUFSIZE 0x10000
#define HEADER_SIZE 12
#define NO_PTS UINT64_C(-1)
static bool
stream_recv_packet(struct stream *stream, AVPacket *packet) {
// The video stream contains raw packets, without time information. When we
// record, we retrieve the timestamps separately, from a "meta" header
// added by the server before each raw packet.
//
// The "meta" header length is 12 bytes:
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
// <-------------> <-----> <-----------------------------...
// PTS packet raw packet
// size
//
// It is followed by <packet_size> bytes containing the packet/frame.
uint8_t header[HEADER_SIZE];
ssize_t r = net_recv_all(stream->socket, header, HEADER_SIZE);
if (r < HEADER_SIZE) {
return false;
}
uint64_t pts = buffer_read64be(header);
uint32_t len = buffer_read32be(&header[8]);
assert(pts == NO_PTS || (pts & 0x8000000000000000) == 0);
assert(len);
if (av_new_packet(packet, len)) {
LOG_OOM();
return false;
}
r = net_recv_all(stream->socket, packet->data, len);
if (r < 0 || ((uint32_t) r) < len) {
av_packet_unref(packet);
return false;
}
packet->pts = pts != NO_PTS ? (int64_t) pts : AV_NOPTS_VALUE;
return true;
}
static bool
push_packet_to_sinks(struct stream *stream, const AVPacket *packet) {
for (unsigned i = 0; i < stream->sink_count; ++i) {
struct sc_packet_sink *sink = stream->sinks[i];
if (!sink->ops->push(sink, packet)) {
LOGE("Could not send config packet to sink %d", i);
return false;
}
}
return true;
}
static bool
stream_parse(struct stream *stream, AVPacket *packet) {
uint8_t *in_data = packet->data;
int in_len = packet->size;
uint8_t *out_data = NULL;
int out_len = 0;
int r = av_parser_parse2(stream->parser, stream->codec_ctx,
&out_data, &out_len, in_data, in_len,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
// PARSER_FLAG_COMPLETE_FRAMES is set
assert(r == in_len);
(void) r;
assert(out_len == in_len);
if (stream->parser->key_frame == 1) {
packet->flags |= AV_PKT_FLAG_KEY;
}
packet->dts = packet->pts;
bool ok = push_packet_to_sinks(stream, packet);
if (!ok) {
LOGE("Could not process packet");
return false;
}
return true;
}
static bool
stream_push_packet(struct stream *stream, AVPacket *packet) {
bool is_config = packet->pts == AV_NOPTS_VALUE;
// A config packet must not be decoded immediately (it contains no
// frame); instead, it must be concatenated with the future data packet.
if (stream->pending || is_config) {
size_t offset;
if (stream->pending) {
offset = stream->pending->size;
if (av_grow_packet(stream->pending, packet->size)) {
LOG_OOM();
return false;
}
} else {
offset = 0;
stream->pending = av_packet_alloc();
if (!stream->pending) {
LOG_OOM();
return false;
}
if (av_new_packet(stream->pending, packet->size)) {
LOG_OOM();
av_packet_free(&stream->pending);
return false;
}
}
memcpy(stream->pending->data + offset, packet->data, packet->size);
if (!is_config) {
// prepare the concat packet to send to the decoder
stream->pending->pts = packet->pts;
stream->pending->dts = packet->dts;
stream->pending->flags = packet->flags;
packet = stream->pending;
}
}
if (is_config) {
// config packet
bool ok = push_packet_to_sinks(stream, packet);
if (!ok) {
return false;
}
} else {
// data packet
bool ok = stream_parse(stream, packet);
if (stream->pending) {
// the pending packet must be discarded (consumed or error)
av_packet_free(&stream->pending);
}
if (!ok) {
return false;
}
}
return true;
}
static void
stream_close_first_sinks(struct stream *stream, unsigned count) {
while (count) {
struct sc_packet_sink *sink = stream->sinks[--count];
sink->ops->close(sink);
}
}
static inline void
stream_close_sinks(struct stream *stream) {
stream_close_first_sinks(stream, stream->sink_count);
}
static bool
stream_open_sinks(struct stream *stream, const AVCodec *codec) {
for (unsigned i = 0; i < stream->sink_count; ++i) {
struct sc_packet_sink *sink = stream->sinks[i];
if (!sink->ops->open(sink, codec)) {
LOGE("Could not open packet sink %d", i);
stream_close_first_sinks(stream, i);
return false;
}
}
return true;
}
static int
run_stream(void *data) {
struct stream *stream = data;
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
LOGE("H.264 decoder not found");
goto end;
}
stream->codec_ctx = avcodec_alloc_context3(codec);
if (!stream->codec_ctx) {
LOG_OOM();
goto end;
}
if (!stream_open_sinks(stream, codec)) {
LOGE("Could not open stream sinks");
goto finally_free_codec_ctx;
}
stream->parser = av_parser_init(AV_CODEC_ID_H264);
if (!stream->parser) {
LOGE("Could not initialize parser");
goto finally_close_sinks;
}
// We must only pass complete frames to av_parser_parse2()!
// It's more complicated, but this allows to reduce the latency by 1 frame!
stream->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
AVPacket *packet = av_packet_alloc();
if (!packet) {
LOG_OOM();
goto finally_close_parser;
}
for (;;) {
bool ok = stream_recv_packet(stream, packet);
if (!ok) {
// end of stream
break;
}
ok = stream_push_packet(stream, packet);
av_packet_unref(packet);
if (!ok) {
// cannot process packet (error already logged)
break;
}
}
LOGD("End of frames");
if (stream->pending) {
av_packet_free(&stream->pending);
}
av_packet_free(&packet);
finally_close_parser:
av_parser_close(stream->parser);
finally_close_sinks:
stream_close_sinks(stream);
finally_free_codec_ctx:
avcodec_free_context(&stream->codec_ctx);
end:
stream->cbs->on_eos(stream, stream->cbs_userdata);
return 0;
}
void
stream_init(struct stream *stream, sc_socket socket,
const struct stream_callbacks *cbs, void *cbs_userdata) {
stream->socket = socket;
stream->pending = NULL;
stream->sink_count = 0;
assert(cbs && cbs->on_eos);
stream->cbs = cbs;
stream->cbs_userdata = cbs_userdata;
}
void
stream_add_sink(struct stream *stream, struct sc_packet_sink *sink) {
assert(stream->sink_count < STREAM_MAX_SINKS);
assert(sink);
assert(sink->ops);
stream->sinks[stream->sink_count++] = sink;
}
bool
stream_start(struct stream *stream) {
LOGD("Starting stream thread");
bool ok =
sc_thread_create(&stream->thread, run_stream, "scrcpy-stream", stream);
if (!ok) {
LOGC("Could not start stream thread");
return false;
}
return true;
}
void
stream_join(struct stream *stream) {
sc_thread_join(&stream->thread, NULL);
}

View File

@ -176,7 +176,7 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
bool
sc_process_terminate(pid_t pid) {
if (pid <= 0) {
LOGC("Requested to kill %d, this is an error. Please report the bug.\n",
LOGE("Requested to kill %d, this is an error. Please report the bug.\n",
(int) pid);
abort();
}

View File

@ -45,84 +45,9 @@ sc_hid_event_destroy(struct sc_hid_event *hid_event) {
free(hid_event->buffer);
}
static inline void
log_libusb_error(enum libusb_error errcode) {
LOGW("libusb error: %s", libusb_strerror(errcode));
}
static bool
accept_device(libusb_device *device, const char *serial) {
// do not log any USB error in this function, it is expected that many USB
// devices available on the computer have permission restrictions
struct libusb_device_descriptor desc;
int result = libusb_get_device_descriptor(device, &desc);
if (result < 0 || !desc.iSerialNumber) {
return false;
}
libusb_device_handle *handle;
result = libusb_open(device, &handle);
if (result < 0) {
return false;
}
char buffer[128];
result = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
(unsigned char *) buffer,
sizeof(buffer));
libusb_close(handle);
if (result < 0) {
return false;
}
buffer[sizeof(buffer) - 1] = '\0'; // just in case
// accept the device if its serial matches
return !strcmp(buffer, serial);
}
static libusb_device *
sc_aoa_find_usb_device(const char *serial) {
if (!serial) {
return NULL;
}
libusb_device **list;
libusb_device *result = NULL;
ssize_t count = libusb_get_device_list(NULL, &list);
if (count < 0) {
log_libusb_error((enum libusb_error) count);
return NULL;
}
for (size_t i = 0; i < (size_t) count; ++i) {
libusb_device *device = list[i];
if (accept_device(device, serial)) {
result = libusb_ref_device(device);
break;
}
}
libusb_free_device_list(list, 1);
return result;
}
static int
sc_aoa_open_usb_handle(libusb_device *device, libusb_device_handle **handle) {
int result = libusb_open(device, handle);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
return result;
}
return 0;
}
bool
sc_aoa_init(struct sc_aoa *aoa, const char *serial,
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
struct sc_acksync *acksync) {
assert(acksync);
cbuf_init(&aoa->queue);
if (!sc_mutex_init(&aoa->mutex)) {
@ -130,39 +55,15 @@ sc_aoa_init(struct sc_aoa *aoa, const char *serial,
}
if (!sc_cond_init(&aoa->event_cond)) {
goto error_destroy_mutex;
}
if (libusb_init(&aoa->usb_context) != LIBUSB_SUCCESS) {
goto error_destroy_cond;
}
aoa->usb_device = sc_aoa_find_usb_device(serial);
if (!aoa->usb_device) {
LOGW("USB device of serial %s not found", serial);
goto error_exit_libusb;
}
if (sc_aoa_open_usb_handle(aoa->usb_device, &aoa->usb_handle) < 0) {
LOGW("Open USB handle failed");
goto error_unref_device;
sc_mutex_destroy(&aoa->mutex);
return false;
}
aoa->stopped = false;
aoa->acksync = acksync;
aoa->usb = usb;
return true;
error_unref_device:
libusb_unref_device(aoa->usb_device);
error_exit_libusb:
libusb_exit(aoa->usb_context);
error_destroy_cond:
sc_cond_destroy(&aoa->event_cond);
error_destroy_mutex:
sc_mutex_destroy(&aoa->mutex);
return false;
}
void
@ -173,9 +74,6 @@ sc_aoa_destroy(struct sc_aoa *aoa) {
sc_hid_event_destroy(&event);
}
libusb_close(aoa->usb_handle);
libusb_unref_device(aoa->usb_device);
libusb_exit(aoa->usb_context);
sc_cond_destroy(&aoa->event_cond);
sc_mutex_destroy(&aoa->mutex);
}
@ -192,11 +90,12 @@ sc_aoa_register_hid(struct sc_aoa *aoa, uint16_t accessory_id,
uint16_t index = report_desc_size;
unsigned char *buffer = NULL;
uint16_t length = 0;
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
LOGE("REGISTER_HID: libusb error: %s", libusb_strerror(result));
sc_usb_check_disconnected(aoa->usb, result);
return false;
}
@ -228,11 +127,12 @@ sc_aoa_set_hid_report_desc(struct sc_aoa *aoa, uint16_t accessory_id,
// libusb_control_transfer expects a pointer to non-const
unsigned char *buffer = (unsigned char *) report_desc;
uint16_t length = report_desc_size;
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
LOGE("SET_HID_REPORT_DESC: libusb error: %s", libusb_strerror(result));
sc_usb_check_disconnected(aoa->usb, result);
return false;
}
@ -270,11 +170,12 @@ sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
uint16_t index = 0;
unsigned char *buffer = event->buffer;
uint16_t length = event->size;
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
LOGE("SEND_HID_EVENT: libusb error: %s", libusb_strerror(result));
sc_usb_check_disconnected(aoa->usb, result);
return false;
}
@ -292,11 +193,12 @@ sc_aoa_unregister_hid(struct sc_aoa *aoa, const uint16_t accessory_id) {
uint16_t index = 0;
unsigned char *buffer = NULL;
uint16_t length = 0;
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
LOGE("UNREGISTER_HID: libusb error: %s", libusb_strerror(result));
sc_usb_check_disconnected(aoa->usb, result);
return false;
}
@ -343,6 +245,11 @@ run_aoa_thread(void *data) {
if (ack_to_wait != SC_SEQUENCE_INVALID) {
LOGD("Waiting ack from server sequence=%" PRIu64_, ack_to_wait);
// If some events have ack_to_wait set, then sc_aoa must have been
// initialized with a non NULL acksync
assert(aoa->acksync);
// Do not block the loop indefinitely if the ack never comes (it
// should never happen)
sc_tick deadline = sc_tick_now() + SC_TICK_FROM_MS(500);
@ -375,7 +282,7 @@ sc_aoa_start(struct sc_aoa *aoa) {
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "scrcpy-aoa", aoa);
if (!ok) {
LOGC("Could not start AOA thread");
LOGE("Could not start AOA thread");
return false;
}
@ -389,7 +296,9 @@ sc_aoa_stop(struct sc_aoa *aoa) {
sc_cond_signal(&aoa->event_cond);
sc_mutex_unlock(&aoa->mutex);
if (aoa->acksync) {
sc_acksync_interrupt(aoa->acksync);
}
}
void

View File

@ -6,6 +6,7 @@
#include <libusb-1.0/libusb.h>
#include "usb.h"
#include "util/acksync.h"
#include "util/cbuf.h"
#include "util/thread.h"
@ -29,9 +30,7 @@ sc_hid_event_destroy(struct sc_hid_event *hid_event);
struct sc_hid_event_queue CBUF(struct sc_hid_event, 64);
struct sc_aoa {
libusb_context *usb_context;
libusb_device *usb_device;
libusb_device_handle *usb_handle;
struct sc_usb *usb;
sc_thread thread;
sc_mutex mutex;
sc_cond event_cond;
@ -42,7 +41,7 @@ struct sc_aoa {
};
bool
sc_aoa_init(struct sc_aoa *aoa, const char *serial, struct sc_acksync *acksync);
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb, struct sc_acksync *acksync);
void
sc_aoa_destroy(struct sc_aoa *aoa);

View File

@ -340,7 +340,7 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t mods_state) {
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
sc_hid_event_destroy(&hid_event);
LOGW("Could request HID event");
LOGW("Could not request HID event (mod lock state)");
return false;
}
@ -382,7 +382,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
sc_hid_event_destroy(&hid_event);
LOGW("Could request HID event");
LOGW("Could not request HID event (key)");
}
}
}

View File

@ -181,7 +181,7 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
sc_hid_event_destroy(&hid_event);
LOGW("Could request HID event");
LOGW("Could not request HID event (mouse motion)");
}
}
@ -203,7 +203,7 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
sc_hid_event_destroy(&hid_event);
LOGW("Could request HID event");
LOGW("Could not request HID event (mouse click)");
}
}
@ -228,7 +228,7 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
sc_hid_event_destroy(&hid_event);
LOGW("Could request HID event");
LOGW("Could not request HID event (mouse scroll)");
}
}

211
app/src/usb/scrcpy_otg.c Normal file
View File

@ -0,0 +1,211 @@
#include "scrcpy_otg.h"
#include <SDL2/SDL.h>
#include "adb/adb.h"
#include "events.h"
#include "screen_otg.h"
#include "util/log.h"
struct scrcpy_otg {
struct sc_usb usb;
struct sc_aoa aoa;
struct sc_hid_keyboard keyboard;
struct sc_hid_mouse mouse;
struct sc_screen_otg screen_otg;
};
static void
sc_usb_on_disconnected(struct sc_usb *usb, void *userdata) {
(void) usb;
(void) userdata;
SDL_Event event;
event.type = EVENT_USB_DEVICE_DISCONNECTED;
int ret = SDL_PushEvent(&event);
if (ret < 0) {
LOGE("Could not post USB disconnection event: %s", SDL_GetError());
}
}
static enum scrcpy_exit_code
event_loop(struct scrcpy_otg *s) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case EVENT_USB_DEVICE_DISCONNECTED:
LOGW("Device disconnected");
return SCRCPY_EXIT_DISCONNECTED;
case SDL_QUIT:
LOGD("User requested to quit");
return SCRCPY_EXIT_SUCCESS;
default:
sc_screen_otg_handle_event(&s->screen_otg, &event);
break;
}
}
return SCRCPY_EXIT_FAILURE;
}
enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options) {
static struct scrcpy_otg scrcpy_otg;
struct scrcpy_otg *s = &scrcpy_otg;
const char *serial = options->serial;
// Minimal SDL initialization
if (SDL_Init(SDL_INIT_EVENTS)) {
LOGE("Could not initialize SDL: %s", SDL_GetError());
return false;
}
atexit(SDL_Quit);
if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
LOGW("Could not enable mouse focus clickthrough");
}
enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;
struct sc_hid_keyboard *keyboard = NULL;
struct sc_hid_mouse *mouse = NULL;
bool usb_device_initialized = false;
bool usb_connected = false;
bool aoa_started = false;
bool aoa_initialized = false;
#ifdef _WIN32
// On Windows, only one process could open a USB device
// <https://github.com/Genymobile/scrcpy/issues/2773>
LOGI("Killing adb daemon (if any)...");
unsigned flags = SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR;
// uninterruptible (intr == NULL), but in practice it's very quick
sc_adb_kill_server(NULL, flags);
#endif
static const struct sc_usb_callbacks cbs = {
.on_disconnected = sc_usb_on_disconnected,
};
bool ok = sc_usb_init(&s->usb);
if (!ok) {
return SCRCPY_EXIT_FAILURE;
}
struct sc_usb_device usb_device;
ok = sc_usb_select_device(&s->usb, serial, &usb_device);
if (!ok) {
goto end;
}
usb_device_initialized = true;
LOGI("USB device: %s (%04x:%04x) %s %s", usb_device.serial,
(unsigned) usb_device.vid, (unsigned) usb_device.pid,
usb_device.manufacturer, usb_device.product);
ok = sc_usb_connect(&s->usb, usb_device.device, &cbs, NULL);
if (!ok) {
goto end;
}
usb_connected = true;
ok = sc_aoa_init(&s->aoa, &s->usb, NULL);
if (!ok) {
goto end;
}
aoa_initialized = true;
bool enable_keyboard =
options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID;
bool enable_mouse =
options->mouse_input_mode == SC_MOUSE_INPUT_MODE_HID;
// If neither --hid-keyboard or --hid-mouse is passed, enable both
if (!enable_keyboard && !enable_mouse) {
enable_keyboard = true;
enable_mouse = true;
}
if (enable_keyboard) {
ok = sc_hid_keyboard_init(&s->keyboard, &s->aoa);
if (!ok) {
goto end;
}
keyboard = &s->keyboard;
}
if (enable_mouse) {
ok = sc_hid_mouse_init(&s->mouse, &s->aoa);
if (!ok) {
goto end;
}
mouse = &s->mouse;
}
ok = sc_aoa_start(&s->aoa);
if (!ok) {
goto end;
}
aoa_started = true;
const char *window_title = options->window_title;
if (!window_title) {
window_title = usb_device.product ? usb_device.product : "scrcpy";
}
struct sc_screen_otg_params params = {
.keyboard = keyboard,
.mouse = mouse,
.window_title = window_title,
.always_on_top = options->always_on_top,
.window_x = options->window_x,
.window_y = options->window_y,
.window_borderless = options->window_borderless,
};
ok = sc_screen_otg_init(&s->screen_otg, &params);
if (!ok) {
goto end;
}
// usb_device not needed anymore
sc_usb_device_destroy(&usb_device);
usb_device_initialized = false;
ret = event_loop(s);
LOGD("quit...");
end:
if (aoa_started) {
sc_aoa_stop(&s->aoa);
}
sc_usb_stop(&s->usb);
if (mouse) {
sc_hid_mouse_destroy(&s->mouse);
}
if (keyboard) {
sc_hid_keyboard_destroy(&s->keyboard);
}
if (aoa_initialized) {
sc_aoa_join(&s->aoa);
sc_aoa_destroy(&s->aoa);
}
sc_usb_join(&s->usb);
if (usb_connected) {
sc_usb_disconnect(&s->usb);
}
if (usb_device_initialized) {
sc_usb_device_destroy(&usb_device);
}
sc_usb_destroy(&s->usb);
return ret;
}

12
app/src/usb/scrcpy_otg.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SCRCPY_OTG_H
#define SCRCPY_OTG_H
#include "common.h"
#include "options.h"
#include "scrcpy.h"
enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options);
#endif

294
app/src/usb/screen_otg.c Normal file
View File

@ -0,0 +1,294 @@
#include "screen_otg.h"
#include "icon.h"
#include "options.h"
#include "util/log.h"
static void
sc_screen_otg_set_mouse_capture(struct sc_screen_otg *screen, bool capture) {
#ifdef __APPLE__
// Workaround for SDL bug on macOS:
// <https://github.com/libsdl-org/SDL/issues/5340>
if (capture) {
int mouse_x, mouse_y;
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
int x, y, w, h;
SDL_GetWindowPosition(screen->window, &x, &y);
SDL_GetWindowSize(screen->window, &w, &h);
bool outside_window = mouse_x < x || mouse_x >= x + w
|| mouse_y < y || mouse_y >= y + h;
if (outside_window) {
SDL_WarpMouseInWindow(screen->window, w / 2, h / 2);
}
}
#else
(void) screen;
#endif
if (SDL_SetRelativeMouseMode(capture)) {
LOGE("Could not set relative mouse mode to %s: %s",
capture ? "true" : "false", SDL_GetError());
}
}
static inline bool
sc_screen_otg_get_mouse_capture(struct sc_screen_otg *screen) {
(void) screen;
return SDL_GetRelativeMouseMode();
}
static inline void
sc_screen_otg_toggle_mouse_capture(struct sc_screen_otg *screen) {
(void) screen;
bool new_value = !sc_screen_otg_get_mouse_capture(screen);
sc_screen_otg_set_mouse_capture(screen, new_value);
}
static void
sc_screen_otg_render(struct sc_screen_otg *screen) {
SDL_RenderClear(screen->renderer);
if (screen->texture) {
SDL_RenderCopy(screen->renderer, screen->texture, NULL, NULL);
}
SDL_RenderPresent(screen->renderer);
}
bool
sc_screen_otg_init(struct sc_screen_otg *screen,
const struct sc_screen_otg_params *params) {
screen->keyboard = params->keyboard;
screen->mouse = params->mouse;
screen->mouse_capture_key_pressed = 0;
const char *title = params->window_title;
assert(title);
int x = params->window_x != SC_WINDOW_POSITION_UNDEFINED
? params->window_x : (int) SDL_WINDOWPOS_UNDEFINED;
int y = params->window_y != SC_WINDOW_POSITION_UNDEFINED
? params->window_y : (int) SDL_WINDOWPOS_UNDEFINED;
int width = 256;
int height = 256;
uint32_t window_flags = 0;
if (params->always_on_top) {
window_flags |= SDL_WINDOW_ALWAYS_ON_TOP;
}
if (params->window_borderless) {
window_flags |= SDL_WINDOW_BORDERLESS;
}
screen->window = SDL_CreateWindow(title, x, y, width, height, window_flags);
if (!screen->window) {
LOGE("Could not create window: %s", SDL_GetError());
return false;
}
screen->renderer = SDL_CreateRenderer(screen->window, -1, 0);
if (!screen->renderer) {
LOGE("Could not create renderer: %s", SDL_GetError());
goto error_destroy_window;
}
SDL_Surface *icon = scrcpy_icon_load();
if (icon) {
SDL_SetWindowIcon(screen->window, icon);
screen->texture = SDL_CreateTextureFromSurface(screen->renderer, icon);
scrcpy_icon_destroy(icon);
if (!screen->texture) {
goto error_destroy_renderer;
}
} else {
screen->texture = NULL;
LOGW("Could not load icon");
}
if (screen->mouse) {
// Capture mouse on start
sc_screen_otg_set_mouse_capture(screen, true);
}
return true;
error_destroy_window:
SDL_DestroyWindow(screen->window);
error_destroy_renderer:
SDL_DestroyRenderer(screen->renderer);
return false;
}
void
sc_screen_otg_destroy(struct sc_screen_otg *screen) {
if (screen->texture) {
SDL_DestroyTexture(screen->texture);
}
SDL_DestroyRenderer(screen->renderer);
SDL_DestroyWindow(screen->window);
}
static inline bool
sc_screen_otg_is_mouse_capture_key(SDL_Keycode key) {
return key == SDLK_LALT || key == SDLK_LGUI || key == SDLK_RGUI;
}
static void
sc_screen_otg_process_key(struct sc_screen_otg *screen,
const SDL_KeyboardEvent *event) {
assert(screen->keyboard);
struct sc_key_processor *kp = &screen->keyboard->key_processor;
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),
.repeat = event->repeat,
.mods_state = sc_mods_state_from_sdl(event->keysym.mod),
};
assert(kp->ops->process_key);
kp->ops->process_key(kp, &evt, SC_SEQUENCE_INVALID);
}
static void
sc_screen_otg_process_mouse_motion(struct sc_screen_otg *screen,
const SDL_MouseMotionEvent *event) {
assert(screen->mouse);
struct sc_mouse_processor *mp = &screen->mouse->mouse_processor;
struct sc_mouse_motion_event evt = {
// .position not used for HID events
.xrel = event->xrel,
.yrel = event->yrel,
.buttons_state = sc_mouse_buttons_state_from_sdl(event->state, true),
};
assert(mp->ops->process_mouse_motion);
mp->ops->process_mouse_motion(mp, &evt);
}
static void
sc_screen_otg_process_mouse_button(struct sc_screen_otg *screen,
const SDL_MouseButtonEvent *event) {
assert(screen->mouse);
struct sc_mouse_processor *mp = &screen->mouse->mouse_processor;
uint32_t sdl_buttons_state = SDL_GetMouseState(NULL, NULL);
struct sc_mouse_click_event evt = {
// .position not used for HID events
.action = sc_action_from_sdl_mousebutton_type(event->type),
.button = sc_mouse_button_from_sdl(event->button),
.buttons_state =
sc_mouse_buttons_state_from_sdl(sdl_buttons_state, true),
};
assert(mp->ops->process_mouse_click);
mp->ops->process_mouse_click(mp, &evt);
}
static void
sc_screen_otg_process_mouse_wheel(struct sc_screen_otg *screen,
const SDL_MouseWheelEvent *event) {
assert(screen->mouse);
struct sc_mouse_processor *mp = &screen->mouse->mouse_processor;
uint32_t sdl_buttons_state = SDL_GetMouseState(NULL, NULL);
struct sc_mouse_scroll_event evt = {
// .position not used for HID events
.hscroll = event->x,
.vscroll = event->y,
.buttons_state =
sc_mouse_buttons_state_from_sdl(sdl_buttons_state, true),
};
assert(mp->ops->process_mouse_scroll);
mp->ops->process_mouse_scroll(mp, &evt);
}
void
sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
switch (event->type) {
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:
sc_screen_otg_render(screen);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (screen->mouse) {
sc_screen_otg_set_mouse_capture(screen, false);
}
break;
}
return;
case SDL_KEYDOWN:
if (screen->mouse) {
SDL_Keycode key = event->key.keysym.sym;
if (sc_screen_otg_is_mouse_capture_key(key)) {
if (!screen->mouse_capture_key_pressed) {
screen->mouse_capture_key_pressed = key;
} else {
// Another mouse capture key has been pressed, cancel
// mouse (un)capture
screen->mouse_capture_key_pressed = 0;
}
// Mouse capture keys are never forwarded to the device
return;
}
}
if (screen->keyboard) {
sc_screen_otg_process_key(screen, &event->key);
}
break;
case SDL_KEYUP:
if (screen->mouse) {
SDL_Keycode key = event->key.keysym.sym;
SDL_Keycode cap = screen->mouse_capture_key_pressed;
screen->mouse_capture_key_pressed = 0;
if (sc_screen_otg_is_mouse_capture_key(key)) {
if (key == cap) {
// A mouse capture key has been pressed then released:
// toggle the capture mouse mode
sc_screen_otg_toggle_mouse_capture(screen);
}
// Mouse capture keys are never forwarded to the device
return;
}
}
if (screen->keyboard) {
sc_screen_otg_process_key(screen, &event->key);
}
break;
case SDL_MOUSEMOTION:
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
sc_screen_otg_process_mouse_motion(screen, &event->motion);
}
break;
case SDL_MOUSEBUTTONDOWN:
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
sc_screen_otg_process_mouse_button(screen, &event->button);
}
break;
case SDL_MOUSEBUTTONUP:
if (screen->mouse) {
if (sc_screen_otg_get_mouse_capture(screen)) {
sc_screen_otg_process_mouse_button(screen, &event->button);
} else {
sc_screen_otg_set_mouse_capture(screen, true);
}
}
break;
case SDL_MOUSEWHEEL:
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
sc_screen_otg_process_mouse_wheel(screen, &event->wheel);
}
break;
}
}

45
app/src/usb/screen_otg.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef SC_SCREEN_OTG_H
#define SC_SCREEN_OTG_H
#include "common.h"
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "hid_keyboard.h"
#include "hid_mouse.h"
struct sc_screen_otg {
struct sc_hid_keyboard *keyboard;
struct sc_hid_mouse *mouse;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
// See equivalent mechanism in screen.h
SDL_Keycode mouse_capture_key_pressed;
};
struct sc_screen_otg_params {
struct sc_hid_keyboard *keyboard;
struct sc_hid_mouse *mouse;
const char *window_title;
bool always_on_top;
int16_t window_x; // accepts SC_WINDOW_POSITION_UNDEFINED
int16_t window_y; // accepts SC_WINDOW_POSITION_UNDEFINED
bool window_borderless;
};
bool
sc_screen_otg_init(struct sc_screen_otg *screen,
const struct sc_screen_otg_params *params);
void
sc_screen_otg_destroy(struct sc_screen_otg *screen);
void
sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event);
#endif

374
app/src/usb/usb.c Normal file
View File

@ -0,0 +1,374 @@
#include "usb.h"
#include <assert.h>
#include "util/log.h"
#include "util/vector.h"
struct sc_vec_usb_devices SC_VECTOR(struct sc_usb_device);
static char *
read_string(libusb_device_handle *handle, uint8_t desc_index) {
char buffer[128];
int result =
libusb_get_string_descriptor_ascii(handle, desc_index,
(unsigned char *) buffer,
sizeof(buffer));
if (result < 0) {
LOGD("Read string: libusb error: %s", libusb_strerror(result));
return NULL;
}
assert((size_t) result <= sizeof(buffer));
// When non-negative, 'result' contains the number of bytes written
char *s = malloc(result + 1);
memcpy(s, buffer, result);
s[result] = '\0';
return s;
}
static bool
sc_usb_read_device(libusb_device *device, struct sc_usb_device *out) {
// Do not log any USB error in this function, it is expected that many USB
// devices available on the computer have permission restrictions
struct libusb_device_descriptor desc;
int result = libusb_get_device_descriptor(device, &desc);
if (result < 0 || !desc.iSerialNumber) {
return false;
}
libusb_device_handle *handle;
result = libusb_open(device, &handle);
if (result < 0) {
// Log at debug level because it is expected that some non-Android USB
// devices present on the computer require special permissions
LOGD("Open USB device %04x:%04x: libusb error: %s",
(unsigned) desc.idVendor, (unsigned) desc.idProduct,
libusb_strerror(result));
return false;
}
char *device_serial = read_string(handle, desc.iSerialNumber);
if (!device_serial) {
libusb_close(handle);
return false;
}
out->device = libusb_ref_device(device);
out->serial = device_serial;
out->vid = desc.idVendor;
out->pid = desc.idProduct;
out->manufacturer = read_string(handle, desc.iManufacturer);
out->product = read_string(handle, desc.iProduct);
out->selected = false;
libusb_close(handle);
return true;
}
void
sc_usb_device_destroy(struct sc_usb_device *usb_device) {
if (usb_device->device) {
libusb_unref_device(usb_device->device);
}
free(usb_device->serial);
free(usb_device->manufacturer);
free(usb_device->product);
}
void
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src) {
*dst = *src;
src->device = NULL;
src->serial = NULL;
src->manufacturer = NULL;
src->product = NULL;
}
void
sc_usb_devices_destroy(struct sc_vec_usb_devices *usb_devices) {
for (size_t i = 0; i < usb_devices->size; ++i) {
sc_usb_device_destroy(&usb_devices->data[i]);
}
sc_vector_destroy(usb_devices);
}
static bool
sc_usb_list_devices(struct sc_usb *usb, struct sc_vec_usb_devices *out_vec) {
libusb_device **list;
ssize_t count = libusb_get_device_list(usb->context, &list);
if (count < 0) {
LOGE("List USB devices: libusb error: %s", libusb_strerror(count));
return false;
}
for (size_t i = 0; i < (size_t) count; ++i) {
libusb_device *device = list[i];
struct sc_usb_device usb_device;
if (sc_usb_read_device(device, &usb_device)) {
bool ok = sc_vector_push(out_vec, usb_device);
if (!ok) {
LOG_OOM();
LOGE("Could not push usb_device to vector");
sc_usb_device_destroy(&usb_device);
// continue anyway
}
}
}
libusb_free_device_list(list, 1);
return true;
}
static bool
sc_usb_accept_device(const struct sc_usb_device *device, const char *serial) {
if (!serial) {
return true;
}
return !strcmp(serial, device->serial);
}
static size_t
sc_usb_devices_select(struct sc_usb_device *devices, size_t len,
const char *serial, size_t *idx_out) {
size_t count = 0;
for (size_t i = 0; i < len; ++i) {
struct sc_usb_device *device = &devices[i];
device->selected = sc_usb_accept_device(device, serial);
if (device->selected) {
if (idx_out && !count) {
*idx_out = i;
}
++count;
}
}
return count;
}
static void
sc_usb_devices_log(enum sc_log_level level, struct sc_usb_device *devices,
size_t count) {
for (size_t i = 0; i < count; ++i) {
struct sc_usb_device *d = &devices[i];
const char *selection = d->selected ? "-->" : " ";
// Convert uint16_t to unsigned because PRIx16 may not exist on Windows
LOG(level, " %s %-18s (%04x:%04x) %s %s",
selection, d->serial, (unsigned) d->vid, (unsigned) d->pid,
d->manufacturer, d->product);
}
}
bool
sc_usb_select_device(struct sc_usb *usb, const char *serial,
struct sc_usb_device *out_device) {
struct sc_vec_usb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_usb_list_devices(usb, &vec);
if (!ok) {
LOGE("Could not list USB devices");
return false;
}
if (vec.size == 0) {
LOGE("Could not find any USB device");
return false;
}
size_t sel_idx; // index of the single matching device if sel_count == 1
size_t sel_count =
sc_usb_devices_select(vec.data, vec.size, serial, &sel_idx);
if (sel_count == 0) {
// if count > 0 && sel_count == 0, then necessarily a serial is provided
assert(serial);
LOGE("Could not find USB device %s", serial);
sc_usb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
sc_usb_devices_destroy(&vec);
return false;
}
if (sel_count > 1) {
if (serial) {
LOGE("Multiple (%" SC_PRIsizet ") USB devices with serial %s:",
sel_count, serial);
} else {
LOGE("Multiple (%" SC_PRIsizet ") USB devices:", sel_count);
}
sc_usb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
LOGE("Select a device via -s (--serial)");
sc_usb_devices_destroy(&vec);
return false;
}
assert(sel_count == 1); // sel_idx is valid only if sel_count == 1
struct sc_usb_device *device = &vec.data[sel_idx];
LOGD("USB device found:");
sc_usb_devices_log(SC_LOG_LEVEL_DEBUG, vec.data, vec.size);
// Move device into out_device (do not destroy device)
sc_usb_device_move(out_device, device);
sc_usb_devices_destroy(&vec);
return true;
}
bool
sc_usb_init(struct sc_usb *usb) {
usb->handle = NULL;
return libusb_init(&usb->context) == LIBUSB_SUCCESS;
}
void
sc_usb_destroy(struct sc_usb *usb) {
libusb_exit(usb->context);
}
static void
sc_usb_report_disconnected(struct sc_usb *usb) {
if (usb->cbs && !atomic_flag_test_and_set(&usb->disconnection_notified)) {
assert(usb->cbs && usb->cbs->on_disconnected);
usb->cbs->on_disconnected(usb, usb->cbs_userdata);
}
}
bool
sc_usb_check_disconnected(struct sc_usb *usb, int result) {
if (result == LIBUSB_ERROR_NO_DEVICE || result == LIBUSB_ERROR_NOT_FOUND) {
sc_usb_report_disconnected(usb);
return false;
}
return true;
}
static LIBUSB_CALL int
sc_usb_libusb_callback(libusb_context *ctx, libusb_device *device,
libusb_hotplug_event event, void *userdata) {
(void) ctx;
(void) device;
(void) event;
struct sc_usb *usb = userdata;
libusb_device *dev = libusb_get_device(usb->handle);
assert(dev);
if (dev != device) {
// Not the connected device
return 0;
}
sc_usb_report_disconnected(usb);
// Do not automatically deregister the callback by returning 1. Instead,
// manually deregister to interrupt libusb_handle_events() from the libusb
// event thread: <https://stackoverflow.com/a/60119225/1987178>
return 0;
}
static int
run_libusb_event_handler(void *data) {
struct sc_usb *usb = data;
while (!atomic_load(&usb->stopped)) {
// Interrupted by events or by libusb_hotplug_deregister_callback()
libusb_handle_events(usb->context);
}
return 0;
}
static bool
sc_usb_register_callback(struct sc_usb *usb) {
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
LOGW("On this platform, libusb does not have hotplug capability; "
"device disconnection will not be detected properly");
return false;
}
libusb_device *device = libusb_get_device(usb->handle);
assert(device);
struct libusb_device_descriptor desc;
int result = libusb_get_device_descriptor(device, &desc);
if (result < 0) {
LOGE("Device descriptor: libusb error: %s", libusb_strerror(result));
return false;
}
int events = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
int flags = LIBUSB_HOTPLUG_NO_FLAGS;
int vendor_id = desc.idVendor;
int product_id = desc.idProduct;
int dev_class = LIBUSB_HOTPLUG_MATCH_ANY;
result = libusb_hotplug_register_callback(usb->context, events, flags,
vendor_id, product_id, dev_class,
sc_usb_libusb_callback, usb,
&usb->callback_handle);
if (result < 0) {
LOGE("Register hotplog callback: libusb error: %s",
libusb_strerror(result));
return false;
}
usb->has_callback_handle = true;
return true;
}
bool
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
const struct sc_usb_callbacks *cbs, void *cbs_userdata) {
int result = libusb_open(device, &usb->handle);
if (result < 0) {
LOGE("Open USB device: libusb error: %s", libusb_strerror(result));
return false;
}
usb->has_callback_handle = false;
usb->has_libusb_event_thread = false;
// If cbs is set, then cbs->on_disconnected must be set
assert(!cbs || cbs->on_disconnected);
usb->cbs = cbs;
usb->cbs_userdata = cbs_userdata;
if (cbs) {
atomic_init(&usb->stopped, false);
usb->disconnection_notified = (atomic_flag) ATOMIC_FLAG_INIT;
if (sc_usb_register_callback(usb)) {
// Create a thread to process libusb events, so that device
// disconnection could be detected immediately
usb->has_libusb_event_thread =
sc_thread_create(&usb->libusb_event_thread,
run_libusb_event_handler, "scrcpy-usbev", usb);
if (!usb->has_libusb_event_thread) {
LOGW("Libusb event thread handler could not be created, USB "
"device disconnection might not be detected immediately");
}
}
}
return true;
}
void
sc_usb_disconnect(struct sc_usb *usb) {
libusb_close(usb->handle);
}
void
sc_usb_stop(struct sc_usb *usb) {
if (usb->has_callback_handle) {
atomic_store(&usb->stopped, true);
libusb_hotplug_deregister_callback(usb->context, usb->callback_handle);
}
}
void
sc_usb_join(struct sc_usb *usb) {
if (usb->has_libusb_event_thread) {
sc_thread_join(&usb->libusb_event_thread, NULL);
}
}

88
app/src/usb/usb.h Normal file
View File

@ -0,0 +1,88 @@
#ifndef SC_USB_H
#define SC_USB_H
#include "common.h"
#include <stdbool.h>
#include <libusb-1.0/libusb.h>
#include "util/thread.h"
struct sc_usb {
libusb_context *context;
libusb_device_handle *handle;
const struct sc_usb_callbacks *cbs;
void *cbs_userdata;
bool has_callback_handle;
libusb_hotplug_callback_handle callback_handle;
bool has_libusb_event_thread;
sc_thread libusb_event_thread;
atomic_bool stopped; // only used if cbs != NULL
atomic_flag disconnection_notified;
};
struct sc_usb_callbacks {
void (*on_disconnected)(struct sc_usb *usb, void *userdata);
};
struct sc_usb_device {
libusb_device *device;
char *serial;
char *manufacturer;
char *product;
uint16_t vid;
uint16_t pid;
bool selected;
};
void
sc_usb_device_destroy(struct sc_usb_device *usb_device);
/**
* Move src to dst
*
* After this call, the content of src is undefined, except that
* sc_usb_device_destroy() can be called.
*
* This is useful to take a device from a list that will be destroyed, without
* making unnecessary copies.
*/
void
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src);
void
sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count);
bool
sc_usb_init(struct sc_usb *usb);
void
sc_usb_destroy(struct sc_usb *usb);
bool
sc_usb_select_device(struct sc_usb *usb, const char *serial,
struct sc_usb_device *out_device);
bool
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
const struct sc_usb_callbacks *cbs, void *cbs_userdata);
void
sc_usb_disconnect(struct sc_usb *usb);
// A client should call this function with the return value of a libusb call
// to detect disconnection immediately
bool
sc_usb_check_disconnected(struct sc_usb *usb, int result);
void
sc_usb_stop(struct sc_usb *usb);
void
sc_usb_join(struct sc_usb *usb);
#endif

View File

@ -1,5 +1,5 @@
#ifndef BUFFER_UTIL_H
#define BUFFER_UTIL_H
#ifndef SC_BUFFER_UTIL_H
#define SC_BUFFER_UTIL_H
#include "common.h"
@ -7,13 +7,13 @@
#include <stdint.h>
static inline void
buffer_write16be(uint8_t *buf, uint16_t value) {
sc_write16be(uint8_t *buf, uint16_t value) {
buf[0] = value >> 8;
buf[1] = value;
}
static inline void
buffer_write32be(uint8_t *buf, uint32_t value) {
sc_write32be(uint8_t *buf, uint32_t value) {
buf[0] = value >> 24;
buf[1] = value >> 16;
buf[2] = value >> 8;
@ -21,25 +21,25 @@ buffer_write32be(uint8_t *buf, uint32_t value) {
}
static inline void
buffer_write64be(uint8_t *buf, uint64_t value) {
buffer_write32be(buf, value >> 32);
buffer_write32be(&buf[4], (uint32_t) value);
sc_write64be(uint8_t *buf, uint64_t value) {
sc_write32be(buf, value >> 32);
sc_write32be(&buf[4], (uint32_t) value);
}
static inline uint16_t
buffer_read16be(const uint8_t *buf) {
sc_read16be(const uint8_t *buf) {
return (buf[0] << 8) | buf[1];
}
static inline uint32_t
buffer_read32be(const uint8_t *buf) {
sc_read32be(const uint8_t *buf) {
return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}
static inline uint64_t
buffer_read64be(const uint8_t *buf) {
uint32_t msb = buffer_read32be(buf);
uint32_t lsb = buffer_read32be(&buf[4]);
sc_read64be(const uint8_t *buf) {
uint32_t msb = sc_read32be(buf);
uint32_t lsb = sc_read32be(&buf[4]);
return ((uint64_t) msb << 32) | lsb;
}

View File

@ -1,5 +1,8 @@
#include "log.h"
#if _WIN32
# include <windows.h>
#endif
#include <assert.h>
static SDL_LogPriority
@ -51,3 +54,34 @@ sc_get_log_level(void) {
SDL_LogPriority sdl_log = SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION);
return log_level_sdl_to_sc(sdl_log);
}
void
sc_log(enum sc_log_level level, const char *fmt, ...) {
SDL_LogPriority sdl_level = log_level_sc_to_sdl(level);
va_list ap;
va_start(ap, fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, sdl_level, fmt, ap);
va_end(ap);
}
#ifdef _WIN32
bool
sc_log_windows_error(const char *prefix, int error) {
assert(prefix);
char *message;
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM;
DWORD lang_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
int ret =
FormatMessage(flags, NULL, error, lang_id, (char *) &message, 0, NULL);
if (ret <= 0) {
return false;
}
// Note: message already contains a trailing '\n'
LOGE("%s: [%d] %s", prefix, error, message);
LocalFree(message);
return true;
}
#endif

View File

@ -15,10 +15,9 @@
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGW(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOG_OOM() \
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
LOGE("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
void
sc_set_log_level(enum sc_log_level level);
@ -26,4 +25,14 @@ sc_set_log_level(enum sc_log_level level);
enum sc_log_level
sc_get_log_level(void);
void
sc_log(enum sc_log_level level, const char *fmt, ...);
#define LOG(LEVEL, ...) sc_log((LEVEL), __VA_ARGS__)
#ifdef _WIN32
// Log system error (typically returned by GetLastError() or similar)
bool
sc_log_windows_error(const char *prefix, int error);
#endif
#endif

View File

@ -33,7 +33,7 @@ net_init(void) {
WSADATA wsa;
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0;
if (res < 0) {
LOGC("WSAStartup failed with error %d", res);
LOGE("WSAStartup failed with error %d", res);
return false;
}
#endif
@ -117,14 +117,7 @@ set_cloexec_flag(sc_raw_socket raw_sock) {
static void
net_perror(const char *s) {
#ifdef _WIN32
int error = WSAGetLastError();
char *wsa_message;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char *) &wsa_message, 0, NULL);
// no explicit '\n', wsa_message already contains a trailing '\n'
fprintf(stderr, "%s: [%d] %s", s, error, wsa_message);
LocalFree(wsa_message);
sc_log_windows_error(s, WSAGetLastError());
#else
perror(s);
#endif

View File

@ -12,10 +12,8 @@
# include <winsock2.h>
# include <windows.h>
# define SC_PRIexitcode "lu"
// <https://stackoverflow.com/a/44383330/1987178>
# define SC_PRIsizet "Iu"
# define SC_PROCESS_NONE NULL
# define SC_EXIT_CODE_NONE -1u // max value as unsigned
# define SC_EXIT_CODE_NONE -1UL // max value as unsigned long
typedef HANDLE sc_pid;
typedef DWORD sc_exit_code;
typedef HANDLE sc_pipe;
@ -23,7 +21,6 @@
#else
# include <sys/types.h>
# define SC_PRIsizet "zu"
# define SC_PRIexitcode "d"
# define SC_PROCESS_NONE -1
# define SC_EXIT_CODE_NONE -1

View File

@ -3,27 +3,33 @@
ssize_t
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
size_t len) {
if (!sc_intr_set_process(intr, pid)) {
if (intr && !sc_intr_set_process(intr, pid)) {
// Already interrupted
return false;
}
ssize_t ret = sc_pipe_read(pipe, data, len);
if (intr) {
sc_intr_set_process(intr, SC_PROCESS_NONE);
}
return ret;
}
ssize_t
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
char *data, size_t len) {
if (!sc_intr_set_process(intr, pid)) {
if (intr && !sc_intr_set_process(intr, pid)) {
// Already interrupted
return false;
}
ssize_t ret = sc_pipe_read_all(pipe, data, len);
if (intr) {
sc_intr_set_process(intr, SC_PROCESS_NONE);
}
return ret;
}

View File

@ -297,14 +297,6 @@ error:
return NULL;
}
size_t
sc_str_truncate(char *data, size_t len, const char *endchars) {
data[len - 1] = '\0';
size_t idx = strcspn(data, endchars);
data[idx] = '\0';
return idx;
}
ssize_t
sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
size_t colidx = 0;

View File

@ -103,17 +103,6 @@ sc_str_from_wchars(const wchar_t *s);
char *
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
/**
* Truncate the data after any of the characters from `endchars`
*
* An '\0' is always written at the end of the data, even if no newline
* character is encountered.
*
* Return the size of the resulting line.
*/
size_t
sc_str_truncate(char *data, size_t len, const char *endchars);
/**
* Find the start of a column in a string
*

View File

@ -54,7 +54,7 @@ sc_mutex_lock(sc_mutex *mutex) {
int r = SDL_LockMutex(mutex->mutex);
#ifndef NDEBUG
if (r) {
LOGC("Could not lock mutex: %s", SDL_GetError());
LOGE("Could not lock mutex: %s", SDL_GetError());
abort();
}
@ -74,7 +74,7 @@ sc_mutex_unlock(sc_mutex *mutex) {
int r = SDL_UnlockMutex(mutex->mutex);
#ifndef NDEBUG
if (r) {
LOGC("Could not lock mutex: %s", SDL_GetError());
LOGE("Could not lock mutex: %s", SDL_GetError());
abort();
}
#else
@ -118,7 +118,7 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
int r = SDL_CondWait(cond->cond, mutex->mutex);
#ifndef NDEBUG
if (r) {
LOGC("Could not wait on condition: %s", SDL_GetError());
LOGE("Could not wait on condition: %s", SDL_GetError());
abort();
}
@ -136,11 +136,13 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
return false; // timeout
}
uint32_t ms = SC_TICK_TO_MS(deadline - now);
// Round up to the next millisecond to guarantee that the deadline is
// reached when returning due to timeout
uint32_t ms = SC_TICK_TO_MS(deadline - now + SC_TICK_FROM_MS(1) - 1);
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
#ifndef NDEBUG
if (r < 0) {
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
LOGE("Could not wait on condition with timeout: %s", SDL_GetError());
abort();
}
@ -148,6 +150,8 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
memory_order_relaxed);
#endif
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
// The deadline is reached on timeout
assert(r != SDL_MUTEX_TIMEDOUT || sc_tick_now() >= deadline);
return r == 0;
}
@ -156,7 +160,7 @@ sc_cond_signal(sc_cond *cond) {
int r = SDL_CondSignal(cond->cond);
#ifndef NDEBUG
if (r) {
LOGC("Could not signal a condition: %s", SDL_GetError());
LOGE("Could not signal a condition: %s", SDL_GetError());
abort();
}
#else
@ -169,7 +173,7 @@ sc_cond_broadcast(sc_cond *cond) {
int r = SDL_CondBroadcast(cond->cond);
#ifndef NDEBUG
if (r) {
LOGC("Could not broadcast a condition: %s", SDL_GetError());
LOGE("Could not broadcast a condition: %s", SDL_GetError());
abort();
}
#else

540
app/src/util/vector.h Normal file
View File

@ -0,0 +1,540 @@
#ifndef SC_VECTOR_H
#define SC_VECTOR_H
#include "common.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
// Adapted from vlc_vector:
// <https://code.videolan.org/videolan/vlc/-/blob/0857947abaed9c89810cd96353aaa1b7e6ba3b0d/include/vlc_vector.h>
/**
* Vector struct body
*
* A vector is a dynamic array, managed by the sc_vector_* helpers.
*
* It is generic over the type of its items, so it is implemented as macros.
*
* To use a vector, a new type must be defined:
*
* struct vec_int SC_VECTOR(int);
*
* The struct may be anonymous:
*
* struct SC_VECTOR(const char *) names;
*
* Vector size is accessible via `vec.size`, and items are intended to be
* accessed directly, via `vec.data[i]`.
*
* Functions and macros having name ending with '_' are private.
*/
#define SC_VECTOR(type) \
{ \
size_t cap; \
size_t size; \
type *data; \
}
/**
* Static initializer for a vector
*/
#define SC_VECTOR_INITIALIZER { 0, 0, NULL }
/**
* Initialize an empty vector
*/
#define sc_vector_init(pv) \
({ \
(pv)->cap = 0; \
(pv)->size = 0; \
(pv)->data = NULL; \
})
/**
* Destroy a vector
*
* The vector may not be used anymore unless sc_vector_init() is called.
*/
#define sc_vector_destroy(pv) \
free((pv)->data)
/**
* Clear a vector
*
* Remove all items from the vector.
*/
#define sc_vector_clear(pv) \
({ \
sc_vector_destroy(pv); \
sc_vector_init(pv);\
})
/**
* The minimal allocation size, in number of items
*
* Private.
*/
#define SC_VECTOR_MINCAP_ ((size_t) 10)
static inline size_t
sc_vector_min_(size_t a, size_t b)
{
return a < b ? a : b;
}
static inline size_t
sc_vector_max_(size_t a, size_t b)
{
return a > b ? a : b;
}
static inline size_t
sc_vector_clamp_(size_t x, size_t min, size_t max)
{
return sc_vector_max_(min, sc_vector_min_(max, x));
}
/**
* Realloc data and update vector fields
*
* On reallocation success, update the vector capacity (*pcap) and size
* (*psize), and return the reallocated data.
*
* On reallocation failure, return NULL without any change.
*
* Private.
*
* \param ptr the current `data` field of the vector to realloc
* \param count the requested capacity, in number of items
* \param size the size of one item
* \param pcap a pointer to the `cap` field of the vector [IN/OUT]
* \param psize a pointer to the `size` field of the vector [IN/OUT]
* \return the new ptr on success, NULL on error
*/
static inline void *
sc_vector_reallocdata_(void *ptr, size_t count, size_t size,
size_t *restrict pcap, size_t *restrict psize)
{
void *p = realloc(ptr, count * size);
if (!p) {
return NULL;
}
*pcap = count;
*psize = sc_vector_min_(*psize, count);
return p;
}
#define sc_vector_realloc_(pv, newcap) \
({ \
void *p = sc_vector_reallocdata_((pv)->data, newcap, sizeof(*(pv)->data), \
&(pv)->cap, &(pv)->size); \
if (p) { \
(pv)->data = p; \
} \
(bool) p; \
});
#define sc_vector_resize_(pv, newcap) \
({ \
bool ok; \
if ((pv)->cap == (newcap)) { \
ok = true; \
} else if ((newcap) > 0) { \
ok = sc_vector_realloc_(pv, (newcap)); \
} else { \
sc_vector_clear(pv); \
ok = true; \
} \
ok; \
})
static inline size_t
sc_vector_growsize_(size_t value)
{
/* integer multiplication by 1.5 */
return value + (value >> 1);
}
/* SIZE_MAX/2 to fit in ssize_t, and so that cap*1.5 does not overflow. */
#define sc_vector_max_cap_(pv) (SIZE_MAX / 2 / sizeof(*(pv)->data))
/**
* Increase the capacity of the vector to at least `mincap`
*
* \param pv a pointer to the vector
* \param mincap (size_t) the requested capacity
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_reserve(pv, mincap) \
({ \
bool ok; \
/* avoid to allocate tiny arrays (< SC_VECTOR_MINCAP_) */ \
size_t mincap_ = sc_vector_max_(mincap, SC_VECTOR_MINCAP_); \
if (mincap_ <= (pv)->cap) { \
/* nothing to do */ \
ok = true; \
} else if (mincap_ <= sc_vector_max_cap_(pv)) { \
/* not too big */ \
size_t newsize = sc_vector_growsize_((pv)->cap); \
newsize = sc_vector_clamp_(newsize, mincap_, sc_vector_max_cap_(pv)); \
ok = sc_vector_realloc_(pv, newsize); \
} else { \
ok = false; \
} \
ok; \
})
#define sc_vector_shrink_to_fit(pv) \
/* decreasing the size may not fail */ \
(void) sc_vector_resize_(pv, (pv)->size)
/**
* Resize the vector down automatically
*
* Shrink only when necessary (in practice when cap > (size+5)*1.5)
*
* \param pv a pointer to the vector
*/
#define sc_vector_autoshrink(pv) \
({ \
bool must_shrink = \
/* do not shrink to tiny size */ \
(pv)->cap > SC_VECTOR_MINCAP_ && \
/* no need to shrink */ \
(pv)->cap >= sc_vector_growsize_((pv)->size + 5); \
if (must_shrink) { \
size_t newsize = sc_vector_max_((pv)->size + 5, SC_VECTOR_MINCAP_); \
sc_vector_resize_(pv, newsize); \
} \
})
#define sc_vector_check_same_ptr_type_(a, b) \
(void) ((a) == (b)) /* warn on type mismatch */
/**
* Push an item at the end of the vector
*
* The amortized complexity is O(1).
*
* \param pv a pointer to the vector
* \param item the item to append
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_push(pv, item) \
({ \
bool ok = sc_vector_reserve(pv, (pv)->size + 1); \
if (ok) { \
(pv)->data[(pv)->size++] = (item); \
} \
ok; \
})
/**
* Append `count` items at the end of the vector
*
* \param pv a pointer to the vector
* \param items the items array to append
* \param count the number of items in the array
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_push_all(pv, items, count) \
sc_vector_push_all_(pv, items, (size_t) count)
#define sc_vector_push_all_(pv, items, count) \
({ \
sc_vector_check_same_ptr_type_((pv)->data, items); \
bool ok = sc_vector_reserve(pv, (pv)->size + (count)); \
if (ok) { \
memcpy(&(pv)->data[(pv)->size], items, (count) * sizeof(*(pv)->data)); \
(pv)->size += count; \
} \
ok; \
})
/**
* Insert an hole of size `count` to the given index
*
* The items in range [index; size-1] will be moved. The items in the hole are
* left uninitialized.
*
* \param pv a pointer to the vector
* \param index the index where the hole is to be inserted
* \param count the number of items in the hole
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_insert_hole(pv, index, count) \
sc_vector_insert_hole_(pv, (size_t) index, (size_t) count);
#define sc_vector_insert_hole_(pv, index, count) \
({ \
bool ok = sc_vector_reserve(pv, (pv)->size + (count)); \
if (ok) { \
if ((index) < (pv)->size) { \
memmove(&(pv)->data[(index) + (count)], \
&(pv)->data[(index)], \
((pv)->size - (index)) * sizeof(*(pv)->data)); \
} \
(pv)->size += count; \
} \
ok; \
})
/**
* Insert an item at the given index
*
* The items in range [index; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index where the item is to be inserted
* \param item the item to append
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_insert(pv, index, item) \
sc_vector_insert_(pv, (size_t) index, (size_t) item);
#define sc_vector_insert_(pv, index, item) \
({ \
bool ok = sc_vector_insert_hole_(pv, index, 1); \
if (ok) { \
(pv)->data[index] = (item); \
} \
ok; \
})
/**
* Insert `count` items at the given index
*
* The items in range [index; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index where the items are to be inserted
* \param items the items array to append
* \param count the number of items in the array
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define sc_vector_insert_all(pv, index, items, count) \
sc_vector_insert_all_(pv, (size_t) index, items, (size_t) count)
#define sc_vector_insert_all_(pv, index, items, count) \
({ \
sc_vector_check_same_ptr_type_((pv)->data, items); \
bool ok = sc_vector_insert_hole_(pv, index, count); \
if (ok) { \
memcpy(&(pv)->data[index], items, count * sizeof(*(pv)->data)); \
} \
ok; \
})
/** Reverse a char array in place */
static inline void
sc_char_array_reverse(char *array, size_t len)
{
for (size_t i = 0; i < len / 2; ++i)
{
char c = array[i];
array[i] = array[len - i - 1];
array[len - i - 1] = c;
}
}
/**
* Right-rotate a (char) array in place
*
* For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
* distance 4 will result in {5, 6, 1, 2, 3, 4}.
*
* Private.
*/
static inline void
sc_char_array_rotate_left(char *array, size_t len, size_t distance)
{
sc_char_array_reverse(array, distance);
sc_char_array_reverse(&array[distance], len - distance);
sc_char_array_reverse(array, len);
}
/**
* Right-rotate a (char) array in place
*
* For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
* distance 2 will result in {5, 6, 1, 2, 3, 4}.
*
* Private.
*/
static inline void
sc_char_array_rotate_right(char *array, size_t len, size_t distance)
{
sc_char_array_rotate_left(array, len, len - distance);
}
/**
* Move items in a (char) array in place
*
* Move slice [index, count] to target.
*/
static inline void
sc_char_array_move(char *array, size_t idx, size_t count, size_t target)
{
if (idx < target) {
sc_char_array_rotate_left(&array[idx], target - idx + count, count);
} else {
sc_char_array_rotate_right(&array[target], idx - target + count, count);
}
}
/**
* Move a slice of items to a given target index
*
* The items in range [index; count] will be moved so that the *new* position
* of the first item is `target`.
*
* \param pv a pointer to the vector
* \param index the index of the first item to move
* \param count the number of items to move
* \param target the new index of the moved slice
*/
#define sc_vector_move_slice(pv, index, count, target) \
sc_vector_move_slice_(pv, (size_t) index, count, (size_t) target);
#define sc_vector_move_slice_(pv, index, count, target) \
({ \
sc_char_array_move((char *) (pv)->data, \
(index) * sizeof(*(pv)->data), \
(count) * sizeof(*(pv)->data), \
(target) * sizeof(*(pv)->data)); \
})
/**
* Move an item to a given target index
*
* The items will be moved so that its *new* position is `target`.
*
* \param pv a pointer to the vector
* \param index the index of the item to move
* \param target the new index of the moved item
*/
#define sc_vector_move(pv, index, target) \
sc_vector_move_slice(pv, index, 1, target)
/**
* Remove a slice of items, without shrinking the array
*
* If you have no good reason to use the _noshrink() version, use
* sc_vector_remove_slice() instead.
*
* The items in range [index+count; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index of the first item to remove
* \param count the number of items to remove
*/
#define sc_vector_remove_slice_noshrink(pv, index, count) \
sc_vector_remove_slice_noshrink_(pv, (size_t) index, (size_t) count)
#define sc_vector_remove_slice_noshrink_(pv, index, count) \
({ \
if ((index) + (count) < (pv)->size) { \
memmove(&(pv)->data[index], \
&(pv)->data[(index) + (count)], \
((pv)->size - (index) - (count)) * sizeof(*(pv)->data)); \
} \
(pv)->size -= count; \
})
/**
* Remove a slice of items
*
* The items in range [index+count; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index of the first item to remove
* \param count the number of items to remove
*/
#define sc_vector_remove_slice(pv, index, count) \
({ \
sc_vector_remove_slice_noshrink(pv, index, count); \
sc_vector_autoshrink(pv); \
})
/**
* Remove an item, without shrinking the array
*
* If you have no good reason to use the _noshrink() version, use
* sc_vector_remove() instead.
*
* The items in range [index+1; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index of item to remove
*/
#define sc_vector_remove_noshrink(pv, index) \
sc_vector_remove_slice_noshrink(pv, index, 1)
/**
* Remove an item
*
* The items in range [index+1; size-1] will be moved.
*
* \param pv a pointer to the vector
* \param index the index of item to remove
*/
#define sc_vector_remove(pv, index) \
({ \
sc_vector_remove_noshrink(pv, index); \
sc_vector_autoshrink(pv); \
})
/**
* Remove an item
*
* The removed item is replaced by the last item of the vector.
*
* This does not preserve ordering, but is O(1). This is useful when the order
* of items is not meaningful.
*
* \param pv a pointer to the vector
* \param index the index of item to remove
*/
#define sc_vector_swap_remove(pv, index) \
sc_vector_swap_remove_(pv, (size_t) index);
#define sc_vector_swap_remove_(pv, index) \
({ \
(pv)->data[index] = (pv)->data[(pv)->size-1]; \
(pv)->size--; \
});
/**
* Return the index of an item
*
* Iterate over all items to find a given item.
*
* Use only for vectors of primitive types or pointers.
*
* Return the index, or -1 if not found.
*
* \param pv a pointer to the vector
* \param item the item to find (compared with ==)
*/
#define sc_vector_index_of(pv, item) \
({ \
ssize_t idx = -1; \
for (size_t i = 0; i < (pv)->size; ++i) { \
if ((pv)->data[i] == (item)) { \
idx = (ssize_t) i; \
break; \
} \
} \
idx; \
})
#endif

View File

@ -274,7 +274,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
LOGD("Starting v4l2 thread");
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "scrcpy-v4l2", vs);
if (!ok) {
LOGC("Could not start v4l2 thread");
LOGE("Could not start v4l2 thread");
goto error_av_packet_free;
}

67
app/src/version.c Normal file
View File

@ -0,0 +1,67 @@
#include "version.h"
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#ifdef HAVE_V4L2
# include <libavdevice/avdevice.h>
#endif
#ifdef HAVE_USB
# include <libusb-1.0/libusb.h>
#endif
void
scrcpy_print_version(void) {
printf("\nDependencies (compiled / linked):\n");
SDL_version sdl;
SDL_GetVersion(&sdl);
printf(" - SDL: %u.%u.%u / %u.%u.%u\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
(unsigned) sdl.major, (unsigned) sdl.minor, (unsigned) sdl.patch);
unsigned avcodec = avcodec_version();
printf(" - libavcodec: %u.%u.%u / %u.%u.%u\n",
LIBAVCODEC_VERSION_MAJOR,
LIBAVCODEC_VERSION_MINOR,
LIBAVCODEC_VERSION_MICRO,
AV_VERSION_MAJOR(avcodec),
AV_VERSION_MINOR(avcodec),
AV_VERSION_MICRO(avcodec));
unsigned avformat = avformat_version();
printf(" - libavformat: %u.%u.%u / %u.%u.%u\n",
LIBAVFORMAT_VERSION_MAJOR,
LIBAVFORMAT_VERSION_MINOR,
LIBAVFORMAT_VERSION_MICRO,
AV_VERSION_MAJOR(avformat),
AV_VERSION_MINOR(avformat),
AV_VERSION_MICRO(avformat));
unsigned avutil = avutil_version();
printf(" - libavutil: %u.%u.%u / %u.%u.%u\n",
LIBAVUTIL_VERSION_MAJOR,
LIBAVUTIL_VERSION_MINOR,
LIBAVUTIL_VERSION_MICRO,
AV_VERSION_MAJOR(avutil),
AV_VERSION_MINOR(avutil),
AV_VERSION_MICRO(avutil));
#ifdef HAVE_V4L2
unsigned avdevice = avdevice_version();
printf(" - libavdevice: %u.%u.%u / %u.%u.%u\n",
LIBAVDEVICE_VERSION_MAJOR,
LIBAVDEVICE_VERSION_MINOR,
LIBAVDEVICE_VERSION_MICRO,
AV_VERSION_MAJOR(avdevice),
AV_VERSION_MINOR(avdevice),
AV_VERSION_MICRO(avdevice));
#endif
#ifdef HAVE_USB
const struct libusb_version *usb = libusb_get_version();
// The compiled version may not be known
printf(" - libusb: - / %u.%u.%u\n",
(unsigned) usb->major, (unsigned) usb->minor, (unsigned) usb->micro);
#endif
}

9
app/src/version.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef SC_VERSION_H
#define SC_VERSION_H
#include "common.h"
void
scrcpy_print_version(void);
#endif

View File

@ -2,13 +2,172 @@
#include <assert.h>
#include "adb_parser.h"
#include "adb/adb_device.h"
#include "adb/adb_parser.h"
static void test_adb_devices() {
char output[] =
"List of devices attached\n"
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice transport_id:1\n"
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
"device:MyWifiDevice trandport_id:2\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 2);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyModel", device->model));
device = &vec.data[1];
assert(!strcmp("192.168.1.1:5555", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyWifiModel", device->model));
sc_adb_devices_destroy(&vec);
}
static void test_adb_devices_cr() {
char output[] =
"List of devices attached\r\n"
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice transport_id:1\r\n"
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
"device:MyWifiDevice trandport_id:2\r\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 2);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyModel", device->model));
device = &vec.data[1];
assert(!strcmp("192.168.1.1:5555", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyWifiModel", device->model));
sc_adb_devices_destroy(&vec);
}
static void test_adb_devices_daemon_start() {
char output[] =
"* daemon not running; starting now at tcp:5037\n"
"* daemon started successfully\n"
"List of devices attached\n"
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice transport_id:1\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 1);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyModel", device->model));
sc_adb_devices_destroy(&vec);
}
static void test_adb_devices_daemon_start_mixed() {
char output[] =
"List of devices attached\n"
"adb server version (41) doesn't match this client (39); killing...\n"
"* daemon started successfully *\n"
"0123456789abcdef unauthorized usb:1-1\n"
"87654321 device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 2);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("unauthorized", device->state));
assert(!device->model);
device = &vec.data[1];
assert(!strcmp("87654321", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyModel", device->model));
sc_adb_devices_destroy(&vec);
}
static void test_adb_devices_without_eol() {
char output[] =
"List of devices attached\n"
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice transport_id:1";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 1);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("device", device->state));
assert(!strcmp("MyModel", device->model));
sc_adb_devices_destroy(&vec);
}
static void test_adb_devices_without_header() {
char output[] =
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
"device:MyDevice transport_id:1\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(!ok);
}
static void test_adb_devices_corrupted() {
char output[] =
"List of devices attached\n"
"corrupted_garbage\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 0);
}
static void test_adb_devices_spaces() {
char output[] =
"List of devices attached\n"
"0123456789abcdef unauthorized usb:1-4 transport_id:3\n";
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
bool ok = sc_adb_parse_devices(output, &vec);
assert(ok);
assert(vec.size == 1);
struct sc_adb_device *device = &vec.data[0];
assert(!strcmp("0123456789abcdef", device->serial));
assert(!strcmp("unauthorized", device->state));
assert(!device->model);
sc_adb_devices_destroy(&vec);
}
static void test_get_ip_single_line() {
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
"192.168.12.34\r\r\n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(ip);
assert(!strcmp(ip, "192.168.12.34"));
free(ip);
@ -18,7 +177,7 @@ static void test_get_ip_single_line_without_eol() {
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
"192.168.12.34";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(ip);
assert(!strcmp(ip, "192.168.12.34"));
free(ip);
@ -28,7 +187,7 @@ static void test_get_ip_single_line_with_trailing_space() {
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
"192.168.12.34 \n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(ip);
assert(!strcmp(ip, "192.168.12.34"));
free(ip);
@ -40,7 +199,7 @@ static void test_get_ip_multiline_first_ok() {
"10.0.0.0/24 dev rmnet proto kernel scope link src "
"10.0.0.2\r\n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(ip);
assert(!strcmp(ip, "192.168.1.2"));
free(ip);
@ -52,7 +211,7 @@ static void test_get_ip_multiline_second_ok() {
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
"192.168.1.3\r\n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(ip);
assert(!strcmp(ip, "192.168.1.3"));
free(ip);
@ -62,7 +221,15 @@ static void test_get_ip_no_wlan() {
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
"192.168.12.34\r\r\n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(!ip);
}
static void test_get_ip_no_wlan_without_eol() {
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
"192.168.12.34";
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(!ip);
}
@ -70,7 +237,7 @@ static void test_get_ip_truncated() {
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
"\n";
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
assert(!ip);
}
@ -78,11 +245,21 @@ int main(int argc, char *argv[]) {
(void) argc;
(void) argv;
test_adb_devices();
test_adb_devices_cr();
test_adb_devices_daemon_start();
test_adb_devices_daemon_start_mixed();
test_adb_devices_without_eol();
test_adb_devices_without_header();
test_adb_devices_corrupted();
test_adb_devices_spaces();
test_get_ip_single_line();
test_get_ip_single_line_without_eol();
test_get_ip_single_line_with_trailing_space();
test_get_ip_multiline_first_ok();
test_get_ip_multiline_second_ok();
test_get_ip_no_wlan();
test_get_ip_no_wlan_without_eol();
test_get_ip_truncated();
}

View File

@ -8,7 +8,7 @@ static void test_buffer_write16be(void) {
uint16_t val = 0xABCD;
uint8_t buf[2];
buffer_write16be(buf, val);
sc_write16be(buf, val);
assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD);
@ -18,7 +18,7 @@ static void test_buffer_write32be(void) {
uint32_t val = 0xABCD1234;
uint8_t buf[4];
buffer_write32be(buf, val);
sc_write32be(buf, val);
assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD);
@ -30,7 +30,7 @@ static void test_buffer_write64be(void) {
uint64_t val = 0xABCD1234567890EF;
uint8_t buf[8];
buffer_write64be(buf, val);
sc_write64be(buf, val);
assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD);
@ -45,7 +45,7 @@ static void test_buffer_write64be(void) {
static void test_buffer_read16be(void) {
uint8_t buf[2] = {0xAB, 0xCD};
uint16_t val = buffer_read16be(buf);
uint16_t val = sc_read16be(buf);
assert(val == 0xABCD);
}
@ -53,7 +53,7 @@ static void test_buffer_read16be(void) {
static void test_buffer_read32be(void) {
uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
uint32_t val = buffer_read32be(buf);
uint32_t val = sc_read32be(buf);
assert(val == 0xABCD1234);
}
@ -62,7 +62,7 @@ static void test_buffer_read64be(void) {
uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
0x56, 0x78, 0x90, 0xEF};
uint64_t val = buffer_read64be(buf);
uint64_t val = sc_read64be(buf);
assert(val == 0xABCD1234567890EF);
}

View File

@ -7,7 +7,7 @@
static void test_serialize_inject_keycode(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_KEYCODE,
.type = SC_CONTROL_MSG_TYPE_INJECT_KEYCODE,
.inject_keycode = {
.action = AKEY_EVENT_ACTION_UP,
.keycode = AKEYCODE_ENTER,
@ -16,12 +16,12 @@ static void test_serialize_inject_keycode(void) {
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 14);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_INJECT_KEYCODE,
SC_CONTROL_MSG_TYPE_INJECT_KEYCODE,
0x01, // AKEY_EVENT_ACTION_UP
0x00, 0x00, 0x00, 0x42, // AKEYCODE_ENTER
0x00, 0x00, 0x00, 0X05, // repeat
@ -32,18 +32,18 @@ static void test_serialize_inject_keycode(void) {
static void test_serialize_inject_text(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_TEXT,
.type = SC_CONTROL_MSG_TYPE_INJECT_TEXT,
.inject_text = {
.text = "hello, world!",
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 18);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_INJECT_TEXT,
SC_CONTROL_MSG_TYPE_INJECT_TEXT,
0x00, 0x00, 0x00, 0x0d, // text length
'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
};
@ -52,30 +52,30 @@ static void test_serialize_inject_text(void) {
static void test_serialize_inject_text_long(void) {
struct sc_control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
char text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH + 1];
memset(text, 'a', CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH] = '\0';
msg.type = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
char text[SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH + 1];
memset(text, 'a', SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
text[SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH] = '\0';
msg.inject_text.text = text;
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 5 + CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
assert(size == 5 + SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
unsigned char expected[5 + CONTROL_MSG_INJECT_TEXT_MAX_LENGTH];
expected[0] = CONTROL_MSG_TYPE_INJECT_TEXT;
unsigned char expected[5 + SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH];
expected[0] = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
expected[1] = 0x00;
expected[2] = 0x00;
expected[3] = 0x01;
expected[4] = 0x2c; // text length (32 bits)
memset(&expected[5], 'a', CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
memset(&expected[5], 'a', SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_inject_touch_event(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
.inject_touch_event = {
.action = AMOTION_EVENT_ACTION_DOWN,
.pointer_id = UINT64_C(0x1234567887654321),
@ -94,12 +94,12 @@ static void test_serialize_inject_touch_event(void) {
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 28);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
0x00, // AKEY_EVENT_ACTION_DOWN
0x12, 0x34, 0x56, 0x78, 0x87, 0x65, 0x43, 0x21, // pointer id
0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0xc8, // 100 200
@ -112,7 +112,7 @@ static void test_serialize_inject_touch_event(void) {
static void test_serialize_inject_scroll_event(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
.type = SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
.inject_scroll_event = {
.position = {
.point = {
@ -130,12 +130,12 @@ static void test_serialize_inject_scroll_event(void) {
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 25);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x04, 0x02, // 260 1026
0x04, 0x38, 0x07, 0x80, // 1080 1920
0x00, 0x00, 0x00, 0x01, // 1
@ -147,18 +147,18 @@ static void test_serialize_inject_scroll_event(void) {
static void test_serialize_back_or_screen_on(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
.type = SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
.back_or_screen_on = {
.action = AKEY_EVENT_ACTION_UP,
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 2);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
0x01, // AKEY_EVENT_ACTION_UP
};
assert(!memcmp(buf, expected, sizeof(expected)));
@ -166,71 +166,71 @@ static void test_serialize_back_or_screen_on(void) {
static void test_serialize_expand_notification_panel(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
.type = SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 1);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
};
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_expand_settings_panel(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
.type = SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 1);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
};
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_collapse_panels(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_COLLAPSE_PANELS,
.type = SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS,
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 1);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_COLLAPSE_PANELS,
SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS,
};
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_get_clipboard(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_GET_CLIPBOARD,
.type = SC_CONTROL_MSG_TYPE_GET_CLIPBOARD,
.get_clipboard = {
.copy_key = GET_CLIPBOARD_COPY_KEY_COPY,
.copy_key = SC_COPY_KEY_COPY,
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 2);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_GET_CLIPBOARD,
GET_CLIPBOARD_COPY_KEY_COPY,
SC_CONTROL_MSG_TYPE_GET_CLIPBOARD,
SC_COPY_KEY_COPY,
};
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_set_clipboard(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_SET_CLIPBOARD,
.type = SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
.set_clipboard = {
.sequence = UINT64_C(0x0102030405060708),
.paste = true,
@ -238,12 +238,12 @@ static void test_serialize_set_clipboard(void) {
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 27);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_SET_CLIPBOARD,
SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
1, // paste
0x00, 0x00, 0x00, 0x0d, // text length
@ -254,7 +254,7 @@ static void test_serialize_set_clipboard(void) {
static void test_serialize_set_clipboard_long(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_SET_CLIPBOARD,
.type = SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
.set_clipboard = {
.sequence = UINT64_C(0x0102030405060708),
.paste = true,
@ -262,60 +262,60 @@ static void test_serialize_set_clipboard_long(void) {
},
};
char text[CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH + 1];
memset(text, 'a', CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
text[CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH] = '\0';
char text[SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH + 1];
memset(text, 'a', SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
text[SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH] = '\0';
msg.set_clipboard.text = text;
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == CONTROL_MSG_MAX_SIZE);
assert(size == SC_CONTROL_MSG_MAX_SIZE);
unsigned char expected[CONTROL_MSG_MAX_SIZE] = {
CONTROL_MSG_TYPE_SET_CLIPBOARD,
unsigned char expected[SC_CONTROL_MSG_MAX_SIZE] = {
SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
1, // paste
// text length
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 24,
(CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 16) & 0xff,
(CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 8) & 0xff,
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH & 0xff,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 24,
(SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 16) & 0xff,
(SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 8) & 0xff,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH & 0xff,
};
memset(expected + 14, 'a', CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
memset(expected + 14, 'a', SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_set_screen_power_mode(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
.type = SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
.set_screen_power_mode = {
.mode = SCREEN_POWER_MODE_NORMAL,
.mode = SC_SCREEN_POWER_MODE_NORMAL,
},
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 2);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
0x02, // SCREEN_POWER_MODE_NORMAL
SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
0x02, // SC_SCREEN_POWER_MODE_NORMAL
};
assert(!memcmp(buf, expected, sizeof(expected)));
}
static void test_serialize_rotate_device(void) {
struct sc_control_msg msg = {
.type = CONTROL_MSG_TYPE_ROTATE_DEVICE,
.type = SC_CONTROL_MSG_TYPE_ROTATE_DEVICE,
};
unsigned char buf[CONTROL_MSG_MAX_SIZE];
unsigned char buf[SC_CONTROL_MSG_MAX_SIZE];
size_t size = sc_control_msg_serialize(&msg, buf);
assert(size == 1);
const unsigned char expected[] = {
CONTROL_MSG_TYPE_ROTATE_DEVICE,
SC_CONTROL_MSG_TYPE_ROTATE_DEVICE,
};
assert(!memcmp(buf, expected, sizeof(expected)));
}

View File

@ -338,32 +338,6 @@ static void test_wrap_lines(void) {
free(formatted);
}
static void test_truncate(void) {
char s[] = "hello\nworld\n!";
size_t len = sc_str_truncate(s, sizeof(s), "\n");
assert(len == 5);
assert(!strcmp("hello", s));
char s2[] = "hello\r\nworkd\r\n!";
len = sc_str_truncate(s2, sizeof(s2), "\n\r");
assert(len == 5);
assert(!strcmp("hello", s));
char s3[] = "hello world\n!";
len = sc_str_truncate(s3, sizeof(s3), " \n\r");
assert(len == 5);
assert(!strcmp("hello", s3));
char s4[] = "hello ";
len = sc_str_truncate(s4, sizeof(s4), " \n\r");
assert(len == 5);
assert(!strcmp("hello", s4));
}
static void test_index_of_column(void) {
assert(sc_str_index_of_column("a bc d", 0, " ") == 0);
assert(sc_str_index_of_column("a bc d", 1, " ") == 2);
@ -417,7 +391,6 @@ int main(int argc, char *argv[]) {
test_parse_integer_with_suffix();
test_strlist_contains();
test_wrap_lines();
test_truncate();
test_index_of_column();
test_remove_trailing_cr();
return 0;

Some files were not shown because too many files have changed in this diff Show More