| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Mapping/MappingCommon.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <deque> |
| 8 | #include <memory> |
| 9 | |
| 10 | #include <QTimer> |
| 11 | |
| 12 | #include "DolphinQt/Config/Mapping/MappingButton.h" |
| 13 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 14 | #include "DolphinQt/QtUtils/BlockUserInputFilter.h" |
| 15 | |
| 16 | #include "InputCommon/ControlReference/ControlReference.h" |
| 17 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 18 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 19 | #include "InputCommon/ControllerInterface/MappingCommon.h" |
| 20 | #include "InputCommon/InputConfig.h" |
| 21 | |
| 22 | namespace MappingCommon |
| 23 | { |
| 24 | constexpr auto INPUT_DETECT_INITIAL_TIME = std::chrono::seconds(3); |
| 25 | constexpr auto INPUT_DETECT_CONFIRMATION_TIME = std::chrono::milliseconds(750); |
| 26 | constexpr auto INPUT_DETECT_MAXIMUM_TIME = std::chrono::seconds(5); |
| 27 | // Ignore the mouse-click when queuing more buttons with "alternate mappings" enabled. |
| 28 | constexpr auto INPUT_DETECT_ENDING_IGNORE_TIME = std::chrono::milliseconds(50); |
| 29 | |
| 30 | class MappingProcessor : public QObject |
| 31 | { |
| 32 | public: |
| 33 | MappingProcessor(MappingWindow* parent) : QObject{parent}, m_parent{parent} |
| 34 | { |
| 35 | using MW = MappingWindow; |
| 36 | using MP = MappingProcessor; |
| 37 | |
| 38 | connect(parent, &MW::Update, this, &MP::ProcessMappingButtons); |
| 39 | connect(parent, &MW::ConfigChanged, this, &MP::CancelMapping); |
| 40 | |
| 41 | connect(parent, &MW::UnQueueInputDetection, this, &MP::UnQueueInputDetection); |
| 42 | connect(parent, &MW::QueueInputDetection, this, &MP::QueueInputDetection); |
| 43 | connect(parent, &MW::CancelMapping, this, &MP::CancelMapping); |
| 44 | |
| 45 | m_input_detection_start_timer = new QTimer(this); |
| 46 | m_input_detection_start_timer->setSingleShot(true); |
| 47 | connect(m_input_detection_start_timer, &QTimer::timeout, this, &MP::StartInputDetection); |
| 48 | } |
| 49 | |
| 50 | void StartInputDetection() |
| 51 | { |
| 52 | const auto& default_device = m_parent->GetController()->GetDefaultDevice(); |
| 53 | auto& button = m_clicked_mapping_buttons.front(); |
| 54 | |
| 55 | // Focus just makes it more clear which button is currently being mapped. |
| 56 | button->setFocus(); |
| 57 | button->setText(tr("[ Press Now ]")); |
| 58 | QtUtils::InstallKeyboardBlocker(button, button, &MappingButton::ConfigChanged); |
| 59 | |
| 60 | std::vector device_strings{default_device.ToString()}; |
| 61 | if (m_parent->IsCreateOtherDeviceMappingsEnabled()) |
| 62 | device_strings = g_controller_interface.GetAllDeviceStrings(); |
| 63 | |
| 64 | m_input_detector = std::make_unique<ciface::Core::InputDetector>(); |
| 65 | const auto lock = m_parent->GetController()->GetStateLock(); |
| 66 | m_input_detector->Start(g_controller_interface, device_strings); |
| 67 | } |
| 68 | |
| 69 | void ProcessMappingButtons() |
| 70 | { |
| 71 | if (!m_input_detector) |
| 72 | return; |
| 73 | |
| 74 | const auto confirmation_time = |
| 75 | INPUT_DETECT_CONFIRMATION_TIME * (m_parent->IsWaitForAlternateMappingsEnabled() ? 1 : 0); |
| 76 | |
| 77 | m_input_detector->Update(INPUT_DETECT_INITIAL_TIME, confirmation_time, |
| 78 | INPUT_DETECT_MAXIMUM_TIME); |
| 79 | |
| 80 | if (!m_input_detector->IsComplete()) |
| 81 | return; |
| 82 | |
| 83 | auto* const button = m_clicked_mapping_buttons.front(); |
| 84 | |
| 85 | auto results = m_input_detector->TakeResults(); |
| 86 | if (!FinalizeMapping(&results)) |
| 87 | { |
| 88 | // No inputs detected. Cancel this and any other queued mappings. |
| 89 | CancelMapping(); |
| 90 | } |
| 91 | else if (m_parent->IsIterativeMappingEnabled() && m_clicked_mapping_buttons.empty()) |
| 92 | { |
| 93 | button->QueueNextButtonMapping(); |
| 94 | |
| 95 | if (m_clicked_mapping_buttons.empty()) |
| 96 | return; |
| 97 | |
| 98 | // Skip "Modifier" mappings when using analog inputs. |
| 99 | auto* next_button = m_clicked_mapping_buttons.front(); |
| 100 | if (next_button->GetControlType() == MappingButton::ControlType::ModifierInput && |
| 101 | std::ranges::any_of(results, &ciface::Core::InputDetector::Detection::IsAnalogPress)) |
| 102 | { |
| 103 | // Clear "Modifier" mapping and queue the next button. |
| 104 | SetButtonExpression(next_button, ""); |
| 105 | UnQueueInputDetection(next_button); |
| 106 | next_button->QueueNextButtonMapping(); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bool FinalizeMapping(ciface::Core::InputDetector::Results* detections_ptr) |
| 112 | { |
| 113 | auto& detections = *detections_ptr; |
| 114 | if (!ciface::MappingCommon::ContainsCompleteDetection(detections)) |
| 115 | return false; |
| 116 | |
| 117 | ciface::MappingCommon::RemoveSpuriousTriggerCombinations(&detections); |
| 118 | |
| 119 | const auto& default_device = m_parent->GetController()->GetDefaultDevice(); |
| 120 | auto& button = m_clicked_mapping_buttons.front(); |
| 121 | SetButtonExpression( |
| 122 | button, BuildExpression(detections, default_device, ciface::MappingCommon::Quote::On)); |
| 123 | |
| 124 | UnQueueInputDetection(button); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void SetButtonExpression(MappingButton* button, const std::string& expression) |
| 129 | { |
| 130 | auto* const control_reference = button->GetControlReference(); |
| 131 | control_reference->SetExpression(expression); |
| 132 | m_parent->Save(); |
| 133 | m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, |
| 134 | control_reference); |
| 135 | m_parent->GetController()->GetConfig()->GenerateControllerTextures(); |
| 136 | } |
| 137 | |
| 138 | void UpdateInputDetectionStartTimer() |
| 139 | { |
| 140 | m_input_detector.reset(); |
| 141 | |
| 142 | if (m_clicked_mapping_buttons.empty()) |
| 143 | m_input_detection_start_timer->stop(); |
| 144 | else |
| 145 | m_input_detection_start_timer->start(INPUT_DETECT_INITIAL_DELAY); |
| 146 | } |
| 147 | |
| 148 | bool UnQueueInputDetection(MappingButton* button) |
| 149 | { |
| 150 | button->ConfigChanged(); |
| 151 | if (!std::erase(m_clicked_mapping_buttons, button)) |
| 152 | return false; |
| 153 | UpdateInputDetectionStartTimer(); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | void QueueInputDetection(MappingButton* button) |
| 158 | { |
| 159 | // Just UnQueue if already queued. |
| 160 | if (UnQueueInputDetection(button)) |
| 161 | return; |
| 162 | |
| 163 | button->setText(QStringLiteral("[ ... ]")); |
| 164 | m_clicked_mapping_buttons.push_back(button); |
| 165 | |
| 166 | if (m_input_detector) |
| 167 | { |
| 168 | // Ignore the mouse-click that queued this new detection and finalize the current mapping. |
| 169 | auto results = m_input_detector->TakeResults(); |
| 170 | ciface::MappingCommon::RemoveDetectionsAfterTimePoint( |
| 171 | &results, Clock::now() - INPUT_DETECT_ENDING_IGNORE_TIME); |
| 172 | FinalizeMapping(&results); |
| 173 | } |
| 174 | UpdateInputDetectionStartTimer(); |
| 175 | } |
| 176 | |
| 177 | void CancelMapping() |
| 178 | { |
| 179 | // Signal buttons to take on their proper input expression text. |
| 180 | for (auto* button : m_clicked_mapping_buttons) |
| 181 | button->ConfigChanged(); |
| 182 | |
| 183 | m_clicked_mapping_buttons = {}; |
| 184 | UpdateInputDetectionStartTimer(); |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | std::deque<MappingButton*> m_clicked_mapping_buttons; |
| 189 | std::unique_ptr<ciface::Core::InputDetector> m_input_detector; |
| 190 | QTimer* m_input_detection_start_timer; |
| 191 | MappingWindow* const m_parent; |
| 192 | }; |
| 193 | |
| 194 | void CreateMappingProcessor(MappingWindow* window) |
| 195 | { |
| 196 | new MappingProcessor{window}; |
| 197 | } |
| 198 | |
| 199 | } // namespace MappingCommon |