| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/EmulatedUSB/WiiSpeakWindow.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QComboBox> |
| 10 | #include <QGridLayout> |
| 11 | #include <QGroupBox> |
| 12 | #include <QHBoxLayout> |
| 13 | #include <QLabel> |
| 14 | #include <QString> |
| 15 | #include <QVBoxLayout> |
| 16 | |
| 17 | #ifdef HAVE_CUBEB |
| 18 | #include "AudioCommon/CubebUtils.h" |
| 19 | #endif |
| 20 | #include "Core/Config/MainSettings.h" |
| 21 | #include "Core/Core.h" |
| 22 | #include "Core/System.h" |
| 23 | #include "DolphinQt/Resources.h" |
| 24 | #include "DolphinQt/Settings.h" |
| 25 | |
| 26 | WiiSpeakWindow::WiiSpeakWindow(QWidget* parent) : QWidget(parent) |
| 27 | { |
| 28 | // i18n: Window for managing the Wii Speak microphone |
| 29 | setWindowTitle(tr("Wii Speak Manager")); |
| 30 | setWindowIcon(Resources::GetAppIcon()); |
| 31 | setObjectName(QStringLiteral("wii_speak_manager")); |
| 32 | setMinimumSize(QSize(700, 200)); |
| 33 | |
| 34 | CreateMainWindow(); |
| 35 | |
| 36 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 37 | &WiiSpeakWindow::OnEmulationStateChanged); |
| 38 | |
| 39 | OnEmulationStateChanged(Core::GetState(Core::System::GetInstance())); |
| 40 | } |
| 41 | |
| 42 | WiiSpeakWindow::~WiiSpeakWindow() = default; |
| 43 | |
| 44 | void WiiSpeakWindow::CreateMainWindow() |
| 45 | { |
| 46 | auto* main_layout = new QVBoxLayout(); |
| 47 | auto* label = new QLabel(); |
| 48 | label->setText(QStringLiteral("<center><i>%1</i></center>") |
| 49 | .arg(tr("Some settings cannot be changed when emulation is running."))); |
| 50 | main_layout->addWidget(label); |
| 51 | |
| 52 | auto* checkbox_group = new QGroupBox(); |
| 53 | auto* checkbox_layout = new QHBoxLayout(); |
| 54 | checkbox_layout->setAlignment(Qt::AlignHCenter); |
| 55 | m_checkbox_enabled = new QCheckBox(tr("Emulate Wii Speak"), this); |
| 56 | m_checkbox_enabled->setChecked(Config::Get(Config::MAIN_EMULATE_WII_SPEAK)); |
| 57 | connect(m_checkbox_enabled, &QCheckBox::toggled, this, &WiiSpeakWindow::EmulateWiiSpeak); |
| 58 | checkbox_layout->addWidget(m_checkbox_enabled); |
| 59 | checkbox_group->setLayout(checkbox_layout); |
| 60 | main_layout->addWidget(checkbox_group); |
| 61 | |
| 62 | auto* config_group = new QGroupBox(tr("Microphone Configuration")); |
| 63 | auto* config_layout = new QHBoxLayout(); |
| 64 | |
| 65 | auto checkbox_mic_muted = new QCheckBox(tr("Mute"), this); |
| 66 | checkbox_mic_muted->setChecked(Settings::Instance().IsWiiSpeakMuted()); |
| 67 | connect(&Settings::Instance(), &Settings::WiiSpeakMuteChanged, checkbox_mic_muted, |
| 68 | &QCheckBox::setChecked); |
| 69 | connect(checkbox_mic_muted, &QCheckBox::toggled, &Settings::Instance(), |
| 70 | &Settings::SetWiiSpeakMuted); |
| 71 | checkbox_mic_muted->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 72 | config_layout->addWidget(checkbox_mic_muted); |
| 73 | |
| 74 | auto* volume_layout = new QGridLayout(); |
| 75 | static constexpr int FILTER_MIN = -50; |
| 76 | static constexpr int FILTER_MAX = 50; |
| 77 | const int volume_modifier = |
| 78 | std::clamp<int>(Config::Get(Config::MAIN_WII_SPEAK_VOLUME_MODIFIER), FILTER_MIN, FILTER_MAX); |
| 79 | auto filter_slider = new QSlider(Qt::Horizontal, this); |
| 80 | auto slider_label = new QLabel(tr("Volume modifier (value: %1dB)").arg(volume_modifier)); |
| 81 | connect(filter_slider, &QSlider::valueChanged, this, [slider_label](int value) { |
| 82 | Config::SetBaseOrCurrent(Config::MAIN_WII_SPEAK_VOLUME_MODIFIER, value); |
| 83 | slider_label->setText(tr("Volume modifier (value: %1dB)").arg(value)); |
| 84 | }); |
| 85 | filter_slider->setMinimum(FILTER_MIN); |
| 86 | filter_slider->setMaximum(FILTER_MAX); |
| 87 | filter_slider->setValue(volume_modifier); |
| 88 | filter_slider->setTickPosition(QSlider::TicksBothSides); |
| 89 | filter_slider->setTickInterval(10); |
| 90 | filter_slider->setSingleStep(1); |
| 91 | volume_layout->addWidget(new QLabel(QStringLiteral("%1dB").arg(FILTER_MIN)), 0, 0, Qt::AlignLeft); |
| 92 | volume_layout->addWidget(slider_label, 0, 1, Qt::AlignCenter); |
| 93 | volume_layout->addWidget(new QLabel(QStringLiteral("%1dB").arg(FILTER_MAX)), 0, 2, |
| 94 | Qt::AlignRight); |
| 95 | volume_layout->addWidget(filter_slider, 1, 0, 1, 3); |
| 96 | config_layout->addLayout(volume_layout); |
| 97 | config_layout->setStretch(1, 3); |
| 98 | |
| 99 | m_combobox_microphones = new QComboBox(); |
| 100 | #ifndef HAVE_CUBEB |
| 101 | m_combobox_microphones->addItem(QLatin1String("(%1)").arg(tr("Audio backend unsupported")), |
| 102 | QString{}); |
| 103 | #else |
| 104 | m_combobox_microphones->addItem(QLatin1String("(%1)").arg(tr("Autodetect preferred microphone")), |
| 105 | QString{}); |
| 106 | for (auto& [device_id, device_name] : CubebUtils::ListInputDevices()) |
| 107 | { |
| 108 | const auto user_data = QString::fromStdString(device_id); |
| 109 | m_combobox_microphones->addItem(QString::fromStdString(device_name), user_data); |
| 110 | } |
| 111 | #endif |
| 112 | connect(m_combobox_microphones, &QComboBox::currentIndexChanged, this, |
| 113 | &WiiSpeakWindow::OnInputDeviceChange); |
| 114 | |
| 115 | auto current_device_id = QString::fromStdString(Config::Get(Config::MAIN_WII_SPEAK_MICROPHONE)); |
| 116 | m_combobox_microphones->setCurrentIndex(m_combobox_microphones->findData(current_device_id)); |
| 117 | config_layout->addWidget(m_combobox_microphones); |
| 118 | |
| 119 | config_group->setLayout(config_layout); |
| 120 | main_layout->addWidget(config_group); |
| 121 | |
| 122 | setLayout(main_layout); |
| 123 | } |
| 124 | |
| 125 | void WiiSpeakWindow::EmulateWiiSpeak(bool emulate) |
| 126 | { |
| 127 | Config::SetBaseOrCurrent(Config::MAIN_EMULATE_WII_SPEAK, emulate); |
| 128 | } |
| 129 | |
| 130 | void WiiSpeakWindow::OnEmulationStateChanged(Core::State state) |
| 131 | { |
| 132 | const bool running = state != Core::State::Uninitialized; |
| 133 | |
| 134 | m_checkbox_enabled->setEnabled(!running); |
| 135 | m_combobox_microphones->setEnabled(!running); |
| 136 | } |
| 137 | |
| 138 | void WiiSpeakWindow::OnInputDeviceChange() |
| 139 | { |
| 140 | auto user_data = m_combobox_microphones->currentData(); |
| 141 | if (!user_data.isValid()) |
| 142 | return; |
| 143 | |
| 144 | const std::string device_id = user_data.toString().toStdString(); |
| 145 | Config::SetBaseOrCurrent(Config::MAIN_WII_SPEAK_MICROPHONE, device_id); |
| 146 | } |