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