Compare commits
249 Commits
hid.13
...
input_even
Author | SHA1 | Date | |
---|---|---|---|
b26ad41da7 | |||
bc84a130a4 | |||
3c663e1c0f | |||
05ef36545c | |||
ac32280a43 | |||
93c49ae446 | |||
15d40b6d16 | |||
c520d9f0e7 | |||
826ddf1a6e | |||
6261bb0b5a | |||
2f091beeaa | |||
720c3064df | |||
ad11c5babb | |||
feb250a973 | |||
d049671908 | |||
0685c491cd | |||
892cfe943e | |||
29570ee819 | |||
cfcbc2ac21 | |||
2cb4e04209 | |||
878ffffc36 | |||
f0361fc8b3 | |||
b5d4ec61fc | |||
3ada5c51bc | |||
09c55b0f93 | |||
682a691173 | |||
ddb9396743 | |||
cabcbc2b15 | |||
80fe12a95f | |||
099c546580 | |||
dca2c5f94f | |||
90cf956f57 | |||
36c8778d2d | |||
ae90ef22db | |||
d80bc25eba | |||
daa06abd34 | |||
94702a4309 | |||
65fbec9643 | |||
a208400133 | |||
ab00210b37 | |||
64a04b8d4a | |||
86c91e183d | |||
cb8713eb1f | |||
003e738106 | |||
30c79f2d25 | |||
b25b674c45 | |||
e2b3968c66 | |||
bfcb9d06c3 | |||
dc19ae334d | |||
cbe73b0bc3 | |||
bf97a46b0c | |||
bd56d81f72 | |||
5e918ac0c3 | |||
0c0f62e4ab | |||
c96505200a | |||
82a053015d | |||
1a6caeb18c | |||
19858e6aeb | |||
3b310f8317 | |||
800ba33ff4 | |||
8543d842ea | |||
f609b406c9 | |||
b7e631791c | |||
b52f87a892 | |||
3bf6fd2894 | |||
bfce22414f | |||
e6e6f865a0 | |||
e3d4aa8c5d | |||
f801d8b312 | |||
01ab503c22 | |||
b9b8b6aab8 | |||
3e54773c48 | |||
b90c89766b | |||
904f0ae61e | |||
680d2cc940 | |||
8ed3328055 | |||
d31725f077 | |||
b0eb1a55d6 | |||
3653fb6b15 | |||
92a458e846 | |||
73e0311d14 | |||
007f616302 | |||
4c47598865 | |||
7b29f5fd2d | |||
04e5537f8c | |||
2eb881c5f1 | |||
2c3099e2de | |||
5434ea543c | |||
d5f6697f3a | |||
d6c0054545 | |||
6f487a2892 | |||
dc0ac01e00 | |||
6abff46c9f | |||
5d17bcf1bc | |||
2d5525eac1 | |||
41abe021e2 | |||
2a0730ee9b | |||
901d837165 | |||
aba1fc03c3 | |||
5b3856c3b6 | |||
854de9659a | |||
ea8028332c | |||
0427a981e5 | |||
44721ed982 | |||
68eaee5bb0 | |||
7bdbde7363 | |||
52e5181c84 | |||
ba547e3895 | |||
226f3b2c91 | |||
2d6a96776c | |||
6da6d905c2 | |||
b25404ee4b | |||
4cfc1cd70a | |||
2fc80eae2d | |||
3fdbd994e0 | |||
ce225f019a | |||
b7559744a7 | |||
afb5a5e80f | |||
84334cf7db | |||
0ba2686e1d | |||
55648d4d64 | |||
13fd693b50 | |||
0426ae885c | |||
ea454e9cee | |||
cb65531533 | |||
9619ade706 | |||
f2781a8b6d | |||
443cb14d6e | |||
b30c3a429f | |||
632bd5697b | |||
de50846905 | |||
ee93d2aac1 | |||
c29a0bf675 | |||
411bb0d18e | |||
cc0902b13c | |||
48b572c272 | |||
94feae71f2 | |||
67170437f1 | |||
57fb08e443 | |||
02ae0db6cd | |||
9cb14b5166 | |||
9cb8766220 | |||
fd4ec784e0 | |||
52cebe1597 | |||
6a27062f48 | |||
739ff9dce0 | |||
65b023ac6d | |||
a045e28df8 | |||
52138fd921 | |||
1bb0df5da1 | |||
562ec6e9b3 | |||
1be5daf999 | |||
57387fa707 | |||
4e811a4a68 | |||
99a4a48477 | |||
1d97d77032 | |||
45b0f8123a | |||
c1a34881d7 | |||
057c7a4df4 | |||
979ce64dc0 | |||
9a0bd545d5 | |||
c4d008b96a | |||
0d45c29d13 | |||
37c840a4c8 | |||
f488cbd7e7 | |||
40340509d9 | |||
e0896142db | |||
0426708544 | |||
5b9c88693e | |||
a54dc8212f | |||
882e4cff5f | |||
9fa4d1cd4a | |||
0bfa75a48b | |||
ed19901db1 | |||
e69356c550 | |||
03de9224fc | |||
aa011832c1 | |||
7e93abcf6d | |||
e80e6631e4 | |||
fcc04f967b | |||
d4c262301f | |||
be55e250ca | |||
c548f017bf | |||
7a733328bc | |||
38332f683c | |||
3f51a2ab43 | |||
74ab0a4df8 | |||
f59e9e3cb0 | |||
9ec3406568 | |||
6dba1922c1 | |||
570a003c39 | |||
b62df7ee91 | |||
30d40f4e78 | |||
f489f7fcad | |||
d72c7076f7 | |||
fc64445555 | |||
f65c3fde69 | |||
48fcfa96ab | |||
676fa73d2c | |||
790f04e91f | |||
58ea238fb2 | |||
13c4aa1a3b | |||
caf594c90e | |||
688477ff65 | |||
ac539e1312 | |||
a57c7d3a2b | |||
d2d18466d4 | |||
dae091e3ab | |||
4c4381de4c | |||
db484d82db | |||
0c9666b733 | |||
8bf28e9f53 | |||
06131ef634 | |||
34eb10ea0b | |||
27fa23846d | |||
e4d5c1ce36 | |||
ac23bec144 | |||
e5ea13770b | |||
3eac212af1 | |||
3adff37c2d | |||
eb6afe7669 | |||
5222f213f4 | |||
e4163321f0 | |||
c96874b257 | |||
511356710d | |||
d55015e4cf | |||
0681480809 | |||
96b18dabaa | |||
eaf4afaad9 | |||
207082977a | |||
f7d1efdf1d | |||
bcf5a9750f | |||
056d36ce4a | |||
bea3197c3f | |||
d6568f64af | |||
580f5d8bb9 | |||
1e340caf76 | |||
3d5b31e0cb | |||
6004f0b6b0 | |||
12ed2f2402 | |||
0e4564da03 | |||
156d958e77 | |||
7229e3cce0 | |||
a7e41b0f85 | |||
46d3e35c30 | |||
07d75eb336 | |||
8df42cec82 | |||
31131039bb | |||
fa100b814b |
16
BUILD.md
16
BUILD.md
@ -14,7 +14,8 @@ First, you need to install the required packages:
|
||||
# for Debian/Ubuntu
|
||||
sudo apt install ffmpeg libsdl2-2.0-0 adb wget \
|
||||
gcc git pkg-config meson ninja-build libsdl2-dev \
|
||||
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev
|
||||
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
|
||||
libusb-1.0-0 libusb-1.0-0-dev
|
||||
```
|
||||
|
||||
Then clone the repo and execute the installation script
|
||||
@ -88,11 +89,12 @@ Install the required packages from your package manager.
|
||||
|
||||
```bash
|
||||
# runtime dependencies
|
||||
sudo apt install ffmpeg libsdl2-2.0-0 adb
|
||||
sudo apt install ffmpeg libsdl2-2.0-0 adb libusb-1.0-0
|
||||
|
||||
# client build dependencies
|
||||
sudo apt install gcc git pkg-config meson ninja-build libsdl2-dev \
|
||||
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev
|
||||
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
|
||||
libusb-1.0-0-dev
|
||||
|
||||
# server build dependencies
|
||||
sudo apt install openjdk-11-jdk
|
||||
@ -114,7 +116,7 @@ pip3 install meson
|
||||
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
|
||||
|
||||
# client build dependencies
|
||||
sudo dnf install SDL2-devel ffms2-devel meson gcc make
|
||||
sudo dnf install SDL2-devel ffms2-devel libusb-devel meson gcc make
|
||||
|
||||
# server build dependencies
|
||||
sudo dnf install java-devel
|
||||
@ -268,10 +270,10 @@ install` must be run as root)._
|
||||
|
||||
#### Option 2: Use prebuilt server
|
||||
|
||||
- [`scrcpy-server-v1.19`][direct-scrcpy-server]
|
||||
_(SHA-256: 876f9322182e6aac6a58db1334f4225855ef3a17eaebc80aab6601d9d1ecb867)_
|
||||
- [`scrcpy-server-v1.21`][direct-scrcpy-server]
|
||||
_(SHA-256: dbcccab523ee26796e55ea33652649e4b7af498edae9aa75e4d4d7869c0ab848)_
|
||||
|
||||
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.19/scrcpy-server-v1.19
|
||||
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-server-v1.21
|
||||
|
||||
Download the prebuilt server somewhere, and specify its path during the Meson
|
||||
configuration:
|
||||
|
31
FAQ.md
31
FAQ.md
@ -118,13 +118,17 @@ In developer options, enable:
|
||||
|
||||
### Special characters do not work
|
||||
|
||||
Injecting text input is [limited to ASCII characters][text-input]. A trick
|
||||
allows to also inject some [accented characters][accented-characters], but
|
||||
that's all. See [#37].
|
||||
The default text injection method is [limited to ASCII characters][text-input].
|
||||
A trick allows to also inject some [accented characters][accented-characters],
|
||||
but that's all. See [#37].
|
||||
|
||||
Since scrcpy v1.20 on Linux, it is possible to simulate a [physical
|
||||
keyboard][hid] (HID).
|
||||
|
||||
[text-input]: https://github.com/Genymobile/scrcpy/issues?q=is%3Aopen+is%3Aissue+label%3Aunicode
|
||||
[accented-characters]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-accented-characters
|
||||
[#37]: https://github.com/Genymobile/scrcpy/issues/37
|
||||
[hid]: README.md#physical-keyboard-simulation-hid
|
||||
|
||||
|
||||
## Client issues
|
||||
@ -218,6 +222,27 @@ scrcpy -m 800
|
||||
You could also try another [encoder](README.md#encoder).
|
||||
|
||||
|
||||
If you encounter this exception on Android 12, then just upgrade to scrcpy >=
|
||||
1.18 (see [#2129]):
|
||||
|
||||
```
|
||||
> ERROR: Exception on thread Thread[main,5,main]
|
||||
java.lang.AssertionError: java.lang.reflect.InvocationTargetException
|
||||
at com.genymobile.scrcpy.wrappers.SurfaceControl.setDisplaySurface(SurfaceControl.java:75)
|
||||
...
|
||||
Caused by: java.lang.reflect.InvocationTargetException
|
||||
at java.lang.reflect.Method.invoke(Native Method)
|
||||
at com.genymobile.scrcpy.wrappers.SurfaceControl.setDisplaySurface(SurfaceControl.java:73)
|
||||
... 7 more
|
||||
Caused by: java.lang.IllegalArgumentException: displayToken must not be null
|
||||
at android.view.SurfaceControl$Transaction.setDisplaySurface(SurfaceControl.java:3067)
|
||||
at android.view.SurfaceControl.setDisplaySurface(SurfaceControl.java:2147)
|
||||
... 9 more
|
||||
```
|
||||
|
||||
[#2129]: https://github.com/Genymobile/scrcpy/issues/2129
|
||||
|
||||
|
||||
## Command line on Windows
|
||||
|
||||
Some Windows users are not familiar with the command line. Here is how to open a
|
||||
|
132
README.jp.md
132
README.jp.md
@ -1,6 +1,6 @@
|
||||
_Only the original [README](README.md) is guaranteed to be up-to-date._
|
||||
|
||||
# scrcpy (v1.17)
|
||||
# scrcpy (v1.19)
|
||||
|
||||
このアプリケーションはUSB(もしくは[TCP/IP経由][article-tcpip])で接続されたAndroidデバイスの表示と制御を提供します。このアプリケーションは _root_ でのアクセスを必要としません。このアプリケーションは _GNU/Linux_ 、 _Windows_ そして _macOS_ 上で動作します。
|
||||
|
||||
@ -103,18 +103,21 @@ scoop install adb # まだ入手していない場合
|
||||
brew install scrcpy
|
||||
```
|
||||
|
||||
`PATH`から`adb`へのアクセスが必要です。もしまだ持っていない場合:
|
||||
`PATH`からアクセス可能な`adb`が必要です。もし持っていない場合はインストールしてください。
|
||||
|
||||
```bash
|
||||
# Homebrew >= 2.6.0
|
||||
brew install --cask android-platform-tools
|
||||
|
||||
# Homebrew < 2.6.0
|
||||
brew cask install android-platform-tools
|
||||
brew install android-platform-tools
|
||||
```
|
||||
|
||||
また、[アプリケーションをビルド][BUILD]することも可能です。
|
||||
`adb`は[MacPorts]からでもインストールできます。
|
||||
|
||||
```bash
|
||||
sudo port install scrcpy
|
||||
```
|
||||
|
||||
[MacPorts]: https://www.macports.org/
|
||||
|
||||
また、[アプリケーションをビルド][BUILD]することも可能です。
|
||||
|
||||
## 実行
|
||||
|
||||
@ -184,10 +187,11 @@ scrcpy --crop 1224:1440:0:0 # オフセット位置(0,0)で1224x1440
|
||||
ミラーリングの向きをロックするには:
|
||||
|
||||
```bash
|
||||
scrcpy --lock-video-orientation 0 # 自然な向き
|
||||
scrcpy --lock-video-orientation 1 # 90°反時計回り
|
||||
scrcpy --lock-video-orientation 2 # 180°
|
||||
scrcpy --lock-video-orientation 3 # 90°時計回り
|
||||
scrcpy --lock-video-orientation # 現在の向き
|
||||
scrcpy --lock-video-orientation=0 # 自然な向き
|
||||
scrcpy --lock-video-orientation=1 # 90°反時計回り
|
||||
scrcpy --lock-video-orientation=2 # 180°
|
||||
scrcpy --lock-video-orientation=3 # 90°時計回り
|
||||
```
|
||||
|
||||
この設定は録画の向きに影響します。
|
||||
@ -210,7 +214,9 @@ scrcpy --encoder OMX.qcom.video.encoder.avc
|
||||
scrcpy --encoder _
|
||||
```
|
||||
|
||||
### 録画
|
||||
### キャプチャ
|
||||
|
||||
#### 録画
|
||||
|
||||
ミラーリング中に画面の録画をすることが可能です:
|
||||
|
||||
@ -233,6 +239,77 @@ scrcpy -Nr file.mkv
|
||||
|
||||
[パケット遅延のバリエーション]: https://en.wikipedia.org/wiki/Packet_delay_variation
|
||||
|
||||
#### v4l2loopback
|
||||
|
||||
Linuxでは、ビデオストリームをv4l2ループバックデバイスに送信することができます。
|
||||
v4l2loopbackのデバイスにビデオストリームを送信することで、Androidデバイスをウェブカメラのようにv4l2対応ツールで開くこともできます。
|
||||
|
||||
`v4l2loopback` モジュールのインストールが必要です。
|
||||
|
||||
```bash
|
||||
sudo apt install v4l2loopback-dkms
|
||||
```
|
||||
|
||||
v4l2デバイスを作成する。
|
||||
|
||||
```bash
|
||||
sudo modprobe v4l2loopback
|
||||
```
|
||||
|
||||
これにより、新しいビデオデバイスが `/dev/videoN` に作成されます。(`N` は整数)
|
||||
(複数のデバイスや特定のIDのデバイスを作成するために、より多くの[オプション](https://github.com/umlaeute/v4l2loopback#options)が利用可能です。
|
||||
多くの[オプション]()が利用可能で複数のデバイスや特定のIDのデバイスを作成できます。
|
||||
|
||||
|
||||
有効なデバイスを一覧表示する:
|
||||
|
||||
```bash
|
||||
# v4l-utilsパッケージが必要
|
||||
v4l2-ctl --list-devices
|
||||
|
||||
# シンプルですが十分これで確認できます
|
||||
ls /dev/video*
|
||||
```
|
||||
|
||||
v4l2シンクを使用してscrcpyを起動する。
|
||||
|
||||
```bash
|
||||
scrcpy --v4l2-sink=/dev/videoN
|
||||
scrcpy --v4l2-sink=/dev/videoN --no-display # ミラーリングウィンドウを無効化する
|
||||
scrcpy --v4l2-sink=/dev/videoN -N # 短縮版
|
||||
```
|
||||
|
||||
(`N` をデバイス ID に置き換えて、`ls /dev/video*` で確認してください)
|
||||
有効にすると、v4l2対応のツールでビデオストリームを開けます。
|
||||
|
||||
```bash
|
||||
ffplay -i /dev/videoN
|
||||
vlc v4l2:///dev/videoN # VLCではバッファリングの遅延が発生する場合があります
|
||||
```
|
||||
|
||||
例えばですが [OBS]の中にこの映像を取り込めことができます。
|
||||
|
||||
[OBS]: https://obsproject.com/
|
||||
|
||||
|
||||
#### Buffering
|
||||
|
||||
バッファリングを追加することも可能です。これによりレイテンシーは増加しますが、ジッターは減少します。(参照
|
||||
[#2464])
|
||||
|
||||
[#2464]: https://github.com/Genymobile/scrcpy/issues/2464
|
||||
|
||||
このオプションでディスプレイバッファリングを設定できます。
|
||||
|
||||
```bash
|
||||
scrcpy --display-buffer=50 # ディスプレイに50msのバッファリングを追加する
|
||||
```
|
||||
|
||||
V4L2の場合はこちらのオプションで設定できます。
|
||||
|
||||
```bash
|
||||
scrcpy --v4l2-buffer=500 # add 500 ms buffering for v4l2 sink
|
||||
```
|
||||
|
||||
### 接続
|
||||
|
||||
@ -457,16 +534,6 @@ scrcpy -Sw
|
||||
```
|
||||
|
||||
|
||||
#### 期限切れフレームをレンダリングする
|
||||
|
||||
初期状態では、待ち時間を最小限にするために、_scrcpy_ は最後にデコードされたフレームをレンダリングし、前のフレームを削除します。
|
||||
|
||||
全フレームのレンダリングを強制するには(待ち時間が長くなる可能性があります):
|
||||
|
||||
```bash
|
||||
scrcpy --render-expired-frames
|
||||
```
|
||||
|
||||
#### タッチを表示
|
||||
|
||||
プレゼンテーションの場合(物理デバイス上で)物理的なタッチを表示すると便利な場合があります。
|
||||
@ -586,14 +653,14 @@ APKをインストールするには、(`.apk`で終わる)APKファイルを _s
|
||||
|
||||
#### デバイスにファイルを送る
|
||||
|
||||
デバイスの`/sdcard/`ディレクトリにファイルを送るには、(APKではない)ファイルを _scrcpy_ の画面にドラッグ&ドロップします。
|
||||
デバイスの`/sdcard/Download`ディレクトリにファイルを送るには、(APKではない)ファイルを _scrcpy_ の画面にドラッグ&ドロップします。
|
||||
|
||||
見た目のフィードバックはありません。コンソールにログが出力されます。
|
||||
|
||||
転送先ディレクトリを起動時に変更することができます:
|
||||
|
||||
```bash
|
||||
scrcpy --push-target /sdcard/foo/bar/
|
||||
scrcpy --push-target=/sdcard/Movies/
|
||||
```
|
||||
|
||||
|
||||
@ -634,7 +701,7 @@ _<kbd>[Super]</kbd>は通常<kbd>Windows</kbd>もしくは<kbd>Cmd</kbd>キー
|
||||
| ウィンドウサイズを変更して黒い境界線を削除 | <kbd>MOD</kbd>+<kbd>w</kbd> \| _ダブルクリック¹_
|
||||
| `HOME`をクリック | <kbd>MOD</kbd>+<kbd>h</kbd> \| _真ん中クリック_
|
||||
| `BACK`をクリック | <kbd>MOD</kbd>+<kbd>b</kbd> \| _右クリック²_
|
||||
| `APP_SWITCH`をクリック | <kbd>MOD</kbd>+<kbd>s</kbd>
|
||||
| `APP_SWITCH`をクリック | <kbd>MOD</kbd>+<kbd>s</kbd> \| _4クリック³_
|
||||
| `MENU` (画面のアンロック)をクリック | <kbd>MOD</kbd>+<kbd>m</kbd>
|
||||
| `VOLUME_UP`をクリック | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上)_
|
||||
| `VOLUME_DOWN`をクリック | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下)_
|
||||
@ -643,7 +710,8 @@ _<kbd>[Super]</kbd>は通常<kbd>Windows</kbd>もしくは<kbd>Cmd</kbd>キー
|
||||
| デバイス画面をオフにする(ミラーリングしたまま) | <kbd>MOD</kbd>+<kbd>o</kbd>
|
||||
| デバイス画面をオンにする | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>
|
||||
| デバイス画面を回転する | <kbd>MOD</kbd>+<kbd>r</kbd>
|
||||
| 通知パネルを展開する | <kbd>MOD</kbd>+<kbd>n</kbd>
|
||||
| 通知パネルを展開する | <kbd>MOD</kbd>+<kbd>n</kbd> \| _5ボタンクリック³_
|
||||
| 設定パネルを展開する | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _5ダブルクリック³_
|
||||
| 通知パネルを折りたたむ | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
||||
| クリップボードへのコピー³ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
||||
| クリップボードへのカット³ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
||||
@ -654,11 +722,17 @@ _<kbd>[Super]</kbd>は通常<kbd>Windows</kbd>もしくは<kbd>Cmd</kbd>キー
|
||||
|
||||
_¹黒い境界線を削除するため、境界線上でダブルクリック_
|
||||
_²もしスクリーンがオフの場合、右クリックでスクリーンをオンする。それ以外の場合はBackを押します._
|
||||
_³Android 7以上のみ._
|
||||
_³4と5はマウスのボタンです、もしあなたのマウスにボタンがあれば使えます._
|
||||
_⁴Android 7以上のみ._
|
||||
|
||||
キーを繰り返すショートカットはキーを離して2回目を押したら実行されます。例えば「設定パネルを展開する」を実行する場合は以下のように操作する。
|
||||
|
||||
1. <kbd>MOD</kbd> キーを押し、押したままにする.
|
||||
2. その後に <kbd>n</kbd>キーを2回押す.
|
||||
3. 最後に <kbd>MOD</kbd>キーを離す.
|
||||
|
||||
全ての<kbd>Ctrl</kbd>+_キー_ ショートカットはデバイスに転送されます、そのためアクティブなアプリケーションによって処理されます。
|
||||
|
||||
|
||||
## カスタムパス
|
||||
|
||||
特定の _adb_ バイナリを使用する場合、そのパスを環境変数`ADB`で構成します:
|
||||
|
210
README.md
210
README.md
@ -1,24 +1,37 @@
|
||||
# scrcpy (v1.19)
|
||||
# scrcpy (v1.21)
|
||||
|
||||
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
||||
|
||||
[Read in another language](#translations)
|
||||
|
||||
This application provides display and control of Android devices connected on
|
||||
USB (or [over TCP/IP][article-tcpip]). It does not require any _root_ access.
|
||||
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.
|
||||
It works on _GNU/Linux_, _Windows_ and _macOS_.
|
||||
|
||||

|
||||
|
||||
It focuses on:
|
||||
|
||||
- **lightness** (native, displays only the device screen)
|
||||
- **performance** (30~60fps)
|
||||
- **quality** (1920×1080 or above)
|
||||
- **low latency** ([35~70ms][lowlatency])
|
||||
- **low startup time** (~1 second to display the first image)
|
||||
- **non-intrusiveness** (nothing is left installed on the device)
|
||||
- **lightness**: native, displays only the device screen
|
||||
- **performance**: 30~120fps, depending on the device
|
||||
- **quality**: 1920×1080 or above
|
||||
- **low latency**: [35~70ms][lowlatency]
|
||||
- **low startup time**: ~1 second to display the first image
|
||||
- **non-intrusiveness**: nothing is left installed on the device
|
||||
- **user benefits**: no account, no ads, no internet required
|
||||
- **freedom**: free and open source software
|
||||
|
||||
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
|
||||
|
||||
Its features include:
|
||||
- [recording](#recording)
|
||||
- mirroring with [device screen off](#turn-screen-off)
|
||||
- [copy-paste](#copy-paste) in both directions
|
||||
- [configurable quality](#capture-configuration)
|
||||
- device screen [as a webcam (V4L2)](#v4l2loopback) (Linux-only)
|
||||
- [physical keyboard simulation (HID)](#physical-keyboard-simulation-hid)
|
||||
(Linux-only)
|
||||
- and more…
|
||||
|
||||
## Requirements
|
||||
|
||||
@ -52,7 +65,7 @@ Build from sources: [BUILD] ([simplified process][BUILD_simple])
|
||||
|
||||
### Linux
|
||||
|
||||
On Debian (_testing_ and _sid_ for now) and Ubuntu (20.04):
|
||||
On Debian and Ubuntu:
|
||||
|
||||
```
|
||||
apt install scrcpy
|
||||
@ -88,10 +101,10 @@ process][BUILD_simple]).
|
||||
For Windows, for simplicity, a prebuilt archive with all the dependencies
|
||||
(including `adb`) is available:
|
||||
|
||||
- [`scrcpy-win64-v1.19.zip`][direct-win64]
|
||||
_(SHA-256: 383d6483f25ac0092d4bb9fef6c967351ecd50fc248e0c82932db97d6d32f11b)_
|
||||
- [`scrcpy-win64-v1.21.zip`][direct-win64]
|
||||
_(SHA-256: fdab0c1421353b592a9bbcebd6e252675eadccca65cca8105686feaa9c1ded53)_
|
||||
|
||||
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.19/scrcpy-win64-v1.19.zip
|
||||
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-win64-v1.21.zip
|
||||
|
||||
It is also available in [Chocolatey]:
|
||||
|
||||
@ -343,10 +356,39 @@ scrcpy --v4l2-buffer=500 # add 500 ms buffering for v4l2 sink
|
||||
|
||||
### Connection
|
||||
|
||||
#### Wireless
|
||||
#### TCP/IP (wireless)
|
||||
|
||||
_Scrcpy_ uses `adb` to communicate with the device, and `adb` can [connect] to a
|
||||
device over TCP/IP:
|
||||
device over TCP/IP. The device must be connected on the same network as the
|
||||
computer.
|
||||
|
||||
##### Automatic
|
||||
|
||||
An option `--tcpip` allows to configure the connection automatically. There are
|
||||
two variants.
|
||||
|
||||
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:
|
||||
|
||||
```bash
|
||||
scrcpy --tcpip=192.168.1.1 # default port is 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
|
||||
address), connect the device over USB, then run:
|
||||
|
||||
```bash
|
||||
scrcpy --tcpip # without arguments
|
||||
```
|
||||
|
||||
It will automatically find the device IP address, enable TCP/IP mode, then
|
||||
connect to the device before starting.
|
||||
|
||||
##### Manual
|
||||
|
||||
Alternatively, it is possible to enable the TCP/IP connection manually using
|
||||
`adb`:
|
||||
|
||||
1. Connect the device to the same Wi-Fi as your computer.
|
||||
2. Get your device IP address, in Settings → About phone → Status, or by
|
||||
@ -399,21 +441,66 @@ autoadb scrcpy -s '{}'
|
||||
|
||||
[AutoAdb]: https://github.com/rom1v/autoadb
|
||||
|
||||
#### SSH tunnel
|
||||
#### Tunnels
|
||||
|
||||
To connect to a remote device, it is possible to connect a local `adb` client to
|
||||
a remote `adb` server (provided they use the same version of the _adb_
|
||||
protocol):
|
||||
protocol).
|
||||
|
||||
##### Remote ADB server
|
||||
|
||||
To connect to a remote ADB server, make the server listen on all interfaces:
|
||||
|
||||
```bash
|
||||
adb kill-server # kill the local adb server on 5037
|
||||
ssh -CN -L5037:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
||||
adb kill-server
|
||||
adb -a nodaemon server start
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal:
|
||||
**Warning: all communications between clients and ADB server are unencrypted.**
|
||||
|
||||
Suppose that this server is accessible at 192.168.1.2. Then, from another
|
||||
terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
|
||||
scrcpy --tunnel-host=192.168.1.2
|
||||
```
|
||||
|
||||
By default, scrcpy uses the local port used for `adb forward` tunnel
|
||||
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
|
||||
redirections are involved):
|
||||
|
||||
```
|
||||
scrcpy --tunnel-port=1234
|
||||
```
|
||||
|
||||
|
||||
##### SSH tunnel
|
||||
|
||||
To communicate with a remote ADB server securely, it is preferable to use a SSH
|
||||
tunnel.
|
||||
|
||||
First, make sure the ADB server is running on the remote computer:
|
||||
|
||||
```bash
|
||||
adb start-server
|
||||
```
|
||||
|
||||
Then, establish a SSH tunnel:
|
||||
|
||||
```bash
|
||||
# local 5038 --> remote 5037
|
||||
# local 27183 <-- remote 27183
|
||||
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||
scrcpy
|
||||
```
|
||||
|
||||
@ -421,14 +508,16 @@ To avoid enabling remote port forwarding, you could force a forward connection
|
||||
instead (notice the `-L` instead of `-R`):
|
||||
|
||||
```bash
|
||||
adb kill-server # kill the local adb server on 5037
|
||||
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
||||
# local 5038 --> remote 5037
|
||||
# local 27183 --> remote 27183
|
||||
ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal:
|
||||
From another terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||
scrcpy --force-adb-forward
|
||||
```
|
||||
|
||||
@ -582,6 +671,14 @@ scrcpy --turn-screen-off --stay-awake
|
||||
scrcpy -Sw
|
||||
```
|
||||
|
||||
#### Power off on close
|
||||
|
||||
To turn the device screen off when closing scrcpy:
|
||||
|
||||
```bash
|
||||
scrcpy --power-off-on-close
|
||||
```
|
||||
|
||||
|
||||
#### Show touches
|
||||
|
||||
@ -662,6 +759,9 @@ 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
|
||||
way as <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).
|
||||
|
||||
To disable automatic clipboard synchronization, use
|
||||
`--no-clipboard-autosync`.
|
||||
|
||||
#### Pinch-to-zoom
|
||||
|
||||
To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
|
||||
@ -673,6 +773,48 @@ content (if supported by the app) relative to the center of the screen.
|
||||
Concretely, scrcpy generates additional touch events from a "virtual finger" at
|
||||
a location inverted through the center of the screen.
|
||||
|
||||
#### Physical keyboard simulation (HID)
|
||||
|
||||
By default, scrcpy uses Android key or text injection: it works everywhere, but
|
||||
is limited to ASCII.
|
||||
|
||||
On Linux, scrcpy can simulate a physical USB keyboard on Android to provide a
|
||||
better input experience (using [USB HID over AOAv2][hid-aoav2]): the virtual
|
||||
keyboard is disabled and it works for all characters and IME.
|
||||
|
||||
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
|
||||
|
||||
However, it only works if the device is connected by USB, and is currently only
|
||||
supported on Linux.
|
||||
|
||||
To enable this mode:
|
||||
|
||||
```bash
|
||||
scrcpy --hid-keyboard
|
||||
scrcpy -K # short version
|
||||
```
|
||||
|
||||
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
|
||||
console). This allows to use the same command line options when connected over
|
||||
USB and TCP/IP.
|
||||
|
||||
In this mode, raw key events (scancodes) are sent to the device, independently
|
||||
of the host key mapping. Therefore, if your keyboard layout does not match, it
|
||||
must be configured on the Android device, in Settings → System → Languages and
|
||||
input → [Physical keyboard].
|
||||
|
||||
This settings page can be started directly:
|
||||
|
||||
```bash
|
||||
adb shell am start -a android.settings.HARD_KEYBOARD_SETTINGS
|
||||
```
|
||||
|
||||
However, the option is only available when the HID keyboard is enabled (or when
|
||||
a physical keyboard is connected).
|
||||
|
||||
[Physical keyboard]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
|
||||
|
||||
|
||||
#### Text injection preference
|
||||
|
||||
@ -692,6 +834,15 @@ scrcpy --prefer-text
|
||||
|
||||
(but this will break keyboard behavior in games)
|
||||
|
||||
On the contrary, you could force to always inject raw key events:
|
||||
|
||||
```bash
|
||||
scrcpy --raw-key-events
|
||||
```
|
||||
|
||||
These options have no effect on HID keyboard (all key events are sent as
|
||||
scancodes in this mode).
|
||||
|
||||
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
||||
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
||||
|
||||
@ -707,6 +858,9 @@ To avoid forwarding repeated key events:
|
||||
scrcpy --no-key-repeat
|
||||
```
|
||||
|
||||
This option has no effect on HID keyboard (key repeat is handled by Android
|
||||
directly in this mode).
|
||||
|
||||
|
||||
#### Right-click and middle-click
|
||||
|
||||
@ -791,7 +945,7 @@ _<kbd>[Super]</kbd> is typically the <kbd>Windows</kbd> or <kbd>Cmd</kbd> key._
|
||||
| Turn device screen on | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>
|
||||
| Rotate device screen | <kbd>MOD</kbd>+<kbd>r</kbd>
|
||||
| Expand notification panel | <kbd>MOD</kbd>+<kbd>n</kbd> \| _5th-click³_
|
||||
| Expand settings panel | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Double-5th-click³_
|
||||
| Expand settings panel | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _Double-5th-click³_
|
||||
| Collapse panels | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
||||
| Copy to clipboard⁴ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
||||
| Cut to clipboard⁴ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
||||
@ -799,6 +953,8 @@ _<kbd>[Super]</kbd> is typically the <kbd>Windows</kbd> or <kbd>Cmd</kbd> key._
|
||||
| Inject computer clipboard text | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
|
||||
| Enable/disable FPS counter (on stdout) | <kbd>MOD</kbd>+<kbd>i</kbd>
|
||||
| Pinch-to-zoom | <kbd>Ctrl</kbd>+_click-and-move_
|
||||
| Drag & drop APK file | Install APK from computer
|
||||
| Drag & drop non-APK file | [Push file to device](#push-file-to-device)
|
||||
|
||||
_¹Double-click on black borders to remove them._
|
||||
_²Right-click turns the screen on if it was off, presses BACK otherwise._
|
||||
@ -828,7 +984,7 @@ ADB=/path/to/adb scrcpy
|
||||
To override the path of the `scrcpy-server` file, configure its path in
|
||||
`SCRCPY_SERVER_PATH`.
|
||||
|
||||
[useful]: https://github.com/Genymobile/scrcpy/issues/278#issuecomment-429330345
|
||||
To override the icon, configure its path in `SCRCPY_ICON_PATH`.
|
||||
|
||||
|
||||
## Why _scrcpy_?
|
||||
@ -889,11 +1045,11 @@ This README is available in other languages:
|
||||
|
||||
- [Indonesian (Indonesia, `id`) - v1.16](README.id.md)
|
||||
- [Italiano (Italiano, `it`) - v1.19](README.it.md)
|
||||
- [日本語 (Japanese, `jp`) - v1.17](README.jp.md)
|
||||
- [日本語 (Japanese, `jp`) - v1.19](README.jp.md)
|
||||
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
|
||||
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
|
||||
- [Español (Spanish, `sp`) - v1.17](README.sp.md)
|
||||
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.17](README.zh-Hans.md)
|
||||
- [简体中文 (Simplified Chinese, `zh-Hans`) - v1.20](README.zh-Hans.md)
|
||||
- [繁體中文 (Traditional Chinese, `zh-Hant`) - v1.15](README.zh-Hant.md)
|
||||
- [Turkish (Turkish, `tr`) - v1.18](README.tr.md)
|
||||
|
||||
|
@ -2,27 +2,41 @@ _Only the original [README](README.md) is guaranteed to be up-to-date._
|
||||
|
||||
只有原版的[README](README.md)会保持最新。
|
||||
|
||||
本文根据[ed130e05]进行翻译。
|
||||
Current version is based on [65b023a]
|
||||
|
||||
[ed130e05]: https://github.com/Genymobile/scrcpy/blob/ed130e05d55615d6014d93f15cfcb92ad62b01d8/README.md
|
||||
本文根据[65b023a]进行翻译。
|
||||
|
||||
# scrcpy (v1.17)
|
||||
[65b023a]: https://github.com/Genymobile/scrcpy/blob/65b023ac6d586593193fd5290f65e25603b68e02/README.md
|
||||
|
||||
# scrcpy (v1.20)
|
||||
|
||||
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
||||
|
||||
本应用程序可以显示并控制通过 USB (或 [TCP/IP][article-tcpip]) 连接的安卓设备,且不需要任何 _root_ 权限。本程序支持 _GNU/Linux_, _Windows_ 和 _macOS_。
|
||||
|
||||

|
||||
|
||||
它专注于:
|
||||
本应用专注于:
|
||||
|
||||
- **轻量** (原生,仅显示设备屏幕)
|
||||
- **性能** (30~60fps)
|
||||
- **质量** (分辨率可达 1920×1080 或更高)
|
||||
- **低延迟** ([35~70ms][lowlatency])
|
||||
- **快速启动** (最快 1 秒内即可显示第一帧)
|
||||
- **无侵入性** (不会在设备上遗留任何程序)
|
||||
- **轻量**: 原生,仅显示设备屏幕
|
||||
- **性能**: 30~120fps,取决于设备
|
||||
- **质量**: 分辨率可达 1920×1080 或更高
|
||||
- **低延迟**: [35~70ms][lowlatency]
|
||||
- **快速启动**: 最快 1 秒内即可显示第一帧
|
||||
- **无侵入性**: 不会在设备上遗留任何程序
|
||||
- **用户利益**: 无需帐号,无广告,无需联网
|
||||
- **自由**: 自由和开源软件
|
||||
|
||||
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
|
||||
|
||||
功能:
|
||||
- [屏幕录制](#屏幕录制)
|
||||
- 镜像时[关闭设备屏幕](#关闭设备屏幕)
|
||||
- 双向[复制粘贴](#复制粘贴)
|
||||
- [可配置显示质量](#采集设置)
|
||||
- 以设备屏幕[作为摄像头(V4L2)](#v4l2loopback) (仅限 Linux)
|
||||
- [模拟物理键盘 (HID)](#物理键盘模拟-hid) (仅限 Linux)
|
||||
- 更多 ……
|
||||
|
||||
## 系统要求
|
||||
|
||||
@ -41,6 +55,17 @@ _Only the original [README](README.md) is guaranteed to be up-to-date._
|
||||
|
||||
<a href="https://repology.org/project/scrcpy/versions"><img src="https://repology.org/badge/vertical-allrepos/scrcpy.svg" alt="Packaging status" align="right"></a>
|
||||
|
||||
### 概要
|
||||
|
||||
- Linux: `apt install scrcpy`
|
||||
- Windows: [下载][direct-win64]
|
||||
- macOS: `brew install scrcpy`
|
||||
|
||||
从源代码编译: [构建][BUILD] ([简化过程][BUILD_simple])
|
||||
|
||||
[BUILD]: BUILD.md
|
||||
[BUILD_simple]: BUILD.md#simple
|
||||
|
||||
### Linux
|
||||
|
||||
在 Debian (目前仅支持 _testing_ 和 _sid_ 分支) 和Ubuntu (20.04) 上:
|
||||
@ -70,13 +95,12 @@ apt install scrcpy
|
||||
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
|
||||
[ebuild-link]: https://github.com/maggu2810/maggu2810-overlay/tree/master/app-mobilephone/scrcpy
|
||||
|
||||
您也可以[自行构建][BUILD] (不必担心,这并不困难)。
|
||||
|
||||
您也可以[自行构建][BUILD] ([简化过程][BUILD_simple])。
|
||||
|
||||
|
||||
### Windows
|
||||
|
||||
在 Windows 上,简便起见,我们提供包含了所有依赖 (包括 `adb`) 的预编译包。
|
||||
在 Windows 上,为简便起见,我们提供包含了所有依赖 (包括 `adb`) 的预编译包。
|
||||
|
||||
- [README](README.md#windows)
|
||||
|
||||
@ -114,13 +138,17 @@ brew install scrcpy
|
||||
你还需要在 `PATH` 内有 `adb`。如果还没有:
|
||||
|
||||
```bash
|
||||
# Homebrew >= 2.6.0
|
||||
brew install --cask android-platform-tools
|
||||
|
||||
# Homebrew < 2.6.0
|
||||
brew cask install android-platform-tools
|
||||
brew install android-platform-tools
|
||||
```
|
||||
|
||||
或者通过 [MacPorts],该方法同时设置好 adb:
|
||||
|
||||
```bash
|
||||
sudo port install scrcpy
|
||||
```
|
||||
|
||||
[MacPorts]: https://www.macports.org/
|
||||
|
||||
您也可以[自行构建][BUILD]。
|
||||
|
||||
|
||||
@ -140,7 +168,7 @@ scrcpy --help
|
||||
|
||||
## 功能介绍
|
||||
|
||||
### 捕获设置
|
||||
### 采集设置
|
||||
|
||||
#### 降低分辨率
|
||||
|
||||
@ -158,7 +186,7 @@ scrcpy -m 1024 # 简写
|
||||
|
||||
#### 修改码率
|
||||
|
||||
默认码率是 8Mbps。要改变视频的码率 (例如改为 2Mbps):
|
||||
默认码率是 8 Mbps。改变视频码率 (例如改为 2 Mbps):
|
||||
|
||||
```bash
|
||||
scrcpy --bit-rate 2M
|
||||
@ -167,7 +195,7 @@ scrcpy -b 2M # 简写
|
||||
|
||||
#### 限制帧率
|
||||
|
||||
要限制捕获的帧率:
|
||||
要限制采集的帧率:
|
||||
|
||||
```bash
|
||||
scrcpy --max-fps 15
|
||||
@ -194,10 +222,11 @@ scrcpy --crop 1224:1440:0:0 # 以 (0,0) 为原点的 1224x1440 像素
|
||||
要锁定镜像画面的方向:
|
||||
|
||||
```bash
|
||||
scrcpy --lock-video-orientation 0 # 自然方向
|
||||
scrcpy --lock-video-orientation 1 # 逆时针旋转 90°
|
||||
scrcpy --lock-video-orientation 2 # 180°
|
||||
scrcpy --lock-video-orientation 3 # 顺时针旋转 90°
|
||||
scrcpy --lock-video-orientation # 初始(目前)方向
|
||||
scrcpy --lock-video-orientation=0 # 自然方向
|
||||
scrcpy --lock-video-orientation=1 # 逆时针旋转 90°
|
||||
scrcpy --lock-video-orientation=2 # 180°
|
||||
scrcpy --lock-video-orientation=3 # 顺时针旋转 90°
|
||||
```
|
||||
|
||||
只影响录制的方向。
|
||||
@ -219,7 +248,9 @@ scrcpy --encoder OMX.qcom.video.encoder.avc
|
||||
scrcpy --encoder _
|
||||
```
|
||||
|
||||
### 屏幕录制
|
||||
### 采集
|
||||
|
||||
#### 屏幕录制
|
||||
|
||||
可以在镜像的同时录制视频:
|
||||
|
||||
@ -241,6 +272,75 @@ scrcpy -Nr file.mkv
|
||||
[packet delay variation]: https://en.wikipedia.org/wiki/Packet_delay_variation
|
||||
|
||||
|
||||
#### v4l2loopback
|
||||
|
||||
在 Linux 上,可以将视频流发送至 v4l2 回环 (loopback) 设备,因此可以使用任何 v4l2 工具像摄像头一样打开安卓设备。
|
||||
|
||||
需安装 `v4l2loopback` 模块:
|
||||
|
||||
```bash
|
||||
sudo apt install v4l2loopback-dkms
|
||||
```
|
||||
|
||||
创建一个 v4l2 设备:
|
||||
|
||||
```bash
|
||||
sudo modprobe v4l2loopback
|
||||
```
|
||||
|
||||
这样会在 `/dev/videoN` 创建一个新的视频设备,其中 `N` 是整数。 ([更多选项](https://github.com/umlaeute/v4l2loopback#options) 可以用来创建多个设备或者特定 ID 的设备)。
|
||||
|
||||
列出已启用的设备:
|
||||
|
||||
```bash
|
||||
# 需要 v4l-utils 包
|
||||
v4l2-ctl --list-devices
|
||||
|
||||
# 简单但或许足够
|
||||
ls /dev/video*
|
||||
```
|
||||
|
||||
使用一个 v4l2 漏开启 scrcpy:
|
||||
|
||||
```bash
|
||||
scrcpy --v4l2-sink=/dev/videoN
|
||||
scrcpy --v4l2-sink=/dev/videoN --no-display # 禁用窗口镜像
|
||||
scrcpy --v4l2-sink=/dev/videoN -N # 简写
|
||||
```
|
||||
|
||||
(将 `N` 替换为设备 ID,使用 `ls /dev/video*` 命令查看)
|
||||
|
||||
启用之后,可以使用 v4l2 工具打开视频流:
|
||||
|
||||
```bash
|
||||
ffplay -i /dev/videoN
|
||||
vlc v4l2:///dev/videoN # VLC 可能存在一些缓冲延迟
|
||||
```
|
||||
|
||||
例如,可以在 [OBS] 中采集视频。
|
||||
|
||||
[OBS]: https://obsproject.com/
|
||||
|
||||
|
||||
#### 缓冲
|
||||
|
||||
可以加入缓冲,会增加延迟,但可以减少抖动 (见 [#2464])。
|
||||
|
||||
[#2464]: https://github.com/Genymobile/scrcpy/issues/2464
|
||||
|
||||
对于显示缓冲:
|
||||
|
||||
```bash
|
||||
scrcpy --display-buffer=50 # 为显示增加 50 毫秒的缓冲
|
||||
```
|
||||
|
||||
对于 V4L2 漏:
|
||||
|
||||
```bash
|
||||
scrcpy --v4l2-buffer=500 # 为 v4l2 漏增加 500 毫秒的缓冲
|
||||
```
|
||||
|
||||
|
||||
### 连接
|
||||
|
||||
#### 无线
|
||||
@ -249,16 +349,17 @@ _Scrcpy_ 使用 `adb` 与设备通信,并且 `adb` 支持通过 TCP/IP [连接
|
||||
|
||||
1. 将设备和电脑连接至同一 Wi-Fi。
|
||||
2. 打开 设置 → 关于手机 → 状态信息,获取设备的 IP 地址,也可以执行以下的命令:
|
||||
|
||||
```bash
|
||||
adb shell ip route | awk '{print $9}'
|
||||
```
|
||||
|
||||
3. 启用设备的网络 adb 功能 `adb tcpip 5555`。
|
||||
3. 启用设备的网络 adb 功能: `adb tcpip 5555`。
|
||||
4. 断开设备的 USB 连接。
|
||||
5. 连接到您的设备:`adb connect DEVICE_IP:5555` _(将 `DEVICE_IP` 替换为设备 IP)_.
|
||||
5. 连接到您的设备:`adb connect DEVICE_IP:5555` _(将 `DEVICE_IP` 替换为设备 IP)_。
|
||||
6. 正常运行 `scrcpy`。
|
||||
|
||||
可能需要降低码率和分辨率:
|
||||
可能降低码率和分辨率会更好一些:
|
||||
|
||||
```bash
|
||||
scrcpy --bit-rate 2M --max-size 800
|
||||
@ -327,7 +428,7 @@ scrcpy --force-adb-forward
|
||||
```
|
||||
|
||||
|
||||
类似无线网络连接,可能需要降低画面质量:
|
||||
类似地,对于无线连接,可能需要降低画面质量:
|
||||
|
||||
```
|
||||
scrcpy -b2M -m800 --max-fps 15
|
||||
@ -353,7 +454,7 @@ scrcpy --window-x 100 --window-y 100 --window-width 800 --window-height 600
|
||||
|
||||
#### 无边框
|
||||
|
||||
关闭边框:
|
||||
禁用窗口边框:
|
||||
|
||||
```bash
|
||||
scrcpy --window-borderless
|
||||
@ -369,7 +470,7 @@ scrcpy --always-on-top
|
||||
|
||||
#### 全屏
|
||||
|
||||
您可以通过如下命令直接全屏启动scrcpy:
|
||||
您可以通过如下命令直接全屏启动 scrcpy:
|
||||
|
||||
```bash
|
||||
scrcpy --fullscreen
|
||||
@ -394,7 +495,7 @@ scrcpy --rotation 1
|
||||
|
||||
也可以使用 <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_ 和 <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_ 随时更改。
|
||||
|
||||
需要注意的是, _scrcpy_ 有三个不同的方向:
|
||||
需要注意的是, _scrcpy_ 中有三类旋转方向:
|
||||
- <kbd>MOD</kbd>+<kbd>r</kbd> 请求设备在竖屏和横屏之间切换 (如果前台应用程序不支持请求的朝向,可能会拒绝该请求)。
|
||||
- [`--lock-video-orientation`](#锁定屏幕方向) 改变镜像的朝向 (设备传输到电脑的画面的朝向)。这会影响录制。
|
||||
- `--rotation` (或 <kbd>MOD</kbd>+<kbd>←</kbd>/<kbd>MOD</kbd>+<kbd>→</kbd>) 只旋转窗口的内容。这只影响显示,不影响录制。
|
||||
@ -404,7 +505,7 @@ scrcpy --rotation 1
|
||||
|
||||
#### 只读
|
||||
|
||||
禁用电脑对设备的控制 (如键盘输入、鼠标事件和文件拖放):
|
||||
禁用电脑对设备的控制 (任何可与设备交互的方式:如键盘输入、鼠标事件和文件拖放):
|
||||
|
||||
```bash
|
||||
scrcpy --no-control
|
||||
@ -430,14 +531,14 @@ adb shell dumpsys display # 在输出中搜索 “mDisplayId=”
|
||||
|
||||
#### 保持常亮
|
||||
|
||||
阻止设备在连接时休眠:
|
||||
阻止设备在连接时一段时间后休眠:
|
||||
|
||||
```bash
|
||||
scrcpy --stay-awake
|
||||
scrcpy -w
|
||||
```
|
||||
|
||||
程序关闭时会恢复设备原来的设置。
|
||||
scrcpy 关闭时会恢复设备原来的设置。
|
||||
|
||||
|
||||
#### 关闭设备屏幕
|
||||
@ -451,7 +552,7 @@ scrcpy -S
|
||||
|
||||
或者在任何时候按 <kbd>MOD</kbd>+<kbd>o</kbd>。
|
||||
|
||||
要重新打开屏幕,按下 <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>.
|
||||
要重新打开屏幕,按下 <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>。
|
||||
|
||||
在Android上,`电源` 按钮始终能把屏幕打开。为了方便,对于在 _scrcpy_ 中发出的 `电源` 事件 (通过鼠标右键或 <kbd>MOD</kbd>+<kbd>p</kbd>),会 (尽最大的努力) 在短暂的延迟后将屏幕关闭。设备上的 `电源` 按钮仍然能打开设备屏幕。
|
||||
|
||||
@ -462,20 +563,17 @@ scrcpy --turn-screen-off --stay-awake
|
||||
scrcpy -Sw
|
||||
```
|
||||
|
||||
#### 退出时息屏
|
||||
|
||||
#### 渲染过期帧
|
||||
|
||||
默认状态下,为了降低延迟, _scrcpy_ 永远渲染解码成功的最近一帧,并跳过前面任意帧。
|
||||
|
||||
强制渲染所有帧 (可能导致延迟变高):
|
||||
scrcpy 退出时关闭设备屏幕:
|
||||
|
||||
```bash
|
||||
scrcpy --render-expired-frames
|
||||
scrcpy --power-off-on-close
|
||||
```
|
||||
|
||||
#### 显示触摸
|
||||
|
||||
在演示时,可能会需要显示物理触摸点 (在物理设备上的触摸点)。
|
||||
在演示时,可能会需要显示 (在物理设备上的) 物理触摸点。
|
||||
|
||||
Android 在 _开发者选项_ 中提供了这项功能。
|
||||
|
||||
@ -538,10 +636,32 @@ scrcpy --disable-screensaver
|
||||
|
||||
更准确的说,在按住鼠标左键时按住 <kbd>Ctrl</kbd>。直到松开鼠标左键,所有鼠标移动将以屏幕中心为原点,缩放或旋转内容 (如果应用支持)。
|
||||
|
||||
实际上,_scrcpy_ 会在以屏幕中心对称的位置上生成由“虚拟手指”发出的额外触摸事件。
|
||||
实际上,_scrcpy_ 会在关于屏幕中心对称的位置上用“虚拟手指”发出触摸事件。
|
||||
|
||||
#### 物理键盘模拟 (HID)
|
||||
|
||||
#### 文字注入偏好
|
||||
默认情况下,scrcpy 使用安卓按键或文本注入,这在任何情况都可以使用,但仅限于ASCII字符。
|
||||
|
||||
在 Linux 上,scrcpy 可以模拟为 Android 上的物理 USB 键盘,以提供更好地输入体验 (使用 [USB HID over AOAv2][hid-aoav2]):禁用虚拟键盘,并适用于任何字符和输入法。
|
||||
|
||||
[hid-aoav2]: https://source.android.com/devices/accessories/aoa2#hid-support
|
||||
|
||||
不过,这种方法仅支持 USB 连接以及 Linux平台。
|
||||
|
||||
启用 HID 模式:
|
||||
|
||||
```bash
|
||||
scrcpy --hid-keyboard
|
||||
scrcpy -K # 简写
|
||||
```
|
||||
|
||||
如果失败了 (如设备未通过 USB 连接),则自动回退至默认模式 (终端中会输出日志)。这即允许通过 USB 和 TCP/IP 连接时使用相同的命令行参数。
|
||||
|
||||
在这种模式下,原始按键事件 (扫描码) 被发送给设备,而与宿主机按键映射无关。因此,若键盘布局不匹配,需要在 Android 设备上进行配置,具体为 设置 → 系统 → 语言和输入法 → [实体键盘]。
|
||||
|
||||
[Physical keyboard]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
|
||||
|
||||
#### 文本注入偏好
|
||||
|
||||
打字的时候,系统会产生两种[事件][textevents]:
|
||||
- _按键事件_ ,代表一个按键被按下或松开。
|
||||
@ -557,13 +677,15 @@ scrcpy --prefer-text
|
||||
|
||||
(这会导致键盘在游戏中工作不正常)
|
||||
|
||||
该选项不影响 HID 键盘 (该模式下,所有按键都发送为扫描码)。
|
||||
|
||||
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
||||
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
||||
|
||||
|
||||
#### 按键重复
|
||||
|
||||
默认状态下,按住一个按键不放会生成多个重复按键事件。在某些游戏中这可能会导致性能问题。
|
||||
默认状态下,按住一个按键不放会生成多个重复按键事件。在某些游戏中这通常没有实际用途,且可能会导致性能问题。
|
||||
|
||||
避免转发重复按键事件:
|
||||
|
||||
@ -571,10 +693,11 @@ scrcpy --prefer-text
|
||||
scrcpy --no-key-repeat
|
||||
```
|
||||
|
||||
该选项不影响 HID 键盘 (该模式下,按键重复由 Android 直接管理)。
|
||||
|
||||
#### 右键和中键
|
||||
|
||||
默认状态下,右键会触发返回键 (或电源键),中键会触发 HOME 键。要禁用这些快捷键并把所有点击转发到设备:
|
||||
默认状态下,右键会触发返回键 (或电源键开启),中键会触发 HOME 键。要禁用这些快捷键并把所有点击转发到设备:
|
||||
|
||||
```bash
|
||||
scrcpy --forward-all-clicks
|
||||
@ -587,27 +710,27 @@ scrcpy --forward-all-clicks
|
||||
|
||||
将 APK 文件 (文件名以 `.apk` 结尾) 拖放到 _scrcpy_ 窗口来安装。
|
||||
|
||||
该操作在屏幕上不会出现任何变化,而会在控制台输出一条日志。
|
||||
不会有视觉反馈,终端会输出一条日志。
|
||||
|
||||
|
||||
#### 将文件推送至设备
|
||||
|
||||
要推送文件到设备的 `/sdcard/`,将 (非 APK) 文件拖放至 _scrcpy_ 窗口。
|
||||
要推送文件到设备的 `/sdcard/Download/`,将 (非 APK) 文件拖放至 _scrcpy_ 窗口。
|
||||
|
||||
该操作没有可见的响应,只会在控制台输出日志。
|
||||
不会有视觉反馈,终端会输出一条日志。
|
||||
|
||||
在启动时可以修改目标目录:
|
||||
|
||||
```bash
|
||||
scrcpy --push-target /sdcard/foo/bar/
|
||||
scrcpy --push-target=/sdcard/Movies/
|
||||
```
|
||||
|
||||
|
||||
### 音频转发
|
||||
|
||||
_Scrcpy_ 不支持音频。请使用 [sndcpy].
|
||||
_Scrcpy_ 不支持音频。请使用 [sndcpy]。
|
||||
|
||||
另外请阅读 [issue #14]。
|
||||
另见 [issue #14]。
|
||||
|
||||
[sndcpy]: https://github.com/rom1v/sndcpy
|
||||
[issue #14]: https://github.com/Genymobile/scrcpy/issues/14
|
||||
@ -632,36 +755,46 @@ _<kbd>[Super]</kbd> 键通常是指 <kbd>Windows</kbd> 或 <kbd>Cmd</kbd> 键。
|
||||
|
||||
[Super]: https://en.wikipedia.org/wiki/Super_key_(keyboard_button)
|
||||
|
||||
| 操作 | 快捷键 |
|
||||
| --------------------------------- | :------------------------------------------- |
|
||||
| 全屏 | <kbd>MOD</kbd>+<kbd>f</kbd> |
|
||||
| 向左旋转屏幕 | <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_ |
|
||||
| 向右旋转屏幕 | <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_ |
|
||||
| 将窗口大小重置为1:1 (匹配像素) | <kbd>MOD</kbd>+<kbd>g</kbd> |
|
||||
| 将窗口大小重置为消除黑边 | <kbd>MOD</kbd>+<kbd>w</kbd> \| _双击¹_ |
|
||||
| 点按 `主屏幕` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _鼠标中键_ |
|
||||
| 点按 `返回` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _鼠标右键²_ |
|
||||
| 点按 `切换应用` | <kbd>MOD</kbd>+<kbd>s</kbd> |
|
||||
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd> |
|
||||
| 点按 `音量+` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上箭头)_ |
|
||||
| 点按 `音量-` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下箭头)_ |
|
||||
| 点按 `电源` | <kbd>MOD</kbd>+<kbd>p</kbd> |
|
||||
| 打开屏幕 | _鼠标右键²_ |
|
||||
| 关闭设备屏幕 (但继续在电脑上显示) | <kbd>MOD</kbd>+<kbd>o</kbd> |
|
||||
| 打开设备屏幕 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd> |
|
||||
| 旋转设备屏幕 | <kbd>MOD</kbd>+<kbd>r</kbd> |
|
||||
| 展开通知面板 | <kbd>MOD</kbd>+<kbd>n</kbd> |
|
||||
| 收起通知面板 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd> |
|
||||
| 复制到剪贴板³ | <kbd>MOD</kbd>+<kbd>c</kbd> |
|
||||
| 剪切到剪贴板³ | <kbd>MOD</kbd>+<kbd>x</kbd> |
|
||||
| 同步剪贴板并粘贴³ | <kbd>MOD</kbd>+<kbd>v</kbd> |
|
||||
| 注入电脑剪贴板文本 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> |
|
||||
| 打开/关闭FPS显示 (在 stdout) | <kbd>MOD</kbd>+<kbd>i</kbd> |
|
||||
| 捏拉缩放 | <kbd>Ctrl</kbd>+_按住并移动鼠标_ |
|
||||
| 操作 | 快捷键
|
||||
| --------------------------------- | :-------------------------------------------
|
||||
| 全屏 | <kbd>MOD</kbd>+<kbd>f</kbd>
|
||||
| 向左旋转屏幕 | <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_
|
||||
| 向右旋转屏幕 | <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_
|
||||
| 将窗口大小重置为1:1 (匹配像素) | <kbd>MOD</kbd>+<kbd>g</kbd>
|
||||
| 将窗口大小重置为消除黑边 | <kbd>MOD</kbd>+<kbd>w</kbd> \| _双击左键¹_
|
||||
| 点按 `主屏幕` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _中键_
|
||||
| 点按 `返回` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _右键²_
|
||||
| 点按 `切换应用` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _第4键³_
|
||||
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd>
|
||||
| 点按 `音量+` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上箭头)_
|
||||
| 点按 `音量-` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下箭头)_
|
||||
| 点按 `电源` | <kbd>MOD</kbd>+<kbd>p</kbd>
|
||||
| 打开屏幕 | _鼠标右键²_
|
||||
| 关闭设备屏幕 (但继续在电脑上显示) | <kbd>MOD</kbd>+<kbd>o</kbd>
|
||||
| 打开设备屏幕 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>
|
||||
| 旋转设备屏幕 | <kbd>MOD</kbd>+<kbd>r</kbd>
|
||||
| 展开通知面板 | <kbd>MOD</kbd>+<kbd>n</kbd> \| _第5键³_
|
||||
| 展开设置面板 | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _双击第5键³_
|
||||
| 收起通知面板 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
||||
| 复制到剪贴板⁴ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
||||
| 剪切到剪贴板⁴ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
||||
| 同步剪贴板并粘贴⁴ | <kbd>MOD</kbd>+<kbd>v</kbd>
|
||||
| 注入电脑剪贴板文本 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
|
||||
| 打开/关闭FPS显示 (至标准输出) | <kbd>MOD</kbd>+<kbd>i</kbd>
|
||||
| 捏拉缩放 | <kbd>Ctrl</kbd>+_按住并移动鼠标_
|
||||
| 拖放 APK 文件 | 从电脑安装 APK 文件
|
||||
| 拖放非 APK 文件 | [将文件推送至设备](#push-file-to-device)
|
||||
|
||||
_¹双击黑边可以去除黑边_
|
||||
_²点击鼠标右键将在屏幕熄灭时点亮屏幕,其余情况则视为按下返回键 。_
|
||||
_³需要安卓版本 Android >= 7。_
|
||||
_¹双击黑边可以去除黑边。_
|
||||
_²点击鼠标右键将在屏幕熄灭时点亮屏幕,其余情况则视为按下返回键 。_
|
||||
_³鼠标的第4键和第5键。_
|
||||
_⁴需要安卓版本 Android >= 7。_
|
||||
|
||||
有重复按键的快捷键通过松开再按下一个按键来进行,如“展开设置面板”:
|
||||
|
||||
1. 按下 <kbd>MOD</kbd> 不放。
|
||||
2. 双击 <kbd>n</kbd>。
|
||||
3. 松开 <kbd>MOD</kbd>。
|
||||
|
||||
所有的 <kbd>Ctrl</kbd>+_按键_ 的快捷键都会被转发到设备,所以会由当前应用程序进行处理。
|
||||
|
||||
@ -670,18 +803,20 @@ _³需要安卓版本 Android >= 7。_
|
||||
|
||||
要使用指定的 _adb_ 二进制文件,可以设置环境变量 `ADB`:
|
||||
|
||||
ADB=/path/to/adb scrcpy
|
||||
```bash
|
||||
ADB=/path/to/adb scrcpy
|
||||
```
|
||||
|
||||
要覆盖 `scrcpy-server` 的路径,可以设置 `SCRCPY_SERVER_PATH`。
|
||||
|
||||
[useful]: https://github.com/Genymobile/scrcpy/issues/278#issuecomment-429330345
|
||||
要覆盖图标,可以设置其路径至 `SCRCPY_ICON_PATH`。
|
||||
|
||||
|
||||
## 为什么叫 _scrcpy_ ?
|
||||
|
||||
一个同事让我找出一个和 [gnirehtet] 一样难以发音的名字。
|
||||
|
||||
[`strcpy`] 复制一个 **str**ing; `scrcpy` 复制一个 **scr**een。
|
||||
[`strcpy`] 复制一个 **str**ing (字符串); `scrcpy` 复制一个 **scr**een (屏幕)。
|
||||
|
||||
[gnirehtet]: https://github.com/Genymobile/gnirehtet
|
||||
[`strcpy`]: http://man7.org/linux/man-pages/man3/strcpy.3.html
|
||||
@ -689,14 +824,12 @@ _³需要安卓版本 Android >= 7。_
|
||||
|
||||
## 如何构建?
|
||||
|
||||
请查看[BUILD]。
|
||||
|
||||
[BUILD]: BUILD.md
|
||||
请查看 [BUILD]。
|
||||
|
||||
|
||||
## 常见问题
|
||||
|
||||
请查看[FAQ](FAQ.md)。
|
||||
请查看 [FAQ](FAQ.md)。
|
||||
|
||||
|
||||
## 开发者
|
||||
|
110
app/meson.build
110
app/meson.build
@ -1,6 +1,8 @@
|
||||
src = [
|
||||
'src/main.c',
|
||||
'src/adb.c',
|
||||
'src/adb_parser.c',
|
||||
'src/adb_tunnel.c',
|
||||
'src/cli.c',
|
||||
'src/clock.c',
|
||||
'src/compat.c',
|
||||
@ -8,32 +10,62 @@ src = [
|
||||
'src/controller.c',
|
||||
'src/decoder.c',
|
||||
'src/device_msg.c',
|
||||
'src/event_converter.c',
|
||||
'src/icon.c',
|
||||
'src/file_handler.c',
|
||||
'src/fps_counter.c',
|
||||
'src/frame_buffer.c',
|
||||
'src/input_events.c',
|
||||
'src/input_manager.c',
|
||||
'src/keyboard_inject.c',
|
||||
'src/mouse_inject.c',
|
||||
'src/opengl.c',
|
||||
'src/options.c',
|
||||
'src/receiver.c',
|
||||
'src/recorder.c',
|
||||
'src/scrcpy.c',
|
||||
'src/screen.c',
|
||||
'src/server.c',
|
||||
'src/stream.c',
|
||||
'src/tiny_xpm.c',
|
||||
'src/video_buffer.c',
|
||||
'src/util/acksync.c',
|
||||
'src/util/file.c',
|
||||
'src/util/intmap.c',
|
||||
'src/util/intr.c',
|
||||
'src/util/log.c',
|
||||
'src/util/net.c',
|
||||
'src/util/net_intr.c',
|
||||
'src/util/process.c',
|
||||
'src/util/str_util.c',
|
||||
'src/util/process_intr.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/str.c',
|
||||
'src/util/term.c',
|
||||
'src/util/thread.c',
|
||||
'src/util/tick.c',
|
||||
]
|
||||
|
||||
conf = configuration_data()
|
||||
|
||||
conf.set('_POSIX_C_SOURCE', '200809L')
|
||||
conf.set('_XOPEN_SOURCE', '700')
|
||||
conf.set('_GNU_SOURCE', true)
|
||||
|
||||
if host_machine.system() == 'windows'
|
||||
src += [ 'src/sys/win/process.c' ]
|
||||
windows = import('windows')
|
||||
src += [
|
||||
'src/sys/win/file.c',
|
||||
'src/sys/win/process.c',
|
||||
windows.compile_resources('scrcpy-windows.rc'),
|
||||
]
|
||||
conf.set('_WIN32_WINNT', '0x0600')
|
||||
conf.set('WINVER', '0x0600')
|
||||
else
|
||||
src += [ 'src/sys/unix/process.c' ]
|
||||
src += [
|
||||
'src/sys/unix/file.c',
|
||||
'src/sys/unix/process.c',
|
||||
]
|
||||
if host_machine.system() == 'darwin'
|
||||
conf.set('_DARWIN_C_SOURCE', true)
|
||||
endif
|
||||
endif
|
||||
|
||||
v4l2_support = host_machine.system() == 'linux'
|
||||
@ -41,28 +73,37 @@ if v4l2_support
|
||||
src += [ 'src/v4l2_sink.c' ]
|
||||
endif
|
||||
|
||||
check_functions = [
|
||||
'strdup'
|
||||
]
|
||||
aoa_hid_support = host_machine.system() == 'linux'
|
||||
if aoa_hid_support
|
||||
src += [
|
||||
'src/aoa_hid.c',
|
||||
'src/hid_keyboard.c',
|
||||
]
|
||||
endif
|
||||
|
||||
cc = meson.get_compiler('c')
|
||||
|
||||
if not get_option('crossbuild_windows')
|
||||
crossbuild_windows = meson.is_cross_build() and host_machine.system() == 'windows'
|
||||
|
||||
if not crossbuild_windows
|
||||
|
||||
# native build
|
||||
dependencies = [
|
||||
dependency('libavformat'),
|
||||
dependency('libavcodec'),
|
||||
dependency('libavformat', version: '>= 57.33'),
|
||||
dependency('libavcodec', version: '>= 57.37'),
|
||||
dependency('libavutil'),
|
||||
dependency('sdl2'),
|
||||
dependency('sdl2', version: '>= 2.0.5'),
|
||||
]
|
||||
|
||||
if v4l2_support
|
||||
dependencies += dependency('libavdevice')
|
||||
endif
|
||||
|
||||
else
|
||||
if aoa_hid_support
|
||||
dependencies += dependency('libusb-1.0')
|
||||
endif
|
||||
|
||||
else
|
||||
# cross-compile mingw32 build (from Linux to Windows)
|
||||
prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2')
|
||||
sdl2_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_sdl2 + '/bin'
|
||||
@ -102,7 +143,11 @@ if host_machine.system() == 'windows'
|
||||
dependencies += cc.find_library('ws2_32')
|
||||
endif
|
||||
|
||||
conf = configuration_data()
|
||||
check_functions = [
|
||||
'strdup',
|
||||
'asprintf',
|
||||
'vasprintf',
|
||||
]
|
||||
|
||||
foreach f : check_functions
|
||||
if cc.has_function(f)
|
||||
@ -111,6 +156,9 @@ foreach f : check_functions
|
||||
endif
|
||||
endforeach
|
||||
|
||||
conf.set('HAVE_SOCK_CLOEXEC', host_machine.system() != 'windows' and
|
||||
cc.has_header_symbol('sys/socket.h', 'SOCK_CLOEXEC'))
|
||||
|
||||
# the version, updated on release
|
||||
conf.set_quoted('SCRCPY_VERSION', meson.project_version())
|
||||
|
||||
@ -139,6 +187,9 @@ conf.set('SERVER_DEBUGGER_METHOD_NEW', get_option('server_debugger_method') == '
|
||||
# enable V4L2 support (linux only)
|
||||
conf.set('HAVE_V4L2', v4l2_support)
|
||||
|
||||
# enable HID over AOA support (linux only)
|
||||
conf.set('HAVE_AOA_HID', aoa_hid_support)
|
||||
|
||||
configure_file(configuration: conf, output: 'config.h')
|
||||
|
||||
src_dir = include_directories('src')
|
||||
@ -150,6 +201,9 @@ executable('scrcpy', src,
|
||||
c_args: [])
|
||||
|
||||
install_man('scrcpy.1')
|
||||
install_data('../data/icon.png',
|
||||
rename: 'scrcpy.png',
|
||||
install_dir: 'share/icons/hicolor/256x256/apps')
|
||||
|
||||
|
||||
### TESTS
|
||||
@ -157,8 +211,14 @@ install_man('scrcpy.1')
|
||||
# do not build tests in release (assertions would not be executed at all)
|
||||
if get_option('buildtype') == 'debug'
|
||||
tests = [
|
||||
['test_adb_parser', [
|
||||
'tests/test_adb_parser.c',
|
||||
'src/adb_parser.c',
|
||||
'src/util/str.c',
|
||||
'src/util/strbuf.c',
|
||||
]],
|
||||
['test_buffer_util', [
|
||||
'tests/test_buffer_util.c'
|
||||
'tests/test_buffer_util.c',
|
||||
]],
|
||||
['test_cbuf', [
|
||||
'tests/test_cbuf.c',
|
||||
@ -166,7 +226,11 @@ if get_option('buildtype') == 'debug'
|
||||
['test_cli', [
|
||||
'tests/test_cli.c',
|
||||
'src/cli.c',
|
||||
'src/util/str_util.c',
|
||||
'src/options.c',
|
||||
'src/util/net.c',
|
||||
'src/util/str.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/term.c',
|
||||
]],
|
||||
['test_clock', [
|
||||
'tests/test_clock.c',
|
||||
@ -175,7 +239,8 @@ if get_option('buildtype') == 'debug'
|
||||
['test_control_msg_serialize', [
|
||||
'tests/test_control_msg_serialize.c',
|
||||
'src/control_msg.c',
|
||||
'src/util/str_util.c',
|
||||
'src/util/str.c',
|
||||
'src/util/strbuf.c',
|
||||
]],
|
||||
['test_device_msg_deserialize', [
|
||||
'tests/test_device_msg_deserialize.c',
|
||||
@ -184,9 +249,14 @@ if get_option('buildtype') == 'debug'
|
||||
['test_queue', [
|
||||
'tests/test_queue.c',
|
||||
]],
|
||||
['test_strutil', [
|
||||
'tests/test_strutil.c',
|
||||
'src/util/str_util.c',
|
||||
['test_strbuf', [
|
||||
'tests/test_strbuf.c',
|
||||
'src/util/strbuf.c',
|
||||
]],
|
||||
['test_str', [
|
||||
'tests/test_str.c',
|
||||
'src/util/str.c',
|
||||
'src/util/strbuf.c',
|
||||
]],
|
||||
]
|
||||
|
||||
|
9
app/scrcpy-windows.manifest
Normal file
9
app/scrcpy-windows.manifest
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
|
||||
<asmv3:application>
|
||||
<asmv3:windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</asmv3:windowsSettings>
|
||||
</asmv3:application>
|
||||
</assembly>
|
23
app/scrcpy-windows.rc
Normal file
23
app/scrcpy-windows.rc
Normal file
@ -0,0 +1,23 @@
|
||||
#include <winuser.h>
|
||||
|
||||
0 ICON "../data/icon.ico"
|
||||
1 RT_MANIFEST "scrcpy-windows.manifest"
|
||||
2 VERSIONINFO
|
||||
BEGIN
|
||||
BLOCK "StringFileInfo"
|
||||
BEGIN
|
||||
BLOCK "040904E4"
|
||||
BEGIN
|
||||
VALUE "FileDescription", "Display and control your Android device"
|
||||
VALUE "InternalName", "scrcpy"
|
||||
VALUE "LegalCopyright", "Romain Vimont, Genymobile"
|
||||
VALUE "OriginalFilename", "scrcpy.exe"
|
||||
VALUE "ProductName", "scrcpy"
|
||||
VALUE "ProductVersion", "1.21"
|
||||
END
|
||||
END
|
||||
BLOCK "VarFileInfo"
|
||||
BEGIN
|
||||
VALUE "Translation", 0x409, 1252
|
||||
END
|
||||
END
|
62
app/scrcpy.1
62
app/scrcpy.1
@ -82,6 +82,20 @@ Start in fullscreen.
|
||||
.B \-h, \-\-help
|
||||
Print this help.
|
||||
|
||||
.TP
|
||||
.B \-K, \-\-hid\-keyboard
|
||||
Simulate a physical keyboard by using HID over AOAv2.
|
||||
|
||||
This provides a better experience for IME users, and allows to generate non-ASCII characters, contrary to the default injection method.
|
||||
|
||||
It may only work over USB, and is currently only supported on Linux.
|
||||
|
||||
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:
|
||||
|
||||
adb shell am start -a android.settings.HARD_KEYBOARD_SETTINGS
|
||||
|
||||
However, the option is only available when the HID keyboard is enabled (or a physical keyboard is connected).
|
||||
|
||||
.TP
|
||||
.B \-\-legacy\-paste
|
||||
Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+Shift+v).
|
||||
@ -106,6 +120,12 @@ Limit both the width and height of the video to \fIvalue\fR. The other dimension
|
||||
|
||||
Default is 0 (unlimited).
|
||||
|
||||
.TP
|
||||
.B \-\-no\-clipboard\-autosync
|
||||
By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.
|
||||
|
||||
This option disables this automatic synchronization.
|
||||
|
||||
.TP
|
||||
.B \-n, \-\-no\-control
|
||||
Disable device control (mirror the device in read\-only).
|
||||
@ -128,6 +148,10 @@ Set the TCP port (range) used by the client to listen.
|
||||
|
||||
Default is 27183:27199.
|
||||
|
||||
.TP
|
||||
.B \-\-power\-off\-on\-close
|
||||
Turn the device screen off when closing scrcpy.
|
||||
|
||||
.TP
|
||||
.B \-\-prefer\-text
|
||||
Inject alpha characters and space as text events instead of key events.
|
||||
@ -141,6 +165,10 @@ Set the target directory for pushing files to the device by drag & drop. It is p
|
||||
|
||||
Default is "/sdcard/Download/".
|
||||
|
||||
.TP
|
||||
.B \-\-raw\-key\-events
|
||||
Inject key events for all input keys, and ignore text events.
|
||||
|
||||
.TP
|
||||
.BI "\-r, \-\-record " file
|
||||
Record screen to
|
||||
@ -181,6 +209,14 @@ For example, to use either LCtrl+LAlt or LSuper for scrcpy shortcuts, pass "lctr
|
||||
|
||||
Default is "lalt,lsuper" (left-Alt or left-Super).
|
||||
|
||||
.TP
|
||||
.BI "\-\-tcpip[=ip[:port]]
|
||||
Configure and reconnect the device over TCP/IP.
|
||||
|
||||
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).
|
||||
|
||||
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.
|
||||
|
||||
.TP
|
||||
.B \-S, \-\-turn\-screen\-off
|
||||
Turn the device screen off immediately.
|
||||
@ -191,6 +227,18 @@ Enable "show touches" on start, restore the initial value on exit.
|
||||
|
||||
It only shows physical touches (not clicks from scrcpy).
|
||||
|
||||
.TP
|
||||
.BI "\-\-tunnel\-host " ip
|
||||
Set the IP address of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.
|
||||
|
||||
Default is localhost.
|
||||
|
||||
.TP
|
||||
.BI "\-\-tunnel\-port " port
|
||||
Set the TCP port of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.
|
||||
|
||||
Default is 0 (not forced): the local port used for establishing the tunnel will be used.
|
||||
|
||||
.TP
|
||||
.BI "\-\-v4l2-sink " /dev/videoN
|
||||
Output to v4l2loopback device.
|
||||
@ -356,16 +404,24 @@ Pinch-to-zoom from the center of the screen
|
||||
.B Drag & drop APK file
|
||||
Install APK from computer
|
||||
|
||||
.TP
|
||||
.B Drag & drop non-APK file
|
||||
Push file to device (see \fB\-\-push\-target\fR)
|
||||
|
||||
|
||||
.SH Environment variables
|
||||
|
||||
.TP
|
||||
.B ADB
|
||||
Specify the path to adb.
|
||||
Path to adb.
|
||||
|
||||
.TP
|
||||
.B SCRCPY_ICON_PATH
|
||||
Path to the program icon.
|
||||
|
||||
.TP
|
||||
.B SCRCPY_SERVER_PATH
|
||||
Specify the path to server binary.
|
||||
Path to the server binary.
|
||||
|
||||
|
||||
.SH AUTHORS
|
||||
@ -390,7 +446,7 @@ Copyright \(co 2018 Genymobile
|
||||
Genymobile
|
||||
.UE
|
||||
|
||||
Copyright \(co 2018\-2020
|
||||
Copyright \(co 2018\-2021
|
||||
.MT rom@rom1v.com
|
||||
Romain Vimont
|
||||
.ME
|
||||
|
333
app/src/adb.c
333
app/src/adb.c
@ -5,8 +5,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "adb_parser.h"
|
||||
#include "util/file.h"
|
||||
#include "util/log.h"
|
||||
#include "util/str_util.h"
|
||||
#include "util/process_intr.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static const char *adb_command;
|
||||
|
||||
@ -68,7 +71,7 @@ show_adb_installation_msg() {
|
||||
{"pacman", "pacman -S android-tools"},
|
||||
};
|
||||
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
|
||||
if (search_executable(pkg_managers[i].binary)) {
|
||||
if (sc_file_executable_exists(pkg_managers[i].binary)) {
|
||||
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
|
||||
return;
|
||||
}
|
||||
@ -80,27 +83,28 @@ show_adb_installation_msg() {
|
||||
}
|
||||
|
||||
static void
|
||||
show_adb_err_msg(enum process_result err, const char *const argv[]) {
|
||||
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) {
|
||||
LOGE("Failed to execute (could not allocate error message)");
|
||||
LOG_OOM();
|
||||
LOGE("Failed to execute");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (err) {
|
||||
case PROCESS_ERROR_GENERIC:
|
||||
case SC_PROCESS_ERROR_GENERIC:
|
||||
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
||||
LOGE("Failed to execute: %s", buf);
|
||||
break;
|
||||
case PROCESS_ERROR_MISSING_BINARY:
|
||||
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 PROCESS_SUCCESS:
|
||||
case SC_PROCESS_SUCCESS:
|
||||
// do nothing
|
||||
break;
|
||||
}
|
||||
@ -108,17 +112,61 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
|
||||
free(buf);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
|
||||
int i;
|
||||
process_t process;
|
||||
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) {
|
||||
return PROCESS_NONE;
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
argv[0] = get_adb_command();
|
||||
int i;
|
||||
if (serial) {
|
||||
argv[1] = "-s";
|
||||
argv[2] = serial;
|
||||
@ -129,98 +177,295 @@ adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
|
||||
|
||||
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
||||
argv[len + i] = NULL;
|
||||
enum process_result r = process_execute(argv, &process);
|
||||
if (r != PROCESS_SUCCESS) {
|
||||
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);
|
||||
process = PROCESS_NONE;
|
||||
pid = SC_PROCESS_NONE;
|
||||
}
|
||||
|
||||
free(argv);
|
||||
return process;
|
||||
return pid;
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_forward(const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name) {
|
||||
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};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
return process_check_success_intr(intr, pid, "adb forward", flags);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_forward_remove(const char *serial, uint16_t local_port) {
|
||||
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};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_reverse(const char *serial, const char *device_socket_name,
|
||||
uint16_t local_port) {
|
||||
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};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
return process_check_success_intr(intr, pid, "adb reverse", flags);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_reverse_remove(const char *serial, const char *device_socket_name) {
|
||||
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};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_push(const char *serial, const char *local, const char *remote) {
|
||||
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 = strquote(local);
|
||||
local = sc_str_quote(local);
|
||||
if (!local) {
|
||||
return PROCESS_NONE;
|
||||
return SC_PROCESS_NONE;
|
||||
}
|
||||
remote = strquote(remote);
|
||||
remote = sc_str_quote(remote);
|
||||
if (!remote) {
|
||||
free((void *) local);
|
||||
return PROCESS_NONE;
|
||||
return SC_PROCESS_NONE;
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *const adb_cmd[] = {"push", local, remote};
|
||||
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
free((void *) remote);
|
||||
free((void *) local);
|
||||
#endif
|
||||
|
||||
return proc;
|
||||
return process_check_success_intr(intr, pid, "adb push", flags);
|
||||
}
|
||||
|
||||
process_t
|
||||
adb_install(const char *serial, const char *local) {
|
||||
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 = strquote(local);
|
||||
local = sc_str_quote(local);
|
||||
if (!local) {
|
||||
return PROCESS_NONE;
|
||||
return SC_PROCESS_NONE;
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *const adb_cmd[] = {"install", "-r", local};
|
||||
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
free((void *) local);
|
||||
#endif
|
||||
|
||||
return proc;
|
||||
return process_check_success_intr(intr, pid, "adb install", flags);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
||||
unsigned flags) {
|
||||
char port_string[5 + 1];
|
||||
sprintf(port_string, "%" PRIu16, port);
|
||||
const char *const adb_cmd[] = {"tcpip", port_string};
|
||||
|
||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||
return process_check_success_intr(intr, pid, "adb tcpip", flags);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||
const char *const adb_cmd[] = {"connect", ip_port};
|
||||
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
LOGE("Could not execute \"adb connect\"");
|
||||
return false;
|
||||
}
|
||||
|
||||
// "adb connect" always returns successfully (with exit code 0), even in
|
||||
// case of failure. As a workaround, check if its output starts with
|
||||
// "connected".
|
||||
char buf[128];
|
||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||
sc_pipe_close(pout);
|
||||
|
||||
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (r == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
||||
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
||||
// "adb connect" also prints errors to stdout. Since we capture it,
|
||||
// re-print the error to stderr.
|
||||
sc_str_truncate(buf, r, "\r\n");
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||
const char *const adb_cmd[] = {"disconnect", ip_port};
|
||||
size_t len = ip_port ? ARRAY_LEN(adb_cmd)
|
||||
: ARRAY_LEN(adb_cmd) - 1;
|
||||
|
||||
sc_pid pid = adb_execute(NULL, adb_cmd, len, flags);
|
||||
return process_check_success_intr(intr, pid, "adb disconnect", flags);
|
||||
}
|
||||
|
||||
char *
|
||||
adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
|
||||
unsigned flags) {
|
||||
const char *const adb_cmd[] = {"shell", "getprop", prop};
|
||||
|
||||
sc_pipe pout;
|
||||
sc_pid pid =
|
||||
adb_execute_p(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
LOGE("Could not execute \"adb getprop\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char buf[128];
|
||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||
sc_pipe_close(pout);
|
||||
|
||||
bool ok = process_check_success_intr(intr, pid, "adb getprop", flags);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (r == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sc_str_truncate(buf, r, " \r\n");
|
||||
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
char *
|
||||
adb_get_serialno(struct sc_intr *intr, unsigned flags) {
|
||||
const char *const adb_cmd[] = {"get-serialno"};
|
||||
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
LOGE("Could not execute \"adb get-serialno\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char buf[128];
|
||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||
sc_pipe_close(pout);
|
||||
|
||||
bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (r == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sc_str_truncate(buf, r, " \r\n");
|
||||
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
char *
|
||||
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
|
||||
const char *const cmd[] = {"shell", "ip", "route"};
|
||||
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_execute_p(serial, cmd, ARRAY_LEN(cmd), flags, &pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
LOGD("Could not execute \"ip route\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// "adb shell ip route" output should contain only a few lines
|
||||
char buf[1024];
|
||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||
sc_pipe_close(pout);
|
||||
|
||||
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (r == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
assert((size_t) r <= sizeof(buf));
|
||||
if (r == sizeof(buf) && buf[sizeof(buf) - 1] != '\0') {
|
||||
// The implementation assumes that the output of "ip route" fits in the
|
||||
// buffer in a single pass
|
||||
LOGW("Result of \"ip route\" does not fit in 1Kb. "
|
||||
"Please report an issue.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sc_adb_parse_device_ip_from_output(buf, r);
|
||||
}
|
||||
|
@ -6,29 +6,89 @@
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "util/process.h"
|
||||
#include "util/intr.h"
|
||||
|
||||
process_t
|
||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
|
||||
#define SC_ADB_NO_STDOUT (1 << 0)
|
||||
#define SC_ADB_NO_STDERR (1 << 1)
|
||||
#define SC_ADB_NO_LOGERR (1 << 2)
|
||||
|
||||
process_t
|
||||
adb_forward(const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name);
|
||||
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
|
||||
|
||||
process_t
|
||||
adb_forward_remove(const char *serial, uint16_t local_port);
|
||||
sc_pid
|
||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
||||
unsigned flags);
|
||||
|
||||
process_t
|
||||
adb_reverse(const char *serial, const char *device_socket_name,
|
||||
uint16_t local_port);
|
||||
bool
|
||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name, unsigned flags);
|
||||
|
||||
process_t
|
||||
adb_reverse_remove(const char *serial, const char *device_socket_name);
|
||||
bool
|
||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port, unsigned flags);
|
||||
|
||||
process_t
|
||||
adb_push(const char *serial, const char *local, const char *remote);
|
||||
bool
|
||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
||||
const char *device_socket_name, uint16_t local_port,
|
||||
unsigned flags);
|
||||
|
||||
process_t
|
||||
adb_install(const char *serial, const char *local);
|
||||
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
|
||||
|
65
app/src/adb_parser.c
Normal file
65
app/src/adb_parser.c
Normal file
@ -0,0 +1,65 @@
|
||||
#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;
|
||||
}
|
14
app/src/adb_parser.h
Normal file
14
app/src/adb_parser.h
Normal file
@ -0,0 +1,14 @@
|
||||
#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
|
169
app/src/adb_tunnel.c
Normal file
169
app/src/adb_tunnel.c
Normal file
@ -0,0 +1,169 @@
|
||||
#include "adb_tunnel.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "adb.h"
|
||||
#include "util/log.h"
|
||||
#include "util/net_intr.h"
|
||||
#include "util/process_intr.h"
|
||||
|
||||
#define SC_SOCKET_NAME "scrcpy"
|
||||
|
||||
static bool
|
||||
listen_on_port(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
||||
return net_listen_intr(intr, socket, IPV4_LOCALHOST, port, 1);
|
||||
}
|
||||
|
||||
static bool
|
||||
enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
||||
struct sc_intr *intr, const char *serial,
|
||||
struct sc_port_range port_range) {
|
||||
uint16_t port = port_range.first;
|
||||
for (;;) {
|
||||
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port,
|
||||
SC_ADB_NO_STDOUT)) {
|
||||
// the command itself failed, it will fail on any port
|
||||
return false;
|
||||
}
|
||||
|
||||
// At the application level, the device part is "the server" because it
|
||||
// serves video stream and control. However, at the network level, the
|
||||
// client listens and the server connects to the client. That way, the
|
||||
// client can listen before starting the server app, so there is no
|
||||
// need to try to connect until the server socket is listening on the
|
||||
// device.
|
||||
sc_socket server_socket = net_socket();
|
||||
if (server_socket != SC_SOCKET_NONE) {
|
||||
bool ok = listen_on_port(intr, server_socket, port);
|
||||
if (ok) {
|
||||
// success
|
||||
tunnel->server_socket = server_socket;
|
||||
tunnel->local_port = port;
|
||||
tunnel->enabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
net_close(server_socket);
|
||||
}
|
||||
|
||||
if (sc_intr_is_interrupted(intr)) {
|
||||
// Stop immediately
|
||||
return false;
|
||||
}
|
||||
|
||||
// failure, disable tunnel and try another port
|
||||
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||
SC_ADB_NO_STDOUT)) {
|
||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||
}
|
||||
|
||||
// check before incrementing to avoid overflow on port 65535
|
||||
if (port < port_range.last) {
|
||||
LOGW("Could not listen on port %" PRIu16", retrying on %" PRIu16,
|
||||
port, (uint16_t) (port + 1));
|
||||
port++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (port_range.first == port_range.last) {
|
||||
LOGE("Could not listen on port %" PRIu16, port_range.first);
|
||||
} else {
|
||||
LOGE("Could not listen on any port in range %" PRIu16 ":%" PRIu16,
|
||||
port_range.first, port_range.last);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
|
||||
struct sc_intr *intr, const char *serial,
|
||||
struct sc_port_range port_range) {
|
||||
tunnel->forward = true;
|
||||
|
||||
uint16_t port = port_range.first;
|
||||
for (;;) {
|
||||
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_ADB_NO_STDOUT)) {
|
||||
// success
|
||||
tunnel->local_port = port;
|
||||
tunnel->enabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sc_intr_is_interrupted(intr)) {
|
||||
// Stop immediately
|
||||
return false;
|
||||
}
|
||||
|
||||
if (port < port_range.last) {
|
||||
LOGW("Could not forward port %" PRIu16", retrying on %" PRIu16,
|
||||
port, (uint16_t) (port + 1));
|
||||
port++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (port_range.first == port_range.last) {
|
||||
LOGE("Could not forward port %" PRIu16, port_range.first);
|
||||
} else {
|
||||
LOGE("Could not forward any port in range %" PRIu16 ":%" PRIu16,
|
||||
port_range.first, port_range.last);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sc_adb_tunnel_init(struct sc_adb_tunnel *tunnel) {
|
||||
tunnel->enabled = false;
|
||||
tunnel->forward = false;
|
||||
tunnel->server_socket = SC_SOCKET_NONE;
|
||||
tunnel->local_port = 0;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_adb_tunnel_open(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
||||
const char *serial, struct sc_port_range port_range,
|
||||
bool force_adb_forward) {
|
||||
assert(!tunnel->enabled);
|
||||
|
||||
if (!force_adb_forward) {
|
||||
// Attempt to use "adb reverse"
|
||||
if (enable_tunnel_reverse_any_port(tunnel, intr, serial, port_range)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// if "adb reverse" does not work (e.g. over "adb connect"), it
|
||||
// fallbacks to "adb forward", so the app socket is the client
|
||||
|
||||
LOGW("'adb reverse' failed, fallback to 'adb forward'");
|
||||
}
|
||||
|
||||
return enable_tunnel_forward_any_port(tunnel, intr, serial, port_range);
|
||||
}
|
||||
|
||||
bool
|
||||
sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
||||
const char *serial) {
|
||||
assert(tunnel->enabled);
|
||||
|
||||
bool ret;
|
||||
if (tunnel->forward) {
|
||||
ret = adb_forward_remove(intr, serial, tunnel->local_port,
|
||||
SC_ADB_NO_STDOUT);
|
||||
} else {
|
||||
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||
SC_ADB_NO_STDOUT);
|
||||
|
||||
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
||||
if (!net_close(tunnel->server_socket)) {
|
||||
LOGW("Could not close server socket");
|
||||
}
|
||||
|
||||
// server_socket is never used anymore
|
||||
}
|
||||
|
||||
// Consider tunnel disabled even if the command failed
|
||||
tunnel->enabled = false;
|
||||
|
||||
return ret;
|
||||
}
|
47
app/src/adb_tunnel.h
Normal file
47
app/src/adb_tunnel.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef SC_ADB_TUNNEL_H
|
||||
#define SC_ADB_TUNNEL_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "options.h"
|
||||
#include "util/intr.h"
|
||||
#include "util/net.h"
|
||||
|
||||
struct sc_adb_tunnel {
|
||||
bool enabled;
|
||||
bool forward; // use "adb forward" instead of "adb reverse"
|
||||
sc_socket server_socket; // only used if !forward
|
||||
uint16_t local_port;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the adb tunnel struct to default values
|
||||
*/
|
||||
void
|
||||
sc_adb_tunnel_init(struct sc_adb_tunnel *tunnel);
|
||||
|
||||
/**
|
||||
* Open a tunnel
|
||||
*
|
||||
* Blocking calls may be interrupted asynchronously via `intr`.
|
||||
*
|
||||
* If `force_adb_forward` is not set, then attempts to set up an "adb reverse"
|
||||
* tunnel first. Only if it fails (typical on old Android version connected via
|
||||
* TCP/IP), use "adb forward".
|
||||
*/
|
||||
bool
|
||||
sc_adb_tunnel_open(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
||||
const char *serial, struct sc_port_range port_range,
|
||||
bool force_adb_forward);
|
||||
|
||||
/**
|
||||
* Close the tunnel
|
||||
*/
|
||||
bool
|
||||
sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
||||
const char *serial);
|
||||
|
||||
#endif
|
398
app/src/aoa_hid.c
Normal file
398
app/src/aoa_hid.c
Normal file
@ -0,0 +1,398 @@
|
||||
#include "util/log.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "aoa_hid.h"
|
||||
#include "util/log.h"
|
||||
|
||||
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
|
||||
#define ACCESSORY_REGISTER_HID 54
|
||||
#define ACCESSORY_SET_HID_REPORT_DESC 56
|
||||
#define ACCESSORY_SEND_HID_EVENT 57
|
||||
#define ACCESSORY_UNREGISTER_HID 55
|
||||
|
||||
#define DEFAULT_TIMEOUT 1000
|
||||
|
||||
static void
|
||||
sc_hid_event_log(const struct sc_hid_event *event) {
|
||||
// HID Event: [00] FF FF FF FF...
|
||||
assert(event->size);
|
||||
unsigned buffer_size = event->size * 3 + 1;
|
||||
char *buffer = malloc(buffer_size);
|
||||
if (!buffer) {
|
||||
LOG_OOM();
|
||||
return;
|
||||
}
|
||||
for (unsigned i = 0; i < event->size; ++i) {
|
||||
snprintf(buffer + i * 3, 4, " %02x", event->buffer[i]);
|
||||
}
|
||||
LOGV("HID Event: [%d]%s", event->accessory_id, buffer);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void
|
||||
sc_hid_event_init(struct sc_hid_event *hid_event, uint16_t accessory_id,
|
||||
unsigned char *buffer, uint16_t buffer_size) {
|
||||
hid_event->accessory_id = accessory_id;
|
||||
hid_event->buffer = buffer;
|
||||
hid_event->size = buffer_size;
|
||||
hid_event->ack_to_wait = SC_SEQUENCE_INVALID;
|
||||
}
|
||||
|
||||
void
|
||||
sc_hid_event_destroy(struct sc_hid_event *hid_event) {
|
||||
free(hid_event->buffer);
|
||||
}
|
||||
|
||||
static inline void
|
||||
log_libusb_error(enum libusb_error errcode) {
|
||||
LOGW("libusb error: %s", libusb_strerror(errcode));
|
||||
}
|
||||
|
||||
static bool
|
||||
accept_device(libusb_device *device, const char *serial) {
|
||||
// do not log any USB error in this function, it is expected that many USB
|
||||
// devices available on the computer have permission restrictions
|
||||
|
||||
struct libusb_device_descriptor desc;
|
||||
libusb_get_device_descriptor(device, &desc);
|
||||
|
||||
if (!desc.iSerialNumber) {
|
||||
return false;
|
||||
}
|
||||
|
||||
libusb_device_handle *handle;
|
||||
int result = libusb_open(device, &handle);
|
||||
if (result < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[128];
|
||||
result = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
|
||||
(unsigned char *) buffer,
|
||||
sizeof(buffer));
|
||||
libusb_close(handle);
|
||||
if (result < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer[sizeof(buffer) - 1] = '\0'; // just in case
|
||||
|
||||
// accept the device if its serial matches
|
||||
return !strcmp(buffer, serial);
|
||||
}
|
||||
|
||||
static libusb_device *
|
||||
sc_aoa_find_usb_device(const char *serial) {
|
||||
if (!serial) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
libusb_device **list;
|
||||
libusb_device *result = NULL;
|
||||
ssize_t count = libusb_get_device_list(NULL, &list);
|
||||
if (count < 0) {
|
||||
log_libusb_error((enum libusb_error) count);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < (size_t) count; ++i) {
|
||||
libusb_device *device = list[i];
|
||||
|
||||
if (accept_device(device, serial)) {
|
||||
result = libusb_ref_device(device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
libusb_free_device_list(list, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int
|
||||
sc_aoa_open_usb_handle(libusb_device *device, libusb_device_handle **handle) {
|
||||
int result = libusb_open(device, handle);
|
||||
if (result < 0) {
|
||||
log_libusb_error((enum libusb_error) result);
|
||||
return result;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_aoa_init(struct sc_aoa *aoa, const char *serial,
|
||||
struct sc_acksync *acksync) {
|
||||
assert(acksync);
|
||||
|
||||
cbuf_init(&aoa->queue);
|
||||
|
||||
if (!sc_mutex_init(&aoa->mutex)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sc_cond_init(&aoa->event_cond)) {
|
||||
sc_mutex_destroy(&aoa->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (libusb_init(&aoa->usb_context) != LIBUSB_SUCCESS) {
|
||||
sc_cond_destroy(&aoa->event_cond);
|
||||
sc_mutex_destroy(&aoa->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
aoa->usb_device = sc_aoa_find_usb_device(serial);
|
||||
if (!aoa->usb_device) {
|
||||
LOGW("USB device of serial %s not found", serial);
|
||||
libusb_exit(aoa->usb_context);
|
||||
sc_mutex_destroy(&aoa->mutex);
|
||||
sc_cond_destroy(&aoa->event_cond);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sc_aoa_open_usb_handle(aoa->usb_device, &aoa->usb_handle) < 0) {
|
||||
LOGW("Open USB handle failed");
|
||||
libusb_unref_device(aoa->usb_device);
|
||||
libusb_exit(aoa->usb_context);
|
||||
sc_cond_destroy(&aoa->event_cond);
|
||||
sc_mutex_destroy(&aoa->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
aoa->stopped = false;
|
||||
aoa->acksync = acksync;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_aoa_destroy(struct sc_aoa *aoa) {
|
||||
// Destroy remaining events
|
||||
struct sc_hid_event event;
|
||||
while (cbuf_take(&aoa->queue, &event)) {
|
||||
sc_hid_event_destroy(&event);
|
||||
}
|
||||
|
||||
libusb_close(aoa->usb_handle);
|
||||
libusb_unref_device(aoa->usb_device);
|
||||
libusb_exit(aoa->usb_context);
|
||||
sc_cond_destroy(&aoa->event_cond);
|
||||
sc_mutex_destroy(&aoa->mutex);
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_aoa_register_hid(struct sc_aoa *aoa, uint16_t accessory_id,
|
||||
uint16_t report_desc_size) {
|
||||
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR;
|
||||
uint8_t request = ACCESSORY_REGISTER_HID;
|
||||
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
|
||||
// value (arg0): accessory assigned ID for the HID device
|
||||
// index (arg1): total length of the HID report descriptor
|
||||
uint16_t value = accessory_id;
|
||||
uint16_t index = report_desc_size;
|
||||
unsigned char *buffer = NULL;
|
||||
uint16_t length = 0;
|
||||
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
|
||||
value, index, buffer, length,
|
||||
DEFAULT_TIMEOUT);
|
||||
if (result < 0) {
|
||||
log_libusb_error((enum libusb_error) result);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_aoa_set_hid_report_desc(struct sc_aoa *aoa, uint16_t accessory_id,
|
||||
const unsigned char *report_desc,
|
||||
uint16_t report_desc_size) {
|
||||
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR;
|
||||
uint8_t request = ACCESSORY_SET_HID_REPORT_DESC;
|
||||
/**
|
||||
* If the HID descriptor is longer than the endpoint zero max packet size,
|
||||
* the descriptor will be sent in multiple ACCESSORY_SET_HID_REPORT_DESC
|
||||
* commands. The data for the descriptor must be sent sequentially
|
||||
* if multiple packets are needed.
|
||||
* <https://source.android.com/devices/accessories/aoa2.html#hid-support>
|
||||
*
|
||||
* libusb handles packet abstraction internally, so we don't need to care
|
||||
* about bMaxPacketSize0 here.
|
||||
*
|
||||
* See <https://libusb.sourceforge.io/api-1.0/libusb_packetoverflow.html>
|
||||
*/
|
||||
// value (arg0): accessory assigned ID for the HID device
|
||||
// index (arg1): offset of data (buffer) in descriptor
|
||||
uint16_t value = accessory_id;
|
||||
uint16_t index = 0;
|
||||
// libusb_control_transfer expects a pointer to non-const
|
||||
unsigned char *buffer = (unsigned char *) report_desc;
|
||||
uint16_t length = report_desc_size;
|
||||
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
|
||||
value, index, buffer, length,
|
||||
DEFAULT_TIMEOUT);
|
||||
if (result < 0) {
|
||||
log_libusb_error((enum libusb_error) result);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_aoa_setup_hid(struct sc_aoa *aoa, uint16_t accessory_id,
|
||||
const unsigned char *report_desc, uint16_t report_desc_size) {
|
||||
bool ok = sc_aoa_register_hid(aoa, accessory_id, report_desc_size);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_aoa_set_hid_report_desc(aoa, accessory_id, report_desc,
|
||||
report_desc_size);
|
||||
if (!ok) {
|
||||
if (!sc_aoa_unregister_hid(aoa, accessory_id)) {
|
||||
LOGW("Could not unregister HID");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
|
||||
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR;
|
||||
uint8_t request = ACCESSORY_SEND_HID_EVENT;
|
||||
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
|
||||
// value (arg0): accessory assigned ID for the HID device
|
||||
// index (arg1): 0 (unused)
|
||||
uint16_t value = event->accessory_id;
|
||||
uint16_t index = 0;
|
||||
unsigned char *buffer = event->buffer;
|
||||
uint16_t length = event->size;
|
||||
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
|
||||
value, index, buffer, length,
|
||||
DEFAULT_TIMEOUT);
|
||||
if (result < 0) {
|
||||
log_libusb_error((enum libusb_error) result);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_aoa_unregister_hid(struct sc_aoa *aoa, const uint16_t accessory_id) {
|
||||
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR;
|
||||
uint8_t request = ACCESSORY_UNREGISTER_HID;
|
||||
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
|
||||
// value (arg0): accessory assigned ID for the HID device
|
||||
// index (arg1): 0
|
||||
uint16_t value = accessory_id;
|
||||
uint16_t index = 0;
|
||||
unsigned char *buffer = NULL;
|
||||
uint16_t length = 0;
|
||||
int result = libusb_control_transfer(aoa->usb_handle, request_type, request,
|
||||
value, index, buffer, length,
|
||||
DEFAULT_TIMEOUT);
|
||||
if (result < 0) {
|
||||
log_libusb_error((enum libusb_error) result);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_aoa_push_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
|
||||
if (sc_get_log_level() <= SC_LOG_LEVEL_VERBOSE) {
|
||||
sc_hid_event_log(event);
|
||||
}
|
||||
|
||||
sc_mutex_lock(&aoa->mutex);
|
||||
bool was_empty = cbuf_is_empty(&aoa->queue);
|
||||
bool res = cbuf_push(&aoa->queue, *event);
|
||||
if (was_empty) {
|
||||
sc_cond_signal(&aoa->event_cond);
|
||||
}
|
||||
sc_mutex_unlock(&aoa->mutex);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int
|
||||
run_aoa_thread(void *data) {
|
||||
struct sc_aoa *aoa = data;
|
||||
|
||||
for (;;) {
|
||||
sc_mutex_lock(&aoa->mutex);
|
||||
while (!aoa->stopped && cbuf_is_empty(&aoa->queue)) {
|
||||
sc_cond_wait(&aoa->event_cond, &aoa->mutex);
|
||||
}
|
||||
if (aoa->stopped) {
|
||||
// Stop immediately, do not process further events
|
||||
sc_mutex_unlock(&aoa->mutex);
|
||||
break;
|
||||
}
|
||||
struct sc_hid_event event;
|
||||
bool non_empty = cbuf_take(&aoa->queue, &event);
|
||||
assert(non_empty);
|
||||
(void) non_empty;
|
||||
|
||||
uint64_t ack_to_wait = event.ack_to_wait;
|
||||
sc_mutex_unlock(&aoa->mutex);
|
||||
|
||||
if (ack_to_wait != SC_SEQUENCE_INVALID) {
|
||||
LOGD("Waiting ack from server sequence=%" PRIu64_, ack_to_wait);
|
||||
// Do not block the loop indefinitely if the ack never comes (it should
|
||||
// never happen)
|
||||
sc_tick deadline = sc_tick_now() + SC_TICK_FROM_MS(500);
|
||||
enum sc_acksync_wait_result result =
|
||||
sc_acksync_wait(aoa->acksync, ack_to_wait, deadline);
|
||||
|
||||
if (result == SC_ACKSYNC_WAIT_TIMEOUT) {
|
||||
LOGW("Ack not received after 500ms, discarding HID event");
|
||||
sc_hid_event_destroy(&event);
|
||||
continue;
|
||||
} else if (result == SC_ACKSYNC_WAIT_INTR) {
|
||||
// stopped
|
||||
sc_hid_event_destroy(&event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool ok = sc_aoa_send_hid_event(aoa, &event);
|
||||
sc_hid_event_destroy(&event);
|
||||
if (!ok) {
|
||||
LOGW("Could not send HID event to USB device");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_aoa_start(struct sc_aoa *aoa) {
|
||||
LOGD("Starting AOA thread");
|
||||
|
||||
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "scrcpy-aoa", aoa);
|
||||
if (!ok) {
|
||||
LOGC("Could not start AOA thread");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_aoa_stop(struct sc_aoa *aoa) {
|
||||
sc_mutex_lock(&aoa->mutex);
|
||||
aoa->stopped = true;
|
||||
sc_cond_signal(&aoa->event_cond);
|
||||
sc_mutex_unlock(&aoa->mutex);
|
||||
|
||||
sc_acksync_interrupt(aoa->acksync);
|
||||
}
|
||||
|
||||
void
|
||||
sc_aoa_join(struct sc_aoa *aoa) {
|
||||
sc_thread_join(&aoa->thread, NULL);
|
||||
}
|
69
app/src/aoa_hid.h
Normal file
69
app/src/aoa_hid.h
Normal file
@ -0,0 +1,69 @@
|
||||
#ifndef SC_AOA_HID_H
|
||||
#define SC_AOA_HID_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
|
||||
#include "util/acksync.h"
|
||||
#include "util/cbuf.h"
|
||||
#include "util/thread.h"
|
||||
#include "util/tick.h"
|
||||
|
||||
struct sc_hid_event {
|
||||
uint16_t accessory_id;
|
||||
unsigned char *buffer;
|
||||
uint16_t size;
|
||||
uint64_t ack_to_wait;
|
||||
};
|
||||
|
||||
// Takes ownership of buffer
|
||||
void
|
||||
sc_hid_event_init(struct sc_hid_event *hid_event, uint16_t accessory_id,
|
||||
unsigned char *buffer, uint16_t buffer_size);
|
||||
|
||||
void
|
||||
sc_hid_event_destroy(struct sc_hid_event *hid_event);
|
||||
|
||||
struct sc_hid_event_queue CBUF(struct sc_hid_event, 64);
|
||||
|
||||
struct sc_aoa {
|
||||
libusb_context *usb_context;
|
||||
libusb_device *usb_device;
|
||||
libusb_device_handle *usb_handle;
|
||||
sc_thread thread;
|
||||
sc_mutex mutex;
|
||||
sc_cond event_cond;
|
||||
bool stopped;
|
||||
struct sc_hid_event_queue queue;
|
||||
|
||||
struct sc_acksync *acksync;
|
||||
};
|
||||
|
||||
bool
|
||||
sc_aoa_init(struct sc_aoa *aoa, const char *serial, struct sc_acksync *acksync);
|
||||
|
||||
void
|
||||
sc_aoa_destroy(struct sc_aoa *aoa);
|
||||
|
||||
bool
|
||||
sc_aoa_start(struct sc_aoa *aoa);
|
||||
|
||||
void
|
||||
sc_aoa_stop(struct sc_aoa *aoa);
|
||||
|
||||
void
|
||||
sc_aoa_join(struct sc_aoa *aoa);
|
||||
|
||||
bool
|
||||
sc_aoa_setup_hid(struct sc_aoa *aoa, uint16_t accessory_id,
|
||||
const unsigned char *report_desc, uint16_t report_desc_size);
|
||||
|
||||
bool
|
||||
sc_aoa_unregister_hid(struct sc_aoa *aoa, uint16_t accessory_id);
|
||||
|
||||
bool
|
||||
sc_aoa_push_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event);
|
||||
|
||||
#endif
|
1382
app/src/cli.c
1382
app/src/cli.c
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "scrcpy.h"
|
||||
#include "options.h"
|
||||
|
||||
struct scrcpy_cli_args {
|
||||
struct scrcpy_options opts;
|
||||
|
@ -26,7 +26,7 @@ struct sc_clock_point {
|
||||
* array.
|
||||
*
|
||||
* To estimate the slope, it splits the last SC_CLOCK_RANGE points into two
|
||||
* sets of SC_CLOCK_RANGE/2 points, and compute their centroid ("average
|
||||
* sets of SC_CLOCK_RANGE/2 points, and computes their centroid ("average
|
||||
* point"). The slope of the estimated affine function is that of the line
|
||||
* passing through these two points.
|
||||
*
|
||||
|
@ -2,6 +2,12 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef HAVE_STRDUP
|
||||
char *strdup(const char *s) {
|
||||
size_t size = strlen(s) + 1;
|
||||
@ -12,3 +18,36 @@ char *strdup(const char *s) {
|
||||
return dup;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_ASPRINTF
|
||||
int asprintf(char **strp, const char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
int ret = vasprintf(strp, fmt, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_VASPRINTF
|
||||
int vasprintf(char **strp, const char *fmt, va_list ap) {
|
||||
va_list va;
|
||||
va_copy(va, ap);
|
||||
int len = vsnprintf(NULL, 0, fmt, va);
|
||||
va_end(va);
|
||||
|
||||
char *str = malloc(len + 1);
|
||||
if (!str) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_copy(va, ap);
|
||||
int len2 = vsnprintf(str, len + 1, fmt, va);
|
||||
(void) len2;
|
||||
assert(len == len2);
|
||||
va_end(va);
|
||||
|
||||
*strp = str;
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,16 +1,17 @@
|
||||
#ifndef COMPAT_H
|
||||
#define COMPAT_H
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define _XOPEN_SOURCE 700
|
||||
#define _GNU_SOURCE
|
||||
#ifdef __APPLE__
|
||||
# define _DARWIN_C_SOURCE
|
||||
#endif
|
||||
#include "config.h"
|
||||
|
||||
#include <libavformat/version.h>
|
||||
#include <SDL2/SDL_version.h>
|
||||
|
||||
#ifndef __WIN32
|
||||
# define PRIu64_ PRIu64
|
||||
#else
|
||||
# define PRIu64_ "I64u" // Windows...
|
||||
#endif
|
||||
|
||||
// In ffmpeg/doc/APIchanges:
|
||||
// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
|
||||
// Deprecate use of av_register_input_format(), av_register_output_format(),
|
||||
@ -34,13 +35,9 @@
|
||||
# define SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 5)
|
||||
// <https://wiki.libsdl.org/SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH>
|
||||
# define SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
||||
// <https://wiki.libsdl.org/SDL_GetDisplayUsableBounds>
|
||||
# define SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS
|
||||
// <https://wiki.libsdl.org/SDL_WindowFlags>
|
||||
# define SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 6)
|
||||
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
|
||||
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 8)
|
||||
@ -52,4 +49,12 @@
|
||||
char *strdup(const char *s);
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_ASPRINTF
|
||||
int asprintf(char **strp, const char *fmt, ...);
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_VASPRINTF
|
||||
int vasprintf(char **strp, const char *fmt, va_list ap);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#include "util/buffer_util.h"
|
||||
#include "util/log.h"
|
||||
#include "util/str_util.h"
|
||||
#include "util/str.h"
|
||||
|
||||
/**
|
||||
* Map an enum value to a string based on an array, without crashing on an
|
||||
@ -41,7 +41,7 @@ static const char *const android_motionevent_action_labels[] = {
|
||||
"pointer-up",
|
||||
"hover-move",
|
||||
"scroll",
|
||||
"hover-enter"
|
||||
"hover-enter",
|
||||
"hover-exit",
|
||||
"btn-press",
|
||||
"btn-release",
|
||||
@ -55,18 +55,24 @@ static const char *const screen_power_mode_labels[] = {
|
||||
"suspend",
|
||||
};
|
||||
|
||||
static const char *const copy_key_labels[] = {
|
||||
"none",
|
||||
"copy",
|
||||
"cut",
|
||||
};
|
||||
|
||||
static void
|
||||
write_position(uint8_t *buf, const struct position *position) {
|
||||
write_position(uint8_t *buf, const struct sc_position *position) {
|
||||
buffer_write32be(&buf[0], position->point.x);
|
||||
buffer_write32be(&buf[4], position->point.y);
|
||||
buffer_write16be(&buf[8], position->screen_size.width);
|
||||
buffer_write16be(&buf[10], position->screen_size.height);
|
||||
}
|
||||
|
||||
// write length (2 bytes) + string (non nul-terminated)
|
||||
// write length (4 bytes) + string (non null-terminated)
|
||||
static size_t
|
||||
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
||||
size_t len = utf8_truncation_index(utf8, max_len);
|
||||
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
||||
buffer_write32be(buf, len);
|
||||
memcpy(&buf[4], utf8, len);
|
||||
return 4 + len;
|
||||
@ -117,12 +123,16 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
||||
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
||||
buf[1] = msg->inject_keycode.action;
|
||||
return 2;
|
||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
||||
buf[1] = msg->get_clipboard.copy_key;
|
||||
return 2;
|
||||
case CONTROL_MSG_TYPE_SET_CLIPBOARD: {
|
||||
buf[1] = !!msg->set_clipboard.paste;
|
||||
buffer_write64be(&buf[1], msg->set_clipboard.sequence);
|
||||
buf[9] = !!msg->set_clipboard.paste;
|
||||
size_t len = write_string(msg->set_clipboard.text,
|
||||
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
||||
&buf[2]);
|
||||
return 2 + len;
|
||||
&buf[10]);
|
||||
return 10 + len;
|
||||
}
|
||||
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
||||
buf[1] = msg->set_screen_power_mode.mode;
|
||||
@ -130,7 +140,6 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
||||
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
|
||||
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
||||
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
||||
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
||||
// no additional data
|
||||
return 1;
|
||||
@ -170,11 +179,6 @@ control_msg_log(const struct control_msg *msg) {
|
||||
(long) msg->inject_touch_event.buttons);
|
||||
} else {
|
||||
// numeric pointer id
|
||||
#ifndef __WIN32
|
||||
# define PRIu64_ PRIu64
|
||||
#else
|
||||
# define PRIu64_ "I64u" // Windows...
|
||||
#endif
|
||||
LOG_CMSG("touch [id=%" PRIu64_ "] %-4s position=%" PRIi32 ",%"
|
||||
PRIi32 " pressure=%g buttons=%06lx",
|
||||
id,
|
||||
@ -198,9 +202,14 @@ control_msg_log(const struct control_msg *msg) {
|
||||
LOG_CMSG("back-or-screen-on %s",
|
||||
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
|
||||
break;
|
||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
||||
LOG_CMSG("get clipboard copy_key=%s",
|
||||
copy_key_labels[msg->get_clipboard.copy_key]);
|
||||
break;
|
||||
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
||||
LOG_CMSG("clipboard %s \"%s\"",
|
||||
msg->set_clipboard.paste ? "paste" : "copy",
|
||||
LOG_CMSG("clipboard %" PRIu64_ " %s \"%s\"",
|
||||
msg->set_clipboard.sequence,
|
||||
msg->set_clipboard.paste ? "paste" : "nopaste",
|
||||
msg->set_clipboard.text);
|
||||
break;
|
||||
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
||||
@ -216,9 +225,6 @@ control_msg_log(const struct control_msg *msg) {
|
||||
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
||||
LOG_CMSG("collapse panels");
|
||||
break;
|
||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
||||
LOG_CMSG("get clipboard");
|
||||
break;
|
||||
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
||||
LOG_CMSG("rotate device");
|
||||
break;
|
||||
|
@ -14,8 +14,8 @@
|
||||
#define CONTROL_MSG_MAX_SIZE (1 << 18) // 256k
|
||||
|
||||
#define CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300
|
||||
// type: 1 byte; paste flag: 1 byte; length: 4 bytes
|
||||
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (CONTROL_MSG_MAX_SIZE - 6)
|
||||
// type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
|
||||
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (CONTROL_MSG_MAX_SIZE - 14)
|
||||
|
||||
#define POINTER_ID_MOUSE UINT64_C(-1)
|
||||
#define POINTER_ID_VIRTUAL_FINGER UINT64_C(-2)
|
||||
@ -41,6 +41,12 @@ enum screen_power_mode {
|
||||
SCREEN_POWER_MODE_NORMAL = 2,
|
||||
};
|
||||
|
||||
enum get_clipboard_copy_key {
|
||||
GET_CLIPBOARD_COPY_KEY_NONE,
|
||||
GET_CLIPBOARD_COPY_KEY_COPY,
|
||||
GET_CLIPBOARD_COPY_KEY_CUT,
|
||||
};
|
||||
|
||||
struct control_msg {
|
||||
enum control_msg_type type;
|
||||
union {
|
||||
@ -57,11 +63,11 @@ struct control_msg {
|
||||
enum android_motionevent_action action;
|
||||
enum android_motionevent_buttons buttons;
|
||||
uint64_t pointer_id;
|
||||
struct position position;
|
||||
struct sc_position position;
|
||||
float pressure;
|
||||
} inject_touch_event;
|
||||
struct {
|
||||
struct position position;
|
||||
struct sc_position position;
|
||||
int32_t hscroll;
|
||||
int32_t vscroll;
|
||||
} inject_scroll_event;
|
||||
@ -70,6 +76,10 @@ struct control_msg {
|
||||
// screen may only be turned on on ACTION_DOWN
|
||||
} back_or_screen_on;
|
||||
struct {
|
||||
enum get_clipboard_copy_key copy_key;
|
||||
} get_clipboard;
|
||||
struct {
|
||||
uint64_t sequence;
|
||||
char *text; // owned, to be freed by free()
|
||||
bool paste;
|
||||
} set_clipboard;
|
||||
|
@ -5,10 +5,11 @@
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
controller_init(struct controller *controller, socket_t control_socket) {
|
||||
controller_init(struct controller *controller, sc_socket control_socket,
|
||||
struct sc_acksync *acksync) {
|
||||
cbuf_init(&controller->queue);
|
||||
|
||||
bool ok = receiver_init(&controller->receiver, control_socket);
|
||||
bool ok = receiver_init(&controller->receiver, control_socket, acksync);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
@ -47,7 +48,7 @@ controller_destroy(struct controller *controller) {
|
||||
|
||||
bool
|
||||
controller_push_msg(struct controller *controller,
|
||||
const struct control_msg *msg) {
|
||||
const struct control_msg *msg) {
|
||||
if (sc_get_log_level() <= SC_LOG_LEVEL_VERBOSE) {
|
||||
control_msg_log(msg);
|
||||
}
|
||||
@ -63,14 +64,14 @@ controller_push_msg(struct controller *controller,
|
||||
}
|
||||
|
||||
static bool
|
||||
process_msg(struct controller *controller,
|
||||
const struct control_msg *msg) {
|
||||
process_msg(struct controller *controller, const struct control_msg *msg) {
|
||||
static unsigned char serialized_msg[CONTROL_MSG_MAX_SIZE];
|
||||
size_t length = control_msg_serialize(msg, serialized_msg);
|
||||
if (!length) {
|
||||
return false;
|
||||
}
|
||||
ssize_t w = net_send_all(controller->control_socket, serialized_msg, length);
|
||||
ssize_t w =
|
||||
net_send_all(controller->control_socket, serialized_msg, length);
|
||||
return (size_t) w == length;
|
||||
}
|
||||
|
||||
@ -109,7 +110,7 @@ controller_start(struct controller *controller) {
|
||||
LOGD("Starting controller thread");
|
||||
|
||||
bool ok = sc_thread_create(&controller->thread, run_controller,
|
||||
"controller", controller);
|
||||
"scrcpy-ctl", controller);
|
||||
if (!ok) {
|
||||
LOGC("Could not start controller thread");
|
||||
return false;
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "control_msg.h"
|
||||
#include "receiver.h"
|
||||
#include "util/acksync.h"
|
||||
#include "util/cbuf.h"
|
||||
#include "util/net.h"
|
||||
#include "util/thread.h"
|
||||
@ -14,7 +15,7 @@
|
||||
struct control_msg_queue CBUF(struct control_msg, 64);
|
||||
|
||||
struct controller {
|
||||
socket_t control_socket;
|
||||
sc_socket control_socket;
|
||||
sc_thread thread;
|
||||
sc_mutex mutex;
|
||||
sc_cond msg_cond;
|
||||
@ -24,7 +25,8 @@ struct controller {
|
||||
};
|
||||
|
||||
bool
|
||||
controller_init(struct controller *controller, socket_t control_socket);
|
||||
controller_init(struct controller *controller, sc_socket control_socket,
|
||||
struct sc_acksync *acksync);
|
||||
|
||||
void
|
||||
controller_destroy(struct controller *controller);
|
||||
|
@ -3,22 +3,22 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct size {
|
||||
struct sc_size {
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
};
|
||||
|
||||
struct point {
|
||||
struct sc_point {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
|
||||
struct position {
|
||||
struct sc_position {
|
||||
// The video screen size may be different from the real device screen size,
|
||||
// so store to which size the absolute position apply, to scale it
|
||||
// accordingly.
|
||||
struct size screen_size;
|
||||
struct point point;
|
||||
struct sc_size screen_size;
|
||||
struct sc_point point;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "decoder.h"
|
||||
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
#include "events.h"
|
||||
@ -41,10 +42,12 @@ static bool
|
||||
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
||||
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
||||
if (!decoder->codec_ctx) {
|
||||
LOGC("Could not allocate decoder context");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
|
||||
|
||||
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
|
||||
LOGE("Could not open codec");
|
||||
avcodec_free_context(&decoder->codec_ctx);
|
||||
@ -53,7 +56,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
||||
|
||||
decoder->frame = av_frame_alloc();
|
||||
if (!decoder->frame) {
|
||||
LOGE("Could not create decoder frame");
|
||||
LOG_OOM();
|
||||
avcodec_close(decoder->codec_ctx);
|
||||
avcodec_free_context(&decoder->codec_ctx);
|
||||
return false;
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "device_msg.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -23,7 +24,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
||||
}
|
||||
char *text = malloc(clipboard_len + 1);
|
||||
if (!text) {
|
||||
LOGW("Could not allocate text for clipboard");
|
||||
LOG_OOM();
|
||||
return -1;
|
||||
}
|
||||
if (clipboard_len) {
|
||||
@ -34,6 +35,11 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
||||
msg->clipboard.text = text;
|
||||
return 5 + clipboard_len;
|
||||
}
|
||||
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: {
|
||||
uint64_t sequence = buffer_read64be(&buf[1]);
|
||||
msg->ack_clipboard.sequence = sequence;
|
||||
return 9;
|
||||
}
|
||||
default:
|
||||
LOGW("Unknown device message type: %d", (int) msg->type);
|
||||
return -1; // error, we cannot recover
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
enum device_msg_type {
|
||||
DEVICE_MSG_TYPE_CLIPBOARD,
|
||||
DEVICE_MSG_TYPE_ACK_CLIPBOARD,
|
||||
};
|
||||
|
||||
struct device_msg {
|
||||
@ -21,6 +22,9 @@ struct device_msg {
|
||||
struct {
|
||||
char *text; // owned, to be freed by free()
|
||||
} clipboard;
|
||||
struct {
|
||||
uint64_t sequence;
|
||||
} ack_clipboard;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -1,195 +0,0 @@
|
||||
#include "event_converter.h"
|
||||
|
||||
#define MAP(FROM, TO) case FROM: *to = TO; return true
|
||||
#define FAIL default: return false
|
||||
|
||||
bool
|
||||
convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to) {
|
||||
switch (from) {
|
||||
MAP(SDL_KEYDOWN, AKEY_EVENT_ACTION_DOWN);
|
||||
MAP(SDL_KEYUP, AKEY_EVENT_ACTION_UP);
|
||||
FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
static enum android_metastate
|
||||
autocomplete_metastate(enum android_metastate metastate) {
|
||||
// fill dependent flags
|
||||
if (metastate & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
|
||||
metastate |= AMETA_SHIFT_ON;
|
||||
}
|
||||
if (metastate & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
|
||||
metastate |= AMETA_CTRL_ON;
|
||||
}
|
||||
if (metastate & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
|
||||
metastate |= AMETA_ALT_ON;
|
||||
}
|
||||
if (metastate & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
|
||||
metastate |= AMETA_META_ON;
|
||||
}
|
||||
|
||||
return metastate;
|
||||
}
|
||||
|
||||
enum android_metastate
|
||||
convert_meta_state(SDL_Keymod mod) {
|
||||
enum android_metastate metastate = 0;
|
||||
if (mod & KMOD_LSHIFT) {
|
||||
metastate |= AMETA_SHIFT_LEFT_ON;
|
||||
}
|
||||
if (mod & KMOD_RSHIFT) {
|
||||
metastate |= AMETA_SHIFT_RIGHT_ON;
|
||||
}
|
||||
if (mod & KMOD_LCTRL) {
|
||||
metastate |= AMETA_CTRL_LEFT_ON;
|
||||
}
|
||||
if (mod & KMOD_RCTRL) {
|
||||
metastate |= AMETA_CTRL_RIGHT_ON;
|
||||
}
|
||||
if (mod & KMOD_LALT) {
|
||||
metastate |= AMETA_ALT_LEFT_ON;
|
||||
}
|
||||
if (mod & KMOD_RALT) {
|
||||
metastate |= AMETA_ALT_RIGHT_ON;
|
||||
}
|
||||
if (mod & KMOD_LGUI) { // Windows key
|
||||
metastate |= AMETA_META_LEFT_ON;
|
||||
}
|
||||
if (mod & KMOD_RGUI) { // Windows key
|
||||
metastate |= AMETA_META_RIGHT_ON;
|
||||
}
|
||||
if (mod & KMOD_NUM) {
|
||||
metastate |= AMETA_NUM_LOCK_ON;
|
||||
}
|
||||
if (mod & KMOD_CAPS) {
|
||||
metastate |= AMETA_CAPS_LOCK_ON;
|
||||
}
|
||||
if (mod & KMOD_MODE) { // Alt Gr
|
||||
// no mapping?
|
||||
}
|
||||
|
||||
// fill the dependent fields
|
||||
return autocomplete_metastate(metastate);
|
||||
}
|
||||
|
||||
bool
|
||||
convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod,
|
||||
bool prefer_text) {
|
||||
switch (from) {
|
||||
MAP(SDLK_RETURN, AKEYCODE_ENTER);
|
||||
MAP(SDLK_KP_ENTER, AKEYCODE_NUMPAD_ENTER);
|
||||
MAP(SDLK_ESCAPE, AKEYCODE_ESCAPE);
|
||||
MAP(SDLK_BACKSPACE, AKEYCODE_DEL);
|
||||
MAP(SDLK_TAB, AKEYCODE_TAB);
|
||||
MAP(SDLK_PAGEUP, AKEYCODE_PAGE_UP);
|
||||
MAP(SDLK_DELETE, AKEYCODE_FORWARD_DEL);
|
||||
MAP(SDLK_HOME, AKEYCODE_MOVE_HOME);
|
||||
MAP(SDLK_END, AKEYCODE_MOVE_END);
|
||||
MAP(SDLK_PAGEDOWN, AKEYCODE_PAGE_DOWN);
|
||||
MAP(SDLK_RIGHT, AKEYCODE_DPAD_RIGHT);
|
||||
MAP(SDLK_LEFT, AKEYCODE_DPAD_LEFT);
|
||||
MAP(SDLK_DOWN, AKEYCODE_DPAD_DOWN);
|
||||
MAP(SDLK_UP, AKEYCODE_DPAD_UP);
|
||||
MAP(SDLK_LCTRL, AKEYCODE_CTRL_LEFT);
|
||||
MAP(SDLK_RCTRL, AKEYCODE_CTRL_RIGHT);
|
||||
MAP(SDLK_LSHIFT, AKEYCODE_SHIFT_LEFT);
|
||||
MAP(SDLK_RSHIFT, AKEYCODE_SHIFT_RIGHT);
|
||||
}
|
||||
|
||||
if (!(mod & (KMOD_NUM | KMOD_SHIFT))) {
|
||||
// Handle Numpad events when Num Lock is disabled
|
||||
// If SHIFT is pressed, a text event will be sent instead
|
||||
switch(from) {
|
||||
MAP(SDLK_KP_0, AKEYCODE_INSERT);
|
||||
MAP(SDLK_KP_1, AKEYCODE_MOVE_END);
|
||||
MAP(SDLK_KP_2, AKEYCODE_DPAD_DOWN);
|
||||
MAP(SDLK_KP_3, AKEYCODE_PAGE_DOWN);
|
||||
MAP(SDLK_KP_4, AKEYCODE_DPAD_LEFT);
|
||||
MAP(SDLK_KP_6, AKEYCODE_DPAD_RIGHT);
|
||||
MAP(SDLK_KP_7, AKEYCODE_MOVE_HOME);
|
||||
MAP(SDLK_KP_8, AKEYCODE_DPAD_UP);
|
||||
MAP(SDLK_KP_9, AKEYCODE_PAGE_UP);
|
||||
MAP(SDLK_KP_PERIOD, AKEYCODE_FORWARD_DEL);
|
||||
}
|
||||
}
|
||||
|
||||
if (prefer_text && !(mod & KMOD_CTRL)) {
|
||||
// do not forward alpha and space key events (unless Ctrl is pressed)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mod & (KMOD_LALT | KMOD_RALT | KMOD_LGUI | KMOD_RGUI)) {
|
||||
return false;
|
||||
}
|
||||
// if ALT and META are not pressed, also handle letters and space
|
||||
switch (from) {
|
||||
MAP(SDLK_a, AKEYCODE_A);
|
||||
MAP(SDLK_b, AKEYCODE_B);
|
||||
MAP(SDLK_c, AKEYCODE_C);
|
||||
MAP(SDLK_d, AKEYCODE_D);
|
||||
MAP(SDLK_e, AKEYCODE_E);
|
||||
MAP(SDLK_f, AKEYCODE_F);
|
||||
MAP(SDLK_g, AKEYCODE_G);
|
||||
MAP(SDLK_h, AKEYCODE_H);
|
||||
MAP(SDLK_i, AKEYCODE_I);
|
||||
MAP(SDLK_j, AKEYCODE_J);
|
||||
MAP(SDLK_k, AKEYCODE_K);
|
||||
MAP(SDLK_l, AKEYCODE_L);
|
||||
MAP(SDLK_m, AKEYCODE_M);
|
||||
MAP(SDLK_n, AKEYCODE_N);
|
||||
MAP(SDLK_o, AKEYCODE_O);
|
||||
MAP(SDLK_p, AKEYCODE_P);
|
||||
MAP(SDLK_q, AKEYCODE_Q);
|
||||
MAP(SDLK_r, AKEYCODE_R);
|
||||
MAP(SDLK_s, AKEYCODE_S);
|
||||
MAP(SDLK_t, AKEYCODE_T);
|
||||
MAP(SDLK_u, AKEYCODE_U);
|
||||
MAP(SDLK_v, AKEYCODE_V);
|
||||
MAP(SDLK_w, AKEYCODE_W);
|
||||
MAP(SDLK_x, AKEYCODE_X);
|
||||
MAP(SDLK_y, AKEYCODE_Y);
|
||||
MAP(SDLK_z, AKEYCODE_Z);
|
||||
MAP(SDLK_SPACE, AKEYCODE_SPACE);
|
||||
FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
enum android_motionevent_buttons
|
||||
convert_mouse_buttons(uint32_t state) {
|
||||
enum android_motionevent_buttons buttons = 0;
|
||||
if (state & SDL_BUTTON_LMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_PRIMARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_RMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_SECONDARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_MMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_TERTIARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_X1MASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_BACK;
|
||||
}
|
||||
if (state & SDL_BUTTON_X2MASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_FORWARD;
|
||||
}
|
||||
return buttons;
|
||||
}
|
||||
|
||||
bool
|
||||
convert_mouse_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||
switch (from) {
|
||||
MAP(SDL_MOUSEBUTTONDOWN, AMOTION_EVENT_ACTION_DOWN);
|
||||
MAP(SDL_MOUSEBUTTONUP, AMOTION_EVENT_ACTION_UP);
|
||||
FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||
switch (from) {
|
||||
MAP(SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE);
|
||||
MAP(SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN);
|
||||
MAP(SDL_FINGERUP, AMOTION_EVENT_ACTION_UP);
|
||||
FAIL;
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
#ifndef CONVERT_H
|
||||
#define CONVERT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <SDL2/SDL_events.h>
|
||||
|
||||
#include "control_msg.h"
|
||||
|
||||
bool
|
||||
convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to);
|
||||
|
||||
enum android_metastate
|
||||
convert_meta_state(SDL_Keymod mod);
|
||||
|
||||
bool
|
||||
convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod,
|
||||
bool prefer_text);
|
||||
|
||||
enum android_motionevent_buttons
|
||||
convert_mouse_buttons(uint32_t state);
|
||||
|
||||
bool
|
||||
convert_mouse_action(SDL_EventType from, enum android_motionevent_action *to);
|
||||
|
||||
bool
|
||||
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to);
|
||||
|
||||
#endif
|
@ -1,2 +1,4 @@
|
||||
#define EVENT_NEW_FRAME SDL_USEREVENT
|
||||
#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1)
|
||||
#define EVENT_NEW_FRAME SDL_USEREVENT
|
||||
#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1)
|
||||
#define EVENT_SERVER_CONNECTION_FAILED (SDL_USEREVENT + 2)
|
||||
#define EVENT_SERVER_CONNECTED (SDL_USEREVENT + 3)
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "adb.h"
|
||||
#include "util/log.h"
|
||||
#include "util/process_intr.h"
|
||||
|
||||
#define DEFAULT_PUSH_TARGET "/sdcard/Download/"
|
||||
|
||||
@ -16,6 +17,7 @@ file_handler_request_destroy(struct file_handler_request *req) {
|
||||
bool
|
||||
file_handler_init(struct file_handler *file_handler, const char *serial,
|
||||
const char *push_target) {
|
||||
assert(serial);
|
||||
|
||||
cbuf_init(&file_handler->queue);
|
||||
|
||||
@ -30,23 +32,26 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (serial) {
|
||||
file_handler->serial = strdup(serial);
|
||||
if (!file_handler->serial) {
|
||||
LOGW("Could not strdup serial");
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
file_handler->serial = NULL;
|
||||
ok = sc_intr_init(&file_handler->intr);
|
||||
if (!ok) {
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
file_handler->serial = strdup(serial);
|
||||
if (!file_handler->serial) {
|
||||
LOG_OOM();
|
||||
sc_intr_destroy(&file_handler->intr);
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
// lazy initialization
|
||||
file_handler->initialized = false;
|
||||
|
||||
file_handler->stopped = false;
|
||||
file_handler->current_process = PROCESS_NONE;
|
||||
|
||||
file_handler->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
|
||||
|
||||
@ -57,6 +62,7 @@ void
|
||||
file_handler_destroy(struct file_handler *file_handler) {
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
sc_intr_destroy(&file_handler->intr);
|
||||
free(file_handler->serial);
|
||||
|
||||
struct file_handler_request req;
|
||||
@ -65,16 +71,6 @@ file_handler_destroy(struct file_handler *file_handler) {
|
||||
}
|
||||
}
|
||||
|
||||
static process_t
|
||||
install_apk(const char *serial, const char *file) {
|
||||
return adb_install(serial, file);
|
||||
}
|
||||
|
||||
static process_t
|
||||
push_file(const char *serial, const char *file, const char *push_target) {
|
||||
return adb_push(serial, file, push_target);
|
||||
}
|
||||
|
||||
bool
|
||||
file_handler_request(struct file_handler *file_handler,
|
||||
file_handler_action_t action, char *file) {
|
||||
@ -106,10 +102,16 @@ file_handler_request(struct file_handler *file_handler,
|
||||
static int
|
||||
run_file_handler(void *data) {
|
||||
struct file_handler *file_handler = data;
|
||||
struct sc_intr *intr = &file_handler->intr;
|
||||
|
||||
const char *serial = file_handler->serial;
|
||||
assert(serial);
|
||||
|
||||
const char *push_target = file_handler->push_target;
|
||||
assert(push_target);
|
||||
|
||||
for (;;) {
|
||||
sc_mutex_lock(&file_handler->mutex);
|
||||
file_handler->current_process = PROCESS_NONE;
|
||||
while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
|
||||
sc_cond_wait(&file_handler->event_cond, &file_handler->mutex);
|
||||
}
|
||||
@ -122,43 +124,26 @@ run_file_handler(void *data) {
|
||||
bool non_empty = cbuf_take(&file_handler->queue, &req);
|
||||
assert(non_empty);
|
||||
(void) non_empty;
|
||||
|
||||
process_t process;
|
||||
if (req.action == ACTION_INSTALL_APK) {
|
||||
LOGI("Installing %s...", req.file);
|
||||
process = install_apk(file_handler->serial, req.file);
|
||||
} else {
|
||||
LOGI("Pushing %s...", req.file);
|
||||
process = push_file(file_handler->serial, req.file,
|
||||
file_handler->push_target);
|
||||
}
|
||||
file_handler->current_process = process;
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
|
||||
if (req.action == ACTION_INSTALL_APK) {
|
||||
if (process_check_success(process, "adb install", false)) {
|
||||
LOGI("Installing %s...", req.file);
|
||||
bool ok = adb_install(intr, serial, req.file, 0);
|
||||
if (ok) {
|
||||
LOGI("%s successfully installed", req.file);
|
||||
} else {
|
||||
LOGE("Failed to install %s", req.file);
|
||||
}
|
||||
} else {
|
||||
if (process_check_success(process, "adb push", false)) {
|
||||
LOGI("%s successfully pushed to %s", req.file,
|
||||
file_handler->push_target);
|
||||
LOGI("Pushing %s...", req.file);
|
||||
bool ok = adb_push(intr, serial, req.file, push_target, 0);
|
||||
if (ok) {
|
||||
LOGI("%s successfully pushed to %s", req.file, push_target);
|
||||
} else {
|
||||
LOGE("Failed to push %s to %s", req.file,
|
||||
file_handler->push_target);
|
||||
LOGE("Failed to push %s to %s", req.file, push_target);
|
||||
}
|
||||
}
|
||||
|
||||
sc_mutex_lock(&file_handler->mutex);
|
||||
// Close the process (it is necessary already terminated)
|
||||
// Execute this call with mutex locked to avoid race conditions with
|
||||
// file_handler_stop()
|
||||
process_close(file_handler->current_process);
|
||||
file_handler->current_process = PROCESS_NONE;
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
|
||||
file_handler_request_destroy(&req);
|
||||
}
|
||||
return 0;
|
||||
@ -169,7 +154,7 @@ file_handler_start(struct file_handler *file_handler) {
|
||||
LOGD("Starting file_handler thread");
|
||||
|
||||
bool ok = sc_thread_create(&file_handler->thread, run_file_handler,
|
||||
"file_handler", file_handler);
|
||||
"scrcpy-file", file_handler);
|
||||
if (!ok) {
|
||||
LOGC("Could not start file_handler thread");
|
||||
return false;
|
||||
@ -183,11 +168,7 @@ file_handler_stop(struct file_handler *file_handler) {
|
||||
sc_mutex_lock(&file_handler->mutex);
|
||||
file_handler->stopped = true;
|
||||
sc_cond_signal(&file_handler->event_cond);
|
||||
if (file_handler->current_process != PROCESS_NONE) {
|
||||
if (!process_terminate(file_handler->current_process)) {
|
||||
LOGW("Could not terminate push/install process");
|
||||
}
|
||||
}
|
||||
sc_intr_interrupt(&file_handler->intr);
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "adb.h"
|
||||
#include "util/cbuf.h"
|
||||
#include "util/thread.h"
|
||||
#include "util/intr.h"
|
||||
|
||||
typedef enum {
|
||||
ACTION_INSTALL_APK,
|
||||
@ -29,8 +30,9 @@ struct file_handler {
|
||||
sc_cond event_cond;
|
||||
bool stopped;
|
||||
bool initialized;
|
||||
process_t current_process;
|
||||
struct file_handler_request_queue queue;
|
||||
|
||||
struct sc_intr intr;
|
||||
};
|
||||
|
||||
bool
|
||||
|
@ -108,7 +108,7 @@ fps_counter_start(struct fps_counter *counter) {
|
||||
// same thread, no need to lock
|
||||
if (!counter->thread_started) {
|
||||
bool ok = sc_thread_create(&counter->thread, run_fps_counter,
|
||||
"fps counter", counter);
|
||||
"scrcpy-fps", counter);
|
||||
if (!ok) {
|
||||
LOGE("Could not start FPS counter thread");
|
||||
return false;
|
||||
|
@ -10,11 +10,13 @@ bool
|
||||
sc_frame_buffer_init(struct sc_frame_buffer *fb) {
|
||||
fb->pending_frame = av_frame_alloc();
|
||||
if (!fb->pending_frame) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
fb->tmp_frame = av_frame_alloc();
|
||||
if (!fb->tmp_frame) {
|
||||
LOG_OOM();
|
||||
av_frame_free(&fb->pending_frame);
|
||||
return false;
|
||||
}
|
||||
|
437
app/src/hid_keyboard.c
Normal file
437
app/src/hid_keyboard.c
Normal file
@ -0,0 +1,437 @@
|
||||
#include "hid_keyboard.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "input_events.h"
|
||||
#include "util/log.h"
|
||||
|
||||
/** Downcast key processor to hid_keyboard */
|
||||
#define DOWNCAST(KP) container_of(KP, struct sc_hid_keyboard, key_processor)
|
||||
|
||||
#define HID_KEYBOARD_ACCESSORY_ID 1
|
||||
|
||||
#define HID_MODIFIER_NONE 0x00
|
||||
#define HID_MODIFIER_LEFT_CONTROL (1 << 0)
|
||||
#define HID_MODIFIER_LEFT_SHIFT (1 << 1)
|
||||
#define HID_MODIFIER_LEFT_ALT (1 << 2)
|
||||
#define HID_MODIFIER_LEFT_GUI (1 << 3)
|
||||
#define HID_MODIFIER_RIGHT_CONTROL (1 << 4)
|
||||
#define HID_MODIFIER_RIGHT_SHIFT (1 << 5)
|
||||
#define HID_MODIFIER_RIGHT_ALT (1 << 6)
|
||||
#define HID_MODIFIER_RIGHT_GUI (1 << 7)
|
||||
|
||||
#define HID_KEYBOARD_INDEX_MODIFIER 0
|
||||
#define HID_KEYBOARD_INDEX_KEYS 2
|
||||
|
||||
// USB HID protocol says 6 keys in an event is the requirement for BIOS
|
||||
// keyboard support, though OS could support more keys via modifying the report
|
||||
// desc. 6 should be enough for scrcpy.
|
||||
#define HID_KEYBOARD_MAX_KEYS 6
|
||||
#define HID_KEYBOARD_EVENT_SIZE (2 + HID_KEYBOARD_MAX_KEYS)
|
||||
|
||||
#define HID_RESERVED 0x00
|
||||
#define HID_ERROR_ROLL_OVER 0x01
|
||||
|
||||
/**
|
||||
* For HID over AOAv2, only report descriptor is needed.
|
||||
*
|
||||
* The specification is available here:
|
||||
* <https://www.usb.org/sites/default/files/hid1_11.pdf>
|
||||
*
|
||||
* In particular, read:
|
||||
* - 6.2.2 Report Descriptor
|
||||
* - Appendix B.1 Protocol 1 (Keyboard)
|
||||
* - Appendix C: Keyboard Implementation
|
||||
*
|
||||
* Normally a basic HID keyboard uses 8 bytes:
|
||||
* Modifier Reserved Key Key Key Key Key Key
|
||||
*
|
||||
* You can dump your device's report descriptor with:
|
||||
*
|
||||
* sudo usbhid-dump -m vid:pid -e descriptor
|
||||
*
|
||||
* (change vid:pid' to your device's vendor ID and product ID).
|
||||
*/
|
||||
static const unsigned char keyboard_report_desc[] = {
|
||||
// Usage Page (Generic Desktop)
|
||||
0x05, 0x01,
|
||||
// Usage (Keyboard)
|
||||
0x09, 0x06,
|
||||
|
||||
// Collection (Application)
|
||||
0xA1, 0x01,
|
||||
|
||||
// Usage Page (Key Codes)
|
||||
0x05, 0x07,
|
||||
// Usage Minimum (224)
|
||||
0x19, 0xE0,
|
||||
// Usage Maximum (231)
|
||||
0x29, 0xE7,
|
||||
// Logical Minimum (0)
|
||||
0x15, 0x00,
|
||||
// Logical Maximum (1)
|
||||
0x25, 0x01,
|
||||
// Report Size (1)
|
||||
0x75, 0x01,
|
||||
// Report Count (8)
|
||||
0x95, 0x08,
|
||||
// Input (Data, Variable, Absolute): Modifier byte
|
||||
0x81, 0x02,
|
||||
|
||||
// Report Size (8)
|
||||
0x75, 0x08,
|
||||
// Report Count (1)
|
||||
0x95, 0x01,
|
||||
// Input (Constant): Reserved byte
|
||||
0x81, 0x01,
|
||||
|
||||
// Usage Page (LEDs)
|
||||
0x05, 0x08,
|
||||
// Usage Minimum (1)
|
||||
0x19, 0x01,
|
||||
// Usage Maximum (5)
|
||||
0x29, 0x05,
|
||||
// Report Size (1)
|
||||
0x75, 0x01,
|
||||
// Report Count (5)
|
||||
0x95, 0x05,
|
||||
// Output (Data, Variable, Absolute): LED report
|
||||
0x91, 0x02,
|
||||
|
||||
// Report Size (3)
|
||||
0x75, 0x03,
|
||||
// Report Count (1)
|
||||
0x95, 0x01,
|
||||
// Output (Constant): LED report padding
|
||||
0x91, 0x01,
|
||||
|
||||
// Usage Page (Key Codes)
|
||||
0x05, 0x07,
|
||||
// Usage Minimum (0)
|
||||
0x19, 0x00,
|
||||
// Usage Maximum (101)
|
||||
0x29, SC_HID_KEYBOARD_KEYS - 1,
|
||||
// Logical Minimum (0)
|
||||
0x15, 0x00,
|
||||
// Logical Maximum(101)
|
||||
0x25, SC_HID_KEYBOARD_KEYS - 1,
|
||||
// Report Size (8)
|
||||
0x75, 0x08,
|
||||
// Report Count (6)
|
||||
0x95, HID_KEYBOARD_MAX_KEYS,
|
||||
// Input (Data, Array): Keys
|
||||
0x81, 0x00,
|
||||
|
||||
// End Collection
|
||||
0xC0
|
||||
};
|
||||
|
||||
/**
|
||||
* A keyboard HID event is 8 bytes long:
|
||||
*
|
||||
* - byte 0: modifiers (1 flag per modifier key, 8 possible modifier keys)
|
||||
* - byte 1: reserved (always 0)
|
||||
* - bytes 2 to 7: pressed keys (6 at most)
|
||||
*
|
||||
* 7 6 5 4 3 2 1 0
|
||||
* +---------------+
|
||||
* byte 0: |. . . . . . . .| modifiers
|
||||
* +---------------+
|
||||
* ^ ^ ^ ^ ^ ^ ^ ^
|
||||
* | | | | | | | `- left Ctrl
|
||||
* | | | | | | `--- left Shift
|
||||
* | | | | | `----- left Alt
|
||||
* | | | | `------- left Gui
|
||||
* | | | `--------- right Ctrl
|
||||
* | | `----------- right Shift
|
||||
* | `------------- right Alt
|
||||
* `--------------- right Gui
|
||||
*
|
||||
* +---------------+
|
||||
* byte 1: |0 0 0 0 0 0 0 0| reserved
|
||||
* +---------------+
|
||||
*
|
||||
* +---------------+
|
||||
* bytes 2 to 7: |. . . . . . . .| scancode of 1st key pressed
|
||||
* +---------------+
|
||||
* |. . . . . . . .| scancode of 2nd key pressed
|
||||
* +---------------+
|
||||
* |. . . . . . . .| scancode of 3rd key pressed
|
||||
* +---------------+
|
||||
* |. . . . . . . .| scancode of 4th key pressed
|
||||
* +---------------+
|
||||
* |. . . . . . . .| scancode of 5th key pressed
|
||||
* +---------------+
|
||||
* |. . . . . . . .| scancode of 6th key pressed
|
||||
* +---------------+
|
||||
*
|
||||
* If there are less than 6 keys pressed, the last items are set to 0.
|
||||
* For example, if A and W are pressed:
|
||||
*
|
||||
* +---------------+
|
||||
* bytes 2 to 7: |0 0 0 0 0 1 0 0| A is pressed (scancode = 4)
|
||||
* +---------------+
|
||||
* |0 0 0 1 1 0 1 0| W is pressed (scancode = 26)
|
||||
* +---------------+
|
||||
* |0 0 0 0 0 0 0 0| ^
|
||||
* +---------------+ | only 2 keys are pressed, the
|
||||
* |0 0 0 0 0 0 0 0| | remaining items are set to 0
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 0| |
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 0| v
|
||||
* +---------------+
|
||||
*
|
||||
* Pressing more than 6 keys is not supported. If this happens (typically,
|
||||
* never in practice), report a "phantom state":
|
||||
*
|
||||
* +---------------+
|
||||
* bytes 2 to 7: |0 0 0 0 0 0 0 1| ^
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 1| | more than 6 keys pressed:
|
||||
* +---------------+ | the list is filled with a special
|
||||
* |0 0 0 0 0 0 0 1| | rollover error code (0x01)
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 1| |
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 1| |
|
||||
* +---------------+ |
|
||||
* |0 0 0 0 0 0 0 1| v
|
||||
* +---------------+
|
||||
*/
|
||||
|
||||
static unsigned char
|
||||
sdl_keymod_to_hid_modifiers(uint16_t mod) {
|
||||
unsigned char modifiers = HID_MODIFIER_NONE;
|
||||
if (mod & SC_MOD_LCTRL) {
|
||||
modifiers |= HID_MODIFIER_LEFT_CONTROL;
|
||||
}
|
||||
if (mod & SC_MOD_LSHIFT) {
|
||||
modifiers |= HID_MODIFIER_LEFT_SHIFT;
|
||||
}
|
||||
if (mod & SC_MOD_LALT) {
|
||||
modifiers |= HID_MODIFIER_LEFT_ALT;
|
||||
}
|
||||
if (mod & SC_MOD_LGUI) {
|
||||
modifiers |= HID_MODIFIER_LEFT_GUI;
|
||||
}
|
||||
if (mod & SC_MOD_RCTRL) {
|
||||
modifiers |= HID_MODIFIER_RIGHT_CONTROL;
|
||||
}
|
||||
if (mod & SC_MOD_RSHIFT) {
|
||||
modifiers |= HID_MODIFIER_RIGHT_SHIFT;
|
||||
}
|
||||
if (mod & SC_MOD_RALT) {
|
||||
modifiers |= HID_MODIFIER_RIGHT_ALT;
|
||||
}
|
||||
if (mod & SC_MOD_RGUI) {
|
||||
modifiers |= HID_MODIFIER_RIGHT_GUI;
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
|
||||
unsigned char *buffer = malloc(HID_KEYBOARD_EVENT_SIZE);
|
||||
if (!buffer) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer[HID_KEYBOARD_INDEX_MODIFIER] = HID_MODIFIER_NONE;
|
||||
buffer[1] = HID_RESERVED;
|
||||
memset(&buffer[HID_KEYBOARD_INDEX_KEYS], 0, HID_KEYBOARD_MAX_KEYS);
|
||||
|
||||
sc_hid_event_init(hid_event, HID_KEYBOARD_ACCESSORY_ID, buffer,
|
||||
HID_KEYBOARD_EVENT_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
scancode_is_modifier(enum sc_scancode scancode) {
|
||||
return scancode >= SC_SCANCODE_LCTRL && scancode <= SC_SCANCODE_RGUI;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
||||
struct sc_hid_event *hid_event,
|
||||
const struct sc_key_event *event) {
|
||||
enum sc_scancode scancode = event->scancode;
|
||||
assert(scancode >= 0);
|
||||
|
||||
// SDL also generates events when only modifiers are pressed, we cannot
|
||||
// ignore them totally, for example press 'a' first then press 'Control',
|
||||
// if we ignore 'Control' event, only 'a' is sent.
|
||||
if (scancode >= SC_HID_KEYBOARD_KEYS && !scancode_is_modifier(scancode)) {
|
||||
// Scancode to ignore
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sc_hid_keyboard_event_init(hid_event)) {
|
||||
LOGW("Could not initialize HID keyboard event");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned char modifiers = sdl_keymod_to_hid_modifiers(event->mods_state);
|
||||
|
||||
if (scancode < SC_HID_KEYBOARD_KEYS) {
|
||||
// Pressed is true and released is false
|
||||
kb->keys[scancode] = (event->action == SC_ACTION_DOWN);
|
||||
LOGV("keys[%02x] = %s", scancode,
|
||||
kb->keys[scancode] ? "true" : "false");
|
||||
}
|
||||
|
||||
hid_event->buffer[HID_KEYBOARD_INDEX_MODIFIER] = modifiers;
|
||||
|
||||
unsigned char *keys_buffer = &hid_event->buffer[HID_KEYBOARD_INDEX_KEYS];
|
||||
// Re-calculate pressed keys every time
|
||||
int keys_pressed_count = 0;
|
||||
for (int i = 0; i < SC_HID_KEYBOARD_KEYS; ++i) {
|
||||
if (kb->keys[i]) {
|
||||
// USB HID protocol says that if keys exceeds report count, a
|
||||
// phantom state should be reported
|
||||
if (keys_pressed_count >= HID_KEYBOARD_MAX_KEYS) {
|
||||
// Phantom state:
|
||||
// - Modifiers
|
||||
// - Reserved
|
||||
// - ErrorRollOver * HID_MAX_KEYS
|
||||
memset(keys_buffer, HID_ERROR_ROLL_OVER, HID_KEYBOARD_MAX_KEYS);
|
||||
goto end;
|
||||
}
|
||||
|
||||
keys_buffer[keys_pressed_count] = i;
|
||||
++keys_pressed_count;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
LOGV("hid keyboard: key %-4s scancode=%02x (%u) mod=%02x",
|
||||
event->action == SC_ACTION_DOWN ? "down" : "up", event->scancode,
|
||||
event->scancode, modifiers);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t mods_state) {
|
||||
bool capslock = mods_state & SC_MOD_CAPS;
|
||||
bool numlock = mods_state & SC_MOD_NUM;
|
||||
if (!capslock && !numlock) {
|
||||
// Nothing to do
|
||||
return true;
|
||||
}
|
||||
|
||||
struct sc_hid_event hid_event;
|
||||
if (!sc_hid_keyboard_event_init(&hid_event)) {
|
||||
LOGW("Could not initialize HID keyboard event");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned i = 0;
|
||||
if (capslock) {
|
||||
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
|
||||
++i;
|
||||
}
|
||||
if (numlock) {
|
||||
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_NUMLOCK;
|
||||
++i;
|
||||
}
|
||||
|
||||
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
|
||||
sc_hid_event_destroy(&hid_event);
|
||||
LOGW("Could request HID event");
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGD("HID keyboard state synchronized");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||
const struct sc_key_event *event,
|
||||
uint64_t ack_to_wait) {
|
||||
if (event->repeat) {
|
||||
// In USB HID protocol, key repeat is handled by the host (Android), so
|
||||
// just ignore key repeat here.
|
||||
return;
|
||||
}
|
||||
|
||||
struct sc_hid_keyboard *kb = DOWNCAST(kp);
|
||||
|
||||
struct sc_hid_event hid_event;
|
||||
// Not all keys are supported, just ignore unsupported keys
|
||||
if (convert_hid_keyboard_event(kb, &hid_event, event)) {
|
||||
if (!kb->mod_lock_synchronized) {
|
||||
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
|
||||
// keyboard state
|
||||
if (push_mod_lock_state(kb, event->mods_state)) {
|
||||
kb->mod_lock_synchronized = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (ack_to_wait) {
|
||||
// Ctrl+v is pressed, so clipboard synchronization has been
|
||||
// requested. Wait until clipboard synchronization is acknowledged
|
||||
// by the server, otherwise it could paste the old clipboard
|
||||
// content.
|
||||
hid_event.ack_to_wait = ack_to_wait;
|
||||
}
|
||||
|
||||
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
|
||||
sc_hid_event_destroy(&hid_event);
|
||||
LOGW("Could request HID event");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||
const struct sc_text_event *event) {
|
||||
(void) kp;
|
||||
(void) event;
|
||||
|
||||
// Never forward text input via HID (all the keys are injected separately)
|
||||
}
|
||||
|
||||
bool
|
||||
sc_hid_keyboard_init(struct sc_hid_keyboard *kb, struct sc_aoa *aoa) {
|
||||
kb->aoa = aoa;
|
||||
|
||||
bool ok = sc_aoa_setup_hid(aoa, HID_KEYBOARD_ACCESSORY_ID,
|
||||
keyboard_report_desc,
|
||||
ARRAY_LEN(keyboard_report_desc));
|
||||
if (!ok) {
|
||||
LOGW("Register HID keyboard failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reset all states
|
||||
memset(kb->keys, false, SC_HID_KEYBOARD_KEYS);
|
||||
|
||||
kb->mod_lock_synchronized = false;
|
||||
|
||||
static const struct sc_key_processor_ops ops = {
|
||||
.process_key = sc_key_processor_process_key,
|
||||
.process_text = sc_key_processor_process_text,
|
||||
};
|
||||
|
||||
// Clipboard synchronization is requested over the control socket, while HID
|
||||
// events are sent over AOA, so it must wait for clipboard synchronization
|
||||
// to be acknowledged by the device before injecting Ctrl+v.
|
||||
kb->key_processor.async_paste = true;
|
||||
kb->key_processor.ops = &ops;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_hid_keyboard_destroy(struct sc_hid_keyboard *kb) {
|
||||
// Unregister HID keyboard so the soft keyboard shows again on Android
|
||||
bool ok = sc_aoa_unregister_hid(kb->aoa, HID_KEYBOARD_ACCESSORY_ID);
|
||||
if (!ok) {
|
||||
LOGW("Could not unregister HID keyboard");
|
||||
}
|
||||
}
|
44
app/src/hid_keyboard.h
Normal file
44
app/src/hid_keyboard.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef SC_HID_KEYBOARD_H
|
||||
#define SC_HID_KEYBOARD_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "aoa_hid.h"
|
||||
#include "trait/key_processor.h"
|
||||
|
||||
// See "SDL2/SDL_scancode.h".
|
||||
// Maybe SDL_Keycode is used by most people, but SDL_Scancode is taken from USB
|
||||
// HID protocol.
|
||||
// 0x65 is Application, typically AT-101 Keyboard ends here.
|
||||
#define SC_HID_KEYBOARD_KEYS 0x66
|
||||
|
||||
/**
|
||||
* HID keyboard events are sequence-based, every time keyboard state changes
|
||||
* it sends an array of currently pressed keys, the host is responsible for
|
||||
* compare events and determine which key becomes pressed and which key becomes
|
||||
* released. In order to convert SDL_KeyboardEvent to HID events, we first use
|
||||
* an array of keys to save each keys' state. And when a SDL_KeyboardEvent was
|
||||
* emitted, we updated our state, and then we use a loop to generate HID
|
||||
* events. The sequence of array elements is unimportant and when too much keys
|
||||
* pressed at the same time (more than report count), we should generate
|
||||
* phantom state. Don't forget that modifiers should be updated too, even for
|
||||
* phantom state.
|
||||
*/
|
||||
struct sc_hid_keyboard {
|
||||
struct sc_key_processor key_processor; // key processor trait
|
||||
|
||||
struct sc_aoa *aoa;
|
||||
bool keys[SC_HID_KEYBOARD_KEYS];
|
||||
|
||||
bool mod_lock_synchronized;
|
||||
};
|
||||
|
||||
bool
|
||||
sc_hid_keyboard_init(struct sc_hid_keyboard *kb, struct sc_aoa *aoa);
|
||||
|
||||
void
|
||||
sc_hid_keyboard_destroy(struct sc_hid_keyboard *kb);
|
||||
|
||||
#endif
|
290
app/src/icon.c
Normal file
290
app/src/icon.c
Normal file
@ -0,0 +1,290 @@
|
||||
#include "icon.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/pixdesc.h>
|
||||
#include <libavutil/pixfmt.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "compat.h"
|
||||
#include "util/file.h"
|
||||
#include "util/log.h"
|
||||
#include "util/str.h"
|
||||
|
||||
#define SCRCPY_PORTABLE_ICON_FILENAME "icon.png"
|
||||
#define SCRCPY_DEFAULT_ICON_PATH \
|
||||
PREFIX "/share/icons/hicolor/256x256/apps/scrcpy.png"
|
||||
|
||||
static char *
|
||||
get_icon_path(void) {
|
||||
#ifdef __WINDOWS__
|
||||
const wchar_t *icon_path_env = _wgetenv(L"SCRCPY_ICON_PATH");
|
||||
#else
|
||||
const char *icon_path_env = getenv("SCRCPY_ICON_PATH");
|
||||
#endif
|
||||
if (icon_path_env) {
|
||||
// if the envvar is set, use it
|
||||
#ifdef __WINDOWS__
|
||||
char *icon_path = sc_str_from_wchars(icon_path_env);
|
||||
#else
|
||||
char *icon_path = strdup(icon_path_env);
|
||||
#endif
|
||||
if (!icon_path) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
|
||||
return icon_path;
|
||||
}
|
||||
|
||||
#ifndef PORTABLE
|
||||
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
|
||||
char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
|
||||
if (!icon_path) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
#else
|
||||
char *icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME);
|
||||
if (!icon_path) {
|
||||
LOGE("Could not get icon path");
|
||||
return NULL;
|
||||
}
|
||||
LOGD("Using icon (portable): %s", icon_path);
|
||||
#endif
|
||||
|
||||
return icon_path;
|
||||
}
|
||||
|
||||
static AVFrame *
|
||||
decode_image(const char *path) {
|
||||
AVFrame *result = NULL;
|
||||
|
||||
AVFormatContext *ctx = avformat_alloc_context();
|
||||
if (!ctx) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (avformat_open_input(&ctx, path, NULL, NULL) < 0) {
|
||||
LOGE("Could not open image codec: %s", path);
|
||||
goto free_ctx;
|
||||
}
|
||||
|
||||
if (avformat_find_stream_info(ctx, NULL) < 0) {
|
||||
LOGE("Could not find image stream info");
|
||||
goto close_input;
|
||||
}
|
||||
|
||||
int stream = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
|
||||
if (stream < 0 ) {
|
||||
LOGE("Could not find best image stream");
|
||||
goto close_input;
|
||||
}
|
||||
|
||||
AVCodecParameters *params = ctx->streams[stream]->codecpar;
|
||||
|
||||
AVCodec *codec = avcodec_find_decoder(params->codec_id);
|
||||
if (!codec) {
|
||||
LOGE("Could not find image decoder");
|
||||
goto close_input;
|
||||
}
|
||||
|
||||
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
|
||||
if (!codec_ctx) {
|
||||
LOG_OOM();
|
||||
goto close_input;
|
||||
}
|
||||
|
||||
if (avcodec_parameters_to_context(codec_ctx, params) < 0) {
|
||||
LOGE("Could not fill codec context");
|
||||
goto free_codec_ctx;
|
||||
}
|
||||
|
||||
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
|
||||
LOGE("Could not open image codec");
|
||||
goto free_codec_ctx;
|
||||
}
|
||||
|
||||
AVFrame *frame = av_frame_alloc();
|
||||
if (!frame) {
|
||||
LOG_OOM();
|
||||
goto close_codec;
|
||||
}
|
||||
|
||||
AVPacket *packet = av_packet_alloc();
|
||||
if (!packet) {
|
||||
LOG_OOM();
|
||||
av_frame_free(&frame);
|
||||
goto close_codec;
|
||||
}
|
||||
|
||||
if (av_read_frame(ctx, packet) < 0) {
|
||||
LOGE("Could not read frame");
|
||||
av_packet_free(&packet);
|
||||
av_frame_free(&frame);
|
||||
goto close_codec;
|
||||
}
|
||||
|
||||
int ret;
|
||||
if ((ret = avcodec_send_packet(codec_ctx, packet)) < 0) {
|
||||
LOGE("Could not send icon packet: %d", ret);
|
||||
av_packet_free(&packet);
|
||||
av_frame_free(&frame);
|
||||
goto close_codec;
|
||||
}
|
||||
|
||||
if ((ret = avcodec_receive_frame(codec_ctx, frame)) != 0) {
|
||||
LOGE("Could not receive icon frame: %d", ret);
|
||||
av_packet_free(&packet);
|
||||
av_frame_free(&frame);
|
||||
goto close_codec;
|
||||
}
|
||||
|
||||
av_packet_free(&packet);
|
||||
|
||||
result = frame;
|
||||
|
||||
close_codec:
|
||||
avcodec_close(codec_ctx);
|
||||
free_codec_ctx:
|
||||
avcodec_free_context(&codec_ctx);
|
||||
close_input:
|
||||
avformat_close_input(&ctx);
|
||||
free_ctx:
|
||||
avformat_free_context(ctx);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if !SDL_VERSION_ATLEAST(2, 0, 10)
|
||||
// SDL_PixelFormatEnum has been introduced in SDL 2.0.10. Use int for older SDL
|
||||
// versions.
|
||||
typedef int SDL_PixelFormatEnum;
|
||||
#endif
|
||||
|
||||
static SDL_PixelFormatEnum
|
||||
to_sdl_pixel_format(enum AVPixelFormat fmt) {
|
||||
switch (fmt) {
|
||||
case AV_PIX_FMT_RGB24: return SDL_PIXELFORMAT_RGB24;
|
||||
case AV_PIX_FMT_BGR24: return SDL_PIXELFORMAT_BGR24;
|
||||
case AV_PIX_FMT_ARGB: return SDL_PIXELFORMAT_ARGB32;
|
||||
case AV_PIX_FMT_RGBA: return SDL_PIXELFORMAT_RGBA32;
|
||||
case AV_PIX_FMT_ABGR: return SDL_PIXELFORMAT_ABGR32;
|
||||
case AV_PIX_FMT_BGRA: return SDL_PIXELFORMAT_BGRA32;
|
||||
case AV_PIX_FMT_RGB565BE: return SDL_PIXELFORMAT_RGB565;
|
||||
case AV_PIX_FMT_RGB555BE: return SDL_PIXELFORMAT_RGB555;
|
||||
case AV_PIX_FMT_BGR565BE: return SDL_PIXELFORMAT_BGR565;
|
||||
case AV_PIX_FMT_BGR555BE: return SDL_PIXELFORMAT_BGR555;
|
||||
case AV_PIX_FMT_RGB444BE: return SDL_PIXELFORMAT_RGB444;
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 12)
|
||||
case AV_PIX_FMT_BGR444BE: return SDL_PIXELFORMAT_BGR444;
|
||||
#endif
|
||||
case AV_PIX_FMT_PAL8: return SDL_PIXELFORMAT_INDEX8;
|
||||
default: return SDL_PIXELFORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_Surface *
|
||||
load_from_path(const char *path) {
|
||||
AVFrame *frame = decode_image(path);
|
||||
if (!frame) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
|
||||
if (!desc) {
|
||||
LOGE("Could not get icon format descriptor");
|
||||
goto error;
|
||||
}
|
||||
|
||||
bool is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
|
||||
if (!is_packed) {
|
||||
LOGE("Could not load non-packed icon");
|
||||
goto error;
|
||||
}
|
||||
|
||||
SDL_PixelFormatEnum format = to_sdl_pixel_format(frame->format);
|
||||
if (format == SDL_PIXELFORMAT_UNKNOWN) {
|
||||
LOGE("Unsupported icon pixel format: %s (%d)", desc->name,
|
||||
frame->format);
|
||||
goto error;
|
||||
}
|
||||
|
||||
int bits_per_pixel = av_get_bits_per_pixel(desc);
|
||||
SDL_Surface *surface =
|
||||
SDL_CreateRGBSurfaceWithFormatFrom(frame->data[0],
|
||||
frame->width, frame->height,
|
||||
bits_per_pixel,
|
||||
frame->linesize[0],
|
||||
format);
|
||||
|
||||
if (!surface) {
|
||||
LOGE("Could not create icon surface");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (frame->format == AV_PIX_FMT_PAL8) {
|
||||
// Initialize the SDL palette
|
||||
uint8_t *data = frame->data[1];
|
||||
SDL_Color colors[256];
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
SDL_Color *color = &colors[i];
|
||||
|
||||
// The palette is transported in AVFrame.data[1], is 1024 bytes
|
||||
// long (256 4-byte entries) and is formatted the same as in
|
||||
// AV_PIX_FMT_RGB32 described above (i.e., it is also
|
||||
// endian-specific).
|
||||
// <https://ffmpeg.org/doxygen/4.1/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5>
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
color->a = data[i * 4];
|
||||
color->r = data[i * 4 + 1];
|
||||
color->g = data[i * 4 + 2];
|
||||
color->b = data[i * 4 + 3];
|
||||
#else
|
||||
color->a = data[i * 4 + 3];
|
||||
color->r = data[i * 4 + 2];
|
||||
color->g = data[i * 4 + 1];
|
||||
color->b = data[i * 4];
|
||||
#endif
|
||||
}
|
||||
|
||||
SDL_Palette *palette = surface->format->palette;
|
||||
assert(palette);
|
||||
int ret = SDL_SetPaletteColors(palette, colors, 0, 256);
|
||||
if (ret) {
|
||||
LOGE("Could not set palette colors");
|
||||
SDL_FreeSurface(surface);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
surface->userdata = frame; // frame owns the data
|
||||
|
||||
return surface;
|
||||
|
||||
error:
|
||||
av_frame_free(&frame);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_Surface *
|
||||
scrcpy_icon_load() {
|
||||
char *icon_path = get_icon_path();
|
||||
if (!icon_path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_Surface *icon = load_from_path(icon_path);
|
||||
free(icon_path);
|
||||
return icon;
|
||||
}
|
||||
|
||||
void
|
||||
scrcpy_icon_destroy(SDL_Surface *icon) {
|
||||
AVFrame *frame = icon->userdata;
|
||||
assert(frame);
|
||||
av_frame_free(&frame);
|
||||
SDL_FreeSurface(icon);
|
||||
}
|
16
app/src/icon.h
Normal file
16
app/src/icon.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef ICON_H
|
||||
#define ICON_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
SDL_Surface *
|
||||
scrcpy_icon_load(void);
|
||||
|
||||
void
|
||||
scrcpy_icon_destroy(SDL_Surface *icon);
|
||||
|
||||
#endif
|
@ -1,53 +0,0 @@
|
||||
/* XPM */
|
||||
static char * icon_xpm[] = {
|
||||
"48 48 2 1",
|
||||
" c None",
|
||||
". c #96C13E",
|
||||
" .. .. ",
|
||||
" ... ... ",
|
||||
" ... ...... ... ",
|
||||
" ................ ",
|
||||
" .............. ",
|
||||
" ................ ",
|
||||
" .................. ",
|
||||
" .................... ",
|
||||
" ..... ........ ..... ",
|
||||
" ..... ........ ..... ",
|
||||
" ...................... ",
|
||||
" ........................ ",
|
||||
" ........................ ",
|
||||
" ........................ ",
|
||||
" ",
|
||||
" ",
|
||||
" .... ........................ .... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" ...... ........................ ...... ",
|
||||
" .... ........................ .... ",
|
||||
" ........................ ",
|
||||
" ...................... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" ...... ...... ",
|
||||
" .... .... "};
|
76
app/src/input_events.c
Normal file
76
app/src/input_events.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <input_events.h>
|
||||
|
||||
static inline uint16_t
|
||||
sc_mods_state_from_sdl(uint16_t mods_state) {
|
||||
return mods_state;
|
||||
}
|
||||
|
||||
static inline enum sc_keycode
|
||||
sc_keycode_from_sdl(SDL_Keycode keycode) {
|
||||
return (enum sc_keycode) keycode;
|
||||
}
|
||||
|
||||
static inline enum sc_scancode
|
||||
sc_scancode_from_sdl(SDL_Scancode scancode) {
|
||||
return (enum sc_scancode) scancode;
|
||||
}
|
||||
|
||||
static inline enum sc_action
|
||||
sc_action_from_sdl_keyboard_type(uint32_t type) {
|
||||
assert(type == SDL_KEYDOWN || type == SDL_KEYUP);
|
||||
if (type == SDL_KEYDOWN) {
|
||||
return SC_ACTION_DOWN;
|
||||
}
|
||||
return SC_ACTION_UP;
|
||||
}
|
||||
|
||||
static inline enum sc_action
|
||||
sc_action_from_sdl_mousebutton_type(uint32_t type) {
|
||||
assert(type == SDL_MOUSEBUTTONDOWN || type == SDL_MOUSEBUTTONUP);
|
||||
if (type == SDL_MOUSEBUTTONDOWN) {
|
||||
return SC_ACTION_DOWN;
|
||||
}
|
||||
return SC_ACTION_UP;
|
||||
}
|
||||
|
||||
static inline enum sc_mouse_button
|
||||
sc_mouse_button_from_sdl(uint8_t button) {
|
||||
if (button >= SDL_BUTTON_LEFT && button <= SDL_BUTTON_X2) {
|
||||
// SC_MOUSE_BUTTON_* constants are initialized from SDL_BUTTON(index)
|
||||
return SDL_BUTTON(button);
|
||||
}
|
||||
|
||||
return SC_MOUSE_BUTTON_UNKNOWN;
|
||||
}
|
||||
|
||||
static inline uint8_t
|
||||
sc_mouse_buttons_state_from_sdl(uint32_t buttons_state) {
|
||||
assert(buttons_state < 0x100); // fits in uint8_t
|
||||
return buttons_state;
|
||||
}
|
||||
|
||||
void
|
||||
sc_key_event_from_sdl(struct sc_key_event *event,
|
||||
const SDL_KeyboardEvent *sdl) {
|
||||
event->action = sc_action_from_sdl_keyboard_type(sdl->type);
|
||||
event->keycode = sc_keycode_from_sdl(sdl->keysym.sym);
|
||||
event->scancode = sc_scancode_from_sdl(sdl->keysym.scancode);
|
||||
event->repeat = sdl->repeat;
|
||||
event->mods_state = sc_mods_state_from_sdl(sdl->keysym.mod);
|
||||
}
|
||||
|
||||
void
|
||||
sc_text_event_from_sdl(struct sc_text_event *event,
|
||||
const SDL_TextInputEvent *sdl) {
|
||||
event->text = sdl->text;
|
||||
}
|
||||
|
||||
void
|
||||
sc_mouse_click_event_from_sdl(struct sc_mouse_click_event *event,
|
||||
const SDL_MouseButtonEvent *sdl,
|
||||
const SDL_Window *window,
|
||||
struct sc_size screen_size) {
|
||||
event->action = sc_action_from_sdl_mousebutton_type(sdl->type);
|
||||
event->button = sc_mouse_button_from_sdl(sdl->button);
|
||||
event->position.screen_size = screen_size;
|
||||
}
|
404
app/src/input_events.h
Normal file
404
app/src/input_events.h
Normal file
@ -0,0 +1,404 @@
|
||||
#ifndef SC_INPUT_EVENTS_H
|
||||
#define SC_INPUT_EVENTS_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <SDL2/SDL_events.h>
|
||||
|
||||
#include "coords.h"
|
||||
|
||||
/* The values are purposely the same as the SDL constants, so that the
|
||||
* implementation to convert from the SDL version to the scrcpy version is
|
||||
* straightforward */
|
||||
|
||||
enum sc_mod {
|
||||
SC_MOD_LSHIFT = KMOD_LSHIFT,
|
||||
SC_MOD_RSHIFT = KMOD_RSHIFT,
|
||||
SC_MOD_LCTRL = KMOD_LCTRL,
|
||||
SC_MOD_RCTRL = KMOD_RCTRL,
|
||||
SC_MOD_LALT = KMOD_LALT,
|
||||
SC_MOD_RALT = KMOD_RALT,
|
||||
SC_MOD_LGUI = KMOD_LGUI,
|
||||
SC_MOD_RGUI = KMOD_RGUI,
|
||||
|
||||
SC_MOD_NUM = KMOD_NUM,
|
||||
SC_MOD_CAPS = KMOD_CAPS,
|
||||
SC_MOD_SCROLL = KMOD_SCROLL,
|
||||
};
|
||||
|
||||
enum sc_action {
|
||||
SC_ACTION_DOWN = 1, // key or button pressed
|
||||
SC_ACTION_UP = 2, // key or button released
|
||||
};
|
||||
|
||||
enum sc_keycode {
|
||||
SC_KEYCODE_UNKNOWN = SDLK_UNKNOWN,
|
||||
|
||||
SC_KEYCODE_RETURN = SDLK_RETURN,
|
||||
SC_KEYCODE_ESCAPE = SDLK_ESCAPE,
|
||||
SC_KEYCODE_BACKSPACE = SDLK_BACKSPACE,
|
||||
SC_KEYCODE_TAB = SDLK_TAB,
|
||||
SC_KEYCODE_SPACE = SDLK_SPACE,
|
||||
SC_KEYCODE_EXCLAIM = SDLK_EXCLAIM,
|
||||
SC_KEYCODE_QUOTEDBL = SDLK_QUOTEDBL,
|
||||
SC_KEYCODE_HASH = SDLK_HASH,
|
||||
SC_KEYCODE_PERCENT = SDLK_PERCENT,
|
||||
SC_KEYCODE_DOLLAR = SDLK_DOLLAR,
|
||||
SC_KEYCODE_AMPERSAND = SDLK_AMPERSAND,
|
||||
SC_KEYCODE_QUOTE = SDLK_QUOTE,
|
||||
SC_KEYCODE_LEFTPAREN = SDLK_LEFTPAREN,
|
||||
SC_KEYCODE_RIGHTPAREN = SDLK_RIGHTPAREN,
|
||||
SC_KEYCODE_ASTERISK = SDLK_ASTERISK,
|
||||
SC_KEYCODE_PLUS = SDLK_PLUS,
|
||||
SC_KEYCODE_COMMA = SDLK_COMMA,
|
||||
SC_KEYCODE_MINUS = SDLK_MINUS,
|
||||
SC_KEYCODE_PERIOD = SDLK_PERIOD,
|
||||
SC_KEYCODE_SLASH = SDLK_SLASH,
|
||||
SC_KEYCODE_0 = SDLK_0,
|
||||
SC_KEYCODE_1 = SDLK_1,
|
||||
SC_KEYCODE_2 = SDLK_2,
|
||||
SC_KEYCODE_3 = SDLK_3,
|
||||
SC_KEYCODE_4 = SDLK_4,
|
||||
SC_KEYCODE_5 = SDLK_5,
|
||||
SC_KEYCODE_6 = SDLK_6,
|
||||
SC_KEYCODE_7 = SDLK_7,
|
||||
SC_KEYCODE_8 = SDLK_8,
|
||||
SC_KEYCODE_9 = SDLK_9,
|
||||
SC_KEYCODE_COLON = SDLK_COLON,
|
||||
SC_KEYCODE_SEMICOLON = SDLK_SEMICOLON,
|
||||
SC_KEYCODE_LESS = SDLK_LESS,
|
||||
SC_KEYCODE_EQUALS = SDLK_EQUALS,
|
||||
SC_KEYCODE_GREATER = SDLK_GREATER,
|
||||
SC_KEYCODE_QUESTION = SDLK_QUESTION,
|
||||
SC_KEYCODE_AT = SDLK_AT,
|
||||
|
||||
SC_KEYCODE_LEFTBRACKET = SDLK_LEFTBRACKET,
|
||||
SC_KEYCODE_BACKSLASH = SDLK_BACKSLASH,
|
||||
SC_KEYCODE_RIGHTBRACKET = SDLK_RIGHTBRACKET,
|
||||
SC_KEYCODE_CARET = SDLK_CARET,
|
||||
SC_KEYCODE_UNDERSCORE = SDLK_UNDERSCORE,
|
||||
SC_KEYCODE_BACKQUOTE = SDLK_BACKQUOTE,
|
||||
SC_KEYCODE_a = SDLK_a,
|
||||
SC_KEYCODE_b = SDLK_b,
|
||||
SC_KEYCODE_c = SDLK_c,
|
||||
SC_KEYCODE_d = SDLK_d,
|
||||
SC_KEYCODE_e = SDLK_e,
|
||||
SC_KEYCODE_f = SDLK_f,
|
||||
SC_KEYCODE_g = SDLK_g,
|
||||
SC_KEYCODE_h = SDLK_h,
|
||||
SC_KEYCODE_i = SDLK_i,
|
||||
SC_KEYCODE_j = SDLK_j,
|
||||
SC_KEYCODE_k = SDLK_k,
|
||||
SC_KEYCODE_l = SDLK_l,
|
||||
SC_KEYCODE_m = SDLK_m,
|
||||
SC_KEYCODE_n = SDLK_n,
|
||||
SC_KEYCODE_o = SDLK_o,
|
||||
SC_KEYCODE_p = SDLK_p,
|
||||
SC_KEYCODE_q = SDLK_q,
|
||||
SC_KEYCODE_r = SDLK_r,
|
||||
SC_KEYCODE_s = SDLK_s,
|
||||
SC_KEYCODE_t = SDLK_t,
|
||||
SC_KEYCODE_u = SDLK_u,
|
||||
SC_KEYCODE_v = SDLK_v,
|
||||
SC_KEYCODE_w = SDLK_w,
|
||||
SC_KEYCODE_x = SDLK_x,
|
||||
SC_KEYCODE_y = SDLK_y,
|
||||
SC_KEYCODE_z = SDLK_z,
|
||||
|
||||
SC_KEYCODE_CAPSLOCK = SDLK_CAPSLOCK,
|
||||
|
||||
SC_KEYCODE_F1 = SDLK_F1,
|
||||
SC_KEYCODE_F2 = SDLK_F2,
|
||||
SC_KEYCODE_F3 = SDLK_F3,
|
||||
SC_KEYCODE_F4 = SDLK_F4,
|
||||
SC_KEYCODE_F5 = SDLK_F5,
|
||||
SC_KEYCODE_F6 = SDLK_F6,
|
||||
SC_KEYCODE_F7 = SDLK_F7,
|
||||
SC_KEYCODE_F8 = SDLK_F8,
|
||||
SC_KEYCODE_F9 = SDLK_F9,
|
||||
SC_KEYCODE_F10 = SDLK_F10,
|
||||
SC_KEYCODE_F11 = SDLK_F11,
|
||||
SC_KEYCODE_F12 = SDLK_F12,
|
||||
|
||||
SC_KEYCODE_PRINTSCREEN = SDLK_PRINTSCREEN,
|
||||
SC_KEYCODE_SCROLLLOCK = SDLK_SCROLLLOCK,
|
||||
SC_KEYCODE_PAUSE = SDLK_PAUSE,
|
||||
SC_KEYCODE_INSERT = SDLK_INSERT,
|
||||
SC_KEYCODE_HOME = SDLK_HOME,
|
||||
SC_KEYCODE_PAGEUP = SDLK_PAGEUP,
|
||||
SC_KEYCODE_DELETE = SDLK_DELETE,
|
||||
SC_KEYCODE_END = SDLK_END,
|
||||
SC_KEYCODE_PAGEDOWN = SDLK_PAGEDOWN,
|
||||
SC_KEYCODE_RIGHT = SDLK_RIGHT,
|
||||
SC_KEYCODE_LEFT = SDLK_LEFT,
|
||||
SC_KEYCODE_DOWN = SDLK_DOWN,
|
||||
SC_KEYCODE_UP = SDLK_UP,
|
||||
|
||||
SC_KEYCODE_KP_DIVIDE = SDLK_KP_DIVIDE,
|
||||
SC_KEYCODE_KP_MULTIPLY = SDLK_KP_MULTIPLY,
|
||||
SC_KEYCODE_KP_MINUS = SDLK_KP_MINUS,
|
||||
SC_KEYCODE_KP_PLUS = SDLK_KP_PLUS,
|
||||
SC_KEYCODE_KP_ENTER = SDLK_KP_ENTER,
|
||||
SC_KEYCODE_KP_1 = SDLK_KP_1,
|
||||
SC_KEYCODE_KP_2 = SDLK_KP_2,
|
||||
SC_KEYCODE_KP_3 = SDLK_KP_3,
|
||||
SC_KEYCODE_KP_4 = SDLK_KP_4,
|
||||
SC_KEYCODE_KP_5 = SDLK_KP_5,
|
||||
SC_KEYCODE_KP_6 = SDLK_KP_6,
|
||||
SC_KEYCODE_KP_7 = SDLK_KP_7,
|
||||
SC_KEYCODE_KP_8 = SDLK_KP_8,
|
||||
SC_KEYCODE_KP_9 = SDLK_KP_9,
|
||||
SC_KEYCODE_KP_0 = SDLK_KP_0,
|
||||
SC_KEYCODE_KP_PERIOD = SDLK_KP_PERIOD,
|
||||
SC_KEYCODE_KP_EQUALS = SDLK_KP_EQUALS,
|
||||
SC_KEYCODE_KP_LEFTPAREN = SDLK_KP_LEFTPAREN,
|
||||
SC_KEYCODE_KP_RIGHTPAREN = SDLK_KP_RIGHTPAREN,
|
||||
|
||||
SC_KEYCODE_LCTRL = SDLK_LCTRL,
|
||||
SC_KEYCODE_LSHIFT = SDLK_LSHIFT,
|
||||
SC_KEYCODE_LALT = SDLK_LALT,
|
||||
SC_KEYCODE_LGUI = SDLK_LGUI,
|
||||
SC_KEYCODE_RCTRL = SDLK_RCTRL,
|
||||
SC_KEYCODE_RSHIFT = SDLK_RSHIFT,
|
||||
SC_KEYCODE_RALT = SDLK_RALT,
|
||||
SC_KEYCODE_RGUI = SDLK_RGUI,
|
||||
};
|
||||
|
||||
enum sc_scancode {
|
||||
SC_SCANCODE_UNKNOWN = SDL_SCANCODE_UNKNOWN,
|
||||
|
||||
SC_SCANCODE_A = SDL_SCANCODE_A,
|
||||
SC_SCANCODE_B = SDL_SCANCODE_B,
|
||||
SC_SCANCODE_C = SDL_SCANCODE_C,
|
||||
SC_SCANCODE_D = SDL_SCANCODE_D,
|
||||
SC_SCANCODE_E = SDL_SCANCODE_E,
|
||||
SC_SCANCODE_F = SDL_SCANCODE_F,
|
||||
SC_SCANCODE_G = SDL_SCANCODE_G,
|
||||
SC_SCANCODE_H = SDL_SCANCODE_H,
|
||||
SC_SCANCODE_I = SDL_SCANCODE_I,
|
||||
SC_SCANCODE_J = SDL_SCANCODE_J,
|
||||
SC_SCANCODE_K = SDL_SCANCODE_K,
|
||||
SC_SCANCODE_L = SDL_SCANCODE_L,
|
||||
SC_SCANCODE_M = SDL_SCANCODE_M,
|
||||
SC_SCANCODE_N = SDL_SCANCODE_N,
|
||||
SC_SCANCODE_O = SDL_SCANCODE_O,
|
||||
SC_SCANCODE_P = SDL_SCANCODE_P,
|
||||
SC_SCANCODE_Q = SDL_SCANCODE_Q,
|
||||
SC_SCANCODE_R = SDL_SCANCODE_R,
|
||||
SC_SCANCODE_S = SDL_SCANCODE_S,
|
||||
SC_SCANCODE_T = SDL_SCANCODE_T,
|
||||
SC_SCANCODE_U = SDL_SCANCODE_U,
|
||||
SC_SCANCODE_V = SDL_SCANCODE_V,
|
||||
SC_SCANCODE_W = SDL_SCANCODE_W,
|
||||
SC_SCANCODE_X = SDL_SCANCODE_X,
|
||||
SC_SCANCODE_Y = SDL_SCANCODE_Y,
|
||||
SC_SCANCODE_Z = SDL_SCANCODE_Z,
|
||||
|
||||
SC_SCANCODE_1 = SDL_SCANCODE_1,
|
||||
SC_SCANCODE_2 = SDL_SCANCODE_2,
|
||||
SC_SCANCODE_3 = SDL_SCANCODE_3,
|
||||
SC_SCANCODE_4 = SDL_SCANCODE_4,
|
||||
SC_SCANCODE_5 = SDL_SCANCODE_5,
|
||||
SC_SCANCODE_6 = SDL_SCANCODE_6,
|
||||
SC_SCANCODE_7 = SDL_SCANCODE_7,
|
||||
SC_SCANCODE_8 = SDL_SCANCODE_8,
|
||||
SC_SCANCODE_9 = SDL_SCANCODE_9,
|
||||
SC_SCANCODE_0 = SDL_SCANCODE_0,
|
||||
|
||||
SC_SCANCODE_RETURN = SDL_SCANCODE_RETURN,
|
||||
SC_SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE,
|
||||
SC_SCANCODE_BACKSPACE = SDL_SCANCODE_BACKSPACE,
|
||||
SC_SCANCODE_TAB = SDL_SCANCODE_TAB,
|
||||
SC_SCANCODE_SPACE = SDL_SCANCODE_SPACE,
|
||||
|
||||
SC_SCANCODE_MINUS = SDL_SCANCODE_MINUS,
|
||||
SC_SCANCODE_EQUALS = SDL_SCANCODE_EQUALS,
|
||||
SC_SCANCODE_LEFTBRACKET = SDL_SCANCODE_LEFTBRACKET,
|
||||
SC_SCANCODE_RIGHTBRACKET = SDL_SCANCODE_RIGHTBRACKET,
|
||||
SC_SCANCODE_BACKSLASH = SDL_SCANCODE_BACKSLASH,
|
||||
SC_SCANCODE_NONUSHASH = SDL_SCANCODE_NONUSHASH,
|
||||
SC_SCANCODE_SEMICOLON = SDL_SCANCODE_SEMICOLON,
|
||||
SC_SCANCODE_APOSTROPHE = SDL_SCANCODE_APOSTROPHE,
|
||||
SC_SCANCODE_GRAVE = SDL_SCANCODE_GRAVE,
|
||||
SC_SCANCODE_COMMA = SDL_SCANCODE_COMMA,
|
||||
SC_SCANCODE_PERIOD = SDL_SCANCODE_PERIOD,
|
||||
SC_SCANCODE_SLASH = SDL_SCANCODE_SLASH,
|
||||
|
||||
SC_SCANCODE_CAPSLOCK = SDL_SCANCODE_CAPSLOCK,
|
||||
|
||||
SC_SCANCODE_F1 = SDL_SCANCODE_F1,
|
||||
SC_SCANCODE_F2 = SDL_SCANCODE_F2,
|
||||
SC_SCANCODE_F3 = SDL_SCANCODE_F3,
|
||||
SC_SCANCODE_F4 = SDL_SCANCODE_F4,
|
||||
SC_SCANCODE_F5 = SDL_SCANCODE_F5,
|
||||
SC_SCANCODE_F6 = SDL_SCANCODE_F6,
|
||||
SC_SCANCODE_F7 = SDL_SCANCODE_F7,
|
||||
SC_SCANCODE_F8 = SDL_SCANCODE_F8,
|
||||
SC_SCANCODE_F9 = SDL_SCANCODE_F9,
|
||||
SC_SCANCODE_F10 = SDL_SCANCODE_F10,
|
||||
SC_SCANCODE_F11 = SDL_SCANCODE_F11,
|
||||
SC_SCANCODE_F12 = SDL_SCANCODE_F12,
|
||||
|
||||
SC_SCANCODE_PRINTSCREEN = SDL_SCANCODE_PRINTSCREEN,
|
||||
SC_SCANCODE_SCROLLLOCK = SDL_SCANCODE_SCROLLLOCK,
|
||||
SC_SCANCODE_PAUSE = SDL_SCANCODE_PAUSE,
|
||||
SC_SCANCODE_INSERT = SDL_SCANCODE_INSERT,
|
||||
SC_SCANCODE_HOME = SDL_SCANCODE_HOME,
|
||||
SC_SCANCODE_PAGEUP = SDL_SCANCODE_PAGEUP,
|
||||
SC_SCANCODE_DELETE = SDL_SCANCODE_DELETE,
|
||||
SC_SCANCODE_END = SDL_SCANCODE_END,
|
||||
SC_SCANCODE_PAGEDOWN = SDL_SCANCODE_PAGEDOWN,
|
||||
SC_SCANCODE_RIGHT = SDL_SCANCODE_RIGHT,
|
||||
SC_SCANCODE_LEFT = SDL_SCANCODE_LEFT,
|
||||
SC_SCANCODE_DOWN = SDL_SCANCODE_DOWN,
|
||||
SC_SCANCODE_UP = SDL_SCANCODE_UP,
|
||||
|
||||
SC_SCANCODE_NUMLOCK = SDL_SCANCODE_NUMLOCKCLEAR,
|
||||
SC_SCANCODE_KP_DIVIDE = SDL_SCANCODE_KP_DIVIDE,
|
||||
SC_SCANCODE_KP_MULTIPLY = SDL_SCANCODE_KP_MULTIPLY,
|
||||
SC_SCANCODE_KP_MINUS = SDL_SCANCODE_KP_MINUS,
|
||||
SC_SCANCODE_KP_PLUS = SDL_SCANCODE_KP_PLUS,
|
||||
SC_SCANCODE_KP_ENTER = SDL_SCANCODE_KP_ENTER,
|
||||
SC_SCANCODE_KP_1 = SDL_SCANCODE_KP_1,
|
||||
SC_SCANCODE_KP_2 = SDL_SCANCODE_KP_2,
|
||||
SC_SCANCODE_KP_3 = SDL_SCANCODE_KP_3,
|
||||
SC_SCANCODE_KP_4 = SDL_SCANCODE_KP_4,
|
||||
SC_SCANCODE_KP_5 = SDL_SCANCODE_KP_5,
|
||||
SC_SCANCODE_KP_6 = SDL_SCANCODE_KP_6,
|
||||
SC_SCANCODE_KP_7 = SDL_SCANCODE_KP_7,
|
||||
SC_SCANCODE_KP_8 = SDL_SCANCODE_KP_8,
|
||||
SC_SCANCODE_KP_9 = SDL_SCANCODE_KP_9,
|
||||
SC_SCANCODE_KP_0 = SDL_SCANCODE_KP_0,
|
||||
SC_SCANCODE_KP_PERIOD = SDL_SCANCODE_KP_PERIOD,
|
||||
|
||||
SC_SCANCODE_LCTRL = SDL_SCANCODE_LCTRL,
|
||||
SC_SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT,
|
||||
SC_SCANCODE_LALT = SDL_SCANCODE_LALT,
|
||||
SC_SCANCODE_LGUI = SDL_SCANCODE_LGUI,
|
||||
SC_SCANCODE_RCTRL = SDL_SCANCODE_RCTRL,
|
||||
SC_SCANCODE_RSHIFT = SDL_SCANCODE_RSHIFT,
|
||||
SC_SCANCODE_RALT = SDL_SCANCODE_RALT,
|
||||
SC_SCANCODE_RGUI = SDL_SCANCODE_RGUI,
|
||||
};
|
||||
|
||||
// On purpose, only use the "mask" values (1, 2, 4, 8, 16) for a single button,
|
||||
// to avoid unnecessary conversions (and confusion).
|
||||
enum sc_mouse_button {
|
||||
SC_MOUSE_BUTTON_UNKNOWN = 0,
|
||||
SC_MOUSE_BUTTON_LEFT = SDL_BUTTON(SDL_BUTTON_LEFT),
|
||||
SC_MOUSE_BUTTON_RIGHT = SDL_BUTTON(SDL_BUTTON_RIGHT),
|
||||
SC_MOUSE_BUTTON_MIDDLE = SDL_BUTTON(SDL_BUTTON_MIDDLE),
|
||||
SC_MOUSE_BUTTON_X1 = SDL_BUTTON(SDL_BUTTON_X1),
|
||||
SC_MOUSE_BUTTON_X2 = SDL_BUTTON(SDL_BUTTON_X2),
|
||||
};
|
||||
|
||||
static_assert(sizeof(enum sc_mod) >= sizeof(SDL_Keymod),
|
||||
"SDL_Keymod must be convertible to sc_mod");
|
||||
|
||||
static_assert(sizeof(enum sc_keycode) >= sizeof(SDL_Keycode),
|
||||
"SDL_Keycode must be convertible to sc_keycode");
|
||||
|
||||
static_assert(sizeof(enum sc_scancode) >= sizeof(SDL_Scancode),
|
||||
"SDL_Scancode must be convertible to sc_scancode");
|
||||
|
||||
enum sc_touch_action {
|
||||
SC_TOUCH_ACTION_MOVE,
|
||||
SC_TOUCH_ACTION_DOWN,
|
||||
SC_TOUCH_ACTION_UP,
|
||||
};
|
||||
|
||||
struct sc_key_event {
|
||||
enum sc_action action;
|
||||
enum sc_keycode keycode;
|
||||
enum sc_scancode scancode;
|
||||
uint16_t mods_state; // bitwise-OR of sc_mod values
|
||||
bool repeat;
|
||||
};
|
||||
|
||||
struct sc_text_event {
|
||||
const char *text; // not owned
|
||||
};
|
||||
|
||||
struct sc_mouse_click_event {
|
||||
struct sc_position position;
|
||||
enum sc_action action;
|
||||
enum sc_mouse_button button;
|
||||
uint8_t buttons_state; // bitwise-OR of sc_mouse_button values
|
||||
};
|
||||
|
||||
struct sc_mouse_scroll_event {
|
||||
struct sc_position position;
|
||||
int32_t hscroll;
|
||||
int32_t vscroll;
|
||||
};
|
||||
|
||||
struct sc_mouse_motion_event {
|
||||
struct sc_position position;
|
||||
int32_t xrel;
|
||||
int32_t yrel;
|
||||
uint8_t buttons_state; // bitwise-OR of sc_mouse_button values
|
||||
};
|
||||
|
||||
//enum sc_mouse_event_type {
|
||||
// SC_MOUSE_EVENT_TYPE_CLICK,
|
||||
// SC_MOUSE_EVENT_TYPE_MOVE,
|
||||
// SC_MOUSE_EVENT_TYPE_SCROLL,
|
||||
//};
|
||||
//
|
||||
//struct sc_mouse_event {
|
||||
// enum sc_mouse_event_type type;
|
||||
// struct sc_position position;
|
||||
// uint8_t buttons_state; // bitwise-OR of sc_mouse_button values
|
||||
// union {
|
||||
// struct {
|
||||
// enum sc_action action;
|
||||
// enum sc_mouse_button button;
|
||||
// } click;
|
||||
// struct {
|
||||
// int32_t xrel;
|
||||
// int32_t yrel;
|
||||
// } move;
|
||||
// struct {
|
||||
// int32_t h;
|
||||
// int32_t v;
|
||||
// } scroll;
|
||||
// };
|
||||
//};
|
||||
|
||||
struct sc_touch_event {
|
||||
struct sc_position position;
|
||||
enum sc_touch_action action;
|
||||
uint64_t pointer_id;
|
||||
float pressure;
|
||||
};
|
||||
|
||||
|
||||
//void
|
||||
//sc_key_event_from_sdl(struct sc_key_event *event, const SDL_KeyboardEvent *sdl);
|
||||
//
|
||||
//void
|
||||
//sc_text_event_from_sdl(struct sc_text_event *event,
|
||||
// const SDL_TextInputEvent *sdl);
|
||||
//
|
||||
//void
|
||||
//sc_mouse_click_event_from_sdl(struct sc_mouse_click_event *event,
|
||||
// const SDL_MouseButtonEvent *sdl,
|
||||
// const SDL_Window *window,
|
||||
// struct sc_size screen_size);
|
||||
//
|
||||
//void
|
||||
//sc_mouse_wheel_event_from_sdl(struct sc_mouse_wheel_event *event,
|
||||
// const SDL_MouseWheelEvent *sdl);
|
||||
//
|
||||
//void
|
||||
//sc_mouse_motion_event_from_sdl(struct sc_mouse_motion_event *event,
|
||||
// const SDL_MouseMotionEvent *sdl);
|
||||
//
|
||||
//void
|
||||
//sc_touch_event_from_sdl(struct sc_touch_event *event,
|
||||
// const SDL_TouchFingerEvent *sdl);
|
||||
|
||||
#endif
|
@ -3,33 +3,117 @@
|
||||
#include <assert.h>
|
||||
#include <SDL2/SDL_keycode.h>
|
||||
|
||||
#include "event_converter.h"
|
||||
#include "input_events.h"
|
||||
#include "util/log.h"
|
||||
|
||||
static const int ACTION_DOWN = 1;
|
||||
static const int ACTION_UP = 1 << 1;
|
||||
static inline uint16_t
|
||||
sc_mods_state_from_sdl(uint16_t mods_state) {
|
||||
return mods_state;
|
||||
}
|
||||
|
||||
static inline enum sc_keycode
|
||||
sc_keycode_from_sdl(SDL_Keycode keycode) {
|
||||
return (enum sc_keycode) keycode;
|
||||
}
|
||||
|
||||
static inline enum sc_scancode
|
||||
sc_scancode_from_sdl(SDL_Scancode scancode) {
|
||||
return (enum sc_scancode) scancode;
|
||||
}
|
||||
|
||||
static inline enum sc_action
|
||||
sc_action_from_sdl_keyboard_type(uint32_t type) {
|
||||
assert(type == SDL_KEYDOWN || type == SDL_KEYUP);
|
||||
if (type == SDL_KEYDOWN) {
|
||||
return SC_ACTION_DOWN;
|
||||
}
|
||||
return SC_ACTION_UP;
|
||||
}
|
||||
|
||||
static inline enum sc_action
|
||||
sc_action_from_sdl_mousebutton_type(uint32_t type) {
|
||||
assert(type == SDL_MOUSEBUTTONDOWN || type == SDL_MOUSEBUTTONUP);
|
||||
if (type == SDL_MOUSEBUTTONDOWN) {
|
||||
return SC_ACTION_DOWN;
|
||||
}
|
||||
return SC_ACTION_UP;
|
||||
}
|
||||
|
||||
static inline enum sc_touch_action
|
||||
sc_touch_action_from_sdl(uint32_t type) {
|
||||
assert(type == SDL_FINGERMOTION || type == SDL_FINGERDOWN ||
|
||||
type == SDL_FINGERUP);
|
||||
if (type == SDL_FINGERMOTION) {
|
||||
return SC_TOUCH_ACTION_MOVE;
|
||||
}
|
||||
if (type == SDL_FINGERDOWN) {
|
||||
return SC_TOUCH_ACTION_DOWN;
|
||||
}
|
||||
return SC_TOUCH_ACTION_UP;
|
||||
}
|
||||
|
||||
static inline enum sc_mouse_button
|
||||
sc_mouse_button_from_sdl(uint8_t button) {
|
||||
if (button >= SDL_BUTTON_LEFT && button <= SDL_BUTTON_X2) {
|
||||
// SC_MOUSE_BUTTON_* constants are initialized from SDL_BUTTON(index)
|
||||
return SDL_BUTTON(button);
|
||||
}
|
||||
|
||||
return SC_MOUSE_BUTTON_UNKNOWN;
|
||||
}
|
||||
|
||||
static inline uint8_t
|
||||
sc_mouse_buttons_state_from_sdl(uint32_t buttons_state) {
|
||||
assert(buttons_state < 0x100); // fits in uint8_t
|
||||
return buttons_state;
|
||||
}
|
||||
|
||||
//void
|
||||
//sc_key_event_from_sdl(struct sc_key_event *event,
|
||||
// const SDL_KeyboardEvent *sdl) {
|
||||
// event->action = sc_action_from_sdl_keyboard_type(sdl->type);
|
||||
// event->keycode = sc_keycode_from_sdl(sdl->keysym.sym);
|
||||
// event->scancode = sc_scancode_from_sdl(sdl->keysym.scancode);
|
||||
// event->repeat = sdl->repeat;
|
||||
// event->mods_state = sc_mods_state_from_sdl(sdl->keysym.mod);
|
||||
//}
|
||||
//
|
||||
//void
|
||||
//sc_text_event_from_sdl(struct sc_text_event *event,
|
||||
// const SDL_TextInputEvent *sdl) {
|
||||
// event->text = sdl->text;
|
||||
//}
|
||||
//
|
||||
//void
|
||||
//sc_mouse_click_event_from_sdl(struct sc_mouse_click_event *event,
|
||||
// const SDL_MouseButtonEvent *sdl,
|
||||
// struct sc_size screen_size) {
|
||||
// event->action = sc_action_from_sdl_mousebutton_type(sdl->type);
|
||||
// event->button = sc_mouse_button_from_sdl(sdl->button);
|
||||
// event->position.screen_size = screen_size;
|
||||
//}
|
||||
|
||||
#define SC_SDL_SHORTCUT_MODS_MASK (KMOD_CTRL | KMOD_ALT | KMOD_GUI)
|
||||
|
||||
static inline uint16_t
|
||||
to_sdl_mod(unsigned mod) {
|
||||
to_sdl_mod(unsigned shortcut_mod) {
|
||||
uint16_t sdl_mod = 0;
|
||||
if (mod & SC_MOD_LCTRL) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_LCTRL) {
|
||||
sdl_mod |= KMOD_LCTRL;
|
||||
}
|
||||
if (mod & SC_MOD_RCTRL) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_RCTRL) {
|
||||
sdl_mod |= KMOD_RCTRL;
|
||||
}
|
||||
if (mod & SC_MOD_LALT) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_LALT) {
|
||||
sdl_mod |= KMOD_LALT;
|
||||
}
|
||||
if (mod & SC_MOD_RALT) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_RALT) {
|
||||
sdl_mod |= KMOD_RALT;
|
||||
}
|
||||
if (mod & SC_MOD_LSUPER) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_LSUPER) {
|
||||
sdl_mod |= KMOD_LGUI;
|
||||
}
|
||||
if (mod & SC_MOD_RSUPER) {
|
||||
if (shortcut_mod & SC_SHORTCUT_MOD_RSUPER) {
|
||||
sdl_mod |= KMOD_RGUI;
|
||||
}
|
||||
return sdl_mod;
|
||||
@ -53,17 +137,21 @@ is_shortcut_mod(struct input_manager *im, uint16_t sdl_mod) {
|
||||
|
||||
void
|
||||
input_manager_init(struct input_manager *im, struct controller *controller,
|
||||
struct screen *screen,
|
||||
struct screen *screen, struct sc_key_processor *kp,
|
||||
struct sc_mouse_processor *mp,
|
||||
const struct scrcpy_options *options) {
|
||||
assert(!options->control || (kp && kp->ops));
|
||||
assert(!options->control || (mp && mp->ops));
|
||||
|
||||
im->controller = controller;
|
||||
im->screen = screen;
|
||||
im->repeat = 0;
|
||||
im->kp = kp;
|
||||
im->mp = mp;
|
||||
|
||||
im->control = options->control;
|
||||
im->forward_key_repeat = options->forward_key_repeat;
|
||||
im->prefer_text = options->prefer_text;
|
||||
im->forward_all_clicks = options->forward_all_clicks;
|
||||
im->legacy_paste = options->legacy_paste;
|
||||
im->clipboard_autosync = options->clipboard_autosync;
|
||||
|
||||
const struct sc_shortcut_mods *shortcut_mods = &options->shortcut_mods;
|
||||
assert(shortcut_mods->count);
|
||||
@ -80,6 +168,8 @@ input_manager_init(struct input_manager *im, struct controller *controller,
|
||||
im->last_keycode = SDLK_UNKNOWN;
|
||||
im->last_mod = 0;
|
||||
im->key_repeat = 0;
|
||||
|
||||
im->next_sequence = 1; // 0 is reserved for SC_SEQUENCE_INVALID
|
||||
}
|
||||
|
||||
static void
|
||||
@ -92,7 +182,7 @@ send_keycode(struct controller *controller, enum android_keycode keycode,
|
||||
msg.inject_keycode.metastate = 0;
|
||||
msg.inject_keycode.repeat = 0;
|
||||
|
||||
if (actions & ACTION_DOWN) {
|
||||
if (actions & SC_ACTION_DOWN) {
|
||||
msg.inject_keycode.action = AKEY_EVENT_ACTION_DOWN;
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'inject %s (DOWN)'", name);
|
||||
@ -100,7 +190,7 @@ send_keycode(struct controller *controller, enum android_keycode keycode,
|
||||
}
|
||||
}
|
||||
|
||||
if (actions & ACTION_UP) {
|
||||
if (actions & SC_ACTION_UP) {
|
||||
msg.inject_keycode.action = AKEY_EVENT_ACTION_UP;
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'inject %s (UP)'", name);
|
||||
@ -143,16 +233,6 @@ action_menu(struct controller *controller, int actions) {
|
||||
send_keycode(controller, AKEYCODE_MENU, actions, "MENU");
|
||||
}
|
||||
|
||||
static inline void
|
||||
action_copy(struct controller *controller, int actions) {
|
||||
send_keycode(controller, AKEYCODE_COPY, actions, "COPY");
|
||||
}
|
||||
|
||||
static inline void
|
||||
action_cut(struct controller *controller, int actions) {
|
||||
send_keycode(controller, AKEYCODE_CUT, actions, "CUT");
|
||||
}
|
||||
|
||||
// turn the screen on if it was off, press BACK otherwise
|
||||
// If the screen is off, it is turned on only on ACTION_DOWN
|
||||
static void
|
||||
@ -160,7 +240,7 @@ press_back_or_turn_screen_on(struct controller *controller, int actions) {
|
||||
struct control_msg msg;
|
||||
msg.type = CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
|
||||
|
||||
if (actions & ACTION_DOWN) {
|
||||
if (actions & SC_ACTION_DOWN) {
|
||||
msg.back_or_screen_on.action = AKEY_EVENT_ACTION_DOWN;
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'press back or turn screen on'");
|
||||
@ -168,7 +248,7 @@ press_back_or_turn_screen_on(struct controller *controller, int actions) {
|
||||
}
|
||||
}
|
||||
|
||||
if (actions & ACTION_UP) {
|
||||
if (actions & SC_ACTION_UP) {
|
||||
msg.back_or_screen_on.action = AKEY_EVENT_ACTION_UP;
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'press back or turn screen on'");
|
||||
@ -206,35 +286,50 @@ collapse_panels(struct controller *controller) {
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
set_device_clipboard(struct controller *controller, bool paste) {
|
||||
static bool
|
||||
get_device_clipboard(struct controller *controller,
|
||||
enum get_clipboard_copy_key copy_key) {
|
||||
struct control_msg msg;
|
||||
msg.type = CONTROL_MSG_TYPE_GET_CLIPBOARD;
|
||||
msg.get_clipboard.copy_key = copy_key;
|
||||
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'get device clipboard'");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
set_device_clipboard(struct controller *controller, bool paste,
|
||||
uint64_t sequence) {
|
||||
char *text = SDL_GetClipboardText();
|
||||
if (!text) {
|
||||
LOGW("Could not get clipboard text: %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
if (!*text) {
|
||||
// empty text
|
||||
SDL_free(text);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
char *text_dup = strdup(text);
|
||||
SDL_free(text);
|
||||
if (!text_dup) {
|
||||
LOGW("Could not strdup input text");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
|
||||
msg.set_clipboard.sequence = sequence;
|
||||
msg.set_clipboard.text = text_dup;
|
||||
msg.set_clipboard.paste = paste;
|
||||
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
free(text_dup);
|
||||
LOGW("Could not request 'set device clipboard'");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
@ -323,32 +418,18 @@ input_manager_process_text_input(struct input_manager *im,
|
||||
// A shortcut must never generate text events
|
||||
return;
|
||||
}
|
||||
if (!im->prefer_text) {
|
||||
char c = event->text[0];
|
||||
if (isalpha(c) || c == ' ') {
|
||||
assert(event->text[1] == '\0');
|
||||
// letters and space are handled as raw key event
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
||||
msg.inject_text.text = strdup(event->text);
|
||||
if (!msg.inject_text.text) {
|
||||
LOGW("Could not strdup input text");
|
||||
return;
|
||||
}
|
||||
if (!controller_push_msg(im->controller, &msg)) {
|
||||
free(msg.inject_text.text);
|
||||
LOGW("Could not request 'inject text'");
|
||||
}
|
||||
struct sc_text_event evt = {
|
||||
.text = event->text,
|
||||
};
|
||||
|
||||
im->kp->ops->process_text(im->kp, &evt);
|
||||
}
|
||||
|
||||
static bool
|
||||
simulate_virtual_finger(struct input_manager *im,
|
||||
enum android_motionevent_action action,
|
||||
struct point point) {
|
||||
struct sc_point point) {
|
||||
bool up = action == AMOTION_EVENT_ACTION_UP;
|
||||
|
||||
struct control_msg msg;
|
||||
@ -368,34 +449,13 @@ simulate_virtual_finger(struct input_manager *im,
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct point
|
||||
inverse_point(struct point point, struct size size) {
|
||||
static struct sc_point
|
||||
inverse_point(struct sc_point point, struct sc_size size) {
|
||||
point.x = size.width - point.x;
|
||||
point.y = size.height - point.y;
|
||||
return point;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to,
|
||||
bool prefer_text, uint32_t repeat) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
||||
|
||||
if (!convert_keycode_action(from->type, &to->inject_keycode.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t mod = from->keysym.mod;
|
||||
if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod,
|
||||
prefer_text)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
to->inject_keycode.repeat = repeat;
|
||||
to->inject_keycode.metastate = convert_meta_state(mod);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
input_manager_process_key(struct input_manager *im,
|
||||
const SDL_KeyboardEvent *event) {
|
||||
@ -425,7 +485,7 @@ input_manager_process_key(struct input_manager *im,
|
||||
|
||||
// The shortcut modifier is pressed
|
||||
if (smod) {
|
||||
int action = down ? ACTION_DOWN : ACTION_UP;
|
||||
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
|
||||
switch (keycode) {
|
||||
case SDLK_h:
|
||||
if (control && !shift && !repeat) {
|
||||
@ -484,13 +544,15 @@ input_manager_process_key(struct input_manager *im,
|
||||
}
|
||||
return;
|
||||
case SDLK_c:
|
||||
if (control && !shift && !repeat) {
|
||||
action_copy(controller, action);
|
||||
if (control && !shift && !repeat && down) {
|
||||
get_device_clipboard(controller,
|
||||
GET_CLIPBOARD_COPY_KEY_COPY);
|
||||
}
|
||||
return;
|
||||
case SDLK_x:
|
||||
if (control && !shift && !repeat) {
|
||||
action_cut(controller, action);
|
||||
if (control && !shift && !repeat && down) {
|
||||
get_device_clipboard(controller,
|
||||
GET_CLIPBOARD_COPY_KEY_CUT);
|
||||
}
|
||||
return;
|
||||
case SDLK_v:
|
||||
@ -499,8 +561,10 @@ input_manager_process_key(struct input_manager *im,
|
||||
// inject the text as input events
|
||||
clipboard_paste(controller);
|
||||
} else {
|
||||
// store the text in the device clipboard and paste
|
||||
set_device_clipboard(controller, true);
|
||||
// store the text in the device clipboard and paste,
|
||||
// without requesting an acknowledgment
|
||||
set_device_clipboard(controller, true,
|
||||
SC_SEQUENCE_INVALID);
|
||||
}
|
||||
}
|
||||
return;
|
||||
@ -549,47 +613,44 @@ input_manager_process_key(struct input_manager *im,
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->repeat) {
|
||||
if (!im->forward_key_repeat) {
|
||||
return;
|
||||
}
|
||||
++im->repeat;
|
||||
} else {
|
||||
im->repeat = 0;
|
||||
}
|
||||
|
||||
if (ctrl && !shift && keycode == SDLK_v && down && !repeat) {
|
||||
uint64_t ack_to_wait = SC_SEQUENCE_INVALID;
|
||||
bool is_ctrl_v = ctrl && !shift && keycode == SDLK_v && down && !repeat;
|
||||
if (im->clipboard_autosync && is_ctrl_v) {
|
||||
if (im->legacy_paste) {
|
||||
// inject the text as input events
|
||||
clipboard_paste(controller);
|
||||
return;
|
||||
}
|
||||
|
||||
// Request an acknowledgement only if necessary
|
||||
uint64_t sequence = im->kp->async_paste ? im->next_sequence
|
||||
: SC_SEQUENCE_INVALID;
|
||||
|
||||
// Synchronize the computer clipboard to the device clipboard before
|
||||
// sending Ctrl+v, to allow seamless copy-paste.
|
||||
set_device_clipboard(controller, false);
|
||||
}
|
||||
bool ok = set_device_clipboard(controller, false, sequence);
|
||||
if (!ok) {
|
||||
LOGW("Clipboard could not be synchronized, Ctrl+v not injected");
|
||||
return;
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
if (convert_input_key(event, &msg, im->prefer_text, im->repeat)) {
|
||||
if (!controller_push_msg(controller, &msg)) {
|
||||
LOGW("Could not request 'inject keycode'");
|
||||
if (im->kp->async_paste) {
|
||||
// The key processor must wait for this ack before injecting Ctrl+v
|
||||
ack_to_wait = sequence;
|
||||
// Increment only when the request succeeded
|
||||
++im->next_sequence;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
to->inject_touch_event.action = AMOTION_EVENT_ACTION_MOVE;
|
||||
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_window_to_frame_coords(screen, from->x, from->y);
|
||||
to->inject_touch_event.pressure = 1.f;
|
||||
to->inject_touch_event.buttons = convert_mouse_buttons(from->state);
|
||||
struct sc_key_event evt = {
|
||||
.action = sc_action_from_sdl_keyboard_type(event->type),
|
||||
.keycode = sc_keycode_from_sdl(event->keysym.sym),
|
||||
.scancode = sc_scancode_from_sdl(event->keysym.scancode),
|
||||
.repeat = event->repeat,
|
||||
.mods_state = sc_mods_state_from_sdl(event->keysym.mod),
|
||||
};
|
||||
|
||||
return true;
|
||||
im->kp->ops->process_key(im->kp, &evt, ack_to_wait);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -607,79 +668,51 @@ input_manager_process_mouse_motion(struct input_manager *im,
|
||||
// simulated from touch events, so it's a duplicate
|
||||
return;
|
||||
}
|
||||
struct control_msg msg;
|
||||
if (!convert_mouse_motion(event, im->screen, &msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!controller_push_msg(im->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse motion event'");
|
||||
}
|
||||
struct sc_mouse_motion_event evt = {
|
||||
.position = {
|
||||
.screen_size = im->screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(im->screen,
|
||||
event->x, event->y),
|
||||
},
|
||||
.xrel = event->xrel,
|
||||
.yrel = event->yrel,
|
||||
.buttons_state = sc_mouse_buttons_state_from_sdl(event->state),
|
||||
};
|
||||
|
||||
im->mp->ops->process_mouse_motion(im->mp, event);
|
||||
|
||||
if (im->vfinger_down) {
|
||||
struct point mouse = msg.inject_touch_event.position.point;
|
||||
struct point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
struct sc_point mouse =
|
||||
screen_convert_window_to_frame_coords(im->screen, event->x,
|
||||
event->y);
|
||||
struct sc_point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
simulate_virtual_finger(im, AMOTION_EVENT_ACTION_MOVE, vfinger);
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_touch(const SDL_TouchFingerEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
|
||||
if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
to->inject_touch_event.pointer_id = from->fingerId;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
|
||||
int dw;
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
// SDL touch event coordinates are normalized in the range [0; 1]
|
||||
int32_t x = from->x * dw;
|
||||
int32_t y = from->y * dh;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_drawable_to_frame_coords(screen, x, y);
|
||||
|
||||
to->inject_touch_event.pressure = from->pressure;
|
||||
to->inject_touch_event.buttons = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
input_manager_process_touch(struct input_manager *im,
|
||||
const SDL_TouchFingerEvent *event) {
|
||||
struct control_msg msg;
|
||||
if (convert_touch(event, im->screen, &msg)) {
|
||||
if (!controller_push_msg(im->controller, &msg)) {
|
||||
LOGW("Could not request 'inject touch event'");
|
||||
}
|
||||
}
|
||||
}
|
||||
int dw;
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(im->screen->window, &dw, &dh);
|
||||
|
||||
static bool
|
||||
convert_mouse_button(const SDL_MouseButtonEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
// SDL touch event coordinates are normalized in the range [0; 1]
|
||||
int32_t x = event->x * dw;
|
||||
int32_t y = event->y * dh;
|
||||
|
||||
if (!convert_mouse_action(from->type, &to->inject_touch_event.action)) {
|
||||
return false;
|
||||
}
|
||||
struct sc_touch_event evt = {
|
||||
.position = {
|
||||
.screen_size = im->screen->frame_size,
|
||||
.point = screen_convert_drawable_to_frame_coords(im->screen, x, y),
|
||||
},
|
||||
.action = sc_touch_action_from_sdl(event->type),
|
||||
.pointer_id = event->fingerId,
|
||||
.pressure = event->pressure,
|
||||
};
|
||||
|
||||
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_window_to_frame_coords(screen, from->x, from->y);
|
||||
to->inject_touch_event.pressure =
|
||||
from->type == SDL_MOUSEBUTTONDOWN ? 1.f : 0.f;
|
||||
to->inject_touch_event.buttons =
|
||||
convert_mouse_buttons(SDL_BUTTON(from->button));
|
||||
|
||||
return true;
|
||||
im->mp->ops->process_touch(im->mp, event);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -694,7 +727,7 @@ input_manager_process_mouse_button(struct input_manager *im,
|
||||
|
||||
bool down = event->type == SDL_MOUSEBUTTONDOWN;
|
||||
if (!im->forward_all_clicks) {
|
||||
int action = down ? ACTION_DOWN : ACTION_UP;
|
||||
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
|
||||
|
||||
if (control && event->button == SDL_BUTTON_X1) {
|
||||
action_app_switch(im->controller, action);
|
||||
@ -739,15 +772,20 @@ input_manager_process_mouse_button(struct input_manager *im,
|
||||
return;
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
if (!convert_mouse_button(event, im->screen, &msg)) {
|
||||
return;
|
||||
}
|
||||
uint32_t sdl_buttons_state = SDL_GetMouseState(NULL, NULL);
|
||||
|
||||
if (!controller_push_msg(im->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse button event'");
|
||||
return;
|
||||
}
|
||||
struct sc_mouse_click_event evt = {
|
||||
.position = {
|
||||
.screen_size = im->screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(im->screen, event->x,
|
||||
event->y),
|
||||
},
|
||||
.action = sc_action_from_sdl_mousebutton_type(event->type),
|
||||
.button = sc_mouse_button_from_sdl(event->button),
|
||||
.buttons_state = sc_mouse_buttons_state_from_sdl(sdl_buttons_state),
|
||||
};
|
||||
|
||||
im->mp->ops->process_mouse_button(im->mp, event);
|
||||
|
||||
// Pinch-to-zoom simulation.
|
||||
//
|
||||
@ -761,8 +799,10 @@ input_manager_process_mouse_button(struct input_manager *im,
|
||||
#define CTRL_PRESSED (SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL))
|
||||
if ((down && !im->vfinger_down && CTRL_PRESSED)
|
||||
|| (!down && im->vfinger_down)) {
|
||||
struct point mouse = msg.inject_touch_event.position.point;
|
||||
struct point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
struct sc_point mouse =
|
||||
screen_convert_window_to_frame_coords(im->screen, event->x,
|
||||
event->y);
|
||||
struct sc_point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
enum android_motionevent_action action = down
|
||||
? AMOTION_EVENT_ACTION_DOWN
|
||||
: AMOTION_EVENT_ACTION_UP;
|
||||
@ -773,39 +813,25 @@ input_manager_process_mouse_button(struct input_manager *im,
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
|
||||
static void
|
||||
input_manager_process_mouse_wheel(struct input_manager *im,
|
||||
const SDL_MouseWheelEvent *event) {
|
||||
// mouse_x and mouse_y are expressed in pixels relative to the window
|
||||
int mouse_x;
|
||||
int mouse_y;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
|
||||
struct position position = {
|
||||
.screen_size = screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(screen,
|
||||
mouse_x, mouse_y),
|
||||
struct sc_mouse_scroll_event evt = {
|
||||
.position = {
|
||||
.screen_size = im->screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(im->screen,
|
||||
mouse_x, mouse_y),
|
||||
},
|
||||
.hscroll = event->x,
|
||||
.vscroll = event->y,
|
||||
};
|
||||
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT;
|
||||
|
||||
to->inject_scroll_event.position = position;
|
||||
to->inject_scroll_event.hscroll = from->x;
|
||||
to->inject_scroll_event.vscroll = from->y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
input_manager_process_mouse_wheel(struct input_manager *im,
|
||||
const SDL_MouseWheelEvent *event) {
|
||||
struct control_msg msg;
|
||||
if (convert_mouse_wheel(event, im->screen, &msg)) {
|
||||
if (!controller_push_msg(im->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse wheel event'");
|
||||
}
|
||||
}
|
||||
im->mp->ops->process_mouse_wheel(im->mp, event);
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -9,22 +9,22 @@
|
||||
|
||||
#include "controller.h"
|
||||
#include "fps_counter.h"
|
||||
#include "scrcpy.h"
|
||||
#include "options.h"
|
||||
#include "screen.h"
|
||||
#include "trait/key_processor.h"
|
||||
#include "trait/mouse_processor.h"
|
||||
|
||||
struct input_manager {
|
||||
struct controller *controller;
|
||||
struct screen *screen;
|
||||
|
||||
// SDL reports repeated events as a boolean, but Android expects the actual
|
||||
// number of repetitions. This variable keeps track of the count.
|
||||
unsigned repeat;
|
||||
struct sc_key_processor *kp;
|
||||
struct sc_mouse_processor *mp;
|
||||
|
||||
bool control;
|
||||
bool forward_key_repeat;
|
||||
bool prefer_text;
|
||||
bool forward_all_clicks;
|
||||
bool legacy_paste;
|
||||
bool clipboard_autosync;
|
||||
|
||||
struct {
|
||||
unsigned data[SC_MAX_SHORTCUT_MODS];
|
||||
@ -39,11 +39,15 @@ struct input_manager {
|
||||
unsigned key_repeat;
|
||||
SDL_Keycode last_keycode;
|
||||
uint16_t last_mod;
|
||||
|
||||
uint64_t next_sequence; // used for request acknowledgements
|
||||
};
|
||||
|
||||
void
|
||||
input_manager_init(struct input_manager *im, struct controller *controller,
|
||||
struct screen *screen, const struct scrcpy_options *options);
|
||||
struct screen *screen, struct sc_key_processor *kp,
|
||||
struct sc_mouse_processor *mp,
|
||||
const struct scrcpy_options *options);
|
||||
|
||||
bool
|
||||
input_manager_handle_event(struct input_manager *im, SDL_Event *event);
|
||||
|
353
app/src/keyboard_inject.c
Normal file
353
app/src/keyboard_inject.c
Normal file
@ -0,0 +1,353 @@
|
||||
#include "keyboard_inject.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "android/input.h"
|
||||
#include "control_msg.h"
|
||||
#include "controller.h"
|
||||
#include "input_events.h"
|
||||
#include "util/intmap.h"
|
||||
#include "util/log.h"
|
||||
|
||||
/** Downcast key processor to sc_keyboard_inject */
|
||||
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
|
||||
|
||||
static bool
|
||||
convert_keycode_action(enum sc_action from, enum android_keyevent_action *to) {
|
||||
static const struct sc_intmap_entry actions[] = {
|
||||
{SC_ACTION_DOWN, AKEY_EVENT_ACTION_DOWN},
|
||||
{SC_ACTION_UP, AKEY_EVENT_ACTION_UP},
|
||||
};
|
||||
|
||||
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_keycode(enum sc_keycode from, enum android_keycode *to, uint16_t mod,
|
||||
enum sc_key_inject_mode key_inject_mode) {
|
||||
// Navigation keys and ENTER.
|
||||
// Used in all modes.
|
||||
static const struct sc_intmap_entry special_keys[] = {
|
||||
{SC_KEYCODE_RETURN, AKEYCODE_ENTER},
|
||||
{SC_KEYCODE_KP_ENTER, AKEYCODE_NUMPAD_ENTER},
|
||||
{SC_KEYCODE_ESCAPE, AKEYCODE_ESCAPE},
|
||||
{SC_KEYCODE_BACKSPACE, AKEYCODE_DEL},
|
||||
{SC_KEYCODE_TAB, AKEYCODE_TAB},
|
||||
{SC_KEYCODE_PAGEUP, AKEYCODE_PAGE_UP},
|
||||
{SC_KEYCODE_DELETE, AKEYCODE_FORWARD_DEL},
|
||||
{SC_KEYCODE_HOME, AKEYCODE_MOVE_HOME},
|
||||
{SC_KEYCODE_END, AKEYCODE_MOVE_END},
|
||||
{SC_KEYCODE_PAGEDOWN, AKEYCODE_PAGE_DOWN},
|
||||
{SC_KEYCODE_RIGHT, AKEYCODE_DPAD_RIGHT},
|
||||
{SC_KEYCODE_LEFT, AKEYCODE_DPAD_LEFT},
|
||||
{SC_KEYCODE_DOWN, AKEYCODE_DPAD_DOWN},
|
||||
{SC_KEYCODE_UP, AKEYCODE_DPAD_UP},
|
||||
{SC_KEYCODE_LCTRL, AKEYCODE_CTRL_LEFT},
|
||||
{SC_KEYCODE_RCTRL, AKEYCODE_CTRL_RIGHT},
|
||||
{SC_KEYCODE_LSHIFT, AKEYCODE_SHIFT_LEFT},
|
||||
{SC_KEYCODE_RSHIFT, AKEYCODE_SHIFT_RIGHT},
|
||||
};
|
||||
|
||||
// Numpad navigation keys.
|
||||
// Used in all modes, when NumLock and Shift are disabled.
|
||||
static const struct sc_intmap_entry kp_nav_keys[] = {
|
||||
{SC_KEYCODE_KP_0, AKEYCODE_INSERT},
|
||||
{SC_KEYCODE_KP_1, AKEYCODE_MOVE_END},
|
||||
{SC_KEYCODE_KP_2, AKEYCODE_DPAD_DOWN},
|
||||
{SC_KEYCODE_KP_3, AKEYCODE_PAGE_DOWN},
|
||||
{SC_KEYCODE_KP_4, AKEYCODE_DPAD_LEFT},
|
||||
{SC_KEYCODE_KP_6, AKEYCODE_DPAD_RIGHT},
|
||||
{SC_KEYCODE_KP_7, AKEYCODE_MOVE_HOME},
|
||||
{SC_KEYCODE_KP_8, AKEYCODE_DPAD_UP},
|
||||
{SC_KEYCODE_KP_9, AKEYCODE_PAGE_UP},
|
||||
{SC_KEYCODE_KP_PERIOD, AKEYCODE_FORWARD_DEL},
|
||||
};
|
||||
|
||||
// Letters and space.
|
||||
// Used in non-text mode.
|
||||
static const struct sc_intmap_entry alphaspace_keys[] = {
|
||||
{SC_KEYCODE_a, AKEYCODE_A},
|
||||
{SC_KEYCODE_b, AKEYCODE_B},
|
||||
{SC_KEYCODE_c, AKEYCODE_C},
|
||||
{SC_KEYCODE_d, AKEYCODE_D},
|
||||
{SC_KEYCODE_e, AKEYCODE_E},
|
||||
{SC_KEYCODE_f, AKEYCODE_F},
|
||||
{SC_KEYCODE_g, AKEYCODE_G},
|
||||
{SC_KEYCODE_h, AKEYCODE_H},
|
||||
{SC_KEYCODE_i, AKEYCODE_I},
|
||||
{SC_KEYCODE_j, AKEYCODE_J},
|
||||
{SC_KEYCODE_k, AKEYCODE_K},
|
||||
{SC_KEYCODE_l, AKEYCODE_L},
|
||||
{SC_KEYCODE_m, AKEYCODE_M},
|
||||
{SC_KEYCODE_n, AKEYCODE_N},
|
||||
{SC_KEYCODE_o, AKEYCODE_O},
|
||||
{SC_KEYCODE_p, AKEYCODE_P},
|
||||
{SC_KEYCODE_q, AKEYCODE_Q},
|
||||
{SC_KEYCODE_r, AKEYCODE_R},
|
||||
{SC_KEYCODE_s, AKEYCODE_S},
|
||||
{SC_KEYCODE_t, AKEYCODE_T},
|
||||
{SC_KEYCODE_u, AKEYCODE_U},
|
||||
{SC_KEYCODE_v, AKEYCODE_V},
|
||||
{SC_KEYCODE_w, AKEYCODE_W},
|
||||
{SC_KEYCODE_x, AKEYCODE_X},
|
||||
{SC_KEYCODE_y, AKEYCODE_Y},
|
||||
{SC_KEYCODE_z, AKEYCODE_Z},
|
||||
{SC_KEYCODE_SPACE, AKEYCODE_SPACE},
|
||||
};
|
||||
|
||||
// Numbers and punctuation keys.
|
||||
// Used in raw mode only.
|
||||
static const struct sc_intmap_entry numbers_punct_keys[] = {
|
||||
{SC_KEYCODE_HASH, AKEYCODE_POUND},
|
||||
{SC_KEYCODE_PERCENT, AKEYCODE_PERIOD},
|
||||
{SC_KEYCODE_QUOTE, AKEYCODE_APOSTROPHE},
|
||||
{SC_KEYCODE_ASTERISK, AKEYCODE_STAR},
|
||||
{SC_KEYCODE_PLUS, AKEYCODE_PLUS},
|
||||
{SC_KEYCODE_COMMA, AKEYCODE_COMMA},
|
||||
{SC_KEYCODE_MINUS, AKEYCODE_MINUS},
|
||||
{SC_KEYCODE_PERIOD, AKEYCODE_PERIOD},
|
||||
{SC_KEYCODE_SLASH, AKEYCODE_SLASH},
|
||||
{SC_KEYCODE_0, AKEYCODE_0},
|
||||
{SC_KEYCODE_1, AKEYCODE_1},
|
||||
{SC_KEYCODE_2, AKEYCODE_2},
|
||||
{SC_KEYCODE_3, AKEYCODE_3},
|
||||
{SC_KEYCODE_4, AKEYCODE_4},
|
||||
{SC_KEYCODE_5, AKEYCODE_5},
|
||||
{SC_KEYCODE_6, AKEYCODE_6},
|
||||
{SC_KEYCODE_7, AKEYCODE_7},
|
||||
{SC_KEYCODE_8, AKEYCODE_8},
|
||||
{SC_KEYCODE_9, AKEYCODE_9},
|
||||
{SC_KEYCODE_SEMICOLON, AKEYCODE_SEMICOLON},
|
||||
{SC_KEYCODE_EQUALS, AKEYCODE_EQUALS},
|
||||
{SC_KEYCODE_AT, AKEYCODE_AT},
|
||||
{SC_KEYCODE_LEFTBRACKET, AKEYCODE_LEFT_BRACKET},
|
||||
{SC_KEYCODE_BACKSLASH, AKEYCODE_BACKSLASH},
|
||||
{SC_KEYCODE_RIGHTBRACKET, AKEYCODE_RIGHT_BRACKET},
|
||||
{SC_KEYCODE_BACKQUOTE, AKEYCODE_GRAVE},
|
||||
{SC_KEYCODE_KP_1, AKEYCODE_NUMPAD_1},
|
||||
{SC_KEYCODE_KP_2, AKEYCODE_NUMPAD_2},
|
||||
{SC_KEYCODE_KP_3, AKEYCODE_NUMPAD_3},
|
||||
{SC_KEYCODE_KP_4, AKEYCODE_NUMPAD_4},
|
||||
{SC_KEYCODE_KP_5, AKEYCODE_NUMPAD_5},
|
||||
{SC_KEYCODE_KP_6, AKEYCODE_NUMPAD_6},
|
||||
{SC_KEYCODE_KP_7, AKEYCODE_NUMPAD_7},
|
||||
{SC_KEYCODE_KP_8, AKEYCODE_NUMPAD_8},
|
||||
{SC_KEYCODE_KP_9, AKEYCODE_NUMPAD_9},
|
||||
{SC_KEYCODE_KP_0, AKEYCODE_NUMPAD_0},
|
||||
{SC_KEYCODE_KP_DIVIDE, AKEYCODE_NUMPAD_DIVIDE},
|
||||
{SC_KEYCODE_KP_MULTIPLY, AKEYCODE_NUMPAD_MULTIPLY},
|
||||
{SC_KEYCODE_KP_MINUS, AKEYCODE_NUMPAD_SUBTRACT},
|
||||
{SC_KEYCODE_KP_PLUS, AKEYCODE_NUMPAD_ADD},
|
||||
{SC_KEYCODE_KP_PERIOD, AKEYCODE_NUMPAD_DOT},
|
||||
{SC_KEYCODE_KP_EQUALS, AKEYCODE_NUMPAD_EQUALS},
|
||||
{SC_KEYCODE_KP_LEFTPAREN, AKEYCODE_NUMPAD_LEFT_PAREN},
|
||||
{SC_KEYCODE_KP_RIGHTPAREN, AKEYCODE_NUMPAD_RIGHT_PAREN},
|
||||
};
|
||||
|
||||
const struct sc_intmap_entry *entry =
|
||||
SC_INTMAP_FIND_ENTRY(special_keys, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(mod & (SC_MOD_NUM | SC_MOD_LSHIFT | SC_MOD_RSHIFT))) {
|
||||
// Handle Numpad events when Num Lock is disabled
|
||||
// If SHIFT is pressed, a text event will be sent instead
|
||||
entry = SC_INTMAP_FIND_ENTRY(kp_nav_keys, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (key_inject_mode == SC_KEY_INJECT_MODE_TEXT &&
|
||||
!(mod & (SC_MOD_LCTRL | SC_MOD_RCTRL))) {
|
||||
// do not forward alpha and space key events (unless Ctrl is pressed)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mod & (SC_MOD_LALT | SC_MOD_RALT | SC_MOD_LGUI | SC_MOD_RGUI)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if ALT and META are not pressed, also handle letters and space
|
||||
entry = SC_INTMAP_FIND_ENTRY(alphaspace_keys, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
|
||||
entry = SC_INTMAP_FIND_ENTRY(numbers_punct_keys, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static enum android_metastate
|
||||
autocomplete_metastate(enum android_metastate metastate) {
|
||||
// fill dependent flags
|
||||
if (metastate & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
|
||||
metastate |= AMETA_SHIFT_ON;
|
||||
}
|
||||
if (metastate & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
|
||||
metastate |= AMETA_CTRL_ON;
|
||||
}
|
||||
if (metastate & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
|
||||
metastate |= AMETA_ALT_ON;
|
||||
}
|
||||
if (metastate & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
|
||||
metastate |= AMETA_META_ON;
|
||||
}
|
||||
|
||||
return metastate;
|
||||
}
|
||||
|
||||
static enum android_metastate
|
||||
convert_meta_state(uint16_t mod) {
|
||||
enum android_metastate metastate = 0;
|
||||
if (mod & SC_MOD_LSHIFT) {
|
||||
metastate |= AMETA_SHIFT_LEFT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_RSHIFT) {
|
||||
metastate |= AMETA_SHIFT_RIGHT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_LCTRL) {
|
||||
metastate |= AMETA_CTRL_LEFT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_RCTRL) {
|
||||
metastate |= AMETA_CTRL_RIGHT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_LALT) {
|
||||
metastate |= AMETA_ALT_LEFT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_RALT) {
|
||||
metastate |= AMETA_ALT_RIGHT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_LGUI) { // Windows key
|
||||
metastate |= AMETA_META_LEFT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_RGUI) { // Windows key
|
||||
metastate |= AMETA_META_RIGHT_ON;
|
||||
}
|
||||
if (mod & SC_MOD_NUM) {
|
||||
metastate |= AMETA_NUM_LOCK_ON;
|
||||
}
|
||||
if (mod & SC_MOD_CAPS) {
|
||||
metastate |= AMETA_CAPS_LOCK_ON;
|
||||
}
|
||||
|
||||
// fill the dependent fields
|
||||
return autocomplete_metastate(metastate);
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_input_key(const struct sc_key_event *event, struct control_msg *msg,
|
||||
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
|
||||
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
||||
|
||||
if (!convert_keycode_action(event->action, &msg->inject_keycode.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
|
||||
event->mods_state, key_inject_mode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
msg->inject_keycode.repeat = repeat;
|
||||
msg->inject_keycode.metastate = convert_meta_state(event->mods_state);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||
const struct sc_key_event *event,
|
||||
uint64_t ack_to_wait) {
|
||||
// The device clipboard synchronization and the key event messages are
|
||||
// serialized, there is nothing special to do to ensure that the clipboard
|
||||
// is set before injecting Ctrl+v.
|
||||
(void) ack_to_wait;
|
||||
|
||||
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
||||
|
||||
if (event->repeat) {
|
||||
if (!ki->forward_key_repeat) {
|
||||
return;
|
||||
}
|
||||
++ki->repeat;
|
||||
} else {
|
||||
ki->repeat = 0;
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
if (convert_input_key(event, &msg, ki->key_inject_mode, ki->repeat)) {
|
||||
if (!controller_push_msg(ki->controller, &msg)) {
|
||||
LOGW("Could not request 'inject keycode'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||
const struct sc_text_event *event) {
|
||||
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
||||
|
||||
if (ki->key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
|
||||
// Never inject text events
|
||||
return;
|
||||
}
|
||||
|
||||
if (ki->key_inject_mode == SC_KEY_INJECT_MODE_MIXED) {
|
||||
char c = event->text[0];
|
||||
if (isalpha(c) || c == ' ') {
|
||||
assert(event->text[1] == '\0');
|
||||
// Letters and space are handled as raw key events
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
struct control_msg msg;
|
||||
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
||||
msg.inject_text.text = strdup(event->text);
|
||||
if (!msg.inject_text.text) {
|
||||
LOGW("Could not strdup input text");
|
||||
return;
|
||||
}
|
||||
if (!controller_push_msg(ki->controller, &msg)) {
|
||||
free(msg.inject_text.text);
|
||||
LOGW("Could not request 'inject text'");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
|
||||
struct controller *controller,
|
||||
const struct scrcpy_options *options) {
|
||||
ki->controller = controller;
|
||||
ki->key_inject_mode = options->key_inject_mode;
|
||||
ki->forward_key_repeat = options->forward_key_repeat;
|
||||
|
||||
ki->repeat = 0;
|
||||
|
||||
static const struct sc_key_processor_ops ops = {
|
||||
.process_key = sc_key_processor_process_key,
|
||||
.process_text = sc_key_processor_process_text,
|
||||
};
|
||||
|
||||
// Key injection and clipboard synchronization are serialized
|
||||
ki->key_processor.async_paste = false;
|
||||
ki->key_processor.ops = &ops;
|
||||
}
|
30
app/src/keyboard_inject.h
Normal file
30
app/src/keyboard_inject.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef SC_KEYBOARD_INJECT_H
|
||||
#define SC_KEYBOARD_INJECT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "controller.h"
|
||||
#include "options.h"
|
||||
#include "trait/key_processor.h"
|
||||
|
||||
struct sc_keyboard_inject {
|
||||
struct sc_key_processor key_processor; // key processor trait
|
||||
|
||||
struct controller *controller;
|
||||
|
||||
// SDL reports repeated events as a boolean, but Android expects the actual
|
||||
// number of repetitions. This variable keeps track of the count.
|
||||
unsigned repeat;
|
||||
|
||||
enum sc_key_inject_mode key_inject_mode;
|
||||
bool forward_key_repeat;
|
||||
};
|
||||
|
||||
void
|
||||
sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
|
||||
struct controller *controller,
|
||||
const struct scrcpy_options *options);
|
||||
|
||||
#endif
|
@ -1,5 +1,3 @@
|
||||
#include "scrcpy.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <assert.h>
|
||||
@ -13,6 +11,8 @@
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "cli.h"
|
||||
#include "options.h"
|
||||
#include "scrcpy.h"
|
||||
#include "util/log.h"
|
||||
|
||||
static void
|
||||
@ -47,8 +47,11 @@ main(int argc, char *argv[]) {
|
||||
setbuf(stderr, NULL);
|
||||
#endif
|
||||
|
||||
printf("scrcpy " SCRCPY_VERSION
|
||||
" <https://github.com/Genymobile/scrcpy>\n");
|
||||
|
||||
struct scrcpy_cli_args args = {
|
||||
.opts = SCRCPY_OPTIONS_DEFAULT,
|
||||
.opts = scrcpy_options_default,
|
||||
.help = false,
|
||||
.version = false,
|
||||
};
|
||||
@ -73,8 +76,6 @@ main(int argc, char *argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
LOGI("scrcpy " SCRCPY_VERSION " <https://github.com/Genymobile/scrcpy>");
|
||||
|
||||
#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL
|
||||
av_register_all();
|
||||
#endif
|
||||
|
224
app/src/mouse_inject.c
Normal file
224
app/src/mouse_inject.c
Normal file
@ -0,0 +1,224 @@
|
||||
#include "mouse_inject.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <SDL2/SDL_events.h>
|
||||
|
||||
#include "android/input.h"
|
||||
#include "control_msg.h"
|
||||
#include "controller.h"
|
||||
#include "util/intmap.h"
|
||||
#include "util/log.h"
|
||||
|
||||
/** Downcast mouse processor to sc_mouse_inject */
|
||||
#define DOWNCAST(MP) container_of(MP, struct sc_mouse_inject, mouse_processor)
|
||||
|
||||
static enum android_motionevent_buttons
|
||||
convert_mouse_buttons(uint32_t state) {
|
||||
enum android_motionevent_buttons buttons = 0;
|
||||
if (state & SDL_BUTTON_LMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_PRIMARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_RMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_SECONDARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_MMASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_TERTIARY;
|
||||
}
|
||||
if (state & SDL_BUTTON_X1MASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_BACK;
|
||||
}
|
||||
if (state & SDL_BUTTON_X2MASK) {
|
||||
buttons |= AMOTION_EVENT_BUTTON_FORWARD;
|
||||
}
|
||||
return buttons;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||
static const struct sc_intmap_entry actions[] = {
|
||||
{SDL_MOUSEBUTTONDOWN, AMOTION_EVENT_ACTION_DOWN},
|
||||
{SDL_MOUSEBUTTONUP, AMOTION_EVENT_ACTION_UP},
|
||||
};
|
||||
|
||||
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||
static const struct sc_intmap_entry actions[] = {
|
||||
{SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE},
|
||||
{SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN},
|
||||
{SDL_FINGERUP, AMOTION_EVENT_ACTION_UP},
|
||||
};
|
||||
|
||||
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||
if (entry) {
|
||||
*to = entry->value;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
to->inject_touch_event.action = AMOTION_EVENT_ACTION_MOVE;
|
||||
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_window_to_frame_coords(screen, from->x, from->y);
|
||||
to->inject_touch_event.pressure = 1.f;
|
||||
to->inject_touch_event.buttons = convert_mouse_buttons(from->state);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_touch(const SDL_TouchFingerEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
|
||||
if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
to->inject_touch_event.pointer_id = from->fingerId;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
|
||||
int dw;
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
// SDL touch event coordinates are normalized in the range [0; 1]
|
||||
int32_t x = from->x * dw;
|
||||
int32_t y = from->y * dh;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_drawable_to_frame_coords(screen, x, y);
|
||||
|
||||
to->inject_touch_event.pressure = from->pressure;
|
||||
to->inject_touch_event.buttons = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_button(const SDL_MouseButtonEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
|
||||
if (!convert_mouse_action(from->type, &to->inject_touch_event.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
||||
to->inject_touch_event.position.screen_size = screen->frame_size;
|
||||
to->inject_touch_event.position.point =
|
||||
screen_convert_window_to_frame_coords(screen, from->x, from->y);
|
||||
to->inject_touch_event.pressure =
|
||||
from->type == SDL_MOUSEBUTTONDOWN ? 1.f : 0.f;
|
||||
to->inject_touch_event.buttons =
|
||||
convert_mouse_buttons(SDL_BUTTON(from->button));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct screen *screen,
|
||||
struct control_msg *to) {
|
||||
|
||||
// mouse_x and mouse_y are expressed in pixels relative to the window
|
||||
int mouse_x;
|
||||
int mouse_y;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
|
||||
struct sc_position position = {
|
||||
.screen_size = screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(screen,
|
||||
mouse_x, mouse_y),
|
||||
};
|
||||
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT;
|
||||
|
||||
to->inject_scroll_event.position = position;
|
||||
to->inject_scroll_event.hscroll = from->x;
|
||||
to->inject_scroll_event.vscroll = from->y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseMotionEvent *event) {
|
||||
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
||||
|
||||
struct control_msg msg;
|
||||
if (!convert_mouse_motion(event, mi->screen, &msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!controller_push_msg(mi->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse motion event'");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
|
||||
const SDL_TouchFingerEvent *event) {
|
||||
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
||||
|
||||
struct control_msg msg;
|
||||
if (convert_touch(event, mi->screen, &msg)) {
|
||||
if (!controller_push_msg(mi->controller, &msg)) {
|
||||
LOGW("Could not request 'inject touch event'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sc_mouse_processor_process_mouse_button(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseButtonEvent *event) {
|
||||
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
||||
|
||||
struct control_msg msg;
|
||||
if (convert_mouse_button(event, mi->screen, &msg)) {
|
||||
if (!controller_push_msg(mi->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse button event'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sc_mouse_processor_process_mouse_wheel(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseWheelEvent *event) {
|
||||
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
||||
|
||||
struct control_msg msg;
|
||||
if (convert_mouse_wheel(event, mi->screen, &msg)) {
|
||||
if (!controller_push_msg(mi->controller, &msg)) {
|
||||
LOGW("Could not request 'inject mouse wheel event'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sc_mouse_inject_init(struct sc_mouse_inject *mi, struct controller *controller,
|
||||
struct screen *screen) {
|
||||
mi->controller = controller;
|
||||
mi->screen = screen;
|
||||
|
||||
static const struct sc_mouse_processor_ops ops = {
|
||||
.process_mouse_motion = sc_mouse_processor_process_mouse_motion,
|
||||
.process_touch = sc_mouse_processor_process_touch,
|
||||
.process_mouse_button = sc_mouse_processor_process_mouse_button,
|
||||
.process_mouse_wheel = sc_mouse_processor_process_mouse_wheel,
|
||||
};
|
||||
|
||||
mi->mouse_processor.ops = &ops;
|
||||
}
|
23
app/src/mouse_inject.h
Normal file
23
app/src/mouse_inject.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef SC_MOUSE_INJECT_H
|
||||
#define SC_MOUSE_INJECT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "controller.h"
|
||||
#include "screen.h"
|
||||
#include "trait/mouse_processor.h"
|
||||
|
||||
struct sc_mouse_inject {
|
||||
struct sc_mouse_processor mouse_processor; // mouse processor trait
|
||||
|
||||
struct controller *controller;
|
||||
struct screen *screen;
|
||||
};
|
||||
|
||||
void
|
||||
sc_mouse_inject_init(struct sc_mouse_inject *mi, struct controller *controller,
|
||||
struct screen *screen);
|
||||
|
||||
#endif
|
59
app/src/options.c
Normal file
59
app/src/options.c
Normal file
@ -0,0 +1,59 @@
|
||||
#include "options.h"
|
||||
|
||||
const struct scrcpy_options scrcpy_options_default = {
|
||||
.serial = NULL,
|
||||
.crop = NULL,
|
||||
.record_filename = NULL,
|
||||
.window_title = NULL,
|
||||
.push_target = NULL,
|
||||
.render_driver = NULL,
|
||||
.codec_options = NULL,
|
||||
.encoder_name = NULL,
|
||||
#ifdef HAVE_V4L2
|
||||
.v4l2_device = NULL,
|
||||
#endif
|
||||
.log_level = SC_LOG_LEVEL_INFO,
|
||||
.record_format = SC_RECORD_FORMAT_AUTO,
|
||||
.keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT,
|
||||
.port_range = {
|
||||
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST,
|
||||
.last = DEFAULT_LOCAL_PORT_RANGE_LAST,
|
||||
},
|
||||
.tunnel_host = 0,
|
||||
.tunnel_port = 0,
|
||||
.shortcut_mods = {
|
||||
.data = {SC_SHORTCUT_MOD_LALT, SC_SHORTCUT_MOD_LSUPER},
|
||||
.count = 2,
|
||||
},
|
||||
.max_size = 0,
|
||||
.bit_rate = DEFAULT_BIT_RATE,
|
||||
.max_fps = 0,
|
||||
.lock_video_orientation = SC_LOCK_VIDEO_ORIENTATION_UNLOCKED,
|
||||
.rotation = 0,
|
||||
.window_x = SC_WINDOW_POSITION_UNDEFINED,
|
||||
.window_y = SC_WINDOW_POSITION_UNDEFINED,
|
||||
.window_width = 0,
|
||||
.window_height = 0,
|
||||
.display_id = 0,
|
||||
.display_buffer = 0,
|
||||
.v4l2_buffer = 0,
|
||||
.show_touches = false,
|
||||
.fullscreen = false,
|
||||
.always_on_top = false,
|
||||
.control = true,
|
||||
.display = true,
|
||||
.turn_screen_off = false,
|
||||
.key_inject_mode = SC_KEY_INJECT_MODE_MIXED,
|
||||
.window_borderless = false,
|
||||
.mipmaps = true,
|
||||
.stay_awake = false,
|
||||
.force_adb_forward = false,
|
||||
.disable_screensaver = false,
|
||||
.forward_key_repeat = true,
|
||||
.forward_all_clicks = false,
|
||||
.legacy_paste = false,
|
||||
.power_off_on_close = false,
|
||||
.clipboard_autosync = true,
|
||||
.tcpip = false,
|
||||
.tcpip_dst = NULL,
|
||||
};
|
132
app/src/options.h
Normal file
132
app/src/options.h
Normal file
@ -0,0 +1,132 @@
|
||||
#ifndef SCRCPY_OPTIONS_H
|
||||
#define SCRCPY_OPTIONS_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util/tick.h"
|
||||
|
||||
enum sc_log_level {
|
||||
SC_LOG_LEVEL_VERBOSE,
|
||||
SC_LOG_LEVEL_DEBUG,
|
||||
SC_LOG_LEVEL_INFO,
|
||||
SC_LOG_LEVEL_WARN,
|
||||
SC_LOG_LEVEL_ERROR,
|
||||
};
|
||||
|
||||
enum sc_record_format {
|
||||
SC_RECORD_FORMAT_AUTO,
|
||||
SC_RECORD_FORMAT_MP4,
|
||||
SC_RECORD_FORMAT_MKV,
|
||||
};
|
||||
|
||||
enum sc_lock_video_orientation {
|
||||
SC_LOCK_VIDEO_ORIENTATION_UNLOCKED = -1,
|
||||
// lock the current orientation when scrcpy starts
|
||||
SC_LOCK_VIDEO_ORIENTATION_INITIAL = -2,
|
||||
SC_LOCK_VIDEO_ORIENTATION_0 = 0,
|
||||
SC_LOCK_VIDEO_ORIENTATION_1,
|
||||
SC_LOCK_VIDEO_ORIENTATION_2,
|
||||
SC_LOCK_VIDEO_ORIENTATION_3,
|
||||
};
|
||||
|
||||
enum sc_keyboard_input_mode {
|
||||
SC_KEYBOARD_INPUT_MODE_INJECT,
|
||||
SC_KEYBOARD_INPUT_MODE_HID,
|
||||
};
|
||||
|
||||
enum sc_key_inject_mode {
|
||||
// Inject special keys, letters and space as key events.
|
||||
// Inject numbers and punctuation as text events.
|
||||
// This is the default mode.
|
||||
SC_KEY_INJECT_MODE_MIXED,
|
||||
|
||||
// Inject special keys as key events.
|
||||
// Inject letters and space, numbers and punctuation as text events.
|
||||
SC_KEY_INJECT_MODE_TEXT,
|
||||
|
||||
// Inject everything as key events.
|
||||
SC_KEY_INJECT_MODE_RAW,
|
||||
};
|
||||
|
||||
#define SC_MAX_SHORTCUT_MODS 8
|
||||
|
||||
enum sc_shortcut_mod {
|
||||
SC_SHORTCUT_MOD_LCTRL = 1 << 0,
|
||||
SC_SHORTCUT_MOD_RCTRL = 1 << 1,
|
||||
SC_SHORTCUT_MOD_LALT = 1 << 2,
|
||||
SC_SHORTCUT_MOD_RALT = 1 << 3,
|
||||
SC_SHORTCUT_MOD_LSUPER = 1 << 4,
|
||||
SC_SHORTCUT_MOD_RSUPER = 1 << 5,
|
||||
};
|
||||
|
||||
struct sc_shortcut_mods {
|
||||
unsigned data[SC_MAX_SHORTCUT_MODS];
|
||||
unsigned count;
|
||||
};
|
||||
|
||||
struct sc_port_range {
|
||||
uint16_t first;
|
||||
uint16_t last;
|
||||
};
|
||||
|
||||
#define SC_WINDOW_POSITION_UNDEFINED (-0x8000)
|
||||
|
||||
struct scrcpy_options {
|
||||
const char *serial;
|
||||
const char *crop;
|
||||
const char *record_filename;
|
||||
const char *window_title;
|
||||
const char *push_target;
|
||||
const char *render_driver;
|
||||
const char *codec_options;
|
||||
const char *encoder_name;
|
||||
#ifdef HAVE_V4L2
|
||||
const char *v4l2_device;
|
||||
#endif
|
||||
enum sc_log_level log_level;
|
||||
enum sc_record_format record_format;
|
||||
enum sc_keyboard_input_mode keyboard_input_mode;
|
||||
struct sc_port_range port_range;
|
||||
uint32_t tunnel_host;
|
||||
uint16_t tunnel_port;
|
||||
struct sc_shortcut_mods shortcut_mods;
|
||||
uint16_t max_size;
|
||||
uint32_t bit_rate;
|
||||
uint16_t max_fps;
|
||||
enum sc_lock_video_orientation lock_video_orientation;
|
||||
uint8_t rotation;
|
||||
int16_t window_x; // SC_WINDOW_POSITION_UNDEFINED for "auto"
|
||||
int16_t window_y; // SC_WINDOW_POSITION_UNDEFINED for "auto"
|
||||
uint16_t window_width;
|
||||
uint16_t window_height;
|
||||
uint32_t display_id;
|
||||
sc_tick display_buffer;
|
||||
sc_tick v4l2_buffer;
|
||||
bool show_touches;
|
||||
bool fullscreen;
|
||||
bool always_on_top;
|
||||
bool control;
|
||||
bool display;
|
||||
bool turn_screen_off;
|
||||
enum sc_key_inject_mode key_inject_mode;
|
||||
bool window_borderless;
|
||||
bool mipmaps;
|
||||
bool stay_awake;
|
||||
bool force_adb_forward;
|
||||
bool disable_screensaver;
|
||||
bool forward_key_repeat;
|
||||
bool forward_all_clicks;
|
||||
bool legacy_paste;
|
||||
bool power_off_on_close;
|
||||
bool clipboard_autosync;
|
||||
bool tcpip;
|
||||
const char *tcpip_dst;
|
||||
};
|
||||
|
||||
extern const struct scrcpy_options scrcpy_options_default;
|
||||
|
||||
#endif
|
@ -7,12 +7,16 @@
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
receiver_init(struct receiver *receiver, socket_t control_socket) {
|
||||
receiver_init(struct receiver *receiver, sc_socket control_socket,
|
||||
struct sc_acksync *acksync) {
|
||||
bool ok = sc_mutex_init(&receiver->mutex);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
receiver->control_socket = control_socket;
|
||||
receiver->acksync = acksync;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -22,7 +26,7 @@ receiver_destroy(struct receiver *receiver) {
|
||||
}
|
||||
|
||||
static void
|
||||
process_msg(struct device_msg *msg) {
|
||||
process_msg(struct receiver *receiver, struct device_msg *msg) {
|
||||
switch (msg->type) {
|
||||
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
||||
char *current = SDL_GetClipboardText();
|
||||
@ -37,11 +41,17 @@ process_msg(struct device_msg *msg) {
|
||||
SDL_SetClipboardText(msg->clipboard.text);
|
||||
break;
|
||||
}
|
||||
case DEVICE_MSG_TYPE_ACK_CLIPBOARD:
|
||||
assert(receiver->acksync);
|
||||
LOGD("Ack device clipboard sequence=%" PRIu64_,
|
||||
msg->ack_clipboard.sequence);
|
||||
sc_acksync_ack(receiver->acksync, msg->ack_clipboard.sequence);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t
|
||||
process_msgs(const unsigned char *buf, size_t len) {
|
||||
process_msgs(struct receiver *receiver, const unsigned char *buf, size_t len) {
|
||||
size_t head = 0;
|
||||
for (;;) {
|
||||
struct device_msg msg;
|
||||
@ -53,7 +63,7 @@ process_msgs(const unsigned char *buf, size_t len) {
|
||||
return head;
|
||||
}
|
||||
|
||||
process_msg(&msg);
|
||||
process_msg(receiver, &msg);
|
||||
device_msg_destroy(&msg);
|
||||
|
||||
head += r;
|
||||
@ -81,7 +91,7 @@ run_receiver(void *data) {
|
||||
}
|
||||
|
||||
head += r;
|
||||
ssize_t consumed = process_msgs(buf, head);
|
||||
ssize_t consumed = process_msgs(receiver, buf, head);
|
||||
if (consumed == -1) {
|
||||
// an error occurred
|
||||
break;
|
||||
@ -101,8 +111,8 @@ bool
|
||||
receiver_start(struct receiver *receiver) {
|
||||
LOGD("Starting receiver thread");
|
||||
|
||||
bool ok = sc_thread_create(&receiver->thread, run_receiver, "receiver",
|
||||
receiver);
|
||||
bool ok = sc_thread_create(&receiver->thread, run_receiver,
|
||||
"scrcpy-receiver", receiver);
|
||||
if (!ok) {
|
||||
LOGC("Could not start receiver thread");
|
||||
return false;
|
||||
|
@ -5,19 +5,23 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "util/acksync.h"
|
||||
#include "util/net.h"
|
||||
#include "util/thread.h"
|
||||
|
||||
// receive events from the device
|
||||
// managed by the controller
|
||||
struct receiver {
|
||||
socket_t control_socket;
|
||||
sc_socket control_socket;
|
||||
sc_thread thread;
|
||||
sc_mutex mutex;
|
||||
|
||||
struct sc_acksync *acksync;
|
||||
};
|
||||
|
||||
bool
|
||||
receiver_init(struct receiver *receiver, socket_t control_socket);
|
||||
receiver_init(struct receiver *receiver, sc_socket control_socket,
|
||||
struct sc_acksync *acksync);
|
||||
|
||||
void
|
||||
receiver_destroy(struct receiver *receiver);
|
||||
|
@ -1,10 +1,12 @@
|
||||
#include "recorder.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/time.h>
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/str_util.h"
|
||||
#include "util/str.h"
|
||||
|
||||
/** Downcast packet_sink to recorder */
|
||||
#define DOWNCAST(SINK) container_of(SINK, struct recorder, packet_sink)
|
||||
@ -24,7 +26,7 @@ find_muxer(const char *name) {
|
||||
oformat = av_oformat_next(oformat);
|
||||
#endif
|
||||
// until null or containing the requested name
|
||||
} while (oformat && !strlist_contains(oformat->name, ',', name));
|
||||
} while (oformat && !sc_str_list_contains(oformat->name, ',', name));
|
||||
return oformat;
|
||||
}
|
||||
|
||||
@ -32,11 +34,13 @@ static struct record_packet *
|
||||
record_packet_new(const AVPacket *packet) {
|
||||
struct record_packet *rec = malloc(sizeof(*rec));
|
||||
if (!rec) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rec->packet = av_packet_alloc();
|
||||
if (!rec->packet) {
|
||||
LOG_OOM();
|
||||
free(rec);
|
||||
return NULL;
|
||||
}
|
||||
@ -79,7 +83,7 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
||||
|
||||
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
||||
if (!extradata) {
|
||||
LOGC("Could not allocate extradata");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -226,13 +230,11 @@ static bool
|
||||
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
||||
bool ok = sc_mutex_init(&recorder->mutex);
|
||||
if (!ok) {
|
||||
LOGC("Could not create mutex");
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_cond_init(&recorder->queue_cond);
|
||||
if (!ok) {
|
||||
LOGC("Could not create cond");
|
||||
goto error_mutex_destroy;
|
||||
}
|
||||
|
||||
@ -252,7 +254,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
||||
|
||||
recorder->ctx = avformat_alloc_context();
|
||||
if (!recorder->ctx) {
|
||||
LOGE("Could not allocate output context");
|
||||
LOG_OOM();
|
||||
goto error_cond_destroy;
|
||||
}
|
||||
|
||||
@ -285,8 +287,8 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
||||
}
|
||||
|
||||
LOGD("Starting recorder thread");
|
||||
ok = sc_thread_create(&recorder->thread, run_recorder, "recorder",
|
||||
recorder);
|
||||
ok = sc_thread_create(&recorder->thread, run_recorder, "scrcpy-recorder",
|
||||
recorder);
|
||||
if (!ok) {
|
||||
LOGC("Could not start recorder thread");
|
||||
goto error_avio_close;
|
||||
@ -336,7 +338,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
||||
|
||||
struct record_packet *rec = record_packet_new(packet);
|
||||
if (!rec) {
|
||||
LOGC("Could not allocate record packet");
|
||||
LOG_OOM();
|
||||
sc_mutex_unlock(&recorder->mutex);
|
||||
return false;
|
||||
}
|
||||
@ -370,10 +372,10 @@ bool
|
||||
recorder_init(struct recorder *recorder,
|
||||
const char *filename,
|
||||
enum sc_record_format format,
|
||||
struct size declared_frame_size) {
|
||||
struct sc_size declared_frame_size) {
|
||||
recorder->filename = strdup(filename);
|
||||
if (!recorder->filename) {
|
||||
LOGE("Could not strdup filename");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
#include "coords.h"
|
||||
#include "scrcpy.h"
|
||||
#include "options.h"
|
||||
#include "trait/packet_sink.h"
|
||||
#include "util/queue.h"
|
||||
#include "util/thread.h"
|
||||
@ -25,7 +25,7 @@ struct recorder {
|
||||
char *filename;
|
||||
enum sc_record_format format;
|
||||
AVFormatContext *ctx;
|
||||
struct size declared_frame_size;
|
||||
struct sc_size declared_frame_size;
|
||||
bool header_written;
|
||||
|
||||
sc_thread thread;
|
||||
@ -44,7 +44,7 @@ struct recorder {
|
||||
|
||||
bool
|
||||
recorder_init(struct recorder *recorder, const char *filename,
|
||||
enum sc_record_format format, struct size declared_frame_size);
|
||||
enum sc_record_format format, struct sc_size declared_frame_size);
|
||||
|
||||
void
|
||||
recorder_destroy(struct recorder *recorder);
|
||||
|
302
app/src/scrcpy.c
302
app/src/scrcpy.c
@ -18,11 +18,16 @@
|
||||
#include "events.h"
|
||||
#include "file_handler.h"
|
||||
#include "input_manager.h"
|
||||
#ifdef HAVE_AOA_HID
|
||||
# include "hid_keyboard.h"
|
||||
#endif
|
||||
#include "keyboard_inject.h"
|
||||
#include "mouse_inject.h"
|
||||
#include "recorder.h"
|
||||
#include "screen.h"
|
||||
#include "server.h"
|
||||
#include "stream.h"
|
||||
#include "tiny_xpm.h"
|
||||
#include "util/acksync.h"
|
||||
#include "util/log.h"
|
||||
#include "util/net.h"
|
||||
#ifdef HAVE_V4L2
|
||||
@ -30,7 +35,7 @@
|
||||
#endif
|
||||
|
||||
struct scrcpy {
|
||||
struct server server;
|
||||
struct sc_server server;
|
||||
struct screen screen;
|
||||
struct stream stream;
|
||||
struct decoder decoder;
|
||||
@ -40,44 +45,45 @@ struct scrcpy {
|
||||
#endif
|
||||
struct controller controller;
|
||||
struct file_handler file_handler;
|
||||
#ifdef HAVE_AOA_HID
|
||||
struct sc_aoa aoa;
|
||||
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
|
||||
struct sc_acksync acksync;
|
||||
#endif
|
||||
union {
|
||||
struct sc_keyboard_inject keyboard_inject;
|
||||
#ifdef HAVE_AOA_HID
|
||||
struct sc_hid_keyboard keyboard_hid;
|
||||
#endif
|
||||
};
|
||||
struct sc_mouse_inject mouse_inject;
|
||||
struct input_manager input_manager;
|
||||
};
|
||||
|
||||
static inline void
|
||||
push_event(uint32_t type, const char *name) {
|
||||
SDL_Event event;
|
||||
event.type = type;
|
||||
int ret = SDL_PushEvent(&event);
|
||||
if (ret < 0) {
|
||||
LOGE("Could not post %s event: %s", name, SDL_GetError());
|
||||
// What could we do?
|
||||
}
|
||||
}
|
||||
#define PUSH_EVENT(TYPE) push_event(TYPE, # TYPE)
|
||||
|
||||
#ifdef _WIN32
|
||||
BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
|
||||
if (ctrl_type == CTRL_C_EVENT) {
|
||||
SDL_Event event;
|
||||
event.type = SDL_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
PUSH_EVENT(SDL_QUIT);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
// init SDL and set appropriate hints
|
||||
static bool
|
||||
sdl_init_and_configure(bool display, const char *render_driver,
|
||||
bool disable_screensaver) {
|
||||
uint32_t flags = display ? SDL_INIT_VIDEO : SDL_INIT_EVENTS;
|
||||
if (SDL_Init(flags)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
atexit(SDL_Quit);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Clean up properly on Ctrl+C on Windows
|
||||
bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
|
||||
if (!ok) {
|
||||
LOGW("Could not set Ctrl+C handler");
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
if (!display) {
|
||||
return true;
|
||||
}
|
||||
static void
|
||||
sdl_set_hints(const char *render_driver) {
|
||||
|
||||
if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
|
||||
LOGW("Could not set render driver");
|
||||
@ -88,11 +94,18 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
LOGW("Could not enable linear filtering");
|
||||
}
|
||||
|
||||
#ifdef SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
||||
// Handle a click to gain focus as any other click
|
||||
if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
|
||||
LOGW("Could not enable mouse focus clickthrough");
|
||||
}
|
||||
|
||||
#ifdef SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||
// Disable synthetic mouse events from touch events
|
||||
// Touch events with id SDL_TOUCH_MOUSEID are ignored anyway, but it is
|
||||
// better not to generate them in the first place.
|
||||
if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0")) {
|
||||
LOGW("Could not disable synthetic mouse events");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
|
||||
@ -106,6 +119,21 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
|
||||
LOGW("Could not disable minimize on focus loss");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sdl_configure(bool display, bool disable_screensaver) {
|
||||
#ifdef _WIN32
|
||||
// Clean up properly on Ctrl+C on Windows
|
||||
bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
|
||||
if (!ok) {
|
||||
LOGW("Could not set Ctrl+C handler");
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
if (!display) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (disable_screensaver) {
|
||||
LOGD("Screensaver disabled");
|
||||
@ -114,8 +142,6 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
LOGD("Screensaver enabled");
|
||||
SDL_EnableScreenSaver();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
@ -192,6 +218,29 @@ event_loop(struct scrcpy *s, const struct scrcpy_options *options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
await_for_server(void) {
|
||||
SDL_Event event;
|
||||
while (SDL_WaitEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
LOGD("User requested to quit");
|
||||
return false;
|
||||
case EVENT_SERVER_CONNECTION_FAILED:
|
||||
LOGE("Server connection failed");
|
||||
return false;
|
||||
case EVENT_SERVER_CONNECTED:
|
||||
LOGD("Server connected");
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
LOGE("SDL_WaitEvent() error: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
static SDL_LogPriority
|
||||
sdl_priority_from_av_level(int level) {
|
||||
switch (level) {
|
||||
@ -220,7 +269,7 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
||||
size_t fmt_len = strlen(fmt);
|
||||
char *local_fmt = malloc(fmt_len + 10);
|
||||
if (!local_fmt) {
|
||||
LOGC("Could not allocate string");
|
||||
LOG_OOM();
|
||||
return;
|
||||
}
|
||||
memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0'
|
||||
@ -234,20 +283,48 @@ stream_on_eos(struct stream *stream, void *userdata) {
|
||||
(void) stream;
|
||||
(void) userdata;
|
||||
|
||||
SDL_Event stop_event;
|
||||
stop_event.type = EVENT_STREAM_STOPPED;
|
||||
SDL_PushEvent(&stop_event);
|
||||
PUSH_EVENT(EVENT_STREAM_STOPPED);
|
||||
}
|
||||
|
||||
static void
|
||||
sc_server_on_connection_failed(struct sc_server *server, void *userdata) {
|
||||
(void) server;
|
||||
(void) userdata;
|
||||
|
||||
PUSH_EVENT(EVENT_SERVER_CONNECTION_FAILED);
|
||||
}
|
||||
|
||||
static void
|
||||
sc_server_on_connected(struct sc_server *server, void *userdata) {
|
||||
(void) server;
|
||||
(void) userdata;
|
||||
|
||||
PUSH_EVENT(EVENT_SERVER_CONNECTED);
|
||||
}
|
||||
|
||||
static void
|
||||
sc_server_on_disconnected(struct sc_server *server, void *userdata) {
|
||||
(void) server;
|
||||
(void) userdata;
|
||||
|
||||
LOGD("Server disconnected");
|
||||
// Do nothing, the disconnection will be handled by the "stream stopped"
|
||||
// event
|
||||
}
|
||||
|
||||
bool
|
||||
scrcpy(const struct scrcpy_options *options) {
|
||||
scrcpy(struct scrcpy_options *options) {
|
||||
static struct scrcpy scrcpy;
|
||||
struct scrcpy *s = &scrcpy;
|
||||
|
||||
if (!server_init(&s->server)) {
|
||||
// Minimal SDL initialization
|
||||
if (SDL_Init(SDL_INIT_EVENTS)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
atexit(SDL_Quit);
|
||||
|
||||
bool ret = false;
|
||||
|
||||
bool server_started = false;
|
||||
@ -257,16 +334,22 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
bool v4l2_sink_initialized = false;
|
||||
#endif
|
||||
bool stream_started = false;
|
||||
#ifdef HAVE_AOA_HID
|
||||
bool aoa_hid_initialized = false;
|
||||
#endif
|
||||
bool controller_initialized = false;
|
||||
bool controller_started = false;
|
||||
bool screen_initialized = false;
|
||||
|
||||
bool record = !!options->record_filename;
|
||||
struct server_params params = {
|
||||
struct sc_acksync *acksync = NULL;
|
||||
|
||||
struct sc_server_params params = {
|
||||
.serial = options->serial,
|
||||
.log_level = options->log_level,
|
||||
.crop = options->crop,
|
||||
.port_range = options->port_range,
|
||||
.tunnel_host = options->tunnel_host,
|
||||
.tunnel_port = options->tunnel_port,
|
||||
.max_size = options->max_size,
|
||||
.bit_rate = options->bit_rate,
|
||||
.max_fps = options->max_fps,
|
||||
@ -279,27 +362,51 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
.encoder_name = options->encoder_name,
|
||||
.force_adb_forward = options->force_adb_forward,
|
||||
.power_off_on_close = options->power_off_on_close,
|
||||
.clipboard_autosync = options->clipboard_autosync,
|
||||
.tcpip = options->tcpip,
|
||||
.tcpip_dst = options->tcpip_dst,
|
||||
};
|
||||
if (!server_start(&s->server, ¶ms)) {
|
||||
|
||||
static const struct sc_server_callbacks cbs = {
|
||||
.on_connection_failed = sc_server_on_connection_failed,
|
||||
.on_connected = sc_server_on_connected,
|
||||
.on_disconnected = sc_server_on_disconnected,
|
||||
};
|
||||
if (!sc_server_init(&s->server, ¶ms, &cbs, NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sc_server_start(&s->server)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
server_started = true;
|
||||
|
||||
if (!sdl_init_and_configure(options->display, options->render_driver,
|
||||
options->disable_screensaver)) {
|
||||
if (options->display) {
|
||||
sdl_set_hints(options->render_driver);
|
||||
}
|
||||
|
||||
// Initialize SDL video in addition if display is enabled
|
||||
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
goto end;
|
||||
}
|
||||
|
||||
char device_name[DEVICE_NAME_FIELD_LENGTH];
|
||||
struct size frame_size;
|
||||
sdl_configure(options->display, options->disable_screensaver);
|
||||
|
||||
if (!server_connect_to(&s->server, device_name, &frame_size)) {
|
||||
// Await for server without blocking Ctrl+C handling
|
||||
if (!await_for_server()) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
// It is necessarily initialized here, since the device is connected
|
||||
struct sc_server_info *info = &s->server.info;
|
||||
|
||||
const char *serial = s->server.params.serial;
|
||||
assert(serial);
|
||||
|
||||
if (options->display && options->control) {
|
||||
if (!file_handler_init(&s->file_handler, s->server.serial,
|
||||
if (!file_handler_init(&s->file_handler, serial,
|
||||
options->push_target)) {
|
||||
goto end;
|
||||
}
|
||||
@ -317,11 +424,11 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
}
|
||||
|
||||
struct recorder *rec = NULL;
|
||||
if (record) {
|
||||
if (options->record_filename) {
|
||||
if (!recorder_init(&s->recorder,
|
||||
options->record_filename,
|
||||
options->record_format,
|
||||
frame_size)) {
|
||||
info->frame_size)) {
|
||||
goto end;
|
||||
}
|
||||
rec = &s->recorder;
|
||||
@ -344,7 +451,18 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
}
|
||||
|
||||
if (options->control) {
|
||||
if (!controller_init(&s->controller, s->server.control_socket)) {
|
||||
#ifdef HAVE_AOA_HID
|
||||
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID) {
|
||||
bool ok = sc_acksync_init(&s->acksync);
|
||||
if (!ok) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
acksync = &s->acksync;
|
||||
}
|
||||
#endif
|
||||
if (!controller_init(&s->controller, s->server.control_socket,
|
||||
acksync)) {
|
||||
goto end;
|
||||
}
|
||||
controller_initialized = true;
|
||||
@ -367,11 +485,11 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
|
||||
if (options->display) {
|
||||
const char *window_title =
|
||||
options->window_title ? options->window_title : device_name;
|
||||
options->window_title ? options->window_title : info->device_name;
|
||||
|
||||
struct screen_params screen_params = {
|
||||
.window_title = window_title,
|
||||
.frame_size = frame_size,
|
||||
.frame_size = info->frame_size,
|
||||
.always_on_top = options->always_on_top,
|
||||
.window_x = options->window_x,
|
||||
.window_y = options->window_y,
|
||||
@ -394,8 +512,8 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
|
||||
#ifdef HAVE_V4L2
|
||||
if (options->v4l2_device) {
|
||||
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size,
|
||||
options->v4l2_buffer)) {
|
||||
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device,
|
||||
info->frame_size, options->v4l2_buffer)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
@ -412,7 +530,63 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
}
|
||||
stream_started = true;
|
||||
|
||||
input_manager_init(&s->input_manager, &s->controller, &s->screen, options);
|
||||
struct sc_key_processor *kp = NULL;
|
||||
struct sc_mouse_processor *mp = NULL;
|
||||
|
||||
if (options->control) {
|
||||
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID) {
|
||||
#ifdef HAVE_AOA_HID
|
||||
bool aoa_hid_ok = false;
|
||||
|
||||
bool ok = sc_aoa_init(&s->aoa, serial, acksync);
|
||||
if (!ok) {
|
||||
goto aoa_hid_end;
|
||||
}
|
||||
|
||||
if (!sc_hid_keyboard_init(&s->keyboard_hid, &s->aoa)) {
|
||||
sc_aoa_destroy(&s->aoa);
|
||||
goto aoa_hid_end;
|
||||
}
|
||||
|
||||
if (!sc_aoa_start(&s->aoa)) {
|
||||
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
||||
sc_aoa_destroy(&s->aoa);
|
||||
goto aoa_hid_end;
|
||||
}
|
||||
|
||||
aoa_hid_ok = true;
|
||||
kp = &s->keyboard_hid.key_processor;
|
||||
|
||||
aoa_hid_initialized = true;
|
||||
|
||||
aoa_hid_end:
|
||||
if (!aoa_hid_ok) {
|
||||
LOGE("Failed to enable HID over AOA, "
|
||||
"fallback to default keyboard injection method "
|
||||
"(-K/--hid-keyboard ignored)");
|
||||
options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT;
|
||||
}
|
||||
#else
|
||||
LOGE("HID over AOA is not supported on this platform, "
|
||||
"fallback to default keyboard injection method "
|
||||
"(-K/--hid-keyboard ignored)");
|
||||
options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT;
|
||||
#endif
|
||||
}
|
||||
|
||||
// keyboard_input_mode may have been reset if HID mode failed
|
||||
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_INJECT) {
|
||||
sc_keyboard_inject_init(&s->keyboard_inject, &s->controller,
|
||||
options);
|
||||
kp = &s->keyboard_inject.key_processor;
|
||||
}
|
||||
|
||||
sc_mouse_inject_init(&s->mouse_inject, &s->controller, &s->screen);
|
||||
mp = &s->mouse_inject.mouse_processor;
|
||||
}
|
||||
|
||||
input_manager_init(&s->input_manager, &s->controller, &s->screen, kp, mp,
|
||||
options);
|
||||
|
||||
ret = event_loop(s, options);
|
||||
LOGD("quit...");
|
||||
@ -424,6 +598,15 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
end:
|
||||
// The stream is not stopped explicitly, because it will stop by itself on
|
||||
// end-of-stream
|
||||
#ifdef HAVE_AOA_HID
|
||||
if (aoa_hid_initialized) {
|
||||
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
||||
sc_aoa_stop(&s->aoa);
|
||||
}
|
||||
if (acksync) {
|
||||
sc_acksync_destroy(acksync);
|
||||
}
|
||||
#endif
|
||||
if (controller_started) {
|
||||
controller_stop(&s->controller);
|
||||
}
|
||||
@ -436,7 +619,7 @@ end:
|
||||
|
||||
if (server_started) {
|
||||
// shutdown the sockets and kill the server
|
||||
server_stop(&s->server);
|
||||
sc_server_stop(&s->server);
|
||||
}
|
||||
|
||||
// now that the sockets are shutdown, the stream and controller are
|
||||
@ -451,6 +634,13 @@ end:
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_AOA_HID
|
||||
if (aoa_hid_initialized) {
|
||||
sc_aoa_join(&s->aoa);
|
||||
sc_aoa_destroy(&s->aoa);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Destroy the screen only after the stream is guaranteed to be finished,
|
||||
// because otherwise the screen could receive new frames after destruction
|
||||
if (screen_initialized) {
|
||||
@ -474,7 +664,7 @@ end:
|
||||
file_handler_destroy(&s->file_handler);
|
||||
}
|
||||
|
||||
server_destroy(&s->server);
|
||||
sc_server_destroy(&s->server);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
148
app/src/scrcpy.h
148
app/src/scrcpy.h
@ -4,153 +4,9 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util/tick.h"
|
||||
|
||||
enum sc_log_level {
|
||||
SC_LOG_LEVEL_VERBOSE,
|
||||
SC_LOG_LEVEL_DEBUG,
|
||||
SC_LOG_LEVEL_INFO,
|
||||
SC_LOG_LEVEL_WARN,
|
||||
SC_LOG_LEVEL_ERROR,
|
||||
};
|
||||
|
||||
enum sc_record_format {
|
||||
SC_RECORD_FORMAT_AUTO,
|
||||
SC_RECORD_FORMAT_MP4,
|
||||
SC_RECORD_FORMAT_MKV,
|
||||
};
|
||||
|
||||
enum sc_lock_video_orientation {
|
||||
SC_LOCK_VIDEO_ORIENTATION_UNLOCKED = -1,
|
||||
// lock the current orientation when scrcpy starts
|
||||
SC_LOCK_VIDEO_ORIENTATION_INITIAL = -2,
|
||||
SC_LOCK_VIDEO_ORIENTATION_0 = 0,
|
||||
SC_LOCK_VIDEO_ORIENTATION_1,
|
||||
SC_LOCK_VIDEO_ORIENTATION_2,
|
||||
SC_LOCK_VIDEO_ORIENTATION_3,
|
||||
};
|
||||
|
||||
#define SC_MAX_SHORTCUT_MODS 8
|
||||
|
||||
enum sc_shortcut_mod {
|
||||
SC_MOD_LCTRL = 1 << 0,
|
||||
SC_MOD_RCTRL = 1 << 1,
|
||||
SC_MOD_LALT = 1 << 2,
|
||||
SC_MOD_RALT = 1 << 3,
|
||||
SC_MOD_LSUPER = 1 << 4,
|
||||
SC_MOD_RSUPER = 1 << 5,
|
||||
};
|
||||
|
||||
struct sc_shortcut_mods {
|
||||
unsigned data[SC_MAX_SHORTCUT_MODS];
|
||||
unsigned count;
|
||||
};
|
||||
|
||||
struct sc_port_range {
|
||||
uint16_t first;
|
||||
uint16_t last;
|
||||
};
|
||||
|
||||
#define SC_WINDOW_POSITION_UNDEFINED (-0x8000)
|
||||
|
||||
struct scrcpy_options {
|
||||
const char *serial;
|
||||
const char *crop;
|
||||
const char *record_filename;
|
||||
const char *window_title;
|
||||
const char *push_target;
|
||||
const char *render_driver;
|
||||
const char *codec_options;
|
||||
const char *encoder_name;
|
||||
const char *v4l2_device;
|
||||
enum sc_log_level log_level;
|
||||
enum sc_record_format record_format;
|
||||
struct sc_port_range port_range;
|
||||
struct sc_shortcut_mods shortcut_mods;
|
||||
uint16_t max_size;
|
||||
uint32_t bit_rate;
|
||||
uint16_t max_fps;
|
||||
enum sc_lock_video_orientation lock_video_orientation;
|
||||
uint8_t rotation;
|
||||
int16_t window_x; // SC_WINDOW_POSITION_UNDEFINED for "auto"
|
||||
int16_t window_y; // SC_WINDOW_POSITION_UNDEFINED for "auto"
|
||||
uint16_t window_width;
|
||||
uint16_t window_height;
|
||||
uint32_t display_id;
|
||||
sc_tick display_buffer;
|
||||
sc_tick v4l2_buffer;
|
||||
bool show_touches;
|
||||
bool fullscreen;
|
||||
bool always_on_top;
|
||||
bool control;
|
||||
bool display;
|
||||
bool turn_screen_off;
|
||||
bool prefer_text;
|
||||
bool window_borderless;
|
||||
bool mipmaps;
|
||||
bool stay_awake;
|
||||
bool force_adb_forward;
|
||||
bool disable_screensaver;
|
||||
bool forward_key_repeat;
|
||||
bool forward_all_clicks;
|
||||
bool legacy_paste;
|
||||
bool power_off_on_close;
|
||||
};
|
||||
|
||||
#define SCRCPY_OPTIONS_DEFAULT { \
|
||||
.serial = NULL, \
|
||||
.crop = NULL, \
|
||||
.record_filename = NULL, \
|
||||
.window_title = NULL, \
|
||||
.push_target = NULL, \
|
||||
.render_driver = NULL, \
|
||||
.codec_options = NULL, \
|
||||
.encoder_name = NULL, \
|
||||
.v4l2_device = NULL, \
|
||||
.log_level = SC_LOG_LEVEL_INFO, \
|
||||
.record_format = SC_RECORD_FORMAT_AUTO, \
|
||||
.port_range = { \
|
||||
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST, \
|
||||
.last = DEFAULT_LOCAL_PORT_RANGE_LAST, \
|
||||
}, \
|
||||
.shortcut_mods = { \
|
||||
.data = {SC_MOD_LALT, SC_MOD_LSUPER}, \
|
||||
.count = 2, \
|
||||
}, \
|
||||
.max_size = 0, \
|
||||
.bit_rate = DEFAULT_BIT_RATE, \
|
||||
.max_fps = 0, \
|
||||
.lock_video_orientation = SC_LOCK_VIDEO_ORIENTATION_UNLOCKED, \
|
||||
.rotation = 0, \
|
||||
.window_x = SC_WINDOW_POSITION_UNDEFINED, \
|
||||
.window_y = SC_WINDOW_POSITION_UNDEFINED, \
|
||||
.window_width = 0, \
|
||||
.window_height = 0, \
|
||||
.display_id = 0, \
|
||||
.display_buffer = 0, \
|
||||
.v4l2_buffer = 0, \
|
||||
.show_touches = false, \
|
||||
.fullscreen = false, \
|
||||
.always_on_top = false, \
|
||||
.control = true, \
|
||||
.display = true, \
|
||||
.turn_screen_off = false, \
|
||||
.prefer_text = false, \
|
||||
.window_borderless = false, \
|
||||
.mipmaps = true, \
|
||||
.stay_awake = false, \
|
||||
.force_adb_forward = false, \
|
||||
.disable_screensaver = false, \
|
||||
.forward_key_repeat = true, \
|
||||
.forward_all_clicks = false, \
|
||||
.legacy_paste = false, \
|
||||
.power_off_on_close = false, \
|
||||
}
|
||||
#include "options.h"
|
||||
|
||||
bool
|
||||
scrcpy(const struct scrcpy_options *options);
|
||||
scrcpy(struct scrcpy_options *options);
|
||||
|
||||
#endif
|
||||
|
217
app/src/screen.c
217
app/src/screen.c
@ -5,9 +5,8 @@
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "events.h"
|
||||
#include "icon.xpm"
|
||||
#include "scrcpy.h"
|
||||
#include "tiny_xpm.h"
|
||||
#include "icon.h"
|
||||
#include "options.h"
|
||||
#include "video_buffer.h"
|
||||
#include "util/log.h"
|
||||
|
||||
@ -15,9 +14,9 @@
|
||||
|
||||
#define DOWNCAST(SINK) container_of(SINK, struct screen, frame_sink)
|
||||
|
||||
static inline struct size
|
||||
get_rotated_size(struct size size, int rotation) {
|
||||
struct size rotated_size;
|
||||
static inline struct sc_size
|
||||
get_rotated_size(struct sc_size size, int rotation) {
|
||||
struct sc_size rotated_size;
|
||||
if (rotation & 1) {
|
||||
rotated_size.width = size.height;
|
||||
rotated_size.height = size.width;
|
||||
@ -28,26 +27,26 @@ get_rotated_size(struct size size, int rotation) {
|
||||
return rotated_size;
|
||||
}
|
||||
|
||||
// get the window size in a struct size
|
||||
static struct size
|
||||
// get the window size in a struct sc_size
|
||||
static struct sc_size
|
||||
get_window_size(const struct screen *screen) {
|
||||
int width;
|
||||
int height;
|
||||
SDL_GetWindowSize(screen->window, &width, &height);
|
||||
|
||||
struct size size;
|
||||
struct sc_size size;
|
||||
size.width = width;
|
||||
size.height = height;
|
||||
return size;
|
||||
}
|
||||
|
||||
static struct point
|
||||
static struct sc_point
|
||||
get_window_position(const struct screen *screen) {
|
||||
int x;
|
||||
int y;
|
||||
SDL_GetWindowPosition(screen->window, &x, &y);
|
||||
|
||||
struct point point;
|
||||
struct sc_point point;
|
||||
point.x = x;
|
||||
point.y = y;
|
||||
return point;
|
||||
@ -55,7 +54,7 @@ get_window_position(const struct screen *screen) {
|
||||
|
||||
// set the window size to be applied when fullscreen is disabled
|
||||
static void
|
||||
set_window_size(struct screen *screen, struct size new_size) {
|
||||
set_window_size(struct screen *screen, struct sc_size new_size) {
|
||||
assert(!screen->fullscreen);
|
||||
assert(!screen->maximized);
|
||||
SDL_SetWindowSize(screen->window, new_size.width, new_size.height);
|
||||
@ -63,14 +62,9 @@ set_window_size(struct screen *screen, struct size new_size) {
|
||||
|
||||
// get the preferred display bounds (i.e. the screen bounds with some margins)
|
||||
static bool
|
||||
get_preferred_display_bounds(struct size *bounds) {
|
||||
get_preferred_display_bounds(struct sc_size *bounds) {
|
||||
SDL_Rect rect;
|
||||
#ifdef SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS
|
||||
# define GET_DISPLAY_BOUNDS(i, r) SDL_GetDisplayUsableBounds((i), (r))
|
||||
#else
|
||||
# define GET_DISPLAY_BOUNDS(i, r) SDL_GetDisplayBounds((i), (r))
|
||||
#endif
|
||||
if (GET_DISPLAY_BOUNDS(0, &rect)) {
|
||||
if (SDL_GetDisplayUsableBounds(0, &rect)) {
|
||||
LOGW("Could not get display usable bounds: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
@ -81,7 +75,7 @@ get_preferred_display_bounds(struct size *bounds) {
|
||||
}
|
||||
|
||||
static bool
|
||||
is_optimal_size(struct size current_size, struct size content_size) {
|
||||
is_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
||||
// The size is optimal if we can recompute one dimension of the current
|
||||
// size from the other
|
||||
return current_size.height == current_size.width * content_size.height
|
||||
@ -95,20 +89,21 @@ is_optimal_size(struct size current_size, struct size content_size) {
|
||||
// crops the black borders)
|
||||
// - it keeps the aspect ratio
|
||||
// - it scales down to make it fit in the display_size
|
||||
static struct size
|
||||
get_optimal_size(struct size current_size, struct size content_size) {
|
||||
static struct sc_size
|
||||
get_optimal_size(struct sc_size current_size, struct sc_size content_size,
|
||||
bool within_display_bounds) {
|
||||
if (content_size.width == 0 || content_size.height == 0) {
|
||||
// avoid division by 0
|
||||
return current_size;
|
||||
}
|
||||
|
||||
struct size window_size;
|
||||
struct sc_size window_size;
|
||||
|
||||
struct size display_size;
|
||||
if (!get_preferred_display_bounds(&display_size)) {
|
||||
// could not get display bounds, do not constraint the size
|
||||
window_size.width = current_size.width;
|
||||
window_size.height = current_size.height;
|
||||
struct sc_size display_size;
|
||||
if (!within_display_bounds ||
|
||||
!get_preferred_display_bounds(&display_size)) {
|
||||
// do not constraint the size
|
||||
window_size = current_size;
|
||||
} else {
|
||||
window_size.width = MIN(current_size.width, display_size.width);
|
||||
window_size.height = MIN(current_size.height, display_size.height);
|
||||
@ -136,12 +131,12 @@ get_optimal_size(struct size current_size, struct size content_size) {
|
||||
|
||||
// initially, there is no current size, so use the frame size as current size
|
||||
// req_width and req_height, if not 0, are the sizes requested by the user
|
||||
static inline struct size
|
||||
get_initial_optimal_size(struct size content_size, uint16_t req_width,
|
||||
static inline struct sc_size
|
||||
get_initial_optimal_size(struct sc_size content_size, uint16_t req_width,
|
||||
uint16_t req_height) {
|
||||
struct size window_size;
|
||||
struct sc_size window_size;
|
||||
if (!req_width && !req_height) {
|
||||
window_size = get_optimal_size(content_size, content_size);
|
||||
window_size = get_optimal_size(content_size, content_size, true);
|
||||
} else {
|
||||
if (req_width) {
|
||||
window_size.width = req_width;
|
||||
@ -167,9 +162,9 @@ screen_update_content_rect(struct screen *screen) {
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
struct size content_size = screen->content_size;
|
||||
struct sc_size content_size = screen->content_size;
|
||||
// The drawable size is the window size * the HiDPI scale
|
||||
struct size drawable_size = {dw, dh};
|
||||
struct sc_size drawable_size = {dw, dh};
|
||||
|
||||
SDL_Rect *rect = &screen->rect;
|
||||
|
||||
@ -201,7 +196,7 @@ screen_update_content_rect(struct screen *screen) {
|
||||
static inline SDL_Texture *
|
||||
create_texture(struct screen *screen) {
|
||||
SDL_Renderer *renderer = screen->renderer;
|
||||
struct size size = screen->frame_size;
|
||||
struct sc_size size = screen->frame_size;
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
size.width, size.height);
|
||||
@ -225,6 +220,45 @@ create_texture(struct screen *screen) {
|
||||
return texture;
|
||||
}
|
||||
|
||||
// render the texture to the renderer
|
||||
//
|
||||
// Set the update_content_rect flag if the window or content size may have
|
||||
// changed, so that the content rectangle is recomputed
|
||||
static void
|
||||
screen_render(struct screen *screen, bool update_content_rect) {
|
||||
if (update_content_rect) {
|
||||
screen_update_content_rect(screen);
|
||||
}
|
||||
|
||||
SDL_RenderClear(screen->renderer);
|
||||
if (screen->rotation == 0) {
|
||||
SDL_RenderCopy(screen->renderer, screen->texture, NULL, &screen->rect);
|
||||
} else {
|
||||
// rotation in RenderCopyEx() is clockwise, while screen->rotation is
|
||||
// counterclockwise (to be consistent with --lock-video-orientation)
|
||||
int cw_rotation = (4 - screen->rotation) % 4;
|
||||
double angle = 90 * cw_rotation;
|
||||
|
||||
SDL_Rect *dstrect = NULL;
|
||||
SDL_Rect rect;
|
||||
if (screen->rotation & 1) {
|
||||
rect.x = screen->rect.x + (screen->rect.w - screen->rect.h) / 2;
|
||||
rect.y = screen->rect.y + (screen->rect.h - screen->rect.w) / 2;
|
||||
rect.w = screen->rect.h;
|
||||
rect.h = screen->rect.w;
|
||||
dstrect = ▭
|
||||
} else {
|
||||
assert(screen->rotation == 2);
|
||||
dstrect = &screen->rect;
|
||||
}
|
||||
|
||||
SDL_RenderCopyEx(screen->renderer, screen->texture, NULL, dstrect,
|
||||
angle, NULL, 0);
|
||||
}
|
||||
SDL_RenderPresent(screen->renderer);
|
||||
}
|
||||
|
||||
|
||||
#if defined(__APPLE__) || defined(__WINDOWS__)
|
||||
# define CONTINUOUS_RESIZING_WORKAROUND
|
||||
#endif
|
||||
@ -283,17 +317,33 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
|
||||
(void) vb;
|
||||
struct screen *screen = userdata;
|
||||
|
||||
// event_failed implies previous_skipped (the previous frame may not have
|
||||
// been consumed if the event was not sent)
|
||||
assert(!screen->event_failed || previous_skipped);
|
||||
|
||||
bool need_new_event;
|
||||
if (previous_skipped) {
|
||||
fps_counter_add_skipped_frame(&screen->fps_counter);
|
||||
// The EVENT_NEW_FRAME triggered for the previous frame will consume
|
||||
// this new frame instead
|
||||
// this new frame instead, unless the previous event failed
|
||||
need_new_event = screen->event_failed;
|
||||
} else {
|
||||
need_new_event = true;
|
||||
}
|
||||
|
||||
if (need_new_event) {
|
||||
static SDL_Event new_frame_event = {
|
||||
.type = EVENT_NEW_FRAME,
|
||||
};
|
||||
|
||||
// Post the event on the UI thread
|
||||
SDL_PushEvent(&new_frame_event);
|
||||
int ret = SDL_PushEvent(&new_frame_event);
|
||||
if (ret < 0) {
|
||||
LOGW("Could not post new frame event: %s", SDL_GetError());
|
||||
screen->event_failed = true;
|
||||
} else {
|
||||
screen->event_failed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -303,6 +353,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
screen->has_frame = false;
|
||||
screen->fullscreen = false;
|
||||
screen->maximized = false;
|
||||
screen->event_failed = false;
|
||||
|
||||
static const struct sc_video_buffer_callbacks cbs = {
|
||||
.on_new_frame = sc_video_buffer_on_new_frame,
|
||||
@ -311,18 +362,15 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
bool ok = sc_video_buffer_init(&screen->vb, params->buffering_time, &cbs,
|
||||
screen);
|
||||
if (!ok) {
|
||||
LOGE("Could not initialize video buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_video_buffer_start(&screen->vb);
|
||||
if (!ok) {
|
||||
LOGE("Could not start video_buffer");
|
||||
goto error_destroy_video_buffer;
|
||||
}
|
||||
|
||||
if (!fps_counter_init(&screen->fps_counter)) {
|
||||
LOGE("Could not initialize FPS counter");
|
||||
goto error_stop_and_join_video_buffer;
|
||||
}
|
||||
|
||||
@ -331,23 +379,18 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
if (screen->rotation) {
|
||||
LOGI("Initial display rotation set to %u", screen->rotation);
|
||||
}
|
||||
struct size content_size =
|
||||
struct sc_size content_size =
|
||||
get_rotated_size(screen->frame_size, screen->rotation);
|
||||
screen->content_size = content_size;
|
||||
|
||||
struct size window_size = get_initial_optimal_size(content_size,
|
||||
params->window_width,
|
||||
params->window_height);
|
||||
struct sc_size window_size =
|
||||
get_initial_optimal_size(content_size,params->window_width,
|
||||
params->window_height);
|
||||
uint32_t window_flags = SDL_WINDOW_HIDDEN
|
||||
| SDL_WINDOW_RESIZABLE
|
||||
| SDL_WINDOW_ALLOW_HIGHDPI;
|
||||
if (params->always_on_top) {
|
||||
#ifdef SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP
|
||||
window_flags |= SDL_WINDOW_ALWAYS_ON_TOP;
|
||||
#else
|
||||
LOGW("The 'always on top' flag is not available "
|
||||
"(compile with SDL >= 2.0.5 to enable it)");
|
||||
#endif
|
||||
}
|
||||
if (params->window_borderless) {
|
||||
window_flags |= SDL_WINDOW_BORDERLESS;
|
||||
@ -405,10 +448,10 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
LOGD("Trilinear filtering disabled (not an OpenGL renderer)");
|
||||
}
|
||||
|
||||
SDL_Surface *icon = read_xpm(icon_xpm);
|
||||
SDL_Surface *icon = scrcpy_icon_load();
|
||||
if (icon) {
|
||||
SDL_SetWindowIcon(screen->window, icon);
|
||||
SDL_FreeSurface(icon);
|
||||
scrcpy_icon_destroy(icon);
|
||||
} else {
|
||||
LOGW("Could not load icon");
|
||||
}
|
||||
@ -423,7 +466,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
|
||||
screen->frame = av_frame_alloc();
|
||||
if (!screen->frame) {
|
||||
LOGC("Could not create screen frame");
|
||||
LOG_OOM();
|
||||
goto error_destroy_texture;
|
||||
}
|
||||
|
||||
@ -509,21 +552,21 @@ screen_destroy(struct screen *screen) {
|
||||
}
|
||||
|
||||
static void
|
||||
resize_for_content(struct screen *screen, struct size old_content_size,
|
||||
struct size new_content_size) {
|
||||
struct size window_size = get_window_size(screen);
|
||||
struct size target_size = {
|
||||
resize_for_content(struct screen *screen, struct sc_size old_content_size,
|
||||
struct sc_size new_content_size) {
|
||||
struct sc_size window_size = get_window_size(screen);
|
||||
struct sc_size target_size = {
|
||||
.width = (uint32_t) window_size.width * new_content_size.width
|
||||
/ old_content_size.width,
|
||||
.height = (uint32_t) window_size.height * new_content_size.height
|
||||
/ old_content_size.height,
|
||||
};
|
||||
target_size = get_optimal_size(target_size, new_content_size);
|
||||
target_size = get_optimal_size(target_size, new_content_size, true);
|
||||
set_window_size(screen, target_size);
|
||||
}
|
||||
|
||||
static void
|
||||
set_content_size(struct screen *screen, struct size new_content_size) {
|
||||
set_content_size(struct screen *screen, struct sc_size new_content_size) {
|
||||
if (!screen->fullscreen && !screen->maximized) {
|
||||
resize_for_content(screen, screen->content_size, new_content_size);
|
||||
} else if (!screen->resize_pending) {
|
||||
@ -554,7 +597,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct size new_content_size =
|
||||
struct sc_size new_content_size =
|
||||
get_rotated_size(screen->frame_size, rotation);
|
||||
|
||||
set_content_size(screen, new_content_size);
|
||||
@ -567,7 +610,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
|
||||
// recreate the texture and resize the window if the frame size has changed
|
||||
static bool
|
||||
prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
prepare_for_frame(struct screen *screen, struct sc_size new_frame_size) {
|
||||
if (screen->frame_size.width != new_frame_size.width
|
||||
|| screen->frame_size.height != new_frame_size.height) {
|
||||
// frame dimension changed, destroy texture
|
||||
@ -575,7 +618,7 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
|
||||
screen->frame_size = new_frame_size;
|
||||
|
||||
struct size new_content_size =
|
||||
struct sc_size new_content_size =
|
||||
get_rotated_size(new_frame_size, screen->rotation);
|
||||
set_content_size(screen, new_content_size);
|
||||
|
||||
@ -616,7 +659,7 @@ screen_update_frame(struct screen *screen) {
|
||||
|
||||
fps_counter_add_rendered_frame(&screen->fps_counter);
|
||||
|
||||
struct 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)) {
|
||||
return false;
|
||||
}
|
||||
@ -626,40 +669,6 @@ screen_update_frame(struct screen *screen) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
screen_render(struct screen *screen, bool update_content_rect) {
|
||||
if (update_content_rect) {
|
||||
screen_update_content_rect(screen);
|
||||
}
|
||||
|
||||
SDL_RenderClear(screen->renderer);
|
||||
if (screen->rotation == 0) {
|
||||
SDL_RenderCopy(screen->renderer, screen->texture, NULL, &screen->rect);
|
||||
} else {
|
||||
// rotation in RenderCopyEx() is clockwise, while screen->rotation is
|
||||
// counterclockwise (to be consistent with --lock-video-orientation)
|
||||
int cw_rotation = (4 - screen->rotation) % 4;
|
||||
double angle = 90 * cw_rotation;
|
||||
|
||||
SDL_Rect *dstrect = NULL;
|
||||
SDL_Rect rect;
|
||||
if (screen->rotation & 1) {
|
||||
rect.x = screen->rect.x + (screen->rect.w - screen->rect.h) / 2;
|
||||
rect.y = screen->rect.y + (screen->rect.h - screen->rect.w) / 2;
|
||||
rect.w = screen->rect.h;
|
||||
rect.h = screen->rect.w;
|
||||
dstrect = ▭
|
||||
} else {
|
||||
assert(screen->rotation == 2);
|
||||
dstrect = &screen->rect;
|
||||
}
|
||||
|
||||
SDL_RenderCopyEx(screen->renderer, screen->texture, NULL, dstrect,
|
||||
angle, NULL, 0);
|
||||
}
|
||||
SDL_RenderPresent(screen->renderer);
|
||||
}
|
||||
|
||||
void
|
||||
screen_switch_fullscreen(struct screen *screen) {
|
||||
uint32_t new_mode = screen->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
@ -683,11 +692,11 @@ screen_resize_to_fit(struct screen *screen) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct point point = get_window_position(screen);
|
||||
struct size window_size = get_window_size(screen);
|
||||
struct sc_point point = get_window_position(screen);
|
||||
struct sc_size window_size = get_window_size(screen);
|
||||
|
||||
struct size optimal_size =
|
||||
get_optimal_size(window_size, screen->content_size);
|
||||
struct sc_size optimal_size =
|
||||
get_optimal_size(window_size, screen->content_size, false);
|
||||
|
||||
// Center the window related to the device screen
|
||||
assert(optimal_size.width <= window_size.width);
|
||||
@ -712,7 +721,7 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
|
||||
screen->maximized = false;
|
||||
}
|
||||
|
||||
struct size content_size = screen->content_size;
|
||||
struct sc_size content_size = screen->content_size;
|
||||
SDL_SetWindowSize(screen->window, content_size.width, content_size.height);
|
||||
LOGD("Resized to pixel-perfect: %ux%u", content_size.width,
|
||||
content_size.height);
|
||||
@ -767,7 +776,7 @@ screen_handle_event(struct screen *screen, SDL_Event *event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y) {
|
||||
unsigned rotation = screen->rotation;
|
||||
@ -781,7 +790,7 @@ screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
y = (int64_t) (y - screen->rect.y) * h / screen->rect.h;
|
||||
|
||||
// rotate
|
||||
struct point result;
|
||||
struct sc_point result;
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
result.x = x;
|
||||
@ -804,7 +813,7 @@ screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
return result;
|
||||
}
|
||||
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_window_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y) {
|
||||
screen_hidpi_scale_coords(screen, &x, &y);
|
||||
|
@ -27,13 +27,13 @@ struct screen {
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *texture;
|
||||
struct sc_opengl gl;
|
||||
struct size frame_size;
|
||||
struct size content_size; // rotated frame_size
|
||||
struct sc_size frame_size;
|
||||
struct sc_size content_size; // rotated frame_size
|
||||
|
||||
bool resize_pending; // resize requested while fullscreen or maximized
|
||||
// The content size the last time the window was not maximized or
|
||||
// fullscreen (meaningful only when resize_pending is true)
|
||||
struct size windowed_content_size;
|
||||
struct sc_size windowed_content_size;
|
||||
|
||||
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
|
||||
unsigned rotation;
|
||||
@ -44,12 +44,14 @@ struct screen {
|
||||
bool maximized;
|
||||
bool mipmaps;
|
||||
|
||||
bool event_failed; // in case SDL_PushEvent() returned an error
|
||||
|
||||
AVFrame *frame;
|
||||
};
|
||||
|
||||
struct screen_params {
|
||||
const char *window_title;
|
||||
struct size frame_size;
|
||||
struct sc_size frame_size;
|
||||
bool always_on_top;
|
||||
|
||||
int16_t window_x;
|
||||
@ -91,13 +93,6 @@ screen_destroy(struct screen *screen);
|
||||
void
|
||||
screen_hide_window(struct screen *screen);
|
||||
|
||||
// render the texture to the renderer
|
||||
//
|
||||
// Set the update_content_rect flag if the window or content size may have
|
||||
// changed, so that the content rectangle is recomputed
|
||||
void
|
||||
screen_render(struct screen *screen, bool update_content_rect);
|
||||
|
||||
// switch the fullscreen mode
|
||||
void
|
||||
screen_switch_fullscreen(struct screen *screen);
|
||||
@ -120,13 +115,13 @@ screen_handle_event(struct screen *screen, SDL_Event *event);
|
||||
|
||||
// convert point from window coordinates to frame coordinates
|
||||
// x and y are expressed in pixels
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_window_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y);
|
||||
|
||||
// convert point from drawable coordinates to frame coordinates
|
||||
// x and y are expressed in pixels
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y);
|
||||
|
||||
|
1106
app/src/server.c
1106
app/src/server.c
File diff suppressed because it is too large
Load Diff
@ -8,37 +8,29 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "adb.h"
|
||||
#include "adb_tunnel.h"
|
||||
#include "coords.h"
|
||||
#include "scrcpy.h"
|
||||
#include "options.h"
|
||||
#include "util/intr.h"
|
||||
#include "util/log.h"
|
||||
#include "util/net.h"
|
||||
#include "util/thread.h"
|
||||
|
||||
struct server {
|
||||
char *serial;
|
||||
process_t process;
|
||||
sc_thread wait_server_thread;
|
||||
atomic_flag server_socket_closed;
|
||||
|
||||
sc_mutex mutex;
|
||||
sc_cond process_terminated_cond;
|
||||
bool process_terminated;
|
||||
|
||||
socket_t server_socket; // only used if !tunnel_forward
|
||||
socket_t video_socket;
|
||||
socket_t control_socket;
|
||||
uint16_t local_port; // selected from port_range
|
||||
bool tunnel_enabled;
|
||||
bool tunnel_forward; // use "adb forward" instead of "adb reverse"
|
||||
#define SC_DEVICE_NAME_FIELD_LENGTH 64
|
||||
struct sc_server_info {
|
||||
char device_name[SC_DEVICE_NAME_FIELD_LENGTH];
|
||||
struct sc_size frame_size;
|
||||
};
|
||||
|
||||
struct server_params {
|
||||
struct sc_server_params {
|
||||
const char *serial;
|
||||
enum sc_log_level log_level;
|
||||
const char *crop;
|
||||
const char *codec_options;
|
||||
const char *encoder_name;
|
||||
struct sc_port_range port_range;
|
||||
uint32_t tunnel_host;
|
||||
uint16_t tunnel_port;
|
||||
uint16_t max_size;
|
||||
uint32_t bit_rate;
|
||||
uint16_t max_fps;
|
||||
@ -49,28 +41,67 @@ struct server_params {
|
||||
bool stay_awake;
|
||||
bool force_adb_forward;
|
||||
bool power_off_on_close;
|
||||
bool clipboard_autosync;
|
||||
bool tcpip;
|
||||
const char *tcpip_dst;
|
||||
};
|
||||
|
||||
// init default values
|
||||
bool
|
||||
server_init(struct server *server);
|
||||
struct sc_server {
|
||||
// The internal allocated strings are copies owned by the server
|
||||
struct sc_server_params params;
|
||||
|
||||
// push, enable tunnel et start the server
|
||||
bool
|
||||
server_start(struct server *server, const struct server_params *params);
|
||||
sc_thread thread;
|
||||
struct sc_server_info info; // initialized once connected
|
||||
|
||||
#define DEVICE_NAME_FIELD_LENGTH 64
|
||||
// block until the communication with the server is established
|
||||
// device_name must point to a buffer of at least DEVICE_NAME_FIELD_LENGTH bytes
|
||||
sc_mutex mutex;
|
||||
sc_cond cond_stopped;
|
||||
bool stopped;
|
||||
|
||||
struct sc_intr intr;
|
||||
struct sc_adb_tunnel tunnel;
|
||||
|
||||
sc_socket video_socket;
|
||||
sc_socket control_socket;
|
||||
|
||||
const struct sc_server_callbacks *cbs;
|
||||
void *cbs_userdata;
|
||||
};
|
||||
|
||||
struct sc_server_callbacks {
|
||||
/**
|
||||
* Called when the server failed to connect
|
||||
*
|
||||
* If it is called, then on_connected() and on_disconnected() will never be
|
||||
* called.
|
||||
*/
|
||||
void (*on_connection_failed)(struct sc_server *server, void *userdata);
|
||||
|
||||
/**
|
||||
* Called on server connection
|
||||
*/
|
||||
void (*on_connected)(struct sc_server *server, void *userdata);
|
||||
|
||||
/**
|
||||
* Called on server disconnection (after it has been connected)
|
||||
*/
|
||||
void (*on_disconnected)(struct sc_server *server, void *userdata);
|
||||
};
|
||||
|
||||
// init the server with the given params
|
||||
bool
|
||||
server_connect_to(struct server *server, char *device_name, struct size *size);
|
||||
sc_server_init(struct sc_server *server, const struct sc_server_params *params,
|
||||
const struct sc_server_callbacks *cbs, void *cbs_userdata);
|
||||
|
||||
// start the server asynchronously
|
||||
bool
|
||||
sc_server_start(struct sc_server *server);
|
||||
|
||||
// disconnect and kill the server process
|
||||
void
|
||||
server_stop(struct server *server);
|
||||
sc_server_stop(struct sc_server *server);
|
||||
|
||||
// close and release sockets
|
||||
void
|
||||
server_destroy(struct server *server);
|
||||
sc_server_destroy(struct sc_server *server);
|
||||
|
||||
#endif
|
||||
|
@ -42,7 +42,7 @@ stream_recv_packet(struct stream *stream, AVPacket *packet) {
|
||||
assert(len);
|
||||
|
||||
if (av_new_packet(packet, len)) {
|
||||
LOGE("Could not allocate packet");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -111,18 +111,18 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
|
||||
if (stream->pending) {
|
||||
offset = stream->pending->size;
|
||||
if (av_grow_packet(stream->pending, packet->size)) {
|
||||
LOGE("Could not grow packet");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
offset = 0;
|
||||
stream->pending = av_packet_alloc();
|
||||
if (!stream->pending) {
|
||||
LOGE("Could not allocate packet");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
if (av_new_packet(stream->pending, packet->size)) {
|
||||
LOGE("Could not create packet");
|
||||
LOG_OOM();
|
||||
av_packet_free(&stream->pending);
|
||||
return false;
|
||||
}
|
||||
@ -200,7 +200,7 @@ run_stream(void *data) {
|
||||
|
||||
stream->codec_ctx = avcodec_alloc_context3(codec);
|
||||
if (!stream->codec_ctx) {
|
||||
LOGC("Could not allocate codec context");
|
||||
LOG_OOM();
|
||||
goto end;
|
||||
}
|
||||
|
||||
@ -221,7 +221,7 @@ run_stream(void *data) {
|
||||
|
||||
AVPacket *packet = av_packet_alloc();
|
||||
if (!packet) {
|
||||
LOGE("Could not allocate packet");
|
||||
LOG_OOM();
|
||||
goto finally_close_parser;
|
||||
}
|
||||
|
||||
@ -260,7 +260,7 @@ end:
|
||||
}
|
||||
|
||||
void
|
||||
stream_init(struct stream *stream, socket_t socket,
|
||||
stream_init(struct stream *stream, sc_socket socket,
|
||||
const struct stream_callbacks *cbs, void *cbs_userdata) {
|
||||
stream->socket = socket;
|
||||
stream->pending = NULL;
|
||||
@ -284,7 +284,8 @@ bool
|
||||
stream_start(struct stream *stream) {
|
||||
LOGD("Starting stream thread");
|
||||
|
||||
bool ok = sc_thread_create(&stream->thread, run_stream, "stream", stream);
|
||||
bool ok =
|
||||
sc_thread_create(&stream->thread, run_stream, "scrcpy-stream", stream);
|
||||
if (!ok) {
|
||||
LOGC("Could not start stream thread");
|
||||
return false;
|
||||
|
@ -14,7 +14,7 @@
|
||||
#define STREAM_MAX_SINKS 2
|
||||
|
||||
struct stream {
|
||||
socket_t socket;
|
||||
sc_socket socket;
|
||||
sc_thread thread;
|
||||
|
||||
struct sc_packet_sink *sinks[STREAM_MAX_SINKS];
|
||||
@ -35,7 +35,7 @@ struct stream_callbacks {
|
||||
};
|
||||
|
||||
void
|
||||
stream_init(struct stream *stream, socket_t socket,
|
||||
stream_init(struct stream *stream, sc_socket socket,
|
||||
const struct stream_callbacks *cbs, void *cbs_userdata);
|
||||
|
||||
void
|
||||
|
81
app/src/sys/unix/file.c
Normal file
81
app/src/sys/unix/file.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include "util/file.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
sc_file_executable_exists(const char *file) {
|
||||
char *path = getenv("PATH");
|
||||
if (!path)
|
||||
return false;
|
||||
path = strdup(path);
|
||||
if (!path)
|
||||
return false;
|
||||
|
||||
bool ret = false;
|
||||
size_t file_len = strlen(file);
|
||||
char *saveptr;
|
||||
for (char *dir = strtok_r(path, ":", &saveptr); dir;
|
||||
dir = strtok_r(NULL, ":", &saveptr)) {
|
||||
size_t dir_len = strlen(dir);
|
||||
char *fullpath = malloc(dir_len + file_len + 2);
|
||||
if (!fullpath)
|
||||
{
|
||||
LOG_OOM();
|
||||
continue;
|
||||
}
|
||||
memcpy(fullpath, dir, dir_len);
|
||||
fullpath[dir_len] = '/';
|
||||
memcpy(fullpath + dir_len + 1, file, file_len + 1);
|
||||
|
||||
struct stat sb;
|
||||
bool fullpath_executable = stat(fullpath, &sb) == 0 &&
|
||||
sb.st_mode & S_IXUSR;
|
||||
free(fullpath);
|
||||
if (fullpath_executable) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
sc_file_get_executable_path(void) {
|
||||
// <https://stackoverflow.com/a/1024937/1987178>
|
||||
#ifdef __linux__
|
||||
char buf[PATH_MAX + 1]; // +1 for the null byte
|
||||
ssize_t len = readlink("/proc/self/exe", buf, PATH_MAX);
|
||||
if (len == -1) {
|
||||
perror("readlink");
|
||||
return NULL;
|
||||
}
|
||||
buf[len] = '\0';
|
||||
return strdup(buf);
|
||||
#else
|
||||
// in practice, we only need this feature for portable builds, only used on
|
||||
// Windows, so we don't care implementing it for every platform
|
||||
// (it's useful to have a working version on Linux for debugging though)
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
sc_file_is_regular(const char *path) {
|
||||
struct stat path_stat;
|
||||
|
||||
if (stat(path, &path_stat)) {
|
||||
perror("stat");
|
||||
return false;
|
||||
}
|
||||
return S_ISREG(path_stat.st_mode);
|
||||
}
|
||||
|
@ -1,118 +1,180 @@
|
||||
#include "util/process.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
search_executable(const char *file) {
|
||||
char *path = getenv("PATH");
|
||||
if (!path)
|
||||
return false;
|
||||
path = strdup(path);
|
||||
if (!path)
|
||||
return false;
|
||||
enum sc_process_result
|
||||
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
||||
int *pin, int *pout, int *perr) {
|
||||
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||
|
||||
bool ret = false;
|
||||
size_t file_len = strlen(file);
|
||||
char *saveptr;
|
||||
for (char *dir = strtok_r(path, ":", &saveptr); dir;
|
||||
dir = strtok_r(NULL, ":", &saveptr)) {
|
||||
size_t dir_len = strlen(dir);
|
||||
char *fullpath = malloc(dir_len + file_len + 2);
|
||||
if (!fullpath)
|
||||
continue;
|
||||
memcpy(fullpath, dir, dir_len);
|
||||
fullpath[dir_len] = '/';
|
||||
memcpy(fullpath + dir_len + 1, file, file_len + 1);
|
||||
int in[2];
|
||||
int out[2];
|
||||
int err[2];
|
||||
int internal[2]; // communication between parent and children
|
||||
|
||||
struct stat sb;
|
||||
bool fullpath_executable = stat(fullpath, &sb) == 0 &&
|
||||
sb.st_mode & S_IXUSR;
|
||||
free(fullpath);
|
||||
if (fullpath_executable) {
|
||||
ret = true;
|
||||
break;
|
||||
if (pipe(internal) == -1) {
|
||||
perror("pipe");
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
if (pin) {
|
||||
if (pipe(in) == -1) {
|
||||
perror("pipe");
|
||||
close(internal[0]);
|
||||
close(internal[1]);
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
}
|
||||
|
||||
free(path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
enum process_result
|
||||
process_execute(const char *const argv[], pid_t *pid) {
|
||||
int fd[2];
|
||||
|
||||
if (pipe(fd) == -1) {
|
||||
perror("pipe");
|
||||
return PROCESS_ERROR_GENERIC;
|
||||
if (pout) {
|
||||
if (pipe(out) == -1) {
|
||||
perror("pipe");
|
||||
// clean up
|
||||
if (pin) {
|
||||
close(in[0]);
|
||||
close(in[1]);
|
||||
}
|
||||
close(internal[0]);
|
||||
close(internal[1]);
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
}
|
||||
if (perr) {
|
||||
if (pipe(err) == -1) {
|
||||
perror("pipe");
|
||||
// clean up
|
||||
if (pout) {
|
||||
close(out[0]);
|
||||
close(out[1]);
|
||||
}
|
||||
if (pin) {
|
||||
close(in[0]);
|
||||
close(in[1]);
|
||||
}
|
||||
close(internal[0]);
|
||||
close(internal[1]);
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
}
|
||||
|
||||
enum process_result ret = PROCESS_SUCCESS;
|
||||
|
||||
*pid = fork();
|
||||
if (*pid == -1) {
|
||||
perror("fork");
|
||||
ret = PROCESS_ERROR_GENERIC;
|
||||
goto end;
|
||||
// clean up
|
||||
if (perr) {
|
||||
close(err[0]);
|
||||
close(err[1]);
|
||||
}
|
||||
if (pout) {
|
||||
close(out[0]);
|
||||
close(out[1]);
|
||||
}
|
||||
if (pin) {
|
||||
close(in[0]);
|
||||
close(in[1]);
|
||||
}
|
||||
close(internal[0]);
|
||||
close(internal[1]);
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
if (*pid > 0) {
|
||||
// parent close write side
|
||||
close(fd[1]);
|
||||
fd[1] = -1;
|
||||
// wait for EOF or receive errno from child
|
||||
if (read(fd[0], &ret, sizeof(ret)) == -1) {
|
||||
perror("read");
|
||||
ret = PROCESS_ERROR_GENERIC;
|
||||
goto end;
|
||||
}
|
||||
} else if (*pid == 0) {
|
||||
// child close read side
|
||||
close(fd[0]);
|
||||
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) {
|
||||
execvp(argv[0], (char *const *)argv);
|
||||
if (errno == ENOENT) {
|
||||
ret = PROCESS_ERROR_MISSING_BINARY;
|
||||
} else {
|
||||
ret = PROCESS_ERROR_GENERIC;
|
||||
if (*pid == 0) {
|
||||
if (pin) {
|
||||
if (in[0] != STDIN_FILENO) {
|
||||
dup2(in[0], STDIN_FILENO);
|
||||
close(in[0]);
|
||||
}
|
||||
close(in[1]);
|
||||
}
|
||||
// Do not close stdin in the child process, this makes adb fail on Linux
|
||||
|
||||
if (pout) {
|
||||
if (out[1] != STDOUT_FILENO) {
|
||||
dup2(out[1], STDOUT_FILENO);
|
||||
close(out[1]);
|
||||
}
|
||||
close(out[0]);
|
||||
} else if (!inherit_stdout) {
|
||||
// Close stdout in the child process
|
||||
close(STDOUT_FILENO);
|
||||
}
|
||||
|
||||
if (perr) {
|
||||
if (err[1] != STDERR_FILENO) {
|
||||
dup2(err[1], STDERR_FILENO);
|
||||
close(err[1]);
|
||||
}
|
||||
close(err[0]);
|
||||
} else if (!inherit_stderr) {
|
||||
// Close stderr in the child process
|
||||
close(STDERR_FILENO);
|
||||
}
|
||||
|
||||
close(internal[0]);
|
||||
enum sc_process_result err;
|
||||
|
||||
// Somehow SDL masks many signals - undo them for other processes
|
||||
// https://github.com/libsdl-org/SDL/blob/release-2.0.18/src/thread/pthread/SDL_systhread.c#L167
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigprocmask(SIG_SETMASK, &mask, NULL);
|
||||
|
||||
if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) {
|
||||
execvp(argv[0], (char *const *) argv);
|
||||
perror("exec");
|
||||
err = errno == ENOENT ? SC_PROCESS_ERROR_MISSING_BINARY
|
||||
: SC_PROCESS_ERROR_GENERIC;
|
||||
} else {
|
||||
perror("fcntl");
|
||||
ret = PROCESS_ERROR_GENERIC;
|
||||
err = SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
// send ret to the parent
|
||||
if (write(fd[1], &ret, sizeof(ret)) == -1) {
|
||||
// send err to the parent
|
||||
if (write(internal[1], &err, sizeof(err)) == -1) {
|
||||
perror("write");
|
||||
}
|
||||
// close write side before exiting
|
||||
close(fd[1]);
|
||||
close(internal[1]);
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
end:
|
||||
if (fd[0] != -1) {
|
||||
close(fd[0]);
|
||||
// parent
|
||||
assert(*pid > 0);
|
||||
|
||||
close(internal[1]);
|
||||
|
||||
enum sc_process_result res = SC_PROCESS_SUCCESS;
|
||||
// wait for EOF or receive err from child
|
||||
if (read(internal[0], &res, sizeof(res)) == -1) {
|
||||
perror("read");
|
||||
res = SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
if (fd[1] != -1) {
|
||||
close(fd[1]);
|
||||
|
||||
close(internal[0]);
|
||||
|
||||
if (pin) {
|
||||
close(in[0]);
|
||||
*pin = in[1];
|
||||
}
|
||||
return ret;
|
||||
if (pout) {
|
||||
*pout = out[0];
|
||||
close(out[1]);
|
||||
}
|
||||
if (perr) {
|
||||
*perr = err[0];
|
||||
close(err[1]);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool
|
||||
process_terminate(pid_t pid) {
|
||||
sc_process_terminate(pid_t pid) {
|
||||
if (pid <= 0) {
|
||||
LOGC("Requested to kill %d, this is an error. Please report the bug.\n",
|
||||
(int) pid);
|
||||
@ -121,8 +183,8 @@ process_terminate(pid_t pid) {
|
||||
return kill(pid, SIGKILL) != -1;
|
||||
}
|
||||
|
||||
exit_code_t
|
||||
process_wait(pid_t pid, bool close) {
|
||||
sc_exit_code
|
||||
sc_process_wait(pid_t pid, bool close) {
|
||||
int code;
|
||||
int options = WEXITED;
|
||||
if (!close) {
|
||||
@ -133,7 +195,7 @@ process_wait(pid_t pid, bool close) {
|
||||
int r = waitid(P_PID, pid, &info, options);
|
||||
if (r == -1 || info.si_code != CLD_EXITED) {
|
||||
// could not wait, or exited unexpectedly, probably by a signal
|
||||
code = NO_EXIT_CODE;
|
||||
code = SC_EXIT_CODE_NONE;
|
||||
} else {
|
||||
code = info.si_status;
|
||||
}
|
||||
@ -141,37 +203,18 @@ process_wait(pid_t pid, bool close) {
|
||||
}
|
||||
|
||||
void
|
||||
process_close(pid_t pid) {
|
||||
process_wait(pid, true); // ignore exit code
|
||||
sc_process_close(pid_t pid) {
|
||||
sc_process_wait(pid, true); // ignore exit code
|
||||
}
|
||||
|
||||
char *
|
||||
get_executable_path(void) {
|
||||
// <https://stackoverflow.com/a/1024937/1987178>
|
||||
#ifdef __linux__
|
||||
char buf[PATH_MAX + 1]; // +1 for the null byte
|
||||
ssize_t len = readlink("/proc/self/exe", buf, PATH_MAX);
|
||||
if (len == -1) {
|
||||
perror("readlink");
|
||||
return NULL;
|
||||
ssize_t
|
||||
sc_pipe_read(int pipe, char *data, size_t len) {
|
||||
return read(pipe, data, len);
|
||||
}
|
||||
|
||||
void
|
||||
sc_pipe_close(int pipe) {
|
||||
if (close(pipe)) {
|
||||
perror("close pipe");
|
||||
}
|
||||
buf[len] = '\0';
|
||||
return strdup(buf);
|
||||
#else
|
||||
// in practice, we only need this feature for portable builds, only used on
|
||||
// Windows, so we don't care implementing it for every platform
|
||||
// (it's useful to have a working version on Linux for debugging though)
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
is_regular_file(const char *path) {
|
||||
struct stat path_stat;
|
||||
|
||||
if (stat(path, &path_stat)) {
|
||||
perror("stat");
|
||||
return false;
|
||||
}
|
||||
return S_ISREG(path_stat.st_mode);
|
||||
}
|
||||
|
43
app/src/sys/win/file.c
Normal file
43
app/src/sys/win/file.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include "util/file.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/str.h"
|
||||
|
||||
char *
|
||||
sc_file_get_executable_path(void) {
|
||||
HMODULE hModule = GetModuleHandleW(NULL);
|
||||
if (!hModule) {
|
||||
return NULL;
|
||||
}
|
||||
WCHAR buf[MAX_PATH + 1]; // +1 for the null byte
|
||||
int len = GetModuleFileNameW(hModule, buf, MAX_PATH);
|
||||
if (!len) {
|
||||
return NULL;
|
||||
}
|
||||
buf[len] = '\0';
|
||||
return sc_str_from_wchars(buf);
|
||||
}
|
||||
|
||||
bool
|
||||
sc_file_is_regular(const char *path) {
|
||||
wchar_t *wide_path = sc_str_to_wchars(path);
|
||||
if (!wide_path) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
struct _stat path_stat;
|
||||
int r = _wstat(wide_path, &path_stat);
|
||||
free(wide_path);
|
||||
|
||||
if (r) {
|
||||
perror("stat");
|
||||
return false;
|
||||
}
|
||||
return S_ISREG(path_stat.st_mode);
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
#include "util/process.h"
|
||||
|
||||
#include <processthreadsapi.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/str_util.h"
|
||||
#include "util/str.h"
|
||||
|
||||
#define CMD_MAX_LEN 8192
|
||||
|
||||
@ -14,61 +15,218 @@ build_cmd(char *cmd, size_t len, const char *const argv[]) {
|
||||
// <http://daviddeley.com/autohotkey/parameters/parameters.htm#WINPASS>
|
||||
// only make it work for this very specific program
|
||||
// (don't handle escaping nor quotes)
|
||||
size_t ret = xstrjoin(cmd, argv, ' ', len);
|
||||
size_t ret = sc_str_join(cmd, argv, ' ', len);
|
||||
if (ret >= len) {
|
||||
LOGE("Command too long (%" PRIsizet " chars)", len - 1);
|
||||
LOGE("Command too long (%" SC_PRIsizet " chars)", len - 1);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
enum process_result
|
||||
process_execute(const char *const argv[], HANDLE *handle) {
|
||||
STARTUPINFOW si;
|
||||
enum sc_process_result
|
||||
sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
|
||||
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
||||
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||
|
||||
// Add 1 per non-NULL pointer
|
||||
unsigned handle_count = !!pin || !!pout || !!perr;
|
||||
|
||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
sa.lpSecurityDescriptor = NULL;
|
||||
sa.bInheritHandle = TRUE;
|
||||
|
||||
HANDLE stdin_read_handle;
|
||||
HANDLE stdout_write_handle;
|
||||
HANDLE stderr_write_handle;
|
||||
if (pin) {
|
||||
if (!CreatePipe(&stdin_read_handle, pin, &sa, 0)) {
|
||||
perror("pipe");
|
||||
return SC_PROCESS_ERROR_GENERIC;
|
||||
}
|
||||
if (!SetHandleInformation(*pin, HANDLE_FLAG_INHERIT, 0)) {
|
||||
LOGE("SetHandleInformation stdin failed");
|
||||
goto error_close_stdin;
|
||||
}
|
||||
}
|
||||
if (pout) {
|
||||
if (!CreatePipe(pout, &stdout_write_handle, &sa, 0)) {
|
||||
perror("pipe");
|
||||
goto error_close_stdin;
|
||||
}
|
||||
if (!SetHandleInformation(*pout, HANDLE_FLAG_INHERIT, 0)) {
|
||||
LOGE("SetHandleInformation stdout failed");
|
||||
goto error_close_stdout;
|
||||
}
|
||||
}
|
||||
if (perr) {
|
||||
if (!CreatePipe(perr, &stderr_write_handle, &sa, 0)) {
|
||||
perror("pipe");
|
||||
goto error_close_stdout;
|
||||
}
|
||||
if (!SetHandleInformation(*perr, HANDLE_FLAG_INHERIT, 0)) {
|
||||
LOGE("SetHandleInformation stderr failed");
|
||||
goto error_close_stderr;
|
||||
}
|
||||
}
|
||||
|
||||
STARTUPINFOEXW si;
|
||||
PROCESS_INFORMATION pi;
|
||||
memset(&si, 0, sizeof(si));
|
||||
si.cb = sizeof(si);
|
||||
si.StartupInfo.cb = sizeof(si);
|
||||
HANDLE handles[3];
|
||||
|
||||
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
||||
if (inherit_stdout) {
|
||||
si.StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
}
|
||||
if (inherit_stderr) {
|
||||
si.StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
||||
}
|
||||
|
||||
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
|
||||
if (handle_count) {
|
||||
unsigned i = 0;
|
||||
if (pin) {
|
||||
si.StartupInfo.hStdInput = stdin_read_handle;
|
||||
handles[i++] = si.StartupInfo.hStdInput;
|
||||
}
|
||||
if (pout) {
|
||||
assert(!inherit_stdout);
|
||||
si.StartupInfo.hStdOutput = stdout_write_handle;
|
||||
handles[i++] = si.StartupInfo.hStdOutput;
|
||||
}
|
||||
if (perr) {
|
||||
assert(!inherit_stderr);
|
||||
si.StartupInfo.hStdError = stderr_write_handle;
|
||||
handles[i++] = si.StartupInfo.hStdError;
|
||||
}
|
||||
|
||||
SIZE_T size;
|
||||
// Call it once to know the required buffer size
|
||||
BOOL ok =
|
||||
InitializeProcThreadAttributeList(NULL, 1, 0, &size)
|
||||
|| GetLastError() == ERROR_INSUFFICIENT_BUFFER;
|
||||
if (!ok) {
|
||||
goto error_close_stderr;
|
||||
}
|
||||
|
||||
lpAttributeList = malloc(size);
|
||||
if (!lpAttributeList) {
|
||||
LOG_OOM();
|
||||
goto error_close_stderr;
|
||||
}
|
||||
|
||||
ok = InitializeProcThreadAttributeList(lpAttributeList, 1, 0, &size);
|
||||
if (!ok) {
|
||||
free(lpAttributeList);
|
||||
goto error_close_stderr;
|
||||
}
|
||||
|
||||
ok = UpdateProcThreadAttribute(lpAttributeList, 0,
|
||||
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
||||
handles, handle_count * sizeof(HANDLE),
|
||||
NULL, NULL);
|
||||
if (!ok) {
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
si.lpAttributeList = lpAttributeList;
|
||||
}
|
||||
|
||||
char *cmd = malloc(CMD_MAX_LEN);
|
||||
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
||||
*handle = NULL;
|
||||
return PROCESS_ERROR_GENERIC;
|
||||
LOG_OOM();
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
wchar_t *wide = utf8_to_wide_char(cmd);
|
||||
wchar_t *wide = sc_str_to_wchars(cmd);
|
||||
free(cmd);
|
||||
if (!wide) {
|
||||
LOGC("Could not allocate wide char string");
|
||||
return PROCESS_ERROR_GENERIC;
|
||||
LOG_OOM();
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, 0, NULL, NULL, &si,
|
||||
&pi)) {
|
||||
free(wide);
|
||||
*handle = NULL;
|
||||
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
||||
return PROCESS_ERROR_MISSING_BINARY;
|
||||
}
|
||||
return PROCESS_ERROR_GENERIC;
|
||||
BOOL bInheritHandles = handle_count > 0 || inherit_stdout || inherit_stderr;
|
||||
DWORD dwCreationFlags = 0;
|
||||
if (handle_count > 0) {
|
||||
dwCreationFlags |= EXTENDED_STARTUPINFO_PRESENT;
|
||||
}
|
||||
|
||||
if (!inherit_stdout && !inherit_stderr) {
|
||||
// DETACHED_PROCESS to disable stdin, stdout and stderr
|
||||
dwCreationFlags |= DETACHED_PROCESS;
|
||||
}
|
||||
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
|
||||
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
||||
free(wide);
|
||||
if (!ok) {
|
||||
int err = GetLastError();
|
||||
LOGE("CreateProcessW() error %d", err);
|
||||
if (err == ERROR_FILE_NOT_FOUND) {
|
||||
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
||||
}
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
if (lpAttributeList) {
|
||||
DeleteProcThreadAttributeList(lpAttributeList);
|
||||
free(lpAttributeList);
|
||||
}
|
||||
|
||||
// These handles are used by the child process, close them for this process
|
||||
if (pin) {
|
||||
CloseHandle(stdin_read_handle);
|
||||
}
|
||||
if (pout) {
|
||||
CloseHandle(stdout_write_handle);
|
||||
}
|
||||
if (perr) {
|
||||
CloseHandle(stderr_write_handle);
|
||||
}
|
||||
|
||||
*handle = pi.hProcess;
|
||||
return PROCESS_SUCCESS;
|
||||
|
||||
return SC_PROCESS_SUCCESS;
|
||||
|
||||
error_free_attribute_list:
|
||||
if (lpAttributeList) {
|
||||
DeleteProcThreadAttributeList(lpAttributeList);
|
||||
free(lpAttributeList);
|
||||
}
|
||||
error_close_stderr:
|
||||
if (perr) {
|
||||
CloseHandle(*perr);
|
||||
CloseHandle(stderr_write_handle);
|
||||
}
|
||||
error_close_stdout:
|
||||
if (pout) {
|
||||
CloseHandle(*pout);
|
||||
CloseHandle(stdout_write_handle);
|
||||
}
|
||||
error_close_stdin:
|
||||
if (pin) {
|
||||
CloseHandle(*pin);
|
||||
CloseHandle(stdin_read_handle);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
process_terminate(HANDLE handle) {
|
||||
sc_process_terminate(HANDLE handle) {
|
||||
return TerminateProcess(handle, 1);
|
||||
}
|
||||
|
||||
exit_code_t
|
||||
process_wait(HANDLE handle, bool close) {
|
||||
sc_exit_code
|
||||
sc_process_wait(HANDLE handle, bool close) {
|
||||
DWORD code;
|
||||
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0
|
||||
|| !GetExitCodeProcess(handle, &code)) {
|
||||
// could not wait or retrieve the exit code
|
||||
code = NO_EXIT_CODE; // max value, it's unsigned
|
||||
code = SC_EXIT_CODE_NONE;
|
||||
}
|
||||
if (close) {
|
||||
CloseHandle(handle);
|
||||
@ -77,42 +235,24 @@ process_wait(HANDLE handle, bool close) {
|
||||
}
|
||||
|
||||
void
|
||||
process_close(HANDLE handle) {
|
||||
sc_process_close(HANDLE handle) {
|
||||
bool closed = CloseHandle(handle);
|
||||
assert(closed);
|
||||
(void) closed;
|
||||
}
|
||||
|
||||
char *
|
||||
get_executable_path(void) {
|
||||
HMODULE hModule = GetModuleHandleW(NULL);
|
||||
if (!hModule) {
|
||||
return NULL;
|
||||
ssize_t
|
||||
sc_pipe_read(HANDLE pipe, char *data, size_t len) {
|
||||
DWORD r;
|
||||
if (!ReadFile(pipe, data, len, &r, NULL)) {
|
||||
return -1;
|
||||
}
|
||||
WCHAR buf[MAX_PATH + 1]; // +1 for the null byte
|
||||
int len = GetModuleFileNameW(hModule, buf, MAX_PATH);
|
||||
if (!len) {
|
||||
return NULL;
|
||||
}
|
||||
buf[len] = '\0';
|
||||
return utf8_from_wide_char(buf);
|
||||
return r;
|
||||
}
|
||||
|
||||
bool
|
||||
is_regular_file(const char *path) {
|
||||
wchar_t *wide_path = utf8_to_wide_char(path);
|
||||
if (!wide_path) {
|
||||
LOGC("Could not allocate wide char string");
|
||||
return false;
|
||||
void
|
||||
sc_pipe_close(HANDLE pipe) {
|
||||
if (!CloseHandle(pipe)) {
|
||||
LOGW("Cannot close pipe");
|
||||
}
|
||||
|
||||
struct _stat path_stat;
|
||||
int r = _wstat(wide_path, &path_stat);
|
||||
free(wide_path);
|
||||
|
||||
if (r) {
|
||||
perror("stat");
|
||||
return false;
|
||||
}
|
||||
return S_ISREG(path_stat.st_mode);
|
||||
}
|
||||
|
@ -1,119 +0,0 @@
|
||||
#include "tiny_xpm.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
struct index {
|
||||
char c;
|
||||
uint32_t color;
|
||||
};
|
||||
|
||||
static bool
|
||||
find_color(struct index *index, int len, char c, uint32_t *color) {
|
||||
// there are typically very few color, so it's ok to iterate over the array
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (index[i].c == c) {
|
||||
*color = index[i].color;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
*color = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// We encounter some problems with SDL2_image on MSYS2 (Windows),
|
||||
// so here is our own XPM parsing not to depend on SDL_image.
|
||||
//
|
||||
// We do not hardcode the binary image to keep some flexibility to replace the
|
||||
// icon easily (just by replacing icon.xpm).
|
||||
//
|
||||
// Parameter is not "const char *" because XPM formats are generally stored in a
|
||||
// (non-const) "char *"
|
||||
SDL_Surface *
|
||||
read_xpm(char *xpm[]) {
|
||||
#ifndef NDEBUG
|
||||
// patch the XPM to change the icon color in debug mode
|
||||
xpm[2] = ". c #CC00CC";
|
||||
#endif
|
||||
|
||||
char *endptr;
|
||||
// *** No error handling, assume the XPM source is valid ***
|
||||
// (it's in our source repo)
|
||||
// Assertions are only checked in debug
|
||||
int width = strtol(xpm[0], &endptr, 10);
|
||||
int height = strtol(endptr + 1, &endptr, 10);
|
||||
int colors = strtol(endptr + 1, &endptr, 10);
|
||||
int chars = strtol(endptr + 1, &endptr, 10);
|
||||
|
||||
// sanity checks
|
||||
assert(0 <= width && width < 256);
|
||||
assert(0 <= height && height < 256);
|
||||
assert(0 <= colors && colors < 256);
|
||||
assert(chars == 1); // this implementation does not support more
|
||||
|
||||
(void) chars;
|
||||
|
||||
// init index
|
||||
struct index index[colors];
|
||||
for (int i = 0; i < colors; ++i) {
|
||||
const char *line = xpm[1+i];
|
||||
index[i].c = line[0];
|
||||
assert(line[1] == '\t');
|
||||
assert(line[2] == 'c');
|
||||
assert(line[3] == ' ');
|
||||
if (line[4] == '#') {
|
||||
index[i].color = 0xff000000 | strtol(&line[5], &endptr, 0x10);
|
||||
assert(*endptr == '\0');
|
||||
} else {
|
||||
assert(!strcmp("None", &line[4]));
|
||||
index[i].color = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// parse image
|
||||
uint32_t *pixels = SDL_malloc(4 * width * height);
|
||||
if (!pixels) {
|
||||
LOGE("Could not allocate icon memory");
|
||||
return NULL;
|
||||
}
|
||||
for (int y = 0; y < height; ++y) {
|
||||
const char *line = xpm[1 + colors + y];
|
||||
for (int x = 0; x < width; ++x) {
|
||||
char c = line[x];
|
||||
uint32_t color;
|
||||
bool color_found = find_color(index, colors, c, &color);
|
||||
assert(color_found);
|
||||
(void) color_found;
|
||||
pixels[y * width + x] = color;
|
||||
}
|
||||
}
|
||||
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
uint32_t amask = 0x000000ff;
|
||||
uint32_t rmask = 0x0000ff00;
|
||||
uint32_t gmask = 0x00ff0000;
|
||||
uint32_t bmask = 0xff000000;
|
||||
#else // little endian, like x86
|
||||
uint32_t amask = 0xff000000;
|
||||
uint32_t rmask = 0x00ff0000;
|
||||
uint32_t gmask = 0x0000ff00;
|
||||
uint32_t bmask = 0x000000ff;
|
||||
#endif
|
||||
|
||||
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(pixels,
|
||||
width, height,
|
||||
32, 4 * width,
|
||||
rmask, gmask, bmask, amask);
|
||||
if (!surface) {
|
||||
LOGE("Could not create icon surface");
|
||||
return NULL;
|
||||
}
|
||||
// make the surface own the raw pixels
|
||||
surface->flags &= ~SDL_PREALLOC;
|
||||
return surface;
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
#ifndef TINYXPM_H
|
||||
#define TINYXPM_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
SDL_Surface *
|
||||
read_xpm(char *xpm[]);
|
||||
|
||||
#endif
|
@ -1,5 +1,5 @@
|
||||
#ifndef SC_FRAME_SINK
|
||||
#define SC_FRAME_SINK
|
||||
#ifndef SC_FRAME_SINK_H
|
||||
#define SC_FRAME_SINK_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
48
app/src/trait/key_processor.h
Normal file
48
app/src/trait/key_processor.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef SC_KEY_PROCESSOR_H
|
||||
#define SC_KEY_PROCESSOR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "input_events.h"
|
||||
|
||||
/**
|
||||
* Key processor trait.
|
||||
*
|
||||
* Component able to process and inject keys should implement this trait.
|
||||
*/
|
||||
struct sc_key_processor {
|
||||
/**
|
||||
* Set by the implementation to indicate that it must explicitly wait for
|
||||
* the clipboard to be set on the device before injecting Ctrl+v to avoid
|
||||
* race conditions. If it is set, the input_manager will pass a valid
|
||||
* ack_to_wait to process_key() in case of clipboard synchronization
|
||||
* resulting of the key event.
|
||||
*/
|
||||
bool async_paste;
|
||||
|
||||
const struct sc_key_processor_ops *ops;
|
||||
};
|
||||
|
||||
struct sc_key_processor_ops {
|
||||
|
||||
/**
|
||||
* Process the keyboard event
|
||||
*
|
||||
* The `sequence` number (if different from `SC_SEQUENCE_INVALID`) indicates
|
||||
* the acknowledgement number to wait for before injecting this event.
|
||||
* This allows to ensure that the device clipboard is set before injecting
|
||||
* Ctrl+v on the device.
|
||||
*/
|
||||
void
|
||||
(*process_key)(struct sc_key_processor *kp,
|
||||
const struct sc_key_event *event, uint64_t ack_to_wait);
|
||||
|
||||
void
|
||||
(*process_text)(struct sc_key_processor *kp,
|
||||
const struct sc_text_event *event);
|
||||
};
|
||||
|
||||
#endif
|
39
app/src/trait/mouse_processor.h
Normal file
39
app/src/trait/mouse_processor.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef SC_MOUSE_PROCESSOR_H
|
||||
#define SC_MOUSE_PROCESSOR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <SDL2/SDL_events.h>
|
||||
|
||||
/**
|
||||
* Mouse processor trait.
|
||||
*
|
||||
* Component able to process and inject mouse events should implement this
|
||||
* trait.
|
||||
*/
|
||||
struct sc_mouse_processor {
|
||||
const struct sc_mouse_processor_ops *ops;
|
||||
};
|
||||
|
||||
struct sc_mouse_processor_ops {
|
||||
void
|
||||
(*process_mouse_motion)(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseMotionEvent *event);
|
||||
|
||||
void
|
||||
(*process_touch)(struct sc_mouse_processor *mp,
|
||||
const SDL_TouchFingerEvent *event);
|
||||
|
||||
void
|
||||
(*process_mouse_button)(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseButtonEvent *event);
|
||||
|
||||
void
|
||||
(*process_mouse_wheel)(struct sc_mouse_processor *mp,
|
||||
const SDL_MouseWheelEvent *event);
|
||||
};
|
||||
|
||||
#endif
|
@ -1,5 +1,5 @@
|
||||
#ifndef SC_PACKET_SINK
|
||||
#define SC_PACKET_SINK
|
||||
#ifndef SC_PACKET_SINK_H
|
||||
#define SC_PACKET_SINK_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
76
app/src/util/acksync.c
Normal file
76
app/src/util/acksync.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include "acksync.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
sc_acksync_init(struct sc_acksync *as) {
|
||||
bool ok = sc_mutex_init(&as->mutex);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_cond_init(&as->cond);
|
||||
if (!ok) {
|
||||
sc_mutex_destroy(&as->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
as->stopped = false;
|
||||
as->ack = SC_SEQUENCE_INVALID;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_acksync_destroy(struct sc_acksync *as) {
|
||||
sc_cond_destroy(&as->cond);
|
||||
sc_mutex_destroy(&as->mutex);
|
||||
}
|
||||
|
||||
void
|
||||
sc_acksync_ack(struct sc_acksync *as, uint64_t sequence) {
|
||||
sc_mutex_lock(&as->mutex);
|
||||
|
||||
// Acknowledgements must be monotonic
|
||||
assert(sequence >= as->ack);
|
||||
|
||||
as->ack = sequence;
|
||||
sc_cond_signal(&as->cond);
|
||||
|
||||
sc_mutex_unlock(&as->mutex);
|
||||
}
|
||||
|
||||
enum sc_acksync_wait_result
|
||||
sc_acksync_wait(struct sc_acksync *as, uint64_t ack, sc_tick deadline) {
|
||||
sc_mutex_lock(&as->mutex);
|
||||
|
||||
bool timed_out = false;
|
||||
while (!as->stopped && as->ack < ack && !timed_out) {
|
||||
timed_out = !sc_cond_timedwait(&as->cond, &as->mutex, deadline);
|
||||
}
|
||||
|
||||
enum sc_acksync_wait_result ret;
|
||||
if (as->stopped) {
|
||||
ret = SC_ACKSYNC_WAIT_INTR;
|
||||
} else if (as->ack >= ack) {
|
||||
ret = SC_ACKSYNC_WAIT_OK;
|
||||
} else {
|
||||
assert(timed_out);
|
||||
ret = SC_ACKSYNC_WAIT_TIMEOUT;
|
||||
}
|
||||
sc_mutex_unlock(&as->mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interrupt any `sc_acksync_wait()`
|
||||
*/
|
||||
void
|
||||
sc_acksync_interrupt(struct sc_acksync *as) {
|
||||
sc_mutex_lock(&as->mutex);
|
||||
as->stopped = true;
|
||||
sc_cond_signal(&as->cond);
|
||||
sc_mutex_unlock(&as->mutex);
|
||||
}
|
66
app/src/util/acksync.h
Normal file
66
app/src/util/acksync.h
Normal file
@ -0,0 +1,66 @@
|
||||
#ifndef SC_ACK_SYNC_H
|
||||
#define SC_ACK_SYNC_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "thread.h"
|
||||
|
||||
#define SC_SEQUENCE_INVALID 0
|
||||
|
||||
/**
|
||||
* Helper to wait for acknowledgments
|
||||
*
|
||||
* In practice, it is used to wait for device clipboard acknowledgement from the
|
||||
* server before injecting Ctrl+v via AOA HID, in order to avoid pasting the
|
||||
* content of the old device clipboard (if Ctrl+v was injected before the
|
||||
* clipboard content was actually set).
|
||||
*/
|
||||
struct sc_acksync {
|
||||
sc_mutex mutex;
|
||||
sc_cond cond;
|
||||
|
||||
bool stopped;
|
||||
|
||||
// Last acked value, initially SC_SEQUENCE_INVALID
|
||||
uint64_t ack;
|
||||
};
|
||||
|
||||
enum sc_acksync_wait_result {
|
||||
// Acknowledgment received
|
||||
SC_ACKSYNC_WAIT_OK,
|
||||
|
||||
// Timeout expired
|
||||
SC_ACKSYNC_WAIT_TIMEOUT,
|
||||
|
||||
// Interrupted from another thread by sc_acksync_interrupt()
|
||||
SC_ACKSYNC_WAIT_INTR,
|
||||
};
|
||||
|
||||
bool
|
||||
sc_acksync_init(struct sc_acksync *as);
|
||||
|
||||
void
|
||||
sc_acksync_destroy(struct sc_acksync *as);
|
||||
|
||||
/**
|
||||
* Acknowledge `sequence`
|
||||
*
|
||||
* The `sequence` must be greater than (or equal to) any previous acknowledged
|
||||
* sequence.
|
||||
*/
|
||||
void
|
||||
sc_acksync_ack(struct sc_acksync *as, uint64_t sequence);
|
||||
|
||||
/**
|
||||
* Wait for acknowledgment of sequence `ack` (or higher)
|
||||
*/
|
||||
enum sc_acksync_wait_result
|
||||
sc_acksync_wait(struct sc_acksync *as, uint64_t ack, sc_tick deadline);
|
||||
|
||||
/**
|
||||
* Interrupt any `sc_acksync_wait()`
|
||||
*/
|
||||
void
|
||||
sc_acksync_interrupt(struct sc_acksync *as);
|
||||
|
||||
#endif
|
48
app/src/util/file.c
Normal file
48
app/src/util/file.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
char *
|
||||
sc_file_get_local_path(const char *name) {
|
||||
char *executable_path = sc_file_get_executable_path();
|
||||
if (!executable_path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// dirname() does not work correctly everywhere, so get the parent
|
||||
// directory manually.
|
||||
// See <https://github.com/Genymobile/scrcpy/issues/2619>
|
||||
char *p = strrchr(executable_path, SC_PATH_SEPARATOR);
|
||||
if (!p) {
|
||||
LOGE("Unexpected executable path: \"%s\" (it should contain a '%c')",
|
||||
executable_path, SC_PATH_SEPARATOR);
|
||||
free(executable_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*p = '\0'; // modify executable_path in place
|
||||
char *dir = executable_path;
|
||||
size_t dirlen = strlen(dir);
|
||||
size_t namelen = strlen(name);
|
||||
|
||||
size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
|
||||
char *file_path = malloc(len);
|
||||
if (!file_path) {
|
||||
LOG_OOM();
|
||||
free(executable_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(file_path, dir, dirlen);
|
||||
file_path[dirlen] = SC_PATH_SEPARATOR;
|
||||
// namelen + 1 to copy the final '\0'
|
||||
memcpy(&file_path[dirlen + 1], name, namelen + 1);
|
||||
|
||||
free(executable_path);
|
||||
|
||||
return file_path;
|
||||
}
|
||||
|
49
app/src/util/file.h
Normal file
49
app/src/util/file.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef SC_FILE_H
|
||||
#define SC_FILE_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
# define SC_PATH_SEPARATOR '\\'
|
||||
#else
|
||||
# define SC_PATH_SEPARATOR '/'
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
/**
|
||||
* Indicate if an executable exists using $PATH
|
||||
*
|
||||
* In practice, it is only used to know if a package manager is available on
|
||||
* the system. It is only implemented on Linux.
|
||||
*/
|
||||
bool
|
||||
sc_file_executable_exists(const char *file);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Return the absolute path of the executable (the scrcpy binary)
|
||||
*
|
||||
* The result must be freed by the caller using free(). It may return NULL on
|
||||
* error.
|
||||
*/
|
||||
char *
|
||||
sc_file_get_executable_path(void);
|
||||
|
||||
/**
|
||||
* Return the absolute path of a file in the same directory as the executable
|
||||
*
|
||||
* The result must be freed by the caller using free(). It may return NULL on
|
||||
* error.
|
||||
*/
|
||||
char *
|
||||
sc_file_get_local_path(const char *name);
|
||||
|
||||
/**
|
||||
* Indicate if the file exists and is not a directory
|
||||
*/
|
||||
bool
|
||||
sc_file_is_regular(const char *path);
|
||||
|
||||
#endif
|
13
app/src/util/intmap.c
Normal file
13
app/src/util/intmap.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include "intmap.h"
|
||||
|
||||
const struct sc_intmap_entry *
|
||||
sc_intmap_find_entry(const struct sc_intmap_entry entries[], size_t len,
|
||||
int32_t key) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
const struct sc_intmap_entry *entry = &entries[i];
|
||||
if (entry->key == key) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
24
app/src/util/intmap.h
Normal file
24
app/src/util/intmap.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef SC_ARRAYMAP_H
|
||||
#define SC_ARRAYMAP_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct sc_intmap_entry {
|
||||
int32_t key;
|
||||
int32_t value;
|
||||
};
|
||||
|
||||
const struct sc_intmap_entry *
|
||||
sc_intmap_find_entry(const struct sc_intmap_entry entries[], size_t len,
|
||||
int32_t key);
|
||||
|
||||
/**
|
||||
* MAP is expected to be a static array of sc_intmap_entry, so that
|
||||
* ARRAY_LEN(MAP) can be computed statically.
|
||||
*/
|
||||
#define SC_INTMAP_FIND_ENTRY(MAP, KEY) \
|
||||
sc_intmap_find_entry(MAP, ARRAY_LEN(MAP), KEY)
|
||||
|
||||
#endif
|
83
app/src/util/intr.c
Normal file
83
app/src/util/intr.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include "intr.h"
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
bool
|
||||
sc_intr_init(struct sc_intr *intr) {
|
||||
bool ok = sc_mutex_init(&intr->mutex);
|
||||
if (!ok) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
intr->socket = SC_SOCKET_NONE;
|
||||
intr->process = SC_PROCESS_NONE;
|
||||
|
||||
atomic_store_explicit(&intr->interrupted, false, memory_order_relaxed);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_intr_set_socket(struct sc_intr *intr, sc_socket socket) {
|
||||
assert(intr->process == SC_PROCESS_NONE);
|
||||
|
||||
sc_mutex_lock(&intr->mutex);
|
||||
bool interrupted =
|
||||
atomic_load_explicit(&intr->interrupted, memory_order_relaxed);
|
||||
if (!interrupted) {
|
||||
intr->socket = socket;
|
||||
}
|
||||
sc_mutex_unlock(&intr->mutex);
|
||||
|
||||
return !interrupted;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_intr_set_process(struct sc_intr *intr, sc_pid pid) {
|
||||
assert(intr->socket == SC_SOCKET_NONE);
|
||||
|
||||
sc_mutex_lock(&intr->mutex);
|
||||
bool interrupted =
|
||||
atomic_load_explicit(&intr->interrupted, memory_order_relaxed);
|
||||
if (!interrupted) {
|
||||
intr->process = pid;
|
||||
}
|
||||
sc_mutex_unlock(&intr->mutex);
|
||||
|
||||
return !interrupted;
|
||||
}
|
||||
|
||||
void
|
||||
sc_intr_interrupt(struct sc_intr *intr) {
|
||||
sc_mutex_lock(&intr->mutex);
|
||||
|
||||
atomic_store_explicit(&intr->interrupted, true, memory_order_relaxed);
|
||||
|
||||
// No more than one component to interrupt
|
||||
assert(intr->socket == SC_SOCKET_NONE ||
|
||||
intr->process == SC_PROCESS_NONE);
|
||||
|
||||
if (intr->socket != SC_SOCKET_NONE) {
|
||||
LOGD("Interrupting socket");
|
||||
net_interrupt(intr->socket);
|
||||
intr->socket = SC_SOCKET_NONE;
|
||||
}
|
||||
if (intr->process != SC_PROCESS_NONE) {
|
||||
LOGD("Interrupting process");
|
||||
sc_process_terminate(intr->process);
|
||||
intr->process = SC_PROCESS_NONE;
|
||||
}
|
||||
|
||||
sc_mutex_unlock(&intr->mutex);
|
||||
}
|
||||
|
||||
void
|
||||
sc_intr_destroy(struct sc_intr *intr) {
|
||||
assert(intr->socket == SC_SOCKET_NONE);
|
||||
assert(intr->process == SC_PROCESS_NONE);
|
||||
|
||||
sc_mutex_destroy(&intr->mutex);
|
||||
}
|
78
app/src/util/intr.h
Normal file
78
app/src/util/intr.h
Normal file
@ -0,0 +1,78 @@
|
||||
#ifndef SC_INTR_H
|
||||
#define SC_INTR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "process.h"
|
||||
#include "thread.h"
|
||||
|
||||
/**
|
||||
* Interruptor to wake up a blocking call from another thread
|
||||
*
|
||||
* It allows to register a socket or a process before a blocking call, and
|
||||
* interrupt/close from another thread to wake up the blocking call.
|
||||
*/
|
||||
struct sc_intr {
|
||||
sc_mutex mutex;
|
||||
|
||||
sc_socket socket;
|
||||
sc_pid process;
|
||||
|
||||
// Written protected by the mutex to avoid race conditions against
|
||||
// sc_intr_set_socket() and sc_intr_set_process(), but can be read
|
||||
// (atomically) without mutex
|
||||
atomic_bool interrupted;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize an interruptor
|
||||
*/
|
||||
bool
|
||||
sc_intr_init(struct sc_intr *intr);
|
||||
|
||||
/**
|
||||
* Set a socket as the interruptible component
|
||||
*
|
||||
* Call with SC_SOCKET_NONE to unset.
|
||||
*/
|
||||
bool
|
||||
sc_intr_set_socket(struct sc_intr *intr, sc_socket socket);
|
||||
|
||||
/**
|
||||
* Set a process as the interruptible component
|
||||
*
|
||||
* Call with SC_PROCESS_NONE to unset.
|
||||
*/
|
||||
bool
|
||||
sc_intr_set_process(struct sc_intr *intr, sc_pid socket);
|
||||
|
||||
/**
|
||||
* Interrupt the current interruptible component
|
||||
*
|
||||
* Must be called from a different thread.
|
||||
*/
|
||||
void
|
||||
sc_intr_interrupt(struct sc_intr *intr);
|
||||
|
||||
/**
|
||||
* Read the interrupted state
|
||||
*
|
||||
* It is exposed as a static inline function because it just loads from an
|
||||
* atomic.
|
||||
*/
|
||||
static inline bool
|
||||
sc_intr_is_interrupted(struct sc_intr *intr) {
|
||||
return atomic_load_explicit(&intr->interrupted, memory_order_relaxed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the interruptor
|
||||
*/
|
||||
void
|
||||
sc_intr_destroy(struct sc_intr *intr);
|
||||
|
||||
#endif
|
@ -5,7 +5,10 @@
|
||||
|
||||
#include <SDL2/SDL_log.h>
|
||||
|
||||
#include "scrcpy.h"
|
||||
#include "options.h"
|
||||
|
||||
#define LOG_STR_IMPL_(x) # x
|
||||
#define LOG_STR(x) LOG_STR_IMPL_(x)
|
||||
|
||||
#define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
@ -14,6 +17,9 @@
|
||||
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
|
||||
#define LOG_OOM() \
|
||||
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
|
||||
|
||||
void
|
||||
sc_set_log_level(enum sc_log_level level);
|
||||
|
||||
|
@ -1,139 +1,32 @@
|
||||
#include "net.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <SDL2/SDL_platform.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
# include <ws2tcpip.h>
|
||||
typedef int socklen_t;
|
||||
typedef SOCKET sc_raw_socket;
|
||||
# define SC_RAW_SOCKET_NONE INVALID_SOCKET
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
# include <sys/socket.h>
|
||||
# include <netinet/in.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# define SOCKET_ERROR -1
|
||||
typedef struct sockaddr_in SOCKADDR_IN;
|
||||
typedef struct sockaddr SOCKADDR;
|
||||
typedef struct in_addr IN_ADDR;
|
||||
typedef int sc_raw_socket;
|
||||
# define SC_RAW_SOCKET_NONE -1
|
||||
#endif
|
||||
|
||||
static void
|
||||
net_perror(const char *s) {
|
||||
#ifdef _WIN32
|
||||
int error = WSAGetLastError();
|
||||
char *wsa_message;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(char *) &wsa_message, 0, NULL);
|
||||
// no explicit '\n', wsa_message already contains a trailing '\n'
|
||||
fprintf(stderr, "%s: [%d] %s", s, error, wsa_message);
|
||||
LocalFree(wsa_message);
|
||||
#else
|
||||
perror(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
socket_t
|
||||
net_connect(uint32_t addr, uint16_t port) {
|
||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
net_perror("socket");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(addr);
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("connect");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
socket_t
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
net_perror("socket");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
int reuse = 1;
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
||||
sizeof(reuse)) == -1) {
|
||||
net_perror("setsockopt(SO_REUSEADDR)");
|
||||
}
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(addr); // htonl() harmless on INADDR_ANY
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("bind");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (listen(sock, backlog) == SOCKET_ERROR) {
|
||||
net_perror("listen");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
socket_t
|
||||
net_accept(socket_t server_socket) {
|
||||
SOCKADDR_IN csin;
|
||||
socklen_t sinsize = sizeof(csin);
|
||||
return accept(server_socket, (SOCKADDR *) &csin, &sinsize);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv(socket_t socket, void *buf, size_t len) {
|
||||
return recv(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv_all(socket_t socket, void *buf, size_t len) {
|
||||
return recv(socket, buf, len, MSG_WAITALL);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send(socket_t socket, const void *buf, size_t len) {
|
||||
return send(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send_all(socket_t socket, const void *buf, size_t len) {
|
||||
size_t copied = 0;
|
||||
ssize_t w = 0;
|
||||
while (len > 0) {
|
||||
w = send(socket, buf, len, 0);
|
||||
if (w == -1) {
|
||||
return copied ? (ssize_t) copied : -1;
|
||||
}
|
||||
len -= w;
|
||||
buf = (char *) buf + w;
|
||||
copied += w;
|
||||
}
|
||||
return copied;
|
||||
}
|
||||
|
||||
bool
|
||||
net_shutdown(socket_t socket, int how) {
|
||||
return !shutdown(socket, how);
|
||||
}
|
||||
|
||||
bool
|
||||
net_init(void) {
|
||||
#ifdef __WINDOWS__
|
||||
@ -154,11 +47,246 @@ net_cleanup(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
net_close(socket_t socket) {
|
||||
static inline sc_socket
|
||||
wrap(sc_raw_socket sock) {
|
||||
#ifdef __WINDOWS__
|
||||
return !closesocket(socket);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
|
||||
struct sc_socket_windows *socket = malloc(sizeof(*socket));
|
||||
if (!socket) {
|
||||
LOG_OOM();
|
||||
closesocket(sock);
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
|
||||
socket->socket = sock;
|
||||
socket->closed = (atomic_flag) ATOMIC_FLAG_INIT;
|
||||
|
||||
return socket;
|
||||
#else
|
||||
return !close(socket);
|
||||
return sock;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline sc_raw_socket
|
||||
unwrap(sc_socket socket) {
|
||||
#ifdef __WINDOWS__
|
||||
if (socket == SC_SOCKET_NONE) {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return socket->socket;
|
||||
#else
|
||||
return socket;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline bool
|
||||
sc_raw_socket_close(sc_raw_socket raw_sock) {
|
||||
#ifndef _WIN32
|
||||
return !close(raw_sock);
|
||||
#else
|
||||
return !closesocket(raw_sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef HAVE_SOCK_CLOEXEC
|
||||
// If SOCK_CLOEXEC does not exist, the flag must be set manually once the
|
||||
// socket is created
|
||||
static bool
|
||||
set_cloexec_flag(sc_raw_socket raw_sock) {
|
||||
#ifndef _WIN32
|
||||
if (fcntl(raw_sock, F_SETFD, FD_CLOEXEC) == -1) {
|
||||
perror("fcntl F_SETFD");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
if (!SetHandleInformation((HANDLE) raw_sock, HANDLE_FLAG_INHERIT, 0)) {
|
||||
LOGE("SetHandleInformation socket failed");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
net_perror(const char *s) {
|
||||
#ifdef _WIN32
|
||||
int error = WSAGetLastError();
|
||||
char *wsa_message;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(char *) &wsa_message, 0, NULL);
|
||||
// no explicit '\n', wsa_message already contains a trailing '\n'
|
||||
fprintf(stderr, "%s: [%d] %s", s, error, wsa_message);
|
||||
LocalFree(wsa_message);
|
||||
#else
|
||||
perror(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
sc_socket
|
||||
net_socket(void) {
|
||||
#ifdef HAVE_SOCK_CLOEXEC
|
||||
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
#else
|
||||
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (raw_sock != SC_RAW_SOCKET_NONE && !set_cloexec_flag(raw_sock)) {
|
||||
sc_raw_socket_close(raw_sock);
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
#endif
|
||||
|
||||
sc_socket sock = wrap(raw_sock);
|
||||
if (sock == SC_SOCKET_NONE) {
|
||||
net_perror("socket");
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
bool
|
||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(addr);
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("connect");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
int reuse = 1;
|
||||
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
||||
sizeof(reuse)) == -1) {
|
||||
net_perror("setsockopt(SO_REUSEADDR)");
|
||||
}
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(addr); // htonl() harmless on INADDR_ANY
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("bind");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
||||
net_perror("listen");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sc_socket
|
||||
net_accept(sc_socket server_socket) {
|
||||
sc_raw_socket raw_server_socket = unwrap(server_socket);
|
||||
|
||||
SOCKADDR_IN csin;
|
||||
socklen_t sinsize = sizeof(csin);
|
||||
|
||||
#ifdef HAVE_SOCK_CLOEXEC
|
||||
sc_raw_socket raw_sock =
|
||||
accept4(raw_server_socket, (SOCKADDR *) &csin, &sinsize, SOCK_CLOEXEC);
|
||||
#else
|
||||
sc_raw_socket raw_sock =
|
||||
accept(raw_server_socket, (SOCKADDR *) &csin, &sinsize);
|
||||
if (raw_sock != SC_RAW_SOCKET_NONE && !set_cloexec_flag(raw_sock)) {
|
||||
sc_raw_socket_close(raw_sock);
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
#endif
|
||||
|
||||
return wrap(raw_sock);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv(sc_socket socket, void *buf, size_t len) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
return recv(raw_sock, buf, len, 0);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv_all(sc_socket socket, void *buf, size_t len) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
return recv(raw_sock, buf, len, MSG_WAITALL);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send(sc_socket socket, const void *buf, size_t len) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
return send(raw_sock, buf, len, 0);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send_all(sc_socket socket, const void *buf, size_t len) {
|
||||
size_t copied = 0;
|
||||
while (len > 0) {
|
||||
ssize_t w = net_send(socket, buf, len);
|
||||
if (w == -1) {
|
||||
return copied ? (ssize_t) copied : -1;
|
||||
}
|
||||
len -= w;
|
||||
buf = (char *) buf + w;
|
||||
copied += w;
|
||||
}
|
||||
return copied;
|
||||
}
|
||||
|
||||
bool
|
||||
net_interrupt(sc_socket socket) {
|
||||
assert(socket != SC_SOCKET_NONE);
|
||||
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
if (!atomic_flag_test_and_set(&socket->closed)) {
|
||||
return !closesocket(raw_sock);
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
return !shutdown(raw_sock, SHUT_RDWR);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
net_close(sc_socket socket) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
bool ret = true;
|
||||
if (!atomic_flag_test_and_set(&socket->closed)) {
|
||||
ret = !closesocket(raw_sock);
|
||||
}
|
||||
free(socket);
|
||||
return ret;
|
||||
#else
|
||||
return !close(raw_sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
net_parse_ipv4(const char *s, uint32_t *ipv4) {
|
||||
struct in_addr addr;
|
||||
if (!inet_pton(AF_INET, s, &addr)) {
|
||||
LOGE("Invalid IPv4 address: %s", s);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ipv4 = ntohl(addr.s_addr);
|
||||
return true;
|
||||
}
|
||||
|
@ -8,50 +8,70 @@
|
||||
#include <SDL2/SDL_platform.h>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
# include <winsock2.h>
|
||||
#define SHUT_RD SD_RECEIVE
|
||||
#define SHUT_WR SD_SEND
|
||||
#define SHUT_RDWR SD_BOTH
|
||||
typedef SOCKET socket_t;
|
||||
#else
|
||||
# include <stdatomic.h>
|
||||
# define SC_SOCKET_NONE NULL
|
||||
typedef struct sc_socket_windows {
|
||||
SOCKET socket;
|
||||
atomic_flag closed;
|
||||
} *sc_socket;
|
||||
|
||||
#else // not __WINDOWS__
|
||||
|
||||
# include <sys/socket.h>
|
||||
# define INVALID_SOCKET -1
|
||||
typedef int socket_t;
|
||||
# define SC_SOCKET_NONE -1
|
||||
typedef int sc_socket;
|
||||
|
||||
#endif
|
||||
|
||||
#define IPV4_LOCALHOST 0x7F000001
|
||||
|
||||
bool
|
||||
net_init(void);
|
||||
|
||||
void
|
||||
net_cleanup(void);
|
||||
|
||||
socket_t
|
||||
net_connect(uint32_t addr, uint16_t port);
|
||||
sc_socket
|
||||
net_socket(void);
|
||||
|
||||
socket_t
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog);
|
||||
bool
|
||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port);
|
||||
|
||||
socket_t
|
||||
net_accept(socket_t server_socket);
|
||||
bool
|
||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog);
|
||||
|
||||
sc_socket
|
||||
net_accept(sc_socket server_socket);
|
||||
|
||||
// the _all versions wait/retry until len bytes have been written/read
|
||||
ssize_t
|
||||
net_recv(socket_t socket, void *buf, size_t len);
|
||||
net_recv(sc_socket socket, void *buf, size_t len);
|
||||
|
||||
ssize_t
|
||||
net_recv_all(socket_t socket, void *buf, size_t len);
|
||||
net_recv_all(sc_socket socket, void *buf, size_t len);
|
||||
|
||||
ssize_t
|
||||
net_send(socket_t socket, const void *buf, size_t len);
|
||||
net_send(sc_socket socket, const void *buf, size_t len);
|
||||
|
||||
ssize_t
|
||||
net_send_all(socket_t socket, const void *buf, size_t len);
|
||||
net_send_all(sc_socket socket, const void *buf, size_t len);
|
||||
|
||||
// how is SHUT_RD (read), SHUT_WR (write) or SHUT_RDWR (both)
|
||||
// Shutdown the socket (or close on Windows) so that any blocking send() or
|
||||
// recv() are interrupted.
|
||||
bool
|
||||
net_shutdown(socket_t socket, int how);
|
||||
net_interrupt(sc_socket socket);
|
||||
|
||||
// Close the socket.
|
||||
// A socket must always be closed, even if net_interrupt() has been called.
|
||||
bool
|
||||
net_close(socket_t socket);
|
||||
net_close(sc_socket socket);
|
||||
|
||||
/**
|
||||
* Parse `ip` "xxx.xxx.xxx.xxx" to an IPv4 host representation
|
||||
*/
|
||||
bool
|
||||
net_parse_ipv4(const char *ip, uint32_t *ipv4);
|
||||
|
||||
#endif
|
||||
|
97
app/src/util/net_intr.c
Normal file
97
app/src/util/net_intr.c
Normal file
@ -0,0 +1,97 @@
|
||||
#include "net_intr.h"
|
||||
|
||||
bool
|
||||
net_connect_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
|
||||
uint16_t port) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = net_connect(socket, addr, port);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
net_listen_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
|
||||
uint16_t port, int backlog) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = net_listen(socket, addr, port, backlog);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
sc_socket
|
||||
net_accept_intr(struct sc_intr *intr, sc_socket server_socket) {
|
||||
if (!sc_intr_set_socket(intr, server_socket)) {
|
||||
// Already interrupted
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
|
||||
sc_socket socket = net_accept(server_socket);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return socket;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv_intr(struct sc_intr *intr, sc_socket socket, void *buf, size_t len) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t r = net_recv(socket, buf, len);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_recv_all_intr(struct sc_intr *intr, sc_socket socket, void *buf,
|
||||
size_t len) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t r = net_recv_all(socket, buf, len);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
|
||||
size_t len) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t w = net_send(socket, buf, len);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return w;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
net_send_all_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
|
||||
size_t len) {
|
||||
if (!sc_intr_set_socket(intr, socket)) {
|
||||
// Already interrupted
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t w = net_send_all(socket, buf, len);
|
||||
|
||||
sc_intr_set_socket(intr, SC_SOCKET_NONE);
|
||||
return w;
|
||||
}
|
35
app/src/util/net_intr.h
Normal file
35
app/src/util/net_intr.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef SC_NET_INTR_H
|
||||
#define SC_NET_INTR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "intr.h"
|
||||
#include "net.h"
|
||||
|
||||
bool
|
||||
net_connect_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
|
||||
uint16_t port);
|
||||
|
||||
bool
|
||||
net_listen_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
|
||||
uint16_t port, int backlog);
|
||||
|
||||
sc_socket
|
||||
net_accept_intr(struct sc_intr *intr, sc_socket server_socket);
|
||||
|
||||
ssize_t
|
||||
net_recv_intr(struct sc_intr *intr, sc_socket socket, void *buf, size_t len);
|
||||
|
||||
ssize_t
|
||||
net_recv_all_intr(struct sc_intr *intr, sc_socket socket, void *buf,
|
||||
size_t len);
|
||||
|
||||
ssize_t
|
||||
net_send_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
|
||||
size_t len);
|
||||
|
||||
ssize_t
|
||||
net_send_all_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
|
||||
size_t len);
|
||||
|
||||
#endif
|
@ -1,21 +1,102 @@
|
||||
#include "process.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <libgen.h>
|
||||
#include "log.h"
|
||||
|
||||
bool
|
||||
process_check_success(process_t proc, const char *name, bool close) {
|
||||
if (proc == PROCESS_NONE) {
|
||||
LOGE("Could not execute \"%s\"", name);
|
||||
return false;
|
||||
}
|
||||
exit_code_t exit_code = process_wait(proc, close);
|
||||
if (exit_code) {
|
||||
if (exit_code != NO_EXIT_CODE) {
|
||||
LOGE("\"%s\" returned with value %" PRIexitcode, name, exit_code);
|
||||
} else {
|
||||
LOGE("\"%s\" exited unexpectedly", name);
|
||||
enum sc_process_result
|
||||
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned flags) {
|
||||
return sc_process_execute_p(argv, pid, flags, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_all(sc_pipe pipe, char *data, size_t len) {
|
||||
size_t copied = 0;
|
||||
while (len > 0) {
|
||||
ssize_t r = sc_pipe_read(pipe, data, len);
|
||||
if (r <= 0) {
|
||||
return copied ? (ssize_t) copied : r;
|
||||
}
|
||||
len -= r;
|
||||
data += r;
|
||||
copied += r;
|
||||
}
|
||||
return copied;
|
||||
}
|
||||
|
||||
static int
|
||||
run_observer(void *data) {
|
||||
struct sc_process_observer *observer = data;
|
||||
sc_process_wait(observer->pid, false); // ignore exit code
|
||||
|
||||
sc_mutex_lock(&observer->mutex);
|
||||
observer->terminated = true;
|
||||
sc_cond_signal(&observer->cond_terminated);
|
||||
sc_mutex_unlock(&observer->mutex);
|
||||
|
||||
if (observer->listener) {
|
||||
observer->listener->on_terminated(observer->listener_userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_process_observer_init(struct sc_process_observer *observer, sc_pid pid,
|
||||
const struct sc_process_listener *listener,
|
||||
void *listener_userdata) {
|
||||
// Either no listener, or on_terminated() is defined
|
||||
assert(!listener || listener->on_terminated);
|
||||
|
||||
bool ok = sc_mutex_init(&observer->mutex);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_cond_init(&observer->cond_terminated);
|
||||
if (!ok) {
|
||||
sc_mutex_destroy(&observer->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
observer->pid = pid;
|
||||
observer->listener = listener;
|
||||
observer->listener_userdata = listener_userdata;
|
||||
observer->terminated = false;
|
||||
|
||||
ok = sc_thread_create(&observer->thread, run_observer, "scrcpy-proc",
|
||||
observer);
|
||||
if (!ok) {
|
||||
sc_cond_destroy(&observer->cond_terminated);
|
||||
sc_mutex_destroy(&observer->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_process_observer_timedwait(struct sc_process_observer *observer,
|
||||
sc_tick deadline) {
|
||||
sc_mutex_lock(&observer->mutex);
|
||||
bool timed_out = false;
|
||||
while (!observer->terminated && !timed_out) {
|
||||
timed_out = !sc_cond_timedwait(&observer->cond_terminated,
|
||||
&observer->mutex, deadline);
|
||||
}
|
||||
bool terminated = observer->terminated;
|
||||
sc_mutex_unlock(&observer->mutex);
|
||||
|
||||
return terminated;
|
||||
}
|
||||
|
||||
void
|
||||
sc_process_observer_join(struct sc_process_observer *observer) {
|
||||
sc_thread_join(&observer->thread, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
sc_process_observer_destroy(struct sc_process_observer *observer) {
|
||||
sc_cond_destroy(&observer->cond_terminated);
|
||||
sc_mutex_destroy(&observer->mutex);
|
||||
}
|
||||
|
@ -4,78 +4,177 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "util/thread.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// not needed here, but winsock2.h must never be included AFTER windows.h
|
||||
# include <winsock2.h>
|
||||
# include <windows.h>
|
||||
# define PATH_SEPARATOR '\\'
|
||||
# define PRIexitcode "lu"
|
||||
# define SC_PRIexitcode "lu"
|
||||
// <https://stackoverflow.com/a/44383330/1987178>
|
||||
# define PRIsizet "Iu"
|
||||
# define PROCESS_NONE NULL
|
||||
# define NO_EXIT_CODE -1u // max value as unsigned
|
||||
typedef HANDLE process_t;
|
||||
typedef DWORD exit_code_t;
|
||||
# define SC_PRIsizet "Iu"
|
||||
# define SC_PROCESS_NONE NULL
|
||||
# define SC_EXIT_CODE_NONE -1u // max value as unsigned
|
||||
typedef HANDLE sc_pid;
|
||||
typedef DWORD sc_exit_code;
|
||||
typedef HANDLE sc_pipe;
|
||||
|
||||
#else
|
||||
|
||||
# include <sys/types.h>
|
||||
# define PATH_SEPARATOR '/'
|
||||
# define PRIsizet "zu"
|
||||
# define PRIexitcode "d"
|
||||
# define PROCESS_NONE -1
|
||||
# define NO_EXIT_CODE -1
|
||||
typedef pid_t process_t;
|
||||
typedef int exit_code_t;
|
||||
# define SC_PRIsizet "zu"
|
||||
# define SC_PRIexitcode "d"
|
||||
# define SC_PROCESS_NONE -1
|
||||
# define SC_EXIT_CODE_NONE -1
|
||||
typedef pid_t sc_pid;
|
||||
typedef int sc_exit_code;
|
||||
typedef int sc_pipe;
|
||||
|
||||
#endif
|
||||
|
||||
enum process_result {
|
||||
PROCESS_SUCCESS,
|
||||
PROCESS_ERROR_GENERIC,
|
||||
PROCESS_ERROR_MISSING_BINARY,
|
||||
struct sc_process_listener {
|
||||
void (*on_terminated)(void *userdata);
|
||||
};
|
||||
|
||||
// execute the command and write the result to the output parameter "process"
|
||||
enum process_result
|
||||
process_execute(const char *const argv[], process_t *process);
|
||||
/**
|
||||
* Tool to observe process termination
|
||||
*
|
||||
* To keep things simple and multiplatform, it runs a separate thread to wait
|
||||
* for process termination (without closing the process to avoid race
|
||||
* conditions).
|
||||
*
|
||||
* It allows a caller to block until the process is terminated (with a
|
||||
* timeout), and to be notified asynchronously from the observer thread.
|
||||
*
|
||||
* The process is not owned by the observer (the observer will never close it).
|
||||
*/
|
||||
struct sc_process_observer {
|
||||
sc_pid pid;
|
||||
|
||||
// kill the process
|
||||
sc_mutex mutex;
|
||||
sc_cond cond_terminated;
|
||||
bool terminated;
|
||||
|
||||
sc_thread thread;
|
||||
const struct sc_process_listener *listener;
|
||||
void *listener_userdata;
|
||||
};
|
||||
|
||||
enum sc_process_result {
|
||||
SC_PROCESS_SUCCESS,
|
||||
SC_PROCESS_ERROR_GENERIC,
|
||||
SC_PROCESS_ERROR_MISSING_BINARY,
|
||||
};
|
||||
|
||||
#define SC_PROCESS_NO_STDOUT (1 << 0)
|
||||
#define SC_PROCESS_NO_STDERR (1 << 1)
|
||||
|
||||
/**
|
||||
* Execute the command and write the process id to `pid`
|
||||
*
|
||||
* The `flags` argument is a bitwise OR of the following values:
|
||||
* - SC_PROCESS_NO_STDOUT
|
||||
* - SC_PROCESS_NO_STDERR
|
||||
*
|
||||
* It indicates if stdout and stderr must be inherited from the scrcpy process
|
||||
* (i.e. if the process must output to the scrcpy console).
|
||||
*/
|
||||
enum sc_process_result
|
||||
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned flags);
|
||||
|
||||
/**
|
||||
* Execute the command and write the process id to `pid`
|
||||
*
|
||||
* If not NULL, provide a pipe for stdin (`pin`), stdout (`pout`) and stderr
|
||||
* (`perr`).
|
||||
*
|
||||
* The `flags` argument has the same semantics as in `sc_process_execute()`.
|
||||
*/
|
||||
enum sc_process_result
|
||||
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
||||
sc_pipe *pin, sc_pipe *pout, sc_pipe *perr);
|
||||
|
||||
/**
|
||||
* Kill the process
|
||||
*/
|
||||
bool
|
||||
process_terminate(process_t pid);
|
||||
sc_process_terminate(sc_pid pid);
|
||||
|
||||
// wait and close the process (like waitpid())
|
||||
// the "close" flag indicates if the process must be "closed" (reaped)
|
||||
// (passing false is equivalent to enable WNOWAIT in waitid())
|
||||
exit_code_t
|
||||
process_wait(process_t pid, bool close);
|
||||
/**
|
||||
* Wait and close the process (similar to waitpid())
|
||||
*
|
||||
* The `close` flag indicates if the process must be _closed_ (reaped) (passing
|
||||
* false is equivalent to enable WNOWAIT in waitid()).
|
||||
*/
|
||||
sc_exit_code
|
||||
sc_process_wait(sc_pid pid, bool close);
|
||||
|
||||
// close the process
|
||||
//
|
||||
// Semantically, process_wait(close) = process_wait(noclose) + process_close
|
||||
/**
|
||||
* Close (reap) the process
|
||||
*
|
||||
* Semantically:
|
||||
* sc_process_wait(close) = sc_process_wait(noclose) + sc_process_close()
|
||||
*/
|
||||
void
|
||||
process_close(process_t pid);
|
||||
sc_process_close(sc_pid pid);
|
||||
|
||||
// convenience function to wait for a successful process execution
|
||||
// automatically log process errors with the provided process name
|
||||
/**
|
||||
* Read from the pipe
|
||||
*
|
||||
* Same semantic as read().
|
||||
*/
|
||||
ssize_t
|
||||
sc_pipe_read(sc_pipe pipe, char *data, size_t len);
|
||||
|
||||
/**
|
||||
* Read exactly `len` chars from a pipe (unless EOF)
|
||||
*/
|
||||
ssize_t
|
||||
sc_pipe_read_all(sc_pipe pipe, char *data, size_t len);
|
||||
|
||||
/**
|
||||
* Close the pipe
|
||||
*/
|
||||
void
|
||||
sc_pipe_close(sc_pipe pipe);
|
||||
|
||||
/**
|
||||
* Start observing process
|
||||
*
|
||||
* The listener is optional. If set, its callback will be called from the
|
||||
* observer thread once the process is terminated.
|
||||
*/
|
||||
bool
|
||||
process_check_success(process_t proc, const char *name, bool close);
|
||||
sc_process_observer_init(struct sc_process_observer *observer, sc_pid pid,
|
||||
const struct sc_process_listener *listener,
|
||||
void *listener_userdata);
|
||||
|
||||
#ifndef _WIN32
|
||||
// only used to find package manager, not implemented for Windows
|
||||
/**
|
||||
* Wait for process termination until a deadline
|
||||
*
|
||||
* Return true if the process is already terminated. Return false if the
|
||||
* process terminatation has not been detected yet (however, it may have
|
||||
* terminated in the meantime).
|
||||
*
|
||||
* To wait without timeout/deadline, just use sc_process_wait() instead.
|
||||
*/
|
||||
bool
|
||||
search_executable(const char *file);
|
||||
#endif
|
||||
sc_process_observer_timedwait(struct sc_process_observer *observer,
|
||||
sc_tick deadline);
|
||||
|
||||
// return the absolute path of the executable (the scrcpy binary)
|
||||
// may be NULL on error; to be freed by free()
|
||||
char *
|
||||
get_executable_path(void);
|
||||
/**
|
||||
* Join the observer thread
|
||||
*/
|
||||
void
|
||||
sc_process_observer_join(struct sc_process_observer *observer);
|
||||
|
||||
// returns true if the file exists and is not a directory
|
||||
bool
|
||||
is_regular_file(const char *path);
|
||||
/**
|
||||
* Destroy the observer
|
||||
*
|
||||
* This does not close the associated process.
|
||||
*/
|
||||
void
|
||||
sc_process_observer_destroy(struct sc_process_observer *observer);
|
||||
|
||||
#endif
|
||||
|
29
app/src/util/process_intr.c
Normal file
29
app/src/util/process_intr.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include "process_intr.h"
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||
size_t len) {
|
||||
if (!sc_intr_set_process(intr, pid)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t ret = sc_pipe_read(pipe, data, len);
|
||||
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
||||
char *data, size_t len) {
|
||||
if (!sc_intr_set_process(intr, pid)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t ret = sc_pipe_read_all(pipe, data, len);
|
||||
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
return ret;
|
||||
}
|
17
app/src/util/process_intr.h
Normal file
17
app/src/util/process_intr.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef SC_PROCESS_INTR_H
|
||||
#define SC_PROCESS_INTR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "intr.h"
|
||||
#include "process.h"
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||
size_t len);
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
||||
char *data, size_t len);
|
||||
|
||||
#endif
|
@ -1,5 +1,6 @@
|
||||
#include "str_util.h"
|
||||
#include "str.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
@ -10,8 +11,11 @@
|
||||
# include <tchar.h>
|
||||
#endif
|
||||
|
||||
#include "log.h"
|
||||
#include "strbuf.h"
|
||||
|
||||
size_t
|
||||
xstrncpy(char *dest, const char *src, size_t n) {
|
||||
sc_strncpy(char *dest, const char *src, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n - 1 && src[i] != '\0'; ++i)
|
||||
dest[i] = src[i];
|
||||
@ -21,7 +25,7 @@ xstrncpy(char *dest, const char *src, size_t n) {
|
||||
}
|
||||
|
||||
size_t
|
||||
xstrjoin(char *dst, const char *const tokens[], char sep, size_t n) {
|
||||
sc_str_join(char *dst, const char *const tokens[], char sep, size_t n) {
|
||||
const char *const *remaining = tokens;
|
||||
const char *token = *remaining++;
|
||||
size_t i = 0;
|
||||
@ -31,7 +35,7 @@ xstrjoin(char *dst, const char *const tokens[], char sep, size_t n) {
|
||||
if (i == n)
|
||||
goto truncated;
|
||||
}
|
||||
size_t w = xstrncpy(dst + i, token, n - i);
|
||||
size_t w = sc_strncpy(dst + i, token, n - i);
|
||||
if (w >= n - i)
|
||||
goto truncated;
|
||||
i += w;
|
||||
@ -45,10 +49,11 @@ truncated:
|
||||
}
|
||||
|
||||
char *
|
||||
strquote(const char *src) {
|
||||
sc_str_quote(const char *src) {
|
||||
size_t len = strlen(src);
|
||||
char *quoted = malloc(len + 3);
|
||||
if (!quoted) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
memcpy("ed[1], src, len);
|
||||
@ -59,7 +64,7 @@ strquote(const char *src) {
|
||||
}
|
||||
|
||||
bool
|
||||
parse_integer(const char *s, long *out) {
|
||||
sc_str_parse_integer(const char *s, long *out) {
|
||||
char *endptr;
|
||||
if (*s == '\0') {
|
||||
return false;
|
||||
@ -78,7 +83,8 @@ parse_integer(const char *s, long *out) {
|
||||
}
|
||||
|
||||
size_t
|
||||
parse_integers(const char *s, const char sep, size_t max_items, long *out) {
|
||||
sc_str_parse_integers(const char *s, const char sep, size_t max_items,
|
||||
long *out) {
|
||||
size_t count = 0;
|
||||
char *endptr;
|
||||
do {
|
||||
@ -107,7 +113,7 @@ parse_integers(const char *s, const char sep, size_t max_items, long *out) {
|
||||
}
|
||||
|
||||
bool
|
||||
parse_integer_with_suffix(const char *s, long *out) {
|
||||
sc_str_parse_integer_with_suffix(const char *s, long *out) {
|
||||
char *endptr;
|
||||
if (*s == '\0') {
|
||||
return false;
|
||||
@ -141,7 +147,7 @@ parse_integer_with_suffix(const char *s, long *out) {
|
||||
}
|
||||
|
||||
bool
|
||||
strlist_contains(const char *list, char sep, const char *s) {
|
||||
sc_str_list_contains(const char *list, char sep, const char *s) {
|
||||
char *p;
|
||||
do {
|
||||
p = strchr(list, sep);
|
||||
@ -159,7 +165,7 @@ strlist_contains(const char *list, char sep, const char *s) {
|
||||
}
|
||||
|
||||
size_t
|
||||
utf8_truncation_index(const char *utf8, size_t max_len) {
|
||||
sc_str_utf8_truncation_index(const char *utf8, size_t max_len) {
|
||||
size_t len = strlen(utf8);
|
||||
if (len <= max_len) {
|
||||
return len;
|
||||
@ -177,7 +183,7 @@ utf8_truncation_index(const char *utf8, size_t max_len) {
|
||||
#ifdef _WIN32
|
||||
|
||||
wchar_t *
|
||||
utf8_to_wide_char(const char *utf8) {
|
||||
sc_str_to_wchars(const char *utf8) {
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
|
||||
if (!len) {
|
||||
return NULL;
|
||||
@ -185,6 +191,7 @@ utf8_to_wide_char(const char *utf8) {
|
||||
|
||||
wchar_t *wide = malloc(len * sizeof(wchar_t));
|
||||
if (!wide) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -193,7 +200,7 @@ utf8_to_wide_char(const char *utf8) {
|
||||
}
|
||||
|
||||
char *
|
||||
utf8_from_wide_char(const wchar_t *ws) {
|
||||
sc_str_from_wchars(const wchar_t *ws) {
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
|
||||
if (!len) {
|
||||
return NULL;
|
||||
@ -201,6 +208,7 @@ utf8_from_wide_char(const wchar_t *ws) {
|
||||
|
||||
char *utf8 = malloc(len);
|
||||
if (!utf8) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -209,3 +217,127 @@ utf8_from_wide_char(const wchar_t *ws) {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
char *
|
||||
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent) {
|
||||
assert(indent < columns);
|
||||
|
||||
struct sc_strbuf buf;
|
||||
|
||||
// The output string should not be much longer than the input string (just
|
||||
// a few '\n' added), so this initial capacity should hopefully almost
|
||||
// always avoid internal realloc() in string buffer
|
||||
size_t cap = strlen(input) * 3 / 2;
|
||||
|
||||
if (!sc_strbuf_init(&buf, cap)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#define APPEND(S,N) if (!sc_strbuf_append(&buf, S, N)) goto error
|
||||
#define APPEND_CHAR(C) if (!sc_strbuf_append_char(&buf, C)) goto error
|
||||
#define APPEND_N(C,N) if (!sc_strbuf_append_n(&buf, C, N)) goto error
|
||||
#define APPEND_INDENT() if (indent) APPEND_N(' ', indent)
|
||||
|
||||
APPEND_INDENT();
|
||||
|
||||
// The last separator encountered, it must be inserted only conditionally,
|
||||
// depending on the next token
|
||||
char pending = 0;
|
||||
|
||||
// col tracks the current column in the current line
|
||||
size_t col = indent;
|
||||
while (*input) {
|
||||
size_t sep_idx = strcspn(input, "\n ");
|
||||
size_t new_col = col + sep_idx;
|
||||
if (pending == ' ') {
|
||||
// The pending space counts
|
||||
++new_col;
|
||||
}
|
||||
bool wrap = new_col > columns;
|
||||
|
||||
char sep = input[sep_idx];
|
||||
if (sep == ' ')
|
||||
sep = ' ';
|
||||
|
||||
if (wrap) {
|
||||
APPEND_CHAR('\n');
|
||||
APPEND_INDENT();
|
||||
col = indent;
|
||||
} else if (pending) {
|
||||
APPEND_CHAR(pending);
|
||||
++col;
|
||||
if (pending == '\n')
|
||||
{
|
||||
APPEND_INDENT();
|
||||
col = indent;
|
||||
}
|
||||
}
|
||||
|
||||
if (sep_idx) {
|
||||
APPEND(input, sep_idx);
|
||||
col += sep_idx;
|
||||
}
|
||||
|
||||
pending = sep;
|
||||
|
||||
input += sep_idx;
|
||||
if (*input != '\0') {
|
||||
// Skip the separator
|
||||
++input;
|
||||
}
|
||||
}
|
||||
|
||||
if (pending)
|
||||
APPEND_CHAR(pending);
|
||||
|
||||
return buf.s;
|
||||
|
||||
error:
|
||||
free(buf.s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars) {
|
||||
data[len - 1] = '\0';
|
||||
size_t idx = strcspn(data, endchars);
|
||||
data[idx] = '\0';
|
||||
return idx;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
|
||||
size_t colidx = 0;
|
||||
|
||||
size_t idx = 0;
|
||||
while (s[idx] != '\0' && colidx != col) {
|
||||
size_t r = strcspn(&s[idx], seps);
|
||||
idx += r;
|
||||
|
||||
if (s[idx] == '\0') {
|
||||
// Not found
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t consecutive_seps = strspn(&s[idx], seps);
|
||||
assert(consecutive_seps); // At least one
|
||||
idx += consecutive_seps;
|
||||
|
||||
if (s[idx] != '\0') {
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
|
||||
return col == colidx ? (ssize_t) idx : -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_remove_trailing_cr(char *s, size_t len) {
|
||||
while (len) {
|
||||
if (s[len - 1] != '\r') {
|
||||
break;
|
||||
}
|
||||
s[--len] = '\0';
|
||||
}
|
||||
return len;
|
||||
}
|
148
app/src/util/str.h
Normal file
148
app/src/util/str.h
Normal file
@ -0,0 +1,148 @@
|
||||
#ifndef SC_STR_H
|
||||
#define SC_STR_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* Like strncpy(), except:
|
||||
* - it copies at most n-1 chars
|
||||
* - the dest string is nul-terminated
|
||||
* - it does not write useless bytes if strlen(src) < n
|
||||
* - it returns the number of chars actually written (max n-1) if src has
|
||||
* been copied completely, or n if src has been truncated
|
||||
*/
|
||||
size_t
|
||||
sc_strncpy(char *dest, const char *src, size_t n);
|
||||
|
||||
/**
|
||||
* Join tokens by separator `sep` into `dst`
|
||||
*
|
||||
* Return the number of chars actually written (max n-1) if no truncation
|
||||
* occurred, or n if truncated.
|
||||
*/
|
||||
size_t
|
||||
sc_str_join(char *dst, const char *const tokens[], char sep, size_t n);
|
||||
|
||||
/**
|
||||
* Quote a string
|
||||
*
|
||||
* Return a new allocated string, surrounded with quotes (`"`).
|
||||
*/
|
||||
char *
|
||||
sc_str_quote(const char *src);
|
||||
|
||||
/**
|
||||
* Parse `s` as an integer into `out`
|
||||
*
|
||||
* Return true if the conversion succeeded, false otherwise.
|
||||
*/
|
||||
bool
|
||||
sc_str_parse_integer(const char *s, long *out);
|
||||
|
||||
/**
|
||||
* Parse `s` as integers separated by `sep` (for example `1234:2000`) into `out`
|
||||
*
|
||||
* Returns the number of integers on success, 0 on failure.
|
||||
*/
|
||||
size_t
|
||||
sc_str_parse_integers(const char *s, const char sep, size_t max_items,
|
||||
long *out);
|
||||
|
||||
/**
|
||||
* Parse `s` as an integer into `out`
|
||||
*
|
||||
* Like `sc_str_parse_integer()`, but accept 'k'/'K' (x1000) and 'm'/'M'
|
||||
* (x1000000) as suffixes.
|
||||
*
|
||||
* Return true if the conversion succeeded, false otherwise.
|
||||
*/
|
||||
bool
|
||||
sc_str_parse_integer_with_suffix(const char *s, long *out);
|
||||
|
||||
/**
|
||||
* Search `s` in the list separated by `sep`
|
||||
*
|
||||
* For example, sc_str_list_contains("a,bc,def", ',', "bc") returns true.
|
||||
*/
|
||||
bool
|
||||
sc_str_list_contains(const char *list, char sep, const char *s);
|
||||
|
||||
/**
|
||||
* Return the index to truncate a UTF-8 string at a valid position
|
||||
*/
|
||||
size_t
|
||||
sc_str_utf8_truncation_index(const char *utf8, size_t max_len);
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* Convert a UTF-8 string to a wchar_t string
|
||||
*
|
||||
* Return the new allocated string, to be freed by the caller.
|
||||
*/
|
||||
wchar_t *
|
||||
sc_str_to_wchars(const char *utf8);
|
||||
|
||||
/**
|
||||
* Convert a wchar_t string to a UTF-8 string
|
||||
*
|
||||
* Return the new allocated string, to be freed by the caller.
|
||||
*/
|
||||
char *
|
||||
sc_str_from_wchars(const wchar_t *s);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Wrap input lines to fit in `columns` columns
|
||||
*
|
||||
* Break input lines at word boundaries (spaces) so that they fit in `columns`
|
||||
* columns, left-indented by `indent` spaces.
|
||||
*/
|
||||
char *
|
||||
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
||||
|
||||
/**
|
||||
* Truncate the data after any of the characters from `endchars`
|
||||
*
|
||||
* An '\0' is always written at the end of the data, even if no newline
|
||||
* character is encountered.
|
||||
*
|
||||
* Return the size of the resulting line.
|
||||
*/
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars);
|
||||
|
||||
/**
|
||||
* Find the start of a column in a string
|
||||
*
|
||||
* A string may represent several columns, separated by some "spaces"
|
||||
* (separators). This function aims to find the start of the column number
|
||||
* `col`.
|
||||
*
|
||||
* For example, to find the 4th column (column number 3):
|
||||
*
|
||||
* // here
|
||||
* // v
|
||||
* const char *s = "abc def ghi jk";
|
||||
* ssize_t index = sc_str_index_of_column(s, 3, " ");
|
||||
* assert(index == 16); // points to "jk"
|
||||
*
|
||||
* Return -1 if no such column exists.
|
||||
*/
|
||||
ssize_t
|
||||
sc_str_index_of_column(const char *s, unsigned col, const char *seps);
|
||||
|
||||
/**
|
||||
* Remove all `\r` at the end of the line
|
||||
*
|
||||
* The line length is provided by `len` (this avoids a call to `strlen()` when
|
||||
* the caller already knows the length).
|
||||
*
|
||||
* Return the new length.
|
||||
*/
|
||||
size_t
|
||||
sc_str_remove_trailing_cr(char *s, size_t len);
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
#ifndef STRUTIL_H
|
||||
#define STRUTIL_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// like strncpy, except:
|
||||
// - it copies at most n-1 chars
|
||||
// - the dest string is nul-terminated
|
||||
// - it does not write useless bytes if strlen(src) < n
|
||||
// - it returns the number of chars actually written (max n-1) if src has
|
||||
// been copied completely, or n if src has been truncated
|
||||
size_t
|
||||
xstrncpy(char *dest, const char *src, size_t n);
|
||||
|
||||
// join tokens by sep into dst
|
||||
// returns the number of chars actually written (max n-1) if no truncation
|
||||
// occurred, or n if truncated
|
||||
size_t
|
||||
xstrjoin(char *dst, const char *const tokens[], char sep, size_t n);
|
||||
|
||||
// quote a string
|
||||
// returns the new allocated string, to be freed by the caller
|
||||
char *
|
||||
strquote(const char *src);
|
||||
|
||||
// parse s as an integer into value
|
||||
// returns true if the conversion succeeded, false otherwise
|
||||
bool
|
||||
parse_integer(const char *s, long *out);
|
||||
|
||||
// parse s as integers separated by sep (for example '1234:2000')
|
||||
// returns the number of integers on success, 0 on failure
|
||||
size_t
|
||||
parse_integers(const char *s, const char sep, size_t max_items, long *out);
|
||||
|
||||
// parse s as an integer into value
|
||||
// like parse_integer(), but accept 'k'/'K' (x1000) and 'm'/'M' (x1000000) as
|
||||
// suffix
|
||||
// returns true if the conversion succeeded, false otherwise
|
||||
bool
|
||||
parse_integer_with_suffix(const char *s, long *out);
|
||||
|
||||
// search s in the list separated by sep
|
||||
// for example, strlist_contains("a,bc,def", ',', "bc") returns true
|
||||
bool
|
||||
strlist_contains(const char *list, char sep, const char *s);
|
||||
|
||||
// return the index to truncate a UTF-8 string at a valid position
|
||||
size_t
|
||||
utf8_truncation_index(const char *utf8, size_t max_len);
|
||||
|
||||
#ifdef _WIN32
|
||||
// convert a UTF-8 string to a wchar_t string
|
||||
// returns the new allocated string, to be freed by the caller
|
||||
wchar_t *
|
||||
utf8_to_wide_char(const char *utf8);
|
||||
|
||||
char *
|
||||
utf8_from_wide_char(const wchar_t *s);
|
||||
#endif
|
||||
|
||||
#endif
|
90
app/src/util/strbuf.c
Normal file
90
app/src/util/strbuf.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include "strbuf.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
bool
|
||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
||||
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
||||
if (!buf->s) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
buf->len = 0;
|
||||
buf->cap = init_cap;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
|
||||
if (buf->len + len > buf->cap) {
|
||||
size_t new_cap = buf->cap * 3 / 2 + len;
|
||||
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
||||
if (!s) {
|
||||
// Leave the old buf->s
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
buf->s = s;
|
||||
buf->cap = new_cap;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len) {
|
||||
assert(s);
|
||||
assert(*s);
|
||||
assert(strlen(s) >= len);
|
||||
if (!sc_strbuf_reserve(buf, len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(&buf->s[buf->len], s, len);
|
||||
buf->len += len;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append_char(struct sc_strbuf *buf, const char c) {
|
||||
if (!sc_strbuf_reserve(buf, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buf->s[buf->len] = c;
|
||||
buf->len ++;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n) {
|
||||
if (!sc_strbuf_reserve(buf, n)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(&buf->s[buf->len], c, n);
|
||||
buf->len += n;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_strbuf_shrink(struct sc_strbuf *buf) {
|
||||
assert(buf->len <= buf->cap);
|
||||
if (buf->len != buf->cap) {
|
||||
char *s = realloc(buf->s, buf->len + 1); // +1 for '\0'
|
||||
assert(s); // decreasing the size may not fail
|
||||
buf->s = s;
|
||||
buf->cap = buf->len;
|
||||
}
|
||||
}
|
73
app/src/util/strbuf.h
Normal file
73
app/src/util/strbuf.h
Normal file
@ -0,0 +1,73 @@
|
||||
#ifndef SC_STRBUF_H
|
||||
#define SC_STRBUF_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
struct sc_strbuf {
|
||||
char *s;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the string buffer
|
||||
*
|
||||
* `buf->s` must be manually freed by the caller.
|
||||
*/
|
||||
bool
|
||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap);
|
||||
|
||||
/**
|
||||
* Append a string
|
||||
*
|
||||
* Append `len` characters from `s` to the buffer.
|
||||
*/
|
||||
bool
|
||||
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len);
|
||||
|
||||
/**
|
||||
* Append a char
|
||||
*
|
||||
* Append a single character to the buffer.
|
||||
*/
|
||||
bool
|
||||
sc_strbuf_append_char(struct sc_strbuf *buf, const char c);
|
||||
|
||||
/**
|
||||
* Append a char `n` times
|
||||
*
|
||||
* Append the same characters `n` times to the buffer.
|
||||
*/
|
||||
bool
|
||||
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n);
|
||||
|
||||
/**
|
||||
* Append a NUL-terminated string
|
||||
*/
|
||||
static inline bool
|
||||
sc_strbuf_append_str(struct sc_strbuf *buf, const char *s) {
|
||||
return sc_strbuf_append(buf, s, strlen(s));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a static string
|
||||
*
|
||||
* Append a string whose size is known at compile time (for
|
||||
* example a string literal).
|
||||
*/
|
||||
#define sc_strbuf_append_staticstr(BUF, S) \
|
||||
sc_strbuf_append(BUF, S, sizeof(S) - 1)
|
||||
|
||||
/**
|
||||
* Shrink the buffer capacity to its current length
|
||||
*
|
||||
* This resizes `buf->s` to fit the content.
|
||||
*/
|
||||
void
|
||||
sc_strbuf_shrink(struct sc_strbuf *buf);
|
||||
|
||||
#endif
|
51
app/src/util/term.c
Normal file
51
app/src/util/term.c
Normal file
@ -0,0 +1,51 @@
|
||||
#include "term.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <unistd.h>
|
||||
# include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
bool
|
||||
sc_term_get_size(unsigned *rows, unsigned *cols) {
|
||||
#ifdef _WIN32
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
|
||||
bool ok =
|
||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rows) {
|
||||
assert(csbi.srWindow.Bottom >= csbi.srWindow.Top);
|
||||
*rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
|
||||
}
|
||||
|
||||
if (cols) {
|
||||
assert(csbi.srWindow.Right >= csbi.srWindow.Left);
|
||||
*cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
#else
|
||||
struct winsize ws;
|
||||
int r = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
||||
if (r == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rows) {
|
||||
*rows = ws.ws_row;
|
||||
}
|
||||
|
||||
if (cols) {
|
||||
*cols = ws.ws_col;
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
21
app/src/util/term.h
Normal file
21
app/src/util/term.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef SC_TERM_H
|
||||
#define SC_TERM_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* Return the terminal dimensions
|
||||
*
|
||||
* Return false if the dimensions could not be retrieved.
|
||||
*
|
||||
* Otherwise, return true, and:
|
||||
* - if `rows` is not NULL, then the number of rows is written to `*rows`.
|
||||
* - if `columns` is not NULL, then the number of columns is written to
|
||||
* `*columns`.
|
||||
*/
|
||||
bool
|
||||
sc_term_get_size(unsigned *rows, unsigned *cols);
|
||||
|
||||
#endif
|
@ -8,8 +8,13 @@
|
||||
bool
|
||||
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
||||
void *userdata) {
|
||||
// The thread name length is limited on some systems. Never use a name
|
||||
// longer than 16 bytes (including the final '\0')
|
||||
assert(strlen(name) <= 15);
|
||||
|
||||
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
|
||||
if (!sdl_thread) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -26,6 +31,7 @@ bool
|
||||
sc_mutex_init(sc_mutex *mutex) {
|
||||
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
||||
if (!sdl_mutex) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -94,6 +100,7 @@ bool
|
||||
sc_cond_init(sc_cond *cond) {
|
||||
SDL_cond *sdl_cond = SDL_CreateCond();
|
||||
if (!sdl_cond) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user