Commit graph

1204 commits

Author SHA1 Message Date
Romain Vimont
a47848f304 Detect Windows using _WIN32 in network util
For consistency, always use _WIN32 instead of a mix of __WINDOWS__ and
_WIN32.
2022-07-27 14:54:27 +02:00
Romain Vimont
db8c1ce8e1 Fix protocol documentation in comments
Flags were in the correct order in the schema, but their description
were reversed.
2022-07-20 11:41:04 +02:00
Romain Vimont
4aeb78ece2 Add missing allocation failure check 2022-07-19 12:17:02 +02:00
Romain Vimont
396e4bd925 Add missing LOG_OOM() on malloc failure 2022-07-19 12:15:06 +02:00
Romain Vimont
af4b7855e1 Remove unused stream.h
The file was not removed by 7dec225ceb.
2022-06-09 15:02:42 +02:00
Romain Vimont
b1d8c72780 Rename function to simplify
For consistency with sc_adb_parse_device(), do not include "from_output"
in the function name.
2022-06-09 15:02:42 +02:00
Romain Vimont
2f038c834a Revert "Make OTG window resizable"
On Windows and macOS, resizing blocks the event loop. Handling it
properly would require the same workaround as done in screen.c.

This reverts commit 436b368f9d.
2022-04-28 20:45:46 +02:00
Romain Vimont
76b3fcf986 Fix inverted check
SDL_RenderSetLogicalSize() returns 0 on success.

Refs fc8942aa03
2022-04-28 20:45:46 +02:00
Romain Vimont
854a56e588 Enable linear filtering in OTG mode
This improves the icon quality with non-standard window size.

PR #3219 <https://github.com/Genymobile/scrcpy/pull/3219>
2022-04-28 19:18:16 +02:00
Romain Vimont
436b368f9d Make OTG window resizable
PR #3219 <https://github.com/Genymobile/scrcpy/pull/3219>
2022-04-28 19:18:12 +02:00
Romain Vimont
fc8942aa03 Apply requested window size in OTG mode
Fixes #3099 <https://github.com/Genymobile/scrcpy/issues/3099>
PR #3219 <https://github.com/Genymobile/scrcpy/pull/3219>
2022-04-28 19:18:09 +02:00
Romain Vimont
c6d9711109 Create OTG window with HIGHDPI flag
This will avoid poor quality with HiDPI displays.

PR #3219 <https://github.com/Genymobile/scrcpy/pull/3219>
2022-04-28 19:17:49 +02:00
Romain Vimont
0fca2ad830 Add option to not power on on start
By default, on start, the device is powered on. To prevent this
behavior, add a new option --no-power-on.

Fixes #3148 <https://github.com/Genymobile/scrcpy/issues/3148>
PR #3210 <https://github.com/Genymobile/scrcpy/pull/3210>
2022-04-28 19:12:52 +02:00
Romain Vimont
326897a0d4 Add missing mouse shortcuts in --help
Document 4th-click and 5th-click shortcuts.

Fixes #3122 <https://github.com/Genymobile/scrcpy/issues/3122>
2022-04-25 18:35:06 +02:00
Romain Vimont
fa5b2a29e9 Add missing SC_ prefix to header guards 2022-04-12 23:59:01 +02:00
Romain Vimont
0c94887075 Add missing include
Refs c3d45c8397
2022-04-12 23:51:05 +02:00
Romain Vimont
aaf3869a54 Fix OpenGL ES prefix skip 2022-03-30 12:01:01 +02:00
Romain Vimont
c3d45c8397 Consider emulators as TCP/IP devices
Emulators were wrongly considered as USB devices, so they were selected
using -d instead of -e (which was inconsistent with the adb behavior).

Refs #3005 <https://github.com/Genymobile/scrcpy/issues/3005>
Refs #3137 <https://github.com/Genymobile/scrcpy/issues/3137>
2022-03-22 21:08:08 +01:00
Romain Vimont
e56f2ac7a9 Log an error on unexpected device state
Refs #3129 <https://github.com/Genymobile/scrcpy/issues/3129>
2022-03-20 14:56:52 +01:00
Romain Vimont
4ce7af42c6 Use $ANDROID_SERIAL if no selector is specified
Like adb, read the ANDROID_SERIAL environment variable to select a
device by serial if no explicit selection (-s, -d, -e or --tcpip=<addr>)
is provided via the command line.

