Debian Codegraph @

Source/Core/InputCommon/ControllerEmu/ControllerEmu.h

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <cmath>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <type_traits>
11 #include <vector>
12
13 #include "Common/BitUtils.h"
14 #include "Common/Common.h"
15 #include "Common/IniFile.h"
16 #include "Common/MathUtil.h"
17 #include "InputCommon/ControlReference/ExpressionParser.h"
18 #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
19 #include "InputCommon/ControllerInterface/CoreDevice.h"
20
21 class ControllerInterface;
22 class InputConfig;
23
24 constexpr const char* DIRECTION_UP = _trans("Up");
25 constexpr const char* DIRECTION_DOWN = _trans("Down");
26 constexpr const char* DIRECTION_LEFT = _trans("Left");
27 constexpr const char* DIRECTION_RIGHT = _trans("Right");
28
29 constexpr const char* named_directions[] = {DIRECTION_UP, DIRECTION_DOWN, DIRECTION_LEFT,
30 DIRECTION_RIGHT};
31
32 class ControlReference;
33
34 namespace ControllerEmu
35 {
36 class ControlGroup;
37
38 // Represents calibration data found on Wii Remotes + extensions with a zero and a max value.
39 // (e.g. accelerometer data)
40 // Bits of precision specified to handle common situation of differing precision in the actual data.
41 template <typename T, size_t Bits>
42 struct TwoPointCalibration
43 {
44 TwoPointCalibration() = default;
45 TwoPointCalibration(const T& zero_, const T& max_) : zero{zero_}, max{max_} {}
46
47 // Sanity check is that max and zero are not equal.
48 constexpr bool IsSane() const
49 {
50 if constexpr (std::is_arithmetic_v<T>)
51 {
52 return max != zero;
53 }
54 else
55 {
56 return std::ranges::equal(max.data, zero.data, std::ranges::not_equal_to{});
57 }
58 }
59
60 static constexpr size_t BITS_OF_PRECISION = Bits;
61
62 T zero;
63 T max;
64 };
65
66 // Represents calibration data with a min, zero, and max value. (e.g. joystick data)
67 template <typename T, size_t Bits>
68 struct ThreePointCalibration
69 {
70 ThreePointCalibration() = default;
71 ThreePointCalibration(const T& min_, const T& zero_, const T& max_)
72 : min{min_}, zero{zero_}, max{max_}
73 {
74 }
75
76 // Sanity check is that min and max are on opposite sides of the zero value.
77 constexpr bool IsSane() const
78 {
79 if constexpr (std::is_arithmetic_v<T>)
80 {
81 return MathUtil::Sign(zero - min) * MathUtil::Sign(zero - max) == -1;
82 }
83 else
84 {
85 for (size_t i = 0; i != std::size(zero.data); ++i)
86 {
87 if (MathUtil::Sign(zero.data[i] - min.data[i]) *
88 MathUtil::Sign(zero.data[i] - max.data[i]) !=
89 -1)
90 {
91 return false;
92 }
93 }
94 return true;
95 }
96 }
97
98 static constexpr size_t BITS_OF_PRECISION = Bits;
99
100 T min;
101 T zero;
102 T max;
103 };
104
105 // Represents a raw/uncalibrated N-dimensional value of input data. (e.g. Joystick X and Y)
106 // A normalized value can be calculated with a provided {Two,Three}PointCalibration.
107 // Values are adjusted with mismatched bits of precision.
108 // Underlying type may be an unsigned type or a a Common::TVecN<> of an unsigned type.
109 template <typename T, size_t Bits>
110 struct RawValue
111 {
112 constexpr RawValue() = default;
113 constexpr explicit RawValue(const T& value_) : value{value_} {}
114
115 static constexpr size_t BITS_OF_PRECISION = Bits;
116
117 T value;
118
119 constexpr bool operator==(const RawValue& other) const = default;
120
121 template <typename OtherT, size_t OtherBits>
122 auto GetNormalizedValue(const TwoPointCalibration<OtherT, OtherBits>& calibration) const
123 {
124 const auto value_expansion =
125 std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
126
127 const auto calibration_expansion =
128 std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
129
130 const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
131 const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
132
133 // Multiplication by 1.f to floatify either a scalar or a Vec.
134 return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
135 (calibration_max - calibration_zero);
136 }
137
138 template <typename OtherT, size_t OtherBits>
139 auto GetNormalizedValue(const ThreePointCalibration<OtherT, OtherBits>& calibration) const
140 {
141 const auto value_expansion =
142 std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
143
144 const auto calibration_expansion =
145 std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
146
147 const auto calibration_min = ExpandValue(calibration.min, calibration_expansion) * 1.f;
148 const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
149 const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
150
151 const auto use_max = calibration.zero < value;
152
153 // Multiplication by 1.f to floatify either a scalar or a Vec.
154 return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
155 (use_max * 1.f * (calibration_max - calibration_zero) +
156 !use_max * 1.f * (calibration_zero - calibration_min));
157 }
158
159 template <typename OtherT>
160 static OtherT ExpandValue(OtherT value, size_t bits)
161 {
162 if constexpr (std::is_arithmetic_v<OtherT>)
163 {
164 return Common::ExpandValue(value, bits);
165 }
166 else
167 {
168 for (size_t i = 0; i != std::size(value.data); ++i)
169 value.data[i] = Common::ExpandValue(value.data[i], bits);
170 return value;
171 }
172 }
173 };
174
175 // Maps a float from -1.0..+1.0 to an integer in the provided range.
176 template <typename T, typename F>
177 T MapFloat(F input_value, T zero_value, T neg_1_value = std::numeric_limits<T>::min(),
178 T pos_1_value = std::numeric_limits<T>::max())
179 {
180 static_assert(std::is_integral<T>(), "T is only sane for int types.");
181 static_assert(std::is_floating_point<F>(), "F is only sane for float types.");
182
183 static_assert(std::numeric_limits<long long>::min() <= std::numeric_limits<T>::min() &&
184 std::numeric_limits<long long>::max() >= std::numeric_limits<T>::max(),
185 "long long is not a superset of T. use of std::llround is not sane.");
186
187 // Here we round when converting from float to int.
188 // After applying our deadzone, resizing, and reshaping math
189 // we sometimes have a near-zero value which is slightly negative. (e.g. -0.0001)
190 // Casting would round down but rounding will yield our "zero_value".
191
192 if (input_value > 0)
193 return T(std::llround((pos_1_value - zero_value) * input_value + zero_value));
194 else
195 return T(std::llround((zero_value - neg_1_value) * input_value + zero_value));
196 }
197
198 // The inverse of the function above.
199 // Maps an integer in the provided range to a float in the range -1.0..1.0.
200 template <typename F, typename T>
201 F MapToFloat(T input_value, T zero_value, T neg_1_value = std::numeric_limits<T>::min(),
202 T pos_1_value = std::numeric_limits<T>::max())
203 {
204 static_assert(std::is_integral<T>(), "T is only sane for int types.");
205 static_assert(std::is_floating_point<F>(), "F is only sane for float types.");
206
207 if (input_value >= zero_value)
208 return F(input_value - zero_value) / F(pos_1_value - zero_value);
209 else
210 return -F(zero_value - input_value) / F(zero_value - neg_1_value);
211 }
212
213 class ControlGroupContainer
214 {
215 public:
216 virtual ~ControlGroupContainer();
217
218 virtual void LoadGroupsConfig(Common::IniFile::Section* sec, const std::string& base);
219 virtual void SaveGroupsConfig(Common::IniFile::Section* sec, const std::string& base);
220
221 virtual std::string GetName() const = 0;
222 virtual std::string GetDisplayName() const;
223
224 void UpdateGroupsReferences(ciface::ExpressionParser::ControlEnvironment& env);
225
226 void SetInputOverrideFunction(InputOverrideFunction override_func);
227 void ClearInputOverrideFunction();
228
229 std::vector<std::unique_ptr<ControlGroup>> groups;
230
231 protected:
232 InputOverrideFunction m_input_override_function;
233 };
234
235 class EmulatedController : public ControlGroupContainer
236 {
237 public:
238 ~EmulatedController() override;
239
240 virtual InputConfig* GetConfig() const = 0;
241
242 virtual void LoadDefaults(const ControllerInterface& ciface);
243
244 void LoadConfig(Common::IniFile::Section* sec);
245 void SaveConfig(Common::IniFile::Section* sec);
246
247 bool IsDefaultDeviceConnected() const;
248 const ciface::Core::DeviceQualifier& GetDefaultDevice() const;
249 void SetDefaultDevice(const std::string& device);
250 void SetDefaultDevice(ciface::Core::DeviceQualifier devq);
251
252 void UpdateReferences(const ControllerInterface& devi);
253 void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref);
254
255 // This returns a lock that should be held before calling State() on any control
256 // references and GetState(), by extension. This prevents a race condition
257 // which happens while handling a hotplug event because a control reference's State()
258 // could be called before we have finished updating the reference.
259 [[nodiscard]] static std::unique_lock<std::recursive_mutex> GetStateLock();
260 const ciface::ExpressionParser::ControlEnvironment::VariableContainer&
261 GetExpressionVariables() const;
262
263 // Resets the values while keeping the list.
264 void ResetExpressionVariables();
265
266 protected:
267 ciface::ExpressionParser::ControlEnvironment::VariableContainer m_expression_vars;
268
269 private:
270 ciface::Core::DeviceQualifier m_default_device;
271 bool m_default_device_is_connected{false};
272 };
273 } // namespace ControllerEmu