Commit Graph

638 Commits

Author SHA1 Message Date
Romain Vimont
790ea5e58c Check screen on for current displayId
Since Android 14, the "screen on" state can be checked per-display.

Refs <956f4084df%5E!/#F17>
PR #5442 <https://github.com/Genymobile/scrcpy/pull/5442>
2024-11-03 19:10:44 +01:00
Romain Vimont
1270997f6b Remove useless assignment
The local variable virtualDisplayId was already initialized to the exact
same value.
2024-11-03 19:02:57 +01:00
Romain Vimont
c905fbba8d Fix indentation 2024-11-03 19:02:37 +01:00
Romain Vimont
f08a6d86c5 Power on the device only for main display
Power on the device on start only if scrcpy is mirroring the main
display.
2024-11-02 18:51:05 +01:00
Romain Vimont
3ac4b64461 Register rotation watcher for non-main displays
While moving code, commit 874eaec487 added
a condition `if (displayId == 0)` to register a rotation watcher,
without good reasons.

This condition was kept when the rotation watcher was moved to a
fallback in e26bdb07a2.

Note: use `git show -b` to show this commit ignoring whitespace changes.

Refs #5428 <https://github.com/Genymobile/scrcpy/issues/5428>
2024-11-02 18:49:08 +01:00
Romain Vimont
c7378f4dc8 Extract setting display power to a separate method
For consistency with the other actions.
2024-10-31 22:49:03 +01:00
Romain Vimont
e26bdb07a2 Listen to display changed events
Replace RotationWatcher and DisplayFoldListener by a single
DisplayListener, which is notified whenever the display size or dpi
changes.

However, the DisplayListener mechanism is broken in the first versions
of Android 14 (it is fixed in android-14.0.0_r29 by commit [1]), so
continue to use the old mechanism specifically for Android 14 (where
DisplayListener may be broken), until we receive the first
"display changed" event (which proves that it works).

[1]: <5653c6b587%5E%21/>

Fixes #161 <https://github.com/Genymobile/scrcpy/issues/161>
Fixes #1918 <https://github.com/Genymobile/scrcpy/issues/1918>
Fixes #4152 <https://github.com/Genymobile/scrcpy/issues/4152>
Fixes #5362 comment <https://github.com/Genymobile/scrcpy/issues/5362#issuecomment-2416219316>
Refs #4469 <https://github.com/Genymobile/scrcpy/pull/4469>
PR #5415 <https://github.com/Genymobile/scrcpy/pull/5415>

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2024-10-31 20:12:23 +01:00
Romain Vimont
04a3e6fb06 Consume reset request on encoding start
If a reset request is pending when a new encoding starts, then it is
implicitly fulfilled.

PR #5415 <https://github.com/Genymobile/scrcpy/pull/5415>
2024-10-31 20:12:23 +01:00
Romain Vimont
d62fa8880e Disable broken options on Android 14
The options --lock-video-orientation and --crop are broken since Android
14. Hopefully, they will be reimplemented differently.

Meanwhile, when running Android >= 14, fail with an error to prevent
incorrect behavior.

Refs #4011 <https://github.com/Genymobile/scrcpy/issues/4011>
Refs #4162 <https://github.com/Genymobile/scrcpy/issues/4162>
PR #5417 <https://github.com/Genymobile/scrcpy/pull/5417>
2024-10-31 19:55:47 +01:00
Romain Vimont
58ba00fa06 Adapt "turn screen off" for Android 15
Android 15 introduced an easy way to set the display power:
<fd8b5efc7f%5E!/#F17>

Refs #3927 <https://github.com/Genymobile/scrcpy/issues/3927>
Refs <https://issuetracker.google.com/issues/303565669>
PR #5418 <https://github.com/Genymobile/scrcpy/pull/5418>
2024-10-30 19:38:35 +01:00
Romain Vimont
569c37cec1 Disable display power for virtual displays
If displayId == Device.DISPLAY_ID_NONE, then the display is virtual: its
power mode cannot be changed.

