Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
virtualhid_input.cpp File Reference

Definitions for libvirtualhid-backed input helpers. More...

#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <format>
#include <limits>
#include <mutex>
#include <numbers>
#include <optional>
#include <random>
#include <string_view>
#include <utility>
#include "src/config.h"
#include "src/logging.h"
#include "virtualhid_input.h"
Include dependency graph for virtualhid_input.cpp:

Classes

struct  platf::virtualhid::gamepad_context_t
 Runtime state for one virtual gamepad. More...

Functions

void platf::abs_mouse (input_t &input, const touch_port_t &touch_port, float x, float y)
 Move the pointer to an absolute client-provided touch coordinate.
void platf::virtualhid::abs_mouse (input_context_t &context, const touch_port_t &touch_port, float x, float y)
 Move the virtual mouse absolutely inside a target touch port.
int platf::virtualhid::alloc_gamepad (input_context_t &context, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue)
 Allocate a libvirtualhid gamepad.
void platf::button_mouse (input_t &input, int button, bool release)
 Press or release a virtual mouse button.
void platf::virtualhid::button_mouse (input_context_t &context, int button, bool release)
 Submit a mouse button event.
bool platf::virtualhid::configured_gamepad_supports_controller_extensions ()
 Return whether the configured gamepad profile needs Moonlight controller extensions.
bool platf::virtualhid::configured_gamepad_supports_touchpad ()
 Return whether the configured gamepad profile can expose touchpad input.
std::unique_ptr< lvh::Runtime > platf::virtualhid::create_runtime (lvh::BackendKind backend=lvh::BackendKind::platform_default)
 Create a platform-default libvirtualhid runtime.
void platf::virtualhid::free_gamepad (input_context_t &context, int nr)
 Release a libvirtualhid gamepad slot.
void platf::virtualhid::gamepad_battery (input_context_t &context, const gamepad_battery_t &battery)
 Submit gamepad battery metadata.
void platf::virtualhid::gamepad_motion (input_context_t &context, const gamepad_motion_t &motion)
 Submit a gamepad motion event.
void platf::virtualhid::gamepad_touch (input_context_t &context, const gamepad_touch_t &touch)
 Submit a gamepad touch event.
void platf::virtualhid::gamepad_update (input_context_t &context, int nr, const gamepad_state_t &state)
 Submit a full gamepad state.
bool platf::virtualhid::has_gamepad (const input_context_t &context, int nr)
 Check whether a libvirtualhid gamepad exists in a slot.
void platf::hscroll (input_t &input, int distance)
 Apply a horizontal scroll event to the virtual mouse.
void platf::virtualhid::hscroll (input_context_t &context, int high_res_distance)
 Submit horizontal scroll input.
void platf::keyboard_update (input_t &input, uint16_t modcode, bool release, uint8_t flags, bool extended=false)
 Press or release a virtual keyboard key.
void platf::virtualhid::keyboard_update (input_context_t &context, std::uint16_t modcode, bool release, std::uint8_t flags, bool extended=false)
 Submit a keyboard key transition.
void platf::move_mouse (input_t &input, const touch_port_t &touch_port, int deltaX, int deltaY)
 Retarget the virtual mouse and move it relatively.
void platf::move_mouse (input_t &input, int deltaX, int deltaY)
 Move mouse using the backend coordinate system.
void platf::virtualhid::move_mouse (input_context_t &context, const touch_port_t &touch_port, int delta_x, int delta_y)
 Retarget the virtual mouse and move it relatively.
void platf::virtualhid::move_mouse (input_context_t &context, int delta_x, int delta_y)
 Move the virtual mouse relatively.
void platf::pen_update (client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen)
 Send a pen event to the OS.
void platf::virtualhid::pen_update (client_context_t &context, const touch_port_t &touch_port, const pen_input_t &pen)
 Submit a pen event.
int platf::virtualhid::rebind_gamepad (input_context_t &context, const gamepad_id_t &id, feedback_queue_t feedback_queue)
 Rebind an existing libvirtualhid gamepad to a resumed client session.
void platf::scroll (input_t &input, int distance)
 Apply a vertical scroll event to the virtual mouse.
