| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/WiimoteControllersWidget.h" |
| 5 | |
| 6 | #include <QAction> |
| 7 | #include <QApplication> |
| 8 | #include <QCheckBox> |
| 9 | #include <QComboBox> |
| 10 | #include <QGridLayout> |
| 11 | #include <QGroupBox> |
| 12 | #include <QLabel> |
| 13 | #include <QPushButton> |
| 14 | #include <QRadioButton> |
| 15 | #include <QScreen> |
| 16 | #include <QTimer> |
| 17 | #include <QToolButton> |
| 18 | #include <QVBoxLayout> |
| 19 | #include <QVariant> |
| 20 | |
| 21 | #include "Common/Config/Config.h" |
| 22 | #include "Common/WorkQueueThread.h" |
| 23 | |
| 24 | #include "Core/Config/MainSettings.h" |
| 25 | #include "Core/Config/WiimoteSettings.h" |
| 26 | #include "Core/ConfigManager.h" |
| 27 | #include "Core/Core.h" |
| 28 | #include "Core/HW/Wiimote.h" |
| 29 | #include "Core/HW/WiimoteReal/WiimoteReal.h" |
| 30 | #include "Core/IOS/IOS.h" |
| 31 | #include "Core/IOS/USB/Bluetooth/LibUSBBluetoothAdapter.h" |
| 32 | #include "Core/NetPlayProto.h" |
| 33 | #include "Core/System.h" |
| 34 | #include "Core/USBUtils.h" |
| 35 | #include "Core/WiiUtils.h" |
| 36 | |
| 37 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 38 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 39 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 40 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 41 | #include "DolphinQt/QtUtils/SignalBlocking.h" |
| 42 | #include "DolphinQt/Settings.h" |
| 43 | #include "DolphinQt/Settings/USBDevicePicker.h" |
| 44 | |
| 45 | #if defined(_WIN32) |
| 46 | #include "Core/HW//WiimoteReal/IOWin.h" |
| 47 | #endif |
| 48 | |
| 49 | WiimoteControllersWidget::WiimoteControllersWidget(QWidget* parent) : QWidget(parent) |
| 50 | { |
| 51 | CreateLayout(); |
| 52 | ConnectWidgets(); |
| 53 | |
| 54 | connect(&Settings::Instance(), &Settings::ConfigChanged, this, |
| 55 | [this] { LoadSettings(Core::GetState(Core::System::GetInstance())); }); |
| 56 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 57 | [this](Core::State state) { LoadSettings(state); }); |
| 58 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 59 | |
| 60 | m_bluetooth_adapter_refresh_thread.Reset("Bluetooth Adapter Refresh Thread"); |
| 61 | StartBluetoothAdapterRefresh(); |
| 62 | } |
| 63 | |
| 64 | WiimoteControllersWidget::~WiimoteControllersWidget() |
| 65 | { |
| 66 | m_bluetooth_adapter_refresh_thread.WaitForCompletion(); |
| 67 | } |
| 68 | |
| 69 | void WiimoteControllersWidget::StartBluetoothAdapterRefresh() |
| 70 | { |
| 71 | #ifdef __LIBUSB__ |
| 72 | if (m_bluetooth_adapter_scan_in_progress) |
| 73 | return; |
| 74 | |
| 75 | m_bluetooth_adapters->clear(); |
| 76 | m_bluetooth_adapters->setDisabled(true); |
| 77 | m_bluetooth_adapters->addItem(tr("Scanning for adapters...")); |
| 78 | |
| 79 | m_bluetooth_adapter_scan_in_progress = true; |
| 80 | |
| 81 | const auto scan_func = [this]() { |
| 82 | INFO_LOG_FMT(COMMON, "Refreshing Bluetooth adapter list..."); |
| 83 | auto device_list = USBUtils::ListDevices(LibUSBBluetoothAdapter::IsBluetoothDevice); |
| 84 | INFO_LOG_FMT(COMMON, "{} Bluetooth adapters available.", device_list.size()); |
| 85 | const auto refresh_complete_func = [this, devices = std::move(device_list)]() { |
| 86 | OnBluetoothAdapterRefreshComplete(devices); |
| 87 | }; |
| 88 | QueueOnObject(this, refresh_complete_func); |
| 89 | }; |
| 90 | |
| 91 | m_bluetooth_adapter_refresh_thread.Push(scan_func); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | void WiimoteControllersWidget::OnBluetoothAdapterRefreshComplete( |
| 96 | std::span<const USBUtils::DeviceInfo> devices) |
| 97 | { |
| 98 | const int configured_vid = Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID); |
| 99 | const int configured_pid = Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID); |
| 100 | bool found_configured_device = configured_vid == -1 || configured_pid == -1; |
| 101 | |
| 102 | m_bluetooth_adapters->clear(); |
| 103 | m_bluetooth_adapter_scan_in_progress = false; |
| 104 | |
| 105 | const auto state = Core::GetState(Core::System::GetInstance()); |
| 106 | UpdateBluetoothAdapterWidgetsEnabled(state); |
| 107 | |
| 108 | m_bluetooth_adapters->addItem(tr("Automatic")); |
| 109 | |
| 110 | for (auto& device : devices) |
| 111 | { |
| 112 | m_bluetooth_adapters->addItem(QString::fromStdString(device.ToDisplayString()), |
| 113 | QVariant::fromValue(device)); |
| 114 | |
| 115 | if (!found_configured_device && |
| 116 | LibUSBBluetoothAdapter::IsConfiguredBluetoothDevice(device.vid, device.pid)) |
| 117 | { |
| 118 | found_configured_device = true; |
| 119 | m_bluetooth_adapters->setCurrentIndex(m_bluetooth_adapters->count() - 1); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (!found_configured_device) |
| 124 | { |
| 125 | const QString name = QLatin1Char{'['} + tr("disconnected") + QLatin1Char(']'); |
| 126 | const std::string name_str = name.toStdString(); |
| 127 | |
| 128 | USBUtils::DeviceInfo disconnected_device; |
| 129 | disconnected_device.vid = configured_vid; |
| 130 | disconnected_device.pid = configured_pid; |
| 131 | |
| 132 | const QString device_info = |
| 133 | QString::fromStdString(disconnected_device.ToDisplayString(name_str)); |
| 134 | |
| 135 | m_bluetooth_adapters->insertSeparator(m_bluetooth_adapters->count()); |
| 136 | m_bluetooth_adapters->addItem(device_info, QVariant::fromValue(disconnected_device)); |
| 137 | m_bluetooth_adapters->setCurrentIndex(m_bluetooth_adapters->count() - 1); |
| 138 | } |
| 139 | |
| 140 | m_bluetooth_adapters->insertSeparator(m_bluetooth_adapters->count()); |
| 141 | m_bluetooth_adapters->addItem(tr("More Options...")); |
| 142 | } |
| 143 | |
| 144 | static int GetRadioButtonIndicatorWidth() |
| 145 | { |
| 146 | const QStyle* style = QApplication::style(); |
| 147 | QStyleOptionButton opt; |
| 148 | |
| 149 | // TODO: why does the macOS style act different? Is it because of the magic with |
| 150 | // Cocoa widgets it does behind the scenes? |
| 151 | if (style->objectName() == QStringLiteral("macintosh")) |
| 152 | return style->subElementRect(QStyle::SE_RadioButtonIndicator, &opt).width(); |
| 153 | |
| 154 | return style->subElementRect(QStyle::SE_RadioButtonContents, &opt).left(); |
| 155 | } |
| 156 | |
| 157 | static int GetLayoutHorizontalSpacing(const QGridLayout* layout) |
| 158 | { |
| 159 | // TODO: shouldn't layout->horizontalSpacing() do all this? Why does it return -1? |
| 160 | int hspacing = layout->horizontalSpacing(); |
| 161 | if (hspacing >= 0) |
| 162 | return hspacing; |
| 163 | |
| 164 | // According to docs, this is the fallback if horizontalSpacing() isn't set. |
| 165 | auto style = layout->parentWidget()->style(); |
| 166 | hspacing = style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); |
| 167 | if (hspacing >= 0) |
| 168 | return hspacing; |
| 169 | |
| 170 | // Docs claim this is deprecated, but on macOS with Qt 5.8 this is the only one that actually |
| 171 | // works. |
| 172 | float pixel_ratio = QGuiApplication::primaryScreen()->devicePixelRatio(); |
| 173 | #ifdef __APPLE__ |
| 174 | // TODO is this still required? |
| 175 | hspacing = pixel_ratio * style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); |
| 176 | if (hspacing >= 0) |
| 177 | return hspacing; |
| 178 | #endif |
| 179 | |
| 180 | // Ripped from qtbase/src/widgets/styles/qcommonstyle.cpp |
| 181 | return pixel_ratio * 6; |
| 182 | } |
| 183 | |
| 184 | void WiimoteControllersWidget::CreateLayout() |
| 185 | { |
| 186 | m_wiimote_layout = new QGridLayout(); |
| 187 | m_wiimote_box = new QGroupBox(tr("Wii Remotes")); |
| 188 | m_wiimote_box->setLayout(m_wiimote_layout); |
| 189 | |
| 190 | m_wiimote_passthrough = new QRadioButton(tr("Passthrough a Bluetooth adapter")); |
| 191 | m_bluetooth_adapters_label = new QLabel(tr("Adapter")); |
| 192 | m_bluetooth_adapters = new QComboBox(); |
| 193 | m_bluetooth_adapters_refresh = new NonDefaultQPushButton(tr("Refresh")); |
| 194 | m_wiimote_sync = new NonDefaultQPushButton(tr("Sync")); |
| 195 | m_wiimote_reset = new NonDefaultQPushButton(tr("Reset")); |
| 196 | |
| 197 | m_wiimote_refresh_indicator = new QLabel{}; |
| 198 | m_wiimote_refresh_indicator->hide(); |
| 199 | m_wiimote_refresh = new QToolButton(); |
| 200 | auto* const wiimote_refresh_action = new QAction(tr("Refresh"), m_wiimote_refresh); |
| 201 | m_wiimote_refresh->setDefaultAction(wiimote_refresh_action); |
| 202 | connect(wiimote_refresh_action, &QAction::triggered, this, |
| 203 | &WiimoteControllersWidget::OnWiimoteRefreshPressed); |
| 204 | m_wiimote_refresh->setPopupMode(QToolButton::ToolButtonPopupMode::MenuButtonPopup); |
| 205 | |
| 206 | #if defined(_WIN32) |
| 207 | m_wiimote_refresh_indicator->setPixmap( |
| 208 | style()->standardIcon(QStyle::SP_BrowserReload).pixmap(16, 16)); |
| 209 | |
| 210 | auto* const wiimote_sync_action = new QAction(tr("Sync"), m_wiimote_refresh); |
| 211 | m_wiimote_refresh->addAction(wiimote_sync_action); |
| 212 | connect(wiimote_sync_action, &QAction::triggered, this, |
| 213 | &WiimoteControllersWidget::TriggerHostWiimoteSync); |
| 214 | |
| 215 | auto* const wiimote_reset_action = new QAction(tr("Reset"), m_wiimote_refresh); |
| 216 | m_wiimote_refresh->addAction(wiimote_reset_action); |
| 217 | connect(wiimote_reset_action, &QAction::triggered, this, |
| 218 | &WiimoteControllersWidget::TriggerHostWiimoteReset); |
| 219 | #endif |
| 220 | |
| 221 | m_wiimote_pt_labels[0] = new QLabel(tr("Sync real Wii Remotes and pair them")); |
| 222 | m_wiimote_pt_labels[1] = new QLabel(tr("Reset all saved Wii Remote pairings")); |
| 223 | m_wiimote_emu = new QRadioButton(tr("Emulate the Wii's Bluetooth adapter")); |
| 224 | m_wiimote_continuous_scanning = new QCheckBox(tr("Continuous Scanning")); |
| 225 | m_wiimote_real_balance_board = new QCheckBox(tr("Real Balance Board")); |
| 226 | m_wiimote_speaker_data = new QCheckBox(tr("Enable Speaker Data")); |
| 227 | m_wiimote_ciface = new QCheckBox(tr("Connect Wii Remotes for Emulated Controllers")); |
| 228 | |
| 229 | m_wiimote_layout->setVerticalSpacing(7); |
| 230 | m_wiimote_layout->setColumnMinimumWidth(0, GetRadioButtonIndicatorWidth() - |
| 231 | GetLayoutHorizontalSpacing(m_wiimote_layout)); |
| 232 | m_wiimote_layout->setColumnStretch(2, 1); |
| 233 | |
| 234 | // Passthrough BT |
| 235 | m_wiimote_layout->addWidget(m_wiimote_passthrough, m_wiimote_layout->rowCount(), 0, 1, -1); |
| 236 | |
| 237 | int adapter_row = m_wiimote_layout->rowCount(); |
| 238 | m_wiimote_layout->addWidget(m_bluetooth_adapters_label, adapter_row, 1, 1, 1); |
| 239 | m_wiimote_layout->addWidget(m_bluetooth_adapters, adapter_row, 2, 1, 1); |
| 240 | m_wiimote_layout->addWidget(m_bluetooth_adapters_refresh, adapter_row, 3, 1, 1); |
| 241 | |
| 242 | int sync_row = m_wiimote_layout->rowCount(); |
| 243 | m_wiimote_layout->addWidget(m_wiimote_pt_labels[0], sync_row, 1, 1, 2); |
| 244 | m_wiimote_layout->addWidget(m_wiimote_sync, sync_row, 3); |
| 245 | |
| 246 | int reset_row = m_wiimote_layout->rowCount(); |
| 247 | m_wiimote_layout->addWidget(m_wiimote_pt_labels[1], reset_row, 1, 1, 2); |
| 248 | m_wiimote_layout->addWidget(m_wiimote_reset, reset_row, 3); |
| 249 | |
| 250 | // Emulated BT |
| 251 | m_wiimote_layout->addWidget(m_wiimote_emu, m_wiimote_layout->rowCount(), 0, 1, -1); |
| 252 | |
| 253 | for (size_t i = 0; i < m_wiimote_groups.size(); i++) |
| 254 | { |
| 255 | auto* wm_label = m_wiimote_labels[i] = new QLabel(tr("Wii Remote %1").arg(i + 1)); |
| 256 | auto* wm_box = m_wiimote_boxes[i] = new QComboBox(); |
| 257 | auto* wm_button = m_wiimote_buttons[i] = new NonDefaultQPushButton(tr("Configure")); |
| 258 | |
| 259 | for (const auto& item : {tr("None"), tr("Emulated Wii Remote"), tr("Real Wii Remote")}) |
| 260 | wm_box->addItem(item); |
| 261 | |
| 262 | int wm_row = m_wiimote_layout->rowCount(); |
| 263 | m_wiimote_layout->addWidget(wm_label, wm_row, 1); |
| 264 | m_wiimote_layout->addWidget(wm_box, wm_row, 2); |
| 265 | m_wiimote_layout->addWidget(wm_button, wm_row, 3); |
| 266 | } |
| 267 | |
| 268 | m_wiimote_layout->addWidget(m_wiimote_real_balance_board, m_wiimote_layout->rowCount(), 1, 1, -1); |
| 269 | m_wiimote_layout->addWidget(m_wiimote_speaker_data, m_wiimote_layout->rowCount(), 1, 1, -1); |
| 270 | |
| 271 | m_wiimote_layout->addWidget(m_wiimote_ciface, m_wiimote_layout->rowCount(), 0, 1, -1); |
| 272 | |
| 273 | int continuous_scanning_row = m_wiimote_layout->rowCount(); |
| 274 | |
| 275 | auto* const left_of_refresh_button_layout = new QHBoxLayout; |
| 276 | left_of_refresh_button_layout->addWidget(m_wiimote_continuous_scanning); |
| 277 | left_of_refresh_button_layout->addStretch(1); |
| 278 | left_of_refresh_button_layout->addWidget(m_wiimote_refresh_indicator); |
| 279 | |
| 280 | m_wiimote_layout->addLayout(left_of_refresh_button_layout, continuous_scanning_row, 0, 1, 3); |
| 281 | m_wiimote_layout->addWidget(m_wiimote_refresh, continuous_scanning_row, 3); |
| 282 | |
| 283 | auto* layout = new QVBoxLayout; |
| 284 | layout->setContentsMargins(0, 0, 0, 0); |
| 285 | layout->setAlignment(Qt::AlignTop); |
| 286 | layout->addWidget(m_wiimote_box); |
| 287 | setLayout(layout); |
| 288 | } |
| 289 | |
| 290 | void WiimoteControllersWidget::ConnectWidgets() |
| 291 | { |
| 292 | connect(m_wiimote_passthrough, &QRadioButton::toggled, this, [this] { |
| 293 | SaveSettings(); |
| 294 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 295 | }); |
| 296 | connect(m_wiimote_ciface, &QCheckBox::toggled, this, [this] { |
| 297 | SaveSettings(); |
| 298 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 299 | WiimoteReal::HandleWiimotesInControllerInterfaceSettingChange(); |
| 300 | }); |
| 301 | connect(m_wiimote_continuous_scanning, &QCheckBox::toggled, this, [this] { |
| 302 | SaveSettings(); |
| 303 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 304 | }); |
| 305 | |
| 306 | connect(m_wiimote_real_balance_board, &QCheckBox::toggled, this, |
| 307 | &WiimoteControllersWidget::SaveSettings); |
| 308 | connect(m_wiimote_speaker_data, &QCheckBox::toggled, this, |
| 309 | &WiimoteControllersWidget::SaveSettings); |
| 310 | connect(m_bluetooth_adapters, &QComboBox::activated, this, |
| 311 | &WiimoteControllersWidget::OnBluetoothPassthroughDeviceChanged); |
| 312 | connect(m_bluetooth_adapters_refresh, &QPushButton::clicked, this, |
| 313 | &WiimoteControllersWidget::StartBluetoothAdapterRefresh); |
| 314 | connect(m_wiimote_sync, &QPushButton::clicked, this, |
| 315 | &WiimoteControllersWidget::OnBluetoothPassthroughSyncPressed); |
| 316 | connect(m_wiimote_reset, &QPushButton::clicked, this, |
| 317 | &WiimoteControllersWidget::OnBluetoothPassthroughResetPressed); |
| 318 | |
| 319 | for (size_t i = 0; i < m_wiimote_groups.size(); i++) |
| 320 | { |
| 321 | connect(m_wiimote_boxes[i], &QComboBox::currentIndexChanged, this, [this] { |
| 322 | SaveSettings(); |
| 323 | LoadSettings(Core::GetState(Core::System::GetInstance())); |
| 324 | }); |
| 325 | connect(m_wiimote_buttons[i], &QPushButton::clicked, this, |
| 326 | [this, i] { OnWiimoteConfigure(i); }); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void WiimoteControllersWidget::OnBluetoothPassthroughDeviceChanged(int index) |
| 331 | { |
| 332 | std::optional<USBUtils::DeviceInfo> device_info; |
| 333 | bool needs_refresh = false; |
| 334 | // "Automatic" selection |
| 335 | if (index == 0) |
| 336 | { |
| 337 | Config::DeleteKey(Config::GetActiveLayerForConfig(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID), |
| 338 | Config::MAIN_BLUETOOTH_PASSTHROUGH_PID); |
| 339 | Config::DeleteKey(Config::GetActiveLayerForConfig(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID), |
| 340 | Config::MAIN_BLUETOOTH_PASSTHROUGH_VID); |
| 341 | return; |
| 342 | } |
| 343 | // "More Options..." selection |
| 344 | else if (index == m_bluetooth_adapters->count() - 1) |
| 345 | { |
| 346 | device_info = USBDevicePicker::Run(this, tr("Select a Bluetooth Device")); |
| 347 | needs_refresh = true; |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | const QVariant item_data = m_bluetooth_adapters->itemData(index); |
| 352 | |
| 353 | if (!item_data.isValid() || !item_data.canConvert<USBUtils::DeviceInfo>()) |
| 354 | { |
| 355 | ERROR_LOG_FMT(COMMON, "Invalid Bluetooth device info selected in WiimoteControllersWidget"); |
| 356 | return; |
| 357 | } |
| 358 | device_info = item_data.value<USBUtils::DeviceInfo>(); |
| 359 | } |
| 360 | |
| 361 | if (device_info.has_value()) |
| 362 | { |
| 363 | Config::SetBaseOrCurrent(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID, device_info->pid); |
| 364 | Config::SetBaseOrCurrent(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID, device_info->vid); |
| 365 | } |
| 366 | if (needs_refresh) |
| 367 | StartBluetoothAdapterRefresh(); |
| 368 | } |
| 369 | |
| 370 | void WiimoteControllersWidget::OnBluetoothPassthroughResetPressed() |
| 371 | { |
| 372 | const auto ios = Core::System::GetInstance().GetIOS(); |
| 373 | |
| 374 | if (!ios) |
| 375 | { |
| 376 | ModalMessageBox::warning( |
| 377 | this, tr("Warning"), |
| 378 | tr("Saved Wii Remote pairings can only be reset when a Wii game is running.")); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | auto device = WiiUtils::GetBluetoothRealDevice(); |
| 383 | if (device != nullptr) |
| 384 | device->TriggerSyncButtonHeldEvent(); |
| 385 | } |
| 386 | |
| 387 | void WiimoteControllersWidget::OnBluetoothPassthroughSyncPressed() |
| 388 | { |
| 389 | const auto ios = Core::System::GetInstance().GetIOS(); |
| 390 | |
| 391 | if (!ios) |
| 392 | { |
| 393 | ModalMessageBox::warning(this, tr("Warning"), |
| 394 | tr("A sync can only be triggered when a Wii game is running.")); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | auto device = WiiUtils::GetBluetoothRealDevice(); |
| 399 | if (device != nullptr) |
| 400 | device->TriggerSyncButtonPressedEvent(); |
| 401 | } |
| 402 | |
| 403 | void WiimoteControllersWidget::OnWiimoteRefreshPressed() |
| 404 | { |
| 405 | WiimoteReal::Refresh(); |
| 406 | } |
| 407 | |
| 408 | void WiimoteControllersWidget::OnWiimoteConfigure(size_t index) |
| 409 | { |
| 410 | MappingWindow::Type type; |
| 411 | switch (m_wiimote_boxes[index]->currentIndex()) |
| 412 | { |
| 413 | case 0: // None |
| 414 | case 2: // Real Wii Remote |
| 415 | return; |
| 416 | case 1: // Emulated Wii Remote |
| 417 | type = MappingWindow::Type::MAPPING_WIIMOTE_EMU; |
| 418 | break; |
| 419 | default: |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | MappingWindow* window = new MappingWindow(this, type, static_cast<int>(index)); |
| 424 | window->setAttribute(Qt::WA_DeleteOnClose, true); |
| 425 | window->setWindowModality(Qt::WindowModality::WindowModal); |
| 426 | window->show(); |
| 427 | } |
| 428 | |
| 429 | void WiimoteControllersWidget::UpdateBluetoothAdapterWidgetsEnabled(const Core::State state) |
| 430 | { |
| 431 | const bool running = state != Core::State::Uninitialized; |
| 432 | const bool running_wii = running && Core::System::GetInstance().IsWii(); |
| 433 | const bool enable_adapter_refresh = m_wiimote_passthrough->isChecked() && !running_wii; |
| 434 | const bool enable_adapter_selection = |
| 435 | enable_adapter_refresh && !m_bluetooth_adapter_scan_in_progress; |
| 436 | |
| 437 | m_bluetooth_adapters_label->setEnabled(enable_adapter_selection); |
| 438 | m_bluetooth_adapters->setEnabled(enable_adapter_selection); |
| 439 | m_bluetooth_adapters_refresh->setEnabled(enable_adapter_refresh); |
| 440 | } |
| 441 | |
| 442 | void WiimoteControllersWidget::LoadSettings(Core::State state) |
| 443 | { |
| 444 | for (size_t i = 0; i < m_wiimote_groups.size(); i++) |
| 445 | { |
| 446 | SignalBlocking(m_wiimote_boxes[i]) |
| 447 | ->setCurrentIndex(int(Config::Get(Config::GetInfoForWiimoteSource(int(i))))); |
| 448 | } |
| 449 | SignalBlocking(m_wiimote_real_balance_board) |
| 450 | ->setChecked(Config::Get(Config::WIIMOTE_BB_SOURCE) == WiimoteSource::Real); |
| 451 | SignalBlocking(m_wiimote_speaker_data) |
| 452 | ->setChecked(Config::Get(Config::MAIN_WIIMOTE_ENABLE_SPEAKER)); |
| 453 | SignalBlocking(m_wiimote_ciface) |
| 454 | ->setChecked(Config::Get(Config::MAIN_CONNECT_WIIMOTES_FOR_CONTROLLER_INTERFACE)); |
| 455 | SignalBlocking(m_wiimote_continuous_scanning) |
| 456 | ->setChecked(Config::Get(Config::MAIN_WIIMOTE_CONTINUOUS_SCANNING)); |
| 457 | |
| 458 | if (Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_ENABLED)) |
| 459 | SignalBlocking(m_wiimote_passthrough)->setChecked(true); |
| 460 | else |
| 461 | SignalBlocking(m_wiimote_emu)->setChecked(true); |
| 462 | |
| 463 | // Make sure continuous scanning setting is applied. |
| 464 | WiimoteReal::Initialize(::Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES); |
| 465 | |
| 466 | const bool running = state != Core::State::Uninitialized; |
| 467 | |
| 468 | m_wiimote_emu->setEnabled(!running); |
| 469 | m_wiimote_passthrough->setEnabled(!running); |
| 470 | |
| 471 | const bool running_gc = running && !Core::System::GetInstance().IsWii(); |
| 472 | const bool enable_passthrough = m_wiimote_passthrough->isChecked() && !running_gc; |
| 473 | const bool enable_emu_bt = !m_wiimote_passthrough->isChecked() && !running_gc; |
| 474 | const bool is_netplay = NetPlay::IsNetPlayRunning(); |
| 475 | const bool running_netplay = running && is_netplay; |
| 476 | |
| 477 | UpdateBluetoothAdapterWidgetsEnabled(state); |
| 478 | |
| 479 | m_wiimote_sync->setEnabled(enable_passthrough); |
| 480 | m_wiimote_reset->setEnabled(enable_passthrough); |
| 481 | |
| 482 | for (auto* pt_label : m_wiimote_pt_labels) |
| 483 | pt_label->setEnabled(enable_passthrough); |
| 484 | |
| 485 | const int num_local_wiimotes = is_netplay ? NetPlay::NumLocalWiimotes() : 4; |
| 486 | for (size_t i = 0; i < m_wiimote_groups.size(); i++) |
| 487 | { |
| 488 | m_wiimote_labels[i]->setEnabled(enable_emu_bt); |
| 489 | m_wiimote_boxes[i]->setEnabled(enable_emu_bt && !running_netplay); |
| 490 | |
| 491 | const bool is_emu_wiimote = m_wiimote_boxes[i]->currentIndex() == 1; |
| 492 | m_wiimote_buttons[i]->setEnabled(enable_emu_bt && is_emu_wiimote && |
| 493 | static_cast<int>(i) < num_local_wiimotes); |
| 494 | } |
| 495 | |
| 496 | m_wiimote_real_balance_board->setEnabled(enable_emu_bt && !running_netplay); |
| 497 | m_wiimote_speaker_data->setEnabled(enable_emu_bt && !running_netplay); |
| 498 | |
| 499 | const bool ciface_wiimotes = m_wiimote_ciface->isChecked(); |
| 500 | |
| 501 | m_wiimote_refresh->setEnabled((enable_emu_bt || ciface_wiimotes) && |
| 502 | !m_wiimote_continuous_scanning->isChecked()); |
| 503 | m_wiimote_continuous_scanning->setEnabled(enable_emu_bt || ciface_wiimotes); |
| 504 | } |
| 505 | |
| 506 | void WiimoteControllersWidget::SaveSettings() |
| 507 | { |
| 508 | { |
| 509 | Config::ConfigChangeCallbackGuard config_guard; |
| 510 | Config::SetBaseOrCurrent(Config::MAIN_WIIMOTE_ENABLE_SPEAKER, |
| 511 | m_wiimote_speaker_data->isChecked()); |
| 512 | Config::SetBaseOrCurrent(Config::MAIN_CONNECT_WIIMOTES_FOR_CONTROLLER_INTERFACE, |
| 513 | m_wiimote_ciface->isChecked()); |
| 514 | Config::SetBaseOrCurrent(Config::MAIN_WIIMOTE_CONTINUOUS_SCANNING, |
| 515 | m_wiimote_continuous_scanning->isChecked()); |
| 516 | Config::SetBaseOrCurrent(Config::MAIN_BLUETOOTH_PASSTHROUGH_ENABLED, |
| 517 | m_wiimote_passthrough->isChecked()); |
| 518 | |
| 519 | const WiimoteSource bb_source = |
| 520 | m_wiimote_real_balance_board->isChecked() ? WiimoteSource::Real : WiimoteSource::None; |
| 521 | Config::SetBaseOrCurrent(Config::WIIMOTE_BB_SOURCE, bb_source); |
| 522 | |
| 523 | for (size_t i = 0; i < m_wiimote_groups.size(); i++) |
| 524 | { |
| 525 | const int index = m_wiimote_boxes[i]->currentIndex(); |
| 526 | Config::SetBaseOrCurrent(Config::GetInfoForWiimoteSource(int(i)), WiimoteSource(index)); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | SConfig::GetInstance().SaveSettings(); |
| 531 | } |
| 532 | |
| 533 | #if defined(_WIN32) |
| 534 | void WiimoteControllersWidget::AsyncRefreshActionHelper(std::invocable<> auto func) |
| 535 | { |
| 536 | m_wiimote_refresh->setEnabled(false); |
| 537 | m_wiimote_refresh_indicator->show(); |
| 538 | |
| 539 | auto result = std::async(std::launch::async, std::move(func)); |
| 540 | |
| 541 | auto* const animation = new QTimer{this}; |
| 542 | connect(animation, &QTimer::timeout, this, [this, animation, result = std::move(result)] { |
| 543 | // Spin the refresh indicator. |
| 544 | m_wiimote_refresh_indicator->setPixmap( |
| 545 | m_wiimote_refresh_indicator->pixmap().transformed(QTransform().rotate(90))); |
| 546 | |
| 547 | if (result.wait_for(std::chrono::seconds{}) != std::future_status::ready) |
| 548 | return; |
| 549 | |
| 550 | // When the async task is done, re-enable the button and hide the indicator. |
| 551 | animation->deleteLater(); |
| 552 | m_wiimote_refresh_indicator->hide(); |
| 553 | m_wiimote_refresh->setEnabled(true); |
| 554 | }); |
| 555 | |
| 556 | animation->start(250); |
| 557 | } |
| 558 | |
| 559 | void WiimoteControllersWidget::TriggerHostWiimoteSync() |
| 560 | { |
| 561 | AsyncRefreshActionHelper(WiimoteReal::WiimoteScannerWindows::FindAndAuthenticateWiimotes); |
| 562 | } |
| 563 | |
| 564 | void WiimoteControllersWidget::TriggerHostWiimoteReset() |
| 565 | { |
| 566 | AsyncRefreshActionHelper(WiimoteReal::WiimoteScannerWindows::RemoveRememberedWiimotes); |
| 567 | } |
| 568 | #endif |