Commit graph

952 commits

Author SHA1 Message Date
Romain Vimont
39c5e71605 Make the server unlink itself
To clean up the device, the client executed "adb shell rm" once the
server was guaranteed to be started (after the connection succeeded).

This implied to track whether the installation state, and failed if an
additional tunnel was used in "forward" mode:
<https://github.com/Genymobile/scrcpy/issues/386#issuecomment-453936034>

Instead, make the server unlink itself on start.
2019-01-14 21:12:23 +01:00
Romain Vimont
fefb9816a9 Handle mouse events outside device screen
Mouse events position were unsigned (so negative values could not be
handled properly).

To avoid issues with negative values, mouse events outside the device
screen were ignored (commit a7fe9ad779).

But as a consequence, drag&drop were "broken" if the "drop" occurred
outside the device screen.

Instead, use signed 32-bits to store the position, and forward events
outside the device screen.

Fixes <https://github.com/Genymobile/scrcpy/issues/357>.
2018-11-27 08:59:22 +01:00
Romain Vimont
7830859c21 Merge branch 'master' into dev 2018-11-27 08:38:57 +01:00
Romain Vimont
a7fe9ad779 Ignore mouse events outside device screen
Never create a "struct point" with a position possibly outside the
device screen (i.e. in the black borders area), and do not transmit such
events.

This fixes an assertion failure on mouse wheel events outside the device
screen area.
2018-11-18 21:31:57 +01:00
Romain Vimont
1e22ebcac2 Always use non-empty arguments
The client passes parameters to the server via "adb shell" arguments.

Use "-" instead of "" when no crop is specified to avoid empty
arguments, which are not handled the same way on all devices.

Fixed <https://github.com/Genymobile/scrcpy/issues/337>.
2018-11-16 18:39:41 +01:00
Romain Vimont
2876463d39 Fix read_packet() return value on error or EOF
Fix warning on error or EOF:

> Invalid return value 0 for stream protocol

See <http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=a606f27f4c610708fa96e35eed7b7537d3d8f712>.

Fixes <https://github.com/Genymobile/scrcpy/issues/333>.
2018-11-12 14:45:43 +01:00
Romain Vimont
6dc6ec05d5 Configure version at meson project level
Make meson aware of the project version, so that it does not print:

    Project version: undefined
2018-11-12 14:10:21 +01:00
Pierre Gordon
a17f1116ce Bump version to 1.5
Signed-off-by: Romain Vimont <rom@rom1v.com>
2018-11-12 08:24:18 +01:00
Romain Vimont
22ff03f2f7 Do not queue invalid PTS
Configuration packets produced by MediaCodec have no valid PTS, and do
not produce frame. Do not queue their (invalid) PTS not to break the
matching between frames and their PTS.
2018-11-11 21:37:31 +01:00
Romain Vimont
60afb46c8d Store queue of PTS for pending frames
Several frames may be read by read_packet() before they are consumed
(returned by av_read_frame()), so we need to store the PTS of frames in
order, so that the right PTS is assigned to the right frame.
2018-11-11 21:37:31 +01:00
Romain Vimont
345f8858d3 Send frame meta only if recording is enabled
The client needs the PTS for each frame only if recording is enabled.
Otherwise, the PTS are not necessary, and the protocol is more
straighforward.
2018-11-11 21:37:31 +01:00
Romain Vimont
22bf0c19d6 Rename --output-file to --record
To record the screen to a local file:

    scrcpy --record file.mp4
2018-11-11 21:37:31 +01:00
Romain Vimont
70579dc709 Wrap receiver state into separate struct
For readability, wrap the state of the receiver in a separate struct
receiver_state.
2018-11-11 21:37:31 +01:00
Romain Vimont
e562837c0b Avoid partial header reads
Use net_recv_all() to avoid partial reads for the "meta" header (this
would break the whole stream).
2018-11-11 21:37:31 +01:00
Romain Vimont
ebe998cf78 Move buffer reader functions to buffer_util.h 2018-11-11 21:37:31 +01:00
Romain Vimont
b98eb7d0fa Support AVStream.codec for old FFmpeg versions
AVStream.codec has been deprecated in favor of AVStream.codecpar.

Due to the FFmpeg/Libav split, this happened in two separate versions:
 - 57.33.100 for FFmpeg
 - 57.5.0 for Libav
2018-11-11 21:37:31 +01:00
yuchenlin
e361b49b4a recorder: use av_oformat_next to support older FFmpeg
Signed-off-by: yuchenlin <npes87184@gmail.com>
2018-11-11 21:37:31 +01:00
Romain Vimont
d0e090e1f9 Reenable custom SDL signal handlers
This partially reverts commit f00c6c5b13.

On Ctrl+C, we need to execute cleanup code. For instance, if recording
is enabled, we need to write MP4 file trailer on exit.

Custom SDL signal handlers were disabled because it leaded to process
hanging on Ctrl+C during network calls on initialization, but now it
seems to work correctly, the network calls return immediately on signal.
2018-11-11 21:37:31 +01:00
Romain Vimont
475912a39c Do not transmit MediaCodec flags
Since PTS handling has been fixed, the recorder do not associate a PTS
to a wrong frame anymore, so PTS of "configuration packets" (which never
produce a frame), are never read by the recorder. Therefore, there is no
need to ignore them explicitly, so we can remove the MediaCodec flags
completely.
2018-11-11 21:37:31 +01:00
Romain Vimont
27e8a9a79d Assign PTS to the right frame
The PTS was read from the socket and set as the current one even before
the frame was consumed, so it could be assigned to the previous frame
"in advance".

Store the PTS for the current frame and the last PTS read from the
packet header of the next frame in separate fields.

As a side-effect, this fixes the warning on quit:

> Application provided invalid, non monotonically increasing dts to
> muxer in stream 0: 17164020 >= 17164020
2018-11-11 21:37:31 +01:00
Romain Vimont
61db575861 Decode and push frame before recording
Handle display before recording, to reduce latency.
2018-11-11 21:37:31 +01:00
Romain Vimont
2cd99e7205 Only set valid PTS/DTS
When the PTS is valid, set both PTS and DTS to avoid FFmpeg warnings.

Since configuration packets have no PTS, do not record these packets.
2018-11-11 21:37:26 +01:00
Romain Vimont
27686e9361 Add recorder
Implement recording in a separate "class".
2018-11-11 16:30:23 +01:00
Ivan Gorinov
d706c5df39 Enable video output file, with pts set by server 2018-11-11 16:30:23 +01:00
Romain Vimont
cb3cf801c8 Extract bit operations to buffer_util.h
Move util functions to a reusable separate header.
2018-11-11 01:01:56 +01:00
Romain Vimont
9160d465ec Add feature test macro to declare kill()
Avoid the following warning on some systems:

> warning: implicit declaration of function 'kill'
> [-Wimplicit-function-declaration]
2018-11-10 16:16:08 +01:00
Romain Vimont
5c739874a4 Fix memory leak on error
On decode error, unref the packet.
2018-11-09 16:10:44 +01:00
Romain Vimont
d061c30965 Replace Ctrl by Meta for volume shortcuts on MacOS
Ctrl+UP and Ctrl+DOWN are already used by the window manager on MacOS.

Use Cmd key instead (like on VLC).
2018-11-01 16:19:07 +01:00
Romain Vimont
5bf1261364 Refactor to support Meta in shortcuts
Move the Ctrl and Meta key down checks to each shortcut individually, so
that we can add a shortcut involving Meta.
2018-11-01 16:19:07 +01:00
yuchenlin
96056e3213 input_manager: fix potential memory leak on text
Fix potential memory leak when controller_push_event failed.

