summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h')
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h907
1 files changed, 907 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h
new file mode 100644
index 0000000..46060a8
--- /dev/null
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h
@@ -0,0 +1,907 @@
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 "../AddonBase.h"
12#include "peripheral/PeripheralUtils.h"
13
14#ifdef __cplusplus
15namespace kodi
16{
17namespace addon
18{
19
20//##############################################################################
21/// @defgroup cpp_kodi_addon_peripheral_Defs Definitions, structures and enumerators
22/// @ingroup cpp_kodi_addon_peripheral
23/// @brief %Peripheral add-on general variables
24///
25/// Used to exchange the available options between Kodi and addon.
26///
27///
28
29//##############################################################################
30/// @defgroup cpp_kodi_addon_peripheral_Defs_General 1. General
31/// @ingroup cpp_kodi_addon_peripheral_Defs
32/// @brief **%Peripheral add-on general variables**\n
33/// Used to exchange the available options between Kodi and addon.
34///
35/// This group also includes @ref cpp_kodi_addon_peripheral_Defs_PeripheralCapabilities
36/// with which Kodi an @ref kodi::addon::CInstancePeripheral::GetCapabilities()
37/// queries the supported **modules** of the addon.
38///
39
40//##############################################################################
41/// @defgroup cpp_kodi_addon_peripheral_Defs_Peripheral 2. Peripheral
42/// @ingroup cpp_kodi_addon_peripheral_Defs
43/// @brief **%Peripheral add-on operation variables**\n
44/// Used to exchange the available options between Kodi and addon.
45///
46
47//##############################################################################
48/// @defgroup cpp_kodi_addon_peripheral_Defs_Event 3. Event
49/// @ingroup cpp_kodi_addon_peripheral_Defs
50/// @brief **%Event add-on operation variables**\n
51/// Used to exchange the available options between Kodi and addon.
52///
53
54//##############################################################################
55/// @defgroup cpp_kodi_addon_peripheral_Defs_Joystick 4. Joystick
56/// @ingroup cpp_kodi_addon_peripheral_Defs
57/// @brief **%Joystick add-on operation variables**\n
58/// Used to exchange the available options between Kodi and addon.
59///
60
61//==============================================================================
62/// @addtogroup cpp_kodi_addon_peripheral
63/// @brief \cpp_class{ kodi::addon::CInstancePeripheral }
64/// **%Peripheral add-on instance**
65///
66/// The peripheral add-ons provides access to many joystick and gamepad
67/// interfaces across various platforms. An input addon is used to map the
68/// buttons/axis on your physical input device, to the buttons/axis of your
69/// virtual system. This is necessary because different retro systems usually
70/// have different button layouts. A controller configuration utility is also
71/// in the works.
72///
73/// ----------------------------------------------------------------------------
74///
75/// Here is an example of what the <b>`addon.xml.in`</b> would look like for an
76/// peripheral addon:
77///
78/// ~~~~~~~~~~~~~{.xml}
79/// <?xml version="1.0" encoding="UTF-8"?>
80/// <addon
81/// id="peripheral.myspecialnamefor"
82/// version="1.0.0"
83/// name="My special peripheral addon"
84/// provider-name="Your Name">
85/// <requires>@ADDON_DEPENDS@</requires>
86/// <extension
87/// point="kodi.peripheral"
88/// provides_joysticks="true"
89/// provides_buttonmaps="true"
90/// library_@PLATFORM@="@LIBRARY_FILENAME@"/>
91/// <extension point="xbmc.addon.metadata">
92/// <summary lang="en_GB">My peripheral addon</summary>
93/// <description lang="en_GB">My peripheral addon description</description>
94/// <platform>@PLATFORM@</platform>
95/// </extension>
96/// </addon>
97/// ~~~~~~~~~~~~~
98///
99/// Description to peripheral related addon.xml values:
100/// | Name | Description
101/// |:------------------------------|----------------------------------------
102/// | <b>`provides_joysticks`</b> | Set to "true" if addon provides joystick support.
103/// | <b>`provides_buttonmaps`</b> | Set to "true" if button map is used and supported by addon.
104/// | <b>`point`</b> | Addon type specification<br>At all addon types and for this kind always <b>"kodi.peripheral"</b>.
105/// | <b>`library_@PLATFORM@`</b> | Sets the used library name, which is automatically set by cmake at addon build.
106///
107/// @remark For more detailed description of the <b>`addon.xml`</b>, see also https://kodi.wiki/view/Addon.xml.
108///
109///
110/// --------------------------------------------------------------------------
111///
112/// **Here is an example of how addon can be used as a single:**
113/// ~~~~~~~~~~~~~{.cpp}
114/// #include <kodi/addon-instance/Peripheral.h>
115///
116/// class CMyPeripheralAddon : public kodi::addon::CAddonBase,
117/// public kodi::addon::CInstancePeripheral
118/// {
119/// public:
120/// CMyPeripheralAddon();
121///
122/// void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) override;
123/// ...
124/// };
125///
126/// CMyPeripheralAddon::CMyPeripheralAddon()
127/// {
128/// ...
129/// }
130///
131/// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
132/// {
133/// capabilities.SetProvidesJoysticks(true);
134/// capabilities.SetProvidesButtonmaps(true);
135/// ...
136/// }
137///
138/// ADDONCREATOR(CMyPeripheralAddon)
139/// ~~~~~~~~~~~~~
140///
141/// @note It is imperative to use the necessary functions of this class in the
142/// addon.
143///
144/// --------------------------------------------------------------------------
145///
146///
147/// **Here is another example where the peripheral is used together with
148/// other instance types:**
149///
150/// ~~~~~~~~~~~~~{.cpp}
151/// #include <kodi/addon-instance/Peripheral.h>
152///
153/// class CMyPeripheralAddon : public kodi::addon::CInstancePeripheral
154/// {
155/// public:
156/// CMyPeripheralAddon(KODI_HANDLE instance, const std::string& version);
157///
158/// void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) override;
159/// ...
160/// };
161///
162/// CMyPeripheralAddon::CMyPeripheralAddon(KODI_HANDLE instance, const std::string& version)
163/// : CInstancePeripheral(instance, version)
164/// {
165/// ...
166/// }
167///
168/// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
169/// {
170/// capabilities.SetProvidesJoysticks(true);
171/// capabilities.SetProvidesButtonmaps(true);
172/// ...
173/// }
174///
175/// //----------------------------------------------------------------------
176///
177/// class CMyAddon : public kodi::addon::CAddonBase
178/// {
179/// public:
180/// CMyAddon() = default;
181/// ADDON_STATUS CreateInstance(int instanceType,
182/// const std::string& instanceID,
183/// KODI_HANDLE instance,
184/// const std::string& version,
185/// KODI_HANDLE& addonInstance) override;
186/// };
187///
188/// // If you use only one instance in your add-on, can be instanceType and
189/// // instanceID ignored
190/// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
191/// const std::string& instanceID,
192/// KODI_HANDLE instance,
193/// const std::string& version,
194/// KODI_HANDLE& addonInstance)
195/// {
196/// if (instanceType == ADDON_INSTANCE_PERIPHERAL)
197/// {
198/// kodi::Log(ADDON_LOG_INFO, "Creating my peripheral addon");
199/// addonInstance = new CMyPeripheralAddon(instance, version);
200/// return ADDON_STATUS_OK;
201/// }
202/// else if (...)
203/// {
204/// ...
205/// }
206/// return ADDON_STATUS_UNKNOWN;
207/// }
208///
209/// ADDONCREATOR(CMyAddon)
210/// ~~~~~~~~~~~~~
211///
212/// The destruction of the example class `CMyPeripheralAddon` is called from
213/// Kodi's header. Manually deleting the add-on instance is not required.
214///
215class ATTRIBUTE_HIDDEN CInstancePeripheral : public IAddonInstance
216{
217public:
218 //============================================================================
219 /// @ingroup cpp_kodi_addon_peripheral
220 /// @brief %Peripheral class constructor.
221 ///
222 /// Used by an add-on that only supports peripheral.
223 ///
224 CInstancePeripheral()
225 : IAddonInstance(ADDON_INSTANCE_PERIPHERAL, GetKodiTypeVersion(ADDON_INSTANCE_PERIPHERAL))
226 {
227 if (CAddonBase::m_interface->globalSingleInstance != nullptr)
228 throw std::logic_error("kodi::addon::CInstancePeripheral: Creation of more as one in single "
229 "instance way is not allowed!");
230
231 SetAddonStruct(CAddonBase::m_interface->firstKodiInstance);
232 CAddonBase::m_interface->globalSingleInstance = this;
233 }
234 //----------------------------------------------------------------------------
235
236 //============================================================================
237 /// @ingroup cpp_kodi_addon_peripheral
238 /// @brief %Peripheral addon class constructor used to support multiple
239 /// instance types.
240 ///
241 /// @param[in] instance The instance value given to
242 /// <b>`kodi::addon::CAddonBase::CreateInstance(...)`</b>.
243 /// @param[in] kodiVersion [opt] Version used in Kodi for this instance, to
244 /// allow compatibility to older Kodi versions.
245 ///
246 /// @note Recommended to set <b>`kodiVersion`</b>.
247 ///
248 ///
249 /// --------------------------------------------------------------------------
250 ///
251 //////*Here's example about the use of this:**
252 /// ~~~~~~~~~~~~~{.cpp}
253 /// class CMyPeripheralAddon : public kodi::addon::CInstancePeripheral
254 /// {
255 /// public:
256 /// CMyPeripheralAddon(KODI_HANDLE instance, const std::string& kodiVersion)
257 /// : kodi::addon::CInstancePeripheral(instance, kodiVersion)
258 /// {
259 /// ...
260 /// }
261 ///
262 /// ...
263 /// };
264 ///
265 /// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
266 /// const std::string& instanceID,
267 /// KODI_HANDLE instance,
268 /// const std::string& version,
269 /// KODI_HANDLE& addonInstance)
270 /// {
271 /// kodi::Log(ADDON_LOG_INFO, "Creating my peripheral");
272 /// addonInstance = new CMyPeripheralAddon(instance, version);
273 /// return ADDON_STATUS_OK;
274 /// }
275 /// ~~~~~~~~~~~~~
276 ///
277 explicit CInstancePeripheral(KODI_HANDLE instance, const std::string& kodiVersion = "")
278 : IAddonInstance(ADDON_INSTANCE_PERIPHERAL,
279 !kodiVersion.empty() ? kodiVersion
280 : GetKodiTypeVersion(ADDON_INSTANCE_PERIPHERAL))
281 {
282 if (CAddonBase::m_interface->globalSingleInstance != nullptr)
283 throw std::logic_error("kodi::addon::CInstancePeripheral: Creation of multiple together with "
284 "single instance way is not allowed!");
285
286 SetAddonStruct(instance);
287 }
288 //----------------------------------------------------------------------------
289
290 //============================================================================
291 /// @ingroup cpp_kodi_addon_peripheral
292 /// @brief Destructor.
293 ///
294 ~CInstancePeripheral() override = default;
295 //----------------------------------------------------------------------------
296
297 //============================================================================
298 /// @defgroup cpp_kodi_addon_peripheral_peripheralOp 1. Peripheral operations
299 /// @ingroup cpp_kodi_addon_peripheral
300 /// @brief %Peripheral operations to handle control about.
301 ///
302 ///---------------------------------------------------------------------------
303 ///
304 /// **%Peripheral parts in interface:**\n
305 /// Copy this to your project and extend with your parts or leave functions
306 /// complete away where not used or supported.
307 ///
308 /// @copydetails cpp_kodi_addon_peripheral_peripheralOp_header_addon_auto_check
309 /// @copydetails cpp_kodi_addon_peripheral_peripheralOp_source_addon_auto_check
310 ///
311 ///@{
312
313 //============================================================================
314 /// @brief Get the list of features that this add-on provides.
315 ///
316 /// Called by the frontend to query the add-on's capabilities and supported
317 /// peripherals. All capabilities that the add-on supports should be set to true.
318 ///
319 /// @param[out] capabilities The add-on's capabilities
320 ///
321 /// @remarks Valid implementation required.
322 ///
323 ///
324 /// ----------------------------------------------------------------------------
325 ///
326 /// @copydetails cpp_kodi_addon_peripheral_Defs_PeripheralCapabilities_Help
327 ///
328 /// --------------------------------------------------------------------------
329 ///
330 /// **Example:**
331 /// ~~~~~~~~~~~~~{.cpp}
332 /// void CMyPeripheralAddon::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
333 /// {
334 /// capabilities.SetProvidesJoysticks(true);
335 /// capabilities.SetProvidesButtonmaps(true);
336 /// }
337 /// ~~~~~~~~~~~~~
338 ///
339 virtual void GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities) {}
340 //----------------------------------------------------------------------------
341
342 //============================================================================
343 /// @brief Perform a scan for joysticks
344 ///
345 /// The frontend calls this when a hardware change is detected. If an add-on
346 /// detects a hardware change, it can trigger this function using the
347 /// @ref TriggerScan() callback.
348 ///
349 /// @param[in] scan_results Assigned to allocated memory
350 /// @return @ref PERIPHERAL_NO_ERROR if successful
351 ///
352 ///
353 /// --------------------------------------------------------------------------
354 ///
355 /// @copydetails cpp_kodi_addon_peripheral_Defs_Peripheral_Peripheral_Help
356 ///
357 virtual PERIPHERAL_ERROR PerformDeviceScan(
358 std::vector<std::shared_ptr<kodi::addon::Peripheral>>& scan_results)
359 {
360 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
361 }
362 //----------------------------------------------------------------------------
363
364 //============================================================================
365 /// @brief Get all events that have occurred since the last call to
366 /// @ref GetEvents().
367 ///
368 /// @param[out] events List of available events within addon
369 /// @return @ref PERIPHERAL_NO_ERROR if successful
370 ///
371 /// ----------------------------------------------------------------------------
372 ///
373 /// @copydetails cpp_kodi_addon_peripheral_Defs_Peripheral_PeripheralEvent_Help
374 ///
375 virtual PERIPHERAL_ERROR GetEvents(std::vector<kodi::addon::PeripheralEvent>& events)
376 {
377 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
378 }
379 //----------------------------------------------------------------------------
380
381 //============================================================================
382 /// @brief Send an input event to the peripheral.
383 ///
384 /// @param[in] event The input event
385 /// @return true if the event was handled, false otherwise
386 ///
387 virtual bool SendEvent(const kodi::addon::PeripheralEvent& event) { return false; }
388 //----------------------------------------------------------------------------
389
390 ///@}
391
392 //============================================================================
393 /// @defgroup cpp_kodi_addon_peripheral_joystickOp 2. Joystick operations
394 /// @ingroup cpp_kodi_addon_peripheral
395 /// @brief %Joystick operations to handle control about.
396 ///
397 ///
398 ///---------------------------------------------------------------------------
399 ///
400 /// **%Joystick parts in interface:**\n
401 /// Copy this to your project and extend with your parts or leave functions
402 /// complete away where not used or supported.
403 ///
404 /// @copydetails cpp_kodi_addon_peripheral_joystickOp_header_addon_auto_check
405 /// @copydetails cpp_kodi_addon_peripheral_joystickOp_source_addon_auto_check
406 ///
407 ///@{
408
409 //============================================================================
410 /// @brief Get extended info about an attached joystick.
411 ///
412 /// @param[in] index The joystick's driver index
413 /// @param[out] info The container for the allocated joystick info
414 /// @return @ref PERIPHERAL_NO_ERROR if successful
415 ///
416 ///
417 /// ----------------------------------------------------------------------------
418 ///
419 /// @copydetails cpp_kodi_addon_peripheral_Defs_Joystick_Joystick_Help
420 ///
421 virtual PERIPHERAL_ERROR GetJoystickInfo(unsigned int index, kodi::addon::Joystick& info)
422 {
423 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
424 }
425 //----------------------------------------------------------------------------
426
427 //============================================================================
428 /// @brief Get the features that allow translating the joystick into the
429 /// controller profile.
430 ///
431 /// @param[in] joystick The device's joystick properties; unknown values may
432 /// be left at their default
433 /// @param[in] controller_id The controller profile being requested, e.g.
434 /// `game.controller.default`
435 /// @param[out] features The array of allocated features
436 /// @return @ref PERIPHERAL_NO_ERROR if successful
437 ///
438 virtual PERIPHERAL_ERROR GetFeatures(const kodi::addon::Joystick& joystick,
439 const std::string& controller_id,
440 std::vector<kodi::addon::JoystickFeature>& features)
441 {
442 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
443 }
444 //----------------------------------------------------------------------------
445
446 //============================================================================
447 /// @brief Add or update joystick features.
448 ///
449 /// @param[in] joystick The device's joystick properties; unknown values may be
450 /// left at their default
451 /// @param[in] controller_id The game controller profile being updated
452 /// @param[in] features The array of features
453 /// @return @ref PERIPHERAL_NO_ERROR if successful
454 ///
455 virtual PERIPHERAL_ERROR MapFeatures(const kodi::addon::Joystick& joystick,
456 const std::string& controller_id,
457 const std::vector<kodi::addon::JoystickFeature>& features)
458 {
459 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
460 }
461 //----------------------------------------------------------------------------
462
463 //============================================================================
464 /// @brief Get the driver primitives that should be ignored while mapping the
465 /// device.
466 ///
467 /// @param[in] joystick The device's joystick properties; unknown values may
468 /// be left at their default
469 /// @param[out] primitives The array of allocated driver primitives to be
470 /// ignored
471 /// @return @ref PERIPHERAL_NO_ERROR if successful
472 ///
473 virtual PERIPHERAL_ERROR GetIgnoredPrimitives(
474 const kodi::addon::Joystick& joystick, std::vector<kodi::addon::DriverPrimitive>& primitives)
475 {
476 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
477 }
478 //----------------------------------------------------------------------------
479
480 //============================================================================
481 /// @brief Set the list of driver primitives that are ignored for the device.
482 ///
483 /// @param[in] joystick The device's joystick properties; unknown values may be left at their default
484 /// @param[in] primitives The array of driver primitives to ignore
485 /// @return @ref PERIPHERAL_NO_ERROR if successful
486 ///
487 virtual PERIPHERAL_ERROR SetIgnoredPrimitives(
488 const kodi::addon::Joystick& joystick,
489 const std::vector<kodi::addon::DriverPrimitive>& primitives)
490 {
491 return PERIPHERAL_ERROR_NOT_IMPLEMENTED;
492 }
493 //----------------------------------------------------------------------------
494
495 //============================================================================
496 /// @brief Save the button map for the given joystick.
497 ///
498 /// @param[in] joystick The device's joystick properties
499 ///
500 virtual void SaveButtonMap(const kodi::addon::Joystick& joystick) {}
501 //----------------------------------------------------------------------------
502
503 //============================================================================
504 /// @brief Revert the button map to the last time it was loaded or committed to disk
505 /// @param[in] joystick The device's joystick properties
506 ///
507 virtual void RevertButtonMap(const kodi::addon::Joystick& joystick) {}
508 //----------------------------------------------------------------------------
509
510 //============================================================================
511 /// @brief Reset the button map for the given joystick and controller profile ID
512 /// @param[in] joystick The device's joystick properties
513 /// @param[in] controller_id The game controller profile being reset
514 ///
515 virtual void ResetButtonMap(const kodi::addon::Joystick& joystick,
516 const std::string& controller_id)
517 {
518 }
519 //----------------------------------------------------------------------------
520
521 //============================================================================
522 /// @brief Powers off the given joystick if supported
523 /// @param[in] index The joystick's driver index
524 ///
525 virtual void PowerOffJoystick(unsigned int index) {}
526 //----------------------------------------------------------------------------
527
528 ///@}
529
530 //============================================================================
531 /// @defgroup cpp_kodi_addon_peripheral_callbacks 3. Callback functions
532 /// @ingroup cpp_kodi_addon_peripheral
533 /// @brief Callback to Kodi functions.
534 ///
535 ///@{
536
537 //============================================================================
538 /// @brief Used to get the full path where the add-on is installed.
539 ///
540 /// @return The add-on installation path
541 ///
542 const std::string AddonPath() const { return m_instanceData->props->addon_path; }
543 //----------------------------------------------------------------------------
544
545 //============================================================================
546 /// @brief Used to get the full path to the add-on's user profile.
547 ///
548 /// @note The trailing folder (consisting of the add-on's ID) is not created
549 /// by default. If it is needed, you must call kodi::vfs::CreateDirectory()
550 /// to create the folder.
551 ///
552 /// @return Path to the user profile
553 ///
554 const std::string UserPath() const { return m_instanceData->props->user_path; }
555 //----------------------------------------------------------------------------
556
557 //============================================================================
558 /// @brief Trigger a scan for peripherals
559 ///
560 /// The add-on calls this if a change in hardware is detected.
561 ///
562 void TriggerScan(void)
563 {
564 return m_instanceData->toKodi->trigger_scan(m_instanceData->toKodi->kodiInstance);
565 }
566 //----------------------------------------------------------------------------
567
568 //============================================================================
569 /// @brief Notify the frontend that button maps have changed.
570 ///
571 /// @param[in] deviceName [optional] The name of the device to refresh, or
572 /// empty/null for all devices
573 /// @param[in] controllerId [optional] The controller ID to refresh, or
574 /// empty/null for all controllers
575 ///
576 void RefreshButtonMaps(const std::string& deviceName = "", const std::string& controllerId = "")
577 {
578 return m_instanceData->toKodi->refresh_button_maps(m_instanceData->toKodi->kodiInstance,
579 deviceName.c_str(), controllerId.c_str());
580 }
581 //----------------------------------------------------------------------------
582
583 //============================================================================
584 /// @brief Return the number of features belonging to the specified
585 /// controller.
586 ///
587 /// @param[in] controllerId The controller ID to enumerate
588 /// @param[in] type [optional] Type to filter by, or @ref JOYSTICK_FEATURE_TYPE_UNKNOWN
589 /// for all features
590 /// @return The number of features matching the request parameters
591 ///
592 unsigned int FeatureCount(const std::string& controllerId,
593 JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN)
594 {
595 return m_instanceData->toKodi->feature_count(m_instanceData->toKodi->kodiInstance,
596 controllerId.c_str(), type);
597 }
598 //----------------------------------------------------------------------------
599
600 //============================================================================
601 /// @brief Return the type of the feature.
602 ///
603 /// @param[in] controllerId The controller ID to check
604 /// @param[in] featureName The feature to check
605 /// @return The type of the specified feature, or @ref JOYSTICK_FEATURE_TYPE_UNKNOWN
606 /// if unknown
607 ///
608 JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId, const std::string& featureName)
609 {
610 return m_instanceData->toKodi->feature_type(m_instanceData->toKodi->kodiInstance,
611 controllerId.c_str(), featureName.c_str());
612 }
613 //----------------------------------------------------------------------------
614
615 ///@}
616
617private:
618 void SetAddonStruct(KODI_HANDLE instance)
619 {
620 if (instance == nullptr)
621 throw std::logic_error("kodi::addon::CInstancePeripheral: Creation with empty addon "
622 "structure not allowed, table must be given from Kodi!");
623
624 m_instanceData = static_cast<AddonInstance_Peripheral*>(instance);
625 m_instanceData->toAddon->addonInstance = this;
626
627 m_instanceData->toAddon->get_capabilities = ADDON_GetCapabilities;
628 m_instanceData->toAddon->perform_device_scan = ADDON_PerformDeviceScan;
629 m_instanceData->toAddon->free_scan_results = ADDON_FreeScanResults;
630 m_instanceData->toAddon->get_events = ADDON_GetEvents;
631 m_instanceData->toAddon->free_events = ADDON_FreeEvents;
632 m_instanceData->toAddon->send_event = ADDON_SendEvent;
633
634 m_instanceData->toAddon->get_joystick_info = ADDON_GetJoystickInfo;
635 m_instanceData->toAddon->free_joystick_info = ADDON_FreeJoystickInfo;
636 m_instanceData->toAddon->get_features = ADDON_GetFeatures;
637 m_instanceData->toAddon->free_features = ADDON_FreeFeatures;
638 m_instanceData->toAddon->map_features = ADDON_MapFeatures;
639 m_instanceData->toAddon->get_ignored_primitives = ADDON_GetIgnoredPrimitives;
640 m_instanceData->toAddon->free_primitives = ADDON_FreePrimitives;
641 m_instanceData->toAddon->set_ignored_primitives = ADDON_SetIgnoredPrimitives;
642 m_instanceData->toAddon->save_button_map = ADDON_SaveButtonMap;
643 m_instanceData->toAddon->revert_button_map = ADDON_RevertButtonMap;
644 m_instanceData->toAddon->reset_button_map = ADDON_ResetButtonMap;
645 m_instanceData->toAddon->power_off_joystick = ADDON_PowerOffJoystick;
646 }
647
648 inline static void ADDON_GetCapabilities(const AddonInstance_Peripheral* addonInstance,
649 PERIPHERAL_CAPABILITIES* capabilities)
650 {
651 if (!addonInstance || !capabilities)
652 return;
653
654 kodi::addon::PeripheralCapabilities peripheralCapabilities(capabilities);
655 static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
656 ->GetCapabilities(peripheralCapabilities);
657 }
658
659 inline static PERIPHERAL_ERROR ADDON_PerformDeviceScan(
660 const AddonInstance_Peripheral* addonInstance,
661 unsigned int* peripheral_count,
662 PERIPHERAL_INFO** scan_results)
663 {
664 if (!addonInstance || !peripheral_count || !scan_results)
665 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
666
667 std::vector<std::shared_ptr<kodi::addon::Peripheral>> peripherals;
668 PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
669 ->PerformDeviceScan(peripherals);
670 if (err == PERIPHERAL_NO_ERROR)
671 {
672 *peripheral_count = static_cast<unsigned int>(peripherals.size());
673 kodi::addon::Peripherals::ToStructs(peripherals, scan_results);
674 }
675
676 return err;
677 }
678
679 inline static void ADDON_FreeScanResults(const AddonInstance_Peripheral* addonInstance,
680 unsigned int peripheral_count,
681 PERIPHERAL_INFO* scan_results)
682 {
683 if (!addonInstance)
684 return;
685
686 kodi::addon::Peripherals::FreeStructs(peripheral_count, scan_results);
687 }
688
689 inline static PERIPHERAL_ERROR ADDON_GetEvents(const AddonInstance_Peripheral* addonInstance,
690 unsigned int* event_count,
691 PERIPHERAL_EVENT** events)
692 {
693 if (!addonInstance || !event_count || !events)
694 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
695
696 std::vector<kodi::addon::PeripheralEvent> peripheralEvents;
697 PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
698 ->GetEvents(peripheralEvents);
699 if (err == PERIPHERAL_NO_ERROR)
700 {
701 *event_count = static_cast<unsigned int>(peripheralEvents.size());
702 kodi::addon::PeripheralEvents::ToStructs(peripheralEvents, events);
703 }
704
705 return err;
706 }
707
708 inline static void ADDON_FreeEvents(const AddonInstance_Peripheral* addonInstance,
709 unsigned int event_count,
710 PERIPHERAL_EVENT* events)
711 {
712 if (!addonInstance)
713 return;
714
715 kodi::addon::PeripheralEvents::FreeStructs(event_count, events);
716 }
717
718 inline static bool ADDON_SendEvent(const AddonInstance_Peripheral* addonInstance,
719 const PERIPHERAL_EVENT* event)
720 {
721 if (!addonInstance || !event)
722 return false;
723 return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
724 ->SendEvent(kodi::addon::PeripheralEvent(*event));
725 }
726
727
728 inline static PERIPHERAL_ERROR ADDON_GetJoystickInfo(
729 const AddonInstance_Peripheral* addonInstance, unsigned int index, JOYSTICK_INFO* info)
730 {
731 if (!addonInstance || !info)
732 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
733
734 kodi::addon::Joystick addonInfo;
735 PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
736 ->GetJoystickInfo(index, addonInfo);
737 if (err == PERIPHERAL_NO_ERROR)
738 {
739 addonInfo.ToStruct(*info);
740 }
741
742 return err;
743 }
744
745 inline static void ADDON_FreeJoystickInfo(const AddonInstance_Peripheral* addonInstance,
746 JOYSTICK_INFO* info)
747 {
748 if (!addonInstance)
749 return;
750
751 kodi::addon::Joystick::FreeStruct(*info);
752 }
753
754 inline static PERIPHERAL_ERROR ADDON_GetFeatures(const AddonInstance_Peripheral* addonInstance,
755 const JOYSTICK_INFO* joystick,
756 const char* controller_id,
757 unsigned int* feature_count,
758 JOYSTICK_FEATURE** features)
759 {
760 if (!addonInstance || !joystick || !controller_id || !feature_count || !features)
761 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
762
763 kodi::addon::Joystick addonJoystick(*joystick);
764 std::vector<kodi::addon::JoystickFeature> featuresVector;
765
766 PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
767 ->GetFeatures(addonJoystick, controller_id, featuresVector);
768 if (err == PERIPHERAL_NO_ERROR)
769 {
770 *feature_count = static_cast<unsigned int>(featuresVector.size());
771 kodi::addon::JoystickFeatures::ToStructs(featuresVector, features);
772 }
773
774 return err;
775 }
776
777 inline static void ADDON_FreeFeatures(const AddonInstance_Peripheral* addonInstance,
778 unsigned int feature_count,
779 JOYSTICK_FEATURE* features)
780 {
781 if (!addonInstance)
782 return;
783
784 kodi::addon::JoystickFeatures::FreeStructs(feature_count, features);
785 }
786
787 inline static PERIPHERAL_ERROR ADDON_MapFeatures(const AddonInstance_Peripheral* addonInstance,
788 const JOYSTICK_INFO* joystick,
789 const char* controller_id,
790 unsigned int feature_count,
791 const JOYSTICK_FEATURE* features)
792 {
793 if (!addonInstance || !joystick || !controller_id || (feature_count > 0 && !features))
794 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
795
796 kodi::addon::Joystick addonJoystick(*joystick);
797 std::vector<kodi::addon::JoystickFeature> primitiveVector;
798
799 for (unsigned int i = 0; i < feature_count; i++)
800 primitiveVector.emplace_back(*(features + i));
801
802 return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
803 ->MapFeatures(addonJoystick, controller_id, primitiveVector);
804 }
805
806 inline static PERIPHERAL_ERROR ADDON_GetIgnoredPrimitives(
807 const AddonInstance_Peripheral* addonInstance,
808 const JOYSTICK_INFO* joystick,
809 unsigned int* primitive_count,
810 JOYSTICK_DRIVER_PRIMITIVE** primitives)
811 {
812 if (!addonInstance || !joystick || !primitive_count || !primitives)
813 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
814
815 kodi::addon::Joystick addonJoystick(*joystick);
816 std::vector<kodi::addon::DriverPrimitive> primitiveVector;
817
818 PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
819 ->GetIgnoredPrimitives(addonJoystick, primitiveVector);
820 if (err == PERIPHERAL_NO_ERROR)
821 {
822 *primitive_count = static_cast<unsigned int>(primitiveVector.size());
823 kodi::addon::DriverPrimitives::ToStructs(primitiveVector, primitives);
824 }
825
826 return err;
827 }
828
829 inline static void ADDON_FreePrimitives(const AddonInstance_Peripheral* addonInstance,
830 unsigned int primitive_count,
831 JOYSTICK_DRIVER_PRIMITIVE* primitives)
832 {
833 if (!addonInstance)
834 return;
835
836 kodi::addon::DriverPrimitives::FreeStructs(primitive_count, primitives);
837 }
838
839 inline static PERIPHERAL_ERROR ADDON_SetIgnoredPrimitives(
840 const AddonInstance_Peripheral* addonInstance,
841 const JOYSTICK_INFO* joystick,
842 unsigned int primitive_count,
843 const JOYSTICK_DRIVER_PRIMITIVE* primitives)
844 {
845 if (!addonInstance || !joystick || (primitive_count > 0 && !primitives))
846 return PERIPHERAL_ERROR_INVALID_PARAMETERS;
847
848 kodi::addon::Joystick addonJoystick(*joystick);
849 std::vector<kodi::addon::DriverPrimitive> primitiveVector;
850
851 for (unsigned int i = 0; i < primitive_count; i++)
852 primitiveVector.emplace_back(*(primitives + i));
853
854 return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
855 ->SetIgnoredPrimitives(addonJoystick, primitiveVector);
856 }
857
858 inline static void ADDON_SaveButtonMap(const AddonInstance_Peripheral* addonInstance,
859 const JOYSTICK_INFO* joystick)
860 {
861 if (!addonInstance || !joystick)
862 return;
863
864 kodi::addon::Joystick addonJoystick(*joystick);
865 static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
866 ->SaveButtonMap(addonJoystick);
867 }
868
869 inline static void ADDON_RevertButtonMap(const AddonInstance_Peripheral* addonInstance,
870 const JOYSTICK_INFO* joystick)
871 {
872 if (!addonInstance || !joystick)
873 return;
874
875 kodi::addon::Joystick addonJoystick(*joystick);
876 static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
877 ->RevertButtonMap(addonJoystick);
878 }
879
880 inline static void ADDON_ResetButtonMap(const AddonInstance_Peripheral* addonInstance,
881 const JOYSTICK_INFO* joystick,
882 const char* controller_id)
883 {
884 if (!addonInstance || !joystick || !controller_id)
885 return;
886
887 kodi::addon::Joystick addonJoystick(*joystick);
888 static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
889 ->ResetButtonMap(addonJoystick, controller_id);
890 }
891
892 inline static void ADDON_PowerOffJoystick(const AddonInstance_Peripheral* addonInstance,
893 unsigned int index)
894 {
895 if (!addonInstance)
896 return;
897
898 static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance)
899 ->PowerOffJoystick(index);
900 }
901
902 AddonInstance_Peripheral* m_instanceData;
903};
904
905} /* namespace addon */
906} /* namespace kodi */
907#endif /* __cplusplus */