Compare commits
121 Commits
faq_dpi_aw
...
pr3177
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c8ebf9abd | ||
|
|
53b1e16f42 | ||
|
|
fc65c6cc4b | ||
|
|
7b7cfc3a3e | ||
|
|
2edc73e4b8 | ||
|
|
adbe7908c6 | ||
|
|
49434da36e | ||
|
|
7deccef1c2 | ||
|
|
977735f916 | ||
|
|
71ef5cc0a9 | ||
|
|
4ab4548769 | ||
|
|
3ce6f8ca91 | ||
|
|
26953784d9 | ||
|
|
2716385887 | ||
|
|
1f951f1a3a | ||
|
|
6a9b2f2c36 | ||
|
|
ff8a69d8ec | ||
|
|
1693797277 | ||
|
|
8610e9a454 | ||
|
|
528275d501 | ||
|
|
78b18b7cee | ||
|
|
90816291d4 | ||
|
|
3e0df6ad05 | ||
|
|
79ed83ab68 | ||
|
|
71b41d846f | ||
|
|
e2e76c5d48 | ||
|
|
4b8cb042c4 | ||
|
|
1790e88278 | ||
|
|
c070723bc8 | ||
|
|
36c75e15b8 | ||
|
|
d9bc5082ab | ||
|
|
73a5311ac6 | ||
|
|
25296ae167 | ||
|
|
3bb24b3926 | ||
|
|
6ee75c0cff | ||
|
|
6b65cd405a | ||
|
|
ff3cb31cb4 | ||
|
|
06243e7c3c | ||
|
|
b9b2879789 | ||
|
|
be1936bb85 | ||
|
|
3ee3f8dc02 | ||
|
|
9db42341e4 | ||
|
|
82a99f69ec | ||
|
|
33202491e1 | ||
|
|
b4fd882ece | ||
|
|
c4ab65eb79 | ||
|
|
6edf50d447 | ||
|
|
4b018be789 | ||
|
|
fa93c8a91b | ||
|
|
03705b828b | ||
|
|
85edba20e7 | ||
|
|
2a872c3865 | ||
|
|
ccbe370cc5 | ||
|
|
bb991f829c | ||
|
|
ca9e1a0514 | ||
|
|
cc27771dd1 | ||
|
|
d0ab8c0e7b | ||
|
|
5c62f3419d | ||
|
|
044acc2259 | ||
|
|
6fc388f369 | ||
|
|
1c02b58412 | ||
|
|
67068e4e3d | ||
|
|
e3c2398aa2 | ||
|
|
29c163959c | ||
|
|
4a95c08d56 | ||
|
|
7848a387c8 | ||
|
|
43ae418752 | ||
|
|
8d583d36e2 | ||
|
|
8498a2e8a6 | ||
|
|
c00a31f1b0 | ||
|
|
f86df817f9 | ||
|
|
9a546ef1af | ||
|
|
9477594f80 | ||
|
|
29828aa330 | ||
|
|
dc5276b0e1 | ||
|
|
582161607e | ||
|
|
146f65d7b2 | ||
|
|
5ed13ef477 | ||
|
|
9c545e8c29 | ||
|
|
0a619dc9ef | ||
|
|
4692d13179 | ||
|
|
02d46b2262 | ||
|
|
4389de1c23 | ||
|
|
85ff70fc95 | ||
|
|
700503df6c | ||
|
|
61969aeb80 | ||
|
|
b88c4aa75e | ||
|
|
8c50342fb2 | ||
|
|
0eadf95a3e | ||
|
|
6df2205cf3 | ||
|
|
61b6324ee9 | ||
|
|
f20137d2ac | ||
|
|
b60809a4da | ||
|
|
b0e04aa327 | ||
|
|
137d2c9791 | ||
|
|
6d41c53b61 | ||
|
|
8d540e83c7 | ||
|
|
2ea12f73db | ||
|
|
5d6bd8f9cd | ||
|
|
5b3ae2cb2f | ||
|
|
08f16a9dde | ||
|
|
386cf7d7ac | ||
|
|
5e2bfccab4 | ||
|
|
ba30ca5c1e | ||
|
|
028e7afe32 | ||
|
|
6ca9825c0f | ||
|
|
f807131c0a | ||
|
|
21106bd70a | ||
|
|
bd3c93ae3d | ||
|
|
9e3902f30c | ||
|
|
7810ca61b0 | ||
|
|
c460243ce2 | ||
|
|
7dec225ceb | ||
|
|
4ee62abe1d | ||
|
|
0080d0b0ff | ||
|
|
0bf7e4ddc4 | ||
|
|
c0a75ca746 | ||
|
|
f02f2135cd | ||
|
|
9b4360b6b8 | ||
|
|
c8d0f5cdeb | ||
|
|
22d9f0faf4 |
18
BUILD.md
18
BUILD.md
@@ -161,7 +161,8 @@ install the required packages:
|
|||||||
```bash
|
```bash
|
||||||
# runtime dependencies
|
# runtime dependencies
|
||||||
pacman -S mingw-w64-x86_64-SDL2 \
|
pacman -S mingw-w64-x86_64-SDL2 \
|
||||||
mingw-w64-x86_64-ffmpeg
|
mingw-w64-x86_64-ffmpeg \
|
||||||
|
mingw-w64-x86_64-libusb
|
||||||
|
|
||||||
# client build dependencies
|
# client build dependencies
|
||||||
pacman -S mingw-w64-x86_64-make \
|
pacman -S mingw-w64-x86_64-make \
|
||||||
@@ -175,7 +176,8 @@ For a 32 bits version, replace `x86_64` by `i686`:
|
|||||||
```bash
|
```bash
|
||||||
# runtime dependencies
|
# runtime dependencies
|
||||||
pacman -S mingw-w64-i686-SDL2 \
|
pacman -S mingw-w64-i686-SDL2 \
|
||||||
mingw-w64-i686-ffmpeg
|
mingw-w64-i686-ffmpeg \
|
||||||
|
mingw-w64-i686-libusb
|
||||||
|
|
||||||
# client build dependencies
|
# client build dependencies
|
||||||
pacman -S mingw-w64-i686-make \
|
pacman -S mingw-w64-i686-make \
|
||||||
@@ -199,7 +201,7 @@ Install the packages with [Homebrew]:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# runtime dependencies
|
# runtime dependencies
|
||||||
brew install sdl2 ffmpeg
|
brew install sdl2 ffmpeg libusb
|
||||||
|
|
||||||
# client build dependencies
|
# client build dependencies
|
||||||
brew install pkg-config meson
|
brew install pkg-config meson
|
||||||
@@ -258,7 +260,7 @@ set ANDROID_SDK_ROOT=%LOCALAPPDATA%\Android\sdk
|
|||||||
Then, build:
|
Then, build:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
meson x --buildtype release --strip -Db_lto=true
|
meson x --buildtype=release --strip -Db_lto=true
|
||||||
ninja -Cx # DO NOT RUN AS ROOT
|
ninja -Cx # DO NOT RUN AS ROOT
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -270,16 +272,16 @@ install` must be run as root)._
|
|||||||
|
|
||||||
#### Option 2: Use prebuilt server
|
#### Option 2: Use prebuilt server
|
||||||
|
|
||||||
- [`scrcpy-server-v1.22`][direct-scrcpy-server]
|
- [`scrcpy-server-v1.23`][direct-scrcpy-server]
|
||||||
_(SHA-256: c05d273eec7533c0e106282e0254cf04e7f5e8f0c2920ca39448865fab2a419b)_
|
_(SHA-256: 2a913fd47478c0b306fca507cb0beb625e49a19ff9fc7ab904e36ef5b9fe7e68)_
|
||||||
|
|
||||||
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.22/scrcpy-server-v1.22
|
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.23/scrcpy-server-v1.23
|
||||||
|
|
||||||
Download the prebuilt server somewhere, and specify its path during the Meson
|
Download the prebuilt server somewhere, and specify its path during the Meson
|
||||||
configuration:
|
configuration:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
meson x --buildtype release --strip -Db_lto=true \
|
meson x --buildtype=release --strip -Db_lto=true \
|
||||||
-Dprebuilt_server=/path/to/scrcpy-server
|
-Dprebuilt_server=/path/to/scrcpy-server
|
||||||
ninja -Cx # DO NOT RUN AS ROOT
|
ninja -Cx # DO NOT RUN AS ROOT
|
||||||
```
|
```
|
||||||
|
|||||||
68
FAQ.md
68
FAQ.md
@@ -4,23 +4,16 @@
|
|||||||
|
|
||||||
Here are the common reported problems and their status.
|
Here are the common reported problems and their status.
|
||||||
|
|
||||||
|
If you encounter any error, the first step is to upgrade to the latest version.
|
||||||
|
|
||||||
|
|
||||||
## `adb` issues
|
## `adb` issues
|
||||||
|
|
||||||
`scrcpy` execute `adb` commands to initialize the connection with the device. If
|
`scrcpy` execute `adb` commands to initialize the connection with the device. If
|
||||||
`adb` fails, then scrcpy will not work.
|
`adb` fails, then scrcpy will not work.
|
||||||
|
|
||||||
In that case, it will print this error:
|
|
||||||
|
|
||||||
> ERROR: "adb get-serialno" returned with value 1
|
|
||||||
|
|
||||||
This is typically not a bug in _scrcpy_, but a problem in your environment.
|
This is typically not a bug in _scrcpy_, but a problem in your environment.
|
||||||
|
|
||||||
To find out the cause, execute:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
adb devices
|
|
||||||
```
|
|
||||||
|
|
||||||
### `adb` not found
|
### `adb` not found
|
||||||
|
|
||||||
@@ -30,13 +23,30 @@ On Windows, the current directory is in your `PATH`, and `adb.exe` is included
|
|||||||
in the release, so it should work out-of-the-box.
|
in the release, so it should work out-of-the-box.
|
||||||
|
|
||||||
|
|
||||||
|
### Device not detected
|
||||||
|
|
||||||
|
> ERROR: Could not find any ADB device
|
||||||
|
|
||||||
|
Check that you correctly enabled [adb debugging][enable-adb].
|
||||||
|
|
||||||
|
Your device must be detected by `adb`:
|
||||||
|
|
||||||
|
```
|
||||||
|
adb devices
|
||||||
|
```
|
||||||
|
|
||||||
|
If your device is not detected, you may need some [drivers] (on Windows). There is a separate [USB driver for Google devices][google-usb-driver].
|
||||||
|
|
||||||
|
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
|
||||||
|
[drivers]: https://developer.android.com/studio/run/oem-usb.html
|
||||||
|
[google-usb-driver]: https://developer.android.com/studio/run/win-usb
|
||||||
|
|
||||||
|
|
||||||
### Device unauthorized
|
### Device unauthorized
|
||||||
|
|
||||||
|
> ERROR: Device is unauthorized:
|
||||||
> error: device unauthorized.
|
> ERROR: --> (usb) 0123456789abcdef unauthorized
|
||||||
> This adb server's $ADB_VENDOR_KEYS is not set
|
> ERROR: A popup should open on the device to request authorization.
|
||||||
> Try 'adb kill-server' if that seems wrong.
|
|
||||||
> Otherwise check for a confirmation dialog on your device.
|
|
||||||
|
|
||||||
When connecting, a popup should open on the device. You must authorize USB
|
When connecting, a popup should open on the device. You must authorize USB
|
||||||
debugging.
|
debugging.
|
||||||
@@ -46,29 +56,27 @@ If it does not open, check [stackoverflow][device-unauthorized].
|
|||||||
[device-unauthorized]: https://stackoverflow.com/questions/23081263/adb-android-device-unauthorized
|
[device-unauthorized]: https://stackoverflow.com/questions/23081263/adb-android-device-unauthorized
|
||||||
|
|
||||||
|
|
||||||
### Device not detected
|
|
||||||
|
|
||||||
> error: no devices/emulators found
|
|
||||||
|
|
||||||
Check that you correctly enabled [adb debugging][enable-adb].
|
|
||||||
|
|
||||||
If your device is not detected, you may need some [drivers] (on Windows). There is a separate [USB driver for Google devices][google-usb-driver].
|
|
||||||
|
|
||||||
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
|
|
||||||
[drivers]: https://developer.android.com/studio/run/oem-usb.html
|
|
||||||
[google-usb-driver]: https://developer.android.com/studio/run/win-usb
|
|
||||||
|
|
||||||
|
|
||||||
### Several devices connected
|
### Several devices connected
|
||||||
|
|
||||||
If several devices are connected, you will encounter this error:
|
If several devices are connected, you will encounter this error:
|
||||||
|
|
||||||
> error: more than one device/emulator
|
ERROR: Multiple (2) ADB devices:
|
||||||
|
ERROR: --> (usb) 0123456789abcdef device Nexus_5
|
||||||
|
ERROR: --> (tcpip) 192.168.1.5:5555 device GM1913
|
||||||
|
ERROR: Select a device via -s (--serial), -d (--select-usb) or -e (--select-tcpip)
|
||||||
|
|
||||||
the identifier of the device you want to mirror must be provided:
|
In that case, you can either provide the identifier of the device you want to
|
||||||
|
mirror:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy -s 01234567890abcdef
|
scrcpy -s 0123456789abcdef
|
||||||
|
```
|
||||||
|
|
||||||
|
Or request the single USB (or TCP/IP) device:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy -d # USB device
|
||||||
|
scrcpy -e # TCP/IP device
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that if your device is connected over TCP/IP, you might get this message:
|
Note that if your device is connected over TCP/IP, you might get this message:
|
||||||
|
|||||||
342
README.it.md
342
README.it.md
@@ -1,23 +1,42 @@
|
|||||||
_Apri il [README](README.md) originale e sempre aggiornato._
|
_Apri il [README](README.md) originale (in inglese) e sempre aggiornato._
|
||||||
|
|
||||||
# scrcpy (v1.19)
|
<img src="app/data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
||||||
|
|
||||||
Questa applicazione fornisce la visualizzazione e il controllo dei dispositivi Android collegati via USB (o [via TCP/IP][article-tcpip]). Non richiede alcun accesso _root_.
|
# scrcpy (v1.23)
|
||||||
|
|
||||||
|
_si pronuncia "**scr**een **c**o**py**"_
|
||||||
|
|
||||||
|
[Leggi in altre lingue](#traduzioni)
|
||||||
|
|
||||||
|
Questa applicazione fornisce la visualizzazione e il controllo di dispositivi Android collegati via USB (o [via TCP/IP](#tcpip-wireless)). Non richiede alcun accesso _root_.
|
||||||
Funziona su _GNU/Linux_, _Windows_ e _macOS_.
|
Funziona su _GNU/Linux_, _Windows_ e _macOS_.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Si concentra su:
|
Si concentra su:
|
||||||
|
|
||||||
- **leggerezza** (nativo, mostra solo lo schermo del dispositivo)
|
- **leggerezza**: nativo, mostra solo lo schermo del dispositivo
|
||||||
- **prestazioni** (30~60fps)
|
- **prestazioni**: 30~120fps, in funzione del dispositivo
|
||||||
- **qualità** (1920×1080 o superiore)
|
- **qualità**: 1920×1080 o superiore
|
||||||
- **bassa latenza** ([35~70ms][lowlatency])
|
- **bassa latenza**: [35~70ms][lowlatency]
|
||||||
- **tempo di avvio basso** (~ 1secondo per visualizzare la prima immagine)
|
- **tempo di avvio basso**: ~ 1secondo per visualizzare la prima immagine
|
||||||
- **non invadenza** (nulla viene lasciato installato sul dispositivo)
|
- **non invadenza**: nulla rimane installato sul dispositivo
|
||||||
|
- **vantaggi per l'utente**: nessun account, nessuna pubblicità, non è richiesta alcuna connessione a internet
|
||||||
|
- **libertà**: software libero e a codice aperto (_free and open source_)
|
||||||
|
|
||||||
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
|
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
|
||||||
|
|
||||||
|
Le sue caratteristiche includono:
|
||||||
|
- [registrazione](#registrazione)
|
||||||
|
- mirroring con [schermo del dispositivo spento](#spegnere-lo-schermo)
|
||||||
|
- [copia-incolla](#copia-incolla) in entrambe le direzioni
|
||||||
|
- [qualità configurabile](#configurazione-di-acquisizione)
|
||||||
|
- schermo del dispositivo [come webcam (V4L2)](#v4l2loopback) (solo per Linux)
|
||||||
|
- [simulazione della tastiera fisica (HID)](#simulazione-della-tastiera-fisica-HID)
|
||||||
|
- [simulazione mouse fisico (HID)](#simulazione-del-mouse-fisico-HID)
|
||||||
|
- [modalità OTG](#otg)
|
||||||
|
- e altro ancora...
|
||||||
|
|
||||||
|
|
||||||
## Requisiti
|
## Requisiti
|
||||||
|
|
||||||
@@ -49,12 +68,18 @@ Compila dai sorgenti: [BUILD] (in inglese) ([procedimento semplificato][BUILD_si
|
|||||||
|
|
||||||
### Linux
|
### Linux
|
||||||
|
|
||||||
Su Debian (_testing_ e _sid_ per ora) e Ubuntu (20.04):
|
Su Debian e Ubuntu:
|
||||||
|
|
||||||
```
|
```
|
||||||
apt install scrcpy
|
apt install scrcpy
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Su Arch Linux:
|
||||||
|
|
||||||
|
```
|
||||||
|
pacman -S scrcpy
|
||||||
|
```
|
||||||
|
|
||||||
È disponibile anche un pacchetto [Snap]: [`scrcpy`][snap-link].
|
È disponibile anche un pacchetto [Snap]: [`scrcpy`][snap-link].
|
||||||
|
|
||||||
[snap-link]: https://snapstats.org/snaps/scrcpy
|
[snap-link]: https://snapstats.org/snaps/scrcpy
|
||||||
@@ -66,10 +91,6 @@ Per Fedora, è disponibile un pacchetto [COPR]: [`scrcpy`][copr-link].
|
|||||||
[COPR]: https://fedoraproject.org/wiki/Category:Copr
|
[COPR]: https://fedoraproject.org/wiki/Category:Copr
|
||||||
[copr-link]: https://copr.fedorainfracloud.org/coprs/zeno/scrcpy/
|
[copr-link]: https://copr.fedorainfracloud.org/coprs/zeno/scrcpy/
|
||||||
|
|
||||||
Per Arch Linux, è disponibile un pacchetto [AUR]: [`scrcpy`][aur-link].
|
|
||||||
|
|
||||||
[AUR]: https://wiki.archlinux.org/index.php/Arch_User_Repository
|
|
||||||
[aur-link]: https://aur.archlinux.org/packages/scrcpy/
|
|
||||||
|
|
||||||
Per Gentoo, è disponibile una [Ebuild]: [`scrcpy/`][ebuild-link].
|
Per Gentoo, è disponibile una [Ebuild]: [`scrcpy/`][ebuild-link].
|
||||||
|
|
||||||
@@ -142,7 +163,7 @@ Collega un dispositivo Android ed esegui:
|
|||||||
scrcpy
|
scrcpy
|
||||||
```
|
```
|
||||||
|
|
||||||
Scrcpy accetta argomenti da riga di comando, essi sono listati con:
|
Scrcpy accetta argomenti da riga di comando, elencati con:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --help
|
scrcpy --help
|
||||||
@@ -186,6 +207,14 @@ scrcpy --max-fps 15
|
|||||||
|
|
||||||
Questo è supportato ufficialmente a partire da Android 10, ma potrebbe funzionare in versioni precedenti.
|
Questo è supportato ufficialmente a partire da Android 10, ma potrebbe funzionare in versioni precedenti.
|
||||||
|
|
||||||
|
L'attuale frame rate di acquisizione può essere stampato sulla console:
|
||||||
|
|
||||||
|
```
|
||||||
|
scrcpy --print-fps
|
||||||
|
```
|
||||||
|
|
||||||
|
Può anche essere abilitato o disabilitato in qualsiasi momento con <kbd>MOD</kbd>+<kbd>i</kbd>.
|
||||||
|
|
||||||
#### Ritaglio
|
#### Ritaglio
|
||||||
|
|
||||||
Lo schermo del dispositivo può essere ritagliato per visualizzare solo parte di esso.
|
Lo schermo del dispositivo può essere ritagliato per visualizzare solo parte di esso.
|
||||||
@@ -258,7 +287,7 @@ I "fotogrammi saltati" sono registrati nonostante non siano mostrati in tempo re
|
|||||||
|
|
||||||
#### v4l2loopback
|
#### v4l2loopback
|
||||||
|
|
||||||
Su Linux è possibile inviare il flusso video ad un dispositivo v4l2 loopback, cosicchè un dispositivo Android possa essere aperto come una webcam da qualsiasi strumento compatibile con v4l2.
|
Su Linux è possibile inviare il flusso video ad un dispositivo v4l2 loopback, cosicché un dispositivo Android possa essere aperto come una webcam da qualsiasi strumento compatibile con v4l2.
|
||||||
|
|
||||||
Il modulo `v4l2loopback` deve essere installato:
|
Il modulo `v4l2loopback` deve essere installato:
|
||||||
|
|
||||||
@@ -321,42 +350,72 @@ scrcpy --display-buffer=50 # aggiungi 50 ms di buffer per la visualizzazione
|
|||||||
e per il V4L2 sink:
|
e per il V4L2 sink:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --v4l2-buffer=500 # aggiungi 50 ms di buffer per il v4l2 sink
|
scrcpy --v4l2-buffer=500 # aggiungi 500 ms di buffer per il v4l2 sink
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Connessione
|
### Connessione
|
||||||
|
|
||||||
#### Wireless
|
#### TCP/IP (wireless)
|
||||||
|
|
||||||
|
_Scrcpy_ usa `adb` per comunicare col dispositivo e `adb` può [connettersi][connect] a un dispositivo mediante TCP/IP. Il dispositivo deve essere collegato alla stessa rete del computer.
|
||||||
|
|
||||||
_Scrcpy_ usa `adb` per comunicare col dispositivo e `adb` può [connettersi][connect] al dispositivo mediante TCP/IP:
|
##### Automatico
|
||||||
|
|
||||||
1. Connetti il dispositivo alla stessa rete Wi-Fi del tuo computer.
|
Un'opzione `--tcpip` permette di configurare automaticamente la connessione. Ci sono due varianti.
|
||||||
2. Trova l'indirizzo IP del tuo dispositivo in Impostazioni → Informazioni sul telefono → Stato, oppure eseguendo questo comando:
|
|
||||||
|
Se il dispositivo (accessibile a 192.168.1.1 in questo esempio) ascolta già su una porta (tipicamente 5555) per le connessioni adb in entrata, allora esegui:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --tcpip=192.168.1.1 # la porta predefinita è 5555
|
||||||
|
scrcpy --tcpip=192.168.1.1:5555
|
||||||
|
```
|
||||||
|
|
||||||
|
Se la modalità TCP/IP di adb è disabilitata sul dispositivo (o se non si conosce l'indirizzo IP indirizzo), collegare il dispositivo tramite USB, quindi eseguire:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --tcpip # senza argomenti
|
||||||
|
```
|
||||||
|
|
||||||
|
Il comando troverà automaticamente l'indirizzo IP del dispositivo, abiliterà la modalità TCP/IP, quindi connettersi al dispositivo prima di iniziare.
|
||||||
|
|
||||||
|
##### Manuale
|
||||||
|
|
||||||
|
In alternativa, è possibile abilitare la connessione TCP/IP manualmente usando `adb`:
|
||||||
|
|
||||||
|
1. Inserisci il dispositivo in una porta USB del tuo computer.
|
||||||
|
2. Connetti il dispositivo alla stessa rete Wi-Fi del tuo computer.
|
||||||
|
3. Ottieni l'indirizzo IP del tuo dispositivo, in Impostazioni → Informazioni sul telefono → Stato, o
|
||||||
|
eseguendo questo comando:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb shell ip route | awk '{print $9}'
|
adb shell ip route | awk '{print $9}'
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Abilita adb via TCP/IP sul tuo dispositivo: `adb tcpip 5555`.
|
4. Abilita adb via TCP/IP sul tuo dispositivo: `adb tcpip 5555`.
|
||||||
4. Scollega il tuo dispositivo.
|
5. Scollega il tuo dispositivo.
|
||||||
5. Connetti il tuo dispositivo: `adb connect IP_DISPOSITVO:5555` _(rimpiazza `IP_DISPOSITIVO`)_.
|
6. Connettiti al tuo dispositivo: `adb connect DEVICE_IP:5555` _(sostituisci `DEVICE_IP`
|
||||||
6. Esegui `scrcpy` come al solito.
|
con l'indirizzo IP del dispositivo che hai trovato)_.
|
||||||
|
7. Esegui `scrcpy` come al solito.
|
||||||
|
|
||||||
Potrebbe essere utile diminuire il bit-rate e la definizione
|
Da Android 11, una [opzione di debug wireless][adb-wireless] permette di evitare di dover collegare fisicamente il dispositivo direttamente al computer.
|
||||||
|
|
||||||
|
[adb-wireless]: https://developer.android.com/studio/command-line/adb#connect-to-a-device-over-wi-fi-android-11+
|
||||||
|
|
||||||
|
Se la connessione cade casualmente, esegui il comando `scrcpy` per riconnetterti. Se il comando dice che non ci sono dispositivi/emulatori trovati, prova ad eseguire `adb connect DEVICE_IP:5555` di nuovo, e poi `scrcpy` come al solito. Se dice ancora che non ne ha trovato nessuno, prova ad eseguire `adb disconnect` e poi esegui di nuovo questi due comandi.
|
||||||
|
|
||||||
|
Potrebbe essere utile diminuire il bit-rate e la definizione:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --bit-rate 2M --max-size 800
|
scrcpy --bit-rate 2M --max-size 800
|
||||||
scrcpy -b2M -m800 # versione breve
|
scrcpy -b2M -m800 # versione breve
|
||||||
```
|
```
|
||||||
|
|
||||||
[connect]: https://developer.android.com/studio/command-line/adb.html#wireless
|
[connect]: https://developer.android.com/studio/command-line/adb.html#wireless
|
||||||
|
|
||||||
|
|
||||||
#### Multi dispositivo
|
#### Multi dispositivo
|
||||||
|
|
||||||
Se in `adb devices` sono listati più dispositivi, è necessario specificare il _seriale_:
|
Se in `adb devices` sono elencati più dispositivi, è necessario specificare il _seriale_:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --serial 0123456789abcdef
|
scrcpy --serial 0123456789abcdef
|
||||||
@@ -370,6 +429,18 @@ scrcpy --serial 192.168.0.1:5555
|
|||||||
scrcpy -s 192.168.0.1:5555 # versione breve
|
scrcpy -s 192.168.0.1:5555 # versione breve
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Se solo un dispositivo è collegato via USB o TCP/IP, è possibile selezionarlo automaticamente:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Select the only device connected via USB
|
||||||
|
scrcpy -d # like adb -d
|
||||||
|
scrcpy --select-usb # long version
|
||||||
|
|
||||||
|
# Select the only device connected via TCP/IP
|
||||||
|
scrcpy -e # like adb -e
|
||||||
|
scrcpy --select-tcpip # long version
|
||||||
|
```
|
||||||
|
|
||||||
Puoi avviare più istanze di _scrcpy_ per diversi dispositivi.
|
Puoi avviare più istanze di _scrcpy_ per diversi dispositivi.
|
||||||
|
|
||||||
|
|
||||||
@@ -383,37 +454,77 @@ autoadb scrcpy -s '{}'
|
|||||||
|
|
||||||
[AutoAdb]: https://github.com/rom1v/autoadb
|
[AutoAdb]: https://github.com/rom1v/autoadb
|
||||||
|
|
||||||
#### Tunnel SSH
|
#### Tunnels
|
||||||
|
|
||||||
Per connettersi a un dispositivo remoto è possibile collegare un client `adb` locale ad un server `adb` remoto (assunto che entrambi stiano usando la stessa versione del protocollo _adb_):
|
Per connettersi a un dispositivo remoto, è possibile collegare un client `adb` locale a un server remoto `adb` (purché usino la stessa versione del protocollo _adb_). ).
|
||||||
|
|
||||||
|
##### Server ADB remoto
|
||||||
|
|
||||||
|
Per connettersi a un server ADB remoto, fate ascoltare il server su tutte le interfacce:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb kill-server # termina il server adb locale su 5037
|
adb kill-server
|
||||||
ssh -CN -L5037:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
adb -a nodaemon server start
|
||||||
# tieni questo aperto
|
# tienilo aperto
|
||||||
```
|
```
|
||||||
|
|
||||||
Da un altro terminale:
|
**Attenzione: tutte le comunicazioni tra i client e il server ADB non sono criptate.**
|
||||||
|
|
||||||
|
Supponi che questo server sia accessibile a 192.168.1.2. Poi, da un altro terminale, esegui scrcpy:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
|
||||||
|
scrcpy --tunnel-host=192.168.1.2
|
||||||
|
```
|
||||||
|
|
||||||
|
Per impostazione predefinita, scrcpy utilizza la porta locale utilizzata per il tunnel `adb forward` (tipicamente `27183`, vedi `--port`). È anche possibile forzare una diversa porta del tunnel (può essere utile in situazioni più complesse, quando sono coinvolti più reindirizzamenti):
|
||||||
|
|
||||||
|
```
|
||||||
|
scrcpy --tunnel-port=1234
|
||||||
|
```
|
||||||
|
|
||||||
|
##### SSH tunnel
|
||||||
|
|
||||||
|
Per comunicare con un server ADB remoto in modo sicuro, è preferibile utilizzare un tunnel SSH.
|
||||||
|
|
||||||
|
Per prima cosa, assicurati che il server ADB sia in esecuzione sul computer remoto:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
adb start-server
|
||||||
|
```
|
||||||
|
|
||||||
|
Poi, crea un tunnel SSH:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# local 5038 --> remote 5037
|
||||||
|
# local 27183 <-- remote 27183
|
||||||
|
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
||||||
|
# keep this open
|
||||||
|
```
|
||||||
|
|
||||||
|
Da un altro terminale, esegui scrcpy:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||||
scrcpy
|
scrcpy
|
||||||
```
|
```
|
||||||
|
|
||||||
Per evitare l'abilitazione dell'apertura porte remota potresti invece forzare una "forward connection" (notare il `-L` invece di `-R`)
|
Per evitare l'abilitazione dell'apertura porte remota potresti invece forzare una "forward connection" (notare il `-L` invece di `-R`)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb kill-server # termina il server adb locale su 5037
|
# local 5038 --> remote 5037
|
||||||
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
# local 27183 --> remote 27183
|
||||||
|
ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
||||||
# tieni questo aperto
|
# tieni questo aperto
|
||||||
```
|
```
|
||||||
|
|
||||||
Da un altro terminale:
|
Da un altro terminale, esegui scrcpy:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||||
scrcpy --force-adb-forward
|
scrcpy --force-adb-forward
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Come per le connessioni wireless potrebbe essere utile ridurre la qualità:
|
Come per le connessioni wireless potrebbe essere utile ridurre la qualità:
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -551,6 +662,14 @@ scrcpy --turn-screen-off --stay-awake
|
|||||||
scrcpy -Sw
|
scrcpy -Sw
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Spegnimento alla chiusura
|
||||||
|
|
||||||
|
Per spegnere lo schermo del dispositivo quando si chiude scrcpy:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --power-off-on-close
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Mostrare i tocchi
|
#### Mostrare i tocchi
|
||||||
|
|
||||||
@@ -596,20 +715,22 @@ Qualsiasi scorciatoia <kbd>Ctrl</kbd> viene inoltrata al dispositivo. In partico
|
|||||||
- <kbd>Ctrl</kbd>+<kbd>x</kbd> taglia
|
- <kbd>Ctrl</kbd>+<kbd>x</kbd> taglia
|
||||||
- <kbd>Ctrl</kbd>+<kbd>v</kbd> incolla (dopo la sincronizzazione degli appunti da computer a dispositivo)
|
- <kbd>Ctrl</kbd>+<kbd>v</kbd> incolla (dopo la sincronizzazione degli appunti da computer a dispositivo)
|
||||||
|
|
||||||
Questo solitamente funziona nella maniera più comune.
|
Questo solitamente funziona come ci si aspetta.
|
||||||
|
|
||||||
Il comportamento reale, però, dipende dall'applicazione attiva. Per esempio _Termux_ invia SIGINT con <kbd>Ctrl</kbd>+<kbd>c</kbd>, e _K-9 Mail_ compone un nuovo messaggio.
|
Il comportamento reale, però, dipende dall'applicazione attiva. Per esempio _Termux_ invia SIGINT con <kbd>Ctrl</kbd>+<kbd>c</kbd>, e _K-9 Mail_ compone un nuovo messaggio.
|
||||||
|
|
||||||
Per copiare, tagliare e incollare in questi casi (ma è solo supportato in Android >= 7):
|
Per copiare, tagliare e incollare in questi casi (ma è solo supportato in Android >= 7):
|
||||||
- <kbd>MOD</kbd>+<kbd>c</kbd> inietta `COPY`
|
- <kbd>MOD</kbd>+<kbd>c</kbd> invia `COPY`
|
||||||
- <kbd>MOD</kbd>+<kbd>x</kbd> inietta `CUT`
|
- <kbd>MOD</kbd>+<kbd>x</kbd> invia `CUT`
|
||||||
- <kbd>MOD</kbd>+<kbd>v</kbd> inietta `PASTE` (dopo la sincronizzazione degli appunti da computer a dispositivo)
|
- <kbd>MOD</kbd>+<kbd>v</kbd> invia `PASTE` (dopo la sincronizzazione degli appunti da computer a dispositivo)
|
||||||
|
|
||||||
In aggiunta, <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> permette l'iniezione del testo degli appunti del computer come una sequenza di eventi pressione dei tasti. Questo è utile quando il componente non accetta l'incollaggio di testo (per esempio in _Termux_), ma questo può rompere il contenuto non ASCII.
|
In aggiunta, <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> permette l'invio del testo degli appunti del computer come una sequenza di eventi pressione dei tasti. Questo è utile quando il componente non accetta l'incollaggio di testo (per esempio in _Termux_), ma questo può compromettere il contenuto non ASCII.
|
||||||
|
|
||||||
**AVVISO:** Incollare gli appunti del computer nel dispositivo (sia con <kbd>Ctrl</kbd>+<kbd>v</kbd> che con <kbd>MOD</kbd>+<kbd>v</kbd>) copia il contenuto negli appunti del dispositivo. Come conseguenza, qualsiasi applicazione Android potrebbe leggere il suo contenuto. Dovresti evitare di incollare contenuti sensibili (come password) in questa maniera.
|
**AVVISO:** Incollare gli appunti del computer nel dispositivo (sia con <kbd>Ctrl</kbd>+<kbd>v</kbd> che con <kbd>MOD</kbd>+<kbd>v</kbd>) copia il contenuto negli appunti del dispositivo. Come conseguenza, qualsiasi applicazione Android potrebbe leggere il suo contenuto. Dovresti evitare di incollare contenuti sensibili (come password) in questa maniera.
|
||||||
|
|
||||||
Alcuni dispositivi non si comportano come aspettato quando si modificano gli appunti del dispositivo a livello di codice. L'opzione `--legacy-paste` è fornita per cambiare il comportamento di <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>MOD</kbd>+<kbd>v</kbd> in modo tale che anch'essi iniettino il testo gli appunti del computer come una sequenza di eventi pressione dei tasti (nella stessa maniera di <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).
|
Alcuni dispositivi non si comportano come aspettato quando si modificano gli appunti del dispositivo a livello di codice. L'opzione `--legacy-paste` è fornita per cambiare il comportamento di <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>MOD</kbd>+<kbd>v</kbd> in modo tale che anch'essi inviino il testo degli appunti del computer come una sequenza di eventi di pressione dei tasti (nella stessa maniera di <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).
|
||||||
|
|
||||||
|
Per disabilitare la sincronizzazione automatica degli appunti, usa `--no-clipboard-autosync`.
|
||||||
|
|
||||||
#### Pizzica per zoomare (pinch-to-zoom)
|
#### Pizzica per zoomare (pinch-to-zoom)
|
||||||
|
|
||||||
@@ -617,16 +738,98 @@ Per simulare il "pizzica per zoomare": <kbd>Ctrl</kbd>+_click e trascina_.
|
|||||||
|
|
||||||
Più precisamente, tieni premuto <kbd>Ctrl</kbd> mentre premi il pulsante sinistro. Finchè il pulsante non sarà rilasciato, tutti i movimenti del mouse ridimensioneranno e ruoteranno il contenuto (se supportato dall'applicazione) relativamente al centro dello schermo.
|
Più precisamente, tieni premuto <kbd>Ctrl</kbd> mentre premi il pulsante sinistro. Finchè il pulsante non sarà rilasciato, tutti i movimenti del mouse ridimensioneranno e ruoteranno il contenuto (se supportato dall'applicazione) relativamente al centro dello schermo.
|
||||||
|
|
||||||
Concretamente scrcpy genera degli eventi di tocco addizionali di un "dito virtuale" nella posizione simmetricamente opposta rispetto al centro dello schermo.
|
Concretamente, scrcpy genera degli eventi di tocco addizionali di un "dito virtuale" nella posizione simmetricamente opposta rispetto al centro dello schermo.
|
||||||
|
|
||||||
|
#### Simulazione della tastiera fisica (HID)
|
||||||
|
|
||||||
|
Per impostazione predefinita, scrcpy utilizza l'invio dei tasti o del testo di Android: funziona ovunque, ma è limitato all'ASCII.
|
||||||
|
|
||||||
|
In alternativa scrcpy può simulare una tastiera fisica USB su Android per fornire una migliore esperienza di input (utilizzando [USB HID over AOAv2][hid-aoav2]): la tastiera virtuale è disabilitata e funziona per tutti i caratteri e IME.
|
||||||
|
|
||||||
|
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
|
||||||
|
|
||||||
|
Tuttavia, funziona solo se il dispositivo è collegato via USB.
|
||||||
|
|
||||||
|
Nota: su Windows, può funzionare solo in [odalità OTG](#otg), non durante il mirroring (non è possibile aprire un dispositivo USB se è già aperto da un altro processo come il daemon adb).
|
||||||
|
|
||||||
|
Per abilitare questa modalità:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --hid-keyboard
|
||||||
|
scrcpy -K # versione breve
|
||||||
|
```
|
||||||
|
|
||||||
|
Se fallisce per qualche motivo (per esempio perché il dispositivo non è connesso via USB), ritorna automaticamente alla modalità predefinita (con un log nella console). Questo permette di usare le stesse opzioni della linea di comando quando si è connessi via USB e TCP/IP.
|
||||||
|
|
||||||
|
In questa modalità, gli eventi i pressione originali (scancodes) sono inviati al dispositivo, indipendentemente dalla mappatura dei tasti dell'host. Pertanto, se il layout della tua tastiera non corrisponde, deve essere configurato sul dispositivo Android, in Impostazioni → Sistema → Lingue e input → [Tastiera fisica] (in inglese).
|
||||||
|
|
||||||
|
Questa pagina di impostazioni può essere avviata direttamente:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
adb shell am start -a android.settings.HARD_KEYBOARD_SETTINGS
|
||||||
|
```
|
||||||
|
|
||||||
|
Tuttavia, l'opzione è disponibile solo quando la tastiera HID è abilitata (o quando una tastiera fisica è collegata).
|
||||||
|
|
||||||
|
[Tastiera fisica]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
|
||||||
|
|
||||||
|
#### Simulazione del mouse fisico (HID)
|
||||||
|
|
||||||
|
In modo simile alla simulazione della tastiera fisica, è possibile simulare un mouse fisico. Allo stesso modo funziona solo se il dispositivo è connesso via USB.
|
||||||
|
|
||||||
|
Per impostazione predefinita, scrcpy utilizza l'invio degli eventi del mouse di Android, utilizzando coordinate assolute. Simulando un mouse fisico, un puntatore del mouse appare sul dispositivo Android e vengono inviati i movimenti relativi del mouse, i click e gli scorrimenti.
|
||||||
|
|
||||||
|
Per abilitare questa modalità:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --hid-mouse
|
||||||
|
scrcpy -M # versione breve
|
||||||
|
```
|
||||||
|
|
||||||
|
Si potrebbe anche aggiungere `--forward-all-clicks` a [inoltra tutti i pulsanti del mouse][forward_all_clicks].
|
||||||
|
|
||||||
|
[forward_all_clicks]: #click-destro-e-click-centrale
|
||||||
|
|
||||||
|
|
||||||
#### Preferenze di iniezione del testo
|
Quando questa modalità è attivata, il mouse del computer viene "catturato" (il puntatore del mouse scompare dal computer e appare invece sul dispositivo Android).
|
||||||
|
|
||||||
|
I tasti speciali di cattura, <kbd>Alt</kbd> o <kbd>Super</kbd>, commutano (disabilitano o abilitano) la cattura del mouse. Usa uno di essi per ridare il controllo del mouse al computer.
|
||||||
|
|
||||||
|
|
||||||
|
#### OTG
|
||||||
|
|
||||||
|
È possibile eseguire _scrcpy_ con la sola simulazione della tastiera fisica e del mouse (HID), come se la tastiera e il mouse del computer fossero collegati direttamente al dispositivo tramite un cavo OTG.
|
||||||
|
|
||||||
|
In questa modalità, _adb_ (debug USB) non è necessario e il mirroring è disabilitato.
|
||||||
|
|
||||||
|
Per attivare la modallità OTG:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --otg
|
||||||
|
# Passa la seriale se sono disponibili diversi dispositivi USB
|
||||||
|
scrcpy --otg -s 0123456789abcdef
|
||||||
|
```
|
||||||
|
|
||||||
|
È possibile abilitare solo la tastiera HID o il mouse HID:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --otg --hid-keyboard # solo la tastiera
|
||||||
|
scrcpy --otg --hid-mouse # solo mouse
|
||||||
|
scrcpy --otg --hid-keyboard --hid-mouse # tastiera e mouse
|
||||||
|
# per comodità, abilita entrambi per default
|
||||||
|
scrcpy --otg # tastiera e mouse
|
||||||
|
```
|
||||||
|
|
||||||
|
Come `--hid-keyboard` e `--hid-mouse`, funziona solo se il dispositivo è collegato via USB.
|
||||||
|
|
||||||
|
|
||||||
|
#### Preferenze di invio del testo
|
||||||
|
|
||||||
Ci sono due tipi di [eventi][textevents] generati quando si scrive testo:
|
Ci sono due tipi di [eventi][textevents] generati quando si scrive testo:
|
||||||
- _eventi di pressione_, segnalano che tasto è stato premuto o rilasciato;
|
- _eventi di pressione_, segnalano che tasto è stato premuto o rilasciato;
|
||||||
- _eventi di testo_, segnalano che del testo è stato inserito.
|
- _eventi di testo_, segnalano che del testo è stato inserito.
|
||||||
|
|
||||||
In maniera predefinita le lettere sono "iniettate" usando gli eventi di pressione, in maniera tale che la tastiera si comporti come aspettato nei giochi (come accade solitamente per i tasti WASD).
|
In maniera predefinita le lettere sono inviate usando gli eventi di pressione, in maniera tale che la tastiera si comporti come aspettato nei giochi (come accade solitamente per i tasti WASD).
|
||||||
|
|
||||||
Questo, però, può [causare problemi][prefertext]. Se incontri un problema del genere, puoi evitarlo con:
|
Questo, però, può [causare problemi][prefertext]. Se incontri un problema del genere, puoi evitarlo con:
|
||||||
|
|
||||||
@@ -636,13 +839,21 @@ scrcpy --prefer-text
|
|||||||
|
|
||||||
(ma questo romperà il normale funzionamento della tastiera nei giochi)
|
(ma questo romperà il normale funzionamento della tastiera nei giochi)
|
||||||
|
|
||||||
|
Al contrario, si potrebbe forzare per inviare sempre eventi di pressione grezzi:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --raw-key-events
|
||||||
|
```
|
||||||
|
|
||||||
|
Queste opzioni non hanno effetto sulla tastiera HID (tutti gli eventi di pressione sono inviati come scancodes in questa modalità).
|
||||||
|
|
||||||
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
||||||
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
||||||
|
|
||||||
|
|
||||||
#### Ripetizione di tasti
|
#### Ripetizione di tasti
|
||||||
|
|
||||||
In maniera predefinita tenere premuto un tasto genera una ripetizione degli eventi di pressione di tale tasto. Questo può creare problemi di performance in alcuni giochi, dove questi eventi sono inutilizzati.
|
In maniera predefinita, tenere premuto un tasto genera una ripetizione degli eventi di pressione di tale tasto. Questo può creare problemi di performance in alcuni giochi, dove questi eventi sono inutilizzati.
|
||||||
|
|
||||||
Per prevenire l'inoltro ripetuto degli eventi di pressione:
|
Per prevenire l'inoltro ripetuto degli eventi di pressione:
|
||||||
|
|
||||||
@@ -650,9 +861,12 @@ Per prevenire l'inoltro ripetuto degli eventi di pressione:
|
|||||||
scrcpy --no-key-repeat
|
scrcpy --no-key-repeat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Questa opzione non ha effetto sulla tastiera HID (la ripetizione dei tasti è gestita da Android direttamente in questa modalità).
|
||||||
|
|
||||||
|
|
||||||
#### Click destro e click centrale
|
#### Click destro e click centrale
|
||||||
|
|
||||||
In maniera predefinita, click destro aziona BACK (indietro) e il click centrale aziona HOME. Per disabilitare queste scorciatoie e, invece, inviare i click al dispositivo:
|
In maniera predefinita, click destro aziona BACK (indietro) o POWER on (accensione) e il click centrale aziona HOME. Per disabilitare queste scorciatoie e, invece, inviare i click al dispositivo:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --forward-all-clicks
|
scrcpy --forward-all-clicks
|
||||||
@@ -705,7 +919,7 @@ scrcpy --shortcut-mod=rctrl
|
|||||||
scrcpy --shortcut-mod=lctrl+lalt,lsuper
|
scrcpy --shortcut-mod=lctrl+lalt,lsuper
|
||||||
```
|
```
|
||||||
|
|
||||||
_<kbd>[Super]</kbd> è il pulsante <kbd>Windows</kbd> o <kbd>Cmd</kbd>._
|
_<kbd>[Super]</kbd> è solitamente il pulsante <kbd>Windows</kbd> o <kbd>Cmd</kbd>._
|
||||||
|
|
||||||
[Super]: https://it.wikipedia.org/wiki/Tasto_Windows
|
[Super]: https://it.wikipedia.org/wiki/Tasto_Windows
|
||||||
<!-- https://en.wikipedia.org/wiki/Super_key_(keyboard_button) è la pagina originale di Wikipedia inglese, l'ho sostituita con una simile in quello italiano -->
|
<!-- https://en.wikipedia.org/wiki/Super_key_(keyboard_button) è la pagina originale di Wikipedia inglese, l'ho sostituita con una simile in quello italiano -->
|
||||||
@@ -720,7 +934,7 @@ _<kbd>[Super]</kbd> è il pulsante <kbd>Windows</kbd> o <kbd>Cmd</kbd>._
|
|||||||
| Premi il tasto `HOME` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _Click centrale_
|
| Premi il tasto `HOME` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _Click centrale_
|
||||||
| Premi il tasto `BACK` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _Click destro²_
|
| Premi il tasto `BACK` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _Click destro²_
|
||||||
| Premi il tasto `APP_SWITCH` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _4° click³_
|
| Premi il tasto `APP_SWITCH` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _4° click³_
|
||||||
| Premi il tasto `MENU` (sblocca lo schermo) | <kbd>MOD</kbd>+<kbd>m</kbd>
|
| Premi il tasto `MENU` (sblocca lo schermo)⁴ | <kbd>MOD</kbd>+<kbd>m</kbd>
|
||||||
| Premi il tasto `VOLUME_UP` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(su)_
|
| Premi il tasto `VOLUME_UP` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(su)_
|
||||||
| Premi il tasto `VOLUME_DOWN` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(giù)_
|
| Premi il tasto `VOLUME_DOWN` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(giù)_
|
||||||
| Premi il tasto `POWER` | <kbd>MOD</kbd>+<kbd>p</kbd>
|
| Premi il tasto `POWER` | <kbd>MOD</kbd>+<kbd>p</kbd>
|
||||||
@@ -731,17 +945,20 @@ _<kbd>[Super]</kbd> è il pulsante <kbd>Windows</kbd> o <kbd>Cmd</kbd>._
|
|||||||
| Espandi il pannello delle notifiche | <kbd>MOD</kbd>+<kbd>n</kbd> \| _5° click³_
|
| Espandi il pannello delle notifiche | <kbd>MOD</kbd>+<kbd>n</kbd> \| _5° click³_
|
||||||
| Espandi il pannello delle impostazioni | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Doppio 5° click³_
|
| Espandi il pannello delle impostazioni | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Doppio 5° click³_
|
||||||
| Chiudi pannelli | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
| Chiudi pannelli | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
||||||
| Copia negli appunti⁴ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
| Copia negli appunti⁵ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
||||||
| Taglia negli appunti⁴ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
| Taglia negli appunti⁵ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
||||||
| Sincronizza gli appunti e incolla⁴ | <kbd>MOD</kbd>+<kbd>v</kbd>
|
| Sincronizza gli appunti e incolla⁵ | <kbd>MOD</kbd>+<kbd>v</kbd>
|
||||||
| Inietta il testo degli appunti del computer | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
|
| Invia il testo degli appunti del computer | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
|
||||||
| Abilita/Disabilita il contatore FPS (su stdout) | <kbd>MOD</kbd>+<kbd>i</kbd>
|
| Abilita/Disabilita il contatore FPS (su stdout) | <kbd>MOD</kbd>+<kbd>i</kbd>
|
||||||
| Pizzica per zoomare | <kbd>Ctrl</kbd>+_click e trascina_
|
| Pizzica per zoomare | <kbd>Ctrl</kbd>+_click e trascina_
|
||||||
|
| Trascina file APK | Installa APK dal computer
|
||||||
|
| Trascina file non-APK | [Trasferisci file verso il dispositivo](#push-file-to-device)
|
||||||
|
|
||||||
_¹Doppio click sui bordi neri per rimuoverli._
|
_¹Doppio click sui bordi neri per rimuoverli._
|
||||||
_²Il tasto destro accende lo schermo se era spento, preme BACK in caso contrario._
|
_²Il tasto destro accende lo schermo se era spento, preme BACK in caso contrario._
|
||||||
_³4° e 5° pulsante del mouse, se il tuo mouse ne dispone._
|
_³4° e 5° pulsante del mouse, se il tuo mouse ne dispone._
|
||||||
_⁴Solo in Android >= 7._
|
_⁴Per le app native react in sviluppo, `MENU` attiva il menu di sviluppo._
|
||||||
|
_⁵Solo in Android >= 7._
|
||||||
|
|
||||||
Le scorciatoie con pulsanti ripetuti sono eseguite rilasciando e premendo il pulsante una seconda volta. Per esempio, per eseguire "Espandi il pannello delle impostazioni":
|
Le scorciatoie con pulsanti ripetuti sono eseguite rilasciando e premendo il pulsante una seconda volta. Per esempio, per eseguire "Espandi il pannello delle impostazioni":
|
||||||
|
|
||||||
@@ -811,3 +1028,14 @@ Leggi la [pagina per sviluppatori].
|
|||||||
|
|
||||||
[article-intro]: https://blog.rom1v.com/2018/03/introducing-scrcpy/
|
[article-intro]: https://blog.rom1v.com/2018/03/introducing-scrcpy/
|
||||||
[article-tcpip]: https://www.genymotion.com/blog/open-source-project-scrcpy-now-works-wirelessly/
|
[article-tcpip]: https://www.genymotion.com/blog/open-source-project-scrcpy-now-works-wirelessly/
|
||||||
|
|
||||||
|
## Contatti
|
||||||
|
|
||||||
|
Se incontri un bug, per favore leggi prima le [FAQ](FAQ.it.md), poi apri una [issue].
|
||||||
|
|
||||||
|
[issue]: https://github.com/Genymobile/scrcpy/issues
|
||||||
|
|
||||||
|
Per domande generali o discussioni, puoi anche usare:
|
||||||
|
|
||||||
|
- Reddit: [`r/scrcpy`](https://www.reddit.com/r/scrcpy)
|
||||||
|
- Twitter: [`@scrcpy_app`](https://twitter.com/scrcpy_app)
|
||||||
|
|||||||
231
README.md
231
README.md
@@ -1,13 +1,13 @@
|
|||||||
# scrcpy (v1.22)
|
# scrcpy (v1.23)
|
||||||
|
|
||||||
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
<img src="app/data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
||||||
|
|
||||||
_pronounced "**scr**een **c**o**py**"_
|
_pronounced "**scr**een **c**o**py**"_
|
||||||
|
|
||||||
[Read in another language](#translations)
|
[Read in another language](#translations)
|
||||||
|
|
||||||
This application provides display and control of Android devices connected via
|
This application provides display and control of Android devices connected via
|
||||||
USB (or [over TCP/IP](#tcpip-wireless)). It does not require any _root_ access.
|
USB or [over TCP/IP](#tcpip-wireless). It does not require any _root_ access.
|
||||||
It works on _GNU/Linux_, _Windows_ and _macOS_.
|
It works on _GNU/Linux_, _Windows_ and _macOS_.
|
||||||
|
|
||||||

|

|
||||||
@@ -19,7 +19,7 @@ It focuses on:
|
|||||||
- **quality**: 1920×1080 or above
|
- **quality**: 1920×1080 or above
|
||||||
- **low latency**: [35~70ms][lowlatency]
|
- **low latency**: [35~70ms][lowlatency]
|
||||||
- **low startup time**: ~1 second to display the first image
|
- **low startup time**: ~1 second to display the first image
|
||||||
- **non-intrusiveness**: nothing is left installed on the device
|
- **non-intrusiveness**: nothing is left installed on the Android device
|
||||||
- **user benefits**: no account, no ads, no internet required
|
- **user benefits**: no account, no ads, no internet required
|
||||||
- **freedom**: free and open source software
|
- **freedom**: free and open source software
|
||||||
|
|
||||||
@@ -27,27 +27,25 @@ It focuses on:
|
|||||||
|
|
||||||
Its features include:
|
Its features include:
|
||||||
- [recording](#recording)
|
- [recording](#recording)
|
||||||
- mirroring with [device screen off](#turn-screen-off)
|
- mirroring with [Android device screen off](#turn-screen-off)
|
||||||
- [copy-paste](#copy-paste) in both directions
|
- [copy-paste](#copy-paste) in both directions
|
||||||
- [configurable quality](#capture-configuration)
|
- [configurable quality](#capture-configuration)
|
||||||
- device screen [as a webcam (V4L2)](#v4l2loopback) (Linux-only)
|
- Android device [as a webcam (V4L2)](#v4l2loopback) (Linux-only)
|
||||||
- [physical keyboard simulation (HID)](#physical-keyboard-simulation-hid)
|
- [physical keyboard simulation (HID)](#physical-keyboard-simulation-hid)
|
||||||
(Linux-only)
|
|
||||||
- [physical mouse simulation (HID)](#physical-mouse-simulation-hid)
|
- [physical mouse simulation (HID)](#physical-mouse-simulation-hid)
|
||||||
(Linux-only)
|
- [OTG mode](#otg)
|
||||||
- [OTG mode](#otg) (Linux-only)
|
|
||||||
- and more…
|
- and more…
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
The Android device requires at least API 21 (Android 5.0).
|
The Android device requires at least API 21 (Android 5.0).
|
||||||
|
|
||||||
Make sure you [enabled adb debugging][enable-adb] on your device(s).
|
Make sure you [enable adb debugging][enable-adb] on your device(s).
|
||||||
|
|
||||||
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
|
[enable-adb]: https://developer.android.com/studio/command-line/adb.html#Enabling
|
||||||
|
|
||||||
On some devices, you also need to enable [an additional option][control] to
|
On some devices, you also need to enable [an additional option][control] to
|
||||||
control it using keyboard and mouse.
|
control it using a keyboard and mouse.
|
||||||
|
|
||||||
[control]: https://github.com/Genymobile/scrcpy/issues/70#issuecomment-373286323
|
[control]: https://github.com/Genymobile/scrcpy/issues/70#issuecomment-373286323
|
||||||
|
|
||||||
@@ -99,19 +97,19 @@ For Gentoo, an [Ebuild] is available: [`scrcpy/`][ebuild-link].
|
|||||||
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
|
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
|
||||||
[ebuild-link]: https://github.com/maggu2810/maggu2810-overlay/tree/master/app-mobilephone/scrcpy
|
[ebuild-link]: https://github.com/maggu2810/maggu2810-overlay/tree/master/app-mobilephone/scrcpy
|
||||||
|
|
||||||
You could also [build the app manually][BUILD] ([simplified
|
You can also [build the app manually][BUILD] ([simplified
|
||||||
process][BUILD_simple]).
|
process][BUILD_simple]).
|
||||||
|
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
|
|
||||||
For Windows, for simplicity, a prebuilt archive with all the dependencies
|
For Windows, a prebuilt archive with all the dependencies (including `adb`) is
|
||||||
(including `adb`) is available:
|
available:
|
||||||
|
|
||||||
- [`scrcpy-win64-v1.22.zip`][direct-win64]
|
- [`scrcpy-win64-v1.23.zip`][direct-win64]
|
||||||
_(SHA-256: ce4d9b8cc761e29862c4a72d8ad6f538bdd1f1831d15fd1f36633cd3b403db82)_
|
_(SHA-256: d2f601b1d0157faf65153d8a093d827fd65aec5d5842d677ac86fb2b5b7704cc)_
|
||||||
|
|
||||||
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.22/scrcpy-win64-v1.22.zip
|
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.23/scrcpy-win64-v1.23.zip
|
||||||
|
|
||||||
It is also available in [Chocolatey]:
|
It is also available in [Chocolatey]:
|
||||||
|
|
||||||
@@ -150,7 +148,7 @@ You need `adb`, accessible from your `PATH`. If you don't have it yet:
|
|||||||
brew install android-platform-tools
|
brew install android-platform-tools
|
||||||
```
|
```
|
||||||
|
|
||||||
It's also available in [MacPorts], which sets up adb for you:
|
It's also available in [MacPorts], which sets up `adb` for you:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo port install scrcpy
|
sudo port install scrcpy
|
||||||
@@ -164,7 +162,7 @@ You can also [build the app manually][BUILD].
|
|||||||
|
|
||||||
## Run
|
## Run
|
||||||
|
|
||||||
Plug an Android device, and execute:
|
Plug an Android device into your computer, and execute:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy
|
scrcpy
|
||||||
@@ -182,7 +180,7 @@ scrcpy --help
|
|||||||
|
|
||||||
#### Reduce size
|
#### Reduce size
|
||||||
|
|
||||||
Sometimes, it is useful to mirror an Android device at a lower definition to
|
Sometimes, it is useful to mirror an Android device at a lower resolution to
|
||||||
increase performance.
|
increase performance.
|
||||||
|
|
||||||
To limit both the width and height to some value (e.g. 1024):
|
To limit both the width and height to some value (e.g. 1024):
|
||||||
@@ -192,8 +190,8 @@ scrcpy --max-size 1024
|
|||||||
scrcpy -m 1024 # short version
|
scrcpy -m 1024 # short version
|
||||||
```
|
```
|
||||||
|
|
||||||
The other dimension is computed to that the device aspect ratio is preserved.
|
The other dimension is computed so that the Android device aspect ratio is
|
||||||
That way, a device in 1920×1080 will be mirrored at 1024×576.
|
preserved. That way, a device in 1920×1080 will be mirrored at 1024×576.
|
||||||
|
|
||||||
|
|
||||||
#### Change bit-rate
|
#### Change bit-rate
|
||||||
@@ -215,11 +213,20 @@ scrcpy --max-fps 15
|
|||||||
|
|
||||||
This is officially supported since Android 10, but may work on earlier versions.
|
This is officially supported since Android 10, but may work on earlier versions.
|
||||||
|
|
||||||
|
The actual capture framerate may be printed to the console:
|
||||||
|
|
||||||
|
```
|
||||||
|
scrcpy --print-fps
|
||||||
|
```
|
||||||
|
|
||||||
|
It may also be enabled or disabled at any time with <kbd>MOD</kbd>+<kbd>i</kbd>.
|
||||||
|
|
||||||
|
|
||||||
#### Crop
|
#### Crop
|
||||||
|
|
||||||
The device screen may be cropped to mirror only part of the screen.
|
The device screen may be cropped to mirror only part of the screen.
|
||||||
|
|
||||||
This is useful for example to mirror only one eye of the Oculus Go:
|
This is useful, for example, to mirror only one eye of the Oculus Go:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --crop 1224:1440:0:0 # 1224x1440 at offset (0,0)
|
scrcpy --crop 1224:1440:0:0 # 1224x1440 at offset (0,0)
|
||||||
@@ -230,7 +237,6 @@ If `--max-size` is also specified, resizing is applied after cropping.
|
|||||||
|
|
||||||
#### Lock video orientation
|
#### Lock video orientation
|
||||||
|
|
||||||
|
|
||||||
To lock the orientation of the mirroring:
|
To lock the orientation of the mirroring:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -255,7 +261,7 @@ crash. It is possible to select a different encoder:
|
|||||||
scrcpy --encoder OMX.qcom.video.encoder.avc
|
scrcpy --encoder OMX.qcom.video.encoder.avc
|
||||||
```
|
```
|
||||||
|
|
||||||
To list the available encoders, you could pass an invalid encoder name, the
|
To list the available encoders, you can pass an invalid encoder name; the
|
||||||
error will give the available encoders:
|
error will give the available encoders:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -319,7 +325,7 @@ v4l2-ctl --list-devices
|
|||||||
ls /dev/video*
|
ls /dev/video*
|
||||||
```
|
```
|
||||||
|
|
||||||
To start scrcpy using a v4l2 sink:
|
To start `scrcpy` using a v4l2 sink:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --v4l2-sink=/dev/videoN
|
scrcpy --v4l2-sink=/dev/videoN
|
||||||
@@ -327,7 +333,7 @@ scrcpy --v4l2-sink=/dev/videoN --no-display # disable mirroring window
|
|||||||
scrcpy --v4l2-sink=/dev/videoN -N # short version
|
scrcpy --v4l2-sink=/dev/videoN -N # short version
|
||||||
```
|
```
|
||||||
|
|
||||||
(replace `N` by the device ID, check with `ls /dev/video*`)
|
(replace `N` with the device ID, check with `ls /dev/video*`)
|
||||||
|
|
||||||
Once enabled, you can open your video stream with a v4l2-capable tool:
|
Once enabled, you can open your video stream with a v4l2-capable tool:
|
||||||
|
|
||||||
@@ -343,7 +349,7 @@ For example, you could capture the video within [OBS].
|
|||||||
|
|
||||||
#### Buffering
|
#### Buffering
|
||||||
|
|
||||||
It is possible to add buffering. This increases latency but reduces jitter (see
|
It is possible to add buffering. This increases latency, but reduces jitter (see
|
||||||
[#2464]).
|
[#2464]).
|
||||||
|
|
||||||
[#2464]: https://github.com/Genymobile/scrcpy/issues/2464
|
[#2464]: https://github.com/Genymobile/scrcpy/issues/2464
|
||||||
@@ -375,14 +381,14 @@ An option `--tcpip` allows to configure the connection automatically. There are
|
|||||||
two variants.
|
two variants.
|
||||||
|
|
||||||
If the device (accessible at 192.168.1.1 in this example) already listens on a
|
If the device (accessible at 192.168.1.1 in this example) already listens on a
|
||||||
port (typically 5555) for incoming adb connections, then run:
|
port (typically 5555) for incoming _adb_ connections, then run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --tcpip=192.168.1.1 # default port is 5555
|
scrcpy --tcpip=192.168.1.1 # default port is 5555
|
||||||
scrcpy --tcpip=192.168.1.1:5555
|
scrcpy --tcpip=192.168.1.1:5555
|
||||||
```
|
```
|
||||||
|
|
||||||
If adb TCP/IP mode is disabled on the device (or if you don't know the IP
|
If _adb_ TCP/IP mode is disabled on the device (or if you don't know the IP
|
||||||
address), connect the device over USB, then run:
|
address), connect the device over USB, then run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -397,20 +403,32 @@ connect to the device before starting.
|
|||||||
Alternatively, it is possible to enable the TCP/IP connection manually using
|
Alternatively, it is possible to enable the TCP/IP connection manually using
|
||||||
`adb`:
|
`adb`:
|
||||||
|
|
||||||
1. Connect the device to the same Wi-Fi as your computer.
|
1. Plug the device into a USB port on your computer.
|
||||||
2. Get your device IP address, in Settings → About phone → Status, or by
|
2. Connect the device to the same Wi-Fi network as your computer.
|
||||||
|
3. Get your device IP address, in Settings → About phone → Status, or by
|
||||||
executing this command:
|
executing this command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb shell ip route | awk '{print $9}'
|
adb shell ip route | awk '{print $9}'
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Enable adb over TCP/IP on your device: `adb tcpip 5555`.
|
4. Enable `adb` over TCP/IP on your device: `adb tcpip 5555`.
|
||||||
4. Unplug your device.
|
5. Unplug your device.
|
||||||
5. Connect to your device: `adb connect DEVICE_IP:5555` _(replace `DEVICE_IP`)_.
|
6. Connect to your device: `adb connect DEVICE_IP:5555` _(replace `DEVICE_IP`
|
||||||
6. Run `scrcpy` as usual.
|
with the device IP address you found)_.
|
||||||
|
7. Run `scrcpy` as usual.
|
||||||
|
|
||||||
It may be useful to decrease the bit-rate and the definition:
|
Since Android 11, a [Wireless debugging option][adb-wireless] allows to bypass
|
||||||
|
having to physically connect your device directly to your computer.
|
||||||
|
|
||||||
|
[adb-wireless]: https://developer.android.com/studio/command-line/adb#connect-to-a-device-over-wi-fi-android-11+
|
||||||
|
|
||||||
|
If the connection randomly drops, run your `scrcpy` command to reconnect. If it
|
||||||
|
says there are no devices/emulators found, try running `adb connect
|
||||||
|
DEVICE_IP:5555` again, and then `scrcpy` as usual. If it still says there are
|
||||||
|
none found, try running `adb disconnect`, and then run those two commands again.
|
||||||
|
|
||||||
|
It may be useful to decrease the bit-rate and the resolution:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --bit-rate 2M --max-size 800
|
scrcpy --bit-rate 2M --max-size 800
|
||||||
@@ -422,7 +440,7 @@ scrcpy -b2M -m800 # short version
|
|||||||
|
|
||||||
#### Multi-devices
|
#### Multi-devices
|
||||||
|
|
||||||
If several devices are listed in `adb devices`, you must specify the _serial_:
|
If several devices are listed in `adb devices`, you can specify the _serial_:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --serial 0123456789abcdef
|
scrcpy --serial 0123456789abcdef
|
||||||
@@ -436,6 +454,19 @@ scrcpy --serial 192.168.0.1:5555
|
|||||||
scrcpy -s 192.168.0.1:5555 # short version
|
scrcpy -s 192.168.0.1:5555 # short version
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If only one device is connected via either USB or TCP/IP, it is possible to
|
||||||
|
select it automatically:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Select the only device connected via USB
|
||||||
|
scrcpy -d # like adb -d
|
||||||
|
scrcpy --select-usb # long version
|
||||||
|
|
||||||
|
# Select the only device connected via TCP/IP
|
||||||
|
scrcpy -e # like adb -e
|
||||||
|
scrcpy --select-tcpip # long version
|
||||||
|
```
|
||||||
|
|
||||||
You can start several instances of _scrcpy_ for several devices.
|
You can start several instances of _scrcpy_ for several devices.
|
||||||
|
|
||||||
#### Autostart on device connection
|
#### Autostart on device connection
|
||||||
@@ -456,7 +487,7 @@ protocol).
|
|||||||
|
|
||||||
##### Remote ADB server
|
##### Remote ADB server
|
||||||
|
|
||||||
To connect to a remote ADB server, make the server listen on all interfaces:
|
To connect to a remote _adb server_, make the server listen on all interfaces:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb kill-server
|
adb kill-server
|
||||||
@@ -464,17 +495,18 @@ adb -a nodaemon server start
|
|||||||
# keep this open
|
# keep this open
|
||||||
```
|
```
|
||||||
|
|
||||||
**Warning: all communications between clients and ADB server are unencrypted.**
|
**Warning: all communications between clients and the _adb server_ are
|
||||||
|
unencrypted.**
|
||||||
|
|
||||||
Suppose that this server is accessible at 192.168.1.2. Then, from another
|
Suppose that this server is accessible at 192.168.1.2. Then, from another
|
||||||
terminal, run scrcpy:
|
terminal, run `scrcpy`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
|
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
|
||||||
scrcpy --tunnel-host=192.168.1.2
|
scrcpy --tunnel-host=192.168.1.2
|
||||||
```
|
```
|
||||||
|
|
||||||
By default, scrcpy uses the local port used for `adb forward` tunnel
|
By default, `scrcpy` uses the local port used for `adb forward` tunnel
|
||||||
establishment (typically `27183`, see `--port`). It is also possible to force a
|
establishment (typically `27183`, see `--port`). It is also possible to force a
|
||||||
different tunnel port (it may be useful in more complex situations, when more
|
different tunnel port (it may be useful in more complex situations, when more
|
||||||
redirections are involved):
|
redirections are involved):
|
||||||
@@ -486,16 +518,16 @@ scrcpy --tunnel-port=1234
|
|||||||
|
|
||||||
##### SSH tunnel
|
##### SSH tunnel
|
||||||
|
|
||||||
To communicate with a remote ADB server securely, it is preferable to use a SSH
|
To communicate with a remote _adb server_ securely, it is preferable to use an
|
||||||
tunnel.
|
SSH tunnel.
|
||||||
|
|
||||||
First, make sure the ADB server is running on the remote computer:
|
First, make sure the _adb server_ is running on the remote computer:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb start-server
|
adb start-server
|
||||||
```
|
```
|
||||||
|
|
||||||
Then, establish a SSH tunnel:
|
Then, establish an SSH tunnel:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# local 5038 --> remote 5037
|
# local 5038 --> remote 5037
|
||||||
@@ -504,7 +536,7 @@ ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
|||||||
# keep this open
|
# keep this open
|
||||||
```
|
```
|
||||||
|
|
||||||
From another terminal, run scrcpy:
|
From another terminal, run `scrcpy`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||||
@@ -521,7 +553,7 @@ ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
|||||||
# keep this open
|
# keep this open
|
||||||
```
|
```
|
||||||
|
|
||||||
From another terminal, run scrcpy:
|
From another terminal, run `scrcpy`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||||
@@ -563,7 +595,7 @@ scrcpy --window-borderless
|
|||||||
|
|
||||||
#### Always on top
|
#### Always on top
|
||||||
|
|
||||||
To keep the scrcpy window always on top:
|
To keep the _scrcpy_ window always on top:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --always-on-top
|
scrcpy --always-on-top
|
||||||
@@ -588,7 +620,7 @@ The window may be rotated:
|
|||||||
scrcpy --rotation 1
|
scrcpy --rotation 1
|
||||||
```
|
```
|
||||||
|
|
||||||
Possibles values are:
|
Possible values:
|
||||||
- `0`: no rotation
|
- `0`: no rotation
|
||||||
- `1`: 90 degrees counterclockwise
|
- `1`: 90 degrees counterclockwise
|
||||||
- `2`: 180 degrees
|
- `2`: 180 degrees
|
||||||
@@ -637,19 +669,19 @@ adb shell dumpsys display # search "mDisplayId=" in the output
|
|||||||
```
|
```
|
||||||
|
|
||||||
The secondary display may only be controlled if the device runs at least Android
|
The secondary display may only be controlled if the device runs at least Android
|
||||||
10 (otherwise it is mirrored in read-only).
|
10 (otherwise it is mirrored as read-only).
|
||||||
|
|
||||||
|
|
||||||
#### Stay awake
|
#### Stay awake
|
||||||
|
|
||||||
To prevent the device to sleep after some delay when the device is plugged in:
|
To prevent the device from sleeping after a delay when the device is plugged in:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --stay-awake
|
scrcpy --stay-awake
|
||||||
scrcpy -w
|
scrcpy -w
|
||||||
```
|
```
|
||||||
|
|
||||||
The initial state is restored when scrcpy is closed.
|
The initial state is restored when _scrcpy_ is closed.
|
||||||
|
|
||||||
|
|
||||||
#### Turn screen off
|
#### Turn screen off
|
||||||
@@ -667,9 +699,10 @@ Or by pressing <kbd>MOD</kbd>+<kbd>o</kbd> at any time.
|
|||||||
To turn it back on, press <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>.
|
To turn it back on, press <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>.
|
||||||
|
|
||||||
On Android, the `POWER` button always turns the screen on. For convenience, if
|
On Android, the `POWER` button always turns the screen on. For convenience, if
|
||||||
`POWER` is sent via scrcpy (via right-click or <kbd>MOD</kbd>+<kbd>p</kbd>), it
|
`POWER` is sent via _scrcpy_ (via right-click or <kbd>MOD</kbd>+<kbd>p</kbd>),
|
||||||
will force to turn the screen off after a small delay (on a best effort basis).
|
it will force to turn the screen off after a small delay (on a best effort
|
||||||
The physical `POWER` button will still cause the screen to be turned on.
|
basis). The physical `POWER` button will still cause the screen to be turned
|
||||||
|
on.
|
||||||
|
|
||||||
It can also be useful to prevent the device from sleeping:
|
It can also be useful to prevent the device from sleeping:
|
||||||
|
|
||||||
@@ -680,7 +713,7 @@ scrcpy -Sw
|
|||||||
|
|
||||||
#### Power off on close
|
#### Power off on close
|
||||||
|
|
||||||
To turn the device screen off when closing scrcpy:
|
To turn the device screen off when closing _scrcpy_:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --power-off-on-close
|
scrcpy --power-off-on-close
|
||||||
@@ -702,12 +735,13 @@ scrcpy --show-touches
|
|||||||
scrcpy -t
|
scrcpy -t
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that it only shows _physical_ touches (with the finger on the device).
|
Note that it only shows _physical_ touches (by a finger on the device).
|
||||||
|
|
||||||
|
|
||||||
#### Disable screensaver
|
#### Disable screensaver
|
||||||
|
|
||||||
By default, scrcpy does not prevent the screensaver to run on the computer.
|
By default, _scrcpy_ does not prevent the screensaver from running on the
|
||||||
|
computer.
|
||||||
|
|
||||||
To disable it:
|
To disable it:
|
||||||
|
|
||||||
@@ -749,18 +783,18 @@ To copy, cut and paste in such cases (but only supported on Android >= 7):
|
|||||||
- <kbd>MOD</kbd>+<kbd>v</kbd> injects `PASTE` (after computer-to-device
|
- <kbd>MOD</kbd>+<kbd>v</kbd> injects `PASTE` (after computer-to-device
|
||||||
clipboard synchronization)
|
clipboard synchronization)
|
||||||
|
|
||||||
In addition, <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> allows to inject the
|
In addition, <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> injects the computer
|
||||||
computer clipboard text as a sequence of key events. This is useful when the
|
clipboard text as a sequence of key events. This is useful when the component
|
||||||
component does not accept text pasting (for example in _Termux_), but it can
|
does not accept text pasting (for example in _Termux_), but it can break
|
||||||
break non-ASCII content.
|
non-ASCII content.
|
||||||
|
|
||||||
**WARNING:** Pasting the computer clipboard to the device (either via
|
**WARNING:** Pasting the computer clipboard to the device (either via
|
||||||
<kbd>Ctrl</kbd>+<kbd>v</kbd> or <kbd>MOD</kbd>+<kbd>v</kbd>) copies the content
|
<kbd>Ctrl</kbd>+<kbd>v</kbd> or <kbd>MOD</kbd>+<kbd>v</kbd>) copies the content
|
||||||
into the device clipboard. As a consequence, any Android application could read
|
into the Android clipboard. As a consequence, any Android application could read
|
||||||
its content. You should avoid to paste sensitive content (like passwords) that
|
its content. You should avoid pasting sensitive content (like passwords) that
|
||||||
way.
|
way.
|
||||||
|
|
||||||
Some devices do not behave as expected when setting the device clipboard
|
Some Android devices do not behave as expected when setting the device clipboard
|
||||||
programmatically. An option `--legacy-paste` is provided to change the behavior
|
programmatically. An option `--legacy-paste` is provided to change the behavior
|
||||||
of <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>MOD</kbd>+<kbd>v</kbd> so that they
|
of <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>MOD</kbd>+<kbd>v</kbd> so that they
|
||||||
also inject the computer clipboard text as a sequence of key events (the same
|
also inject the computer clipboard text as a sequence of key events (the same
|
||||||
@@ -773,26 +807,29 @@ To disable automatic clipboard synchronization, use
|
|||||||
|
|
||||||
To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
|
To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
|
||||||
|
|
||||||
More precisely, hold <kbd>Ctrl</kbd> while pressing the left-click button. Until
|
More precisely, hold down <kbd>Ctrl</kbd> while pressing the left-click button.
|
||||||
the left-click button is released, all mouse movements scale and rotate the
|
Until the left-click button is released, all mouse movements scale and rotate
|
||||||
content (if supported by the app) relative to the center of the screen.
|
the content (if supported by the app) relative to the center of the screen.
|
||||||
|
|
||||||
Concretely, scrcpy generates additional touch events from a "virtual finger" at
|
Technically, _scrcpy_ generates additional touch events from a "virtual finger"
|
||||||
a location inverted through the center of the screen.
|
at a location inverted through the center of the screen.
|
||||||
|
|
||||||
#### Physical keyboard simulation (HID)
|
#### Physical keyboard simulation (HID)
|
||||||
|
|
||||||
By default, scrcpy uses Android key or text injection: it works everywhere, but
|
By default, _scrcpy_ uses Android key or text injection: it works everywhere,
|
||||||
is limited to ASCII.
|
but is limited to ASCII.
|
||||||
|
|
||||||
On Linux, scrcpy can simulate a physical USB keyboard on Android to provide a
|
Alternatively, `scrcpy` can simulate a physical USB keyboard on Android to
|
||||||
better input experience (using [USB HID over AOAv2][hid-aoav2]): the virtual
|
provide a better input experience (using [USB HID over AOAv2][hid-aoav2]): the
|
||||||
keyboard is disabled and it works for all characters and IME.
|
virtual keyboard is disabled and it works for all characters and IME.
|
||||||
|
|
||||||
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
|
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
|
||||||
|
|
||||||
However, it only works if the device is connected by USB, and is currently only
|
However, it only works if the device is connected via USB.
|
||||||
supported on Linux.
|
|
||||||
|
Note: On Windows, it may only work in [OTG mode](#otg), not while mirroring (it
|
||||||
|
is not possible to open a USB device if it is already open by another process
|
||||||
|
like the _adb daemon_).
|
||||||
|
|
||||||
To enable this mode:
|
To enable this mode:
|
||||||
|
|
||||||
@@ -803,7 +840,7 @@ scrcpy -K # short version
|
|||||||
|
|
||||||
If it fails for some reason (for example because the device is not connected via
|
If it fails for some reason (for example because the device is not connected via
|
||||||
USB), it automatically fallbacks to the default mode (with a log in the
|
USB), it automatically fallbacks to the default mode (with a log in the
|
||||||
console). This allows to use the same command line options when connected over
|
console). This allows using the same command line options when connected over
|
||||||
USB and TCP/IP.
|
USB and TCP/IP.
|
||||||
|
|
||||||
In this mode, raw key events (scancodes) are sent to the device, independently
|
In this mode, raw key events (scancodes) are sent to the device, independently
|
||||||
@@ -825,10 +862,9 @@ a physical keyboard is connected).
|
|||||||
#### Physical mouse simulation (HID)
|
#### Physical mouse simulation (HID)
|
||||||
|
|
||||||
Similarly to the physical keyboard simulation, it is possible to simulate a
|
Similarly to the physical keyboard simulation, it is possible to simulate a
|
||||||
physical mouse. Likewise, it only works if the device is connected by USB, and
|
physical mouse. Likewise, it only works if the device is connected by USB.
|
||||||
is currently only supported on Linux.
|
|
||||||
|
|
||||||
By default, scrcpy uses Android mouse events injection, using absolute
|
By default, _scrcpy_ uses Android mouse events injection with absolute
|
||||||
coordinates. By simulating a physical mouse, a mouse pointer appears on the
|
coordinates. By simulating a physical mouse, a mouse pointer appears on the
|
||||||
Android device, and relative mouse motion, clicks and scrolls are injected.
|
Android device, and relative mouse motion, clicks and scrolls are injected.
|
||||||
|
|
||||||
@@ -839,7 +875,7 @@ scrcpy --hid-mouse
|
|||||||
scrcpy -M # short version
|
scrcpy -M # short version
|
||||||
```
|
```
|
||||||
|
|
||||||
You could also add `--forward-all-clicks` to [forward all mouse
|
You can also add `--forward-all-clicks` to [forward all mouse
|
||||||
buttons][forward_all_clicks].
|
buttons][forward_all_clicks].
|
||||||
|
|
||||||
[forward_all_clicks]: #right-click-and-middle-click
|
[forward_all_clicks]: #right-click-and-middle-click
|
||||||
@@ -858,7 +894,7 @@ It is possible to run _scrcpy_ with only physical keyboard and mouse simulation
|
|||||||
(HID), as if the computer keyboard and mouse were plugged directly to the device
|
(HID), as if the computer keyboard and mouse were plugged directly to the device
|
||||||
via an OTG cable.
|
via an OTG cable.
|
||||||
|
|
||||||
In this mode, _adb_ (USB debugging) is not necessary, and mirroring is disabled.
|
In this mode, `adb` (USB debugging) is not necessary, and mirroring is disabled.
|
||||||
|
|
||||||
To enable OTG mode:
|
To enable OTG mode:
|
||||||
|
|
||||||
@@ -879,12 +915,12 @@ scrcpy --otg # keyboard and mouse
|
|||||||
```
|
```
|
||||||
|
|
||||||
Like `--hid-keyboard` and `--hid-mouse`, it only works if the device is
|
Like `--hid-keyboard` and `--hid-mouse`, it only works if the device is
|
||||||
connected by USB, and is currently only supported on Linux.
|
connected by USB.
|
||||||
|
|
||||||
|
|
||||||
#### Text injection preference
|
#### Text injection preference
|
||||||
|
|
||||||
There are two kinds of [events][textevents] generated when typing text:
|
Two kinds of [events][textevents] are generated when typing text:
|
||||||
- _key events_, signaling that a key is pressed or released;
|
- _key events_, signaling that a key is pressed or released;
|
||||||
- _text events_, signaling that a text has been entered.
|
- _text events_, signaling that a text has been entered.
|
||||||
|
|
||||||
@@ -1028,7 +1064,7 @@ _³4th and 5th mouse buttons, if your mouse has them._
|
|||||||
_⁴For react-native apps in development, `MENU` triggers development menu._
|
_⁴For react-native apps in development, `MENU` triggers development menu._
|
||||||
_⁵Only on Android >= 7._
|
_⁵Only on Android >= 7._
|
||||||
|
|
||||||
Shortcuts with repeated keys are executted by releasing and pressing the key a
|
Shortcuts with repeated keys are executed by releasing and pressing the key a
|
||||||
second time. For example, to execute "Expand settings panel":
|
second time. For example, to execute "Expand settings panel":
|
||||||
|
|
||||||
1. Press and keep pressing <kbd>MOD</kbd>.
|
1. Press and keep pressing <kbd>MOD</kbd>.
|
||||||
@@ -1041,7 +1077,7 @@ handled by the active application.
|
|||||||
|
|
||||||
## Custom paths
|
## Custom paths
|
||||||
|
|
||||||
To use a specific _adb_ binary, configure its path in the environment variable
|
To use a specific `adb` binary, configure its path in the environment variable
|
||||||
`ADB`:
|
`ADB`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -1054,7 +1090,7 @@ To override the path of the `scrcpy-server` file, configure its path in
|
|||||||
To override the icon, configure its path in `SCRCPY_ICON_PATH`.
|
To override the icon, configure its path in `SCRCPY_ICON_PATH`.
|
||||||
|
|
||||||
|
|
||||||
## Why _scrcpy_?
|
## Why the name _scrcpy_?
|
||||||
|
|
||||||
A colleague challenged me to find a name as unpronounceable as [gnirehtet].
|
A colleague challenged me to find a name as unpronounceable as [gnirehtet].
|
||||||
|
|
||||||
@@ -1071,7 +1107,9 @@ See [BUILD].
|
|||||||
|
|
||||||
## Common issues
|
## Common issues
|
||||||
|
|
||||||
See the [FAQ](FAQ.md).
|
See the [FAQ].
|
||||||
|
|
||||||
|
[FAQ]: FAQ.md
|
||||||
|
|
||||||
|
|
||||||
## Developers
|
## Developers
|
||||||
@@ -1106,13 +1144,24 @@ Read the [developers page].
|
|||||||
[article-intro]: https://blog.rom1v.com/2018/03/introducing-scrcpy/
|
[article-intro]: https://blog.rom1v.com/2018/03/introducing-scrcpy/
|
||||||
[article-tcpip]: https://www.genymotion.com/blog/open-source-project-scrcpy-now-works-wirelessly/
|
[article-tcpip]: https://www.genymotion.com/blog/open-source-project-scrcpy-now-works-wirelessly/
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
If you encounter a bug, please read the [FAQ] first, then open an [issue].
|
||||||
|
|
||||||
|
[issue]: https://github.com/Genymobile/scrcpy/issues
|
||||||
|
|
||||||
|
For general questions or discussions, you can also use:
|
||||||
|
|
||||||
|
- Reddit: [`r/scrcpy`](https://www.reddit.com/r/scrcpy)
|
||||||
|
- Twitter: [`@scrcpy_app`](https://twitter.com/scrcpy_app)
|
||||||
|
|
||||||
## Translations
|
## Translations
|
||||||
|
|
||||||
This README is available in other languages:
|
This README is available in other languages:
|
||||||
|
|
||||||
- [Deutsch (German, `de`) - v1.22](README.de.md)
|
- [Deutsch (German, `de`) - v1.22](README.de.md)
|
||||||
- [Indonesian (Indonesia, `id`) - v1.16](README.id.md)
|
- [Indonesian (Indonesia, `id`) - v1.16](README.id.md)
|
||||||
- [Italiano (Italiano, `it`) - v1.19](README.it.md)
|
- [Italiano (Italiano, `it`) - v1.23](README.it.md)
|
||||||
- [日本語 (Japanese, `jp`) - v1.19](README.jp.md)
|
- [日本語 (Japanese, `jp`) - v1.19](README.jp.md)
|
||||||
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
|
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
|
||||||
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
|
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
|
||||||
|
|||||||
@@ -704,11 +704,11 @@ scrcpy --disable-screensaver
|
|||||||
|
|
||||||
#### 双指缩放
|
#### 双指缩放
|
||||||
|
|
||||||
模拟“双指缩放”:<kbd>Ctrl</kbd>+_按住并移动鼠标_。
|
模拟“双指缩放”:<kbd>Ctrl</kbd>+_按下并拖动鼠标_。
|
||||||
|
|
||||||
更准确的说,在按住鼠标左键时按住 <kbd>Ctrl</kbd>。直到松开鼠标左键,所有鼠标移动将以屏幕中心为原点,缩放或旋转内容 (如果应用支持)。
|
在按住 <kbd>Ctrl</kbd> 时按下鼠标左键,直到松开鼠标左键前,移动鼠标会使屏幕内容相对于屏幕中心进行缩放或旋转 (如果应用支持)。
|
||||||
|
|
||||||
实际上,_scrcpy_ 会在关于屏幕中心对称的位置上用“虚拟手指”发出触摸事件。
|
具体来说,_scrcpy_ 会在鼠标位置,以及鼠标以屏幕中心镜像的位置分别生成触摸事件。
|
||||||
|
|
||||||
#### 物理键盘模拟 (HID)
|
#### 物理键盘模拟 (HID)
|
||||||
|
|
||||||
|
|||||||
121
app/data/bash-completion/scrcpy
Normal file
121
app/data/bash-completion/scrcpy
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
_scrcpy() {
|
||||||
|
local cur prev words cword
|
||||||
|
local opts="
|
||||||
|
--always-on-top
|
||||||
|
-b --bit-rate=
|
||||||
|
--codec-options=
|
||||||
|
--crop=
|
||||||
|
-d --select-usb
|
||||||
|
--disable-screensaver
|
||||||
|
--display=
|
||||||
|
--display-buffer=
|
||||||
|
-e --select-tcpip
|
||||||
|
--encoder=
|
||||||
|
--force-adb-forward
|
||||||
|
--forward-all-clicks
|
||||||
|
-f --fullscreen
|
||||||
|
-K --hid-keyboard
|
||||||
|
-h --help
|
||||||
|
--legacy-paste
|
||||||
|
--lock-video-orientation
|
||||||
|
--lock-video-orientation=
|
||||||
|
--max-fps=
|
||||||
|
-M --hid-mouse
|
||||||
|
-m --max-size=
|
||||||
|
--no-cleanup
|
||||||
|
--no-clipboard-on-error
|
||||||
|
--no-downsize-on-error
|
||||||
|
-n --no-control
|
||||||
|
-N --no-display
|
||||||
|
--no-key-repeat
|
||||||
|
--no-mipmaps
|
||||||
|
--otg
|
||||||
|
-p --port=
|
||||||
|
--power-off-on-close
|
||||||
|
--prefer-text
|
||||||
|
--print-fps
|
||||||
|
--push-target=
|
||||||
|
--raw-key-events
|
||||||
|
-r --record=
|
||||||
|
--record-format=
|
||||||
|
--render-driver=
|
||||||
|
--rotation=
|
||||||
|
-s --serial=
|
||||||
|
--shortcut-mod=
|
||||||
|
-S --turn-screen-off
|
||||||
|
-t --show-touches
|
||||||
|
--tcpip
|
||||||
|
--tcpip=
|
||||||
|
--tunnel-host=
|
||||||
|
--tunnel-port=
|
||||||
|
--v4l2-buffer=
|
||||||
|
--v4l2-sink=
|
||||||
|
-V --verbosity=
|
||||||
|
-v --version
|
||||||
|
-w --stay-awake
|
||||||
|
--window-borderless
|
||||||
|
--window-title=
|
||||||
|
--window-x=
|
||||||
|
--window-y=
|
||||||
|
--window-width=
|
||||||
|
--window-height="
|
||||||
|
|
||||||
|
_init_completion -s || return
|
||||||
|
|
||||||
|
case "$prev" in
|
||||||
|
--lock-video-orientation)
|
||||||
|
COMPREPLY=($(compgen -W 'unlocked initial 0 1 2 3' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
-r|--record)
|
||||||
|
COMPREPLY=($(compgen -f -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
--record-format)
|
||||||
|
COMPREPLY=($(compgen -W 'mkv mp4' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
--render-driver)
|
||||||
|
COMPREPLY=($(compgen -W 'direct3d opengl opengles2 opengles metal software' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
--rotation)
|
||||||
|
COMPREPLY=($(compgen -W '0 1 2 3' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
--shortcut-mod)
|
||||||
|
# Only auto-complete a single key
|
||||||
|
COMPREPLY=($(compgen -W 'lctrl rctrl lalt ralt lsuper rsuper' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
-V|--verbosity)
|
||||||
|
COMPREPLY=($(compgen -W 'verbose debug info warn error' -- "$cur"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
-b|--bitrate \
|
||||||
|
|--codec-options \
|
||||||
|
|--crop \
|
||||||
|
|--display \
|
||||||
|
|--display-buffer \
|
||||||
|
|--encoder \
|
||||||
|
|--max-fps \
|
||||||
|
|-m|--max-size \
|
||||||
|
|-p|--port \
|
||||||
|
|--push-target \
|
||||||
|
|-s|--serial \
|
||||||
|
|--tunnel-host \
|
||||||
|
|--tunnel-port \
|
||||||
|
|--v4l2-buffer \
|
||||||
|
|--v4l2-sink \
|
||||||
|
|--tcpip \
|
||||||
|
|--window-*)
|
||||||
|
# Option accepting an argument, but nothing to auto-complete
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
||||||
|
[[ $COMPREPLY == *= ]] && compopt -o nospace
|
||||||
|
}
|
||||||
|
|
||||||
|
complete -F _scrcpy scrcpy
|
||||||
|
Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 8.6 KiB |
|
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 6.4 KiB |
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
69
app/data/zsh-completion/_scrcpy
Normal file
69
app/data/zsh-completion/_scrcpy
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#compdef -N scrcpy -N scrcpy.exe
|
||||||
|
#
|
||||||
|
# name: scrcpy
|
||||||
|
# auth: hltdev [hltdev8642@gmail.com]
|
||||||
|
# desc: completion file for scrcpy (all OSes)
|
||||||
|
#
|
||||||
|
|
||||||
|
local arguments
|
||||||
|
|
||||||
|
arguments=(
|
||||||
|
'--always-on-top[Make scrcpy window always on top \(above other windows\)]'
|
||||||
|
{-b,--bit-rate=}'[Encode the video at the given bit-rate]'
|
||||||
|
'--codec-options=[Set a list of comma-separated key\:type=value options for the device encoder]'
|
||||||
|
'--crop=[\[width\:height\:x\:y\] Crop the device screen on the server]'
|
||||||
|
{-d,--select-usb}'[Use USB device]'
|
||||||
|
'--disable-screensaver[Disable screensaver while scrcpy is running]'
|
||||||
|
'--display=[Specify the display id to mirror]'
|
||||||
|
'--display-buffer=[Add a buffering delay \(in milliseconds\) before displaying]'
|
||||||
|
{-e,--select-tcpip}'[Use TCP/IP device]'
|
||||||
|
'--encoder=[Use a specific MediaCodec encoder \(must be a H.264 encoder\)]'
|
||||||
|
'--force-adb-forward[Do not attempt to use \"adb reverse\" to connect to the device]'
|
||||||
|
'--forward-all-clicks[Forward clicks to device]'
|
||||||
|
{-f,--fullscreen}'[Start in fullscreen]'
|
||||||
|
{-K,--hid-keyboard}'[Simulate a physical keyboard by using HID over AOAv2]'
|
||||||
|
{-h,--help}'[Print the help]'
|
||||||
|
'--legacy-paste[Inject computer clipboard text as a sequence of key events on Ctrl+v]'
|
||||||
|
'--lock-video-orientation=[Lock video orientation]:orientation:(unlocked initial 0 1 2 3)'
|
||||||
|
'--max-fps=[Limit the frame rate of screen capture]'
|
||||||
|
{-M,--hid-mouse}'[Simulate a physical mouse by using HID over AOAv2]'
|
||||||
|
{-m,--max-size=}'[Limit both the width and height of the video to value]'
|
||||||
|
'--no-cleanup[Disable device cleanup actions on exit]'
|
||||||
|
'--no-clipboard-autosync[Disable automatic clipboard synchronization]'
|
||||||
|
'--no-downsize-on-error[Disable lowering definition on MediaCodec error]'
|
||||||
|
{-n,--no-control}'[Disable device control \(mirror the device in read only\)]'
|
||||||
|
{-N,--no-display}'[Do not display device \(during screen recording or when V4L2 sink is enabled\)]'
|
||||||
|
'--no-key-repeat[Do not forward repeated key events when a key is held down]'
|
||||||
|
'--no-mipmaps[Disable the generation of mipmaps]'
|
||||||
|
'--otg[Run in OTG mode \(simulating physical keyboard and mouse\)]'
|
||||||
|
{-p,--port=}'[\[port\[\:port\]\] Set the TCP port \(range\) used by the client to listen]'
|
||||||
|
'--power-off-on-close[Turn the device screen off when closing scrcpy]'
|
||||||
|
'--prefer-text[Inject alpha characters and space as text events instead of key events]'
|
||||||
|
'--print-fps[Start FPS counter, to print frame logs to the console]'
|
||||||
|
'--push-target=[Set the target directory for pushing files to the device by drag and drop]'
|
||||||
|
'--raw-key-events[Inject key events for all input keys, and ignore text events]'
|
||||||
|
{-r,--record=}'[Record screen to file]:record file:_files'
|
||||||
|
'--record-format=[Force recording format]:format:(mp4 mkv)'
|
||||||
|
'--render-driver=[Request SDL to use the given render driver]:driver name:(direct3d opengl opengles2 opengles metal software)'
|
||||||
|
'--rotation=[Set the initial display rotation]:rotation values:(0 1 2 3)'
|
||||||
|
{-s,--serial=}'[The device serial number \(mandatory for multiple devices only\)]'
|
||||||
|
'--shortcut-mod=[\[key1,key2+key3,...\] Specify the modifiers to use for scrcpy shortcuts]:shortcut mod:(lctrl rctrl lalt ralt lsuper rsuper)'
|
||||||
|
{-S,--turn-screen-off}'[Turn the device screen off immediately]'
|
||||||
|
{-t,--show-touches}'[Show physical touches]'
|
||||||
|
'--tcpip[\(optional \[ip\:port\]\) Configure and connect the device over TCP/IP]'
|
||||||
|
'--tunnel-host=[Set the IP address of the adb tunnel to reach the scrcpy server]'
|
||||||
|
'--tunnel-port=[Set the TCP port of the adb tunnel to reach the scrcpy server]'
|
||||||
|
'--v4l2-buffer=[Add a buffering delay \(in milliseconds\) before pushing frames]'
|
||||||
|
'--v4l2-sink=[\[\/dev\/videoN\] Output to v4l2loopback device]'
|
||||||
|
{-V,--verbosity=}'[Set the log level]:verbosity:(verbose debug info warn error)'
|
||||||
|
{-v,--version}'[Print the version of scrcpy]'
|
||||||
|
{-w,--stay-awake}'[Keep the device on while scrcpy is running, when the device is plugged in]'
|
||||||
|
'--window-borderless[Disable window decorations \(display borderless window\)]'
|
||||||
|
'--window-title=[Set a custom window title]'
|
||||||
|
'--window-x=[Set the initial window horizontal position]'
|
||||||
|
'--window-y=[Set the initial window vertical position]'
|
||||||
|
'--window-width=[Set the initial window width]'
|
||||||
|
'--window-height=[Set the initial window height]'
|
||||||
|
)
|
||||||
|
|
||||||
|
_arguments -s $arguments
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
src = [
|
src = [
|
||||||
'src/main.c',
|
'src/main.c',
|
||||||
'src/adb.c',
|
'src/adb/adb.c',
|
||||||
'src/adb_parser.c',
|
'src/adb/adb_device.c',
|
||||||
'src/adb_tunnel.c',
|
'src/adb/adb_parser.c',
|
||||||
|
'src/adb/adb_tunnel.c',
|
||||||
'src/cli.c',
|
'src/cli.c',
|
||||||
'src/clock.c',
|
'src/clock.c',
|
||||||
'src/compat.c',
|
'src/compat.c',
|
||||||
'src/control_msg.c',
|
'src/control_msg.c',
|
||||||
'src/controller.c',
|
'src/controller.c',
|
||||||
'src/decoder.c',
|
'src/decoder.c',
|
||||||
|
'src/demuxer.c',
|
||||||
'src/device_msg.c',
|
'src/device_msg.c',
|
||||||
'src/icon.c',
|
'src/icon.c',
|
||||||
'src/file_pusher.c',
|
'src/file_pusher.c',
|
||||||
@@ -24,7 +26,7 @@ src = [
|
|||||||
'src/scrcpy.c',
|
'src/scrcpy.c',
|
||||||
'src/screen.c',
|
'src/screen.c',
|
||||||
'src/server.c',
|
'src/server.c',
|
||||||
'src/stream.c',
|
'src/version.c',
|
||||||
'src/video_buffer.c',
|
'src/video_buffer.c',
|
||||||
'src/util/acksync.c',
|
'src/util/acksync.c',
|
||||||
'src/util/file.c',
|
'src/util/file.c',
|
||||||
@@ -67,12 +69,12 @@ else
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
v4l2_support = host_machine.system() == 'linux'
|
v4l2_support = get_option('v4l2') and host_machine.system() == 'linux'
|
||||||
if v4l2_support
|
if v4l2_support
|
||||||
src += [ 'src/v4l2_sink.c' ]
|
src += [ 'src/v4l2_sink.c' ]
|
||||||
endif
|
endif
|
||||||
|
|
||||||
usb_support = host_machine.system() == 'linux'
|
usb_support = get_option('usb')
|
||||||
if usb_support
|
if usb_support
|
||||||
src += [
|
src += [
|
||||||
'src/usb/aoa_hid.c',
|
'src/usb/aoa_hid.c',
|
||||||
@@ -109,9 +111,9 @@ if not crossbuild_windows
|
|||||||
else
|
else
|
||||||
# cross-compile mingw32 build (from Linux to Windows)
|
# cross-compile mingw32 build (from Linux to Windows)
|
||||||
prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2')
|
prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2')
|
||||||
sdl2_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/data/' + prebuilt_sdl2 + '/bin'
|
sdl2_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_sdl2 + '/bin'
|
||||||
sdl2_lib_dir = meson.current_source_dir() + '/../prebuilt-deps/data/' + prebuilt_sdl2 + '/lib'
|
sdl2_lib_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_sdl2 + '/lib'
|
||||||
sdl2_include_dir = '../prebuilt-deps/data/' + prebuilt_sdl2 + '/include'
|
sdl2_include_dir = 'prebuilt-deps/data/' + prebuilt_sdl2 + '/include'
|
||||||
|
|
||||||
sdl2 = declare_dependency(
|
sdl2 = declare_dependency(
|
||||||
dependencies: [
|
dependencies: [
|
||||||
@@ -122,8 +124,8 @@ else
|
|||||||
)
|
)
|
||||||
|
|
||||||
prebuilt_ffmpeg = meson.get_cross_property('prebuilt_ffmpeg')
|
prebuilt_ffmpeg = meson.get_cross_property('prebuilt_ffmpeg')
|
||||||
ffmpeg_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/data/' + prebuilt_ffmpeg + '/bin'
|
ffmpeg_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_ffmpeg + '/bin'
|
||||||
ffmpeg_include_dir = '../prebuilt-deps/data/' + prebuilt_ffmpeg + '/include'
|
ffmpeg_include_dir = 'prebuilt-deps/data/' + prebuilt_ffmpeg + '/include'
|
||||||
|
|
||||||
# ffmpeg versions are different for win32 and win64 builds
|
# ffmpeg versions are different for win32 and win64 builds
|
||||||
ffmpeg_avcodec = meson.get_cross_property('ffmpeg_avcodec')
|
ffmpeg_avcodec = meson.get_cross_property('ffmpeg_avcodec')
|
||||||
@@ -139,9 +141,22 @@ else
|
|||||||
include_directories: include_directories(ffmpeg_include_dir)
|
include_directories: include_directories(ffmpeg_include_dir)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
prebuilt_libusb = meson.get_cross_property('prebuilt_libusb')
|
||||||
|
prebuilt_libusb_root = meson.get_cross_property('prebuilt_libusb_root')
|
||||||
|
libusb_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_libusb + '/dll'
|
||||||
|
libusb_include_dir = 'prebuilt-deps/data/' + prebuilt_libusb_root + '/include'
|
||||||
|
|
||||||
|
libusb = declare_dependency(
|
||||||
|
dependencies: [
|
||||||
|
cc.find_library('libusb-1.0', dirs: libusb_bin_dir),
|
||||||
|
],
|
||||||
|
include_directories: include_directories(libusb_include_dir)
|
||||||
|
)
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
ffmpeg,
|
ffmpeg,
|
||||||
sdl2,
|
sdl2,
|
||||||
|
libusb,
|
||||||
cc.find_library('mingw32')
|
cc.find_library('mingw32')
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -209,9 +224,13 @@ executable('scrcpy', src,
|
|||||||
c_args: [])
|
c_args: [])
|
||||||
|
|
||||||
install_man('scrcpy.1')
|
install_man('scrcpy.1')
|
||||||
install_data('../data/icon.png',
|
install_data('data/icon.png',
|
||||||
rename: 'scrcpy.png',
|
rename: 'scrcpy.png',
|
||||||
install_dir: 'share/icons/hicolor/256x256/apps')
|
install_dir: 'share/icons/hicolor/256x256/apps')
|
||||||
|
install_data('data/zsh-completion/_scrcpy',
|
||||||
|
install_dir: 'share/zsh/site-functions')
|
||||||
|
install_data('data/bash-completion/scrcpy',
|
||||||
|
install_dir: 'share/bash-completion/completions')
|
||||||
|
|
||||||
|
|
||||||
### TESTS
|
### TESTS
|
||||||
@@ -221,7 +240,8 @@ if get_option('buildtype') == 'debug'
|
|||||||
tests = [
|
tests = [
|
||||||
['test_adb_parser', [
|
['test_adb_parser', [
|
||||||
'tests/test_adb_parser.c',
|
'tests/test_adb_parser.c',
|
||||||
'src/adb_parser.c',
|
'src/adb/adb_device.c',
|
||||||
|
'src/adb/adb_parser.c',
|
||||||
'src/util/str.c',
|
'src/util/str.c',
|
||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
]],
|
]],
|
||||||
@@ -266,6 +286,9 @@ if get_option('buildtype') == 'debug'
|
|||||||
'src/util/str.c',
|
'src/util/str.c',
|
||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
]],
|
]],
|
||||||
|
['test_vector', [
|
||||||
|
'tests/test_vector.c',
|
||||||
|
]],
|
||||||
]
|
]
|
||||||
|
|
||||||
foreach t : tests
|
foreach t : tests
|
||||||
|
|||||||
28
app/prebuilt-deps/prepare-libusb.sh
Executable file
28
app/prebuilt-deps/prepare-libusb.sh
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
DIR=$(dirname ${BASH_SOURCE[0]})
|
||||||
|
cd "$DIR"
|
||||||
|
. common
|
||||||
|
mkdir -p "$PREBUILT_DATA_DIR"
|
||||||
|
cd "$PREBUILT_DATA_DIR"
|
||||||
|
|
||||||
|
DEP_DIR=libusb-1.0.25
|
||||||
|
|
||||||
|
FILENAME=libusb-1.0.25.7z
|
||||||
|
SHA256SUM=3d1c98416f454026034b2b5d67f8a294053898cb70a8b489874e75b136c6674d
|
||||||
|
|
||||||
|
if [[ -d "$DEP_DIR" ]]
|
||||||
|
then
|
||||||
|
echo "$DEP_DIR" found
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
get_file "https://github.com/libusb/libusb/releases/download/v1.0.25/$FILENAME" "$FILENAME" "$SHA256SUM"
|
||||||
|
|
||||||
|
mkdir "$DEP_DIR"
|
||||||
|
cd "$DEP_DIR"
|
||||||
|
|
||||||
|
7z x "../$FILENAME" \
|
||||||
|
MinGW32/dll/libusb-1.0.dll \
|
||||||
|
MinGW64/dll/libusb-1.0.dll \
|
||||||
|
include /
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <winuser.h>
|
#include <winuser.h>
|
||||||
|
|
||||||
0 ICON "../data/icon.ico"
|
0 ICON "data/icon.ico"
|
||||||
1 RT_MANIFEST "scrcpy-windows.manifest"
|
1 RT_MANIFEST "scrcpy-windows.manifest"
|
||||||
2 VERSIONINFO
|
2 VERSIONINFO
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -13,7 +13,7 @@ BEGIN
|
|||||||
VALUE "LegalCopyright", "Romain Vimont, Genymobile"
|
VALUE "LegalCopyright", "Romain Vimont, Genymobile"
|
||||||
VALUE "OriginalFilename", "scrcpy.exe"
|
VALUE "OriginalFilename", "scrcpy.exe"
|
||||||
VALUE "ProductName", "scrcpy"
|
VALUE "ProductName", "scrcpy"
|
||||||
VALUE "ProductVersion", "1.22"
|
VALUE "ProductVersion", "1.23"
|
||||||
END
|
END
|
||||||
END
|
END
|
||||||
BLOCK "VarFileInfo"
|
BLOCK "VarFileInfo"
|
||||||
|
|||||||
28
app/scrcpy.1
28
app/scrcpy.1
@@ -43,6 +43,12 @@ The values are expressed in the device natural orientation (typically, portrait
|
|||||||
.B \-\-max\-size
|
.B \-\-max\-size
|
||||||
value is computed on the cropped size.
|
value is computed on the cropped size.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-d, \-\-select\-usb
|
||||||
|
Use USB device (if there is exactly one, like adb -d).
|
||||||
|
|
||||||
|
Also see \fB\-e\fR (\fB\-\-select\-tcpip\fR).
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-\-disable-screensaver"
|
.BI "\-\-disable-screensaver"
|
||||||
Disable screensaver while scrcpy is running.
|
Disable screensaver while scrcpy is running.
|
||||||
@@ -62,6 +68,12 @@ Add a buffering delay (in milliseconds) before displaying. This increases latenc
|
|||||||
|
|
||||||
Default is 0 (no buffering).
|
Default is 0 (no buffering).
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-e, \-\-select\-tcpip
|
||||||
|
Use TCP/IP device (if there is exactly one, like adb -e).
|
||||||
|
|
||||||
|
Also see \fB\-d\fR (\fB\-\-select\-usb\fR).
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-\-encoder " name
|
.BI "\-\-encoder " name
|
||||||
Use a specific MediaCodec encoder (must be a H.264 encoder).
|
Use a specific MediaCodec encoder (must be a H.264 encoder).
|
||||||
@@ -88,7 +100,7 @@ Simulate a physical keyboard by using HID over AOAv2.
|
|||||||
|
|
||||||
This provides a better experience for IME users, and allows to generate non-ASCII characters, contrary to the default injection method.
|
This provides a better experience for IME users, and allows to generate non-ASCII characters, contrary to the default injection method.
|
||||||
|
|
||||||
It may only work over USB, and is currently only supported on Linux.
|
It may only work over USB.
|
||||||
|
|
||||||
The keyboard layout must be configured (once and for all) on the device, via Settings -> System -> Languages and input -> Physical keyboard. This settings page can be started directly:
|
The keyboard layout must be configured (once and for all) on the device, via Settings -> System -> Languages and input -> Physical keyboard. This settings page can be started directly:
|
||||||
|
|
||||||
@@ -130,10 +142,16 @@ In this mode, the computer mouse is captured to control the device directly (rel
|
|||||||
|
|
||||||
LAlt, LSuper or RSuper toggle the capture mode, to give control of the mouse back to the computer.
|
LAlt, LSuper or RSuper toggle the capture mode, to give control of the mouse back to the computer.
|
||||||
|
|
||||||
It may only work over USB, and is currently only supported on Linux.
|
It may only work over USB.
|
||||||
|
|
||||||
Also see \fB\-\-hid\-keyboard\fR.
|
Also see \fB\-\-hid\-keyboard\fR.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-\-no\-cleanup
|
||||||
|
By default, scrcpy removes the server binary from the device and restores the device state (show touches, stay awake and power mode) on exit.
|
||||||
|
|
||||||
|
This option disables this cleanup.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B \-\-no\-clipboard\-autosync
|
.B \-\-no\-clipboard\-autosync
|
||||||
By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.
|
By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.
|
||||||
@@ -172,7 +190,7 @@ LAlt, LSuper or RSuper toggle the mouse capture mode, to give control of the mou
|
|||||||
|
|
||||||
If any of \fB\-\-hid\-keyboard\fR or \fB\-\-hid\-mouse\fR is set, only enable keyboard or mouse respectively, otherwise enable both.
|
If any of \fB\-\-hid\-keyboard\fR or \fB\-\-hid\-mouse\fR is set, only enable keyboard or mouse respectively, otherwise enable both.
|
||||||
|
|
||||||
It may only work over USB, and is currently only supported on Linux.
|
It may only work over USB.
|
||||||
|
|
||||||
See \fB\-\-hid\-keyboard\fR and \fB\-\-hid\-mouse\fR.
|
See \fB\-\-hid\-keyboard\fR and \fB\-\-hid\-mouse\fR.
|
||||||
|
|
||||||
@@ -193,6 +211,10 @@ Inject alpha characters and space as text events instead of key events.
|
|||||||
This avoids issues when combining multiple keys to enter special characters,
|
This avoids issues when combining multiple keys to enter special characters,
|
||||||
but breaks the expected behavior of alpha keys in games (typically WASD).
|
but breaks the expected behavior of alpha keys in games (typically WASD).
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B "\-\-print\-fps
|
||||||
|
Start FPS counter, to print framerate logs to the console. It can be started or stopped at any time with MOD+i.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-\-push\-target " path
|
.BI "\-\-push\-target " path
|
||||||
Set the target directory for pushing files to the device by drag & drop. It is passed as\-is to "adb push".
|
Set the target directory for pushing files to the device by drag & drop. It is passed as\-is to "adb push".
|
||||||
|
|||||||
471
app/src/adb.c
471
app/src/adb.c
@@ -1,471 +0,0 @@
|
|||||||
#include "adb.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "adb_parser.h"
|
|
||||||
#include "util/file.h"
|
|
||||||
#include "util/log.h"
|
|
||||||
#include "util/process_intr.h"
|
|
||||||
#include "util/str.h"
|
|
||||||
|
|
||||||
static const char *adb_command;
|
|
||||||
|
|
||||||
static inline const char *
|
|
||||||
get_adb_command(void) {
|
|
||||||
if (!adb_command) {
|
|
||||||
adb_command = getenv("ADB");
|
|
||||||
if (!adb_command)
|
|
||||||
adb_command = "adb";
|
|
||||||
}
|
|
||||||
return adb_command;
|
|
||||||
}
|
|
||||||
|
|
||||||
// serialize argv to string "[arg1], [arg2], [arg3]"
|
|
||||||
static size_t
|
|
||||||
argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
|
|
||||||
size_t idx = 0;
|
|
||||||
bool first = true;
|
|
||||||
while (*argv) {
|
|
||||||
const char *arg = *argv;
|
|
||||||
size_t len = strlen(arg);
|
|
||||||
// count space for "[], ...\0"
|
|
||||||
if (idx + len + 8 >= bufsize) {
|
|
||||||
// not enough space, truncate
|
|
||||||
assert(idx < bufsize - 4);
|
|
||||||
memcpy(&buf[idx], "...", 3);
|
|
||||||
idx += 3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (first) {
|
|
||||||
first = false;
|
|
||||||
} else {
|
|
||||||
buf[idx++] = ',';
|
|
||||||
buf[idx++] = ' ';
|
|
||||||
}
|
|
||||||
buf[idx++] = '[';
|
|
||||||
memcpy(&buf[idx], arg, len);
|
|
||||||
idx += len;
|
|
||||||
buf[idx++] = ']';
|
|
||||||
argv++;
|
|
||||||
}
|
|
||||||
assert(idx < bufsize);
|
|
||||||
buf[idx] = '\0';
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
show_adb_installation_msg() {
|
|
||||||
#ifndef __WINDOWS__
|
|
||||||
static const struct {
|
|
||||||
const char *binary;
|
|
||||||
const char *command;
|
|
||||||
} pkg_managers[] = {
|
|
||||||
{"apt", "apt install adb"},
|
|
||||||
{"apt-get", "apt-get install adb"},
|
|
||||||
{"brew", "brew cask install android-platform-tools"},
|
|
||||||
{"dnf", "dnf install android-tools"},
|
|
||||||
{"emerge", "emerge dev-util/android-tools"},
|
|
||||||
{"pacman", "pacman -S android-tools"},
|
|
||||||
};
|
|
||||||
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
|
|
||||||
if (sc_file_executable_exists(pkg_managers[i].binary)) {
|
|
||||||
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
LOGI("You may download and install 'adb' from "
|
|
||||||
"https://developer.android.com/studio/releases/platform-tools");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
|
||||||
#define MAX_COMMAND_STRING_LEN 1024
|
|
||||||
char *buf = malloc(MAX_COMMAND_STRING_LEN);
|
|
||||||
if (!buf) {
|
|
||||||
LOG_OOM();
|
|
||||||
LOGE("Failed to execute");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (err) {
|
|
||||||
case SC_PROCESS_ERROR_GENERIC:
|
|
||||||
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
|
||||||
LOGE("Failed to execute: %s", buf);
|
|
||||||
break;
|
|
||||||
case SC_PROCESS_ERROR_MISSING_BINARY:
|
|
||||||
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
|
||||||
LOGE("Command not found: %s", buf);
|
|
||||||
LOGE("(make 'adb' accessible from your PATH or define its full"
|
|
||||||
"path in the ADB environment variable)");
|
|
||||||
show_adb_installation_msg();
|
|
||||||
break;
|
|
||||||
case SC_PROCESS_SUCCESS:
|
|
||||||
// do nothing
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
process_check_success_internal(sc_pid pid, const char *name, bool close,
|
|
||||||
unsigned flags) {
|
|
||||||
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
|
|
||||||
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
if (log_errors) {
|
|
||||||
LOGE("Could not execute \"%s\"", name);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
sc_exit_code exit_code = sc_process_wait(pid, close);
|
|
||||||
if (exit_code) {
|
|
||||||
if (log_errors) {
|
|
||||||
if (exit_code != SC_EXIT_CODE_NONE) {
|
|
||||||
LOGE("\"%s\" returned with value %" SC_PRIexitcode, name,
|
|
||||||
exit_code);
|
|
||||||
} else {
|
|
||||||
LOGE("\"%s\" exited unexpectedly", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name,
|
|
||||||
unsigned flags) {
|
|
||||||
if (!sc_intr_set_process(intr, pid)) {
|
|
||||||
// Already interrupted
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always pass close=false, interrupting would be racy otherwise
|
|
||||||
bool ret = process_check_success_internal(pid, name, false, flags);
|
|
||||||
|
|
||||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
|
||||||
|
|
||||||
// Close separately
|
|
||||||
sc_process_close(pid);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char **
|
|
||||||
adb_create_argv(const char *serial, const char *const adb_cmd[], size_t len) {
|
|
||||||
const char **argv = malloc((len + 4) * sizeof(*argv));
|
|
||||||
if (!argv) {
|
|
||||||
LOG_OOM();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
argv[0] = get_adb_command();
|
|
||||||
int i;
|
|
||||||
if (serial) {
|
|
||||||
argv[1] = "-s";
|
|
||||||
argv[2] = serial;
|
|
||||||
i = 3;
|
|
||||||
} else {
|
|
||||||
i = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
|
||||||
argv[len + i] = NULL;
|
|
||||||
return argv;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sc_pid
|
|
||||||
adb_execute_p(const char *serial, const char *const adb_cmd[], size_t len,
|
|
||||||
unsigned flags, sc_pipe *pout) {
|
|
||||||
const char **argv = adb_create_argv(serial, adb_cmd, len);
|
|
||||||
if (!argv) {
|
|
||||||
return SC_PROCESS_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned process_flags = 0;
|
|
||||||
if (flags & SC_ADB_NO_STDOUT) {
|
|
||||||
process_flags |= SC_PROCESS_NO_STDOUT;
|
|
||||||
}
|
|
||||||
if (flags & SC_ADB_NO_STDERR) {
|
|
||||||
process_flags |= SC_PROCESS_NO_STDERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
sc_pid pid;
|
|
||||||
enum sc_process_result r =
|
|
||||||
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
|
|
||||||
if (r != SC_PROCESS_SUCCESS) {
|
|
||||||
// If the execution itself failed (not the command exit code), log the
|
|
||||||
// error in all cases
|
|
||||||
show_adb_err_msg(r, argv);
|
|
||||||
pid = SC_PROCESS_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(argv);
|
|
||||||
return pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
sc_pid
|
|
||||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
|
||||||
unsigned flags) {
|
|
||||||
return adb_execute_p(serial, adb_cmd, len, flags, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
|
||||||
const char *device_socket_name, unsigned flags) {
|
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
||||||
const char *const adb_cmd[] = {"forward", local, remote};
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb forward", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
|
||||||
uint16_t local_port, unsigned flags) {
|
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
||||||
const char *const adb_cmd[] = {"forward", "--remove", local};
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
|
||||||
const char *device_socket_name, uint16_t local_port,
|
|
||||||
unsigned flags) {
|
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
||||||
const char *const adb_cmd[] = {"reverse", remote, local};
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb reverse", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
|
||||||
const char *device_socket_name, unsigned flags) {
|
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
||||||
const char *const adb_cmd[] = {"reverse", "--remove", remote};
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
|
||||||
const char *remote, unsigned flags) {
|
|
||||||
#ifdef __WINDOWS__
|
|
||||||
// Windows will parse the string, so the paths must be quoted
|
|
||||||
// (see sys/win/command.c)
|
|
||||||
local = sc_str_quote(local);
|
|
||||||
if (!local) {
|
|
||||||
return SC_PROCESS_NONE;
|
|
||||||
}
|
|
||||||
remote = sc_str_quote(remote);
|
|
||||||
if (!remote) {
|
|
||||||
free((void *) local);
|
|
||||||
return SC_PROCESS_NONE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const char *const adb_cmd[] = {"push", local, remote};
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
|
||||||
free((void *) remote);
|
|
||||||
free((void *) local);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return process_check_success_intr(intr, pid, "adb push", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
|
||||||
unsigned flags) {
|
|
||||||
#ifdef __WINDOWS__
|
|
||||||
// Windows will parse the string, so the local name must be quoted
|
|
||||||
// (see sys/win/command.c)
|
|
||||||
local = sc_str_quote(local);
|
|
||||||
if (!local) {
|
|
||||||
return SC_PROCESS_NONE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const char *const adb_cmd[] = {"install", "-r", local};
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
|
||||||
free((void *) local);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return process_check_success_intr(intr, pid, "adb install", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
|
||||||
unsigned flags) {
|
|
||||||
char port_string[5 + 1];
|
|
||||||
sprintf(port_string, "%" PRIu16, port);
|
|
||||||
const char *const adb_cmd[] = {"tcpip", port_string};
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb tcpip", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
|
||||||
const char *const adb_cmd[] = {"connect", ip_port};
|
|
||||||
|
|
||||||
sc_pipe pout;
|
|
||||||
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
LOGE("Could not execute \"adb connect\"");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// "adb connect" always returns successfully (with exit code 0), even in
|
|
||||||
// case of failure. As a workaround, check if its output starts with
|
|
||||||
// "connected".
|
|
||||||
char buf[128];
|
|
||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
||||||
sc_pipe_close(pout);
|
|
||||||
|
|
||||||
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
|
||||||
if (!ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
|
||||||
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
|
||||||
// "adb connect" also prints errors to stdout. Since we capture it,
|
|
||||||
// re-print the error to stderr.
|
|
||||||
sc_str_truncate(buf, r, "\r\n");
|
|
||||||
fprintf(stderr, "%s\n", buf);
|
|
||||||
}
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
|
||||||
const char *const adb_cmd[] = {"disconnect", ip_port};
|
|
||||||
size_t len = ip_port ? ARRAY_LEN(adb_cmd)
|
|
||||||
: ARRAY_LEN(adb_cmd) - 1;
|
|
||||||
|
|
||||||
sc_pid pid = adb_execute(NULL, adb_cmd, len, flags);
|
|
||||||
return process_check_success_intr(intr, pid, "adb disconnect", flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
|
|
||||||
unsigned flags) {
|
|
||||||
const char *const adb_cmd[] = {"shell", "getprop", prop};
|
|
||||||
|
|
||||||
sc_pipe pout;
|
|
||||||
sc_pid pid =
|
|
||||||
adb_execute_p(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
LOGE("Could not execute \"adb getprop\"");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[128];
|
|
||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
||||||
sc_pipe_close(pout);
|
|
||||||
|
|
||||||
bool ok = process_check_success_intr(intr, pid, "adb getprop", flags);
|
|
||||||
if (!ok) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
sc_str_truncate(buf, r, " \r\n");
|
|
||||||
|
|
||||||
return strdup(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
adb_get_serialno(struct sc_intr *intr, unsigned flags) {
|
|
||||||
const char *const adb_cmd[] = {"get-serialno"};
|
|
||||||
|
|
||||||
sc_pipe pout;
|
|
||||||
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
LOGE("Could not execute \"adb get-serialno\"");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[128];
|
|
||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
||||||
sc_pipe_close(pout);
|
|
||||||
|
|
||||||
bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
|
|
||||||
if (!ok) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
sc_str_truncate(buf, r, " \r\n");
|
|
||||||
|
|
||||||
return strdup(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
|
|
||||||
const char *const cmd[] = {"shell", "ip", "route"};
|
|
||||||
|
|
||||||
sc_pipe pout;
|
|
||||||
sc_pid pid = adb_execute_p(serial, cmd, ARRAY_LEN(cmd), flags, &pout);
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
LOGD("Could not execute \"ip route\"");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// "adb shell ip route" output should contain only a few lines
|
|
||||||
char buf[1024];
|
|
||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
||||||
sc_pipe_close(pout);
|
|
||||||
|
|
||||||
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
|
|
||||||
if (!ok) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert((size_t) r <= sizeof(buf));
|
|
||||||
if (r == sizeof(buf) && buf[sizeof(buf) - 1] != '\0') {
|
|
||||||
// The implementation assumes that the output of "ip route" fits in the
|
|
||||||
// buffer in a single pass
|
|
||||||
LOGW("Result of \"ip route\" does not fit in 1Kb. "
|
|
||||||
"Please report an issue.\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sc_adb_parse_device_ip_from_output(buf, r);
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
#ifndef SC_ADB_H
|
|
||||||
#define SC_ADB_H
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
#include "util/intr.h"
|
|
||||||
|
|
||||||
#define SC_ADB_NO_STDOUT (1 << 0)
|
|
||||||
#define SC_ADB_NO_STDERR (1 << 1)
|
|
||||||
#define SC_ADB_NO_LOGERR (1 << 2)
|
|
||||||
|
|
||||||
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
|
|
||||||
|
|
||||||
sc_pid
|
|
||||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
|
||||||
unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
|
||||||
const char *device_socket_name, unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
|
||||||
uint16_t local_port, unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
|
||||||
const char *device_socket_name, uint16_t local_port,
|
|
||||||
unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
|
||||||
const char *device_socket_name, unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
|
||||||
const char *remote, unsigned flags);
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
|
||||||
unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute `adb tcpip <port>`
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
|
||||||
unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute `adb connect <ip_port>`
|
|
||||||
*
|
|
||||||
* `ip_port` may not be NULL.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute `adb disconnect [<ip_port>]`
|
|
||||||
*
|
|
||||||
* If `ip_port` is NULL, execute `adb disconnect`.
|
|
||||||
* Otherwise, execute `adb disconnect <ip_port>`.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute `adb getprop <prop>`
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
|
|
||||||
unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute `adb get-serialno`
|
|
||||||
*
|
|
||||||
* Return the result, to be freed by the caller, or NULL on error.
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
adb_get_serialno(struct sc_intr *intr, unsigned flags);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempt to retrieve the device IP
|
|
||||||
*
|
|
||||||
* Return the IP as a string of the form "xxx.xxx.xxx.xxx", to be freed by the
|
|
||||||
* caller, or NULL on error.
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
712
app/src/adb/adb.c
Normal file
712
app/src/adb/adb.c
Normal file
@@ -0,0 +1,712 @@
|
|||||||
|
#include "adb.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "adb_device.h"
|
||||||
|
#include "adb_parser.h"
|
||||||
|
#include "util/file.h"
|
||||||
|
#include "util/log.h"
|
||||||
|
#include "util/process_intr.h"
|
||||||
|
#include "util/str.h"
|
||||||
|
|
||||||
|
/* Convenience macro to expand:
|
||||||
|
*
|
||||||
|
* const char *const argv[] =
|
||||||
|
* SC_ADB_COMMAND("shell", "echo", "hello");
|
||||||
|
*
|
||||||
|
* to:
|
||||||
|
*
|
||||||
|
* const char *const argv[] =
|
||||||
|
* { sc_adb_get_executable(), "shell", "echo", "hello", NULL };
|
||||||
|
*/
|
||||||
|
#define SC_ADB_COMMAND(...) { sc_adb_get_executable(), __VA_ARGS__, NULL }
|
||||||
|
|
||||||
|
static const char *adb_executable;
|
||||||
|
|
||||||
|
const char *
|
||||||
|
sc_adb_get_executable(void) {
|
||||||
|
if (!adb_executable) {
|
||||||
|
adb_executable = getenv("ADB");
|
||||||
|
if (!adb_executable)
|
||||||
|
adb_executable = "adb";
|
||||||
|
}
|
||||||
|
return adb_executable;
|
||||||
|
}
|
||||||
|
|
||||||
|
// serialize argv to string "[arg1], [arg2], [arg3]"
|
||||||
|
static size_t
|
||||||
|
argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
|
||||||
|
size_t idx = 0;
|
||||||
|
bool first = true;
|
||||||
|
while (*argv) {
|
||||||
|
const char *arg = *argv;
|
||||||
|
size_t len = strlen(arg);
|
||||||
|
// count space for "[], ...\0"
|
||||||
|
if (idx + len + 8 >= bufsize) {
|
||||||
|
// not enough space, truncate
|
||||||
|
assert(idx < bufsize - 4);
|
||||||
|
memcpy(&buf[idx], "...", 3);
|
||||||
|
idx += 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
buf[idx++] = ',';
|
||||||
|
buf[idx++] = ' ';
|
||||||
|
}
|
||||||
|
buf[idx++] = '[';
|
||||||
|
memcpy(&buf[idx], arg, len);
|
||||||
|
idx += len;
|
||||||
|
buf[idx++] = ']';
|
||||||
|
argv++;
|
||||||
|
}
|
||||||
|
assert(idx < bufsize);
|
||||||
|
buf[idx] = '\0';
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
show_adb_installation_msg() {
|
||||||
|
#ifndef __WINDOWS__
|
||||||
|
static const struct {
|
||||||
|
const char *binary;
|
||||||
|
const char *command;
|
||||||
|
} pkg_managers[] = {
|
||||||
|
{"apt", "apt install adb"},
|
||||||
|
{"apt-get", "apt-get install adb"},
|
||||||
|
{"brew", "brew cask install android-platform-tools"},
|
||||||
|
{"dnf", "dnf install android-tools"},
|
||||||
|
{"emerge", "emerge dev-util/android-tools"},
|
||||||
|
{"pacman", "pacman -S android-tools"},
|
||||||
|
};
|
||||||
|
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
|
||||||
|
if (sc_file_executable_exists(pkg_managers[i].binary)) {
|
||||||
|
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
||||||
|
#define MAX_COMMAND_STRING_LEN 1024
|
||||||
|
char *buf = malloc(MAX_COMMAND_STRING_LEN);
|
||||||
|
if (!buf) {
|
||||||
|
LOG_OOM();
|
||||||
|
LOGE("Failed to execute");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (err) {
|
||||||
|
case SC_PROCESS_ERROR_GENERIC:
|
||||||
|
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
||||||
|
LOGE("Failed to execute: %s", buf);
|
||||||
|
break;
|
||||||
|
case SC_PROCESS_ERROR_MISSING_BINARY:
|
||||||
|
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
||||||
|
LOGE("Command not found: %s", buf);
|
||||||
|
LOGE("(make 'adb' accessible from your PATH or define its full"
|
||||||
|
"path in the ADB environment variable)");
|
||||||
|
show_adb_installation_msg();
|
||||||
|
break;
|
||||||
|
case SC_PROCESS_SUCCESS:
|
||||||
|
// do nothing
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
process_check_success_internal(sc_pid pid, const char *name, bool close,
|
||||||
|
unsigned flags) {
|
||||||
|
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
|
||||||
|
|
||||||
|
if (pid == SC_PROCESS_NONE) {
|
||||||
|
if (log_errors) {
|
||||||
|
LOGE("Could not execute \"%s\"", name);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
sc_exit_code exit_code = sc_process_wait(pid, close);
|
||||||
|
if (exit_code) {
|
||||||
|
if (log_errors) {
|
||||||
|
if (exit_code != SC_EXIT_CODE_NONE) {
|
||||||
|
LOGE("\"%s\" returned with value %" SC_PRIexitcode, name,
|
||||||
|
exit_code);
|
||||||
|
} else {
|
||||||
|
LOGE("\"%s\" exited unexpectedly", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name,
|
||||||
|
unsigned flags) {
|
||||||
|
if (intr && !sc_intr_set_process(intr, pid)) {
|
||||||
|
// Already interrupted
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always pass close=false, interrupting would be racy otherwise
|
||||||
|
bool ret = process_check_success_internal(pid, name, false, flags);
|
||||||
|
|
||||||
|
if (intr) {
|
||||||
|
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close separately
|
||||||
|
sc_process_close(pid);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static sc_pid
|
||||||
|
sc_adb_execute_p(const char *const argv[], unsigned flags, sc_pipe *pout) {
|
||||||
|
unsigned process_flags = 0;
|
||||||
|
if (flags & SC_ADB_NO_STDOUT) {
|
||||||
|
process_flags |= SC_PROCESS_NO_STDOUT;
|
||||||
|
}
|
||||||
|
if (flags & SC_ADB_NO_STDERR) {
|
||||||
|
process_flags |= SC_PROCESS_NO_STDERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_pid pid;
|
||||||
|
enum sc_process_result r =
|
||||||
|
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
|
||||||
|
if (r != SC_PROCESS_SUCCESS) {
|
||||||
|
// If the execution itself failed (not the command exit code), log the
|
||||||
|
// error in all cases
|
||||||
|
show_adb_err_msg(r, argv);
|
||||||
|
pid = SC_PROCESS_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_pid
|
||||||
|
sc_adb_execute(const char *const argv[], unsigned flags) {
|
||||||
|
return sc_adb_execute_p(argv, flags, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_start_server(struct sc_intr *intr, unsigned flags) {
|
||||||
|
const char *const argv[] = SC_ADB_COMMAND("start-server");
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb start-server", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_kill_server(struct sc_intr *intr, unsigned flags) {
|
||||||
|
const char *const argv[] = SC_ADB_COMMAND("kill-server");
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb kill-server", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||||
|
const char *device_socket_name, unsigned flags) {
|
||||||
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "forward", local, remote);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb forward", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
uint16_t local_port, unsigned flags) {
|
||||||
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "forward", "--remove", local);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_reverse(struct sc_intr *intr, const char *serial,
|
||||||
|
const char *device_socket_name, uint16_t local_port,
|
||||||
|
unsigned flags) {
|
||||||
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "reverse", remote, local);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb reverse", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
const char *device_socket_name, unsigned flags) {
|
||||||
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "reverse", "--remove", remote);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
const char *remote, unsigned flags) {
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
// Windows will parse the string, so the paths must be quoted
|
||||||
|
// (see sys/win/command.c)
|
||||||
|
local = sc_str_quote(local);
|
||||||
|
if (!local) {
|
||||||
|
return SC_PROCESS_NONE;
|
||||||
|
}
|
||||||
|
remote = sc_str_quote(remote);
|
||||||
|
if (!remote) {
|
||||||
|
free((void *) local);
|
||||||
|
return SC_PROCESS_NONE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "push", local, remote);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
free((void *) remote);
|
||||||
|
free((void *) local);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return process_check_success_intr(intr, pid, "adb push", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
unsigned flags) {
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
// Windows will parse the string, so the local name must be quoted
|
||||||
|
// (see sys/win/command.c)
|
||||||
|
local = sc_str_quote(local);
|
||||||
|
if (!local) {
|
||||||
|
return SC_PROCESS_NONE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "install", "-r", local);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
free((void *) local);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return process_check_success_intr(intr, pid, "adb install", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
||||||
|
unsigned flags) {
|
||||||
|
char port_string[5 + 1];
|
||||||
|
sprintf(port_string, "%" PRIu16, port);
|
||||||
|
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "tcpip", port_string);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb tcpip", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
|
const char *const argv[] = SC_ADB_COMMAND("connect", ip_port);
|
||||||
|
|
||||||
|
sc_pipe pout;
|
||||||
|
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
|
||||||
|
if (pid == SC_PROCESS_NONE) {
|
||||||
|
LOGE("Could not execute \"adb connect\"");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// "adb connect" always returns successfully (with exit code 0), even in
|
||||||
|
// case of failure. As a workaround, check if its output starts with
|
||||||
|
// "connected".
|
||||||
|
char buf[128];
|
||||||
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
|
||||||
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
|
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert((size_t) r < sizeof(buf));
|
||||||
|
buf[r] = '\0';
|
||||||
|
|
||||||
|
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
||||||
|
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
||||||
|
// "adb connect" also prints errors to stdout. Since we capture it,
|
||||||
|
// re-print the error to stderr.
|
||||||
|
size_t len = strcspn(buf, "\r\n");
|
||||||
|
buf[len] = '\0';
|
||||||
|
fprintf(stderr, "%s\n", buf);
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
|
assert(ip_port);
|
||||||
|
const char *const argv[] = SC_ADB_COMMAND("disconnect", ip_port);
|
||||||
|
|
||||||
|
sc_pid pid = sc_adb_execute(argv, flags);
|
||||||
|
return process_check_success_intr(intr, pid, "adb disconnect", flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_adb_list_devices(struct sc_intr *intr, unsigned flags,
|
||||||
|
struct sc_vec_adb_devices *out_vec) {
|
||||||
|
const char *const argv[] = SC_ADB_COMMAND("devices", "-l");
|
||||||
|
|
||||||
|
#define BUFSIZE 65536
|
||||||
|
char *buf = malloc(BUFSIZE);
|
||||||
|
if (!buf) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_pipe pout;
|
||||||
|
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
|
||||||
|
if (pid == SC_PROCESS_NONE) {
|
||||||
|
LOGE("Could not execute \"adb devices -l\"");
|
||||||
|
free(buf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, BUFSIZE - 1);
|
||||||
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
|
bool ok = process_check_success_intr(intr, pid, "adb devices -l", flags);
|
||||||
|
if (!ok) {
|
||||||
|
free(buf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
free(buf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert((size_t) r < BUFSIZE);
|
||||||
|
if (r == BUFSIZE - 1) {
|
||||||
|
// The implementation assumes that the output of "adb devices -l" fits
|
||||||
|
// in the buffer in a single pass
|
||||||
|
LOGW("Result of \"adb devices -l\" does not fit in 64Kb. "
|
||||||
|
"Please report an issue.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It is parsed as a NUL-terminated string
|
||||||
|
buf[r] = '\0';
|
||||||
|
|
||||||
|
// List all devices to the output list directly
|
||||||
|
ok = sc_adb_parse_devices(buf, out_vec);
|
||||||
|
free(buf);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_adb_accept_device(const struct sc_adb_device *device,
|
||||||
|
const struct sc_adb_device_selector *selector) {
|
||||||
|
switch (selector->type) {
|
||||||
|
case SC_ADB_DEVICE_SELECT_ALL:
|
||||||
|
return true;
|
||||||
|
case SC_ADB_DEVICE_SELECT_SERIAL:
|
||||||
|
assert(selector->serial);
|
||||||
|
char *device_serial_colon = strchr(device->serial, ':');
|
||||||
|
if (device_serial_colon) {
|
||||||
|
// The device serial is an IP:port...
|
||||||
|
char *serial_colon = strchr(selector->serial, ':');
|
||||||
|
if (!serial_colon) {
|
||||||
|
// But the requested serial has no ':', so only consider
|
||||||
|
// the IP part of the device serial. This allows to use
|
||||||
|
// "192.168.1.1" to match any "192.168.1.1:port".
|
||||||
|
size_t serial_len = strlen(selector->serial);
|
||||||
|
size_t device_ip_len = device_serial_colon - device->serial;
|
||||||
|
if (serial_len != device_ip_len) {
|
||||||
|
// They are not equal, they don't even have the same
|
||||||
|
// length
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !strncmp(selector->serial, device->serial,
|
||||||
|
device_ip_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !strcmp(selector->serial, device->serial);
|
||||||
|
case SC_ADB_DEVICE_SELECT_USB:
|
||||||
|
return !sc_adb_is_serial_tcpip(device->serial);
|
||||||
|
case SC_ADB_DEVICE_SELECT_TCPIP:
|
||||||
|
return sc_adb_is_serial_tcpip(device->serial);
|
||||||
|
default:
|
||||||
|
assert(!"Missing SC_ADB_DEVICE_SELECT_* handling");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
sc_adb_devices_select(struct sc_adb_device *devices, size_t len,
|
||||||
|
const struct sc_adb_device_selector *selector,
|
||||||
|
size_t *idx_out) {
|
||||||
|
size_t count = 0;
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
struct sc_adb_device *device = &devices[i];
|
||||||
|
device->selected = sc_adb_accept_device(device, selector);
|
||||||
|
if (device->selected) {
|
||||||
|
if (idx_out && !count) {
|
||||||
|
*idx_out = i;
|
||||||
|
}
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sc_adb_devices_log(enum sc_log_level level, struct sc_adb_device *devices,
|
||||||
|
size_t count) {
|
||||||
|
for (size_t i = 0; i < count; ++i) {
|
||||||
|
struct sc_adb_device *d = &devices[i];
|
||||||
|
const char *selection = d->selected ? "-->" : " ";
|
||||||
|
const char *type = sc_adb_is_serial_tcpip(d->serial) ? "(tcpip)"
|
||||||
|
: " (usb)";
|
||||||
|
LOG(level, " %s %s %-20s %16s %s",
|
||||||
|
selection, type, d->serial, d->state, d->model ? d->model : "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_adb_device_check_state(struct sc_adb_device *device,
|
||||||
|
struct sc_adb_device *devices, size_t count) {
|
||||||
|
const char *state = device->state;
|
||||||
|
|
||||||
|
if (!strcmp("device", state)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strcmp("unauthorized", state)) {
|
||||||
|
LOGE("Device is unauthorized:");
|
||||||
|
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, devices, count);
|
||||||
|
LOGE("A popup should open on the device to request authorization.");
|
||||||
|
LOGE("Check the FAQ: "
|
||||||
|
"<https://github.com/Genymobile/scrcpy/blob/master/FAQ.md>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_select_device(struct sc_intr *intr,
|
||||||
|
const struct sc_adb_device_selector *selector,
|
||||||
|
unsigned flags, struct sc_adb_device *out_device) {
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_list_devices(intr, flags, &vec);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not list ADB devices");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vec.size == 0) {
|
||||||
|
LOGE("Could not find any ADB device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t sel_idx; // index of the single matching device if sel_count == 1
|
||||||
|
size_t sel_count =
|
||||||
|
sc_adb_devices_select(vec.data, vec.size, selector, &sel_idx);
|
||||||
|
|
||||||
|
if (sel_count == 0) {
|
||||||
|
// if count > 0 && sel_count == 0, then necessarily a selection is
|
||||||
|
// requested
|
||||||
|
assert(selector->type != SC_ADB_DEVICE_SELECT_ALL);
|
||||||
|
|
||||||
|
switch (selector->type) {
|
||||||
|
case SC_ADB_DEVICE_SELECT_SERIAL:
|
||||||
|
assert(selector->serial);
|
||||||
|
LOGE("Could not find ADB device %s:", selector->serial);
|
||||||
|
break;
|
||||||
|
case SC_ADB_DEVICE_SELECT_USB:
|
||||||
|
LOGE("Could not find any ADB device over USB:");
|
||||||
|
break;
|
||||||
|
case SC_ADB_DEVICE_SELECT_TCPIP:
|
||||||
|
LOGE("Could not find any ADB device over TCP/IP:");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(!"Unexpected selector type");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sel_count > 1) {
|
||||||
|
switch (selector->type) {
|
||||||
|
case SC_ADB_DEVICE_SELECT_ALL:
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") ADB devices:", sel_count);
|
||||||
|
break;
|
||||||
|
case SC_ADB_DEVICE_SELECT_SERIAL:
|
||||||
|
assert(selector->serial);
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") ADB devices with serial %s:",
|
||||||
|
sel_count, selector->serial);
|
||||||
|
break;
|
||||||
|
case SC_ADB_DEVICE_SELECT_USB:
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") ADB devices over USB:",
|
||||||
|
sel_count);
|
||||||
|
break;
|
||||||
|
case SC_ADB_DEVICE_SELECT_TCPIP:
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") ADB devices over TCP/IP:",
|
||||||
|
sel_count);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(!"Unexpected selector type");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sc_adb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
|
||||||
|
LOGE("Select a device via -s (--serial), -d (--select-usb) or -e "
|
||||||
|
"(--select-tcpip)");
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(sel_count == 1); // sel_idx is valid only if sel_count == 1
|
||||||
|
struct sc_adb_device *device = &vec.data[sel_idx];
|
||||||
|
|
||||||
|
ok = sc_adb_device_check_state(device, vec.data, vec.size);
|
||||||
|
if (!ok) {
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGD("ADB device found:");
|
||||||
|
sc_adb_devices_log(SC_LOG_LEVEL_DEBUG, vec.data, vec.size);
|
||||||
|
|
||||||
|
// Move devics into out_device (do not destroy device)
|
||||||
|
sc_adb_device_move(out_device, device);
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
|
||||||
|
unsigned flags) {
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "shell", "getprop", prop);
|
||||||
|
|
||||||
|
sc_pipe pout;
|
||||||
|
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
|
||||||
|
if (pid == SC_PROCESS_NONE) {
|
||||||
|
LOGE("Could not execute \"adb getprop\"");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[128];
|
||||||
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
|
||||||
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
|
bool ok = process_check_success_intr(intr, pid, "adb getprop", flags);
|
||||||
|
if (!ok) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert((size_t) r < sizeof(buf));
|
||||||
|
buf[r] = '\0';
|
||||||
|
size_t len = strcspn(buf, " \r\n");
|
||||||
|
buf[len] = '\0';
|
||||||
|
|
||||||
|
return strdup(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
|
||||||
|
assert(serial);
|
||||||
|
const char *const argv[] =
|
||||||
|
SC_ADB_COMMAND("-s", serial, "shell", "ip", "route");
|
||||||
|
|
||||||
|
sc_pipe pout;
|
||||||
|
sc_pid pid = sc_adb_execute_p(argv, flags, &pout);
|
||||||
|
if (pid == SC_PROCESS_NONE) {
|
||||||
|
LOGD("Could not execute \"ip route\"");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// "adb shell ip route" output should contain only a few lines
|
||||||
|
char buf[1024];
|
||||||
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
|
||||||
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
|
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
|
||||||
|
if (!ok) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert((size_t) r < sizeof(buf));
|
||||||
|
if (r == sizeof(buf) - 1) {
|
||||||
|
// The implementation assumes that the output of "ip route" fits in the
|
||||||
|
// buffer in a single pass
|
||||||
|
LOGW("Result of \"ip route\" does not fit in 1Kb. "
|
||||||
|
"Please report an issue.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It is parsed as a NUL-terminated string
|
||||||
|
buf[r] = '\0';
|
||||||
|
|
||||||
|
return sc_adb_parse_device_ip_from_output(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_is_serial_tcpip(const char *serial) {
|
||||||
|
return strchr(serial, ':');
|
||||||
|
}
|
||||||
126
app/src/adb/adb.h
Normal file
126
app/src/adb/adb.h
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
#ifndef SC_ADB_H
|
||||||
|
#define SC_ADB_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#include "adb_device.h"
|
||||||
|
#include "util/intr.h"
|
||||||
|
|
||||||
|
#define SC_ADB_NO_STDOUT (1 << 0)
|
||||||
|
#define SC_ADB_NO_STDERR (1 << 1)
|
||||||
|
#define SC_ADB_NO_LOGERR (1 << 2)
|
||||||
|
|
||||||
|
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
|
||||||
|
|
||||||
|
const char *
|
||||||
|
sc_adb_get_executable(void);
|
||||||
|
|
||||||
|
enum sc_adb_device_selector_type {
|
||||||
|
SC_ADB_DEVICE_SELECT_ALL,
|
||||||
|
SC_ADB_DEVICE_SELECT_SERIAL,
|
||||||
|
SC_ADB_DEVICE_SELECT_USB,
|
||||||
|
SC_ADB_DEVICE_SELECT_TCPIP,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sc_adb_device_selector {
|
||||||
|
enum sc_adb_device_selector_type type;
|
||||||
|
const char *serial;
|
||||||
|
};
|
||||||
|
|
||||||
|
sc_pid
|
||||||
|
sc_adb_execute(const char *const argv[], unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_start_server(struct sc_intr *intr, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_kill_server(struct sc_intr *intr, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||||
|
const char *device_socket_name, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
uint16_t local_port, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_reverse(struct sc_intr *intr, const char *serial,
|
||||||
|
const char *device_socket_name, uint16_t local_port,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
const char *device_socket_name, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
const char *remote, unsigned flags);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute `adb tcpip <port>`
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute `adb connect <ip_port>`
|
||||||
|
*
|
||||||
|
* `ip_port` may not be NULL.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute `adb disconnect [<ip_port>]`
|
||||||
|
*
|
||||||
|
* If `ip_port` is NULL, execute `adb disconnect`.
|
||||||
|
* Otherwise, execute `adb disconnect <ip_port>`.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute `adb devices` and parse the result to select a device
|
||||||
|
*
|
||||||
|
* Return true if a single matching device is found, and write it to out_device.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_select_device(struct sc_intr *intr,
|
||||||
|
const struct sc_adb_device_selector *selector,
|
||||||
|
unsigned flags, struct sc_adb_device *out_device);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute `adb getprop <prop>`
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
sc_adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to retrieve the device IP
|
||||||
|
*
|
||||||
|
* Return the IP as a string of the form "xxx.xxx.xxx.xxx", to be freed by the
|
||||||
|
* caller, or NULL on error.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate if the serial represents an IP address
|
||||||
|
*
|
||||||
|
* In practice, it just returns true if and only if it contains a ':', which is
|
||||||
|
* sufficient to distinguish an ip:port from a real USB serial.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_is_serial_tcpip(const char *serial);
|
||||||
|
|
||||||
|
#endif
|
||||||
27
app/src/adb/adb_device.c
Normal file
27
app/src/adb/adb_device.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "adb_device.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_adb_device_destroy(struct sc_adb_device *device) {
|
||||||
|
free(device->serial);
|
||||||
|
free(device->state);
|
||||||
|
free(device->model);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_adb_device_move(struct sc_adb_device *dst, struct sc_adb_device *src) {
|
||||||
|
*dst = *src;
|
||||||
|
src->serial = NULL;
|
||||||
|
src->state = NULL;
|
||||||
|
src->model = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_adb_devices_destroy(struct sc_vec_adb_devices *devices) {
|
||||||
|
for (size_t i = 0; i < devices->size; ++i) {
|
||||||
|
sc_adb_device_destroy(&devices->data[i]);
|
||||||
|
}
|
||||||
|
sc_vector_destroy(devices);
|
||||||
|
}
|
||||||
|
|
||||||
38
app/src/adb/adb_device.h
Normal file
38
app/src/adb/adb_device.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef SC_ADB_DEVICE_H
|
||||||
|
#define SC_ADB_DEVICE_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "util/vector.h"
|
||||||
|
|
||||||
|
struct sc_adb_device {
|
||||||
|
char *serial;
|
||||||
|
char *state;
|
||||||
|
char *model;
|
||||||
|
bool selected;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices SC_VECTOR(struct sc_adb_device);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_adb_device_destroy(struct sc_adb_device *device);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move src to dst
|
||||||
|
*
|
||||||
|
* After this call, the content of src is undefined, except that
|
||||||
|
* sc_adb_device_destroy() can be called.
|
||||||
|
*
|
||||||
|
* This is useful to take a device from a list that will be destroyed, without
|
||||||
|
* making unnecessary copies.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
sc_adb_device_move(struct sc_adb_device *dst, struct sc_adb_device *src);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_adb_devices_destroy(struct sc_vec_adb_devices *devices);
|
||||||
|
|
||||||
|
#endif
|
||||||
227
app/src/adb/adb_parser.c
Normal file
227
app/src/adb/adb_parser.c
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
#include "adb_parser.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "util/log.h"
|
||||||
|
#include "util/str.h"
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_parse_device(char *line, struct sc_adb_device *device) {
|
||||||
|
// One device line looks like:
|
||||||
|
// "0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
// "device:MyDevice transport_id:1"
|
||||||
|
|
||||||
|
if (line[0] == '*') {
|
||||||
|
// Garbage lines printed by adb daemon while starting start with a '*'
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strncmp("adb server", line, sizeof("adb server") - 1)) {
|
||||||
|
// Ignore lines starting with "adb server":
|
||||||
|
// adb server version (41) doesn't match this client (39); killing...
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *s = line; // cursor in the line
|
||||||
|
|
||||||
|
// After the serial:
|
||||||
|
// - "adb devices" writes a single '\t'
|
||||||
|
// - "adb devices -l" writes multiple spaces
|
||||||
|
// For flexibility, accept both.
|
||||||
|
size_t serial_len = strcspn(s, " \t");
|
||||||
|
if (!serial_len) {
|
||||||
|
// empty serial
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool eol = s[serial_len] == '\0';
|
||||||
|
if (eol) {
|
||||||
|
// serial alone is unexpected
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s[serial_len] = '\0';
|
||||||
|
char *serial = s;
|
||||||
|
s += serial_len + 1;
|
||||||
|
// After the serial, there might be several spaces
|
||||||
|
s += strspn(s, " \t"); // consume all separators
|
||||||
|
|
||||||
|
size_t state_len = strcspn(s, " ");
|
||||||
|
if (!state_len) {
|
||||||
|
// empty state
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
eol = s[state_len] == '\0';
|
||||||
|
s[state_len] = '\0';
|
||||||
|
char *state = s;
|
||||||
|
|
||||||
|
char *model = NULL;
|
||||||
|
if (!eol) {
|
||||||
|
s += state_len + 1;
|
||||||
|
|
||||||
|
// Iterate over all properties "key:value key:value ..."
|
||||||
|
for (;;) {
|
||||||
|
size_t token_len = strcspn(s, " ");
|
||||||
|
if (!token_len) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
eol = s[token_len] == '\0';
|
||||||
|
s[token_len] = '\0';
|
||||||
|
char *token = s;
|
||||||
|
|
||||||
|
if (!strncmp("model:", token, sizeof("model:") - 1)) {
|
||||||
|
model = &token[sizeof("model:") - 1];
|
||||||
|
// We only need the model
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eol) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
s+= token_len + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
device->serial = strdup(serial);
|
||||||
|
if (!device->serial) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
device->state = strdup(state);
|
||||||
|
if (!device->state) {
|
||||||
|
free(device->serial);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model) {
|
||||||
|
device->model = strdup(model);
|
||||||
|
if (!device->model) {
|
||||||
|
LOG_OOM();
|
||||||
|
// model is optional, do not fail
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
device->model = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
device->selected = false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_adb_parse_devices(char *str, struct sc_vec_adb_devices *out_vec) {
|
||||||
|
#define HEADER "List of devices attached"
|
||||||
|
#define HEADER_LEN (sizeof(HEADER) - 1)
|
||||||
|
bool header_found = false;
|
||||||
|
|
||||||
|
size_t idx_line = 0;
|
||||||
|
while (str[idx_line] != '\0') {
|
||||||
|
char *line = &str[idx_line];
|
||||||
|
size_t len = strcspn(line, "\n");
|
||||||
|
|
||||||
|
// The next line starts after the '\n' (replaced by `\0`)
|
||||||
|
idx_line += len;
|
||||||
|
|
||||||
|
if (str[idx_line] != '\0') {
|
||||||
|
// The next line starts after the '\n'
|
||||||
|
++idx_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!header_found) {
|
||||||
|
if (!strncmp(line, HEADER, HEADER_LEN)) {
|
||||||
|
header_found = true;
|
||||||
|
}
|
||||||
|
// Skip everything until the header, there might be garbage lines
|
||||||
|
// related to daemon starting before
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The line, but without any trailing '\r'
|
||||||
|
size_t line_len = sc_str_remove_trailing_cr(line, len);
|
||||||
|
line[line_len] = '\0';
|
||||||
|
|
||||||
|
struct sc_adb_device device;
|
||||||
|
bool ok = sc_adb_parse_device(line, &device);
|
||||||
|
if (!ok) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = sc_vector_push(out_vec, device);
|
||||||
|
if (!ok) {
|
||||||
|
LOG_OOM();
|
||||||
|
LOGE("Could not push adb_device to vector");
|
||||||
|
sc_adb_device_destroy(&device);
|
||||||
|
// continue anyway
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(header_found || out_vec->size == 0);
|
||||||
|
return header_found;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
sc_adb_parse_device_ip_from_line(char *line) {
|
||||||
|
// One line from "ip route" looks like:
|
||||||
|
// "192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.x"
|
||||||
|
|
||||||
|
// Get the location of the device name (index of "wlan0" in the example)
|
||||||
|
ssize_t idx_dev_name = sc_str_index_of_column(line, 2, " ");
|
||||||
|
if (idx_dev_name == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the location of the ip address (column 8, but column 6 if we start
|
||||||
|
// from column 2). Must be computed before truncating individual columns.
|
||||||
|
ssize_t idx_ip = sc_str_index_of_column(&line[idx_dev_name], 6, " ");
|
||||||
|
if (idx_ip == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// idx_ip is searched from &line[idx_dev_name]
|
||||||
|
idx_ip += idx_dev_name;
|
||||||
|
|
||||||
|
char *dev_name = &line[idx_dev_name];
|
||||||
|
size_t dev_name_len = strcspn(dev_name, " \t");
|
||||||
|
dev_name[dev_name_len] = '\0';
|
||||||
|
|
||||||
|
char *ip = &line[idx_ip];
|
||||||
|
size_t ip_len = strcspn(ip, " \t");
|
||||||
|
ip[ip_len] = '\0';
|
||||||
|
|
||||||
|
// Only consider lines where the device name starts with "wlan"
|
||||||
|
if (strncmp(dev_name, "wlan", sizeof("wlan") - 1)) {
|
||||||
|
LOGD("Device ip lookup: ignoring %s (%s)", ip, dev_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strdup(ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_adb_parse_device_ip_from_output(char *str) {
|
||||||
|
size_t idx_line = 0;
|
||||||
|
while (str[idx_line] != '\0') {
|
||||||
|
char *line = &str[idx_line];
|
||||||
|
size_t len = strcspn(line, "\n");
|
||||||
|
|
||||||
|
// The same, but without any trailing '\r'
|
||||||
|
size_t line_len = sc_str_remove_trailing_cr(line, len);
|
||||||
|
line[line_len] = '\0';
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_line(line);
|
||||||
|
if (ip) {
|
||||||
|
// Found
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx_line += len;
|
||||||
|
|
||||||
|
if (str[idx_line] != '\0') {
|
||||||
|
// The next line starts after the '\n'
|
||||||
|
++idx_line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
30
app/src/adb/adb_parser.h
Normal file
30
app/src/adb/adb_parser.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef SC_ADB_PARSER_H
|
||||||
|
#define SC_ADB_PARSER_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "adb_device.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the available devices from the output of `adb devices`
|
||||||
|
*
|
||||||
|
* The parameter must be a NUL-terminated string.
|
||||||
|
*
|
||||||
|
* Warning: this function modifies the buffer for optimization purposes.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sc_adb_parse_devices(char *str, struct sc_vec_adb_devices *out_vec);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the ip from the output of `adb shell ip route`
|
||||||
|
*
|
||||||
|
* The parameter must be a NUL-terminated string.
|
||||||
|
*
|
||||||
|
* Warning: this function modifies the buffer for optimization purposes.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
sc_adb_parse_device_ip_from_output(char *str);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -20,8 +20,8 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
struct sc_port_range port_range) {
|
struct sc_port_range port_range) {
|
||||||
uint16_t port = port_range.first;
|
uint16_t port = port_range.first;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port,
|
if (!sc_adb_reverse(intr, serial, SC_SOCKET_NAME, port,
|
||||||
SC_ADB_NO_STDOUT)) {
|
SC_ADB_NO_STDOUT)) {
|
||||||
// the command itself failed, it will fail on any port
|
// the command itself failed, it will fail on any port
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// failure, disable tunnel and try another port
|
// failure, disable tunnel and try another port
|
||||||
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
if (!sc_adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||||
SC_ADB_NO_STDOUT)) {
|
SC_ADB_NO_STDOUT)) {
|
||||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,8 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
|
|
||||||
uint16_t port = port_range.first;
|
uint16_t port = port_range.first;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_ADB_NO_STDOUT)) {
|
if (sc_adb_forward(intr, serial, port, SC_SOCKET_NAME,
|
||||||
|
SC_ADB_NO_STDOUT)) {
|
||||||
// success
|
// success
|
||||||
tunnel->local_port = port;
|
tunnel->local_port = port;
|
||||||
tunnel->enabled = true;
|
tunnel->enabled = true;
|
||||||
@@ -148,11 +149,11 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
|||||||
|
|
||||||
bool ret;
|
bool ret;
|
||||||
if (tunnel->forward) {
|
if (tunnel->forward) {
|
||||||
ret = adb_forward_remove(intr, serial, tunnel->local_port,
|
ret = sc_adb_forward_remove(intr, serial, tunnel->local_port,
|
||||||
SC_ADB_NO_STDOUT);
|
SC_ADB_NO_STDOUT);
|
||||||
} else {
|
} else {
|
||||||
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
ret = sc_adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||||
SC_ADB_NO_STDOUT);
|
SC_ADB_NO_STDOUT);
|
||||||
|
|
||||||
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
||||||
if (!net_close(tunnel->server_socket)) {
|
if (!net_close(tunnel->server_socket)) {
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#include "adb_parser.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "util/log.h"
|
|
||||||
#include "util/str.h"
|
|
||||||
|
|
||||||
static char *
|
|
||||||
sc_adb_parse_device_ip_from_line(char *line, size_t len) {
|
|
||||||
// One line from "ip route" looks lile:
|
|
||||||
// "192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.x"
|
|
||||||
|
|
||||||
// Get the location of the device name (index of "wlan0" in the example)
|
|
||||||
ssize_t idx_dev_name = sc_str_index_of_column(line, 2, " ");
|
|
||||||
if (idx_dev_name == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the location of the ip address (column 8, but column 6 if we start
|
|
||||||
// from column 2). Must be computed before truncating individual columns.
|
|
||||||
ssize_t idx_ip = sc_str_index_of_column(&line[idx_dev_name], 6, " ");
|
|
||||||
if (idx_ip == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
// idx_ip is searched from &line[idx_dev_name]
|
|
||||||
idx_ip += idx_dev_name;
|
|
||||||
|
|
||||||
char *dev_name = &line[idx_dev_name];
|
|
||||||
sc_str_truncate(dev_name, len - idx_dev_name + 1, " \t");
|
|
||||||
|
|
||||||
char *ip = &line[idx_ip];
|
|
||||||
sc_str_truncate(ip, len - idx_ip + 1, " \t");
|
|
||||||
|
|
||||||
// Only consider lines where the device name starts with "wlan"
|
|
||||||
if (strncmp(dev_name, "wlan", sizeof("wlan") - 1)) {
|
|
||||||
LOGD("Device ip lookup: ignoring %s (%s)", ip, dev_name);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return strdup(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
sc_adb_parse_device_ip_from_output(char *buf, size_t buf_len) {
|
|
||||||
size_t idx_line = 0;
|
|
||||||
while (idx_line < buf_len && buf[idx_line] != '\0') {
|
|
||||||
char *line = &buf[idx_line];
|
|
||||||
size_t len = sc_str_truncate(line, buf_len - idx_line, "\n");
|
|
||||||
|
|
||||||
// The same, but without any trailing '\r'
|
|
||||||
size_t line_len = sc_str_remove_trailing_cr(line, len);
|
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_line(line, line_len);
|
|
||||||
if (ip) {
|
|
||||||
// Found
|
|
||||||
return ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The next line starts after the '\n' (replaced by `\0`)
|
|
||||||
idx_line += len + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef SC_ADB_PARSER_H
|
|
||||||
#define SC_ADB_PARSER_H
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
|
|
||||||
#include "stddef.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse the ip from the output of `adb shell ip route`
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
sc_adb_parse_device_ip_from_output(char *buf, size_t buf_len);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
133
app/src/cli.c
133
app/src/cli.c
@@ -54,6 +54,8 @@
|
|||||||
#define OPT_RAW_KEY_EVENTS 1034
|
#define OPT_RAW_KEY_EVENTS 1034
|
||||||
#define OPT_NO_DOWNSIZE_ON_ERROR 1035
|
#define OPT_NO_DOWNSIZE_ON_ERROR 1035
|
||||||
#define OPT_OTG 1036
|
#define OPT_OTG 1036
|
||||||
|
#define OPT_NO_CLEANUP 1037
|
||||||
|
#define OPT_PRINT_FPS 1038
|
||||||
|
|
||||||
struct sc_option {
|
struct sc_option {
|
||||||
char shortopt;
|
char shortopt;
|
||||||
@@ -116,7 +118,13 @@ static const struct sc_option options[] = {
|
|||||||
.text = "Crop the device screen on the server.\n"
|
.text = "Crop the device screen on the server.\n"
|
||||||
"The values are expressed in the device natural orientation "
|
"The values are expressed in the device natural orientation "
|
||||||
"(typically, portrait for a phone, landscape for a tablet). "
|
"(typically, portrait for a phone, landscape for a tablet). "
|
||||||
"Any --max-size value is cmoputed on the cropped size.",
|
"Any --max-size value is computed on the cropped size.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.shortopt = 'd',
|
||||||
|
.longopt = "select-usb",
|
||||||
|
.text = "Use USB device (if there is exactly one, like adb -d).\n"
|
||||||
|
"Also see -e (--select-tcpip).",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_DISABLE_SCREENSAVER,
|
.longopt_id = OPT_DISABLE_SCREENSAVER,
|
||||||
@@ -141,6 +149,12 @@ static const struct sc_option options[] = {
|
|||||||
"This increases latency to compensate for jitter.\n"
|
"This increases latency to compensate for jitter.\n"
|
||||||
"Default is 0 (no buffering).",
|
"Default is 0 (no buffering).",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.shortopt = 'e',
|
||||||
|
.longopt = "select-tcpip",
|
||||||
|
.text = "Use TCP/IP device (if there is exactly one, like adb -e).\n"
|
||||||
|
"Also see -d (--select-usb).",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_ENCODER_NAME,
|
.longopt_id = OPT_ENCODER_NAME,
|
||||||
.longopt = "encoder",
|
.longopt = "encoder",
|
||||||
@@ -172,8 +186,7 @@ static const struct sc_option options[] = {
|
|||||||
"It provides a better experience for IME users, and allows to "
|
"It provides a better experience for IME users, and allows to "
|
||||||
"generate non-ASCII characters, contrary to the default "
|
"generate non-ASCII characters, contrary to the default "
|
||||||
"injection method.\n"
|
"injection method.\n"
|
||||||
"It may only work over USB, and is currently only supported "
|
"It may only work over USB.\n"
|
||||||
"on Linux.\n"
|
|
||||||
"The keyboard layout must be configured (once and for all) on "
|
"The keyboard layout must be configured (once and for all) on "
|
||||||
"the device, via Settings -> System -> Languages and input -> "
|
"the device, via Settings -> System -> Languages and input -> "
|
||||||
"Physical keyboard. This settings page can be started "
|
"Physical keyboard. This settings page can be started "
|
||||||
@@ -225,8 +238,7 @@ static const struct sc_option options[] = {
|
|||||||
"device directly (relative mouse mode).\n"
|
"device directly (relative mouse mode).\n"
|
||||||
"LAlt, LSuper or RSuper toggle the capture mode, to give "
|
"LAlt, LSuper or RSuper toggle the capture mode, to give "
|
||||||
"control of the mouse back to the computer.\n"
|
"control of the mouse back to the computer.\n"
|
||||||
"It may only work over USB, and is currently only supported "
|
"It may only work over USB.\n"
|
||||||
"on Linux.\n"
|
|
||||||
"Also see --hid-keyboard.",
|
"Also see --hid-keyboard.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -239,11 +251,12 @@ static const struct sc_option options[] = {
|
|||||||
"Default is 0 (unlimited).",
|
"Default is 0 (unlimited).",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_NO_DOWNSIZE_ON_ERROR,
|
.longopt_id = OPT_NO_CLEANUP,
|
||||||
.longopt = "no-downsize-on-error",
|
.longopt = "no-cleanup",
|
||||||
.text = "By default, on MediaCodec error, scrcpy automatically tries "
|
.text = "By default, scrcpy removes the server binary from the device "
|
||||||
"again with a lower definition.\n"
|
"and restores the device state (show touches, stay awake and "
|
||||||
"This option disables this behavior.",
|
"power mode) on exit.\n"
|
||||||
|
"This option disables this cleanup."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
|
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
|
||||||
@@ -254,6 +267,13 @@ static const struct sc_option options[] = {
|
|||||||
"it changes.\n"
|
"it changes.\n"
|
||||||
"This option disables this automatic synchronization."
|
"This option disables this automatic synchronization."
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_NO_DOWNSIZE_ON_ERROR,
|
||||||
|
.longopt = "no-downsize-on-error",
|
||||||
|
.text = "By default, on MediaCodec error, scrcpy automatically tries "
|
||||||
|
"again with a lower definition.\n"
|
||||||
|
"This option disables this behavior.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.shortopt = 'n',
|
.shortopt = 'n',
|
||||||
.longopt = "no-control",
|
.longopt = "no-control",
|
||||||
@@ -288,9 +308,8 @@ static const struct sc_option options[] = {
|
|||||||
"LAlt, LSuper or RSuper toggle the mouse capture mode, to give "
|
"LAlt, LSuper or RSuper toggle the mouse capture mode, to give "
|
||||||
"control of the mouse back to the computer.\n"
|
"control of the mouse back to the computer.\n"
|
||||||
"If any of --hid-keyboard or --hid-mouse is set, only enable "
|
"If any of --hid-keyboard or --hid-mouse is set, only enable "
|
||||||
"keyboard or mouse respectively, otherwise enable both."
|
"keyboard or mouse respectively, otherwise enable both.\n"
|
||||||
"It may only work over USB, and is currently only supported "
|
"It may only work over USB.\n"
|
||||||
"on Linux.\n"
|
|
||||||
"See --hid-keyboard and --hid-mouse.",
|
"See --hid-keyboard and --hid-mouse.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -309,12 +328,18 @@ static const struct sc_option options[] = {
|
|||||||
{
|
{
|
||||||
.longopt_id = OPT_PREFER_TEXT,
|
.longopt_id = OPT_PREFER_TEXT,
|
||||||
.longopt = "prefer-text",
|
.longopt = "prefer-text",
|
||||||
.text = "Inject alpha characters and space as text events instead of"
|
.text = "Inject alpha characters and space as text events instead of "
|
||||||
"key events.\n"
|
"key events.\n"
|
||||||
"This avoids issues when combining multiple keys to enter a "
|
"This avoids issues when combining multiple keys to enter a "
|
||||||
"special character, but breaks the expected behavior of alpha "
|
"special character, but breaks the expected behavior of alpha "
|
||||||
"keys in games (typically WASD).",
|
"keys in games (typically WASD).",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_PRINT_FPS,
|
||||||
|
.longopt = "print-fps",
|
||||||
|
.text = "Start FPS counter, to print framerate logs to the console. "
|
||||||
|
"It can be started or stopped at any time with MOD+i.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_PUSH_TARGET,
|
.longopt_id = OPT_PUSH_TARGET,
|
||||||
.longopt = "push-target",
|
.longopt = "push-target",
|
||||||
@@ -397,6 +422,20 @@ static const struct sc_option options[] = {
|
|||||||
"on exit.\n"
|
"on exit.\n"
|
||||||
"It only shows physical touches (not clicks from scrcpy).",
|
"It only shows physical touches (not clicks from scrcpy).",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_TCPIP,
|
||||||
|
.longopt = "tcpip",
|
||||||
|
.argdesc = "ip[:port]",
|
||||||
|
.optional_arg = true,
|
||||||
|
.text = "Configure and reconnect the device over TCP/IP.\n"
|
||||||
|
"If a destination address is provided, then scrcpy connects to "
|
||||||
|
"this address before starting. The device must listen on the "
|
||||||
|
"given TCP port (default is 5555).\n"
|
||||||
|
"If no destination address is provided, then scrcpy attempts "
|
||||||
|
"to find the IP address of the current device (typically "
|
||||||
|
"connected over USB), enables TCP/IP mode, then connects to "
|
||||||
|
"this address before starting.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_TUNNEL_HOST,
|
.longopt_id = OPT_TUNNEL_HOST,
|
||||||
.longopt = "tunnel-host",
|
.longopt = "tunnel-host",
|
||||||
@@ -458,20 +497,6 @@ static const struct sc_option options[] = {
|
|||||||
.text = "Keep the device on while scrcpy is running, when the device "
|
.text = "Keep the device on while scrcpy is running, when the device "
|
||||||
"is plugged in.",
|
"is plugged in.",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
.longopt_id = OPT_TCPIP,
|
|
||||||
.longopt = "tcpip",
|
|
||||||
.argdesc = "ip[:port]",
|
|
||||||
.optional_arg = true,
|
|
||||||
.text = "Configure and reconnect the device over TCP/IP.\n"
|
|
||||||
"If a destination address is provided, then scrcpy connects to "
|
|
||||||
"this address before starting. The device must listen on the "
|
|
||||||
"given TCP port (default is 5555).\n"
|
|
||||||
"If no destination address is provided, then scrcpy attempts "
|
|
||||||
"to find the IP address of the current device (typically "
|
|
||||||
"connected over USB), enables TCP/IP mode, then connects to "
|
|
||||||
"this address before starting.",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_WINDOW_BORDERLESS,
|
.longopt_id = OPT_WINDOW_BORDERLESS,
|
||||||
.longopt = "window-borderless",
|
.longopt = "window-borderless",
|
||||||
@@ -1320,6 +1345,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'd':
|
||||||
|
opts->select_usb = true;
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
opts->select_tcpip = true;
|
||||||
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
opts->fullscreen = true;
|
opts->fullscreen = true;
|
||||||
break;
|
break;
|
||||||
@@ -1339,8 +1370,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_HID;
|
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_HID;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
LOGE("HID over AOA (-K/--hid-keyboard) is not supported on "
|
LOGE("HID over AOA (-K/--hid-keyboard) is disabled.");
|
||||||
"this platform. It is only available on Linux.");
|
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
case OPT_MAX_FPS:
|
case OPT_MAX_FPS:
|
||||||
@@ -1358,8 +1388,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_HID;
|
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_HID;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
LOGE("HID over AOA (-M/--hid-mouse) is not supported on this"
|
LOGE("HID over AOA (-M/--hid-mouse) is disabled.");
|
||||||
"platform. It is only available on Linux.");
|
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
case OPT_LOCK_VIDEO_ORIENTATION:
|
case OPT_LOCK_VIDEO_ORIENTATION:
|
||||||
@@ -1517,13 +1546,18 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
case OPT_NO_DOWNSIZE_ON_ERROR:
|
case OPT_NO_DOWNSIZE_ON_ERROR:
|
||||||
opts->downsize_on_error = false;
|
opts->downsize_on_error = false;
|
||||||
break;
|
break;
|
||||||
|
case OPT_NO_CLEANUP:
|
||||||
|
opts->cleanup = false;
|
||||||
|
break;
|
||||||
|
case OPT_PRINT_FPS:
|
||||||
|
opts->start_fps_counter = true;
|
||||||
|
break;
|
||||||
case OPT_OTG:
|
case OPT_OTG:
|
||||||
#ifdef HAVE_USB
|
#ifdef HAVE_USB
|
||||||
opts->otg = true;
|
opts->otg = true;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
LOGE("OTG mode (--otg) is not supported on this platform. It "
|
LOGE("OTG mode (--otg) is disabled.");
|
||||||
"is only available on Linux.");
|
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
case OPT_V4L2_SINK:
|
case OPT_V4L2_SINK:
|
||||||
@@ -1531,7 +1565,8 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
opts->v4l2_device = optarg;
|
opts->v4l2_device = optarg;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
LOGE("V4L2 (--v4l2-sink) is only available on Linux.");
|
LOGE("V4L2 (--v4l2-sink) is disabled (or unsupported on this "
|
||||||
|
"platform).");
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
case OPT_V4L2_BUFFER:
|
case OPT_V4L2_BUFFER:
|
||||||
@@ -1559,8 +1594,16 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
// If a TCP/IP address is provided, then tcpip must be enabled
|
// If a TCP/IP address is provided, then tcpip must be enabled
|
||||||
assert(opts->tcpip || !opts->tcpip_dst);
|
assert(opts->tcpip || !opts->tcpip_dst);
|
||||||
|
|
||||||
if (opts->serial && opts->tcpip_dst) {
|
unsigned selectors = !!opts->serial
|
||||||
LOGE("Incompatible options: -s/--serial and --tcpip with an argument");
|
+ !!opts->tcpip_dst
|
||||||
|
+ opts->select_tcpip
|
||||||
|
+ opts->select_usb;
|
||||||
|
if (selectors > 1) {
|
||||||
|
LOGE("At most one device selector option may be passed, among:\n"
|
||||||
|
" --serial (-s)\n"
|
||||||
|
" --select-usb (-d)\n"
|
||||||
|
" --select-tcpip (-e)\n"
|
||||||
|
" --tcpip=<addr> (with an argument)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1637,6 +1680,18 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_USB
|
#ifdef HAVE_USB
|
||||||
|
|
||||||
|
# ifdef _WIN32
|
||||||
|
if (!opts->otg && (opts->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID
|
||||||
|
|| opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_HID)) {
|
||||||
|
LOGE("On Windows, it is not possible to open a USB device already open "
|
||||||
|
"by another process (like adb).");
|
||||||
|
LOGE("Therefore, -K/--hid-keyboard and -M/--hid-mouse may only work in "
|
||||||
|
"OTG mode (--otg).");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
if (opts->otg) {
|
if (opts->otg) {
|
||||||
// OTG mode is compatible with only very few options.
|
// OTG mode is compatible with only very few options.
|
||||||
// Only report obvious errors.
|
// Only report obvious errors.
|
||||||
@@ -1664,12 +1719,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
LOGE("OTG mode: could not select display");
|
LOGE("OTG mode: could not select display");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_V4L2
|
# ifdef HAVE_V4L2
|
||||||
if (opts->v4l2_device) {
|
if (opts->v4l2_device) {
|
||||||
LOGE("OTG mode: could not sink to V4L2 device");
|
LOGE("OTG mode: could not sink to V4L2 device");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
# endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,10 @@
|
|||||||
|
|
||||||
#ifndef __WIN32
|
#ifndef __WIN32
|
||||||
# define PRIu64_ PRIu64
|
# define PRIu64_ PRIu64
|
||||||
|
# define SC_PRIsizet "zu"
|
||||||
#else
|
#else
|
||||||
# define PRIu64_ "I64u" // Windows...
|
# define PRIu64_ "I64u" // Windows...
|
||||||
|
# define SC_PRIsizet "Iu"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// In ffmpeg/doc/APIchanges:
|
// In ffmpeg/doc/APIchanges:
|
||||||
|
|||||||
@@ -63,17 +63,17 @@ static const char *const copy_key_labels[] = {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
write_position(uint8_t *buf, const struct sc_position *position) {
|
write_position(uint8_t *buf, const struct sc_position *position) {
|
||||||
buffer_write32be(&buf[0], position->point.x);
|
sc_write32be(&buf[0], position->point.x);
|
||||||
buffer_write32be(&buf[4], position->point.y);
|
sc_write32be(&buf[4], position->point.y);
|
||||||
buffer_write16be(&buf[8], position->screen_size.width);
|
sc_write16be(&buf[8], position->screen_size.width);
|
||||||
buffer_write16be(&buf[10], position->screen_size.height);
|
sc_write16be(&buf[10], position->screen_size.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write length (4 bytes) + string (non null-terminated)
|
// write length (4 bytes) + string (non null-terminated)
|
||||||
static size_t
|
static size_t
|
||||||
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
||||||
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
||||||
buffer_write32be(buf, len);
|
sc_write32be(buf, len);
|
||||||
memcpy(&buf[4], utf8, len);
|
memcpy(&buf[4], utf8, len);
|
||||||
return 4 + len;
|
return 4 + len;
|
||||||
}
|
}
|
||||||
@@ -94,9 +94,9 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
|
|||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
||||||
buf[1] = msg->inject_keycode.action;
|
buf[1] = msg->inject_keycode.action;
|
||||||
buffer_write32be(&buf[2], msg->inject_keycode.keycode);
|
sc_write32be(&buf[2], msg->inject_keycode.keycode);
|
||||||
buffer_write32be(&buf[6], msg->inject_keycode.repeat);
|
sc_write32be(&buf[6], msg->inject_keycode.repeat);
|
||||||
buffer_write32be(&buf[10], msg->inject_keycode.metastate);
|
sc_write32be(&buf[10], msg->inject_keycode.metastate);
|
||||||
return 14;
|
return 14;
|
||||||
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
|
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
|
||||||
size_t len =
|
size_t len =
|
||||||
@@ -106,20 +106,20 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
|
|||||||
}
|
}
|
||||||
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
|
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
|
||||||
buf[1] = msg->inject_touch_event.action;
|
buf[1] = msg->inject_touch_event.action;
|
||||||
buffer_write64be(&buf[2], msg->inject_touch_event.pointer_id);
|
sc_write64be(&buf[2], msg->inject_touch_event.pointer_id);
|
||||||
write_position(&buf[10], &msg->inject_touch_event.position);
|
write_position(&buf[10], &msg->inject_touch_event.position);
|
||||||
uint16_t pressure =
|
uint16_t pressure =
|
||||||
to_fixed_point_16(msg->inject_touch_event.pressure);
|
to_fixed_point_16(msg->inject_touch_event.pressure);
|
||||||
buffer_write16be(&buf[22], pressure);
|
sc_write16be(&buf[22], pressure);
|
||||||
buffer_write32be(&buf[24], msg->inject_touch_event.buttons);
|
sc_write32be(&buf[24], msg->inject_touch_event.buttons);
|
||||||
return 28;
|
return 28;
|
||||||
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
||||||
write_position(&buf[1], &msg->inject_scroll_event.position);
|
write_position(&buf[1], &msg->inject_scroll_event.position);
|
||||||
buffer_write32be(&buf[13],
|
sc_write32be(&buf[13],
|
||||||
(uint32_t) msg->inject_scroll_event.hscroll);
|
(uint32_t) msg->inject_scroll_event.hscroll);
|
||||||
buffer_write32be(&buf[17],
|
sc_write32be(&buf[17],
|
||||||
(uint32_t) msg->inject_scroll_event.vscroll);
|
(uint32_t) msg->inject_scroll_event.vscroll);
|
||||||
buffer_write32be(&buf[21], msg->inject_scroll_event.buttons);
|
sc_write32be(&buf[21], msg->inject_scroll_event.buttons);
|
||||||
return 25;
|
return 25;
|
||||||
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
||||||
buf[1] = msg->inject_keycode.action;
|
buf[1] = msg->inject_keycode.action;
|
||||||
@@ -128,7 +128,7 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
|
|||||||
buf[1] = msg->get_clipboard.copy_key;
|
buf[1] = msg->get_clipboard.copy_key;
|
||||||
return 2;
|
return 2;
|
||||||
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
||||||
buffer_write64be(&buf[1], msg->set_clipboard.sequence);
|
sc_write64be(&buf[1], msg->set_clipboard.sequence);
|
||||||
buf[9] = !!msg->set_clipboard.paste;
|
buf[9] = !!msg->set_clipboard.paste;
|
||||||
size_t len = write_string(msg->set_clipboard.text,
|
size_t len = write_string(msg->set_clipboard.text,
|
||||||
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ sc_controller_start(struct sc_controller *controller) {
|
|||||||
bool ok = sc_thread_create(&controller->thread, run_controller,
|
bool ok = sc_thread_create(&controller->thread, run_controller,
|
||||||
"scrcpy-ctl", controller);
|
"scrcpy-ctl", controller);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start controller thread");
|
LOGE("Could not start controller thread");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,10 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
/** Downcast packet_sink to decoder */
|
/** Downcast packet_sink to decoder */
|
||||||
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
|
#define DOWNCAST(SINK) container_of(SINK, struct sc_decoder, packet_sink)
|
||||||
|
|
||||||
static void
|
static void
|
||||||
decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
|
sc_decoder_close_first_sinks(struct sc_decoder *decoder, unsigned count) {
|
||||||
while (count) {
|
while (count) {
|
||||||
struct sc_frame_sink *sink = decoder->sinks[--count];
|
struct sc_frame_sink *sink = decoder->sinks[--count];
|
||||||
sink->ops->close(sink);
|
sink->ops->close(sink);
|
||||||
@@ -20,17 +20,17 @@ decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
decoder_close_sinks(struct decoder *decoder) {
|
sc_decoder_close_sinks(struct sc_decoder *decoder) {
|
||||||
decoder_close_first_sinks(decoder, decoder->sink_count);
|
sc_decoder_close_first_sinks(decoder, decoder->sink_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
decoder_open_sinks(struct decoder *decoder) {
|
sc_decoder_open_sinks(struct sc_decoder *decoder) {
|
||||||
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
||||||
struct sc_frame_sink *sink = decoder->sinks[i];
|
struct sc_frame_sink *sink = decoder->sinks[i];
|
||||||
if (!sink->ops->open(sink)) {
|
if (!sink->ops->open(sink)) {
|
||||||
LOGE("Could not open frame sink %d", i);
|
LOGE("Could not open frame sink %d", i);
|
||||||
decoder_close_first_sinks(decoder, i);
|
sc_decoder_close_first_sinks(decoder, i);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ decoder_open_sinks(struct decoder *decoder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
|
||||||
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
||||||
if (!decoder->codec_ctx) {
|
if (!decoder->codec_ctx) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
@@ -62,7 +62,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!decoder_open_sinks(decoder)) {
|
if (!sc_decoder_open_sinks(decoder)) {
|
||||||
LOGE("Could not open decoder sinks");
|
LOGE("Could not open decoder sinks");
|
||||||
av_frame_free(&decoder->frame);
|
av_frame_free(&decoder->frame);
|
||||||
avcodec_close(decoder->codec_ctx);
|
avcodec_close(decoder->codec_ctx);
|
||||||
@@ -74,15 +74,15 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
decoder_close(struct decoder *decoder) {
|
sc_decoder_close(struct sc_decoder *decoder) {
|
||||||
decoder_close_sinks(decoder);
|
sc_decoder_close_sinks(decoder);
|
||||||
av_frame_free(&decoder->frame);
|
av_frame_free(&decoder->frame);
|
||||||
avcodec_close(decoder->codec_ctx);
|
avcodec_close(decoder->codec_ctx);
|
||||||
avcodec_free_context(&decoder->codec_ctx);
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
|
push_frame_to_sinks(struct sc_decoder *decoder, const AVFrame *frame) {
|
||||||
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
||||||
struct sc_frame_sink *sink = decoder->sinks[i];
|
struct sc_frame_sink *sink = decoder->sinks[i];
|
||||||
if (!sink->ops->push(sink, frame)) {
|
if (!sink->ops->push(sink, frame)) {
|
||||||
@@ -95,7 +95,7 @@ push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
|
||||||
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
||||||
if (is_config) {
|
if (is_config) {
|
||||||
// nothing to do
|
// nothing to do
|
||||||
@@ -124,39 +124,40 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
sc_decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
||||||
struct decoder *decoder = DOWNCAST(sink);
|
struct sc_decoder *decoder = DOWNCAST(sink);
|
||||||
return decoder_open(decoder, codec);
|
return sc_decoder_open(decoder, codec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
decoder_packet_sink_close(struct sc_packet_sink *sink) {
|
sc_decoder_packet_sink_close(struct sc_packet_sink *sink) {
|
||||||
struct decoder *decoder = DOWNCAST(sink);
|
struct sc_decoder *decoder = DOWNCAST(sink);
|
||||||
decoder_close(decoder);
|
sc_decoder_close(decoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
decoder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
|
sc_decoder_packet_sink_push(struct sc_packet_sink *sink,
|
||||||
struct decoder *decoder = DOWNCAST(sink);
|
const AVPacket *packet) {
|
||||||
return decoder_push(decoder, packet);
|
struct sc_decoder *decoder = DOWNCAST(sink);
|
||||||
|
return sc_decoder_push(decoder, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_init(struct decoder *decoder) {
|
sc_decoder_init(struct sc_decoder *decoder) {
|
||||||
decoder->sink_count = 0;
|
decoder->sink_count = 0;
|
||||||
|
|
||||||
static const struct sc_packet_sink_ops ops = {
|
static const struct sc_packet_sink_ops ops = {
|
||||||
.open = decoder_packet_sink_open,
|
.open = sc_decoder_packet_sink_open,
|
||||||
.close = decoder_packet_sink_close,
|
.close = sc_decoder_packet_sink_close,
|
||||||
.push = decoder_packet_sink_push,
|
.push = sc_decoder_packet_sink_push,
|
||||||
};
|
};
|
||||||
|
|
||||||
decoder->packet_sink.ops = &ops;
|
decoder->packet_sink.ops = &ops;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink) {
|
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink) {
|
||||||
assert(decoder->sink_count < DECODER_MAX_SINKS);
|
assert(decoder->sink_count < SC_DECODER_MAX_SINKS);
|
||||||
assert(sink);
|
assert(sink);
|
||||||
assert(sink->ops);
|
assert(sink->ops);
|
||||||
decoder->sinks[decoder->sink_count++] = sink;
|
decoder->sinks[decoder->sink_count++] = sink;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef DECODER_H
|
#ifndef SC_DECODER_H
|
||||||
#define DECODER_H
|
#define SC_DECODER_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
@@ -9,12 +9,12 @@
|
|||||||
#include <libavcodec/avcodec.h>
|
#include <libavcodec/avcodec.h>
|
||||||
#include <libavformat/avformat.h>
|
#include <libavformat/avformat.h>
|
||||||
|
|
||||||
#define DECODER_MAX_SINKS 2
|
#define SC_DECODER_MAX_SINKS 2
|
||||||
|
|
||||||
struct decoder {
|
struct sc_decoder {
|
||||||
struct sc_packet_sink packet_sink; // packet sink trait
|
struct sc_packet_sink packet_sink; // packet sink trait
|
||||||
|
|
||||||
struct sc_frame_sink *sinks[DECODER_MAX_SINKS];
|
struct sc_frame_sink *sinks[SC_DECODER_MAX_SINKS];
|
||||||
unsigned sink_count;
|
unsigned sink_count;
|
||||||
|
|
||||||
AVCodecContext *codec_ctx;
|
AVCodecContext *codec_ctx;
|
||||||
@@ -22,9 +22,9 @@ struct decoder {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_init(struct decoder *decoder);
|
sc_decoder_init(struct sc_decoder *decoder);
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink);
|
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
280
app/src/demuxer.c
Normal file
280
app/src/demuxer.c
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
#include "demuxer.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <libavutil/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "decoder.h"
|
||||||
|
#include "events.h"
|
||||||
|
#include "recorder.h"
|
||||||
|
#include "util/buffer_util.h"
|
||||||
|
#include "util/log.h"
|
||||||
|
|
||||||
|
#define SC_PACKET_HEADER_SIZE 12
|
||||||
|
|
||||||
|
#define SC_PACKET_FLAG_CONFIG (UINT64_C(1) << 63)
|
||||||
|
#define SC_PACKET_FLAG_KEY_FRAME (UINT64_C(1) << 62)
|
||||||
|
|
||||||
|
#define SC_PACKET_PTS_MASK (SC_PACKET_FLAG_KEY_FRAME - 1)
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
|
||||||
|
// The video stream contains raw packets, without time information. When we
|
||||||
|
// record, we retrieve the timestamps separately, from a "meta" header
|
||||||
|
// added by the server before each raw packet.
|
||||||
|
//
|
||||||
|
// The "meta" header length is 12 bytes:
|
||||||
|
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
|
||||||
|
// <-------------> <-----> <-----------------------------...
|
||||||
|
// PTS packet raw packet
|
||||||
|
// size
|
||||||
|
//
|
||||||
|
// It is followed by <packet_size> bytes containing the packet/frame.
|
||||||
|
//
|
||||||
|
// The most significant bits of the PTS are used for packet flags:
|
||||||
|
//
|
||||||
|
// byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
|
||||||
|
// CK...... ........ ........ ........ ........ ........ ........ ........
|
||||||
|
// ^^<------------------------------------------------------------------->
|
||||||
|
// || PTS
|
||||||
|
// | `- config packet
|
||||||
|
// `-- key frame
|
||||||
|
|
||||||
|
uint8_t header[SC_PACKET_HEADER_SIZE];
|
||||||
|
ssize_t r = net_recv_all(demuxer->socket, header, SC_PACKET_HEADER_SIZE);
|
||||||
|
if (r < SC_PACKET_HEADER_SIZE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t pts_flags = sc_read64be(header);
|
||||||
|
uint32_t len = sc_read32be(&header[8]);
|
||||||
|
assert(len);
|
||||||
|
|
||||||
|
if (av_new_packet(packet, len)) {
|
||||||
|
LOG_OOM();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = net_recv_all(demuxer->socket, packet->data, len);
|
||||||
|
if (r < 0 || ((uint32_t) r) < len) {
|
||||||
|
av_packet_unref(packet);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pts_flags & SC_PACKET_FLAG_CONFIG) {
|
||||||
|
packet->pts = AV_NOPTS_VALUE;
|
||||||
|
} else {
|
||||||
|
packet->pts = pts_flags & SC_PACKET_PTS_MASK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pts_flags & SC_PACKET_FLAG_KEY_FRAME) {
|
||||||
|
packet->flags |= AV_PKT_FLAG_KEY;
|
||||||
|
}
|
||||||
|
|
||||||
|
packet->dts = packet->pts;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
push_packet_to_sinks(struct sc_demuxer *demuxer, const AVPacket *packet) {
|
||||||
|
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
|
||||||
|
struct sc_packet_sink *sink = demuxer->sinks[i];
|
||||||
|
if (!sink->ops->push(sink, packet)) {
|
||||||
|
LOGE("Could not send config packet to sink %d", i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_demuxer_push_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
|
||||||
|
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
||||||
|
|
||||||
|
// A config packet must not be decoded immediately (it contains no
|
||||||
|
// frame); instead, it must be concatenated with the future data packet.
|
||||||
|
if (demuxer->pending || is_config) {
|
||||||
|
size_t offset;
|
||||||
|
if (demuxer->pending) {
|
||||||
|
offset = demuxer->pending->size;
|
||||||
|
if (av_grow_packet(demuxer->pending, packet->size)) {
|
||||||
|
LOG_OOM();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
offset = 0;
|
||||||
|
demuxer->pending = av_packet_alloc();
|
||||||
|
if (!demuxer->pending) {
|
||||||
|
LOG_OOM();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (av_new_packet(demuxer->pending, packet->size)) {
|
||||||
|
LOG_OOM();
|
||||||
|
av_packet_free(&demuxer->pending);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(demuxer->pending->data + offset, packet->data, packet->size);
|
||||||
|
|
||||||
|
if (!is_config) {
|
||||||
|
// prepare the concat packet to send to the decoder
|
||||||
|
demuxer->pending->pts = packet->pts;
|
||||||
|
demuxer->pending->dts = packet->dts;
|
||||||
|
demuxer->pending->flags = packet->flags;
|
||||||
|
packet = demuxer->pending;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok = push_packet_to_sinks(demuxer, packet);
|
||||||
|
|
||||||
|
if (!is_config && demuxer->pending) {
|
||||||
|
// the pending packet must be discarded (consumed or error)
|
||||||
|
av_packet_free(&demuxer->pending);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not process packet");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sc_demuxer_close_first_sinks(struct sc_demuxer *demuxer, unsigned count) {
|
||||||
|
while (count) {
|
||||||
|
struct sc_packet_sink *sink = demuxer->sinks[--count];
|
||||||
|
sink->ops->close(sink);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
sc_demuxer_close_sinks(struct sc_demuxer *demuxer) {
|
||||||
|
sc_demuxer_close_first_sinks(demuxer, demuxer->sink_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_demuxer_open_sinks(struct sc_demuxer *demuxer, const AVCodec *codec) {
|
||||||
|
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
|
||||||
|
struct sc_packet_sink *sink = demuxer->sinks[i];
|
||||||
|
if (!sink->ops->open(sink, codec)) {
|
||||||
|
LOGE("Could not open packet sink %d", i);
|
||||||
|
sc_demuxer_close_first_sinks(demuxer, i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
run_demuxer(void *data) {
|
||||||
|
struct sc_demuxer *demuxer = data;
|
||||||
|
|
||||||
|
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
|
||||||
|
if (!codec) {
|
||||||
|
LOGE("H.264 decoder not found");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
demuxer->codec_ctx = avcodec_alloc_context3(codec);
|
||||||
|
if (!demuxer->codec_ctx) {
|
||||||
|
LOG_OOM();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sc_demuxer_open_sinks(demuxer, codec)) {
|
||||||
|
LOGE("Could not open demuxer sinks");
|
||||||
|
goto finally_free_codec_ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
demuxer->parser = av_parser_init(AV_CODEC_ID_H264);
|
||||||
|
if (!demuxer->parser) {
|
||||||
|
LOGE("Could not initialize parser");
|
||||||
|
goto finally_close_sinks;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We must only pass complete frames to av_parser_parse2()!
|
||||||
|
// It's more complicated, but this allows to reduce the latency by 1 frame!
|
||||||
|
demuxer->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
|
||||||
|
|
||||||
|
AVPacket *packet = av_packet_alloc();
|
||||||
|
if (!packet) {
|
||||||
|
LOG_OOM();
|
||||||
|
goto finally_close_parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
bool ok = sc_demuxer_recv_packet(demuxer, packet);
|
||||||
|
if (!ok) {
|
||||||
|
// end of stream
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = sc_demuxer_push_packet(demuxer, packet);
|
||||||
|
av_packet_unref(packet);
|
||||||
|
if (!ok) {
|
||||||
|
// cannot process packet (error already logged)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGD("End of frames");
|
||||||
|
|
||||||
|
if (demuxer->pending) {
|
||||||
|
av_packet_free(&demuxer->pending);
|
||||||
|
}
|
||||||
|
|
||||||
|
av_packet_free(&packet);
|
||||||
|
finally_close_parser:
|
||||||
|
av_parser_close(demuxer->parser);
|
||||||
|
finally_close_sinks:
|
||||||
|
sc_demuxer_close_sinks(demuxer);
|
||||||
|
finally_free_codec_ctx:
|
||||||
|
avcodec_free_context(&demuxer->codec_ctx);
|
||||||
|
end:
|
||||||
|
demuxer->cbs->on_eos(demuxer, demuxer->cbs_userdata);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
|
||||||
|
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata) {
|
||||||
|
demuxer->socket = socket;
|
||||||
|
demuxer->pending = NULL;
|
||||||
|
demuxer->sink_count = 0;
|
||||||
|
|
||||||
|
assert(cbs && cbs->on_eos);
|
||||||
|
|
||||||
|
demuxer->cbs = cbs;
|
||||||
|
demuxer->cbs_userdata = cbs_userdata;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_add_sink(struct sc_demuxer *demuxer, struct sc_packet_sink *sink) {
|
||||||
|
assert(demuxer->sink_count < SC_DEMUXER_MAX_SINKS);
|
||||||
|
assert(sink);
|
||||||
|
assert(sink->ops);
|
||||||
|
demuxer->sinks[demuxer->sink_count++] = sink;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_demuxer_start(struct sc_demuxer *demuxer) {
|
||||||
|
LOGD("Starting demuxer thread");
|
||||||
|
|
||||||
|
bool ok = sc_thread_create(&demuxer->thread, run_demuxer, "scrcpy-demuxer",
|
||||||
|
demuxer);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not start demuxer thread");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_join(struct sc_demuxer *demuxer) {
|
||||||
|
sc_thread_join(&demuxer->thread, NULL);
|
||||||
|
}
|
||||||
51
app/src/demuxer.h
Normal file
51
app/src/demuxer.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#ifndef SC_DEMUXER_H
|
||||||
|
#define SC_DEMUXER_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <libavcodec/avcodec.h>
|
||||||
|
#include <libavformat/avformat.h>
|
||||||
|
|
||||||
|
#include "trait/packet_sink.h"
|
||||||
|
#include "util/net.h"
|
||||||
|
#include "util/thread.h"
|
||||||
|
|
||||||
|
#define SC_DEMUXER_MAX_SINKS 2
|
||||||
|
|
||||||
|
struct sc_demuxer {
|
||||||
|
sc_socket socket;
|
||||||
|
sc_thread thread;
|
||||||
|
|
||||||
|
struct sc_packet_sink *sinks[SC_DEMUXER_MAX_SINKS];
|
||||||
|
unsigned sink_count;
|
||||||
|
|
||||||
|
AVCodecContext *codec_ctx;
|
||||||
|
AVCodecParserContext *parser;
|
||||||
|
// successive packets may need to be concatenated, until a non-config
|
||||||
|
// packet is available
|
||||||
|
AVPacket *pending;
|
||||||
|
|
||||||
|
const struct sc_demuxer_callbacks *cbs;
|
||||||
|
void *cbs_userdata;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sc_demuxer_callbacks {
|
||||||
|
void (*on_eos)(struct sc_demuxer *demuxer, void *userdata);
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
|
||||||
|
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_add_sink(struct sc_demuxer *demuxer, struct sc_packet_sink *sink);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_demuxer_start(struct sc_demuxer *demuxer);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_demuxer_join(struct sc_demuxer *demuxer);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -18,7 +18,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
|||||||
msg->type = buf[0];
|
msg->type = buf[0];
|
||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
||||||
size_t clipboard_len = buffer_read32be(&buf[1]);
|
size_t clipboard_len = sc_read32be(&buf[1]);
|
||||||
if (clipboard_len > len - 5) {
|
if (clipboard_len > len - 5) {
|
||||||
return 0; // not available
|
return 0; // not available
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
|||||||
return 5 + clipboard_len;
|
return 5 + clipboard_len;
|
||||||
}
|
}
|
||||||
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: {
|
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: {
|
||||||
uint64_t sequence = buffer_read64be(&buf[1]);
|
uint64_t sequence = sc_read64be(&buf[1]);
|
||||||
msg->ack_clipboard.sequence = sequence;
|
msg->ack_clipboard.sequence = sequence;
|
||||||
return 9;
|
return 9;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "adb.h"
|
#include "adb/adb.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/process_intr.h"
|
#include "util/process_intr.h"
|
||||||
|
|
||||||
@@ -129,7 +129,7 @@ run_file_pusher(void *data) {
|
|||||||
|
|
||||||
if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) {
|
if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) {
|
||||||
LOGI("Installing %s...", req.file);
|
LOGI("Installing %s...", req.file);
|
||||||
bool ok = adb_install(intr, serial, req.file, 0);
|
bool ok = sc_adb_install(intr, serial, req.file, 0);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
LOGI("%s successfully installed", req.file);
|
LOGI("%s successfully installed", req.file);
|
||||||
} else {
|
} else {
|
||||||
@@ -137,7 +137,7 @@ run_file_pusher(void *data) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LOGI("Pushing %s...", req.file);
|
LOGI("Pushing %s...", req.file);
|
||||||
bool ok = adb_push(intr, serial, req.file, push_target, 0);
|
bool ok = sc_adb_push(intr, serial, req.file, push_target, 0);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
LOGI("%s successfully pushed to %s", req.file, push_target);
|
LOGI("%s successfully pushed to %s", req.file, push_target);
|
||||||
} else {
|
} else {
|
||||||
@@ -156,7 +156,7 @@ sc_file_pusher_start(struct sc_file_pusher *fp) {
|
|||||||
|
|
||||||
bool ok = sc_thread_create(&fp->thread, run_file_pusher, "scrcpy-file", fp);
|
bool ok = sc_thread_create(&fp->thread, run_file_pusher, "scrcpy-file", fp);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start file_pusher thread");
|
LOGE("Could not start file_pusher thread");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "adb.h"
|
|
||||||
#include "util/cbuf.h"
|
#include "util/cbuf.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
#include "util/intr.h"
|
#include "util/intr.h"
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
#define FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
|
#define SC_FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_init(struct fps_counter *counter) {
|
sc_fps_counter_init(struct sc_fps_counter *counter) {
|
||||||
bool ok = sc_mutex_init(&counter->mutex);
|
bool ok = sc_mutex_init(&counter->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
@@ -27,26 +27,26 @@ fps_counter_init(struct fps_counter *counter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_destroy(struct fps_counter *counter) {
|
sc_fps_counter_destroy(struct sc_fps_counter *counter) {
|
||||||
sc_cond_destroy(&counter->state_cond);
|
sc_cond_destroy(&counter->state_cond);
|
||||||
sc_mutex_destroy(&counter->mutex);
|
sc_mutex_destroy(&counter->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool
|
static inline bool
|
||||||
is_started(struct fps_counter *counter) {
|
is_started(struct sc_fps_counter *counter) {
|
||||||
return atomic_load_explicit(&counter->started, memory_order_acquire);
|
return atomic_load_explicit(&counter->started, memory_order_acquire);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
set_started(struct fps_counter *counter, bool started) {
|
set_started(struct sc_fps_counter *counter, bool started) {
|
||||||
atomic_store_explicit(&counter->started, started, memory_order_release);
|
atomic_store_explicit(&counter->started, started, memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be called with mutex locked
|
// must be called with mutex locked
|
||||||
static void
|
static void
|
||||||
display_fps(struct fps_counter *counter) {
|
display_fps(struct sc_fps_counter *counter) {
|
||||||
unsigned rendered_per_second =
|
unsigned rendered_per_second =
|
||||||
counter->nr_rendered * SC_TICK_FREQ / FPS_COUNTER_INTERVAL;
|
counter->nr_rendered * SC_TICK_FREQ / SC_FPS_COUNTER_INTERVAL;
|
||||||
if (counter->nr_skipped) {
|
if (counter->nr_skipped) {
|
||||||
LOGI("%u fps (+%u frames skipped)", rendered_per_second,
|
LOGI("%u fps (+%u frames skipped)", rendered_per_second,
|
||||||
counter->nr_skipped);
|
counter->nr_skipped);
|
||||||
@@ -57,7 +57,7 @@ display_fps(struct fps_counter *counter) {
|
|||||||
|
|
||||||
// must be called with mutex locked
|
// must be called with mutex locked
|
||||||
static void
|
static void
|
||||||
check_interval_expired(struct fps_counter *counter, uint32_t now) {
|
check_interval_expired(struct sc_fps_counter *counter, sc_tick now) {
|
||||||
if (now < counter->next_timestamp) {
|
if (now < counter->next_timestamp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -67,13 +67,13 @@ check_interval_expired(struct fps_counter *counter, uint32_t now) {
|
|||||||
counter->nr_skipped = 0;
|
counter->nr_skipped = 0;
|
||||||
// add a multiple of the interval
|
// add a multiple of the interval
|
||||||
uint32_t elapsed_slices =
|
uint32_t elapsed_slices =
|
||||||
(now - counter->next_timestamp) / FPS_COUNTER_INTERVAL + 1;
|
(now - counter->next_timestamp) / SC_FPS_COUNTER_INTERVAL + 1;
|
||||||
counter->next_timestamp += FPS_COUNTER_INTERVAL * elapsed_slices;
|
counter->next_timestamp += SC_FPS_COUNTER_INTERVAL * elapsed_slices;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
run_fps_counter(void *data) {
|
run_fps_counter(void *data) {
|
||||||
struct fps_counter *counter = data;
|
struct sc_fps_counter *counter = data;
|
||||||
|
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
while (!counter->interrupted) {
|
while (!counter->interrupted) {
|
||||||
@@ -94,9 +94,9 @@ run_fps_counter(void *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_start(struct fps_counter *counter) {
|
sc_fps_counter_start(struct sc_fps_counter *counter) {
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
counter->next_timestamp = sc_tick_now() + FPS_COUNTER_INTERVAL;
|
counter->next_timestamp = sc_tick_now() + SC_FPS_COUNTER_INTERVAL;
|
||||||
counter->nr_rendered = 0;
|
counter->nr_rendered = 0;
|
||||||
counter->nr_skipped = 0;
|
counter->nr_skipped = 0;
|
||||||
sc_mutex_unlock(&counter->mutex);
|
sc_mutex_unlock(&counter->mutex);
|
||||||
@@ -117,22 +117,24 @@ fps_counter_start(struct fps_counter *counter) {
|
|||||||
counter->thread_started = true;
|
counter->thread_started = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGI("FPS counter started");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_stop(struct fps_counter *counter) {
|
sc_fps_counter_stop(struct sc_fps_counter *counter) {
|
||||||
set_started(counter, false);
|
set_started(counter, false);
|
||||||
sc_cond_signal(&counter->state_cond);
|
sc_cond_signal(&counter->state_cond);
|
||||||
|
LOGI("FPS counter stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_is_started(struct fps_counter *counter) {
|
sc_fps_counter_is_started(struct sc_fps_counter *counter) {
|
||||||
return is_started(counter);
|
return is_started(counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_interrupt(struct fps_counter *counter) {
|
sc_fps_counter_interrupt(struct sc_fps_counter *counter) {
|
||||||
if (!counter->thread_started) {
|
if (!counter->thread_started) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -145,7 +147,7 @@ fps_counter_interrupt(struct fps_counter *counter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_join(struct fps_counter *counter) {
|
sc_fps_counter_join(struct sc_fps_counter *counter) {
|
||||||
if (counter->thread_started) {
|
if (counter->thread_started) {
|
||||||
// interrupted must be set by the thread calling join(), so no need to
|
// interrupted must be set by the thread calling join(), so no need to
|
||||||
// lock for the assertion
|
// lock for the assertion
|
||||||
@@ -156,7 +158,7 @@ fps_counter_join(struct fps_counter *counter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter) {
|
||||||
if (!is_started(counter)) {
|
if (!is_started(counter)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -169,7 +171,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter) {
|
||||||
if (!is_started(counter)) {
|
if (!is_started(counter)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
|
|
||||||
struct fps_counter {
|
struct sc_fps_counter {
|
||||||
sc_thread thread;
|
sc_thread thread;
|
||||||
sc_mutex mutex;
|
sc_mutex mutex;
|
||||||
sc_cond state_cond;
|
sc_cond state_cond;
|
||||||
@@ -28,32 +28,32 @@ struct fps_counter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_init(struct fps_counter *counter);
|
sc_fps_counter_init(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_destroy(struct fps_counter *counter);
|
sc_fps_counter_destroy(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_start(struct fps_counter *counter);
|
sc_fps_counter_start(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_stop(struct fps_counter *counter);
|
sc_fps_counter_stop(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fps_counter_is_started(struct fps_counter *counter);
|
sc_fps_counter_is_started(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
// request to stop the thread (on quit)
|
// request to stop the thread (on quit)
|
||||||
// must be called before fps_counter_join()
|
// must be called before sc_fps_counter_join()
|
||||||
void
|
void
|
||||||
fps_counter_interrupt(struct fps_counter *counter);
|
sc_fps_counter_interrupt(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_join(struct fps_counter *counter);
|
sc_fps_counter_join(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_rendered_frame(struct fps_counter *counter);
|
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_skipped_frame(struct fps_counter *counter);
|
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -242,18 +242,14 @@ set_screen_power_mode(struct sc_controller *controller,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
switch_fps_counter_state(struct fps_counter *fps_counter) {
|
switch_fps_counter_state(struct sc_fps_counter *fps_counter) {
|
||||||
// the started state can only be written from the current thread, so there
|
// the started state can only be written from the current thread, so there
|
||||||
// is no ToCToU issue
|
// is no ToCToU issue
|
||||||
if (fps_counter_is_started(fps_counter)) {
|
if (sc_fps_counter_is_started(fps_counter)) {
|
||||||
fps_counter_stop(fps_counter);
|
sc_fps_counter_stop(fps_counter);
|
||||||
LOGI("FPS counter stopped");
|
|
||||||
} else {
|
} else {
|
||||||
if (fps_counter_start(fps_counter)) {
|
sc_fps_counter_start(fps_counter);
|
||||||
LOGI("FPS counter started");
|
// Any error is already logged
|
||||||
} else {
|
|
||||||
LOGE("FPS counter starting failed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,27 +15,7 @@
|
|||||||
#include "scrcpy.h"
|
#include "scrcpy.h"
|
||||||
#include "usb/scrcpy_otg.h"
|
#include "usb/scrcpy_otg.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
#include "version.h"
|
||||||
static void
|
|
||||||
print_version(void) {
|
|
||||||
printf("\ndependencies:\n");
|
|
||||||
printf(" - SDL %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION,
|
|
||||||
SDL_PATCHLEVEL);
|
|
||||||
printf(" - libavcodec %d.%d.%d\n", LIBAVCODEC_VERSION_MAJOR,
|
|
||||||
LIBAVCODEC_VERSION_MINOR,
|
|
||||||
LIBAVCODEC_VERSION_MICRO);
|
|
||||||
printf(" - libavformat %d.%d.%d\n", LIBAVFORMAT_VERSION_MAJOR,
|
|
||||||
LIBAVFORMAT_VERSION_MINOR,
|
|
||||||
LIBAVFORMAT_VERSION_MICRO);
|
|
||||||
printf(" - libavutil %d.%d.%d\n", LIBAVUTIL_VERSION_MAJOR,
|
|
||||||
LIBAVUTIL_VERSION_MINOR,
|
|
||||||
LIBAVUTIL_VERSION_MICRO);
|
|
||||||
#ifdef HAVE_V4L2
|
|
||||||
printf(" - libavdevice %d.%d.%d\n", LIBAVDEVICE_VERSION_MAJOR,
|
|
||||||
LIBAVDEVICE_VERSION_MINOR,
|
|
||||||
LIBAVDEVICE_VERSION_MICRO);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[]) {
|
main(int argc, char *argv[]) {
|
||||||
@@ -71,7 +51,7 @@ main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.version) {
|
if (args.version) {
|
||||||
print_version();
|
scrcpy_print_version();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,4 +60,8 @@ const struct scrcpy_options scrcpy_options_default = {
|
|||||||
.downsize_on_error = true,
|
.downsize_on_error = true,
|
||||||
.tcpip = false,
|
.tcpip = false,
|
||||||
.tcpip_dst = NULL,
|
.tcpip_dst = NULL,
|
||||||
|
.select_tcpip = false,
|
||||||
|
.select_usb = false,
|
||||||
|
.cleanup = true,
|
||||||
|
.start_fps_counter = false,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -135,6 +135,10 @@ struct scrcpy_options {
|
|||||||
bool downsize_on_error;
|
bool downsize_on_error;
|
||||||
bool tcpip;
|
bool tcpip;
|
||||||
const char *tcpip_dst;
|
const char *tcpip_dst;
|
||||||
|
bool select_usb;
|
||||||
|
bool select_tcpip;
|
||||||
|
bool cleanup;
|
||||||
|
bool start_fps_counter;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const struct scrcpy_options scrcpy_options_default;
|
extern const struct scrcpy_options scrcpy_options_default;
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ receiver_start(struct receiver *receiver) {
|
|||||||
bool ok = sc_thread_create(&receiver->thread, run_receiver,
|
bool ok = sc_thread_create(&receiver->thread, run_receiver,
|
||||||
"scrcpy-receiver", receiver);
|
"scrcpy-receiver", receiver);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start receiver thread");
|
LOGE("Could not start receiver thread");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "util/str.h"
|
#include "util/str.h"
|
||||||
|
|
||||||
/** Downcast packet_sink to recorder */
|
/** Downcast packet_sink to recorder */
|
||||||
#define DOWNCAST(SINK) container_of(SINK, struct recorder, packet_sink)
|
#define DOWNCAST(SINK) container_of(SINK, struct sc_recorder, packet_sink)
|
||||||
|
|
||||||
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
||||||
|
|
||||||
@@ -30,9 +30,9 @@ find_muxer(const char *name) {
|
|||||||
return oformat;
|
return oformat;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct record_packet *
|
static struct sc_record_packet *
|
||||||
record_packet_new(const AVPacket *packet) {
|
sc_record_packet_new(const AVPacket *packet) {
|
||||||
struct record_packet *rec = malloc(sizeof(*rec));
|
struct sc_record_packet *rec = malloc(sizeof(*rec));
|
||||||
if (!rec) {
|
if (!rec) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -54,22 +54,22 @@ record_packet_new(const AVPacket *packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
record_packet_delete(struct record_packet *rec) {
|
sc_record_packet_delete(struct sc_record_packet *rec) {
|
||||||
av_packet_free(&rec->packet);
|
av_packet_free(&rec->packet);
|
||||||
free(rec);
|
free(rec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
recorder_queue_clear(struct recorder_queue *queue) {
|
sc_recorder_queue_clear(struct sc_recorder_queue *queue) {
|
||||||
while (!sc_queue_is_empty(queue)) {
|
while (!sc_queue_is_empty(queue)) {
|
||||||
struct record_packet *rec;
|
struct sc_record_packet *rec;
|
||||||
sc_queue_take(queue, next, &rec);
|
sc_queue_take(queue, next, &rec);
|
||||||
record_packet_delete(rec);
|
sc_record_packet_delete(rec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
recorder_get_format_name(enum sc_record_format format) {
|
sc_recorder_get_format_name(enum sc_record_format format) {
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case SC_RECORD_FORMAT_MP4: return "mp4";
|
case SC_RECORD_FORMAT_MP4: return "mp4";
|
||||||
case SC_RECORD_FORMAT_MKV: return "matroska";
|
case SC_RECORD_FORMAT_MKV: return "matroska";
|
||||||
@@ -78,7 +78,7 @@ recorder_get_format_name(enum sc_record_format format) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
sc_recorder_write_header(struct sc_recorder *recorder, const AVPacket *packet) {
|
||||||
AVStream *ostream = recorder->ctx->streams[0];
|
AVStream *ostream = recorder->ctx->streams[0];
|
||||||
|
|
||||||
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
||||||
@@ -103,19 +103,19 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) {
|
sc_recorder_rescale_packet(struct sc_recorder *recorder, AVPacket *packet) {
|
||||||
AVStream *ostream = recorder->ctx->streams[0];
|
AVStream *ostream = recorder->ctx->streams[0];
|
||||||
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
|
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_write(struct recorder *recorder, AVPacket *packet) {
|
sc_recorder_write(struct sc_recorder *recorder, AVPacket *packet) {
|
||||||
if (!recorder->header_written) {
|
if (!recorder->header_written) {
|
||||||
if (packet->pts != AV_NOPTS_VALUE) {
|
if (packet->pts != AV_NOPTS_VALUE) {
|
||||||
LOGE("The first packet is not a config packet");
|
LOGE("The first packet is not a config packet");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool ok = recorder_write_header(recorder, packet);
|
bool ok = sc_recorder_write_header(recorder, packet);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -128,13 +128,13 @@ recorder_write(struct recorder *recorder, AVPacket *packet) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
recorder_rescale_packet(recorder, packet);
|
sc_recorder_rescale_packet(recorder, packet);
|
||||||
return av_write_frame(recorder->ctx, packet) >= 0;
|
return av_write_frame(recorder->ctx, packet) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
run_recorder(void *data) {
|
run_recorder(void *data) {
|
||||||
struct recorder *recorder = data;
|
struct sc_recorder *recorder = data;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
@@ -148,29 +148,29 @@ run_recorder(void *data) {
|
|||||||
|
|
||||||
if (recorder->stopped && sc_queue_is_empty(&recorder->queue)) {
|
if (recorder->stopped && sc_queue_is_empty(&recorder->queue)) {
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
struct record_packet *last = recorder->previous;
|
struct sc_record_packet *last = recorder->previous;
|
||||||
if (last) {
|
if (last) {
|
||||||
// assign an arbitrary duration to the last packet
|
// assign an arbitrary duration to the last packet
|
||||||
last->packet->duration = 100000;
|
last->packet->duration = 100000;
|
||||||
bool ok = recorder_write(recorder, last->packet);
|
bool ok = sc_recorder_write(recorder, last->packet);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
// failing to write the last frame is not very serious, no
|
// failing to write the last frame is not very serious, no
|
||||||
// future frame may depend on it, so the resulting file
|
// future frame may depend on it, so the resulting file
|
||||||
// will still be valid
|
// will still be valid
|
||||||
LOGW("Could not record last packet");
|
LOGW("Could not record last packet");
|
||||||
}
|
}
|
||||||
record_packet_delete(last);
|
sc_record_packet_delete(last);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct record_packet *rec;
|
struct sc_record_packet *rec;
|
||||||
sc_queue_take(&recorder->queue, next, &rec);
|
sc_queue_take(&recorder->queue, next, &rec);
|
||||||
|
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
|
|
||||||
// recorder->previous is only written from this thread, no need to lock
|
// recorder->previous is only written from this thread, no need to lock
|
||||||
struct record_packet *previous = recorder->previous;
|
struct sc_record_packet *previous = recorder->previous;
|
||||||
recorder->previous = rec;
|
recorder->previous = rec;
|
||||||
|
|
||||||
if (!previous) {
|
if (!previous) {
|
||||||
@@ -186,15 +186,15 @@ run_recorder(void *data) {
|
|||||||
rec->packet->pts - previous->packet->pts;
|
rec->packet->pts - previous->packet->pts;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = recorder_write(recorder, previous->packet);
|
bool ok = sc_recorder_write(recorder, previous->packet);
|
||||||
record_packet_delete(previous);
|
sc_record_packet_delete(previous);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not record packet");
|
LOGE("Could not record packet");
|
||||||
|
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
recorder->failed = true;
|
recorder->failed = true;
|
||||||
// discard pending packets
|
// discard pending packets
|
||||||
recorder_queue_clear(&recorder->queue);
|
sc_recorder_queue_clear(&recorder->queue);
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -216,7 +216,7 @@ run_recorder(void *data) {
|
|||||||
if (recorder->failed) {
|
if (recorder->failed) {
|
||||||
LOGE("Recording failed to %s", recorder->filename);
|
LOGE("Recording failed to %s", recorder->filename);
|
||||||
} else {
|
} else {
|
||||||
const char *format_name = recorder_get_format_name(recorder->format);
|
const char *format_name = sc_recorder_get_format_name(recorder->format);
|
||||||
LOGI("Recording complete to %s file: %s", format_name,
|
LOGI("Recording complete to %s file: %s", format_name,
|
||||||
recorder->filename);
|
recorder->filename);
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ run_recorder(void *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
sc_recorder_open(struct sc_recorder *recorder, const AVCodec *input_codec) {
|
||||||
bool ok = sc_mutex_init(&recorder->mutex);
|
bool ok = sc_mutex_init(&recorder->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
@@ -244,7 +244,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
|||||||
recorder->header_written = false;
|
recorder->header_written = false;
|
||||||
recorder->previous = NULL;
|
recorder->previous = NULL;
|
||||||
|
|
||||||
const char *format_name = recorder_get_format_name(recorder->format);
|
const char *format_name = sc_recorder_get_format_name(recorder->format);
|
||||||
assert(format_name);
|
assert(format_name);
|
||||||
const AVOutputFormat *format = find_muxer(format_name);
|
const AVOutputFormat *format = find_muxer(format_name);
|
||||||
if (!format) {
|
if (!format) {
|
||||||
@@ -290,7 +290,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
|||||||
ok = sc_thread_create(&recorder->thread, run_recorder, "scrcpy-recorder",
|
ok = sc_thread_create(&recorder->thread, run_recorder, "scrcpy-recorder",
|
||||||
recorder);
|
recorder);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start recorder thread");
|
LOGE("Could not start recorder thread");
|
||||||
goto error_avio_close;
|
goto error_avio_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,7 +311,7 @@ error_mutex_destroy:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
recorder_close(struct recorder *recorder) {
|
sc_recorder_close(struct sc_recorder *recorder) {
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
recorder->stopped = true;
|
recorder->stopped = true;
|
||||||
sc_cond_signal(&recorder->queue_cond);
|
sc_cond_signal(&recorder->queue_cond);
|
||||||
@@ -326,7 +326,7 @@ recorder_close(struct recorder *recorder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
sc_recorder_push(struct sc_recorder *recorder, const AVPacket *packet) {
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
assert(!recorder->stopped);
|
assert(!recorder->stopped);
|
||||||
|
|
||||||
@@ -336,7 +336,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct record_packet *rec = record_packet_new(packet);
|
struct sc_record_packet *rec = sc_record_packet_new(packet);
|
||||||
if (!rec) {
|
if (!rec) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
@@ -351,28 +351,30 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
sc_recorder_packet_sink_open(struct sc_packet_sink *sink,
|
||||||
struct recorder *recorder = DOWNCAST(sink);
|
const AVCodec *codec) {
|
||||||
return recorder_open(recorder, codec);
|
struct sc_recorder *recorder = DOWNCAST(sink);
|
||||||
|
return sc_recorder_open(recorder, codec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
recorder_packet_sink_close(struct sc_packet_sink *sink) {
|
sc_recorder_packet_sink_close(struct sc_packet_sink *sink) {
|
||||||
struct recorder *recorder = DOWNCAST(sink);
|
struct sc_recorder *recorder = DOWNCAST(sink);
|
||||||
recorder_close(recorder);
|
sc_recorder_close(recorder);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
|
sc_recorder_packet_sink_push(struct sc_packet_sink *sink,
|
||||||
struct recorder *recorder = DOWNCAST(sink);
|
const AVPacket *packet) {
|
||||||
return recorder_push(recorder, packet);
|
struct sc_recorder *recorder = DOWNCAST(sink);
|
||||||
|
return sc_recorder_push(recorder, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
recorder_init(struct recorder *recorder,
|
sc_recorder_init(struct sc_recorder *recorder,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
enum sc_record_format format,
|
enum sc_record_format format,
|
||||||
struct sc_size declared_frame_size) {
|
struct sc_size declared_frame_size) {
|
||||||
recorder->filename = strdup(filename);
|
recorder->filename = strdup(filename);
|
||||||
if (!recorder->filename) {
|
if (!recorder->filename) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
@@ -383,9 +385,9 @@ recorder_init(struct recorder *recorder,
|
|||||||
recorder->declared_frame_size = declared_frame_size;
|
recorder->declared_frame_size = declared_frame_size;
|
||||||
|
|
||||||
static const struct sc_packet_sink_ops ops = {
|
static const struct sc_packet_sink_ops ops = {
|
||||||
.open = recorder_packet_sink_open,
|
.open = sc_recorder_packet_sink_open,
|
||||||
.close = recorder_packet_sink_close,
|
.close = sc_recorder_packet_sink_close,
|
||||||
.push = recorder_packet_sink_push,
|
.push = sc_recorder_packet_sink_push,
|
||||||
};
|
};
|
||||||
|
|
||||||
recorder->packet_sink.ops = &ops;
|
recorder->packet_sink.ops = &ops;
|
||||||
@@ -394,6 +396,6 @@ recorder_init(struct recorder *recorder,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
recorder_destroy(struct recorder *recorder) {
|
sc_recorder_destroy(struct sc_recorder *recorder) {
|
||||||
free(recorder->filename);
|
free(recorder->filename);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef RECORDER_H
|
#ifndef SC_RECORDER_H
|
||||||
#define RECORDER_H
|
#define SC_RECORDER_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
@@ -12,14 +12,14 @@
|
|||||||
#include "util/queue.h"
|
#include "util/queue.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
|
|
||||||
struct record_packet {
|
struct sc_record_packet {
|
||||||
AVPacket *packet;
|
AVPacket *packet;
|
||||||
struct record_packet *next;
|
struct sc_record_packet *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct recorder_queue SC_QUEUE(struct record_packet);
|
struct sc_recorder_queue SC_QUEUE(struct sc_record_packet);
|
||||||
|
|
||||||
struct recorder {
|
struct sc_recorder {
|
||||||
struct sc_packet_sink packet_sink; // packet sink trait
|
struct sc_packet_sink packet_sink; // packet sink trait
|
||||||
|
|
||||||
char *filename;
|
char *filename;
|
||||||
@@ -33,20 +33,21 @@ struct recorder {
|
|||||||
sc_cond queue_cond;
|
sc_cond queue_cond;
|
||||||
bool stopped; // set on recorder_close()
|
bool stopped; // set on recorder_close()
|
||||||
bool failed; // set on packet write failure
|
bool failed; // set on packet write failure
|
||||||
struct recorder_queue queue;
|
struct sc_recorder_queue queue;
|
||||||
|
|
||||||
// we can write a packet only once we received the next one so that we can
|
// we can write a packet only once we received the next one so that we can
|
||||||
// set its duration (next_pts - current_pts)
|
// set its duration (next_pts - current_pts)
|
||||||
// "previous" is only accessed from the recorder thread, so it does not
|
// "previous" is only accessed from the recorder thread, so it does not
|
||||||
// need to be protected by the mutex
|
// need to be protected by the mutex
|
||||||
struct record_packet *previous;
|
struct sc_record_packet *previous;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
recorder_init(struct recorder *recorder, const char *filename,
|
sc_recorder_init(struct sc_recorder *recorder, const char *filename,
|
||||||
enum sc_record_format format, struct sc_size declared_frame_size);
|
enum sc_record_format format,
|
||||||
|
struct sc_size declared_frame_size);
|
||||||
|
|
||||||
void
|
void
|
||||||
recorder_destroy(struct recorder *recorder);
|
sc_recorder_destroy(struct sc_recorder *recorder);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
103
app/src/scrcpy.c
103
app/src/scrcpy.c
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include "decoder.h"
|
#include "decoder.h"
|
||||||
|
#include "demuxer.h"
|
||||||
#include "events.h"
|
#include "events.h"
|
||||||
#include "file_pusher.h"
|
#include "file_pusher.h"
|
||||||
#include "keyboard_inject.h"
|
#include "keyboard_inject.h"
|
||||||
@@ -22,7 +23,6 @@
|
|||||||
#include "recorder.h"
|
#include "recorder.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "stream.h"
|
|
||||||
#ifdef HAVE_USB
|
#ifdef HAVE_USB
|
||||||
# include "usb/aoa_hid.h"
|
# include "usb/aoa_hid.h"
|
||||||
# include "usb/hid_keyboard.h"
|
# include "usb/hid_keyboard.h"
|
||||||
@@ -39,9 +39,9 @@
|
|||||||
struct scrcpy {
|
struct scrcpy {
|
||||||
struct sc_server server;
|
struct sc_server server;
|
||||||
struct sc_screen screen;
|
struct sc_screen screen;
|
||||||
struct stream stream;
|
struct sc_demuxer demuxer;
|
||||||
struct decoder decoder;
|
struct sc_decoder decoder;
|
||||||
struct recorder recorder;
|
struct sc_recorder recorder;
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
struct sc_v4l2_sink v4l2_sink;
|
struct sc_v4l2_sink v4l2_sink;
|
||||||
#endif
|
#endif
|
||||||
@@ -143,10 +143,8 @@ sdl_configure(bool display, bool disable_screensaver) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (disable_screensaver) {
|
if (disable_screensaver) {
|
||||||
LOGD("Screensaver disabled");
|
|
||||||
SDL_DisableScreenSaver();
|
SDL_DisableScreenSaver();
|
||||||
} else {
|
} else {
|
||||||
LOGD("Screensaver enabled");
|
|
||||||
SDL_EnableScreenSaver();
|
SDL_EnableScreenSaver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,8 +229,8 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
stream_on_eos(struct stream *stream, void *userdata) {
|
sc_demuxer_on_eos(struct sc_demuxer *demuxer, void *userdata) {
|
||||||
(void) stream;
|
(void) demuxer;
|
||||||
(void) userdata;
|
(void) userdata;
|
||||||
|
|
||||||
PUSH_EVENT(EVENT_STREAM_STOPPED);
|
PUSH_EVENT(EVENT_STREAM_STOPPED);
|
||||||
@@ -271,7 +269,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
|
|
||||||
// Minimal SDL initialization
|
// Minimal SDL initialization
|
||||||
if (SDL_Init(SDL_INIT_EVENTS)) {
|
if (SDL_Init(SDL_INIT_EVENTS)) {
|
||||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
LOGE("Could not initialize SDL: %s", SDL_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +283,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
bool v4l2_sink_initialized = false;
|
bool v4l2_sink_initialized = false;
|
||||||
#endif
|
#endif
|
||||||
bool stream_started = false;
|
bool demuxer_started = false;
|
||||||
#ifdef HAVE_USB
|
#ifdef HAVE_USB
|
||||||
bool aoa_hid_initialized = false;
|
bool aoa_hid_initialized = false;
|
||||||
bool hid_keyboard_initialized = false;
|
bool hid_keyboard_initialized = false;
|
||||||
@@ -298,7 +296,9 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
struct sc_acksync *acksync = NULL;
|
struct sc_acksync *acksync = NULL;
|
||||||
|
|
||||||
struct sc_server_params params = {
|
struct sc_server_params params = {
|
||||||
.serial = options->serial,
|
.req_serial = options->serial,
|
||||||
|
.select_usb = options->select_usb,
|
||||||
|
.select_tcpip = options->select_tcpip,
|
||||||
.log_level = options->log_level,
|
.log_level = options->log_level,
|
||||||
.crop = options->crop,
|
.crop = options->crop,
|
||||||
.port_range = options->port_range,
|
.port_range = options->port_range,
|
||||||
@@ -320,6 +320,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
.downsize_on_error = options->downsize_on_error,
|
.downsize_on_error = options->downsize_on_error,
|
||||||
.tcpip = options->tcpip,
|
.tcpip = options->tcpip,
|
||||||
.tcpip_dst = options->tcpip_dst,
|
.tcpip_dst = options->tcpip_dst,
|
||||||
|
.cleanup = options->cleanup,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct sc_server_callbacks cbs = {
|
static const struct sc_server_callbacks cbs = {
|
||||||
@@ -343,7 +344,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
|
|
||||||
// Initialize SDL video in addition if display is enabled
|
// Initialize SDL video in addition if display is enabled
|
||||||
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
|
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
LOGE("Could not initialize SDL: %s", SDL_GetError());
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,7 +358,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
// It is necessarily initialized here, since the device is connected
|
// It is necessarily initialized here, since the device is connected
|
||||||
struct sc_server_info *info = &s->server.info;
|
struct sc_server_info *info = &s->server.info;
|
||||||
|
|
||||||
const char *serial = s->server.params.serial;
|
const char *serial = s->server.serial;
|
||||||
assert(serial);
|
assert(serial);
|
||||||
|
|
||||||
struct sc_file_pusher *fp = NULL;
|
struct sc_file_pusher *fp = NULL;
|
||||||
@@ -371,22 +372,22 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
file_pusher_initialized = true;
|
file_pusher_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct decoder *dec = NULL;
|
struct sc_decoder *dec = NULL;
|
||||||
bool needs_decoder = options->display;
|
bool needs_decoder = options->display;
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
needs_decoder |= !!options->v4l2_device;
|
needs_decoder |= !!options->v4l2_device;
|
||||||
#endif
|
#endif
|
||||||
if (needs_decoder) {
|
if (needs_decoder) {
|
||||||
decoder_init(&s->decoder);
|
sc_decoder_init(&s->decoder);
|
||||||
dec = &s->decoder;
|
dec = &s->decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct recorder *rec = NULL;
|
struct sc_recorder *rec = NULL;
|
||||||
if (options->record_filename) {
|
if (options->record_filename) {
|
||||||
if (!recorder_init(&s->recorder,
|
if (!sc_recorder_init(&s->recorder,
|
||||||
options->record_filename,
|
options->record_filename,
|
||||||
options->record_format,
|
options->record_format,
|
||||||
info->frame_size)) {
|
info->frame_size)) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
rec = &s->recorder;
|
rec = &s->recorder;
|
||||||
@@ -395,17 +396,17 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
|
|
||||||
av_log_set_callback(av_log_callback);
|
av_log_set_callback(av_log_callback);
|
||||||
|
|
||||||
static const struct stream_callbacks stream_cbs = {
|
static const struct sc_demuxer_callbacks demuxer_cbs = {
|
||||||
.on_eos = stream_on_eos,
|
.on_eos = sc_demuxer_on_eos,
|
||||||
};
|
};
|
||||||
stream_init(&s->stream, s->server.video_socket, &stream_cbs, NULL);
|
sc_demuxer_init(&s->demuxer, s->server.video_socket, &demuxer_cbs, NULL);
|
||||||
|
|
||||||
if (dec) {
|
if (dec) {
|
||||||
stream_add_sink(&s->stream, &dec->packet_sink);
|
sc_demuxer_add_sink(&s->demuxer, &dec->packet_sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rec) {
|
if (rec) {
|
||||||
stream_add_sink(&s->stream, &rec->packet_sink);
|
sc_demuxer_add_sink(&s->demuxer, &rec->packet_sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sc_controller *controller = NULL;
|
struct sc_controller *controller = NULL;
|
||||||
@@ -432,32 +433,19 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert(serial);
|
assert(serial);
|
||||||
struct sc_usb_device usb_devices[16];
|
struct sc_usb_device usb_device;
|
||||||
ssize_t count = sc_usb_find_devices(&s->usb, serial, usb_devices,
|
ok = sc_usb_select_device(&s->usb, serial, &usb_device);
|
||||||
ARRAY_LEN(usb_devices));
|
if (!ok) {
|
||||||
if (count <= 0) {
|
|
||||||
LOGE("Could not find USB device %s", serial);
|
|
||||||
sc_usb_destroy(&s->usb);
|
sc_usb_destroy(&s->usb);
|
||||||
sc_acksync_destroy(&s->acksync);
|
|
||||||
goto aoa_hid_end;
|
goto aoa_hid_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count > 1) {
|
|
||||||
LOGE("Multiple (%d) devices with serial %s", (int) count, serial);
|
|
||||||
sc_usb_device_destroy_all(usb_devices, count);
|
|
||||||
sc_usb_destroy(&s->usb);
|
|
||||||
sc_acksync_destroy(&s->acksync);
|
|
||||||
goto aoa_hid_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sc_usb_device *usb_device = &usb_devices[0];
|
|
||||||
|
|
||||||
LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
|
LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
|
||||||
usb_device->serial, usb_device->vid, usb_device->pid,
|
usb_device.serial, usb_device.vid, usb_device.pid,
|
||||||
usb_device->manufacturer, usb_device->product);
|
usb_device.manufacturer, usb_device.product);
|
||||||
|
|
||||||
ok = sc_usb_connect(&s->usb, usb_device->device, NULL, NULL);
|
ok = sc_usb_connect(&s->usb, usb_device.device, NULL, NULL);
|
||||||
sc_usb_device_destroy(usb_device);
|
sc_usb_device_destroy(&usb_device);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Failed to connect to USB device %s", serial);
|
LOGE("Failed to connect to USB device %s", serial);
|
||||||
sc_usb_destroy(&s->usb);
|
sc_usb_destroy(&s->usb);
|
||||||
@@ -600,6 +588,7 @@ aoa_hid_end:
|
|||||||
.rotation = options->rotation,
|
.rotation = options->rotation,
|
||||||
.mipmaps = options->mipmaps,
|
.mipmaps = options->mipmaps,
|
||||||
.fullscreen = options->fullscreen,
|
.fullscreen = options->fullscreen,
|
||||||
|
.start_fps_counter = options->start_fps_counter,
|
||||||
.buffering_time = options->display_buffer,
|
.buffering_time = options->display_buffer,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -608,7 +597,7 @@ aoa_hid_end:
|
|||||||
}
|
}
|
||||||
screen_initialized = true;
|
screen_initialized = true;
|
||||||
|
|
||||||
decoder_add_sink(&s->decoder, &s->screen.frame_sink);
|
sc_decoder_add_sink(&s->decoder, &s->screen.frame_sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
@@ -618,28 +607,28 @@ aoa_hid_end:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
|
sc_decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
|
||||||
|
|
||||||
v4l2_sink_initialized = true;
|
v4l2_sink_initialized = true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// now we consumed the header values, the socket receives the video stream
|
// now we consumed the header values, the socket receives the video stream
|
||||||
// start the stream
|
// start the demuxer
|
||||||
if (!stream_start(&s->stream)) {
|
if (!sc_demuxer_start(&s->demuxer)) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
stream_started = true;
|
demuxer_started = true;
|
||||||
|
|
||||||
ret = event_loop(s);
|
ret = event_loop(s);
|
||||||
LOGD("quit...");
|
LOGD("quit...");
|
||||||
|
|
||||||
// Close the window immediately on closing, because screen_destroy() may
|
// Close the window immediately on closing, because screen_destroy() may
|
||||||
// only be called once the stream thread is joined (it may take time)
|
// only be called once the demuxer thread is joined (it may take time)
|
||||||
sc_screen_hide_window(&s->screen);
|
sc_screen_hide_window(&s->screen);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
// The stream is not stopped explicitly, because it will stop by itself on
|
// The demuxer is not stopped explicitly, because it will stop by itself on
|
||||||
// end-of-stream
|
// end-of-stream
|
||||||
#ifdef HAVE_USB
|
#ifdef HAVE_USB
|
||||||
if (aoa_hid_initialized) {
|
if (aoa_hid_initialized) {
|
||||||
@@ -671,10 +660,10 @@ end:
|
|||||||
sc_server_stop(&s->server);
|
sc_server_stop(&s->server);
|
||||||
}
|
}
|
||||||
|
|
||||||
// now that the sockets are shutdown, the stream and controller are
|
// now that the sockets are shutdown, the demuxer and controller are
|
||||||
// interrupted, we can join them
|
// interrupted, we can join them
|
||||||
if (stream_started) {
|
if (demuxer_started) {
|
||||||
stream_join(&s->stream);
|
sc_demuxer_join(&s->demuxer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
@@ -693,7 +682,7 @@ end:
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Destroy the screen only after the stream is guaranteed to be finished,
|
// Destroy the screen only after the demuxer is guaranteed to be finished,
|
||||||
// because otherwise the screen could receive new frames after destruction
|
// because otherwise the screen could receive new frames after destruction
|
||||||
if (screen_initialized) {
|
if (screen_initialized) {
|
||||||
sc_screen_join(&s->screen);
|
sc_screen_join(&s->screen);
|
||||||
@@ -708,7 +697,7 @@ end:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (recorder_initialized) {
|
if (recorder_initialized) {
|
||||||
recorder_destroy(&s->recorder);
|
sc_recorder_destroy(&s->recorder);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_pusher_initialized) {
|
if (file_pusher_initialized) {
|
||||||
|
|||||||
@@ -163,14 +163,44 @@ sc_screen_is_relative_mode(struct sc_screen *screen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_screen_capture_mouse(struct sc_screen *screen, bool capture) {
|
sc_screen_set_mouse_capture(struct sc_screen *screen, bool capture) {
|
||||||
|
#ifdef __APPLE__
|
||||||
|
// Workaround for SDL bug on macOS:
|
||||||
|
// <https://github.com/libsdl-org/SDL/issues/5340>
|
||||||
|
if (capture) {
|
||||||
|
int mouse_x, mouse_y;
|
||||||
|
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
|
||||||
|
|
||||||
|
int x, y, w, h;
|
||||||
|
SDL_GetWindowPosition(screen->window, &x, &y);
|
||||||
|
SDL_GetWindowSize(screen->window, &w, &h);
|
||||||
|
|
||||||
|
bool outside_window = mouse_x < x || mouse_x >= x + w
|
||||||
|
|| mouse_y < y || mouse_y >= y + h;
|
||||||
|
if (outside_window) {
|
||||||
|
SDL_WarpMouseInWindow(screen->window, w / 2, h / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
(void) screen;
|
||||||
|
#endif
|
||||||
if (SDL_SetRelativeMouseMode(capture)) {
|
if (SDL_SetRelativeMouseMode(capture)) {
|
||||||
LOGE("Could not set relative mouse mode to %s: %s",
|
LOGE("Could not set relative mouse mode to %s: %s",
|
||||||
capture ? "true" : "false", SDL_GetError());
|
capture ? "true" : "false", SDL_GetError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
screen->mouse_captured = capture;
|
static inline bool
|
||||||
|
sc_screen_get_mouse_capture(struct sc_screen *screen) {
|
||||||
|
(void) screen;
|
||||||
|
return SDL_GetRelativeMouseMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
sc_screen_toggle_mouse_capture(struct sc_screen *screen) {
|
||||||
|
(void) screen;
|
||||||
|
bool new_value = !sc_screen_get_mouse_capture(screen);
|
||||||
|
sc_screen_set_mouse_capture(screen, new_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -340,7 +370,7 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
|
|||||||
|
|
||||||
bool need_new_event;
|
bool need_new_event;
|
||||||
if (previous_skipped) {
|
if (previous_skipped) {
|
||||||
fps_counter_add_skipped_frame(&screen->fps_counter);
|
sc_fps_counter_add_skipped_frame(&screen->fps_counter);
|
||||||
// The EVENT_NEW_FRAME triggered for the previous frame will consume
|
// The EVENT_NEW_FRAME triggered for the previous frame will consume
|
||||||
// this new frame instead, unless the previous event failed
|
// this new frame instead, unless the previous event failed
|
||||||
need_new_event = screen->event_failed;
|
need_new_event = screen->event_failed;
|
||||||
@@ -372,7 +402,6 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
screen->fullscreen = false;
|
screen->fullscreen = false;
|
||||||
screen->maximized = false;
|
screen->maximized = false;
|
||||||
screen->event_failed = false;
|
screen->event_failed = false;
|
||||||
screen->mouse_captured = false;
|
|
||||||
screen->mouse_capture_key_pressed = 0;
|
screen->mouse_capture_key_pressed = 0;
|
||||||
|
|
||||||
screen->req.x = params->window_x;
|
screen->req.x = params->window_x;
|
||||||
@@ -380,6 +409,7 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
screen->req.width = params->window_width;
|
screen->req.width = params->window_width;
|
||||||
screen->req.height = params->window_height;
|
screen->req.height = params->window_height;
|
||||||
screen->req.fullscreen = params->fullscreen;
|
screen->req.fullscreen = params->fullscreen;
|
||||||
|
screen->req.start_fps_counter = params->start_fps_counter;
|
||||||
|
|
||||||
static const struct sc_video_buffer_callbacks cbs = {
|
static const struct sc_video_buffer_callbacks cbs = {
|
||||||
.on_new_frame = sc_video_buffer_on_new_frame,
|
.on_new_frame = sc_video_buffer_on_new_frame,
|
||||||
@@ -396,7 +426,7 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
goto error_destroy_video_buffer;
|
goto error_destroy_video_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fps_counter_init(&screen->fps_counter)) {
|
if (!sc_fps_counter_init(&screen->fps_counter)) {
|
||||||
goto error_stop_and_join_video_buffer;
|
goto error_stop_and_join_video_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,14 +453,14 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
screen->window =
|
screen->window =
|
||||||
SDL_CreateWindow(params->window_title, 0, 0, 0, 0, window_flags);
|
SDL_CreateWindow(params->window_title, 0, 0, 0, 0, window_flags);
|
||||||
if (!screen->window) {
|
if (!screen->window) {
|
||||||
LOGC("Could not create window: %s", SDL_GetError());
|
LOGE("Could not create window: %s", SDL_GetError());
|
||||||
goto error_destroy_fps_counter;
|
goto error_destroy_fps_counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
screen->renderer = SDL_CreateRenderer(screen->window, -1,
|
screen->renderer = SDL_CreateRenderer(screen->window, -1,
|
||||||
SDL_RENDERER_ACCELERATED);
|
SDL_RENDERER_ACCELERATED);
|
||||||
if (!screen->renderer) {
|
if (!screen->renderer) {
|
||||||
LOGC("Could not create renderer: %s", SDL_GetError());
|
LOGE("Could not create renderer: %s", SDL_GetError());
|
||||||
goto error_destroy_window;
|
goto error_destroy_window;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,7 +509,7 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
params->frame_size.height);
|
params->frame_size.height);
|
||||||
screen->texture = create_texture(screen);
|
screen->texture = create_texture(screen);
|
||||||
if (!screen->texture) {
|
if (!screen->texture) {
|
||||||
LOGC("Could not create texture: %s", SDL_GetError());
|
LOGE("Could not create texture: %s", SDL_GetError());
|
||||||
goto error_destroy_renderer;
|
goto error_destroy_renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,7 +558,7 @@ error_destroy_renderer:
|
|||||||
error_destroy_window:
|
error_destroy_window:
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
error_destroy_fps_counter:
|
error_destroy_fps_counter:
|
||||||
fps_counter_destroy(&screen->fps_counter);
|
sc_fps_counter_destroy(&screen->fps_counter);
|
||||||
error_stop_and_join_video_buffer:
|
error_stop_and_join_video_buffer:
|
||||||
sc_video_buffer_stop(&screen->vb);
|
sc_video_buffer_stop(&screen->vb);
|
||||||
sc_video_buffer_join(&screen->vb);
|
sc_video_buffer_join(&screen->vb);
|
||||||
@@ -556,6 +586,10 @@ sc_screen_show_initial_window(struct sc_screen *screen) {
|
|||||||
sc_screen_switch_fullscreen(screen);
|
sc_screen_switch_fullscreen(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (screen->req.start_fps_counter) {
|
||||||
|
sc_fps_counter_start(&screen->fps_counter);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_ShowWindow(screen->window);
|
SDL_ShowWindow(screen->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,13 +601,13 @@ sc_screen_hide_window(struct sc_screen *screen) {
|
|||||||
void
|
void
|
||||||
sc_screen_interrupt(struct sc_screen *screen) {
|
sc_screen_interrupt(struct sc_screen *screen) {
|
||||||
sc_video_buffer_stop(&screen->vb);
|
sc_video_buffer_stop(&screen->vb);
|
||||||
fps_counter_interrupt(&screen->fps_counter);
|
sc_fps_counter_interrupt(&screen->fps_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_screen_join(struct sc_screen *screen) {
|
sc_screen_join(struct sc_screen *screen) {
|
||||||
sc_video_buffer_join(&screen->vb);
|
sc_video_buffer_join(&screen->vb);
|
||||||
fps_counter_join(&screen->fps_counter);
|
sc_fps_counter_join(&screen->fps_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -585,7 +619,7 @@ sc_screen_destroy(struct sc_screen *screen) {
|
|||||||
SDL_DestroyTexture(screen->texture);
|
SDL_DestroyTexture(screen->texture);
|
||||||
SDL_DestroyRenderer(screen->renderer);
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
fps_counter_destroy(&screen->fps_counter);
|
sc_fps_counter_destroy(&screen->fps_counter);
|
||||||
sc_video_buffer_destroy(&screen->vb);
|
sc_video_buffer_destroy(&screen->vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -666,7 +700,7 @@ prepare_for_frame(struct sc_screen *screen, struct sc_size new_frame_size) {
|
|||||||
screen->frame_size.width, screen->frame_size.height);
|
screen->frame_size.width, screen->frame_size.height);
|
||||||
screen->texture = create_texture(screen);
|
screen->texture = create_texture(screen);
|
||||||
if (!screen->texture) {
|
if (!screen->texture) {
|
||||||
LOGC("Could not create texture: %s", SDL_GetError());
|
LOGE("Could not create texture: %s", SDL_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -695,7 +729,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
|
|||||||
sc_video_buffer_consume(&screen->vb, screen->frame);
|
sc_video_buffer_consume(&screen->vb, screen->frame);
|
||||||
AVFrame *frame = screen->frame;
|
AVFrame *frame = screen->frame;
|
||||||
|
|
||||||
fps_counter_add_rendered_frame(&screen->fps_counter);
|
sc_fps_counter_add_rendered_frame(&screen->fps_counter);
|
||||||
|
|
||||||
struct sc_size new_frame_size = {frame->width, frame->height};
|
struct sc_size new_frame_size = {frame->width, frame->height};
|
||||||
if (!prepare_for_frame(screen, new_frame_size)) {
|
if (!prepare_for_frame(screen, new_frame_size)) {
|
||||||
@@ -710,7 +744,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
|
|||||||
|
|
||||||
if (sc_screen_is_relative_mode(screen)) {
|
if (sc_screen_is_relative_mode(screen)) {
|
||||||
// Capture mouse on start
|
// Capture mouse on start
|
||||||
sc_screen_capture_mouse(screen, true);
|
sc_screen_set_mouse_capture(screen, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -823,7 +857,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||||||
break;
|
break;
|
||||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||||
if (relative_mode) {
|
if (relative_mode) {
|
||||||
sc_screen_capture_mouse(screen, false);
|
sc_screen_set_mouse_capture(screen, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -853,8 +887,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||||||
if (key == cap) {
|
if (key == cap) {
|
||||||
// A mouse capture key has been pressed then released:
|
// A mouse capture key has been pressed then released:
|
||||||
// toggle the capture mouse mode
|
// toggle the capture mouse mode
|
||||||
sc_screen_capture_mouse(screen,
|
sc_screen_toggle_mouse_capture(screen);
|
||||||
!screen->mouse_captured);
|
|
||||||
}
|
}
|
||||||
// Mouse capture keys are never forwarded to the device
|
// Mouse capture keys are never forwarded to the device
|
||||||
return;
|
return;
|
||||||
@@ -864,7 +897,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||||||
case SDL_MOUSEWHEEL:
|
case SDL_MOUSEWHEEL:
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
if (relative_mode && !screen->mouse_captured) {
|
if (relative_mode && !sc_screen_get_mouse_capture(screen)) {
|
||||||
// Do not forward to input manager, the mouse will be captured
|
// Do not forward to input manager, the mouse will be captured
|
||||||
// on SDL_MOUSEBUTTONUP
|
// on SDL_MOUSEBUTTONUP
|
||||||
return;
|
return;
|
||||||
@@ -880,8 +913,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if (relative_mode && !screen->mouse_captured) {
|
if (relative_mode && !sc_screen_get_mouse_capture(screen)) {
|
||||||
sc_screen_capture_mouse(screen, true);
|
sc_screen_set_mouse_capture(screen, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ struct sc_screen {
|
|||||||
|
|
||||||
struct sc_input_manager im;
|
struct sc_input_manager im;
|
||||||
struct sc_video_buffer vb;
|
struct sc_video_buffer vb;
|
||||||
struct fps_counter fps_counter;
|
struct sc_fps_counter fps_counter;
|
||||||
|
|
||||||
// The initial requested window properties
|
// The initial requested window properties
|
||||||
struct {
|
struct {
|
||||||
@@ -35,6 +35,7 @@ struct sc_screen {
|
|||||||
uint16_t width;
|
uint16_t width;
|
||||||
uint16_t height;
|
uint16_t height;
|
||||||
bool fullscreen;
|
bool fullscreen;
|
||||||
|
bool start_fps_counter;
|
||||||
} req;
|
} req;
|
||||||
|
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
@@ -60,7 +61,6 @@ struct sc_screen {
|
|||||||
|
|
||||||
bool event_failed; // in case SDL_PushEvent() returned an error
|
bool event_failed; // in case SDL_PushEvent() returned an error
|
||||||
|
|
||||||
bool mouse_captured; // only relevant in relative mouse mode
|
|
||||||
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
|
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
|
||||||
// RGUI) must be pressed. This variable tracks the pressed capture key.
|
// RGUI) must be pressed. This variable tracks the pressed capture key.
|
||||||
SDL_Keycode mouse_capture_key_pressed;
|
SDL_Keycode mouse_capture_key_pressed;
|
||||||
@@ -94,6 +94,7 @@ struct sc_screen_params {
|
|||||||
bool mipmaps;
|
bool mipmaps;
|
||||||
|
|
||||||
bool fullscreen;
|
bool fullscreen;
|
||||||
|
bool start_fps_counter;
|
||||||
|
|
||||||
sc_tick buffering_time;
|
sc_tick buffering_time;
|
||||||
};
|
};
|
||||||
|
|||||||
261
app/src/server.c
261
app/src/server.c
@@ -7,7 +7,7 @@
|
|||||||
#include <SDL2/SDL_timer.h>
|
#include <SDL2/SDL_timer.h>
|
||||||
#include <SDL2/SDL_platform.h>
|
#include <SDL2/SDL_platform.h>
|
||||||
|
|
||||||
#include "adb.h"
|
#include "adb/adb.h"
|
||||||
#include "util/file.h"
|
#include "util/file.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/net_intr.h"
|
#include "util/net_intr.h"
|
||||||
@@ -65,7 +65,7 @@ get_server_path(void) {
|
|||||||
static void
|
static void
|
||||||
sc_server_params_destroy(struct sc_server_params *params) {
|
sc_server_params_destroy(struct sc_server_params *params) {
|
||||||
// The server stores a copy of the params provided by the user
|
// The server stores a copy of the params provided by the user
|
||||||
free((char *) params->serial);
|
free((char *) params->req_serial);
|
||||||
free((char *) params->crop);
|
free((char *) params->crop);
|
||||||
free((char *) params->codec_options);
|
free((char *) params->codec_options);
|
||||||
free((char *) params->encoder_name);
|
free((char *) params->encoder_name);
|
||||||
@@ -89,7 +89,7 @@ sc_server_params_copy(struct sc_server_params *dst,
|
|||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
COPY(serial);
|
COPY(req_serial);
|
||||||
COPY(crop);
|
COPY(crop);
|
||||||
COPY(codec_options);
|
COPY(codec_options);
|
||||||
COPY(encoder_name);
|
COPY(encoder_name);
|
||||||
@@ -114,7 +114,7 @@ push_server(struct sc_intr *intr, const char *serial) {
|
|||||||
free(server_path);
|
free(server_path);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);
|
bool ok = sc_adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);
|
||||||
free(server_path);
|
free(server_path);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@@ -157,8 +157,14 @@ execute_server(struct sc_server *server,
|
|||||||
const struct sc_server_params *params) {
|
const struct sc_server_params *params) {
|
||||||
sc_pid pid = SC_PROCESS_NONE;
|
sc_pid pid = SC_PROCESS_NONE;
|
||||||
|
|
||||||
|
const char *serial = server->serial;
|
||||||
|
assert(serial);
|
||||||
|
|
||||||
const char *cmd[128];
|
const char *cmd[128];
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
|
cmd[count++] = sc_adb_get_executable();
|
||||||
|
cmd[count++] = "-s";
|
||||||
|
cmd[count++] = serial;
|
||||||
cmd[count++] = "shell";
|
cmd[count++] = "shell";
|
||||||
cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
|
cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
|
||||||
cmd[count++] = "app_process";
|
cmd[count++] = "app_process";
|
||||||
@@ -238,9 +244,14 @@ execute_server(struct sc_server *server,
|
|||||||
// By default, downsize_on_error is true
|
// By default, downsize_on_error is true
|
||||||
ADD_PARAM("downsize_on_error=false");
|
ADD_PARAM("downsize_on_error=false");
|
||||||
}
|
}
|
||||||
|
if (!params->cleanup) {
|
||||||
|
// By default, cleanup is true
|
||||||
|
ADD_PARAM("cleanup=false");
|
||||||
|
}
|
||||||
|
|
||||||
#undef ADD_PARAM
|
#undef ADD_PARAM
|
||||||
#undef STRBOOL
|
|
||||||
|
cmd[count++] = NULL;
|
||||||
|
|
||||||
#ifdef SERVER_DEBUGGER
|
#ifdef SERVER_DEBUGGER
|
||||||
LOGI("Server debugger waiting for a client on device port "
|
LOGI("Server debugger waiting for a client on device port "
|
||||||
@@ -254,7 +265,7 @@ execute_server(struct sc_server *server,
|
|||||||
// Then click on "Debug"
|
// Then click on "Debug"
|
||||||
#endif
|
#endif
|
||||||
// Inherit both stdout and stderr (all server logs are printed to stdout)
|
// Inherit both stdout and stderr (all server logs are printed to stdout)
|
||||||
pid = adb_execute(params->serial, cmd, count, 0);
|
pid = sc_adb_execute(cmd, 0);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
for (unsigned i = dyn_idx; i < count; ++i) {
|
for (unsigned i = dyn_idx; i < count; ++i) {
|
||||||
@@ -346,6 +357,7 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server->serial = NULL;
|
||||||
server->stopped = false;
|
server->stopped = false;
|
||||||
|
|
||||||
server->video_socket = SC_SOCKET_NONE;
|
server->video_socket = SC_SOCKET_NONE;
|
||||||
@@ -390,7 +402,9 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||||||
|
|
||||||
assert(tunnel->enabled);
|
assert(tunnel->enabled);
|
||||||
|
|
||||||
const char *serial = server->params.serial;
|
const char *serial = server->serial;
|
||||||
|
assert(serial);
|
||||||
|
|
||||||
bool control = server->params.control;
|
bool control = server->params.control;
|
||||||
|
|
||||||
sc_socket video_socket = SC_SOCKET_NONE;
|
sc_socket video_socket = SC_SOCKET_NONE;
|
||||||
@@ -472,8 +486,10 @@ fail:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always leave this function with tunnel disabled
|
if (tunnel->enabled) {
|
||||||
sc_adb_tunnel_close(tunnel, &server->intr, serial);
|
// Always leave this function with tunnel disabled
|
||||||
|
sc_adb_tunnel_close(tunnel, &server->intr, serial);
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -494,32 +510,11 @@ sc_server_on_terminated(void *userdata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
sc_server_fill_serial(struct sc_server *server) {
|
is_tcpip_mode_enabled(struct sc_server *server, const char *serial) {
|
||||||
// Retrieve the actual device immediately if not provided, so that all
|
|
||||||
// future adb commands are executed for this specific device, even if other
|
|
||||||
// devices are connected afterwards (without "more than one
|
|
||||||
// device/emulator" error)
|
|
||||||
if (!server->params.serial) {
|
|
||||||
// The serial is owned by sc_server_params, and will be freed on destroy
|
|
||||||
server->params.serial = adb_get_serialno(&server->intr, 0);
|
|
||||||
if (!server->params.serial) {
|
|
||||||
LOGE("Could not get device serial");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGD("Device serial: %s", server->params.serial);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
is_tcpip_mode_enabled(struct sc_server *server) {
|
|
||||||
struct sc_intr *intr = &server->intr;
|
struct sc_intr *intr = &server->intr;
|
||||||
const char *serial = server->params.serial;
|
|
||||||
|
|
||||||
char *current_port =
|
char *current_port =
|
||||||
adb_getprop(intr, serial, "service.adb.tcp.port", SC_ADB_SILENT);
|
sc_adb_getprop(intr, serial, "service.adb.tcp.port", SC_ADB_SILENT);
|
||||||
if (!current_port) {
|
if (!current_port) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -531,9 +526,9 @@ is_tcpip_mode_enabled(struct sc_server *server) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
wait_tcpip_mode_enabled(struct sc_server *server, unsigned attempts,
|
wait_tcpip_mode_enabled(struct sc_server *server, const char *serial,
|
||||||
sc_tick delay) {
|
unsigned attempts, sc_tick delay) {
|
||||||
if (is_tcpip_mode_enabled(server)) {
|
if (is_tcpip_mode_enabled(server, serial)) {
|
||||||
LOGI("TCP/IP mode enabled");
|
LOGI("TCP/IP mode enabled");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -548,7 +543,7 @@ wait_tcpip_mode_enabled(struct sc_server *server, unsigned attempts,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_tcpip_mode_enabled(server)) {
|
if (is_tcpip_mode_enabled(server, serial)) {
|
||||||
LOGI("TCP/IP mode enabled");
|
LOGI("TCP/IP mode enabled");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -573,29 +568,30 @@ append_port_5555(const char *ip) {
|
|||||||
return ip_port;
|
return ip_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static char *
|
||||||
sc_server_switch_to_tcpip(struct sc_server *server, char **out_ip_port) {
|
sc_server_switch_to_tcpip(struct sc_server *server, const char *serial) {
|
||||||
const char *serial = server->params.serial;
|
|
||||||
assert(serial);
|
assert(serial);
|
||||||
|
|
||||||
struct sc_intr *intr = &server->intr;
|
struct sc_intr *intr = &server->intr;
|
||||||
|
|
||||||
char *ip = adb_get_device_ip(intr, serial, 0);
|
LOGI("Switching device %s to TCP/IP...", serial);
|
||||||
|
|
||||||
|
char *ip = sc_adb_get_device_ip(intr, serial, 0);
|
||||||
if (!ip) {
|
if (!ip) {
|
||||||
LOGE("Device IP not found");
|
LOGE("Device IP not found");
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ip_port = append_port_5555(ip);
|
char *ip_port = append_port_5555(ip);
|
||||||
free(ip);
|
free(ip);
|
||||||
if (!ip_port) {
|
if (!ip_port) {
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tcp_mode = is_tcpip_mode_enabled(server);
|
bool tcp_mode = is_tcpip_mode_enabled(server, serial);
|
||||||
|
|
||||||
if (!tcp_mode) {
|
if (!tcp_mode) {
|
||||||
bool ok = adb_tcpip(intr, serial, 5555, SC_ADB_NO_STDOUT);
|
bool ok = sc_adb_tcpip(intr, serial, 5555, SC_ADB_NO_STDOUT);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not restart adbd in TCP/IP mode");
|
LOGE("Could not restart adbd in TCP/IP mode");
|
||||||
goto error;
|
goto error;
|
||||||
@@ -603,19 +599,17 @@ sc_server_switch_to_tcpip(struct sc_server *server, char **out_ip_port) {
|
|||||||
|
|
||||||
unsigned attempts = 40;
|
unsigned attempts = 40;
|
||||||
sc_tick delay = SC_TICK_FROM_MS(250);
|
sc_tick delay = SC_TICK_FROM_MS(250);
|
||||||
ok = wait_tcpip_mode_enabled(server, attempts, delay);
|
ok = wait_tcpip_mode_enabled(server, serial, attempts, delay);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_ip_port = ip_port;
|
return ip_port;
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
free(ip_port);
|
free(ip_port);
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@@ -623,73 +617,52 @@ sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
|
|||||||
struct sc_intr *intr = &server->intr;
|
struct sc_intr *intr = &server->intr;
|
||||||
|
|
||||||
// Error expected if not connected, do not report any error
|
// Error expected if not connected, do not report any error
|
||||||
adb_disconnect(intr, ip_port, SC_ADB_SILENT);
|
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
|
||||||
|
|
||||||
bool ok = adb_connect(intr, ip_port, 0);
|
LOGI("Connecting to %s...", ip_port);
|
||||||
|
|
||||||
|
bool ok = sc_adb_connect(intr, ip_port, 0);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not connect to %s", ip_port);
|
LOGE("Could not connect to %s", ip_port);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override the serial, owned by the sc_server_params
|
|
||||||
free((void *) server->params.serial);
|
|
||||||
server->params.serial = strdup(ip_port);
|
|
||||||
if (!server->params.serial) {
|
|
||||||
LOG_OOM();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGI("Connected to %s", ip_port);
|
LOGI("Connected to %s", ip_port);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
sc_server_configure_tcpip(struct sc_server *server) {
|
sc_server_configure_tcpip_known_address(struct sc_server *server,
|
||||||
char *ip_port;
|
const char *addr) {
|
||||||
|
// Append ":5555" if no port is present
|
||||||
const struct sc_server_params *params = &server->params;
|
bool contains_port = strchr(addr, ':');
|
||||||
|
char *ip_port = contains_port ? strdup(addr) : append_port_5555(addr);
|
||||||
// If tcpip parameter is given, then it must connect to this address.
|
if (!ip_port) {
|
||||||
// Therefore, the device is unknown, so serial is meaningless at this point.
|
LOG_OOM();
|
||||||
assert(!params->serial || !params->tcpip_dst);
|
return false;
|
||||||
|
|
||||||
if (params->tcpip_dst) {
|
|
||||||
// Append ":5555" if no port is present
|
|
||||||
bool contains_port = strchr(params->tcpip_dst, ':');
|
|
||||||
ip_port = contains_port ? strdup(params->tcpip_dst)
|
|
||||||
: append_port_5555(params->tcpip_dst);
|
|
||||||
if (!ip_port) {
|
|
||||||
LOG_OOM();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// The device IP address must be retrieved from the current
|
|
||||||
// connected device
|
|
||||||
if (!sc_server_fill_serial(server)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The serial is either the real serial when connected via USB, or
|
|
||||||
// the IP:PORT when connected over TCP/IP. Only the latter contains
|
|
||||||
// a colon.
|
|
||||||
bool is_already_tcpip = strchr(params->serial, ':');
|
|
||||||
if (is_already_tcpip) {
|
|
||||||
// Nothing to do
|
|
||||||
LOGI("Device already connected via TCP/IP: %s", params->serial);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = sc_server_switch_to_tcpip(server, &ip_port);
|
|
||||||
if (!ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// On success, this call changes params->serial
|
server->serial = ip_port;
|
||||||
bool ok = sc_server_connect_to_tcpip(server, ip_port);
|
return sc_server_connect_to_tcpip(server, ip_port);
|
||||||
free(ip_port);
|
}
|
||||||
return ok;
|
|
||||||
|
static bool
|
||||||
|
sc_server_configure_tcpip_unknown_address(struct sc_server *server,
|
||||||
|
const char *serial) {
|
||||||
|
bool is_already_tcpip = sc_adb_is_serial_tcpip(serial);
|
||||||
|
if (is_already_tcpip) {
|
||||||
|
// Nothing to do
|
||||||
|
LOGI("Device already connected via TCP/IP: %s", serial);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ip_port = sc_server_switch_to_tcpip(server, serial);
|
||||||
|
if (!ip_port) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
server->serial = ip_port;
|
||||||
|
return sc_server_connect_to_tcpip(server, ip_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -698,30 +671,83 @@ run_server(void *data) {
|
|||||||
|
|
||||||
const struct sc_server_params *params = &server->params;
|
const struct sc_server_params *params = &server->params;
|
||||||
|
|
||||||
if (params->serial) {
|
// Execute "adb start-server" before "adb devices" so that daemon starting
|
||||||
LOGD("Device serial: %s", params->serial);
|
// output/errors is correctly printed in the console ("adb devices" output
|
||||||
|
// is parsed, so it is not output)
|
||||||
|
bool ok = sc_adb_start_server(&server->intr, 0);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not start adb daemon");
|
||||||
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params->tcpip) {
|
// params->tcpip_dst implies params->tcpip
|
||||||
// params->serial may be changed after this call
|
assert(!params->tcpip_dst || params->tcpip);
|
||||||
bool ok = sc_server_configure_tcpip(server);
|
|
||||||
|
// If tcpip_dst parameter is given, then it must connect to this address.
|
||||||
|
// Therefore, the device is unknown, so serial is meaningless at this point.
|
||||||
|
assert(!params->req_serial || !params->tcpip_dst);
|
||||||
|
|
||||||
|
// A device must be selected via a serial in all cases except when --tcpip=
|
||||||
|
// is called with a parameter (in that case, the device may initially not
|
||||||
|
// exist, and scrcpy will execute "adb connect").
|
||||||
|
bool need_initial_serial = !params->tcpip_dst;
|
||||||
|
|
||||||
|
if (need_initial_serial) {
|
||||||
|
// At most one of the 3 following parameters may be set
|
||||||
|
assert(!!params->req_serial
|
||||||
|
+ params->select_usb
|
||||||
|
+ params->select_tcpip <= 1);
|
||||||
|
|
||||||
|
struct sc_adb_device_selector selector;
|
||||||
|
if (params->req_serial) {
|
||||||
|
selector.type = SC_ADB_DEVICE_SELECT_SERIAL;
|
||||||
|
selector.serial = params->req_serial;
|
||||||
|
} else if (params->select_usb) {
|
||||||
|
selector.type = SC_ADB_DEVICE_SELECT_USB;
|
||||||
|
} else if (params->select_tcpip) {
|
||||||
|
selector.type = SC_ADB_DEVICE_SELECT_TCPIP;
|
||||||
|
} else {
|
||||||
|
selector.type = SC_ADB_DEVICE_SELECT_ALL;
|
||||||
|
}
|
||||||
|
struct sc_adb_device device;
|
||||||
|
ok = sc_adb_select_device(&server->intr, &selector, 0, &device);
|
||||||
|
if (!ok) {
|
||||||
|
goto error_connection_failed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params->tcpip) {
|
||||||
|
assert(!params->tcpip_dst);
|
||||||
|
ok = sc_server_configure_tcpip_unknown_address(server,
|
||||||
|
device.serial);
|
||||||
|
sc_adb_device_destroy(&device);
|
||||||
|
if (!ok) {
|
||||||
|
goto error_connection_failed;
|
||||||
|
}
|
||||||
|
assert(server->serial);
|
||||||
|
} else {
|
||||||
|
// "move" the device.serial without copy
|
||||||
|
server->serial = device.serial;
|
||||||
|
// the serial must not be freed by the destructor
|
||||||
|
device.serial = NULL;
|
||||||
|
sc_adb_device_destroy(&device);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ok = sc_server_configure_tcpip_known_address(server, params->tcpip_dst);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// It is ok to call this function even if the device serial has been
|
const char *serial = server->serial;
|
||||||
// changed by switching over TCP/IP
|
assert(serial);
|
||||||
if (!sc_server_fill_serial(server)) {
|
LOGD("Device serial: %s", serial);
|
||||||
goto error_connection_failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = push_server(&server->intr, params->serial);
|
ok = push_server(&server->intr, serial);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, params->serial,
|
ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, serial,
|
||||||
params->port_range, params->force_adb_forward);
|
params->port_range, params->force_adb_forward);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
@@ -730,7 +756,7 @@ run_server(void *data) {
|
|||||||
// server will connect to our server socket
|
// server will connect to our server socket
|
||||||
sc_pid pid = execute_server(server, params);
|
sc_pid pid = execute_server(server, params);
|
||||||
if (pid == SC_PROCESS_NONE) {
|
if (pid == SC_PROCESS_NONE) {
|
||||||
sc_adb_tunnel_close(&server->tunnel, &server->intr, params->serial);
|
sc_adb_tunnel_close(&server->tunnel, &server->intr, serial);
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -742,7 +768,7 @@ run_server(void *data) {
|
|||||||
if (!ok) {
|
if (!ok) {
|
||||||
sc_process_terminate(pid);
|
sc_process_terminate(pid);
|
||||||
sc_process_wait(pid, true); // ignore exit code
|
sc_process_wait(pid, true); // ignore exit code
|
||||||
sc_adb_tunnel_close(&server->tunnel, &server->intr, params->serial);
|
sc_adb_tunnel_close(&server->tunnel, &server->intr, serial);
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -835,6 +861,7 @@ sc_server_destroy(struct sc_server *server) {
|
|||||||
net_close(server->control_socket);
|
net_close(server->control_socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(server->serial);
|
||||||
sc_server_params_destroy(&server->params);
|
sc_server_params_destroy(&server->params);
|
||||||
sc_intr_destroy(&server->intr);
|
sc_intr_destroy(&server->intr);
|
||||||
sc_cond_destroy(&server->cond_stopped);
|
sc_cond_destroy(&server->cond_stopped);
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "adb.h"
|
#include "adb/adb_tunnel.h"
|
||||||
#include "adb_tunnel.h"
|
|
||||||
#include "coords.h"
|
#include "coords.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "util/intr.h"
|
#include "util/intr.h"
|
||||||
@@ -23,7 +22,7 @@ struct sc_server_info {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct sc_server_params {
|
struct sc_server_params {
|
||||||
const char *serial;
|
const char *req_serial;
|
||||||
enum sc_log_level log_level;
|
enum sc_log_level log_level;
|
||||||
const char *crop;
|
const char *crop;
|
||||||
const char *codec_options;
|
const char *codec_options;
|
||||||
@@ -45,11 +44,15 @@ struct sc_server_params {
|
|||||||
bool downsize_on_error;
|
bool downsize_on_error;
|
||||||
bool tcpip;
|
bool tcpip;
|
||||||
const char *tcpip_dst;
|
const char *tcpip_dst;
|
||||||
|
bool select_usb;
|
||||||
|
bool select_tcpip;
|
||||||
|
bool cleanup;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sc_server {
|
struct sc_server {
|
||||||
// The internal allocated strings are copies owned by the server
|
// The internal allocated strings are copies owned by the server
|
||||||
struct sc_server_params params;
|
struct sc_server_params params;
|
||||||
|
char *serial;
|
||||||
|
|
||||||
sc_thread thread;
|
sc_thread thread;
|
||||||
struct sc_server_info info; // initialized once connected
|
struct sc_server_info info; // initialized once connected
|
||||||
|
|||||||
298
app/src/stream.c
298
app/src/stream.c
@@ -1,298 +0,0 @@
|
|||||||
#include "stream.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <libavutil/time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "decoder.h"
|
|
||||||
#include "events.h"
|
|
||||||
#include "recorder.h"
|
|
||||||
#include "util/buffer_util.h"
|
|
||||||
#include "util/log.h"
|
|
||||||
|
|
||||||
#define BUFSIZE 0x10000
|
|
||||||
|
|
||||||
#define HEADER_SIZE 12
|
|
||||||
#define NO_PTS UINT64_C(-1)
|
|
||||||
|
|
||||||
static bool
|
|
||||||
stream_recv_packet(struct stream *stream, AVPacket *packet) {
|
|
||||||
// The video stream contains raw packets, without time information. When we
|
|
||||||
// record, we retrieve the timestamps separately, from a "meta" header
|
|
||||||
// added by the server before each raw packet.
|
|
||||||
//
|
|
||||||
// The "meta" header length is 12 bytes:
|
|
||||||
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
|
|
||||||
// <-------------> <-----> <-----------------------------...
|
|
||||||
// PTS packet raw packet
|
|
||||||
// size
|
|
||||||
//
|
|
||||||
// It is followed by <packet_size> bytes containing the packet/frame.
|
|
||||||
|
|
||||||
uint8_t header[HEADER_SIZE];
|
|
||||||
ssize_t r = net_recv_all(stream->socket, header, HEADER_SIZE);
|
|
||||||
if (r < HEADER_SIZE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t pts = buffer_read64be(header);
|
|
||||||
uint32_t len = buffer_read32be(&header[8]);
|
|
||||||
assert(pts == NO_PTS || (pts & 0x8000000000000000) == 0);
|
|
||||||
assert(len);
|
|
||||||
|
|
||||||
if (av_new_packet(packet, len)) {
|
|
||||||
LOG_OOM();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = net_recv_all(stream->socket, packet->data, len);
|
|
||||||
if (r < 0 || ((uint32_t) r) < len) {
|
|
||||||
av_packet_unref(packet);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
packet->pts = pts != NO_PTS ? (int64_t) pts : AV_NOPTS_VALUE;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
push_packet_to_sinks(struct stream *stream, const AVPacket *packet) {
|
|
||||||
for (unsigned i = 0; i < stream->sink_count; ++i) {
|
|
||||||
struct sc_packet_sink *sink = stream->sinks[i];
|
|
||||||
if (!sink->ops->push(sink, packet)) {
|
|
||||||
LOGE("Could not send config packet to sink %d", i);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
stream_parse(struct stream *stream, AVPacket *packet) {
|
|
||||||
uint8_t *in_data = packet->data;
|
|
||||||
int in_len = packet->size;
|
|
||||||
uint8_t *out_data = NULL;
|
|
||||||
int out_len = 0;
|
|
||||||
int r = av_parser_parse2(stream->parser, stream->codec_ctx,
|
|
||||||
&out_data, &out_len, in_data, in_len,
|
|
||||||
AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
|
|
||||||
|
|
||||||
// PARSER_FLAG_COMPLETE_FRAMES is set
|
|
||||||
assert(r == in_len);
|
|
||||||
(void) r;
|
|
||||||
assert(out_len == in_len);
|
|
||||||
|
|
||||||
if (stream->parser->key_frame == 1) {
|
|
||||||
packet->flags |= AV_PKT_FLAG_KEY;
|
|
||||||
}
|
|
||||||
|
|
||||||
packet->dts = packet->pts;
|
|
||||||
|
|
||||||
bool ok = push_packet_to_sinks(stream, packet);
|
|
||||||
if (!ok) {
|
|
||||||
LOGE("Could not process packet");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
stream_push_packet(struct stream *stream, AVPacket *packet) {
|
|
||||||
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
|
||||||
|
|
||||||
// A config packet must not be decoded immediately (it contains no
|
|
||||||
// frame); instead, it must be concatenated with the future data packet.
|
|
||||||
if (stream->pending || is_config) {
|
|
||||||
size_t offset;
|
|
||||||
if (stream->pending) {
|
|
||||||
offset = stream->pending->size;
|
|
||||||
if (av_grow_packet(stream->pending, packet->size)) {
|
|
||||||
LOG_OOM();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
offset = 0;
|
|
||||||
stream->pending = av_packet_alloc();
|
|
||||||
if (!stream->pending) {
|
|
||||||
LOG_OOM();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (av_new_packet(stream->pending, packet->size)) {
|
|
||||||
LOG_OOM();
|
|
||||||
av_packet_free(&stream->pending);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(stream->pending->data + offset, packet->data, packet->size);
|
|
||||||
|
|
||||||
if (!is_config) {
|
|
||||||
// prepare the concat packet to send to the decoder
|
|
||||||
stream->pending->pts = packet->pts;
|
|
||||||
stream->pending->dts = packet->dts;
|
|
||||||
stream->pending->flags = packet->flags;
|
|
||||||
packet = stream->pending;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_config) {
|
|
||||||
// config packet
|
|
||||||
bool ok = push_packet_to_sinks(stream, packet);
|
|
||||||
if (!ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// data packet
|
|
||||||
bool ok = stream_parse(stream, packet);
|
|
||||||
|
|
||||||
if (stream->pending) {
|
|
||||||
// the pending packet must be discarded (consumed or error)
|
|
||||||
av_packet_free(&stream->pending);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
stream_close_first_sinks(struct stream *stream, unsigned count) {
|
|
||||||
while (count) {
|
|
||||||
struct sc_packet_sink *sink = stream->sinks[--count];
|
|
||||||
sink->ops->close(sink);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
|
||||||
stream_close_sinks(struct stream *stream) {
|
|
||||||
stream_close_first_sinks(stream, stream->sink_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
stream_open_sinks(struct stream *stream, const AVCodec *codec) {
|
|
||||||
for (unsigned i = 0; i < stream->sink_count; ++i) {
|
|
||||||
struct sc_packet_sink *sink = stream->sinks[i];
|
|
||||||
if (!sink->ops->open(sink, codec)) {
|
|
||||||
LOGE("Could not open packet sink %d", i);
|
|
||||||
stream_close_first_sinks(stream, i);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
run_stream(void *data) {
|
|
||||||
struct stream *stream = data;
|
|
||||||
|
|
||||||
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
|
|
||||||
if (!codec) {
|
|
||||||
LOGE("H.264 decoder not found");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->codec_ctx = avcodec_alloc_context3(codec);
|
|
||||||
if (!stream->codec_ctx) {
|
|
||||||
LOG_OOM();
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream_open_sinks(stream, codec)) {
|
|
||||||
LOGE("Could not open stream sinks");
|
|
||||||
goto finally_free_codec_ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->parser = av_parser_init(AV_CODEC_ID_H264);
|
|
||||||
if (!stream->parser) {
|
|
||||||
LOGE("Could not initialize parser");
|
|
||||||
goto finally_close_sinks;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We must only pass complete frames to av_parser_parse2()!
|
|
||||||
// It's more complicated, but this allows to reduce the latency by 1 frame!
|
|
||||||
stream->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
|
|
||||||
|
|
||||||
AVPacket *packet = av_packet_alloc();
|
|
||||||
if (!packet) {
|
|
||||||
LOG_OOM();
|
|
||||||
goto finally_close_parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
bool ok = stream_recv_packet(stream, packet);
|
|
||||||
if (!ok) {
|
|
||||||
// end of stream
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = stream_push_packet(stream, packet);
|
|
||||||
av_packet_unref(packet);
|
|
||||||
if (!ok) {
|
|
||||||
// cannot process packet (error already logged)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGD("End of frames");
|
|
||||||
|
|
||||||
if (stream->pending) {
|
|
||||||
av_packet_free(&stream->pending);
|
|
||||||
}
|
|
||||||
|
|
||||||
av_packet_free(&packet);
|
|
||||||
finally_close_parser:
|
|
||||||
av_parser_close(stream->parser);
|
|
||||||
finally_close_sinks:
|
|
||||||
stream_close_sinks(stream);
|
|
||||||
finally_free_codec_ctx:
|
|
||||||
avcodec_free_context(&stream->codec_ctx);
|
|
||||||
end:
|
|
||||||
stream->cbs->on_eos(stream, stream->cbs_userdata);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
stream_init(struct stream *stream, sc_socket socket,
|
|
||||||
const struct stream_callbacks *cbs, void *cbs_userdata) {
|
|
||||||
stream->socket = socket;
|
|
||||||
stream->pending = NULL;
|
|
||||||
stream->sink_count = 0;
|
|
||||||
|
|
||||||
assert(cbs && cbs->on_eos);
|
|
||||||
|
|
||||||
stream->cbs = cbs;
|
|
||||||
stream->cbs_userdata = cbs_userdata;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
stream_add_sink(struct stream *stream, struct sc_packet_sink *sink) {
|
|
||||||
assert(stream->sink_count < STREAM_MAX_SINKS);
|
|
||||||
assert(sink);
|
|
||||||
assert(sink->ops);
|
|
||||||
stream->sinks[stream->sink_count++] = sink;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
stream_start(struct stream *stream) {
|
|
||||||
LOGD("Starting stream thread");
|
|
||||||
|
|
||||||
bool ok =
|
|
||||||
sc_thread_create(&stream->thread, run_stream, "scrcpy-stream", stream);
|
|
||||||
if (!ok) {
|
|
||||||
LOGC("Could not start stream thread");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
stream_join(struct stream *stream) {
|
|
||||||
sc_thread_join(&stream->thread, NULL);
|
|
||||||
}
|
|
||||||
@@ -176,7 +176,7 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
|||||||
bool
|
bool
|
||||||
sc_process_terminate(pid_t pid) {
|
sc_process_terminate(pid_t pid) {
|
||||||
if (pid <= 0) {
|
if (pid <= 0) {
|
||||||
LOGC("Requested to kill %d, this is an error. Please report the bug.\n",
|
LOGE("Requested to kill %d, this is an error. Please report the bug.\n",
|
||||||
(int) pid);
|
(int) pid);
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,6 @@ sc_hid_event_destroy(struct sc_hid_event *hid_event) {
|
|||||||
free(hid_event->buffer);
|
free(hid_event->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
|
||||||
log_libusb_error(enum libusb_error errcode) {
|
|
||||||
LOGW("libusb error: %s", libusb_strerror(errcode));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
|
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
|
||||||
struct sc_acksync *acksync) {
|
struct sc_acksync *acksync) {
|
||||||
@@ -99,7 +94,8 @@ sc_aoa_register_hid(struct sc_aoa *aoa, uint16_t accessory_id,
|
|||||||
request, value, index, buffer, length,
|
request, value, index, buffer, length,
|
||||||
DEFAULT_TIMEOUT);
|
DEFAULT_TIMEOUT);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("REGISTER_HID: libusb error: %s", libusb_strerror(result));
|
||||||
|
sc_usb_check_disconnected(aoa->usb, result);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +131,8 @@ sc_aoa_set_hid_report_desc(struct sc_aoa *aoa, uint16_t accessory_id,
|
|||||||
request, value, index, buffer, length,
|
request, value, index, buffer, length,
|
||||||
DEFAULT_TIMEOUT);
|
DEFAULT_TIMEOUT);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("SET_HID_REPORT_DESC: libusb error: %s", libusb_strerror(result));
|
||||||
|
sc_usb_check_disconnected(aoa->usb, result);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +174,8 @@ sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
|
|||||||
request, value, index, buffer, length,
|
request, value, index, buffer, length,
|
||||||
DEFAULT_TIMEOUT);
|
DEFAULT_TIMEOUT);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("SEND_HID_EVENT: libusb error: %s", libusb_strerror(result));
|
||||||
|
sc_usb_check_disconnected(aoa->usb, result);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +197,8 @@ sc_aoa_unregister_hid(struct sc_aoa *aoa, const uint16_t accessory_id) {
|
|||||||
request, value, index, buffer, length,
|
request, value, index, buffer, length,
|
||||||
DEFAULT_TIMEOUT);
|
DEFAULT_TIMEOUT);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("UNREGISTER_HID: libusb error: %s", libusb_strerror(result));
|
||||||
|
sc_usb_check_disconnected(aoa->usb, result);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,7 +282,7 @@ sc_aoa_start(struct sc_aoa *aoa) {
|
|||||||
|
|
||||||
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "scrcpy-aoa", aoa);
|
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "scrcpy-aoa", aoa);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start AOA thread");
|
LOGE("Could not start AOA thread");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include "adb/adb.h"
|
||||||
#include "events.h"
|
#include "events.h"
|
||||||
#include "screen_otg.h"
|
#include "screen_otg.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
@@ -56,7 +57,7 @@ scrcpy_otg(struct scrcpy_options *options) {
|
|||||||
|
|
||||||
// Minimal SDL initialization
|
// Minimal SDL initialization
|
||||||
if (SDL_Init(SDL_INIT_EVENTS)) {
|
if (SDL_Init(SDL_INIT_EVENTS)) {
|
||||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
LOGE("Could not initialize SDL: %s", SDL_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +76,15 @@ scrcpy_otg(struct scrcpy_options *options) {
|
|||||||
bool aoa_started = false;
|
bool aoa_started = false;
|
||||||
bool aoa_initialized = false;
|
bool aoa_initialized = false;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// On Windows, only one process could open a USB device
|
||||||
|
// <https://github.com/Genymobile/scrcpy/issues/2773>
|
||||||
|
LOGI("Killing adb daemon (if any)...");
|
||||||
|
unsigned flags = SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR;
|
||||||
|
// uninterruptible (intr == NULL), but in practice it's very quick
|
||||||
|
sc_adb_kill_server(NULL, flags);
|
||||||
|
#endif
|
||||||
|
|
||||||
static const struct sc_usb_callbacks cbs = {
|
static const struct sc_usb_callbacks cbs = {
|
||||||
.on_disconnected = sc_usb_on_disconnected,
|
.on_disconnected = sc_usb_on_disconnected,
|
||||||
};
|
};
|
||||||
@@ -83,50 +93,19 @@ scrcpy_otg(struct scrcpy_options *options) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sc_usb_device usb_devices[16];
|
struct sc_usb_device usb_device;
|
||||||
ssize_t count = sc_usb_find_devices(&s->usb, serial, usb_devices,
|
ok = sc_usb_select_device(&s->usb, serial, &usb_device);
|
||||||
ARRAY_LEN(usb_devices));
|
if (!ok) {
|
||||||
if (count < 0) {
|
|
||||||
LOGE("Could not list USB devices");
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count == 0) {
|
|
||||||
if (serial) {
|
|
||||||
LOGE("Could not find USB device %s", serial);
|
|
||||||
} else {
|
|
||||||
LOGE("Could not find any USB device");
|
|
||||||
}
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count > 1) {
|
|
||||||
if (serial) {
|
|
||||||
LOGE("Multiple (%d) USB devices with serial %s:", (int) count,
|
|
||||||
serial);
|
|
||||||
} else {
|
|
||||||
LOGE("Multiple (%d) USB devices:", (int) count);
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < (size_t) count; ++i) {
|
|
||||||
struct sc_usb_device *d = &usb_devices[i];
|
|
||||||
LOGE(" %-18s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
|
|
||||||
d->serial, d->vid, d->pid, d->manufacturer, d->product);
|
|
||||||
}
|
|
||||||
if (!serial) {
|
|
||||||
LOGE("Specify the device via -s or --serial");
|
|
||||||
}
|
|
||||||
sc_usb_device_destroy_all(usb_devices, count);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
usb_device_initialized = true;
|
usb_device_initialized = true;
|
||||||
|
|
||||||
struct sc_usb_device *usb_device = &usb_devices[0];
|
LOGI("USB device: %s (%04x:%04x) %s %s", usb_device.serial,
|
||||||
|
(unsigned) usb_device.vid, (unsigned) usb_device.pid,
|
||||||
|
usb_device.manufacturer, usb_device.product);
|
||||||
|
|
||||||
LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
|
ok = sc_usb_connect(&s->usb, usb_device.device, &cbs, NULL);
|
||||||
usb_device->serial, usb_device->vid, usb_device->pid,
|
|
||||||
usb_device->manufacturer, usb_device->product);
|
|
||||||
|
|
||||||
ok = sc_usb_connect(&s->usb, usb_device->device, &cbs, NULL);
|
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -173,7 +152,7 @@ scrcpy_otg(struct scrcpy_options *options) {
|
|||||||
|
|
||||||
const char *window_title = options->window_title;
|
const char *window_title = options->window_title;
|
||||||
if (!window_title) {
|
if (!window_title) {
|
||||||
window_title = usb_device->product ? usb_device->product : "scrcpy";
|
window_title = usb_device.product ? usb_device.product : "scrcpy";
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sc_screen_otg_params params = {
|
struct sc_screen_otg_params params = {
|
||||||
@@ -192,7 +171,7 @@ scrcpy_otg(struct scrcpy_options *options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// usb_device not needed anymore
|
// usb_device not needed anymore
|
||||||
sc_usb_device_destroy(usb_device);
|
sc_usb_device_destroy(&usb_device);
|
||||||
usb_device_initialized = false;
|
usb_device_initialized = false;
|
||||||
|
|
||||||
ret = event_loop(s);
|
ret = event_loop(s);
|
||||||
@@ -223,7 +202,7 @@ end:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (usb_device_initialized) {
|
if (usb_device_initialized) {
|
||||||
sc_usb_device_destroy(usb_device);
|
sc_usb_device_destroy(&usb_device);
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_usb_destroy(&s->usb);
|
sc_usb_destroy(&s->usb);
|
||||||
|
|||||||
@@ -5,15 +5,44 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_screen_otg_capture_mouse(struct sc_screen_otg *screen, bool capture) {
|
sc_screen_otg_set_mouse_capture(struct sc_screen_otg *screen, bool capture) {
|
||||||
assert(screen->mouse);
|
#ifdef __APPLE__
|
||||||
|
// Workaround for SDL bug on macOS:
|
||||||
|
// <https://github.com/libsdl-org/SDL/issues/5340>
|
||||||
|
if (capture) {
|
||||||
|
int mouse_x, mouse_y;
|
||||||
|
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
|
||||||
|
|
||||||
|
int x, y, w, h;
|
||||||
|
SDL_GetWindowPosition(screen->window, &x, &y);
|
||||||
|
SDL_GetWindowSize(screen->window, &w, &h);
|
||||||
|
|
||||||
|
bool outside_window = mouse_x < x || mouse_x >= x + w
|
||||||
|
|| mouse_y < y || mouse_y >= y + h;
|
||||||
|
if (outside_window) {
|
||||||
|
SDL_WarpMouseInWindow(screen->window, w / 2, h / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
(void) screen;
|
||||||
|
#endif
|
||||||
if (SDL_SetRelativeMouseMode(capture)) {
|
if (SDL_SetRelativeMouseMode(capture)) {
|
||||||
LOGE("Could not set relative mouse mode to %s: %s",
|
LOGE("Could not set relative mouse mode to %s: %s",
|
||||||
capture ? "true" : "false", SDL_GetError());
|
capture ? "true" : "false", SDL_GetError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
screen->mouse_captured = capture;
|
static inline bool
|
||||||
|
sc_screen_otg_get_mouse_capture(struct sc_screen_otg *screen) {
|
||||||
|
(void) screen;
|
||||||
|
return SDL_GetRelativeMouseMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
sc_screen_otg_toggle_mouse_capture(struct sc_screen_otg *screen) {
|
||||||
|
(void) screen;
|
||||||
|
bool new_value = !sc_screen_otg_get_mouse_capture(screen);
|
||||||
|
sc_screen_otg_set_mouse_capture(screen, new_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -31,7 +60,6 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
|
|||||||
screen->keyboard = params->keyboard;
|
screen->keyboard = params->keyboard;
|
||||||
screen->mouse = params->mouse;
|
screen->mouse = params->mouse;
|
||||||
|
|
||||||
screen->mouse_captured = false;
|
|
||||||
screen->mouse_capture_key_pressed = 0;
|
screen->mouse_capture_key_pressed = 0;
|
||||||
|
|
||||||
const char *title = params->window_title;
|
const char *title = params->window_title;
|
||||||
@@ -81,7 +109,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
|
|||||||
|
|
||||||
if (screen->mouse) {
|
if (screen->mouse) {
|
||||||
// Capture mouse on start
|
// Capture mouse on start
|
||||||
sc_screen_otg_capture_mouse(screen, true);
|
sc_screen_otg_set_mouse_capture(screen, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -193,7 +221,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||||||
break;
|
break;
|
||||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||||
if (screen->mouse) {
|
if (screen->mouse) {
|
||||||
sc_screen_otg_capture_mouse(screen, false);
|
sc_screen_otg_set_mouse_capture(screen, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -227,8 +255,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||||||
if (key == cap) {
|
if (key == cap) {
|
||||||
// A mouse capture key has been pressed then released:
|
// A mouse capture key has been pressed then released:
|
||||||
// toggle the capture mouse mode
|
// toggle the capture mouse mode
|
||||||
sc_screen_otg_capture_mouse(screen,
|
sc_screen_otg_toggle_mouse_capture(screen);
|
||||||
!screen->mouse_captured);
|
|
||||||
}
|
}
|
||||||
// Mouse capture keys are never forwarded to the device
|
// Mouse capture keys are never forwarded to the device
|
||||||
return;
|
return;
|
||||||
@@ -240,26 +267,26 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
if (screen->mouse && screen->mouse_captured) {
|
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
|
||||||
sc_screen_otg_process_mouse_motion(screen, &event->motion);
|
sc_screen_otg_process_mouse_motion(screen, &event->motion);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
if (screen->mouse && screen->mouse_captured) {
|
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
|
||||||
sc_screen_otg_process_mouse_button(screen, &event->button);
|
sc_screen_otg_process_mouse_button(screen, &event->button);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if (screen->mouse) {
|
if (screen->mouse) {
|
||||||
if (screen->mouse_captured) {
|
if (sc_screen_otg_get_mouse_capture(screen)) {
|
||||||
sc_screen_otg_process_mouse_button(screen, &event->button);
|
sc_screen_otg_process_mouse_button(screen, &event->button);
|
||||||
} else {
|
} else {
|
||||||
sc_screen_otg_capture_mouse(screen, true);
|
sc_screen_otg_set_mouse_capture(screen, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEWHEEL:
|
case SDL_MOUSEWHEEL:
|
||||||
if (screen->mouse && screen->mouse_captured) {
|
if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) {
|
||||||
sc_screen_otg_process_mouse_wheel(screen, &event->wheel);
|
sc_screen_otg_process_mouse_wheel(screen, &event->wheel);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ struct sc_screen_otg {
|
|||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
|
|
||||||
// See equivalent mechanism in screen.h
|
// See equivalent mechanism in screen.h
|
||||||
bool mouse_captured;
|
|
||||||
SDL_Keycode mouse_capture_key_pressed;
|
SDL_Keycode mouse_capture_key_pressed;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,9 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
#include "util/vector.h"
|
||||||
|
|
||||||
static inline void
|
struct sc_vec_usb_devices SC_VECTOR(struct sc_usb_device);
|
||||||
log_libusb_error(enum libusb_error errcode) {
|
|
||||||
LOGW("libusb error: %s", libusb_strerror(errcode));
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
read_string(libusb_device_handle *handle, uint8_t desc_index) {
|
read_string(libusb_device_handle *handle, uint8_t desc_index) {
|
||||||
@@ -30,8 +28,7 @@ read_string(libusb_device_handle *handle, uint8_t desc_index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
accept_device(libusb_device *device, const char *serial,
|
sc_usb_read_device(libusb_device *device, struct sc_usb_device *out) {
|
||||||
struct sc_usb_device *out) {
|
|
||||||
// Do not log any USB error in this function, it is expected that many USB
|
// Do not log any USB error in this function, it is expected that many USB
|
||||||
// devices available on the computer have permission restrictions
|
// devices available on the computer have permission restrictions
|
||||||
|
|
||||||
@@ -44,6 +41,11 @@ accept_device(libusb_device *device, const char *serial,
|
|||||||
libusb_device_handle *handle;
|
libusb_device_handle *handle;
|
||||||
result = libusb_open(device, &handle);
|
result = libusb_open(device, &handle);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
|
// Log at debug level because it is expected that some non-Android USB
|
||||||
|
// devices present on the computer require special permissions
|
||||||
|
LOGD("Open USB device %04x:%04x: libusb error: %s",
|
||||||
|
(unsigned) desc.idVendor, (unsigned) desc.idProduct,
|
||||||
|
libusb_strerror(result));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,22 +55,13 @@ accept_device(libusb_device *device, const char *serial,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serial) {
|
|
||||||
// Filter by serial
|
|
||||||
bool matches = !strcmp(serial, device_serial);
|
|
||||||
if (!matches) {
|
|
||||||
free(device_serial);
|
|
||||||
libusb_close(handle);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out->device = libusb_ref_device(device);
|
out->device = libusb_ref_device(device);
|
||||||
out->serial = device_serial;
|
out->serial = device_serial;
|
||||||
out->vid = desc.idVendor;
|
out->vid = desc.idVendor;
|
||||||
out->pid = desc.idProduct;
|
out->pid = desc.idProduct;
|
||||||
out->manufacturer = read_string(handle, desc.iManufacturer);
|
out->manufacturer = read_string(handle, desc.iManufacturer);
|
||||||
out->product = read_string(handle, desc.iProduct);
|
out->product = read_string(handle, desc.iProduct);
|
||||||
|
out->selected = false;
|
||||||
|
|
||||||
libusb_close(handle);
|
libusb_close(handle);
|
||||||
|
|
||||||
@@ -77,52 +70,151 @@ accept_device(libusb_device *device, const char *serial,
|
|||||||
|
|
||||||
void
|
void
|
||||||
sc_usb_device_destroy(struct sc_usb_device *usb_device) {
|
sc_usb_device_destroy(struct sc_usb_device *usb_device) {
|
||||||
libusb_unref_device(usb_device->device);
|
if (usb_device->device) {
|
||||||
|
libusb_unref_device(usb_device->device);
|
||||||
|
}
|
||||||
free(usb_device->serial);
|
free(usb_device->serial);
|
||||||
free(usb_device->manufacturer);
|
free(usb_device->manufacturer);
|
||||||
free(usb_device->product);
|
free(usb_device->product);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_usb_device_destroy_all(struct sc_usb_device *usb_devices, size_t count) {
|
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src) {
|
||||||
for (size_t i = 0; i < count; ++i) {
|
*dst = *src;
|
||||||
sc_usb_device_destroy(&usb_devices[i]);
|
src->device = NULL;
|
||||||
}
|
src->serial = NULL;
|
||||||
|
src->manufacturer = NULL;
|
||||||
|
src->product = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
void
|
||||||
sc_usb_find_devices(struct sc_usb *usb, const char *serial,
|
sc_usb_devices_destroy(struct sc_vec_usb_devices *usb_devices) {
|
||||||
struct sc_usb_device *devices, size_t len) {
|
for (size_t i = 0; i < usb_devices->size; ++i) {
|
||||||
|
sc_usb_device_destroy(&usb_devices->data[i]);
|
||||||
|
}
|
||||||
|
sc_vector_destroy(usb_devices);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_usb_list_devices(struct sc_usb *usb, struct sc_vec_usb_devices *out_vec) {
|
||||||
libusb_device **list;
|
libusb_device **list;
|
||||||
ssize_t count = libusb_get_device_list(usb->context, &list);
|
ssize_t count = libusb_get_device_list(usb->context, &list);
|
||||||
if (count < 0) {
|
if (count < 0) {
|
||||||
log_libusb_error((enum libusb_error) count);
|
LOGE("List USB devices: libusb error: %s", libusb_strerror(count));
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t idx = 0;
|
for (size_t i = 0; i < (size_t) count; ++i) {
|
||||||
for (size_t i = 0; i < (size_t) count && idx < len; ++i) {
|
|
||||||
libusb_device *device = list[i];
|
libusb_device *device = list[i];
|
||||||
|
|
||||||
if (accept_device(device, serial, &devices[idx])) {
|
struct sc_usb_device usb_device;
|
||||||
++idx;
|
if (sc_usb_read_device(device, &usb_device)) {
|
||||||
|
bool ok = sc_vector_push(out_vec, usb_device);
|
||||||
|
if (!ok) {
|
||||||
|
LOG_OOM();
|
||||||
|
LOGE("Could not push usb_device to vector");
|
||||||
|
sc_usb_device_destroy(&usb_device);
|
||||||
|
// continue anyway
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_free_device_list(list, 1);
|
libusb_free_device_list(list, 1);
|
||||||
return idx;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static libusb_device_handle *
|
static bool
|
||||||
sc_usb_open_handle(libusb_device *device) {
|
sc_usb_accept_device(const struct sc_usb_device *device, const char *serial) {
|
||||||
libusb_device_handle *handle;
|
if (!serial) {
|
||||||
int result = libusb_open(device, &handle);
|
return true;
|
||||||
if (result < 0) {
|
|
||||||
log_libusb_error((enum libusb_error) result);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
return handle;
|
|
||||||
}
|
return !strcmp(serial, device->serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
sc_usb_devices_select(struct sc_usb_device *devices, size_t len,
|
||||||
|
const char *serial, size_t *idx_out) {
|
||||||
|
size_t count = 0;
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
struct sc_usb_device *device = &devices[i];
|
||||||
|
device->selected = sc_usb_accept_device(device, serial);
|
||||||
|
if (device->selected) {
|
||||||
|
if (idx_out && !count) {
|
||||||
|
*idx_out = i;
|
||||||
|
}
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sc_usb_devices_log(enum sc_log_level level, struct sc_usb_device *devices,
|
||||||
|
size_t count) {
|
||||||
|
for (size_t i = 0; i < count; ++i) {
|
||||||
|
struct sc_usb_device *d = &devices[i];
|
||||||
|
const char *selection = d->selected ? "-->" : " ";
|
||||||
|
// Convert uint16_t to unsigned because PRIx16 may not exist on Windows
|
||||||
|
LOG(level, " %s %-18s (%04x:%04x) %s %s",
|
||||||
|
selection, d->serial, (unsigned) d->vid, (unsigned) d->pid,
|
||||||
|
d->manufacturer, d->product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_usb_select_device(struct sc_usb *usb, const char *serial,
|
||||||
|
struct sc_usb_device *out_device) {
|
||||||
|
struct sc_vec_usb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_usb_list_devices(usb, &vec);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not list USB devices");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vec.size == 0) {
|
||||||
|
LOGE("Could not find any USB device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t sel_idx; // index of the single matching device if sel_count == 1
|
||||||
|
size_t sel_count =
|
||||||
|
sc_usb_devices_select(vec.data, vec.size, serial, &sel_idx);
|
||||||
|
|
||||||
|
if (sel_count == 0) {
|
||||||
|
// if count > 0 && sel_count == 0, then necessarily a serial is provided
|
||||||
|
assert(serial);
|
||||||
|
LOGE("Could not find USB device %s", serial);
|
||||||
|
sc_usb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
|
||||||
|
sc_usb_devices_destroy(&vec);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sel_count > 1) {
|
||||||
|
if (serial) {
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") USB devices with serial %s:",
|
||||||
|
sel_count, serial);
|
||||||
|
} else {
|
||||||
|
LOGE("Multiple (%" SC_PRIsizet ") USB devices:", sel_count);
|
||||||
|
}
|
||||||
|
sc_usb_devices_log(SC_LOG_LEVEL_ERROR, vec.data, vec.size);
|
||||||
|
LOGE("Select a device via -s (--serial)");
|
||||||
|
sc_usb_devices_destroy(&vec);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(sel_count == 1); // sel_idx is valid only if sel_count == 1
|
||||||
|
struct sc_usb_device *device = &vec.data[sel_idx];
|
||||||
|
|
||||||
|
LOGD("USB device found:");
|
||||||
|
sc_usb_devices_log(SC_LOG_LEVEL_DEBUG, vec.data, vec.size);
|
||||||
|
|
||||||
|
// Move device into out_device (do not destroy device)
|
||||||
|
sc_usb_device_move(out_device, device);
|
||||||
|
sc_usb_devices_destroy(&vec);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_usb_init(struct sc_usb *usb) {
|
sc_usb_init(struct sc_usb *usb) {
|
||||||
@@ -135,7 +227,25 @@ sc_usb_destroy(struct sc_usb *usb) {
|
|||||||
libusb_exit(usb->context);
|
libusb_exit(usb->context);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static void
|
||||||
|
sc_usb_report_disconnected(struct sc_usb *usb) {
|
||||||
|
if (usb->cbs && !atomic_flag_test_and_set(&usb->disconnection_notified)) {
|
||||||
|
assert(usb->cbs && usb->cbs->on_disconnected);
|
||||||
|
usb->cbs->on_disconnected(usb, usb->cbs_userdata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_usb_check_disconnected(struct sc_usb *usb, int result) {
|
||||||
|
if (result == LIBUSB_ERROR_NO_DEVICE || result == LIBUSB_ERROR_NOT_FOUND) {
|
||||||
|
sc_usb_report_disconnected(usb);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static LIBUSB_CALL int
|
||||||
sc_usb_libusb_callback(libusb_context *ctx, libusb_device *device,
|
sc_usb_libusb_callback(libusb_context *ctx, libusb_device *device,
|
||||||
libusb_hotplug_event event, void *userdata) {
|
libusb_hotplug_event event, void *userdata) {
|
||||||
(void) ctx;
|
(void) ctx;
|
||||||
@@ -151,8 +261,7 @@ sc_usb_libusb_callback(libusb_context *ctx, libusb_device *device,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(usb->cbs && usb->cbs->on_disconnected);
|
sc_usb_report_disconnected(usb);
|
||||||
usb->cbs->on_disconnected(usb, usb->cbs_userdata);
|
|
||||||
|
|
||||||
// Do not automatically deregister the callback by returning 1. Instead,
|
// Do not automatically deregister the callback by returning 1. Instead,
|
||||||
// manually deregister to interrupt libusb_handle_events() from the libusb
|
// manually deregister to interrupt libusb_handle_events() from the libusb
|
||||||
@@ -173,7 +282,8 @@ run_libusb_event_handler(void *data) {
|
|||||||
static bool
|
static bool
|
||||||
sc_usb_register_callback(struct sc_usb *usb) {
|
sc_usb_register_callback(struct sc_usb *usb) {
|
||||||
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
||||||
LOGW("libusb does not have hotplug capability");
|
LOGW("On this platform, libusb does not have hotplug capability; "
|
||||||
|
"device disconnection will not be detected properly");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,8 +293,7 @@ sc_usb_register_callback(struct sc_usb *usb) {
|
|||||||
struct libusb_device_descriptor desc;
|
struct libusb_device_descriptor desc;
|
||||||
int result = libusb_get_device_descriptor(device, &desc);
|
int result = libusb_get_device_descriptor(device, &desc);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("Device descriptor: libusb error: %s", libusb_strerror(result));
|
||||||
LOGW("Could not read USB device descriptor");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,8 +307,8 @@ sc_usb_register_callback(struct sc_usb *usb) {
|
|||||||
sc_usb_libusb_callback, usb,
|
sc_usb_libusb_callback, usb,
|
||||||
&usb->callback_handle);
|
&usb->callback_handle);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
log_libusb_error((enum libusb_error) result);
|
LOGE("Register hotplog callback: libusb error: %s",
|
||||||
LOGW("Could not register USB callback");
|
libusb_strerror(result));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,8 +319,9 @@ sc_usb_register_callback(struct sc_usb *usb) {
|
|||||||
bool
|
bool
|
||||||
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
||||||
const struct sc_usb_callbacks *cbs, void *cbs_userdata) {
|
const struct sc_usb_callbacks *cbs, void *cbs_userdata) {
|
||||||
usb->handle = sc_usb_open_handle(device);
|
int result = libusb_open(device, &usb->handle);
|
||||||
if (!usb->handle) {
|
if (result < 0) {
|
||||||
|
LOGE("Open USB device: libusb error: %s", libusb_strerror(result));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,6 +335,7 @@ sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
|||||||
|
|
||||||
if (cbs) {
|
if (cbs) {
|
||||||
atomic_init(&usb->stopped, false);
|
atomic_init(&usb->stopped, false);
|
||||||
|
usb->disconnection_notified = (atomic_flag) ATOMIC_FLAG_INIT;
|
||||||
if (sc_usb_register_callback(usb)) {
|
if (sc_usb_register_callback(usb)) {
|
||||||
// Create a thread to process libusb events, so that device
|
// Create a thread to process libusb events, so that device
|
||||||
// disconnection could be detected immediately
|
// disconnection could be detected immediately
|
||||||
@@ -235,8 +346,6 @@ sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
|||||||
LOGW("Libusb event thread handler could not be created, USB "
|
LOGW("Libusb event thread handler could not be created, USB "
|
||||||
"device disconnection might not be detected immediately");
|
"device disconnection might not be detected immediately");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
LOGW("Could not register USB device disconnection callback");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ struct sc_usb {
|
|||||||
sc_thread libusb_event_thread;
|
sc_thread libusb_event_thread;
|
||||||
|
|
||||||
atomic_bool stopped; // only used if cbs != NULL
|
atomic_bool stopped; // only used if cbs != NULL
|
||||||
|
atomic_flag disconnection_notified;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sc_usb_callbacks {
|
struct sc_usb_callbacks {
|
||||||
@@ -35,13 +36,26 @@ struct sc_usb_device {
|
|||||||
char *product;
|
char *product;
|
||||||
uint16_t vid;
|
uint16_t vid;
|
||||||
uint16_t pid;
|
uint16_t pid;
|
||||||
|
bool selected;
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_usb_device_destroy(struct sc_usb_device *usb_device);
|
sc_usb_device_destroy(struct sc_usb_device *usb_device);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move src to dst
|
||||||
|
*
|
||||||
|
* After this call, the content of src is undefined, except that
|
||||||
|
* sc_usb_device_destroy() can be called.
|
||||||
|
*
|
||||||
|
* This is useful to take a device from a list that will be destroyed, without
|
||||||
|
* making unnecessary copies.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
sc_usb_device_destroy_all(struct sc_usb_device *usb_devices, size_t count);
|
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_usb_init(struct sc_usb *usb);
|
sc_usb_init(struct sc_usb *usb);
|
||||||
@@ -49,9 +63,9 @@ sc_usb_init(struct sc_usb *usb);
|
|||||||
void
|
void
|
||||||
sc_usb_destroy(struct sc_usb *usb);
|
sc_usb_destroy(struct sc_usb *usb);
|
||||||
|
|
||||||
ssize_t
|
bool
|
||||||
sc_usb_find_devices(struct sc_usb *usb, const char *serial,
|
sc_usb_select_device(struct sc_usb *usb, const char *serial,
|
||||||
struct sc_usb_device *devices, size_t len);
|
struct sc_usb_device *out_device);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
||||||
@@ -60,6 +74,11 @@ sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
|||||||
void
|
void
|
||||||
sc_usb_disconnect(struct sc_usb *usb);
|
sc_usb_disconnect(struct sc_usb *usb);
|
||||||
|
|
||||||
|
// A client should call this function with the return value of a libusb call
|
||||||
|
// to detect disconnection immediately
|
||||||
|
bool
|
||||||
|
sc_usb_check_disconnected(struct sc_usb *usb, int result);
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_usb_stop(struct sc_usb *usb);
|
sc_usb_stop(struct sc_usb *usb);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef BUFFER_UTIL_H
|
#ifndef SC_BUFFER_UTIL_H
|
||||||
#define BUFFER_UTIL_H
|
#define SC_BUFFER_UTIL_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
buffer_write16be(uint8_t *buf, uint16_t value) {
|
sc_write16be(uint8_t *buf, uint16_t value) {
|
||||||
buf[0] = value >> 8;
|
buf[0] = value >> 8;
|
||||||
buf[1] = value;
|
buf[1] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
buffer_write32be(uint8_t *buf, uint32_t value) {
|
sc_write32be(uint8_t *buf, uint32_t value) {
|
||||||
buf[0] = value >> 24;
|
buf[0] = value >> 24;
|
||||||
buf[1] = value >> 16;
|
buf[1] = value >> 16;
|
||||||
buf[2] = value >> 8;
|
buf[2] = value >> 8;
|
||||||
@@ -21,25 +21,25 @@ buffer_write32be(uint8_t *buf, uint32_t value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
buffer_write64be(uint8_t *buf, uint64_t value) {
|
sc_write64be(uint8_t *buf, uint64_t value) {
|
||||||
buffer_write32be(buf, value >> 32);
|
sc_write32be(buf, value >> 32);
|
||||||
buffer_write32be(&buf[4], (uint32_t) value);
|
sc_write32be(&buf[4], (uint32_t) value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint16_t
|
static inline uint16_t
|
||||||
buffer_read16be(const uint8_t *buf) {
|
sc_read16be(const uint8_t *buf) {
|
||||||
return (buf[0] << 8) | buf[1];
|
return (buf[0] << 8) | buf[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t
|
static inline uint32_t
|
||||||
buffer_read32be(const uint8_t *buf) {
|
sc_read32be(const uint8_t *buf) {
|
||||||
return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint64_t
|
static inline uint64_t
|
||||||
buffer_read64be(const uint8_t *buf) {
|
sc_read64be(const uint8_t *buf) {
|
||||||
uint32_t msb = buffer_read32be(buf);
|
uint32_t msb = sc_read32be(buf);
|
||||||
uint32_t lsb = buffer_read32be(&buf[4]);
|
uint32_t lsb = sc_read32be(&buf[4]);
|
||||||
return ((uint64_t) msb << 32) | lsb;
|
return ((uint64_t) msb << 32) | lsb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,16 @@ sc_get_log_level(void) {
|
|||||||
return log_level_sdl_to_sc(sdl_log);
|
return log_level_sdl_to_sc(sdl_log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_log(enum sc_log_level level, const char *fmt, ...) {
|
||||||
|
SDL_LogPriority sdl_level = log_level_sc_to_sdl(level);
|
||||||
|
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, sdl_level, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
bool
|
bool
|
||||||
sc_log_windows_error(const char *prefix, int error) {
|
sc_log_windows_error(const char *prefix, int error) {
|
||||||
|
|||||||
@@ -15,10 +15,9 @@
|
|||||||
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGW(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGW(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define LOG_OOM() \
|
#define LOG_OOM() \
|
||||||
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
|
LOGE("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_set_log_level(enum sc_log_level level);
|
sc_set_log_level(enum sc_log_level level);
|
||||||
@@ -26,6 +25,10 @@ sc_set_log_level(enum sc_log_level level);
|
|||||||
enum sc_log_level
|
enum sc_log_level
|
||||||
sc_get_log_level(void);
|
sc_get_log_level(void);
|
||||||
|
|
||||||
|
void
|
||||||
|
sc_log(enum sc_log_level level, const char *fmt, ...);
|
||||||
|
#define LOG(LEVEL, ...) sc_log((LEVEL), __VA_ARGS__)
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Log system error (typically returned by GetLastError() or similar)
|
// Log system error (typically returned by GetLastError() or similar)
|
||||||
bool
|
bool
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ net_init(void) {
|
|||||||
WSADATA wsa;
|
WSADATA wsa;
|
||||||
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0;
|
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0;
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
LOGC("WSAStartup failed with error %d", res);
|
LOGE("WSAStartup failed with error %d", res);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,8 +12,6 @@
|
|||||||
# include <winsock2.h>
|
# include <winsock2.h>
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
# define SC_PRIexitcode "lu"
|
# define SC_PRIexitcode "lu"
|
||||||
// <https://stackoverflow.com/a/44383330/1987178>
|
|
||||||
# define SC_PRIsizet "Iu"
|
|
||||||
# define SC_PROCESS_NONE NULL
|
# define SC_PROCESS_NONE NULL
|
||||||
# define SC_EXIT_CODE_NONE -1UL // max value as unsigned long
|
# define SC_EXIT_CODE_NONE -1UL // max value as unsigned long
|
||||||
typedef HANDLE sc_pid;
|
typedef HANDLE sc_pid;
|
||||||
@@ -23,7 +21,6 @@
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# define SC_PRIsizet "zu"
|
|
||||||
# define SC_PRIexitcode "d"
|
# define SC_PRIexitcode "d"
|
||||||
# define SC_PROCESS_NONE -1
|
# define SC_PROCESS_NONE -1
|
||||||
# define SC_EXIT_CODE_NONE -1
|
# define SC_EXIT_CODE_NONE -1
|
||||||
|
|||||||
@@ -3,27 +3,33 @@
|
|||||||
ssize_t
|
ssize_t
|
||||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
if (!sc_intr_set_process(intr, pid)) {
|
if (intr && !sc_intr_set_process(intr, pid)) {
|
||||||
// Already interrupted
|
// Already interrupted
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ret = sc_pipe_read(pipe, data, len);
|
ssize_t ret = sc_pipe_read(pipe, data, len);
|
||||||
|
|
||||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
if (intr) {
|
||||||
|
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
||||||
char *data, size_t len) {
|
char *data, size_t len) {
|
||||||
if (!sc_intr_set_process(intr, pid)) {
|
if (intr && !sc_intr_set_process(intr, pid)) {
|
||||||
// Already interrupted
|
// Already interrupted
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ret = sc_pipe_read_all(pipe, data, len);
|
ssize_t ret = sc_pipe_read_all(pipe, data, len);
|
||||||
|
|
||||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
if (intr) {
|
||||||
|
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -297,14 +297,6 @@ error:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
|
||||||
sc_str_truncate(char *data, size_t len, const char *endchars) {
|
|
||||||
data[len - 1] = '\0';
|
|
||||||
size_t idx = strcspn(data, endchars);
|
|
||||||
data[idx] = '\0';
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
|
sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
|
||||||
size_t colidx = 0;
|
size_t colidx = 0;
|
||||||
|
|||||||
@@ -103,17 +103,6 @@ sc_str_from_wchars(const wchar_t *s);
|
|||||||
char *
|
char *
|
||||||
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
||||||
|
|
||||||
/**
|
|
||||||
* Truncate the data after any of the characters from `endchars`
|
|
||||||
*
|
|
||||||
* An '\0' is always written at the end of the data, even if no newline
|
|
||||||
* character is encountered.
|
|
||||||
*
|
|
||||||
* Return the size of the resulting line.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
sc_str_truncate(char *data, size_t len, const char *endchars);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the start of a column in a string
|
* Find the start of a column in a string
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ sc_mutex_lock(sc_mutex *mutex) {
|
|||||||
int r = SDL_LockMutex(mutex->mutex);
|
int r = SDL_LockMutex(mutex->mutex);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r) {
|
if (r) {
|
||||||
LOGC("Could not lock mutex: %s", SDL_GetError());
|
LOGE("Could not lock mutex: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ sc_mutex_unlock(sc_mutex *mutex) {
|
|||||||
int r = SDL_UnlockMutex(mutex->mutex);
|
int r = SDL_UnlockMutex(mutex->mutex);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r) {
|
if (r) {
|
||||||
LOGC("Could not lock mutex: %s", SDL_GetError());
|
LOGE("Could not lock mutex: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -118,7 +118,7 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
|
|||||||
int r = SDL_CondWait(cond->cond, mutex->mutex);
|
int r = SDL_CondWait(cond->cond, mutex->mutex);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r) {
|
if (r) {
|
||||||
LOGC("Could not wait on condition: %s", SDL_GetError());
|
LOGE("Could not wait on condition: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,11 +136,13 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
|||||||
return false; // timeout
|
return false; // timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ms = SC_TICK_TO_MS(deadline - now);
|
// Round up to the next millisecond to guarantee that the deadline is
|
||||||
|
// reached when returning due to timeout
|
||||||
|
uint32_t ms = SC_TICK_TO_MS(deadline - now + SC_TICK_FROM_MS(1) - 1);
|
||||||
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
|
LOGE("Could not wait on condition with timeout: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +150,8 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
|||||||
memory_order_relaxed);
|
memory_order_relaxed);
|
||||||
#endif
|
#endif
|
||||||
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
||||||
|
// The deadline is reached on timeout
|
||||||
|
assert(r != SDL_MUTEX_TIMEDOUT || sc_tick_now() >= deadline);
|
||||||
return r == 0;
|
return r == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +160,7 @@ sc_cond_signal(sc_cond *cond) {
|
|||||||
int r = SDL_CondSignal(cond->cond);
|
int r = SDL_CondSignal(cond->cond);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r) {
|
if (r) {
|
||||||
LOGC("Could not signal a condition: %s", SDL_GetError());
|
LOGE("Could not signal a condition: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -169,7 +173,7 @@ sc_cond_broadcast(sc_cond *cond) {
|
|||||||
int r = SDL_CondBroadcast(cond->cond);
|
int r = SDL_CondBroadcast(cond->cond);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r) {
|
if (r) {
|
||||||
LOGC("Could not broadcast a condition: %s", SDL_GetError());
|
LOGE("Could not broadcast a condition: %s", SDL_GetError());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|||||||
540
app/src/util/vector.h
Normal file
540
app/src/util/vector.h
Normal file
@@ -0,0 +1,540 @@
|
|||||||
|
#ifndef SC_VECTOR_H
|
||||||
|
#define SC_VECTOR_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Adapted from vlc_vector:
|
||||||
|
// <https://code.videolan.org/videolan/vlc/-/blob/0857947abaed9c89810cd96353aaa1b7e6ba3b0d/include/vlc_vector.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vector struct body
|
||||||
|
*
|
||||||
|
* A vector is a dynamic array, managed by the sc_vector_* helpers.
|
||||||
|
*
|
||||||
|
* It is generic over the type of its items, so it is implemented as macros.
|
||||||
|
*
|
||||||
|
* To use a vector, a new type must be defined:
|
||||||
|
*
|
||||||
|
* struct vec_int SC_VECTOR(int);
|
||||||
|
*
|
||||||
|
* The struct may be anonymous:
|
||||||
|
*
|
||||||
|
* struct SC_VECTOR(const char *) names;
|
||||||
|
*
|
||||||
|
* Vector size is accessible via `vec.size`, and items are intended to be
|
||||||
|
* accessed directly, via `vec.data[i]`.
|
||||||
|
*
|
||||||
|
* Functions and macros having name ending with '_' are private.
|
||||||
|
*/
|
||||||
|
#define SC_VECTOR(type) \
|
||||||
|
{ \
|
||||||
|
size_t cap; \
|
||||||
|
size_t size; \
|
||||||
|
type *data; \
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static initializer for a vector
|
||||||
|
*/
|
||||||
|
#define SC_VECTOR_INITIALIZER { 0, 0, NULL }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize an empty vector
|
||||||
|
*/
|
||||||
|
#define sc_vector_init(pv) \
|
||||||
|
({ \
|
||||||
|
(pv)->cap = 0; \
|
||||||
|
(pv)->size = 0; \
|
||||||
|
(pv)->data = NULL; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy a vector
|
||||||
|
*
|
||||||
|
* The vector may not be used anymore unless sc_vector_init() is called.
|
||||||
|
*/
|
||||||
|
#define sc_vector_destroy(pv) \
|
||||||
|
free((pv)->data)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear a vector
|
||||||
|
*
|
||||||
|
* Remove all items from the vector.
|
||||||
|
*/
|
||||||
|
#define sc_vector_clear(pv) \
|
||||||
|
({ \
|
||||||
|
sc_vector_destroy(pv); \
|
||||||
|
sc_vector_init(pv);\
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimal allocation size, in number of items
|
||||||
|
*
|
||||||
|
* Private.
|
||||||
|
*/
|
||||||
|
#define SC_VECTOR_MINCAP_ ((size_t) 10)
|
||||||
|
|
||||||
|
static inline size_t
|
||||||
|
sc_vector_min_(size_t a, size_t b)
|
||||||
|
{
|
||||||
|
return a < b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline size_t
|
||||||
|
sc_vector_max_(size_t a, size_t b)
|
||||||
|
{
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline size_t
|
||||||
|
sc_vector_clamp_(size_t x, size_t min, size_t max)
|
||||||
|
{
|
||||||
|
return sc_vector_max_(min, sc_vector_min_(max, x));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realloc data and update vector fields
|
||||||
|
*
|
||||||
|
* On reallocation success, update the vector capacity (*pcap) and size
|
||||||
|
* (*psize), and return the reallocated data.
|
||||||
|
*
|
||||||
|
* On reallocation failure, return NULL without any change.
|
||||||
|
*
|
||||||
|
* Private.
|
||||||
|
*
|
||||||
|
* \param ptr the current `data` field of the vector to realloc
|
||||||
|
* \param count the requested capacity, in number of items
|
||||||
|
* \param size the size of one item
|
||||||
|
* \param pcap a pointer to the `cap` field of the vector [IN/OUT]
|
||||||
|
* \param psize a pointer to the `size` field of the vector [IN/OUT]
|
||||||
|
* \return the new ptr on success, NULL on error
|
||||||
|
*/
|
||||||
|
static inline void *
|
||||||
|
sc_vector_reallocdata_(void *ptr, size_t count, size_t size,
|
||||||
|
size_t *restrict pcap, size_t *restrict psize)
|
||||||
|
{
|
||||||
|
void *p = realloc(ptr, count * size);
|
||||||
|
if (!p) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pcap = count;
|
||||||
|
*psize = sc_vector_min_(*psize, count);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define sc_vector_realloc_(pv, newcap) \
|
||||||
|
({ \
|
||||||
|
void *p = sc_vector_reallocdata_((pv)->data, newcap, sizeof(*(pv)->data), \
|
||||||
|
&(pv)->cap, &(pv)->size); \
|
||||||
|
if (p) { \
|
||||||
|
(pv)->data = p; \
|
||||||
|
} \
|
||||||
|
(bool) p; \
|
||||||
|
});
|
||||||
|
|
||||||
|
#define sc_vector_resize_(pv, newcap) \
|
||||||
|
({ \
|
||||||
|
bool ok; \
|
||||||
|
if ((pv)->cap == (newcap)) { \
|
||||||
|
ok = true; \
|
||||||
|
} else if ((newcap) > 0) { \
|
||||||
|
ok = sc_vector_realloc_(pv, (newcap)); \
|
||||||
|
} else { \
|
||||||
|
sc_vector_clear(pv); \
|
||||||
|
ok = true; \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
static inline size_t
|
||||||
|
sc_vector_growsize_(size_t value)
|
||||||
|
{
|
||||||
|
/* integer multiplication by 1.5 */
|
||||||
|
return value + (value >> 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SIZE_MAX/2 to fit in ssize_t, and so that cap*1.5 does not overflow. */
|
||||||
|
#define sc_vector_max_cap_(pv) (SIZE_MAX / 2 / sizeof(*(pv)->data))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increase the capacity of the vector to at least `mincap`
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param mincap (size_t) the requested capacity
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_reserve(pv, mincap) \
|
||||||
|
({ \
|
||||||
|
bool ok; \
|
||||||
|
/* avoid to allocate tiny arrays (< SC_VECTOR_MINCAP_) */ \
|
||||||
|
size_t mincap_ = sc_vector_max_(mincap, SC_VECTOR_MINCAP_); \
|
||||||
|
if (mincap_ <= (pv)->cap) { \
|
||||||
|
/* nothing to do */ \
|
||||||
|
ok = true; \
|
||||||
|
} else if (mincap_ <= sc_vector_max_cap_(pv)) { \
|
||||||
|
/* not too big */ \
|
||||||
|
size_t newsize = sc_vector_growsize_((pv)->cap); \
|
||||||
|
newsize = sc_vector_clamp_(newsize, mincap_, sc_vector_max_cap_(pv)); \
|
||||||
|
ok = sc_vector_realloc_(pv, newsize); \
|
||||||
|
} else { \
|
||||||
|
ok = false; \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define sc_vector_shrink_to_fit(pv) \
|
||||||
|
/* decreasing the size may not fail */ \
|
||||||
|
(void) sc_vector_resize_(pv, (pv)->size)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resize the vector down automatically
|
||||||
|
*
|
||||||
|
* Shrink only when necessary (in practice when cap > (size+5)*1.5)
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
*/
|
||||||
|
#define sc_vector_autoshrink(pv) \
|
||||||
|
({ \
|
||||||
|
bool must_shrink = \
|
||||||
|
/* do not shrink to tiny size */ \
|
||||||
|
(pv)->cap > SC_VECTOR_MINCAP_ && \
|
||||||
|
/* no need to shrink */ \
|
||||||
|
(pv)->cap >= sc_vector_growsize_((pv)->size + 5); \
|
||||||
|
if (must_shrink) { \
|
||||||
|
size_t newsize = sc_vector_max_((pv)->size + 5, SC_VECTOR_MINCAP_); \
|
||||||
|
sc_vector_resize_(pv, newsize); \
|
||||||
|
} \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define sc_vector_check_same_ptr_type_(a, b) \
|
||||||
|
(void) ((a) == (b)) /* warn on type mismatch */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push an item at the end of the vector
|
||||||
|
*
|
||||||
|
* The amortized complexity is O(1).
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param item the item to append
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_push(pv, item) \
|
||||||
|
({ \
|
||||||
|
bool ok = sc_vector_reserve(pv, (pv)->size + 1); \
|
||||||
|
if (ok) { \
|
||||||
|
(pv)->data[(pv)->size++] = (item); \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append `count` items at the end of the vector
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param items the items array to append
|
||||||
|
* \param count the number of items in the array
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_push_all(pv, items, count) \
|
||||||
|
sc_vector_push_all_(pv, items, (size_t) count)
|
||||||
|
|
||||||
|
#define sc_vector_push_all_(pv, items, count) \
|
||||||
|
({ \
|
||||||
|
sc_vector_check_same_ptr_type_((pv)->data, items); \
|
||||||
|
bool ok = sc_vector_reserve(pv, (pv)->size + (count)); \
|
||||||
|
if (ok) { \
|
||||||
|
memcpy(&(pv)->data[(pv)->size], items, (count) * sizeof(*(pv)->data)); \
|
||||||
|
(pv)->size += count; \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert an hole of size `count` to the given index
|
||||||
|
*
|
||||||
|
* The items in range [index; size-1] will be moved. The items in the hole are
|
||||||
|
* left uninitialized.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index where the hole is to be inserted
|
||||||
|
* \param count the number of items in the hole
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_insert_hole(pv, index, count) \
|
||||||
|
sc_vector_insert_hole_(pv, (size_t) index, (size_t) count);
|
||||||
|
|
||||||
|
#define sc_vector_insert_hole_(pv, index, count) \
|
||||||
|
({ \
|
||||||
|
bool ok = sc_vector_reserve(pv, (pv)->size + (count)); \
|
||||||
|
if (ok) { \
|
||||||
|
if ((index) < (pv)->size) { \
|
||||||
|
memmove(&(pv)->data[(index) + (count)], \
|
||||||
|
&(pv)->data[(index)], \
|
||||||
|
((pv)->size - (index)) * sizeof(*(pv)->data)); \
|
||||||
|
} \
|
||||||
|
(pv)->size += count; \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert an item at the given index
|
||||||
|
*
|
||||||
|
* The items in range [index; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index where the item is to be inserted
|
||||||
|
* \param item the item to append
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_insert(pv, index, item) \
|
||||||
|
sc_vector_insert_(pv, (size_t) index, (size_t) item);
|
||||||
|
|
||||||
|
#define sc_vector_insert_(pv, index, item) \
|
||||||
|
({ \
|
||||||
|
bool ok = sc_vector_insert_hole_(pv, index, 1); \
|
||||||
|
if (ok) { \
|
||||||
|
(pv)->data[index] = (item); \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert `count` items at the given index
|
||||||
|
*
|
||||||
|
* The items in range [index; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index where the items are to be inserted
|
||||||
|
* \param items the items array to append
|
||||||
|
* \param count the number of items in the array
|
||||||
|
* \retval true if no allocation failed
|
||||||
|
* \retval false on allocation failure (the vector is left untouched)
|
||||||
|
*/
|
||||||
|
#define sc_vector_insert_all(pv, index, items, count) \
|
||||||
|
sc_vector_insert_all_(pv, (size_t) index, items, (size_t) count)
|
||||||
|
|
||||||
|
#define sc_vector_insert_all_(pv, index, items, count) \
|
||||||
|
({ \
|
||||||
|
sc_vector_check_same_ptr_type_((pv)->data, items); \
|
||||||
|
bool ok = sc_vector_insert_hole_(pv, index, count); \
|
||||||
|
if (ok) { \
|
||||||
|
memcpy(&(pv)->data[index], items, count * sizeof(*(pv)->data)); \
|
||||||
|
} \
|
||||||
|
ok; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/** Reverse a char array in place */
|
||||||
|
static inline void
|
||||||
|
sc_char_array_reverse(char *array, size_t len)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < len / 2; ++i)
|
||||||
|
{
|
||||||
|
char c = array[i];
|
||||||
|
array[i] = array[len - i - 1];
|
||||||
|
array[len - i - 1] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Right-rotate a (char) array in place
|
||||||
|
*
|
||||||
|
* For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
|
||||||
|
* distance 4 will result in {5, 6, 1, 2, 3, 4}.
|
||||||
|
*
|
||||||
|
* Private.
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
sc_char_array_rotate_left(char *array, size_t len, size_t distance)
|
||||||
|
{
|
||||||
|
sc_char_array_reverse(array, distance);
|
||||||
|
sc_char_array_reverse(&array[distance], len - distance);
|
||||||
|
sc_char_array_reverse(array, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Right-rotate a (char) array in place
|
||||||
|
*
|
||||||
|
* For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
|
||||||
|
* distance 2 will result in {5, 6, 1, 2, 3, 4}.
|
||||||
|
*
|
||||||
|
* Private.
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
sc_char_array_rotate_right(char *array, size_t len, size_t distance)
|
||||||
|
{
|
||||||
|
sc_char_array_rotate_left(array, len, len - distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move items in a (char) array in place
|
||||||
|
*
|
||||||
|
* Move slice [index, count] to target.
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
sc_char_array_move(char *array, size_t idx, size_t count, size_t target)
|
||||||
|
{
|
||||||
|
if (idx < target) {
|
||||||
|
sc_char_array_rotate_left(&array[idx], target - idx + count, count);
|
||||||
|
} else {
|
||||||
|
sc_char_array_rotate_right(&array[target], idx - target + count, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move a slice of items to a given target index
|
||||||
|
*
|
||||||
|
* The items in range [index; count] will be moved so that the *new* position
|
||||||
|
* of the first item is `target`.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of the first item to move
|
||||||
|
* \param count the number of items to move
|
||||||
|
* \param target the new index of the moved slice
|
||||||
|
*/
|
||||||
|
#define sc_vector_move_slice(pv, index, count, target) \
|
||||||
|
sc_vector_move_slice_(pv, (size_t) index, count, (size_t) target);
|
||||||
|
|
||||||
|
#define sc_vector_move_slice_(pv, index, count, target) \
|
||||||
|
({ \
|
||||||
|
sc_char_array_move((char *) (pv)->data, \
|
||||||
|
(index) * sizeof(*(pv)->data), \
|
||||||
|
(count) * sizeof(*(pv)->data), \
|
||||||
|
(target) * sizeof(*(pv)->data)); \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move an item to a given target index
|
||||||
|
*
|
||||||
|
* The items will be moved so that its *new* position is `target`.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of the item to move
|
||||||
|
* \param target the new index of the moved item
|
||||||
|
*/
|
||||||
|
#define sc_vector_move(pv, index, target) \
|
||||||
|
sc_vector_move_slice(pv, index, 1, target)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a slice of items, without shrinking the array
|
||||||
|
*
|
||||||
|
* If you have no good reason to use the _noshrink() version, use
|
||||||
|
* sc_vector_remove_slice() instead.
|
||||||
|
*
|
||||||
|
* The items in range [index+count; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of the first item to remove
|
||||||
|
* \param count the number of items to remove
|
||||||
|
*/
|
||||||
|
#define sc_vector_remove_slice_noshrink(pv, index, count) \
|
||||||
|
sc_vector_remove_slice_noshrink_(pv, (size_t) index, (size_t) count)
|
||||||
|
|
||||||
|
#define sc_vector_remove_slice_noshrink_(pv, index, count) \
|
||||||
|
({ \
|
||||||
|
if ((index) + (count) < (pv)->size) { \
|
||||||
|
memmove(&(pv)->data[index], \
|
||||||
|
&(pv)->data[(index) + (count)], \
|
||||||
|
((pv)->size - (index) - (count)) * sizeof(*(pv)->data)); \
|
||||||
|
} \
|
||||||
|
(pv)->size -= count; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a slice of items
|
||||||
|
*
|
||||||
|
* The items in range [index+count; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of the first item to remove
|
||||||
|
* \param count the number of items to remove
|
||||||
|
*/
|
||||||
|
#define sc_vector_remove_slice(pv, index, count) \
|
||||||
|
({ \
|
||||||
|
sc_vector_remove_slice_noshrink(pv, index, count); \
|
||||||
|
sc_vector_autoshrink(pv); \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an item, without shrinking the array
|
||||||
|
*
|
||||||
|
* If you have no good reason to use the _noshrink() version, use
|
||||||
|
* sc_vector_remove() instead.
|
||||||
|
*
|
||||||
|
* The items in range [index+1; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of item to remove
|
||||||
|
*/
|
||||||
|
#define sc_vector_remove_noshrink(pv, index) \
|
||||||
|
sc_vector_remove_slice_noshrink(pv, index, 1)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an item
|
||||||
|
*
|
||||||
|
* The items in range [index+1; size-1] will be moved.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of item to remove
|
||||||
|
*/
|
||||||
|
#define sc_vector_remove(pv, index) \
|
||||||
|
({ \
|
||||||
|
sc_vector_remove_noshrink(pv, index); \
|
||||||
|
sc_vector_autoshrink(pv); \
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an item
|
||||||
|
*
|
||||||
|
* The removed item is replaced by the last item of the vector.
|
||||||
|
*
|
||||||
|
* This does not preserve ordering, but is O(1). This is useful when the order
|
||||||
|
* of items is not meaningful.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param index the index of item to remove
|
||||||
|
*/
|
||||||
|
#define sc_vector_swap_remove(pv, index) \
|
||||||
|
sc_vector_swap_remove_(pv, (size_t) index);
|
||||||
|
|
||||||
|
#define sc_vector_swap_remove_(pv, index) \
|
||||||
|
({ \
|
||||||
|
(pv)->data[index] = (pv)->data[(pv)->size-1]; \
|
||||||
|
(pv)->size--; \
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the index of an item
|
||||||
|
*
|
||||||
|
* Iterate over all items to find a given item.
|
||||||
|
*
|
||||||
|
* Use only for vectors of primitive types or pointers.
|
||||||
|
*
|
||||||
|
* Return the index, or -1 if not found.
|
||||||
|
*
|
||||||
|
* \param pv a pointer to the vector
|
||||||
|
* \param item the item to find (compared with ==)
|
||||||
|
*/
|
||||||
|
#define sc_vector_index_of(pv, item) \
|
||||||
|
({ \
|
||||||
|
ssize_t idx = -1; \
|
||||||
|
for (size_t i = 0; i < (pv)->size; ++i) { \
|
||||||
|
if ((pv)->data[i] == (item)) { \
|
||||||
|
idx = (ssize_t) i; \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
idx; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -274,7 +274,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
LOGD("Starting v4l2 thread");
|
LOGD("Starting v4l2 thread");
|
||||||
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "scrcpy-v4l2", vs);
|
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "scrcpy-v4l2", vs);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start v4l2 thread");
|
LOGE("Could not start v4l2 thread");
|
||||||
goto error_av_packet_free;
|
goto error_av_packet_free;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
67
app/src/version.c
Normal file
67
app/src/version.c
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#include "version.h"
|
||||||
|
|
||||||
|
#include <libavcodec/avcodec.h>
|
||||||
|
#include <libavformat/avformat.h>
|
||||||
|
#include <libavutil/avutil.h>
|
||||||
|
#ifdef HAVE_V4L2
|
||||||
|
# include <libavdevice/avdevice.h>
|
||||||
|
#endif
|
||||||
|
#ifdef HAVE_USB
|
||||||
|
# include <libusb-1.0/libusb.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
scrcpy_print_version(void) {
|
||||||
|
printf("\nDependencies (compiled / linked):\n");
|
||||||
|
|
||||||
|
SDL_version sdl;
|
||||||
|
SDL_GetVersion(&sdl);
|
||||||
|
printf(" - SDL: %u.%u.%u / %u.%u.%u\n",
|
||||||
|
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
|
||||||
|
(unsigned) sdl.major, (unsigned) sdl.minor, (unsigned) sdl.patch);
|
||||||
|
|
||||||
|
unsigned avcodec = avcodec_version();
|
||||||
|
printf(" - libavcodec: %u.%u.%u / %u.%u.%u\n",
|
||||||
|
LIBAVCODEC_VERSION_MAJOR,
|
||||||
|
LIBAVCODEC_VERSION_MINOR,
|
||||||
|
LIBAVCODEC_VERSION_MICRO,
|
||||||
|
AV_VERSION_MAJOR(avcodec),
|
||||||
|
AV_VERSION_MINOR(avcodec),
|
||||||
|
AV_VERSION_MICRO(avcodec));
|
||||||
|
|
||||||
|
unsigned avformat = avformat_version();
|
||||||
|
printf(" - libavformat: %u.%u.%u / %u.%u.%u\n",
|
||||||
|
LIBAVFORMAT_VERSION_MAJOR,
|
||||||
|
LIBAVFORMAT_VERSION_MINOR,
|
||||||
|
LIBAVFORMAT_VERSION_MICRO,
|
||||||
|
AV_VERSION_MAJOR(avformat),
|
||||||
|
AV_VERSION_MINOR(avformat),
|
||||||
|
AV_VERSION_MICRO(avformat));
|
||||||
|
|
||||||
|
unsigned avutil = avutil_version();
|
||||||
|
printf(" - libavutil: %u.%u.%u / %u.%u.%u\n",
|
||||||
|
LIBAVUTIL_VERSION_MAJOR,
|
||||||
|
LIBAVUTIL_VERSION_MINOR,
|
||||||
|
LIBAVUTIL_VERSION_MICRO,
|
||||||
|
AV_VERSION_MAJOR(avutil),
|
||||||
|
AV_VERSION_MINOR(avutil),
|
||||||
|
AV_VERSION_MICRO(avutil));
|
||||||
|
|
||||||
|
#ifdef HAVE_V4L2
|
||||||
|
unsigned avdevice = avdevice_version();
|
||||||
|
printf(" - libavdevice: %u.%u.%u / %u.%u.%u\n",
|
||||||
|
LIBAVDEVICE_VERSION_MAJOR,
|
||||||
|
LIBAVDEVICE_VERSION_MINOR,
|
||||||
|
LIBAVDEVICE_VERSION_MICRO,
|
||||||
|
AV_VERSION_MAJOR(avdevice),
|
||||||
|
AV_VERSION_MINOR(avdevice),
|
||||||
|
AV_VERSION_MICRO(avdevice));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_USB
|
||||||
|
const struct libusb_version *usb = libusb_get_version();
|
||||||
|
// The compiled version may not be known
|
||||||
|
printf(" - libusb: - / %u.%u.%u\n",
|
||||||
|
(unsigned) usb->major, (unsigned) usb->minor, (unsigned) usb->micro);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
9
app/src/version.h
Normal file
9
app/src/version.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef SC_VERSION_H
|
||||||
|
#define SC_VERSION_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
scrcpy_print_version(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,13 +2,172 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "adb_parser.h"
|
#include "adb/adb_device.h"
|
||||||
|
#include "adb/adb_parser.h"
|
||||||
|
|
||||||
|
static void test_adb_devices() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\n"
|
||||||
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice transport_id:1\n"
|
||||||
|
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
|
||||||
|
"device:MyWifiDevice trandport_id:2\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 2);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyModel", device->model));
|
||||||
|
|
||||||
|
device = &vec.data[1];
|
||||||
|
assert(!strcmp("192.168.1.1:5555", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyWifiModel", device->model));
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_cr() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\r\n"
|
||||||
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice transport_id:1\r\n"
|
||||||
|
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
|
||||||
|
"device:MyWifiDevice trandport_id:2\r\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 2);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyModel", device->model));
|
||||||
|
|
||||||
|
device = &vec.data[1];
|
||||||
|
assert(!strcmp("192.168.1.1:5555", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyWifiModel", device->model));
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_daemon_start() {
|
||||||
|
char output[] =
|
||||||
|
"* daemon not running; starting now at tcp:5037\n"
|
||||||
|
"* daemon started successfully\n"
|
||||||
|
"List of devices attached\n"
|
||||||
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice transport_id:1\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 1);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyModel", device->model));
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_daemon_start_mixed() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\n"
|
||||||
|
"adb server version (41) doesn't match this client (39); killing...\n"
|
||||||
|
"* daemon started successfully *\n"
|
||||||
|
"0123456789abcdef unauthorized usb:1-1\n"
|
||||||
|
"87654321 device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 2);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("unauthorized", device->state));
|
||||||
|
assert(!device->model);
|
||||||
|
|
||||||
|
device = &vec.data[1];
|
||||||
|
assert(!strcmp("87654321", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyModel", device->model));
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_without_eol() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\n"
|
||||||
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice transport_id:1";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 1);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("device", device->state));
|
||||||
|
assert(!strcmp("MyModel", device->model));
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_without_header() {
|
||||||
|
char output[] =
|
||||||
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
||||||
|
"device:MyDevice transport_id:1\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(!ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_corrupted() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\n"
|
||||||
|
"corrupted_garbage\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_adb_devices_spaces() {
|
||||||
|
char output[] =
|
||||||
|
"List of devices attached\n"
|
||||||
|
"0123456789abcdef unauthorized usb:1-4 transport_id:3\n";
|
||||||
|
|
||||||
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok = sc_adb_parse_devices(output, &vec);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 1);
|
||||||
|
|
||||||
|
struct sc_adb_device *device = &vec.data[0];
|
||||||
|
assert(!strcmp("0123456789abcdef", device->serial));
|
||||||
|
assert(!strcmp("unauthorized", device->state));
|
||||||
|
assert(!device->model);
|
||||||
|
|
||||||
|
sc_adb_devices_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_get_ip_single_line() {
|
static void test_get_ip_single_line() {
|
||||||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
"192.168.12.34\r\r\n";
|
"192.168.12.34\r\r\n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(ip);
|
assert(ip);
|
||||||
assert(!strcmp(ip, "192.168.12.34"));
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
free(ip);
|
free(ip);
|
||||||
@@ -18,7 +177,7 @@ static void test_get_ip_single_line_without_eol() {
|
|||||||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
"192.168.12.34";
|
"192.168.12.34";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(ip);
|
assert(ip);
|
||||||
assert(!strcmp(ip, "192.168.12.34"));
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
free(ip);
|
free(ip);
|
||||||
@@ -28,7 +187,7 @@ static void test_get_ip_single_line_with_trailing_space() {
|
|||||||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
"192.168.12.34 \n";
|
"192.168.12.34 \n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(ip);
|
assert(ip);
|
||||||
assert(!strcmp(ip, "192.168.12.34"));
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
free(ip);
|
free(ip);
|
||||||
@@ -40,7 +199,7 @@ static void test_get_ip_multiline_first_ok() {
|
|||||||
"10.0.0.0/24 dev rmnet proto kernel scope link src "
|
"10.0.0.0/24 dev rmnet proto kernel scope link src "
|
||||||
"10.0.0.2\r\n";
|
"10.0.0.2\r\n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(ip);
|
assert(ip);
|
||||||
assert(!strcmp(ip, "192.168.1.2"));
|
assert(!strcmp(ip, "192.168.1.2"));
|
||||||
free(ip);
|
free(ip);
|
||||||
@@ -52,7 +211,7 @@ static void test_get_ip_multiline_second_ok() {
|
|||||||
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
"192.168.1.3\r\n";
|
"192.168.1.3\r\n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(ip);
|
assert(ip);
|
||||||
assert(!strcmp(ip, "192.168.1.3"));
|
assert(!strcmp(ip, "192.168.1.3"));
|
||||||
free(ip);
|
free(ip);
|
||||||
@@ -62,7 +221,15 @@ static void test_get_ip_no_wlan() {
|
|||||||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
"192.168.12.34\r\r\n";
|
"192.168.12.34\r\r\n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
|
assert(!ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_no_wlan_without_eol() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
|
"192.168.12.34";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(!ip);
|
assert(!ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +237,7 @@ static void test_get_ip_truncated() {
|
|||||||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||||
assert(!ip);
|
assert(!ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +245,21 @@ int main(int argc, char *argv[]) {
|
|||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
|
|
||||||
|
test_adb_devices();
|
||||||
|
test_adb_devices_cr();
|
||||||
|
test_adb_devices_daemon_start();
|
||||||
|
test_adb_devices_daemon_start_mixed();
|
||||||
|
test_adb_devices_without_eol();
|
||||||
|
test_adb_devices_without_header();
|
||||||
|
test_adb_devices_corrupted();
|
||||||
|
test_adb_devices_spaces();
|
||||||
|
|
||||||
test_get_ip_single_line();
|
test_get_ip_single_line();
|
||||||
test_get_ip_single_line_without_eol();
|
test_get_ip_single_line_without_eol();
|
||||||
test_get_ip_single_line_with_trailing_space();
|
test_get_ip_single_line_with_trailing_space();
|
||||||
test_get_ip_multiline_first_ok();
|
test_get_ip_multiline_first_ok();
|
||||||
test_get_ip_multiline_second_ok();
|
test_get_ip_multiline_second_ok();
|
||||||
test_get_ip_no_wlan();
|
test_get_ip_no_wlan();
|
||||||
|
test_get_ip_no_wlan_without_eol();
|
||||||
test_get_ip_truncated();
|
test_get_ip_truncated();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ static void test_buffer_write16be(void) {
|
|||||||
uint16_t val = 0xABCD;
|
uint16_t val = 0xABCD;
|
||||||
uint8_t buf[2];
|
uint8_t buf[2];
|
||||||
|
|
||||||
buffer_write16be(buf, val);
|
sc_write16be(buf, val);
|
||||||
|
|
||||||
assert(buf[0] == 0xAB);
|
assert(buf[0] == 0xAB);
|
||||||
assert(buf[1] == 0xCD);
|
assert(buf[1] == 0xCD);
|
||||||
@@ -18,7 +18,7 @@ static void test_buffer_write32be(void) {
|
|||||||
uint32_t val = 0xABCD1234;
|
uint32_t val = 0xABCD1234;
|
||||||
uint8_t buf[4];
|
uint8_t buf[4];
|
||||||
|
|
||||||
buffer_write32be(buf, val);
|
sc_write32be(buf, val);
|
||||||
|
|
||||||
assert(buf[0] == 0xAB);
|
assert(buf[0] == 0xAB);
|
||||||
assert(buf[1] == 0xCD);
|
assert(buf[1] == 0xCD);
|
||||||
@@ -30,7 +30,7 @@ static void test_buffer_write64be(void) {
|
|||||||
uint64_t val = 0xABCD1234567890EF;
|
uint64_t val = 0xABCD1234567890EF;
|
||||||
uint8_t buf[8];
|
uint8_t buf[8];
|
||||||
|
|
||||||
buffer_write64be(buf, val);
|
sc_write64be(buf, val);
|
||||||
|
|
||||||
assert(buf[0] == 0xAB);
|
assert(buf[0] == 0xAB);
|
||||||
assert(buf[1] == 0xCD);
|
assert(buf[1] == 0xCD);
|
||||||
@@ -45,7 +45,7 @@ static void test_buffer_write64be(void) {
|
|||||||
static void test_buffer_read16be(void) {
|
static void test_buffer_read16be(void) {
|
||||||
uint8_t buf[2] = {0xAB, 0xCD};
|
uint8_t buf[2] = {0xAB, 0xCD};
|
||||||
|
|
||||||
uint16_t val = buffer_read16be(buf);
|
uint16_t val = sc_read16be(buf);
|
||||||
|
|
||||||
assert(val == 0xABCD);
|
assert(val == 0xABCD);
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ static void test_buffer_read16be(void) {
|
|||||||
static void test_buffer_read32be(void) {
|
static void test_buffer_read32be(void) {
|
||||||
uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
|
uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
|
||||||
|
|
||||||
uint32_t val = buffer_read32be(buf);
|
uint32_t val = sc_read32be(buf);
|
||||||
|
|
||||||
assert(val == 0xABCD1234);
|
assert(val == 0xABCD1234);
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ static void test_buffer_read64be(void) {
|
|||||||
uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
|
uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
|
||||||
0x56, 0x78, 0x90, 0xEF};
|
0x56, 0x78, 0x90, 0xEF};
|
||||||
|
|
||||||
uint64_t val = buffer_read64be(buf);
|
uint64_t val = sc_read64be(buf);
|
||||||
|
|
||||||
assert(val == 0xABCD1234567890EF);
|
assert(val == 0xABCD1234567890EF);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -338,32 +338,6 @@ static void test_wrap_lines(void) {
|
|||||||
free(formatted);
|
free(formatted);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_truncate(void) {
|
|
||||||
char s[] = "hello\nworld\n!";
|
|
||||||
size_t len = sc_str_truncate(s, sizeof(s), "\n");
|
|
||||||
|
|
||||||
assert(len == 5);
|
|
||||||
assert(!strcmp("hello", s));
|
|
||||||
|
|
||||||
char s2[] = "hello\r\nworkd\r\n!";
|
|
||||||
len = sc_str_truncate(s2, sizeof(s2), "\n\r");
|
|
||||||
|
|
||||||
assert(len == 5);
|
|
||||||
assert(!strcmp("hello", s));
|
|
||||||
|
|
||||||
char s3[] = "hello world\n!";
|
|
||||||
len = sc_str_truncate(s3, sizeof(s3), " \n\r");
|
|
||||||
|
|
||||||
assert(len == 5);
|
|
||||||
assert(!strcmp("hello", s3));
|
|
||||||
|
|
||||||
char s4[] = "hello ";
|
|
||||||
len = sc_str_truncate(s4, sizeof(s4), " \n\r");
|
|
||||||
|
|
||||||
assert(len == 5);
|
|
||||||
assert(!strcmp("hello", s4));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_index_of_column(void) {
|
static void test_index_of_column(void) {
|
||||||
assert(sc_str_index_of_column("a bc d", 0, " ") == 0);
|
assert(sc_str_index_of_column("a bc d", 0, " ") == 0);
|
||||||
assert(sc_str_index_of_column("a bc d", 1, " ") == 2);
|
assert(sc_str_index_of_column("a bc d", 1, " ") == 2);
|
||||||
@@ -417,7 +391,6 @@ int main(int argc, char *argv[]) {
|
|||||||
test_parse_integer_with_suffix();
|
test_parse_integer_with_suffix();
|
||||||
test_strlist_contains();
|
test_strlist_contains();
|
||||||
test_wrap_lines();
|
test_wrap_lines();
|
||||||
test_truncate();
|
|
||||||
test_index_of_column();
|
test_index_of_column();
|
||||||
test_remove_trailing_cr();
|
test_remove_trailing_cr();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
421
app/tests/test_vector.c
Normal file
421
app/tests/test_vector.c
Normal file
@@ -0,0 +1,421 @@
|
|||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "util/vector.h"
|
||||||
|
|
||||||
|
static void test_vector_insert_remove(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 42);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.data[0] == 42);
|
||||||
|
assert(vec.size == 1);
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 37);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 2);
|
||||||
|
assert(vec.data[0] == 42);
|
||||||
|
assert(vec.data[1] == 37);
|
||||||
|
|
||||||
|
ok = sc_vector_insert(&vec, 1, 100);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 3);
|
||||||
|
assert(vec.data[0] == 42);
|
||||||
|
assert(vec.data[1] == 100);
|
||||||
|
assert(vec.data[2] == 37);
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 77);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 4);
|
||||||
|
assert(vec.data[0] == 42);
|
||||||
|
assert(vec.data[1] == 100);
|
||||||
|
assert(vec.data[2] == 37);
|
||||||
|
assert(vec.data[3] == 77);
|
||||||
|
|
||||||
|
sc_vector_remove(&vec, 1);
|
||||||
|
assert(vec.size == 3);
|
||||||
|
assert(vec.data[0] == 42);
|
||||||
|
assert(vec.data[1] == 37);
|
||||||
|
assert(vec.data[2] == 77);
|
||||||
|
|
||||||
|
sc_vector_clear(&vec);
|
||||||
|
assert(vec.size == 0);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_push_array(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 3); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 14); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 15); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 92); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 65); assert(ok);
|
||||||
|
assert(vec.size == 5);
|
||||||
|
|
||||||
|
int items[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||||
|
ok = sc_vector_push_all(&vec, items, 8);
|
||||||
|
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 13);
|
||||||
|
assert(vec.data[0] == 3);
|
||||||
|
assert(vec.data[1] == 14);
|
||||||
|
assert(vec.data[2] == 15);
|
||||||
|
assert(vec.data[3] == 92);
|
||||||
|
assert(vec.data[4] == 65);
|
||||||
|
assert(vec.data[5] == 1);
|
||||||
|
assert(vec.data[6] == 2);
|
||||||
|
assert(vec.data[7] == 3);
|
||||||
|
assert(vec.data[8] == 4);
|
||||||
|
assert(vec.data[9] == 5);
|
||||||
|
assert(vec.data[10] == 6);
|
||||||
|
assert(vec.data[11] == 7);
|
||||||
|
assert(vec.data[12] == 8);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_insert_array(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 3); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 14); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 15); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 92); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 65); assert(ok);
|
||||||
|
assert(vec.size == 5);
|
||||||
|
|
||||||
|
int items[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||||
|
ok = sc_vector_insert_all(&vec, 3, items, 8);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.size == 13);
|
||||||
|
assert(vec.data[0] == 3);
|
||||||
|
assert(vec.data[1] == 14);
|
||||||
|
assert(vec.data[2] == 15);
|
||||||
|
assert(vec.data[3] == 1);
|
||||||
|
assert(vec.data[4] == 2);
|
||||||
|
assert(vec.data[5] == 3);
|
||||||
|
assert(vec.data[6] == 4);
|
||||||
|
assert(vec.data[7] == 5);
|
||||||
|
assert(vec.data[8] == 6);
|
||||||
|
assert(vec.data[9] == 7);
|
||||||
|
assert(vec.data[10] == 8);
|
||||||
|
assert(vec.data[11] == 92);
|
||||||
|
assert(vec.data[12] == 65);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_remove_slice(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(vec.size == 100);
|
||||||
|
|
||||||
|
sc_vector_remove_slice(&vec, 32, 60);
|
||||||
|
assert(vec.size == 40);
|
||||||
|
assert(vec.data[31] == 31);
|
||||||
|
assert(vec.data[32] == 92);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_swap_remove(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_push(&vec, 3); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 14); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 15); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 92); assert(ok);
|
||||||
|
ok = sc_vector_push(&vec, 65); assert(ok);
|
||||||
|
assert(vec.size == 5);
|
||||||
|
|
||||||
|
sc_vector_swap_remove(&vec, 1);
|
||||||
|
assert(vec.size == 4);
|
||||||
|
assert(vec.data[0] == 3);
|
||||||
|
assert(vec.data[1] == 65);
|
||||||
|
assert(vec.data[2] == 15);
|
||||||
|
assert(vec.data[3] == 92);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_index_of(void) {
|
||||||
|
struct SC_VECTOR(int) vec;
|
||||||
|
sc_vector_init(&vec);
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t idx;
|
||||||
|
|
||||||
|
idx = sc_vector_index_of(&vec, 0);
|
||||||
|
assert(idx == 0);
|
||||||
|
|
||||||
|
idx = sc_vector_index_of(&vec, 1);
|
||||||
|
assert(idx == 1);
|
||||||
|
|
||||||
|
idx = sc_vector_index_of(&vec, 4);
|
||||||
|
assert(idx == 4);
|
||||||
|
|
||||||
|
idx = sc_vector_index_of(&vec, 9);
|
||||||
|
assert(idx == 9);
|
||||||
|
|
||||||
|
idx = sc_vector_index_of(&vec, 12);
|
||||||
|
assert(idx == -1);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_grow() {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
for (int i = 0; i < 50; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i); /* append */
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(vec.cap >= 50);
|
||||||
|
assert(vec.size == 50);
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_insert(&vec, 20, i); /* insert in the middle */
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(vec.cap >= 75);
|
||||||
|
assert(vec.size == 75);
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_insert(&vec, 0, i); /* prepend */
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(vec.cap >= 100);
|
||||||
|
assert(vec.size == 100);
|
||||||
|
|
||||||
|
for (int i = 0; i < 50; ++i)
|
||||||
|
sc_vector_remove(&vec, 20); /* remove from the middle */
|
||||||
|
|
||||||
|
assert(vec.cap >= 50);
|
||||||
|
assert(vec.size == 50);
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; ++i)
|
||||||
|
sc_vector_remove(&vec, 0); /* remove from the head */
|
||||||
|
|
||||||
|
assert(vec.cap >= 25);
|
||||||
|
assert(vec.size == 25);
|
||||||
|
|
||||||
|
for (int i = 24; i >=0; --i)
|
||||||
|
sc_vector_remove(&vec, i); /* remove from the tail */
|
||||||
|
|
||||||
|
assert(vec.size == 0);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_exp_growth(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
size_t oldcap = vec.cap;
|
||||||
|
int realloc_count = 0;
|
||||||
|
bool ok;
|
||||||
|
for (int i = 0; i < 10000; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
if (vec.cap != oldcap)
|
||||||
|
{
|
||||||
|
realloc_count++;
|
||||||
|
oldcap = vec.cap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test speciically for an expected growth factor of 1.5. In practice, the
|
||||||
|
* result is even lower (19) due to the first alloc of size 10 */
|
||||||
|
assert(realloc_count <= 23); /* ln(10000) / ln(1.5) ~= 23 */
|
||||||
|
|
||||||
|
realloc_count = 0;
|
||||||
|
for (int i = 9999; i >= 0; --i)
|
||||||
|
{
|
||||||
|
sc_vector_remove(&vec, i);
|
||||||
|
if (vec.cap != oldcap)
|
||||||
|
{
|
||||||
|
realloc_count++;
|
||||||
|
oldcap = vec.cap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(realloc_count <= 23); /* same expectations for removals */
|
||||||
|
assert(realloc_count > 0); /* sc_vector_remove() must autoshrink */
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_reserve(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_reserve(&vec, 800);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.cap >= 800);
|
||||||
|
assert(vec.size == 0);
|
||||||
|
|
||||||
|
size_t initial_cap = vec.cap;
|
||||||
|
|
||||||
|
for (int i = 0; i < 800; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
assert(vec.cap == initial_cap); /* no realloc */
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_shrink_to_fit(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
ok = sc_vector_reserve(&vec, 800);
|
||||||
|
assert(ok);
|
||||||
|
for (int i = 0; i < 250; ++i)
|
||||||
|
{
|
||||||
|
ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(vec.cap >= 800);
|
||||||
|
assert(vec.size == 250);
|
||||||
|
|
||||||
|
sc_vector_shrink_to_fit(&vec);
|
||||||
|
assert(vec.cap == 250);
|
||||||
|
assert(vec.size == 250);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_move(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
for (int i = 0; i < 7; ++i)
|
||||||
|
{
|
||||||
|
bool ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* move item at 1 so that its new position is 4 */
|
||||||
|
sc_vector_move(&vec, 1, 4);
|
||||||
|
|
||||||
|
assert(vec.size == 7);
|
||||||
|
assert(vec.data[0] == 0);
|
||||||
|
assert(vec.data[1] == 2);
|
||||||
|
assert(vec.data[2] == 3);
|
||||||
|
assert(vec.data[3] == 4);
|
||||||
|
assert(vec.data[4] == 1);
|
||||||
|
assert(vec.data[5] == 5);
|
||||||
|
assert(vec.data[6] == 6);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_move_slice_forward(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; ++i)
|
||||||
|
{
|
||||||
|
bool ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* move slice {2, 3, 4, 5} so that its new position is 5 */
|
||||||
|
sc_vector_move_slice(&vec, 2, 4, 5);
|
||||||
|
|
||||||
|
assert(vec.size == 10);
|
||||||
|
assert(vec.data[0] == 0);
|
||||||
|
assert(vec.data[1] == 1);
|
||||||
|
assert(vec.data[2] == 6);
|
||||||
|
assert(vec.data[3] == 7);
|
||||||
|
assert(vec.data[4] == 8);
|
||||||
|
assert(vec.data[5] == 2);
|
||||||
|
assert(vec.data[6] == 3);
|
||||||
|
assert(vec.data[7] == 4);
|
||||||
|
assert(vec.data[8] == 5);
|
||||||
|
assert(vec.data[9] == 9);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_vector_move_slice_backward(void) {
|
||||||
|
struct SC_VECTOR(int) vec = SC_VECTOR_INITIALIZER;
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; ++i)
|
||||||
|
{
|
||||||
|
bool ok = sc_vector_push(&vec, i);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* move slice {5, 6, 7} so that its new position is 2 */
|
||||||
|
sc_vector_move_slice(&vec, 5, 3, 2);
|
||||||
|
|
||||||
|
assert(vec.size == 10);
|
||||||
|
assert(vec.data[0] == 0);
|
||||||
|
assert(vec.data[1] == 1);
|
||||||
|
assert(vec.data[2] == 5);
|
||||||
|
assert(vec.data[3] == 6);
|
||||||
|
assert(vec.data[4] == 7);
|
||||||
|
assert(vec.data[5] == 2);
|
||||||
|
assert(vec.data[6] == 3);
|
||||||
|
assert(vec.data[7] == 4);
|
||||||
|
assert(vec.data[8] == 8);
|
||||||
|
assert(vec.data[9] == 9);
|
||||||
|
|
||||||
|
sc_vector_destroy(&vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
(void) argc;
|
||||||
|
(void) argv;
|
||||||
|
|
||||||
|
test_vector_insert_remove();
|
||||||
|
test_vector_push_array();
|
||||||
|
test_vector_insert_array();
|
||||||
|
test_vector_remove_slice();
|
||||||
|
test_vector_swap_remove();
|
||||||
|
test_vector_move();
|
||||||
|
test_vector_move_slice_forward();
|
||||||
|
test_vector_move_slice_backward();
|
||||||
|
test_vector_index_of();
|
||||||
|
test_vector_grow();
|
||||||
|
test_vector_exp_growth();
|
||||||
|
test_vector_reserve();
|
||||||
|
test_vector_shrink_to_fit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -21,3 +21,5 @@ ffmpeg_avformat = 'avformat-58'
|
|||||||
ffmpeg_avutil = 'avutil-56'
|
ffmpeg_avutil = 'avutil-56'
|
||||||
prebuilt_ffmpeg = 'ffmpeg-win32-4.3.1'
|
prebuilt_ffmpeg = 'ffmpeg-win32-4.3.1'
|
||||||
prebuilt_sdl2 = 'SDL2-2.0.20/i686-w64-mingw32'
|
prebuilt_sdl2 = 'SDL2-2.0.20/i686-w64-mingw32'
|
||||||
|
prebuilt_libusb_root = 'libusb-1.0.25'
|
||||||
|
prebuilt_libusb = prebuilt_libusb_root + '/MinGW32'
|
||||||
|
|||||||
@@ -21,3 +21,5 @@ ffmpeg_avformat = 'avformat-59'
|
|||||||
ffmpeg_avutil = 'avutil-57'
|
ffmpeg_avutil = 'avutil-57'
|
||||||
prebuilt_ffmpeg = 'ffmpeg-win64-5.0'
|
prebuilt_ffmpeg = 'ffmpeg-win64-5.0'
|
||||||
prebuilt_sdl2 = 'SDL2-2.0.20/x86_64-w64-mingw32'
|
prebuilt_sdl2 = 'SDL2-2.0.20/x86_64-w64-mingw32'
|
||||||
|
prebuilt_libusb_root = 'libusb-1.0.25'
|
||||||
|
prebuilt_libusb = prebuilt_libusb_root + '/MinGW64'
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
BUILDDIR=build-auto
|
BUILDDIR=build-auto
|
||||||
PREBUILT_SERVER_URL=https://github.com/Genymobile/scrcpy/releases/download/v1.22/scrcpy-server-v1.22
|
PREBUILT_SERVER_URL=https://github.com/Genymobile/scrcpy/releases/download/v1.23/scrcpy-server-v1.23
|
||||||
PREBUILT_SERVER_SHA256=c05d273eec7533c0e106282e0254cf04e7f5e8f0c2920ca39448865fab2a419b
|
PREBUILT_SERVER_SHA256=2a913fd47478c0b306fca507cb0beb625e49a19ff9fc7ab904e36ef5b9fe7e68
|
||||||
|
|
||||||
echo "[scrcpy] Downloading prebuilt server..."
|
echo "[scrcpy] Downloading prebuilt server..."
|
||||||
wget "$PREBUILT_SERVER_URL" -O scrcpy-server
|
wget "$PREBUILT_SERVER_URL" -O scrcpy-server
|
||||||
@@ -12,7 +12,7 @@ echo "$PREBUILT_SERVER_SHA256 scrcpy-server" | sha256sum --check
|
|||||||
|
|
||||||
echo "[scrcpy] Building client..."
|
echo "[scrcpy] Building client..."
|
||||||
rm -rf "$BUILDDIR"
|
rm -rf "$BUILDDIR"
|
||||||
meson "$BUILDDIR" --buildtype release --strip -Db_lto=true \
|
meson "$BUILDDIR" --buildtype=release --strip -Db_lto=true \
|
||||||
-Dprebuilt_server=scrcpy-server
|
-Dprebuilt_server=scrcpy-server
|
||||||
cd "$BUILDDIR"
|
cd "$BUILDDIR"
|
||||||
ninja
|
ninja
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
project('scrcpy', 'c',
|
project('scrcpy', 'c',
|
||||||
version: '1.22',
|
version: '1.23',
|
||||||
meson_version: '>= 0.48',
|
meson_version: '>= 0.48',
|
||||||
default_options: [
|
default_options: [
|
||||||
'c_std=c11',
|
'c_std=c11',
|
||||||
|
|||||||
@@ -4,3 +4,5 @@ option('prebuilt_server', type: 'string', description: 'Path of the prebuilt ser
|
|||||||
option('portable', type: 'boolean', value: false, description: 'Use scrcpy-server from the same directory as the scrcpy executable')
|
option('portable', type: 'boolean', value: false, description: 'Use scrcpy-server from the same directory as the scrcpy executable')
|
||||||
option('server_debugger', type: 'boolean', value: false, description: 'Run a server debugger and wait for a client to be attached')
|
option('server_debugger', type: 'boolean', value: false, description: 'Run a server debugger and wait for a client to be attached')
|
||||||
option('server_debugger_method', type: 'combo', choices: ['old', 'new'], value: 'new', description: 'Select the debugger method (Android < 9: "old", Android >= 9: "new")')
|
option('server_debugger_method', type: 'combo', choices: ['old', 'new'], value: 'new', description: 'Select the debugger method (Android < 9: "old", Android >= 9: "new")')
|
||||||
|
option('v4l2', type: 'boolean', value: true, description: 'Enable V4L2 feature when supported')
|
||||||
|
option('usb', type: 'boolean', value: true, description: 'Enable HID/OTG features when supported')
|
||||||
|
|||||||
74
release.mk
74
release.mk
@@ -63,24 +63,28 @@ build-server:
|
|||||||
ninja -C "$(SERVER_BUILD_DIR)"
|
ninja -C "$(SERVER_BUILD_DIR)"
|
||||||
|
|
||||||
prepare-deps-win32:
|
prepare-deps-win32:
|
||||||
@prebuilt-deps/prepare-adb.sh
|
@app/prebuilt-deps/prepare-adb.sh
|
||||||
@prebuilt-deps/prepare-sdl.sh
|
@app/prebuilt-deps/prepare-sdl.sh
|
||||||
@prebuilt-deps/prepare-ffmpeg-win32.sh
|
@app/prebuilt-deps/prepare-ffmpeg-win32.sh
|
||||||
|
@app/prebuilt-deps/prepare-libusb.sh
|
||||||
|
|
||||||
|
prepare-deps-win64:
|
||||||
|
@app/prebuilt-deps/prepare-adb.sh
|
||||||
|
@app/prebuilt-deps/prepare-sdl.sh
|
||||||
|
@app/prebuilt-deps/prepare-ffmpeg-win64.sh
|
||||||
|
@app/prebuilt-deps/prepare-libusb.sh
|
||||||
|
|
||||||
build-win32: prepare-deps-win32
|
build-win32: prepare-deps-win32
|
||||||
|
# -Dusb=false because of libusb-win32 build issue, cf #3011
|
||||||
[ -d "$(WIN32_BUILD_DIR)" ] || ( mkdir "$(WIN32_BUILD_DIR)" && \
|
[ -d "$(WIN32_BUILD_DIR)" ] || ( mkdir "$(WIN32_BUILD_DIR)" && \
|
||||||
meson "$(WIN32_BUILD_DIR)" \
|
meson "$(WIN32_BUILD_DIR)" \
|
||||||
--cross-file cross_win32.txt \
|
--cross-file cross_win32.txt \
|
||||||
--buildtype release --strip -Db_lto=true \
|
--buildtype release --strip -Db_lto=true \
|
||||||
|
-Dusb=false \
|
||||||
-Dcompile_server=false \
|
-Dcompile_server=false \
|
||||||
-Dportable=true )
|
-Dportable=true )
|
||||||
ninja -C "$(WIN32_BUILD_DIR)"
|
ninja -C "$(WIN32_BUILD_DIR)"
|
||||||
|
|
||||||
prepare-deps-win64:
|
|
||||||
@prebuilt-deps/prepare-adb.sh
|
|
||||||
@prebuilt-deps/prepare-sdl.sh
|
|
||||||
@prebuilt-deps/prepare-ffmpeg-win64.sh
|
|
||||||
|
|
||||||
build-win64: prepare-deps-win64
|
build-win64: prepare-deps-win64
|
||||||
[ -d "$(WIN64_BUILD_DIR)" ] || ( mkdir "$(WIN64_BUILD_DIR)" && \
|
[ -d "$(WIN64_BUILD_DIR)" ] || ( mkdir "$(WIN64_BUILD_DIR)" && \
|
||||||
meson "$(WIN64_BUILD_DIR)" \
|
meson "$(WIN64_BUILD_DIR)" \
|
||||||
@@ -94,37 +98,39 @@ dist-win32: build-server build-win32
|
|||||||
mkdir -p "$(DIST)/$(WIN32_TARGET_DIR)"
|
mkdir -p "$(DIST)/$(WIN32_TARGET_DIR)"
|
||||||
cp "$(SERVER_BUILD_DIR)"/server/scrcpy-server "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp "$(SERVER_BUILD_DIR)"/server/scrcpy-server "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp "$(WIN32_BUILD_DIR)"/app/scrcpy.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp "$(WIN32_BUILD_DIR)"/app/scrcpy.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp data/scrcpy-console.bat "$(DIST)/$(WIN32_TARGET_DIR)"
|
cp app/data/scrcpy-console.bat "$(DIST)/$(WIN32_TARGET_DIR)"
|
||||||
cp data/scrcpy-noconsole.vbs "$(DIST)/$(WIN32_TARGET_DIR)"
|
cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN32_TARGET_DIR)"
|
||||||
cp data/icon.png "$(DIST)/$(WIN32_TARGET_DIR)"
|
cp app/data/icon.png "$(DIST)/$(WIN32_TARGET_DIR)"
|
||||||
cp data/open_a_terminal_here.bat "$(DIST)/$(WIN32_TARGET_DIR)"
|
cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN32_TARGET_DIR)"
|
||||||
cp prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avutil-56.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avutil-56.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avcodec-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avcodec-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avformat-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avformat-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swresample-3.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swresample-3.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swscale-5.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swscale-5.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/SDL2-2.0.20/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/SDL2-2.0.20/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
|
#cp app/prebuilt-deps/data/libusb-1.0.25/MinGW32/dll/libusb-1.0.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
|
|
||||||
dist-win64: build-server build-win64
|
dist-win64: build-server build-win64
|
||||||
mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)"
|
mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
cp "$(SERVER_BUILD_DIR)"/server/scrcpy-server "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp "$(SERVER_BUILD_DIR)"/server/scrcpy-server "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp "$(WIN64_BUILD_DIR)"/app/scrcpy.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp "$(WIN64_BUILD_DIR)"/app/scrcpy.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp data/scrcpy-console.bat "$(DIST)/$(WIN64_TARGET_DIR)"
|
cp app/data/scrcpy-console.bat "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
cp data/scrcpy-noconsole.vbs "$(DIST)/$(WIN64_TARGET_DIR)"
|
cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
cp data/icon.png "$(DIST)/$(WIN64_TARGET_DIR)"
|
cp app/data/icon.png "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
cp data/open_a_terminal_here.bat "$(DIST)/$(WIN64_TARGET_DIR)"
|
cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
cp prebuilt-deps/data/ffmpeg-win64-5.0/bin/avutil-57.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win64-5.0/bin/avutil-57.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win64-5.0/bin/avcodec-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win64-5.0/bin/avcodec-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win64-5.0/bin/avformat-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win64-5.0/bin/avformat-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win64-5.0/bin/swresample-4.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win64-5.0/bin/swresample-4.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/ffmpeg-win64-5.0/bin/swscale-6.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/ffmpeg-win64-5.0/bin/swscale-6.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/platform-tools-31.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/platform-tools-31.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/data/SDL2-2.0.20/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp app/prebuilt-deps/data/SDL2-2.0.20/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
|
cp app/prebuilt-deps/data/libusb-1.0.25/MinGW64/dll/libusb-1.0.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
|
|
||||||
zip-win32: dist-win32
|
zip-win32: dist-win32
|
||||||
cd "$(DIST)/$(WIN32_TARGET_DIR)"; \
|
cd "$(DIST)/$(WIN32_TARGET_DIR)"; \
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ android {
|
|||||||
applicationId "com.genymobile.scrcpy"
|
applicationId "com.genymobile.scrcpy"
|
||||||
minSdkVersion 21
|
minSdkVersion 21
|
||||||
targetSdkVersion 31
|
targetSdkVersion 31
|
||||||
versionCode 12200
|
versionCode 12300
|
||||||
versionName "1.22"
|
versionName "1.23"
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
SCRCPY_DEBUG=false
|
SCRCPY_DEBUG=false
|
||||||
SCRCPY_VERSION_NAME=1.22
|
SCRCPY_VERSION_NAME=1.23
|
||||||
|
|
||||||
PLATFORM=${ANDROID_PLATFORM:-31}
|
PLATFORM=${ANDROID_PLATFORM:-31}
|
||||||
BUILD_TOOLS=${ANDROID_BUILD_TOOLS:-31.0.0}
|
BUILD_TOOLS=${ANDROID_BUILD_TOOLS:-31.0.0}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public class Options {
|
|||||||
private boolean powerOffScreenOnClose;
|
private boolean powerOffScreenOnClose;
|
||||||
private boolean clipboardAutosync = true;
|
private boolean clipboardAutosync = true;
|
||||||
private boolean downsizeOnError = true;
|
private boolean downsizeOnError = true;
|
||||||
|
private boolean cleanup = true;
|
||||||
|
|
||||||
// Options not used by the scrcpy client, but useful to use scrcpy-server directly
|
// Options not used by the scrcpy client, but useful to use scrcpy-server directly
|
||||||
private boolean sendDeviceMeta = true; // send device name and size
|
private boolean sendDeviceMeta = true; // send device name and size
|
||||||
@@ -155,6 +156,14 @@ public class Options {
|
|||||||
this.downsizeOnError = downsizeOnError;
|
this.downsizeOnError = downsizeOnError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getCleanup() {
|
||||||
|
return cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCleanup(boolean cleanup) {
|
||||||
|
this.cleanup = cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getSendDeviceMeta() {
|
public boolean getSendDeviceMeta() {
|
||||||
return sendDeviceMeta;
|
return sendDeviceMeta;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
// Keep the values in descending order
|
// Keep the values in descending order
|
||||||
private static final int[] MAX_SIZE_FALLBACK = {2560, 1920, 1600, 1280, 1024, 800};
|
private static final int[] MAX_SIZE_FALLBACK = {2560, 1920, 1600, 1280, 1024, 800};
|
||||||
|
|
||||||
private static final int NO_PTS = -1;
|
private static final long PACKET_FLAG_CONFIG = 1L << 63;
|
||||||
|
private static final long PACKET_FLAG_KEY_FRAME = 1L << 62;
|
||||||
|
|
||||||
private final AtomicBoolean rotationChanged = new AtomicBoolean();
|
private final AtomicBoolean rotationChanged = new AtomicBoolean();
|
||||||
private final ByteBuffer headerBuffer = ByteBuffer.allocate(12);
|
private final ByteBuffer headerBuffer = ByteBuffer.allocate(12);
|
||||||
@@ -89,17 +90,19 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
Rect unlockedVideoRect = screenInfo.getUnlockedVideoSize().toRect();
|
Rect unlockedVideoRect = screenInfo.getUnlockedVideoSize().toRect();
|
||||||
int videoRotation = screenInfo.getVideoRotation();
|
int videoRotation = screenInfo.getVideoRotation();
|
||||||
int layerStack = device.getLayerStack();
|
int layerStack = device.getLayerStack();
|
||||||
|
|
||||||
setSize(format, videoRect.width(), videoRect.height());
|
setSize(format, videoRect.width(), videoRect.height());
|
||||||
configure(codec, format);
|
|
||||||
Surface surface = codec.createInputSurface();
|
Surface surface = null;
|
||||||
setDisplaySurface(display, surface, videoRotation, contentRect, unlockedVideoRect, layerStack);
|
|
||||||
codec.start();
|
|
||||||
try {
|
try {
|
||||||
|
configure(codec, format);
|
||||||
|
surface = codec.createInputSurface();
|
||||||
|
setDisplaySurface(display, surface, videoRotation, contentRect, unlockedVideoRect, layerStack);
|
||||||
|
codec.start();
|
||||||
|
|
||||||
alive = encode(codec, fd);
|
alive = encode(codec, fd);
|
||||||
// do not call stop() on exception, it would trigger an IllegalStateException
|
// do not call stop() on exception, it would trigger an IllegalStateException
|
||||||
codec.stop();
|
codec.stop();
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException | IllegalArgumentException e) {
|
||||||
Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage());
|
Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage());
|
||||||
if (!downsizeOnError || firstFrameSent) {
|
if (!downsizeOnError || firstFrameSent) {
|
||||||
// Fail immediately
|
// Fail immediately
|
||||||
@@ -119,7 +122,9 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
} finally {
|
} finally {
|
||||||
destroyDisplay(display);
|
destroyDisplay(display);
|
||||||
codec.release();
|
codec.release();
|
||||||
surface.release();
|
if (surface != null) {
|
||||||
|
surface.release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} while (alive);
|
} while (alive);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -179,12 +184,15 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
|
|
||||||
long pts;
|
long pts;
|
||||||
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
|
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
|
||||||
pts = NO_PTS; // non-media data packet
|
pts = PACKET_FLAG_CONFIG; // non-media data packet
|
||||||
} else {
|
} else {
|
||||||
if (ptsOrigin == 0) {
|
if (ptsOrigin == 0) {
|
||||||
ptsOrigin = bufferInfo.presentationTimeUs;
|
ptsOrigin = bufferInfo.presentationTimeUs;
|
||||||
}
|
}
|
||||||
pts = bufferInfo.presentationTimeUs - ptsOrigin;
|
pts = bufferInfo.presentationTimeUs - ptsOrigin;
|
||||||
|
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0) {
|
||||||
|
pts |= PACKET_FLAG_KEY_FRAME;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
headerBuffer.putLong(pts);
|
headerBuffer.putLong(pts);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import java.util.Locale;
|
|||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
|
|
||||||
|
|
||||||
private Server() {
|
private Server() {
|
||||||
// not instantiable
|
// not instantiable
|
||||||
}
|
}
|
||||||
@@ -51,11 +50,13 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (options.getCleanup()) {
|
||||||
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode,
|
try {
|
||||||
options.getPowerOffScreenOnClose());
|
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode,
|
||||||
} catch (IOException e) {
|
options.getPowerOffScreenOnClose());
|
||||||
Ln.e("Could not configure cleanup", e);
|
} catch (IOException e) {
|
||||||
|
Ln.e("Could not configure cleanup", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,6 +244,10 @@ public final class Server {
|
|||||||
boolean downsizeOnError = Boolean.parseBoolean(value);
|
boolean downsizeOnError = Boolean.parseBoolean(value);
|
||||||
options.setDownsizeOnError(downsizeOnError);
|
options.setDownsizeOnError(downsizeOnError);
|
||||||
break;
|
break;
|
||||||
|
case "cleanup":
|
||||||
|
boolean cleanup = Boolean.parseBoolean(value);
|
||||||
|
options.setCleanup(cleanup);
|
||||||
|
break;
|
||||||
case "send_device_meta":
|
case "send_device_meta":
|
||||||
boolean sendDeviceMeta = Boolean.parseBoolean(value);
|
boolean sendDeviceMeta = Boolean.parseBoolean(value);
|
||||||
options.setSendDeviceMeta(sendDeviceMeta);
|
options.setSendDeviceMeta(sendDeviceMeta);
|
||||||
|
|||||||
Reference in New Issue
Block a user