Signed-off-by: yuchenlin <npes87184@gmail.com>
2018-10-27 20:07:22 +08:00
Romain Vimont
0b92b93358 Capture Alt and Meta keys
Alt and Meta keys should not be forwarded to the device. For now, they
are not used for shortcuts, but they could be.
2018-10-24 19:08:36 +02:00
Romain Vimont
c20245630e Factorize Windows command building
Extract command line building to a separate method.
2018-10-21 18:57:06 +02:00
Romain Vimont
8875955921 Support paths containing spaces on Windows
Quote the arguments of "adb push" to support paths which contain spaces
on Windows.

Fixes <https://github.com/Genymobile/scrcpy/issues/288>.
2018-10-04 21:01:23 +02:00
Romain Vimont
ff4430b2a3 Declare fun(void) functions with no parameters
This is not C++.
2018-10-04 17:04:20 +02:00
Romain Vimont
24d107d017 Bump version to 1.4 2018-10-03 23:03:27 +02:00
Romain Vimont
411aa4fcfd Handle alpha and space chars as raw events
To handle special chars, text is handled as text input instead of key
events. However, this breaks the separation of DOWN and UP key events.

As a compromise, send letters and space as key events, to preserve
original DOWN/UP events, but send other text input events as text, to be
able to send "special" characters.

Fixes <https://github.com/Genymobile/scrcpy/issues/87>.

Suggested-by: pete1414
Suggested-by: King-Slide <kingslide@gmail.com>
2018-10-03 22:07:09 +02:00
yuchenlin
140b1ef6a5 prevent closing console right after process error in windows
Signed-off-by: yuchenlin <npes87184@gmail.com>
2018-09-14 20:34:59 +08:00
Romain Vimont
eca99d5af7 Fix header guard name 2018-09-13 16:27:19 +02:00
yuchenlin
27bed948d4 Use specific error for missing binary on Windows
Signed-off-by: yuchenlin <npes87184@gmail.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2018-09-09 15:28:25 +02:00
Philipp Sandhaus
af9808cf02 Add option to start in fullscreen
Signed-off-by: Romain Vimont <rom@rom1v.com>
2018-09-04 18:51:32 +02:00
Romain Vimont
55d33ddd5f Do not handle system-specific values in command.c
The common command.c handled process errors from system-specific int
values (errno).

Rather, expose a new enum process_result to handle error cause in a
generic way.
2018-09-04 08:57:07 +02:00
yuchenlin
6d2d803003 Notify adb missing
There are many user who encounters missing adb.
To stop things happens again, we check it and show
sexy response to user.

Signed-off-by: yuchenlin <npes87184@gmail.com>
2018-09-04 08:46:17 +02:00
Romain Vimont
89e0203682 Add missing include for lock_util.h
lock_util.c did not include lock_util.h. This was catched by the gcc
option -Wmissing-prototypes.
2018-08-15 19:30:02 +02:00
Romain Vimont
536b31829a Separate multi-words filenames by '_'
Rename foobar.ext to foo_bar.ext.

<https://github.com/Genymobile/scrcpy/pull/226#discussion_r209454865>
2018-08-15 19:30:01 +02:00
Romain Vimont
6581f9feb9 Make request_queue functions static
These functions are local to file_handler.c.
2018-08-15 17:19:32 +02:00
Romain Vimont
359685b1db Simplify SDL_assert() calls
SDL_assert() already prevents "unused variable" warnings.
2018-08-15 17:19:32 +02:00
Romain Vimont
4527be4cde Add missing include config.h
When config.h is not included, BUILD_DEBUG is not set.
2018-08-15 17:19:32 +02:00
npes87184
66f45f9dae Support drag&drop a file to transfer it to device
Signed-off-by: npes87184 <npes87184@gmail.com>
2018-08-15 17:12:10 +02:00
npes87184
aa97eed24b installer -> file_handler
Signed-off-by: npes87184 <npes87184@gmail.com>
2018-08-15 17:11:41 +02:00
Romain Vimont
2daeb1fd5f Reset current installer process
The current_process field was never reset after an installation is
complete. As a consequence, installer_stop() attempted to terminate it,
leading to a warning, at best.
2018-08-15 17:10:07 +02:00
Grief
cde0b3d248 Return non-zero value on connection loss
Make scrscpy to return 0 exit code only in case when the user closes its
app, otherwise, i.e. in case of connection loss, return 1.
2018-08-15 14:20:45 +02:00
Romain Vimont
6fa209fa82 Remove AINPUT_SOURCE_ANY value
In the Android input header file, an enum has a value taking more than
31 bits, leading to the following warning:

    ISO C restricts enumerator values to range of ‘int’

Since we don't use it, remove it.
2018-08-12 15:35:29 +02:00
Romain Vimont
b37c0f6cd6 Replace Uint32 by int to fix warnings in tinyxpm 2018-08-12 15:35:25 +02:00
npes87184
a3ab92226d Destroy mutex if strdup failed
Signed-off-by: npes87184 <npes87184@gmail.com>
2018-08-12 14:57:36 +02:00
npes87184
f8ef4f1cf7 remove redundant semicolon
Signed-off-by: npes87184 <npes87184@gmail.com>
2018-08-11 21:16:36 +08:00
Romain Vimont
7e42358a7b Bump version to 1.3 2018-08-09 19:14:17 +02:00
Romain Vimont
caa9e30004 Add crop feature
Add an option to crop the screen on the server. This allows to mirror
only part of the device screen.
2018-08-09 19:12:27 +02:00
Romain Vimont
8793c104ee Increase "adb forward" connection attempts
5 seconds might not be sufficient:
<https://github.com/Genymobile/scrcpy/issues/213>

Increase to 10 seconds (it does not harm).
2018-08-09 18:22:42 +02:00
Romain Vimont
fca806e095 Do not call deprecated av_register_all()
av_register_all() is deprecated in FFmpeg since this commit:
<http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=0694d8702421e7aff1340038559c438b61bb30dd>

It is now useless to call it:
<https://ffmpeg.org/pipermail/ffmpeg-devel/2018-February/225051.html>

Fixes <https://github.com/Genymobile/scrcpy/issues/203>.
2018-08-09 18:18:22 +02:00
Romain Vimont
49b2e63d13 Forward repeated volume events
Send repeated events when holding volume up/down shortcuts.
2018-06-24 21:51:54 +02:00
Romain Vimont
c12c64ed41 Send separate DOWN/UP key events
Shortcuts generated instant DOWN/UP key events. Instead, generate DOWN
event on Ctrl+key down and UP event on Ctrl+key up.

Fixes <https://github.com/Genymobile/scrcpy/issues/166>.
2018-06-24 21:50:53 +02:00
Romain Vimont
2f66acd75d Improve English comment
Replace "implying" by "involving" (both "impliquant" in French).
2018-06-24 20:46:14 +02:00
Romain Vimont
1846d2f078 Prevent killing unexpected process
A missing initialization (fixed by the previous commit) leaded to kill
unexpected process.

In order to prevent consequences of similar errors in the future, never
call kill() with a non-positive PID.

See <https://github.com/Genymobile/scrcpy/issues/182>.
2018-06-22 19:56:58 +02:00
Romain Vimont
1a0139321b Fix missing installer initialization
The current_process field of struct installer was not initialized.
Since the installer instance is static, its default value was 0.

The call to installer_stop() then called kill(0, SIGTERM) (on Linux),
which sent SIGTERM to every process in the process group. In particular,
the scrcpy process was killed.

As a consequence, the last cleanup steps, like disabling "show touches",
were not executed.

