You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
4.9 KiB
99 lines
4.9 KiB
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "common/assert.h"
|
|
#include "common/logging.h"
|
|
#include "common/settings.h"
|
|
#include "common/settings_enums.h"
|
|
#include "core/frontend/applets/controller.h"
|
|
#include "hid_core/frontend/emulated_controller.h"
|
|
#include "hid_core/hid_core.h"
|
|
#include "hid_core/hid_types.h"
|
|
#include <array>
|
|
|
|
namespace Core::Frontend {
|
|
|
|
ControllerApplet::~ControllerApplet() = default;
|
|
|
|
DefaultControllerApplet::DefaultControllerApplet(HID::HIDCore& hid_core_) : hid_core{hid_core_} {}
|
|
|
|
DefaultControllerApplet::~DefaultControllerApplet() = default;
|
|
|
|
void DefaultControllerApplet::Close() const {}
|
|
|
|
void DefaultControllerApplet::ReconfigureControllers(ReconfigureCallback callback,
|
|
const ControllerParameters& parameters) const {
|
|
LOG_INFO(Service_HID, "called, deducing the best configuration based on the given parameters!");
|
|
|
|
const std::size_t min_supported_players =
|
|
parameters.enable_single_mode ? 1 : parameters.min_players;
|
|
using Core::HID::NpadStyleIndex;
|
|
const std::size_t max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players;
|
|
std::size_t num_selected_players = 0;
|
|
std::array<bool, HID::HIDCore::available_controllers> keep_connected{};
|
|
|
|
// reserve existing AND valid players before filling slots. include Handheld, but not other
|
|
for (std::size_t index = 0; index < hid_core.available_controllers - 1; ++index) {
|
|
const auto* controller = hid_core.GetEmulatedControllerByIndex(index);
|
|
if (!parameters.keep_controllers_connected || !controller->IsConnected() || num_selected_players >= max_supported_players) continue;
|
|
|
|
const auto style = controller->GetNpadStyleIndex();
|
|
keep_connected[index] =
|
|
(style == NpadStyleIndex::Fullkey && parameters.allow_pro_controller) ||
|
|
(style == NpadStyleIndex::JoyconDual && parameters.allow_dual_joycons) ||
|
|
(style == NpadStyleIndex::JoyconLeft && parameters.allow_left_joycon) ||
|
|
(style == NpadStyleIndex::JoyconRight && parameters.allow_right_joycon) ||
|
|
(style == NpadStyleIndex::Handheld && parameters.enable_single_mode && parameters.allow_handheld && !Settings::IsDockedMode()) ||
|
|
(style == NpadStyleIndex::GameCube && parameters.allow_gamecube_controller);
|
|
num_selected_players += keep_connected[index];
|
|
}
|
|
auto* handheld = hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
|
|
if (!keep_connected[hid_core.available_controllers - 2]) handheld->Disconnect();
|
|
|
|
// Deduce the best configuration based on the input parameters.
|
|
for (std::size_t index = 0; index < hid_core.available_controllers - 2; ++index) {
|
|
auto* controller = hid_core.GetEmulatedControllerByIndex(index);
|
|
|
|
if (keep_connected[index]) continue;
|
|
controller->Disconnect();
|
|
|
|
// only add players still needed to reach the minimum
|
|
if (num_selected_players >= min_supported_players) continue;
|
|
++num_selected_players;
|
|
|
|
// Connect controllers based on the following priority list from highest to lowest priority:
|
|
// Pro Controller -> Dual Joycons -> Left Joycon/Right Joycon -> Handheld
|
|
if (parameters.allow_pro_controller) {
|
|
controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::Fullkey);
|
|
controller->Connect(true);
|
|
} else if (parameters.allow_dual_joycons) {
|
|
controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconDual);
|
|
controller->Connect(true);
|
|
} 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) {
|
|
controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconLeft);
|
|
controller->Connect(true);
|
|
} else {
|
|
controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::JoyconRight);
|
|
controller->Connect(true);
|
|
}
|
|
} else if (index == 0 && parameters.enable_single_mode && parameters.allow_handheld &&
|
|
!Settings::IsDockedMode()) {
|
|
// We should *never* reach here under any normal circumstances.
|
|
controller->SetNpadStyleIndex(Core::HID::NpadStyleIndex::Handheld);
|
|
controller->Connect(true);
|
|
} else {
|
|
ASSERT_MSG(false, "Unable to add a new controller based on the given parameters!");
|
|
}
|
|
}
|
|
|
|
callback(true);
|
|
}
|
|
|
|
} // namespace Core::Frontend
|