PR #5418 <https://github.com/Genymobile/scrcpy/pull/5418>
2024-10-30 19:38:07 +01:00
Romain Vimont
58a0fbbf2e Refactor display power mode
Accept a single boolean "on" rather than a "mode" (which, in practice,
could only take 2 values: NORMAL and OFF).

Also rename "screen power mode" to "display power".

PR #5418 <https://github.com/Genymobile/scrcpy/pull/5418>
2024-10-30 19:38:04 +01:00
Romain Vimont
acff5b005c Add more details to --list-encoders output
Add more information about each codec (hw/sw, vendor, alias).

Before:

    [server] INFO: List of video encoders:
        --video-codec=h264 --video-encoder=c2.exynos.h264.encoder
        --video-codec=h264 --video-encoder=c2.android.avc.encoder
        --video-codec=h264 --video-encoder=OMX.google.h264.encoder
        --video-codec=h265 --video-encoder=c2.exynos.hevc.encoder
        --video-codec=h265 --video-encoder=c2.android.hevc.encoder
        --video-codec=av1 --video-encoder=c2.google.av1.encoder
        --video-codec=av1 --video-encoder=c2.android.av1.encoder
    // audio encoders omitted

After:

    [server] INFO: List of video encoders:
        --video-codec=h264 --video-encoder=c2.exynos.h264.encoder         (hw) [vendor]
        --video-codec=h264 --video-encoder=c2.android.avc.encoder         (sw)
        --video-codec=h264 --video-encoder=OMX.google.h264.encoder        (sw) (alias for c2.android.avc.encoder)
        --video-codec=h265 --video-encoder=c2.exynos.hevc.encoder         (hw) [vendor]
        --video-codec=h265 --video-encoder=c2.android.hevc.encoder        (sw)
        --video-codec=av1 --video-encoder=c2.google.av1.encoder           (hw) [vendor]
        --video-codec=av1 --video-encoder=c2.android.av1.encoder          (sw)
    // audio encoders omitted

PR #5416 <https://github.com/Genymobile/scrcpy/pull/5416>
2024-10-30 18:51:00 +01:00
Romain Vimont
5474ae6bd6 Factorize codec info listing
Make the listing of video and audio encoders share the same code.

PR #5416 <https://github.com/Genymobile/scrcpy/pull/5416>
2024-10-30 18:50:47 +01:00
Romain Vimont
566b5be0f6 Add option to start an app by its name
By adding the '?' prefix, the app is searched by its name instead of its
package name (retrieving app names on the device may take some time):

    scrcpy --start-app=?firefox

An app matches if its label starts with the given name,
case-insensitive.

If '+' is also passed to force-stop the app before starting, then the
prefixes must be in that order:

    scrcpy --start-app=+?firefox

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:18:36 +01:00
Romain Vimont
dd20efa41c Add option to force-stop app before starting
The previous commit introduced:

    scrcpy --start-app=name

By adding a '+' prefix, the app is stopped beforehand:

    scrcpy --start-app=+name

This may be useful to start a fresh app on a new virtual display:

    scrcpy --new-display --start-app=+org.mozilla.firefox

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:18:36 +01:00
Romain Vimont
13ce277e1f Add --start-app
Add a command line option --start-app=name to start an Android app by
its package name. For example:

    scrcpy --start-app=org.mozilla.firefox

The app will be started on the correct target display:

    scrcpy --new-display=1920x1080 --start-app=org.videolan.vlc

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

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2024-10-28 18:18:36 +01:00
Romain Vimont
9c9d92fb1c Add --list-apps
Add an option to list all apps installed on the device:

    scrcpy --list-apps

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:18:36 +01:00
Romain Vimont
408a388fc5 Reject --new-display for Android <= 10
Fail explicitly if a new virtual display is requested on an Android
version lower than 10.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:18:36 +01:00
Romain Vimont
98ed5eb643 Add virtual display feature
Add a feature to create a new (separate) virtual display instead of
mirroring the device screen:

    scrcpy --new-display=1920x1080
    scrcpy --new-display=1920x1080/420  # force 420 dpi
    scrcpy --new-display         # use the main display size and density
    scrcpy --new-display -m1920  # scaled to fit a max size of 1920
    scrcpy --new-display=/240    # use the main display size and 240 dpi

