Add a GUI option to select a bluetooth device

This commit is contained in:
Joshua Vandaële
2025-02-28 08:07:17 +01:00
parent 826625c7be
commit 4773a6f323
8 changed files with 227 additions and 22 deletions

View File

@ -13,6 +13,7 @@
#include <QRadioButton>
#include <QScreen>
#include <QVBoxLayout>
#include <QVariant>
#include <map>
#include <optional>
@ -57,6 +58,50 @@ void WiimoteControllersWidget::UpdateBluetoothAvailableStatus()
m_bluetooth_unavailable->setHidden(WiimoteReal::IsScannerReady());
}
void WiimoteControllersWidget::RefreshBluetoothAdapters()
{
m_bluetooth_adapters->clear();
const int configured_vid = Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID);
const int configured_pid = Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID);
bool found_configured_device = configured_vid == -1 || configured_pid == -1;
m_bluetooth_adapters->addItem(tr("Automatic"));
for (auto& device : IOS::HLE::BluetoothRealDevice::ListDevices())
{
std::string name = device.name.empty() ? tr("Unknown Device").toStdString() : device.name;
QString device_info =
QString::fromStdString(fmt::format("{} ({:04x}:{:04x})", name, device.vid, device.pid));
m_bluetooth_adapters->addItem(device_info, QVariant::fromValue(device));
if (!found_configured_device &&
IOS::HLE::BluetoothRealDevice::IsConfiguredBluetoothDevice(device.vid, device.pid))
{
found_configured_device = true;
m_bluetooth_adapters->setCurrentIndex(m_bluetooth_adapters->count() - 1);
}
}
if (!found_configured_device)
{
const QString name = QLatin1Char{'['} + tr("disconnected") + QLatin1Char(']');
const std::string name_str = name.toStdString();
IOS::HLE::BluetoothRealDevice::BluetoothDeviceInfo disconnected_device;
disconnected_device.vid = configured_vid;
disconnected_device.pid = configured_pid;
disconnected_device.name = name_str;
QString device_info = QString::fromStdString(
fmt::format("{} ({:04x}:{:04x})", name_str, configured_vid, configured_pid));
m_bluetooth_adapters->insertSeparator(m_bluetooth_adapters->count());
m_bluetooth_adapters->addItem(device_info, QVariant::fromValue(disconnected_device));
m_bluetooth_adapters->setCurrentIndex(m_bluetooth_adapters->count() - 1);
}
}
static int GetRadioButtonIndicatorWidth()
{
const QStyle* style = QApplication::style();
@ -104,6 +149,8 @@ void WiimoteControllersWidget::CreateLayout()
m_wiimote_box->setLayout(m_wiimote_layout);
m_wiimote_passthrough = new QRadioButton(tr("Passthrough a Bluetooth adapter"));
m_bluetooth_adapters_label = new QLabel(tr("Bluetooth adapter"));
m_bluetooth_adapters = new QComboBox();
m_wiimote_sync = new NonDefaultQPushButton(tr("Sync"));
m_wiimote_reset = new NonDefaultQPushButton(tr("Reset"));
m_wiimote_refresh = new NonDefaultQPushButton(tr("Refresh"));
@ -123,6 +170,10 @@ void WiimoteControllersWidget::CreateLayout()
// Passthrough BT
m_wiimote_layout->addWidget(m_wiimote_passthrough, m_wiimote_layout->rowCount(), 0, 1, -1);
int adapter_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(m_bluetooth_adapters_label, adapter_row, 1, 1, 1);
m_wiimote_layout->addWidget(m_bluetooth_adapters, adapter_row, 2, 1, 2);
int sync_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(m_wiimote_pt_labels[0], sync_row, 1, 1, 2);
m_wiimote_layout->addWidget(m_wiimote_sync, sync_row, 3);
@ -189,6 +240,8 @@ void WiimoteControllersWidget::ConnectWidgets()
&WiimoteControllersWidget::SaveSettings);
connect(m_wiimote_speaker_data, &QCheckBox::toggled, this,
&WiimoteControllersWidget::SaveSettings);
connect(m_bluetooth_adapters, &QComboBox::activated, this,
&WiimoteControllersWidget::OnBluetoothPassthroughDeviceChanged);
connect(m_wiimote_sync, &QPushButton::clicked, this,
&WiimoteControllersWidget::OnBluetoothPassthroughSyncPressed);
connect(m_wiimote_reset, &QPushButton::clicked, this,
@ -207,6 +260,25 @@ void WiimoteControllersWidget::ConnectWidgets()
}
}
void WiimoteControllersWidget::OnBluetoothPassthroughDeviceChanged(int index)
{
// "Automatic" selection
if (index == 0)
{
Config::DeleteKey(Config::GetActiveLayerForConfig(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID),
Config::MAIN_BLUETOOTH_PASSTHROUGH_PID);
Config::DeleteKey(Config::GetActiveLayerForConfig(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID),
Config::MAIN_BLUETOOTH_PASSTHROUGH_VID);
return;
}
auto device_info = m_bluetooth_adapters->itemData(index)
.value<IOS::HLE::BluetoothRealDevice::BluetoothDeviceInfo>();
Config::SetBaseOrCurrent(Config::MAIN_BLUETOOTH_PASSTHROUGH_PID, device_info.pid);
Config::SetBaseOrCurrent(Config::MAIN_BLUETOOTH_PASSTHROUGH_VID, device_info.vid);
}
void WiimoteControllersWidget::OnBluetoothPassthroughResetPressed()
{
const auto ios = Core::System::GetInstance().GetIOS();
@ -297,11 +369,15 @@ void WiimoteControllersWidget::LoadSettings(Core::State state)
m_wiimote_passthrough->setEnabled(!running);
const bool running_gc = running && !Core::System::GetInstance().IsWii();
const bool running_wii = running && Core::System::GetInstance().IsWii();
const bool enable_passthrough = m_wiimote_passthrough->isChecked() && !running_gc;
const bool enable_adapter_selection = m_wiimote_passthrough->isChecked() && !running_wii;
const bool enable_emu_bt = !m_wiimote_passthrough->isChecked() && !running_gc;
const bool is_netplay = NetPlay::IsNetPlayRunning();
const bool running_netplay = running && is_netplay;
m_bluetooth_adapters_label->setEnabled(enable_adapter_selection);
m_bluetooth_adapters->setEnabled(enable_adapter_selection);
m_wiimote_sync->setEnabled(enable_passthrough);
m_wiimote_reset->setEnabled(enable_passthrough);