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 GetFeatures(const CPeripheral* device,
84  const std::string& strControllerId,
85  FeatureMap& features);
86  bool MapFeature(const CPeripheral* device,
87  const std::string& strControllerId,
88  const kodi::addon::JoystickFeature& feature);
89  bool GetIgnoredPrimitives(const CPeripheral* device, PrimitiveVector& primitives);
90  bool SetIgnoredPrimitives(const CPeripheral* device, const PrimitiveVector& primitives);
91  void SaveButtonMap(const CPeripheral* device);
92  void RevertButtonMap(const CPeripheral* device);
93  void ResetButtonMap(const CPeripheral* device, const std::string& strControllerId);
94  void PowerOffJoystick(unsigned int index);
96 
97  void RegisterButtonMap(CPeripheral* device, KODI::JOYSTICK::IButtonMap* buttonMap);
98  void UnregisterButtonMap(KODI::JOYSTICK::IButtonMap* buttonMap);
99 
100  static bool ProvidesJoysticks(const ADDON::AddonInfoPtr& addonInfo);
101  static bool ProvidesButtonMaps(const ADDON::AddonInfoPtr& addonInfo);
102 
103 private:
104  void UnregisterButtonMap(CPeripheral* device);
105 
106  // Binary add-on callbacks
107  void TriggerDeviceScan();
108  void RefreshButtonMaps(const std::string& strDeviceName = "");
109  unsigned int FeatureCount(const std::string& controllerId, JOYSTICK_FEATURE_TYPE type) const;
110  JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId,
111  const std::string& featureName) const;
112 
116  static void GetPeripheralInfo(const CPeripheral* device, kodi::addon::Peripheral& peripheralInfo);
117 
118  static void GetJoystickInfo(const CPeripheral* device, kodi::addon::Joystick& joystickInfo);
119  static void SetJoystickInfo(CPeripheralJoystick& joystick,
120  const kodi::addon::Joystick& joystickInfo);
121 
125  void ResetProperties(void);
126 
130  bool GetAddonProperties(void);
131 
132  bool LogError(const PERIPHERAL_ERROR error, const char* strMethod) const;
133 
134  static std::string GetDeviceName(PeripheralType type);
135  static std::string GetProvider(PeripheralType type);
136 
137  // Construction parameters
138  CPeripherals& m_manager;
139 
140  /* @brief Cache for const char* members in PERIPHERAL_PROPERTIES */
141  std::string m_strUserPath;
142  std::string m_strClientPath;
148  static void cb_trigger_scan(void* kodiInstance);
149  static void cb_refresh_button_maps(void* kodiInstance,
150  const char* deviceName,
151  const char* controllerId);
152  static unsigned int cb_feature_count(void* kodiInstance,
153  const char* controllerId,
154  JOYSTICK_FEATURE_TYPE type);
155  static JOYSTICK_FEATURE_TYPE cb_feature_type(void* kodiInstance,
156  const char* controllerId,
157  const char* featureName);
159 
160  /* @brief Add-on properties */
161  bool m_bProvidesJoysticks;
162  bool m_bSupportsJoystickRumble;
163  bool m_bSupportsJoystickPowerOff;
164  bool m_bProvidesButtonMaps;
165 
166  /* @brief Map of peripherals belonging to the add-on */
167  std::map<unsigned int, PeripheralPtr> m_peripherals;
168 
169  /* @brief Button map observers */
170  std::vector<std::pair<CPeripheral*, KODI::JOYSTICK::IButtonMap*>> m_buttonMaps;
171  CCriticalSection m_buttonMapMutex;
172 
173  /* @brief Thread synchronization */
174  mutable CCriticalSection m_critSection;
175 
176  CSharedSection m_dllSection;
177 };
178 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:348
Definition: RetroPlayerInput.h:15
Represents a list of files.
Definition: FileItem.h:713
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:493
Definition: AddonInstanceHandler.h:33
Definition: PeripheralJoystick.h:40
Controller configuration window.
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:62
Definition: Peripherals.h:49