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>
|
2020-02-12 22:49:17 +00:00
|
|
|
#include <vector>
|
2019-02-28 00:10:18 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPushButton>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <QRegExp>
|
|
|
|
#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"
|
|
|
|
|
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);
|
|
|
|
constexpr auto INPUT_DETECT_CONFIRMATION_TIME = std::chrono::milliseconds(500);
|
|
|
|
constexpr auto INPUT_DETECT_MAXIMUM_TIME = std::chrono::seconds(5);
|
|
|
|
|
|
|
|
constexpr auto OUTPUT_TEST_TIME = std::chrono::seconds(2);
|
|
|
|
|
|
|
|
// Pressing inputs at the same time will result in the & operator vs a hotkey expression.
|
|
|
|
constexpr auto HOTKEY_VS_CONJUNCION_THRESHOLD = std::chrono::milliseconds(50);
|
|
|
|
|
|
|
|
// Some devices (e.g. DS4) provide an analog and digital input for the trigger.
|
|
|
|
// We prefer just the analog input for simultaneous digital+analog input detections.
|
|
|
|
constexpr auto SPURIOUS_TRIGGER_COMBO_THRESHOLD = std::chrono::milliseconds(150);
|
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());
|
2019-07-30 11:57:06 +00:00
|
|
|
expr += QLatin1Char{':'};
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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);
|
|
|
|
|
|
|
|
RemoveSpuriousTriggerCombinations(&detections);
|
2019-02-28 00:10:18 +00:00
|
|
|
|
2019-04-27 17:35:26 +00:00
|
|
|
const auto timer = new QTimer(button);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
return 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);
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
void RemoveSpuriousTriggerCombinations(
|
|
|
|
std::vector<ciface::Core::DeviceContainer::InputDetection>* detections)
|
|
|
|
{
|
|
|
|
const auto is_spurious = [&](auto& detection) {
|
|
|
|
return std::any_of(detections->begin(), detections->end(), [&](auto& d) {
|
|
|
|
// This is a suprious digital detection if a "smooth" (analog) detection is temporally near.
|
|
|
|
return &d != &detection && d.smoothness > 1 &&
|
|
|
|
abs(d.press_time - detection.press_time) < SPURIOUS_TRIGGER_COMBO_THRESHOLD;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
detections->erase(std::remove_if(detections->begin(), detections->end(), is_spurious),
|
|
|
|
detections->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
BuildExpression(const std::vector<ciface::Core::DeviceContainer::InputDetection>& detections,
|
|
|
|
const ciface::Core::DeviceQualifier& default_device, Quote quote)
|
|
|
|
{
|
|
|
|
std::vector<const ciface::Core::DeviceContainer::InputDetection*> pressed_inputs;
|
|
|
|
|
|
|
|
QStringList alternations;
|
|
|
|
|
|
|
|
const auto get_control_expression = [&](auto& detection) {
|
|
|
|
// Return the parent-most name if there is one for better hotkey strings.
|
|
|
|
// Detection of L/R_Ctrl will be changed to just Ctrl.
|
|
|
|
// Users can manually map L_Ctrl if they so desire.
|
|
|
|
const auto input = (quote == Quote::On) ?
|
|
|
|
detection.device->GetParentMostInput(detection.input) :
|
|
|
|
detection.input;
|
|
|
|
|
|
|
|
ciface::Core::DeviceQualifier device_qualifier;
|
|
|
|
device_qualifier.FromDevice(detection.device.get());
|
|
|
|
|
|
|
|
return MappingCommon::GetExpressionForControl(QString::fromStdString(input->GetName()),
|
|
|
|
device_qualifier, default_device, quote);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool new_alternation = false;
|
|
|
|
|
|
|
|
const auto handle_press = [&](auto& detection) {
|
|
|
|
pressed_inputs.emplace_back(&detection);
|
|
|
|
new_alternation = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto handle_release = [&]() {
|
|
|
|
if (!new_alternation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_alternation = false;
|
|
|
|
|
|
|
|
QStringList alternation;
|
|
|
|
for (auto* input : pressed_inputs)
|
|
|
|
alternation.push_back(get_control_expression(*input));
|
|
|
|
|
|
|
|
const bool is_hotkey = pressed_inputs.size() >= 2 &&
|
|
|
|
(pressed_inputs[1]->press_time - pressed_inputs[0]->press_time) >
|
|
|
|
HOTKEY_VS_CONJUNCION_THRESHOLD;
|
|
|
|
|
|
|
|
if (is_hotkey)
|
|
|
|
{
|
|
|
|
alternations.push_back(QStringLiteral("@(%1)").arg(alternation.join(QLatin1Char('+'))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alternation.sort();
|
|
|
|
alternations.push_back(alternation.join(QLatin1Char('&')));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& detection : detections)
|
|
|
|
{
|
|
|
|
// Remove since released inputs.
|
|
|
|
for (auto it = pressed_inputs.begin(); it != pressed_inputs.end();)
|
|
|
|
{
|
|
|
|
if (!((*it)->release_time > detection.press_time))
|
|
|
|
{
|
|
|
|
handle_release();
|
|
|
|
it = pressed_inputs.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_press(detection);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_release();
|
|
|
|
|
|
|
|
alternations.removeDuplicates();
|
|
|
|
return alternations.join(QLatin1Char('|'));
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:15:37 +00:00
|
|
|
} // namespace MappingCommon
|