Fixes #1887 <https://github.com/Genymobile/scrcpy/issues/1887>
PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
Co-authored-by: anirudhb <anirudhb@users.noreply.github.com>
2024-10-28 18:18:34 +01:00
Romain Vimont
5d0e012a4c Add DPI to DisplayInfo
It will be useful to automatically set an appropriate DPI for new
virtual displays.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
d19396718e Inject display-related events to virtual display
Mouse and touch events must be sent to the virtual display id (used for
mirroring), other events (like key events) must be sent to the original
display id.

Fixes #4598 <https://github.com/Genymobile/scrcpy/issues/4598>
Fixes #5137 <https://github.com/Genymobile/scrcpy/issues/5137>
PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>

Co-authored-by: nightmare <mengyanshou@gmail.com>
2024-10-28 18:13:55 +01:00
Romain Vimont
7024d38199 Send PositionMapper to Controller directly
When a new capture starts, send a new PositionMapper to the Controller
without using the global Device as an intermediate.

Now all Device methods are static.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
f1368d9a8f Introduce PositionMapper
Extract the function that converts coordinates from video space to
display space into a separate component.

It only requires the specific data it uses and does not need a full
ScreenInfo object (although it can be created from a ScreenInfo
instance).

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
d916429566 Move clipboard management to Controller
Continue to declutter the global Device.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
7cfefae5e1 Move implicit displayId to Controller
Remove from Device the functions using an implicit displayId.

Move them to Controller, which knows best which displayId it must use.

This will allow to properly dispatch events either to the origin display
or to the virtual display created for mirroring.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
b60e174780 Add capture prepare() step
Add a function called before each capture starts (before getSize() is
called).

This allows to compute the ScreenInfo instance once exactly when needed.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
5851b62580 Simplify virtual display video size
Do not use an unnecessary intermediate Rect object.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
12d5ca4d5e Move local variables in ScreenCapture
Do not initialize variables when they are not used.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
68e54d9b0b Refactor to call getSize() only once
Avoid to call capture.getSize() (provided by the SurfaceCapture
implementation) twice.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
5f0480c039 Ignore first displayFoldChanged event
An event is posted on registration to signal the initial state.

This had no impact when the listener was registered from Device (before
it was moved to ScreenCapture), because this first initial event was
already triggered when ScreenCapture started listening.

But now, it causes the first encoding to be reset immediately. To avoid
that, ignore the first event.

Refs <https://android.googlesource.com/platform/frameworks/base.git/+/refs/tags/android-15.0.0_r3/services/core/java/com/android/server/policy/DisplayFoldController.java#138>
PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
874eaec487 Move screen-related features out of Device.java
Move the code related to screen size and rotation/fold to ScreenCapture.

For now, keep the ScreenInfo instance in the Device class to communicate
with the Controller, but it will be removed by further commits.

PR #5370 <https://github.com/Genymobile/scrcpy/pull/5370>
2024-10-28 18:13:55 +01:00
Romain Vimont
9578aae34e Use explicit constant for @TargetApi 2024-10-22 19:47:56 +02:00
Romain Vimont
7b3dd595b4 Remove useless version check
Scrcpy requires Android 5.0+, so there is no point testing for older
versions.

Btw, there were two mistakes:
 - the constant name in AndroidVersions should have been
   API_20_ANDROID_4_4W (Android 4.4 without 'W' is API 19)
 - the method isInteractive() was introduced in Android 5.0, not 4.4W:
   <037c33eae7%5E%21/>
2024-10-21 18:51:35 +02:00
Romain Vimont
a46150f753 Upgrade Android SDK to 35
Also adapt the call to build-tools/35.0.0/aidl, which now requires an
import path (-I. for the current directory).

