Debian Codegraph @

xbmc/input/joysticks/generic/FeatureHandling.cpp

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 #include "FeatureHandling.h"
10
11 #include "ServiceBroker.h"
12 #include "games/controllers/Controller.h"
13 #include "games/controllers/ControllerManager.h"
14 #include "input/joysticks/DriverPrimitive.h"
15 #include "input/joysticks/interfaces/IButtonMap.h"
16 #include "input/joysticks/interfaces/IInputHandler.h"
17 #include "utils/log.h"
18
19 #include <vector>
20
21 using namespace KODI;
22 using namespace JOYSTICK;
23
24 #define ANALOG_DIGITAL_THRESHOLD 0.5f
25 #define DISCRETE_ANALOG_RAMPUP_TIME_MS 1500
26 #define DISCRETE_ANALOG_START_VALUE 0.3f
27
28 // --- CJoystickFeature --------------------------------------------------------
29
30 CJoystickFeature::CJoystickFeature(const FeatureName& name,
31 IInputHandler* handler,
32 IButtonMap* buttonMap)
33 : m_name(name),
34 m_handler(handler),
35 m_buttonMap(buttonMap),
36 m_bEnabled(m_handler->HasFeature(name))
37 {
38 }
39
40 bool CJoystickFeature::AcceptsInput(bool bActivation)
41 {
42 bool bAcceptsInput = false;
43
44 if (m_bEnabled)
45 {
46 if (m_handler->AcceptsInput(m_name))
47 bAcceptsInput = true;
48 }
49
50 return bAcceptsInput;
51 }
52
53 void CJoystickFeature::ResetMotion()
54 {
55 m_motionStartTimeMs = {};
56 }
57
58 void CJoystickFeature::StartMotion()
59 {
60 m_motionStartTimeMs = std::chrono::steady_clock::now();
61 }
62
63 bool CJoystickFeature::InMotion() const
64 {
65 return m_motionStartTimeMs.time_since_epoch().count() > 0;
66 }
67
68 unsigned int CJoystickFeature::MotionTimeMs() const
69 {
70 if (!InMotion())
71 return 0;
72
73 auto now = std::chrono::steady_clock::now();
74 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_motionStartTimeMs);
75
76 return duration.count();
77 }
78
79 // --- CScalarFeature ----------------------------------------------------------
80
81 CScalarFeature::CScalarFeature(const FeatureName& name,
82 IInputHandler* handler,
83 IButtonMap* buttonMap)
84 : CJoystickFeature(name, handler, buttonMap)
85 {
86 GAME::ControllerPtr controller =
87 CServiceBroker::GetGameControllerManager().GetController(handler->ControllerID());
88 if (controller)
89 m_inputType = controller->GetInputType(name);
90 }
91
92 bool CScalarFeature::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
93 {
94 // Feature must accept input to be considered handled
95 bool bHandled = AcceptsInput(bPressed);
96
97 if (m_inputType == INPUT_TYPE::DIGITAL)
98 bHandled &= OnDigitalMotion(bPressed);
99 else if (m_inputType == INPUT_TYPE::ANALOG)
100 bHandled &= OnAnalogMotion(bPressed ? 1.0f : 0.0f);
101
102 return bHandled;
103 }
104
105 bool CScalarFeature::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
106 {
107 // Update activated status
108 if (magnitude > 0.0f)
109 m_bActivated = true;
110
111 // Update discrete status
112 if (magnitude != 0.0f && magnitude != 1.0f)
113 m_bDiscrete = false;
114
115 // Feature must accept input to be considered handled
116 bool bHandled = AcceptsInput(magnitude > 0.0f);
117
118 if (m_inputType == INPUT_TYPE::DIGITAL)
119 bHandled &= OnDigitalMotion(magnitude >= ANALOG_DIGITAL_THRESHOLD);
120 else if (m_inputType == INPUT_TYPE::ANALOG)
121 bHandled &= OnAnalogMotion(magnitude);
122
123 return bHandled;
124 }
125
126 void CScalarFeature::ProcessMotions(void)
127 {
128 if (m_inputType == INPUT_TYPE::DIGITAL && m_bDigitalState)
129 ProcessDigitalMotion();
130 else if (m_inputType == INPUT_TYPE::ANALOG)
131 ProcessAnalogMotion();
132 }
133
134 bool CScalarFeature::OnDigitalMotion(bool bPressed)
135 {
136 bool bHandled = false;
137
138 if (m_bDigitalState != bPressed)
139 {
140 m_bDigitalState = bPressed;
141
142 // Motion is initiated in ProcessMotions()
143 ResetMotion();
144
145 bHandled = m_bInitialPressHandled = m_handler->OnButtonPress(m_name, bPressed);
146
147 if (m_bDigitalState)
148 CLog::Log(LOGDEBUG, "FEATURE [ {} ] on {} pressed ({})", m_name, m_handler->ControllerID(),
149 bHandled ? "handled" : "ignored");
150 else
151 CLog::Log(LOGDEBUG, "FEATURE [ {} ] on {} released", m_name, m_handler->ControllerID());
152 }
153 else if (m_bDigitalState)
154 {
155 bHandled = m_bInitialPressHandled;
156 }
157
158 return bHandled;
159 }
160
161 bool CScalarFeature::OnAnalogMotion(float magnitude)
162 {
163 const bool bActivated = (magnitude != 0.0f);
164
165 // Update analog state
166 m_analogState = magnitude;
167
168 // Update motion time
169 if (!bActivated)
170 ResetMotion();
171 else if (!InMotion())
172 StartMotion();
173
174 // Log activation/deactivation
175 if (m_bDigitalState != bActivated)
176 {
177 m_bDigitalState = bActivated;
178 CLog::Log(LOGDEBUG, "FEATURE [ {} ] on {} {}", m_name, m_handler->ControllerID(),
179 bActivated ? "activated" : "deactivated");
180 }
181
182 return true;
183 }
184
185 void CScalarFeature::ProcessDigitalMotion()
186 {
187 if (!InMotion())
188 {
189 // Button was just pressed, record start time and exit (button press event
190 // was already sent this frame)
191 StartMotion();
192 }
193 else
194 {
195 // Button has been pressed more than one event frame
196 const unsigned int elapsed = MotionTimeMs();
197 m_handler->OnButtonHold(m_name, elapsed);
198 }
199 }
200
201 void CScalarFeature::ProcessAnalogMotion()
202 {
203 float magnitude = m_analogState;
204
205 // Calculate time elapsed since motion began
206 unsigned int elapsed = MotionTimeMs();
207
208 // If analog value is discrete, ramp up magnitude
209 if (m_bActivated && m_bDiscrete)
210 {
211 if (elapsed < DISCRETE_ANALOG_RAMPUP_TIME_MS)
212 {
213 magnitude *= static_cast<float>(elapsed) / DISCRETE_ANALOG_RAMPUP_TIME_MS;
214 if (magnitude < DISCRETE_ANALOG_START_VALUE)
215 magnitude = DISCRETE_ANALOG_START_VALUE;
216 }
217 }
218
219 m_handler->OnButtonMotion(m_name, magnitude, elapsed);
220 }
221
222 // --- CAxisFeature ------------------------------------------------------------
223
224 CAxisFeature::CAxisFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap)
225 : CJoystickFeature(name, handler, buttonMap)
226 {
227 }
228
229 bool CAxisFeature::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
230 {
231 return OnAnalogMotion(source, bPressed ? 1.0f : 0.0f);
232 }
233
234 void CAxisFeature::ProcessMotions(void)
235 {
236 const float newState = m_axis.GetPosition();
237
238 const bool bActivated = (newState != 0.0f);
239
240 if (!AcceptsInput(bActivated))
241 return;
242
243 const bool bWasActivated = (m_state != 0.0f);
244
245 if (!bActivated && bWasActivated)
246 CLog::Log(LOGDEBUG, "Feature [ {} ] on {} deactivated", m_name, m_handler->ControllerID());
247 else if (bActivated && !bWasActivated)
248 {
249 CLog::Log(LOGDEBUG, "Feature [ {} ] on {} activated {}", m_name, m_handler->ControllerID(),
250 newState > 0.0f ? "positive" : "negative");
251 }
252
253 if (bActivated || bWasActivated)
254 {
255 m_state = newState;
256
257 unsigned int motionTimeMs = 0;
258
259 if (bActivated)
260 {
261 if (!InMotion())
262 StartMotion();
263 else
264 motionTimeMs = MotionTimeMs();
265 }
266 else
267 ResetMotion();
268
269 switch (m_buttonMap->GetFeatureType(m_name))
270 {
271 case FEATURE_TYPE::WHEEL:
272 m_handler->OnWheelMotion(m_name, newState, motionTimeMs);
273 break;
274 case FEATURE_TYPE::THROTTLE:
275 m_handler->OnThrottleMotion(m_name, newState, motionTimeMs);
276 break;
277 default:
278 break;
279 }
280 }
281 }
282
283 // --- CWheel ------------------------------------------------------------------
284
285 CWheel::CWheel(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap)
286 : CAxisFeature(name, handler, buttonMap)
287 {
288 }
289
290 bool CWheel::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
291 {
292 WHEEL_DIRECTION direction = WHEEL_DIRECTION::NONE;
293
294 std::vector<WHEEL_DIRECTION> dirs = {
295 WHEEL_DIRECTION::RIGHT,
296 WHEEL_DIRECTION::LEFT,
297 };
298
299 CDriverPrimitive primitive;
300 for (auto dir : dirs)
301 {
302 if (m_buttonMap->GetWheel(m_name, dir, primitive) && primitive == source)
303 {
304 direction = dir;
305 break;
306 }
307 }
308
309 // Feature must accept input to be considered handled
310 bool bHandled = AcceptsInput(magnitude > 0.0f);
311
312 switch (direction)
313 {
314 case WHEEL_DIRECTION::RIGHT:
315 m_axis.SetPositiveDistance(magnitude);
316 break;
317 case WHEEL_DIRECTION::LEFT:
318 m_axis.SetNegativeDistance(magnitude);
319 break;
320 default:
321 // Just in case, avoid sticking
322 m_axis.Reset();
323 break;
324 }
325
326 return bHandled;
327 }
328
329 // --- CThrottle ---------------------------------------------------------------
330
331 CThrottle::CThrottle(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap)
332 : CAxisFeature(name, handler, buttonMap)
333 {
334 }
335
336 bool CThrottle::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
337 {
338 THROTTLE_DIRECTION direction = THROTTLE_DIRECTION::NONE;
339
340 std::vector<THROTTLE_DIRECTION> dirs = {
341 THROTTLE_DIRECTION::UP,
342 THROTTLE_DIRECTION::DOWN,
343 };
344
345 CDriverPrimitive primitive;
346 for (auto dir : dirs)
347 {
348 if (m_buttonMap->GetThrottle(m_name, dir, primitive) && primitive == source)
349 {
350 direction = dir;
351 break;
352 }
353 }
354
355 // Feature must accept input to be considered handled
356 bool bHandled = AcceptsInput(magnitude > 0.0f);
357
358 switch (direction)
359 {
360 case THROTTLE_DIRECTION::UP:
361 m_axis.SetPositiveDistance(magnitude);
362 break;
363 case THROTTLE_DIRECTION::DOWN:
364 m_axis.SetNegativeDistance(magnitude);
365 break;
366 default:
367 // Just in case, avoid sticking
368 m_axis.Reset();
369 break;
370 }
371
372 return bHandled;
373 }
374
375 // --- CAnalogStick ------------------------------------------------------------
376
377 CAnalogStick::CAnalogStick(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap)
378 : CJoystickFeature(name, handler, buttonMap)
379 {
380 }
381
382 bool CAnalogStick::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
383 {
384 return OnAnalogMotion(source, bPressed ? 1.0f : 0.0f);
385 }
386
387 bool CAnalogStick::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
388 {
389 ANALOG_STICK_DIRECTION direction = ANALOG_STICK_DIRECTION::NONE;
390
391 std::vector<ANALOG_STICK_DIRECTION> dirs = {
392 ANALOG_STICK_DIRECTION::UP,
393 ANALOG_STICK_DIRECTION::DOWN,
394 ANALOG_STICK_DIRECTION::RIGHT,
395 ANALOG_STICK_DIRECTION::LEFT,
396 };
397
398 CDriverPrimitive primitive;
399 for (auto dir : dirs)
400 {
401 if (m_buttonMap->GetAnalogStick(m_name, dir, primitive) && primitive == source)
402 {
403 direction = dir;
404 break;
405 }
406 }
407
408 // Feature must accept input to be considered handled
409 bool bHandled = AcceptsInput(magnitude > 0.0f);
410
411 switch (direction)
412 {
413 case ANALOG_STICK_DIRECTION::UP:
414 m_vertAxis.SetPositiveDistance(magnitude);
415 break;
416 case ANALOG_STICK_DIRECTION::DOWN:
417 m_vertAxis.SetNegativeDistance(magnitude);
418 break;
419 case ANALOG_STICK_DIRECTION::RIGHT:
420 m_horizAxis.SetPositiveDistance(magnitude);
421 break;
422 case ANALOG_STICK_DIRECTION::LEFT:
423 m_horizAxis.SetNegativeDistance(magnitude);
424 break;
425 default:
426 // Just in case, avoid sticking
427 m_vertAxis.Reset();
428 m_horizAxis.Reset();
429 break;
430 }
431
432 return bHandled;
433 }
434
435 void CAnalogStick::ProcessMotions(void)
436 {
437 const float newVertState = m_vertAxis.GetPosition();
438 const float newHorizState = m_horizAxis.GetPosition();
439
440 const bool bActivated = (newVertState != 0.0f || newHorizState != 0.0f);
441 const bool bWasActivated = (m_vertState != 0.0f || m_horizState != 0.0f);
442
443 if (bActivated ^ bWasActivated)
444 {
445 CLog::Log(LOGDEBUG, "Feature [ {} ] on {} {}", m_name, m_handler->ControllerID(),
446 bActivated ? "activated" : "deactivated");
447 }
448
449 if (bActivated || bWasActivated)
450 {
451 m_vertState = newVertState;
452 m_horizState = newHorizState;
453
454 unsigned int motionTimeMs = 0;
455
456 if (bActivated)
457 {
458 if (!InMotion())
459 StartMotion();
460 else
461 motionTimeMs = MotionTimeMs();
462 }
463 else
464 {
465 ResetMotion();
466 }
467
468 m_handler->OnAnalogStickMotion(m_name, newHorizState, newVertState, motionTimeMs);
469 }
470 }
471
472 // --- CAccelerometer ----------------------------------------------------------
473
474 CAccelerometer::CAccelerometer(const FeatureName& name,
475 IInputHandler* handler,
476 IButtonMap* buttonMap)
477 : CJoystickFeature(name, handler, buttonMap)
478 {
479 }
480
481 bool CAccelerometer::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
482 {
483 return OnAnalogMotion(source, bPressed ? 1.0f : 0.0f);
484 }
485
486 bool CAccelerometer::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
487 {
488 // Feature must accept input to be considered handled
489 bool bHandled = AcceptsInput(true);
490
491 CDriverPrimitive positiveX;
492 CDriverPrimitive positiveY;
493 CDriverPrimitive positiveZ;
494
495 m_buttonMap->GetAccelerometer(m_name, positiveX, positiveY, positiveZ);
496
497 if (source == positiveX)
498 {
499 m_xAxis.SetPositiveDistance(magnitude);
500 }
501 else if (source == positiveY)
502 {
503 m_yAxis.SetPositiveDistance(magnitude);
504 }
505 else if (source == positiveZ)
506 {
507 m_zAxis.SetPositiveDistance(magnitude);
508 }
509 else
510 {
511 // Just in case, avoid sticking
512 m_xAxis.Reset();
513 m_xAxis.Reset();
514 m_yAxis.Reset();
515 }
516
517 return bHandled;
518 }
519
520 void CAccelerometer::ProcessMotions(void)
521 {
522 const float newXAxis = m_xAxis.GetPosition();
523 const float newYAxis = m_yAxis.GetPosition();
524 const float newZAxis = m_zAxis.GetPosition();
525
526 const bool bActivated = (newXAxis != 0.0f || newYAxis != 0.0f || newZAxis != 0.0f);
527
528 if (!AcceptsInput(bActivated))
529 return;
530
531 const bool bWasActivated = (m_xAxisState != 0.0f || m_yAxisState != 0.0f || m_zAxisState != 0.0f);
532
533 if (bActivated || bWasActivated)
534 {
535 m_xAxisState = newXAxis;
536 m_yAxisState = newYAxis;
537 m_zAxisState = newZAxis;
538 m_handler->OnAccelerometerMotion(m_name, newXAxis, newYAxis, newZAxis);
539 }
540 }