xbmc
PeripheralAddon.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 "addons/binary-addons/AddonInstanceHandler.h"
12 #include "addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.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 
43 {
44 public:
45  explicit CPeripheralAddon(const ADDON::AddonInfoPtr& addonInfo, CPeripherals& manager);
46  ~CPeripheralAddon(void) override;
47 
51  bool CreateAddon(void);
52 
56  void DestroyAddon();
57 
58  bool Register(unsigned int peripheralIndex, const PeripheralPtr& peripheral);
59  void UnregisterRemovedDevices(const PeripheralScanResults& results,
60  PeripheralVector& removedPeripherals);
61  void GetFeatures(std::vector<PeripheralFeature>& features) const;
62  bool HasFeature(const PeripheralFeature feature) const;
63  PeripheralPtr GetPeripheral(unsigned int index) const;
64  PeripheralPtr GetByPath(const std::string& strPath) const;
65  bool SupportsFeature(PeripheralFeature feature) const;
66  unsigned int GetPeripheralsWithFeature(PeripheralVector& results,
67  const PeripheralFeature feature) const;
68  unsigned int GetNumberOfPeripherals(void) const;
69  unsigned int GetNumberOfPeripheralsWithId(const int iVendorId, const int iProductId) const;
70  void GetDirectory(const std::string& strPath, CFileItemList& items) const;
71 
74  bool PerformDeviceScan(PeripheralScanResults& results);
75  bool ProcessEvents(void);
76  bool SendRumbleEvent(unsigned int index, unsigned int driverIndex, float magnitude);
78 
81  bool GetJoystickProperties(unsigned int index, CPeripheralJoystick& joystick);
82  bool HasButtonMaps(void) const { return m_bProvidesButtonMaps; }
83  bool GetAppearance(const CPeripheral* device, std::string& controllerId);
84  bool SetAppearance(const CPeripheral* device, const std::string& controllerId);
85  bool GetFeatures(const CPeripheral* device,
86  const std::string& strControllerId,
87  FeatureMap& features);
88  bool MapFeature(const CPeripheral* device,
89  const std::string& strControllerId,
90  const kodi::addon::JoystickFeature& feature);
91  bool GetIgnoredPrimitives(const CPeripheral* device, PrimitiveVector& primitives);
92  bool SetIgnoredPrimitives(const CPeripheral* device, const PrimitiveVector& primitives);
93  void SaveButtonMap(const CPeripheral* device);
94  void RevertButtonMap(const CPeripheral* device);
95  void ResetButtonMap(const CPeripheral* device, const std::string& strControllerId);
96  void PowerOffJoystick(unsigned int index);
98 
99  void RegisterButtonMap(CPeripheral* device, KODI::JOYSTICK::IButtonMap* buttonMap);
100  void UnregisterButtonMap(KODI::JOYSTICK::IButtonMap* buttonMap);
101 
102  static bool ProvidesJoysticks(const ADDON::AddonInfoPtr& addonInfo);
103  static bool ProvidesButtonMaps(const ADDON::AddonInfoPtr& addonInfo);
104 
105 private:
106  void UnregisterButtonMap(CPeripheral* device);
107 
108  // Binary add-on callbacks
109  void TriggerDeviceScan();
110  void RefreshButtonMaps(const std::string& strDeviceName = "");
111  unsigned int FeatureCount(const std::string& controllerId, JOYSTICK_FEATURE_TYPE type) const;
112  JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId,
113  const std::string& featureName) const;
114 
118  static void GetPeripheralInfo(const CPeripheral* device, kodi::addon::Peripheral& peripheralInfo);
119 
120  static void GetJoystickInfo(const CPeripheral* device, kodi::addon::Joystick& joystickInfo);
121  static void SetJoystickInfo(CPeripheralJoystick& joystick,
122  const kodi::addon::Joystick& joystickInfo);
123 
127  void ResetProperties(void);
128 
132  bool GetAddonProperties(void);
133 
134  bool LogError(const PERIPHERAL_ERROR error, const char* strMethod) const;
135 
136  static std::string GetDeviceName(PeripheralType type);
137  static std::string GetProvider(PeripheralType type);
138 
139  // Construction parameters
140  CPeripherals& m_manager;
141 
142  /* @brief Cache for const char* members in PERIPHERAL_PROPERTIES */
143  std::string m_strUserPath;
144  std::string m_strClientPath;
150  static void cb_trigger_scan(void* kodiInstance);
151  static void cb_refresh_button_maps(void* kodiInstance,
152  const char* deviceName,
153  const char* controllerId);
154  static unsigned int cb_feature_count(void* kodiInstance,
155  const char* controllerId,
156  JOYSTICK_FEATURE_TYPE type);
157  static JOYSTICK_FEATURE_TYPE cb_feature_type(void* kodiInstance,
158  const char* controllerId,
159  const char* featureName);
161 
162  /* @brief Add-on properties */
163  bool m_bProvidesJoysticks;
164  bool m_bSupportsJoystickRumble = false;
165  bool m_bSupportsJoystickPowerOff = false;
166  bool m_bProvidesButtonMaps;
167 
168  /* @brief Map of peripherals belonging to the add-on */
169  std::map<unsigned int, PeripheralPtr> m_peripherals;
170 
171  /* @brief Button map observers */
172  std::vector<std::pair<CPeripheral*, KODI::JOYSTICK::IButtonMap*>> m_buttonMaps;
173  CCriticalSection m_buttonMapMutex;
174 
175  /* @brief Thread synchronization */
176  mutable CCriticalSection m_critSection;
177 
178  CSharedSection m_dllSection;
179 };
180 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:348
Definition: RetroPlayerInput.h:15
Represents a list of files.
Definition: FileItem.h:721
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:28
Definition: PeripheralAddon.h:42
JOYSTICK_FEATURE_TYPE
Definition: peripheral.h:496
Definition: AddonInstanceHandler.h:33
Definition: PeripheralJoystick.h:43
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:61
Definition: Peripherals.h:53