kodi
Peripherals.h
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 "interfaces/IAnnouncer.h"
14 #include "messaging/IMessageTarget.h"
15 #include "peripherals/events/interfaces/IEventScannerCallback.h"
16 #include "settings/lib/ISettingCallback.h"
17 #include "threads/CriticalSection.h"
18 #include "threads/Thread.h"
19 #include "utils/Observer.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 class CFileItemList;
25 class CInputManager;
26 class CSetting;
27 class CSettingsCategory;
28 class CAction;
29 class CKey;
30 
31 namespace tinyxml2
32 {
33 class XMLElement;
34 }
35 
36 namespace KODI
37 {
38 namespace GAME
39 {
40 class CControllerManager;
41 }
42 
43 namespace JOYSTICK
44 {
45 class IButtonMapper;
46 }
47 } // namespace KODI
48 
49 namespace PERIPHERALS
50 {
51 class CEventScanner;
52 
57  public Observable,
59  public IEventScannerCallback,
61 {
62 public:
63  explicit CPeripherals(CInputManager& inputManager,
64  KODI::GAME::CControllerManager& controllerProfiles);
65 
66  ~CPeripherals() override;
67 
71  void Initialise();
72 
76  void Clear();
77 
84  PeripheralPtr GetPeripheralAtLocation(const std::string& strLocation,
85  PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
86 
93  bool HasPeripheralAtLocation(const std::string& strLocation,
94  PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
95 
101  PeripheralBusPtr GetBusWithDevice(const std::string& strLocation) const;
102 
108  bool SupportsFeature(PeripheralFeature feature) const;
109 
117  int GetPeripheralsWithFeature(PeripheralVector& results,
118  const PeripheralFeature feature,
119  PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
120 
121  size_t GetNumberOfPeripherals() const;
122 
129  bool HasPeripheralWithFeature(const PeripheralFeature feature,
130  PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;
131 
137  void OnDeviceAdded(const CPeripheralBus& bus, const CPeripheral& peripheral);
138 
144  void OnDeviceDeleted(const CPeripheralBus& bus, const CPeripheral& peripheral);
145 
152  void CreatePeripheral(CPeripheralBus& bus, const PeripheralScanResult& result);
153 
159  void GetSettingsFromMapping(CPeripheral& peripheral) const;
160 
164  void TriggerDeviceScan(const PeripheralBusType type = PERIPHERAL_BUS_UNKNOWN);
165 
171  PeripheralBusPtr GetBusByType(const PeripheralBusType type) const;
172 
178  void GetDirectory(const std::string& strPath, CFileItemList& items) const;
179 
185  PeripheralPtr GetByPath(const std::string& strPath) const;
186 
193  bool OnAction(const CAction& action);
194 
199  bool IsMuted();
200 
206  bool ToggleMute();
207 
213  bool ToggleDeviceState(const CecStateChange mode = STATE_SWITCH_TOGGLE);
214 
220  bool Mute()
221  {
222  return ToggleMute();
223  }
224 
230  bool UnMute()
231  {
232  return ToggleMute();
233  }
234 
241  bool GetNextKeypress(float frameTime, CKey& key);
242 
247  EventPollHandlePtr RegisterEventPoller();
248 
253  EventLockHandlePtr RegisterEventLock();
254 
258  void OnUserNotification();
259 
264  void TestFeature(PeripheralFeature feature);
265 
269  void PowerOffDevices();
270 
271  bool SupportsCEC() const
272  {
273 #if defined(HAVE_LIBCEC)
274  return true;
275 #else
276  return false;
277 #endif
278  }
279 
280  // implementation of IEventScannerCallback
281  void ProcessEvents(void) override;
282 
292  void EnableButtonMapping();
293 
298  PeripheralAddonPtr GetAddonWithButtonMap(const CPeripheral* device);
299 
305  void ResetButtonMaps(const std::string& controllerId);
306 
319  void RegisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
320 
325  void UnregisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);
326 
327  // implementation of ISettingCallback
328  void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
329  void OnSettingAction(const std::shared_ptr<const CSetting>& setting) override;
330 
331  // implementation of IMessageTarget
332  void OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg) override;
333  int GetMessageMask() override;
334 
335  // implementation of IAnnouncer
336  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
337  const std::string& sender,
338  const std::string& message,
339  const CVariant& data) override;
340 
345  {
346  return m_inputManager;
347  }
348 
353  {
354  return m_controllerProfiles;
355  }
356 
360  CCriticalSection& GetAddonInstallMutex()
361  {
362  return m_addonInstallMutex;
363  }
364 
365 private:
366  bool LoadMappings();
367  bool GetMappingForDevice(const CPeripheralBus& bus, PeripheralScanResult& result) const;
368  static void GetSettingsFromMappingsFile(
369  tinyxml2::XMLElement* xmlNode, std::map<std::string, PeripheralDeviceSetting>& m_settings);
370 
371  void OnDeviceChanged();
372 
373  // Construction parameters
374  CInputManager& m_inputManager;
375  KODI::GAME::CControllerManager& m_controllerProfiles;
376 
377 #if !defined(HAVE_LIBCEC)
378  bool m_bMissingLibCecWarningDisplayed = false;
379 #endif
380  std::vector<PeripheralBusPtr> m_busses;
381  std::vector<PeripheralDeviceMapping> m_mappings;
382  std::unique_ptr<CEventScanner> m_eventScanner;
383  mutable CCriticalSection m_critSectionBusses;
384  mutable CCriticalSection m_critSectionMappings;
385  CCriticalSection m_addonInstallMutex;
386 };
387 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:326
KODI::GAME::CControllerManager & GetControllerProfiles()
Access controller profiles through the construction parameter.
Definition: Peripherals.h:352
bool UnMute()
Try to unmute the audio via a peripheral.
Definition: Peripherals.h:230
A class wishing to receive messages should implement this and call.
Definition: IMessageTarget.h:23
Main input processing class.
Definition: InputManager.h:68
CInputManager & GetInputManager()
Access the input manager passed to the constructor.
Definition: Peripherals.h:344
Definition: RetroPlayerInput.h:15
Represents a list of files.
Definition: FileItem.h:702
Definition: IAnnouncer.h:70
Definition: ISettingCallback.h:16
Setting base class containing all the properties which are common to all settings independent of the ...
Definition: Setting.h:46
Definition: ThreadMessage.h:25
Definition: SkinTimerManager.h:18
CCriticalSection & GetAddonInstallMutex()
Get a mutex that allows for add-on install tasks to block on each other.
Definition: Peripherals.h:360
Definition: Variant.h:31
Class encapsulating information regarding a particular user action to be sent to windows and controls...
Definition: Action.h:21
Definition: ControllerManager.h:31
Definition: AudioDecoder.h:18
Definition: PeripheralBus.h:36
bool Mute()
Try to mute the audio via a peripheral.
Definition: Peripherals.h:220
Definition: IEventScannerCallback.h:16
Button mapper interface to assign the driver&#39;s raw button/hat/axis elements to physical joystick feat...
Definition: IButtonMapper.h:37
Definition: Peripheral.h:71
Definition: Observer.h:44
Definition: Key.h:17
Definition: Peripherals.h:56