Kodi Development  20.0
for Binary and Script based Add-Ons
Peripheral.h
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
15 namespace kodi
16 {
17 namespace addon
18 {
19 
20 //##############################################################################
28 
29 //##############################################################################
39 
40 //##############################################################################
46 
47 //##############################################################################
53 
54 //##############################################################################
60 
61 //==============================================================================
215 class ATTRIBUTE_HIDDEN CInstancePeripheral : public IAddonInstance
216 {
217 public:
218  //============================================================================
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  //============================================================================
277  explicit CInstancePeripheral(KODI_HANDLE instance, const std::string& kodiVersion = "")
279  !kodiVersion.empty() ? kodiVersion
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  //============================================================================
294  ~CInstancePeripheral() override = default;
295  //----------------------------------------------------------------------------
296 
297  //============================================================================
312 
313  //============================================================================
340  //----------------------------------------------------------------------------
341 
342  //============================================================================
358  std::vector<std::shared_ptr<kodi::addon::Peripheral>>& scan_results)
359  {
361  }
362  //----------------------------------------------------------------------------
363 
364  //============================================================================
375  virtual PERIPHERAL_ERROR GetEvents(std::vector<kodi::addon::PeripheralEvent>& events)
376  {
378  }
379  //----------------------------------------------------------------------------
380 
381  //============================================================================
387  virtual bool SendEvent(const kodi::addon::PeripheralEvent& event) { return false; }
388  //----------------------------------------------------------------------------
389 
391 
392  //============================================================================
408 
409  //============================================================================
421  virtual PERIPHERAL_ERROR GetJoystickInfo(unsigned int index, kodi::addon::Joystick& info)
422  {
424  }
425  //----------------------------------------------------------------------------
426 
427  //============================================================================
439  const std::string& controller_id,
440  std::vector<kodi::addon::JoystickFeature>& features)
441  {
443  }
444  //----------------------------------------------------------------------------
445 
446  //============================================================================
456  const std::string& controller_id,
457  const std::vector<kodi::addon::JoystickFeature>& features)
458  {
460  }
461  //----------------------------------------------------------------------------
462 
463  //============================================================================
474  const kodi::addon::Joystick& joystick, std::vector<kodi::addon::DriverPrimitive>& primitives)
475  {
477  }
478  //----------------------------------------------------------------------------
479 
480  //============================================================================
488  const kodi::addon::Joystick& joystick,
489  const std::vector<kodi::addon::DriverPrimitive>& primitives)
490  {
492  }
493  //----------------------------------------------------------------------------
494 
495  //============================================================================
500  virtual void SaveButtonMap(const kodi::addon::Joystick& joystick) {}
501  //----------------------------------------------------------------------------
502 
503  //============================================================================
507  virtual void RevertButtonMap(const kodi::addon::Joystick& joystick) {}
508  //----------------------------------------------------------------------------
509 
510  //============================================================================
515  virtual void ResetButtonMap(const kodi::addon::Joystick& joystick,
516  const std::string& controller_id)
517  {
518  }
519  //----------------------------------------------------------------------------
520 
521  //============================================================================
525  virtual void PowerOffJoystick(unsigned int index) {}
526  //----------------------------------------------------------------------------
527 
529 
530  //============================================================================
536 
537  //============================================================================
542  const std::string AddonPath() const { return m_instanceData->props->addon_path; }
543  //----------------------------------------------------------------------------
544 
545  //============================================================================
554  const std::string UserPath() const { return m_instanceData->props->user_path; }
555  //----------------------------------------------------------------------------
556 
557  //============================================================================
562  void TriggerScan(void)
563  {
564  return m_instanceData->toKodi->trigger_scan(m_instanceData->toKodi->kodiInstance);
565  }
566  //----------------------------------------------------------------------------
567 
568  //============================================================================
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  //============================================================================
592  unsigned int FeatureCount(const std::string& controllerId,
594  {
595  return m_instanceData->toKodi->feature_count(m_instanceData->toKodi->kodiInstance,
596  controllerId.c_str(), type);
597  }
598  //----------------------------------------------------------------------------
599 
600  //============================================================================
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 
616 
617 private:
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)
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)
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)
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)
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))
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)
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))
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 */
Definition: Peripheral.h:216
Definition: AddonBase.h:186
Definition: PeripheralUtils.h:569
Definition: PeripheralUtils.h:133
Definition: PeripheralUtils.h:369
@ ADDON_INSTANCE_PERIPHERAL
Peripheral instance, see kodi::addon::CInstancePeripheral.
Definition: versions.h:229
PERIPHERAL_ERROR
Definition: peripheral.h:36
@ PERIPHERAL_ERROR_INVALID_PARAMETERS
-3 : The parameters of the method are invalid for this operation
Definition: peripheral.h:47
@ PERIPHERAL_NO_ERROR
0 : No error occurred
Definition: peripheral.h:38
@ PERIPHERAL_ERROR_NOT_IMPLEMENTED
-4 : The method that the frontend called is not implemented
Definition: peripheral.h:50
JOYSTICK_FEATURE_TYPE
Definition: peripheral.h:494
@ JOYSTICK_FEATURE_TYPE_UNKNOWN
Unknown type.
Definition: peripheral.h:496
unsigned int FeatureCount(const std::string &controllerId, JOYSTICK_FEATURE_TYPE type=JOYSTICK_FEATURE_TYPE_UNKNOWN)
Return the number of features belonging to the specified controller.
Definition: Peripheral.h:592
void RefreshButtonMaps(const std::string &deviceName="", const std::string &controllerId="")
Notify the frontend that button maps have changed.
Definition: Peripheral.h:576
void TriggerScan(void)
Trigger a scan for peripherals.
Definition: Peripheral.h:562
JOYSTICK_FEATURE_TYPE FeatureType(const std::string &controllerId, const std::string &featureName)
Return the type of the feature.
Definition: Peripheral.h:608
const std::string UserPath() const
Used to get the full path to the add-on's user profile.
Definition: Peripheral.h:554
const std::string AddonPath() const
Used to get the full path where the add-on is installed.
Definition: Peripheral.h:542
virtual void RevertButtonMap(const kodi::addon::Joystick &joystick)
Revert the button map to the last time it was loaded or committed to disk.
Definition: Peripheral.h:507
virtual PERIPHERAL_ERROR MapFeatures(const kodi::addon::Joystick &joystick, const std::string &controller_id, const std::vector< kodi::addon::JoystickFeature > &features)
Add or update joystick features.
Definition: Peripheral.h:455
virtual PERIPHERAL_ERROR SetIgnoredPrimitives(const kodi::addon::Joystick &joystick, const std::vector< kodi::addon::DriverPrimitive > &primitives)
Set the list of driver primitives that are ignored for the device.
Definition: Peripheral.h:487
virtual void SaveButtonMap(const kodi::addon::Joystick &joystick)
Save the button map for the given joystick.
Definition: Peripheral.h:500
virtual void PowerOffJoystick(unsigned int index)
Powers off the given joystick if supported.
Definition: Peripheral.h:525
virtual void ResetButtonMap(const kodi::addon::Joystick &joystick, const std::string &controller_id)
Reset the button map for the given joystick and controller profile ID.
Definition: Peripheral.h:515
virtual PERIPHERAL_ERROR GetFeatures(const kodi::addon::Joystick &joystick, const std::string &controller_id, std::vector< kodi::addon::JoystickFeature > &features)
Get the features that allow translating the joystick into the controller profile.
Definition: Peripheral.h:438
virtual PERIPHERAL_ERROR GetIgnoredPrimitives(const kodi::addon::Joystick &joystick, std::vector< kodi::addon::DriverPrimitive > &primitives)
Get the driver primitives that should be ignored while mapping the device.
Definition: Peripheral.h:473
virtual PERIPHERAL_ERROR GetJoystickInfo(unsigned int index, kodi::addon::Joystick &info)
Get extended info about an attached joystick.
Definition: Peripheral.h:421
virtual bool SendEvent(const kodi::addon::PeripheralEvent &event)
Send an input event to the peripheral.
Definition: Peripheral.h:387
virtual PERIPHERAL_ERROR GetEvents(std::vector< kodi::addon::PeripheralEvent > &events)
Get all events that have occurred since the last call to GetEvents().
Definition: Peripheral.h:375
virtual void GetCapabilities(kodi::addon::PeripheralCapabilities &capabilities)
Get the list of features that this add-on provides.
Definition: Peripheral.h:339
virtual PERIPHERAL_ERROR PerformDeviceScan(std::vector< std::shared_ptr< kodi::addon::Peripheral >> &scan_results)
Perform a scan for joysticks.
Definition: Peripheral.h:357
CInstancePeripheral()
Peripheral class constructor.
Definition: Peripheral.h:224
CInstancePeripheral(KODI_HANDLE instance, const std::string &kodiVersion="")
Peripheral addon class constructor used to support multiple instance types.
Definition: Peripheral.h:277
~CInstancePeripheral() override=default
Destructor.
std::string ATTRIBUTE_HIDDEN GetKodiTypeVersion(int type)
To get used version inside Kodi itself about asked type.
Definition: AddonBase.h:630
Definition: peripheral.h:697
Driver primitive struct.
Definition: peripheral.h:469
Mapping between higher-level controller feature and its driver primitives.
Definition: peripheral.h:594
Info specific to joystick peripherals.
Definition: peripheral.h:239
Peripheral add-on capabilities.
Definition: peripheral.h:100
Event information.
Definition: peripheral.h:214
Information shared between peripherals.
Definition: peripheral.h:88