void platf::virtualhid::scroll (input_context_t &context, int high_res_distance)
 Submit vertical scroll input.
bool platf::virtualhid::should_try_vigembus_fallback (std::string_view configured_gamepad, bool virtualhid_selected, std::string_view gamepad_driver)
 Decide whether ViGEmBus should be tried for a gamepad allocation.
bool platf::virtualhid::should_use_gamepad_runtime (const lvh::BackendCapabilities &capabilities, std::string_view gamepad_driver, bool virtualhid_licensed)
 Decide whether a libvirtualhid runtime may create a gamepad.
std::vector< supported_gamepad_t > platf::virtualhid::static_supported_gamepads ()
 Return static gamepad choices for config validation.
std::vector< supported_gamepad_t > platf::virtualhid::supported_gamepads (lvh::Runtime *runtime, bool fallback_vigem_available=false, bool virtualhid_licensed=true, bool require_license=false)
 Return gamepad choices and runtime availability.
void platf::touch_update (client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch)
 Send a touch event to the OS.
void platf::virtualhid::touch_update (client_context_t &context, const touch_port_t &touch_port, const touch_input_t &touch)
 Submit a touchscreen event.
void platf::unicode (input_t &input, const char *utf8, int size)
 Submit UTF-8 text input to the keyboard backend.
void platf::virtualhid::unicode (input_context_t &context, const char *utf8, int size)
 Submit UTF-8 text input.

Detailed Description

Definitions for libvirtualhid-backed input helpers.

Function Documentation

◆ abs_mouse() [1/2]

void platf::abs_mouse ( input_t & input,
const touch_port_t & touch_port,
float x,
float y )

Move the pointer to an absolute client-provided touch coordinate.

Parameters
inputPlatform input backend that receives the event.
touch_portTouch coordinate bounds used for scaling.
xHorizontal absolute coordinate from the client.
yVertical absolute coordinate from the client.

◆ abs_mouse() [2/2]

void platf::virtualhid::abs_mouse ( input_context_t & context,
const touch_port_t & touch_port,
float x,
float y )

Move the virtual mouse absolutely inside a target touch port.

Parameters
contextInput context.
touch_portTarget coordinate space.
xAbsolute X coordinate.
yAbsolute Y coordinate.

◆ alloc_gamepad()

int platf::virtualhid::alloc_gamepad ( input_context_t & context,
const gamepad_id_t & id,
const gamepad_arrival_t & metadata,
feedback_queue_t feedback_queue )

Allocate a libvirtualhid gamepad.

Parameters
contextInput context.
idSunshine gamepad identifiers.
metadataClient-reported controller metadata.
feedback_queueQueue used to return gamepad feedback to the client.
Returns
0 on success, otherwise -1.

◆ button_mouse() [1/2]

void platf::button_mouse ( input_t & input,
int button,
bool release )

Press or release a virtual mouse button.

Parameters
inputPlatform input backend that receives the event.
buttonMouse button identifier to press or release.
releaseWhether the key or button event is a release.

◆ button_mouse() [2/2]

void platf::virtualhid::button_mouse ( input_context_t & context,
int button,
bool release )

Submit a mouse button event.

Parameters
contextInput context.
buttonMoonlight mouse button.
releaseWhether the button was released.

◆ configured_gamepad_supports_controller_extensions()

bool platf::virtualhid::configured_gamepad_supports_controller_extensions ( )

Return whether the configured gamepad profile needs Moonlight controller extensions.

Moonlight uses the controller-touch feature flag to authorize both controller touchpad and motion packets.

Returns
True when controller touchpad or motion input should be advertised.

◆ configured_gamepad_supports_touchpad()

bool platf::virtualhid::configured_gamepad_supports_touchpad ( )

Return whether the configured gamepad profile can expose touchpad input.

Returns
True when controller touchpad input should be advertised.

◆ create_runtime()

std::unique_ptr< lvh::Runtime > platf::virtualhid::create_runtime ( lvh::BackendKind backend = lvh::BackendKind::platform_default)

Create a platform-default libvirtualhid runtime.

Parameters
backendBackend used to create the runtime.
Returns
Runtime instance.

◆ free_gamepad()

void platf::virtualhid::free_gamepad ( input_context_t & context,
int nr )

