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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
|
/*
* 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 "../AddonBase.h"
#include "peripheral/PeripheralUtils.h"
#ifdef __cplusplus
namespace kodi
{
namespace addon
{
//##############################################################################
/// @defgroup cpp_kodi_addon_peripheral_Defs Definitions, structures and enumerators
/// @ingroup cpp_kodi_addon_peripheral
/// @brief %Peripheral add-on general variables
///
/// Used to exchange the available options between Kodi and addon.
///
///
//##############################################################################
/// @defgroup cpp_kodi_addon_peripheral_Defs_General 1. General
/// @ingroup cpp_kodi_addon_peripheral_Defs
/// @brief **%Peripheral add-on general variables**\n
/// Used to exchange the available options between Kodi and addon.
///
/// This group also includes @ref cpp_kodi_addon_peripheral_Defs_PeripheralCapabilities
/// with which Kodi an @ref kodi::addon::CInstancePeripheral::GetCapabilities()
/// queries the supported **modules** of the addon.
///
//##############################################################################
/// @defgroup cpp_kodi_addon_peripheral_Defs_Peripheral 2. Peripheral
/// @ingroup cpp_kodi_addon_peripheral_Defs
/// @brief **%Peripheral add-on operation variables**\n
/// Used to exchange the available options between Kodi and addon.
///
//##############################################################################
/// @defgroup cpp_kodi_addon_peripheral_Defs_Event 3. Event
/// @ingroup cpp_kodi_addon_peripheral_Defs
/// @brief **%Event add-on operation variables**\n
/// Used to exchange the available options between Kodi and addon.
///
//##############################################################################
/// @defgroup cpp_kodi_addon_peripheral_Defs_Joystick 4. Joystick
/// @ingroup cpp_kodi_addon_peripheral_Defs
/// @brief **%Joystick add-on operation variables**\n
/// Used to exchange the available options between Kodi and addon.
///
//==============================================================================
/// @addtogroup cpp_kodi_addon_peripheral
/// @brief \cpp_class{ kodi::addon::CInstancePeripheral }
/// **%Peripheral add-on instance**
///
/// The peripheral add-ons provides access to many joystick and gamepad
/// interfaces across various platforms. An input addon is used to map the
/// buttons/axis on your physical input device, to the buttons/axis of your
/// virtual system. This is necessary because different retro systems usually
/// have different button layouts. A controller configuration utility is also
/// in the works.
///
/// ----------------------------------------------------------------------------
///
/// Here is an example of what the <b>`addon.xml.in`</b> would look like for an
/// peripheral addon:
///
/// ~~~~~~~~~~~~~{.xml}
/// <?xml version="1.0" encoding="UTF-8"?>
/// <addon
/// id="peripheral.myspecialnamefor"
/// version="1.0.0"
/// name="My special peripheral addon"
/// provider-name="Your Name">
/// <requires>@ADDON_DEPENDS@</requires>
/// <extension
/// point="kodi.peripheral"
/// provides_joysticks="true"
/// provides_buttonmaps="true"
/// library_@PLATFORM@="@LIBRARY_FILENAME@"/>
/// <extension point="xbmc.addon.metadata">
/// <summary lang="en_GB">My peripheral addon</summary>
/// <description lang="en_GB">My peripheral addon description</description>
/// <platform>@PLATFORM@</platform>
/// </extension>
/// </addon>
/// ~~~~~~~~~~~~~
///
/// Description to peripheral related addon.xml values:
/// | Name | Description
/// |:------------------------------|----------------------------------------
/// | <b>`provides_joysticks`</b> | Set to "true" if addon provides joystick support.
/// | <b>`provides_buttonmaps`</b> | Set to "true" if button map is used and supported by addon.
/// | <b>`point`</b> | Addon type specification<br>At all addon types and for this kind always <b>"kodi.peripheral"</b>.
/// | <b>`library_@PLATFORM@`</b> | Sets the used library name, which is automatically set by cmake at addon build.
///
/// @remark For more detailed description of the <b>`addon.xml`</b>, see also https://kodi.wiki/view/Addon.xml.
///
///
/// --------------------------------------------------------------------------
///
/// **Here is an example of how addon can be used as a single:**
/// ~~~~~~~~~~~~~{.cpp}
/// #include <kodi/addon-instance/Peripheral.h>
///
/// class CMyPeripheralAddon : public kodi::addon::CAddonBase,
/// public kodi::addon::CInstancePeripheral
/// {
/// public:
/// CMyPeripheralAddon();
///
/// void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) override;
/// ...
/// };
///
/// CMyPeripheralAddon::CMyPeripheralAddon()
/// {
/// ...
/// }
///
/// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
/// {
/// capabilities.SetProvidesJoysticks(true);
/// capabilities.SetProvidesButtonmaps(true);
/// ...
/// }
///
/// ADDONCREATOR(CMyPeripheralAddon)
/// ~~~~~~~~~~~~~
///
/// @note It is imperative to use the necessary functions of this class in the
/// addon.
///
/// --------------------------------------------------------------------------
///
///
/// **Here is another example where the peripheral is used together with
/// other instance types:**
///
/// ~~~~~~~~~~~~~{.cpp}
/// #include <kodi/addon-instance/Peripheral.h>
///
/// class CMyPeripheralAddon : public kodi::addon::CInstancePeripheral
/// {
/// public:
/// CMyPeripheralAddon(KODI_HANDLE instance, const std::string& version);
///
/// void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) override;
/// ...
/// };
///
/// CMyPeripheralAddon::CMyPeripheralAddon(KODI_HANDLE instance, const std::string& version)
/// : CInstancePeripheral(instance, version)
/// {
/// ...
/// }
///
/// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
/// {
/// capabilities.SetProvidesJoysticks(true);
/// capabilities.SetProvidesButtonmaps(true);
/// ...
/// }
///
/// //----------------------------------------------------------------------
///
/// class CMyAddon : public kodi::addon::CAddonBase
/// {
/// public:
/// CMyAddon() = default;
/// ADDON_STATUS CreateInstance(int instanceType,
/// const std::string& instanceID,
/// KODI_HANDLE instance,
/// const std::string& version,
/// KODI_HANDLE& addonInstance) override;
/// };
///
/// // If you use only one instance in your add-on, can be instanceType and
/// // instanceID ignored
/// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
/// const std::string& instanceID,
/// KODI_HANDLE instance,
/// const std::string& version,
/// KODI_HANDLE& addonInstance)
/// {
/// if (instanceType == ADDON_INSTANCE_PERIPHERAL)
/// {
/// kodi::Log(ADDON_LOG_INFO, "Creating my peripheral addon");
/// addonInstance = new CMyPeripheralAddon(instance, version);
/// return ADDON_STATUS_OK;
/// }
/// else if (...)
/// {
/// ...
/// }
/// return ADDON_STATUS_UNKNOWN;
/// }
///
/// ADDONCREATOR(CMyAddon)
/// ~~~~~~~~~~~~~
///
/// The destruction of the example class `CMyPeripheralAddon` is called from
/// Kodi's header. Manually deleting the add-on instance is not required.
///
class ATTRIBUTE_HIDDEN CInstancePeripheral : public IAddonInstance
{
public:
//============================================================================
/// @ingroup cpp_kodi_addon_peripheral
/// @brief %Peripheral class constructor.
///
/// Used by an add-on that only supports peripheral.
///
CInstancePeripheral()
: IAddonInstance(ADDON_INSTANCE_PERIPHERAL, GetKodiTypeVersion(ADDON_INSTANCE_PERIPHERAL))
{
if (CAddonBase::m_interface->globalSingleInstance != nullptr)
throw std::logic_error("kodi::addon::CInstancePeripheral: Creation of more as one in single "
"instance way is not allowed!");
SetAddonStruct(CAddonBase::m_interface->firstKodiInstance);
CAddonBase::m_interface->globalSingleInstance = this;
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_addon_peripheral
/// @brief %Peripheral addon class constructor used to support multiple
/// instance types.
///
/// @param[in] instance The instance value given to
/// <b>`kodi::addon::CAddonBase::CreateInstance(...)`</b>.
/// @param[in] kodiVersion [opt] Version used in Kodi for this instance, to
/// allow compatibility to older Kodi versions.
///
/// @note Recommended to set <b>`kodiVersion`</b>.
///
///
/// --------------------------------------------------------------------------
///
//////*Here's example about the use of this:**
/// ~~~~~~~~~~~~~{.cpp}
/// class CMyPeripheralAddon : public kodi::addon::CInstancePeripheral
/// {
/// public:
/// CMyPeripheralAddon(KODI_HANDLE instance, const std::string& kodiVersion)
/// : kodi::addon::CInstancePeripheral(instance, kodiVersion)
/// {
/// ...
/// }
///
/// ...
/// };
///
/// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
/// const std::string& instanceID,
/// KODI_HANDLE instance,
/// const std::string& version,
/// KODI_HANDLE& addonInstance)
/// {
/// kodi::Log(ADDON_LOG_INFO, "Creating my peripheral");
/// addonInstance = new CMyPeripheralAddon(instance, version);
/// return ADDON_STATUS_OK;
/// }
/// ~~~~~~~~~~~~~
///
explicit CInstancePeripheral(KODI_HANDLE instance, const std::string& kodiVersion = "")
: IAddonInstance(ADDON_INSTANCE_PERIPHERAL,
!kodiVersion.empty() ? kodiVersion
: GetKodiTypeVersion(ADDON_INSTANCE_PERIPHERAL))
{
if (CAddonBase::m_interface->globalSingleInstance != nullptr)
throw std::logic_error("kodi::addon::CInstancePeripheral: Creation of multiple together with "
"single instance way is not allowed!");
SetAddonStruct(instance);
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_addon_peripheral
/// @brief Destructor.
///
~CInstancePeripheral() override = default;
//----------------------------------------------------------------------------
//============================================================================
/// @defgroup cpp_kodi_addon_peripheral_peripheralOp 1. Peripheral operations
/// @ingroup cpp_kodi_addon_peripheral
/// @brief %Peripheral operations to handle control about.
///
///---------------------------------------------------------------------------
///
/// **%Peripheral parts in interface:**\n
/// Copy this to your project and extend with your parts or leave functions
/// complete away where not used or supported.
///
/// @copydetails cpp_kodi_addon_peripheral_peripheralOp_header_addon_auto_check
/// @copydetails cpp_kodi_addon_peripheral_peripheralOp_source_addon_auto_check
///
///@{
//============================================================================
/// @brief Get the list of features that this add-on provides.
///
/// Called by the frontend to query the add-on's capabilities and supported
/// peripherals. All capabilities that the add-on supports should be set to true.
///
/// @param[out] capabilities The add-on's capabilities
///
/// @remarks Valid implementation required.
///
///
/// ----------------------------------------------------------------------------
///
/// @copydetails cpp_kodi_addon_peripheral_Defs_PeripheralCapabilities_Help
///
/// --------------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.cpp}
/// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
/// {
/// capabilities.SetProvidesJoysticks(true);
/// capabilities.SetProvidesButtonmaps(true);
/// }
/// ~~~~~~~~~~~~~
///
virtual void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) {}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Perform a scan for joysticks
///
/// The frontend calls this when a hardware change is detected. If an add-on
/// detects a hardware change, it can trigger this function using the
/// @ref TriggerScan() callback.
///
/// @param[in] scan_results Assigned to allocated memory
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
///
/// --------------------------------------------------------------------------
///
/// @copydetails cpp_kodi_addon_peripheral_Defs_Peripheral_Peripheral_Help
///
virtual PERIPHERAL_ERROR PerformDeviceScan(
std::vector<std::shared_ptr<kodi::addon::Peripheral>>& scan_results)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Get all events that have occurred since the last call to
/// @ref GetEvents().
///
/// @param[out] events List of available events within addon
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
/// ----------------------------------------------------------------------------
///
/// @copydetails cpp_kodi_addon_peripheral_Defs_Peripheral_PeripheralEvent_Help
///
virtual PERIPHERAL_ERROR GetEvents(std::vector<kodi::addon::PeripheralEvent>& events)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Send an input event to the peripheral.
///
/// @param[in] event The input event
/// @return true if the event was handled, false otherwise
///
virtual bool SendEvent(const kodi::addon::PeripheralEvent& event) { return false; }
//----------------------------------------------------------------------------
///@}
//============================================================================
/// @defgroup cpp_kodi_addon_peripheral_joystickOp 2. Joystick operations
/// @ingroup cpp_kodi_addon_peripheral
/// @brief %Joystick operations to handle control about.
///
///
///---------------------------------------------------------------------------
///
/// **%Joystick parts in interface:**\n
/// Copy this to your project and extend with your parts or leave functions
/// complete away where not used or supported.
///
/// @copydetails cpp_kodi_addon_peripheral_joystickOp_header_addon_auto_check
/// @copydetails cpp_kodi_addon_peripheral_joystickOp_source_addon_auto_check
///
///@{
//============================================================================
/// @brief Get extended info about an attached joystick.
///
/// @param[in] index The joystick's driver index
/// @param[out] info The container for the allocated joystick info
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
///
/// ----------------------------------------------------------------------------
///
/// @copydetails cpp_kodi_addon_peripheral_Defs_Joystick_Joystick_Help
///
virtual PERIPHERAL_ERROR GetJoystickInfo(unsigned int index, kodi::addon::Joystick& info)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Get the features that allow translating the joystick into the
/// controller profile.
///
/// @param[in] joystick The device's joystick properties; unknown values may
/// be left at their default
/// @param[in] controller_id The controller profile being requested, e.g.
/// `game.controller.default`
/// @param[out] features The array of allocated features
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
virtual PERIPHERAL_ERROR GetFeatures(const kodi::addon::Joystick& joystick,
const std::string& controller_id,
std::vector<kodi::addon::JoystickFeature>& features)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Add or update joystick features.
///
/// @param[in] joystick The device's joystick properties; unknown values may be
/// left at their default
/// @param[in] controller_id The game controller profile being updated
/// @param[in] features The array of features
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
virtual PERIPHERAL_ERROR MapFeatures(const kodi::addon::Joystick& joystick,
const std::string& controller_id,
const std::vector<kodi::addon::JoystickFeature>& features)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Get the driver primitives that should be ignored while mapping the
/// device.
///
/// @param[in] joystick The device's joystick properties; unknown values may
/// be left at their default
/// @param[out] primitives The array of allocated driver primitives to be
/// ignored
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
virtual PERIPHERAL_ERROR GetIgnoredPrimitives(
const kodi::addon::Joystick& joystick, std::vector<kodi::addon::DriverPrimitive>& primitives)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Set the list of driver primitives that are ignored for the device.
///
/// @param[in] joystick The device's joystick properties; unknown values may be left at their default
/// @param[in] primitives The array of driver primitives to ignore
/// @return @ref PERIPHERAL_NO_ERROR if successful
///
virtual PERIPHERAL_ERROR SetIgnoredPrimitives(
const kodi::addon::Joystick& joystick,
const std::vector<kodi::addon::DriverPrimitive>& primitives)
{
return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Save the button map for the given joystick.
///
/// @param[in] joystick The device's joystick properties
///
virtual void SaveButtonMap(const kodi::addon::Joystick& joystick) {}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Revert the button map to the last time it was loaded or committed to disk
/// @param[in] joystick The device's joystick properties
///
virtual void RevertButtonMap(const kodi::addon::Joystick& joystick) {}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Reset the button map for the given joystick and controller profile ID
/// @param[in] joystick The device's joystick properties
/// @param[in] controller_id The game controller profile being reset
///
virtual void ResetButtonMap(const kodi::addon::Joystick& joystick,
const std::string& controller_id)
{
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Powers off the given joystick if supported
/// @param[in] index The joystick's driver index
///
virtual void PowerOffJoystick(unsigned int index) {}
//----------------------------------------------------------------------------
///@}
//============================================================================
/// @defgroup cpp_kodi_addon_peripheral_callbacks 3. Callback functions
/// @ingroup cpp_kodi_addon_peripheral
/// @brief Callback to Kodi functions.
///
///@{
//============================================================================
/// @brief Used to get the full path where the add-on is installed.
///
/// @return The add-on installation path
///
const std::string AddonPath() const { return m_instanceData->props->addon_path; }
//----------------------------------------------------------------------------
//============================================================================
/// @brief Used to get the full path to the add-on's user profile.
///
/// @note The trailing folder (consisting of the add-on's ID) is not created
/// by default. If it is needed, you must call kodi::vfs::CreateDirectory()
/// to create the folder.
///
/// @return Path to the user profile
///
const std::string UserPath() const { return m_instanceData->props->user_path; }
//----------------------------------------------------------------------------
//============================================================================
/// @brief Trigger a scan for peripherals
///
/// The add-on calls this if a change in hardware is detected.
///
void TriggerScan(void)
{
return m_instanceData->toKodi->trigger_scan(m_instanceData->toKodi->kodiInstance);
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Notify the frontend that button maps have changed.
///
/// @param[in] deviceName [optional] The name of the device to refresh, or
/// empty/null for all devices
/// @param[in] controllerId [optional] The controller ID to refresh, or
/// empty/null for all controllers
///
void RefreshButtonMaps(const std::string& deviceName = "", const std::string& controllerId = "")
{
return m_instanceData->toKodi->refresh_button_maps(m_instanceData->toKodi->kodiInstance,
deviceName.c_str(), controllerId.c_str());
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Return the number of features belonging to the specified
/// controller.
///
/// @param[in] controllerId The controller ID to enumerate
/// @param[in] type [optional] Type to filter by, or @ref JOYSTICK_FEATURE_TYPE_UNKNOWN
/// for all features
/// @return The number of features matching the request parameters
///
unsigned int FeatureCount(const std::string& controllerId,
JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN)
{
return m_instanceData->toKodi->feature_count(m_instanceData->toKodi->kodiInstance,
controllerId.c_str(), type);
}
//----------------------------------------------------------------------------
//============================================================================
/// @brief Return the type of the feature.
///
/// @param[in] controllerId The controller ID to check
/// @param[in] featureName The feature to check
/// @return The type of the specified feature, or @ref JOYSTICK_FEATURE_TYPE_UNKNOWN
/// if unknown
///
JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId, const std::string& featureName)
{
return m_instanceData->toKodi->feature_type(m_instanceData->toKodi->kodiInstance,
controllerId.c_str(), featureName.c_str());
}
//----------------------------------------------------------------------------
///@}
private:
void SetAddonStruct(KODI_HANDLE instance)
{
if (instance == nullptr)
throw std::logic_error("kodi::addon::CInstancePeripheral: Creation with empty addon "
"structure not allowed, table must be given from Kodi!");
m_instanceData = static_cast<AddonInstance_Peripheral*>(instance);
m_instanceData->toAddon->addonInstance = this;
m_instanceData->toAddon->get_capabilities = ADDON_GetCapabilities;
m_instanceData->toAddon->perform_device_scan = ADDON_PerformDeviceScan;
m_instanceData->toAddon->free_scan_results = ADDON_FreeScanResults;
m_instanceData->toAddon->get_events = ADDON_GetEvents;
m_instanceData->toAddon->free_events = ADDON_FreeEvents;
m_instanceData->toAddon->send_event = ADDON_SendEvent;
m_instanceData->toAddon->get_joystick_info = ADDON_GetJoystickInfo;
m_instanceData->toAddon->free_joystick_info = ADDON_FreeJoystickInfo;
m_instanceData->toAddon->get_features = ADDON_GetFeatures;
m_instanceData->toAddon->free_features = ADDON_FreeFeatures;
m_instanceData->toAddon->map_features = ADDON_MapFeatures;
m_instanceData->toAddon->get_ignored_primitives = ADDON_GetIgnoredPrimitives;
m_instanceData->toAddon->free_primitives = ADDON_FreePrimitives;
m_instanceData->toAddon->set_ignored_primitives = ADDON_SetIgnoredPrimitives;
m_instanceData->toAddon->save_button_map = ADDON_SaveButtonMap;
m_instanceData->toAddon->revert_button_map = ADDON_RevertButtonMap;
m_instanceData->toAddon->reset_button_map = ADDON_ResetButtonMap;
m_instanceData->toAddon->power_off_joystick = ADDON_PowerOffJoystick;
}
inline static void ADDON_GetCapabilities(const AddonInstance_Peripheral* addonInstance,
PERIPHERAL_CAPABILITIES* capabilities)
{
if (!addonInstance || !capabilities)
return;
kodi::addon::PeripheralCapabilities peripheralCapabilities(capabilities);
static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->GetCapabilities(peripheralCapabilities);
}
inline static PERIPHERAL_ERROR ADDON_PerformDeviceScan(
const AddonInstance_Peripheral* addonInstance,
unsigned int* peripheral_count,
PERIPHERAL_INFO** scan_results)
{
if (!addonInstance || !peripheral_count || !scan_results)
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
std::vector<std::shared_ptr<kodi::addon::Peripheral>> peripherals;
PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->PerformDeviceScan(peripherals);
if (err == PERIPHERAL_NO_ERROR)
{
*peripheral_count = static_cast<unsigned int>(peripherals.size());
kodi::addon::Peripherals::ToStructs(peripherals, scan_results);
}
return err;
}
inline static void ADDON_FreeScanResults(const AddonInstance_Peripheral* addonInstance,
unsigned int peripheral_count,
PERIPHERAL_INFO* scan_results)
{
if (!addonInstance)
return;
kodi::addon::Peripherals::FreeStructs(peripheral_count, scan_results);
}
inline static PERIPHERAL_ERROR ADDON_GetEvents(const AddonInstance_Peripheral* addonInstance,
unsigned int* event_count,
PERIPHERAL_EVENT** events)
{
if (!addonInstance || !event_count || !events)
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
std::vector<kodi::addon::PeripheralEvent> peripheralEvents;
PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->GetEvents(peripheralEvents);
if (err == PERIPHERAL_NO_ERROR)
{
*event_count = static_cast<unsigned int>(peripheralEvents.size());
kodi::addon::PeripheralEvents::ToStructs(peripheralEvents, events);
}
return err;
}
inline static void ADDON_FreeEvents(const AddonInstance_Peripheral* addonInstance,
unsigned int event_count,
PERIPHERAL_EVENT* events)
{
if (!addonInstance)
return;
kodi::addon::PeripheralEvents::FreeStructs(event_count, events);
}
inline static bool ADDON_SendEvent(const AddonInstance_Peripheral* addonInstance,
const PERIPHERAL_EVENT* event)
{
if (!addonInstance || !event)
return false;
return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->SendEvent(kodi::addon::PeripheralEvent(*event));
}
inline static PERIPHERAL_ERROR ADDON_GetJoystickInfo(
const AddonInstance_Peripheral* addonInstance, unsigned int index, JOYSTICK_INFO* info)
{
if (!addonInstance || !info)
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
kodi::addon::Joystick addonInfo;
PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->GetJoystickInfo(index, addonInfo);
if (err == PERIPHERAL_NO_ERROR)
{
addonInfo.ToStruct(*info);
}
return err;
}
inline static void ADDON_FreeJoystickInfo(const AddonInstance_Peripheral* addonInstance,
JOYSTICK_INFO* info)
{
if (!addonInstance)
return;
kodi::addon::Joystick::FreeStruct(*info);
}
inline static PERIPHERAL_ERROR ADDON_GetFeatures(const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick,
const char* controller_id,
unsigned int* feature_count,
JOYSTICK_FEATURE** features)
{
if (!addonInstance || !joystick || !controller_id || !feature_count || !features)
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
kodi::addon::Joystick addonJoystick(*joystick);
std::vector<kodi::addon::JoystickFeature> featuresVector;
PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->GetFeatures(addonJoystick, controller_id, featuresVector);
if (err == PERIPHERAL_NO_ERROR)
{
*feature_count = static_cast<unsigned int>(featuresVector.size());
kodi::addon::JoystickFeatures::ToStructs(featuresVector, features);
}
return err;
}
inline static void ADDON_FreeFeatures(const AddonInstance_Peripheral* addonInstance,
unsigned int feature_count,
JOYSTICK_FEATURE* features)
{
if (!addonInstance)
return;
kodi::addon::JoystickFeatures::FreeStructs(feature_count, features);
}
inline static PERIPHERAL_ERROR ADDON_MapFeatures(const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick,
const char* controller_id,
unsigned int feature_count,
const JOYSTICK_FEATURE* features)
{
if (!addonInstance || !joystick || !controller_id || (feature_count > 0 && !features))
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
kodi::addon::Joystick addonJoystick(*joystick);
std::vector<kodi::addon::JoystickFeature> primitiveVector;
for (unsigned int i = 0; i < feature_count; i++)
primitiveVector.emplace_back(*(features + i));
return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->MapFeatures(addonJoystick, controller_id, primitiveVector);
}
inline static PERIPHERAL_ERROR ADDON_GetIgnoredPrimitives(
const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick,
unsigned int* primitive_count,
JOYSTICK_DRIVER_PRIMITIVE** primitives)
{
if (!addonInstance || !joystick || !primitive_count || !primitives)
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
kodi::addon::Joystick addonJoystick(*joystick);
std::vector<kodi::addon::DriverPrimitive> primitiveVector;
PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->GetIgnoredPrimitives(addonJoystick, primitiveVector);
if (err == PERIPHERAL_NO_ERROR)
{
*primitive_count = static_cast<unsigned int>(primitiveVector.size());
kodi::addon::DriverPrimitives::ToStructs(primitiveVector, primitives);
}
return err;
}
inline static void ADDON_FreePrimitives(const AddonInstance_Peripheral* addonInstance,
unsigned int primitive_count,
JOYSTICK_DRIVER_PRIMITIVE* primitives)
{
if (!addonInstance)
return;
kodi::addon::DriverPrimitives::FreeStructs(primitive_count, primitives);
}
inline static PERIPHERAL_ERROR ADDON_SetIgnoredPrimitives(
const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick,
unsigned int primitive_count,
const JOYSTICK_DRIVER_PRIMITIVE* primitives)
{
if (!addonInstance || !joystick || (primitive_count > 0 && !primitives))
return PERIPHERAL_ERROR_INVALID_PARAMETERS;
kodi::addon::Joystick addonJoystick(*joystick);
std::vector<kodi::addon::DriverPrimitive> primitiveVector;
for (unsigned int i = 0; i < primitive_count; i++)
primitiveVector.emplace_back(*(primitives + i));
return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->SetIgnoredPrimitives(addonJoystick, primitiveVector);
}
inline static void ADDON_SaveButtonMap(const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick)
{
if (!addonInstance || !joystick)
return;
kodi::addon::Joystick addonJoystick(*joystick);
static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->SaveButtonMap(addonJoystick);
}
inline static void ADDON_RevertButtonMap(const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick)
{
if (!addonInstance || !joystick)
return;
kodi::addon::Joystick addonJoystick(*joystick);
static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->RevertButtonMap(addonJoystick);
}
inline static void ADDON_ResetButtonMap(const AddonInstance_Peripheral* addonInstance,
const JOYSTICK_INFO* joystick,
const char* controller_id)
{
if (!addonInstance || !joystick || !controller_id)
return;
kodi::addon::Joystick addonJoystick(*joystick);
static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->ResetButtonMap(addonJoystick, controller_id);
}
inline static void ADDON_PowerOffJoystick(const AddonInstance_Peripheral* addonInstance,
unsigned int index)
{
if (!addonInstance)
return;
static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
->PowerOffJoystick(index);
}
AddonInstance_Peripheral* m_instanceData;
};
} /* namespace addon */
} /* namespace kodi */
#endif /* __cplusplus */
|