Otherwise, it fails with:

    ERROR: android/view/IRotationWatcher.aidl:23.1-10: directory ./ is not found in any of the import paths:
     -

Also upgrade AGP (8.7.1) and Gradle (8.9), required for SDK 35.

<https://developer.android.com/build/releases/gradle-plugin#updating-gradle>
2024-10-20 15:26:19 +02:00
Romain Vimont
3acffaae57 Use explicit constants for Android versions
Who remembers code names? This avoids to check the mapping every time.
2024-10-20 13:15:22 +02:00
Romain Vimont
c15df01171 Reject non-positive camera sizes early
Throw an exception on parsing if the camera size dimensions are not both
positive.
2024-10-11 22:51:15 +02:00
Romain Vimont
e724ff4349 Simplify IO.writeFully() for Android >= 6
Do not handle buffer properties manually for Android >= 6 (where it is
already handled by Os.write()).

Refs <d9f7e57f5d%5E%21/>
2024-10-01 22:50:34 +02:00
Romain Vimont
79014143b9 Fix IO.writeFully() on Android 5
Os.write() did not update the ByteBuffer position before Android 6.

A workaround was added by commit
b882322f73, which fixed part of the
problem, but the position was still not updated across calls, causing
the wrong chunk to be written.

Refs <d9f7e57f5d%5E%21/>
2024-10-01 22:49:55 +02:00
Romain Vimont
c0a6432967 Extract EINTR handling for Os.write()
Expose a function which retries automatically on EINTR, and throws an
IOException on other errors.
2024-10-01 22:39:06 +02:00
Romain Vimont
ec602a0334 Suggest command line arguments without quotes
Replace argument suggestion:

    --video-encoder='c2.android.avc.encoder'

by:

    --video-encoder=c2.android.avc.encoder

On Linux, the quotes are interpreted by the shell, but on Windows they
are passed as is.

This was harmless, because even transmitted as is, they were interpreted
by the shell on the device. However, special characters are now
validated since commit bec3321fff, making
the command fail.

Fixes #5329 <https://github.com/Genymobile/scrcpy/issues/5329>
2024-09-30 08:16:33 +02:00
Romain Vimont
0cc6f6aa09 Detect codec/encoder mismatch
Fail with an explicit error when the requested encoder does not match
the requested codec.

Refs #5066 <https://github.com/Genymobile/scrcpy/issues/5066>
2024-09-20 08:45:46 +02:00
Romain Vimont
f69ac40534 Reorganize server imports
Moving classes into subpackages changed the expected imports order.

Reorganize them all at once automatically to avoid spurious changes in
future commits.
2024-09-20 08:43:42 +02:00
Romain Vimont
292adf294d Bump version to 2.7 2024-09-15 18:59:27 +02:00
Romain Vimont
7f250dd669 Mention physical gamepad names for UHID devices
Initialize UHID devices with a custom name:
 - "scrcpy: $GAMEPAD_NAME" for gamepads
 - "scrcpy" for keyboard and mouse (or if no gamepad name is available)

The name may appear in Android apps.

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

Can be enabled with --gamepad=uhid or -G.

It is not enabled by default because not all devices support UHID
(there is a permission error on old Android versions).

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

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

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
2024-09-15 11:21:56 +02:00
Romain Vimont
265a15e0b1 Accept float values for --max-fps
Android accepts a float value, there is no reason to limit the option
to be an integer.

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

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

It was already possible to pass a float manually:

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

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

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

Refs #5228 <https://github.com/Genymobile/scrcpy/issues/5228>
2024-09-13 20:03:17 +02:00
Romain Vimont
dea1fe3386 Validate crop and video size
A video width or height of 0 triggered an assert.

Fail explicitly instead: the server may actually send this size in
practice (for example on cropping with small dimensions, even if the
requested crop size is not 0).
2024-09-13 19:48:55 +02:00