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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* 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/DriverPrimitive.h"
#include "input/joysticks/interfaces/IButtonMapCallback.h"
#include "input/joysticks/interfaces/IDriverHandler.h"
#include "input/keyboard/interfaces/IKeyboardDriverHandler.h"
#include "input/mouse/MouseTypes.h"
#include "input/mouse/interfaces/IMouseDriverHandler.h"
#include <chrono>
#include <map>
#include <memory>
#include <stdint.h>
class IKeymap;
namespace KODI
{
namespace JOYSTICK
{
class CButtonMapping;
class IButtonMap;
class IButtonMapper;
/*!
* \brief Detects and dispatches mapping events
*
* A mapping event usually occurs when a driver primitive is pressed or
* exceeds a certain threshold.
*
* Detection can be quite complicated due to driver bugs, so each type of
* driver primitive is given its own detector class inheriting from this one.
*/
class CPrimitiveDetector
{
protected:
CPrimitiveDetector(CButtonMapping* buttonMapping);
/*!
* \brief Dispatch a mapping event
*
* \return True if the primitive was mapped, false otherwise
*/
bool MapPrimitive(const CDriverPrimitive& primitive);
private:
CButtonMapping* const m_buttonMapping;
};
/*!
* \brief Detects when a button should be mapped
*/
class CButtonDetector : public CPrimitiveDetector
{
public:
CButtonDetector(CButtonMapping* buttonMapping, unsigned int buttonIndex);
/*!
* \brief Button state has been updated
*
* \param bPressed The new state
*
* \return True if this press was handled, false if it should fall through
* to the next driver handler
*/
bool OnMotion(bool bPressed);
private:
// Construction parameters
const unsigned int m_buttonIndex;
};
/*!
* \brief Detects when a D-pad direction should be mapped
*/
class CHatDetector : public CPrimitiveDetector
{
public:
CHatDetector(CButtonMapping* buttonMapping, unsigned int hatIndex);
/*!
* \brief Hat state has been updated
*
* \param state The new state
*
* \return True if state is a cardinal direction, false otherwise
*/
bool OnMotion(HAT_STATE state);
private:
// Construction parameters
const unsigned int m_hatIndex;
};
struct AxisConfiguration
{
bool bKnown = false;
int center = 0;
unsigned int range = 1;
bool bLateDiscovery = false;
};
/*!
* \brief Detects when an axis should be mapped
*/
class CAxisDetector : public CPrimitiveDetector
{
public:
CAxisDetector(CButtonMapping* buttonMapping,
unsigned int axisIndex,
const AxisConfiguration& config);
/*!
* \brief Axis state has been updated
*
* \param position The new state
*
* \return Always true - axis motion events are always absorbed while button mapping
*/
bool OnMotion(float position);
/*!
* \brief Called once per frame
*
* If an axis was activated, the button mapping command will be emitted
* here.
*/
void ProcessMotion();
/*!
* \brief Check if the axis was mapped and is still in motion
*
* \return True between when the axis is mapped and when it crosses zero
*/
bool IsMapping() const { return m_state == AXIS_STATE::MAPPED; }
/*!
* \brief Set the state such that this axis has generated a mapping event
*
* If an axis is mapped to the Select action, it may be pressed when button
* mapping begins. This function is used to indicate that the axis shouldn't
* be mapped until after it crosses zero again.
*/
void SetEmitted(const CDriverPrimitive& activePrimitive);
private:
enum class AXIS_STATE
{
/*!
* \brief Axis is inactive (position is less than threshold)
*/
INACTIVE,
/*!
* \brief Axis is activated (position has exceeded threshold)
*/
ACTIVATED,
/*!
* \brief Axis has generated a mapping event, but has not been centered yet
*/
MAPPED,
};
enum class AXIS_TYPE
{
/*!
* \brief Axis type is initially unknown
*/
UNKNOWN,
/*!
* \brief Axis is centered about 0
*
* - If the axis is an analog stick, it can travel to -1 or +1.
* - If the axis is a pressure-sensitive button or a normal trigger,
* it can travel to +1.
* - If the axis is a DirectInput trigger, then it is possible that two
* triggers can be on the same axis in opposite directions.
* - Normally, D-pads appear as a hat or four buttons. However, some
* D-pads are reported as two axes that can have the discrete values
* -1, 0 or 1. This is called a "discrete D-pad".
*/
NORMAL,
/*!
* \brief Axis is centered about -1 or 1
*
* - On OSX, with the cocoa driver, triggers are centered about -1 and
* travel to +1. In this case, the range is 2 and the direction is
* positive.
* - The author of SDL has observed triggers centered at +1 and travel
* to 0. In this case, the range is 1 and the direction is negative.
*/
OFFSET,
};
void DetectType(float position);
// Construction parameters
const unsigned int m_axisIndex;
AxisConfiguration m_config; // mutable
// State variables
AXIS_STATE m_state;
CDriverPrimitive m_activatedPrimitive;
AXIS_TYPE m_type;
bool m_initialPositionKnown; // set to true on first motion
float m_initialPosition; // set to position of first motion
bool m_initialPositionChanged; // set to true when position differs from the initial position
std::chrono::time_point<std::chrono::steady_clock>
m_activationTimeMs; // only used to delay anomalous trigger mapping to detect full range
};
/*!
* \brief Detects when a keyboard key should be mapped
*/
class CKeyDetector : public CPrimitiveDetector
{
public:
CKeyDetector(CButtonMapping* buttonMapping, XBMCKey keycode);
/*!
* \brief Key state has been updated
*
* \param bPressed The new state
*
* \return True if this press was handled, false if it should fall through
* to the next driver handler
*/
bool OnMotion(bool bPressed);
private:
// Construction parameters
const XBMCKey m_keycode;
};
/*!
* \brief Detects when a mouse button should be mapped
*/
class CMouseButtonDetector : public CPrimitiveDetector
{
public:
CMouseButtonDetector(CButtonMapping* buttonMapping, MOUSE::BUTTON_ID buttonIndex);
/*!
* \brief Button state has been updated
*
* \param bPressed The new state
*
* \return True if this press was handled, false if it should fall through
* to the next driver handler
*/
bool OnMotion(bool bPressed);
private:
// Construction parameters
const MOUSE::BUTTON_ID m_buttonIndex;
};
/*!
* \brief Detects when a mouse button should be mapped
*/
class CPointerDetector : public CPrimitiveDetector
{
public:
CPointerDetector(CButtonMapping* buttonMapping);
/*!
* \brief Pointer position has been updated
*
* \param x The new x coordinate
* \param y The new y coordinate
*
* \return Always true - pointer motion events are always absorbed while
* button mapping
*/
bool OnMotion(int x, int y);
private:
// Utility function
static INPUT::INTERCARDINAL_DIRECTION GetPointerDirection(int x, int y);
static const unsigned int MIN_FRAME_COUNT = 10;
// State variables
bool m_bStarted = false;
int m_startX = 0;
int m_startY = 0;
unsigned int m_frameCount = 0;
};
/*!
* \ingroup joystick
* \brief Generic implementation of a class that provides button mapping by
* translating driver events to button mapping commands
*
* Button mapping commands are invoked instantly for buttons and hats.
*
* Button mapping commands are deferred for a short while after an axis is
* activated, and only one button mapping command will be invoked per
* activation.
*/
class CButtonMapping : public IDriverHandler,
public KEYBOARD::IKeyboardDriverHandler,
public MOUSE::IMouseDriverHandler,
public IButtonMapCallback
{
public:
/*!
* \brief Constructor for CButtonMapping
*
* \param buttonMapper Carries out button-mapping commands using <buttonMap>
* \param buttonMap The button map given to <buttonMapper> on each command
*/
CButtonMapping(IButtonMapper* buttonMapper, IButtonMap* buttonMap, IKeymap* keymap);
~CButtonMapping() override = default;
// implementation of IDriverHandler
bool OnButtonMotion(unsigned int buttonIndex, bool bPressed) override;
bool OnHatMotion(unsigned int hatIndex, HAT_STATE state) override;
bool OnAxisMotion(unsigned int axisIndex,
float position,
int center,
unsigned int range) override;
void OnInputFrame() override;
// implementation of IKeyboardDriverHandler
bool OnKeyPress(const CKey& key) override;
void OnKeyRelease(const CKey& key) override {}
// implementation of IMouseDriverHandler
bool OnPosition(int x, int y) override;
bool OnButtonPress(MOUSE::BUTTON_ID button) override;
void OnButtonRelease(MOUSE::BUTTON_ID button) override;
// implementation of IButtonMapCallback
void SaveButtonMap() override;
void ResetIgnoredPrimitives() override;
void RevertButtonMap() override;
/*!
* \brief Process the primitive mapping command
*
* First, this function checks if the input should be dropped. This can
* happen if the input is ignored or the cooldown period is active. If the
* input is dropped, this returns true with no effect, effectively absorbing
* the input. Otherwise, the mapping command is sent to m_buttonMapper.
*
* \param primitive The primitive being mapped
* \return True if the mapping command was handled, false otherwise
*/
bool MapPrimitive(const CDriverPrimitive& primitive);
private:
bool IsMapping() const;
void OnLateDiscovery(unsigned int axisIndex);
CButtonDetector& GetButton(unsigned int buttonIndex);
CHatDetector& GetHat(unsigned int hatIndex);
CAxisDetector& GetAxis(unsigned int axisIndex,
float position,
const AxisConfiguration& initialConfig = AxisConfiguration());
CKeyDetector& GetKey(XBMCKey keycode);
CMouseButtonDetector& GetMouseButton(MOUSE::BUTTON_ID buttonIndex);
CPointerDetector& GetPointer();
// Construction parameters
IButtonMapper* const m_buttonMapper;
IButtonMap* const m_buttonMap;
IKeymap* const m_keymap;
std::map<unsigned int, CButtonDetector> m_buttons;
std::map<unsigned int, CHatDetector> m_hats;
std::map<unsigned int, CAxisDetector> m_axes;
std::map<XBMCKey, CKeyDetector> m_keys;
std::map<MOUSE::BUTTON_ID, CMouseButtonDetector> m_mouseButtons;
std::unique_ptr<CPointerDetector> m_pointer;
std::chrono::time_point<std::chrono::steady_clock> m_lastAction;
uint64_t m_frameCount;
};
} // namespace JOYSTICK
} // namespace KODI
|