Skip to content

Commit

Permalink
Add Windows build support for libusb
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
AlynxZhou committed Sep 18, 2021
1 parent 2aa94b8 commit f9e3be6
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ scrcpy --crop 1224:1440:0:0 # 1224x1440 at offset (0,0)

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

#### USB HID over AOAv2

Scrcpy can simulate a USB physical keyboard on Android to provide better input
experience, you need to connect your device via USB, not wireless.

However, due to some limitation of libusb and WinUSB driver, you cannot use HID
over AOAv2 on Windows.

Currently a USB serial number is needed to use HID over AOAv2.

```bash
scrcpy --serial XXXXXXXXXXXXXXXX # try HID first and fallback to inject
scrcpy --serial XXXXXXXXXXXXXXXX --input-mode hid # try HID and exit if failed
scrcpy --serial XXXXXXXXXXXXXXXX --input-mode inject # don't use HID
```

Serial number can be found by `adb get-serialno`.

#### Lock video orientation


Expand Down
14 changes: 13 additions & 1 deletion app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,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 Expand Up @@ -93,8 +93,20 @@ else
include_directories: include_directories(ffmpeg_include_dir)
)

prebuilt_libusb = meson.get_cross_property('prebuilt_libusb')
libusb_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_libusb + '/dll'
libusb_include_dir = '../prebuilt-deps/' + prebuilt_libusb + '/include'

libusb = declare_dependency(
dependencies: [
cc.find_library('libusb-1.0', dirs: libusb_bin_dir),
],
include_directories: include_directories(libusb_include_dir)
)

dependencies = [
ffmpeg,
libusb,
sdl2,
cc.find_library('mingw32')
]
Expand Down
5 changes: 4 additions & 1 deletion app/src/aoa_hid.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "util/log.h"

#include <assert.h>
#include <stdio.h>

#include "aoa_hid.h"

Expand Down Expand Up @@ -108,6 +109,7 @@ aoa_open_usb_handle(libusb_device *device, libusb_device_handle **handle) {
}

