ApolloInput Nova 0.2.0
Standalone virtual gamepad library
Loading...
Searching...
No Matches
vigem_backend.cpp
Go to the documentation of this file.
1
6
8
9#ifdef _WIN32
10
11 #ifndef NOMINMAX
12 #define NOMINMAX
13 #endif
14 #include <windows.h>
15
16 #include <ViGEm/Client.h>
17
18 #include <algorithm>
19 #include <atomic>
20 #include <cstdio>
21 #include <cstdint>
22 #include <mutex>
23 #include <string>
24 #include <utility>
25
26namespace apollo::input::detail {
27 namespace {
34 std::string vigem_error_string(VIGEM_ERROR error) {
35 char buffer[64];
36 std::snprintf(buffer, sizeof(buffer), "ViGEmBus error 0x%08X", static_cast<unsigned int>(error));
37 return buffer;
38 }
39
46 std::uint8_t ds4_stick(std::int16_t value) {
47 return static_cast<std::uint8_t>((static_cast<std::int32_t>(value) + 32768) >> 8);
48 }
49
57 SHORT ds4_sensor(float value, float scale) {
58 const float scaled = value * scale;
59 const float clamped = std::clamp(scaled, -32768.0F, 32767.0F);
60 return static_cast<SHORT>(clamped);
61 }
62
68 constexpr float DS4_GYRO_SCALE = 16.0F * 57.2957795F;
69
75 constexpr float DS4_ACCEL_SCALE = 8192.0F / 9.80665F;
76
83 DS4_DPAD_DIRECTIONS ds4_dpad(const GamepadState &state) {
84 const bool up = state.pressed(Button::dpad_up);
85 const bool down = state.pressed(Button::dpad_down);
86 const bool left = state.pressed(Button::dpad_left);
87 const bool right = state.pressed(Button::dpad_right);
88
89 if (up && left) {
90 return DS4_BUTTON_DPAD_NORTHWEST;
91 }
92 if (up && right) {
93 return DS4_BUTTON_DPAD_NORTHEAST;
94 }
95 if (down && left) {
96 return DS4_BUTTON_DPAD_SOUTHWEST;
97 }
98 if (down && right) {
99 return DS4_BUTTON_DPAD_SOUTHEAST;
100 }
101 if (up) {
102 return DS4_BUTTON_DPAD_NORTH;
103 }
104 if (down) {
105 return DS4_BUTTON_DPAD_SOUTH;
106 }
107 if (left) {
108 return DS4_BUTTON_DPAD_WEST;
109 }
110 if (right) {
111 return DS4_BUTTON_DPAD_EAST;
112 }
113 return DS4_BUTTON_DPAD_NONE;
114 }
115
119 class VigemDevice final: public Device {
120 public:
121 VigemDevice(PVIGEM_CLIENT client, PVIGEM_TARGET target, Profile profile, std::string name):
122 client_(client),
123 target_(target),
124 info_ {profile, capabilities_for(profile), std::move(name)} {
125 if (info_.profile == Profile::xbox_360) {
126 vigem_target_x360_register_notification(client_, target_, &VigemDevice::x360_notification, this);
127 } else {
128 vigem_target_ds4_register_notification(client_, target_, &VigemDevice::ds4_notification, this);
129 }
130 }
131
132 ~VigemDevice() override {
133 disconnect();
134 }
135
136 const DeviceInfo &info() const override {
137 return info_;
138 }
139
140 bool connected() const override {
141 return connected_;
142 }
143
144 bool update(const GamepadState &state) override {
145 std::scoped_lock lock(mutex_);
146 if (!connected_) {
147 return false;
148 }
149
150 auto sanitized = state;
151 sanitize_gamepad_state(sanitized, deadzone_);
152
153 if (info_.profile == Profile::xbox_360) {
154 return send_x360(sanitized);
155 }
156 if (info_.profile == Profile::dualshock4) {
157 update_ds4_state(sanitized);
158 return send_ds4();
159 }
160 return false;
161 }
162
163 void set_deadzone(const AnalogDeadzone &deadzone) override {
164 std::scoped_lock lock(mutex_);
165 deadzone_ = deadzone;
166 }
167
168 bool update_touchpad(const TouchpadState &state) override {
169 std::scoped_lock lock(mutex_);
170 if (!connected_ || info_.profile != Profile::dualshock4) {
171 return false;
172 }
173
174 auto &report = ds4_report_.Report;
175 report.bTouchPacketsN = state.active ? 1 : 0;
176 report.sCurrentTouch.bPacketCounter = ++touch_packet_;
177 report.sCurrentTouch.bIsUpTrackingNum1 = state.active ? 0x00 : 0x80;
178 const auto x = static_cast<std::uint16_t>(std::min<std::uint32_t>(state.x, 1919));
179 const auto y = static_cast<std::uint16_t>(std::min<std::uint32_t>(state.y, 942));
180 report.sCurrentTouch.bTouchData1[0] = static_cast<BYTE>(x & 0xFF);
181 report.sCurrentTouch.bTouchData1[1] = static_cast<BYTE>(((x >> 8) & 0x0F) | ((y & 0x0F) << 4));
182 report.sCurrentTouch.bTouchData1[2] = static_cast<BYTE>((y >> 4) & 0xFF);
183 return send_ds4();
184 }
185
186 bool update_imu(const ImuSample &sample) override {
187 std::scoped_lock lock(mutex_);
188 if (!connected_ || info_.profile != Profile::dualshock4) {
189 return false;
190 }
191
192 auto &report = ds4_report_.Report;
193 report.wGyroX = ds4_sensor(sample.gyro_x, DS4_GYRO_SCALE);
194 report.wGyroY = ds4_sensor(sample.gyro_y, DS4_GYRO_SCALE);
195 report.wGyroZ = ds4_sensor(sample.gyro_z, DS4_GYRO_SCALE);
196 report.wAccelX = ds4_sensor(sample.accel_x, DS4_ACCEL_SCALE);
197 report.wAccelY = ds4_sensor(sample.accel_y, DS4_ACCEL_SCALE);
198 report.wAccelZ = ds4_sensor(sample.accel_z, DS4_ACCEL_SCALE);
199 return send_ds4();
200 }
201
202 void set_feedback_callback(FeedbackCallback callback) override {
203 std::scoped_lock lock(mutex_);
204 feedback_ = std::move(callback);
205 }
206
207 void disconnect() override {
208 PVIGEM_CLIENT client = nullptr;
209 PVIGEM_TARGET target = nullptr;
210 Profile profile = Profile::generic;
211 {
212 std::scoped_lock lock(mutex_);
213 if (!connected_) {
214 return;
215 }
216 connected_ = false;
217 client = client_;
218 target = target_;
219 profile = info_.profile;
220 client_ = nullptr;
221 target_ = nullptr;
222 }
223
224 if (profile == Profile::xbox_360) {
225 vigem_target_x360_unregister_notification(target);
226 }
227 vigem_target_remove(client, target);
228 vigem_target_free(target);
229 vigem_disconnect(client);
230 vigem_free(client);
231 }
232
233 private:
240 static Capabilities capabilities_for(Profile profile) {
241 auto capabilities = profile_capabilities(profile);
242
243 // ViGEmBus only provides the features implemented by its X360 and DS4
244 // reports. Callers should use create_compatible() for other profiles.
245 capabilities.trigger_rumble = false;
246 capabilities.player_leds = false;
247 capabilities.battery = false;
248 capabilities.paddles = false;
249 capabilities.misc_buttons = 0;
250 capabilities.adaptive_triggers = false;
251
252 if (profile != Profile::dualshock4) {
253 capabilities.leds = false;
254 capabilities.imu = false;
255 capabilities.touchpad = false;
256 }
257 return capabilities;
258 }
259
266 bool send_x360(const GamepadState &state) {
267 XUSB_REPORT report {};
268 XUSB_REPORT_INIT(&report);
269
270 if (state.pressed(Button::a)) {
271 report.wButtons |= XUSB_GAMEPAD_A;
272 }
273 if (state.pressed(Button::b)) {
274 report.wButtons |= XUSB_GAMEPAD_B;
275 }
276 if (state.pressed(Button::x)) {
277 report.wButtons |= XUSB_GAMEPAD_X;
278 }
279 if (state.pressed(Button::y)) {
280 report.wButtons |= XUSB_GAMEPAD_Y;
281 }
282 if (state.pressed(Button::dpad_up)) {
283 report.wButtons |= XUSB_GAMEPAD_DPAD_UP;
284 }
285 if (state.pressed(Button::dpad_down)) {
286 report.wButtons |= XUSB_GAMEPAD_DPAD_DOWN;
287 }
288 if (state.pressed(Button::dpad_left)) {
289 report.wButtons |= XUSB_GAMEPAD_DPAD_LEFT;
290 }
291 if (state.pressed(Button::dpad_right)) {
292 report.wButtons |= XUSB_GAMEPAD_DPAD_RIGHT;
293 }
294 if (state.pressed(Button::start) || state.pressed(Button::options)) {
295 report.wButtons |= XUSB_GAMEPAD_START;
296 }
297 if (state.pressed(Button::back) || state.pressed(Button::share) || state.pressed(Button::misc2)) {
298 report.wButtons |= XUSB_GAMEPAD_BACK;
299 }
300 if (state.pressed(Button::guide)) {
301 report.wButtons |= XUSB_GAMEPAD_GUIDE;
302 }
303 if (state.pressed(Button::left_thumb)) {
304 report.wButtons |= XUSB_GAMEPAD_LEFT_THUMB;
305 }
306 if (state.pressed(Button::right_thumb)) {
307 report.wButtons |= XUSB_GAMEPAD_RIGHT_THUMB;
308 }
309 if (state.pressed(Button::left_shoulder)) {
310 report.wButtons |= XUSB_GAMEPAD_LEFT_SHOULDER;
311 }
312 if (state.pressed(Button::right_shoulder)) {
313 report.wButtons |= XUSB_GAMEPAD_RIGHT_SHOULDER;
314 }
315
316 report.bLeftTrigger = state.left_trigger;
317 report.bRightTrigger = state.right_trigger;
318 report.sThumbLX = state.left_x;
319 report.sThumbLY = state.left_y;
320 report.sThumbRX = state.right_x;
321 report.sThumbRY = state.right_y;
322
323 return VIGEM_SUCCESS(vigem_target_x360_update(client_, target_, report));
324 }
325
331 void update_ds4_state(const GamepadState &state) {
332 auto &report = ds4_report_.Report;
333 report.bThumbLX = ds4_stick(state.left_x);
334 report.bThumbLY = ds4_stick(state.left_y);
335 report.bThumbRX = ds4_stick(state.right_x);
336 report.bThumbRY = ds4_stick(state.right_y);
337 report.bTriggerL = state.left_trigger;
338 report.bTriggerR = state.right_trigger;
339 report.wButtons = 0;
340
341 if (state.pressed(Button::a)) {
342 report.wButtons |= DS4_BUTTON_CROSS;
343 }
344 if (state.pressed(Button::b)) {
345 report.wButtons |= DS4_BUTTON_CIRCLE;
346 }
347 if (state.pressed(Button::x)) {
348 report.wButtons |= DS4_BUTTON_SQUARE;
349 }
350 if (state.pressed(Button::y)) {
351 report.wButtons |= DS4_BUTTON_TRIANGLE;
352 }
353 if (state.pressed(Button::left_shoulder)) {
354 report.wButtons |= DS4_BUTTON_SHOULDER_LEFT;
355 }
356 if (state.pressed(Button::right_shoulder)) {
357 report.wButtons |= DS4_BUTTON_SHOULDER_RIGHT;
358 }
359 if (state.left_trigger > 0) {
360 report.wButtons |= DS4_BUTTON_TRIGGER_LEFT;
361 }
362 if (state.right_trigger > 0) {
363 report.wButtons |= DS4_BUTTON_TRIGGER_RIGHT;
364 }
365 if (state.pressed(Button::left_thumb)) {
366 report.wButtons |= DS4_BUTTON_THUMB_LEFT;
367 }
368 if (state.pressed(Button::right_thumb)) {
369 report.wButtons |= DS4_BUTTON_THUMB_RIGHT;
370 }
371 if (state.pressed(Button::back) || state.pressed(Button::share)) {
372 report.wButtons |= DS4_BUTTON_SHARE;
373 }
374 if (state.pressed(Button::start) || state.pressed(Button::options)) {
375 report.wButtons |= DS4_BUTTON_OPTIONS;
376 }
377
378 report.wButtons &= ~0xF;
379 report.wButtons |= static_cast<USHORT>(ds4_dpad(state));
380
381 report.bSpecial = 0;
382 if (state.pressed(Button::guide)) {
383 report.bSpecial |= DS4_SPECIAL_BUTTON_PS;
384 }
385 if (state.pressed(Button::touchpad_click) || state.pressed(Button::touchpad)) {
386 report.bSpecial |= DS4_SPECIAL_BUTTON_TOUCHPAD;
387 }
388 }
389
395 bool send_ds4() {
396 return VIGEM_SUCCESS(vigem_target_ds4_update_ex(client_, target_, ds4_report_));
397 }
398
402 static void CALLBACK x360_notification(PVIGEM_CLIENT, PVIGEM_TARGET, UCHAR large_motor, UCHAR small_motor, UCHAR, LPVOID user_data) {
403 if (!user_data) {
404 return;
405 }
406 static_cast<VigemDevice *>(user_data)->emit_feedback(large_motor, small_motor, 0, 0, 0, false);
407 }
408
412 static void CALLBACK ds4_notification(PVIGEM_CLIENT, PVIGEM_TARGET, UCHAR large_motor, UCHAR small_motor, DS4_LIGHTBAR_COLOR color, LPVOID user_data) {
413 if (!user_data) {
414 return;
415 }
416 static_cast<VigemDevice *>(user_data)->emit_feedback(large_motor, small_motor, color.Red, color.Green, color.Blue, true);
417 }
418
422 void emit_feedback(UCHAR large_motor, UCHAR small_motor, UCHAR red, UCHAR green, UCHAR blue, bool has_lightbar) {
423 FeedbackCallback callback;
424 {
425 std::scoped_lock lock(mutex_);
426 callback = feedback_;
427 }
428 if (!callback) {
429 return;
430 }
431
432 Feedback feedback;
433 feedback.type = FeedbackType::rumble;
434 feedback.low_frequency = static_cast<std::uint16_t>(large_motor) * 257U;
435 feedback.high_frequency = static_cast<std::uint16_t>(small_motor) * 257U;
436 callback(feedback);
437
438 if (has_lightbar) {
439 feedback = {};
440 feedback.type = FeedbackType::led;
441 feedback.led_red = red;
442 feedback.led_green = green;
443 feedback.led_blue = blue;
444 callback(feedback);
445 }
446 }
447
448 PVIGEM_CLIENT client_ = nullptr;
449 PVIGEM_TARGET target_ = nullptr;
450 DeviceInfo info_;
451 DS4_REPORT_EX ds4_report_ {};
452 std::uint8_t touch_packet_ = 0;
453 AnalogDeadzone deadzone_;
454 FeedbackCallback feedback_;
455 std::mutex mutex_;
456 std::atomic<bool> connected_ {true};
457 };
458
462 class VigemBackend final: public Backend {
463 public:
464 const char *name() const override {
465 return "vigem";
466 }
467
468 bool available(std::string *error) const override {
469 PVIGEM_CLIENT client = vigem_alloc();
470 if (!client) {
471 if (error) {
472 *error = "Unable to allocate a ViGEmBus client";
473 }
474 return false;
475 }
476
477 const auto result = vigem_connect(client);
478 if (!VIGEM_SUCCESS(result)) {
479 if (error) {
480 *error = vigem_error_string(result);
481 }
482 vigem_free(client);
483 return false;
484 }
485
486 vigem_disconnect(client);
487 vigem_free(client);
488 if (error) {
489 error->clear();
490 }
491 return true;
492 }
493
494 std::unique_ptr<Device> create(Profile profile, const std::string &name, std::string *error) override {
495 if (profile != Profile::xbox_360 && profile != Profile::dualshock4) {
496 if (error) {
497 *error = "ViGEmBus only supports Xbox 360 and DualShock 4 profiles; use create_compatible() to fall back";
498 }
499 return nullptr;
500 }
501
502 PVIGEM_CLIENT client = vigem_alloc();
503 if (!client) {
504 if (error) {
505 *error = "Unable to allocate a ViGEmBus client";
506 }
507 return nullptr;
508 }
509
510 auto result = vigem_connect(client);
511 if (!VIGEM_SUCCESS(result)) {
512 if (error) {
513 *error = vigem_error_string(result);
514 }
515 vigem_free(client);
516 return nullptr;
517 }
518
519 PVIGEM_TARGET target = profile == Profile::xbox_360 ? vigem_target_x360_alloc() : vigem_target_ds4_alloc();
520 if (!target) {
521 if (error) {
522 *error = "Unable to allocate a ViGEmBus target";
523 }
524 vigem_disconnect(client);
525 vigem_free(client);
526 return nullptr;
527 }
528
529 result = vigem_target_add(client, target);
530 if (!VIGEM_SUCCESS(result)) {
531 if (error) {
532 *error = vigem_error_string(result);
533 }
534 vigem_target_free(target);
535 vigem_disconnect(client);
536 vigem_free(client);
537 return nullptr;
538 }
539
540 if (error) {
541 error->clear();
542 }
543 return std::make_unique<VigemDevice>(client, target, profile, name);
544 }
545
546 std::vector<Profile> supported_profiles() const override {
548 }
549 };
550 } // namespace
551
552 std::unique_ptr<Backend> create_vigem_backend() {
553 return std::make_unique<VigemBackend>();
554 }
555} // namespace apollo::input::detail
556
557#endif
Backend and device interfaces for the Apollo input library.
Internal backend factory declarations.
std::unique_ptr< Backend > create_vigem_backend()
Capabilities profile_capabilities(Profile profile)
Features a profile can expose when the backend supports them.
Definition gamepad.cpp:201
std::function< void(const Feedback &)> FeedbackCallback
Callback invoked when the virtual device receives feedback.
Definition backend.hpp:53
@ led
LED or lightbar.
Definition backend.hpp:31
@ rumble
Rumble motors.
Definition backend.hpp:30
Profile
Virtual controller profile exposed by a backend.
Definition gamepad.hpp:16
void sanitize_gamepad_state(GamepadState &state, const AnalogDeadzone &deadzone={})
Remove stick drift, trigger resting noise, and center jitter.
Definition gamepad.cpp:484
@ misc2
Second miscellaneous button, when a controller exposes one.
Definition gamepad.hpp:89