2017-06-13 15:16:41 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/MappingCommon.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPushButton>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <QRegExp>
|
|
|
|
#include <QString>
|
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
#include "DolphinQt/QtUtils/BlockUserInputFilter.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
|
|
|
#include "InputCommon/ControllerInterface/Device.h"
|
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
namespace MappingCommon
|
|
|
|
{
|
2018-12-22 16:58:34 +00:00
|
|
|
constexpr int INPUT_DETECT_TIME = 3000;
|
2019-02-28 00:10:18 +00:00
|
|
|
constexpr int OUTPUT_TEST_TIME = 2000;
|
2018-12-22 16:58:34 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
QString GetExpressionForControl(const QString& control_name,
|
|
|
|
const ciface::Core::DeviceQualifier& control_device,
|
2018-07-16 21:15:37 +00:00
|
|
|
const ciface::Core::DeviceQualifier& default_device, Quote quote)
|
2017-06-13 15:16:41 +00:00
|
|
|
{
|
|
|
|
QString expr;
|
|
|
|
|
|
|
|
// non-default device
|
|
|
|
if (control_device != default_device)
|
|
|
|
{
|
|
|
|
expr += QString::fromStdString(control_device.ToString());
|
|
|
|
expr += QStringLiteral(":");
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the control name
|
|
|
|
expr += control_name;
|
|
|
|
|
2018-07-16 21:15:37 +00:00
|
|
|
if (quote == Quote::On)
|
|
|
|
{
|
|
|
|
QRegExp reg(QStringLiteral("[a-zA-Z]+"));
|
|
|
|
if (!reg.exactMatch(expr))
|
|
|
|
expr = QStringLiteral("`%1`").arg(expr);
|
|
|
|
}
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
QString DetectExpression(QPushButton* button, ciface::Core::DeviceContainer& device_container,
|
|
|
|
const std::vector<std::string>& device_strings,
|
2018-07-16 21:15:37 +00:00
|
|
|
const ciface::Core::DeviceQualifier& default_device, Quote quote)
|
2017-06-13 15:16:41 +00:00
|
|
|
{
|
2019-02-28 00:10:18 +00:00
|
|
|
button->installEventFilter(BlockUserInputFilter::Instance());
|
|
|
|
button->grabKeyboard();
|
|
|
|
button->grabMouse();
|
2018-12-22 16:58:34 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
const auto old_text = button->text();
|
|
|
|
button->setText(QStringLiteral("..."));
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
// The button text won't be updated if we don't process events here
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
|
|
// Avoid that the button press itself is registered as an event
|
|
|
|
Common::SleepCurrentThread(100);
|
|
|
|
|
|
|
|
std::shared_ptr<ciface::Core::Device> device;
|
|
|
|
ciface::Core::Device::Input* input;
|
2019-03-15 01:27:49 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
std::tie(device, input) = device_container.DetectInput(INPUT_DETECT_TIME, device_strings);
|
|
|
|
|
|
|
|
button->releaseMouse();
|
|
|
|
button->releaseKeyboard();
|
|
|
|
button->removeEventFilter(BlockUserInputFilter::Instance());
|
|
|
|
|
|
|
|
button->setText(old_text);
|
|
|
|
|
|
|
|
if (!input)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ciface::Core::DeviceQualifier device_qualifier;
|
|
|
|
device_qualifier.FromDevice(device.get());
|
|
|
|
|
|
|
|
return MappingCommon::GetExpressionForControl(QString::fromStdString(input->GetName()),
|
|
|
|
device_qualifier, default_device, quote);
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
2019-02-28 00:10:18 +00:00
|
|
|
|
|
|
|
void TestOutput(QPushButton* button, OutputReference* reference)
|
|
|
|
{
|
|
|
|
const auto old_text = button->text();
|
|
|
|
button->setText(QStringLiteral("..."));
|
|
|
|
|
|
|
|
// The button text won't be updated if we don't process events here
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
|
|
reference->State(1.0);
|
|
|
|
Common::SleepCurrentThread(OUTPUT_TEST_TIME);
|
|
|
|
reference->State(0.0);
|
|
|
|
|
|
|
|
button->setText(old_text);
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:15:37 +00:00
|
|
|
} // namespace MappingCommon
|