kodi
FeatureHandling.h
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 
35 {
36 public:
37  CJoystickFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
38  virtual ~CJoystickFeature() = default;
39 
48  virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) = 0;
49 
63  virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) = 0;
64 
71  virtual void ProcessMotions(void) = 0;
72 
81  bool AcceptsInput(bool bActivation);
82 
83 protected:
87  void ResetMotion();
88 
92  void StartMotion();
93 
97  bool InMotion() const;
98 
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 
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 
160 {
161 public:
162  CFeatureAxis(void) { Reset(); }
163 
167  void SetPositiveDistance(float distance) { m_positiveDistance = distance; }
168 
172  void SetNegativeDistance(float distance) { m_negativeDistance = distance; }
173 
199  float GetPosition(void) const { return m_positiveDistance - m_negativeDistance; }
200 
204  void Reset(void) { m_positiveDistance = m_negativeDistance = 0.0f; }
205 
206 protected:
207  float m_positiveDistance;
208  float m_negativeDistance;
209 };
210 
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 
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 
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 
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 
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
virtual bool OnAnalogMotion(const CDriverPrimitive &source, float magnitude)=0
An analog motion has occurred.
Definition: FeatureHandling.h:259
Definition: IInputHandler.h:16
float GetPosition(void) const
Get the final value of this axis.
Definition: FeatureHandling.h:199
Button map interface to translate between the driver&#39;s raw button/hat/axis elements and physical joys...
Definition: IButtonMap.h:29
virtual void ProcessMotions(void)=0
Process the motions that have occurred since the last invocation.
void SetNegativeDistance(float distance)
Set value of negative axis.
Definition: FeatureHandling.h:172
Definition: FeatureHandling.h:233
Definition: FeatureHandling.h:246
void Reset(void)
Reset both positive and negative values to zero.
Definition: FeatureHandling.h:204
void StartMotion()
Start the motion timer.
Definition: FeatureHandling.cpp:58
bool AcceptsInput(bool bActivation)
Check if the input handler is accepting input.
Definition: FeatureHandling.cpp:40
Definition: AudioDecoder.h:18
Interface for handling input events for game controllers.
Definition: IInputHandler.h:26
void SetPositiveDistance(float distance)
Set value of positive axis.
Definition: FeatureHandling.h:167
unsigned int MotionTimeMs() const
Get the time for which the feature has been in motion.
Definition: FeatureHandling.cpp:68
Basic driver element associated with input events.
Definition: DriverPrimitive.h:70
void ResetMotion()
Reset motion timer.
Definition: FeatureHandling.cpp:53
Definition: FeatureHandling.h:214
Definition: FeatureHandling.h:116
Axis of a feature (analog stick, accelerometer, etc)
Definition: FeatureHandling.h:159
Definition: FeatureHandling.h:281
Base class for joystick features.
Definition: FeatureHandling.h:34
bool InMotion() const
Check if the feature is in motion.
Definition: FeatureHandling.cpp:63
virtual bool OnDigitalMotion(const CDriverPrimitive &source, bool bPressed)=0
A digital motion has occurred.