Fixes <https://github.com/Genymobile/scrcpy/issues/183>.
2018-06-22 18:35:58 +02:00
Romain Vimont
f705a73149 Use a meson option to crossbuild for Windows
Meson decided to crossbuild for Windows as soon as
meson.is_cross_build() returned true. This made non-Windows crossbuilds
fail.

Instead, add an explicit option "crossbuild_windows".

Fixes <https://github.com/Genymobile/scrcpy/issues/165>.
2018-06-05 20:45:41 +02:00
Romain Vimont
8106bb8215 Bump version to 1.2 2018-05-28 22:32:14 +02:00
Romain Vimont
a63dd47f2d Make CreateProcess() flags depend on "noconsole"
On Windows, display the output of external commands (adb) when a console
is available.
2018-05-28 22:28:20 +02:00
Romain Vimont
aedc2c2da9 Also build "noconsole" binary for Windows
On Windows, an application is either console or gui, it cannot be both.

Scrcpy should be both: it outputs important information to console, but
we still want to be able to ignore the console and launch it without a
visible cmd.exe window.

Therefore, build two binaries:
 - scrcpy.exe
 - scrcpy-noconsole.exe
2018-05-28 20:45:54 +02:00
Romain Vimont
106b87a4d2 Add cross-compilation scripts for Windows
Build the Windows binary from mingw on Linux, using the official
prebuilt binaries for ffmpeg, SDL2 and adb.

MSYS2 and all its packaged dll are not necessary anymore.
2018-05-28 20:45:54 +02:00
Romain Vimont
6274cc3767 Document APK drag & drop
Add the drag & drop shortcut for installing an APK file to the README
and the help.
2018-05-28 20:45:54 +02:00
Romain Vimont
9c6f9b24f9 Quote apk path on Windows
Windows will parse the string, so the local name must be quoted.
2018-05-28 20:45:54 +02:00
Adonis Najimi
e2a2973990 Drag and drop to install apk files from computer
<https://github.com/Genymobile/scrcpy/pull/133>
2018-05-28 20:45:54 +02:00
Romain Vimont
41419a2e77 Fix net_send_all() warning
If len is not positive, w could be returned uninitialized.
2018-05-28 20:21:56 +02:00
Romain Vimont
e3d0a59c80 Fix proc_show_touches warning
In practice, proc_show_touches may not be used uninitialized, since it
checks the flag options->show_touches, but the compiler can't know that,
so initialize it to avoid the warning.
2018-05-28 20:21:50 +02:00
Romain Vimont
bb3a7f05ac Release controller lock while processing events
Once the controller took a control event, release the mutex before
processing it, so that the main thread is not blocked to push a new
event.
2018-05-26 15:14:46 +02:00
Romain Vimont
ec02823045 Fix leak on server start error
Fail on SDL_strdup() failure, and free the duplicated serial on further
error.
2018-05-26 14:20:05 +02:00
Romain Vimont
435a7fe314 Add missing includes
Include string.h and stdio.h explicitly.
2018-05-13 15:33:13 +02:00
Romain Vimont
78da66f126 Merge branch 'master' into dev 2018-04-08 12:41:51 +02:00
Romain Vimont
9aa88b6fc3 Map numpad ENTER key
Forward numpad ENTER key to the device.

Fixes <https://github.com/Genymobile/scrcpy/issues/117>.
2018-04-08 12:40:05 +02:00
Romain Vimont
0871bca9c7 Avoid pointer arithmetic on "void *"
Fix the following warning (with -Wpedantic enabled):

    pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
2018-04-04 10:50:12 +02:00
Romain Vimont
b2b5404883 Use const pointers when possible
Sending data only require to read the input buffer, so declare it const.
2018-04-04 10:50:07 +02:00
Romain Vimont
1bddb80b5f Change volume shortcuts
I could not make Ctrl+'+' and Ctrl+'-' work for every keyboard on every
platform.

Instead, use Ctrl+UP and Ctrl+DOWN (like in VLC) to change the volume.

Fixes <https://github.com/Genymobile/scrcpy/issues/103>.
2018-04-03 14:25:11 +02:00
Romain Vimont
71f50fb697 Merge branch 'master' into dev 2018-03-28 10:45:23 +02:00
Romain Vimont
82efff34e8 Factorize texture creation
SDL_CreateTexture() is called both during initialization and on frame
size change.

To avoid inconsistent changes to arguments value, factorize them to a
single function create_texture().
2018-03-27 11:01:40 +02:00
Romain Vimont
860006e082 Forward double-click events
Double-clicks were not sent to the device anymore since the
"double-click on black borders" feature.

When a double click occurs inside the device screen, send the event to
the device normally.

Fixes <https://github.com/Genymobile/scrcpy/issues/97>.
2018-03-26 14:49:10 +02:00
Romain Vimont
57eaf05289 Improve startup time when show_touches is enabled
Enabling "show touches" involves the execution of an adb command, which
takes some time.

In order to parallelize, execute the command as soon as possible, but
reap the process only once everything is initialized.
2018-03-25 16:39:36 +02:00
Romain Vimont
dd2a5c1ecf Disable "show touches" once window is closed
If --show-touches is set, then the option must be disabled on quit.

Since it executes an adb command, it takes some time, so close the
window beforehand so that the close window button does not seem
unresponsive.
2018-03-25 16:03:02 +02:00
Romain Vimont
66ec252893 Add an option to enable "show touches"
Add -t/--show-touches option to show physical touches while scrcpy is
running.

See <https://github.com/Genymobile/scrcpy/issues/96>.
2018-03-25 15:43:27 +02:00
Romain Vimont
b13d25b9f4 Group scrcpy options into a struct
The scrcpy() function accepts as many parameters as there are options.

To simplify, group all options in a separate struct.
2018-03-25 15:16:29 +02:00
Romain Vimont
b449c09442 Merge branch 'master' into dev 2018-03-25 15:16:19 +02:00
Romain Vimont
35298bb0c6 Process the last video frame
On H.264 stream EOF, the eof_reached flag is set, but av_read_frame()
still provides a frame, so check the flag only afterwards.

