From ea60ff2235a8fd69d8e57e6abd0dc09c4c098c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 30 Mar 2023 15:28:08 +0200 Subject: [PATCH] Add missing locking to control mapper --- Core/ControlMapper.cpp | 6 ++++++ Core/ControlMapper.h | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/Core/ControlMapper.cpp b/Core/ControlMapper.cpp index 1c6c4f925a..e39006dbf5 100644 --- a/Core/ControlMapper.cpp +++ b/Core/ControlMapper.cpp @@ -130,6 +130,7 @@ static int RotatePSPKeyCode(int x) { } } +// Can only be called from Key or Axis. bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping) { // Instead of taking an input key and finding what it outputs, we loop through the OUTPUTS and // see if the input that corresponds to it has a value. That way we can easily implement all sorts @@ -247,6 +248,8 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) { return true; } + std::lock_guard guard(mutex_); + InputMapping mapping(key.deviceId, key.keyCode); if (key.flags & KEY_DOWN) { @@ -255,6 +258,7 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) { curInput_[mapping] = 0.0f; } + // TODO: See if this can be simplified further somehow. bool mappingFound = KeyMap::InputMappingToPspButton(mapping, nullptr); DEBUG_LOG(SYSTEM, "Key: %d DeviceId: %d", key.keyCode, key.deviceId); if (!mappingFound || key.deviceId == DEVICE_ID_DEFAULT) { @@ -268,6 +272,7 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) { } void ControlMapper::Axis(const AxisInput &axis) { + std::lock_guard guard(mutex_); if (axis.value > 0) { InputMapping mapping(axis.deviceId, axis.axisId, 1); curInput_[mapping] = axis.value; @@ -305,6 +310,7 @@ void ControlMapper::Update() { } void ControlMapper::PSPKey(int deviceId, int pspKeyCode, int flags) { + std::lock_guard guard(mutex_); if (pspKeyCode >= VIRTKEY_FIRST) { int vk = pspKeyCode - VIRTKEY_FIRST; if (flags & KEY_DOWN) { diff --git a/Core/ControlMapper.h b/Core/ControlMapper.h index 87decc1a27..9d116d5517 100644 --- a/Core/ControlMapper.h +++ b/Core/ControlMapper.h @@ -5,6 +5,7 @@ #include #include +#include // Utilities for mapping input events to PSP inputs and virtual keys. // Main use is of course from EmuScreen.cpp, but also useful from control settings etc. @@ -13,6 +14,7 @@ public: void Update(); // Inputs to the table-based mapping + // These functions are free-threaded. bool Key(const KeyInput &key, bool *pauseTrigger); void Axis(const AxisInput &axis); @@ -50,6 +52,9 @@ private: bool autoRotatingAnalogCW_ = false; bool autoRotatingAnalogCCW_ = false; + // Protects basically all the state. + std::mutex mutex_; + std::map curInput_; // Callbacks