kodi
PeripheralJoystick.h
1 /*
2  * Copyright (C) 2014-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 "Peripheral.h"
12 #include "XBDateTime.h"
13 #include "games/controllers/ControllerTypes.h"
14 #include "input/joysticks/JoystickTypes.h"
15 #include "input/joysticks/interfaces/IDriverReceiver.h"
16 #include "threads/CriticalSection.h"
17 
18 #include <future>
19 #include <memory>
20 #include <queue>
21 #include <string>
22 #include <vector>
23 
24 namespace KODI
25 {
26 namespace JOYSTICK
27 {
28 class CDeadzoneFilter;
29 class CRumbleGenerator;
30 class IButtonMap;
31 class IDriverHandler;
32 class IInputHandler;
33 } // namespace JOYSTICK
34 
35 namespace KEYMAP
36 {
37 class CKeymapHandling;
38 } // namespace KEYMAP
39 } // namespace KODI
40 
41 namespace PERIPHERALS
42 {
43 class CPeripherals;
44 
50 {
51 public:
53  const PeripheralScanResult& scanResult,
54  CPeripheralBus* bus);
55 
56  ~CPeripheralJoystick(void) override;
57 
58  // implementation of CPeripheral
59  bool InitialiseFeature(const PeripheralFeature feature) override;
60  void OnUserNotification() override;
61  bool TestFeature(PeripheralFeature feature) override;
62  void RegisterJoystickDriverHandler(KODI::JOYSTICK::IDriverHandler* handler,
63  bool bPromiscuous) override;
64  void UnregisterJoystickDriverHandler(KODI::JOYSTICK::IDriverHandler* handler) override;
65  KODI::JOYSTICK::IDriverReceiver* GetDriverReceiver() override { return this; }
66  KODI::KEYMAP::IKeymap* GetKeymap(const std::string& controllerId) override;
67  CDateTime LastActive() const override { return m_lastActive; }
68  KODI::GAME::ControllerPtr ControllerProfile() const override;
69  void SetControllerProfile(const KODI::GAME::ControllerPtr& controller) override;
70 
71  bool OnButtonMotion(unsigned int buttonIndex, bool bPressed);
72  bool OnHatMotion(unsigned int hatIndex, KODI::JOYSTICK::HAT_STATE state);
73  bool OnAxisMotion(unsigned int axisIndex, float position);
74  void OnInputFrame(void);
75 
76  // implementation of IDriverReceiver
77  bool SetMotorState(unsigned int motorIndex, float magnitude) override;
78 
82  const std::string& Provider(void) const { return m_strProvider; }
83 
93  int RequestedPort(void) const { return m_requestedPort; }
94 
98  unsigned int ButtonCount(void) const { return m_buttonCount; }
99  unsigned int HatCount(void) const { return m_hatCount; }
100  unsigned int AxisCount(void) const { return m_axisCount; }
101  unsigned int MotorCount(void) const { return m_motorCount; }
102  bool SupportsPowerOff(void) const { return m_supportsPowerOff; }
103 
107  void SetProvider(const std::string& provider) { m_strProvider = provider; }
108  void SetRequestedPort(int port) { m_requestedPort = port; }
109  void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; }
110  void SetHatCount(unsigned int hatCount) { m_hatCount = hatCount; }
111  void SetAxisCount(unsigned int axisCount) { m_axisCount = axisCount; }
112  void SetMotorCount(unsigned int motorCount); // specialized to update m_features
113  void SetSupportsPowerOff(bool bSupportsPowerOff); // specialized to update m_features
114 
115 protected:
116  void InitializeDeadzoneFiltering(KODI::JOYSTICK::IButtonMap& buttonMap);
117  void InitializeControllerProfile(KODI::JOYSTICK::IButtonMap& buttonMap);
118 
119  void PowerOff();
120 
121  // Helper functions
122  KODI::GAME::ControllerPtr InstallAsync(const std::string& controllerId);
123  static bool InstallSync(const std::string& controllerId);
124 
126  {
128  bool bPromiscuous;
129  };
130 
131  // State parameters
132  std::string m_strProvider;
133  int m_requestedPort{JOYSTICK_NO_PORT_REQUESTED};
134  unsigned int m_buttonCount = 0;
135  unsigned int m_hatCount = 0;
136  unsigned int m_axisCount = 0;
137  unsigned int m_motorCount = 0;
138  bool m_supportsPowerOff = false;
139  CDateTime m_lastActive;
140  std::queue<std::string> m_controllersToInstall;
141  std::vector<std::future<void>> m_installTasks;
142 
143  // Input clients
144  std::unique_ptr<KODI::KEYMAP::CKeymapHandling> m_appInput;
145  std::unique_ptr<KODI::JOYSTICK::CRumbleGenerator> m_rumbleGenerator;
146  std::unique_ptr<KODI::JOYSTICK::IInputHandler> m_joystickMonitor;
147  std::unique_ptr<KODI::JOYSTICK::IButtonMap> m_buttonMap;
148  std::unique_ptr<KODI::JOYSTICK::CDeadzoneFilter> m_deadzoneFilter;
149  std::vector<DriverHandler> m_driverHandlers;
150 
151  // Synchronization parameters
152  CCriticalSection m_handlerMutex;
153  CCriticalSection m_controllerInstallMutex;
154 };
155 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:326
const std::string & Provider(void) const
Get the name of the driver or API providing this joystick.
Definition: PeripheralJoystick.h:82
Definition: IInputHandler.h:16
unsigned int ButtonCount(void) const
Get the number of elements reported by the driver.
Definition: PeripheralJoystick.h:98
int RequestedPort(void) const
Get the specific port number requested by this joystick.
Definition: PeripheralJoystick.h:93
Definition: RetroPlayerInput.h:15
Interface defining methods to handle joystick events for raw driver elements (buttons, hats, axes)
Definition: IDriverHandler.h:23
Button map interface to translate between the driver&#39;s raw button/hat/axis elements and physical joys...
Definition: IButtonMap.h:29
void SetProvider(const std::string &provider)
Set joystick properties.
Definition: PeripheralJoystick.h:107
Definition: PeripheralJoystick.h:48
Definition: PeripheralJoystick.h:125
Definition: AudioDecoder.h:18
std::shared_ptr< CController > ControllerPtr
Smart pointer to a game controller (CController)
Definition: ControllerTypes.h:25
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
Definition: PeripheralBus.h:36
CDateTime LastActive() const override
Return the last time this peripheral was active.
Definition: PeripheralJoystick.h:67
Definition: Peripheral.h:71
Interface for sending input events to joystick drivers.
Definition: IDriverReceiver.h:20
Definition: Peripherals.h:56
Interface for mapping buttons to Kodi actions.
Definition: IKeymap.h:28