As a side-effect, it also fixes a memory leak (the very last packet was
not unref).
2018-03-23 14:01:58 +01:00
Romain Vimont
73c332e3e4 Unref last packet on exit 2018-03-23 13:57:32 +01:00
Romain Vimont
29b5c5b8f4 Merge branch 'arich/addShake' into dev (#85)
Add support for CTRL+S to send hardware "shake" to device
2018-03-23 10:13:19 +01:00
Romain Vimont
88ee6bc928 Swap MENU and APP_SWITCH shortcuts
Ctrl+s was mapped to MENU, while Ctrl+m was mapped to APP_SWITCH.

To avoid confusion, swap the shortcuts:
 - Ctrl+m like _M_enu
 - Ctrl+s like _S_witch
2018-03-23 10:10:24 +01:00
Romain Vimont
35e9a64c34 Rename "shake" to "menu"
The action sends AKEYCODE_MENU, so just name it "menu".

See <https://github.com/Genymobile/scrcpy/pull/85>.
2018-03-23 10:07:48 +01:00
Andy Rich
301c52b603 Add support for CTRL+S to send hardware "shake" to device w/readme 2018-03-22 16:15:24 -07:00
Romain Vimont
f00c6c5b13 Disable custom SDL signal handlers
Request SDL not to replace the SIGINT and SIGTERM handlers, so that the
process is immediately terminated on Ctrl+C.

This avoids process hanging on Ctrl+C during network calls on
initialization.

Some of them accepted a timeout, but it was not used since
commit 9b056f5091 anymore.
2018-03-21 21:43:12 +01:00
Romain Vimont
3b3803da0d Remove useless blocks in switch/case
Remove unnecessary additional blocks.
2018-03-21 11:14:15 +01:00
Romain Vimont
f5cf6c1b2c Include source root directory
All headers and sources are in src/. To avoid using relative includes
from subdirectories ("../../"), include the source root directory.
2018-03-20 21:32:41 +01:00
Romain Vimont
c65cb36d3b Increase the number of connection attempts
In "adb forward" mode, it may take a while before the server socket is
listening, so increase the number of connection attempts.

See <https://github.com/Genymobile/scrcpy/issues/5#issuecomment-373718551>.
2018-03-16 14:59:08 +01:00
Romain Vimont
821ec9843c Fix win32 build
The types size_t and ssize_t are defined on Windows (in MSYS2), so there
is no need to typedef SIZE_T and SSIZE_T.

Exit code is "unsigned long" both on Windows 32 and 64 bits.

See <https://github.com/Genymobile/scrcpy/issues/46#issuecomment-373603596>.
2018-03-16 08:58:59 +01:00
Romain Vimont
f16bd88802 Remove useless cast
For consistency with mouse button events handling, directly assign from
Sint32 to Uint16.
2018-03-15 16:30:51 +01:00
Romain Vimont
f3e8834a3c Fix warning message
Mouse "wheel button" is meaningless :)
2018-03-15 16:14:40 +01:00
Romain Vimont
080df5eb5d Fix switch/case code style
For readability and consistency, indent case statatements, and remove
unnecessary additional blocks.
2018-03-15 16:00:40 +01:00
Romain Vimont
d744837f13 Bump version to 1.1 2018-03-14 09:34:00 +01:00
Romain Vimont
f7bc0bd5b5 Merge branch 'dev' into release 2018-03-14 09:33:53 +01:00
Romain Vimont
8a3c6a3ae7 Remove useless argument
Do not pass any data to the event watcher, it is unused.
2018-03-14 09:32:05 +01:00
Romain Vimont
0b1e59186f Workaround continuous resizing on Windows/MacOS
On Windows and MacOS, resizing blocks the event loop, so resizing events
are not triggered:
 - <https://bugzilla.libsdl.org/show_bug.cgi?id=2077>
 - <https://stackoverflow.com/a/40693139/1987178>

As a workaround, register an event watcher to render the screen from
another thread.

Since the whole event loop is blocked during resizing, the screen
content is not refreshed (on Windows and MacOS) until resizing ends.
2018-03-13 22:48:04 +01:00
Romain Vimont
e69f6f710d Disable stdout/stderr buffering on Windows
In MSYS2 on Windows, the output is buffered by default. Disable
buffering to print output immediately.

Note that in cmd.exe, it still prints nothing.
2018-03-13 10:20:09 +01:00
Romain Vimont
b858204786 Remove black borders on double-click
Resize the window to fit the device screen on click on black borders
(same as Ctrl+x).

Suggested-by: Guillaume Roche <groche@genymobile.com>
2018-03-13 08:37:46 +01:00
Romain Vimont
1038bad385 Make it work over tcpip
"adb reverse" currently does not work over tcpip (i.e. on a device
connected by "adb connect"):
<https://issuetracker.google.com/issues/37066218>

To work around the problem, if the call to "adb reverse" fails, then
fallback to "adb forward", and reverse the client/server roles.

Keep the "adb reverse" mode as the default because it does not involve
connection retries: when using "adb forward", the client must try to
connect successively until the server listens.

Due to the tunnel, every connect() will succeed, so the client must
attempt to read() to detect a connection failure. For this purpose, when
using the "adb forward" mode, the server initially writes a dummy byte,
read by the client.

Fixes <https://github.com/Genymobile/scrcpy/issues/5>.
2018-03-12 14:10:32 +01:00
Romain Vimont
2b3ed5bcdb Store serial in server instance
The serial is needed for many server actions, but this is an
implementation detail, so the caller should not have to provide it on
every call.

Instead, store the serial in the server instance on server_start().

This paves the way to implement the "adb forward" fallback properly.
2018-03-12 14:10:32 +01:00
Romain Vimont
9e328ef98b Always use the best render scale quality available
Because why not.

See <https://wiki.libsdl.org/SDL_HINT_RENDER_SCALE_QUALITY>.
2018-03-12 14:10:12 +01:00
Romain Vimont
f9a63ec272 Reverse horizontal scrolling behavior
The SDL mouse wheel event seems inconsistent between horizontal and
vertical scrolling.

> Movements to the left generate negative x values and to the right
> generate positive x values. Movements down (scroll backward) generate
> negative y values and up (scroll forward) generate positive y values.

<https://wiki.libsdl.org/SDL_MouseWheelEvent#Remarks>

Reverse the horizontal.

Fixes <https://github.com/Genymobile/scrcpy/issues/49>.
2018-03-12 09:37:46 +01:00
Romain Vimont
c87d94ee27 Map middle-click to HOME
Middle-click is useless in practice. Use it for HOME.
2018-03-10 00:44:19 +01:00
Romain Vimont
675704c71c Map right-click to BACK if screen is on
Right-click was used to turn the screen on. It did nothing when the
screen was already on.

Instead, in that case, press BACK (like Vysor).

Suggested by: <https://www.reddit.com/r/Android/comments/834zmr/introducing_scrcpy_an_app_to_display_and_control/dvfueft/>
2018-03-10 00:16:29 +01:00
Romain Vimont
9396ea6d42 Fix text input event segfault
The text input control_event was initially designed for mapping
SDL_TextInputEvent, limited to 32 characters.

For simplicity, the copy/paste feature was implemented using the same
control_event: it just sends the text to paste.

However, the pasted text might have a length breaking some assumptions:
 - on the client, the event max-size was smaller than the text
   max-length,
 - on the server, the raw buffer storing the events was smaller than the
   max event size.

Fix these inconsistencies, and encode the length on 2 bytes, to accept
more than 256 characters.

Fixes <https://github.com/Genymobile/scrcpy/issues/10>.
2018-03-09 22:30:10 +01:00
Romain Vimont
f9562f537a Unref the packet on error
Do not leak the packet data on error.
2018-03-08 21:36:04 +01:00
Romain Vimont
a34fbd23e9 Do not leak the packet data
Oops! The content of the packets were never freed.
2018-03-08 20:46:02 +01:00
Romain Vimont
c2ac6fe7bd Upgrade version to 1.0 2018-03-08 09:21:06 +01:00
Romain Vimont
e2a7abcd53 Implement clipboard paste
Paste computer clipboard to the device on Ctrl+v.

The other direction (pasting the device clipboard to the computer) is
not implemented. It would require a communication channel from the
device to the computer, other than the socket used by the video stream.
2018-03-07 18:07:02 +01:00
Romain Vimont
e4d64e8752 Initialize struct field by field
Initializing with braces initializes the other fields to 0, which is not
necessary.
2018-03-07 18:07:02 +01:00
Romain Vimont
fffeedffda Expose High DPI support configuration flag
The High DPI support is enabled by default, so that the renderer use the
full definition of High DPI screens.

However, there are still mouse coordinates problems on some MacOS having
High DPI support (but not all), so expose a way to disable it.
2018-03-07 18:07:02 +01:00
Romain Vimont
82b4acee73 Do not fail on EAGAIN
A call to avcodec_receive_frame() may return AVERROR(EAGAIN) if more
input is required. This is not an error, do not fail.
2018-03-07 18:07:02 +01:00
Romain Vimont
ab780ce26d Avoid useless variables initialization
Initialize variables only when necessary.
2018-03-07 18:07:02 +01:00
Romain Vimont
84ad6633a6 Move the new avcodec implementation before the old
The API to decode the video frames is different depending on the
libavcodec version.

