Skip to content

Commit

Permalink
GCPadWiiUConfigDialog: Update the adapter state dynamically
Browse files Browse the repository at this point in the history
Update the GC adapter config GUI if the adapter is plugged or unplugged.
  • Loading branch information
VinDuv committed May 29, 2019
1 parent b08e2ec commit 2ac1ca1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
61 changes: 37 additions & 24 deletions Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp
Expand Up @@ -10,6 +10,7 @@
#include <QVBoxLayout>

#include "Core/ConfigManager.h"
#include "DolphinQt/QtUtils/QueueOnObject.h"

#include "InputCommon/GCAdapter.h"

Expand All @@ -24,44 +25,31 @@ 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));

const char* error_message = nullptr;
const bool detected = GCAdapter::IsDetected(&error_message);
QString status_text;

if (detected)
{
status_text = tr("Adapter Detected");
}
else if (error_message)
{
status_text = tr("Error Opening Adapter: %1").arg(QString::fromUtf8(error_message));
}
else
{
status_text = tr("No Adapter Detected");
}

m_layout = new QVBoxLayout();
m_status_label = new QLabel(status_text);
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);

if (!detected)
{
m_rumble->setEnabled(false);
m_simulate_bongos->setEnabled(false);
}

setLayout(m_layout);
}

Expand All @@ -72,6 +60,31 @@ void GCPadWiiUConfigDialog::ConnectWidgets()
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;

if (detected)
{
status_text = tr("Adapter Detected");
}
else if (error_message)
{
status_text = tr("Error Opening Adapter: %1").arg(QString::fromUtf8(error_message));
}
else
{
status_text = tr("No Adapter Detected");
}

m_status_label->setText(status_text);

m_rumble->setEnabled(detected);
m_simulate_bongos->setEnabled(detected);
}

void GCPadWiiUConfigDialog::LoadSettings()
{
m_rumble->setChecked(SConfig::GetInstance().m_AdapterRumble[m_port]);
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h
Expand Up @@ -16,6 +16,7 @@ class GCPadWiiUConfigDialog final : public QDialog
Q_OBJECT
public:
explicit GCPadWiiUConfigDialog(int port, QWidget* parent = nullptr);
~GCPadWiiUConfigDialog();

private:
void LoadSettings();
Expand All @@ -24,6 +25,9 @@ class GCPadWiiUConfigDialog final : public QDialog
void CreateLayout();
void ConnectWidgets();

private:
void UpdateAdapterStatus();

int m_port;

QVBoxLayout* m_layout;
Expand Down
10 changes: 9 additions & 1 deletion Source/Core/InputCommon/GCAdapter.cpp
Expand Up @@ -122,6 +122,10 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl
std::lock_guard<std::mutex> 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)
{
Expand All @@ -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;
}
Expand Down

0 comments on commit 2ac1ca1

Please sign in to comment.