| 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 "addons/binary-addons/AddonInstanceHandler.h" |
| 12 |
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h" |
| 13 |
#include "input/joysticks/JoystickTypes.h" |
| 14 |
#include "peripherals/PeripheralTypes.h" |
| 15 |
#include "threads/CriticalSection.h" |
| 16 |
#include "threads/SharedSection.h" |
| 17 |
|
| 18 |
#include <map> |
| 19 |
#include <memory> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
class CFileItemList; |
| 23 |
|
| 24 |
namespace KODI |
| 25 |
{ |
| 26 |
namespace JOYSTICK |
| 27 |
{ |
| 28 |
class IButtonMap; |
| 29 |
class IDriverHandler; |
| 30 |
} // namespace JOYSTICK |
| 31 |
} // namespace KODI |
| 32 |
|
| 33 |
namespace PERIPHERALS |
| 34 |
{ |
| 35 |
class CPeripheral; |
| 36 |
class CPeripheralJoystick; |
| 37 |
class CPeripherals; |
| 38 |
|
| 39 |
typedef std::vector<kodi::addon::DriverPrimitive> PrimitiveVector; |
| 40 |
typedef std::map<KODI::JOYSTICK::FeatureName, kodi::addon::JoystickFeature> FeatureMap; |
| 41 |
|
| 42 |
/*! |
| 43 |
* \ingroup peripherals |
| 44 |
*/ |
| 45 |
class CPeripheralAddon : public ADDON::IAddonInstanceHandler |
| 46 |
{ |
| 47 |
public: |
| 48 |
explicit CPeripheralAddon(const ADDON::AddonInfoPtr& addonInfo, CPeripherals& manager); |
| 49 |
~CPeripheralAddon(void) override; |
| 50 |
|
| 51 |
/*! |
| 52 |
* @brief Initialise the instance of this add-on |
| 53 |
*/ |
| 54 |
bool CreateAddon(void); |
| 55 |
|
| 56 |
/*! |
| 57 |
* \brief Deinitialize the instance of this add-on |
| 58 |
*/ |
| 59 |
void DestroyAddon(); |
| 60 |
|
| 61 |
bool Register(unsigned int peripheralIndex, const PeripheralPtr& peripheral); |
| 62 |
void UnregisterRemovedDevices(const PeripheralScanResults& results, |
| 63 |
PeripheralVector& removedPeripherals); |
| 64 |
void GetFeatures(std::vector<PeripheralFeature>& features) const; |
| 65 |
bool HasFeature(const PeripheralFeature feature) const; |
| 66 |
PeripheralPtr GetPeripheral(unsigned int index) const; |
| 67 |
PeripheralPtr GetByPath(const std::string& strPath) const; |
| 68 |
bool SupportsFeature(PeripheralFeature feature) const; |
| 69 |
unsigned int GetPeripheralsWithFeature(PeripheralVector& results, |
| 70 |
const PeripheralFeature feature) const; |
| 71 |
unsigned int GetNumberOfPeripherals(void) const; |
| 72 |
unsigned int GetNumberOfPeripheralsWithId(const int iVendorId, const int iProductId) const; |
| 73 |
void GetDirectory(const std::string& strPath, CFileItemList& items) const; |
| 74 |
|
| 75 |
/** @name Peripheral add-on methods */ |
| 76 |
//@{ |
| 77 |
bool PerformDeviceScan(PeripheralScanResults& results); |
| 78 |
bool ProcessEvents(void); |
| 79 |
bool SendRumbleEvent(unsigned int index, unsigned int driverIndex, float magnitude); |
| 80 |
//@} |
| 81 |
|
| 82 |
/** @name Joystick methods */ |
| 83 |
//@{ |
| 84 |
bool GetJoystickProperties(unsigned int index, CPeripheralJoystick& joystick); |
| 85 |
bool HasButtonMaps(void) const { return m_bProvidesButtonMaps; } |
| 86 |
bool GetAppearance(const CPeripheral* device, std::string& controllerId); |
| 87 |
bool SetAppearance(const CPeripheral* device, const std::string& controllerId); |
| 88 |
bool GetFeatures(const CPeripheral* device, |
| 89 |
const std::string& strControllerId, |
| 90 |
FeatureMap& features); |
| 91 |
bool MapFeature(const CPeripheral* device, |
| 92 |
const std::string& strControllerId, |
| 93 |
const kodi::addon::JoystickFeature& feature); |
| 94 |
bool GetIgnoredPrimitives(const CPeripheral* device, PrimitiveVector& primitives); |
| 95 |
bool SetIgnoredPrimitives(const CPeripheral* device, const PrimitiveVector& primitives); |
| 96 |
void SaveButtonMap(const CPeripheral* device); |
| 97 |
void RevertButtonMap(const CPeripheral* device); |
| 98 |
void ResetButtonMap(const CPeripheral* device, const std::string& strControllerId); |
| 99 |
void PowerOffJoystick(unsigned int index); |
| 100 |
//@} |
| 101 |
|
| 102 |
void RegisterButtonMap(CPeripheral* device, KODI::JOYSTICK::IButtonMap* buttonMap); |
| 103 |
void UnregisterButtonMap(KODI::JOYSTICK::IButtonMap* buttonMap); |
| 104 |
|
| 105 |
static bool ProvidesJoysticks(const ADDON::AddonInfoPtr& addonInfo); |
| 106 |
static bool ProvidesButtonMaps(const ADDON::AddonInfoPtr& addonInfo); |
| 107 |
|
| 108 |
private: |
| 109 |
void UnregisterButtonMap(CPeripheral* device); |
| 110 |
|
| 111 |
// Binary add-on callbacks |
| 112 |
void TriggerDeviceScan(); |
| 113 |
void RefreshButtonMaps(const std::string& strDeviceName = ""); |
| 114 |
unsigned int FeatureCount(const std::string& controllerId, JOYSTICK_FEATURE_TYPE type) const; |
| 115 |
JOYSTICK_FEATURE_TYPE FeatureType(const std::string& controllerId, |
| 116 |
const std::string& featureName) const; |
| 117 |
|
| 118 |
/*! |
| 119 |
* @brief Helper functions |
| 120 |
*/ |
| 121 |
static void GetPeripheralInfo(const CPeripheral* device, kodi::addon::Peripheral& peripheralInfo); |
| 122 |
|
| 123 |
static void GetJoystickInfo(const CPeripheral* device, kodi::addon::Joystick& joystickInfo); |
| 124 |
static void SetJoystickInfo(CPeripheralJoystick& joystick, |
| 125 |
const kodi::addon::Joystick& joystickInfo); |
| 126 |
|
| 127 |
/*! |
| 128 |
* @brief Reset all class members to their defaults. Called by the constructors |
| 129 |
*/ |
| 130 |
void ResetProperties(void); |
| 131 |
|
| 132 |
/*! |
| 133 |
* @brief Retrieve add-on properties from the add-on |
| 134 |
*/ |
| 135 |
bool GetAddonProperties(void); |
| 136 |
|
| 137 |
bool LogError(const PERIPHERAL_ERROR error, const char* strMethod) const; |
| 138 |
|
| 139 |
static std::string GetDeviceName(PeripheralType type); |
| 140 |
static std::string GetProvider(PeripheralType type); |
| 141 |
|
| 142 |
// Construction parameters |
| 143 |
CPeripherals& m_manager; |
| 144 |
|
| 145 |
/* @brief Cache for const char* members in PERIPHERAL_PROPERTIES */ |
| 146 |
std::string m_strUserPath; /*!< @brief translated path to the user profile */ |
| 147 |
std::string m_strClientPath; /*!< @brief translated path to this add-on */ |
| 148 |
|
| 149 |
/*! |
| 150 |
* @brief Callback functions from addon to kodi |
| 151 |
*/ |
| 152 |
//@{ |
| 153 |
static void cb_trigger_scan(void* kodiInstance); |
| 154 |
static void cb_refresh_button_maps(void* kodiInstance, |
| 155 |
const char* deviceName, |
| 156 |
const char* controllerId); |
| 157 |
static unsigned int cb_feature_count(void* kodiInstance, |
| 158 |
const char* controllerId, |
| 159 |
JOYSTICK_FEATURE_TYPE type); |
| 160 |
static JOYSTICK_FEATURE_TYPE cb_feature_type(void* kodiInstance, |
| 161 |
const char* controllerId, |
| 162 |
const char* featureName); |
| 163 |
//@} |
| 164 |
|
| 165 |
/* @brief Add-on properties */ |
| 166 |
bool m_bProvidesJoysticks; |
| 167 |
bool m_bSupportsJoystickRumble = false; |
| 168 |
bool m_bSupportsJoystickPowerOff = false; |
| 169 |
bool m_bProvidesButtonMaps; |
| 170 |
|
| 171 |
/* @brief Map of peripherals belonging to the add-on */ |
| 172 |
std::map<unsigned int, PeripheralPtr> m_peripherals; |
| 173 |
|
| 174 |
/* @brief Button map observers */ |
| 175 |
std::vector<std::pair<CPeripheral*, KODI::JOYSTICK::IButtonMap*>> m_buttonMaps; |
| 176 |
CCriticalSection m_buttonMapMutex; |
| 177 |
|
| 178 |
/* @brief Thread synchronization */ |
| 179 |
mutable CCriticalSection m_critSection; |
| 180 |
|
| 181 |
CSharedSection m_dllSection; |
| 182 |
}; |
| 183 |
} // namespace PERIPHERALS |