Move the new API usage to the #if-block.
2018-03-07 18:07:01 +01:00
Romain Vimont
1b0cea61a5 Do not use return code for thread run function
The decoder sometimes returned a non-zero value on error, but not on
every path.

Since we never use the value, always return 0 at the end (like in the
controller).
2018-03-07 18:07:01 +01:00
Romain Vimont
42f6341a14 Revert "Enable high dpi support"
Just enabling this flag breaks mouse location values.

This reverts commit 38b56f552e.
2018-03-07 18:06:43 +01:00
Romain Vimont
acd2dc3183 Shutdown sockets before closing
The server socket does not release the port it was listening for if we
just close it: we must also shutdown it.
2018-03-07 18:04:39 +01:00
Romain Vimont
db396f2138 Fix scroll wheel mouse position
SDL_MouseWheelEvent does not provide the mouse location, so we used
SDL_GetMouseState() to retrieve it.

Unfortunately, SDL_GetMouseState() returns a position expressed in the
window coordinate system while the position filled in SDL events are
expressed in the renderer coordinate system. As a consequence, the
scroll was not applied at the right position on the device.

Therefore, convert the coordinate system.

See <https://stackoverflow.com/questions/49111054/how-to-get-mouse-position-on-mouse-wheel-event>.
2018-03-07 18:04:39 +01:00
Romain Vimont
e6feb991db Fix comment typo
Replace "at network level" by "at the network level".
2018-03-07 18:04:38 +01:00
Romain Vimont
e3f5d3b49b Double the default bitrate
Set the default video bitrate to 8Mbps. This greatly increase quality on
fast motion, without negative side effects.
2018-03-07 18:04:38 +01:00
Romain Vimont
38b56f552e Enable high dpi support
Use high DPI if available.

Note that on Mac OS X, setting this flag is not sufficient:

> On Apple's OS X you must set the NSHighResolutionCapable Info.plist
> property to YES, otherwise you will not receive a High DPI OpenGL
> display.

<https://wiki.libsdl.org/SDL_CreateWindow#flags>
2018-03-07 18:04:38 +01:00
Romain Vimont
c78ad6377c Terminate the server on stop
On user request to quit, two kinds of blocking calls must be interrupted
on the server:
 1. the reads from and writes to the socket;
 2. the call to MediaCodec.dequeueOutputBuffer().

The former case was handled by calling shutdown() on the socket from the
client, but the latter was not managed.

There is no easy way to wake this call properly, so just terminate the
process from the client (i.e. send SIGTERM on Linux) instead.
2018-02-28 16:56:20 +01:00
Romain Vimont
221a7d0826 Clean up the server from temporary folder
The server is copied to /data/local/tmp/scrcpy-server.jar and executed
on the device.

As soon as we are connected, we can unlink (rm) it from /data/local/tmp,
to keep the device clean.
2018-02-28 15:27:45 +01:00
Romain Vimont
08d32e3bae Clean up useless const-casts
The function adb_execute() now expects a "const char *const *" parameter
for the adb command, so there is no need to cast.
2018-02-28 15:05:49 +01:00
Romain Vimont
487cb10cf0 Rename ScrCpyServer to Server
The Server class is in package scrcpy, there is no need to repeat the
name in the classname.
2018-02-28 14:57:18 +01:00
Romain Vimont
089378926b Rename SCRCPY_SERVER_JAR to SCRCPY_SERVER_PATH
The server is currently a JAR, but it may ba an APK or a DEX, so the
variable name should not contain the type.

Rename the environment variable, the Meson options and the C
definitions.
2018-02-16 15:36:25 +01:00
Romain Vimont
60f3185185 Reuse server address
Set SO_REUSEADDR flag to server socket, so that bind() does not fail if
we restart scrcpy immediately after we closed the previous one.
2018-02-16 15:36:25 +01:00
Romain Vimont
6db22ef339 Log socket errors
Do not silently ignore close() and shutdown() errors, and use perror()
to get the errno.
2018-02-16 15:03:50 +01:00
Romain Vimont
ad6209f6ff Terminate the device process gracefully
Call shutdown() on the device socket to make the device process finish
its execution quickly and gracefully, without killing it.
2018-02-16 11:47:42 +01:00
Romain Vimont
a791c272bc Send and recv all data when necessary
Expose net_recv_all() and net_send_all(), equivalent of net_recv() and
net_send(), but that waits/retries until the requested length has been
transferred.

Use these new functions where it was (wrongly) assumed that the
requested length had been transferred.
2018-02-16 00:57:48 +01:00
Romain Vimont
d646f95a31 Avoid division by 0
If the frame_size width or height is 0, just return the current size to
avoid calculations involving divison by 0.
2018-02-16 00:57:48 +01:00
Romain Vimont
27b9159b07 Cleanup old code comment 2018-02-16 00:57:48 +01:00
Romain Vimont
cf59d49d25 Detect failure of device information read
If the received data is smaller than the device information size, then
something is wrong.
2018-02-16 00:57:47 +01:00
Romain Vimont
9b056f5091 Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.

For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.

But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.

This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)

Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-16 00:56:58 +01:00
Romain Vimont
bf41e5479b Improve decoder stopped event
The syntax was correct, but less readable, and it unnecessarily zeroed
the fields other than "type".

Create the event properly, from a separate method.
2018-02-15 22:59:04 +01:00
Romain Vimont
518d6d5dcd Prevent new window opening with CreateProcess()
Executing commands (like "adb push") created a new terminal window on
Windows. Avoid it.
2018-02-15 22:42:37 +01:00
Romain Vimont
e8cad790a4 Reword Ctrl+x description
Pressing Ctrl+x resizes the window to remove black borders, "optimal" is
not well-defined.
2018-02-15 14:04:48 +01:00
Romain Vimont
5ebf31d483 Remove useless screen render on initialization
screen_render() should not be called on initialization:
 1. it is useless, since the window is hidden until the first frame;
 2. it writes an empty texture (probably green) to the renderer.
2018-02-15 13:42:31 +01:00
Romain Vimont
d977202224 Add Ctrl+i shortcut to enable/disable FPS counter
Disable FPS counter on start, and use Ctrl+i to enable/disable it.
2018-02-15 12:24:16 +01:00
Romain Vimont
000ced9ba8 Refactor screencontrol to inputmanager
The "screen control" handled user input, which happened to be only
used to control the screen.

The controller and screen were passed to every function. Instead, group
them in a struct input_manager.

The purpose is to add a new shortcut to enable/disable FPS counter. This
feature is not related to "screen control", and will require access to
the "frames" instance.
2018-02-15 12:07:47 +01:00
Romain Vimont
42882702d7 Expose skip_frames as a build option
It can be initially configured by:

    meson builddir -Dskip_frames=false

Or on an existing builddir by:

    mesonconf builddir -Dskip_frames=false
2018-02-15 11:24:18 +01:00
Romain Vimont
38e6682875 Add FPS counter
Remove frame counter from scrcpy.c and add a new FPS counter, logging as
INFO the measured frame rate every second (on new frame).
2018-02-15 11:22:48 +01:00
Romain Vimont
c6c17af840 Do not print usage on command error
On error, a message is printed. If we print usage afterwards, it's easy
to miss it.
2018-02-14 19:01:26 +01:00
Romain Vimont
8697659890 Expose device serial as an optional argument
The device serial was provided as a positional argument:

    scrcpy 0123456789abcdef

Instead, expose it as an optional argument, -s or --serial:

    scrcpy -s 0123456789abcdef

