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