| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <mutex> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include "Common/IniFile.h" |
| 12 | |
| 13 | #include "InputCommon/ControlReference/ControlReference.h" |
| 14 | #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" |
| 15 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 16 | |
| 17 | namespace ControllerEmu |
| 18 | { |
| 19 | // This should theoretically be per EmulatedController instance, |
| 20 | // though no EmulatedController usually run in parallel, so it makes little difference |
| 21 | static std::recursive_mutex s_get_state_mutex; |
| 22 | |
| 23 | std::string ControlGroupContainer::GetDisplayName() const |
| 24 | { |
| 25 | return GetName(); |
| 26 | } |
| 27 | |
| 28 | EmulatedController::~EmulatedController() = default; |
| 29 | |
| 30 | // This should be called before calling GetState() or State() on a control reference |
| 31 | // to prevent a race condition. |
| 32 | // This is a recursive mutex because UpdateReferences is recursive. |
| 33 | std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock() |
| 34 | { |
| 35 | std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex); |
| 36 | return lock; |
| 37 | } |
| 38 | |
| 39 | void EmulatedController::UpdateReferences(const ControllerInterface& devi) |
| 40 | { |
| 41 | std::scoped_lock lk(s_get_state_mutex, devi.GetDevicesMutex()); |
| 42 | |
| 43 | m_default_device_is_connected = devi.HasConnectedDevice(m_default_device); |
| 44 | |
| 45 | ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars); |
| 46 | |
| 47 | UpdateGroupsReferences(env); |
| 48 | |
| 49 | env.CleanUnusedVariables(); |
| 50 | } |
| 51 | |
| 52 | void ControlGroupContainer::UpdateGroupsReferences( |
| 53 | ciface::ExpressionParser::ControlEnvironment& env) |
| 54 | { |
| 55 | const auto lock = EmulatedController::GetStateLock(); |
| 56 | |
| 57 | for (auto& group : groups) |
| 58 | group->UpdateReferences(env); |
| 59 | } |
| 60 | |
| 61 | void EmulatedController::UpdateSingleControlReference(const ControllerInterface& devi, |
| 62 | ControlReference* ref) |
| 63 | { |
| 64 | ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars); |
| 65 | |
| 66 | const auto lock = GetStateLock(); |
| 67 | ref->UpdateReference(env); |
| 68 | |
| 69 | env.CleanUnusedVariables(); |
| 70 | } |
| 71 | |
| 72 | const ciface::ExpressionParser::ControlEnvironment::VariableContainer& |
| 73 | EmulatedController::GetExpressionVariables() const |
| 74 | { |
| 75 | return m_expression_vars; |
| 76 | } |
| 77 | |
| 78 | void EmulatedController::ResetExpressionVariables() |
| 79 | { |
| 80 | for (auto& var : m_expression_vars) |
| 81 | { |
| 82 | if (var.second) |
| 83 | { |
| 84 | *var.second = 0; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool EmulatedController::IsDefaultDeviceConnected() const |
| 90 | { |
| 91 | return m_default_device_is_connected; |
| 92 | } |
| 93 | |
| 94 | const ciface::Core::DeviceQualifier& EmulatedController::GetDefaultDevice() const |
| 95 | { |
| 96 | return m_default_device; |
| 97 | } |
| 98 | |
| 99 | void EmulatedController::SetDefaultDevice(const std::string& device) |
| 100 | { |
| 101 | ciface::Core::DeviceQualifier devq; |
| 102 | devq.FromString(device); |
| 103 | SetDefaultDevice(std::move(devq)); |
| 104 | } |
| 105 | |
| 106 | void EmulatedController::SetDefaultDevice(ciface::Core::DeviceQualifier devq) |
| 107 | { |
| 108 | m_default_device = std::move(devq); |
| 109 | } |
| 110 | |
| 111 | ControlGroupContainer::~ControlGroupContainer() = default; |
| 112 | |
| 113 | void EmulatedController::LoadConfig(Common::IniFile::Section* sec) |
| 114 | { |
| 115 | const auto lock = EmulatedController::GetStateLock(); |
| 116 | |
| 117 | std::string defdev; |
| 118 | if (sec->Get("Device", &defdev, "") && !defdev.empty()) |
| 119 | SetDefaultDevice(defdev); |
| 120 | |
| 121 | LoadGroupsConfig(sec, ""); |
| 122 | } |
| 123 | |
| 124 | void ControlGroupContainer::LoadGroupsConfig(Common::IniFile::Section* sec, const std::string& base) |
| 125 | { |
| 126 | for (auto& cg : groups) |
| 127 | cg->LoadConfig(sec, base); |
| 128 | } |
| 129 | |
| 130 | void EmulatedController::SaveConfig(Common::IniFile::Section* sec) |
| 131 | { |
| 132 | const auto lock = EmulatedController::GetStateLock(); |
| 133 | |
| 134 | sec->Set("Device", GetDefaultDevice().ToString(), ""); |
| 135 | |
| 136 | SaveGroupsConfig(sec, ""); |
| 137 | } |
| 138 | |
| 139 | void ControlGroupContainer::SaveGroupsConfig(Common::IniFile::Section* sec, const std::string& base) |
| 140 | { |
| 141 | for (auto& cg : groups) |
| 142 | cg->SaveConfig(sec, base); |
| 143 | } |
| 144 | |
| 145 | void EmulatedController::LoadDefaults(const ControllerInterface& ciface) |
| 146 | { |
| 147 | const auto lock = GetStateLock(); |
| 148 | // load an empty inifile section, clears everything |
| 149 | Common::IniFile::Section sec; |
| 150 | LoadConfig(&sec); |
| 151 | |
| 152 | const std::string& default_device_string = ciface.GetDefaultDeviceString(); |
| 153 | if (!default_device_string.empty()) |
| 154 | { |
| 155 | SetDefaultDevice(default_device_string); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void ControlGroupContainer::SetInputOverrideFunction(InputOverrideFunction override_func) |
| 160 | { |
| 161 | m_input_override_function = std::move(override_func); |
| 162 | } |
| 163 | |
| 164 | void ControlGroupContainer::ClearInputOverrideFunction() |
| 165 | { |
| 166 | m_input_override_function = {}; |
| 167 | } |
| 168 | |
| 169 | } // namespace ControllerEmu |