From 2ac1ca133fac669e876e80d4b31202bcc2826d71 Mon Sep 17 00:00:00 2001 From: Vincent Duvert Date: Thu, 19 Jul 2018 08:23:16 +0200 Subject: [PATCH] GCPadWiiUConfigDialog: Update the adapter state dynamically Update the GC adapter config GUI if the adapter is plugged or unplugged. --- .../Config/Mapping/GCPadWiiUConfigDialog.cpp | 61 +++++++++++-------- .../Config/Mapping/GCPadWiiUConfigDialog.h | 4 ++ Source/Core/InputCommon/GCAdapter.cpp | 10 ++- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp b/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp index 85cd35fb79..232957ea1f 100644 --- a/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp @@ -10,6 +10,7 @@ #include #include "Core/ConfigManager.h" +#include "DolphinQt/QtUtils/QueueOnObject.h" #include "InputCommon/GCAdapter.h" @@ -24,10 +25,43 @@ GCPadWiiUConfigDialog::GCPadWiiUConfigDialog(int port, QWidget* parent) ConnectWidgets(); } +GCPadWiiUConfigDialog::~GCPadWiiUConfigDialog() +{ + GCAdapter::SetAdapterCallback(nullptr); +} + void GCPadWiiUConfigDialog::CreateLayout() { setWindowTitle(tr("GameCube Adapter for Wii U at Port %1").arg(m_port + 1)); + m_layout = new QVBoxLayout(); + m_status_label = new QLabel(); + m_rumble = new QCheckBox(tr("Enable Rumble")); + m_simulate_bongos = new QCheckBox(tr("Simulate DK Bongos")); + m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok); + + UpdateAdapterStatus(); + + auto callback = [this] { QueueOnObject(this, &GCPadWiiUConfigDialog::UpdateAdapterStatus); }; + GCAdapter::SetAdapterCallback(callback); + + m_layout->addWidget(m_status_label); + m_layout->addWidget(m_rumble); + m_layout->addWidget(m_simulate_bongos); + m_layout->addWidget(m_button_box); + + setLayout(m_layout); +} + +void GCPadWiiUConfigDialog::ConnectWidgets() +{ + connect(m_rumble, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); + connect(m_simulate_bongos, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); + connect(m_button_box, &QDialogButtonBox::accepted, this, &GCPadWiiUConfigDialog::accept); +} + +void GCPadWiiUConfigDialog::UpdateAdapterStatus() +{ const char* error_message = nullptr; const bool detected = GCAdapter::IsDetected(&error_message); QString status_text; @@ -45,31 +79,10 @@ void GCPadWiiUConfigDialog::CreateLayout() status_text = tr("No Adapter Detected"); } - m_layout = new QVBoxLayout(); - m_status_label = new QLabel(status_text); - m_rumble = new QCheckBox(tr("Enable Rumble")); - m_simulate_bongos = new QCheckBox(tr("Simulate DK Bongos")); - m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok); + m_status_label->setText(status_text); - m_layout->addWidget(m_status_label); - m_layout->addWidget(m_rumble); - m_layout->addWidget(m_simulate_bongos); - m_layout->addWidget(m_button_box); - - if (!detected) - { - m_rumble->setEnabled(false); - m_simulate_bongos->setEnabled(false); - } - - setLayout(m_layout); -} - -void GCPadWiiUConfigDialog::ConnectWidgets() -{ - connect(m_rumble, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); - connect(m_simulate_bongos, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); - connect(m_button_box, &QDialogButtonBox::accepted, this, &GCPadWiiUConfigDialog::accept); + m_rumble->setEnabled(detected); + m_simulate_bongos->setEnabled(detected); } void GCPadWiiUConfigDialog::LoadSettings() diff --git a/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h b/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h index 77cdb93355..bd1adb6d03 100644 --- a/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h +++ b/Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h @@ -16,6 +16,7 @@ class GCPadWiiUConfigDialog final : public QDialog Q_OBJECT public: explicit GCPadWiiUConfigDialog(int port, QWidget* parent = nullptr); + ~GCPadWiiUConfigDialog(); private: void LoadSettings(); @@ -24,6 +25,9 @@ private: void CreateLayout(); void ConnectWidgets(); +private: + void UpdateAdapterStatus(); + int m_port; QVBoxLayout* m_layout; diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 174e374bf8..a4a4cee5cf 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -122,6 +122,10 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl std::lock_guard lk(s_init_mutex); AddGCAdapter(dev); } + else if (s_status < 0 && s_detect_callback != nullptr) + { + s_detect_callback(); + } } else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) { @@ -130,7 +134,11 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl // Reset a potential error status now that the adapter is unplugged if (s_status < 0) - s_status = 0; + { + s_status = NO_ADAPTER_DETECTED; + if (s_detect_callback != nullptr) + s_detect_callback(); + } } return 0; }