summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h')
-rw-r--r--xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h735
1 files changed, 0 insertions, 735 deletions
diff --git a/xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h b/xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h
deleted file mode 100644
index 62e5a93..0000000
--- a/xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h
+++ /dev/null
@@ -1,735 +0,0 @@
1/*
2 * Copyright (C) 2014-2018 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 "Peripheral.h"
12
13#include <array> // Requires c++11
14#include <cstring>
15#include <string>
16#include <utility>
17#include <vector>
18
19#define PERIPHERAL_SAFE_DELETE(x) do { delete (x); (x) = NULL; } while (0)
20#define PERIPHERAL_SAFE_DELETE_ARRAY(x) do { delete[] (x); (x) = NULL; } while (0)
21
22namespace kodi
23{
24namespace addon
25{
26 /*!
27 * Utility class to manipulate arrays of peripheral types.
28 */
29 template <class THE_CLASS, typename THE_STRUCT>
30 class PeripheralVector
31 {
32 public:
33 static void ToStructs(const std::vector<THE_CLASS>& vecObjects, THE_STRUCT** pStructs)
34 {
35 if (!pStructs)
36 return;
37
38 if (vecObjects.empty())
39 {
40 *pStructs = NULL;
41 }
42 else
43 {
44 (*pStructs) = new THE_STRUCT[vecObjects.size()];
45 for (unsigned int i = 0; i < vecObjects.size(); i++)
46 vecObjects.at(i).ToStruct((*pStructs)[i]);
47 }
48 }
49
50 static void ToStructs(const std::vector<THE_CLASS*>& vecObjects, THE_STRUCT** pStructs)
51 {
52 if (!pStructs)
53 return;
54
55 if (vecObjects.empty())
56 {
57 *pStructs = NULL;
58 }
59 else
60 {
61 *pStructs = new THE_STRUCT[vecObjects.size()];
62 for (unsigned int i = 0; i < vecObjects.size(); i++)
63 vecObjects.at(i)->ToStruct((*pStructs)[i]);
64 }
65 }
66
67 static void FreeStructs(unsigned int structCount, THE_STRUCT* structs)
68 {
69 if (structs)
70 {
71 for (unsigned int i = 0; i < structCount; i++)
72 THE_CLASS::FreeStruct(structs[i]);
73 }
74 PERIPHERAL_SAFE_DELETE_ARRAY(structs);
75 }
76 };
77
78 /*!
79 * ADDON::Peripheral
80 *
81 * Wrapper class providing peripheral information. Classes can extend
82 * Peripheral to inherit peripheral properties.
83 */
84 class Peripheral
85 {
86 public:
87 Peripheral(PERIPHERAL_TYPE type = PERIPHERAL_TYPE_UNKNOWN, const std::string& strName = "") :
88 m_type(type),
89 m_strName(strName)
90 {
91 }
92
93 explicit Peripheral(const PERIPHERAL_INFO& info) :
94 m_type(info.type),
95 m_strName(info.name ? info.name : ""),
96 m_vendorId(info.vendor_id),
97 m_productId(info.product_id),
98 m_index(info.index)
99 {
100 }
101
102 virtual ~Peripheral(void) = default;
103
104 PERIPHERAL_TYPE Type(void) const { return m_type; }
105 const std::string& Name(void) const { return m_strName; }
106 uint16_t VendorID(void) const { return m_vendorId; }
107 uint16_t ProductID(void) const { return m_productId; }
108 unsigned int Index(void) const { return m_index; }
109
110 // Derived property: VID and PID are 0x0000 if unknown
111 bool IsVidPidKnown(void) const { return m_vendorId != 0 || m_productId != 0; }
112
113 void SetType(PERIPHERAL_TYPE type) { m_type = type; }
114 void SetName(const std::string& strName) { m_strName = strName; }
115 void SetVendorID(uint16_t vendorId) { m_vendorId = vendorId; }
116 void SetProductID(uint16_t productId) { m_productId = productId; }
117 void SetIndex(unsigned int index) { m_index = index; }
118
119 void ToStruct(PERIPHERAL_INFO& info) const
120 {
121 info.type = m_type;
122 info.name = new char[m_strName.size() + 1];
123 info.vendor_id = m_vendorId;
124 info.product_id = m_productId;
125 info.index = m_index;
126
127 std::strcpy(info.name, m_strName.c_str());
128 }
129
130 static void FreeStruct(PERIPHERAL_INFO& info)
131 {
132 PERIPHERAL_SAFE_DELETE_ARRAY(info.name);
133 }
134
135 private:
136 PERIPHERAL_TYPE m_type;
137 std::string m_strName;
138 uint16_t m_vendorId = 0;
139 uint16_t m_productId = 0;
140 unsigned int m_index = 0;
141 };
142
143 typedef PeripheralVector<Peripheral, PERIPHERAL_INFO> Peripherals;
144
145 /*!
146 * ADDON::PeripheralEvent
147 *
148 * Wrapper class for peripheral events.
149 */
150 class PeripheralEvent
151 {
152 public:
153 PeripheralEvent() = default;
154
155 PeripheralEvent(unsigned int peripheralIndex, unsigned int buttonIndex, JOYSTICK_STATE_BUTTON state) :
156 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_BUTTON),
157 m_peripheralIndex(peripheralIndex),
158 m_driverIndex(buttonIndex),
159 m_buttonState(state)
160 {
161 }
162
163 PeripheralEvent(unsigned int peripheralIndex, unsigned int hatIndex, JOYSTICK_STATE_HAT state) :
164 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_HAT),
165 m_peripheralIndex(peripheralIndex),
166 m_driverIndex(hatIndex),
167 m_hatState(state)
168 {
169 }
170
171 PeripheralEvent(unsigned int peripheralIndex, unsigned int axisIndex, JOYSTICK_STATE_AXIS state) :
172 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_AXIS),
173 m_peripheralIndex(peripheralIndex),
174 m_driverIndex(axisIndex),
175 m_axisState(state)
176 {
177 }
178
179 explicit PeripheralEvent(const PERIPHERAL_EVENT& event) :
180 m_type(event.type),
181 m_peripheralIndex(event.peripheral_index),
182 m_driverIndex(event.driver_index),
183 m_buttonState(event.driver_button_state),
184 m_hatState(event.driver_hat_state),
185 m_axisState(event.driver_axis_state),
186 m_motorState(event.motor_state)
187 {
188 }
189
190 PERIPHERAL_EVENT_TYPE Type(void) const { return m_type; }
191 unsigned int PeripheralIndex(void) const { return m_peripheralIndex; }
192 unsigned int DriverIndex(void) const { return m_driverIndex; }
193 JOYSTICK_STATE_BUTTON ButtonState(void) const { return m_buttonState; }
194 JOYSTICK_STATE_HAT HatState(void) const { return m_hatState; }
195 JOYSTICK_STATE_AXIS AxisState(void) const { return m_axisState; }
196 JOYSTICK_STATE_MOTOR MotorState(void) const { return m_motorState; }
197
198 void SetType(PERIPHERAL_EVENT_TYPE type) { m_type = type; }
199 void SetPeripheralIndex(unsigned int index) { m_peripheralIndex = index; }
200 void SetDriverIndex(unsigned int index) { m_driverIndex = index; }
201 void SetButtonState(JOYSTICK_STATE_BUTTON state) { m_buttonState = state; }
202 void SetHatState(JOYSTICK_STATE_HAT state) { m_hatState = state; }
203 void SetAxisState(JOYSTICK_STATE_AXIS state) { m_axisState = state; }
204 void SetMotorState(JOYSTICK_STATE_MOTOR state) { m_motorState = state; }
205
206 void ToStruct(PERIPHERAL_EVENT& event) const
207 {
208 event.type = m_type;
209 event.peripheral_index = m_peripheralIndex;
210 event.driver_index = m_driverIndex;
211 event.driver_button_state = m_buttonState;
212 event.driver_hat_state = m_hatState;
213 event.driver_axis_state = m_axisState;
214 event.motor_state = m_motorState;
215 }
216
217 static void FreeStruct(PERIPHERAL_EVENT& event)
218 {
219 (void)event;
220 }
221
222 private:
223 PERIPHERAL_EVENT_TYPE m_type = PERIPHERAL_EVENT_TYPE_NONE;
224 unsigned int m_peripheralIndex = 0;
225 unsigned int m_driverIndex = 0;
226 JOYSTICK_STATE_BUTTON m_buttonState = JOYSTICK_STATE_BUTTON_UNPRESSED;
227 JOYSTICK_STATE_HAT m_hatState = JOYSTICK_STATE_HAT_UNPRESSED;
228 JOYSTICK_STATE_AXIS m_axisState = 0.0f;
229 JOYSTICK_STATE_MOTOR m_motorState = 0.0f;
230 };
231
232 typedef PeripheralVector<PeripheralEvent, PERIPHERAL_EVENT> PeripheralEvents;
233
234 /*!
235 * kodi::addon::Joystick
236 *
237 * Wrapper class providing additional joystick information not provided by
238 * ADDON::Peripheral.
239 */
240 class Joystick : public Peripheral
241 {
242 public:
243 Joystick(const std::string& provider = "", const std::string& strName = "") :
244 Peripheral(PERIPHERAL_TYPE_JOYSTICK, strName),
245 m_provider(provider),
246 m_requestedPort(NO_PORT_REQUESTED)
247 {
248 }
249
250 Joystick(const Joystick& other)
251 {
252 *this = other;
253 }
254
255 explicit Joystick(const JOYSTICK_INFO& info) :
256 Peripheral(info.peripheral),
257 m_provider(info.provider ? info.provider : ""),
258 m_requestedPort(info.requested_port),
259 m_buttonCount(info.button_count),
260 m_hatCount(info.hat_count),
261 m_axisCount(info.axis_count),
262 m_motorCount(info.motor_count),
263 m_supportsPowerOff(info.supports_poweroff)
264 {
265 }
266
267 ~Joystick(void) override = default;
268
269 Joystick& operator=(const Joystick& rhs)
270 {
271 if (this != &rhs)
272 {
273 Peripheral::operator=(rhs);
274
275 m_provider = rhs.m_provider;
276 m_requestedPort = rhs.m_requestedPort;
277 m_buttonCount = rhs.m_buttonCount;
278 m_hatCount = rhs.m_hatCount;
279 m_axisCount = rhs.m_axisCount;
280 m_motorCount = rhs.m_motorCount;
281 m_supportsPowerOff = rhs.m_supportsPowerOff;
282 }
283 return *this;
284 }
285
286 const std::string& Provider(void) const { return m_provider; }
287 int RequestedPort(void) const { return m_requestedPort; }
288 unsigned int ButtonCount(void) const { return m_buttonCount; }
289 unsigned int HatCount(void) const { return m_hatCount; }
290 unsigned int AxisCount(void) const { return m_axisCount; }
291 unsigned int MotorCount(void) const { return m_motorCount; }
292 bool SupportsPowerOff(void) const { return m_supportsPowerOff; }
293
294 void SetProvider(const std::string& provider) { m_provider = provider; }
295 void SetRequestedPort(int requestedPort) { m_requestedPort = requestedPort; }
296 void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; }
297 void SetHatCount(unsigned int hatCount) { m_hatCount = hatCount; }
298 void SetAxisCount(unsigned int axisCount) { m_axisCount = axisCount; }
299 void SetMotorCount(unsigned int motorCount) { m_motorCount = motorCount; }
300 void SetSupportsPowerOff(bool supportsPowerOff) { m_supportsPowerOff = supportsPowerOff; }
301
302 void ToStruct(JOYSTICK_INFO& info) const
303 {
304 Peripheral::ToStruct(info.peripheral);
305
306 info.provider = new char[m_provider.size() + 1];
307 info.requested_port = m_requestedPort;
308 info.button_count = m_buttonCount;
309 info.hat_count = m_hatCount;
310 info.axis_count = m_axisCount;
311 info.motor_count = m_motorCount;
312 info.supports_poweroff = m_supportsPowerOff;
313
314 std::strcpy(info.provider, m_provider.c_str());
315 }
316
317 static void FreeStruct(JOYSTICK_INFO& info)
318 {
319 Peripheral::FreeStruct(info.peripheral);
320
321 PERIPHERAL_SAFE_DELETE_ARRAY(info.provider);
322 }
323
324 private:
325 std::string m_provider;
326 int m_requestedPort;
327 unsigned int m_buttonCount = 0;
328 unsigned int m_hatCount = 0;
329 unsigned int m_axisCount = 0;
330 unsigned int m_motorCount = 0;
331 bool m_supportsPowerOff = false;
332 };
333
334 typedef PeripheralVector<Joystick, JOYSTICK_INFO> Joysticks;
335
336 /*!
337 * ADDON::DriverPrimitive
338 *
339 * Base class for joystick driver primitives. A driver primitive can be:
340 *
341 * 1) a button
342 * 2) a hat direction
343 * 3) a semiaxis (either the positive or negative half of an axis)
344 * 4) a motor
345 * 5) a keyboard key
346 * 6) a mouse button
347 * 7) a relative pointer direction
348 *
349 * The type determines the fields in use:
350 *
351 * Button:
352 * - driver index
353 *
354 * Hat direction:
355 * - driver index
356 * - hat direction
357 *
358 * Semiaxis:
359 * - driver index
360 * - center
361 * - semiaxis direction
362 * - range
363 *
364 * Motor:
365 * - driver index
366 *
367 * Key:
368 * - key code
369 *
370 * Mouse button:
371 * - driver index
372 *
373 * Relative pointer direction:
374 * - relative pointer direction
375 */
376 struct DriverPrimitive
377 {
378 protected:
379 /*!
380 * \brief Construct a driver primitive of the specified type
381 */
382 DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE type, unsigned int driverIndex) :
383 m_type(type),
384 m_driverIndex(driverIndex)
385 {
386 }
387
388 public:
389 /*!
390 * \brief Construct an invalid driver primitive
391 */
392 DriverPrimitive(void) = default;
393
394 /*!
395 * \brief Construct a driver primitive representing a joystick button
396 */
397 static DriverPrimitive CreateButton(unsigned int buttonIndex)
398 {
399 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON, buttonIndex);
400 }
401
402 /*!
403 * \brief Construct a driver primitive representing one of the four direction
404 * arrows on a dpad
405 */
406 DriverPrimitive(unsigned int hatIndex, JOYSTICK_DRIVER_HAT_DIRECTION direction) :
407 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION),
408 m_driverIndex(hatIndex),
409 m_hatDirection(direction)
410 {
411 }
412
413 /*!
414 * \brief Construct a driver primitive representing the positive or negative
415 * half of an axis
416 */
417 DriverPrimitive(unsigned int axisIndex, int center, JOYSTICK_DRIVER_SEMIAXIS_DIRECTION direction, unsigned int range) :
418 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS),
419 m_driverIndex(axisIndex),
420 m_center(center),
421 m_semiAxisDirection(direction),
422 m_range(range)
423 {
424 }
425
426 /*!
427 * \brief Construct a driver primitive representing a motor
428 */
429 static DriverPrimitive CreateMotor(unsigned int motorIndex)
430 {
431 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR, motorIndex);
432 }
433
434 /*!
435 * \brief Construct a driver primitive representing a key on a keyboard
436 */
437 DriverPrimitive(std::string keycode) :
438 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY),
439 m_keycode(std::move(keycode))
440 {
441 }
442
443 /*!
444 * \brief Construct a driver primitive representing a mouse button
445 */
446 static DriverPrimitive CreateMouseButton(JOYSTICK_DRIVER_MOUSE_INDEX buttonIndex)
447 {
448 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON, static_cast<unsigned int>(buttonIndex));
449 }
450
451 /*!
452 * \brief Construct a driver primitive representing one of the four
453 * direction in which a relative pointer can move
454 */
455 DriverPrimitive(JOYSTICK_DRIVER_RELPOINTER_DIRECTION direction) :
456 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION),
457 m_relPointerDirection(direction)
458 {
459 }
460
461 explicit DriverPrimitive(const JOYSTICK_DRIVER_PRIMITIVE& primitive) :
462 m_type(primitive.type)
463 {
464 switch (m_type)
465 {
466 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON:
467 {
468 m_driverIndex = primitive.button.index;
469 break;
470 }
471 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION:
472 {
473 m_driverIndex = primitive.hat.index;
474 m_hatDirection = primitive.hat.direction;
475 break;
476 }
477 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS:
478 {
479 m_driverIndex = primitive.semiaxis.index;
480 m_center = primitive.semiaxis.center;
481 m_semiAxisDirection = primitive.semiaxis.direction;
482 m_range = primitive.semiaxis.range;
483 break;
484 }
485 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
486 {
487 m_driverIndex = primitive.motor.index;
488 break;
489 }
490 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
491 {
492 m_keycode = primitive.key.keycode;
493 break;
494 }
495 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
496 {
497 m_driverIndex = primitive.mouse.button;
498 break;
499 }
500 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
501 {
502 m_relPointerDirection = primitive.relpointer.direction;
503 break;
504 }
505 default:
506 break;
507 }
508 }
509
510 JOYSTICK_DRIVER_PRIMITIVE_TYPE Type(void) const { return m_type; }
511 unsigned int DriverIndex(void) const { return m_driverIndex; }
512 JOYSTICK_DRIVER_HAT_DIRECTION HatDirection(void) const { return m_hatDirection; }
513 int Center(void) const { return m_center; }
514 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION SemiAxisDirection(void) const { return m_semiAxisDirection; }
515 unsigned int Range(void) const { return m_range; }
516 const std::string& Keycode(void) const { return m_keycode; }
517 JOYSTICK_DRIVER_MOUSE_INDEX MouseIndex(void) const { return static_cast<JOYSTICK_DRIVER_MOUSE_INDEX>(m_driverIndex); }
518 JOYSTICK_DRIVER_RELPOINTER_DIRECTION RelPointerDirection(void) const { return m_relPointerDirection; }
519
520 bool operator==(const DriverPrimitive& other) const
521 {
522 if (m_type == other.m_type)
523 {
524 switch (m_type)
525 {
526 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON:
527 {
528 return m_driverIndex == other.m_driverIndex;
529 }
530 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION:
531 {
532 return m_driverIndex == other.m_driverIndex &&
533 m_hatDirection == other.m_hatDirection;
534 }
535 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS:
536 {
537 return m_driverIndex == other.m_driverIndex &&
538 m_center == other.m_center &&
539 m_semiAxisDirection == other.m_semiAxisDirection &&
540 m_range == other.m_range;
541 }
542 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
543 {
544 return m_keycode == other.m_keycode;
545 }
546 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
547 {
548 return m_driverIndex == other.m_driverIndex;
549 }
550 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
551 {
552 return m_driverIndex == other.m_driverIndex;
553 }
554 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
555 {
556 return m_relPointerDirection == other.m_relPointerDirection;
557 }
558 default:
559 break;
560 }
561 }
562 return false;
563 }
564
565 void ToStruct(JOYSTICK_DRIVER_PRIMITIVE& driver_primitive) const
566 {
567 driver_primitive.type = m_type;
568 switch (m_type)
569 {
570 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON:
571 {
572 driver_primitive.button.index = m_driverIndex;
573 break;
574 }
575 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION:
576 {
577 driver_primitive.hat.index = m_driverIndex;
578 driver_primitive.hat.direction = m_hatDirection;
579 break;
580 }
581 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS:
582 {
583 driver_primitive.semiaxis.index = m_driverIndex;
584 driver_primitive.semiaxis.center = m_center;
585 driver_primitive.semiaxis.direction = m_semiAxisDirection;
586 driver_primitive.semiaxis.range = m_range;
587 break;
588 }
589 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
590 {
591 driver_primitive.motor.index = m_driverIndex;
592 break;
593 }
594 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
595 {
596 const size_t size = sizeof(driver_primitive.key.keycode);
597 std::strncpy(driver_primitive.key.keycode, m_keycode.c_str(), size - 1);
598 driver_primitive.key.keycode[size - 1] = '\0';
599 break;
600 }
601 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
602 {
603 driver_primitive.mouse.button = static_cast<JOYSTICK_DRIVER_MOUSE_INDEX>(m_driverIndex);
604 break;
605 }
606 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
607 {
608 driver_primitive.relpointer.direction = m_relPointerDirection;
609 break;
610 }
611 default:
612 break;
613 }
614 }
615
616 static void FreeStruct(JOYSTICK_DRIVER_PRIMITIVE& primitive)
617 {
618 (void)primitive;
619 }
620
621 private:
622 JOYSTICK_DRIVER_PRIMITIVE_TYPE m_type = JOYSTICK_DRIVER_PRIMITIVE_TYPE_UNKNOWN;
623 unsigned int m_driverIndex = 0;
624 JOYSTICK_DRIVER_HAT_DIRECTION m_hatDirection = JOYSTICK_DRIVER_HAT_UNKNOWN;
625 int m_center = 0;
626 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION m_semiAxisDirection = JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN;
627 unsigned int m_range = 1;
628 std::string m_keycode;
629 JOYSTICK_DRIVER_RELPOINTER_DIRECTION m_relPointerDirection = JOYSTICK_DRIVER_RELPOINTER_UNKNOWN;
630 };
631
632 typedef PeripheralVector<DriverPrimitive, JOYSTICK_DRIVER_PRIMITIVE> DriverPrimitives;
633
634 /*!
635 * kodi::addon::JoystickFeature
636 *
637 * Class for joystick features. A feature can be:
638 *
639 * 1) scalar[1]
640 * 2) analog stick
641 * 3) accelerometer
642 * 4) motor
643 * 5) relative pointer[2]
644 * 6) absolute pointer
645 * 7) wheel
646 * 8) throttle
647 * 9) keyboard key
648 *
649 * [1] All three driver primitives (buttons, hats and axes) have a state that
650 * can be represented using a single scalar value. For this reason,
651 * features that map to a single primitive are called "scalar features".
652 *
653 * [2] Relative pointers are similar to analog sticks, but they use
654 * relative distances instead of positions.
655 */
656 class JoystickFeature
657 {
658 public:
659 JoystickFeature(const std::string& name = "", JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN) :
660 m_name(name),
661 m_type(type),
662 m_primitives{}
663 {
664 }
665
666 JoystickFeature(const JoystickFeature& other)
667 {
668 *this = other;
669 }
670
671 explicit JoystickFeature(const JOYSTICK_FEATURE& feature) :
672 m_name(feature.name ? feature.name : ""),
673 m_type(feature.type)
674 {
675 for (unsigned int i = 0; i < JOYSTICK_PRIMITIVE_MAX; i++)
676 m_primitives[i] = DriverPrimitive(feature.primitives[i]);
677 }
678
679 JoystickFeature& operator=(const JoystickFeature& rhs)
680 {
681 if (this != &rhs)
682 {
683 m_name = rhs.m_name;
684 m_type = rhs.m_type;
685 m_primitives = rhs.m_primitives;
686 }
687 return *this;
688 }
689
690 bool operator==(const JoystickFeature& other) const
691 {
692 return m_name == other.m_name &&
693 m_type == other.m_type &&
694 m_primitives == other.m_primitives;
695 }
696
697 const std::string& Name(void) const { return m_name; }
698 JOYSTICK_FEATURE_TYPE Type(void) const { return m_type; }
699 bool IsValid() const { return m_type != JOYSTICK_FEATURE_TYPE_UNKNOWN; }
700
701 void SetName(const std::string& name) { m_name = name; }
702 void SetType(JOYSTICK_FEATURE_TYPE type) { m_type = type; }
703 void SetInvalid(void) { m_type = JOYSTICK_FEATURE_TYPE_UNKNOWN; }
704
705 const DriverPrimitive& Primitive(JOYSTICK_FEATURE_PRIMITIVE which) const { return m_primitives[which]; }
706 void SetPrimitive(JOYSTICK_FEATURE_PRIMITIVE which, const DriverPrimitive& primitive) { m_primitives[which] = primitive; }
707
708 std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX>& Primitives() { return m_primitives; }
709 const std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX>& Primitives() const { return m_primitives; }
710
711 void ToStruct(JOYSTICK_FEATURE& feature) const
712 {
713 feature.name = new char[m_name.length() + 1];
714 feature.type = m_type;
715 for (unsigned int i = 0; i < JOYSTICK_PRIMITIVE_MAX; i++)
716 m_primitives[i].ToStruct(feature.primitives[i]);
717
718 std::strcpy(feature.name, m_name.c_str());
719 }
720
721 static void FreeStruct(JOYSTICK_FEATURE& feature)
722 {
723 PERIPHERAL_SAFE_DELETE_ARRAY(feature.name);
724 }
725
726 private:
727 std::string m_name;
728 JOYSTICK_FEATURE_TYPE m_type;
729 std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX> m_primitives;
730 };
731
732 typedef PeripheralVector<JoystickFeature, JOYSTICK_FEATURE> JoystickFeatures;
733
734} /* namespace addon */
735} /* namespace kodi */