This avoids inconsistency between platforms when the positional
argument is passed before the options (which is undefined).
2018-02-14 18:57:29 +01:00
Romain Vimont
23d92a95b6 Extract argument parsing to specific functions
To avoid a big switch/case, implement the argument parsing logic in
separate static functions.
2018-02-14 18:53:16 +01:00
Romain Vimont
111068d733 Use SDL_bool return to indicate success
For clarity and consistency across the application, return SDL_TRUE
(instead of 0) on success and SDL_FALSE on failure (instead of
non-zero).
2018-02-14 18:44:28 +01:00
Romain Vimont
d3c76c004e Sort parameters by letter
For readability, sort the command-line arguments parsing by letter.
2018-02-14 18:39:46 +01:00
Romain Vimont
0efa9305eb Require Meson 0.37
Older versions of Meson are too limited, and it's simple to install a
newer version ("pip3 install meson").
2018-02-14 14:03:56 +01:00
Romain Vimont
c2127d0819 Replace meson subprojects by subdir
Since Meson 0.44, subproject_dir may not be '.' anymore. This implies we
must move app/ and server/ to a subprojects/ directory, which requires
to also change some gradle files.

Instead, just use subdir(), with options to disable building of the app
or the server.
2018-02-14 14:03:56 +01:00
Romain Vimont
ff94462d8a Refactor build system
The client was built with Meson, the server with Gradle, and were run by
a Makefile.

Add a Meson script for the server (which delegates to Gradle), and a
parent script to build and install both the client and the server to the
system, typically with:

    meson --buildtype release build
    cd build
    ninja
    sudo ninja install

In addition, use a separate Makefile to build a "portable" version of
the application (where the client expects the server to be in the
current directory). Typically:

    make release-portable
    cd dist/scrcpy
    ./scrcpy

This is especially useful for Windows builds, which are not "installed".
2018-02-14 09:23:14 +01:00
Romain Vimont
396df8a9d8 Provide config.h.in for old meson versions
The current meson version is able to generate a config.h from a
configuration data object without any template.

However, older versions of meson require a template, so provide it for
compatibility.
2018-02-13 10:24:02 +01:00
Romain Vimont
3ed80a1fac Define macros wrappers for logs
Use macros to wrap SDL_Log* functions with the "application" category.
2018-02-13 10:10:18 +01:00
Romain Vimont
d45ef1a295 Do not use too recent set_quoted()
Old versions of meson do not support set_quoted(). Replace the call by
the old-fashioned manual quotation.
2018-02-12 17:27:25 +01:00
Romain Vimont
ad41bacb48 Fix "terminate process" on Windows
CloseHandle() does not terminate the process. TerminateProcess() does.
2018-02-12 16:35:23 +01:00
Romain Vimont
6fe65d9f5c Log with category APPLICATION
All our logs should use APPLICATION category. The logs for other
categories are not printed by default under the "critical" level.
2018-02-12 16:09:34 +01:00
Romain Vimont
4dbc450d01 Enable debug logs only for debug builds
In release mode, use the default log priorities.
2018-02-12 14:17:29 +01:00
Romain Vimont
0fce4f95b9 Properly clean up on exit
The SDL clean up does not crash anymore on exit, probably since the
memory corruption caused by calling SDLNet_TCP_Close() too early has
been resolved.
2018-02-09 16:21:10 +01:00
Romain Vimont
eb09fefd43 Timeout the server socket connection
Wait no more than 2 seconds for accepting the connection from the
device, since it blocks the event loop, preventing to react to SIGTERM
(Ctrl+C).
2018-02-09 16:21:10 +01:00
Romain Vimont
90a46b4c45 Improve startup time
On startup, the client has to:
 1. listen on a port
 2. push and start the server to the device
 3. wait for the server to connect (accept)
 4. read device name and size
 5. initialize SDL
 6. initialize the window and renderer
 7. show the window

From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.

Once complete, the client initializes SDL, which takes ~500ms.

These two expensive actions are executed sequentially:

                     HOST              DEVICE
listen on port        |                  |
push/start the server |----------------->|| app_process loads the jar
accept the connection .   ^              ||
                      .   |              ||
                      .   | WASTE        ||
                      .   |  OF          ||
                      .   | TIME         ||
                      .   |              ||
                      .   |              ||
                      .   v              X execution of our java main
connection accepted   |<-----------------| connect to the host
init SDL             ||                  |
                     || ,----------------| send frames
                     || |,---------------|
                     || ||,--------------|
                     || |||,-------------|
                     || ||||,------------|
init window/renderer  | |||||,-----------|
display frames        |<++++++-----------|
(many frames skipped)

The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).

But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):

                     HOST              DEVICE
listen on port        |                  |
push/start the server |----------------->||app_process loads the jar
init SDL             ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
accept the connection .                  ||
                      .                  X execution of our java main
connection accepted   |<-----------------| connect to the host
init window/renderer  |                  |
display frames        |<-----------------| send frames
                      |<-----------------|

In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).

Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 16:19:50 +01:00
Romain Vimont
523097eadf Provide decoder_init()
Expose an initializer so that the caller does not have to guess what
fields must be initialized.
2018-02-09 13:30:49 +01:00
Romain Vimont
4662198261 Do not release TCP sockets while still in use
SDLNet_TCP_Close() not only closes, but also release the resources.

Therefore, we must not close the socket if another thread attempts to
read it.

For that purpose, move socket closing from server_stop() to
server_destroy().
2018-02-09 12:59:36 +01:00
Romain Vimont
fe21d9dfb5 Move frame updating to screen.c
Replace screen_update() by a higher-level screen_update_frame() handling
the whole frame updating, so that scrcpy.c just call it without managing
implementation details.
2018-02-09 11:14:47 +01:00
Romain Vimont
7458d8271e Kill the server immediately on close
Do not wait 100ms anymore to let the server print any exception: we
justly want to ignore them.

Moreover, there is no nanosleep() on Windows, so this solve another
problem.
2018-02-09 11:01:50 +01:00
Romain Vimont
2fdc368c41 Do not try to decode video when EOF is reached
When the video stream socket is closed and read_packey() returns -1,
av_read_frame() still returns 0.

To detect EOF, check the flag eof_reached in the AVIOContext.

This avoids garbage errors on closing.
2018-02-09 09:57:23 +01:00
Romain Vimont
a8aa3d39b7 Send "screen on" command only on mouse down
Avoid to send the command twice, once on mouse down, once on mouse up.
2018-02-09 09:43:58 +01:00
Romain Vimont
127e56780a Fix deadlock on exit if SKIP_FRAMES disabled
On exit, the renderer will not consume frames anymore, so signal the
condition variable to wake up the decoder.
2018-02-09 09:41:05 +01:00
Romain Vimont
629c296207 Move frame swapping logic to frame.c
Expose frames_offer_decoded_frame() and frames_consume_rendered_frame()
so that callers are not exposed to frame swapping (between the decoding
and rendering frames) details.
2018-02-08 20:57:29 +01:00
Romain Vimont
0d7f050389 Unlock mutex on screen update failure
The mutex was not unlocked on all code paths.
2018-02-08 18:39:38 +01:00
Romain Vimont
e8dfb723af Move control-related code to screencontrol.c
Move the code handling user input from scrcpy.c to a separate file,
screencontrol.c.
2018-02-08 18:14:50 +01:00
Romain Vimont
e1749a0c09 Remove the "adb reverse" tunnel immediately
As soon as we accepted a connection, we can remove the "adb reverse"
tunnel.
2018-02-08 17:40:09 +01:00
Romain Vimont
3b06e7d500 Move device-related code to device.c
Move the code to read the initial device info from scrcpy.c to a
separate file, device.c.
2018-02-08 17:40:09 +01:00
Romain Vimont
28c5cc030b Move server-related code to server.c
The file server.c already existed, but exposed a low-level API. Make it
higher-level, so that scrcpy.c does not handle server details directly.
2018-02-08 17:40:09 +01:00
Romain Vimont
6c578b5caa Move screen-related code to screen.c
The file scrcpy.c contains too many different things in addition to the
main logic, so move the screen code to a separate file, screen.c.
2018-02-08 15:43:28 +01:00
Romain Vimont
14c58546a7 Add missing include guards
In practice, these headers are included only once, but it's a good
practice to always use include guards.
2018-02-08 15:26:08 +01:00
Romain Vimont
ffae15e36a Rename control to controller
The struct decoder is defined in decoder.h.

