-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathvirtualhid_input.h
More file actions
282 lines (246 loc) · 8.68 KB
/
Copy pathvirtualhid_input.h
File metadata and controls
282 lines (246 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @file src/platform/virtualhid_input.h
* @brief Declarations for libvirtualhid-backed input helpers.
*/
#pragma once
// standard includes
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
// lib includes
#include <libvirtualhid/libvirtualhid.hpp>
// local includes
#include "src/platform/common.h"
namespace platf::virtualhid {
/**
* @brief Runtime and virtual devices owned by one platform input context.
*/
struct input_context_t {
/**
* @brief Construct the libvirtualhid input context using the platform-default backend.
*/
input_context_t();
/**
* @brief Construct the libvirtualhid input context using a selected backend.
*
* @param backend Backend used to create the libvirtualhid runtime.
*/
explicit input_context_t(lvh::BackendKind backend);
/**
* @brief Recreate the shared keyboard using the runtime's current driver and license state.
*/
void refresh_keyboard();
/**
* @brief Recreate the shared mouse using the runtime's current driver and license state.
*/
void refresh_mouse();
std::unique_ptr<lvh::Runtime> runtime; ///< libvirtualhid runtime.
std::unique_ptr<lvh::Keyboard> keyboard; ///< Shared virtual keyboard.
std::unique_ptr<lvh::Mouse> mouse; ///< Shared virtual mouse.
std::vector<std::shared_ptr<struct gamepad_context_t>> gamepads {static_cast<std::size_t>(MAX_GAMEPADS)}; ///< Virtual gamepad slots.
};
/**
* @brief Per-client virtual touch and pen state.
*/
struct client_context_t {
/**
* @brief Create per-client libvirtualhid devices.
*
* @param input Global input context.
*/
explicit client_context_t(input_context_t &input);
input_context_t *global = nullptr; ///< Shared global input context.
std::unique_ptr<lvh::Touchscreen> touch; ///< Per-client touchscreen.
std::unique_ptr<lvh::PenTablet> pen; ///< Per-client pen tablet.
std::set<std::int32_t> active_touches; ///< Active touchscreen contacts.
std::set<lvh::PenButton> pressed_pen_buttons; ///< Active pen tablet buttons.
};
/**
* @brief Return the shared libvirtualhid context from a platform input backend.
*
* @param input Platform input backend.
* @return Shared libvirtualhid input context.
*/
input_context_t &get_input_context(input_t &input);
/**
* @brief Return the per-client libvirtualhid context from a platform input backend.
*
* @param input Per-client platform input backend.
* @return Per-client libvirtualhid input context.
*/
client_context_t &get_client_context(client_input_t *input);
/**
* @brief Create a platform-default libvirtualhid runtime.
*
* @param backend Backend used to create the runtime.
* @return Runtime instance.
*/
std::unique_ptr<lvh::Runtime> create_runtime(lvh::BackendKind backend = lvh::BackendKind::platform_default);
/**
* @brief Return static gamepad choices for config validation.
*
* @return Supported gamepad choices.
*/
std::vector<supported_gamepad_t> static_supported_gamepads();
/**
* @brief Return gamepad choices and runtime availability.
*
* @param runtime Runtime to probe.
* @param fallback_vigem_available Whether Windows ViGEm fallback can create gamepads.
* @return Supported gamepad choices.
*/
std::vector<supported_gamepad_t> supported_gamepads(lvh::Runtime *runtime, bool fallback_vigem_available = false);
/**
* @brief Allocate a libvirtualhid gamepad.
*
* @param context Input context.
* @param id Sunshine gamepad identifiers.
* @param metadata Client-reported controller metadata.
* @param feedback_queue Queue used to return gamepad feedback to the client.
* @return 0 on success, otherwise -1.
*/
int alloc_gamepad(input_context_t &context, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
/**
* @brief Rebind an existing libvirtualhid gamepad to a resumed client session.
*
* @param context Input context.
* @param id Sunshine gamepad identifiers for the resumed client.
* @param feedback_queue Queue used to return gamepad feedback to the resumed client.
* @return 0 when the gamepad exists and was rebound; otherwise -1.
*/
int rebind_gamepad(input_context_t &context, const gamepad_id_t &id, feedback_queue_t feedback_queue);
/**
* @brief Check whether a libvirtualhid gamepad exists in a slot.
*
* @param context Input context.
* @param nr Gamepad slot index.
* @return True when a virtual gamepad is active.
*/
bool has_gamepad(const input_context_t &context, int nr);
#ifdef SUNSHINE_TESTS
/**
* @brief Return the adapter allocated in a gamepad slot for unit testing.
*
* @param context Input context.
* @param nr Gamepad slot index.
* @return Allocated adapter, or `nullptr` when the slot is inactive.
*/
lvh::GamepadStateAdapter *gamepad_adapter_for_testing(input_context_t &context, int nr);
#endif
/**
* @brief Release a libvirtualhid gamepad slot.
*
* @param context Input context.
* @param nr Gamepad slot index.
*/
void free_gamepad(input_context_t &context, int nr);
/**
* @brief Submit a full gamepad state.
*
* @param context Input context.
* @param nr Gamepad slot index.
* @param state Sunshine gamepad state.
*/
void gamepad_update(input_context_t &context, int nr, const gamepad_state_t &state);
/**
* @brief Submit a gamepad touch event.
*
* @param context Input context.
* @param touch Sunshine touch event.
*/
void gamepad_touch(input_context_t &context, const gamepad_touch_t &touch);
/**
* @brief Submit a gamepad motion event.
*
* @param context Input context.
* @param motion Sunshine motion event.
*/
void gamepad_motion(input_context_t &context, const gamepad_motion_t &motion);
/**
* @brief Submit gamepad battery metadata.
*
* @param context Input context.
* @param battery Sunshine battery event.
*/
void gamepad_battery(input_context_t &context, const gamepad_battery_t &battery);
/**
* @brief Move the virtual mouse relatively.
*
* @param context Input context.
* @param delta_x Horizontal delta.
* @param delta_y Vertical delta.
*/
void move_mouse(input_context_t &context, int delta_x, int delta_y);
/**
* @brief Move the virtual mouse absolutely inside a target touch port.
*
* @param context Input context.
* @param touch_port Target coordinate space.
* @param x Absolute X coordinate.
* @param y Absolute Y coordinate.
*/
void abs_mouse(input_context_t &context, const touch_port_t &touch_port, float x, float y);
/**
* @brief Submit a mouse button event.
*
* @param context Input context.
* @param button Moonlight mouse button.
* @param release Whether the button was released.
*/
void button_mouse(input_context_t &context, int button, bool release);
/**
* @brief Submit vertical scroll input.
*
* @param context Input context.
* @param high_res_distance High-resolution scroll distance.
*/
void scroll(input_context_t &context, int high_res_distance);
/**
* @brief Submit horizontal scroll input.
*
* @param context Input context.
* @param high_res_distance High-resolution scroll distance.
*/
void hscroll(input_context_t &context, int high_res_distance);
/**
* @brief Submit a keyboard key transition.
*
* @param context Input context.
* @param modcode Portable key code.
* @param release Whether the key was released.
* @param flags Bit flags that modify the requested operation.
*/
void keyboard_update(input_context_t &context, std::uint16_t modcode, bool release, std::uint8_t flags);
/**
* @brief Submit UTF-8 text input.
*
* @param context Input context.
* @param utf8 UTF-8 text buffer.
* @param size Text buffer size.
*/
void unicode(input_context_t &context, const char *utf8, int size);
/**
* @brief Submit a touchscreen event.
*
* @param context Client context.
* @param touch_port Touch coordinate bounds used for scaling.
* @param touch Touch event.
*/
void touch_update(client_context_t &context, const touch_port_t &touch_port, const touch_input_t &touch);
/**
* @brief Submit a pen event.
*
* @param context Client context.
* @param touch_port Touch coordinate bounds used for scaling.
* @param pen Pen event.
*/
void pen_update(client_context_t &context, const touch_port_t &touch_port, const pen_input_t &pen);
/**
* @brief Return whether the configured gamepad profile can expose touchpad input.
*
* @return True when controller touchpad input should be advertised.
*/
bool configured_gamepad_supports_touchpad();
} // namespace platf::virtualhid