| 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 |
|
| 45 |
/*! |
| 46 |
* \ingroup peripherals |
| 47 |
*/ |
| 48 |
class CPeripheralJoystick : public CPeripheral, //! @todo extend CPeripheralHID |
| 49 |
public KODI::JOYSTICK::IDriverReceiver |
| 50 |
{ |
| 51 |
public: |
| 52 |
CPeripheralJoystick(CPeripherals& manager, |
| 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 |
void SetLastActive(const CDateTime& lastActive) override; |
| 69 |
KODI::GAME::ControllerPtr ControllerProfile() const override; |
| 70 |
void SetControllerProfile(const KODI::GAME::ControllerPtr& controller) override; |
| 71 |
|
| 72 |
bool OnButtonMotion(unsigned int buttonIndex, bool bPressed); |
| 73 |
bool OnHatMotion(unsigned int hatIndex, KODI::JOYSTICK::HAT_STATE state); |
| 74 |
bool OnAxisMotion(unsigned int axisIndex, float position); |
| 75 |
void OnInputFrame(void); |
| 76 |
|
| 77 |
// implementation of IDriverReceiver |
| 78 |
bool SetMotorState(unsigned int motorIndex, float magnitude) override; |
| 79 |
|
| 80 |
/*! |
| 81 |
* \brief Get the name of the driver or API providing this joystick |
| 82 |
*/ |
| 83 |
const std::string& Provider(void) const { return m_strProvider; } |
| 84 |
|
| 85 |
/*! |
| 86 |
* \brief Get the specific port number requested by this joystick |
| 87 |
* |
| 88 |
* This could indicate that the joystick is connected to a hardware port |
| 89 |
* with a number label; some controllers, such as the Xbox 360 controller, |
| 90 |
* also have LEDs that indicate the controller is on a specific port. |
| 91 |
* |
| 92 |
* \return The 0-indexed port number, or JOYSTICK_PORT_UNKNOWN if no port is requested |
| 93 |
*/ |
| 94 |
int RequestedPort(void) const { return m_requestedPort; } |
| 95 |
|
| 96 |
/*! |
| 97 |
* \brief Get the number of elements reported by the driver |
| 98 |
*/ |
| 99 |
unsigned int ButtonCount(void) const { return m_buttonCount; } |
| 100 |
unsigned int HatCount(void) const { return m_hatCount; } |
| 101 |
unsigned int AxisCount(void) const { return m_axisCount; } |
| 102 |
unsigned int MotorCount(void) const { return m_motorCount; } |
| 103 |
bool SupportsPowerOff(void) const { return m_supportsPowerOff; } |
| 104 |
|
| 105 |
/*! |
| 106 |
* \brief Set joystick properties |
| 107 |
*/ |
| 108 |
void SetProvider(const std::string& provider) { m_strProvider = provider; } |
| 109 |
void SetRequestedPort(int port) { m_requestedPort = port; } |
| 110 |
void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; } |
| 111 |
void SetHatCount(unsigned int hatCount) { m_hatCount = hatCount; } |
| 112 |
void SetAxisCount(unsigned int axisCount) { m_axisCount = axisCount; } |
| 113 |
void SetMotorCount(unsigned int motorCount); // specialized to update m_features |
| 114 |
void SetSupportsPowerOff(bool bSupportsPowerOff); // specialized to update m_features |
| 115 |
|
| 116 |
protected: |
| 117 |
void InitializeDeadzoneFiltering(KODI::JOYSTICK::IButtonMap& buttonMap); |
| 118 |
void InitializeControllerProfile(KODI::JOYSTICK::IButtonMap& buttonMap); |
| 119 |
|
| 120 |
void PowerOff(); |
| 121 |
|
| 122 |
// Helper functions |
| 123 |
KODI::GAME::ControllerPtr InstallAsync(const std::string& controllerId); |
| 124 |
static bool InstallSync(const std::string& controllerId); |
| 125 |
|
| 126 |
struct DriverHandler |
| 127 |
{ |
| 128 |
KODI::JOYSTICK::IDriverHandler* handler; |
| 129 |
bool bPromiscuous; |
| 130 |
}; |
| 131 |
|
| 132 |
// State parameters |
| 133 |
std::string m_strProvider; |
| 134 |
int m_requestedPort{JOYSTICK_NO_PORT_REQUESTED}; |
| 135 |
unsigned int m_buttonCount = 0; |
| 136 |
unsigned int m_hatCount = 0; |
| 137 |
unsigned int m_axisCount = 0; |
| 138 |
unsigned int m_motorCount = 0; |
| 139 |
bool m_supportsPowerOff = false; |
| 140 |
CDateTime m_lastActive; |
| 141 |
std::queue<std::string> m_controllersToInstall; |
| 142 |
std::vector<std::future<void>> m_installTasks; |
| 143 |
|
| 144 |
// Input clients |
| 145 |
std::unique_ptr<KODI::KEYMAP::CKeymapHandling> m_appInput; |
| 146 |
std::unique_ptr<KODI::JOYSTICK::CRumbleGenerator> m_rumbleGenerator; |
| 147 |
std::unique_ptr<KODI::JOYSTICK::IInputHandler> m_joystickMonitor; |
| 148 |
std::unique_ptr<KODI::JOYSTICK::IButtonMap> m_buttonMap; |
| 149 |
std::unique_ptr<KODI::JOYSTICK::CDeadzoneFilter> m_deadzoneFilter; |
| 150 |
std::vector<DriverHandler> m_driverHandlers; |
| 151 |
|
| 152 |
// Synchronization parameters |
| 153 |
CCriticalSection m_handlerMutex; |
| 154 |
CCriticalSection m_controllerInstallMutex; |
| 155 |
}; |
| 156 |
} // namespace PERIPHERALS |