For naming consistency, define the struct controller in controller.h.
2018-02-08 13:26:01 +01:00
Romain Vimont
7b7fd77134 Add missing static
Some functions in scrcpy.c are not used outside the file, so declare
them static.
2018-02-08 09:08:55 +01:00
Romain Vimont
f39de46a39 Add delay before stopping server
Let some time to print any exception trace before killing it.
2018-02-07 15:41:59 +01:00
Romain Vimont
cb1428223f Log user request to quit
Log at debug level user requests to quit.
2018-02-07 15:41:59 +01:00
Romain Vimont
7fe11033cb Include dependencies version
On --version, also print the dependencies version scrcpy has been
compiled against.
2018-02-07 15:41:59 +01:00
Romain Vimont
9f6464acff Expose application version
Expose scrcpy version via -v or --version.
2018-02-07 15:41:59 +01:00
Romain Vimont
8d30d40b79 Make SKIP_FRAMES a compilation flag
The skip_frames flag was a non-configurable runtime flag. Since it is
not exposed to the user, there is no need for a (possible) runtime cost.

For testing purpose, we still want it to be configurable, so make it a
compilation flag.
2018-02-07 15:40:24 +01:00
Romain Vimont
53ff1aa410 Use meson to configure default values
Make meson generate config.h with configured values.
2018-02-07 12:09:10 +01:00
Romain Vimont
cb7e29180f Change the window icon color in debug mode
To highlight the debug/release mode of the running application, use a
different window icon color in debug mode.
2018-02-07 11:48:25 +01:00
Romain Vimont
71c2bfdd22 Parse XPM without SDL_image
We encounter some problems with SDL2_image on MSYS2 (Windows), so
implement our own XPM parsing which does not depend on SDL_image.

