diff options
Diffstat (limited to 'src/core/frontend/applets/controller.cpp')
| -rw-r--r-- | src/core/frontend/applets/controller.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/core/frontend/applets/controller.cpp b/src/core/frontend/applets/controller.cpp new file mode 100644 index 000000000..4505da758 --- /dev/null +++ b/src/core/frontend/applets/controller.cpp | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "core/core.h" | ||
| 8 | #include "core/frontend/applets/controller.h" | ||
| 9 | #include "core/hle/service/hid/controllers/npad.h" | ||
| 10 | #include "core/hle/service/hid/hid.h" | ||
| 11 | #include "core/hle/service/sm/sm.h" | ||
| 12 | |||
| 13 | namespace Core::Frontend { | ||
| 14 | |||
| 15 | ControllerApplet::~ControllerApplet() = default; | ||
| 16 | |||
| 17 | DefaultControllerApplet::~DefaultControllerApplet() = default; | ||
| 18 | |||
| 19 | void DefaultControllerApplet::ReconfigureControllers(std::function<void()> callback, | ||
| 20 | ControllerParameters parameters) const { | ||
| 21 | LOG_INFO(Service_HID, "called, deducing the best configuration based on the given parameters!"); | ||
| 22 | |||
| 23 | auto& npad = | ||
| 24 | Core::System::GetInstance() | ||
| 25 | .ServiceManager() | ||
| 26 | .GetService<Service::HID::Hid>("hid") | ||
| 27 | ->GetAppletResource() | ||
| 28 | ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad); | ||
| 29 | |||
| 30 | auto& players = Settings::values.players; | ||
| 31 | |||
| 32 | const std::size_t min_supported_players = | ||
| 33 | parameters.enable_single_mode ? 1 : parameters.min_players; | ||
| 34 | |||
| 35 | // Disconnect Handheld first. | ||
| 36 | npad.DisconnectNPadAtIndex(8); | ||
| 37 | |||
| 38 | // Deduce the best configuration based on the input parameters. | ||
| 39 | for (std::size_t index = 0; index < players.size() - 2; ++index) { | ||
| 40 | // First, disconnect all controllers regardless of the value of keep_controllers_connected. | ||
| 41 | // This makes it easy to connect the desired controllers. | ||
| 42 | npad.DisconnectNPadAtIndex(index); | ||
| 43 | |||
| 44 | // Only connect the minimum number of required players. | ||
| 45 | if (index >= min_supported_players) { | ||
| 46 | continue; | ||
| 47 | } | ||
| 48 | |||
| 49 | // Connect controllers based on the following priority list from highest to lowest priority: | ||
| 50 | // Pro Controller -> Dual Joycons -> Left Joycon/Right Joycon -> Handheld | ||
| 51 | if (parameters.allow_pro_controller) { | ||
| 52 | npad.AddNewControllerAt( | ||
| 53 | npad.MapSettingsTypeToNPad(Settings::ControllerType::ProController), index); | ||
| 54 | } else if (parameters.allow_dual_joycons) { | ||
| 55 | npad.AddNewControllerAt( | ||
| 56 | npad.MapSettingsTypeToNPad(Settings::ControllerType::DualJoyconDetached), index); | ||
| 57 | } else if (parameters.allow_left_joycon && parameters.allow_right_joycon) { | ||
| 58 | // Assign left joycons to even player indices and right joycons to odd player indices. | ||
| 59 | // We do this since Captain Toad Treasure Tracker expects a left joycon for Player 1 and | ||
| 60 | // a right Joycon for Player 2 in 2 Player Assist mode. | ||
| 61 | if (index % 2 == 0) { | ||
| 62 | npad.AddNewControllerAt( | ||
| 63 | npad.MapSettingsTypeToNPad(Settings::ControllerType::LeftJoycon), index); | ||
| 64 | } else { | ||
| 65 | npad.AddNewControllerAt( | ||
| 66 | npad.MapSettingsTypeToNPad(Settings::ControllerType::RightJoycon), index); | ||
| 67 | } | ||
| 68 | } else if (index == 0 && parameters.enable_single_mode && parameters.allow_handheld && | ||
| 69 | !Settings::values.use_docked_mode) { | ||
| 70 | // We should *never* reach here under any normal circumstances. | ||
| 71 | npad.AddNewControllerAt(npad.MapSettingsTypeToNPad(Settings::ControllerType::Handheld), | ||
| 72 | index); | ||
| 73 | } else { | ||
| 74 | UNREACHABLE_MSG("Unable to add a new controller based on the given parameters!"); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | callback(); | ||
| 79 | } | ||
| 80 | |||
| 81 | } // namespace Core::Frontend | ||