summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/generic/FeatureHandling.h
blob: 1a42d5c8d3327cde257c43c5c80443ecfaf18389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *  Copyright (C) 2014-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "input/joysticks/JoystickTypes.h"

#include <chrono>
#include <memory>

namespace KODI
{
namespace JOYSTICK
{
class CDriverPrimitive;
class IInputHandler;
class IButtonMap;

class CJoystickFeature;
using FeaturePtr = std::shared_ptr<CJoystickFeature>;

/*!
 * \ingroup joystick
 * \brief Base class for joystick features
 *
 * See list of feature types in JoystickTypes.h.
 */
class CJoystickFeature
{
public:
  CJoystickFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  virtual ~CJoystickFeature() = default;

  /*!
   * \brief A digital motion has occurred
   *
   * \param source The source of the motion. Must be digital (button or hat)
   * \param bPressed True for press motion, false for release motion
   *
   * \return true if the motion was handled, false otherwise
   */
  virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) = 0;

  /*!
   * \brief An analog motion has occurred
   *
   * \param source The source of the motion. Must be a semiaxis
   * \param magnitude The magnitude of the press or motion in the interval [0.0, 1.0]
   *
   * For semiaxes, the magnitude is the force or travel distance in the
   * direction of the semiaxis. If the value is in the opposite direction,
   * the magnitude is 0.0.
   *
   * For example, if the analog stick goes left, the negative semiaxis will
   * have a value of 1.0 and the positive semiaxis will have a value of 0.0.
   */
  virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) = 0;

  /*!
   * \brief Process the motions that have occurred since the last invocation
   *
   * This allows features with motion on multiple driver primitives to call
   * their handler once all driver primitives are accounted for.
   */
  virtual void ProcessMotions(void) = 0;

  /*!
   * \brief Check if the input handler is accepting input
   *
   * \param bActivation True if the motion is activating (true or positive),
   *                    false if the motion is deactivating (false or zero)
   *
   * \return True if input should be sent to the input handler, false otherwise
   */
  bool AcceptsInput(bool bActivation);

protected:
  /*!
   * \brief Reset motion timer
   */
  void ResetMotion();

  /*!
   * \brief Start the motion timer
   */
  void StartMotion();

  /*!
   * \brief Check if the feature is in motion
   */
  bool InMotion() const;

  /*!
   * \brief Get the time for which the feature has been in motion
   */
  unsigned int MotionTimeMs() const;

  const FeatureName m_name;
  IInputHandler* const m_handler;
  IButtonMap* const m_buttonMap;
  const bool m_bEnabled;

private:
  std::chrono::time_point<std::chrono::steady_clock> m_motionStartTimeMs;
};

class CScalarFeature : public CJoystickFeature
{
public:
  CScalarFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CScalarFeature() override = default;

  // implementation of CJoystickFeature
  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
  void ProcessMotions() override;

private:
  bool OnDigitalMotion(bool bPressed);
  bool OnAnalogMotion(float magnitude);

  void ProcessDigitalMotion();
  void ProcessAnalogMotion();

  // State variables
  INPUT_TYPE m_inputType = INPUT_TYPE::UNKNOWN;
  bool m_bDigitalState;
  bool m_bInitialPressHandled = false;

  // Analog state variables
  float m_analogState; // The current magnitude
  float m_bActivated; // Set to true when first activated (magnitude > 0.0)
  bool m_bDiscrete; // Set to false when a non-discrete axis is detected
};

/*!
 * \ingroup joystick
 * \brief Axis of a feature (analog stick, accelerometer, etc)
 *
 * Axes are composed of two driver primitives, one for the positive semiaxis
 * and one for the negative semiaxis.
 *
 * This effectively means that an axis is two-dimensional, with each dimension
 * either:
 *
 *    - a digital value (0.0 or 1.0)
 *    - an analog value (continuous in the interval [0.0, 1.0])
 */
class CFeatureAxis
{
public:
  CFeatureAxis(void) { Reset(); }

  /*!
   * \brief Set value of positive axis
   */
  void SetPositiveDistance(float distance) { m_positiveDistance = distance; }

  /*!
   * \brief Set value of negative axis
   */
  void SetNegativeDistance(float distance) { m_negativeDistance = distance; }

  /*!
   * \brief Get the final value of this axis.
   *
   * This axis is two-dimensional, so we need to compress these into a single
   * dimension. This is done by subtracting the negative from the positive.
   * Some examples:
   *
   *      Positive axis:  1.0 (User presses right or analog stick moves right)
   *      Negative axis:  0.0
   *      -------------------
   *      Pos - Neg:      1.0 (Emulated analog stick moves right)
   *
   *
   *      Positive axis:  0.0
   *      Negative axis:  1.0 (User presses left or analog stick moves left)
   *      -------------------
   *      Pos - Neg:     -1.0 (Emulated analog stick moves left)
   *
   *
   *      Positive axis:  1.0 (User presses both buttons)
   *      Negative axis:  1.0
   *      -------------------
   *      Pos - Neg:      0.0 (Emulated analog stick is centered)
   *
   */
  float GetPosition(void) const { return m_positiveDistance - m_negativeDistance; }

  /*!
   * \brief Reset both positive and negative values to zero
   */
  void Reset(void) { m_positiveDistance = m_negativeDistance = 0.0f; }

protected:
  float m_positiveDistance;
  float m_negativeDistance;
};

class CAxisFeature : public CJoystickFeature
{
public:
  CAxisFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CAxisFeature() override = default;

  // partial implementation of CJoystickFeature
  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
  void ProcessMotions() override;

protected:
  CFeatureAxis m_axis;

  float m_state;
};

class CWheel : public CAxisFeature
{
public:
  CWheel(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CWheel() override = default;

  // partial implementation of CJoystickFeature
  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
};

class CThrottle : public CAxisFeature
{
public:
  CThrottle(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CThrottle() override = default;

  // partial implementation of CJoystickFeature
  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
};

class CAnalogStick : public CJoystickFeature
{
public:
  CAnalogStick(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CAnalogStick() override = default;

  // implementation of CJoystickFeature
  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
  void ProcessMotions() override;

protected:
  CFeatureAxis m_vertAxis;
  CFeatureAxis m_horizAxis;

  float m_vertState;
  float m_horizState;
};

class CAccelerometer : public CJoystickFeature
{
public:
  CAccelerometer(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
  ~CAccelerometer() override = default;

  // implementation of CJoystickFeature
  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
  void ProcessMotions() override;

protected:
  CFeatureAxis m_xAxis;
  CFeatureAxis m_yAxis;
  CFeatureAxis m_zAxis;

  float m_xAxisState;
  float m_yAxisState;
  float m_zAxisState;
};
} // namespace JOYSTICK
} // namespace KODI