Debian Codegraph @

xbmc/input/joysticks/generic/FeatureHandling.h

raw
1 /*
2 * Copyright (C) 2014-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #pragma once
10
11 #include "input/joysticks/JoystickTypes.h"
12
13 #include <chrono>
14 #include <memory>
15
16 namespace KODI
17 {
18 namespace JOYSTICK
19 {
20 class CDriverPrimitive;
21 class IInputHandler;
22 class IButtonMap;
23
24 class CJoystickFeature;
25 using FeaturePtr = std::shared_ptr<CJoystickFeature>;
26
27 /*!
28 * \ingroup joystick
29 *
30 * \brief Base class for joystick features
31 *
32 * See list of feature types in JoystickTypes.h.
33 */
34 class CJoystickFeature
35 {
36 public:
37 CJoystickFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
38 virtual ~CJoystickFeature() = default;
39
40 /*!
41 * \brief A digital motion has occurred
42 *
43 * \param source The source of the motion. Must be digital (button or hat)
44 * \param bPressed True for press motion, false for release motion
45 *
46 * \return true if the motion was handled, false otherwise
47 */
48 virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) = 0;
49
50 /*!
51 * \brief An analog motion has occurred
52 *
53 * \param source The source of the motion. Must be a semiaxis
54 * \param magnitude The magnitude of the press or motion in the interval [0.0, 1.0]
55 *
56 * For semiaxes, the magnitude is the force or travel distance in the
57 * direction of the semiaxis. If the value is in the opposite direction,
58 * the magnitude is 0.0.
59 *
60 * For example, if the analog stick goes left, the negative semiaxis will
61 * have a value of 1.0 and the positive semiaxis will have a value of 0.0.
62 */
63 virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) = 0;
64
65 /*!
66 * \brief Process the motions that have occurred since the last invocation
67 *
68 * This allows features with motion on multiple driver primitives to call
69 * their handler once all driver primitives are accounted for.
70 */
71 virtual void ProcessMotions(void) = 0;
72
73 /*!
74 * \brief Check if the input handler is accepting input
75 *
76 * \param bActivation True if the motion is activating (true or positive),
77 * false if the motion is deactivating (false or zero)
78 *
79 * \return True if input should be sent to the input handler, false otherwise
80 */
81 bool AcceptsInput(bool bActivation);
82
83 protected:
84 /*!
85 * \brief Reset motion timer
86 */
87 void ResetMotion();
88
89 /*!
90 * \brief Start the motion timer
91 */
92 void StartMotion();
93
94 /*!
95 * \brief Check if the feature is in motion
96 */
97 bool InMotion() const;
98
99 /*!
100 * \brief Get the time for which the feature has been in motion
101 */
102 unsigned int MotionTimeMs() const;
103
104 const FeatureName m_name;
105 IInputHandler* const m_handler;
106 IButtonMap* const m_buttonMap;
107 const bool m_bEnabled;
108
109 private:
110 std::chrono::time_point<std::chrono::steady_clock> m_motionStartTimeMs;
111 };
112
113 /*!
114 * \ingroup joystick
115 */
116 class CScalarFeature : public CJoystickFeature
117 {
118 public:
119 CScalarFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
120 ~CScalarFeature() override = default;
121
122 // implementation of CJoystickFeature
123 bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
124 bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
125 void ProcessMotions() override;
126
127 private:
128 bool OnDigitalMotion(bool bPressed);
129 bool OnAnalogMotion(float magnitude);
130
131 void ProcessDigitalMotion();
132 void ProcessAnalogMotion();
133
134 // State variables
135 INPUT_TYPE m_inputType = INPUT_TYPE::UNKNOWN;
136 bool m_bDigitalState = false;
137 bool m_bInitialPressHandled = false;
138
139 // Analog state variables
140 float m_analogState = 0.0f; // The current magnitude
141 float m_bActivated = false; // Set to true when first activated (magnitude > 0.0)
142 bool m_bDiscrete = true; // Set to false when a non-discrete axis is detected
143 };
144
145 /*!
146 * \ingroup joystick
147 *
148 * \brief Axis of a feature (analog stick, accelerometer, etc)
149 *
150 * Axes are composed of two driver primitives, one for the positive semiaxis
151 * and one for the negative semiaxis.
152 *
153 * This effectively means that an axis is two-dimensional, with each dimension
154 * either:
155 *
156 * - a digital value (0.0 or 1.0)
157 * - an analog value (continuous in the interval [0.0, 1.0])
158 */
159 class CFeatureAxis
160 {
161 public:
162 CFeatureAxis(void) { Reset(); }
163
164 /*!
165 * \brief Set value of positive axis
166 */
167 void SetPositiveDistance(float distance) { m_positiveDistance = distance; }
168
169 /*!
170 * \brief Set value of negative axis
171 */
172 void SetNegativeDistance(float distance) { m_negativeDistance = distance; }
173
174 /*!
175 * \brief Get the final value of this axis.
176 *
177 * This axis is two-dimensional, so we need to compress these into a single
178 * dimension. This is done by subtracting the negative from the positive.
179 * Some examples:
180 *
181 * Positive axis: 1.0 (User presses right or analog stick moves right)
182 * Negative axis: 0.0
183 * -------------------
184 * Pos - Neg: 1.0 (Emulated analog stick moves right)
185 *
186 *
187 * Positive axis: 0.0
188 * Negative axis: 1.0 (User presses left or analog stick moves left)
189 * -------------------
190 * Pos - Neg: -1.0 (Emulated analog stick moves left)
191 *
192 *
193 * Positive axis: 1.0 (User presses both buttons)
194 * Negative axis: 1.0
195 * -------------------
196 * Pos - Neg: 0.0 (Emulated analog stick is centered)
197 *
198 */
199 float GetPosition(void) const { return m_positiveDistance - m_negativeDistance; }
200
201 /*!
202 * \brief Reset both positive and negative values to zero
203 */
204 void Reset(void) { m_positiveDistance = m_negativeDistance = 0.0f; }
205
206 protected:
207 float m_positiveDistance;
208 float m_negativeDistance;
209 };
210
211 /*!
212 * \ingroup joystick
213 */
214 class CAxisFeature : public CJoystickFeature
215 {
216 public:
217 CAxisFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
218 ~CAxisFeature() override = default;
219
220 // partial implementation of CJoystickFeature
221 bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
222 void ProcessMotions() override;
223
224 protected:
225 CFeatureAxis m_axis;
226
227 float m_state = 0.0f;
228 };
229
230 /*!
231 * \ingroup joystick
232 */
233 class CWheel : public CAxisFeature
234 {
235 public:
236 CWheel(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
237 ~CWheel() override = default;
238
239 // partial implementation of CJoystickFeature
240 bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
241 };
242
243 /*!
244 * \ingroup joystick
245 */
246 class CThrottle : public CAxisFeature
247 {
248 public:
249 CThrottle(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
250 ~CThrottle() override = default;
251
252 // partial implementation of CJoystickFeature
253 bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
254 };
255
256 /*!
257 * \ingroup joystick
258 */
259 class CAnalogStick : public CJoystickFeature
260 {
261 public:
262 CAnalogStick(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
263 ~CAnalogStick() override = default;
264
265 // implementation of CJoystickFeature
266 bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
267 bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
268 void ProcessMotions() override;
269
270 protected:
271 CFeatureAxis m_vertAxis;
272 CFeatureAxis m_horizAxis;
273
274 float m_vertState = 0.0f;
275 float m_horizState = 0.0f;
276 };
277
278 /*!
279 * \ingroup joystick
280 */
281 class CAccelerometer : public CJoystickFeature
282 {
283 public:
284 CAccelerometer(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
285 ~CAccelerometer() override = default;
286
287 // implementation of CJoystickFeature
288 bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
289 bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
290 void ProcessMotions() override;
291
292 protected:
293 CFeatureAxis m_xAxis;
294 CFeatureAxis m_yAxis;
295 CFeatureAxis m_zAxis;
296
297 float m_xAxisState = 0.0f;
298 float m_yAxisState = 0.0f;
299 float m_zAxisState = 0.0f;
300 };
301 } // namespace JOYSTICK
302 } // namespace KODI