Release a libvirtualhid gamepad slot.

Parameters
contextInput context.
nrGamepad slot index.

◆ gamepad_battery()

void platf::virtualhid::gamepad_battery ( input_context_t & context,
const gamepad_battery_t & battery )

Submit gamepad battery metadata.

Parameters
contextInput context.
batterySunshine battery event.

◆ gamepad_motion()

void platf::virtualhid::gamepad_motion ( input_context_t & context,
const gamepad_motion_t & motion )

Submit a gamepad motion event.

Parameters
contextInput context.
motionSunshine motion event.

◆ gamepad_touch()

void platf::virtualhid::gamepad_touch ( input_context_t & context,
const gamepad_touch_t & touch )

Submit a gamepad touch event.

Parameters
contextInput context.
touchSunshine touch event.

◆ gamepad_update()

void platf::virtualhid::gamepad_update ( input_context_t & context,
int nr,
const gamepad_state_t & state )

Submit a full gamepad state.

Parameters
contextInput context.
nrGamepad slot index.
stateSunshine gamepad state.

◆ has_gamepad()

bool platf::virtualhid::has_gamepad ( const input_context_t & context,
int nr )

Check whether a libvirtualhid gamepad exists in a slot.

Parameters
contextInput context.
nrGamepad slot index.
Returns
True when a virtual gamepad is active.

◆ hscroll() [1/2]

void platf::hscroll ( input_t & input,
int distance )

Apply a horizontal scroll event to the virtual mouse.

Parameters
inputPlatform input backend that receives the event.
distanceHigh-resolution scroll distance reported by the client.

◆ hscroll() [2/2]

void platf::virtualhid::hscroll ( input_context_t & context,
int high_res_distance )

Submit horizontal scroll input.

Parameters
contextInput context.
high_res_distanceHigh-resolution scroll distance.

◆ keyboard_update() [1/2]

void platf::keyboard_update ( input_t & input,
uint16_t modcode,
bool release,
uint8_t flags,
bool extended = false )

Press or release a virtual keyboard key.

Parameters
inputPlatform input backend that receives the event.
modcodePortable key code to update.
releaseWhether the key or button event is a release.
flagsBit flags that modify the requested operation.
extendedWhether the client positively identified an extended key.

◆ keyboard_update() [2/2]

void platf::virtualhid::keyboard_update ( input_context_t & context,
std::uint16_t modcode,
bool release,
std::uint8_t flags,
bool extended = false )

Submit a keyboard key transition.

Parameters
contextInput context.
modcodePortable key code.
releaseWhether the key was released.
flagsBit flags that modify the requested operation.
extendedWhether the client positively identified an extended key.

◆ move_mouse() [1/4]

void platf::move_mouse ( input_t & input,
const touch_port_t & touch_port,
int deltaX,
int deltaY )

Retarget the virtual mouse and move it relatively.

Parameters
inputPlatform input backend that receives the event.
touch_portDesktop and streamed-display bounds for the pointer.
deltaXDelta x.
deltaYDelta y.

◆ move_mouse() [2/4]

void platf::move_mouse ( input_t & input,
int deltaX,
int deltaY )

Move mouse using the backend coordinate system.

Parameters
inputPlatform input backend that receives the event.
deltaXDelta x.
deltaYDelta y.

◆ move_mouse() [3/4]

void platf::virtualhid::move_mouse ( input_context_t & context,
const touch_port_t & touch_port,
int delta_x,
int delta_y )

Retarget the virtual mouse and move it relatively.

Parameters
contextInput context.
touch_portDesktop and streamed-display bounds for the pointer.
delta_xHorizontal delta.
delta_yVertical delta.

◆ move_mouse() [4/4]

void platf::virtualhid::move_mouse ( input_context_t & context,
int delta_x,
int delta_y )

Move the virtual mouse relatively.

Parameters
contextInput context.
delta_xHorizontal delta.
delta_yVertical delta.

◆ pen_update() [1/2]

void platf::pen_update ( client_input_t * input,
const touch_port_t & touch_port,
const pen_input_t & pen )

Send a pen event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
penThe pen event.

◆ pen_update() [2/2]

