Skip to content

Add OTG mode (HID only) - #2974

Merged
rom1v merged 20 commits into
devfrom
otg
Jan 27, 2022
Merged

rom1v merged 20 commits into
devfrom
otg

Conversation

@rom1v

@rom1v rom1v commented Jan 26, 2022 •

Copy link
Copy Markdown
Collaborator

Add an option --otg to run scrcpy with only physical keyboard (#2632) and mouse (#2916) simulation, without mirroring and without requiring adb (USB debugging), as if the computer keyboard and mouse were plugged directly to the device via an OTG cable.

scrcpy --otg
# Pass the serial if several USB devices are available
scrcpy --otg -s 0123456789abcdef

This opens a window (with just the scrcpy logo, there is no mirroring), which when focused forwards the keyboard and mouse to the device:

otg

The mouse capture works the same way as in "normal" mode (as explained in #2916):

  • when the mouse is "captured", the mouse pointer disappears from the computer and appears on the Android device
  • a click in the window captures the mouse
  • special captures keys, either Alt or Super, toggle (disable or enable) the mouse capture (use one of them to give the control of the mouse back to the computer)

Like --hid-keyboard and --hid-mouse, --otg only works if the device is connected by USB, and is currently only supported on Linux.

@lucas-bortoli

Copy link
Copy Markdown

This is awesome! Nice work!

rom1v added 20 commits January 27, 2022 23:35
The condition actually determines whether scrcpy can use libusb or not.

PR #2974 <#2974>
The AOA code handled both USB initialization and AOA commands/events.
Extract USB-related code to a separate file and structure.

PR #2974 <#2974>
In practice, it is already mandatory.

PR #2974 <#2974>
This will allow to initialize a USB device separately and pass it to
sc_aoa.

PR #2974 <#2974>
It is possible to retrieve the device instance from the handle via
libusb_get_device(), so we don't need to reference the device one more
time.

PR #2974 <#2974>
An explicit context was used everywhere except for listing the devices.

PR #2974 <#2974>
This will allow to execute other USB calls (retrieving the device list
for example) before connecting to the selected device.

PR #2974 <#2974>
Use it from accept_device() to simplify (at the cost an additional
allocation for each serial, but it is not important).

It will also be useful in other functions in further commits.

PR #2974 <#2974>
The device was automatically found by sc_usb_connect(). Instead, expose
a function to find a device from a serial, and let the caller connect to
the device found (if any).

This will allow to list all devices first, then select one device to
connect to.

PR #2974 <#2974>
Introduce a structure to wrap a libusb_device and expose its descriptor
data read during discovery.

PR #2974 <#2974>
Several devices may match the requested serial, but above all, this
paves the way to list all devices (when no serial is provided).

PR #2974 <#2974>
If no serial is provided, then list all available USB devices (which can
be open and having a serial).

PR #2974 <#2974>
Acksync is used to delay HID events until some request (in practice,
device clipboard synchronization) is acknowledged by the device.

This mechanism will not be necessary for OTG mode.

PR #2974 <#2974>
The device disconnection is detected when the video socket closes.

In order to introduce an OTG mode (HID events) without mirroring (and
without server), we must be able to detect USB device disconnection.

This feature will only be used in OTG mode.

PR #2974 <#2974>
Input events helpers to convert from SDL events to scrcpy events were
implemented in input_manager. To reuse them for OTG mode, move them to
input_events.h.

PR #2974 <#2974>
Add an option --otg to run scrcpy with only physical keyboard and mouse
simulation (HID over AOA), without mirroring and without requiring adb.

To avoid adding complexity into the scrcpy initialization and screen
implementation, OTG mode is implemented totally separately, with a
separate window.

PR #2974 <#2974>
Allow to only enable HID keyboard or HID mouse:

    scrcpy --otg -K   # keyboard only
    scrcpy --otg -M   # mouse only
    scrcpy --otg -KM  # keyboard and mouse
    scrcpy --otg      # keyboard and mouse

PR #2974 <#2974>
A single click on the window must both give focus and capture the mouse.

PR #2974 <#2974>
@BachoSeven

Copy link
Copy Markdown

@rom1v Would it be possible to also add mirroring to this OTG mode which doesn't require USB debugging? This would be especially useful for broken screens :'D

thanks in advance

@rom1v

rom1v commented Feb 5, 2022

Copy link
Copy Markdown
Collaborator Author

@BachoSeven This is not possible without installing an app on the device and authorizing capture, so that would not work with a broken screen.

@vielavlrs

vielavlrs commented Feb 12, 2022 •

Copy link
Copy Markdown

I tried installing scrcpy v1.22 on Ubuntu 20.04 thru snap but the terminal shows my scrcpy is still v1.12? How do I run v1.22 in Ubuntu 20.04?

@rom1v

rom1v commented Feb 12, 2022

Copy link
Copy Markdown
Collaborator Author

I tried installing scrcpy v1.22 on Ubuntu 20.04 but the terminal shows my scrcpy is still v1.12? How do I run v1.22 in Ubuntu 20.04?

Uninstall the one from the distribution (apt remove scrcpy), and follow these steps to install the latest release.

rom1v added a commit that referenced this pull request Feb 3, 2026
Originally, the default scrcpy window always displayed the video stream.
Since the OTG mode window only contains the scrcpy logo, it was
implemented as a separate component [1].

Later, the --no-video option was added [2] to control the device without
mirroring (while still using an adb connection, unlike OTG mode). To
support this, sc_screen gained the ability to display a window
containing only the scrcpy logo, like in OTG mode.

As a result, the main sc_screen component can now be reused in OTG mode,
allowing removal of the OTG-specific duplicate implementation
(sc_screen_otg).

[1] commit 91418c7
[2] commit 8c650e5

Refs #2974 <#2974>
Refs #3978 <#3978>
rom1v added a commit to rom1v/scrcpy that referenced this pull request Feb 4, 2026
Originally, the default scrcpy window always displayed the video stream.
Since the OTG mode window only contains the scrcpy logo, it was
implemented as a separate component [1].

Later, the --no-video option was added [2] to control the device without
mirroring (while still using an adb connection, unlike OTG mode). To
support this, sc_screen gained the ability to display a window
containing only the scrcpy logo, like in OTG mode.

As a result, the main sc_screen component can now be reused in OTG mode,
allowing removal of the OTG-specific duplicate implementation
(sc_screen_otg).

[1] commit 91418c7
[2] commit 8c650e5

Refs Genymobile#2974 <Genymobile#2974>
Refs Genymobile#3978 <Genymobile#3978>
rom1v added a commit that referenced this pull request Feb 4, 2026
Originally, the default scrcpy window always displayed the video stream.
Since the OTG mode window only contains the scrcpy logo, it was
implemented as a separate component [1].

Later, the --no-video option was added [2] to control the device without
mirroring (while still using an adb connection, unlike OTG mode). To
support this, sc_screen gained the ability to display a window
containing only the scrcpy logo, like in OTG mode.

As a result, the main sc_screen component can now be reused in OTG mode,
allowing removal of the OTG-specific duplicate implementation
(sc_screen_otg).

[1] commit 91418c7
[2] commit 8c650e5

Refs #2974 <#2974>
Refs #3978 <#3978>
PR #6649 <#6649>
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.

4 participants