bool aoa_init(struct aoa *aoa, const struct scrcpy_options *options) {
aoa->usb_context = NULL;
aoa->usb_device = NULL;
aoa->usb_handle = NULL;
aoa->next_accessories_id = 0;
Expand All @@ -123,7 +125,7 @@ bool aoa_init(struct aoa *aoa, const struct scrcpy_options *options) {
return false;
}

libusb_init(NULL);
libusb_init(&aoa->usb_context);

aoa->usb_device = aoa_find_usb_device(options->serial);
if (!aoa->usb_device) {
Expand Down Expand Up @@ -313,6 +315,7 @@ void aoa_thread_join(struct aoa *aoa) {
void aoa_destroy(struct aoa *aoa) {
libusb_close(aoa->usb_handle);
libusb_unref_device(aoa->usb_device);
libusb_exit(aoa->usb_context);
sc_cond_destroy(&aoa->event_cond);
sc_mutex_destroy(&aoa->mutex);
}
1 change: 1 addition & 0 deletions app/src/aoa_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct hid_event {
struct hid_event_queue CBUF(struct hid_event, 64);

struct aoa {
libusb_context *usb_context;
libusb_device *usb_device;
libusb_device_handle *usb_handle;
sc_thread thread;
Expand Down
1 change: 1 addition & 0 deletions cross_win32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ endian = 'little'
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win32-shared'
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win32-dev'
prebuilt_sdl2 = 'SDL2-2.0.16/i686-w64-mingw32'
prebuilt_libusb = 'libusb-1.0.24/MinGW32'
1 change: 1 addition & 0 deletions cross_win64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ endian = 'little'
prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win64-shared'
prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win64-dev'
prebuilt_sdl2 = 'SDL2-2.0.16/x86_64-w64-mingw32'
prebuilt_libusb = 'libusb-1.0.24/MinGW64'
18 changes: 16 additions & 2 deletions prebuilt-deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
prepare-ffmpeg-dev-win32 \
prepare-ffmpeg-shared-win64 \
prepare-ffmpeg-dev-win64 \
prepare-libusb \
prepare-sdl2 \
prepare-adb

prepare-win32: prepare-sdl2 prepare-ffmpeg-shared-win32 prepare-ffmpeg-dev-win32 prepare-adb
prepare-win64: prepare-sdl2 prepare-ffmpeg-shared-win64 prepare-ffmpeg-dev-win64 prepare-adb
LIBUSB_DIR := libusb-1.0.24

prepare-win32: prepare-sdl2 prepare-ffmpeg-shared-win32 prepare-ffmpeg-dev-win32 prepare-libusb prepare-adb
prepare-win64: prepare-sdl2 prepare-ffmpeg-shared-win64 prepare-ffmpeg-dev-win64 prepare-libusb prepare-adb

prepare-ffmpeg-shared-win32:
@./prepare-dep https://github.com/Genymobile/scrcpy/releases/download/v1.16/ffmpeg-4.3.1-win32-shared.zip \
Expand All @@ -29,6 +32,17 @@ prepare-ffmpeg-dev-win64:
2e8038242cf8e1bd095c2978f196ff0462b122cc6ef7e74626a6af15459d8b81 \
ffmpeg-4.3.1-win64-dev

prepare-libusb:
# libusb put all things under the root of 7z file, so we pass the last
# argument which means creating extract dir manually.
@./prepare-dep https://github.com/libusb/libusb/releases/download/v1.0.24/libusb-1.0.24.7z \
620cec4dbe4868202949294157da5adb75c9fbb4f04266146fc833eef85f90fb \
"$(LIBUSB_DIR)" \
1
# Our code expects include dir under architechture dir.
cp -a "$(LIBUSB_DIR)"/include "$(LIBUSB_DIR)"/MinGW32
cp -a "$(LIBUSB_DIR)"/include "$(LIBUSB_DIR)"/MinGW64

prepare-sdl2:
@./prepare-dep https://libsdl.org/release/SDL2-devel-2.0.16-mingw.tar.gz \
2bfe48628aa9635c12eac7d421907e291525de1d0b04b3bca4a5bd6e6c881a6f \
Expand Down
29 changes: 25 additions & 4 deletions prebuilt-deps/prepare-dep
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -e
url="$1"
sum="$2"
dir="$3"
create_extract_dir="$4"

checksum() {
local file="$1"
Expand All @@ -27,13 +28,32 @@ get_file() {

extract() {
local file="$1"
local create_extract_dir="$2"
echo "Extracting $file..."
if [[ "$file" == *.zip ]]
then
unzip -q "$file"
if [[ -n "$create_extract_dir" ]]
then
unzip -q "$file" -d "$dir"
else
unzip -q "$file"
fi
elif [[ "$file" == *.tar.gz ]]
then
tar xf "$file"
if [[ -n "$create_extract_dir" ]]
then
tar xf "$file" --one-top-level="$dir"
else
tar xf "$file"
fi
elif [[ "$file" == *.7z ]]
then
if [[ -n "$create_extract_dir" ]]
then
7z x "$file" -o"$dir"
else
7z x "$file"
fi
else
echo "Unsupported file: $file"
return 1
Expand All @@ -44,15 +64,16 @@ get_dep() {
local url="$1"
local sum="$2"
local dir="$3"
local create_extract_dir="$4"
local file="${url##*/}"
if [[ -d "$dir" ]]
then
echo "$dir: found"
else
echo "$dir: not found"
get_file "$url" "$file" "$sum"
extract "$file"
extract "$file" "$create_extract_dir"
fi
}

get_dep "$url" "$sum" "$dir"
get_dep "$url" "$sum" "$dir" "$create_extract_dir"
2 changes: 2 additions & 0 deletions release.mk
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dist-win32: build-server build-win32
cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/avformat-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/swresample-3.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/swscale-5.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp prebuilt-deps/libusb-1.0.24/MinGW32/dll/libusb-1.0.dll "$(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/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
Expand All @@ -115,6 +116,7 @@ dist-win64: build-server build-win64
cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/avformat-58.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/swresample-3.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/swscale-5.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp prebuilt-deps/libusb-1.0.24/MinGW64/dll/libusb-1.0.dll "$(DIST)/$(WIN32_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/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
Expand Down

0 comments on commit f9e3be6

Please sign in to comment.