summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/addon-instance/PeripheralUtils.h
diff options
context:
space:
mode:
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.h220
1 files changed, 169 insertions, 51 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
index 3c4cab3..c2efc05 100644
--- 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
@@ -23,8 +23,8 @@
23 23
24#include <array> // Requires c++11 24#include <array> // Requires c++11
25#include <cstring> 25#include <cstring>
26#include <map>
27#include <string> 26#include <string>
27#include <utility>
28#include <vector> 28#include <vector>
29 29
30#define PERIPHERAL_SAFE_DELETE(x) do { delete (x); (x) = NULL; } while (0) 30#define PERIPHERAL_SAFE_DELETE(x) do { delete (x); (x) = NULL; } while (0)
@@ -164,62 +164,70 @@ namespace addon
164 class PeripheralEvent 164 class PeripheralEvent
165 { 165 {
166 public: 166 public:
167 PeripheralEvent(void) : 167 PeripheralEvent(void)
168 m_event()
169 { 168 {
170 } 169 }
171 170
172 PeripheralEvent(unsigned int peripheralIndex, unsigned int buttonIndex, JOYSTICK_STATE_BUTTON state) : 171 PeripheralEvent(unsigned int peripheralIndex, unsigned int buttonIndex, JOYSTICK_STATE_BUTTON state) :
173 m_event() 172 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_BUTTON),
173 m_peripheralIndex(peripheralIndex),
174 m_driverIndex(buttonIndex),
175 m_buttonState(state)
174 { 176 {
175 SetType(PERIPHERAL_EVENT_TYPE_DRIVER_BUTTON);
176 SetPeripheralIndex(peripheralIndex);
177 SetDriverIndex(buttonIndex);
178 SetButtonState(state);
179 } 177 }
180 178
181 PeripheralEvent(unsigned int peripheralIndex, unsigned int hatIndex, JOYSTICK_STATE_HAT state) : 179 PeripheralEvent(unsigned int peripheralIndex, unsigned int hatIndex, JOYSTICK_STATE_HAT state) :
182 m_event() 180 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_HAT),
181 m_peripheralIndex(peripheralIndex),
182 m_driverIndex(hatIndex),
183 m_hatState(state)
183 { 184 {
184 SetType(PERIPHERAL_EVENT_TYPE_DRIVER_HAT);
185 SetPeripheralIndex(peripheralIndex);
186 SetDriverIndex(hatIndex);
187 SetHatState(state);
188 } 185 }
189 186
190 PeripheralEvent(unsigned int peripheralIndex, unsigned int axisIndex, JOYSTICK_STATE_AXIS state) : 187 PeripheralEvent(unsigned int peripheralIndex, unsigned int axisIndex, JOYSTICK_STATE_AXIS state) :
191 m_event() 188 m_type(PERIPHERAL_EVENT_TYPE_DRIVER_AXIS),
189 m_peripheralIndex(peripheralIndex),
190 m_driverIndex(axisIndex),
191 m_axisState(state)
192 { 192 {
193 SetType(PERIPHERAL_EVENT_TYPE_DRIVER_AXIS);
194 SetPeripheralIndex(peripheralIndex);
195 SetDriverIndex(axisIndex);
196 SetAxisState(state);
197 } 193 }
198 194
199 explicit PeripheralEvent(const PERIPHERAL_EVENT& event) : 195 explicit PeripheralEvent(const PERIPHERAL_EVENT& event) :
200 m_event(event) 196 m_type(event.type),
201 { 197 m_peripheralIndex(event.peripheral_index),
202 } 198 m_driverIndex(event.driver_index),
203 199 m_buttonState(event.driver_button_state),
204 PERIPHERAL_EVENT_TYPE Type(void) const { return m_event.type; } 200 m_hatState(event.driver_hat_state),
205 unsigned int PeripheralIndex(void) const { return m_event.peripheral_index; } 201 m_axisState(event.driver_axis_state),
206 unsigned int DriverIndex(void) const { return m_event.driver_index; } 202 m_motorState(event.motor_state)
207 JOYSTICK_STATE_BUTTON ButtonState(void) const { return m_event.driver_button_state; } 203 {
208 JOYSTICK_STATE_HAT HatState(void) const { return m_event.driver_hat_state; } 204 }
209 JOYSTICK_STATE_AXIS AxisState(void) const { return m_event.driver_axis_state; } 205
210 JOYSTICK_STATE_MOTOR MotorState(void) const { return m_event.motor_state; } 206 PERIPHERAL_EVENT_TYPE Type(void) const { return m_type; }
211 207 unsigned int PeripheralIndex(void) const { return m_peripheralIndex; }
212 void SetType(PERIPHERAL_EVENT_TYPE type) { m_event.type = type; } 208 unsigned int DriverIndex(void) const { return m_driverIndex; }
213 void SetPeripheralIndex(unsigned int index) { m_event.peripheral_index = index; } 209 JOYSTICK_STATE_BUTTON ButtonState(void) const { return m_buttonState; }
214 void SetDriverIndex(unsigned int index) { m_event.driver_index = index; } 210 JOYSTICK_STATE_HAT HatState(void) const { return m_hatState; }
215 void SetButtonState(JOYSTICK_STATE_BUTTON state) { m_event.driver_button_state = state; } 211 JOYSTICK_STATE_AXIS AxisState(void) const { return m_axisState; }
216 void SetHatState(JOYSTICK_STATE_HAT state) { m_event.driver_hat_state = state; } 212 JOYSTICK_STATE_MOTOR MotorState(void) const { return m_motorState; }
217 void SetAxisState(JOYSTICK_STATE_AXIS state) { m_event.driver_axis_state = state; } 213
218 void SetMotorState(JOYSTICK_STATE_MOTOR state) { m_event.motor_state = state; } 214 void SetType(PERIPHERAL_EVENT_TYPE type) { m_type = type; }
215 void SetPeripheralIndex(unsigned int index) { m_peripheralIndex = index; }
216 void SetDriverIndex(unsigned int index) { m_driverIndex = index; }
217 void SetButtonState(JOYSTICK_STATE_BUTTON state) { m_buttonState = state; }
218 void SetHatState(JOYSTICK_STATE_HAT state) { m_hatState = state; }
219 void SetAxisState(JOYSTICK_STATE_AXIS state) { m_axisState = state; }
220 void SetMotorState(JOYSTICK_STATE_MOTOR state) { m_motorState = state; }
219 221
220 void ToStruct(PERIPHERAL_EVENT& event) const 222 void ToStruct(PERIPHERAL_EVENT& event) const
221 { 223 {
222 event = m_event; 224 event.type = m_type;
225 event.peripheral_index = m_peripheralIndex;
226 event.driver_index = m_driverIndex;
227 event.driver_button_state = m_buttonState;
228 event.driver_hat_state = m_hatState;
229 event.driver_axis_state = m_axisState;
230 event.motor_state = m_motorState;
223 } 231 }
224 232
225 static void FreeStruct(PERIPHERAL_EVENT& event) 233 static void FreeStruct(PERIPHERAL_EVENT& event)
@@ -228,7 +236,13 @@ namespace addon
228 } 236 }
229 237
230 private: 238 private:
231 PERIPHERAL_EVENT m_event; 239 PERIPHERAL_EVENT_TYPE m_type = PERIPHERAL_EVENT_TYPE_NONE;
240 unsigned int m_peripheralIndex = 0;
241 unsigned int m_driverIndex = 0;
242 JOYSTICK_STATE_BUTTON m_buttonState = JOYSTICK_STATE_BUTTON_UNPRESSED;
243 JOYSTICK_STATE_HAT m_hatState = JOYSTICK_STATE_HAT_UNPRESSED;
244 JOYSTICK_STATE_AXIS m_axisState = 0.0f;
245 JOYSTICK_STATE_MOTOR m_motorState = 0.0f;
232 }; 246 };
233 247
234 typedef PeripheralVector<PeripheralEvent, PERIPHERAL_EVENT> PeripheralEvents; 248 typedef PeripheralVector<PeripheralEvent, PERIPHERAL_EVENT> PeripheralEvents;
@@ -298,9 +312,6 @@ namespace addon
298 unsigned int MotorCount(void) const { return m_motorCount; } 312 unsigned int MotorCount(void) const { return m_motorCount; }
299 bool SupportsPowerOff(void) const { return m_supportsPowerOff; } 313 bool SupportsPowerOff(void) const { return m_supportsPowerOff; }
300 314
301 // Derived property: Counts are unknown if all are zero
302 bool AreElementCountsKnown(void) const { return m_buttonCount != 0 || m_hatCount != 0 || m_axisCount != 0; }
303
304 void SetProvider(const std::string& provider) { m_provider = provider; } 315 void SetProvider(const std::string& provider) { m_provider = provider; }
305 void SetRequestedPort(int requestedPort) { m_requestedPort = requestedPort; } 316 void SetRequestedPort(int requestedPort) { m_requestedPort = requestedPort; }
306 void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; } 317 void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; }
@@ -352,6 +363,9 @@ namespace addon
352 * 2) a hat direction 363 * 2) a hat direction
353 * 3) a semiaxis (either the positive or negative half of an axis) 364 * 3) a semiaxis (either the positive or negative half of an axis)
354 * 4) a motor 365 * 4) a motor
366 * 5) a keyboard key
367 * 6) a mouse button
368 * 7) a relative pointer direction
355 * 369 *
356 * The type determines the fields in use: 370 * The type determines the fields in use:
357 * 371 *
@@ -370,6 +384,15 @@ namespace addon
370 * 384 *
371 * Motor: 385 * Motor:
372 * - driver index 386 * - driver index
387 *
388 * Key:
389 * - key code
390 *
391 * Mouse button:
392 * - driver index
393 *
394 * Relative pointer direction:
395 * - relative pointer direction
373 */ 396 */
374 struct DriverPrimitive 397 struct DriverPrimitive
375 { 398 {
@@ -383,7 +406,8 @@ namespace addon
383 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), 406 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
384 m_center(0), 407 m_center(0),
385 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), 408 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
386 m_range(1) 409 m_range(1),
410 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
387 { 411 {
388 } 412 }
389 413
@@ -397,12 +421,13 @@ namespace addon
397 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), 421 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
398 m_center(0), 422 m_center(0),
399 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), 423 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
400 m_range(1) 424 m_range(1),
425 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
401 { 426 {
402 } 427 }
403 428
404 /*! 429 /*!
405 * \brief Construct a driver primitive representing a button 430 * \brief Construct a driver primitive representing a joystick button
406 */ 431 */
407 static DriverPrimitive CreateButton(unsigned int buttonIndex) 432 static DriverPrimitive CreateButton(unsigned int buttonIndex)
408 { 433 {
@@ -419,7 +444,8 @@ namespace addon
419 m_hatDirection(direction), 444 m_hatDirection(direction),
420 m_center(0), 445 m_center(0),
421 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), 446 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
422 m_range(1) 447 m_range(1),
448 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
423 { 449 {
424 } 450 }
425 451
@@ -433,7 +459,8 @@ namespace addon
433 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), 459 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
434 m_center(center), 460 m_center(center),
435 m_semiAxisDirection(direction), 461 m_semiAxisDirection(direction),
436 m_range(range) 462 m_range(range),
463 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
437 { 464 {
438 } 465 }
439 466
@@ -445,13 +472,52 @@ namespace addon
445 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR, motorIndex); 472 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR, motorIndex);
446 } 473 }
447 474
475 /*!
476 * \brief Construct a driver primitive representing a key on a keyboard
477 */
478 DriverPrimitive(std::string keycode) :
479 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY),
480 m_driverIndex(0),
481 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
482 m_center(0),
483 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
484 m_range(1),
485 m_keycode(std::move(keycode)),
486 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
487 {
488 }
489
490 /*!
491 * \brief Construct a driver primitive representing a mouse button
492 */
493 static DriverPrimitive CreateMouseButton(JOYSTICK_DRIVER_MOUSE_INDEX buttonIndex)
494 {
495 return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON, static_cast<unsigned int>(buttonIndex));
496 }
497
498 /*!
499 * \brief Construct a driver primitive representing one of the four
500 * direction in which a relative pointer can move
501 */
502 DriverPrimitive(JOYSTICK_DRIVER_RELPOINTER_DIRECTION direction) :
503 m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION),
504 m_driverIndex(0),
505 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
506 m_center(0),
507 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
508 m_range(1),
509 m_relPointerDirection(direction)
510 {
511 }
512
448 explicit DriverPrimitive(const JOYSTICK_DRIVER_PRIMITIVE& primitive) : 513 explicit DriverPrimitive(const JOYSTICK_DRIVER_PRIMITIVE& primitive) :
449 m_type(primitive.type), 514 m_type(primitive.type),
450 m_driverIndex(0), 515 m_driverIndex(0),
451 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), 516 m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN),
452 m_center(0), 517 m_center(0),
453 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), 518 m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN),
454 m_range(1) 519 m_range(1),
520 m_relPointerDirection(JOYSTICK_DRIVER_RELPOINTER_UNKNOWN)
455 { 521 {
456 switch (m_type) 522 switch (m_type)
457 { 523 {
@@ -479,6 +545,21 @@ namespace addon
479 m_driverIndex = primitive.motor.index; 545 m_driverIndex = primitive.motor.index;
480 break; 546 break;
481 } 547 }
548 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
549 {
550 m_keycode = primitive.key.keycode;
551 break;
552 }
553 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
554 {
555 m_driverIndex = primitive.mouse.button;
556 break;
557 }
558 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
559 {
560 m_relPointerDirection = primitive.relpointer.direction;
561 break;
562 }
482 default: 563 default:
483 break; 564 break;
484 } 565 }
@@ -490,6 +571,9 @@ namespace addon
490 int Center(void) const { return m_center; } 571 int Center(void) const { return m_center; }
491 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION SemiAxisDirection(void) const { return m_semiAxisDirection; } 572 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION SemiAxisDirection(void) const { return m_semiAxisDirection; }
492 unsigned int Range(void) const { return m_range; } 573 unsigned int Range(void) const { return m_range; }
574 const std::string& Keycode(void) const { return m_keycode; }
575 JOYSTICK_DRIVER_MOUSE_INDEX MouseIndex(void) const { return static_cast<JOYSTICK_DRIVER_MOUSE_INDEX>(m_driverIndex); }
576 JOYSTICK_DRIVER_RELPOINTER_DIRECTION RelPointerDirection(void) const { return m_relPointerDirection; }
493 577
494 bool operator==(const DriverPrimitive& other) const 578 bool operator==(const DriverPrimitive& other) const
495 { 579 {
@@ -498,7 +582,6 @@ namespace addon
498 switch (m_type) 582 switch (m_type)
499 { 583 {
500 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON: 584 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON:
501 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
502 { 585 {
503 return m_driverIndex == other.m_driverIndex; 586 return m_driverIndex == other.m_driverIndex;
504 } 587 }
@@ -514,6 +597,22 @@ namespace addon
514 m_semiAxisDirection == other.m_semiAxisDirection && 597 m_semiAxisDirection == other.m_semiAxisDirection &&
515 m_range == other.m_range; 598 m_range == other.m_range;
516 } 599 }
600 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
601 {
602 return m_keycode == other.m_keycode;
603 }
604 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
605 {
606 return m_driverIndex == other.m_driverIndex;
607 }
608 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
609 {
610 return m_driverIndex == other.m_driverIndex;
611 }
612 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
613 {
614 return m_relPointerDirection == other.m_relPointerDirection;
615 }
517 default: 616 default:
518 break; 617 break;
519 } 618 }
@@ -550,6 +649,23 @@ namespace addon
550 driver_primitive.motor.index = m_driverIndex; 649 driver_primitive.motor.index = m_driverIndex;
551 break; 650 break;
552 } 651 }
652 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_KEY:
653 {
654 const size_t size = sizeof(driver_primitive.key.keycode);
655 std::strncpy(driver_primitive.key.keycode, m_keycode.c_str(), size - 1);
656 driver_primitive.key.keycode[size - 1] = '\0';
657 break;
658 }
659 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOUSE_BUTTON:
660 {
661 driver_primitive.mouse.button = static_cast<JOYSTICK_DRIVER_MOUSE_INDEX>(m_driverIndex);
662 break;
663 }
664 case JOYSTICK_DRIVER_PRIMITIVE_TYPE_RELPOINTER_DIRECTION:
665 {
666 driver_primitive.relpointer.direction = m_relPointerDirection;
667 break;
668 }
553 default: 669 default:
554 break; 670 break;
555 } 671 }
@@ -567,6 +683,8 @@ namespace addon
567 int m_center; 683 int m_center;
568 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION m_semiAxisDirection; 684 JOYSTICK_DRIVER_SEMIAXIS_DIRECTION m_semiAxisDirection;
569 unsigned int m_range; 685 unsigned int m_range;
686 std::string m_keycode;
687 JOYSTICK_DRIVER_RELPOINTER_DIRECTION m_relPointerDirection;
570 }; 688 };
571 689
572 typedef PeripheralVector<DriverPrimitive, JOYSTICK_DRIVER_PRIMITIVE> DriverPrimitives; 690 typedef PeripheralVector<DriverPrimitive, JOYSTICK_DRIVER_PRIMITIVE> DriverPrimitives;
@@ -584,6 +702,7 @@ namespace addon
584 * 6) absolute pointer 702 * 6) absolute pointer
585 * 7) wheel 703 * 7) wheel
586 * 8) throttle 704 * 8) throttle
705 * 9) keyboard key
587 * 706 *
588 * [1] All three driver primitives (buttons, hats and axes) have a state that 707 * [1] All three driver primitives (buttons, hats and axes) have a state that
589 * can be represented using a single scalar value. For this reason, 708 * can be represented using a single scalar value. For this reason,
@@ -598,7 +717,7 @@ namespace addon
598 JoystickFeature(const std::string& name = "", JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN) : 717 JoystickFeature(const std::string& name = "", JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN) :
599 m_name(name), 718 m_name(name),
600 m_type(type), 719 m_type(type),
601 m_primitives() 720 m_primitives{}
602 { 721 {
603 } 722 }
604 723
@@ -672,4 +791,3 @@ namespace addon
672 791
673} /* namespace addon */ 792} /* namespace addon */
674} /* namespace kodi */ 793} /* namespace kodi */
675