| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/GamecubeControllersWidget.h" |
| 5 | |
| 6 | #include <QComboBox> |
| 7 | #include <QGridLayout> |
| 8 | #include <QGroupBox> |
| 9 | #include <QLabel> |
| 10 | #include <QPushButton> |
| 11 | #include <QVBoxLayout> |
| 12 | |
| 13 | #include <optional> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "Core/ConfigManager.h" |
| 17 | #include "Core/Core.h" |
| 18 | #include "Core/HW/SI/SI.h" |
| 19 | #include "Core/HW/SI/SI_Device.h" |
| 20 | #include "Core/NetPlayProto.h" |
| 21 | #include "Core/System.h" |
| 22 | |
| 23 | #include "DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h" |
| 24 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 25 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 26 | #include "DolphinQt/QtUtils/SignalBlocking.h" |
| 27 | #include "DolphinQt/Settings.h" |
| 28 | |
| 29 | using SIDeviceName = std::pair<SerialInterface::SIDevices, const char*>; |
| 30 | static constexpr std::array s_gc_types = { |
| 31 | SIDeviceName{SerialInterface::SIDEVICE_NONE, _trans("None")}, |
| 32 | SIDeviceName{SerialInterface::SIDEVICE_GC_CONTROLLER, _trans("Standard Controller")}, |
| 33 | SIDeviceName{SerialInterface::SIDEVICE_WIIU_ADAPTER, |
| 34 | _trans("GameCube Controller Adapter (USB)")}, |
| 35 | SIDeviceName{SerialInterface::SIDEVICE_GC_STEERING, _trans("Steering Wheel")}, |
| 36 | SIDeviceName{SerialInterface::SIDEVICE_DANCEMAT, _trans("Dance Mat")}, |
| 37 | SIDeviceName{SerialInterface::SIDEVICE_GC_TARUKONGA, _trans("DK Bongos")}, |
| 38 | #ifdef HAS_LIBMGBA |
| 39 | SIDeviceName{SerialInterface::SIDEVICE_GC_GBA_EMULATED, _trans("GBA (Integrated)")}, |
| 40 | #endif |
| 41 | SIDeviceName{SerialInterface::SIDEVICE_GC_GBA, _trans("GBA (TCP)")}, |
| 42 | SIDeviceName{SerialInterface::SIDEVICE_GC_KEYBOARD, _trans("Keyboard Controller")}, |
| 43 | SIDeviceName{SerialInterface::SIDEVICE_AM_BASEBOARD, _trans("Triforce Baseboard")}, |
| 44 | }; |
| 45 | |
| 46 | static std::optional<int> ToGCMenuIndex(const SerialInterface::SIDevices sidevice) |
| 47 | { |
| 48 | for (size_t i = 0; i < s_gc_types.size(); ++i) |
| 49 | { |
| 50 | if (s_gc_types[i].first == sidevice) |
| 51 | return static_cast<int>(i); |
| 52 | } |
| 53 | return {}; |
| 54 | } |
| 55 | |
| 56 | static SerialInterface::SIDevices FromGCMenuIndex(const int menudevice) |
| 57 | { |
| 58 | return s_gc_types[menudevice].first; |
| 59 | } |
| 60 | |
| 61 | GamecubeControllersWidget::GamecubeControllersWidget(QWidget* parent) : QWidget(parent) |
| 62 | { |
| 63 | CreateLayout(); |
| 64 | ConnectWidgets(); |
| 65 | |
| 66 | connect(&Settings::Instance(), &Settings::ConfigChanged, this, |
| 67 | [this] { LoadSettings(Core::GetState(Core::System::GetInstance())); }); |
| 68 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 69 | [this](Core::State state) { LoadSettings(state); }); |
| 70 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 71 | } |
| 72 | |
| 73 | void GamecubeControllersWidget::CreateLayout() |
| 74 | { |
| 75 | m_gc_box = new QGroupBox(tr("GameCube Controllers")); |
| 76 | m_gc_layout = new QGridLayout(); |
| 77 | m_gc_layout->setVerticalSpacing(7); |
| 78 | m_gc_layout->setColumnStretch(1, 1); |
| 79 | |
| 80 | for (size_t i = 0; i < m_gc_groups.size(); i++) |
| 81 | { |
| 82 | auto* gc_label = new QLabel(tr("Port %1").arg(i + 1)); |
| 83 | auto* gc_box = m_gc_controller_boxes[i] = new QComboBox(); |
| 84 | auto* gc_button = m_gc_buttons[i] = new NonDefaultQPushButton(tr("Configure")); |
| 85 | |
| 86 | for (const auto& item : s_gc_types) |
| 87 | { |
| 88 | gc_box->addItem(tr(item.second)); |
| 89 | } |
| 90 | |
| 91 | int controller_row = m_gc_layout->rowCount(); |
| 92 | m_gc_layout->addWidget(gc_label, controller_row, 0); |
| 93 | m_gc_layout->addWidget(gc_box, controller_row, 1); |
| 94 | m_gc_layout->addWidget(gc_button, controller_row, 2); |
| 95 | } |
| 96 | m_gc_box->setLayout(m_gc_layout); |
| 97 | |
| 98 | auto* layout = new QVBoxLayout; |
| 99 | layout->setContentsMargins(0, 0, 0, 0); |
| 100 | layout->setAlignment(Qt::AlignTop); |
| 101 | layout->addWidget(m_gc_box); |
| 102 | setLayout(layout); |
| 103 | } |
| 104 | |
| 105 | void GamecubeControllersWidget::ConnectWidgets() |
| 106 | { |
| 107 | for (size_t i = 0; i < m_gc_controller_boxes.size(); ++i) |
| 108 | { |
| 109 | connect(m_gc_controller_boxes[i], &QComboBox::currentIndexChanged, this, [this, i] { |
| 110 | OnGCTypeChanged(i); |
| 111 | SaveSettings(); |
| 112 | }); |
| 113 | connect(m_gc_buttons[i], &QPushButton::clicked, this, [this, i] { OnGCPadConfigure(i); }); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void GamecubeControllersWidget::OnGCTypeChanged(size_t index) |
| 118 | { |
| 119 | const SerialInterface::SIDevices si_device = |
| 120 | FromGCMenuIndex(m_gc_controller_boxes[index]->currentIndex()); |
| 121 | m_gc_buttons[index]->setEnabled(si_device != SerialInterface::SIDEVICE_NONE && |
| 122 | si_device != SerialInterface::SIDEVICE_GC_GBA); |
| 123 | } |
| 124 | |
| 125 | void GamecubeControllersWidget::OnGCPadConfigure(size_t index) |
| 126 | { |
| 127 | MappingWindow::Type type; |
| 128 | |
| 129 | switch (FromGCMenuIndex(m_gc_controller_boxes[index]->currentIndex())) |
| 130 | { |
| 131 | case SerialInterface::SIDEVICE_NONE: |
| 132 | case SerialInterface::SIDEVICE_GC_GBA: |
| 133 | return; |
| 134 | case SerialInterface::SIDEVICE_GC_CONTROLLER: |
| 135 | type = MappingWindow::Type::MAPPING_GCPAD; |
| 136 | break; |
| 137 | case SerialInterface::SIDEVICE_WIIU_ADAPTER: |
| 138 | { |
| 139 | GCPadWiiUConfigDialog dialog(static_cast<int>(index), this); |
| 140 | dialog.exec(); |
| 141 | return; |
| 142 | } |
| 143 | case SerialInterface::SIDEVICE_GC_STEERING: |
| 144 | type = MappingWindow::Type::MAPPING_GC_STEERINGWHEEL; |
| 145 | break; |
| 146 | case SerialInterface::SIDEVICE_DANCEMAT: |
| 147 | type = MappingWindow::Type::MAPPING_GC_DANCEMAT; |
| 148 | break; |
| 149 | case SerialInterface::SIDEVICE_GC_TARUKONGA: |
| 150 | type = MappingWindow::Type::MAPPING_GC_BONGOS; |
| 151 | break; |
| 152 | case SerialInterface::SIDEVICE_GC_GBA_EMULATED: |
| 153 | type = MappingWindow::Type::MAPPING_GC_GBA; |
| 154 | break; |
| 155 | case SerialInterface::SIDEVICE_GC_KEYBOARD: |
| 156 | type = MappingWindow::Type::MAPPING_GC_KEYBOARD; |
| 157 | break; |
| 158 | case SerialInterface::SIDEVICE_AM_BASEBOARD: |
| 159 | type = MappingWindow::Type::MAPPING_AM_BASEBOARD; |
| 160 | break; |
| 161 | default: |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | MappingWindow* window = new MappingWindow(this, type, static_cast<int>(index)); |
| 166 | window->setAttribute(Qt::WA_DeleteOnClose, true); |
| 167 | window->setWindowModality(Qt::WindowModality::WindowModal); |
| 168 | window->show(); |
| 169 | } |
| 170 | |
| 171 | void GamecubeControllersWidget::LoadSettings(Core::State state) |
| 172 | { |
| 173 | const bool running = state != Core::State::Uninitialized; |
| 174 | for (size_t i = 0; i < m_gc_groups.size(); i++) |
| 175 | { |
| 176 | const SerialInterface::SIDevices si_device = |
| 177 | Config::Get(Config::GetInfoForSIDevice(static_cast<int>(i))); |
| 178 | const std::optional<int> gc_index = ToGCMenuIndex(si_device); |
| 179 | if (gc_index) |
| 180 | { |
| 181 | SignalBlocking(m_gc_controller_boxes[i])->setCurrentIndex(*gc_index); |
| 182 | m_gc_controller_boxes[i]->setEnabled(NetPlay::IsNetPlayRunning() ? !running : true); |
| 183 | OnGCTypeChanged(i); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void GamecubeControllersWidget::SaveSettings() |
| 189 | { |
| 190 | { |
| 191 | Config::ConfigChangeCallbackGuard config_guard; |
| 192 | |
| 193 | auto& system = Core::System::GetInstance(); |
| 194 | for (size_t i = 0; i < m_gc_groups.size(); ++i) |
| 195 | { |
| 196 | const SerialInterface::SIDevices si_device = |
| 197 | FromGCMenuIndex(m_gc_controller_boxes[i]->currentIndex()); |
| 198 | Config::SetBaseOrCurrent(Config::GetInfoForSIDevice(static_cast<int>(i)), si_device); |
| 199 | |
| 200 | if (Core::IsRunning(system)) |
| 201 | { |
| 202 | system.GetSerialInterface().ChangeDevice(si_device, static_cast<s32>(i)); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | SConfig::GetInstance().SaveSettings(); |
| 208 | } |