Debian Codegraph @

xbmc/peripherals/devices/Peripheral.h

raw
1 /*
2 * Copyright (C) 2005-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 "games/controllers/ControllerTypes.h"
12 #include "input/joysticks/interfaces/IInputProvider.h"
13 #include "input/keyboard/interfaces/IKeyboardInputProvider.h"
14 #include "input/mouse/interfaces/IMouseInputProvider.h"
15 #include "peripherals/PeripheralTypes.h"
16
17 #include <map>
18 #include <set>
19 #include <string>
20 #include <vector>
21
22 class CDateTime;
23 class CSetting;
24
25 namespace KODI
26 {
27 namespace JOYSTICK
28 {
29 class IButtonMapper;
30 class IDriverHandler;
31 class IDriverReceiver;
32 class IInputHandler;
33 } // namespace JOYSTICK
34
35 namespace KEYBOARD
36 {
37 class IKeyboardDriverHandler;
38 } // namespace KEYBOARD
39
40 namespace KEYMAP
41 {
42 class IKeymap;
43 } // namespace KEYMAP
44
45 namespace MOUSE
46 {
47 class IMouseDriverHandler;
48 } // namespace MOUSE
49 } // namespace KODI
50
51 namespace PERIPHERALS
52 {
53 class CAddonButtonMapping;
54 class CGUIDialogPeripheralSettings;
55 class CPeripheralBus;
56 class CPeripherals;
57
58 /*!
59 * \ingroup peripherals
60 */
61 typedef enum
62 {
63 STATE_SWITCH_TOGGLE,
64 STATE_ACTIVATE_SOURCE,
65 STATE_STANDBY
66 } CecStateChange;
67
68 /*!
69 * \ingroup peripherals
70 */
71 class CPeripheral : public KODI::JOYSTICK::IInputProvider,
72 public KODI::KEYBOARD::IKeyboardInputProvider,
73 public KODI::MOUSE::IMouseInputProvider
74 {
75 friend class CGUIDialogPeripheralSettings;
76
77 public:
78 CPeripheral(CPeripherals& manager, const PeripheralScanResult& scanResult, CPeripheralBus* bus);
79 ~CPeripheral(void) override;
80
81 bool operator==(const CPeripheral& right) const;
82 bool operator!=(const CPeripheral& right) const;
83 bool operator==(const PeripheralScanResult& right) const;
84 bool operator!=(const PeripheralScanResult& right) const;
85
86 const std::string& FileLocation(void) const { return m_strFileLocation; }
87 const std::string& Location(void) const { return m_strLocation; }
88 int VendorId(void) const { return m_iVendorId; }
89 const char* VendorIdAsString(void) const { return m_strVendorId.c_str(); }
90 int ProductId(void) const { return m_iProductId; }
91 const char* ProductIdAsString(void) const { return m_strProductId.c_str(); }
92 PeripheralType Type(void) const { return m_type; }
93 PeripheralBusType GetBusType(void) const { return m_busType; }
94 const std::string& DeviceName(void) const { return m_strDeviceName; }
95 bool IsHidden(void) const { return m_bHidden; }
96 void SetHidden(bool bSetTo = true) { m_bHidden = bSetTo; }
97 const std::string& GetVersionInfo(void) const { return m_strVersionInfo; }
98
99 /*!
100 * @brief Get an icon for this peripheral
101 * @return Path to an icon, or skin icon file name
102 */
103 virtual std::string GetIcon() const;
104
105 /*!
106 * @brief Check whether this device has the given feature.
107 * @param feature The feature to check for.
108 * @return True when the device has the feature, false otherwise.
109 */
110 bool HasFeature(const PeripheralFeature feature) const;
111
112 /*!
113 * @brief Get all features that are supported by this device.
114 * @param features The features.
115 */
116 void GetFeatures(std::vector<PeripheralFeature>& features) const;
117
118 /*!
119 * @brief Initialises the peripheral.
120 * @return True when the peripheral has been initialised successfully, false otherwise.
121 */
122 bool Initialise(void);
123
124 /*!
125 * @brief Initialise one of the features of this peripheral.
126 * @param feature The feature to initialise.
127 * @return True when the feature has been initialised successfully, false otherwise.
128 */
129 virtual bool InitialiseFeature(const PeripheralFeature feature) { return true; }
130
131 /*!
132 * @brief Briefly activate a feature to notify the user
133 */
134 virtual void OnUserNotification() {}
135
136 /*!
137 * @brief Briefly test one of the features of this peripheral.
138 * @param feature The feature to test.
139 * @return True if the test succeeded, false otherwise.
140 */
141 virtual bool TestFeature(PeripheralFeature feature) { return false; }
142
143 /*!
144 * @brief Called when a setting changed.
145 * @param strChangedSetting The changed setting.
146 */
147 virtual void OnSettingChanged(const std::string& strChangedSetting) {}
148
149 /*!
150 * @brief Called when this device is removed, before calling the destructor.
151 */
152 virtual void OnDeviceRemoved(void) {}
153
154 /*!
155 * @brief Get all subdevices if this device is multifunctional.
156 * @param subDevices The subdevices.
157 */
158 virtual void GetSubdevices(PeripheralVector& subDevices) const;
159
160 /*!
161 * @return True when this device is multifunctional, false otherwise.
162 */
163 virtual bool IsMultiFunctional(void) const;
164
165 /*!
166 * @brief Add a setting to this peripheral. This will overwrite a previous setting with the same
167 * key.
168 * @param strKey The key of the setting.
169 * @param setting The setting.
170 */
171 virtual void AddSetting(const std::string& strKey,
172 const std::shared_ptr<const CSetting>& setting,
173 int order);
174
175 /*!
176 * @brief Check whether a setting is known with the given key.
177 * @param strKey The key to search.
178 * @return True when found, false otherwise.
179 */
180 virtual bool HasSetting(const std::string& strKey) const;
181
182 /*!
183 * @return True when this device has any settings, false otherwise.
184 */
185 virtual bool HasSettings(void) const;
186
187 /*!
188 * @return True when this device has any configurable settings, false otherwise.
189 */
190 virtual bool HasConfigurableSettings(void) const;
191
192 /*!
193 * @brief Get the value of a setting.
194 * @param strKey The key to search.
195 * @return The value or an empty string if it wasn't found.
196 */
197 virtual const std::string GetSettingString(const std::string& strKey) const;
198 virtual bool SetSetting(const std::string& strKey, const std::string& strValue);
199 virtual void SetSettingVisible(const std::string& strKey, bool bSetTo);
200 virtual bool IsSettingVisible(const std::string& strKey) const;
201
202 virtual int GetSettingInt(const std::string& strKey) const;
203 virtual bool SetSetting(const std::string& strKey, int iValue);
204
205 virtual bool GetSettingBool(const std::string& strKey) const;
206 virtual bool SetSetting(const std::string& strKey, bool bValue);
207
208 virtual float GetSettingFloat(const std::string& strKey) const;
209 virtual bool SetSetting(const std::string& strKey, float fValue);
210
211 virtual void PersistSettings(bool bExiting = false);
212 virtual void LoadPersistedSettings(void);
213 virtual void ResetDefaultSettings(void);
214
215 virtual std::vector<std::shared_ptr<CSetting>> GetSettings(void) const;
216
217 virtual bool ErrorOccured(void) const { return m_bError; }
218
219 virtual void RegisterJoystickDriverHandler(KODI::JOYSTICK::IDriverHandler* handler,
220 bool bPromiscuous)
221 {
222 }
223 virtual void UnregisterJoystickDriverHandler(KODI::JOYSTICK::IDriverHandler* handler) {}
224
225 virtual void RegisterKeyboardDriverHandler(KODI::KEYBOARD::IKeyboardDriverHandler* handler,
226 bool bPromiscuous)
227 {
228 }
229 virtual void UnregisterKeyboardDriverHandler(KODI::KEYBOARD::IKeyboardDriverHandler* handler) {}
230
231 virtual void RegisterMouseDriverHandler(KODI::MOUSE::IMouseDriverHandler* handler,
232 bool bPromiscuous)
233 {
234 }
235 virtual void UnregisterMouseDriverHandler(KODI::MOUSE::IMouseDriverHandler* handler) {}
236
237 // implementation of IInputProvider
238 void RegisterInputHandler(KODI::JOYSTICK::IInputHandler* handler, bool bPromiscuous) override;
239 void UnregisterInputHandler(KODI::JOYSTICK::IInputHandler* handler) override;
240
241 // implementation of IKeyboardInputProvider
242 void RegisterKeyboardHandler(KODI::KEYBOARD::IKeyboardInputHandler* handler,
243 bool bPromiscuous,
244 bool forceDefaultMap) override;
245 void UnregisterKeyboardHandler(KODI::KEYBOARD::IKeyboardInputHandler* handler) override;
246
247 // implementation of IMouseInputProvider
248 void RegisterMouseHandler(KODI::MOUSE::IMouseInputHandler* handler,
249 bool bPromiscuous,
250 bool forceDefaultMap) override;
251 void UnregisterMouseHandler(KODI::MOUSE::IMouseInputHandler* handler) override;
252
253 virtual void RegisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
254 virtual void UnregisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
255
256 virtual KODI::JOYSTICK::IDriverReceiver* GetDriverReceiver() { return nullptr; }
257
258 virtual KODI::KEYMAP::IKeymap* GetKeymap(const std::string& controllerId) { return nullptr; }
259
260 /*!
261 * \brief Return the last time this peripheral was active
262 *
263 * \return The time of last activation, or invalid if unknown/never active
264 */
265 virtual CDateTime LastActive() const;
266
267 /*!
268 * \brief Set the last time this peripheral was active
269 *
270 * \param lastActive The time of last activation, or invalid if unknown/never active
271 */
272 virtual void SetLastActive(const CDateTime& lastActive);
273
274 /*!
275 * \brief Get the controller profile that best represents this peripheral
276 *
277 * \return The controller profile, or empty if unknown
278 */
279 virtual KODI::GAME::ControllerPtr ControllerProfile() const { return m_controllerProfile; }
280
281 /*!
282 * \brief Set the controller profile for this peripheral
283 *
284 * \param controller The new controller profile
285 */
286 virtual void SetControllerProfile(const KODI::GAME::ControllerPtr& controller)
287 {
288 m_controllerProfile = controller;
289 }
290
291 protected:
292 virtual void ClearSettings(void);
293
294 CPeripherals& m_manager;
295 PeripheralType m_type;
296 PeripheralBusType m_busType;
297 PeripheralBusType m_mappedBusType;
298 std::string m_strLocation;
299 std::string m_strDeviceName;
300 std::string m_strSettingsFile;
301 std::string m_strFileLocation;
302 int m_iVendorId;
303 std::string m_strVendorId;
304 int m_iProductId;
305 std::string m_strProductId;
306 std::string m_strVersionInfo;
307 bool m_bInitialised = false;
308 bool m_bHidden = false;
309 bool m_bError = false;
310 std::vector<PeripheralFeature> m_features;
311 PeripheralVector m_subDevices;
312 std::map<std::string, PeripheralDeviceSetting> m_settings;
313 std::set<std::string> m_changedSettings;
314 CPeripheralBus* m_bus;
315 std::map<KODI::JOYSTICK::IInputHandler*, std::unique_ptr<KODI::JOYSTICK::IDriverHandler>>
316 m_inputHandlers;
317 std::map<KODI::KEYBOARD::IKeyboardInputHandler*,
318 std::unique_ptr<KODI::KEYBOARD::IKeyboardDriverHandler>>
319 m_keyboardHandlers;
320 std::map<KODI::MOUSE::IMouseInputHandler*, std::unique_ptr<KODI::MOUSE::IMouseDriverHandler>>
321 m_mouseHandlers;
322 std::map<KODI::JOYSTICK::IButtonMapper*, std::unique_ptr<CAddonButtonMapping>> m_buttonMappers;
323 KODI::GAME::ControllerPtr m_controllerProfile;
324 };
325 } // namespace PERIPHERALS