summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/sdl_driver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/drivers/sdl_driver.cpp')
-rw-r--r--src/input_common/drivers/sdl_driver.cpp926
1 files changed, 926 insertions, 0 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp
new file mode 100644
index 000000000..0cda9df62
--- /dev/null
+++ b/src/input_common/drivers/sdl_driver.cpp
@@ -0,0 +1,926 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/logging/log.h"
6#include "common/math_util.h"
7#include "common/param_package.h"
8#include "common/settings.h"
9#include "common/thread.h"
10#include "common/vector_math.h"
11#include "input_common/drivers/sdl_driver.h"
12
13namespace InputCommon {
14
15namespace {
16std::string GetGUID(SDL_Joystick* joystick) {
17 const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
18 char guid_str[33];
19 SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
20 return guid_str;
21}
22} // Anonymous namespace
23
24static int SDLEventWatcher(void* user_data, SDL_Event* event) {
25 auto* const sdl_state = static_cast<SDLDriver*>(user_data);
26
27 sdl_state->HandleGameControllerEvent(*event);
28
29 return 0;
30}
31
32class SDLJoystick {
33public:
34 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick,
35 SDL_GameController* game_controller)
36 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose},
37 sdl_controller{game_controller, &SDL_GameControllerClose} {
38 EnableMotion();
39 }
40
41 void EnableMotion() {
42 if (sdl_controller) {
43 SDL_GameController* controller = sdl_controller.get();
44 if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) && !has_accel) {
45 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
46 has_accel = true;
47 }
48 if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) && !has_gyro) {
49 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE);
50 has_gyro = true;
51 }
52 }
53 }
54
55 bool HasGyro() const {
56 return has_gyro;
57 }
58
59 bool HasAccel() const {
60 return has_accel;
61 }
62
63 bool UpdateMotion(SDL_ControllerSensorEvent event) {
64 constexpr float gravity_constant = 9.80665f;
65 std::lock_guard lock{mutex};
66 const u64 time_difference = event.timestamp - last_motion_update;
67 last_motion_update = event.timestamp;
68 switch (event.sensor) {
69 case SDL_SENSOR_ACCEL: {
70 motion.accel_x = -event.data[0] / gravity_constant;
71 motion.accel_y = event.data[2] / gravity_constant;
72 motion.accel_z = -event.data[1] / gravity_constant;
73 break;
74 }
75 case SDL_SENSOR_GYRO: {
76 motion.gyro_x = event.data[0] / (Common::PI * 2);
77 motion.gyro_y = -event.data[2] / (Common::PI * 2);
78 motion.gyro_z = event.data[1] / (Common::PI * 2);
79 break;
80 }
81 }
82
83 // Ignore duplicated timestamps
84 if (time_difference == 0) {
85 return false;
86 }
87 motion.delta_timestamp = time_difference * 1000;
88 return true;
89 }
90
91 const BasicMotion& GetMotion() const {
92 return motion;
93 }
94
95 bool RumblePlay(const Common::Input::VibrationStatus vibration) {
96 constexpr u32 rumble_max_duration_ms = 1000;
97 if (sdl_controller) {
98 return SDL_GameControllerRumble(
99 sdl_controller.get(), static_cast<u16>(vibration.low_amplitude),
100 static_cast<u16>(vibration.high_amplitude), rumble_max_duration_ms) != -1;
101 } else if (sdl_joystick) {
102 return SDL_JoystickRumble(sdl_joystick.get(), static_cast<u16>(vibration.low_amplitude),
103 static_cast<u16>(vibration.high_amplitude),
104 rumble_max_duration_ms) != -1;
105 }
106
107 return false;
108 }
109
110 bool HasHDRumble() const {
111 if (sdl_controller) {
112 return (SDL_GameControllerGetType(sdl_controller.get()) ==
113 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO);
114 }
115 return false;
116 }
117 /**
118 * The Pad identifier of the joystick
119 */
120 const PadIdentifier GetPadIdentifier() const {
121 return {
122 .guid = Common::UUID{guid},
123 .port = static_cast<std::size_t>(port),
124 .pad = 0,
125 };
126 }
127
128 /**
129 * The guid of the joystick
130 */
131 const std::string& GetGUID() const {
132 return guid;
133 }
134
135 /**
136 * The number of joystick from the same type that were connected before this joystick
137 */
138 int GetPort() const {
139 return port;
140 }
141
142 SDL_Joystick* GetSDLJoystick() const {
143 return sdl_joystick.get();
144 }
145
146 SDL_GameController* GetSDLGameController() const {
147 return sdl_controller.get();
148 }
149
150 void SetSDLJoystick(SDL_Joystick* joystick, SDL_GameController* controller) {
151 sdl_joystick.reset(joystick);
152 sdl_controller.reset(controller);
153 }
154
155 bool IsJoyconLeft() const {
156 const std::string controller_name = GetControllerName();
157 if (std::strstr(controller_name.c_str(), "Joy-Con Left") != nullptr) {
158 return true;
159 }
160 if (std::strstr(controller_name.c_str(), "Joy-Con (L)") != nullptr) {
161 return true;
162 }
163 return false;
164 }
165
166 bool IsJoyconRight() const {
167 const std::string controller_name = GetControllerName();
168 if (std::strstr(controller_name.c_str(), "Joy-Con Right") != nullptr) {
169 return true;
170 }
171 if (std::strstr(controller_name.c_str(), "Joy-Con (R)") != nullptr) {
172 return true;
173 }
174 return false;
175 }
176
177 BatteryLevel GetBatteryLevel() {
178 const auto level = SDL_JoystickCurrentPowerLevel(sdl_joystick.get());
179 switch (level) {
180 case SDL_JOYSTICK_POWER_EMPTY:
181 return BatteryLevel::Empty;
182 case SDL_JOYSTICK_POWER_LOW:
183 return BatteryLevel::Critical;
184 case SDL_JOYSTICK_POWER_MEDIUM:
185 return BatteryLevel::Low;
186 case SDL_JOYSTICK_POWER_FULL:
187 return BatteryLevel::Medium;
188 case SDL_JOYSTICK_POWER_MAX:
189 return BatteryLevel::Full;
190 case SDL_JOYSTICK_POWER_UNKNOWN:
191 case SDL_JOYSTICK_POWER_WIRED:
192 default:
193 return BatteryLevel::Charging;
194 }
195 }
196
197 std::string GetControllerName() const {
198 if (sdl_controller) {
199 switch (SDL_GameControllerGetType(sdl_controller.get())) {
200 case SDL_CONTROLLER_TYPE_XBOX360:
201 return "XBox 360 Controller";
202 case SDL_CONTROLLER_TYPE_XBOXONE:
203 return "XBox One Controller";
204 default:
205 break;
206 }
207 const auto name = SDL_GameControllerName(sdl_controller.get());
208 if (name) {
209 return name;
210 }
211 }
212
213 if (sdl_joystick) {
214 const auto name = SDL_JoystickName(sdl_joystick.get());
215 if (name) {
216 return name;
217 }
218 }
219
220 return "Unknown";
221 }
222
223private:
224 std::string guid;
225 int port;
226 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
227 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
228 mutable std::mutex mutex;
229
230 u64 last_motion_update{};
231 bool has_gyro{false};
232 bool has_accel{false};
233 BasicMotion motion;
234};
235
236std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) {
237 std::lock_guard lock{joystick_map_mutex};
238 const auto it = joystick_map.find(guid);
239
240 if (it != joystick_map.end()) {
241 while (it->second.size() <= static_cast<std::size_t>(port)) {
242 auto joystick = std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()),
243 nullptr, nullptr);
244 it->second.emplace_back(std::move(joystick));
245 }
246
247 return it->second[static_cast<std::size_t>(port)];
248 }
249
250 auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr);
251
252 return joystick_map[guid].emplace_back(std::move(joystick));
253}
254
255std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) {
256 auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id);
257 const std::string guid = GetGUID(sdl_joystick);
258
259 std::lock_guard lock{joystick_map_mutex};
260 const auto map_it = joystick_map.find(guid);
261
262 if (map_it == joystick_map.end()) {
263 return nullptr;
264 }
265
266 const auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(),
267 [&sdl_joystick](const auto& joystick) {
268 return joystick->GetSDLJoystick() == sdl_joystick;
269 });
270
271 if (vec_it == map_it->second.end()) {
272 return nullptr;
273 }
274
275 return *vec_it;
276}
277
278void SDLDriver::InitJoystick(int joystick_index) {
279 SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index);
280 SDL_GameController* sdl_gamecontroller = nullptr;
281
282 if (SDL_IsGameController(joystick_index)) {
283 sdl_gamecontroller = SDL_GameControllerOpen(joystick_index);
284 }
285
286 if (!sdl_joystick) {
287 LOG_ERROR(Input, "Failed to open joystick {}", joystick_index);
288 return;
289 }
290
291 const std::string guid = GetGUID(sdl_joystick);
292
293 std::lock_guard lock{joystick_map_mutex};
294 if (joystick_map.find(guid) == joystick_map.end()) {
295 auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller);
296 PreSetController(joystick->GetPadIdentifier());
297 SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel());
298 joystick_map[guid].emplace_back(std::move(joystick));
299 return;
300 }
301
302 auto& joystick_guid_list = joystick_map[guid];
303 const auto joystick_it =
304 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
305 [](const auto& joystick) { return !joystick->GetSDLJoystick(); });
306
307 if (joystick_it != joystick_guid_list.end()) {
308 (*joystick_it)->SetSDLJoystick(sdl_joystick, sdl_gamecontroller);
309 return;
310 }
311
312 const int port = static_cast<int>(joystick_guid_list.size());
313 auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller);
314 PreSetController(joystick->GetPadIdentifier());
315 SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel());
316 joystick_guid_list.emplace_back(std::move(joystick));
317}
318
319void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) {
320 const std::string guid = GetGUID(sdl_joystick);
321
322 std::lock_guard lock{joystick_map_mutex};
323 // This call to guid is safe since the joystick is guaranteed to be in the map
324 const auto& joystick_guid_list = joystick_map[guid];
325 const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
326 [&sdl_joystick](const auto& joystick) {
327 return joystick->GetSDLJoystick() == sdl_joystick;
328 });
329
330 if (joystick_it != joystick_guid_list.end()) {
331 (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
332 }
333}
334
335void SDLDriver::HandleGameControllerEvent(const SDL_Event& event) {
336 switch (event.type) {
337 case SDL_JOYBUTTONUP: {
338 if (const auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) {
339 const PadIdentifier identifier = joystick->GetPadIdentifier();
340 SetButton(identifier, event.jbutton.button, false);
341 }
342 break;
343 }
344 case SDL_JOYBUTTONDOWN: {
345 if (const auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) {
346 const PadIdentifier identifier = joystick->GetPadIdentifier();
347 SetButton(identifier, event.jbutton.button, true);
348 }
349 break;
350 }
351 case SDL_JOYHATMOTION: {
352 if (const auto joystick = GetSDLJoystickBySDLID(event.jhat.which)) {
353 const PadIdentifier identifier = joystick->GetPadIdentifier();
354 SetHatButton(identifier, event.jhat.hat, event.jhat.value);
355 }
356 break;
357 }
358 case SDL_JOYAXISMOTION: {
359 if (const auto joystick = GetSDLJoystickBySDLID(event.jaxis.which)) {
360 const PadIdentifier identifier = joystick->GetPadIdentifier();
361 SetAxis(identifier, event.jaxis.axis, event.jaxis.value / 32767.0f);
362 }
363 break;
364 }
365 case SDL_CONTROLLERSENSORUPDATE: {
366 if (auto joystick = GetSDLJoystickBySDLID(event.csensor.which)) {
367 if (joystick->UpdateMotion(event.csensor)) {
368 const PadIdentifier identifier = joystick->GetPadIdentifier();
369 SetMotion(identifier, 0, joystick->GetMotion());
370 }
371 }
372 break;
373 }
374 case SDL_JOYDEVICEREMOVED:
375 LOG_DEBUG(Input, "Controller removed with Instance_ID {}", event.jdevice.which);
376 CloseJoystick(SDL_JoystickFromInstanceID(event.jdevice.which));
377 break;
378 case SDL_JOYDEVICEADDED:
379 LOG_DEBUG(Input, "Controller connected with device index {}", event.jdevice.which);
380 InitJoystick(event.jdevice.which);
381 break;
382 }
383}
384
385void SDLDriver::CloseJoysticks() {
386 std::lock_guard lock{joystick_map_mutex};
387 joystick_map.clear();
388}
389
390SDLDriver::SDLDriver(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
391 if (!Settings::values.enable_raw_input) {
392 // Disable raw input. When enabled this setting causes SDL to die when a web applet opens
393 SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0");
394 }
395
396 // Prevent SDL from adding undesired axis
397 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
398
399 // Enable HIDAPI rumble. This prevents SDL from disabling motion on PS4 and PS5 controllers
400 SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
401 SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
402 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
403
404 // Use hidapi driver for joycons. This will allow joycons to be detected as a GameController and
405 // not a generic one
406 SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, "1");
407
408 // Disable hidapi driver for xbox. Already default on Windows, this causes conflict with native
409 // driver on Linux.
410 SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX, "0");
411
412 // If the frontend is going to manage the event loop, then we don't start one here
413 start_thread = SDL_WasInit(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) == 0;
414 if (start_thread && SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) {
415 LOG_CRITICAL(Input, "SDL_Init failed with: {}", SDL_GetError());
416 return;
417 }
418
419 SDL_AddEventWatch(&SDLEventWatcher, this);
420
421 initialized = true;
422 if (start_thread) {
423 poll_thread = std::thread([this] {
424 Common::SetCurrentThreadName("yuzu:input:SDL");
425 using namespace std::chrono_literals;
426 while (initialized) {
427 SDL_PumpEvents();
428 std::this_thread::sleep_for(1ms);
429 }
430 });
431 }
432 // Because the events for joystick connection happens before we have our event watcher added, we
433 // can just open all the joysticks right here
434 for (int i = 0; i < SDL_NumJoysticks(); ++i) {
435 InitJoystick(i);
436 }
437}
438
439SDLDriver::~SDLDriver() {
440 CloseJoysticks();
441 SDL_DelEventWatch(&SDLEventWatcher, this);
442
443 initialized = false;
444 if (start_thread) {
445 poll_thread.join();
446 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
447 }
448}
449
450std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const {
451 std::vector<Common::ParamPackage> devices;
452 std::unordered_map<int, std::shared_ptr<SDLJoystick>> joycon_pairs;
453 for (const auto& [key, value] : joystick_map) {
454 for (const auto& joystick : value) {
455 if (!joystick->GetSDLJoystick()) {
456 continue;
457 }
458 const std::string name =
459 fmt::format("{} {}", joystick->GetControllerName(), joystick->GetPort());
460 devices.emplace_back(Common::ParamPackage{
461 {"engine", GetEngineName()},
462 {"display", std::move(name)},
463 {"guid", joystick->GetGUID()},
464 {"port", std::to_string(joystick->GetPort())},
465 });
466 if (joystick->IsJoyconLeft()) {
467 joycon_pairs.insert_or_assign(joystick->GetPort(), joystick);
468 }
469 }
470 }
471
472 // Add dual controllers
473 for (const auto& [key, value] : joystick_map) {
474 for (const auto& joystick : value) {
475 if (joystick->IsJoyconRight()) {
476 if (!joycon_pairs.contains(joystick->GetPort())) {
477 continue;
478 }
479 const auto joystick2 = joycon_pairs.at(joystick->GetPort());
480
481 const std::string name =
482 fmt::format("{} {}", "Nintendo Dual Joy-Con", joystick->GetPort());
483 devices.emplace_back(Common::ParamPackage{
484 {"engine", GetEngineName()},
485 {"display", std::move(name)},
486 {"guid", joystick->GetGUID()},
487 {"guid2", joystick2->GetGUID()},
488 {"port", std::to_string(joystick->GetPort())},
489 });
490 }
491 }
492 }
493 return devices;
494}
495
496Common::Input::VibrationError SDLDriver::SetRumble(
497 const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) {
498 const auto joystick =
499 GetSDLJoystickByGUID(identifier.guid.Format(), static_cast<int>(identifier.port));
500 const auto process_amplitude_exp = [](f32 amplitude, f32 factor) {
501 return (amplitude + std::pow(amplitude, factor)) * 0.5f * 0xFFFF;
502 };
503
504 // Default exponential curve for rumble
505 f32 factor = 0.35f;
506
507 // If vibration is set as a linear output use a flatter value
508 if (vibration.type == Common::Input::VibrationAmplificationType::Linear) {
509 factor = 0.5f;
510 }
511
512 // Amplitude for HD rumble needs no modification
513 if (joystick->HasHDRumble()) {
514 factor = 1.0f;
515 }
516
517 const Common::Input::VibrationStatus new_vibration{
518 .low_amplitude = process_amplitude_exp(vibration.low_amplitude, factor),
519 .low_frequency = vibration.low_frequency,
520 .high_amplitude = process_amplitude_exp(vibration.high_amplitude, factor),
521 .high_frequency = vibration.high_frequency,
522 .type = Common::Input::VibrationAmplificationType::Exponential,
523 };
524
525 if (!joystick->RumblePlay(new_vibration)) {
526 return Common::Input::VibrationError::Unknown;
527 }
528
529 return Common::Input::VibrationError::None;
530}
531
532Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid,
533 s32 axis, float value) const {
534 Common::ParamPackage params{};
535 params.Set("engine", GetEngineName());
536 params.Set("port", port);
537 params.Set("guid", std::move(guid));
538 params.Set("axis", axis);
539 params.Set("threshold", "0.5");
540 params.Set("invert", value < 0 ? "-" : "+");
541 return params;
542}
543
544Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid,
545 s32 button) const {
546 Common::ParamPackage params{};
547 params.Set("engine", GetEngineName());
548 params.Set("port", port);
549 params.Set("guid", std::move(guid));
550 params.Set("button", button);
551 return params;
552}
553
554Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat,
555 u8 value) const {
556 Common::ParamPackage params{};
557 params.Set("engine", GetEngineName());
558 params.Set("port", port);
559 params.Set("guid", std::move(guid));
560 params.Set("hat", hat);
561 params.Set("direction", GetHatButtonName(value));
562 return params;
563}
564
565Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const {
566 Common::ParamPackage params{};
567 params.Set("engine", GetEngineName());
568 params.Set("motion", 0);
569 params.Set("port", port);
570 params.Set("guid", std::move(guid));
571 return params;
572}
573
574Common::ParamPackage SDLDriver::BuildParamPackageForBinding(
575 int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const {
576 switch (binding.bindType) {
577 case SDL_CONTROLLER_BINDTYPE_NONE:
578 break;
579 case SDL_CONTROLLER_BINDTYPE_AXIS:
580 return BuildAnalogParamPackageForButton(port, guid, binding.value.axis);
581 case SDL_CONTROLLER_BINDTYPE_BUTTON:
582 return BuildButtonParamPackageForButton(port, guid, binding.value.button);
583 case SDL_CONTROLLER_BINDTYPE_HAT:
584 return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat,
585 static_cast<u8>(binding.value.hat.hat_mask));
586 }
587 return {};
588}
589
590Common::ParamPackage SDLDriver::BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x,
591 int axis_y, float offset_x,
592 float offset_y) const {
593 Common::ParamPackage params;
594 params.Set("engine", GetEngineName());
595 params.Set("port", static_cast<int>(identifier.port));
596 params.Set("guid", identifier.guid.Format());
597 params.Set("axis_x", axis_x);
598 params.Set("axis_y", axis_y);
599 params.Set("offset_x", offset_x);
600 params.Set("offset_y", offset_y);
601 params.Set("invert_x", "+");
602 params.Set("invert_y", "+");
603 return params;
604}
605
606ButtonMapping SDLDriver::GetButtonMappingForDevice(const Common::ParamPackage& params) {
607 if (!params.Has("guid") || !params.Has("port")) {
608 return {};
609 }
610 const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
611
612 auto* controller = joystick->GetSDLGameController();
613 if (controller == nullptr) {
614 return {};
615 }
616
617 // This list is missing ZL/ZR since those are not considered buttons in SDL GameController.
618 // We will add those afterwards
619 // This list also excludes Screenshot since theres not really a mapping for that
620 ButtonBindings switch_to_sdl_button;
621
622 if (SDL_GameControllerGetType(controller) == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO) {
623 switch_to_sdl_button = GetNintendoButtonBinding(joystick);
624 } else {
625 switch_to_sdl_button = GetDefaultButtonBinding();
626 }
627
628 // Add the missing bindings for ZL/ZR
629 static constexpr ZButtonBindings switch_to_sdl_axis{{
630 {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
631 {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
632 }};
633
634 // Parameters contain two joysticks return dual
635 if (params.Has("guid2")) {
636 const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0));
637
638 if (joystick2->GetSDLGameController() != nullptr) {
639 return GetDualControllerMapping(joystick, joystick2, switch_to_sdl_button,
640 switch_to_sdl_axis);
641 }
642 }
643
644 return GetSingleControllerMapping(joystick, switch_to_sdl_button, switch_to_sdl_axis);
645}
646
647ButtonBindings SDLDriver::GetDefaultButtonBinding() const {
648 return {
649 std::pair{Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B},
650 {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A},
651 {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y},
652 {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X},
653 {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK},
654 {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK},
655 {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
656 {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
657 {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START},
658 {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK},
659 {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT},
660 {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP},
661 {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
662 {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN},
663 {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
664 {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
665 {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE},
666 };
667}
668
669ButtonBindings SDLDriver::GetNintendoButtonBinding(
670 const std::shared_ptr<SDLJoystick>& joystick) const {
671 // Default SL/SR mapping for pro controllers
672 auto sl_button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
673 auto sr_button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
674
675 if (joystick->IsJoyconLeft()) {
676 sl_button = SDL_CONTROLLER_BUTTON_PADDLE2;
677 sr_button = SDL_CONTROLLER_BUTTON_PADDLE4;
678 }
679 if (joystick->IsJoyconRight()) {
680 sl_button = SDL_CONTROLLER_BUTTON_PADDLE3;
681 sr_button = SDL_CONTROLLER_BUTTON_PADDLE1;
682 }
683
684 return {
685 std::pair{Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_A},
686 {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_B},
687 {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_X},
688 {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_Y},
689 {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK},
690 {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK},
691 {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
692 {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
693 {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START},
694 {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK},
695 {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT},
696 {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP},
697 {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
698 {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN},
699 {Settings::NativeButton::SL, sl_button},
700 {Settings::NativeButton::SR, sr_button},
701 {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE},
702 };
703}
704
705ButtonMapping SDLDriver::GetSingleControllerMapping(
706 const std::shared_ptr<SDLJoystick>& joystick, const ButtonBindings& switch_to_sdl_button,
707 const ZButtonBindings& switch_to_sdl_axis) const {
708 ButtonMapping mapping;
709 mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size());
710 auto* controller = joystick->GetSDLGameController();
711
712 for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) {
713 const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button);
714 mapping.insert_or_assign(
715 switch_button,
716 BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
717 }
718 for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) {
719 const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis);
720 mapping.insert_or_assign(
721 switch_button,
722 BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
723 }
724
725 return mapping;
726}
727
728ButtonMapping SDLDriver::GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
729 const std::shared_ptr<SDLJoystick>& joystick2,
730 const ButtonBindings& switch_to_sdl_button,
731 const ZButtonBindings& switch_to_sdl_axis) const {
732 ButtonMapping mapping;
733 mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size());
734 auto* controller = joystick->GetSDLGameController();
735 auto* controller2 = joystick2->GetSDLGameController();
736
737 for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) {
738 if (IsButtonOnLeftSide(switch_button)) {
739 const auto& binding = SDL_GameControllerGetBindForButton(controller2, sdl_button);
740 mapping.insert_or_assign(
741 switch_button,
742 BuildParamPackageForBinding(joystick2->GetPort(), joystick2->GetGUID(), binding));
743 continue;
744 }
745 const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button);
746 mapping.insert_or_assign(
747 switch_button,
748 BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
749 }
750 for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) {
751 if (IsButtonOnLeftSide(switch_button)) {
752 const auto& binding = SDL_GameControllerGetBindForAxis(controller2, sdl_axis);
753 mapping.insert_or_assign(
754 switch_button,
755 BuildParamPackageForBinding(joystick2->GetPort(), joystick2->GetGUID(), binding));
756 continue;
757 }
758 const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis);
759 mapping.insert_or_assign(
760 switch_button,
761 BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
762 }
763
764 return mapping;
765}
766
767bool SDLDriver::IsButtonOnLeftSide(Settings::NativeButton::Values button) const {
768 switch (button) {
769 case Settings::NativeButton::DDown:
770 case Settings::NativeButton::DLeft:
771 case Settings::NativeButton::DRight:
772 case Settings::NativeButton::DUp:
773 case Settings::NativeButton::L:
774 case Settings::NativeButton::LStick:
775 case Settings::NativeButton::Minus:
776 case Settings::NativeButton::Screenshot:
777 case Settings::NativeButton::ZL:
778 return true;
779 default:
780 return false;
781 }
782}
783
784AnalogMapping SDLDriver::GetAnalogMappingForDevice(const Common::ParamPackage& params) {
785 if (!params.Has("guid") || !params.Has("port")) {
786 return {};
787 }
788 const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
789 const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0));
790 auto* controller = joystick->GetSDLGameController();
791 if (controller == nullptr) {
792 return {};
793 }
794
795 AnalogMapping mapping = {};
796 const auto& binding_left_x =
797 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
798 const auto& binding_left_y =
799 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
800 if (params.Has("guid2")) {
801 const auto identifier = joystick2->GetPadIdentifier();
802 PreSetController(identifier);
803 PreSetAxis(identifier, binding_left_x.value.axis);
804 PreSetAxis(identifier, binding_left_y.value.axis);
805 const auto left_offset_x = -GetAxis(identifier, binding_left_x.value.axis);
806 const auto left_offset_y = -GetAxis(identifier, binding_left_y.value.axis);
807 mapping.insert_or_assign(Settings::NativeAnalog::LStick,
808 BuildParamPackageForAnalog(identifier, binding_left_x.value.axis,
809 binding_left_y.value.axis,
810 left_offset_x, left_offset_y));
811 } else {
812 const auto identifier = joystick->GetPadIdentifier();
813 PreSetController(identifier);
814 PreSetAxis(identifier, binding_left_x.value.axis);
815 PreSetAxis(identifier, binding_left_y.value.axis);
816 const auto left_offset_x = -GetAxis(identifier, binding_left_x.value.axis);
817 const auto left_offset_y = -GetAxis(identifier, binding_left_y.value.axis);
818 mapping.insert_or_assign(Settings::NativeAnalog::LStick,
819 BuildParamPackageForAnalog(identifier, binding_left_x.value.axis,
820 binding_left_y.value.axis,
821 left_offset_x, left_offset_y));
822 }
823 const auto& binding_right_x =
824 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
825 const auto& binding_right_y =
826 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
827 const auto identifier = joystick->GetPadIdentifier();
828 PreSetController(identifier);
829 PreSetAxis(identifier, binding_right_x.value.axis);
830 PreSetAxis(identifier, binding_right_y.value.axis);
831 const auto right_offset_x = -GetAxis(identifier, binding_right_x.value.axis);
832 const auto right_offset_y = -GetAxis(identifier, binding_right_y.value.axis);
833 mapping.insert_or_assign(Settings::NativeAnalog::RStick,
834 BuildParamPackageForAnalog(identifier, binding_right_x.value.axis,
835 binding_right_y.value.axis, right_offset_x,
836 right_offset_y));
837 return mapping;
838}
839
840MotionMapping SDLDriver::GetMotionMappingForDevice(const Common::ParamPackage& params) {
841 if (!params.Has("guid") || !params.Has("port")) {
842 return {};
843 }
844 const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
845 const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0));
846 auto* controller = joystick->GetSDLGameController();
847 if (controller == nullptr) {
848 return {};
849 }
850
851 MotionMapping mapping = {};
852 joystick->EnableMotion();
853
854 if (joystick->HasGyro() || joystick->HasAccel()) {
855 mapping.insert_or_assign(Settings::NativeMotion::MotionRight,
856 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
857 }
858 if (params.Has("guid2")) {
859 joystick2->EnableMotion();
860 if (joystick2->HasGyro() || joystick2->HasAccel()) {
861 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
862 BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID()));
863 }
864 } else {
865 if (joystick->HasGyro() || joystick->HasAccel()) {
866 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
867 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
868 }
869 }
870
871 return mapping;
872}
873
874Common::Input::ButtonNames SDLDriver::GetUIName(const Common::ParamPackage& params) const {
875 if (params.Has("button")) {
876 // TODO(German77): Find how to substitue the values for real button names
877 return Common::Input::ButtonNames::Value;
878 }
879 if (params.Has("hat")) {
880 return Common::Input::ButtonNames::Value;
881 }
882 if (params.Has("axis")) {
883 return Common::Input::ButtonNames::Value;
884 }
885 if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
886 return Common::Input::ButtonNames::Value;
887 }
888 if (params.Has("motion")) {
889 return Common::Input::ButtonNames::Engine;
890 }
891
892 return Common::Input::ButtonNames::Invalid;
893}
894
895std::string SDLDriver::GetHatButtonName(u8 direction_value) const {
896 switch (direction_value) {
897 case SDL_HAT_UP:
898 return "up";
899 case SDL_HAT_DOWN:
900 return "down";
901 case SDL_HAT_LEFT:
902 return "left";
903 case SDL_HAT_RIGHT:
904 return "right";
905 default:
906 return {};
907 }
908}
909
910u8 SDLDriver::GetHatButtonId(const std::string& direction_name) const {
911 Uint8 direction;
912 if (direction_name == "up") {
913 direction = SDL_HAT_UP;
914 } else if (direction_name == "down") {
915 direction = SDL_HAT_DOWN;
916 } else if (direction_name == "left") {
917 direction = SDL_HAT_LEFT;
918 } else if (direction_name == "right") {
919 direction = SDL_HAT_RIGHT;
920 } else {
921 direction = 0;
922 }
923 return direction;
924}
925
926} // namespace InputCommon