2017-06-13 15:16:41 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/MappingCommon.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2022-02-20 17:32:39 +00:00
|
|
|
#include <chrono>
|
2019-02-28 00:10:18 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPushButton>
|
2021-01-13 09:06:05 +00:00
|
|
|
#include <QRegularExpression>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <QString>
|
2019-04-27 17:35:26 +00:00
|
|
|
#include <QTimer>
|
2017-06-13 15:16:41 +00:00
|
|
|
|
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"
|
2022-02-20 17:32:39 +00:00
|
|
|
#include "InputCommon/ControllerInterface/MappingCommon.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
namespace MappingCommon
|
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
constexpr auto INPUT_DETECT_INITIAL_TIME = std::chrono::seconds(3);
|
2021-05-02 19:27:11 +00:00
|
|
|
constexpr auto INPUT_DETECT_CONFIRMATION_TIME = std::chrono::milliseconds(0);
|
2020-02-12 22:49:17 +00:00
|
|
|
constexpr auto INPUT_DETECT_MAXIMUM_TIME = std::chrono::seconds(5);
|
|
|
|
|
|
|
|
constexpr auto OUTPUT_TEST_TIME = std::chrono::seconds(2);
|
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
QString DetectExpression(QPushButton* button, ciface::Core::DeviceContainer& device_container,
|
|
|
|
const std::vector<std::string>& device_strings,
|
2022-02-20 17:32:39 +00:00
|
|
|
const ciface::Core::DeviceQualifier& default_device,
|
|
|
|
ciface::MappingCommon::Quote quote)
|
2017-06-13 15:16:41 +00:00
|
|
|
{
|
2019-04-27 17:35:26 +00:00
|
|
|
const auto filter = new BlockUserInputFilter(button);
|
|
|
|
|
|
|
|
button->installEventFilter(filter);
|
2019-02-28 00:10:18 +00:00
|
|
|
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
|
2019-04-27 17:35:26 +00:00
|
|
|
Common::SleepCurrentThread(50);
|
2019-02-28 00:10:18 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
auto detections =
|
|
|
|
device_container.DetectInput(device_strings, INPUT_DETECT_INITIAL_TIME,
|
|
|
|
INPUT_DETECT_CONFIRMATION_TIME, INPUT_DETECT_MAXIMUM_TIME);
|
|
|
|
|
2022-02-20 17:32:39 +00:00
|
|
|
ciface::MappingCommon::RemoveSpuriousTriggerCombinations(&detections);
|
2019-02-28 00:10:18 +00:00
|
|
|
|
2019-04-27 17:35:26 +00:00
|
|
|
const auto timer = new QTimer(button);
|
|
|
|
|
2021-02-04 13:32:03 +00:00
|
|
|
timer->setSingleShot(true);
|
|
|
|
|
2019-04-27 17:35:26 +00:00
|
|
|
button->connect(timer, &QTimer::timeout, [button, filter] {
|
|
|
|
button->releaseMouse();
|
|
|
|
button->releaseKeyboard();
|
|
|
|
button->removeEventFilter(filter);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Prevent mappings of "space", "return", or mouse clicks from re-activating detection.
|
|
|
|
timer->start(500);
|
2019-02-28 00:10:18 +00:00
|
|
|
|
|
|
|
button->setText(old_text);
|
|
|
|
|
2022-02-20 17:32:39 +00:00
|
|
|
return QString::fromStdString(BuildExpression(detections, 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);
|
2020-02-12 22:49:17 +00:00
|
|
|
std::this_thread::sleep_for(OUTPUT_TEST_TIME);
|
2019-02-28 00:10:18 +00:00
|
|
|
reference->State(0.0);
|
|
|
|
|
|
|
|
button->setText(old_text);
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:15:37 +00:00
|
|
|
} // namespace MappingCommon
|