From 574c609709e12326284ef694d955c7a018cfd89f Mon Sep 17 00:00:00 2001 From: spycrab Date: Wed, 23 May 2018 23:47:42 +0200 Subject: [PATCH] Qt/Mapping: Add option to map all devices at once --- .../Config/Mapping/MappingButton.cpp | 59 +++++++++++++++++-- .../Config/Mapping/MappingWindow.cpp | 10 ++++ .../DolphinQt2/Config/Mapping/MappingWindow.h | 1 + 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp b/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp index b63073cfa6..d60ff43459 100644 --- a/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp +++ b/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp @@ -2,7 +2,9 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include +#include #include #include @@ -108,15 +110,64 @@ void MappingButton::Detect() // Make sure that we don't block event handling std::thread thread([this] { - const auto dev = m_parent->GetDevice(); - setText(QStringLiteral("...")); // Avoid that the button press itself is registered as an event Common::SleepCurrentThread(100); - const auto expr = MappingCommon::DetectExpression( - m_reference, dev.get(), m_parent->GetController()->GetDefaultDevice()); + std::vector>> futures; + QString expr; + + if (m_parent->GetParent()->IsMappingAllDevices()) + { + for (const std::string& device_str : g_controller_interface.GetAllDeviceStrings()) + { + ciface::Core::DeviceQualifier devq; + devq.FromString(device_str); + + auto dev = g_controller_interface.FindDevice(devq); + + auto future = std::async(std::launch::async, [this, devq, dev, device_str] { + return std::make_pair( + QString::fromStdString(device_str), + MappingCommon::DetectExpression(m_reference, dev.get(), + m_parent->GetController()->GetDefaultDevice())); + }); + + futures.push_back(std::move(future)); + } + + bool done = false; + + while (!done) + { + for (auto& future : futures) + { + const auto status = future.wait_for(std::chrono::milliseconds(10)); + if (status == std::future_status::ready) + { + const auto pair = future.get(); + + done = true; + + if (pair.second.isEmpty()) + break; + + expr = QStringLiteral("`%1:%2`") + .arg(pair.first) + .arg(pair.second.startsWith(QLatin1Char('`')) ? pair.second.mid(1) : + pair.second); + break; + } + } + } + } + else + { + const auto dev = m_parent->GetDevice(); + expr = MappingCommon::DetectExpression(m_reference, dev.get(), + m_parent->GetController()->GetDefaultDevice()); + } releaseMouse(); releaseKeyboard(); diff --git a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp index 814d0070ae..3f8fd13d1b 100644 --- a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp +++ b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp @@ -222,10 +222,18 @@ void MappingWindow::OnSaveProfilePressed() void MappingWindow::OnDeviceChanged(int index) { + if (IsMappingAllDevices()) + return; + const auto device = m_devices_combo->currentText().toStdString(); m_controller->SetDefaultDevice(device); } +bool MappingWindow::IsMappingAllDevices() const +{ + return m_devices_combo->currentIndex() == m_devices_combo->count() - 1; +} + void MappingWindow::RefreshDevices() { m_devices_combo->clear(); @@ -245,6 +253,8 @@ void MappingWindow::RefreshDevices() m_devices_combo->addItem(QString::fromStdString(name)); } + m_devices_combo->addItem(tr("All devices")); + m_devices_combo->setCurrentIndex(0); }); } diff --git a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.h b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.h index 27c36e1726..c0a9f47af2 100644 --- a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.h +++ b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.h @@ -52,6 +52,7 @@ public: std::shared_ptr GetDevice() const; ControllerEmu::EmulatedController* GetController() const; bool IsIterativeInput() const; + bool IsMappingAllDevices() const; signals: void Update();