Skip to content

Commit

Permalink
Support USB HID over AoAv2 mode for keyboard input
Browse files Browse the repository at this point in the history
This provides a better input experience via
simulate physical keyboard, it converts
SDL_KeyboardEvent to proper HID events and send it
via HID over AoAv2.

This is a rewriting and bugfix of the origin code
from [@amosbird](https://github.com/amosbird).

Make sdl_keymod_to_hid_modifiers() more readable

Support MOD keys in HID mode

Enable Ctrl+V on HID mode

Support to send media events from hid_keyboard

Use existing --serial to replace --usb

Use explict option for input mode

Move HID keyboard setup code into functions

Send HID events in separated thread

Let libusb handle max package size

Fix HID keyboard report desc
  • Loading branch information
AlynxZhou committed Sep 18, 2021
1 parent b5e98db commit 2aa94b8
Show file tree
Hide file tree
Showing 15 changed files with 997 additions and 19 deletions.
15 changes: 9 additions & 6 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ Install the required packages from your package manager.

```bash
# runtime dependencies
sudo apt install ffmpeg libsdl2-2.0-0 adb
sudo apt install ffmpeg libsdl2-2.0-0 adb libusb-1.0-0

# client build dependencies
sudo apt install gcc git pkg-config meson ninja-build libsdl2-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev\
libusb-dev

# server build dependencies
sudo apt install openjdk-11-jdk
Expand All @@ -114,7 +115,7 @@ pip3 install meson
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

# client build dependencies
sudo dnf install SDL2-devel ffms2-devel meson gcc make
sudo dnf install SDL2-devel ffms2-devel libusb-devel meson gcc make

# server build dependencies
sudo dnf install java-devel
Expand Down Expand Up @@ -159,7 +160,8 @@ install the required packages:
```bash
# runtime dependencies
pacman -S mingw-w64-x86_64-SDL2 \
mingw-w64-x86_64-ffmpeg
mingw-w64-x86_64-ffmpeg \
mingw-w64-x86_64-libusb

# client build dependencies
pacman -S mingw-w64-x86_64-make \
Expand All @@ -173,7 +175,8 @@ For a 32 bits version, replace `x86_64` by `i686`:
```bash
# runtime dependencies
pacman -S mingw-w64-i686-SDL2 \
mingw-w64-i686-ffmpeg
mingw-w64-i686-ffmpeg \
mingw-w64-i686-libusb

# client build dependencies
pacman -S mingw-w64-i686-make \
Expand All @@ -197,7 +200,7 @@ Install the packages with [Homebrew]:

```bash
# runtime dependencies
brew install sdl2 ffmpeg
brew install sdl2 ffmpeg libusb

# client build dependencies
brew install pkg-config meson
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ scrcpy --crop 1224:1440:0:0 # 1224x1440 at offset (0,0)

If `--max-size` is also specified, resizing is applied after cropping.


#### Lock video orientation


Expand Down
5 changes: 4 additions & 1 deletion app/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
src = [
'src/main.c',
'src/adb.c',
'src/aoa_hid.c',
'src/cli.c',
'src/clock.c',
'src/compat.c',
Expand All @@ -12,6 +13,7 @@ src = [
'src/file_handler.c',
'src/fps_counter.c',
'src/frame_buffer.c',
'src/hid_keyboard.c',
'src/input_manager.c',
'src/opengl.c',
'src/receiver.c',
Expand Down Expand Up @@ -54,6 +56,7 @@ if not get_option('crossbuild_windows')
dependency('libavformat'),
dependency('libavcodec'),
dependency('libavutil'),
dependency('libusb-1.0'),
dependency('sdl2'),
]

Expand All @@ -62,7 +65,7 @@ if not get_option('crossbuild_windows')
endif

else

# TODO: add libusb dependency for windows.
# cross-compile mingw32 build (from Linux to Windows)
prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2')
sdl2_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_sdl2 + '/bin'
Expand Down
12 changes: 12 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ Start in fullscreen.
.B \-h, \-\-help
Print this help.

.TP
.B \-i, \-\-input\-mode mode
Select input mode for keyboard events.

Possible values are "auto", "hid" and "inject".

"auto" is default if not specified, which attemps "hid" first and will fallback to "inject" if failed.

"hid" uses Android's USB HID over AOAv2 feature to simulate physical keyboard's events, which provides better experience for IME users if supported by you device.

"inject" is the legacy scrcpy way by injecting keycode events on Android, works on most devices.

.TP
.B \-\-legacy\-paste
Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+Shift+v).
Expand Down
7 changes: 7 additions & 0 deletions app/src/adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,10 @@ adb_install(const char *serial, const char *local) {

return proc;
}

process_t
adb_get_serialno(const char *serial) {
const char *const adb_cmd[] = {"get-serialno"};
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return proc;
}
3 changes: 3 additions & 0 deletions app/src/adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ adb_push(const char *serial, const char *local, const char *remote);
process_t
adb_install(const char *serial, const char *local);

process_t
adb_get_serialno(const char *serial);

#endif
Loading

0 comments on commit 2aa94b8

Please sign in to comment.