kodi
PeripheralAddon.h
1 /*
2  * Copyright (C) 2014-2024 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 "addons/binary-addons/AddonInstanceHandler.h"
12 #include "addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h"
13 #include "input/joysticks/JoystickTypes.h"
14 #include "peripherals/PeripheralTypes.h"
15 #include "threads/CriticalSection.h"
16 #include "threads/SharedSection.h"
17 
18 #include <map>
19 #include <memory>
20 #include <vector>
21 
22 class CFileItemList;
23 
24 namespace KODI
25 {
26 namespace JOYSTICK
27 {
28 class IButtonMap;
29 class IDriverHandler;
30 } // namespace JOYSTICK
31 } // namespace KODI
32 
33 namespace PERIPHERALS
34 {
35 class CPeripheral;
36 class CPeripheralJoystick;
37 class CPeripherals;
38 
39 typedef std::vector<kodi::addon::DriverPrimitive> PrimitiveVector;
40 typedef std::map<KODI::JOYSTICK::FeatureName, kodi::addon::JoystickFeature> FeatureMap;
41 
46 {
47 public:
48  explicit CPeripheralAddon(const ADDON::AddonInfoPtr& addonInfo, CPeripherals& manager);
49  ~CPeripheralAddon(void) override;
50 
54  bool CreateAddon(void);
55 
59  void DestroyAddon();
60 
61  bool Register(unsigned int peripheralIndex, const PeripheralPtr& peripheral);
62  void UnregisterRemovedDevices(const PeripheralScanResults& results,
63  PeripheralVector& removedPeripherals);
64  void GetFeatures(std::vector<PeripheralFeature>& features) const;
65  bool HasFeature(const PeripheralFeature feature) const;
66  PeripheralPtr GetPeripheral(unsigned int index) const;
67  PeripheralPtr GetByPath(const std::string& strPath) const;
68  bool SupportsFeature(PeripheralFeature feature) const;
69  unsigned int GetPeripheralsWithFeature(PeripheralVector& results,
70  const PeripheralFeature feature) const;
71  unsigned int GetNumberOfPeripherals(void) const;
72  unsigned int GetNumberOfPeripheralsWithId(const int iVendorId, const int iProductId) const;
73  void GetDirectory(const std::string& strPath, CFileItemList& items) const;
74 
77  bool PerformDeviceScan(PeripheralScanResults& results);
78  bool ProcessEvents(void);
79  bool SendRumbleEvent(unsigned int index, unsigned int driverIndex, float magnitude);
81 
84  bool GetJoystickProperties(unsigned int index, CPeripheralJoystick& joystick);
85  bool HasButtonMaps(void) const { return m_bProvidesButtonMaps; }
86  bool GetAppearance(const CPeripheral* device, std::string& controllerId);
87  bool SetAppearance(const CPeripheral* device, const std::string& controllerId);
88  bool GetFeatures(const CPeripheral* device,
89  const std::string& strControllerId,
90  FeatureMap& features);
91  bool MapFeature(const CPeripheral* device,
92  const std::string& strControllerId,
93  const kodi::addon::JoystickFeature& feature);
94  bool GetIgnoredPrimitives(const CPeripheral* device, PrimitiveVector& primitives);
95  bool SetIgnoredPrimitives(const CPeripheral* device, const PrimitiveVector& primitives);
96  void SaveButtonMap(const CPeripheral* device);
97  void RevertButtonMap(const CPeripheral* device);
98  void ResetButtonMap(const CPeripheral* device, const std::string& strControllerId);
99  void PowerOffJoystick(unsigned int index);
101 
102  void RegisterButtonMap(CPeripheral* device, KODI::JOYSTICK::IButtonMap* buttonMap);
103  void UnregisterButtonMap(KODI::JOYSTICK::IButtonMap* buttonMap);
104 
105  static bool ProvidesJoysticks(const ADDON::AddonInfoPtr& addonInfo);
106  static bool ProvidesButtonMaps(const ADDON::AddonInfoPtr& addonInfo);
107 
108 private:
109  void UnregisterButtonMap(CPeripheral* device);
110 
111  // Binary add-on callbacks
112  void TriggerDeviceScan();
113  void RefreshButtonMaps(const std::string& strDeviceName = "");
114  unsigned int FeatureCount(const std::string& controllerId, JOYSTICK_FEATURE_TYPE type) const;
115  JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId,
116  const std::string& featureName) const;
117 
121  static void GetPeripheralInfo(const CPeripheral* device, kodi::addon::Peripheral& peripheralInfo);
122 
123  static void GetJoystickInfo(const CPeripheral* device, kodi::addon::Joystick& joystickInfo);
124  static void SetJoystickInfo(CPeripheralJoystick& joystick,
125  const kodi::addon::Joystick& joystickInfo);
126 
130  void ResetProperties(void);
131 
135  bool GetAddonProperties(void);
136 
137  bool LogError(const PERIPHERAL_ERROR error, const char* strMethod) const;
138 
139  static std::string GetDeviceName(PeripheralType type);
140  static std::string GetProvider(PeripheralType type);
141 
142  // Construction parameters
143  CPeripherals& m_manager;
144 
145  /* @brief Cache for const char* members in PERIPHERAL_PROPERTIES */
146  std::string m_strUserPath;
147  std::string m_strClientPath;
153  static void cb_trigger_scan(void* kodiInstance);
154  static void cb_refresh_button_maps(void* kodiInstance,
155  const char* deviceName,
156  const char* controllerId);
157  static unsigned int cb_feature_count(void* kodiInstance,
158  const char* controllerId,
159  JOYSTICK_FEATURE_TYPE type);
160  static JOYSTICK_FEATURE_TYPE cb_feature_type(void* kodiInstance,
161  const char* controllerId,
162  const char* featureName);
164 
165  /* @brief Add-on properties */
166  bool m_bProvidesJoysticks;
167  bool m_bSupportsJoystickRumble = false;
168  bool m_bSupportsJoystickPowerOff = false;
169  bool m_bProvidesButtonMaps;
170 
171  /* @brief Map of peripherals belonging to the add-on */
172  std::map<unsigned int, PeripheralPtr> m_peripherals;
173 
174  /* @brief Button map observers */
175  std::vector<std::pair<CPeripheral*, KODI::JOYSTICK::IButtonMap*>> m_buttonMaps;
176  CCriticalSection m_buttonMapMutex;
177 
178  /* @brief Thread synchronization */
179  mutable CCriticalSection m_critSection;
180 
181  CSharedSection m_dllSection;
182 };
183 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:356
Definition: RetroPlayerInput.h:15
Represents a list of files.
Definition: FileItem.h:702
PERIPHERAL_ERROR
Definition: peripheral.h:35
Button map interface to translate between the driver&#39;s raw button/hat/axis elements and physical joys...
Definition: IButtonMap.h:29
Definition: PeripheralAddon.h:45
JOYSTICK_FEATURE_TYPE
Definition: peripheral.h:496
Definition: AddonInstanceHandler.h:33
Definition: PeripheralJoystick.h:48
Definition: AudioDecoder.h:18
A CSharedSection is a mutex that satisfies the Shared Lockable concept (see Lockables.h).
Definition: SharedSection.h:19
Definition: Peripheral.h:71
Definition: Peripherals.h:56