Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
virtualhid.cpp
Go to the documentation of this file.
1
5#include "kwin_input.h"
6#include "src/config.h"
7#include "src/logging.h"
8#include "virtualhid_common.h"
10#include "virtualhid_keyboard.h"
11#include "virtualhid_mouse.h"
12
13#include <utility>
14
15using namespace std::literals;
16
17namespace platf::virtualhid {
18 platform_caps::caps_t input_capabilities(const lvh::BackendCapabilities &backend) {
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;
22 }
23 if (config::input.controller && backend.supports_gamepad && gamepad::supports_controller_extensions()) {
24 capabilities |= platform_caps::controller_touch;
25 }
26 return capabilities;
27 }
28
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;
35 } else {
36 BOOST_LOG(debug) << "[libvirtualhid] " << message;
37 }
38 };
39 return lvh::Runtime::create(std::move(options));
40 }
41
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;
48 return profile;
49 }
50
51 void log_failure(const char *operation, const lvh::OperationStatus &result) {
52 if (!result.ok()) {
53 BOOST_LOG(warning) << "[libvirtualhid] " << operation << ": " << result.message();
54 }
55 }
56} // namespace platf::virtualhid
57
58namespace platf {
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) {
63 if (!runtime) {
64 BOOST_LOG(error) << "[libvirtualhid] Failed to create Linux input runtime";
65 return;
66 }
67
68 lvh::CreateMouseOptions mouse_options;
69 mouse_options.profile = virtualhid::with_hermes_identity(
70 lvh::profiles::mouse(),
71 session_tag,
72 session_tag.empty() ? "Mouse passthrough" : "Hermes Session Mouse"
73 );
74 mouse_options.stable_id = session_tag + "-mouse";
75 auto created_mouse = runtime->create_mouse(mouse_options);
76 if (created_mouse) {
77 mouse = std::move(created_mouse.mouse);
78 } else {
79 BOOST_LOG(warning) << "[libvirtualhid] Mouse unavailable: " << created_mouse.status.message();
80 }
81
82 lvh::CreateKeyboardOptions keyboard_options;
83 keyboard_options.profile = virtualhid::with_hermes_identity(
84 lvh::profiles::keyboard(),
85 session_tag,
86 session_tag.empty() ? "Keyboard passthrough" : "Hermes Session Keyboard"
87 );
88 keyboard_options.stable_id = session_tag + "-keyboard";
89 // Hermes owns repeat timing so the backend must only emit explicit events.
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);
94 } else {
95 BOOST_LOG(warning) << "[libvirtualhid] Keyboard unavailable: " << created_keyboard.status.message();
96 }
97 }
98
99 input_t input(const std::string &session_tag) {
100 return {new input_raw_t(session_tag)};
101 }
102
103 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input) {
104 return std::make_unique<client_input_raw_t>(input);
105 }
106
107 void freeInput(void *pointer) {
108 delete static_cast<input_raw_t *>(pointer);
109 }
110
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);
114 }
115 mouse::move(static_cast<input_raw_t *>(input.get()), delta_x, delta_y);
116 }
117
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);
121 }
122 mouse::move_abs(static_cast<input_raw_t *>(input.get()), touch_port, x, y);
123 }
124
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);
128 }
129 mouse::button(static_cast<input_raw_t *>(input.get()), button, release);
130 }
131
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);
135 }
136 mouse::scroll(static_cast<input_raw_t *>(input.get()), high_resolution_distance);
137 }
138
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);
142 }
143 mouse::hscroll(static_cast<input_raw_t *>(input.get()), high_resolution_distance);
144 }
145
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);
149 }
150 keyboard::update(static_cast<input_raw_t *>(input.get()), key_code, release, flags);
151 }
152
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);
156 }
157 keyboard::unicode(static_cast<input_raw_t *>(input.get()), utf8, size);
158 }
159
160 void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch) {
161 if ((!config::input.mouse || !config::input.native_pen_touch) && touch.eventType != LI_TOUCH_EVENT_CANCEL_ALL) {
162 return;
163 }
164 auto *raw = static_cast<client_input_raw_t *>(input);
165 if (raw && kwin_input::active(raw->global)) {
166 return kwin_input::touch(raw, touch);
167 }
168 if (raw && raw->virtualhid_devices) {
169 virtualhid::touch_update(*raw->virtualhid_devices, touch_port, touch);
170 }
171 }
172
173 void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen) {
174 if ((!config::input.mouse || !config::input.native_pen_touch) && pen.eventType != LI_TOUCH_EVENT_CANCEL_ALL) {
175 return;
176 }
177 auto *raw = static_cast<client_input_raw_t *>(input);
178 if (raw && raw->virtualhid_devices) {
179 virtualhid::pen_update(*raw->virtualhid_devices, touch_port, pen);
180 }
181 }
182
183 int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue) {
184 if (auto *raw = static_cast<input_raw_t *>(input.get()); kwin_input::active(raw) && raw->session_tag.empty()) {
185 // Without a private /dev of its own, a gamepad would belong to every
186 // desktop of the user, not this one.
187 BOOST_LOG(warning) << "The detached desktop has no private /dev; refusing a gamepad the host would also see";
188 return -1;
189 }
190 return gamepad::alloc(static_cast<input_raw_t *>(input.get()), id, metadata, feedback_queue);
191 }
192
193 void free_gamepad(input_t &input, int index) {
194 gamepad::free(static_cast<input_raw_t *>(input.get()), index);
195 }
196
197 void release_gamepad(input_t &input, int index) {
198 gamepad::release(static_cast<input_raw_t *>(input.get()), index);
199 }
200
201 void forget_kept_gamepads(const std::string &session_tag) {
202 gamepad::forget(session_tag);
203 }
204
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);
207 }
208
209 void gamepad_touch(input_t &input, const gamepad_touch_t &touch) {
210 gamepad::touch(static_cast<input_raw_t *>(input.get()), touch);
211 }
212
213 void gamepad_motion(input_t &input, const gamepad_motion_t &motion) {
214 gamepad::motion(static_cast<input_raw_t *>(input.get()), motion);
215 }
216
217 void gamepad_battery(input_t &input, const gamepad_battery_t &battery) {
218 gamepad::battery(static_cast<input_raw_t *>(input.get()), battery);
219 }
220
221 platform_caps::caps_t get_capabilities() {
222 const auto runtime = virtualhid::create_runtime();
223 return runtime ? virtualhid::input_capabilities(runtime->capabilities()) : 0;
224 }
225
227 return mouse::get_location(static_cast<input_raw_t *>(input.get()));
228 }
229
230 std::vector<supported_gamepad_t> &supported_gamepads(input_t *input) {
231 return gamepad::supported_gamepads(input);
232 }
233} // namespace platf
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.
Keyboard, mouse and touch for a private KWin desktop, through its fake-input protocol.
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.