controller.cpp 3.6 KB

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