void platf::virtualhid::pen_update ( client_context_t & context,
const touch_port_t & touch_port,
const pen_input_t & pen )

Submit a pen event.

Parameters
contextClient context.
touch_portTouch coordinate bounds used for scaling.
penPen event.

◆ rebind_gamepad()

int platf::virtualhid::rebind_gamepad ( input_context_t & context,
const gamepad_id_t & id,
feedback_queue_t feedback_queue )

Rebind an existing libvirtualhid gamepad to a resumed client session.

Parameters
contextInput context.
idSunshine gamepad identifiers for the resumed client.
feedback_queueQueue used to return gamepad feedback to the resumed client.
Returns
0 when the gamepad exists and was rebound; otherwise -1.

◆ scroll() [1/2]

void platf::scroll ( input_t & input,
int distance )

Apply a vertical scroll event to the virtual mouse.

Parameters
inputPlatform input backend that receives the event.
distanceHigh-resolution scroll distance reported by the client.

◆ scroll() [2/2]

void platf::virtualhid::scroll ( input_context_t & context,
int high_res_distance )

Submit vertical scroll input.

Parameters
contextInput context.
high_res_distanceHigh-resolution scroll distance.

◆ should_try_vigembus_fallback()

bool platf::virtualhid::should_try_vigembus_fallback ( std::string_view configured_gamepad,
bool virtualhid_selected,
std::string_view gamepad_driver )

Decide whether ViGEmBus should be tried for a gamepad allocation.

When Virtual HID Driver is unavailable or deliberately bypassed, ViGEmBus uses automatic selection for profiles it cannot represent directly.

Parameters
configured_gamepadConfigured virtual gamepad profile.
virtualhid_selectedWhether Virtual HID Driver was selected for the allocation.
gamepad_driverConfigured Windows virtual gamepad driver policy.
Returns
True when Sunshine should attempt the ViGEmBus fallback.

◆ should_use_gamepad_runtime()

bool platf::virtualhid::should_use_gamepad_runtime ( const lvh::BackendCapabilities & capabilities,
std::string_view gamepad_driver,
bool virtualhid_licensed )

Decide whether a libvirtualhid runtime may create a gamepad.

Parameters
capabilitiesRuntime backend capabilities.
gamepad_driverConfigured Windows virtual gamepad driver policy.
virtualhid_licensedWhether the Virtual HID Driver machine license is valid.
Returns
True when the libvirtualhid runtime should receive gamepad allocations.

◆ static_supported_gamepads()

std::vector< supported_gamepad_t > platf::virtualhid::static_supported_gamepads ( )

Return static gamepad choices for config validation.

Returns
Supported gamepad choices.

◆ supported_gamepads()

std::vector< supported_gamepad_t > platf::virtualhid::supported_gamepads ( lvh::Runtime * runtime,
bool fallback_vigem_available = false,
bool virtualhid_licensed = true,
bool require_license = false )

Return gamepad choices and runtime availability.

Parameters
runtimeRuntime to probe.
fallback_vigem_availableWhether Windows ViGEm fallback can create gamepads.
virtualhid_licensedWhether the broker has a valid license.
require_licenseWhether this platform requires a broker license for gamepads.
Returns
Supported gamepad choices.

◆ touch_update() [1/2]

void platf::touch_update ( client_input_t * input,
const touch_port_t & touch_port,
const touch_input_t & touch )

Send a touch event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
touchThe touch event.

◆ touch_update() [2/2]

void platf::virtualhid::touch_update ( client_context_t & context,
const touch_port_t & touch_port,
const touch_input_t & touch )

Submit a touchscreen event.

Parameters
contextClient context.
touch_portTouch coordinate bounds used for scaling.
touchTouch event.

◆ unicode() [1/2]

void platf::unicode ( input_t & input,
const char * utf8,
int size )

Submit UTF-8 text input to the keyboard backend.

Parameters
inputPlatform input backend that receives the event.
utf8UTF-8 text submitted by the client.
sizeNumber of bytes or elements requested.

◆ unicode() [2/2]

void platf::virtualhid::unicode ( input_context_t & context,
const char * utf8,
int size )

Submit UTF-8 text input.

Parameters
contextInput context.
utf8UTF-8 text buffer.
sizeText buffer size.