Debian Codegraph @

xbmc/peripherals/Peripherals.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 "bus/PeripheralBus.h"
12 #include "devices/Peripheral.h"
13 #include "guilib/IMsgTargetCallback.h"
14 #include "interfaces/IAnnouncer.h"
15 #include "messaging/IMessageTarget.h"
16 #include "peripherals/events/interfaces/IEventScannerCallback.h"
17 #include "settings/lib/ISettingCallback.h"
18 #include "threads/CriticalSection.h"
19 #include "threads/Thread.h"
20 #include "utils/Observer.h"
21
22 #include <atomic>
23 #include <memory>
24 #include <vector>
25
26 class CFileItemList;
27 class CInputManager;
28 class CSetting;
29 class CSettingsCategory;
30 class CAction;
31 class CKey;
32 class CEvent;
33
34 namespace tinyxml2
35 {
36 class XMLElement;
37 }
38
39 namespace KODI
40 {
41 namespace GAME
42 {
43 class CControllerManager;
44 }
45
46 namespace JOYSTICK
47 {
48 class IButtonMapper;
49 }
50 } // namespace KODI
51
52 namespace PERIPHERALS
53 {
54 class CEventScanner;
55
56 /*!
57 * \ingroup peripherals
58 */
59 class CPeripherals : public ISettingCallback,
60 public Observable,
61 public KODI::MESSAGING::IMessageTarget,
62 public IEventScannerCallback,
63 public ANNOUNCEMENT::IAnnouncer,
64 public IMsgTargetCallback
65 {
66 public:
67 explicit CPeripherals(CInputManager& inputManager,
68 KODI::GAME::CControllerManager& controllerProfiles);
69
70 ~CPeripherals() override;
71
72 /*!
73 * @brief Initialise the peripherals manager.
74 */
75 void Initialise();
76
77 /*!
78 * @brief Clear all data known by the peripherals manager.
79 */
80 void Clear();
81
82 /*!
83 * @brief Get the instance of the peripheral at the given location.
84 * @param strLocation The location.
85 * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
86 * @return The peripheral or NULL if it wasn't found.
87 */
88 PeripheralPtr GetPeripheralAtLocation(const std::string& strLocation,
89 PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
90
91 /*!
92 * @brief Check whether a peripheral is present at the given location.
93 * @param strLocation The location.
94 * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
95 * @return True when a peripheral was found, false otherwise.
96 */
97 bool HasPeripheralAtLocation(const std::string& strLocation,
98 PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
99
100 /*!
101 * @brief Get the bus that holds the device with the given location.
102 * @param strLocation The location.
103 * @return The bus or NULL if no device was found.
104 */
105 PeripheralBusPtr GetBusWithDevice(const std::string& strLocation) const;
106
107 /*!
108 * @brief Check if any busses support the given feature
109 * @param feature The feature to check for
110 * @return True if a bus supports the feature, false otherwise
111 */
112 bool SupportsFeature(PeripheralFeature feature) const;
113
114 /*!
115 * @brief Get all peripheral instances that have the given feature.
116 * @param results The list of results.
117 * @param feature The feature to search for.
118 * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
119 * @return The number of devices that have been found.
120 */
121 int GetPeripheralsWithFeature(PeripheralVector& results,
122 const PeripheralFeature feature,
123 PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
124
125 size_t GetNumberOfPeripherals() const;
126
127 /*!
128 * @brief Check whether there is at least one device present with the given feature.
129 * @param feature The feature to check for.
130 * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
131 * @return True when at least one device was found with this feature, false otherwise.
132 */
133 bool HasPeripheralWithFeature(const PeripheralFeature feature,
134 PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
135
136 /*!
137 * @brief Called when a device has been added to a bus.
138 * @param bus The bus the device was added to.
139 * @param peripheral The peripheral that has been added.
140 */
141 void OnDeviceAdded(const CPeripheralBus& bus, const CPeripheral& peripheral);
142
143 /*!
144 * @brief Called when a device has been deleted from a bus.
145 * @param bus The bus from which the device removed.
146 * @param peripheral The peripheral that has been removed.
147 */
148 void OnDeviceDeleted(const CPeripheralBus& bus, const CPeripheral& peripheral);
149
150 /*!
151 * @brief Creates a new instance of a peripheral.
152 * @param bus The bus on which this peripheral is present.
153 * @param result The scan result from the device scanning code.
154 * @return The new peripheral or NULL if it could not be created.
155 */
156 void CreatePeripheral(CPeripheralBus& bus, const PeripheralScanResult& result);
157
158 /*!
159 * @brief Add the settings that are defined in the mappings file to the peripheral (if there is
160 * anything defined).
161 * @param peripheral The peripheral to get the settings for.
162 */
163 void GetSettingsFromMapping(CPeripheral& peripheral) const;
164
165 /*!
166 * @brief Trigger a device scan on all known busses
167 */
168 void TriggerDeviceScan(const PeripheralBusType type = PERIPHERAL_BUS_UNKNOWN);
169
170 /*!
171 * @brief Get the instance of a bus given it's type.
172 * @param type The bus type.
173 * @return The bus or NULL if it wasn't found.
174 */
175 PeripheralBusPtr GetBusByType(const PeripheralBusType type) const;
176
177 /*!
178 * @brief Get all fileitems for a path.
179 * @param strPath The path to the directory to get the items from.
180 * @param items The item list.
181 */
182 void GetDirectory(const std::string& strPath, CFileItemList& items) const;
183
184 /*!
185 * @brief Get the instance of a peripheral given it's path.
186 * @param strPath The path to the peripheral.
187 * @return The peripheral or NULL if it wasn't found.
188 */
189 PeripheralPtr GetByPath(const std::string& strPath) const;
190
191 /*!
192 * @brief Try to let one of the peripherals handle an action.
193 * @param action The change to handle.
194 * @return True when this change was handled by a peripheral (and should not be handled by
195 * anything else), false otherwise.
196 */
197 bool OnAction(const CAction& action);
198
199 /*!
200 * @brief Check whether there's a peripheral that reports to be muted.
201 * @return True when at least one peripheral reports to be muted, false otherwise.
202 */
203 bool IsMuted();
204
205 /*!
206 * @brief Try to toggle the mute status via a peripheral.
207 * @return True when this change was handled by a peripheral (and should not be handled by
208 * anything else), false otherwise.
209 */
210 bool ToggleMute();
211
212 /*!
213 * @brief Try to toggle the playing device state via a peripheral.
214 * @param mode Whether to activate, put on standby or toggle the source.
215 * @return True when the playing device has been switched on, false otherwise.
216 */
217 bool ToggleDeviceState(const CecStateChange mode = STATE_SWITCH_TOGGLE);
218
219 /*!
220 * @brief Try to mute the audio via a peripheral.
221 * @return True when this change was handled by a peripheral (and should not be handled by
222 * anything else), false otherwise.
223 */
224 bool Mute()
225 {
226 return ToggleMute();
227 } //! @todo CEC only supports toggling the mute status at this time
228
229 /*!
230 * @brief Try to unmute the audio via a peripheral.
231 * @return True when this change was handled by a peripheral (and should not be handled by
232 * anything else), false otherwise.
233 */
234 bool UnMute()
235 {
236 return ToggleMute();
237 } //! @todo CEC only supports toggling the mute status at this time
238
239 /*!
240 * @brief Try to get a keypress from a peripheral.
241 * @param frameTime The current frametime.
242 * @param key The fetched key.
243 * @return True when a keypress was fetched, false otherwise.
244 */
245 bool GetNextKeypress(float frameTime, CKey& key);
246
247 /*!
248 * @brief Register with the event scanner to control scan timing
249 * @return A handle that unregisters itself when expired
250 */
251 EventPollHandlePtr RegisterEventPoller();
252
253 /*!
254 * @brief Register with the event scanner to disable event processing
255 * @return A handle that unregisters itself when expired
256 */
257 EventLockHandlePtr RegisterEventLock();
258
259 /*!
260 *
261 */
262 void OnUserNotification();
263
264 /*!
265 * @brief Request peripherals with the specified feature to perform a quick test
266 * @return true if any peripherals support the feature, false otherwise
267 */
268 void TestFeature(PeripheralFeature feature);
269
270 /*!
271 * \brief Request all devices with power-off support to power down
272 */
273 void PowerOffDevices();
274
275 bool SupportsCEC() const
276 {
277 #if defined(HAVE_LIBCEC)
278 return true;
279 #else
280 return false;
281 #endif
282 }
283
284 // implementation of IEventScannerCallback
285 void ProcessEvents(void) override;
286
287 /*!
288 * \brief Initialize button mapping
289 *
290 * This command enables button mapping on all busses. Button maps allow
291 * connect events from the driver to the higher-level features used by
292 * controller profiles.
293 *
294 * If user input is required, a blocking dialog may be shown.
295 */
296 void EnableButtonMapping();
297
298 /*!
299 * \brief Get an add-on that can provide button maps for a device
300 * \return An add-on that provides button maps, or empty if no add-on is found
301 */
302 PeripheralAddonPtr GetAddonWithButtonMap(const CPeripheral* device);
303
304 /*!
305 * \brief Reset all button maps to the defaults for all devices and the given controller
306 * \param controllerId The controller profile to reset
307 * @todo Add a device parameter to allow resetting button maps per-device
308 */
309 void ResetButtonMaps(const std::string& controllerId);
310
311 /*!
312 * \brief Register a button mapper interface
313 * \param mapper The button mapper
314 *
315 * Clients implementing the IButtonMapper interface call
316 * \ref CPeripherals::RegisterJoystickButtonMapper to register themselves
317 * as eligible for button mapping commands.
318 *
319 * When registering the mapper is forwarded to all peripherals. See
320 * \ref CPeripheral::RegisterJoystickButtonMapper for what is done to the
321 * mapper after being given to the peripheral.
322 */
323 void RegisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
324
325 /*!
326 * \brief Unregister a button mapper interface
327 * \param mapper The button mapper
328 */
329 void UnregisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
330
331 // implementation of ISettingCallback
332 void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
333 void OnSettingAction(const std::shared_ptr<const CSetting>& setting) override;
334
335 // implementation of IMessageTarget
336 void OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg) override;
337 int GetMessageMask() override;
338
339 // implementation of IAnnouncer
340 void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
341 const std::string& sender,
342 const std::string& message,
343 const CVariant& data) override;
344
345 // Implementation of IMsgTargetCallback
346 bool OnMessage(CGUIMessage& message) override;
347
348 /*!
349 * \brief Access the input manager passed to the constructor
350 */
351 CInputManager& GetInputManager()
352 {
353 return m_inputManager;
354 }
355
356 /*!
357 * \brief Access controller profiles through the construction parameter
358 */
359 KODI::GAME::CControllerManager& GetControllerProfiles()
360 {
361 return m_controllerProfiles;
362 }
363
364 /*!
365 * \brief Get a mutex that allows for add-on install tasks to block on each other
366 */
367 CCriticalSection& GetAddonInstallMutex()
368 {
369 return m_addonInstallMutex;
370 }
371
372 // GUI functions
373 bool WaitForGUI();
374
375 private:
376 bool LoadMappings();
377 bool GetMappingForDevice(const CPeripheralBus& bus, PeripheralScanResult& result) const;
378 static void GetSettingsFromMappingsFile(
379 tinyxml2::XMLElement* xmlNode, std::map<std::string, PeripheralDeviceSetting>& m_settings);
380
381 void OnDeviceChanged();
382
383 // Construction parameters
384 CInputManager& m_inputManager;
385 KODI::GAME::CControllerManager& m_controllerProfiles;
386
387 #if !defined(HAVE_LIBCEC)
388 bool m_bMissingLibCecWarningDisplayed = false;
389 #endif
390 std::vector<PeripheralBusPtr> m_busses;
391 std::vector<PeripheralDeviceMapping> m_mappings;
392 std::unique_ptr<CEventScanner> m_eventScanner;
393 mutable CCriticalSection m_critSectionBusses;
394 mutable CCriticalSection m_critSectionMappings;
395 CCriticalSection m_addonInstallMutex;
396 std::atomic<bool> m_guiReady{false};
397 std::unique_ptr<CEvent> m_guiReadyEvent;
398 };
399 } // namespace PERIPHERALS