controller.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/assert.h"
  4. #include "common/logging/log.h"
  5. #include "common/settings.h"
  6. #include "common/settings_enums.h"
  7. #include "core/frontend/applets/controller.h"
  8. #include "hid_core/frontend/emulated_controller.h"
  9. #include "hid_core/hid_core.h"
  10. #include "hid_core/hid_types.h"
  11. namespace Core::Frontend {
  12. ControllerApplet::~ControllerApplet() = default;
  13. DefaultControllerApplet::DefaultControllerApplet(HID::HIDCore& hid_core_) : hid_core{hid_core_} {}
  14. DefaultControllerApplet::~DefaultControllerApplet() = default;
  15. void DefaultControllerApplet::Close() const {}
  16. void DefaultControllerApplet::ReconfigureControllers(ReconfigureCallback callback,
  17. const ControllerParameters& parameters) const {
  18. LOG_INFO(Service_HID, "called, deducing the best configuration based on the given parameters!");
  19. const std::size_t min_supported_players =
  20. parameters.enable_single_mode ? 1 : parameters.min_players;
  21. // Disconnect Handheld first.
  22. auto* handheld = hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
  23. handheld->Disconnect();
  24. // Deduce the best configuration based on the input parameters.
  25. for (std::size_t index = 0; index < hid_core.available_controllers - 2; ++index) {
  26. auto* controller = hid_core.GetEmulatedControllerByIndex(index);
  27. // First, disconnect all controllers regardless of the value of keep_controllers_connected.
  28. // This makes it easy to connect the desired controllers.
  29. controller->Disconnect();
  30. // Only connect the minimum number of required players.
  31. if (index >= min_supported_players) {
  32. continue;
  33. }
  34. // Connect controllers based on the following priority list from highest to lowest priority:
  35. // Pro Controller -> Dual Joycons -> Left Joycon/Right Joycon -> Handheld
  36. if (parameters.allow_pro_controller) {
  37. controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::ProController);
  38. controller->Connect(true);
  39. } else if (parameters.allow_dual_joycons) {
  40. controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconDual);
  41. controller->Connect(true);
  42. } else if (parameters.allow_left_joycon && parameters.allow_right_joycon) {
  43. // Assign left joycons to even player indices and right joycons to odd player indices.
  44. // We do this since Captain Toad Treasure Tracker expects a left joycon for Player 1 and
  45. // a right Joycon for Player 2 in 2 Player Assist mode.
  46. if (index % 2 == 0) {
  47. controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconLeft);
  48. controller->Connect(true);
  49. } else {
  50. controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconRight);
  51. controller->Connect(true);
  52. }
  53. } else if (index == 0 && parameters.enable_single_mode && parameters.allow_handheld &&
  54. !Settings::IsDockedMode()) {
  55. // We should *never* reach here under any normal circumstances.
  56. controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::Handheld);
  57. controller->Connect(true);
  58. } else {
  59. ASSERT_MSG(false, "Unable to add a new controller based on the given parameters!");
  60. }
  61. }
  62. callback(true);
  63. }
  64. } // namespace Core::Frontend