The input XPM is considered safe (it's in our source repo), so do not
check XPM format errors. This implies that read_xpm() is not safe to
call on any unsafe input.

Although less straightforward, use SDL_CreateRGBSurfaceFrom() instead of
SDL_CreateRGBSurfaceWithFormatFrom() because it is available with SDL
versions older than 2.0.5.
2018-02-05 19:24:33 +01:00
Romain Vimont
f22d4decca Enable mouse focus clickthrough only if available
The hint SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH appeared in SDL 2.0.5. Ignore
it if the SDL version is older.
2018-02-05 16:15:52 +01:00
Romain Vimont
52c89c7afb Add window icon
Add a bugdroid icon loaded from an XPM.
2018-02-05 14:46:00 +01:00
Romain Vimont
5eb91a4ca7 Fix scrcpy() return value
The scrcpy() function returns a SDL_bool to indicate its success, but
was initialized to 0 (SDL_FALSE) instead of SDL_TRUE.
2018-02-04 12:39:17 +01:00
Romain Vimont
69a359c7f4 Refactor actions calls
The purpose of handle_shortcut() was to group all actions together,
whether they are initiated from a text input event or a keycode event.

However, it did not handle the case where it was initiated from a mouse
event (a right-click must turn the screen on), since the action was
identified by the shortcut char.

Instead, expose one function per action, to be called directly from
where the event is handled.
2018-02-02 15:23:04 +01:00
Romain Vimont
d73dee9833 fixup! Handle all shortcuts in the same function 2018-02-02 15:17:03 +01:00
Romain Vimont
0a70e24e80 Add new shortcuts description in help
Document the new shortcuts in the help (scrcpy --help).
2018-02-02 15:10:44 +01:00
Romain Vimont
a139509f11 Turn screen on on right-click
The right-click is almost useless on Android, so use it to turn the
screen on.

Add a new control event type (command) to request the server to turn the
screen on.
2018-02-02 14:56:03 +01:00
Romain Vimont
deba69d022 Handle all shortcuts in the same function
Shortcuts are sometimes initiated from a keycode event, and sometimes
from a text input event.

Move the handling to a unique function.
2018-02-02 12:01:38 +01:00
Romain Vimont
7f6a565401 Extract shortcut actions to separate functions
To simplify event handling code, move all actions to separate functions.
2018-02-02 11:52:19 +01:00
Romain Vimont
f8ad007a28 Move switch fullscreen logs
Logs were printed by the caller of switch_fullscreen(). Move them inside
the function to simplify event handling code.
2018-02-02 11:48:05 +01:00
Romain Vimont
6fe7b84629 Add shortcuts for physical keys
Add shortcuts for HOME, BACK, APPS, POWER, VOLUME_UP and VOLUME_DOWN.
2018-02-02 11:39:25 +01:00
Romain Vimont
1faf6cfd9d Rename the server scrcpy-server.jar
The server is built as an APK to simplify the build, but in fact this is
a simple jar (it is not even signed).

In order to avoid confusion, rename it to .jar, so that users do not try
to "adb install" it.

Also rename it from "scrcpy" to "scrcpy-server" to distinguish from the
client-side.
2018-02-02 09:37:10 +01:00
Romain Vimont
2172c53b7b Force the server target filename
The server path may be customized using SCRCPY_APK. If its basename is
different from "scrcpy.apk", it will be pushed with a different name,
so the execution would fail.

Therefore, force the push target filename.
2018-02-02 09:34:52 +01:00
Romain Vimont
ca44585f96 Do not send simple mouse move events
Moving the mouse without any button pressed has no effect on Android.
Therefore, do not even send these very frequent events.
2018-02-02 09:26:25 +01:00
Romain Vimont
79b28eb68f Enable mouse focus clickthrough
Consider a click to gain focus as a click on the device.
2018-02-01 20:52:44 +01:00
Romain Vimont
6b546a87ab Add bit-rate command-line option
Add a command-line option (-b/--bit-rate) to customize the video
bit-rate.
2018-02-01 17:11:40 +01:00
Romain Vimont
7fe7bbf58c Check empty string before strtol()
There is no need to call strtol() if the input string is empty.
2018-02-01 17:11:40 +01:00
Romain Vimont
3bc63708b4 Remove useless newlines in SDL_Log*
Trailing new line is not necessary in SDL_Log* methods.
2018-02-01 17:11:40 +01:00
Romain Vimont
2683fa20ed Add debug log for shortcuts
Add a debug log for every succeeded shortcut action.
2018-02-01 17:11:40 +01:00
Romain Vimont
274e1ac9ec Fix rotation bug in fullscreen mode on X11
On rotation, scrcpy resize the window to match the new rotation.
However, in fullscreen mode, setting the window size does not change the
windowed size on X11, so the behavior is incorrect.

To avoid the problem, apply the resize only after fullscreen is
disabled.
2018-02-01 17:11:40 +01:00
Romain Vimont
35a111d56e Add --help
Provide command-line help, with -h/--help option.
2018-02-01 17:11:40 +01:00
Romain Vimont
ee93f3f23a Rename maximum_size to max_size
The long option is --max-size, so for consistency, adapt the code
accordingly.
2018-02-01 17:11:40 +01:00
Romain Vimont
213b721ff9 Use long command-line options
In addition to the short form (e.g. "-p"), add the long form ("--port").
2018-02-01 15:35:20 +01:00
Romain Vimont
60b2f2ca64 Indent switch blocks content
For readability, indent "case" in switch blocks.

Replace:

    switch (x) {
    case 1:
        // ...
    case 2:
        // ...
    case 3: { // a local scope block
        int i = 42;
        // ...
    }
    }

By:

    switch (x) {
        case 1:
            // ...
        case 2:
            // ...
        case 3: { // a local scope block
            int i = 42;
            // ...
        }
    }
2018-02-01 11:44:20 +01:00
Romain Vimont
628f88ab89 Use ADB environment variable
Use the ADB environment variable to provide a custom adb path.
2018-01-31 19:51:43 +01:00
Romain Vimont
b67907e24e Convert server to an Android project
To simplify the device server-side build, use gradle to create an APK,
even if we use it as a simple jar, by running its main() method.
2018-01-30 12:01:36 +01:00
Romain Vimont
89f6a3cfe7 Handle resized video stream
Accept a parameter to limit the video size.

For instance, with "-m 960", the great side of the video will be scaled
down to 960 (if necessary), while the other side will be scaled down so
that the aspect ratio is preserved. Both dimensions must be a multiple
of 8, so black bands might be added, and the mouse positions must be
computed accordingly.
2018-01-29 15:40:33 +01:00
Romain Vimont
879941355d Swap position/point names
A point is a 2D vector. A position represent a point relative to the
screen size.
2018-01-29 14:52:22 +01:00
Romain Vimont
2aa15db210 Add shortcut to resize to ratio 1:1
Make Ctrl+g resize the window to the size of the video (pixel-perfect).
2018-01-24 09:56:42 +01:00
Romain Vimont
ab2c3de9f5 Capture all Ctrl events
For consistency, capture all Ctrl events (not only those we react to).
2018-01-24 09:53:09 +01:00
Romain Vimont
2c35220618 Rename screen -> scrcpy -> main
Rename scrcpy.c to main.c (this file handles the command line parsing),
and screen.c to scrcpy.c (it exposes the entry point scrcpy()).
2018-01-23 16:44:59 +01:00
Romain Vimont
8984c1a7c4 Scale mouse events
The video screen size on the client may differ from the real device
screen size (e.g. the video stream may be scaled down). As a
consequence, mouse events must be scaled to match the real device
coordinates.

For this purpose, make the client send the video screen size along with
the absolute pointer location, and the server scale the location to
match the real device size before injecting mouse events.
2018-01-23 16:44:59 +01:00
Romain Vimont
d7b00a9bab Move server-related functions to server.c
In addition to the functions to start/stop the server, move the
functions to push the jar and enable/disable the "adb reverse" tunnel.
2018-01-23 16:20:03 +01:00
Romain Vimont
7d67696b7e Extract server-related functions
To lighten screen.c, move start_server() and stop_server() to a separate
file.
2018-01-22 11:29:44 +01:00
Romain Vimont
52af91f6b0 Add unit tests for control event serialization
Test serialization of the 4 types of events (keycode, text, mouse,
scroll).
2018-01-19 16:55:39 +01:00
Romain Vimont
90c69d2c79 Use pointers-to-const where relevant
Explicitly declare const a function parameter intended to be read-only.
2018-01-19 15:13:38 +01:00
Romain Vimont
53cd59605a Ignore keycodes generating unwanted events
Ctrl, Alt, Shift and Meta should not be transmitted to the Android
device: they may generate unwanted events. For instance, resizing the
window using Alt+click will generate an Alt event which may open a menu
on the device.

All keycodes that generate a text input must also be excluded, to avoid
the text input to be written twice.
2018-01-19 14:09:15 +01:00
Romain Vimont
66b7a99db6 Fix includes windows-specific source
The header strutil.h is two levels above sys/win/command.c, and
SDL_log.h was missing.
2018-01-19 11:15:51 +01:00
Romain Vimont
f75c404a26 Add tests for strutil
Test our custom string handling functions.
2018-01-18 17:25:13 +01:00
Romain Vimont
cabb102a04 Implement keyboard/mouse control
To control the device from the computer:
 - retrieve mouse and keyboard SDL events;
 - convert them to Android events;
 - serialize them;
 - send them on the same socket used by the video stream (but in the
 opposite direction);
 - deserialize the events on the Android side;
 - inject them using the InputManager.
2018-01-18 17:25:13 +01:00
Romain Vimont
95591d2938 Move platform specific to sys/
Move unix/ and win/ to sys/, so that we can use android/ for android
headers without confusion.
2018-01-04 10:22:09 +01:00
Romain Vimont
a919944372 Use _exit() instead of exit() in child process
exit() should not be called from within a child process, since it would
call functions registered with atexit(), and flush stdio streams. Use
_exit() instead.
2018-01-04 10:22:09 +01:00
Romain Vimont
07b3918129 Do not call SDL_Quit()
It may crash in i965_dri.so when calling SDL_Quit (probably a driver
bug). To avoid a segmentation fault, do not call SDL_Quit().
2017-12-19 14:12:11 +01:00
Romain Vimont
a005df7b37 Always destroy in reverse order
For consistency and safety, always destroy objects in reverse order they
were initialized.
2017-12-19 14:11:55 +01:00
Romain Vimont
de106747b6 Improve main() error handling
Parse the command-line arguments first, and do not ignore avformat
network init failure. At the end, deinit the avformat network.
2017-12-18 11:29:34 +01:00
Romain Vimont
bb8afa9324 Fix comment about data sent over the socket
We now send the device name in addition to the screen dimensions on the
socket. Update the comment accordingly.
2017-12-18 11:27:55 +01:00
Romain Vimont
d972a88c1a Optimize includes
Only include SDL_stdinc.h for SDL_bool, not the whole SDL.h.
2017-12-18 11:07:42 +01:00
Romain Vimont
d5b349f670 Do not inline lockutil functions
This duplicates chars in the final binary.
2017-12-18 11:07:42 +01:00
Romain Vimont
a9b276aa67 Remove useless forward declarations
The required headers are included anyway.
2017-12-15 17:19:43 +01:00
Romain Vimont
b9c9466d65 Handle condition variable failure
Add condition variables function wrappers to handle unexpected failure.
2017-12-15 17:15:55 +01:00
Romain Vimont
c4266e487b Rename (un)lock_mutex to mutex_(un)lock
For consistency, rename lock_mutex and unlock_mutex to mutex_lock and
mutex_unlock.
2017-12-15 17:15:55 +01:00
Romain Vimont
ad667bfa20 Fix function signature
Use void for parameters (this project is in C).
2017-12-15 11:37:29 +01:00
Romain Vimont
37d88b8a6a Use SDL_bool return type instead of int
Many functions returned an int to indicate their success. For clarity,
use SDL_bool instead.
2017-12-15 11:27:54 +01:00
Romain Vimont
2b44052f80 Destroy condition variable on frames destruction
A condition variable was init for the "struct frames", but was never destroyed.
2017-12-15 11:12:02 +01:00
Romain Vimont
39fd6ce518 Send device name on the socket
Make the server send the device name along with the width and height, so
that the client may use it as the window title.
2017-12-13 10:02:07 +01:00
Romain Vimont
54d9148a36 Initial commit
Start a new clean history from here.
2017-12-12 15:25:15 +01:00