ApolloInput Nova 0.2.0
Standalone virtual gamepad library
Loading...
Searching...
No Matches
gamepad.cpp
Go to the documentation of this file.
1
6
7#include <algorithm>
8#include <array>
9#include <cctype>
10#include <cmath>
11#include <cstdint>
12#include <string_view>
13#include <tuple>
14#include <vector>
15
16namespace apollo::input {
17 namespace {
18 struct profile_name_t {
19 Profile profile;
20 std::string_view name;
21 };
22
23 constexpr std::array PROFILE_NAMES {
24 profile_name_t {Profile::xbox_360, "xbox_360"},
25 profile_name_t {Profile::xbox_one, "xbox_one"},
26 profile_name_t {Profile::xbox_series, "xbox_series"},
27 profile_name_t {Profile::xbox_elite, "xbox_elite"},
28 profile_name_t {Profile::dualshock4, "dualshock4"},
29 profile_name_t {Profile::dualsense, "dualsense"},
30 profile_name_t {Profile::dualsense_edge, "dualsense_edge"},
31 profile_name_t {Profile::switch_pro, "switch_pro"},
32 profile_name_t {Profile::steam_controller, "steam_controller"},
33 profile_name_t {Profile::steam_deck, "steam_deck"},
34 profile_name_t {Profile::generic, "generic"},
35 };
36
37 struct profile_alias_t {
38 std::string_view alias;
39 Profile profile;
40 };
41
42 constexpr std::array PROFILE_ALIASES {
43 profile_alias_t {"xbox_360", Profile::xbox_360},
44 profile_alias_t {"xbox360", Profile::xbox_360},
45 profile_alias_t {"x360", Profile::xbox_360},
46 profile_alias_t {"xbox_one", Profile::xbox_one},
47 profile_alias_t {"xboxone", Profile::xbox_one},
48 profile_alias_t {"xone", Profile::xbox_one},
49 profile_alias_t {"xbox_series", Profile::xbox_series},
50 profile_alias_t {"xboxseries", Profile::xbox_series},
51 profile_alias_t {"xseries", Profile::xbox_series},
52 profile_alias_t {"xbox_elite", Profile::xbox_elite},
53 profile_alias_t {"xboxelite", Profile::xbox_elite},
54 profile_alias_t {"elite", Profile::xbox_elite},
55 profile_alias_t {"elite_series_2", Profile::xbox_elite},
56 profile_alias_t {"elite_2", Profile::xbox_elite},
57 profile_alias_t {"dualshock4", Profile::dualshock4},
58 profile_alias_t {"dualshock_4", Profile::dualshock4},
59 profile_alias_t {"ds4", Profile::dualshock4},
60 profile_alias_t {"ps4", Profile::dualshock4},
61 profile_alias_t {"dualsense", Profile::dualsense},
62 profile_alias_t {"dual_sense", Profile::dualsense},
63 profile_alias_t {"ds5", Profile::dualsense},
64 profile_alias_t {"ps5", Profile::dualsense},
65 profile_alias_t {"dualsense_edge", Profile::dualsense_edge},
66 profile_alias_t {"dual_sense_edge", Profile::dualsense_edge},
67 profile_alias_t {"ds5_edge", Profile::dualsense_edge},
68 profile_alias_t {"ds5edge", Profile::dualsense_edge},
69 profile_alias_t {"ps5_edge", Profile::dualsense_edge},
70 profile_alias_t {"switch_pro", Profile::switch_pro},
71 profile_alias_t {"switchpro", Profile::switch_pro},
72 profile_alias_t {"switch", Profile::switch_pro},
73 profile_alias_t {"pro_controller", Profile::switch_pro},
74 profile_alias_t {"steam_controller", Profile::steam_controller},
75 profile_alias_t {"steamcontroller", Profile::steam_controller},
76 profile_alias_t {"steam", Profile::steam_controller},
77 profile_alias_t {"valve_steam_controller", Profile::steam_controller},
78 profile_alias_t {"steam_deck", Profile::steam_deck},
79 profile_alias_t {"steamdeck", Profile::steam_deck},
80 profile_alias_t {"deck", Profile::steam_deck},
81 profile_alias_t {"generic", Profile::generic},
82 profile_alias_t {"hid", Profile::generic},
83 };
84
85 struct family_name_t {
86 ControllerFamily family;
87 std::string_view name;
88 };
89
90 constexpr std::array FAMILY_NAMES {
91 family_name_t {ControllerFamily::unknown, "unknown"},
92 family_name_t {ControllerFamily::xbox, "xbox"},
93 family_name_t {ControllerFamily::playstation, "playstation"},
94 family_name_t {ControllerFamily::nintendo, "nintendo"},
95 family_name_t {ControllerFamily::steam, "steam"},
96 family_name_t {ControllerFamily::steam_deck, "steam_deck"},
97 };
98
99 struct family_alias_t {
100 std::string_view alias;
101 ControllerFamily family;
102 };
103
104 constexpr std::array FAMILY_ALIASES {
105 family_alias_t {"unknown", ControllerFamily::unknown},
106 family_alias_t {"auto", ControllerFamily::unknown},
107 family_alias_t {"xbox", ControllerFamily::xbox},
108 family_alias_t {"xbox360", ControllerFamily::xbox},
109 family_alias_t {"xbox_one", ControllerFamily::xbox},
110 family_alias_t {"xboxseries", ControllerFamily::xbox},
111 family_alias_t {"xbox_elite", ControllerFamily::xbox},
112 family_alias_t {"elite", ControllerFamily::xbox},
113 family_alias_t {"playstation", ControllerFamily::playstation},
114 family_alias_t {"ps", ControllerFamily::playstation},
115 family_alias_t {"ps3", ControllerFamily::playstation},
116 family_alias_t {"ps4", ControllerFamily::playstation},
117 family_alias_t {"ps5", ControllerFamily::playstation},
118 family_alias_t {"dualshock", ControllerFamily::playstation},
119 family_alias_t {"dualshock4", ControllerFamily::playstation},
120 family_alias_t {"dualsense", ControllerFamily::playstation},
121 family_alias_t {"dualsense_edge", ControllerFamily::playstation},
122 family_alias_t {"ds4", ControllerFamily::playstation},
123 family_alias_t {"ds5", ControllerFamily::playstation},
124 family_alias_t {"nintendo", ControllerFamily::nintendo},
125 family_alias_t {"switch", ControllerFamily::nintendo},
126 family_alias_t {"switch_pro", ControllerFamily::nintendo},
127 family_alias_t {"joycon", ControllerFamily::nintendo},
128 family_alias_t {"joy_con", ControllerFamily::nintendo},
129 family_alias_t {"steam", ControllerFamily::steam},
130 family_alias_t {"steam_controller", ControllerFamily::steam},
131 family_alias_t {"valve", ControllerFamily::steam},
132 family_alias_t {"steam_deck", ControllerFamily::steam_deck},
133 family_alias_t {"steamdeck", ControllerFamily::steam_deck},
134 family_alias_t {"deck", ControllerFamily::steam_deck},
135 };
136
144 bool equals_ignore_case(std::string_view left, std::string_view right) {
145 return left.size() == right.size() && std::equal(left.begin(), left.end(), right.begin(), [](char lhs, char rhs) {
146 return std::tolower(static_cast<unsigned char>(lhs)) == std::tolower(static_cast<unsigned char>(rhs));
147 });
148 }
149
156 void append_unique(std::vector<Profile> &chain, Profile profile) {
157 if (std::find(chain.begin(), chain.end(), profile) == chain.end()) {
158 chain.push_back(profile);
159 }
160 }
161 } // namespace
162
163 const char *to_string(Profile profile) {
164 for (const auto &entry : PROFILE_NAMES) {
165 if (entry.profile == profile) {
166 return entry.name.data();
167 }
168 }
169 return "generic";
170 }
171
172 bool profile_from_string(const std::string &name, Profile &profile) {
173 for (const auto &entry : PROFILE_ALIASES) {
174 if (equals_ignore_case(entry.alias, name)) {
175 profile = entry.profile;
176 return true;
177 }
178 }
179 return false;
180 }
181
182 const char *to_string(ControllerFamily family) {
183 for (const auto &entry : FAMILY_NAMES) {
184 if (entry.family == family) {
185 return entry.name.data();
186 }
187 }
188 return "unknown";
189 }
190
191 bool controller_family_from_string(const std::string &name, ControllerFamily &family) {
192 for (const auto &entry : FAMILY_ALIASES) {
193 if (equals_ignore_case(entry.alias, name)) {
194 family = entry.family;
195 return true;
196 }
197 }
198 return false;
199 }
200
202 Capabilities capabilities;
203 switch (profile) {
205 capabilities.rumble = true;
206 return capabilities;
208 capabilities.rumble = true;
209 capabilities.trigger_rumble = true;
210 capabilities.battery = true;
211 return capabilities;
213 capabilities.rumble = true;
214 capabilities.trigger_rumble = true;
215 capabilities.battery = true;
216 capabilities.misc_buttons = 1;
217 return capabilities;
219 capabilities.rumble = true;
220 capabilities.trigger_rumble = true;
221 capabilities.battery = true;
222 capabilities.paddles = true;
223 capabilities.misc_buttons = 1;
224 return capabilities;
226 capabilities.rumble = true;
227 capabilities.leds = true;
228 capabilities.imu = true;
229 capabilities.touchpad = true;
230 capabilities.battery = true;
231 return capabilities;
233 capabilities.rumble = true;
234 capabilities.leds = true;
235 capabilities.imu = true;
236 capabilities.touchpad = true;
237 capabilities.adaptive_triggers = true;
238 capabilities.player_leds = true;
239 capabilities.battery = true;
240 capabilities.misc_buttons = 1;
241 return capabilities;
244 capabilities.paddles = true;
245 return capabilities;
247 capabilities.rumble = true;
248 capabilities.leds = true;
249 capabilities.player_leds = true;
250 capabilities.imu = true;
251 capabilities.battery = true;
252 capabilities.misc_buttons = 1;
253 return capabilities;
255 capabilities.rumble = true;
256 capabilities.imu = true;
257 capabilities.touchpad = true;
258 capabilities.battery = true;
259 capabilities.paddles = true;
260 return capabilities;
262 capabilities.rumble = true;
263 capabilities.imu = true;
264 capabilities.touchpad = true;
265 capabilities.battery = true;
266 capabilities.paddles = true;
267 capabilities.misc_buttons = 1;
268 return capabilities;
269 case Profile::generic:
270 capabilities.rumble = true;
271 return capabilities;
272 }
273
274 return capabilities;
275 }
276
278 switch (profile) {
280 return {0x045E, 0x028E, 0x0114};
282 return {0x045E, 0x02EA, 0x0408};
284 return {0x045E, 0x0B12, 0x0500};
286 return {0x045E, 0x0B05, 0x0903};
288 return {0x054C, 0x05C4, 0x0100};
290 return {0x054C, 0x0CE6, 0x8111};
292 return {0x054C, 0x0DF2, 0x8111};
294 return {0x057E, 0x2009, 0x0210};
296 return {0x28DE, 0x1142, 0x0001};
298 return {0x28DE, 0x1205, 0x0001};
299 case Profile::generic:
300 return {0x1209, 0x0001, 0x0001};
301 }
302
303 return {0x1209, 0x0001, 0x0001};
304 }
305
307 switch (controller.family) {
311 if (controller.has_paddles) {
313 }
314 if (controller.has_touchpad || controller.has_motion) {
315 return Profile::dualsense;
316 }
317 return Profile::dualshock4;
319 return Profile::switch_pro;
321 return (controller.has_touchpad || controller.has_motion) ? Profile::steam_controller : Profile::xbox_series;
323 return (controller.has_touchpad || controller.has_motion || controller.has_paddles) ? Profile::steam_deck : Profile::xbox_series;
325 if (controller.has_paddles) {
326 return controller.has_touchpad || controller.has_motion ? Profile::dualsense_edge : Profile::xbox_elite;
327 }
328 if (controller.has_touchpad || controller.has_motion) {
329 return Profile::dualsense;
330 }
331 return controller.misc_buttons > 1 ? Profile::switch_pro : Profile::xbox_series;
332 }
333
335 }
336
337 std::vector<Profile> profile_compatibility_chain(Profile profile) {
338 std::vector<Profile> chain;
339 append_unique(chain, profile);
340
341 switch (profile) {
343 append_unique(chain, Profile::generic);
344 break;
346 append_unique(chain, Profile::xbox_series);
347 append_unique(chain, Profile::xbox_360);
348 append_unique(chain, Profile::generic);
349 break;
351 append_unique(chain, Profile::xbox_one);
352 append_unique(chain, Profile::xbox_360);
353 append_unique(chain, Profile::generic);
354 break;
356 append_unique(chain, Profile::xbox_series);
357 append_unique(chain, Profile::xbox_one);
358 append_unique(chain, Profile::xbox_360);
359 append_unique(chain, Profile::generic);
360 break;
362 append_unique(chain, Profile::xbox_360);
363 append_unique(chain, Profile::generic);
364 break;
366 append_unique(chain, Profile::dualshock4);
367 append_unique(chain, Profile::xbox_360);
368 append_unique(chain, Profile::generic);
369 break;
371 append_unique(chain, Profile::dualsense);
372 append_unique(chain, Profile::dualshock4);
373 append_unique(chain, Profile::xbox_360);
374 append_unique(chain, Profile::generic);
375 break;
377 append_unique(chain, Profile::xbox_360);
378 append_unique(chain, Profile::generic);
379 break;
381 append_unique(chain, Profile::dualsense);
382 append_unique(chain, Profile::dualshock4);
383 append_unique(chain, Profile::xbox_360);
384 append_unique(chain, Profile::generic);
385 break;
387 append_unique(chain, Profile::steam_controller);
388 append_unique(chain, Profile::dualsense);
389 append_unique(chain, Profile::dualshock4);
390 append_unique(chain, Profile::xbox_360);
391 append_unique(chain, Profile::generic);
392 break;
393 case Profile::generic:
394 append_unique(chain, Profile::xbox_360);
395 break;
396 }
397
398 return chain;
399 }
400
401 namespace {
408 std::uint8_t clamp_deadzone(std::uint8_t percent) {
409 return std::min<std::uint8_t>(percent, 30);
410 }
411
418 float normalize_axis(std::int16_t value) {
419 return value < 0 ? static_cast<float>(value) / 32768.0F : static_cast<float>(value) / 32767.0F;
420 }
421
428 std::int16_t denormalize_axis(float value) {
429 const auto scaled = value < 0 ? value * 32768.0F : value * 32767.0F;
430 return static_cast<std::int16_t>(std::clamp<std::int32_t>(static_cast<std::int32_t>(std::lround(scaled)), -32768, 32767));
431 }
432
441 std::pair<std::int16_t, std::int16_t> sanitize_stick(std::int16_t x, std::int16_t y, std::uint8_t percent) {
442 const auto deadzone = static_cast<float>(clamp_deadzone(percent)) / 100.0F;
443 if (deadzone <= 0.0F) {
444 return {x, y};
445 }
446
447 auto nx = normalize_axis(x);
448 auto ny = normalize_axis(y);
449 const auto magnitude = std::sqrt(nx * nx + ny * ny);
450 if (magnitude <= deadzone) {
451 return {0, 0};
452 }
453
454 const auto clamped_magnitude = std::min(magnitude, 1.0F);
455 const auto scaled_magnitude = (clamped_magnitude - deadzone) / (1.0F - deadzone);
456 const auto scale = scaled_magnitude / magnitude;
457 return {denormalize_axis(nx * scale), denormalize_axis(ny * scale)};
458 }
459
467 std::uint8_t sanitize_trigger(std::uint8_t value, std::uint8_t percent) {
468 const auto deadzone = clamp_deadzone(percent);
469 if (deadzone == 0) {
470 return value;
471 }
472
473 const auto threshold = static_cast<int>(std::lround(255.0 * deadzone / 100.0));
474 if (value <= threshold) {
475 return 0;
476 }
477 if (threshold >= 255) {
478 return 255;
479 }
480 return static_cast<std::uint8_t>(std::lround((value - threshold) * 255.0 / (255 - threshold)));
481 }
482 } // namespace
483
484 void sanitize_gamepad_state(GamepadState &state, const AnalogDeadzone &deadzone) {
485 std::tie(state.left_x, state.left_y) = sanitize_stick(state.left_x, state.left_y, deadzone.stick_percent);
486 std::tie(state.right_x, state.right_y) = sanitize_stick(state.right_x, state.right_y, deadzone.stick_percent);
487 state.left_trigger = sanitize_trigger(state.left_trigger, deadzone.trigger_percent);
488 state.right_trigger = sanitize_trigger(state.right_trigger, deadzone.trigger_percent);
489 }
490} // namespace apollo::input
Portable gamepad state, profiles, and capability definitions.
Profile recommended_profile(const ControllerDescription &controller)
Pick the most capable virtual profile for a client controller.
Definition gamepad.cpp:306
ControllerFamily
Physical controller family reported by a streaming client.
Definition gamepad.hpp:37
@ playstation
DualShock 3/4, DualSense, or another PlayStation-style pad.
Definition gamepad.hpp:40
@ xbox
Xbox 360, Xbox One, Xbox Series, or Xbox Elite controller.
Definition gamepad.hpp:39
@ steam
Valve Steam Controller.
Definition gamepad.hpp:42
@ nintendo
Switch Pro Controller, Joy-Con, or another Nintendo-style pad.
Definition gamepad.hpp:41
@ steam_deck
Valve Steam Deck built-in controls.
Definition gamepad.hpp:43
@ unknown
No reliable family information.
Definition gamepad.hpp:38
Capabilities profile_capabilities(Profile profile)
Features a profile can expose when the backend supports them.
Definition gamepad.cpp:201
bool profile_from_string(const std::string &name, Profile &profile)
Parse a profile from its short name or a compatible alias.
Definition gamepad.cpp:172
Profile
Virtual controller profile exposed by a backend.
Definition gamepad.hpp:16
@ steam_controller
Valve Steam Controller layout.
Definition gamepad.hpp:25
@ dualsense_edge
DualSense Edge with rear paddles and extra function buttons.
Definition gamepad.hpp:23
@ xbox_elite
Xbox Elite Series 2 with rear paddles and Share.
Definition gamepad.hpp:20
@ steam_deck
Valve Steam Deck built-in controls.
Definition gamepad.hpp:26
const char * to_string(BackendKind kind)
Convert a backend kind to a stable short name.
Definition backend.cpp:61
void sanitize_gamepad_state(GamepadState &state, const AnalogDeadzone &deadzone={})
Remove stick drift, trigger resting noise, and center jitter.
Definition gamepad.cpp:484
ProfileIdentity profile_identity(Profile profile)
USB identity associated with a profile.
Definition gamepad.cpp:277
bool controller_family_from_string(const std::string &name, ControllerFamily &family)
Parse a controller family from its short name or alias.
Definition gamepad.cpp:191
std::vector< Profile > profile_compatibility_chain(Profile profile)
Return a profile followed by progressively simpler alternatives.
Definition gamepad.cpp:337
Host-side analog filtering used to remove drift and resting noise.
Definition gamepad.hpp:198
std::uint8_t stick_percent
Radial stick deadzone percentage (0-30).
Definition gamepad.hpp:199
std::uint8_t trigger_percent
Trigger deadzone percentage (0-30).
Definition gamepad.hpp:200
Features a backend can expose for a profile.
Definition gamepad.hpp:171
bool touchpad
Touchpad input.
Definition gamepad.hpp:175
bool player_leds
Player indicator LEDs.
Definition gamepad.hpp:180
bool paddles
Rear paddle or grip buttons.
Definition gamepad.hpp:182
std::uint8_t misc_buttons
Number of Share, Capture, or misc buttons (0-2).
Definition gamepad.hpp:183
bool trigger_rumble
Independent left/right trigger rumble.
Definition gamepad.hpp:179
bool leds
LED or lightbar feedback.
Definition gamepad.hpp:173
bool rumble
Main rumble feedback.
Definition gamepad.hpp:172
bool adaptive_triggers
Adaptive trigger feedback.
Definition gamepad.hpp:176
bool imu
Gyroscope and accelerometer.
Definition gamepad.hpp:174
bool battery
Battery state reported by the client.
Definition gamepad.hpp:181
Capabilities reported by a client controller.
Definition gamepad.hpp:53
bool has_touchpad
Reports touchpad contacts.
Definition gamepad.hpp:55
std::uint8_t misc_buttons
Number of extra misc/share buttons reported by the client.
Definition gamepad.hpp:59
bool has_paddles
Reports rear paddle or grip buttons.
Definition gamepad.hpp:57
bool has_motion
Reports accelerometer or gyroscope samples.
Definition gamepad.hpp:56
ControllerFamily family
Client-reported controller family.
Definition gamepad.hpp:54
One complete gamepad input report.
Definition gamepad.hpp:105
std::int16_t right_x
-32768 to 32767.
Definition gamepad.hpp:111
std::uint8_t left_trigger
0-255.
Definition gamepad.hpp:107
std::uint8_t right_trigger
0-255.
Definition gamepad.hpp:108
std::int16_t left_x
-32768 to 32767.
Definition gamepad.hpp:109
std::int16_t left_y
-32768 to 32767.
Definition gamepad.hpp:110
std::int16_t right_y
-32768 to 32767.
Definition gamepad.hpp:112
USB identity used by backends that create HID-style devices.
Definition gamepad.hpp:189