Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OTG support for Windows #3011

Merged
merged 10 commits into from
Feb 20, 2022
Merged

Add OTG support for Windows #3011

merged 10 commits into from
Feb 20, 2022

Conversation

rom1v
Copy link
Collaborator

@rom1v rom1v commented Feb 10, 2022

scrcpy v1.22 introduced a new OTG mode. It was based on HID keyboard introduced in v1.20 and HID mouse also introduced in v1.22. It was only supported on Linux.

On Windows, it is not possible to open a USB device if it is already open by another process (like the adb daemon). As a consequence, we can't send HID events if adb is running. Therefore, on Windows, --hid-keyboard and --hid-mouse while mirroring are not possible. These features are still limited to Linux.

However, in OTG mode (--otg), no adb is necessary, so it is possible to open the USB device to send HID events. This PR adds support for --otg on Windows.

Concretely:

  • download the unofficial release below
  • extract it
  • double-click on open_a_terminal_here.bat (extracted from the release)
  • plug an Android device over USB
  • execute scrcpy --otg

Fixes #2773


Here is an unofficial release with this branch, so that you can test and report issues:

Note that on my test computer with Windows 10 64bit, the win32 version does not work due to missing libgcc_s_sjlj-1.dll.
According to stackoverflow, a linker flag -static-libgcc must be added somewhere, but I think it is related to prebuilt libusb dll for win32 (since it works for win64).

EDIT: yes, it's a problem on the libusb prebuilt dll for win32: libusb/libusb#1049

@rom1v rom1v changed the base branch from master to dev February 10, 2022 08:55
@rom1v rom1v mentioned this pull request Feb 17, 2022
rom1v added a commit that referenced this pull request Feb 20, 2022
Currently, there is an issue with the libusb prebuilt dll.

Refs #3011 <#3011>
Refs libusb/#1049 <libusb/libusb#1049>
rom1v added 10 commits February 20, 2022 17:38
USB device disconnection is detected via a hotplug callback when it is
supported.

In addition, report disconnection on libusb calls returning
LIBUSB_ERROR_NO_DEVICE or LIBUSB_ERROR_NOT_FOUND. This allows to detect
disconnection after a libusb call when hotplug is not available.

PR #3011 <#3011>
If it fails, the error is already logged by sc_usb_register_callback().

PR #3011 <#3011>
Convert uint16_t to unsigned to avoid using PRIx16, which may not exist
on Windows.

PR #3011 <#3011>
Add LIBUSB_CALL so that the callback has the correct signature on
Windows (including __attribute__((stdcall))).

PR #3011 <#3011>
The HID/OTG features are now available on all platforms.

PR #3011 <#3011>
All adb commands are executed with an "interruptor", so that they can be
interrupted on Ctrl+C.

Make this interruptor optional, so that we could call "adb kill-server"
in OTG mode. This command always returns almost immediately anyway.

Ideally, we should make all blocking calls interruptible (including
libusb calls, by using the asynchronous API), but it's a lot of work,
and in practice it works well enough.

PR #3011 <#3011>
On Windows, it is not possible to open a USB device from several
process, so HID events may only work if no adb daemon is running.

PR #3011 <#3011>
On Windows, if the adb daemon is running, opening the USB device will
necessarily fail, so HID input is not possible.

Refs #2773 <#2773>
PR #3011 <#3011>
Currently, there is an issue with the libusb prebuilt dll.

Refs libusb/#1049 <libusb/libusb#1049>
PR #3011 <#3011>
@rom1v
Copy link
Collaborator Author

rom1v commented Feb 20, 2022

I disabled the feature for win32 due to this prebuilt dll issue. On Win64, it works as expected. I merge. 🚀

@rom1v rom1v merged commit d9bc508 into dev Feb 20, 2022
@quyleanh
Copy link
Contributor

quyleanh commented Feb 22, 2022

Thank you for your hard work.

Just a minor suggest so I don't want to throw new issue.

Do you have any plan to make a Github Action to build automatically for all platforms? For example when each PR is merged to dev branch, the test build will be built and others can test.

@rom1v
Copy link
Collaborator Author

rom1v commented Feb 22, 2022

There is a PR for GitHub Actions: #1709
But for some reason, gradle check didn't work and had to be removed for the CI.

I spend some time to figure out why, but didn't manage to make it work, then I worked on something else… I should retry (any help is welcome).

@sameerasw
Copy link

sameerasw commented Feb 23, 2022

DEBUG: Open USB device 04e8:6860: libusb error: Operation not supported or unimplemented on this platform

any workarounds? Running Android 11 on the device and also compatible with both OTG mouse and kb too. but not in scrcpy?
image

@rom1v
Copy link
Collaborator Author

rom1v commented Feb 23, 2022

Operation not supported or unimplemented on this platform

That might be an error on another USB device (that's why it's logged at debug level).

Is the device detected by adb (when USB debugging is enabled)?

PS: please post console output as text instead of screenshot.

@sameerasw
Copy link

Operation not supported or unimplemented on this platform

That might be an error on another USB device (that's why it's logged at debug level).

Is the device detected by adb (when USB debugging is enabled)?

PS: please post console output as text instead of screenshot.

Yes ,everything else works perfectly except just the OTG function and it says no USB devices. even if I connected multiple devices, the new error screen shows this as a USB device... could windows 11 be a problem?

@rom1v rom1v mentioned this pull request Feb 23, 2022
2 tasks
@rom1v
Copy link
Collaborator Author

rom1v commented Feb 23, 2022

@54m33r4 #3050

rom1v added a commit that referenced this pull request Apr 22, 2022
Upgrade and enable libusb support for Windows 32-bit builds.

Refs #3011 <#3011>
PR #3206 <#3206>
rom1v added a commit that referenced this pull request Apr 22, 2022
Upgrade and enable libusb support for Windows 32-bit builds.

Refs #3011 <#3011>
Fixes #3204 <#3204>
PR #3206 <#3206>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HID support on Windows
3 participants