2020-08-21 11:39:24 +00:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-08-28 16:45:15 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2020-08-21 11:39:24 +00:00
|
|
|
#include "core/frontend/applets/controller.h"
|
|
|
|
#include "core/hle/service/hid/controllers/npad.h"
|
|
|
|
#include "core/hle/service/hid/hid.h"
|
|
|
|
#include "core/hle/service/sm/sm.h"
|
|
|
|
|
|
|
|
namespace Core::Frontend {
|
|
|
|
|
|
|
|
ControllerApplet::~ControllerApplet() = default;
|
|
|
|
|
2020-09-25 23:13:10 +00:00
|
|
|
DefaultControllerApplet::DefaultControllerApplet(Service::SM::ServiceManager& service_manager_)
|
|
|
|
: service_manager{service_manager_} {}
|
|
|
|
|
2020-08-21 11:39:24 +00:00
|
|
|
DefaultControllerApplet::~DefaultControllerApplet() = default;
|
|
|
|
|
|
|
|
void DefaultControllerApplet::ReconfigureControllers(std::function<void()> callback,
|
2020-10-26 23:36:58 +00:00
|
|
|
const ControllerParameters& parameters) const {
|
2020-08-21 11:39:24 +00:00
|
|
|
LOG_INFO(Service_HID, "called, deducing the best configuration based on the given parameters!");
|
|
|
|
|
|
|
|
auto& npad =
|
2020-09-25 23:13:10 +00:00
|
|
|
service_manager.GetService<Service::HID::Hid>("hid")
|
2020-08-21 11:39:24 +00:00
|
|
|
->GetAppletResource()
|
|
|
|
->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad);
|
|
|
|
|
2020-09-28 14:00:15 +00:00
|
|
|
auto& players = Settings::values.players.GetValue();
|
2020-08-21 11:39:24 +00:00
|
|
|
|
2020-08-28 17:14:19 +00:00
|
|
|
const std::size_t min_supported_players =
|
2020-10-21 02:07:39 +00:00
|
|
|
parameters.enable_single_mode ? 1 : parameters.min_players;
|
2020-08-27 09:33:46 +00:00
|
|
|
|
|
|
|
// Disconnect Handheld first.
|
2020-10-06 11:00:18 +00:00
|
|
|
npad.DisconnectNpadAtIndex(8);
|
2020-08-27 09:33:46 +00:00
|
|
|
|
2020-08-21 11:39:24 +00:00
|
|
|
// Deduce the best configuration based on the input parameters.
|
2020-08-27 09:33:46 +00:00
|
|
|
for (std::size_t index = 0; index < players.size() - 2; ++index) {
|
2020-08-21 11:39:24 +00:00
|
|
|
// First, disconnect all controllers regardless of the value of keep_controllers_connected.
|
|
|
|
// This makes it easy to connect the desired controllers.
|
2020-10-06 11:00:18 +00:00
|
|
|
npad.DisconnectNpadAtIndex(index);
|
2020-08-27 09:33:46 +00:00
|
|
|
|
|
|
|
// Only connect the minimum number of required players.
|
|
|
|
if (index >= min_supported_players) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect controllers based on the following priority list from highest to lowest priority:
|
2020-08-28 03:38:26 +00:00
|
|
|
// Pro Controller -> Dual Joycons -> Left Joycon/Right Joycon -> Handheld
|
2020-08-27 09:33:46 +00:00
|
|
|
if (parameters.allow_pro_controller) {
|
2021-09-21 01:46:17 +00:00
|
|
|
npad.AddNewControllerAt(Core::HID::EmulatedController::MapSettingsTypeToNPad(
|
|
|
|
Settings::ControllerType::ProController),
|
|
|
|
index);
|
2020-08-27 09:33:46 +00:00
|
|
|
} else if (parameters.allow_dual_joycons) {
|
2021-09-21 01:46:17 +00:00
|
|
|
npad.AddNewControllerAt(Core::HID::EmulatedController::MapSettingsTypeToNPad(
|
|
|
|
Settings::ControllerType::DualJoyconDetached),
|
|
|
|
index);
|
2020-08-28 03:38:26 +00:00
|
|
|
} else if (parameters.allow_left_joycon && parameters.allow_right_joycon) {
|
|
|
|
// Assign left joycons to even player indices and right joycons to odd player indices.
|
|
|
|
// We do this since Captain Toad Treasure Tracker expects a left joycon for Player 1 and
|
|
|
|
// a right Joycon for Player 2 in 2 Player Assist mode.
|
|
|
|
if (index % 2 == 0) {
|
2021-09-21 01:46:17 +00:00
|
|
|
npad.AddNewControllerAt(Core::HID::EmulatedController::MapSettingsTypeToNPad(
|
|
|
|
Settings::ControllerType::LeftJoycon),
|
|
|
|
index);
|
2020-08-28 03:38:26 +00:00
|
|
|
} else {
|
2021-09-21 01:46:17 +00:00
|
|
|
npad.AddNewControllerAt(Core::HID::EmulatedController::MapSettingsTypeToNPad(
|
|
|
|
Settings::ControllerType::RightJoycon),
|
|
|
|
index);
|
2020-08-28 03:38:26 +00:00
|
|
|
}
|
2020-08-27 09:33:46 +00:00
|
|
|
} else if (index == 0 && parameters.enable_single_mode && parameters.allow_handheld &&
|
2020-09-28 14:00:15 +00:00
|
|
|
!Settings::values.use_docked_mode.GetValue()) {
|
2020-08-27 09:33:46 +00:00
|
|
|
// We should *never* reach here under any normal circumstances.
|
2021-09-21 01:46:17 +00:00
|
|
|
npad.AddNewControllerAt(Core::HID::EmulatedController::MapSettingsTypeToNPad(
|
|
|
|
Settings::ControllerType::Handheld),
|
2020-08-27 09:33:46 +00:00
|
|
|
index);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE_MSG("Unable to add a new controller based on the given parameters!");
|
|
|
|
}
|
2020-08-21 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core::Frontend
|