Fixes #3111 <https://github.com/Genymobile/scrcpy/issues/3111>
PR #3113 <https://github.com/Genymobile/scrcpy/pull/3113>
2022-03-15 08:32:34 +01:00
Romain Vimont
b1dbc30072 Document exit status in --help
Refs #3085 <https://github.com/Genymobile/scrcpy/pull/3085>
2022-03-10 09:13:21 +01:00
martin f. krafft
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
59656fe649 Fix typo in error message 2022-02-24 23:26:12 +01:00
Romain Vimont
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
Romain Vimont
71ef5cc0a9 Add missing include for vector
Include stdlib.h for realloc().
2022-02-22 21:00:43 +01:00
Romain Vimont
79ed83ab68 Reorder --tcpip option in cli
To keep options in alphabetic order.
2022-02-22 18:10:30 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
b4fd882ece Fix typo 2022-02-20 17:21:25 +01:00
Romain Vimont
c4ab65eb79 Remove useless '\n' in log 2022-02-18 21:18:36 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
03705b828b Use sc_prefix for fps counter 2022-02-17 19:55:24 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
5c62f3419d Rename buffer util functions with sc_ prefix 2022-02-12 09:12:46 +01:00
Romain Vimont
044acc2259 Rename HEADER_SIZE to SC_PACKET_HEADER_SIZE
Prefix the constant for consistency.
2022-02-11 21:34:58 +01:00
Romain Vimont
6fc388f369 Remove unused BUFSIZE 2022-02-11 21:34:12 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
29c163959c Indent ifdef for clarity
Make it explicit that the ifdef is an inner block.
2022-02-10 09:24:19 +01:00
Romain Vimont
4a95c08d56 Improve error message for unsupported usb hotplug 2022-02-10 08:54:43 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
f86df817f9 Print libusb version on --version 2022-02-09 10:15:19 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
f20137d2ac Improve USB device open log
For consistency with "List USB devices", log "Open USB device".
2022-02-07 13:10:53 +01:00
Romain Vimont
b60809a4da Inline USB device opening
Such a separate function was useless.
2022-02-07 13:03:23 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
f807131c0a Remove useless undef
The #define was removed in 1c71bd16be.
2022-02-05 10:09:24 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
9e3902f30c Use sc_ prefix for adb 2022-02-04 08:39:10 +01:00
Romain Vimont
7810ca61b0 Move ADB code to adb/ 2022-02-04 08:39:10 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
4ee62abe1d Use sc_ prefix for recorder 2022-02-02 21:03:55 +01:00
Romain Vimont
0080d0b0ff Use sc_ prefix for decoder 2022-02-02 21:03:55 +01:00
CennoxX
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
Romain Vimont
f02f2135cd Fix include for standard library header 2022-02-01 21:40:15 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
22d9f0faf4 Fix comment typo 2022-02-01 21:09:02 +01:00
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
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
Romain Vimont
2762f5d183 Move AOA/HID code to usb/
PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
2022-01-27 23:35:34 +01:00
Romain Vimont
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
Romain Vimont
8ea6fb1f0f Print version on stdout
Refs b25404ee4b
2022-01-27 21:26:56 +01:00
Romain Vimont
b546c33eff Do not print scrcpy version twice on --version
Refs 6da6d905c2
2022-01-27 21:12:46 +01:00
Romain Vimont
4817cadd09 Fix code style
Align function parameters.
2022-01-27 19:02:07 +01:00
Romain Vimont
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
Romain Vimont
8e4d3beb01 Fix return value on adb commands error 2022-01-27 16:47:51 +01:00
Romain Vimont
9d2e00697e Use sc_ prefix for control_msg enums
Refs afa4a1b728
2022-01-27 16:47:51 +01:00
Romain Vimont
f289d206ea Disable more actions if --no-control
If control is disabled, then do not enable "show touches" or
automatically power off the device on close.
2022-01-24 21:45:44 +01:00
Romain Vimont
ca516f4318 Refactor if-block in cli
Several tests must be performed if opts->control is false.
2022-01-24 21:44:28 +01:00
Romain Vimont
5d6076bffd Move misplaced break statements
With ifdefs, the resulting code could contain both a return statement
and a break.
2022-01-24 21:38:30 +01:00
Romain Vimont
e0bce1725b Fix header guard prefix 2022-01-24 21:37:40 +01:00
Romain Vimont
063d103dd6 Capture mouse on start for --hid-mouse
If relative mode is enabled, capture the mouse immediately.
2022-01-23 21:31:53 +01:00
Romain Vimont
4bf9c057fe Extract relative mode check to an inline function
This will allow to reuse the condition in another function.
2022-01-23 21:31:11 +01:00
Romain Vimont
17c97820b2 Never forward capture keys
In relative mode, Alt and Super are "capture keys". Never forward them
to the input manager, to avoid inconsistencies between UP and DOWN
events.
2022-01-23 21:16:40 +01:00
Romain Vimont
8c7f0ed5ea Fix warning message
Make the message consistent for HID keyboard and HID mouse.
2022-01-23 21:16:35 +01:00