Compare commits
111 Commits
process_in
...
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 | |||
57fb08e443 | |||
02ae0db6cd |
10
BUILD.md
10
BUILD.md
@ -15,7 +15,7 @@ First, you need to install the required packages:
|
|||||||
sudo apt install ffmpeg libsdl2-2.0-0 adb wget \
|
sudo apt install ffmpeg libsdl2-2.0-0 adb wget \
|
||||||
gcc git pkg-config meson ninja-build libsdl2-dev \
|
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-dev
|
libusb-1.0-0 libusb-1.0-0-dev
|
||||||
```
|
```
|
||||||
|
|
||||||
Then clone the repo and execute the installation script
|
Then clone the repo and execute the installation script
|
||||||
@ -94,7 +94,7 @@ sudo apt install ffmpeg libsdl2-2.0-0 adb libusb-1.0-0
|
|||||||
# client build dependencies
|
# client build dependencies
|
||||||
sudo apt install gcc git pkg-config meson ninja-build libsdl2-dev \
|
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-dev
|
libusb-1.0-0-dev
|
||||||
|
|
||||||
# server build dependencies
|
# server build dependencies
|
||||||
sudo apt install openjdk-11-jdk
|
sudo apt install openjdk-11-jdk
|
||||||
@ -270,10 +270,10 @@ install` must be run as root)._
|
|||||||
|
|
||||||
#### Option 2: Use prebuilt server
|
#### Option 2: Use prebuilt server
|
||||||
|
|
||||||
- [`scrcpy-server-v1.20`][direct-scrcpy-server]
|
- [`scrcpy-server-v1.21`][direct-scrcpy-server]
|
||||||
_(SHA-256: b20aee4951f99b060c4a44000ba94de973f9604758ef62beb253b371aad3df34)_
|
_(SHA-256: dbcccab523ee26796e55ea33652649e4b7af498edae9aa75e4d4d7869c0ab848)_
|
||||||
|
|
||||||
[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.20/scrcpy-server-v1.20
|
[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
|
Download the prebuilt server somewhere, and specify its path during the Meson
|
||||||
configuration:
|
configuration:
|
||||||
|
104
README.md
104
README.md
@ -1,11 +1,11 @@
|
|||||||
# scrcpy (v1.20)
|
# scrcpy (v1.21)
|
||||||
|
|
||||||
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
<img src="data/icon.svg" width="128" height="128" alt="scrcpy" align="right" />
|
||||||
|
|
||||||
[Read in another language](#translations)
|
[Read in another language](#translations)
|
||||||
|
|
||||||
This application provides display and control of Android devices connected via
|
This application provides display and control of Android devices connected via
|
||||||
USB (or [over TCP/IP](#wireless)). It does not require any _root_ access.
|
USB (or [over TCP/IP](#tcpip-wireless)). It does not require any _root_ access.
|
||||||
It works on _GNU/Linux_, _Windows_ and _macOS_.
|
It works on _GNU/Linux_, _Windows_ and _macOS_.
|
||||||
|
|
||||||

|

|
||||||
@ -65,7 +65,7 @@ Build from sources: [BUILD] ([simplified process][BUILD_simple])
|
|||||||
|
|
||||||
### Linux
|
### Linux
|
||||||
|
|
||||||
On Debian (_testing_ and _sid_ for now) and Ubuntu (20.04):
|
On Debian and Ubuntu:
|
||||||
|
|
||||||
```
|
```
|
||||||
apt install scrcpy
|
apt install scrcpy
|
||||||
@ -101,10 +101,10 @@ process][BUILD_simple]).
|
|||||||
For Windows, for simplicity, a prebuilt archive with all the dependencies
|
For Windows, for simplicity, a prebuilt archive with all the dependencies
|
||||||
(including `adb`) is available:
|
(including `adb`) is available:
|
||||||
|
|
||||||
- [`scrcpy-win64-v1.20.zip`][direct-win64]
|
- [`scrcpy-win64-v1.21.zip`][direct-win64]
|
||||||
_(SHA-256: 548532b616288bcaeceff6881ad5e6f0928e5ae2b48c380385f03627401cfdba)_
|
_(SHA-256: fdab0c1421353b592a9bbcebd6e252675eadccca65cca8105686feaa9c1ded53)_
|
||||||
|
|
||||||
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.20/scrcpy-win64-v1.20.zip
|
[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-win64-v1.21.zip
|
||||||
|
|
||||||
It is also available in [Chocolatey]:
|
It is also available in [Chocolatey]:
|
||||||
|
|
||||||
@ -356,10 +356,39 @@ scrcpy --v4l2-buffer=500 # add 500 ms buffering for v4l2 sink
|
|||||||
|
|
||||||
### Connection
|
### Connection
|
||||||
|
|
||||||
#### Wireless
|
#### TCP/IP (wireless)
|
||||||
|
|
||||||
_Scrcpy_ uses `adb` to communicate with the device, and `adb` can [connect] to a
|
_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.
|
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
|
2. Get your device IP address, in Settings → About phone → Status, or by
|
||||||
@ -412,12 +441,47 @@ autoadb scrcpy -s '{}'
|
|||||||
|
|
||||||
[AutoAdb]: https://github.com/rom1v/autoadb
|
[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
|
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_
|
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
|
||||||
|
adb -a nodaemon server start
|
||||||
|
# keep this open
|
||||||
|
```
|
||||||
|
|
||||||
|
**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:
|
First, make sure the ADB server is running on the remote computer:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -695,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
|
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>).
|
way as <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>).
|
||||||
|
|
||||||
|
To disable automatic clipboard synchronization, use
|
||||||
|
`--no-clipboard-autosync`.
|
||||||
|
|
||||||
#### Pinch-to-zoom
|
#### Pinch-to-zoom
|
||||||
|
|
||||||
To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
|
To simulate "pinch-to-zoom": <kbd>Ctrl</kbd>+_click-and-move_.
|
||||||
@ -737,6 +804,15 @@ 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
|
must be configured on the Android device, in Settings → System → Languages and
|
||||||
input → [Physical keyboard].
|
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
|
[Physical keyboard]: https://github.com/Genymobile/scrcpy/pull/2632#issuecomment-923756915
|
||||||
|
|
||||||
|
|
||||||
@ -758,7 +834,13 @@ scrcpy --prefer-text
|
|||||||
|
|
||||||
(but this will break keyboard behavior in games)
|
(but this will break keyboard behavior in games)
|
||||||
|
|
||||||
This option has no effect on HID keyboard (all key events are sent as
|
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).
|
scancodes in this mode).
|
||||||
|
|
||||||
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
||||||
@ -967,7 +1049,7 @@ This README is available in other languages:
|
|||||||
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
|
- [한국어 (Korean, `ko`) - v1.11](README.ko.md)
|
||||||
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
|
- [Português Brasileiro (Brazilian Portuguese, `pt-BR`) - v1.19](README.pt-br.md)
|
||||||
- [Español (Spanish, `sp`) - v1.17](README.sp.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)
|
- [繁體中文 (Traditional Chinese, `zh-Hant`) - v1.15](README.zh-Hant.md)
|
||||||
- [Turkish (Turkish, `tr`) - v1.18](README.tr.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)会保持最新。
|
只有原版的[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_。
|
本应用程序可以显示并控制通过 USB (或 [TCP/IP][article-tcpip]) 连接的安卓设备,且不需要任何 _root_ 权限。本程序支持 _GNU/Linux_, _Windows_ 和 _macOS_。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
它专注于:
|
本应用专注于:
|
||||||
|
|
||||||
- **轻量** (原生,仅显示设备屏幕)
|
- **轻量**: 原生,仅显示设备屏幕
|
||||||
- **性能** (30~60fps)
|
- **性能**: 30~120fps,取决于设备
|
||||||
- **质量** (分辨率可达 1920×1080 或更高)
|
- **质量**: 分辨率可达 1920×1080 或更高
|
||||||
- **低延迟** ([35~70ms][lowlatency])
|
- **低延迟**: [35~70ms][lowlatency]
|
||||||
- **快速启动** (最快 1 秒内即可显示第一帧)
|
- **快速启动**: 最快 1 秒内即可显示第一帧
|
||||||
- **无侵入性** (不会在设备上遗留任何程序)
|
- **无侵入性**: 不会在设备上遗留任何程序
|
||||||
|
- **用户利益**: 无需帐号,无广告,无需联网
|
||||||
|
- **自由**: 自由和开源软件
|
||||||
|
|
||||||
[lowlatency]: https://github.com/Genymobile/scrcpy/pull/646
|
[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>
|
<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
|
### Linux
|
||||||
|
|
||||||
在 Debian (目前仅支持 _testing_ 和 _sid_ 分支) 和Ubuntu (20.04) 上:
|
在 Debian (目前仅支持 _testing_ 和 _sid_ 分支) 和Ubuntu (20.04) 上:
|
||||||
@ -70,13 +95,12 @@ apt install scrcpy
|
|||||||
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
|
[Ebuild]: https://wiki.gentoo.org/wiki/Ebuild
|
||||||
[ebuild-link]: https://github.com/maggu2810/maggu2810-overlay/tree/master/app-mobilephone/scrcpy
|
[ebuild-link]: https://github.com/maggu2810/maggu2810-overlay/tree/master/app-mobilephone/scrcpy
|
||||||
|
|
||||||
您也可以[自行构建][BUILD] (不必担心,这并不困难)。
|
您也可以[自行构建][BUILD] ([简化过程][BUILD_simple])。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
|
|
||||||
在 Windows 上,简便起见,我们提供包含了所有依赖 (包括 `adb`) 的预编译包。
|
在 Windows 上,为简便起见,我们提供包含了所有依赖 (包括 `adb`) 的预编译包。
|
||||||
|
|
||||||
- [README](README.md#windows)
|
- [README](README.md#windows)
|
||||||
|
|
||||||
@ -114,13 +138,17 @@ brew install scrcpy
|
|||||||
你还需要在 `PATH` 内有 `adb`。如果还没有:
|
你还需要在 `PATH` 内有 `adb`。如果还没有:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Homebrew >= 2.6.0
|
brew install android-platform-tools
|
||||||
brew install --cask android-platform-tools
|
|
||||||
|
|
||||||
# Homebrew < 2.6.0
|
|
||||||
brew cask install android-platform-tools
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
或者通过 [MacPorts],该方法同时设置好 adb:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo port install scrcpy
|
||||||
|
```
|
||||||
|
|
||||||
|
[MacPorts]: https://www.macports.org/
|
||||||
|
|
||||||
您也可以[自行构建][BUILD]。
|
您也可以[自行构建][BUILD]。
|
||||||
|
|
||||||
|
|
||||||
@ -140,7 +168,7 @@ scrcpy --help
|
|||||||
|
|
||||||
## 功能介绍
|
## 功能介绍
|
||||||
|
|
||||||
### 捕获设置
|
### 采集设置
|
||||||
|
|
||||||
#### 降低分辨率
|
#### 降低分辨率
|
||||||
|
|
||||||
@ -158,7 +186,7 @@ scrcpy -m 1024 # 简写
|
|||||||
|
|
||||||
#### 修改码率
|
#### 修改码率
|
||||||
|
|
||||||
默认码率是 8Mbps。要改变视频的码率 (例如改为 2Mbps):
|
默认码率是 8 Mbps。改变视频码率 (例如改为 2 Mbps):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --bit-rate 2M
|
scrcpy --bit-rate 2M
|
||||||
@ -167,7 +195,7 @@ scrcpy -b 2M # 简写
|
|||||||
|
|
||||||
#### 限制帧率
|
#### 限制帧率
|
||||||
|
|
||||||
要限制捕获的帧率:
|
要限制采集的帧率:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --max-fps 15
|
scrcpy --max-fps 15
|
||||||
@ -194,10 +222,11 @@ scrcpy --crop 1224:1440:0:0 # 以 (0,0) 为原点的 1224x1440 像素
|
|||||||
要锁定镜像画面的方向:
|
要锁定镜像画面的方向:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --lock-video-orientation 0 # 自然方向
|
scrcpy --lock-video-orientation # 初始(目前)方向
|
||||||
scrcpy --lock-video-orientation 1 # 逆时针旋转 90°
|
scrcpy --lock-video-orientation=0 # 自然方向
|
||||||
scrcpy --lock-video-orientation 2 # 180°
|
scrcpy --lock-video-orientation=1 # 逆时针旋转 90°
|
||||||
scrcpy --lock-video-orientation 3 # 顺时针旋转 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 _
|
scrcpy --encoder _
|
||||||
```
|
```
|
||||||
|
|
||||||
### 屏幕录制
|
### 采集
|
||||||
|
|
||||||
|
#### 屏幕录制
|
||||||
|
|
||||||
可以在镜像的同时录制视频:
|
可以在镜像的同时录制视频:
|
||||||
|
|
||||||
@ -241,6 +272,75 @@ scrcpy -Nr file.mkv
|
|||||||
[packet delay variation]: https://en.wikipedia.org/wiki/Packet_delay_variation
|
[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。
|
1. 将设备和电脑连接至同一 Wi-Fi。
|
||||||
2. 打开 设置 → 关于手机 → 状态信息,获取设备的 IP 地址,也可以执行以下的命令:
|
2. 打开 设置 → 关于手机 → 状态信息,获取设备的 IP 地址,也可以执行以下的命令:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb shell ip route | awk '{print $9}'
|
adb shell ip route | awk '{print $9}'
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 启用设备的网络 adb 功能 `adb tcpip 5555`。
|
3. 启用设备的网络 adb 功能: `adb tcpip 5555`。
|
||||||
4. 断开设备的 USB 连接。
|
4. 断开设备的 USB 连接。
|
||||||
5. 连接到您的设备:`adb connect DEVICE_IP:5555` _(将 `DEVICE_IP` 替换为设备 IP)_.
|
5. 连接到您的设备:`adb connect DEVICE_IP:5555` _(将 `DEVICE_IP` 替换为设备 IP)_。
|
||||||
6. 正常运行 `scrcpy`。
|
6. 正常运行 `scrcpy`。
|
||||||
|
|
||||||
可能需要降低码率和分辨率:
|
可能降低码率和分辨率会更好一些:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --bit-rate 2M --max-size 800
|
scrcpy --bit-rate 2M --max-size 800
|
||||||
@ -327,7 +428,7 @@ scrcpy --force-adb-forward
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
类似无线网络连接,可能需要降低画面质量:
|
类似地,对于无线连接,可能需要降低画面质量:
|
||||||
|
|
||||||
```
|
```
|
||||||
scrcpy -b2M -m800 --max-fps 15
|
scrcpy -b2M -m800 --max-fps 15
|
||||||
@ -353,7 +454,7 @@ scrcpy --window-x 100 --window-y 100 --window-width 800 --window-height 600
|
|||||||
|
|
||||||
#### 无边框
|
#### 无边框
|
||||||
|
|
||||||
关闭边框:
|
禁用窗口边框:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --window-borderless
|
scrcpy --window-borderless
|
||||||
@ -369,7 +470,7 @@ scrcpy --always-on-top
|
|||||||
|
|
||||||
#### 全屏
|
#### 全屏
|
||||||
|
|
||||||
您可以通过如下命令直接全屏启动scrcpy:
|
您可以通过如下命令直接全屏启动 scrcpy:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --fullscreen
|
scrcpy --fullscreen
|
||||||
@ -394,7 +495,7 @@ scrcpy --rotation 1
|
|||||||
|
|
||||||
也可以使用 <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_ 和 <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_ 随时更改。
|
也可以使用 <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_ 和 <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_ 随时更改。
|
||||||
|
|
||||||
需要注意的是, _scrcpy_ 有三个不同的方向:
|
需要注意的是, _scrcpy_ 中有三类旋转方向:
|
||||||
- <kbd>MOD</kbd>+<kbd>r</kbd> 请求设备在竖屏和横屏之间切换 (如果前台应用程序不支持请求的朝向,可能会拒绝该请求)。
|
- <kbd>MOD</kbd>+<kbd>r</kbd> 请求设备在竖屏和横屏之间切换 (如果前台应用程序不支持请求的朝向,可能会拒绝该请求)。
|
||||||
- [`--lock-video-orientation`](#锁定屏幕方向) 改变镜像的朝向 (设备传输到电脑的画面的朝向)。这会影响录制。
|
- [`--lock-video-orientation`](#锁定屏幕方向) 改变镜像的朝向 (设备传输到电脑的画面的朝向)。这会影响录制。
|
||||||
- `--rotation` (或 <kbd>MOD</kbd>+<kbd>←</kbd>/<kbd>MOD</kbd>+<kbd>→</kbd>) 只旋转窗口的内容。这只影响显示,不影响录制。
|
- `--rotation` (或 <kbd>MOD</kbd>+<kbd>←</kbd>/<kbd>MOD</kbd>+<kbd>→</kbd>) 只旋转窗口的内容。这只影响显示,不影响录制。
|
||||||
@ -404,7 +505,7 @@ scrcpy --rotation 1
|
|||||||
|
|
||||||
#### 只读
|
#### 只读
|
||||||
|
|
||||||
禁用电脑对设备的控制 (如键盘输入、鼠标事件和文件拖放):
|
禁用电脑对设备的控制 (任何可与设备交互的方式:如键盘输入、鼠标事件和文件拖放):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --no-control
|
scrcpy --no-control
|
||||||
@ -430,14 +531,14 @@ adb shell dumpsys display # 在输出中搜索 “mDisplayId=”
|
|||||||
|
|
||||||
#### 保持常亮
|
#### 保持常亮
|
||||||
|
|
||||||
阻止设备在连接时休眠:
|
阻止设备在连接时一段时间后休眠:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --stay-awake
|
scrcpy --stay-awake
|
||||||
scrcpy -w
|
scrcpy -w
|
||||||
```
|
```
|
||||||
|
|
||||||
程序关闭时会恢复设备原来的设置。
|
scrcpy 关闭时会恢复设备原来的设置。
|
||||||
|
|
||||||
|
|
||||||
#### 关闭设备屏幕
|
#### 关闭设备屏幕
|
||||||
@ -451,7 +552,7 @@ scrcpy -S
|
|||||||
|
|
||||||
或者在任何时候按 <kbd>MOD</kbd>+<kbd>o</kbd>。
|
或者在任何时候按 <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>),会 (尽最大的努力) 在短暂的延迟后将屏幕关闭。设备上的 `电源` 按钮仍然能打开设备屏幕。
|
在Android上,`电源` 按钮始终能把屏幕打开。为了方便,对于在 _scrcpy_ 中发出的 `电源` 事件 (通过鼠标右键或 <kbd>MOD</kbd>+<kbd>p</kbd>),会 (尽最大的努力) 在短暂的延迟后将屏幕关闭。设备上的 `电源` 按钮仍然能打开设备屏幕。
|
||||||
|
|
||||||
@ -462,20 +563,17 @@ scrcpy --turn-screen-off --stay-awake
|
|||||||
scrcpy -Sw
|
scrcpy -Sw
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 退出时息屏
|
||||||
|
|
||||||
#### 渲染过期帧
|
scrcpy 退出时关闭设备屏幕:
|
||||||
|
|
||||||
默认状态下,为了降低延迟, _scrcpy_ 永远渲染解码成功的最近一帧,并跳过前面任意帧。
|
|
||||||
|
|
||||||
强制渲染所有帧 (可能导致延迟变高):
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --render-expired-frames
|
scrcpy --power-off-on-close
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 显示触摸
|
#### 显示触摸
|
||||||
|
|
||||||
在演示时,可能会需要显示物理触摸点 (在物理设备上的触摸点)。
|
在演示时,可能会需要显示 (在物理设备上的) 物理触摸点。
|
||||||
|
|
||||||
Android 在 _开发者选项_ 中提供了这项功能。
|
Android 在 _开发者选项_ 中提供了这项功能。
|
||||||
|
|
||||||
@ -538,10 +636,32 @@ scrcpy --disable-screensaver
|
|||||||
|
|
||||||
更准确的说,在按住鼠标左键时按住 <kbd>Ctrl</kbd>。直到松开鼠标左键,所有鼠标移动将以屏幕中心为原点,缩放或旋转内容 (如果应用支持)。
|
更准确的说,在按住鼠标左键时按住 <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]:
|
打字的时候,系统会产生两种[事件][textevents]:
|
||||||
- _按键事件_ ,代表一个按键被按下或松开。
|
- _按键事件_ ,代表一个按键被按下或松开。
|
||||||
@ -557,13 +677,15 @@ scrcpy --prefer-text
|
|||||||
|
|
||||||
(这会导致键盘在游戏中工作不正常)
|
(这会导致键盘在游戏中工作不正常)
|
||||||
|
|
||||||
|
该选项不影响 HID 键盘 (该模式下,所有按键都发送为扫描码)。
|
||||||
|
|
||||||
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
[textevents]: https://blog.rom1v.com/2018/03/introducing-scrcpy/#handle-text-input
|
||||||
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
[prefertext]: https://github.com/Genymobile/scrcpy/issues/650#issuecomment-512945343
|
||||||
|
|
||||||
|
|
||||||
#### 按键重复
|
#### 按键重复
|
||||||
|
|
||||||
默认状态下,按住一个按键不放会生成多个重复按键事件。在某些游戏中这可能会导致性能问题。
|
默认状态下,按住一个按键不放会生成多个重复按键事件。在某些游戏中这通常没有实际用途,且可能会导致性能问题。
|
||||||
|
|
||||||
避免转发重复按键事件:
|
避免转发重复按键事件:
|
||||||
|
|
||||||
@ -571,10 +693,11 @@ scrcpy --prefer-text
|
|||||||
scrcpy --no-key-repeat
|
scrcpy --no-key-repeat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
该选项不影响 HID 键盘 (该模式下,按键重复由 Android 直接管理)。
|
||||||
|
|
||||||
#### 右键和中键
|
#### 右键和中键
|
||||||
|
|
||||||
默认状态下,右键会触发返回键 (或电源键),中键会触发 HOME 键。要禁用这些快捷键并把所有点击转发到设备:
|
默认状态下,右键会触发返回键 (或电源键开启),中键会触发 HOME 键。要禁用这些快捷键并把所有点击转发到设备:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --forward-all-clicks
|
scrcpy --forward-all-clicks
|
||||||
@ -587,27 +710,27 @@ scrcpy --forward-all-clicks
|
|||||||
|
|
||||||
将 APK 文件 (文件名以 `.apk` 结尾) 拖放到 _scrcpy_ 窗口来安装。
|
将 APK 文件 (文件名以 `.apk` 结尾) 拖放到 _scrcpy_ 窗口来安装。
|
||||||
|
|
||||||
该操作在屏幕上不会出现任何变化,而会在控制台输出一条日志。
|
不会有视觉反馈,终端会输出一条日志。
|
||||||
|
|
||||||
|
|
||||||
#### 将文件推送至设备
|
#### 将文件推送至设备
|
||||||
|
|
||||||
要推送文件到设备的 `/sdcard/`,将 (非 APK) 文件拖放至 _scrcpy_ 窗口。
|
要推送文件到设备的 `/sdcard/Download/`,将 (非 APK) 文件拖放至 _scrcpy_ 窗口。
|
||||||
|
|
||||||
该操作没有可见的响应,只会在控制台输出日志。
|
不会有视觉反馈,终端会输出一条日志。
|
||||||
|
|
||||||
在启动时可以修改目标目录:
|
在启动时可以修改目标目录:
|
||||||
|
|
||||||
```bash
|
```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
|
[sndcpy]: https://github.com/rom1v/sndcpy
|
||||||
[issue #14]: https://github.com/Genymobile/scrcpy/issues/14
|
[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)
|
[Super]: https://en.wikipedia.org/wiki/Super_key_(keyboard_button)
|
||||||
|
|
||||||
| 操作 | 快捷键 |
|
| 操作 | 快捷键
|
||||||
| --------------------------------- | :------------------------------------------- |
|
| --------------------------------- | :-------------------------------------------
|
||||||
| 全屏 | <kbd>MOD</kbd>+<kbd>f</kbd> |
|
| 全屏 | <kbd>MOD</kbd>+<kbd>f</kbd>
|
||||||
| 向左旋转屏幕 | <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_ |
|
| 向左旋转屏幕 | <kbd>MOD</kbd>+<kbd>←</kbd> _(左箭头)_
|
||||||
| 向右旋转屏幕 | <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_ |
|
| 向右旋转屏幕 | <kbd>MOD</kbd>+<kbd>→</kbd> _(右箭头)_
|
||||||
| 将窗口大小重置为1:1 (匹配像素) | <kbd>MOD</kbd>+<kbd>g</kbd> |
|
| 将窗口大小重置为1:1 (匹配像素) | <kbd>MOD</kbd>+<kbd>g</kbd>
|
||||||
| 将窗口大小重置为消除黑边 | <kbd>MOD</kbd>+<kbd>w</kbd> \| _双击¹_ |
|
| 将窗口大小重置为消除黑边 | <kbd>MOD</kbd>+<kbd>w</kbd> \| _双击左键¹_
|
||||||
| 点按 `主屏幕` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _鼠标中键_ |
|
| 点按 `主屏幕` | <kbd>MOD</kbd>+<kbd>h</kbd> \| _中键_
|
||||||
| 点按 `返回` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _鼠标右键²_ |
|
| 点按 `返回` | <kbd>MOD</kbd>+<kbd>b</kbd> \| _右键²_
|
||||||
| 点按 `切换应用` | <kbd>MOD</kbd>+<kbd>s</kbd> |
|
| 点按 `切换应用` | <kbd>MOD</kbd>+<kbd>s</kbd> \| _第4键³_
|
||||||
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd> |
|
| 点按 `菜单` (解锁屏幕) | <kbd>MOD</kbd>+<kbd>m</kbd>
|
||||||
| 点按 `音量+` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上箭头)_ |
|
| 点按 `音量+` | <kbd>MOD</kbd>+<kbd>↑</kbd> _(上箭头)_
|
||||||
| 点按 `音量-` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下箭头)_ |
|
| 点按 `音量-` | <kbd>MOD</kbd>+<kbd>↓</kbd> _(下箭头)_
|
||||||
| 点按 `电源` | <kbd>MOD</kbd>+<kbd>p</kbd> |
|
| 点按 `电源` | <kbd>MOD</kbd>+<kbd>p</kbd>
|
||||||
| 打开屏幕 | _鼠标右键²_ |
|
| 打开屏幕 | _鼠标右键²_
|
||||||
| 关闭设备屏幕 (但继续在电脑上显示) | <kbd>MOD</kbd>+<kbd>o</kbd> |
|
| 关闭设备屏幕 (但继续在电脑上显示) | <kbd>MOD</kbd>+<kbd>o</kbd>
|
||||||
| 打开设备屏幕 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd> |
|
| 打开设备屏幕 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd>
|
||||||
| 旋转设备屏幕 | <kbd>MOD</kbd>+<kbd>r</kbd> |
|
| 旋转设备屏幕 | <kbd>MOD</kbd>+<kbd>r</kbd>
|
||||||
| 展开通知面板 | <kbd>MOD</kbd>+<kbd>n</kbd> |
|
| 展开通知面板 | <kbd>MOD</kbd>+<kbd>n</kbd> \| _第5键³_
|
||||||
| 收起通知面板 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd> |
|
| 展开设置面板 | <kbd>MOD</kbd>+<kbd>n</kbd>+<kbd>n</kbd> \| _双击第5键³_
|
||||||
| 复制到剪贴板³ | <kbd>MOD</kbd>+<kbd>c</kbd> |
|
| 收起通知面板 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>n</kbd>
|
||||||
| 剪切到剪贴板³ | <kbd>MOD</kbd>+<kbd>x</kbd> |
|
| 复制到剪贴板⁴ | <kbd>MOD</kbd>+<kbd>c</kbd>
|
||||||
| 同步剪贴板并粘贴³ | <kbd>MOD</kbd>+<kbd>v</kbd> |
|
| 剪切到剪贴板⁴ | <kbd>MOD</kbd>+<kbd>x</kbd>
|
||||||
| 注入电脑剪贴板文本 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd> |
|
| 同步剪贴板并粘贴⁴ | <kbd>MOD</kbd>+<kbd>v</kbd>
|
||||||
| 打开/关闭FPS显示 (在 stdout) | <kbd>MOD</kbd>+<kbd>i</kbd> |
|
| 注入电脑剪贴板文本 | <kbd>MOD</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>
|
||||||
| 捏拉缩放 | <kbd>Ctrl</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>+_按键_ 的快捷键都会被转发到设备,所以会由当前应用程序进行处理。
|
所有的 <kbd>Ctrl</kbd>+_按键_ 的快捷键都会被转发到设备,所以会由当前应用程序进行处理。
|
||||||
|
|
||||||
@ -670,18 +803,20 @@ _³需要安卓版本 Android >= 7。_
|
|||||||
|
|
||||||
要使用指定的 _adb_ 二进制文件,可以设置环境变量 `ADB`:
|
要使用指定的 _adb_ 二进制文件,可以设置环境变量 `ADB`:
|
||||||
|
|
||||||
ADB=/path/to/adb scrcpy
|
```bash
|
||||||
|
ADB=/path/to/adb scrcpy
|
||||||
|
```
|
||||||
|
|
||||||
要覆盖 `scrcpy-server` 的路径,可以设置 `SCRCPY_SERVER_PATH`。
|
要覆盖 `scrcpy-server` 的路径,可以设置 `SCRCPY_SERVER_PATH`。
|
||||||
|
|
||||||
[useful]: https://github.com/Genymobile/scrcpy/issues/278#issuecomment-429330345
|
要覆盖图标,可以设置其路径至 `SCRCPY_ICON_PATH`。
|
||||||
|
|
||||||
|
|
||||||
## 为什么叫 _scrcpy_ ?
|
## 为什么叫 _scrcpy_ ?
|
||||||
|
|
||||||
一个同事让我找出一个和 [gnirehtet] 一样难以发音的名字。
|
一个同事让我找出一个和 [gnirehtet] 一样难以发音的名字。
|
||||||
|
|
||||||
[`strcpy`] 复制一个 **str**ing; `scrcpy` 复制一个 **scr**een。
|
[`strcpy`] 复制一个 **str**ing (字符串); `scrcpy` 复制一个 **scr**een (屏幕)。
|
||||||
|
|
||||||
[gnirehtet]: https://github.com/Genymobile/gnirehtet
|
[gnirehtet]: https://github.com/Genymobile/gnirehtet
|
||||||
[`strcpy`]: http://man7.org/linux/man-pages/man3/strcpy.3.html
|
[`strcpy`]: http://man7.org/linux/man-pages/man3/strcpy.3.html
|
||||||
@ -689,14 +824,12 @@ _³需要安卓版本 Android >= 7。_
|
|||||||
|
|
||||||
## 如何构建?
|
## 如何构建?
|
||||||
|
|
||||||
请查看[BUILD]。
|
请查看 [BUILD]。
|
||||||
|
|
||||||
[BUILD]: BUILD.md
|
|
||||||
|
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
请查看[FAQ](FAQ.md)。
|
请查看 [FAQ](FAQ.md)。
|
||||||
|
|
||||||
|
|
||||||
## 开发者
|
## 开发者
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
src = [
|
src = [
|
||||||
'src/main.c',
|
'src/main.c',
|
||||||
'src/adb.c',
|
'src/adb.c',
|
||||||
|
'src/adb_parser.c',
|
||||||
'src/adb_tunnel.c',
|
'src/adb_tunnel.c',
|
||||||
'src/cli.c',
|
'src/cli.c',
|
||||||
'src/clock.c',
|
'src/clock.c',
|
||||||
@ -13,6 +14,7 @@ src = [
|
|||||||
'src/file_handler.c',
|
'src/file_handler.c',
|
||||||
'src/fps_counter.c',
|
'src/fps_counter.c',
|
||||||
'src/frame_buffer.c',
|
'src/frame_buffer.c',
|
||||||
|
'src/input_events.c',
|
||||||
'src/input_manager.c',
|
'src/input_manager.c',
|
||||||
'src/keyboard_inject.c',
|
'src/keyboard_inject.c',
|
||||||
'src/mouse_inject.c',
|
'src/mouse_inject.c',
|
||||||
@ -25,7 +27,9 @@ src = [
|
|||||||
'src/server.c',
|
'src/server.c',
|
||||||
'src/stream.c',
|
'src/stream.c',
|
||||||
'src/video_buffer.c',
|
'src/video_buffer.c',
|
||||||
|
'src/util/acksync.c',
|
||||||
'src/util/file.c',
|
'src/util/file.c',
|
||||||
|
'src/util/intmap.c',
|
||||||
'src/util/intr.c',
|
'src/util/intr.c',
|
||||||
'src/util/log.c',
|
'src/util/log.c',
|
||||||
'src/util/net.c',
|
'src/util/net.c',
|
||||||
@ -39,16 +43,29 @@ src = [
|
|||||||
'src/util/tick.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'
|
if host_machine.system() == 'windows'
|
||||||
|
windows = import('windows')
|
||||||
src += [
|
src += [
|
||||||
'src/sys/win/file.c',
|
'src/sys/win/file.c',
|
||||||
'src/sys/win/process.c',
|
'src/sys/win/process.c',
|
||||||
|
windows.compile_resources('scrcpy-windows.rc'),
|
||||||
]
|
]
|
||||||
|
conf.set('_WIN32_WINNT', '0x0600')
|
||||||
|
conf.set('WINVER', '0x0600')
|
||||||
else
|
else
|
||||||
src += [
|
src += [
|
||||||
'src/sys/unix/file.c',
|
'src/sys/unix/file.c',
|
||||||
'src/sys/unix/process.c',
|
'src/sys/unix/process.c',
|
||||||
]
|
]
|
||||||
|
if host_machine.system() == 'darwin'
|
||||||
|
conf.set('_DARWIN_C_SOURCE', true)
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
v4l2_support = host_machine.system() == 'linux'
|
v4l2_support = host_machine.system() == 'linux'
|
||||||
@ -64,20 +81,18 @@ if aoa_hid_support
|
|||||||
]
|
]
|
||||||
endif
|
endif
|
||||||
|
|
||||||
check_functions = [
|
|
||||||
'strdup'
|
|
||||||
]
|
|
||||||
|
|
||||||
cc = meson.get_compiler('c')
|
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
|
# native build
|
||||||
dependencies = [
|
dependencies = [
|
||||||
dependency('libavformat'),
|
dependency('libavformat', version: '>= 57.33'),
|
||||||
dependency('libavcodec'),
|
dependency('libavcodec', version: '>= 57.37'),
|
||||||
dependency('libavutil'),
|
dependency('libavutil'),
|
||||||
dependency('sdl2'),
|
dependency('sdl2', version: '>= 2.0.5'),
|
||||||
]
|
]
|
||||||
|
|
||||||
if v4l2_support
|
if v4l2_support
|
||||||
@ -128,7 +143,11 @@ if host_machine.system() == 'windows'
|
|||||||
dependencies += cc.find_library('ws2_32')
|
dependencies += cc.find_library('ws2_32')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
conf = configuration_data()
|
check_functions = [
|
||||||
|
'strdup',
|
||||||
|
'asprintf',
|
||||||
|
'vasprintf',
|
||||||
|
]
|
||||||
|
|
||||||
foreach f : check_functions
|
foreach f : check_functions
|
||||||
if cc.has_function(f)
|
if cc.has_function(f)
|
||||||
@ -137,6 +156,9 @@ foreach f : check_functions
|
|||||||
endif
|
endif
|
||||||
endforeach
|
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
|
# the version, updated on release
|
||||||
conf.set_quoted('SCRCPY_VERSION', meson.project_version())
|
conf.set_quoted('SCRCPY_VERSION', meson.project_version())
|
||||||
|
|
||||||
@ -189,8 +211,14 @@ install_data('../data/icon.png',
|
|||||||
# do not build tests in release (assertions would not be executed at all)
|
# do not build tests in release (assertions would not be executed at all)
|
||||||
if get_option('buildtype') == 'debug'
|
if get_option('buildtype') == 'debug'
|
||||||
tests = [
|
tests = [
|
||||||
|
['test_adb_parser', [
|
||||||
|
'tests/test_adb_parser.c',
|
||||||
|
'src/adb_parser.c',
|
||||||
|
'src/util/str.c',
|
||||||
|
'src/util/strbuf.c',
|
||||||
|
]],
|
||||||
['test_buffer_util', [
|
['test_buffer_util', [
|
||||||
'tests/test_buffer_util.c'
|
'tests/test_buffer_util.c',
|
||||||
]],
|
]],
|
||||||
['test_cbuf', [
|
['test_cbuf', [
|
||||||
'tests/test_cbuf.c',
|
'tests/test_cbuf.c',
|
||||||
@ -199,6 +227,7 @@ if get_option('buildtype') == 'debug'
|
|||||||
'tests/test_cli.c',
|
'tests/test_cli.c',
|
||||||
'src/cli.c',
|
'src/cli.c',
|
||||||
'src/options.c',
|
'src/options.c',
|
||||||
|
'src/util/net.c',
|
||||||
'src/util/str.c',
|
'src/util/str.c',
|
||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
'src/util/term.c',
|
'src/util/term.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
|
46
app/scrcpy.1
46
app/scrcpy.1
@ -90,6 +90,12 @@ This provides a better experience for IME users, and allows to generate non-ASCI
|
|||||||
|
|
||||||
It may only work over USB, and is currently only supported on Linux.
|
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
|
.TP
|
||||||
.B \-\-legacy\-paste
|
.B \-\-legacy\-paste
|
||||||
Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+Shift+v).
|
Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+Shift+v).
|
||||||
@ -114,6 +120,12 @@ Limit both the width and height of the video to \fIvalue\fR. The other dimension
|
|||||||
|
|
||||||
Default is 0 (unlimited).
|
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
|
.TP
|
||||||
.B \-n, \-\-no\-control
|
.B \-n, \-\-no\-control
|
||||||
Disable device control (mirror the device in read\-only).
|
Disable device control (mirror the device in read\-only).
|
||||||
@ -153,6 +165,10 @@ Set the target directory for pushing files to the device by drag & drop. It is p
|
|||||||
|
|
||||||
Default is "/sdcard/Download/".
|
Default is "/sdcard/Download/".
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-\-raw\-key\-events
|
||||||
|
Inject key events for all input keys, and ignore text events.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-r, \-\-record " file
|
.BI "\-r, \-\-record " file
|
||||||
Record screen to
|
Record screen to
|
||||||
@ -193,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).
|
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
|
.TP
|
||||||
.B \-S, \-\-turn\-screen\-off
|
.B \-S, \-\-turn\-screen\-off
|
||||||
Turn the device screen off immediately.
|
Turn the device screen off immediately.
|
||||||
@ -203,6 +227,18 @@ Enable "show touches" on start, restore the initial value on exit.
|
|||||||
|
|
||||||
It only shows physical touches (not clicks from scrcpy).
|
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
|
.TP
|
||||||
.BI "\-\-v4l2-sink " /dev/videoN
|
.BI "\-\-v4l2-sink " /dev/videoN
|
||||||
Output to v4l2loopback device.
|
Output to v4l2loopback device.
|
||||||
@ -377,11 +413,15 @@ Push file to device (see \fB\-\-push\-target\fR)
|
|||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B ADB
|
.B ADB
|
||||||
Specify the path to adb.
|
Path to adb.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B SCRCPY_ICON_PATH
|
||||||
|
Path to the program icon.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B SCRCPY_SERVER_PATH
|
.B SCRCPY_SERVER_PATH
|
||||||
Specify the path to server binary.
|
Path to the server binary.
|
||||||
|
|
||||||
|
|
||||||
.SH AUTHORS
|
.SH AUTHORS
|
||||||
@ -406,7 +446,7 @@ Copyright \(co 2018 Genymobile
|
|||||||
Genymobile
|
Genymobile
|
||||||
.UE
|
.UE
|
||||||
|
|
||||||
Copyright \(co 2018\-2020
|
Copyright \(co 2018\-2021
|
||||||
.MT rom@rom1v.com
|
.MT rom@rom1v.com
|
||||||
Romain Vimont
|
Romain Vimont
|
||||||
.ME
|
.ME
|
||||||
|
313
app/src/adb.c
313
app/src/adb.c
@ -5,6 +5,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "adb_parser.h"
|
||||||
#include "util/file.h"
|
#include "util/file.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/process_intr.h"
|
#include "util/process_intr.h"
|
||||||
@ -86,7 +87,8 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
|||||||
#define MAX_COMMAND_STRING_LEN 1024
|
#define MAX_COMMAND_STRING_LEN 1024
|
||||||
char *buf = malloc(MAX_COMMAND_STRING_LEN);
|
char *buf = malloc(MAX_COMMAND_STRING_LEN);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
LOGE("Failed to execute (could not allocate error message)");
|
LOG_OOM();
|
||||||
|
LOGE("Failed to execute");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,18 +112,61 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
|||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
static bool
|
||||||
adb_execute_p(const char *serial, const char *const adb_cmd[],
|
process_check_success_internal(sc_pid pid, const char *name, bool close,
|
||||||
size_t len, sc_pipe *pout) {
|
unsigned flags) {
|
||||||
int i;
|
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
|
||||||
sc_pid pid;
|
|
||||||
|
|
||||||
|
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));
|
const char **argv = malloc((len + 4) * sizeof(*argv));
|
||||||
if (!argv) {
|
if (!argv) {
|
||||||
return SC_PROCESS_NONE;
|
LOG_OOM();
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
argv[0] = get_adb_command();
|
argv[0] = get_adb_command();
|
||||||
|
int i;
|
||||||
if (serial) {
|
if (serial) {
|
||||||
argv[1] = "-s";
|
argv[1] = "-s";
|
||||||
argv[2] = serial;
|
argv[2] = serial;
|
||||||
@ -132,9 +177,31 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
|
|||||||
|
|
||||||
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
||||||
argv[len + i] = NULL;
|
argv[len + i] = NULL;
|
||||||
|
return argv;
|
||||||
|
}
|
||||||
|
|
||||||
|
static sc_pid
|
||||||
|
adb_execute_p(const char *serial, const char *const adb_cmd[], size_t len,
|
||||||
|
unsigned flags, sc_pipe *pout) {
|
||||||
|
const char **argv = adb_create_argv(serial, adb_cmd, len);
|
||||||
|
if (!argv) {
|
||||||
|
return SC_PROCESS_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned process_flags = 0;
|
||||||
|
if (flags & SC_ADB_NO_STDOUT) {
|
||||||
|
process_flags |= SC_PROCESS_NO_STDOUT;
|
||||||
|
}
|
||||||
|
if (flags & SC_ADB_NO_STDERR) {
|
||||||
|
process_flags |= SC_PROCESS_NO_STDERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc_pid pid;
|
||||||
enum sc_process_result r =
|
enum sc_process_result r =
|
||||||
sc_process_execute_p(argv, &pid, NULL, pout, NULL);
|
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
|
||||||
if (r != SC_PROCESS_SUCCESS) {
|
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);
|
show_adb_err_msg(r, argv);
|
||||||
pid = SC_PROCESS_NONE;
|
pid = SC_PROCESS_NONE;
|
||||||
}
|
}
|
||||||
@ -144,50 +211,63 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
|
|||||||
}
|
}
|
||||||
|
|
||||||
sc_pid
|
sc_pid
|
||||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
|
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
||||||
return adb_execute_p(serial, adb_cmd, len, NULL);
|
unsigned flags) {
|
||||||
|
return adb_execute_p(serial, adb_cmd, len, flags, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_forward(const char *serial, uint16_t local_port,
|
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||||
const char *device_socket_name) {
|
const char *device_socket_name, unsigned flags) {
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
const char *const adb_cmd[] = {"forward", local, remote};
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_forward_remove(const char *serial, uint16_t local_port) {
|
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
uint16_t local_port, unsigned flags) {
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
const char *const adb_cmd[] = {"forward", "--remove", local};
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_reverse(const char *serial, const char *device_socket_name,
|
adb_reverse(struct sc_intr *intr, const char *serial,
|
||||||
uint16_t local_port) {
|
const char *device_socket_name, uint16_t local_port,
|
||||||
|
unsigned flags) {
|
||||||
char local[4 + 5 + 1]; // tcp:PORT
|
char local[4 + 5 + 1]; // tcp:PORT
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
const char *const adb_cmd[] = {"reverse", remote, local};
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_reverse_remove(const char *serial, const char *device_socket_name) {
|
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||||
|
const char *device_socket_name, unsigned flags) {
|
||||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||||
const char *const adb_cmd[] = {"reverse", "--remove", remote};
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_push(const char *serial, const char *local, const char *remote) {
|
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
const char *remote, unsigned flags) {
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
// Windows will parse the string, so the paths must be quoted
|
// Windows will parse the string, so the paths must be quoted
|
||||||
// (see sys/win/command.c)
|
// (see sys/win/command.c)
|
||||||
@ -203,18 +283,19 @@ adb_exec_push(const char *serial, const char *local, const char *remote) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char *const adb_cmd[] = {"push", local, remote};
|
const char *const adb_cmd[] = {"push", local, remote};
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
free((void *) remote);
|
free((void *) remote);
|
||||||
free((void *) local);
|
free((void *) local);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return pid;
|
return process_check_success_intr(intr, pid, "adb push", flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_pid
|
bool
|
||||||
adb_exec_install(const char *serial, const char *local) {
|
adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
|
unsigned flags) {
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
// Windows will parse the string, so the local name must be quoted
|
// Windows will parse the string, so the local name must be quoted
|
||||||
// (see sys/win/command.c)
|
// (see sys/win/command.c)
|
||||||
@ -225,68 +306,110 @@ adb_exec_install(const char *serial, const char *local) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char *const adb_cmd[] = {"install", "-r", local};
|
const char *const adb_cmd[] = {"install", "-r", local};
|
||||||
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
free((void *) local);
|
free((void *) local);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return pid;
|
return process_check_success_intr(intr, pid, "adb install", flags);
|
||||||
}
|
|
||||||
|
|
||||||
static sc_pid
|
|
||||||
adb_exec_get_serialno(sc_pipe *pout) {
|
|
||||||
const char *const adb_cmd[] = {"get-serialno"};
|
|
||||||
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
||||||
const char *device_socket_name) {
|
unsigned flags) {
|
||||||
sc_pid pid = adb_exec_forward(serial, local_port, device_socket_name);
|
char port_string[5 + 1];
|
||||||
return sc_process_check_success_intr(intr, pid, "adb forward", true);
|
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
|
bool
|
||||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
uint16_t local_port) {
|
const char *const adb_cmd[] = {"connect", ip_port};
|
||||||
sc_pid pid = adb_exec_forward_remove(serial, local_port);
|
|
||||||
return sc_process_check_success_intr(intr, pid, "adb forward --remove",
|
sc_pipe pout;
|
||||||
true);
|
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
|
bool
|
||||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
const char *device_socket_name, uint16_t local_port) {
|
const char *const adb_cmd[] = {"disconnect", ip_port};
|
||||||
sc_pid pid = adb_exec_reverse(serial, device_socket_name, local_port);
|
size_t len = ip_port ? ARRAY_LEN(adb_cmd)
|
||||||
return sc_process_check_success_intr(intr, pid, "adb reverse", true);
|
: ARRAY_LEN(adb_cmd) - 1;
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
sc_pid pid = adb_execute(NULL, adb_cmd, len, flags);
|
||||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
return process_check_success_intr(intr, pid, "adb disconnect", flags);
|
||||||
const char *device_socket_name) {
|
|
||||||
sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name);
|
|
||||||
return sc_process_check_success_intr(intr, pid, "adb reverse --remove",
|
|
||||||
true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
|
||||||
const char *remote) {
|
|
||||||
sc_pid pid = adb_exec_push(serial, local, remote);
|
|
||||||
return sc_process_check_success_intr(intr, pid, "adb push", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
adb_install(struct sc_intr *intr, const char *serial, const char *local) {
|
|
||||||
sc_pid pid = adb_exec_install(serial, local);
|
|
||||||
return sc_process_check_success_intr(intr, pid, "adb install", true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
adb_get_serialno(struct sc_intr *intr) {
|
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_pipe pout;
|
||||||
sc_pid pid = adb_exec_get_serialno(&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) {
|
if (pid == SC_PROCESS_NONE) {
|
||||||
LOGE("Could not execute \"adb get-serialno\"");
|
LOGE("Could not execute \"adb get-serialno\"");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -296,13 +419,53 @@ adb_get_serialno(struct sc_intr *intr) {
|
|||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||||
sc_pipe_close(pout);
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
bool ok =
|
bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
|
||||||
sc_process_check_success_intr(intr, pid, "adb get-serialno", true);
|
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
sc_str_truncate(buf, r, " \r\n");
|
sc_str_truncate(buf, r, " \r\n");
|
||||||
|
|
||||||
return strdup(buf);
|
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);
|
||||||
|
}
|
||||||
|
@ -8,31 +8,71 @@
|
|||||||
|
|
||||||
#include "util/intr.h"
|
#include "util/intr.h"
|
||||||
|
|
||||||
|
#define SC_ADB_NO_STDOUT (1 << 0)
|
||||||
|
#define SC_ADB_NO_STDERR (1 << 1)
|
||||||
|
#define SC_ADB_NO_LOGERR (1 << 2)
|
||||||
|
|
||||||
|
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
|
||||||
|
|
||||||
sc_pid
|
sc_pid
|
||||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
|
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||||
const char *device_socket_name);
|
const char *device_socket_name, unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||||
uint16_t local_port);
|
uint16_t local_port, unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
adb_reverse(struct sc_intr *intr, const char *serial,
|
||||||
const char *device_socket_name, uint16_t local_port);
|
const char *device_socket_name, uint16_t local_port,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||||
const char *device_socket_name);
|
const char *device_socket_name, unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||||
const char *remote);
|
const char *remote, unsigned flags);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adb_install(struct sc_intr *intr, const char *serial, const char *local);
|
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`
|
* Execute `adb get-serialno`
|
||||||
@ -40,6 +80,15 @@ adb_install(struct sc_intr *intr, const char *serial, const char *local);
|
|||||||
* Return the result, to be freed by the caller, or NULL on error.
|
* Return the result, to be freed by the caller, or NULL on error.
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
adb_get_serialno(struct sc_intr *intr);
|
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
|
#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
|
@ -20,7 +20,8 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
struct sc_port_range port_range) {
|
struct sc_port_range port_range) {
|
||||||
uint16_t port = port_range.first;
|
uint16_t port = port_range.first;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port)) {
|
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port,
|
||||||
|
SC_ADB_NO_STDOUT)) {
|
||||||
// the command itself failed, it will fail on any port
|
// the command itself failed, it will fail on any port
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -51,7 +52,8 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// failure, disable tunnel and try another port
|
// failure, disable tunnel and try another port
|
||||||
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME)) {
|
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||||
|
SC_ADB_NO_STDOUT)) {
|
||||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
|
|||||||
|
|
||||||
uint16_t port = port_range.first;
|
uint16_t port = port_range.first;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (adb_forward(intr, serial, port, SC_SOCKET_NAME)) {
|
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_ADB_NO_STDOUT)) {
|
||||||
// success
|
// success
|
||||||
tunnel->local_port = port;
|
tunnel->local_port = port;
|
||||||
tunnel->enabled = true;
|
tunnel->enabled = true;
|
||||||
@ -146,9 +148,11 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
|||||||
|
|
||||||
bool ret;
|
bool ret;
|
||||||
if (tunnel->forward) {
|
if (tunnel->forward) {
|
||||||
ret = adb_forward_remove(intr, serial, tunnel->local_port);
|
ret = adb_forward_remove(intr, serial, tunnel->local_port,
|
||||||
|
SC_ADB_NO_STDOUT);
|
||||||
} else {
|
} else {
|
||||||
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME);
|
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME,
|
||||||
|
SC_ADB_NO_STDOUT);
|
||||||
|
|
||||||
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
||||||
if (!net_close(tunnel->server_socket)) {
|
if (!net_close(tunnel->server_socket)) {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "aoa_hid.h"
|
#include "aoa_hid.h"
|
||||||
|
#include "util/log.h"
|
||||||
|
|
||||||
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
|
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
|
||||||
#define ACCESSORY_REGISTER_HID 54
|
#define ACCESSORY_REGISTER_HID 54
|
||||||
@ -20,6 +21,7 @@ sc_hid_event_log(const struct sc_hid_event *event) {
|
|||||||
unsigned buffer_size = event->size * 3 + 1;
|
unsigned buffer_size = event->size * 3 + 1;
|
||||||
char *buffer = malloc(buffer_size);
|
char *buffer = malloc(buffer_size);
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
|
LOG_OOM();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < event->size; ++i) {
|
for (unsigned i = 0; i < event->size; ++i) {
|
||||||
@ -35,7 +37,7 @@ sc_hid_event_init(struct sc_hid_event *hid_event, uint16_t accessory_id,
|
|||||||
hid_event->accessory_id = accessory_id;
|
hid_event->accessory_id = accessory_id;
|
||||||
hid_event->buffer = buffer;
|
hid_event->buffer = buffer;
|
||||||
hid_event->size = buffer_size;
|
hid_event->size = buffer_size;
|
||||||
hid_event->delay = 0;
|
hid_event->ack_to_wait = SC_SEQUENCE_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -118,7 +120,10 @@ sc_aoa_open_usb_handle(libusb_device *device, libusb_device_handle **handle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_aoa_init(struct sc_aoa *aoa, const char *serial) {
|
sc_aoa_init(struct sc_aoa *aoa, const char *serial,
|
||||||
|
struct sc_acksync *acksync) {
|
||||||
|
assert(acksync);
|
||||||
|
|
||||||
cbuf_init(&aoa->queue);
|
cbuf_init(&aoa->queue);
|
||||||
|
|
||||||
if (!sc_mutex_init(&aoa->mutex)) {
|
if (!sc_mutex_init(&aoa->mutex)) {
|
||||||
@ -155,6 +160,7 @@ sc_aoa_init(struct sc_aoa *aoa, const char *serial) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
aoa->stopped = false;
|
aoa->stopped = false;
|
||||||
|
aoa->acksync = acksync;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -332,23 +338,28 @@ run_aoa_thread(void *data) {
|
|||||||
assert(non_empty);
|
assert(non_empty);
|
||||||
(void) non_empty;
|
(void) non_empty;
|
||||||
|
|
||||||
assert(event.delay >= 0);
|
uint64_t ack_to_wait = event.ack_to_wait;
|
||||||
if (event.delay) {
|
sc_mutex_unlock(&aoa->mutex);
|
||||||
// Wait during the specified delay before injecting the HID event
|
|
||||||
sc_tick deadline = sc_tick_now() + event.delay;
|
if (ack_to_wait != SC_SEQUENCE_INVALID) {
|
||||||
bool timed_out = false;
|
LOGD("Waiting ack from server sequence=%" PRIu64_, ack_to_wait);
|
||||||
while (!aoa->stopped && !timed_out) {
|
// Do not block the loop indefinitely if the ack never comes (it should
|
||||||
timed_out = !sc_cond_timedwait(&aoa->event_cond, &aoa->mutex,
|
// never happen)
|
||||||
deadline);
|
sc_tick deadline = sc_tick_now() + SC_TICK_FROM_MS(500);
|
||||||
}
|
enum sc_acksync_wait_result result =
|
||||||
if (aoa->stopped) {
|
sc_acksync_wait(aoa->acksync, ack_to_wait, deadline);
|
||||||
sc_mutex_unlock(&aoa->mutex);
|
|
||||||
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_mutex_unlock(&aoa->mutex);
|
|
||||||
|
|
||||||
bool ok = sc_aoa_send_hid_event(aoa, &event);
|
bool ok = sc_aoa_send_hid_event(aoa, &event);
|
||||||
sc_hid_event_destroy(&event);
|
sc_hid_event_destroy(&event);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
@ -362,7 +373,7 @@ bool
|
|||||||
sc_aoa_start(struct sc_aoa *aoa) {
|
sc_aoa_start(struct sc_aoa *aoa) {
|
||||||
LOGD("Starting AOA thread");
|
LOGD("Starting AOA thread");
|
||||||
|
|
||||||
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "aoa_thread", aoa);
|
bool ok = sc_thread_create(&aoa->thread, run_aoa_thread, "scrcpy-aoa", aoa);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start AOA thread");
|
LOGC("Could not start AOA thread");
|
||||||
return false;
|
return false;
|
||||||
@ -377,6 +388,8 @@ sc_aoa_stop(struct sc_aoa *aoa) {
|
|||||||
aoa->stopped = true;
|
aoa->stopped = true;
|
||||||
sc_cond_signal(&aoa->event_cond);
|
sc_cond_signal(&aoa->event_cond);
|
||||||
sc_mutex_unlock(&aoa->mutex);
|
sc_mutex_unlock(&aoa->mutex);
|
||||||
|
|
||||||
|
sc_acksync_interrupt(aoa->acksync);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
|
#include "util/acksync.h"
|
||||||
#include "util/cbuf.h"
|
#include "util/cbuf.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
#include "util/tick.h"
|
#include "util/tick.h"
|
||||||
@ -14,7 +15,7 @@ struct sc_hid_event {
|
|||||||
uint16_t accessory_id;
|
uint16_t accessory_id;
|
||||||
unsigned char *buffer;
|
unsigned char *buffer;
|
||||||
uint16_t size;
|
uint16_t size;
|
||||||
sc_tick delay;
|
uint64_t ack_to_wait;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Takes ownership of buffer
|
// Takes ownership of buffer
|
||||||
@ -36,10 +37,12 @@ struct sc_aoa {
|
|||||||
sc_cond event_cond;
|
sc_cond event_cond;
|
||||||
bool stopped;
|
bool stopped;
|
||||||
struct sc_hid_event_queue queue;
|
struct sc_hid_event_queue queue;
|
||||||
|
|
||||||
|
struct sc_acksync *acksync;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_aoa_init(struct sc_aoa *aoa, const char *serial);
|
sc_aoa_init(struct sc_aoa *aoa, const char *serial, struct sc_acksync *acksync);
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_aoa_destroy(struct sc_aoa *aoa);
|
sc_aoa_destroy(struct sc_aoa *aoa);
|
||||||
|
193
app/src/cli.c
193
app/src/cli.c
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
#include "util/net.h"
|
||||||
#include "util/str.h"
|
#include "util/str.h"
|
||||||
#include "util/strbuf.h"
|
#include "util/strbuf.h"
|
||||||
#include "util/term.h"
|
#include "util/term.h"
|
||||||
@ -46,6 +47,11 @@
|
|||||||
#define OPT_V4L2_SINK 1027
|
#define OPT_V4L2_SINK 1027
|
||||||
#define OPT_DISPLAY_BUFFER 1028
|
#define OPT_DISPLAY_BUFFER 1028
|
||||||
#define OPT_V4L2_BUFFER 1029
|
#define OPT_V4L2_BUFFER 1029
|
||||||
|
#define OPT_TUNNEL_HOST 1030
|
||||||
|
#define OPT_TUNNEL_PORT 1031
|
||||||
|
#define OPT_NO_CLIPBOARD_AUTOSYNC 1032
|
||||||
|
#define OPT_TCPIP 1033
|
||||||
|
#define OPT_RAW_KEY_EVENTS 1034
|
||||||
|
|
||||||
struct sc_option {
|
struct sc_option {
|
||||||
char shortopt;
|
char shortopt;
|
||||||
@ -65,6 +71,11 @@ struct sc_shortcut {
|
|||||||
const char *text;
|
const char *text;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct sc_envvar {
|
||||||
|
const char *name;
|
||||||
|
const char *text;
|
||||||
|
};
|
||||||
|
|
||||||
struct sc_getopt_adapter {
|
struct sc_getopt_adapter {
|
||||||
char *optstring;
|
char *optstring;
|
||||||
struct option *longopts;
|
struct option *longopts;
|
||||||
@ -160,7 +171,14 @@ static const struct sc_option options[] = {
|
|||||||
"generate non-ASCII characters, contrary to the default "
|
"generate non-ASCII characters, contrary to the default "
|
||||||
"injection method.\n"
|
"injection method.\n"
|
||||||
"It may only work over USB, and is currently only supported "
|
"It may only work over USB, and is currently only supported "
|
||||||
"on Linux.",
|
"on Linux.\n"
|
||||||
|
"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`.\n"
|
||||||
|
"However, the option is only available when the HID keyboard "
|
||||||
|
"is enabled (or a physical keyboard is connected).",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.shortopt = 'h',
|
.shortopt = 'h',
|
||||||
@ -205,6 +223,15 @@ static const struct sc_option options[] = {
|
|||||||
"is preserved.\n"
|
"is preserved.\n"
|
||||||
"Default is 0 (unlimited).",
|
"Default is 0 (unlimited).",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_NO_CLIPBOARD_AUTOSYNC,
|
||||||
|
.longopt = "no-clipboard-autosync",
|
||||||
|
.text = "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.\n"
|
||||||
|
"This option disables this automatic synchronization."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.shortopt = 'n',
|
.shortopt = 'n',
|
||||||
.longopt = "no-control",
|
.longopt = "no-control",
|
||||||
@ -261,6 +288,11 @@ static const struct sc_option options[] = {
|
|||||||
"drag & drop. It is passed as is to \"adb push\".\n"
|
"drag & drop. It is passed as is to \"adb push\".\n"
|
||||||
"Default is \"/sdcard/Download/\".",
|
"Default is \"/sdcard/Download/\".",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_RAW_KEY_EVENTS,
|
||||||
|
.longopt = "raw-key-events",
|
||||||
|
.text = "Inject key events for all input keys, and ignore text events."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.shortopt = 'r',
|
.shortopt = 'r',
|
||||||
.longopt = "record",
|
.longopt = "record",
|
||||||
@ -330,6 +362,25 @@ static const struct sc_option options[] = {
|
|||||||
"on exit.\n"
|
"on exit.\n"
|
||||||
"It only shows physical touches (not clicks from scrcpy).",
|
"It only shows physical touches (not clicks from scrcpy).",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_TUNNEL_HOST,
|
||||||
|
.longopt = "tunnel-host",
|
||||||
|
.argdesc = "ip",
|
||||||
|
.text = "Set the IP address of the adb tunnel to reach the scrcpy "
|
||||||
|
"server. This option automatically enables "
|
||||||
|
"--force-adb-forward.\n"
|
||||||
|
"Default is localhost.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_TUNNEL_PORT,
|
||||||
|
.longopt = "tunnel-port",
|
||||||
|
.argdesc = "port",
|
||||||
|
.text = "Set the TCP port of the adb tunnel to reach the scrcpy "
|
||||||
|
"server. This option automatically enables "
|
||||||
|
"--force-adb-forward.\n"
|
||||||
|
"Default is 0 (not forced): the local port used for "
|
||||||
|
"establishing the tunnel will be used.",
|
||||||
|
},
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_V4L2_SINK,
|
.longopt_id = OPT_V4L2_SINK,
|
||||||
@ -372,6 +423,20 @@ static const struct sc_option options[] = {
|
|||||||
.text = "Keep the device on while scrcpy is running, when the device "
|
.text = "Keep the device on while scrcpy is running, when the device "
|
||||||
"is plugged in.",
|
"is plugged in.",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.longopt_id = OPT_TCPIP,
|
||||||
|
.longopt = "tcpip",
|
||||||
|
.argdesc = "ip[:port]",
|
||||||
|
.optional_arg = true,
|
||||||
|
.text = "Configure and reconnect the device over TCP/IP.\n"
|
||||||
|
"If a destination address is provided, then scrcpy connects to "
|
||||||
|
"this address before starting. The device must listen on the "
|
||||||
|
"given TCP port (default is 5555).\n"
|
||||||
|
"If no destination address is provided, then scrcpy attempts "
|
||||||
|
"to find the IP address of the current device (typically "
|
||||||
|
"connected over USB), enables TCP/IP mode, then connects to "
|
||||||
|
"this address before starting.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.longopt_id = OPT_WINDOW_BORDERLESS,
|
.longopt_id = OPT_WINDOW_BORDERLESS,
|
||||||
.longopt = "window-borderless",
|
.longopt = "window-borderless",
|
||||||
@ -525,6 +590,21 @@ static const struct sc_shortcut shortcuts[] = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const struct sc_envvar envvars[] = {
|
||||||
|
{
|
||||||
|
.name = "ADB",
|
||||||
|
.text = "Path to adb executable",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.name = "SCRCPY_ICON_PATH",
|
||||||
|
.text = "Path to the program icon",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.name = "SCRCPY_SERVER_PATH",
|
||||||
|
.text = "Path to the server binary",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
sc_getopt_adapter_create_optstring(void) {
|
sc_getopt_adapter_create_optstring(void) {
|
||||||
struct sc_strbuf buf;
|
struct sc_strbuf buf;
|
||||||
@ -563,6 +643,7 @@ sc_getopt_adapter_create_longopts(void) {
|
|||||||
struct option *longopts =
|
struct option *longopts =
|
||||||
malloc((ARRAY_LEN(options) + 1) * sizeof(*longopts));
|
malloc((ARRAY_LEN(options) + 1) * sizeof(*longopts));
|
||||||
if (!longopts) {
|
if (!longopts) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -617,7 +698,7 @@ sc_getopt_adapter_init(struct sc_getopt_adapter *adapter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_getopt_adapter_destroy(struct sc_getopt_adapter *adapter) {
|
sc_getopt_adapter_destroy(struct sc_getopt_adapter *adapter) {
|
||||||
@ -715,7 +796,7 @@ print_shortcuts_intro(unsigned cols) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", intro);
|
printf("\n%s\n", intro);
|
||||||
free(intro);
|
free(intro);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -743,6 +824,23 @@ print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
|
|||||||
free(text);
|
free(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_envvar(const struct sc_envvar *envvar, unsigned cols) {
|
||||||
|
assert(cols > 8); // sc_str_wrap_lines() requires indent < columns
|
||||||
|
assert(envvar->name);
|
||||||
|
assert(envvar->text);
|
||||||
|
|
||||||
|
printf("\n %s\n", envvar->name);
|
||||||
|
char *text = sc_str_wrap_lines(envvar->text, cols, 8);
|
||||||
|
if (!text) {
|
||||||
|
printf("<ERROR>\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n", text);
|
||||||
|
free(text);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
scrcpy_print_usage(const char *arg0) {
|
scrcpy_print_usage(const char *arg0) {
|
||||||
#define SC_TERM_COLS_DEFAULT 80
|
#define SC_TERM_COLS_DEFAULT 80
|
||||||
@ -770,11 +868,17 @@ scrcpy_print_usage(const char *arg0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print shortcuts section
|
// Print shortcuts section
|
||||||
printf("\nShortcuts:\n\n");
|
printf("\nShortcuts:\n");
|
||||||
print_shortcuts_intro(cols);
|
print_shortcuts_intro(cols);
|
||||||
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
|
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
|
||||||
print_shortcut(&shortcuts[i], cols);
|
print_shortcut(&shortcuts[i], cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print environment variables section
|
||||||
|
printf("\nEnvironment variables:\n");
|
||||||
|
for (size_t i = 0; i < ARRAY_LEN(envvars); ++i) {
|
||||||
|
print_envvar(&envvars[i], cols);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -1017,7 +1121,7 @@ parse_log_level(const char *s, enum sc_log_level *log_level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// item is a list of mod keys separated by '+' (e.g. "lctrl+lalt")
|
// item is a list of mod keys separated by '+' (e.g. "lctrl+lalt")
|
||||||
// returns a bitwise-or of SC_MOD_* constants (or 0 on error)
|
// returns a bitwise-or of SC_SHORTCUT_MOD_* constants (or 0 on error)
|
||||||
static unsigned
|
static unsigned
|
||||||
parse_shortcut_mods_item(const char *item, size_t len) {
|
parse_shortcut_mods_item(const char *item, size_t len) {
|
||||||
unsigned mod = 0;
|
unsigned mod = 0;
|
||||||
@ -1035,17 +1139,17 @@ parse_shortcut_mods_item(const char *item, size_t len) {
|
|||||||
((sizeof(literal)-1 == len) && !memcmp(literal, s, len))
|
((sizeof(literal)-1 == len) && !memcmp(literal, s, len))
|
||||||
|
|
||||||
if (STREQ("lctrl", item, key_len)) {
|
if (STREQ("lctrl", item, key_len)) {
|
||||||
mod |= SC_MOD_LCTRL;
|
mod |= SC_SHORTCUT_MOD_LCTRL;
|
||||||
} else if (STREQ("rctrl", item, key_len)) {
|
} else if (STREQ("rctrl", item, key_len)) {
|
||||||
mod |= SC_MOD_RCTRL;
|
mod |= SC_SHORTCUT_MOD_RCTRL;
|
||||||
} else if (STREQ("lalt", item, key_len)) {
|
} else if (STREQ("lalt", item, key_len)) {
|
||||||
mod |= SC_MOD_LALT;
|
mod |= SC_SHORTCUT_MOD_LALT;
|
||||||
} else if (STREQ("ralt", item, key_len)) {
|
} else if (STREQ("ralt", item, key_len)) {
|
||||||
mod |= SC_MOD_RALT;
|
mod |= SC_SHORTCUT_MOD_RALT;
|
||||||
} else if (STREQ("lsuper", item, key_len)) {
|
} else if (STREQ("lsuper", item, key_len)) {
|
||||||
mod |= SC_MOD_LSUPER;
|
mod |= SC_SHORTCUT_MOD_LSUPER;
|
||||||
} else if (STREQ("rsuper", item, key_len)) {
|
} else if (STREQ("rsuper", item, key_len)) {
|
||||||
mod |= SC_MOD_RSUPER;
|
mod |= SC_SHORTCUT_MOD_RSUPER;
|
||||||
} else {
|
} else {
|
||||||
LOGE("Unknown modifier key: %.*s "
|
LOGE("Unknown modifier key: %.*s "
|
||||||
"(must be one of: lctrl, rctrl, lalt, ralt, lsuper, rsuper)",
|
"(must be one of: lctrl, rctrl, lalt, ralt, lsuper, rsuper)",
|
||||||
@ -1127,6 +1231,21 @@ parse_record_format(const char *optarg, enum sc_record_format *format) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
parse_ip(const char *optarg, uint32_t *ipv4) {
|
||||||
|
return net_parse_ipv4(optarg, ipv4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
parse_port(const char *optarg, uint16_t *port) {
|
||||||
|
long value;
|
||||||
|
if (!parse_integer_arg(optarg, &value, false, 0, 0xFFFF, "port")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*port = (uint16_t) value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static enum sc_record_format
|
static enum sc_record_format
|
||||||
guess_record_format(const char *filename) {
|
guess_record_format(const char *filename) {
|
||||||
size_t len = strlen(filename);
|
size_t len = strlen(filename);
|
||||||
@ -1199,6 +1318,16 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case OPT_TUNNEL_HOST:
|
||||||
|
if (!parse_ip(optarg, &opts->tunnel_host)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case OPT_TUNNEL_PORT:
|
||||||
|
if (!parse_port(optarg, &opts->tunnel_port)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
opts->control = false;
|
opts->control = false;
|
||||||
break;
|
break;
|
||||||
@ -1270,7 +1399,18 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
opts->push_target = optarg;
|
opts->push_target = optarg;
|
||||||
break;
|
break;
|
||||||
case OPT_PREFER_TEXT:
|
case OPT_PREFER_TEXT:
|
||||||
opts->prefer_text = true;
|
if (opts->key_inject_mode != SC_KEY_INJECT_MODE_MIXED) {
|
||||||
|
LOGE("--prefer-text is incompatible with --raw-key-events");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
opts->key_inject_mode = SC_KEY_INJECT_MODE_TEXT;
|
||||||
|
break;
|
||||||
|
case OPT_RAW_KEY_EVENTS:
|
||||||
|
if (opts->key_inject_mode != SC_KEY_INJECT_MODE_MIXED) {
|
||||||
|
LOGE("--prefer-text is incompatible with --raw-key-events");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
opts->key_inject_mode = SC_KEY_INJECT_MODE_RAW;
|
||||||
break;
|
break;
|
||||||
case OPT_ROTATION:
|
case OPT_ROTATION:
|
||||||
if (!parse_rotation(optarg, &opts->rotation)) {
|
if (!parse_rotation(optarg, &opts->rotation)) {
|
||||||
@ -1317,6 +1457,13 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case OPT_NO_CLIPBOARD_AUTOSYNC:
|
||||||
|
opts->clipboard_autosync = false;
|
||||||
|
break;
|
||||||
|
case OPT_TCPIP:
|
||||||
|
opts->tcpip = true;
|
||||||
|
opts->tcpip_dst = optarg;
|
||||||
|
break;
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
case OPT_V4L2_SINK:
|
case OPT_V4L2_SINK:
|
||||||
opts->v4l2_device = optarg;
|
opts->v4l2_device = optarg;
|
||||||
@ -1333,6 +1480,20 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int index = optind;
|
||||||
|
if (index < argc) {
|
||||||
|
LOGE("Unexpected additional argument: %s", argv[index]);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a TCP/IP address is provided, then tcpip must be enabled
|
||||||
|
assert(opts->tcpip || !opts->tcpip_dst);
|
||||||
|
|
||||||
|
if (opts->serial && opts->tcpip_dst) {
|
||||||
|
LOGE("Incompatible options: -s/--serial and --tcpip with an argument");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
if (!opts->display && !opts->record_filename && !opts->v4l2_device) {
|
if (!opts->display && !opts->record_filename && !opts->v4l2_device) {
|
||||||
LOGE("-N/--no-display requires either screen recording (-r/--record)"
|
LOGE("-N/--no-display requires either screen recording (-r/--record)"
|
||||||
@ -1358,10 +1519,10 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int index = optind;
|
if ((opts->tunnel_host || opts->tunnel_port) && !opts->force_adb_forward) {
|
||||||
if (index < argc) {
|
LOGI("Tunnel host/port is set, "
|
||||||
LOGE("Unexpected additional argument: %s", argv[index]);
|
"--force-adb-forward automatically enabled.");
|
||||||
return false;
|
opts->force_adb_forward = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->record_format && !opts->record_filename) {
|
if (opts->record_format && !opts->record_filename) {
|
||||||
|
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#ifndef HAVE_STRDUP
|
#ifndef HAVE_STRDUP
|
||||||
char *strdup(const char *s) {
|
char *strdup(const char *s) {
|
||||||
size_t size = strlen(s) + 1;
|
size_t size = strlen(s) + 1;
|
||||||
@ -12,3 +18,36 @@ char *strdup(const char *s) {
|
|||||||
return dup;
|
return dup;
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
#ifndef COMPAT_H
|
||||||
#define COMPAT_H
|
#define COMPAT_H
|
||||||
|
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#include "config.h"
|
||||||
#define _XOPEN_SOURCE 700
|
|
||||||
#define _GNU_SOURCE
|
|
||||||
#ifdef __APPLE__
|
|
||||||
# define _DARWIN_C_SOURCE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <libavformat/version.h>
|
#include <libavformat/version.h>
|
||||||
#include <SDL2/SDL_version.h>
|
#include <SDL2/SDL_version.h>
|
||||||
|
|
||||||
|
#ifndef __WIN32
|
||||||
|
# define PRIu64_ PRIu64
|
||||||
|
#else
|
||||||
|
# define PRIu64_ "I64u" // Windows...
|
||||||
|
#endif
|
||||||
|
|
||||||
// In ffmpeg/doc/APIchanges:
|
// In ffmpeg/doc/APIchanges:
|
||||||
// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
|
// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
|
||||||
// Deprecate use of av_register_input_format(), av_register_output_format(),
|
// Deprecate use of av_register_input_format(), av_register_output_format(),
|
||||||
@ -34,15 +35,6 @@
|
|||||||
# define SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
# define SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
||||||
#endif
|
#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
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if SDL_VERSION_ATLEAST(2, 0, 6)
|
#if SDL_VERSION_ATLEAST(2, 0, 6)
|
||||||
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
|
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
|
||||||
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||||
@ -57,4 +49,12 @@
|
|||||||
char *strdup(const char *s);
|
char *strdup(const char *s);
|
||||||
#endif
|
#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
|
#endif
|
||||||
|
@ -41,7 +41,7 @@ static const char *const android_motionevent_action_labels[] = {
|
|||||||
"pointer-up",
|
"pointer-up",
|
||||||
"hover-move",
|
"hover-move",
|
||||||
"scroll",
|
"scroll",
|
||||||
"hover-enter"
|
"hover-enter",
|
||||||
"hover-exit",
|
"hover-exit",
|
||||||
"btn-press",
|
"btn-press",
|
||||||
"btn-release",
|
"btn-release",
|
||||||
@ -55,6 +55,12 @@ static const char *const screen_power_mode_labels[] = {
|
|||||||
"suspend",
|
"suspend",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const char *const copy_key_labels[] = {
|
||||||
|
"none",
|
||||||
|
"copy",
|
||||||
|
"cut",
|
||||||
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
write_position(uint8_t *buf, const struct sc_position *position) {
|
write_position(uint8_t *buf, const struct sc_position *position) {
|
||||||
buffer_write32be(&buf[0], position->point.x);
|
buffer_write32be(&buf[0], position->point.x);
|
||||||
@ -63,7 +69,7 @@ write_position(uint8_t *buf, const struct sc_position *position) {
|
|||||||
buffer_write16be(&buf[10], position->screen_size.height);
|
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
|
static size_t
|
||||||
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
||||||
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
||||||
@ -117,12 +123,16 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
|||||||
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
||||||
buf[1] = msg->inject_keycode.action;
|
buf[1] = msg->inject_keycode.action;
|
||||||
return 2;
|
return 2;
|
||||||
|
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
||||||
|
buf[1] = msg->get_clipboard.copy_key;
|
||||||
|
return 2;
|
||||||
case CONTROL_MSG_TYPE_SET_CLIPBOARD: {
|
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,
|
size_t len = write_string(msg->set_clipboard.text,
|
||||||
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
||||||
&buf[2]);
|
&buf[10]);
|
||||||
return 2 + len;
|
return 10 + len;
|
||||||
}
|
}
|
||||||
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
||||||
buf[1] = msg->set_screen_power_mode.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_NOTIFICATION_PANEL:
|
||||||
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
||||||
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
||||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
|
||||||
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
||||||
// no additional data
|
// no additional data
|
||||||
return 1;
|
return 1;
|
||||||
@ -170,11 +179,6 @@ control_msg_log(const struct control_msg *msg) {
|
|||||||
(long) msg->inject_touch_event.buttons);
|
(long) msg->inject_touch_event.buttons);
|
||||||
} else {
|
} else {
|
||||||
// numeric pointer id
|
// numeric pointer id
|
||||||
#ifndef __WIN32
|
|
||||||
# define PRIu64_ PRIu64
|
|
||||||
#else
|
|
||||||
# define PRIu64_ "I64u" // Windows...
|
|
||||||
#endif
|
|
||||||
LOG_CMSG("touch [id=%" PRIu64_ "] %-4s position=%" PRIi32 ",%"
|
LOG_CMSG("touch [id=%" PRIu64_ "] %-4s position=%" PRIi32 ",%"
|
||||||
PRIi32 " pressure=%g buttons=%06lx",
|
PRIi32 " pressure=%g buttons=%06lx",
|
||||||
id,
|
id,
|
||||||
@ -198,9 +202,14 @@ control_msg_log(const struct control_msg *msg) {
|
|||||||
LOG_CMSG("back-or-screen-on %s",
|
LOG_CMSG("back-or-screen-on %s",
|
||||||
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
|
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
|
||||||
break;
|
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:
|
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
||||||
LOG_CMSG("clipboard %s \"%s\"",
|
LOG_CMSG("clipboard %" PRIu64_ " %s \"%s\"",
|
||||||
msg->set_clipboard.paste ? "paste" : "copy",
|
msg->set_clipboard.sequence,
|
||||||
|
msg->set_clipboard.paste ? "paste" : "nopaste",
|
||||||
msg->set_clipboard.text);
|
msg->set_clipboard.text);
|
||||||
break;
|
break;
|
||||||
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
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:
|
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
||||||
LOG_CMSG("collapse panels");
|
LOG_CMSG("collapse panels");
|
||||||
break;
|
break;
|
||||||
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
|
||||||
LOG_CMSG("get clipboard");
|
|
||||||
break;
|
|
||||||
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
||||||
LOG_CMSG("rotate device");
|
LOG_CMSG("rotate device");
|
||||||
break;
|
break;
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
#define CONTROL_MSG_MAX_SIZE (1 << 18) // 256k
|
#define CONTROL_MSG_MAX_SIZE (1 << 18) // 256k
|
||||||
|
|
||||||
#define CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300
|
#define CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300
|
||||||
// type: 1 byte; paste flag: 1 byte; length: 4 bytes
|
// type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
|
||||||
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (CONTROL_MSG_MAX_SIZE - 6)
|
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (CONTROL_MSG_MAX_SIZE - 14)
|
||||||
|
|
||||||
#define POINTER_ID_MOUSE UINT64_C(-1)
|
#define POINTER_ID_MOUSE UINT64_C(-1)
|
||||||
#define POINTER_ID_VIRTUAL_FINGER UINT64_C(-2)
|
#define POINTER_ID_VIRTUAL_FINGER UINT64_C(-2)
|
||||||
@ -41,6 +41,12 @@ enum screen_power_mode {
|
|||||||
SCREEN_POWER_MODE_NORMAL = 2,
|
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 {
|
struct control_msg {
|
||||||
enum control_msg_type type;
|
enum control_msg_type type;
|
||||||
union {
|
union {
|
||||||
@ -70,6 +76,10 @@ struct control_msg {
|
|||||||
// screen may only be turned on on ACTION_DOWN
|
// screen may only be turned on on ACTION_DOWN
|
||||||
} back_or_screen_on;
|
} back_or_screen_on;
|
||||||
struct {
|
struct {
|
||||||
|
enum get_clipboard_copy_key copy_key;
|
||||||
|
} get_clipboard;
|
||||||
|
struct {
|
||||||
|
uint64_t sequence;
|
||||||
char *text; // owned, to be freed by free()
|
char *text; // owned, to be freed by free()
|
||||||
bool paste;
|
bool paste;
|
||||||
} set_clipboard;
|
} set_clipboard;
|
||||||
|
@ -5,10 +5,11 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
controller_init(struct controller *controller, sc_socket control_socket) {
|
controller_init(struct controller *controller, sc_socket control_socket,
|
||||||
|
struct sc_acksync *acksync) {
|
||||||
cbuf_init(&controller->queue);
|
cbuf_init(&controller->queue);
|
||||||
|
|
||||||
bool ok = receiver_init(&controller->receiver, control_socket);
|
bool ok = receiver_init(&controller->receiver, control_socket, acksync);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -109,7 +110,7 @@ controller_start(struct controller *controller) {
|
|||||||
LOGD("Starting controller thread");
|
LOGD("Starting controller thread");
|
||||||
|
|
||||||
bool ok = sc_thread_create(&controller->thread, run_controller,
|
bool ok = sc_thread_create(&controller->thread, run_controller,
|
||||||
"controller", controller);
|
"scrcpy-ctl", controller);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start controller thread");
|
LOGC("Could not start controller thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "control_msg.h"
|
#include "control_msg.h"
|
||||||
#include "receiver.h"
|
#include "receiver.h"
|
||||||
|
#include "util/acksync.h"
|
||||||
#include "util/cbuf.h"
|
#include "util/cbuf.h"
|
||||||
#include "util/net.h"
|
#include "util/net.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
@ -24,7 +25,8 @@ struct controller {
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
controller_init(struct controller *controller, sc_socket control_socket);
|
controller_init(struct controller *controller, sc_socket control_socket,
|
||||||
|
struct sc_acksync *acksync);
|
||||||
|
|
||||||
void
|
void
|
||||||
controller_destroy(struct controller *controller);
|
controller_destroy(struct controller *controller);
|
||||||
|
@ -42,10 +42,12 @@ static bool
|
|||||||
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
||||||
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
||||||
if (!decoder->codec_ctx) {
|
if (!decoder->codec_ctx) {
|
||||||
LOGC("Could not allocate decoder context");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
|
||||||
|
|
||||||
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
|
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
|
||||||
LOGE("Could not open codec");
|
LOGE("Could not open codec");
|
||||||
avcodec_free_context(&decoder->codec_ctx);
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
@ -54,7 +56,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
|||||||
|
|
||||||
decoder->frame = av_frame_alloc();
|
decoder->frame = av_frame_alloc();
|
||||||
if (!decoder->frame) {
|
if (!decoder->frame) {
|
||||||
LOGE("Could not create decoder frame");
|
LOG_OOM();
|
||||||
avcodec_close(decoder->codec_ctx);
|
avcodec_close(decoder->codec_ctx);
|
||||||
avcodec_free_context(&decoder->codec_ctx);
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "device_msg.h"
|
#include "device_msg.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -23,7 +24,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
|||||||
}
|
}
|
||||||
char *text = malloc(clipboard_len + 1);
|
char *text = malloc(clipboard_len + 1);
|
||||||
if (!text) {
|
if (!text) {
|
||||||
LOGW("Could not allocate text for clipboard");
|
LOG_OOM();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (clipboard_len) {
|
if (clipboard_len) {
|
||||||
@ -34,6 +35,11 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
|
|||||||
msg->clipboard.text = text;
|
msg->clipboard.text = text;
|
||||||
return 5 + clipboard_len;
|
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:
|
default:
|
||||||
LOGW("Unknown device message type: %d", (int) msg->type);
|
LOGW("Unknown device message type: %d", (int) msg->type);
|
||||||
return -1; // error, we cannot recover
|
return -1; // error, we cannot recover
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
enum device_msg_type {
|
enum device_msg_type {
|
||||||
DEVICE_MSG_TYPE_CLIPBOARD,
|
DEVICE_MSG_TYPE_CLIPBOARD,
|
||||||
|
DEVICE_MSG_TYPE_ACK_CLIPBOARD,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct device_msg {
|
struct device_msg {
|
||||||
@ -21,6 +22,9 @@ struct device_msg {
|
|||||||
struct {
|
struct {
|
||||||
char *text; // owned, to be freed by free()
|
char *text; // owned, to be freed by free()
|
||||||
} clipboard;
|
} clipboard;
|
||||||
|
struct {
|
||||||
|
uint64_t sequence;
|
||||||
|
} ack_clipboard;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,14 +34,14 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
|
|||||||
|
|
||||||
ok = sc_intr_init(&file_handler->intr);
|
ok = sc_intr_init(&file_handler->intr);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not create intr");
|
|
||||||
sc_cond_destroy(&file_handler->event_cond);
|
sc_cond_destroy(&file_handler->event_cond);
|
||||||
sc_mutex_destroy(&file_handler->mutex);
|
sc_mutex_destroy(&file_handler->mutex);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_handler->serial = strdup(serial);
|
file_handler->serial = strdup(serial);
|
||||||
if (!file_handler->serial) {
|
if (!file_handler->serial) {
|
||||||
LOGE("Could not strdup serial");
|
LOG_OOM();
|
||||||
sc_intr_destroy(&file_handler->intr);
|
sc_intr_destroy(&file_handler->intr);
|
||||||
sc_cond_destroy(&file_handler->event_cond);
|
sc_cond_destroy(&file_handler->event_cond);
|
||||||
sc_mutex_destroy(&file_handler->mutex);
|
sc_mutex_destroy(&file_handler->mutex);
|
||||||
@ -128,7 +128,7 @@ run_file_handler(void *data) {
|
|||||||
|
|
||||||
if (req.action == ACTION_INSTALL_APK) {
|
if (req.action == ACTION_INSTALL_APK) {
|
||||||
LOGI("Installing %s...", req.file);
|
LOGI("Installing %s...", req.file);
|
||||||
bool ok = adb_install(intr, serial, req.file);
|
bool ok = adb_install(intr, serial, req.file, 0);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
LOGI("%s successfully installed", req.file);
|
LOGI("%s successfully installed", req.file);
|
||||||
} else {
|
} else {
|
||||||
@ -136,7 +136,7 @@ run_file_handler(void *data) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LOGI("Pushing %s...", req.file);
|
LOGI("Pushing %s...", req.file);
|
||||||
bool ok = adb_push(intr, serial, req.file, push_target);
|
bool ok = adb_push(intr, serial, req.file, push_target, 0);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
LOGI("%s successfully pushed to %s", req.file, push_target);
|
LOGI("%s successfully pushed to %s", req.file, push_target);
|
||||||
} else {
|
} else {
|
||||||
@ -154,7 +154,7 @@ file_handler_start(struct file_handler *file_handler) {
|
|||||||
LOGD("Starting file_handler thread");
|
LOGD("Starting file_handler thread");
|
||||||
|
|
||||||
bool ok = sc_thread_create(&file_handler->thread, run_file_handler,
|
bool ok = sc_thread_create(&file_handler->thread, run_file_handler,
|
||||||
"file_handler", file_handler);
|
"scrcpy-file", file_handler);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start file_handler thread");
|
LOGC("Could not start file_handler thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -108,7 +108,7 @@ fps_counter_start(struct fps_counter *counter) {
|
|||||||
// same thread, no need to lock
|
// same thread, no need to lock
|
||||||
if (!counter->thread_started) {
|
if (!counter->thread_started) {
|
||||||
bool ok = sc_thread_create(&counter->thread, run_fps_counter,
|
bool ok = sc_thread_create(&counter->thread, run_fps_counter,
|
||||||
"fps counter", counter);
|
"scrcpy-fps", counter);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not start FPS counter thread");
|
LOGE("Could not start FPS counter thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -10,11 +10,13 @@ bool
|
|||||||
sc_frame_buffer_init(struct sc_frame_buffer *fb) {
|
sc_frame_buffer_init(struct sc_frame_buffer *fb) {
|
||||||
fb->pending_frame = av_frame_alloc();
|
fb->pending_frame = av_frame_alloc();
|
||||||
if (!fb->pending_frame) {
|
if (!fb->pending_frame) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fb->tmp_frame = av_frame_alloc();
|
fb->tmp_frame = av_frame_alloc();
|
||||||
if (!fb->tmp_frame) {
|
if (!fb->tmp_frame) {
|
||||||
|
LOG_OOM();
|
||||||
av_frame_free(&fb->pending_frame);
|
av_frame_free(&fb->pending_frame);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#include "hid_keyboard.h"
|
#include "hid_keyboard.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_events.h>
|
|
||||||
|
|
||||||
|
#include "input_events.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
/** Downcast key processor to hid_keyboard */
|
/** Downcast key processor to hid_keyboard */
|
||||||
@ -126,31 +126,105 @@ static const unsigned char keyboard_report_desc[] = {
|
|||||||
0xC0
|
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
|
static unsigned char
|
||||||
sdl_keymod_to_hid_modifiers(SDL_Keymod mod) {
|
sdl_keymod_to_hid_modifiers(uint16_t mod) {
|
||||||
unsigned char modifiers = HID_MODIFIER_NONE;
|
unsigned char modifiers = HID_MODIFIER_NONE;
|
||||||
if (mod & KMOD_LCTRL) {
|
if (mod & SC_MOD_LCTRL) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_CONTROL;
|
modifiers |= HID_MODIFIER_LEFT_CONTROL;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LSHIFT) {
|
if (mod & SC_MOD_LSHIFT) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_SHIFT;
|
modifiers |= HID_MODIFIER_LEFT_SHIFT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LALT) {
|
if (mod & SC_MOD_LALT) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_ALT;
|
modifiers |= HID_MODIFIER_LEFT_ALT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LGUI) {
|
if (mod & SC_MOD_LGUI) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_GUI;
|
modifiers |= HID_MODIFIER_LEFT_GUI;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RCTRL) {
|
if (mod & SC_MOD_RCTRL) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_CONTROL;
|
modifiers |= HID_MODIFIER_RIGHT_CONTROL;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RSHIFT) {
|
if (mod & SC_MOD_RSHIFT) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_SHIFT;
|
modifiers |= HID_MODIFIER_RIGHT_SHIFT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RALT) {
|
if (mod & SC_MOD_RALT) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_ALT;
|
modifiers |= HID_MODIFIER_RIGHT_ALT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RGUI) {
|
if (mod & SC_MOD_RGUI) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_GUI;
|
modifiers |= HID_MODIFIER_RIGHT_GUI;
|
||||||
}
|
}
|
||||||
return modifiers;
|
return modifiers;
|
||||||
@ -160,6 +234,7 @@ static bool
|
|||||||
sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
|
sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
|
||||||
unsigned char *buffer = malloc(HID_KEYBOARD_EVENT_SIZE);
|
unsigned char *buffer = malloc(HID_KEYBOARD_EVENT_SIZE);
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,15 +248,15 @@ sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline bool
|
static inline bool
|
||||||
scancode_is_modifier(SDL_Scancode scancode) {
|
scancode_is_modifier(enum sc_scancode scancode) {
|
||||||
return scancode >= SDL_SCANCODE_LCTRL && scancode <= SDL_SCANCODE_RGUI;
|
return scancode >= SC_SCANCODE_LCTRL && scancode <= SC_SCANCODE_RGUI;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
||||||
struct sc_hid_event *hid_event,
|
struct sc_hid_event *hid_event,
|
||||||
const SDL_KeyboardEvent *event) {
|
const struct sc_key_event *event) {
|
||||||
SDL_Scancode scancode = event->keysym.scancode;
|
enum sc_scancode scancode = event->scancode;
|
||||||
assert(scancode >= 0);
|
assert(scancode >= 0);
|
||||||
|
|
||||||
// SDL also generates events when only modifiers are pressed, we cannot
|
// SDL also generates events when only modifiers are pressed, we cannot
|
||||||
@ -197,11 +272,11 @@ convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char modifiers = sdl_keymod_to_hid_modifiers(event->keysym.mod);
|
unsigned char modifiers = sdl_keymod_to_hid_modifiers(event->mods_state);
|
||||||
|
|
||||||
if (scancode < SC_HID_KEYBOARD_KEYS) {
|
if (scancode < SC_HID_KEYBOARD_KEYS) {
|
||||||
// Pressed is true and released is false
|
// Pressed is true and released is false
|
||||||
kb->keys[scancode] = (event->type == SDL_KEYDOWN);
|
kb->keys[scancode] = (event->action == SC_ACTION_DOWN);
|
||||||
LOGV("keys[%02x] = %s", scancode,
|
LOGV("keys[%02x] = %s", scancode,
|
||||||
kb->keys[scancode] ? "true" : "false");
|
kb->keys[scancode] ? "true" : "false");
|
||||||
}
|
}
|
||||||
@ -216,7 +291,7 @@ convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
|||||||
// USB HID protocol says that if keys exceeds report count, a
|
// USB HID protocol says that if keys exceeds report count, a
|
||||||
// phantom state should be reported
|
// phantom state should be reported
|
||||||
if (keys_pressed_count >= HID_KEYBOARD_MAX_KEYS) {
|
if (keys_pressed_count >= HID_KEYBOARD_MAX_KEYS) {
|
||||||
// Pantom state:
|
// Phantom state:
|
||||||
// - Modifiers
|
// - Modifiers
|
||||||
// - Reserved
|
// - Reserved
|
||||||
// - ErrorRollOver * HID_MAX_KEYS
|
// - ErrorRollOver * HID_MAX_KEYS
|
||||||
@ -231,17 +306,17 @@ convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
|||||||
|
|
||||||
end:
|
end:
|
||||||
LOGV("hid keyboard: key %-4s scancode=%02x (%u) mod=%02x",
|
LOGV("hid keyboard: key %-4s scancode=%02x (%u) mod=%02x",
|
||||||
event->type == SDL_KEYDOWN ? "down" : "up", event->keysym.scancode,
|
event->action == SC_ACTION_DOWN ? "down" : "up", event->scancode,
|
||||||
event->keysym.scancode, modifiers);
|
event->scancode, modifiers);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t mods_state) {
|
||||||
bool capslock = sdl_mod & KMOD_CAPS;
|
bool capslock = mods_state & SC_MOD_CAPS;
|
||||||
bool numlock = sdl_mod & KMOD_NUM;
|
bool numlock = mods_state & SC_MOD_NUM;
|
||||||
if (!capslock && !numlock) {
|
if (!capslock && !numlock) {
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
return true;
|
return true;
|
||||||
@ -253,8 +328,6 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SC_SCANCODE_CAPSLOCK SDL_SCANCODE_CAPSLOCK
|
|
||||||
#define SC_SCANCODE_NUMLOCK SDL_SCANCODE_NUMLOCKCLEAR
|
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
if (capslock) {
|
if (capslock) {
|
||||||
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
|
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
|
||||||
@ -278,7 +351,8 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
const SDL_KeyboardEvent *event) {
|
const struct sc_key_event *event,
|
||||||
|
uint64_t ack_to_wait) {
|
||||||
if (event->repeat) {
|
if (event->repeat) {
|
||||||
// In USB HID protocol, key repeat is handled by the host (Android), so
|
// In USB HID protocol, key repeat is handled by the host (Android), so
|
||||||
// just ignore key repeat here.
|
// just ignore key repeat here.
|
||||||
@ -293,21 +367,17 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
|||||||
if (!kb->mod_lock_synchronized) {
|
if (!kb->mod_lock_synchronized) {
|
||||||
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
|
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
|
||||||
// keyboard state
|
// keyboard state
|
||||||
if (push_mod_lock_state(kb, event->keysym.mod)) {
|
if (push_mod_lock_state(kb, event->mods_state)) {
|
||||||
kb->mod_lock_synchronized = true;
|
kb->mod_lock_synchronized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Keycode keycode = event->keysym.sym;
|
if (ack_to_wait) {
|
||||||
bool down = event->type == SDL_KEYDOWN;
|
|
||||||
bool ctrl = event->keysym.mod & KMOD_CTRL;
|
|
||||||
bool shift = event->keysym.mod & KMOD_SHIFT;
|
|
||||||
if (ctrl && !shift && keycode == SDLK_v && down) {
|
|
||||||
// Ctrl+v is pressed, so clipboard synchronization has been
|
// Ctrl+v is pressed, so clipboard synchronization has been
|
||||||
// requested. Wait a bit so that the clipboard is set before
|
// requested. Wait until clipboard synchronization is acknowledged
|
||||||
// injecting Ctrl+v via HID, otherwise it would paste the old
|
// by the server, otherwise it could paste the old clipboard
|
||||||
// clipboard content.
|
// content.
|
||||||
hid_event.delay = SC_TICK_FROM_MS(5);
|
hid_event.ack_to_wait = ack_to_wait;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
|
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
|
||||||
@ -319,7 +389,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event) {
|
const struct sc_text_event *event) {
|
||||||
(void) kp;
|
(void) kp;
|
||||||
(void) event;
|
(void) event;
|
||||||
|
|
||||||
@ -348,6 +418,10 @@ sc_hid_keyboard_init(struct sc_hid_keyboard *kb, struct sc_aoa *aoa) {
|
|||||||
.process_text = sc_key_processor_process_text,
|
.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;
|
kb->key_processor.ops = &ops;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -31,7 +31,7 @@ get_icon_path(void) {
|
|||||||
char *icon_path = strdup(icon_path_env);
|
char *icon_path = strdup(icon_path_env);
|
||||||
#endif
|
#endif
|
||||||
if (!icon_path) {
|
if (!icon_path) {
|
||||||
LOGE("Could not allocate memory");
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
|
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
|
||||||
@ -42,7 +42,7 @@ get_icon_path(void) {
|
|||||||
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
|
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
|
||||||
char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
|
char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
|
||||||
if (!icon_path) {
|
if (!icon_path) {
|
||||||
LOGE("Could not allocate memory");
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -63,7 +63,7 @@ decode_image(const char *path) {
|
|||||||
|
|
||||||
AVFormatContext *ctx = avformat_alloc_context();
|
AVFormatContext *ctx = avformat_alloc_context();
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
LOGE("Could not allocate image decoder context");
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ decode_image(const char *path) {
|
|||||||
|
|
||||||
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
|
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
|
||||||
if (!codec_ctx) {
|
if (!codec_ctx) {
|
||||||
LOGE("Could not allocate codec context");
|
LOG_OOM();
|
||||||
goto close_input;
|
goto close_input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,13 +109,13 @@ decode_image(const char *path) {
|
|||||||
|
|
||||||
AVFrame *frame = av_frame_alloc();
|
AVFrame *frame = av_frame_alloc();
|
||||||
if (!frame) {
|
if (!frame) {
|
||||||
LOGE("Could not allocate frame");
|
LOG_OOM();
|
||||||
goto close_codec;
|
goto close_codec;
|
||||||
}
|
}
|
||||||
|
|
||||||
AVPacket *packet = av_packet_alloc();
|
AVPacket *packet = av_packet_alloc();
|
||||||
if (!packet) {
|
if (!packet) {
|
||||||
LOGE("Could not allocate packet");
|
LOG_OOM();
|
||||||
av_frame_free(&frame);
|
av_frame_free(&frame);
|
||||||
goto close_codec;
|
goto close_codec;
|
||||||
}
|
}
|
||||||
|
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,32 +3,117 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_keycode.h>
|
#include <SDL2/SDL_keycode.h>
|
||||||
|
|
||||||
|
#include "input_events.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
static const int ACTION_DOWN = 1;
|
static inline uint16_t
|
||||||
static const int ACTION_UP = 1 << 1;
|
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)
|
#define SC_SDL_SHORTCUT_MODS_MASK (KMOD_CTRL | KMOD_ALT | KMOD_GUI)
|
||||||
|
|
||||||
static inline uint16_t
|
static inline uint16_t
|
||||||
to_sdl_mod(unsigned mod) {
|
to_sdl_mod(unsigned shortcut_mod) {
|
||||||
uint16_t sdl_mod = 0;
|
uint16_t sdl_mod = 0;
|
||||||
if (mod & SC_MOD_LCTRL) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_LCTRL) {
|
||||||
sdl_mod |= KMOD_LCTRL;
|
sdl_mod |= KMOD_LCTRL;
|
||||||
}
|
}
|
||||||
if (mod & SC_MOD_RCTRL) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_RCTRL) {
|
||||||
sdl_mod |= KMOD_RCTRL;
|
sdl_mod |= KMOD_RCTRL;
|
||||||
}
|
}
|
||||||
if (mod & SC_MOD_LALT) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_LALT) {
|
||||||
sdl_mod |= KMOD_LALT;
|
sdl_mod |= KMOD_LALT;
|
||||||
}
|
}
|
||||||
if (mod & SC_MOD_RALT) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_RALT) {
|
||||||
sdl_mod |= KMOD_RALT;
|
sdl_mod |= KMOD_RALT;
|
||||||
}
|
}
|
||||||
if (mod & SC_MOD_LSUPER) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_LSUPER) {
|
||||||
sdl_mod |= KMOD_LGUI;
|
sdl_mod |= KMOD_LGUI;
|
||||||
}
|
}
|
||||||
if (mod & SC_MOD_RSUPER) {
|
if (shortcut_mod & SC_SHORTCUT_MOD_RSUPER) {
|
||||||
sdl_mod |= KMOD_RGUI;
|
sdl_mod |= KMOD_RGUI;
|
||||||
}
|
}
|
||||||
return sdl_mod;
|
return sdl_mod;
|
||||||
@ -66,6 +151,7 @@ input_manager_init(struct input_manager *im, struct controller *controller,
|
|||||||
im->control = options->control;
|
im->control = options->control;
|
||||||
im->forward_all_clicks = options->forward_all_clicks;
|
im->forward_all_clicks = options->forward_all_clicks;
|
||||||
im->legacy_paste = options->legacy_paste;
|
im->legacy_paste = options->legacy_paste;
|
||||||
|
im->clipboard_autosync = options->clipboard_autosync;
|
||||||
|
|
||||||
const struct sc_shortcut_mods *shortcut_mods = &options->shortcut_mods;
|
const struct sc_shortcut_mods *shortcut_mods = &options->shortcut_mods;
|
||||||
assert(shortcut_mods->count);
|
assert(shortcut_mods->count);
|
||||||
@ -82,6 +168,8 @@ input_manager_init(struct input_manager *im, struct controller *controller,
|
|||||||
im->last_keycode = SDLK_UNKNOWN;
|
im->last_keycode = SDLK_UNKNOWN;
|
||||||
im->last_mod = 0;
|
im->last_mod = 0;
|
||||||
im->key_repeat = 0;
|
im->key_repeat = 0;
|
||||||
|
|
||||||
|
im->next_sequence = 1; // 0 is reserved for SC_SEQUENCE_INVALID
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -94,7 +182,7 @@ send_keycode(struct controller *controller, enum android_keycode keycode,
|
|||||||
msg.inject_keycode.metastate = 0;
|
msg.inject_keycode.metastate = 0;
|
||||||
msg.inject_keycode.repeat = 0;
|
msg.inject_keycode.repeat = 0;
|
||||||
|
|
||||||
if (actions & ACTION_DOWN) {
|
if (actions & SC_ACTION_DOWN) {
|
||||||
msg.inject_keycode.action = AKEY_EVENT_ACTION_DOWN;
|
msg.inject_keycode.action = AKEY_EVENT_ACTION_DOWN;
|
||||||
if (!controller_push_msg(controller, &msg)) {
|
if (!controller_push_msg(controller, &msg)) {
|
||||||
LOGW("Could not request 'inject %s (DOWN)'", name);
|
LOGW("Could not request 'inject %s (DOWN)'", name);
|
||||||
@ -102,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;
|
msg.inject_keycode.action = AKEY_EVENT_ACTION_UP;
|
||||||
if (!controller_push_msg(controller, &msg)) {
|
if (!controller_push_msg(controller, &msg)) {
|
||||||
LOGW("Could not request 'inject %s (UP)'", name);
|
LOGW("Could not request 'inject %s (UP)'", name);
|
||||||
@ -145,16 +233,6 @@ action_menu(struct controller *controller, int actions) {
|
|||||||
send_keycode(controller, AKEYCODE_MENU, actions, "MENU");
|
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
|
// turn the screen on if it was off, press BACK otherwise
|
||||||
// If the screen is off, it is turned on only on ACTION_DOWN
|
// If the screen is off, it is turned on only on ACTION_DOWN
|
||||||
static void
|
static void
|
||||||
@ -162,7 +240,7 @@ press_back_or_turn_screen_on(struct controller *controller, int actions) {
|
|||||||
struct control_msg msg;
|
struct control_msg msg;
|
||||||
msg.type = CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
|
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;
|
msg.back_or_screen_on.action = AKEY_EVENT_ACTION_DOWN;
|
||||||
if (!controller_push_msg(controller, &msg)) {
|
if (!controller_push_msg(controller, &msg)) {
|
||||||
LOGW("Could not request 'press back or turn screen on'");
|
LOGW("Could not request 'press back or turn screen on'");
|
||||||
@ -170,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;
|
msg.back_or_screen_on.action = AKEY_EVENT_ACTION_UP;
|
||||||
if (!controller_push_msg(controller, &msg)) {
|
if (!controller_push_msg(controller, &msg)) {
|
||||||
LOGW("Could not request 'press back or turn screen on'");
|
LOGW("Could not request 'press back or turn screen on'");
|
||||||
@ -208,35 +286,50 @@ collapse_panels(struct controller *controller) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static bool
|
||||||
set_device_clipboard(struct controller *controller, bool paste) {
|
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();
|
char *text = SDL_GetClipboardText();
|
||||||
if (!text) {
|
if (!text) {
|
||||||
LOGW("Could not get clipboard text: %s", SDL_GetError());
|
LOGW("Could not get clipboard text: %s", SDL_GetError());
|
||||||
return;
|
return false;
|
||||||
}
|
|
||||||
if (!*text) {
|
|
||||||
// empty text
|
|
||||||
SDL_free(text);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *text_dup = strdup(text);
|
char *text_dup = strdup(text);
|
||||||
SDL_free(text);
|
SDL_free(text);
|
||||||
if (!text_dup) {
|
if (!text_dup) {
|
||||||
LOGW("Could not strdup input text");
|
LOGW("Could not strdup input text");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct control_msg msg;
|
struct control_msg msg;
|
||||||
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
|
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
|
||||||
|
msg.set_clipboard.sequence = sequence;
|
||||||
msg.set_clipboard.text = text_dup;
|
msg.set_clipboard.text = text_dup;
|
||||||
msg.set_clipboard.paste = paste;
|
msg.set_clipboard.paste = paste;
|
||||||
|
|
||||||
if (!controller_push_msg(controller, &msg)) {
|
if (!controller_push_msg(controller, &msg)) {
|
||||||
free(text_dup);
|
free(text_dup);
|
||||||
LOGW("Could not request 'set device clipboard'");
|
LOGW("Could not request 'set device clipboard'");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -326,7 +419,11 @@ input_manager_process_text_input(struct input_manager *im,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
im->kp->ops->process_text(im->kp, event);
|
struct sc_text_event evt = {
|
||||||
|
.text = event->text,
|
||||||
|
};
|
||||||
|
|
||||||
|
im->kp->ops->process_text(im->kp, &evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -388,7 +485,7 @@ input_manager_process_key(struct input_manager *im,
|
|||||||
|
|
||||||
// The shortcut modifier is pressed
|
// The shortcut modifier is pressed
|
||||||
if (smod) {
|
if (smod) {
|
||||||
int action = down ? ACTION_DOWN : ACTION_UP;
|
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case SDLK_h:
|
case SDLK_h:
|
||||||
if (control && !shift && !repeat) {
|
if (control && !shift && !repeat) {
|
||||||
@ -447,13 +544,15 @@ input_manager_process_key(struct input_manager *im,
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case SDLK_c:
|
case SDLK_c:
|
||||||
if (control && !shift && !repeat) {
|
if (control && !shift && !repeat && down) {
|
||||||
action_copy(controller, action);
|
get_device_clipboard(controller,
|
||||||
|
GET_CLIPBOARD_COPY_KEY_COPY);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case SDLK_x:
|
case SDLK_x:
|
||||||
if (control && !shift && !repeat) {
|
if (control && !shift && !repeat && down) {
|
||||||
action_cut(controller, action);
|
get_device_clipboard(controller,
|
||||||
|
GET_CLIPBOARD_COPY_KEY_CUT);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case SDLK_v:
|
case SDLK_v:
|
||||||
@ -462,8 +561,10 @@ input_manager_process_key(struct input_manager *im,
|
|||||||
// inject the text as input events
|
// inject the text as input events
|
||||||
clipboard_paste(controller);
|
clipboard_paste(controller);
|
||||||
} else {
|
} else {
|
||||||
// store the text in the device clipboard and paste
|
// store the text in the device clipboard and paste,
|
||||||
set_device_clipboard(controller, true);
|
// without requesting an acknowledgment
|
||||||
|
set_device_clipboard(controller, true,
|
||||||
|
SC_SEQUENCE_INVALID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -512,18 +613,44 @@ input_manager_process_key(struct input_manager *im,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
if (im->legacy_paste) {
|
||||||
// inject the text as input events
|
// inject the text as input events
|
||||||
clipboard_paste(controller);
|
clipboard_paste(controller);
|
||||||
return;
|
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
|
// Synchronize the computer clipboard to the device clipboard before
|
||||||
// sending Ctrl+v, to allow seamless copy-paste.
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
im->kp->ops->process_key(im->kp, event);
|
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),
|
||||||
|
};
|
||||||
|
|
||||||
|
im->kp->ops->process_key(im->kp, &evt, ack_to_wait);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -542,6 +669,17 @@ input_manager_process_mouse_motion(struct input_manager *im,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
im->mp->ops->process_mouse_motion(im->mp, event);
|
||||||
|
|
||||||
if (im->vfinger_down) {
|
if (im->vfinger_down) {
|
||||||
@ -556,6 +694,24 @@ input_manager_process_mouse_motion(struct input_manager *im,
|
|||||||
static void
|
static void
|
||||||
input_manager_process_touch(struct input_manager *im,
|
input_manager_process_touch(struct input_manager *im,
|
||||||
const SDL_TouchFingerEvent *event) {
|
const SDL_TouchFingerEvent *event) {
|
||||||
|
int dw;
|
||||||
|
int dh;
|
||||||
|
SDL_GL_GetDrawableSize(im->screen->window, &dw, &dh);
|
||||||
|
|
||||||
|
// SDL touch event coordinates are normalized in the range [0; 1]
|
||||||
|
int32_t x = event->x * dw;
|
||||||
|
int32_t y = event->y * dh;
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
im->mp->ops->process_touch(im->mp, event);
|
im->mp->ops->process_touch(im->mp, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -571,7 +727,7 @@ input_manager_process_mouse_button(struct input_manager *im,
|
|||||||
|
|
||||||
bool down = event->type == SDL_MOUSEBUTTONDOWN;
|
bool down = event->type == SDL_MOUSEBUTTONDOWN;
|
||||||
if (!im->forward_all_clicks) {
|
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) {
|
if (control && event->button == SDL_BUTTON_X1) {
|
||||||
action_app_switch(im->controller, action);
|
action_app_switch(im->controller, action);
|
||||||
@ -616,6 +772,19 @@ input_manager_process_mouse_button(struct input_manager *im,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t sdl_buttons_state = SDL_GetMouseState(NULL, NULL);
|
||||||
|
|
||||||
|
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);
|
im->mp->ops->process_mouse_button(im->mp, event);
|
||||||
|
|
||||||
// Pinch-to-zoom simulation.
|
// Pinch-to-zoom simulation.
|
||||||
@ -647,6 +816,21 @@ input_manager_process_mouse_button(struct input_manager *im,
|
|||||||
static void
|
static void
|
||||||
input_manager_process_mouse_wheel(struct input_manager *im,
|
input_manager_process_mouse_wheel(struct input_manager *im,
|
||||||
const SDL_MouseWheelEvent *event) {
|
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 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,
|
||||||
|
};
|
||||||
|
|
||||||
im->mp->ops->process_mouse_wheel(im->mp, event);
|
im->mp->ops->process_mouse_wheel(im->mp, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ struct input_manager {
|
|||||||
bool control;
|
bool control;
|
||||||
bool forward_all_clicks;
|
bool forward_all_clicks;
|
||||||
bool legacy_paste;
|
bool legacy_paste;
|
||||||
|
bool clipboard_autosync;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
unsigned data[SC_MAX_SHORTCUT_MODS];
|
unsigned data[SC_MAX_SHORTCUT_MODS];
|
||||||
@ -38,6 +39,8 @@ struct input_manager {
|
|||||||
unsigned key_repeat;
|
unsigned key_repeat;
|
||||||
SDL_Keycode last_keycode;
|
SDL_Keycode last_keycode;
|
||||||
uint16_t last_mod;
|
uint16_t last_mod;
|
||||||
|
|
||||||
|
uint64_t next_sequence; // used for request acknowledgements
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -1,108 +1,198 @@
|
|||||||
#include "keyboard_inject.h"
|
#include "keyboard_inject.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_events.h>
|
|
||||||
|
|
||||||
#include "android/input.h"
|
#include "android/input.h"
|
||||||
#include "control_msg.h"
|
#include "control_msg.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
#include "input_events.h"
|
||||||
|
#include "util/intmap.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
/** Downcast key processor to sc_keyboard_inject */
|
/** Downcast key processor to sc_keyboard_inject */
|
||||||
#define DOWNCAST(KP) \
|
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
|
||||||
container_of(KP, struct sc_keyboard_inject, key_processor)
|
|
||||||
|
|
||||||
#define MAP(FROM, TO) case FROM: *to = TO; return true
|
|
||||||
#define FAIL default: return false
|
|
||||||
static bool
|
static bool
|
||||||
convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to) {
|
convert_keycode_action(enum sc_action from, enum android_keyevent_action *to) {
|
||||||
switch (from) {
|
static const struct sc_intmap_entry actions[] = {
|
||||||
MAP(SDL_KEYDOWN, AKEY_EVENT_ACTION_DOWN);
|
{SC_ACTION_DOWN, AKEY_EVENT_ACTION_DOWN},
|
||||||
MAP(SDL_KEYUP, AKEY_EVENT_ACTION_UP);
|
{SC_ACTION_UP, AKEY_EVENT_ACTION_UP},
|
||||||
FAIL;
|
};
|
||||||
|
|
||||||
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||||
|
if (entry) {
|
||||||
|
*to = entry->value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod,
|
convert_keycode(enum sc_keycode from, enum android_keycode *to, uint16_t mod,
|
||||||
bool prefer_text) {
|
enum sc_key_inject_mode key_inject_mode) {
|
||||||
switch (from) {
|
// Navigation keys and ENTER.
|
||||||
MAP(SDLK_RETURN, AKEYCODE_ENTER);
|
// Used in all modes.
|
||||||
MAP(SDLK_KP_ENTER, AKEYCODE_NUMPAD_ENTER);
|
static const struct sc_intmap_entry special_keys[] = {
|
||||||
MAP(SDLK_ESCAPE, AKEYCODE_ESCAPE);
|
{SC_KEYCODE_RETURN, AKEYCODE_ENTER},
|
||||||
MAP(SDLK_BACKSPACE, AKEYCODE_DEL);
|
{SC_KEYCODE_KP_ENTER, AKEYCODE_NUMPAD_ENTER},
|
||||||
MAP(SDLK_TAB, AKEYCODE_TAB);
|
{SC_KEYCODE_ESCAPE, AKEYCODE_ESCAPE},
|
||||||
MAP(SDLK_PAGEUP, AKEYCODE_PAGE_UP);
|
{SC_KEYCODE_BACKSPACE, AKEYCODE_DEL},
|
||||||
MAP(SDLK_DELETE, AKEYCODE_FORWARD_DEL);
|
{SC_KEYCODE_TAB, AKEYCODE_TAB},
|
||||||
MAP(SDLK_HOME, AKEYCODE_MOVE_HOME);
|
{SC_KEYCODE_PAGEUP, AKEYCODE_PAGE_UP},
|
||||||
MAP(SDLK_END, AKEYCODE_MOVE_END);
|
{SC_KEYCODE_DELETE, AKEYCODE_FORWARD_DEL},
|
||||||
MAP(SDLK_PAGEDOWN, AKEYCODE_PAGE_DOWN);
|
{SC_KEYCODE_HOME, AKEYCODE_MOVE_HOME},
|
||||||
MAP(SDLK_RIGHT, AKEYCODE_DPAD_RIGHT);
|
{SC_KEYCODE_END, AKEYCODE_MOVE_END},
|
||||||
MAP(SDLK_LEFT, AKEYCODE_DPAD_LEFT);
|
{SC_KEYCODE_PAGEDOWN, AKEYCODE_PAGE_DOWN},
|
||||||
MAP(SDLK_DOWN, AKEYCODE_DPAD_DOWN);
|
{SC_KEYCODE_RIGHT, AKEYCODE_DPAD_RIGHT},
|
||||||
MAP(SDLK_UP, AKEYCODE_DPAD_UP);
|
{SC_KEYCODE_LEFT, AKEYCODE_DPAD_LEFT},
|
||||||
MAP(SDLK_LCTRL, AKEYCODE_CTRL_LEFT);
|
{SC_KEYCODE_DOWN, AKEYCODE_DPAD_DOWN},
|
||||||
MAP(SDLK_RCTRL, AKEYCODE_CTRL_RIGHT);
|
{SC_KEYCODE_UP, AKEYCODE_DPAD_UP},
|
||||||
MAP(SDLK_LSHIFT, AKEYCODE_SHIFT_LEFT);
|
{SC_KEYCODE_LCTRL, AKEYCODE_CTRL_LEFT},
|
||||||
MAP(SDLK_RSHIFT, AKEYCODE_SHIFT_RIGHT);
|
{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 & (KMOD_NUM | KMOD_SHIFT))) {
|
if (!(mod & (SC_MOD_NUM | SC_MOD_LSHIFT | SC_MOD_RSHIFT))) {
|
||||||
// Handle Numpad events when Num Lock is disabled
|
// Handle Numpad events when Num Lock is disabled
|
||||||
// If SHIFT is pressed, a text event will be sent instead
|
// If SHIFT is pressed, a text event will be sent instead
|
||||||
switch(from) {
|
entry = SC_INTMAP_FIND_ENTRY(kp_nav_keys, from);
|
||||||
MAP(SDLK_KP_0, AKEYCODE_INSERT);
|
if (entry) {
|
||||||
MAP(SDLK_KP_1, AKEYCODE_MOVE_END);
|
*to = entry->value;
|
||||||
MAP(SDLK_KP_2, AKEYCODE_DPAD_DOWN);
|
return true;
|
||||||
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)) {
|
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)
|
// do not forward alpha and space key events (unless Ctrl is pressed)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mod & (KMOD_LALT | KMOD_RALT | KMOD_LGUI | KMOD_RGUI)) {
|
if (mod & (SC_MOD_LALT | SC_MOD_RALT | SC_MOD_LGUI | SC_MOD_RGUI)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if ALT and META are not pressed, also handle letters and space
|
// if ALT and META are not pressed, also handle letters and space
|
||||||
switch (from) {
|
entry = SC_INTMAP_FIND_ENTRY(alphaspace_keys, from);
|
||||||
MAP(SDLK_a, AKEYCODE_A);
|
if (entry) {
|
||||||
MAP(SDLK_b, AKEYCODE_B);
|
*to = entry->value;
|
||||||
MAP(SDLK_c, AKEYCODE_C);
|
return true;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
static enum android_metastate
|
||||||
@ -125,70 +215,72 @@ autocomplete_metastate(enum android_metastate metastate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum android_metastate
|
static enum android_metastate
|
||||||
convert_meta_state(SDL_Keymod mod) {
|
convert_meta_state(uint16_t mod) {
|
||||||
enum android_metastate metastate = 0;
|
enum android_metastate metastate = 0;
|
||||||
if (mod & KMOD_LSHIFT) {
|
if (mod & SC_MOD_LSHIFT) {
|
||||||
metastate |= AMETA_SHIFT_LEFT_ON;
|
metastate |= AMETA_SHIFT_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RSHIFT) {
|
if (mod & SC_MOD_RSHIFT) {
|
||||||
metastate |= AMETA_SHIFT_RIGHT_ON;
|
metastate |= AMETA_SHIFT_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LCTRL) {
|
if (mod & SC_MOD_LCTRL) {
|
||||||
metastate |= AMETA_CTRL_LEFT_ON;
|
metastate |= AMETA_CTRL_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RCTRL) {
|
if (mod & SC_MOD_RCTRL) {
|
||||||
metastate |= AMETA_CTRL_RIGHT_ON;
|
metastate |= AMETA_CTRL_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LALT) {
|
if (mod & SC_MOD_LALT) {
|
||||||
metastate |= AMETA_ALT_LEFT_ON;
|
metastate |= AMETA_ALT_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RALT) {
|
if (mod & SC_MOD_RALT) {
|
||||||
metastate |= AMETA_ALT_RIGHT_ON;
|
metastate |= AMETA_ALT_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LGUI) { // Windows key
|
if (mod & SC_MOD_LGUI) { // Windows key
|
||||||
metastate |= AMETA_META_LEFT_ON;
|
metastate |= AMETA_META_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RGUI) { // Windows key
|
if (mod & SC_MOD_RGUI) { // Windows key
|
||||||
metastate |= AMETA_META_RIGHT_ON;
|
metastate |= AMETA_META_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_NUM) {
|
if (mod & SC_MOD_NUM) {
|
||||||
metastate |= AMETA_NUM_LOCK_ON;
|
metastate |= AMETA_NUM_LOCK_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_CAPS) {
|
if (mod & SC_MOD_CAPS) {
|
||||||
metastate |= AMETA_CAPS_LOCK_ON;
|
metastate |= AMETA_CAPS_LOCK_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_MODE) { // Alt Gr
|
|
||||||
// no mapping?
|
|
||||||
}
|
|
||||||
|
|
||||||
// fill the dependent fields
|
// fill the dependent fields
|
||||||
return autocomplete_metastate(metastate);
|
return autocomplete_metastate(metastate);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to,
|
convert_input_key(const struct sc_key_event *event, struct control_msg *msg,
|
||||||
bool prefer_text, uint32_t repeat) {
|
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
|
||||||
to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
||||||
|
|
||||||
if (!convert_keycode_action(from->type, &to->inject_keycode.action)) {
|
if (!convert_keycode_action(event->action, &msg->inject_keycode.action)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t mod = from->keysym.mod;
|
if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
|
||||||
if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod,
|
event->mods_state, key_inject_mode)) {
|
||||||
prefer_text)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
to->inject_keycode.repeat = repeat;
|
msg->inject_keycode.repeat = repeat;
|
||||||
to->inject_keycode.metastate = convert_meta_state(mod);
|
msg->inject_keycode.metastate = convert_meta_state(event->mods_state);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
const SDL_KeyboardEvent *event) {
|
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);
|
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
||||||
|
|
||||||
if (event->repeat) {
|
if (event->repeat) {
|
||||||
@ -201,7 +293,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct control_msg msg;
|
struct control_msg msg;
|
||||||
if (convert_input_key(event, &msg, ki->prefer_text, ki->repeat)) {
|
if (convert_input_key(event, &msg, ki->key_inject_mode, ki->repeat)) {
|
||||||
if (!controller_push_msg(ki->controller, &msg)) {
|
if (!controller_push_msg(ki->controller, &msg)) {
|
||||||
LOGW("Could not request 'inject keycode'");
|
LOGW("Could not request 'inject keycode'");
|
||||||
}
|
}
|
||||||
@ -210,14 +302,19 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event) {
|
const struct sc_text_event *event) {
|
||||||
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
||||||
|
|
||||||
if (!ki->prefer_text) {
|
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];
|
char c = event->text[0];
|
||||||
if (isalpha(c) || c == ' ') {
|
if (isalpha(c) || c == ' ') {
|
||||||
assert(event->text[1] == '\0');
|
assert(event->text[1] == '\0');
|
||||||
// letters and space are handled as raw key event
|
// Letters and space are handled as raw key events
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -240,7 +337,7 @@ sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
|
|||||||
struct controller *controller,
|
struct controller *controller,
|
||||||
const struct scrcpy_options *options) {
|
const struct scrcpy_options *options) {
|
||||||
ki->controller = controller;
|
ki->controller = controller;
|
||||||
ki->prefer_text = options->prefer_text;
|
ki->key_inject_mode = options->key_inject_mode;
|
||||||
ki->forward_key_repeat = options->forward_key_repeat;
|
ki->forward_key_repeat = options->forward_key_repeat;
|
||||||
|
|
||||||
ki->repeat = 0;
|
ki->repeat = 0;
|
||||||
@ -250,5 +347,7 @@ sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
|
|||||||
.process_text = sc_key_processor_process_text,
|
.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;
|
ki->key_processor.ops = &ops;
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ struct sc_keyboard_inject {
|
|||||||
// number of repetitions. This variable keeps track of the count.
|
// number of repetitions. This variable keeps track of the count.
|
||||||
unsigned repeat;
|
unsigned repeat;
|
||||||
|
|
||||||
bool prefer_text;
|
enum sc_key_inject_mode key_inject_mode;
|
||||||
bool forward_key_repeat;
|
bool forward_key_repeat;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "android/input.h"
|
#include "android/input.h"
|
||||||
#include "control_msg.h"
|
#include "control_msg.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
#include "util/intmap.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
/** Downcast mouse processor to sc_mouse_inject */
|
/** Downcast mouse processor to sc_mouse_inject */
|
||||||
@ -32,25 +33,37 @@ convert_mouse_buttons(uint32_t state) {
|
|||||||
return buttons;
|
return buttons;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAP(FROM, TO) case FROM: *to = TO; return true
|
|
||||||
#define FAIL default: return false
|
|
||||||
static bool
|
static bool
|
||||||
convert_mouse_action(SDL_EventType from, enum android_motionevent_action *to) {
|
convert_mouse_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||||
switch (from) {
|
static const struct sc_intmap_entry actions[] = {
|
||||||
MAP(SDL_MOUSEBUTTONDOWN, AMOTION_EVENT_ACTION_DOWN);
|
{SDL_MOUSEBUTTONDOWN, AMOTION_EVENT_ACTION_DOWN},
|
||||||
MAP(SDL_MOUSEBUTTONUP, AMOTION_EVENT_ACTION_UP);
|
{SDL_MOUSEBUTTONUP, AMOTION_EVENT_ACTION_UP},
|
||||||
FAIL;
|
};
|
||||||
|
|
||||||
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||||
|
if (entry) {
|
||||||
|
*to = entry->value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
|
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||||
switch (from) {
|
static const struct sc_intmap_entry actions[] = {
|
||||||
MAP(SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE);
|
{SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE},
|
||||||
MAP(SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN);
|
{SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN},
|
||||||
MAP(SDL_FINGERUP, AMOTION_EVENT_ACTION_UP);
|
{SDL_FINGERUP, AMOTION_EVENT_ACTION_UP},
|
||||||
FAIL;
|
};
|
||||||
|
|
||||||
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||||
|
if (entry) {
|
||||||
|
*to = entry->value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
@ -19,8 +19,10 @@ const struct scrcpy_options scrcpy_options_default = {
|
|||||||
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST,
|
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST,
|
||||||
.last = DEFAULT_LOCAL_PORT_RANGE_LAST,
|
.last = DEFAULT_LOCAL_PORT_RANGE_LAST,
|
||||||
},
|
},
|
||||||
|
.tunnel_host = 0,
|
||||||
|
.tunnel_port = 0,
|
||||||
.shortcut_mods = {
|
.shortcut_mods = {
|
||||||
.data = {SC_MOD_LALT, SC_MOD_LSUPER},
|
.data = {SC_SHORTCUT_MOD_LALT, SC_SHORTCUT_MOD_LSUPER},
|
||||||
.count = 2,
|
.count = 2,
|
||||||
},
|
},
|
||||||
.max_size = 0,
|
.max_size = 0,
|
||||||
@ -41,7 +43,7 @@ const struct scrcpy_options scrcpy_options_default = {
|
|||||||
.control = true,
|
.control = true,
|
||||||
.display = true,
|
.display = true,
|
||||||
.turn_screen_off = false,
|
.turn_screen_off = false,
|
||||||
.prefer_text = false,
|
.key_inject_mode = SC_KEY_INJECT_MODE_MIXED,
|
||||||
.window_borderless = false,
|
.window_borderless = false,
|
||||||
.mipmaps = true,
|
.mipmaps = true,
|
||||||
.stay_awake = false,
|
.stay_awake = false,
|
||||||
@ -51,4 +53,7 @@ const struct scrcpy_options scrcpy_options_default = {
|
|||||||
.forward_all_clicks = false,
|
.forward_all_clicks = false,
|
||||||
.legacy_paste = false,
|
.legacy_paste = false,
|
||||||
.power_off_on_close = false,
|
.power_off_on_close = false,
|
||||||
|
.clipboard_autosync = true,
|
||||||
|
.tcpip = false,
|
||||||
|
.tcpip_dst = NULL,
|
||||||
};
|
};
|
||||||
|
@ -38,15 +38,29 @@ enum sc_keyboard_input_mode {
|
|||||||
SC_KEYBOARD_INPUT_MODE_HID,
|
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
|
#define SC_MAX_SHORTCUT_MODS 8
|
||||||
|
|
||||||
enum sc_shortcut_mod {
|
enum sc_shortcut_mod {
|
||||||
SC_MOD_LCTRL = 1 << 0,
|
SC_SHORTCUT_MOD_LCTRL = 1 << 0,
|
||||||
SC_MOD_RCTRL = 1 << 1,
|
SC_SHORTCUT_MOD_RCTRL = 1 << 1,
|
||||||
SC_MOD_LALT = 1 << 2,
|
SC_SHORTCUT_MOD_LALT = 1 << 2,
|
||||||
SC_MOD_RALT = 1 << 3,
|
SC_SHORTCUT_MOD_RALT = 1 << 3,
|
||||||
SC_MOD_LSUPER = 1 << 4,
|
SC_SHORTCUT_MOD_LSUPER = 1 << 4,
|
||||||
SC_MOD_RSUPER = 1 << 5,
|
SC_SHORTCUT_MOD_RSUPER = 1 << 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sc_shortcut_mods {
|
struct sc_shortcut_mods {
|
||||||
@ -77,6 +91,8 @@ struct scrcpy_options {
|
|||||||
enum sc_record_format record_format;
|
enum sc_record_format record_format;
|
||||||
enum sc_keyboard_input_mode keyboard_input_mode;
|
enum sc_keyboard_input_mode keyboard_input_mode;
|
||||||
struct sc_port_range port_range;
|
struct sc_port_range port_range;
|
||||||
|
uint32_t tunnel_host;
|
||||||
|
uint16_t tunnel_port;
|
||||||
struct sc_shortcut_mods shortcut_mods;
|
struct sc_shortcut_mods shortcut_mods;
|
||||||
uint16_t max_size;
|
uint16_t max_size;
|
||||||
uint32_t bit_rate;
|
uint32_t bit_rate;
|
||||||
@ -96,7 +112,7 @@ struct scrcpy_options {
|
|||||||
bool control;
|
bool control;
|
||||||
bool display;
|
bool display;
|
||||||
bool turn_screen_off;
|
bool turn_screen_off;
|
||||||
bool prefer_text;
|
enum sc_key_inject_mode key_inject_mode;
|
||||||
bool window_borderless;
|
bool window_borderless;
|
||||||
bool mipmaps;
|
bool mipmaps;
|
||||||
bool stay_awake;
|
bool stay_awake;
|
||||||
@ -106,6 +122,9 @@ struct scrcpy_options {
|
|||||||
bool forward_all_clicks;
|
bool forward_all_clicks;
|
||||||
bool legacy_paste;
|
bool legacy_paste;
|
||||||
bool power_off_on_close;
|
bool power_off_on_close;
|
||||||
|
bool clipboard_autosync;
|
||||||
|
bool tcpip;
|
||||||
|
const char *tcpip_dst;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const struct scrcpy_options scrcpy_options_default;
|
extern const struct scrcpy_options scrcpy_options_default;
|
||||||
|
@ -7,12 +7,16 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
receiver_init(struct receiver *receiver, sc_socket control_socket) {
|
receiver_init(struct receiver *receiver, sc_socket control_socket,
|
||||||
|
struct sc_acksync *acksync) {
|
||||||
bool ok = sc_mutex_init(&receiver->mutex);
|
bool ok = sc_mutex_init(&receiver->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
receiver->control_socket = control_socket;
|
receiver->control_socket = control_socket;
|
||||||
|
receiver->acksync = acksync;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,7 +26,7 @@ receiver_destroy(struct receiver *receiver) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
process_msg(struct device_msg *msg) {
|
process_msg(struct receiver *receiver, struct device_msg *msg) {
|
||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
||||||
char *current = SDL_GetClipboardText();
|
char *current = SDL_GetClipboardText();
|
||||||
@ -37,11 +41,17 @@ process_msg(struct device_msg *msg) {
|
|||||||
SDL_SetClipboardText(msg->clipboard.text);
|
SDL_SetClipboardText(msg->clipboard.text);
|
||||||
break;
|
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
|
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;
|
size_t head = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
struct device_msg msg;
|
struct device_msg msg;
|
||||||
@ -53,7 +63,7 @@ process_msgs(const unsigned char *buf, size_t len) {
|
|||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
process_msg(&msg);
|
process_msg(receiver, &msg);
|
||||||
device_msg_destroy(&msg);
|
device_msg_destroy(&msg);
|
||||||
|
|
||||||
head += r;
|
head += r;
|
||||||
@ -81,7 +91,7 @@ run_receiver(void *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
head += r;
|
head += r;
|
||||||
ssize_t consumed = process_msgs(buf, head);
|
ssize_t consumed = process_msgs(receiver, buf, head);
|
||||||
if (consumed == -1) {
|
if (consumed == -1) {
|
||||||
// an error occurred
|
// an error occurred
|
||||||
break;
|
break;
|
||||||
@ -101,8 +111,8 @@ bool
|
|||||||
receiver_start(struct receiver *receiver) {
|
receiver_start(struct receiver *receiver) {
|
||||||
LOGD("Starting receiver thread");
|
LOGD("Starting receiver thread");
|
||||||
|
|
||||||
bool ok = sc_thread_create(&receiver->thread, run_receiver, "receiver",
|
bool ok = sc_thread_create(&receiver->thread, run_receiver,
|
||||||
receiver);
|
"scrcpy-receiver", receiver);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start receiver thread");
|
LOGC("Could not start receiver thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "util/acksync.h"
|
||||||
#include "util/net.h"
|
#include "util/net.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
|
|
||||||
@ -14,10 +15,13 @@ struct receiver {
|
|||||||
sc_socket control_socket;
|
sc_socket control_socket;
|
||||||
sc_thread thread;
|
sc_thread thread;
|
||||||
sc_mutex mutex;
|
sc_mutex mutex;
|
||||||
|
|
||||||
|
struct sc_acksync *acksync;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
receiver_init(struct receiver *receiver, sc_socket control_socket);
|
receiver_init(struct receiver *receiver, sc_socket control_socket,
|
||||||
|
struct sc_acksync *acksync);
|
||||||
|
|
||||||
void
|
void
|
||||||
receiver_destroy(struct receiver *receiver);
|
receiver_destroy(struct receiver *receiver);
|
||||||
|
@ -34,11 +34,13 @@ static struct record_packet *
|
|||||||
record_packet_new(const AVPacket *packet) {
|
record_packet_new(const AVPacket *packet) {
|
||||||
struct record_packet *rec = malloc(sizeof(*rec));
|
struct record_packet *rec = malloc(sizeof(*rec));
|
||||||
if (!rec) {
|
if (!rec) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
rec->packet = av_packet_alloc();
|
rec->packet = av_packet_alloc();
|
||||||
if (!rec->packet) {
|
if (!rec->packet) {
|
||||||
|
LOG_OOM();
|
||||||
free(rec);
|
free(rec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -81,7 +83,7 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
|
|
||||||
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
||||||
if (!extradata) {
|
if (!extradata) {
|
||||||
LOGC("Could not allocate extradata");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,13 +230,11 @@ static bool
|
|||||||
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
||||||
bool ok = sc_mutex_init(&recorder->mutex);
|
bool ok = sc_mutex_init(&recorder->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create mutex");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_cond_init(&recorder->queue_cond);
|
ok = sc_cond_init(&recorder->queue_cond);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create cond");
|
|
||||||
goto error_mutex_destroy;
|
goto error_mutex_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
|||||||
|
|
||||||
recorder->ctx = avformat_alloc_context();
|
recorder->ctx = avformat_alloc_context();
|
||||||
if (!recorder->ctx) {
|
if (!recorder->ctx) {
|
||||||
LOGE("Could not allocate output context");
|
LOG_OOM();
|
||||||
goto error_cond_destroy;
|
goto error_cond_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,8 +287,8 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOGD("Starting recorder thread");
|
LOGD("Starting recorder thread");
|
||||||
ok = sc_thread_create(&recorder->thread, run_recorder, "recorder",
|
ok = sc_thread_create(&recorder->thread, run_recorder, "scrcpy-recorder",
|
||||||
recorder);
|
recorder);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start recorder thread");
|
LOGC("Could not start recorder thread");
|
||||||
goto error_avio_close;
|
goto error_avio_close;
|
||||||
@ -338,7 +338,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
|
|
||||||
struct record_packet *rec = record_packet_new(packet);
|
struct record_packet *rec = record_packet_new(packet);
|
||||||
if (!rec) {
|
if (!rec) {
|
||||||
LOGC("Could not allocate record packet");
|
LOG_OOM();
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -375,7 +375,7 @@ recorder_init(struct recorder *recorder,
|
|||||||
struct sc_size declared_frame_size) {
|
struct sc_size declared_frame_size) {
|
||||||
recorder->filename = strdup(filename);
|
recorder->filename = strdup(filename);
|
||||||
if (!recorder->filename) {
|
if (!recorder->filename) {
|
||||||
LOGE("Could not strdup filename");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
#include "util/acksync.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/net.h"
|
#include "util/net.h"
|
||||||
#ifdef HAVE_V4L2
|
#ifdef HAVE_V4L2
|
||||||
@ -46,6 +47,8 @@ struct scrcpy {
|
|||||||
struct file_handler file_handler;
|
struct file_handler file_handler;
|
||||||
#ifdef HAVE_AOA_HID
|
#ifdef HAVE_AOA_HID
|
||||||
struct sc_aoa aoa;
|
struct sc_aoa aoa;
|
||||||
|
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
|
||||||
|
struct sc_acksync acksync;
|
||||||
#endif
|
#endif
|
||||||
union {
|
union {
|
||||||
struct sc_keyboard_inject keyboard_inject;
|
struct sc_keyboard_inject keyboard_inject;
|
||||||
@ -91,12 +94,10 @@ sdl_set_hints(const char *render_driver) {
|
|||||||
LOGW("Could not enable linear filtering");
|
LOGW("Could not enable linear filtering");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
|
||||||
// Handle a click to gain focus as any other click
|
// Handle a click to gain focus as any other click
|
||||||
if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
|
if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
|
||||||
LOGW("Could not enable mouse focus clickthrough");
|
LOGW("Could not enable mouse focus clickthrough");
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
#ifdef SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||||
// Disable synthetic mouse events from touch events
|
// Disable synthetic mouse events from touch events
|
||||||
@ -268,7 +269,7 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
|||||||
size_t fmt_len = strlen(fmt);
|
size_t fmt_len = strlen(fmt);
|
||||||
char *local_fmt = malloc(fmt_len + 10);
|
char *local_fmt = malloc(fmt_len + 10);
|
||||||
if (!local_fmt) {
|
if (!local_fmt) {
|
||||||
LOGC("Could not allocate string");
|
LOG_OOM();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0'
|
memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0'
|
||||||
@ -340,11 +341,15 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
bool controller_started = false;
|
bool controller_started = false;
|
||||||
bool screen_initialized = false;
|
bool screen_initialized = false;
|
||||||
|
|
||||||
|
struct sc_acksync *acksync = NULL;
|
||||||
|
|
||||||
struct sc_server_params params = {
|
struct sc_server_params params = {
|
||||||
.serial = options->serial,
|
.serial = options->serial,
|
||||||
.log_level = options->log_level,
|
.log_level = options->log_level,
|
||||||
.crop = options->crop,
|
.crop = options->crop,
|
||||||
.port_range = options->port_range,
|
.port_range = options->port_range,
|
||||||
|
.tunnel_host = options->tunnel_host,
|
||||||
|
.tunnel_port = options->tunnel_port,
|
||||||
.max_size = options->max_size,
|
.max_size = options->max_size,
|
||||||
.bit_rate = options->bit_rate,
|
.bit_rate = options->bit_rate,
|
||||||
.max_fps = options->max_fps,
|
.max_fps = options->max_fps,
|
||||||
@ -357,6 +362,9 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
.encoder_name = options->encoder_name,
|
.encoder_name = options->encoder_name,
|
||||||
.force_adb_forward = options->force_adb_forward,
|
.force_adb_forward = options->force_adb_forward,
|
||||||
.power_off_on_close = options->power_off_on_close,
|
.power_off_on_close = options->power_off_on_close,
|
||||||
|
.clipboard_autosync = options->clipboard_autosync,
|
||||||
|
.tcpip = options->tcpip,
|
||||||
|
.tcpip_dst = options->tcpip_dst,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct sc_server_callbacks cbs = {
|
static const struct sc_server_callbacks cbs = {
|
||||||
@ -443,7 +451,18 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options->control) {
|
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;
|
goto end;
|
||||||
}
|
}
|
||||||
controller_initialized = true;
|
controller_initialized = true;
|
||||||
@ -519,7 +538,7 @@ scrcpy(struct scrcpy_options *options) {
|
|||||||
#ifdef HAVE_AOA_HID
|
#ifdef HAVE_AOA_HID
|
||||||
bool aoa_hid_ok = false;
|
bool aoa_hid_ok = false;
|
||||||
|
|
||||||
bool ok = sc_aoa_init(&s->aoa, serial);
|
bool ok = sc_aoa_init(&s->aoa, serial, acksync);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto aoa_hid_end;
|
goto aoa_hid_end;
|
||||||
}
|
}
|
||||||
@ -584,6 +603,9 @@ end:
|
|||||||
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
||||||
sc_aoa_stop(&s->aoa);
|
sc_aoa_stop(&s->aoa);
|
||||||
}
|
}
|
||||||
|
if (acksync) {
|
||||||
|
sc_acksync_destroy(acksync);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (controller_started) {
|
if (controller_started) {
|
||||||
controller_stop(&s->controller);
|
controller_stop(&s->controller);
|
||||||
|
@ -64,12 +64,7 @@ set_window_size(struct screen *screen, struct sc_size new_size) {
|
|||||||
static bool
|
static bool
|
||||||
get_preferred_display_bounds(struct sc_size *bounds) {
|
get_preferred_display_bounds(struct sc_size *bounds) {
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
#ifdef SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS
|
if (SDL_GetDisplayUsableBounds(0, &rect)) {
|
||||||
# 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)) {
|
|
||||||
LOGW("Could not get display usable bounds: %s", SDL_GetError());
|
LOGW("Could not get display usable bounds: %s", SDL_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -95,7 +90,8 @@ is_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
|||||||
// - it keeps the aspect ratio
|
// - it keeps the aspect ratio
|
||||||
// - it scales down to make it fit in the display_size
|
// - it scales down to make it fit in the display_size
|
||||||
static struct sc_size
|
static struct sc_size
|
||||||
get_optimal_size(struct sc_size current_size, struct sc_size content_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) {
|
if (content_size.width == 0 || content_size.height == 0) {
|
||||||
// avoid division by 0
|
// avoid division by 0
|
||||||
return current_size;
|
return current_size;
|
||||||
@ -104,10 +100,10 @@ get_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
|||||||
struct sc_size window_size;
|
struct sc_size window_size;
|
||||||
|
|
||||||
struct sc_size display_size;
|
struct sc_size display_size;
|
||||||
if (!get_preferred_display_bounds(&display_size)) {
|
if (!within_display_bounds ||
|
||||||
// could not get display bounds, do not constraint the size
|
!get_preferred_display_bounds(&display_size)) {
|
||||||
window_size.width = current_size.width;
|
// do not constraint the size
|
||||||
window_size.height = current_size.height;
|
window_size = current_size;
|
||||||
} else {
|
} else {
|
||||||
window_size.width = MIN(current_size.width, display_size.width);
|
window_size.width = MIN(current_size.width, display_size.width);
|
||||||
window_size.height = MIN(current_size.height, display_size.height);
|
window_size.height = MIN(current_size.height, display_size.height);
|
||||||
@ -140,7 +136,7 @@ get_initial_optimal_size(struct sc_size content_size, uint16_t req_width,
|
|||||||
uint16_t req_height) {
|
uint16_t req_height) {
|
||||||
struct sc_size window_size;
|
struct sc_size window_size;
|
||||||
if (!req_width && !req_height) {
|
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 {
|
} else {
|
||||||
if (req_width) {
|
if (req_width) {
|
||||||
window_size.width = req_width;
|
window_size.width = req_width;
|
||||||
@ -366,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,
|
bool ok = sc_video_buffer_init(&screen->vb, params->buffering_time, &cbs,
|
||||||
screen);
|
screen);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not initialize video buffer");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_video_buffer_start(&screen->vb);
|
ok = sc_video_buffer_start(&screen->vb);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not start video_buffer");
|
|
||||||
goto error_destroy_video_buffer;
|
goto error_destroy_video_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fps_counter_init(&screen->fps_counter)) {
|
if (!fps_counter_init(&screen->fps_counter)) {
|
||||||
LOGE("Could not initialize FPS counter");
|
|
||||||
goto error_stop_and_join_video_buffer;
|
goto error_stop_and_join_video_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -397,12 +390,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
|||||||
| SDL_WINDOW_RESIZABLE
|
| SDL_WINDOW_RESIZABLE
|
||||||
| SDL_WINDOW_ALLOW_HIGHDPI;
|
| SDL_WINDOW_ALLOW_HIGHDPI;
|
||||||
if (params->always_on_top) {
|
if (params->always_on_top) {
|
||||||
#ifdef SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP
|
|
||||||
window_flags |= SDL_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) {
|
if (params->window_borderless) {
|
||||||
window_flags |= SDL_WINDOW_BORDERLESS;
|
window_flags |= SDL_WINDOW_BORDERLESS;
|
||||||
@ -478,7 +466,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
|||||||
|
|
||||||
screen->frame = av_frame_alloc();
|
screen->frame = av_frame_alloc();
|
||||||
if (!screen->frame) {
|
if (!screen->frame) {
|
||||||
LOGC("Could not create screen frame");
|
LOG_OOM();
|
||||||
goto error_destroy_texture;
|
goto error_destroy_texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -573,7 +561,7 @@ resize_for_content(struct screen *screen, struct sc_size old_content_size,
|
|||||||
.height = (uint32_t) window_size.height * new_content_size.height
|
.height = (uint32_t) window_size.height * new_content_size.height
|
||||||
/ old_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);
|
set_window_size(screen, target_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -708,7 +696,7 @@ screen_resize_to_fit(struct screen *screen) {
|
|||||||
struct sc_size window_size = get_window_size(screen);
|
struct sc_size window_size = get_window_size(screen);
|
||||||
|
|
||||||
struct sc_size optimal_size =
|
struct sc_size optimal_size =
|
||||||
get_optimal_size(window_size, screen->content_size);
|
get_optimal_size(window_size, screen->content_size, false);
|
||||||
|
|
||||||
// Center the window related to the device screen
|
// Center the window related to the device screen
|
||||||
assert(optimal_size.width <= window_size.width);
|
assert(optimal_size.width <= window_size.width);
|
||||||
|
437
app/src/server.c
437
app/src/server.c
@ -34,7 +34,7 @@ get_server_path(void) {
|
|||||||
char *server_path = strdup(server_path_env);
|
char *server_path = strdup(server_path_env);
|
||||||
#endif
|
#endif
|
||||||
if (!server_path) {
|
if (!server_path) {
|
||||||
LOGE("Could not allocate memory");
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
|
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
|
||||||
@ -45,7 +45,7 @@ get_server_path(void) {
|
|||||||
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
|
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
|
||||||
char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
|
char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
|
||||||
if (!server_path) {
|
if (!server_path) {
|
||||||
LOGE("Could not allocate memory");
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -69,6 +69,7 @@ sc_server_params_destroy(struct sc_server_params *params) {
|
|||||||
free((char *) params->crop);
|
free((char *) params->crop);
|
||||||
free((char *) params->codec_options);
|
free((char *) params->codec_options);
|
||||||
free((char *) params->encoder_name);
|
free((char *) params->encoder_name);
|
||||||
|
free((char *) params->tcpip_dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -92,6 +93,7 @@ sc_server_params_copy(struct sc_server_params *dst,
|
|||||||
COPY(crop);
|
COPY(crop);
|
||||||
COPY(codec_options);
|
COPY(codec_options);
|
||||||
COPY(encoder_name);
|
COPY(encoder_name);
|
||||||
|
COPY(tcpip_dst);
|
||||||
#undef COPY
|
#undef COPY
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -112,7 +114,7 @@ push_server(struct sc_intr *intr, const char *serial) {
|
|||||||
free(server_path);
|
free(server_path);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH);
|
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);
|
||||||
free(server_path);
|
free(server_path);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@ -136,28 +138,34 @@ log_level_to_server_string(enum sc_log_level level) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_server_sleep(struct sc_server *server, sc_tick deadline) {
|
||||||
|
sc_mutex_lock(&server->mutex);
|
||||||
|
bool timed_out = false;
|
||||||
|
while (!server->stopped && !timed_out) {
|
||||||
|
timed_out = !sc_cond_timedwait(&server->cond_stopped,
|
||||||
|
&server->mutex, deadline);
|
||||||
|
}
|
||||||
|
bool stopped = server->stopped;
|
||||||
|
sc_mutex_unlock(&server->mutex);
|
||||||
|
|
||||||
|
return !stopped;
|
||||||
|
}
|
||||||
|
|
||||||
static sc_pid
|
static sc_pid
|
||||||
execute_server(struct sc_server *server,
|
execute_server(struct sc_server *server,
|
||||||
const struct sc_server_params *params) {
|
const struct sc_server_params *params) {
|
||||||
const char *serial = server->params.serial;
|
sc_pid pid = SC_PROCESS_NONE;
|
||||||
|
|
||||||
|
const char *cmd[128];
|
||||||
|
unsigned count = 0;
|
||||||
|
cmd[count++] = "shell";
|
||||||
|
cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
|
||||||
|
cmd[count++] = "app_process";
|
||||||
|
|
||||||
char max_size_string[6];
|
|
||||||
char bit_rate_string[11];
|
|
||||||
char max_fps_string[6];
|
|
||||||
char lock_video_orientation_string[5];
|
|
||||||
char display_id_string[11];
|
|
||||||
sprintf(max_size_string, "%"PRIu16, params->max_size);
|
|
||||||
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
|
|
||||||
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
|
|
||||||
sprintf(lock_video_orientation_string, "%"PRIi8,
|
|
||||||
params->lock_video_orientation);
|
|
||||||
sprintf(display_id_string, "%"PRIu32, params->display_id);
|
|
||||||
const char *const cmd[] = {
|
|
||||||
"shell",
|
|
||||||
"CLASSPATH=" SC_DEVICE_SERVER_PATH,
|
|
||||||
"app_process",
|
|
||||||
#ifdef SERVER_DEBUGGER
|
#ifdef SERVER_DEBUGGER
|
||||||
# define SERVER_DEBUGGER_PORT "5005"
|
# define SERVER_DEBUGGER_PORT "5005"
|
||||||
|
cmd[count++] =
|
||||||
# ifdef SERVER_DEBUGGER_METHOD_NEW
|
# ifdef SERVER_DEBUGGER_METHOD_NEW
|
||||||
/* Android 9 and above */
|
/* Android 9 and above */
|
||||||
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,"
|
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,"
|
||||||
@ -166,27 +174,71 @@ execute_server(struct sc_server *server,
|
|||||||
/* Android 8 and below */
|
/* Android 8 and below */
|
||||||
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
|
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
|
||||||
# endif
|
# endif
|
||||||
SERVER_DEBUGGER_PORT,
|
SERVER_DEBUGGER_PORT;
|
||||||
#endif
|
#endif
|
||||||
"/", // unused
|
cmd[count++] = "/"; // unused
|
||||||
"com.genymobile.scrcpy.Server",
|
cmd[count++] = "com.genymobile.scrcpy.Server";
|
||||||
SCRCPY_VERSION,
|
cmd[count++] = SCRCPY_VERSION;
|
||||||
log_level_to_server_string(params->log_level),
|
|
||||||
max_size_string,
|
unsigned dyn_idx = count; // from there, the strings are allocated
|
||||||
bit_rate_string,
|
#define ADD_PARAM(fmt, ...) { \
|
||||||
max_fps_string,
|
char *p = (char *) &cmd[count]; \
|
||||||
lock_video_orientation_string,
|
if (asprintf(&p, fmt, ## __VA_ARGS__) == -1) { \
|
||||||
server->tunnel.forward ? "true" : "false",
|
goto end; \
|
||||||
params->crop ? params->crop : "-",
|
} \
|
||||||
"true", // always send frame meta (packet boundaries + timestamp)
|
cmd[count++] = p; \
|
||||||
params->control ? "true" : "false",
|
}
|
||||||
display_id_string,
|
#define STRBOOL(v) (v ? "true" : "false")
|
||||||
params->show_touches ? "true" : "false",
|
|
||||||
params->stay_awake ? "true" : "false",
|
ADD_PARAM("log_level=%s", log_level_to_server_string(params->log_level));
|
||||||
params->codec_options ? params->codec_options : "-",
|
ADD_PARAM("bit_rate=%" PRIu32, params->bit_rate);
|
||||||
params->encoder_name ? params->encoder_name : "-",
|
|
||||||
params->power_off_on_close ? "true" : "false",
|
if (params->max_size) {
|
||||||
};
|
ADD_PARAM("max_size=%" PRIu16, params->max_size);
|
||||||
|
}
|
||||||
|
if (params->max_fps) {
|
||||||
|
ADD_PARAM("max_fps=%" PRIu16, params->max_fps);
|
||||||
|
}
|
||||||
|
if (params->lock_video_orientation != SC_LOCK_VIDEO_ORIENTATION_UNLOCKED) {
|
||||||
|
ADD_PARAM("lock_video_orientation=%" PRIi8,
|
||||||
|
params->lock_video_orientation);
|
||||||
|
}
|
||||||
|
if (server->tunnel.forward) {
|
||||||
|
ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel.forward));
|
||||||
|
}
|
||||||
|
if (params->crop) {
|
||||||
|
ADD_PARAM("crop=%s", params->crop);
|
||||||
|
}
|
||||||
|
if (!params->control) {
|
||||||
|
// By default, control is true
|
||||||
|
ADD_PARAM("control=%s", STRBOOL(params->control));
|
||||||
|
}
|
||||||
|
if (params->display_id) {
|
||||||
|
ADD_PARAM("display_id=%" PRIu32, params->display_id);
|
||||||
|
}
|
||||||
|
if (params->show_touches) {
|
||||||
|
ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
|
||||||
|
}
|
||||||
|
if (params->stay_awake) {
|
||||||
|
ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
|
||||||
|
}
|
||||||
|
if (params->codec_options) {
|
||||||
|
ADD_PARAM("codec_options=%s", params->codec_options);
|
||||||
|
}
|
||||||
|
if (params->encoder_name) {
|
||||||
|
ADD_PARAM("encoder_name=%s", params->encoder_name);
|
||||||
|
}
|
||||||
|
if (params->power_off_on_close) {
|
||||||
|
ADD_PARAM("power_off_on_close=%s", STRBOOL(params->power_off_on_close));
|
||||||
|
}
|
||||||
|
if (!params->clipboard_autosync) {
|
||||||
|
// By default, clipboard_autosync is true
|
||||||
|
ADD_PARAM("clipboard_autosync=%s", STRBOOL(params->clipboard_autosync));
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef ADD_PARAM
|
||||||
|
#undef STRBOOL
|
||||||
|
|
||||||
#ifdef SERVER_DEBUGGER
|
#ifdef SERVER_DEBUGGER
|
||||||
LOGI("Server debugger waiting for a client on device port "
|
LOGI("Server debugger waiting for a client on device port "
|
||||||
SERVER_DEBUGGER_PORT "...");
|
SERVER_DEBUGGER_PORT "...");
|
||||||
@ -198,12 +250,21 @@ execute_server(struct sc_server *server,
|
|||||||
// Port: 5005
|
// Port: 5005
|
||||||
// Then click on "Debug"
|
// Then click on "Debug"
|
||||||
#endif
|
#endif
|
||||||
return adb_execute(serial, cmd, ARRAY_LEN(cmd));
|
// Inherit both stdout and stderr (all server logs are printed to stdout)
|
||||||
|
pid = adb_execute(params->serial, cmd, count, 0);
|
||||||
|
|
||||||
|
end:
|
||||||
|
for (unsigned i = dyn_idx; i < count; ++i) {
|
||||||
|
free((char *) cmd[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
connect_and_read_byte(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
connect_and_read_byte(struct sc_intr *intr, sc_socket socket,
|
||||||
bool ok = net_connect_intr(intr, socket, IPV4_LOCALHOST, port);
|
uint32_t tunnel_host, uint16_t tunnel_port) {
|
||||||
|
bool ok = net_connect_intr(intr, socket, tunnel_host, tunnel_port);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -220,13 +281,13 @@ connect_and_read_byte(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static sc_socket
|
static sc_socket
|
||||||
connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
|
connect_to_server(struct sc_server *server, unsigned attempts, sc_tick delay,
|
||||||
uint16_t port = server->tunnel.local_port;
|
uint32_t host, uint16_t port) {
|
||||||
do {
|
do {
|
||||||
LOGD("Remaining connection attempts: %d", (int) attempts);
|
LOGD("Remaining connection attempts: %u", attempts);
|
||||||
sc_socket socket = net_socket();
|
sc_socket socket = net_socket();
|
||||||
if (socket != SC_SOCKET_NONE) {
|
if (socket != SC_SOCKET_NONE) {
|
||||||
bool ok = connect_and_read_byte(&server->intr, socket, port);
|
bool ok = connect_and_read_byte(&server->intr, socket, host, port);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
// it worked!
|
// it worked!
|
||||||
return socket;
|
return socket;
|
||||||
@ -241,22 +302,14 @@ connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (attempts) {
|
if (attempts) {
|
||||||
sc_mutex_lock(&server->mutex);
|
|
||||||
sc_tick deadline = sc_tick_now() + delay;
|
sc_tick deadline = sc_tick_now() + delay;
|
||||||
bool timed_out = false;
|
bool ok = sc_server_sleep(server, deadline);
|
||||||
while (!server->stopped && !timed_out) {
|
if (!ok) {
|
||||||
timed_out = !sc_cond_timedwait(&server->cond_stopped,
|
|
||||||
&server->mutex, deadline);
|
|
||||||
}
|
|
||||||
bool stopped = server->stopped;
|
|
||||||
sc_mutex_unlock(&server->mutex);
|
|
||||||
|
|
||||||
if (stopped) {
|
|
||||||
LOGI("Connection attempt stopped");
|
LOGI("Connection attempt stopped");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (--attempts > 0);
|
} while (--attempts);
|
||||||
return SC_SOCKET_NONE;
|
return SC_SOCKET_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,20 +318,18 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
|
|||||||
const struct sc_server_callbacks *cbs, void *cbs_userdata) {
|
const struct sc_server_callbacks *cbs, void *cbs_userdata) {
|
||||||
bool ok = sc_server_params_copy(&server->params, params);
|
bool ok = sc_server_params_copy(&server->params, params);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not copy server params");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_mutex_init(&server->mutex);
|
ok = sc_mutex_init(&server->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not create server mutex");
|
|
||||||
sc_server_params_destroy(&server->params);
|
sc_server_params_destroy(&server->params);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_cond_init(&server->cond_stopped);
|
ok = sc_cond_init(&server->cond_stopped);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not create server cond_stopped");
|
|
||||||
sc_mutex_destroy(&server->mutex);
|
sc_mutex_destroy(&server->mutex);
|
||||||
sc_server_params_destroy(&server->params);
|
sc_server_params_destroy(&server->params);
|
||||||
return false;
|
return false;
|
||||||
@ -286,7 +337,6 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
|
|||||||
|
|
||||||
ok = sc_intr_init(&server->intr);
|
ok = sc_intr_init(&server->intr);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not create intr");
|
|
||||||
sc_cond_destroy(&server->cond_stopped);
|
sc_cond_destroy(&server->cond_stopped);
|
||||||
sc_mutex_destroy(&server->mutex);
|
sc_mutex_destroy(&server->mutex);
|
||||||
sc_server_params_destroy(&server->params);
|
sc_server_params_destroy(&server->params);
|
||||||
@ -338,6 +388,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||||||
assert(tunnel->enabled);
|
assert(tunnel->enabled);
|
||||||
|
|
||||||
const char *serial = server->params.serial;
|
const char *serial = server->params.serial;
|
||||||
|
bool control = server->params.control;
|
||||||
|
|
||||||
sc_socket video_socket = SC_SOCKET_NONE;
|
sc_socket video_socket = SC_SOCKET_NONE;
|
||||||
sc_socket control_socket = SC_SOCKET_NONE;
|
sc_socket control_socket = SC_SOCKET_NONE;
|
||||||
@ -347,27 +398,44 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
control_socket = net_accept_intr(&server->intr, tunnel->server_socket);
|
if (control) {
|
||||||
if (control_socket == SC_SOCKET_NONE) {
|
control_socket =
|
||||||
goto fail;
|
net_accept_intr(&server->intr, tunnel->server_socket);
|
||||||
|
if (control_socket == SC_SOCKET_NONE) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
uint32_t attempts = 100;
|
uint32_t tunnel_host = server->params.tunnel_host;
|
||||||
|
if (!tunnel_host) {
|
||||||
|
tunnel_host = IPV4_LOCALHOST;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t tunnel_port = server->params.tunnel_port;
|
||||||
|
if (!tunnel_port) {
|
||||||
|
tunnel_port = tunnel->local_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned attempts = 100;
|
||||||
sc_tick delay = SC_TICK_FROM_MS(100);
|
sc_tick delay = SC_TICK_FROM_MS(100);
|
||||||
video_socket = connect_to_server(server, attempts, delay);
|
video_socket = connect_to_server(server, attempts, delay, tunnel_host,
|
||||||
|
tunnel_port);
|
||||||
if (video_socket == SC_SOCKET_NONE) {
|
if (video_socket == SC_SOCKET_NONE) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we know that the device is listening, we don't need several attempts
|
if (control) {
|
||||||
control_socket = net_socket();
|
// we know that the device is listening, we don't need several
|
||||||
if (control_socket == SC_SOCKET_NONE) {
|
// attempts
|
||||||
goto fail;
|
control_socket = net_socket();
|
||||||
}
|
if (control_socket == SC_SOCKET_NONE) {
|
||||||
bool ok = net_connect_intr(&server->intr, control_socket,
|
goto fail;
|
||||||
IPV4_LOCALHOST, tunnel->local_port);
|
}
|
||||||
if (!ok) {
|
bool ok = net_connect_intr(&server->intr, control_socket,
|
||||||
goto fail;
|
tunnel_host, tunnel_port);
|
||||||
|
if (!ok) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,7 +449,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert(video_socket != SC_SOCKET_NONE);
|
assert(video_socket != SC_SOCKET_NONE);
|
||||||
assert(control_socket != SC_SOCKET_NONE);
|
assert(!control || control_socket != SC_SOCKET_NONE);
|
||||||
|
|
||||||
server->video_socket = video_socket;
|
server->video_socket = video_socket;
|
||||||
server->control_socket = control_socket;
|
server->control_socket = control_socket;
|
||||||
@ -430,28 +498,221 @@ sc_server_fill_serial(struct sc_server *server) {
|
|||||||
// device/emulator" error)
|
// device/emulator" error)
|
||||||
if (!server->params.serial) {
|
if (!server->params.serial) {
|
||||||
// The serial is owned by sc_server_params, and will be freed on destroy
|
// The serial is owned by sc_server_params, and will be freed on destroy
|
||||||
server->params.serial = adb_get_serialno(&server->intr);
|
server->params.serial = adb_get_serialno(&server->intr, 0);
|
||||||
if (!server->params.serial) {
|
if (!server->params.serial) {
|
||||||
LOGE("Could not get device serial");
|
LOGE("Could not get device serial");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGD("Device serial: %s", server->params.serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
is_tcpip_mode_enabled(struct sc_server *server) {
|
||||||
|
struct sc_intr *intr = &server->intr;
|
||||||
|
const char *serial = server->params.serial;
|
||||||
|
|
||||||
|
char *current_port =
|
||||||
|
adb_getprop(intr, serial, "service.adb.tcp.port", SC_ADB_SILENT);
|
||||||
|
if (!current_port) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is the device is listening on TCP on port 5555?
|
||||||
|
bool enabled = !strcmp("5555", current_port);
|
||||||
|
free(current_port);
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
wait_tcpip_mode_enabled(struct sc_server *server, unsigned attempts,
|
||||||
|
sc_tick delay) {
|
||||||
|
if (is_tcpip_mode_enabled(server)) {
|
||||||
|
LOGI("TCP/IP mode enabled");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only print this log if TCP/IP is not enabled
|
||||||
|
LOGI("Waiting for TCP/IP mode enabled...");
|
||||||
|
|
||||||
|
do {
|
||||||
|
sc_tick deadline = sc_tick_now() + delay;
|
||||||
|
if (!sc_server_sleep(server, deadline)) {
|
||||||
|
LOGI("TCP/IP mode waiting interrupted");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_tcpip_mode_enabled(server)) {
|
||||||
|
LOGI("TCP/IP mode enabled");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} while (--attempts);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
append_port_5555(const char *ip) {
|
||||||
|
size_t len = strlen(ip);
|
||||||
|
|
||||||
|
// sizeof counts the final '\0'
|
||||||
|
char *ip_port = malloc(len + sizeof(":5555"));
|
||||||
|
if (!ip_port) {
|
||||||
|
LOG_OOM();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(ip_port, ip, len);
|
||||||
|
memcpy(ip_port + len, ":5555", sizeof(":5555"));
|
||||||
|
|
||||||
|
return ip_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_server_switch_to_tcpip(struct sc_server *server, char **out_ip_port) {
|
||||||
|
const char *serial = server->params.serial;
|
||||||
|
assert(serial);
|
||||||
|
|
||||||
|
struct sc_intr *intr = &server->intr;
|
||||||
|
|
||||||
|
char *ip = adb_get_device_ip(intr, serial, 0);
|
||||||
|
if (!ip) {
|
||||||
|
LOGE("Device IP not found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ip_port = append_port_5555(ip);
|
||||||
|
free(ip);
|
||||||
|
if (!ip_port) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tcp_mode = is_tcpip_mode_enabled(server);
|
||||||
|
|
||||||
|
if (!tcp_mode) {
|
||||||
|
bool ok = adb_tcpip(intr, serial, 5555, SC_ADB_NO_STDOUT);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not restart adbd in TCP/IP mode");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned attempts = 40;
|
||||||
|
sc_tick delay = SC_TICK_FROM_MS(250);
|
||||||
|
ok = wait_tcpip_mode_enabled(server, attempts, delay);
|
||||||
|
if (!ok) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_ip_port = ip_port;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
error:
|
||||||
|
free(ip_port);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
|
||||||
|
struct sc_intr *intr = &server->intr;
|
||||||
|
|
||||||
|
// Error expected if not connected, do not report any error
|
||||||
|
adb_disconnect(intr, ip_port, SC_ADB_SILENT);
|
||||||
|
|
||||||
|
bool ok = adb_connect(intr, ip_port, 0);
|
||||||
|
if (!ok) {
|
||||||
|
LOGE("Could not connect to %s", ip_port);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override the serial, owned by the sc_server_params
|
||||||
|
free((void *) server->params.serial);
|
||||||
|
server->params.serial = strdup(ip_port);
|
||||||
|
if (!server->params.serial) {
|
||||||
|
LOG_OOM();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGI("Connected to %s", ip_port);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_server_configure_tcpip(struct sc_server *server) {
|
||||||
|
char *ip_port;
|
||||||
|
|
||||||
|
const struct sc_server_params *params = &server->params;
|
||||||
|
|
||||||
|
// If tcpip parameter is given, then it must connect to this address.
|
||||||
|
// Therefore, the device is unknown, so serial is meaningless at this point.
|
||||||
|
assert(!params->serial || !params->tcpip_dst);
|
||||||
|
|
||||||
|
if (params->tcpip_dst) {
|
||||||
|
// Append ":5555" if no port is present
|
||||||
|
bool contains_port = strchr(params->tcpip_dst, ':');
|
||||||
|
ip_port = contains_port ? strdup(params->tcpip_dst)
|
||||||
|
: append_port_5555(params->tcpip_dst);
|
||||||
|
if (!ip_port) {
|
||||||
|
LOG_OOM();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The device IP address must be retrieved from the current
|
||||||
|
// connected device
|
||||||
|
if (!sc_server_fill_serial(server)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The serial is either the real serial when connected via USB, or
|
||||||
|
// the IP:PORT when connected over TCP/IP. Only the latter contains
|
||||||
|
// a colon.
|
||||||
|
bool is_already_tcpip = strchr(params->serial, ':');
|
||||||
|
if (is_already_tcpip) {
|
||||||
|
// Nothing to do
|
||||||
|
LOGI("Device already connected via TCP/IP: %s", params->serial);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok = sc_server_switch_to_tcpip(server, &ip_port);
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// On success, this call changes params->serial
|
||||||
|
bool ok = sc_server_connect_to_tcpip(server, ip_port);
|
||||||
|
free(ip_port);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
run_server(void *data) {
|
run_server(void *data) {
|
||||||
struct sc_server *server = data;
|
struct sc_server *server = data;
|
||||||
|
|
||||||
|
const struct sc_server_params *params = &server->params;
|
||||||
|
|
||||||
|
if (params->serial) {
|
||||||
|
LOGD("Device serial: %s", params->serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params->tcpip) {
|
||||||
|
// params->serial may be changed after this call
|
||||||
|
bool ok = sc_server_configure_tcpip(server);
|
||||||
|
if (!ok) {
|
||||||
|
goto error_connection_failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// It is ok to call this function even if the device serial has been
|
||||||
|
// changed by switching over TCP/IP
|
||||||
if (!sc_server_fill_serial(server)) {
|
if (!sc_server_fill_serial(server)) {
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct sc_server_params *params = &server->params;
|
|
||||||
|
|
||||||
LOGD("Device serial: %s", params->serial);
|
|
||||||
|
|
||||||
bool ok = push_server(&server->intr, params->serial);
|
bool ok = push_server(&server->intr, params->serial);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error_connection_failed;
|
goto error_connection_failed;
|
||||||
@ -502,6 +763,17 @@ run_server(void *data) {
|
|||||||
}
|
}
|
||||||
sc_mutex_unlock(&server->mutex);
|
sc_mutex_unlock(&server->mutex);
|
||||||
|
|
||||||
|
// Interrupt sockets to wake up socket blocking calls on the server
|
||||||
|
assert(server->video_socket != SC_SOCKET_NONE);
|
||||||
|
net_interrupt(server->video_socket);
|
||||||
|
net_close(server->video_socket);
|
||||||
|
|
||||||
|
if (server->control_socket != SC_SOCKET_NONE) {
|
||||||
|
// There is no control_socket if --no-control is set
|
||||||
|
net_interrupt(server->control_socket);
|
||||||
|
net_close(server->control_socket);
|
||||||
|
}
|
||||||
|
|
||||||
// Give some delay for the server to terminate properly
|
// Give some delay for the server to terminate properly
|
||||||
#define WATCHDOG_DELAY SC_TICK_FROM_SEC(1)
|
#define WATCHDOG_DELAY SC_TICK_FROM_SEC(1)
|
||||||
sc_tick deadline = sc_tick_now() + WATCHDOG_DELAY;
|
sc_tick deadline = sc_tick_now() + WATCHDOG_DELAY;
|
||||||
@ -532,7 +804,8 @@ error_connection_failed:
|
|||||||
|
|
||||||
bool
|
bool
|
||||||
sc_server_start(struct sc_server *server) {
|
sc_server_start(struct sc_server *server) {
|
||||||
bool ok = sc_thread_create(&server->thread, run_server, "server", server);
|
bool ok =
|
||||||
|
sc_thread_create(&server->thread, run_server, "scrcpy-server", server);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not create server thread");
|
LOGE("Could not create server thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -29,6 +29,8 @@ struct sc_server_params {
|
|||||||
const char *codec_options;
|
const char *codec_options;
|
||||||
const char *encoder_name;
|
const char *encoder_name;
|
||||||
struct sc_port_range port_range;
|
struct sc_port_range port_range;
|
||||||
|
uint32_t tunnel_host;
|
||||||
|
uint16_t tunnel_port;
|
||||||
uint16_t max_size;
|
uint16_t max_size;
|
||||||
uint32_t bit_rate;
|
uint32_t bit_rate;
|
||||||
uint16_t max_fps;
|
uint16_t max_fps;
|
||||||
@ -39,6 +41,9 @@ struct sc_server_params {
|
|||||||
bool stay_awake;
|
bool stay_awake;
|
||||||
bool force_adb_forward;
|
bool force_adb_forward;
|
||||||
bool power_off_on_close;
|
bool power_off_on_close;
|
||||||
|
bool clipboard_autosync;
|
||||||
|
bool tcpip;
|
||||||
|
const char *tcpip_dst;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sc_server {
|
struct sc_server {
|
||||||
|
@ -42,7 +42,7 @@ stream_recv_packet(struct stream *stream, AVPacket *packet) {
|
|||||||
assert(len);
|
assert(len);
|
||||||
|
|
||||||
if (av_new_packet(packet, len)) {
|
if (av_new_packet(packet, len)) {
|
||||||
LOGE("Could not allocate packet");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,18 +111,18 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
|
|||||||
if (stream->pending) {
|
if (stream->pending) {
|
||||||
offset = stream->pending->size;
|
offset = stream->pending->size;
|
||||||
if (av_grow_packet(stream->pending, packet->size)) {
|
if (av_grow_packet(stream->pending, packet->size)) {
|
||||||
LOGE("Could not grow packet");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
stream->pending = av_packet_alloc();
|
stream->pending = av_packet_alloc();
|
||||||
if (!stream->pending) {
|
if (!stream->pending) {
|
||||||
LOGE("Could not allocate packet");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (av_new_packet(stream->pending, packet->size)) {
|
if (av_new_packet(stream->pending, packet->size)) {
|
||||||
LOGE("Could not create packet");
|
LOG_OOM();
|
||||||
av_packet_free(&stream->pending);
|
av_packet_free(&stream->pending);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -200,7 +200,7 @@ run_stream(void *data) {
|
|||||||
|
|
||||||
stream->codec_ctx = avcodec_alloc_context3(codec);
|
stream->codec_ctx = avcodec_alloc_context3(codec);
|
||||||
if (!stream->codec_ctx) {
|
if (!stream->codec_ctx) {
|
||||||
LOGC("Could not allocate codec context");
|
LOG_OOM();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +221,7 @@ run_stream(void *data) {
|
|||||||
|
|
||||||
AVPacket *packet = av_packet_alloc();
|
AVPacket *packet = av_packet_alloc();
|
||||||
if (!packet) {
|
if (!packet) {
|
||||||
LOGE("Could not allocate packet");
|
LOG_OOM();
|
||||||
goto finally_close_parser;
|
goto finally_close_parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +284,8 @@ bool
|
|||||||
stream_start(struct stream *stream) {
|
stream_start(struct stream *stream) {
|
||||||
LOGD("Starting stream thread");
|
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) {
|
if (!ok) {
|
||||||
LOGC("Could not start stream thread");
|
LOGC("Could not start stream thread");
|
||||||
return false;
|
return false;
|
||||||
|
@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "util/log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_file_executable_exists(const char *file) {
|
sc_file_executable_exists(const char *file) {
|
||||||
char *path = getenv("PATH");
|
char *path = getenv("PATH");
|
||||||
@ -23,7 +26,10 @@ sc_file_executable_exists(const char *file) {
|
|||||||
size_t dir_len = strlen(dir);
|
size_t dir_len = strlen(dir);
|
||||||
char *fullpath = malloc(dir_len + file_len + 2);
|
char *fullpath = malloc(dir_len + file_len + 2);
|
||||||
if (!fullpath)
|
if (!fullpath)
|
||||||
|
{
|
||||||
|
LOG_OOM();
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
memcpy(fullpath, dir, dir_len);
|
memcpy(fullpath, dir, dir_len);
|
||||||
fullpath[dir_len] = '/';
|
fullpath[dir_len] = '/';
|
||||||
memcpy(fullpath + dir_len + 1, file, file_len + 1);
|
memcpy(fullpath + dir_len + 1, file, file_len + 1);
|
||||||
|
@ -11,8 +11,11 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
enum sc_process_result
|
enum sc_process_result
|
||||||
sc_process_execute_p(const char *const argv[], sc_pid *pid,
|
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
||||||
int *pin, int *pout, int *perr) {
|
int *pin, int *pout, int *perr) {
|
||||||
|
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||||
|
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||||
|
|
||||||
int in[2];
|
int in[2];
|
||||||
int out[2];
|
int out[2];
|
||||||
int err[2];
|
int err[2];
|
||||||
@ -90,22 +93,39 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid,
|
|||||||
}
|
}
|
||||||
close(in[1]);
|
close(in[1]);
|
||||||
}
|
}
|
||||||
|
// Do not close stdin in the child process, this makes adb fail on Linux
|
||||||
|
|
||||||
if (pout) {
|
if (pout) {
|
||||||
if (out[1] != STDOUT_FILENO) {
|
if (out[1] != STDOUT_FILENO) {
|
||||||
dup2(out[1], STDOUT_FILENO);
|
dup2(out[1], STDOUT_FILENO);
|
||||||
close(out[1]);
|
close(out[1]);
|
||||||
}
|
}
|
||||||
close(out[0]);
|
close(out[0]);
|
||||||
|
} else if (!inherit_stdout) {
|
||||||
|
// Close stdout in the child process
|
||||||
|
close(STDOUT_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (perr) {
|
if (perr) {
|
||||||
if (err[1] != STDERR_FILENO) {
|
if (err[1] != STDERR_FILENO) {
|
||||||
dup2(err[1], STDERR_FILENO);
|
dup2(err[1], STDERR_FILENO);
|
||||||
close(err[1]);
|
close(err[1]);
|
||||||
}
|
}
|
||||||
close(err[0]);
|
close(err[0]);
|
||||||
|
} else if (!inherit_stderr) {
|
||||||
|
// Close stderr in the child process
|
||||||
|
close(STDERR_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(internal[0]);
|
close(internal[0]);
|
||||||
enum sc_process_result err;
|
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) {
|
if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) {
|
||||||
execvp(argv[0], (char *const *) argv);
|
execvp(argv[0], (char *const *) argv);
|
||||||
perror("exec");
|
perror("exec");
|
||||||
|
@ -26,7 +26,7 @@ bool
|
|||||||
sc_file_is_regular(const char *path) {
|
sc_file_is_regular(const char *path) {
|
||||||
wchar_t *wide_path = sc_str_to_wchars(path);
|
wchar_t *wide_path = sc_str_to_wchars(path);
|
||||||
if (!wide_path) {
|
if (!wide_path) {
|
||||||
LOGC("Could not allocate wide char string");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
// <https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873>
|
|
||||||
#define _WIN32_WINNT 0x0600 // For extended process API
|
|
||||||
|
|
||||||
#include "util/process.h"
|
#include "util/process.h"
|
||||||
|
|
||||||
#include <processthreadsapi.h>
|
#include <processthreadsapi.h>
|
||||||
@ -27,12 +24,15 @@ build_cmd(char *cmd, size_t len, const char *const argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum sc_process_result
|
enum sc_process_result
|
||||||
sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
|
||||||
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
||||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||||
|
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||||
|
|
||||||
// Add 1 per non-NULL pointer
|
// Add 1 per non-NULL pointer
|
||||||
unsigned handle_count = !!pin + !!pout + !!perr;
|
unsigned handle_count = !!pin || !!pout || !!perr;
|
||||||
|
|
||||||
|
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||||
|
|
||||||
SECURITY_ATTRIBUTES sa;
|
SECURITY_ATTRIBUTES sa;
|
||||||
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||||
@ -79,17 +79,30 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||||||
si.StartupInfo.cb = sizeof(si);
|
si.StartupInfo.cb = sizeof(si);
|
||||||
HANDLE handles[3];
|
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;
|
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
|
||||||
if (handle_count) {
|
if (handle_count) {
|
||||||
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
unsigned i = 0;
|
||||||
if (pin) {
|
if (pin) {
|
||||||
si.StartupInfo.hStdInput = stdin_read_handle;
|
si.StartupInfo.hStdInput = stdin_read_handle;
|
||||||
|
handles[i++] = si.StartupInfo.hStdInput;
|
||||||
}
|
}
|
||||||
if (pout) {
|
if (pout) {
|
||||||
|
assert(!inherit_stdout);
|
||||||
si.StartupInfo.hStdOutput = stdout_write_handle;
|
si.StartupInfo.hStdOutput = stdout_write_handle;
|
||||||
|
handles[i++] = si.StartupInfo.hStdOutput;
|
||||||
}
|
}
|
||||||
if (perr) {
|
if (perr) {
|
||||||
|
assert(!inherit_stderr);
|
||||||
si.StartupInfo.hStdError = stderr_write_handle;
|
si.StartupInfo.hStdError = stderr_write_handle;
|
||||||
|
handles[i++] = si.StartupInfo.hStdError;
|
||||||
}
|
}
|
||||||
|
|
||||||
SIZE_T size;
|
SIZE_T size;
|
||||||
@ -103,6 +116,7 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||||||
|
|
||||||
lpAttributeList = malloc(size);
|
lpAttributeList = malloc(size);
|
||||||
if (!lpAttributeList) {
|
if (!lpAttributeList) {
|
||||||
|
LOG_OOM();
|
||||||
goto error_close_stderr;
|
goto error_close_stderr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,17 +126,6 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||||||
goto error_close_stderr;
|
goto error_close_stderr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicitly pass the HANDLEs that must be inherited
|
|
||||||
unsigned i = 0;
|
|
||||||
if (pin) {
|
|
||||||
handles[i++] = stdin_read_handle;
|
|
||||||
}
|
|
||||||
if (pout) {
|
|
||||||
handles[i++] = stdout_write_handle;
|
|
||||||
}
|
|
||||||
if (perr) {
|
|
||||||
handles[i++] = stderr_write_handle;
|
|
||||||
}
|
|
||||||
ok = UpdateProcThreadAttribute(lpAttributeList, 0,
|
ok = UpdateProcThreadAttribute(lpAttributeList, 0,
|
||||||
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
||||||
handles, handle_count * sizeof(HANDLE),
|
handles, handle_count * sizeof(HANDLE),
|
||||||
@ -136,23 +139,33 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||||||
|
|
||||||
char *cmd = malloc(CMD_MAX_LEN);
|
char *cmd = malloc(CMD_MAX_LEN);
|
||||||
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
||||||
|
LOG_OOM();
|
||||||
goto error_free_attribute_list;
|
goto error_free_attribute_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
wchar_t *wide = sc_str_to_wchars(cmd);
|
wchar_t *wide = sc_str_to_wchars(cmd);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
if (!wide) {
|
if (!wide) {
|
||||||
LOGC("Could not allocate wide char string");
|
LOG_OOM();
|
||||||
goto error_free_attribute_list;
|
goto error_free_attribute_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL bInheritHandles = handle_count > 0;
|
BOOL bInheritHandles = handle_count > 0 || inherit_stdout || inherit_stderr;
|
||||||
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT : 0;
|
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,
|
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
|
||||||
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
||||||
free(wide);
|
free(wide);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
int err = GetLastError();
|
||||||
|
LOGE("CreateProcessW() error %d", err);
|
||||||
|
if (err == ERROR_FILE_NOT_FOUND) {
|
||||||
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
||||||
}
|
}
|
||||||
goto error_free_attribute_list;
|
goto error_free_attribute_list;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <SDL2/SDL_events.h>
|
#include "input_events.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key processor trait.
|
* Key processor trait.
|
||||||
@ -14,16 +14,35 @@
|
|||||||
* Component able to process and inject keys should implement this trait.
|
* Component able to process and inject keys should implement this trait.
|
||||||
*/
|
*/
|
||||||
struct sc_key_processor {
|
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;
|
const struct sc_key_processor_ops *ops;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sc_key_processor_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
|
void
|
||||||
(*process_key)(struct sc_key_processor *kp, const SDL_KeyboardEvent *event);
|
(*process_key)(struct sc_key_processor *kp,
|
||||||
|
const struct sc_key_event *event, uint64_t ack_to_wait);
|
||||||
|
|
||||||
void
|
void
|
||||||
(*process_text)(struct sc_key_processor *kp,
|
(*process_text)(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event);
|
const struct sc_text_event *event);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
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
|
@ -31,7 +31,7 @@ sc_file_get_local_path(const char *name) {
|
|||||||
size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
|
size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
|
||||||
char *file_path = malloc(len);
|
char *file_path = malloc(len);
|
||||||
if (!file_path) {
|
if (!file_path) {
|
||||||
LOGE("Could not alloc path");
|
LOG_OOM();
|
||||||
free(executable_path);
|
free(executable_path);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
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
|
@ -8,7 +8,7 @@ bool
|
|||||||
sc_intr_init(struct sc_intr *intr) {
|
sc_intr_init(struct sc_intr *intr) {
|
||||||
bool ok = sc_mutex_init(&intr->mutex);
|
bool ok = sc_mutex_init(&intr->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not init intr mutex");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
#include "options.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 LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
@ -14,6 +17,9 @@
|
|||||||
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||||
|
|
||||||
|
#define LOG_OOM() \
|
||||||
|
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_set_log_level(enum sc_log_level level);
|
sc_set_log_level(enum sc_log_level level);
|
||||||
|
|
||||||
|
@ -1,25 +1,30 @@
|
|||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <SDL2/SDL_platform.h>
|
#include <SDL2/SDL_platform.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
|
# include <ws2tcpip.h>
|
||||||
typedef int socklen_t;
|
typedef int socklen_t;
|
||||||
typedef SOCKET sc_raw_socket;
|
typedef SOCKET sc_raw_socket;
|
||||||
|
# define SC_RAW_SOCKET_NONE INVALID_SOCKET
|
||||||
#else
|
#else
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include <sys/socket.h>
|
# include <sys/socket.h>
|
||||||
# include <netinet/in.h>
|
# include <netinet/in.h>
|
||||||
# include <arpa/inet.h>
|
# include <arpa/inet.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include <fcntl.h>
|
||||||
# define SOCKET_ERROR -1
|
# define SOCKET_ERROR -1
|
||||||
typedef struct sockaddr_in SOCKADDR_IN;
|
typedef struct sockaddr_in SOCKADDR_IN;
|
||||||
typedef struct sockaddr SOCKADDR;
|
typedef struct sockaddr SOCKADDR;
|
||||||
typedef struct in_addr IN_ADDR;
|
typedef struct in_addr IN_ADDR;
|
||||||
typedef int sc_raw_socket;
|
typedef int sc_raw_socket;
|
||||||
|
# define SC_RAW_SOCKET_NONE -1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -51,6 +56,7 @@ wrap(sc_raw_socket sock) {
|
|||||||
|
|
||||||
struct sc_socket_windows *socket = malloc(sizeof(*socket));
|
struct sc_socket_windows *socket = malloc(sizeof(*socket));
|
||||||
if (!socket) {
|
if (!socket) {
|
||||||
|
LOG_OOM();
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
return SC_SOCKET_NONE;
|
return SC_SOCKET_NONE;
|
||||||
}
|
}
|
||||||
@ -77,6 +83,35 @@ unwrap(sc_socket socket) {
|
|||||||
#endif
|
#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
|
static void
|
||||||
net_perror(const char *s) {
|
net_perror(const char *s) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -95,7 +130,16 @@ net_perror(const char *s) {
|
|||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
net_socket(void) {
|
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);
|
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);
|
sc_socket sock = wrap(raw_sock);
|
||||||
if (sock == SC_SOCKET_NONE) {
|
if (sock == SC_SOCKET_NONE) {
|
||||||
net_perror("socket");
|
net_perror("socket");
|
||||||
@ -154,8 +198,18 @@ net_accept(sc_socket server_socket) {
|
|||||||
|
|
||||||
SOCKADDR_IN csin;
|
SOCKADDR_IN csin;
|
||||||
socklen_t sinsize = sizeof(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 =
|
sc_raw_socket raw_sock =
|
||||||
accept(raw_server_socket, (SOCKADDR *) &csin, &sinsize);
|
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);
|
return wrap(raw_sock);
|
||||||
}
|
}
|
||||||
@ -209,7 +263,6 @@ net_interrupt(sc_socket socket) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
bool
|
bool
|
||||||
net_close(sc_socket socket) {
|
net_close(sc_socket socket) {
|
||||||
sc_raw_socket raw_sock = unwrap(socket);
|
sc_raw_socket raw_sock = unwrap(socket);
|
||||||
@ -225,3 +278,15 @@ net_close(sc_socket socket) {
|
|||||||
return !close(raw_sock);
|
return !close(raw_sock);
|
||||||
#endif
|
#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;
|
||||||
|
}
|
||||||
|
@ -68,4 +68,10 @@ net_interrupt(sc_socket socket);
|
|||||||
bool
|
bool
|
||||||
net_close(sc_socket 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
|
#endif
|
||||||
|
@ -5,27 +5,8 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
enum sc_process_result
|
enum sc_process_result
|
||||||
sc_process_execute(const char *const argv[], sc_pid *pid) {
|
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned flags) {
|
||||||
return sc_process_execute_p(argv, pid, NULL, NULL, NULL);
|
return sc_process_execute_p(argv, pid, flags, NULL, NULL, NULL);
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_process_check_success(sc_pid pid, const char *name, bool close) {
|
|
||||||
if (pid == SC_PROCESS_NONE) {
|
|
||||||
LOGE("Could not execute \"%s\"", name);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
sc_exit_code exit_code = sc_process_wait(pid, close);
|
|
||||||
if (exit_code) {
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
@ -83,7 +64,7 @@ sc_process_observer_init(struct sc_process_observer *observer, sc_pid pid,
|
|||||||
observer->listener_userdata = listener_userdata;
|
observer->listener_userdata = listener_userdata;
|
||||||
observer->terminated = false;
|
observer->terminated = false;
|
||||||
|
|
||||||
ok = sc_thread_create(&observer->thread, run_observer, "process_observer",
|
ok = sc_thread_create(&observer->thread, run_observer, "scrcpy-proc",
|
||||||
observer);
|
observer);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
sc_cond_destroy(&observer->cond_terminated);
|
sc_cond_destroy(&observer->cond_terminated);
|
||||||
|
@ -67,20 +67,32 @@ enum sc_process_result {
|
|||||||
SC_PROCESS_ERROR_MISSING_BINARY,
|
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`
|
* 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
|
enum sc_process_result
|
||||||
sc_process_execute(const char *const argv[], sc_pid *pid);
|
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the command and write the process id to `pid`
|
* Execute the command and write the process id to `pid`
|
||||||
*
|
*
|
||||||
* If not NULL, provide a pipe for stdin (`pin`), stdout (`pout`) and stderr
|
* If not NULL, provide a pipe for stdin (`pin`), stdout (`pout`) and stderr
|
||||||
* (`perr`).
|
* (`perr`).
|
||||||
|
*
|
||||||
|
* The `flags` argument has the same semantics as in `sc_process_execute()`.
|
||||||
*/
|
*/
|
||||||
enum sc_process_result
|
enum sc_process_result
|
||||||
sc_process_execute_p(const char *const argv[], sc_pid *pid,
|
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
||||||
sc_pipe *pin, sc_pipe *pout, sc_pipe *perr);
|
sc_pipe *pin, sc_pipe *pout, sc_pipe *perr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,14 +119,6 @@ sc_process_wait(sc_pid pid, bool close);
|
|||||||
void
|
void
|
||||||
sc_process_close(sc_pid 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.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
sc_process_check_success(sc_pid pid, const char *name, bool close);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from the pipe
|
* Read from the pipe
|
||||||
*
|
*
|
||||||
|
@ -1,26 +1,5 @@
|
|||||||
#include "process_intr.h"
|
#include "process_intr.h"
|
||||||
|
|
||||||
bool
|
|
||||||
sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
|
||||||
const char *name, bool close) {
|
|
||||||
if (!sc_intr_set_process(intr, pid)) {
|
|
||||||
// Already interrupted
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always pass close=false, interrupting would be racy otherwise
|
|
||||||
bool ret = sc_process_check_success(pid, name, false);
|
|
||||||
|
|
||||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
|
||||||
|
|
||||||
if (close) {
|
|
||||||
// Close separately
|
|
||||||
sc_process_close(pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
|
@ -6,10 +6,6 @@
|
|||||||
#include "intr.h"
|
#include "intr.h"
|
||||||
#include "process.h"
|
#include "process.h"
|
||||||
|
|
||||||
bool
|
|
||||||
sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
|
||||||
const char *name, bool close);
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||||
size_t len);
|
size_t len);
|
||||||
|
@ -5,13 +5,15 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "util/strbuf.h"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
# include <tchar.h>
|
# include <tchar.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
|
#include "strbuf.h"
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
sc_strncpy(char *dest, const char *src, size_t n) {
|
sc_strncpy(char *dest, const char *src, size_t n) {
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -51,6 +53,7 @@ sc_str_quote(const char *src) {
|
|||||||
size_t len = strlen(src);
|
size_t len = strlen(src);
|
||||||
char *quoted = malloc(len + 3);
|
char *quoted = malloc(len + 3);
|
||||||
if (!quoted) {
|
if (!quoted) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
memcpy("ed[1], src, len);
|
memcpy("ed[1], src, len);
|
||||||
@ -188,6 +191,7 @@ sc_str_to_wchars(const char *utf8) {
|
|||||||
|
|
||||||
wchar_t *wide = malloc(len * sizeof(wchar_t));
|
wchar_t *wide = malloc(len * sizeof(wchar_t));
|
||||||
if (!wide) {
|
if (!wide) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,6 +208,7 @@ sc_str_from_wchars(const wchar_t *ws) {
|
|||||||
|
|
||||||
char *utf8 = malloc(len);
|
char *utf8 = malloc(len);
|
||||||
if (!utf8) {
|
if (!utf8) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +240,7 @@ sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent) {
|
|||||||
|
|
||||||
APPEND_INDENT();
|
APPEND_INDENT();
|
||||||
|
|
||||||
// The last separator encountered, it must be inserted only conditionnaly,
|
// The last separator encountered, it must be inserted only conditionally,
|
||||||
// depending on the next token
|
// depending on the next token
|
||||||
char pending = 0;
|
char pending = 0;
|
||||||
|
|
||||||
@ -299,3 +304,40 @@ sc_str_truncate(char *data, size_t len, const char *endchars) {
|
|||||||
data[idx] = '\0';
|
data[idx] = '\0';
|
||||||
return idx;
|
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;
|
||||||
|
}
|
||||||
|
@ -114,4 +114,35 @@ sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
|||||||
size_t
|
size_t
|
||||||
sc_str_truncate(char *data, size_t len, const char *endchars);
|
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
|
#endif
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
#include "strbuf.h"
|
#include "strbuf.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include "log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
||||||
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
||||||
if (!buf->s) {
|
if (!buf->s) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +27,7 @@ sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
|
|||||||
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
||||||
if (!s) {
|
if (!s) {
|
||||||
// Leave the old buf->s
|
// Leave the old buf->s
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
buf->s = s;
|
buf->s = s;
|
||||||
|
@ -8,8 +8,13 @@
|
|||||||
bool
|
bool
|
||||||
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
||||||
void *userdata) {
|
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);
|
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
|
||||||
if (!sdl_thread) {
|
if (!sdl_thread) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,6 +31,7 @@ bool
|
|||||||
sc_mutex_init(sc_mutex *mutex) {
|
sc_mutex_init(sc_mutex *mutex) {
|
||||||
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
||||||
if (!sdl_mutex) {
|
if (!sdl_mutex) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,6 +100,7 @@ bool
|
|||||||
sc_cond_init(sc_cond *cond) {
|
sc_cond_init(sc_cond *cond) {
|
||||||
SDL_cond *sdl_cond = SDL_CreateCond();
|
SDL_cond *sdl_cond = SDL_CreateCond();
|
||||||
if (!sdl_cond) {
|
if (!sdl_cond) {
|
||||||
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,55 @@
|
|||||||
#include "tick.h"
|
#include "tick.h"
|
||||||
|
|
||||||
#include <SDL2/SDL_timer.h>
|
#include <assert.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
sc_tick
|
sc_tick
|
||||||
sc_tick_now(void) {
|
sc_tick_now(void) {
|
||||||
// SDL_GetTicks() resolution is in milliseconds, but sc_tick are expressed
|
#ifndef _WIN32
|
||||||
// in microseconds to store PTS without precision loss.
|
// Maximum sc_tick precision (microsecond)
|
||||||
//
|
struct timespec ts;
|
||||||
// As an alternative, SDL_GetPerformanceCounter() and
|
int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
// SDL_GetPerformanceFrequency() could be used, but:
|
if (ret) {
|
||||||
// - the conversions (avoiding overflow) are expansive, since the
|
abort();
|
||||||
// frequency is not known at compile time;
|
}
|
||||||
// - in practice, we don't need more precision for now.
|
|
||||||
return (sc_tick) SDL_GetTicks() * 1000;
|
return SC_TICK_FROM_SEC(ts.tv_sec) + SC_TICK_FROM_NS(ts.tv_nsec);
|
||||||
|
#else
|
||||||
|
LARGE_INTEGER c;
|
||||||
|
|
||||||
|
// On systems that run Windows XP or later, the function will always
|
||||||
|
// succeed and will thus never return zero.
|
||||||
|
// <https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter>
|
||||||
|
// <https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency>
|
||||||
|
|
||||||
|
BOOL ok = QueryPerformanceCounter(&c);
|
||||||
|
assert(ok);
|
||||||
|
(void) ok;
|
||||||
|
|
||||||
|
LONGLONG counter = c.QuadPart;
|
||||||
|
|
||||||
|
static LONGLONG frequency;
|
||||||
|
if (!frequency) {
|
||||||
|
// Initialize on first call
|
||||||
|
LARGE_INTEGER f;
|
||||||
|
ok = QueryPerformanceFrequency(&f);
|
||||||
|
assert(ok);
|
||||||
|
frequency = f.QuadPart;
|
||||||
|
assert(frequency);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (frequency % SC_TICK_FREQ == 0) {
|
||||||
|
// Expected case (typically frequency = 10000000, i.e. 100ns precision)
|
||||||
|
sc_tick div = frequency / SC_TICK_FREQ;
|
||||||
|
return SC_TICK_FROM_US(counter / div);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split the division to avoid overflow
|
||||||
|
sc_tick secs = SC_TICK_FROM_SEC(counter / frequency);
|
||||||
|
sc_tick subsec = SC_TICK_FREQ * (counter % frequency) / frequency;
|
||||||
|
return secs + subsec;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,11 @@ typedef int64_t sc_tick;
|
|||||||
#define SC_TICK_FREQ 1000000 // microsecond
|
#define SC_TICK_FREQ 1000000 // microsecond
|
||||||
|
|
||||||
// To be adapted if SC_TICK_FREQ changes
|
// To be adapted if SC_TICK_FREQ changes
|
||||||
|
#define SC_TICK_TO_NS(tick) ((tick) * 1000)
|
||||||
#define SC_TICK_TO_US(tick) (tick)
|
#define SC_TICK_TO_US(tick) (tick)
|
||||||
#define SC_TICK_TO_MS(tick) ((tick) / 1000)
|
#define SC_TICK_TO_MS(tick) ((tick) / 1000)
|
||||||
#define SC_TICK_TO_SEC(tick) ((tick) / 1000000)
|
#define SC_TICK_TO_SEC(tick) ((tick) / 1000000)
|
||||||
|
#define SC_TICK_FROM_NS(ns) ((ns) / 1000)
|
||||||
#define SC_TICK_FROM_US(us) (us)
|
#define SC_TICK_FROM_US(us) (us)
|
||||||
#define SC_TICK_FROM_MS(ms) ((ms) * 1000)
|
#define SC_TICK_FROM_MS(ms) ((ms) * 1000)
|
||||||
#define SC_TICK_FROM_SEC(sec) ((sec) * 1000000)
|
#define SC_TICK_FROM_SEC(sec) ((sec) * 1000000)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "v4l2_sink.h"
|
#include "v4l2_sink.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/str.h"
|
#include "util/str.h"
|
||||||
|
|
||||||
@ -31,7 +33,7 @@ write_header(struct sc_v4l2_sink *vs, const AVPacket *packet) {
|
|||||||
|
|
||||||
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
||||||
if (!extradata) {
|
if (!extradata) {
|
||||||
LOGC("Could not allocate extradata");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,25 +163,21 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
|
|
||||||
bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs);
|
bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not initialize video buffer");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_video_buffer_start(&vs->vb);
|
ok = sc_video_buffer_start(&vs->vb);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not start video buffer");
|
|
||||||
goto error_video_buffer_destroy;
|
goto error_video_buffer_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_mutex_init(&vs->mutex);
|
ok = sc_mutex_init(&vs->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create mutex");
|
|
||||||
goto error_video_buffer_stop_and_join;
|
goto error_video_buffer_stop_and_join;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_cond_init(&vs->cond);
|
ok = sc_cond_init(&vs->cond);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create cond");
|
|
||||||
goto error_mutex_destroy;
|
goto error_mutex_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +199,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
|
|
||||||
vs->format_ctx = avformat_alloc_context();
|
vs->format_ctx = avformat_alloc_context();
|
||||||
if (!vs->format_ctx) {
|
if (!vs->format_ctx) {
|
||||||
LOGE("Could not allocate v4l2 output context");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,9 +211,8 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
#ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
#ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
||||||
vs->format_ctx->url = strdup(vs->device_name);
|
vs->format_ctx->url = strdup(vs->device_name);
|
||||||
if (!vs->format_ctx->url) {
|
if (!vs->format_ctx->url) {
|
||||||
LOGE("Could not strdup v4l2 device name");
|
LOG_OOM();
|
||||||
goto error_avformat_free_context;
|
goto error_avformat_free_context;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
strncpy(vs->format_ctx->filename, vs->device_name,
|
strncpy(vs->format_ctx->filename, vs->device_name,
|
||||||
@ -224,9 +221,8 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
|
|
||||||
AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder);
|
AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder);
|
||||||
if (!ostream) {
|
if (!ostream) {
|
||||||
LOGE("Could not allocate new v4l2 stream");
|
LOG_OOM();
|
||||||
goto error_avformat_free_context;
|
goto error_avformat_free_context;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||||
@ -244,7 +240,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
|
|
||||||
vs->encoder_ctx = avcodec_alloc_context3(encoder);
|
vs->encoder_ctx = avcodec_alloc_context3(encoder);
|
||||||
if (!vs->encoder_ctx) {
|
if (!vs->encoder_ctx) {
|
||||||
LOGC("Could not allocate codec context for v4l2");
|
LOG_OOM();
|
||||||
goto error_avio_close;
|
goto error_avio_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,13 +257,13 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
|
|
||||||
vs->frame = av_frame_alloc();
|
vs->frame = av_frame_alloc();
|
||||||
if (!vs->frame) {
|
if (!vs->frame) {
|
||||||
LOGE("Could not create v4l2 frame");
|
LOG_OOM();
|
||||||
goto error_avcodec_close;
|
goto error_avcodec_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
vs->packet = av_packet_alloc();
|
vs->packet = av_packet_alloc();
|
||||||
if (!vs->packet) {
|
if (!vs->packet) {
|
||||||
LOGE("Could not allocate packet");
|
LOG_OOM();
|
||||||
goto error_av_frame_free;
|
goto error_av_frame_free;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,7 +272,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
|||||||
vs->stopped = false;
|
vs->stopped = false;
|
||||||
|
|
||||||
LOGD("Starting v4l2 thread");
|
LOGD("Starting v4l2 thread");
|
||||||
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "v4l2", vs);
|
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "scrcpy-v4l2", vs);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start v4l2 thread");
|
LOGC("Could not start v4l2 thread");
|
||||||
goto error_av_packet_free;
|
goto error_av_packet_free;
|
||||||
|
@ -14,11 +14,13 @@ static struct sc_video_buffer_frame *
|
|||||||
sc_video_buffer_frame_new(const AVFrame *frame) {
|
sc_video_buffer_frame_new(const AVFrame *frame) {
|
||||||
struct sc_video_buffer_frame *vb_frame = malloc(sizeof(*vb_frame));
|
struct sc_video_buffer_frame *vb_frame = malloc(sizeof(*vb_frame));
|
||||||
if (!vb_frame) {
|
if (!vb_frame) {
|
||||||
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
vb_frame->frame = av_frame_alloc();
|
vb_frame->frame = av_frame_alloc();
|
||||||
if (!vb_frame->frame) {
|
if (!vb_frame->frame) {
|
||||||
|
LOG_OOM();
|
||||||
free(vb_frame);
|
free(vb_frame);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -132,14 +134,12 @@ sc_video_buffer_init(struct sc_video_buffer *vb, sc_tick buffering_time,
|
|||||||
if (buffering_time) {
|
if (buffering_time) {
|
||||||
ok = sc_mutex_init(&vb->b.mutex);
|
ok = sc_mutex_init(&vb->b.mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create mutex");
|
|
||||||
sc_frame_buffer_destroy(&vb->fb);
|
sc_frame_buffer_destroy(&vb->fb);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_cond_init(&vb->b.queue_cond);
|
ok = sc_cond_init(&vb->b.queue_cond);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create cond");
|
|
||||||
sc_mutex_destroy(&vb->b.mutex);
|
sc_mutex_destroy(&vb->b.mutex);
|
||||||
sc_frame_buffer_destroy(&vb->fb);
|
sc_frame_buffer_destroy(&vb->fb);
|
||||||
return false;
|
return false;
|
||||||
@ -147,7 +147,6 @@ sc_video_buffer_init(struct sc_video_buffer *vb, sc_tick buffering_time,
|
|||||||
|
|
||||||
ok = sc_cond_init(&vb->b.wait_cond);
|
ok = sc_cond_init(&vb->b.wait_cond);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not create wait cond");
|
|
||||||
sc_cond_destroy(&vb->b.queue_cond);
|
sc_cond_destroy(&vb->b.queue_cond);
|
||||||
sc_mutex_destroy(&vb->b.mutex);
|
sc_mutex_destroy(&vb->b.mutex);
|
||||||
sc_frame_buffer_destroy(&vb->fb);
|
sc_frame_buffer_destroy(&vb->fb);
|
||||||
@ -171,7 +170,7 @@ bool
|
|||||||
sc_video_buffer_start(struct sc_video_buffer *vb) {
|
sc_video_buffer_start(struct sc_video_buffer *vb) {
|
||||||
if (vb->buffering_time) {
|
if (vb->buffering_time) {
|
||||||
bool ok =
|
bool ok =
|
||||||
sc_thread_create(&vb->b.thread, run_buffering, "buffering", vb);
|
sc_thread_create(&vb->b.thread, run_buffering, "scrcpy-vbuf", vb);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not start buffering thread");
|
LOGE("Could not start buffering thread");
|
||||||
return false;
|
return false;
|
||||||
@ -234,7 +233,7 @@ sc_video_buffer_push(struct sc_video_buffer *vb, const AVFrame *frame) {
|
|||||||
struct sc_video_buffer_frame *vb_frame = sc_video_buffer_frame_new(frame);
|
struct sc_video_buffer_frame *vb_frame = sc_video_buffer_frame_new(frame);
|
||||||
if (!vb_frame) {
|
if (!vb_frame) {
|
||||||
sc_mutex_unlock(&vb->b.mutex);
|
sc_mutex_unlock(&vb->b.mutex);
|
||||||
LOGE("Could not allocate frame");
|
LOG_OOM();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
83
app/tests/test_adb_parser.c
Normal file
83
app/tests/test_adb_parser.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "adb_parser.h"
|
||||||
|
|
||||||
|
static void test_get_ip_single_line() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
|
"192.168.12.34\r\r\n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(ip);
|
||||||
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_single_line_without_eol() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
|
"192.168.12.34";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(ip);
|
||||||
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_single_line_with_trailing_space() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
|
"192.168.12.34 \n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(ip);
|
||||||
|
assert(!strcmp(ip, "192.168.12.34"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_multiline_first_ok() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
|
"192.168.1.2\r\n"
|
||||||
|
"10.0.0.0/24 dev rmnet proto kernel scope link src "
|
||||||
|
"10.0.0.2\r\n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(ip);
|
||||||
|
assert(!strcmp(ip, "192.168.1.2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_multiline_second_ok() {
|
||||||
|
char ip_route[] = "10.0.0.0/24 dev rmnet proto kernel scope link src "
|
||||||
|
"10.0.0.3\r\n"
|
||||||
|
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||||
|
"192.168.1.3\r\n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(ip);
|
||||||
|
assert(!strcmp(ip, "192.168.1.3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_no_wlan() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
|
"192.168.12.34\r\r\n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(!ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_get_ip_truncated() {
|
||||||
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
|
"\n";
|
||||||
|
|
||||||
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||||
|
assert(!ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
(void) argc;
|
||||||
|
(void) argv;
|
||||||
|
|
||||||
|
test_get_ip_single_line();
|
||||||
|
test_get_ip_single_line_without_eol();
|
||||||
|
test_get_ip_single_line_with_trailing_space();
|
||||||
|
test_get_ip_multiline_first_ok();
|
||||||
|
test_get_ip_multiline_second_ok();
|
||||||
|
test_get_ip_no_wlan();
|
||||||
|
test_get_ip_truncated();
|
||||||
|
}
|
@ -89,7 +89,7 @@ static void test_options(void) {
|
|||||||
assert(!strcmp(opts->serial, "0123456789abcdef"));
|
assert(!strcmp(opts->serial, "0123456789abcdef"));
|
||||||
assert(opts->show_touches);
|
assert(opts->show_touches);
|
||||||
assert(opts->turn_screen_off);
|
assert(opts->turn_screen_off);
|
||||||
assert(opts->prefer_text);
|
assert(opts->key_inject_mode == SC_KEY_INJECT_MODE_TEXT);
|
||||||
assert(!strcmp(opts->window_title, "my device"));
|
assert(!strcmp(opts->window_title, "my device"));
|
||||||
assert(opts->window_x == 100);
|
assert(opts->window_x == 100);
|
||||||
assert(opts->window_y == -1);
|
assert(opts->window_y == -1);
|
||||||
@ -129,25 +129,26 @@ static void test_parse_shortcut_mods(void) {
|
|||||||
ok = sc_parse_shortcut_mods("lctrl", &mods);
|
ok = sc_parse_shortcut_mods("lctrl", &mods);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
assert(mods.count == 1);
|
assert(mods.count == 1);
|
||||||
assert(mods.data[0] == SC_MOD_LCTRL);
|
assert(mods.data[0] == SC_SHORTCUT_MOD_LCTRL);
|
||||||
|
|
||||||
ok = sc_parse_shortcut_mods("lctrl+lalt", &mods);
|
ok = sc_parse_shortcut_mods("lctrl+lalt", &mods);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
assert(mods.count == 1);
|
assert(mods.count == 1);
|
||||||
assert(mods.data[0] == (SC_MOD_LCTRL | SC_MOD_LALT));
|
assert(mods.data[0] == (SC_SHORTCUT_MOD_LCTRL | SC_SHORTCUT_MOD_LALT));
|
||||||
|
|
||||||
ok = sc_parse_shortcut_mods("rctrl,lalt", &mods);
|
ok = sc_parse_shortcut_mods("rctrl,lalt", &mods);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
assert(mods.count == 2);
|
assert(mods.count == 2);
|
||||||
assert(mods.data[0] == SC_MOD_RCTRL);
|
assert(mods.data[0] == SC_SHORTCUT_MOD_RCTRL);
|
||||||
assert(mods.data[1] == SC_MOD_LALT);
|
assert(mods.data[1] == SC_SHORTCUT_MOD_LALT);
|
||||||
|
|
||||||
ok = sc_parse_shortcut_mods("lsuper,rsuper+lalt,lctrl+rctrl+ralt", &mods);
|
ok = sc_parse_shortcut_mods("lsuper,rsuper+lalt,lctrl+rctrl+ralt", &mods);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
assert(mods.count == 3);
|
assert(mods.count == 3);
|
||||||
assert(mods.data[0] == SC_MOD_LSUPER);
|
assert(mods.data[0] == SC_SHORTCUT_MOD_LSUPER);
|
||||||
assert(mods.data[1] == (SC_MOD_RSUPER | SC_MOD_LALT));
|
assert(mods.data[1] == (SC_SHORTCUT_MOD_RSUPER | SC_SHORTCUT_MOD_LALT));
|
||||||
assert(mods.data[2] == (SC_MOD_LCTRL | SC_MOD_RCTRL | SC_MOD_RALT));
|
assert(mods.data[2] == (SC_SHORTCUT_MOD_LCTRL | SC_SHORTCUT_MOD_RCTRL |
|
||||||
|
SC_SHORTCUT_MOD_RALT));
|
||||||
|
|
||||||
ok = sc_parse_shortcut_mods("", &mods);
|
ok = sc_parse_shortcut_mods("", &mods);
|
||||||
assert(!ok);
|
assert(!ok);
|
||||||
@ -169,4 +170,4 @@ int main(int argc, char *argv[]) {
|
|||||||
test_options2();
|
test_options2();
|
||||||
test_parse_shortcut_mods();
|
test_parse_shortcut_mods();
|
||||||
return 0;
|
return 0;
|
||||||
};
|
}
|
||||||
|
@ -54,7 +54,7 @@ static void test_serialize_inject_text_long(void) {
|
|||||||
struct control_msg msg;
|
struct control_msg msg;
|
||||||
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
||||||
char text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH + 1];
|
char text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH + 1];
|
||||||
memset(text, 'a', sizeof(text));
|
memset(text, 'a', CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
|
||||||
text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH] = '\0';
|
text[CONTROL_MSG_INJECT_TEXT_MAX_LENGTH] = '\0';
|
||||||
msg.inject_text.text = text;
|
msg.inject_text.text = text;
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ static void test_serialize_inject_touch_event(void) {
|
|||||||
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
||||||
.inject_touch_event = {
|
.inject_touch_event = {
|
||||||
.action = AMOTION_EVENT_ACTION_DOWN,
|
.action = AMOTION_EVENT_ACTION_DOWN,
|
||||||
.pointer_id = 0x1234567887654321L,
|
.pointer_id = UINT64_C(0x1234567887654321),
|
||||||
.position = {
|
.position = {
|
||||||
.point = {
|
.point = {
|
||||||
.x = 100,
|
.x = 100,
|
||||||
@ -210,14 +210,18 @@ static void test_serialize_collapse_panels(void) {
|
|||||||
static void test_serialize_get_clipboard(void) {
|
static void test_serialize_get_clipboard(void) {
|
||||||
struct control_msg msg = {
|
struct control_msg msg = {
|
||||||
.type = CONTROL_MSG_TYPE_GET_CLIPBOARD,
|
.type = CONTROL_MSG_TYPE_GET_CLIPBOARD,
|
||||||
|
.get_clipboard = {
|
||||||
|
.copy_key = GET_CLIPBOARD_COPY_KEY_COPY,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned char buf[CONTROL_MSG_MAX_SIZE];
|
unsigned char buf[CONTROL_MSG_MAX_SIZE];
|
||||||
size_t size = control_msg_serialize(&msg, buf);
|
size_t size = control_msg_serialize(&msg, buf);
|
||||||
assert(size == 1);
|
assert(size == 2);
|
||||||
|
|
||||||
const unsigned char expected[] = {
|
const unsigned char expected[] = {
|
||||||
CONTROL_MSG_TYPE_GET_CLIPBOARD,
|
CONTROL_MSG_TYPE_GET_CLIPBOARD,
|
||||||
|
GET_CLIPBOARD_COPY_KEY_COPY,
|
||||||
};
|
};
|
||||||
assert(!memcmp(buf, expected, sizeof(expected)));
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
||||||
}
|
}
|
||||||
@ -226,6 +230,7 @@ static void test_serialize_set_clipboard(void) {
|
|||||||
struct control_msg msg = {
|
struct control_msg msg = {
|
||||||
.type = CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
.type = CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
||||||
.set_clipboard = {
|
.set_clipboard = {
|
||||||
|
.sequence = UINT64_C(0x0102030405060708),
|
||||||
.paste = true,
|
.paste = true,
|
||||||
.text = "hello, world!",
|
.text = "hello, world!",
|
||||||
},
|
},
|
||||||
@ -233,10 +238,11 @@ static void test_serialize_set_clipboard(void) {
|
|||||||
|
|
||||||
unsigned char buf[CONTROL_MSG_MAX_SIZE];
|
unsigned char buf[CONTROL_MSG_MAX_SIZE];
|
||||||
size_t size = control_msg_serialize(&msg, buf);
|
size_t size = control_msg_serialize(&msg, buf);
|
||||||
assert(size == 19);
|
assert(size == 27);
|
||||||
|
|
||||||
const unsigned char expected[] = {
|
const unsigned char expected[] = {
|
||||||
CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
|
||||||
1, // paste
|
1, // paste
|
||||||
0x00, 0x00, 0x00, 0x0d, // text length
|
0x00, 0x00, 0x00, 0x0d, // text length
|
||||||
'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
|
'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
|
||||||
@ -244,6 +250,40 @@ static void test_serialize_set_clipboard(void) {
|
|||||||
assert(!memcmp(buf, expected, sizeof(expected)));
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_serialize_set_clipboard_long(void) {
|
||||||
|
struct control_msg msg = {
|
||||||
|
.type = CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
||||||
|
.set_clipboard = {
|
||||||
|
.sequence = UINT64_C(0x0102030405060708),
|
||||||
|
.paste = true,
|
||||||
|
.text = NULL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
char text[CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH + 1];
|
||||||
|
memset(text, 'a', CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
|
||||||
|
text[CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH] = '\0';
|
||||||
|
msg.set_clipboard.text = text;
|
||||||
|
|
||||||
|
unsigned char buf[CONTROL_MSG_MAX_SIZE];
|
||||||
|
size_t size = control_msg_serialize(&msg, buf);
|
||||||
|
assert(size == CONTROL_MSG_MAX_SIZE);
|
||||||
|
|
||||||
|
unsigned char expected[CONTROL_MSG_MAX_SIZE] = {
|
||||||
|
CONTROL_MSG_TYPE_SET_CLIPBOARD,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
|
||||||
|
1, // paste
|
||||||
|
// text length
|
||||||
|
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 24,
|
||||||
|
(CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 16) & 0xff,
|
||||||
|
(CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 8) & 0xff,
|
||||||
|
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH & 0xff,
|
||||||
|
};
|
||||||
|
memset(expected + 14, 'a', CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
|
||||||
|
|
||||||
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
||||||
|
}
|
||||||
|
|
||||||
static void test_serialize_set_screen_power_mode(void) {
|
static void test_serialize_set_screen_power_mode(void) {
|
||||||
struct control_msg msg = {
|
struct control_msg msg = {
|
||||||
.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
|
.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
|
||||||
@ -293,6 +333,7 @@ int main(int argc, char *argv[]) {
|
|||||||
test_serialize_collapse_panels();
|
test_serialize_collapse_panels();
|
||||||
test_serialize_get_clipboard();
|
test_serialize_get_clipboard();
|
||||||
test_serialize_set_clipboard();
|
test_serialize_set_clipboard();
|
||||||
|
test_serialize_set_clipboard_long();
|
||||||
test_serialize_set_screen_power_mode();
|
test_serialize_set_screen_power_mode();
|
||||||
test_serialize_rotate_device();
|
test_serialize_rotate_device();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -47,11 +47,26 @@ static void test_deserialize_clipboard_big(void) {
|
|||||||
device_msg_destroy(&msg);
|
device_msg_destroy(&msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_deserialize_ack_set_clipboard(void) {
|
||||||
|
const unsigned char input[] = {
|
||||||
|
DEVICE_MSG_TYPE_ACK_CLIPBOARD,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
|
||||||
|
};
|
||||||
|
|
||||||
|
struct device_msg msg;
|
||||||
|
ssize_t r = device_msg_deserialize(input, sizeof(input), &msg);
|
||||||
|
assert(r == 9);
|
||||||
|
|
||||||
|
assert(msg.type == DEVICE_MSG_TYPE_ACK_CLIPBOARD);
|
||||||
|
assert(msg.ack_clipboard.sequence == UINT64_C(0x0102030405060708));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
|
|
||||||
test_deserialize_clipboard();
|
test_deserialize_clipboard();
|
||||||
test_deserialize_clipboard_big();
|
test_deserialize_clipboard_big();
|
||||||
|
test_deserialize_ack_set_clipboard();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "util/str.h"
|
#include "util/str.h"
|
||||||
@ -363,6 +364,40 @@ static void test_truncate(void) {
|
|||||||
assert(!strcmp("hello", s4));
|
assert(!strcmp("hello", s4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_index_of_column(void) {
|
||||||
|
assert(sc_str_index_of_column("a bc d", 0, " ") == 0);
|
||||||
|
assert(sc_str_index_of_column("a bc d", 1, " ") == 2);
|
||||||
|
assert(sc_str_index_of_column("a bc d", 2, " ") == 6);
|
||||||
|
assert(sc_str_index_of_column("a bc d", 3, " ") == -1);
|
||||||
|
|
||||||
|
assert(sc_str_index_of_column("a ", 0, " ") == 0);
|
||||||
|
assert(sc_str_index_of_column("a ", 1, " ") == -1);
|
||||||
|
|
||||||
|
assert(sc_str_index_of_column("", 0, " ") == 0);
|
||||||
|
assert(sc_str_index_of_column("", 1, " ") == -1);
|
||||||
|
|
||||||
|
assert(sc_str_index_of_column("a \t \t bc \t d\t", 0, " \t") == 0);
|
||||||
|
assert(sc_str_index_of_column("a \t \t bc \t d\t", 1, " \t") == 8);
|
||||||
|
assert(sc_str_index_of_column("a \t \t bc \t d\t", 2, " \t") == 15);
|
||||||
|
assert(sc_str_index_of_column("a \t \t bc \t d\t", 3, " \t") == -1);
|
||||||
|
|
||||||
|
assert(sc_str_index_of_column(" a bc d", 1, " ") == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_remove_trailing_cr() {
|
||||||
|
char s[] = "abc\r";
|
||||||
|
sc_str_remove_trailing_cr(s, sizeof(s) - 1);
|
||||||
|
assert(!strcmp(s, "abc"));
|
||||||
|
|
||||||
|
char s2[] = "def\r\r\r\r";
|
||||||
|
sc_str_remove_trailing_cr(s2, sizeof(s2) - 1);
|
||||||
|
assert(!strcmp(s2, "def"));
|
||||||
|
|
||||||
|
char s3[] = "adb\rdef\r";
|
||||||
|
sc_str_remove_trailing_cr(s3, sizeof(s3) - 1);
|
||||||
|
assert(!strcmp(s3, "adb\rdef"));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
@ -383,5 +418,7 @@ int main(int argc, char *argv[]) {
|
|||||||
test_strlist_contains();
|
test_strlist_contains();
|
||||||
test_wrap_lines();
|
test_wrap_lines();
|
||||||
test_truncate();
|
test_truncate();
|
||||||
|
test_index_of_column();
|
||||||
|
test_remove_trailing_cr();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "util/strbuf.h"
|
#include "util/strbuf.h"
|
||||||
|
@ -7,7 +7,7 @@ buildscript {
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:7.0.2'
|
classpath 'com.android.tools.build:gradle:7.0.3'
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
34
bump_version
Executable file
34
bump_version
Executable file
@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# This script bump scrcpy version by editing all the necessary files.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
#
|
||||||
|
# ./bump_version 1.23.4
|
||||||
|
#
|
||||||
|
# Then check the diff manually to confirm that everything is ok.
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [[ $# != 1 ]]
|
||||||
|
then
|
||||||
|
echo "Syntax: $0 <version>" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
VERSION="$1"
|
||||||
|
|
||||||
|
a=( ${VERSION//./ } )
|
||||||
|
MAJOR="${a[0]:-0}"
|
||||||
|
MINOR="${a[1]:-0}"
|
||||||
|
PATCH="${a[2]:-0}"
|
||||||
|
|
||||||
|
# If VERSION is 1.23.4, then VERSION_CODE is 12304
|
||||||
|
VERSION_CODE="$(( $MAJOR * 10000 + $MINOR * 100 + "$PATCH" ))"
|
||||||
|
|
||||||
|
echo "$VERSION: major=$MAJOR minor=$MINOR patch=$PATCH [versionCode=$VERSION_CODE]"
|
||||||
|
sed -i "s/^\(\s*version: \)'[^']*'/\1'$VERSION'/" meson.build
|
||||||
|
sed -i "s/^\(\s*versionCode \).*/\1$VERSION_CODE/;s/^\(\s*versionName \).*/\1\"$VERSION\"/" server/build.gradle
|
||||||
|
sed -i "s/^\(SCRCPY_VERSION_NAME=\).*/\1$VERSION/" server/build_without_gradle.sh
|
||||||
|
sed -i "s/^\(\s*VALUE \"ProductVersion\", \)\"[^\"]*\"/\1\"$VERSION\"/" app/scrcpy-windows.rc
|
||||||
|
echo done
|
@ -7,6 +7,7 @@ cpp = 'i686-w64-mingw32-g++'
|
|||||||
ar = 'i686-w64-mingw32-ar'
|
ar = 'i686-w64-mingw32-ar'
|
||||||
strip = 'i686-w64-mingw32-strip'
|
strip = 'i686-w64-mingw32-strip'
|
||||||
pkgconfig = 'i686-w64-mingw32-pkg-config'
|
pkgconfig = 'i686-w64-mingw32-pkg-config'
|
||||||
|
windres = 'i686-w64-mingw32-windres'
|
||||||
|
|
||||||
[host_machine]
|
[host_machine]
|
||||||
system = 'windows'
|
system = 'windows'
|
||||||
@ -17,4 +18,4 @@ endian = 'little'
|
|||||||
[properties]
|
[properties]
|
||||||
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win32-shared'
|
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win32-shared'
|
||||||
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win32-dev'
|
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win32-dev'
|
||||||
prebuilt_sdl2 = 'SDL2-2.0.16/i686-w64-mingw32'
|
prebuilt_sdl2 = 'SDL2-2.0.18/i686-w64-mingw32'
|
||||||
|
@ -7,6 +7,7 @@ cpp = 'x86_64-w64-mingw32-g++'
|
|||||||
ar = 'x86_64-w64-mingw32-ar'
|
ar = 'x86_64-w64-mingw32-ar'
|
||||||
strip = 'x86_64-w64-mingw32-strip'
|
strip = 'x86_64-w64-mingw32-strip'
|
||||||
pkgconfig = 'x86_64-w64-mingw32-pkg-config'
|
pkgconfig = 'x86_64-w64-mingw32-pkg-config'
|
||||||
|
windres = 'x86_64-w64-mingw32-windres'
|
||||||
|
|
||||||
[host_machine]
|
[host_machine]
|
||||||
system = 'windows'
|
system = 'windows'
|
||||||
@ -17,4 +18,4 @@ endian = 'little'
|
|||||||
[properties]
|
[properties]
|
||||||
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win64-shared'
|
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win64-shared'
|
||||||
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win64-dev'
|
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win64-dev'
|
||||||
prebuilt_sdl2 = 'SDL2-2.0.16/x86_64-w64-mingw32'
|
prebuilt_sdl2 = 'SDL2-2.0.18/x86_64-w64-mingw32'
|
||||||
|
BIN
data/icon.ico
Normal file
BIN
data/icon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.6 KiB |
@ -2,8 +2,8 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
BUILDDIR=build-auto
|
BUILDDIR=build-auto
|
||||||
PREBUILT_SERVER_URL=https://github.com/Genymobile/scrcpy/releases/download/v1.20/scrcpy-server-v1.20
|
PREBUILT_SERVER_URL=https://github.com/Genymobile/scrcpy/releases/download/v1.21/scrcpy-server-v1.21
|
||||||
PREBUILT_SERVER_SHA256=b20aee4951f99b060c4a44000ba94de973f9604758ef62beb253b371aad3df34
|
PREBUILT_SERVER_SHA256=dbcccab523ee26796e55ea33652649e4b7af498edae9aa75e4d4d7869c0ab848
|
||||||
|
|
||||||
echo "[scrcpy] Downloading prebuilt server..."
|
echo "[scrcpy] Downloading prebuilt server..."
|
||||||
wget "$PREBUILT_SERVER_URL" -O scrcpy-server
|
wget "$PREBUILT_SERVER_URL" -O scrcpy-server
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
project('scrcpy', 'c',
|
project('scrcpy', 'c',
|
||||||
version: '1.20',
|
version: '1.21',
|
||||||
meson_version: '>= 0.48',
|
meson_version: '>= 0.48',
|
||||||
default_options: [
|
default_options: [
|
||||||
'c_std=c11',
|
'c_std=c11',
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
option('compile_app', type: 'boolean', value: true, description: 'Build the client')
|
option('compile_app', type: 'boolean', value: true, description: 'Build the client')
|
||||||
option('compile_server', type: 'boolean', value: true, description: 'Build the server')
|
option('compile_server', type: 'boolean', value: true, description: 'Build the server')
|
||||||
option('crossbuild_windows', type: 'boolean', value: false, description: 'Build for Windows from Linux')
|
|
||||||
option('prebuilt_server', type: 'string', description: 'Path of the prebuilt server')
|
option('prebuilt_server', type: 'string', description: 'Path of the prebuilt server')
|
||||||
option('portable', type: 'boolean', value: false, description: 'Use scrcpy-server from the same directory as the scrcpy executable')
|
option('portable', type: 'boolean', value: false, description: 'Use scrcpy-server from the same directory as the scrcpy executable')
|
||||||
option('server_debugger', type: 'boolean', value: false, description: 'Run a server debugger and wait for a client to be attached')
|
option('server_debugger', type: 'boolean', value: false, description: 'Run a server debugger and wait for a client to be attached')
|
||||||
|
@ -30,9 +30,9 @@ prepare-ffmpeg-dev-win64:
|
|||||||
ffmpeg-4.3.1-win64-dev
|
ffmpeg-4.3.1-win64-dev
|
||||||
|
|
||||||
prepare-sdl2:
|
prepare-sdl2:
|
||||||
@./prepare-dep https://libsdl.org/release/SDL2-devel-2.0.16-mingw.tar.gz \
|
@./prepare-dep https://libsdl.org/release/SDL2-devel-2.0.18-mingw.tar.gz \
|
||||||
2bfe48628aa9635c12eac7d421907e291525de1d0b04b3bca4a5bd6e6c881a6f \
|
bbad7c6947f6ca3e05292f065852ed8b62f319fc5533047e7708769c4dbae394 \
|
||||||
SDL2-2.0.16
|
SDL2-2.0.18
|
||||||
|
|
||||||
prepare-adb:
|
prepare-adb:
|
||||||
@./prepare-dep https://dl.google.com/android/repository/platform-tools_r31.0.3-windows.zip \
|
@./prepare-dep https://dl.google.com/android/repository/platform-tools_r31.0.3-windows.zip \
|
||||||
|
@ -70,7 +70,6 @@ build-win32: prepare-deps-win32
|
|||||||
meson "$(WIN32_BUILD_DIR)" \
|
meson "$(WIN32_BUILD_DIR)" \
|
||||||
--cross-file cross_win32.txt \
|
--cross-file cross_win32.txt \
|
||||||
--buildtype release --strip -Db_lto=true \
|
--buildtype release --strip -Db_lto=true \
|
||||||
-Dcrossbuild_windows=true \
|
|
||||||
-Dcompile_server=false \
|
-Dcompile_server=false \
|
||||||
-Dportable=true )
|
-Dportable=true )
|
||||||
ninja -C "$(WIN32_BUILD_DIR)"
|
ninja -C "$(WIN32_BUILD_DIR)"
|
||||||
@ -83,7 +82,6 @@ build-win64: prepare-deps-win64
|
|||||||
meson "$(WIN64_BUILD_DIR)" \
|
meson "$(WIN64_BUILD_DIR)" \
|
||||||
--cross-file cross_win64.txt \
|
--cross-file cross_win64.txt \
|
||||||
--buildtype release --strip -Db_lto=true \
|
--buildtype release --strip -Db_lto=true \
|
||||||
-Dcrossbuild_windows=true \
|
|
||||||
-Dcompile_server=false \
|
-Dcompile_server=false \
|
||||||
-Dportable=true )
|
-Dportable=true )
|
||||||
ninja -C "$(WIN64_BUILD_DIR)"
|
ninja -C "$(WIN64_BUILD_DIR)"
|
||||||
@ -103,7 +101,7 @@ dist-win32: build-server build-win32
|
|||||||
cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/SDL2-2.0.16/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
cp prebuilt-deps/SDL2-2.0.18/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
|
||||||
|
|
||||||
dist-win64: build-server build-win64
|
dist-win64: build-server build-win64
|
||||||
mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)"
|
mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)"
|
||||||
@ -120,7 +118,7 @@ dist-win64: build-server build-win64
|
|||||||
cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
cp prebuilt-deps/SDL2-2.0.16/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
cp prebuilt-deps/SDL2-2.0.18/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
|
||||||
|
|
||||||
zip-win32: dist-win32
|
zip-win32: dist-win32
|
||||||
cd "$(DIST)/$(WIN32_TARGET_DIR)"; \
|
cd "$(DIST)/$(WIN32_TARGET_DIR)"; \
|
||||||
|
@ -6,8 +6,8 @@ android {
|
|||||||
applicationId "com.genymobile.scrcpy"
|
applicationId "com.genymobile.scrcpy"
|
||||||
minSdkVersion 21
|
minSdkVersion 21
|
||||||
targetSdkVersion 31
|
targetSdkVersion 31
|
||||||
versionCode 12000
|
versionCode 12100
|
||||||
versionName "1.20"
|
versionName "1.21"
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
@ -12,10 +12,9 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
SCRCPY_DEBUG=false
|
SCRCPY_DEBUG=false
|
||||||
SCRCPY_VERSION_NAME=1.20
|
SCRCPY_VERSION_NAME=1.21
|
||||||
|
|
||||||
PLATFORM_VERSION=31
|
PLATFORM=${ANDROID_PLATFORM:-31}
|
||||||
PLATFORM=${ANDROID_PLATFORM:-$PLATFORM_VERSION}
|
|
||||||
BUILD_TOOLS=${ANDROID_BUILD_TOOLS:-31.0.0}
|
BUILD_TOOLS=${ANDROID_BUILD_TOOLS:-31.0.0}
|
||||||
|
|
||||||
BUILD_DIR="$(realpath ${BUILD_DIR:-build_manual})"
|
BUILD_DIR="$(realpath ${BUILD_DIR:-build_manual})"
|
||||||
@ -57,7 +56,7 @@ javac -bootclasspath "$ANDROID_JAR" -cp "$CLASSES_DIR" -d "$CLASSES_DIR" \
|
|||||||
echo "Dexing..."
|
echo "Dexing..."
|
||||||
cd "$CLASSES_DIR"
|
cd "$CLASSES_DIR"
|
||||||
|
|
||||||
if [[ $PLATFORM_VERSION -lt 31 ]]
|
if [[ $PLATFORM -lt 31 ]]
|
||||||
then
|
then
|
||||||
# use dx
|
# use dx
|
||||||
"$ANDROID_HOME/build-tools/$BUILD_TOOLS/dx" --dex \
|
"$ANDROID_HOME/build-tools/$BUILD_TOOLS/dx" --dex \
|
||||||
|
@ -21,7 +21,7 @@ public class CodecOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static List<CodecOption> parse(String codecOptions) {
|
public static List<CodecOption> parse(String codecOptions) {
|
||||||
if ("-".equals(codecOptions)) {
|
if (codecOptions.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,12 @@ public final class ControlMessage {
|
|||||||
public static final int TYPE_SET_SCREEN_POWER_MODE = 10;
|
public static final int TYPE_SET_SCREEN_POWER_MODE = 10;
|
||||||
public static final int TYPE_ROTATE_DEVICE = 11;
|
public static final int TYPE_ROTATE_DEVICE = 11;
|
||||||
|
|
||||||
|
public static final long SEQUENCE_INVALID = 0;
|
||||||
|
|
||||||
|
public static final int COPY_KEY_NONE = 0;
|
||||||
|
public static final int COPY_KEY_COPY = 1;
|
||||||
|
public static final int COPY_KEY_CUT = 2;
|
||||||
|
|
||||||
private int type;
|
private int type;
|
||||||
private String text;
|
private String text;
|
||||||
private int metaState; // KeyEvent.META_*
|
private int metaState; // KeyEvent.META_*
|
||||||
@ -29,8 +35,10 @@ public final class ControlMessage {
|
|||||||
private Position position;
|
private Position position;
|
||||||
private int hScroll;
|
private int hScroll;
|
||||||
private int vScroll;
|
private int vScroll;
|
||||||
|
private int copyKey;
|
||||||
private boolean paste;
|
private boolean paste;
|
||||||
private int repeat;
|
private int repeat;
|
||||||
|
private long sequence;
|
||||||
|
|
||||||
private ControlMessage() {
|
private ControlMessage() {
|
||||||
}
|
}
|
||||||
@ -79,9 +87,17 @@ public final class ControlMessage {
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ControlMessage createSetClipboard(String text, boolean paste) {
|
public static ControlMessage createGetClipboard(int copyKey) {
|
||||||
|
ControlMessage msg = new ControlMessage();
|
||||||
|
msg.type = TYPE_GET_CLIPBOARD;
|
||||||
|
msg.copyKey = copyKey;
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ControlMessage createSetClipboard(long sequence, String text, boolean paste) {
|
||||||
ControlMessage msg = new ControlMessage();
|
ControlMessage msg = new ControlMessage();
|
||||||
msg.type = TYPE_SET_CLIPBOARD;
|
msg.type = TYPE_SET_CLIPBOARD;
|
||||||
|
msg.sequence = sequence;
|
||||||
msg.text = text;
|
msg.text = text;
|
||||||
msg.paste = paste;
|
msg.paste = paste;
|
||||||
return msg;
|
return msg;
|
||||||
@ -147,6 +163,10 @@ public final class ControlMessage {
|
|||||||
return vScroll;
|
return vScroll;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getCopyKey() {
|
||||||
|
return copyKey;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getPaste() {
|
public boolean getPaste() {
|
||||||
return paste;
|
return paste;
|
||||||
}
|
}
|
||||||
@ -154,4 +174,8 @@ public final class ControlMessage {
|
|||||||
public int getRepeat() {
|
public int getRepeat() {
|
||||||
return repeat;
|
return repeat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getSequence() {
|
||||||
|
return sequence;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,12 @@ public class ControlMessageReader {
|
|||||||
static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
|
static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
|
||||||
static final int BACK_OR_SCREEN_ON_LENGTH = 1;
|
static final int BACK_OR_SCREEN_ON_LENGTH = 1;
|
||||||
static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
|
static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
|
||||||
static final int SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH = 1;
|
static final int GET_CLIPBOARD_LENGTH = 1;
|
||||||
|
static final int SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH = 9;
|
||||||
|
|
||||||
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
|
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
|
||||||
|
|
||||||
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 6; // type: 1 byte; paste flag: 1 byte; length: 4 bytes
|
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 14; // type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
|
||||||
public static final int INJECT_TEXT_MAX_LENGTH = 300;
|
public static final int INJECT_TEXT_MAX_LENGTH = 300;
|
||||||
|
|
||||||
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
|
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
|
||||||
@ -70,6 +71,9 @@ public class ControlMessageReader {
|
|||||||
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
|
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
|
||||||
msg = parseBackOrScreenOnEvent();
|
msg = parseBackOrScreenOnEvent();
|
||||||
break;
|
break;
|
||||||
|
case ControlMessage.TYPE_GET_CLIPBOARD:
|
||||||
|
msg = parseGetClipboard();
|
||||||
|
break;
|
||||||
case ControlMessage.TYPE_SET_CLIPBOARD:
|
case ControlMessage.TYPE_SET_CLIPBOARD:
|
||||||
msg = parseSetClipboard();
|
msg = parseSetClipboard();
|
||||||
break;
|
break;
|
||||||
@ -79,7 +83,6 @@ public class ControlMessageReader {
|
|||||||
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
|
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
|
||||||
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
|
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
|
||||||
case ControlMessage.TYPE_COLLAPSE_PANELS:
|
case ControlMessage.TYPE_COLLAPSE_PANELS:
|
||||||
case ControlMessage.TYPE_GET_CLIPBOARD:
|
|
||||||
case ControlMessage.TYPE_ROTATE_DEVICE:
|
case ControlMessage.TYPE_ROTATE_DEVICE:
|
||||||
msg = ControlMessage.createEmpty(type);
|
msg = ControlMessage.createEmpty(type);
|
||||||
break;
|
break;
|
||||||
@ -162,16 +165,25 @@ public class ControlMessageReader {
|
|||||||
return ControlMessage.createBackOrScreenOn(action);
|
return ControlMessage.createBackOrScreenOn(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ControlMessage parseGetClipboard() {
|
||||||
|
if (buffer.remaining() < GET_CLIPBOARD_LENGTH) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int copyKey = toUnsigned(buffer.get());
|
||||||
|
return ControlMessage.createGetClipboard(copyKey);
|
||||||
|
}
|
||||||
|
|
||||||
private ControlMessage parseSetClipboard() {
|
private ControlMessage parseSetClipboard() {
|
||||||
if (buffer.remaining() < SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH) {
|
if (buffer.remaining() < SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
long sequence = buffer.getLong();
|
||||||
boolean paste = buffer.get() != 0;
|
boolean paste = buffer.get() != 0;
|
||||||
String text = parseString();
|
String text = parseString();
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return ControlMessage.createSetClipboard(text, paste);
|
return ControlMessage.createSetClipboard(sequence, text, paste);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ControlMessage parseSetScreenPowerMode() {
|
private ControlMessage parseSetScreenPowerMode() {
|
||||||
|
@ -21,6 +21,7 @@ public class Controller {
|
|||||||
private final Device device;
|
private final Device device;
|
||||||
private final DesktopConnection connection;
|
private final DesktopConnection connection;
|
||||||
private final DeviceMessageSender sender;
|
private final DeviceMessageSender sender;
|
||||||
|
private final boolean clipboardAutosync;
|
||||||
|
|
||||||
private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
|
private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
|
||||||
|
|
||||||
@ -31,9 +32,10 @@ public class Controller {
|
|||||||
|
|
||||||
private boolean keepPowerModeOff;
|
private boolean keepPowerModeOff;
|
||||||
|
|
||||||
public Controller(Device device, DesktopConnection connection) {
|
public Controller(Device device, DesktopConnection connection, boolean clipboardAutosync) {
|
||||||
this.device = device;
|
this.device = device;
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
this.clipboardAutosync = clipboardAutosync;
|
||||||
initPointers();
|
initPointers();
|
||||||
sender = new DeviceMessageSender(connection);
|
sender = new DeviceMessageSender(connection);
|
||||||
}
|
}
|
||||||
@ -55,7 +57,7 @@ public class Controller {
|
|||||||
public void control() throws IOException {
|
public void control() throws IOException {
|
||||||
// on start, power on the device
|
// on start, power on the device
|
||||||
if (!Device.isScreenOn()) {
|
if (!Device.isScreenOn()) {
|
||||||
device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER);
|
device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC);
|
||||||
|
|
||||||
// dirty hack
|
// dirty hack
|
||||||
// After POWER is injected, the device is powered on asynchronously.
|
// After POWER is injected, the device is powered on asynchronously.
|
||||||
@ -114,13 +116,10 @@ public class Controller {
|
|||||||
Device.collapsePanels();
|
Device.collapsePanels();
|
||||||
break;
|
break;
|
||||||
case ControlMessage.TYPE_GET_CLIPBOARD:
|
case ControlMessage.TYPE_GET_CLIPBOARD:
|
||||||
String clipboardText = Device.getClipboardText();
|
getClipboard(msg.getCopyKey());
|
||||||
if (clipboardText != null) {
|
|
||||||
sender.pushClipboardText(clipboardText);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ControlMessage.TYPE_SET_CLIPBOARD:
|
case ControlMessage.TYPE_SET_CLIPBOARD:
|
||||||
setClipboard(msg.getText(), msg.getPaste());
|
setClipboard(msg.getText(), msg.getPaste(), msg.getSequence());
|
||||||
break;
|
break;
|
||||||
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
|
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
|
||||||
if (device.supportsInputEvents()) {
|
if (device.supportsInputEvents()) {
|
||||||
@ -144,7 +143,7 @@ public class Controller {
|
|||||||
if (keepPowerModeOff && action == KeyEvent.ACTION_UP && (keycode == KeyEvent.KEYCODE_POWER || keycode == KeyEvent.KEYCODE_WAKEUP)) {
|
if (keepPowerModeOff && action == KeyEvent.ACTION_UP && (keycode == KeyEvent.KEYCODE_POWER || keycode == KeyEvent.KEYCODE_WAKEUP)) {
|
||||||
schedulePowerModeOff();
|
schedulePowerModeOff();
|
||||||
}
|
}
|
||||||
return device.injectKeyEvent(action, keycode, repeat, metaState);
|
return device.injectKeyEvent(action, keycode, repeat, metaState, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean injectChar(char c) {
|
private boolean injectChar(char c) {
|
||||||
@ -155,7 +154,7 @@ public class Controller {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (KeyEvent event : events) {
|
for (KeyEvent event : events) {
|
||||||
if (!device.injectEvent(event)) {
|
if (!device.injectEvent(event, Device.INJECT_MODE_ASYNC)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +218,7 @@ public class Controller {
|
|||||||
MotionEvent event = MotionEvent
|
MotionEvent event = MotionEvent
|
||||||
.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source,
|
.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source,
|
||||||
0);
|
0);
|
||||||
return device.injectEvent(event);
|
return device.injectEvent(event, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean injectScroll(Position position, int hScroll, int vScroll) {
|
private boolean injectScroll(Position position, int hScroll, int vScroll) {
|
||||||
@ -242,7 +241,7 @@ public class Controller {
|
|||||||
MotionEvent event = MotionEvent
|
MotionEvent event = MotionEvent
|
||||||
.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEFAULT_DEVICE_ID, 0,
|
.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEFAULT_DEVICE_ID, 0,
|
||||||
InputDevice.SOURCE_MOUSE, 0);
|
InputDevice.SOURCE_MOUSE, 0);
|
||||||
return device.injectEvent(event);
|
return device.injectEvent(event, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -260,7 +259,7 @@ public class Controller {
|
|||||||
|
|
||||||
private boolean pressBackOrTurnScreenOn(int action) {
|
private boolean pressBackOrTurnScreenOn(int action) {
|
||||||
if (Device.isScreenOn()) {
|
if (Device.isScreenOn()) {
|
||||||
return device.injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0);
|
return device.injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Screen is off
|
// Screen is off
|
||||||
@ -273,10 +272,29 @@ public class Controller {
|
|||||||
if (keepPowerModeOff) {
|
if (keepPowerModeOff) {
|
||||||
schedulePowerModeOff();
|
schedulePowerModeOff();
|
||||||
}
|
}
|
||||||
return device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER);
|
return device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean setClipboard(String text, boolean paste) {
|
private void getClipboard(int copyKey) {
|
||||||
|
// On Android >= 7, press the COPY or CUT key if requested
|
||||||
|
if (copyKey != ControlMessage.COPY_KEY_NONE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && device.supportsInputEvents()) {
|
||||||
|
int key = copyKey == ControlMessage.COPY_KEY_COPY ? KeyEvent.KEYCODE_COPY : KeyEvent.KEYCODE_CUT;
|
||||||
|
// Wait until the event is finished, to ensure that the clipboard text we read just after is the correct one
|
||||||
|
device.pressReleaseKeycode(key, Device.INJECT_MODE_WAIT_FOR_FINISH);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If clipboard autosync is enabled, then the device clipboard is synchronized to the computer clipboard whenever it changes, in
|
||||||
|
// particular when COPY or CUT are injected, so it should not be synchronized twice. On Android < 7, do not synchronize at all rather than
|
||||||
|
// copying an old clipboard content.
|
||||||
|
if (!clipboardAutosync) {
|
||||||
|
String clipboardText = Device.getClipboardText();
|
||||||
|
if (clipboardText != null) {
|
||||||
|
sender.pushClipboardText(clipboardText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean setClipboard(String text, boolean paste, long sequence) {
|
||||||
boolean ok = device.setClipboardText(text);
|
boolean ok = device.setClipboardText(text);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
Ln.i("Device clipboard set");
|
Ln.i("Device clipboard set");
|
||||||
@ -284,7 +302,12 @@ public class Controller {
|
|||||||
|
|
||||||
// On Android >= 7, also press the PASTE key if requested
|
// On Android >= 7, also press the PASTE key if requested
|
||||||
if (paste && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && device.supportsInputEvents()) {
|
if (paste && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && device.supportsInputEvents()) {
|
||||||
device.pressReleaseKeycode(KeyEvent.KEYCODE_PASTE);
|
device.pressReleaseKeycode(KeyEvent.KEYCODE_PASTE, Device.INJECT_MODE_ASYNC);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sequence != ControlMessage.SEQUENCE_INVALID) {
|
||||||
|
// Acknowledgement requested
|
||||||
|
sender.pushAckClipboard(sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
|
@ -30,8 +30,13 @@ public final class DesktopConnection implements Closeable {
|
|||||||
private DesktopConnection(LocalSocket videoSocket, LocalSocket controlSocket) throws IOException {
|
private DesktopConnection(LocalSocket videoSocket, LocalSocket controlSocket) throws IOException {
|
||||||
this.videoSocket = videoSocket;
|
this.videoSocket = videoSocket;
|
||||||
this.controlSocket = controlSocket;
|
this.controlSocket = controlSocket;
|
||||||
controlInputStream = controlSocket.getInputStream();
|
if (controlSocket != null) {
|
||||||
controlOutputStream = controlSocket.getOutputStream();
|
controlInputStream = controlSocket.getInputStream();
|
||||||
|
controlOutputStream = controlSocket.getOutputStream();
|
||||||
|
} else {
|
||||||
|
controlInputStream = null;
|
||||||
|
controlOutputStream = null;
|
||||||
|
}
|
||||||
videoFd = videoSocket.getFileDescriptor();
|
videoFd = videoSocket.getFileDescriptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,31 +46,35 @@ public final class DesktopConnection implements Closeable {
|
|||||||
return localSocket;
|
return localSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DesktopConnection open(Device device, boolean tunnelForward) throws IOException {
|
public static DesktopConnection open(Device device, boolean tunnelForward, boolean control) throws IOException {
|
||||||
LocalSocket videoSocket;
|
LocalSocket videoSocket;
|
||||||
LocalSocket controlSocket;
|
LocalSocket controlSocket = null;
|
||||||
if (tunnelForward) {
|
if (tunnelForward) {
|
||||||
LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME);
|
LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME);
|
||||||
try {
|
try {
|
||||||
videoSocket = localServerSocket.accept();
|
videoSocket = localServerSocket.accept();
|
||||||
// send one byte so the client may read() to detect a connection error
|
// send one byte so the client may read() to detect a connection error
|
||||||
videoSocket.getOutputStream().write(0);
|
videoSocket.getOutputStream().write(0);
|
||||||
try {
|
if (control) {
|
||||||
controlSocket = localServerSocket.accept();
|
try {
|
||||||
} catch (IOException | RuntimeException e) {
|
controlSocket = localServerSocket.accept();
|
||||||
videoSocket.close();
|
} catch (IOException | RuntimeException e) {
|
||||||
throw e;
|
videoSocket.close();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
localServerSocket.close();
|
localServerSocket.close();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
videoSocket = connect(SOCKET_NAME);
|
videoSocket = connect(SOCKET_NAME);
|
||||||
try {
|
if (control) {
|
||||||
controlSocket = connect(SOCKET_NAME);
|
try {
|
||||||
} catch (IOException | RuntimeException e) {
|
controlSocket = connect(SOCKET_NAME);
|
||||||
videoSocket.close();
|
} catch (IOException | RuntimeException e) {
|
||||||
throw e;
|
videoSocket.close();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,9 +88,11 @@ public final class DesktopConnection implements Closeable {
|
|||||||
videoSocket.shutdownInput();
|
videoSocket.shutdownInput();
|
||||||
videoSocket.shutdownOutput();
|
videoSocket.shutdownOutput();
|
||||||
videoSocket.close();
|
videoSocket.close();
|
||||||
controlSocket.shutdownInput();
|
if (controlSocket != null) {
|
||||||
controlSocket.shutdownOutput();
|
controlSocket.shutdownInput();
|
||||||
controlSocket.close();
|
controlSocket.shutdownOutput();
|
||||||
|
controlSocket.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(String deviceName, int width, int height) throws IOException {
|
private void send(String deviceName, int width, int height) throws IOException {
|
||||||
|
@ -24,6 +24,10 @@ public final class Device {
|
|||||||
public static final int POWER_MODE_OFF = SurfaceControl.POWER_MODE_OFF;
|
public static final int POWER_MODE_OFF = SurfaceControl.POWER_MODE_OFF;
|
||||||
public static final int POWER_MODE_NORMAL = SurfaceControl.POWER_MODE_NORMAL;
|
public static final int POWER_MODE_NORMAL = SurfaceControl.POWER_MODE_NORMAL;
|
||||||
|
|
||||||
|
public static final int INJECT_MODE_ASYNC = InputManager.INJECT_INPUT_EVENT_MODE_ASYNC;
|
||||||
|
public static final int INJECT_MODE_WAIT_FOR_RESULT = InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT;
|
||||||
|
public static final int INJECT_MODE_WAIT_FOR_FINISH = InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH;
|
||||||
|
|
||||||
public static final int LOCK_VIDEO_ORIENTATION_UNLOCKED = -1;
|
public static final int LOCK_VIDEO_ORIENTATION_UNLOCKED = -1;
|
||||||
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
|
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
|
||||||
|
|
||||||
@ -65,7 +69,7 @@ public final class Device {
|
|||||||
|
|
||||||
int displayInfoFlags = displayInfo.getFlags();
|
int displayInfoFlags = displayInfo.getFlags();
|
||||||
|
|
||||||
screenInfo = ScreenInfo.computeScreenInfo(displayInfo, options.getCrop(), options.getMaxSize(), options.getLockedVideoOrientation());
|
screenInfo = ScreenInfo.computeScreenInfo(displayInfo, options.getCrop(), options.getMaxSize(), options.getLockVideoOrientation());
|
||||||
layerStack = displayInfo.getLayerStack();
|
layerStack = displayInfo.getLayerStack();
|
||||||
|
|
||||||
SERVICE_MANAGER.getWindowManager().registerRotationWatcher(new IRotationWatcher.Stub() {
|
SERVICE_MANAGER.getWindowManager().registerRotationWatcher(new IRotationWatcher.Stub() {
|
||||||
@ -82,8 +86,8 @@ public final class Device {
|
|||||||
}
|
}
|
||||||
}, displayId);
|
}, displayId);
|
||||||
|
|
||||||
if (options.getControl()) {
|
if (options.getControl() && options.getClipboardAutosync()) {
|
||||||
// If control is enabled, synchronize Android clipboard to the computer automatically
|
// If control and autosync are enabled, synchronize Android clipboard to the computer automatically
|
||||||
ClipboardManager clipboardManager = SERVICE_MANAGER.getClipboardManager();
|
ClipboardManager clipboardManager = SERVICE_MANAGER.getClipboardManager();
|
||||||
if (clipboardManager != null) {
|
if (clipboardManager != null) {
|
||||||
clipboardManager.addPrimaryClipChangedListener(new IOnPrimaryClipChangedListener.Stub() {
|
clipboardManager.addPrimaryClipChangedListener(new IOnPrimaryClipChangedListener.Stub() {
|
||||||
@ -164,7 +168,7 @@ public final class Device {
|
|||||||
return supportsInputEvents;
|
return supportsInputEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean injectEvent(InputEvent inputEvent, int displayId) {
|
public static boolean injectEvent(InputEvent inputEvent, int displayId, int injectMode) {
|
||||||
if (!supportsInputEvents(displayId)) {
|
if (!supportsInputEvents(displayId)) {
|
||||||
throw new AssertionError("Could not inject input event if !supportsInputEvents()");
|
throw new AssertionError("Could not inject input event if !supportsInputEvents()");
|
||||||
}
|
}
|
||||||
@ -173,30 +177,31 @@ public final class Device {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SERVICE_MANAGER.getInputManager().injectInputEvent(inputEvent, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
return SERVICE_MANAGER.getInputManager().injectInputEvent(inputEvent, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean injectEvent(InputEvent event) {
|
public boolean injectEvent(InputEvent event, int injectMode) {
|
||||||
return injectEvent(event, displayId);
|
return injectEvent(event, displayId, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int displayId) {
|
public static boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int displayId, int injectMode) {
|
||||||
long now = SystemClock.uptimeMillis();
|
long now = SystemClock.uptimeMillis();
|
||||||
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
|
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
|
||||||
InputDevice.SOURCE_KEYBOARD);
|
InputDevice.SOURCE_KEYBOARD);
|
||||||
return injectEvent(event, displayId);
|
return injectEvent(event, displayId, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
|
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int injectMode) {
|
||||||
return injectKeyEvent(action, keyCode, repeat, metaState, displayId);
|
return injectKeyEvent(action, keyCode, repeat, metaState, displayId, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean pressReleaseKeycode(int keyCode, int displayId) {
|
public static boolean pressReleaseKeycode(int keyCode, int displayId, int injectMode) {
|
||||||
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0, displayId) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId);
|
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0, displayId, injectMode)
|
||||||
|
&& injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean pressReleaseKeycode(int keyCode) {
|
public boolean pressReleaseKeycode(int keyCode, int injectMode) {
|
||||||
return pressReleaseKeycode(keyCode, displayId);
|
return pressReleaseKeycode(keyCode, displayId, injectMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isScreenOn() {
|
public static boolean isScreenOn() {
|
||||||
@ -272,7 +277,7 @@ public final class Device {
|
|||||||
if (!isScreenOn()) {
|
if (!isScreenOn()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId);
|
return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,9 +3,13 @@ package com.genymobile.scrcpy;
|
|||||||
public final class DeviceMessage {
|
public final class DeviceMessage {
|
||||||
|
|
||||||
public static final int TYPE_CLIPBOARD = 0;
|
public static final int TYPE_CLIPBOARD = 0;
|
||||||
|
public static final int TYPE_ACK_CLIPBOARD = 1;
|
||||||
|
|
||||||
|
public static final long SEQUENCE_INVALID = ControlMessage.SEQUENCE_INVALID;
|
||||||
|
|
||||||
private int type;
|
private int type;
|
||||||
private String text;
|
private String text;
|
||||||
|
private long sequence;
|
||||||
|
|
||||||
private DeviceMessage() {
|
private DeviceMessage() {
|
||||||
}
|
}
|
||||||
@ -17,6 +21,13 @@ public final class DeviceMessage {
|
|||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DeviceMessage createAckClipboard(long sequence) {
|
||||||
|
DeviceMessage event = new DeviceMessage();
|
||||||
|
event.type = TYPE_ACK_CLIPBOARD;
|
||||||
|
event.sequence = sequence;
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
public int getType() {
|
public int getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@ -24,4 +35,8 @@ public final class DeviceMessage {
|
|||||||
public String getText() {
|
public String getText() {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getSequence() {
|
||||||
|
return sequence;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ public final class DeviceMessageSender {
|
|||||||
|
|
||||||
private String clipboardText;
|
private String clipboardText;
|
||||||
|
|
||||||
|
private long ack;
|
||||||
|
|
||||||
public DeviceMessageSender(DesktopConnection connection) {
|
public DeviceMessageSender(DesktopConnection connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
@ -17,18 +19,34 @@ public final class DeviceMessageSender {
|
|||||||
notify();
|
notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void pushAckClipboard(long sequence) {
|
||||||
|
ack = sequence;
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
|
||||||
public void loop() throws IOException, InterruptedException {
|
public void loop() throws IOException, InterruptedException {
|
||||||
while (true) {
|
while (true) {
|
||||||
String text;
|
String text;
|
||||||
|
long sequence;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
while (clipboardText == null) {
|
while (ack == DeviceMessage.SEQUENCE_INVALID && clipboardText == null) {
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
text = clipboardText;
|
text = clipboardText;
|
||||||
clipboardText = null;
|
clipboardText = null;
|
||||||
|
|
||||||
|
sequence = ack;
|
||||||
|
ack = DeviceMessage.SEQUENCE_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sequence != DeviceMessage.SEQUENCE_INVALID) {
|
||||||
|
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
|
||||||
|
connection.sendDeviceMessage(event);
|
||||||
|
}
|
||||||
|
if (text != null) {
|
||||||
|
DeviceMessage event = DeviceMessage.createClipboard(text);
|
||||||
|
connection.sendDeviceMessage(event);
|
||||||
}
|
}
|
||||||
DeviceMessage event = DeviceMessage.createClipboard(text);
|
|
||||||
connection.sendDeviceMessage(event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public class DeviceMessageWriter {
|
|||||||
|
|
||||||
public void writeTo(DeviceMessage msg, OutputStream output) throws IOException {
|
public void writeTo(DeviceMessage msg, OutputStream output) throws IOException {
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
buffer.put((byte) DeviceMessage.TYPE_CLIPBOARD);
|
buffer.put((byte) msg.getType());
|
||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
case DeviceMessage.TYPE_CLIPBOARD:
|
case DeviceMessage.TYPE_CLIPBOARD:
|
||||||
String text = msg.getText();
|
String text = msg.getText();
|
||||||
@ -25,6 +25,10 @@ public class DeviceMessageWriter {
|
|||||||
buffer.put(raw, 0, len);
|
buffer.put(raw, 0, len);
|
||||||
output.write(rawBuffer, 0, buffer.position());
|
output.write(rawBuffer, 0, buffer.position());
|
||||||
break;
|
break;
|
||||||
|
case DeviceMessage.TYPE_ACK_CLIPBOARD:
|
||||||
|
buffer.putLong(msg.getSequence());
|
||||||
|
output.write(rawBuffer, 0, buffer.position());
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Ln.w("Unknown device message: " + msg.getType());
|
Ln.w("Unknown device message: " + msg.getType());
|
||||||
break;
|
break;
|
||||||
|
@ -2,22 +2,25 @@ package com.genymobile.scrcpy;
|
|||||||
|
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Options {
|
public class Options {
|
||||||
private Ln.Level logLevel;
|
private Ln.Level logLevel = Ln.Level.DEBUG;
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
private int bitRate;
|
private int bitRate = 8000000;
|
||||||
private int maxFps;
|
private int maxFps;
|
||||||
private int lockedVideoOrientation;
|
private int lockVideoOrientation = -1;
|
||||||
private boolean tunnelForward;
|
private boolean tunnelForward;
|
||||||
private Rect crop;
|
private Rect crop;
|
||||||
private boolean sendFrameMeta; // send PTS so that the client may record properly
|
private boolean sendFrameMeta = true; // send PTS so that the client may record properly
|
||||||
private boolean control;
|
private boolean control = true;
|
||||||
private int displayId;
|
private int displayId;
|
||||||
private boolean showTouches;
|
private boolean showTouches;
|
||||||
private boolean stayAwake;
|
private boolean stayAwake;
|
||||||
private String codecOptions;
|
private List<CodecOption> codecOptions;
|
||||||
private String encoderName;
|
private String encoderName;
|
||||||
private boolean powerOffScreenOnClose;
|
private boolean powerOffScreenOnClose;
|
||||||
|
private boolean clipboardAutosync = true;
|
||||||
|
|
||||||
public Ln.Level getLogLevel() {
|
public Ln.Level getLogLevel() {
|
||||||
return logLevel;
|
return logLevel;
|
||||||
@ -51,12 +54,12 @@ public class Options {
|
|||||||
this.maxFps = maxFps;
|
this.maxFps = maxFps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLockedVideoOrientation() {
|
public int getLockVideoOrientation() {
|
||||||
return lockedVideoOrientation;
|
return lockVideoOrientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLockedVideoOrientation(int lockedVideoOrientation) {
|
public void setLockVideoOrientation(int lockVideoOrientation) {
|
||||||
this.lockedVideoOrientation = lockedVideoOrientation;
|
this.lockVideoOrientation = lockVideoOrientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTunnelForward() {
|
public boolean isTunnelForward() {
|
||||||
@ -115,11 +118,11 @@ public class Options {
|
|||||||
this.stayAwake = stayAwake;
|
this.stayAwake = stayAwake;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCodecOptions() {
|
public List<CodecOption> getCodecOptions() {
|
||||||
return codecOptions;
|
return codecOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCodecOptions(String codecOptions) {
|
public void setCodecOptions(List<CodecOption> codecOptions) {
|
||||||
this.codecOptions = codecOptions;
|
this.codecOptions = codecOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,4 +141,12 @@ public class Options {
|
|||||||
public boolean getPowerOffScreenOnClose() {
|
public boolean getPowerOffScreenOnClose() {
|
||||||
return this.powerOffScreenOnClose;
|
return this.powerOffScreenOnClose;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getClipboardAutosync() {
|
||||||
|
return clipboardAutosync;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClipboardAutosync(boolean clipboardAutosync) {
|
||||||
|
this.clipboardAutosync = clipboardAutosync;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user