15using namespace std::literals;
17namespace platf::virtualhid {
19 platform_caps::caps_t capabilities = 0;
20 if (config::input.mouse && config::input.native_pen_touch && (backend.supports_touchscreen || backend.supports_pen_tablet)) {
21 capabilities |= platform_caps::pen_touch;
24 capabilities |= platform_caps::controller_touch;
29 std::unique_ptr<lvh::Runtime> create_runtime() {
30 lvh::RuntimeOptions options;
31 options.backend = lvh::BackendKind::platform_default;
32 options.log_callback = [](lvh::LogLevel level,
const std::string &message) {
33 if (level == lvh::LogLevel::error || level == lvh::LogLevel::warning) {
34 BOOST_LOG(warning) <<
"[libvirtualhid] " << message;
36 BOOST_LOG(debug) <<
"[libvirtualhid] " << message;
39 return lvh::Runtime::create(std::move(options));
42 lvh::DeviceProfile with_hermes_identity(lvh::DeviceProfile profile,
const std::string &session_tag,
const std::string &display_name) {
43 profile.vendor_id = VIRTUAL_INPUT_VENDOR_ID;
44 profile.product_id = VIRTUAL_INPUT_PRODUCT_ID;
45 profile.version = 0x111;
46 profile.name = display_name;
47 profile.linux_phys = session_tag;
51 void log_failure(
const char *operation,
const lvh::OperationStatus &result) {
53 BOOST_LOG(warning) <<
"[libvirtualhid] " << operation <<
": " << result.message();
59 input_raw_t::input_raw_t(std::string input_session_tag, std::unique_ptr<lvh::Runtime> input_runtime):
60 session_tag(std::move(input_session_tag)),
61 runtime(input_runtime ? std::move(input_runtime) : virtualhid::create_runtime()),
62 gamepads(MAX_GAMEPADS) {
64 BOOST_LOG(error) <<
"[libvirtualhid] Failed to create Linux input runtime";
68 lvh::CreateMouseOptions mouse_options;
69 mouse_options.profile = virtualhid::with_hermes_identity(
70 lvh::profiles::mouse(),
72 session_tag.empty() ?
"Mouse passthrough" :
"Hermes Session Mouse"
74 mouse_options.stable_id = session_tag +
"-mouse";
75 auto created_mouse = runtime->create_mouse(mouse_options);
77 mouse = std::move(created_mouse.mouse);
79 BOOST_LOG(warning) <<
"[libvirtualhid] Mouse unavailable: " << created_mouse.status.message();
82 lvh::CreateKeyboardOptions keyboard_options;
83 keyboard_options.profile = virtualhid::with_hermes_identity(
84 lvh::profiles::keyboard(),
86 session_tag.empty() ?
"Keyboard passthrough" :
"Hermes Session Keyboard"
88 keyboard_options.stable_id = session_tag +
"-keyboard";
90 keyboard_options.auto_repeat_interval_ms = 0;
91 auto created_keyboard = runtime->create_keyboard(keyboard_options);
92 if (created_keyboard) {
93 keyboard = std::move(created_keyboard.keyboard);
95 BOOST_LOG(warning) <<
"[libvirtualhid] Keyboard unavailable: " << created_keyboard.status.message();
99 input_t
input(
const std::string &session_tag) {
104 return std::make_unique<client_input_raw_t>(
input);
107 void freeInput(
void *pointer) {
108 delete static_cast<input_raw_t *
>(pointer);
111 void move_mouse(input_t &
input,
int delta_x,
int delta_y) {
112 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
113 return kwin_input::move(raw, delta_x, delta_y);
118 void abs_mouse(input_t &
input,
const touch_port_t &touch_port,
float x,
float y) {
119 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
120 return kwin_input::move_abs(raw, x, y);
125 void button_mouse(input_t &
input,
int button,
bool release) {
126 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
127 return kwin_input::button(raw, button, release);
132 void scroll(input_t &
input,
int high_resolution_distance) {
133 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
134 return kwin_input::scroll(raw, high_resolution_distance,
false);
136 mouse::scroll(
static_cast<input_raw_t *
>(
input.get()), high_resolution_distance);
139 void hscroll(input_t &
input,
int high_resolution_distance) {
140 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
141 return kwin_input::scroll(raw, high_resolution_distance,
true);
143 mouse::hscroll(
static_cast<input_raw_t *
>(
input.get()), high_resolution_distance);
146 void keyboard_update(input_t &
input, std::uint16_t key_code,
bool release, std::uint8_t flags) {
147 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
148 return kwin_input::key(raw, key_code, release);
150 keyboard::update(
static_cast<input_raw_t *
>(
input.get()), key_code, release, flags);
153 void unicode(input_t &
input,
char *utf8,
int size) {
154 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw)) {
155 return kwin_input::unicode(raw, utf8, size);
161 if ((!config::input.mouse || !config::input.native_pen_touch) && touch.eventType != LI_TOUCH_EVENT_CANCEL_ALL) {
165 if (raw && kwin_input::active(raw->global)) {
166 return kwin_input::touch(raw, touch);
168 if (raw && raw->virtualhid_devices) {
169 virtualhid::touch_update(*raw->virtualhid_devices, touch_port, touch);
174 if ((!config::input.mouse || !config::input.native_pen_touch) && pen.eventType != LI_TOUCH_EVENT_CANCEL_ALL) {
178 if (raw && raw->virtualhid_devices) {
179 virtualhid::pen_update(*raw->virtualhid_devices, touch_port, pen);
184 if (
auto *raw =
static_cast<input_raw_t *
>(
input.get()); kwin_input::active(raw) && raw->session_tag.empty()) {
187 BOOST_LOG(warning) <<
"The detached desktop has no private /dev; refusing a gamepad the host would also see";
190 return gamepad::alloc(
static_cast<input_raw_t *
>(
input.get()),
id, metadata, feedback_queue);
193 void free_gamepad(input_t &input,
int index) {
194 gamepad::free(
static_cast<input_raw_t *
>(input.get()), index);
205 void gamepad_update(input_t &input,
int index,
const gamepad_state_t &state) {
206 gamepad::update(
static_cast<input_raw_t *
>(input.get()), index, state);
222 const auto runtime = virtualhid::create_runtime();
231 return gamepad::supported_gamepads(
input);
std::vector< supported_gamepad_t > & supported_gamepads(input_t *input)
Gets the supported gamepads for this platform backend.
int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue)
Create a new virtual gamepad.
void forget_kept_gamepads(const std::string &session_tag)
Remove the gamepads a session tag kept between streams.
util::point_t get_mouse_loc(input_t &input)
Get the current mouse position on screen.
std::unique_ptr< client_input_t > allocate_client_input_context(input_t &input)
Allocate a context to store per-client input data.
void 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 gamepad_motion(input_t &input, const gamepad_motion_t &motion)
Send a gamepad motion event to the OS.
void gamepad_battery(input_t &input, const gamepad_battery_t &battery)
Send a gamepad battery event to the OS.
void release_gamepad(input_t &input, int nr)
Release a gamepad because its stream ended, not because the client removed it. A detached desktop kee...
platform_caps::caps_t get_capabilities()
Get the supported platform capabilities to advertise to the client.
void gamepad_touch(input_t &input, const gamepad_touch_t &touch)
Send a gamepad touch event to the OS.
input_t input(const std::string &session_tag={})
void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch)
Send a touch event to the OS.
Declarations for the configuration of Sunshine.
Declarations for logging related functions.
platform_caps::caps_t input_capabilities(const lvh::BackendCapabilities &backend)
Compute advertised protocol capabilities from backend availability and input policy.
Shared state for the Linux libvirtualhid input backend.
bool supports_controller_extensions()
Whether the configured controller profile can expose motion or touchpad input.
void release(input_raw_t *raw, int index)
Free a gamepad whose stream ended; a detached desktop keeps it.
void forget(const std::string &session_tag)
Close the gamepads kept for session_tag, and its runtime.
Gamepad translation for the Linux libvirtualhid backend.
Keyboard translation for the Linux libvirtualhid backend.
Mouse translation for the Linux libvirtualhid backend.