diff options
author | Garrett Brown <themagnificentmrb@gmail.com> | 2023-03-02 02:04:37 -0800 |
---|---|---|
committer | Garrett Brown <themagnificentmrb@gmail.com> | 2023-03-02 17:46:04 -0800 |
commit | a24dfafc9c24b7f378e539dcf3afadc82653f794 (patch) | |
tree | f54f58f944d21d9f50941477bc1cc9afc940d9e0 | |
parent | 0b4ec3a278baa875ab1eff6200ffc3c8c6a1e3f5 (diff) |
Peripheral API v3.0.0: Save controller appearance
6 files changed, 174 insertions, 2 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h index ef3665b2c4..6df51e4a73 100644 --- a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h +++ b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Peripheral.h @@ -409,6 +409,41 @@ public: //---------------------------------------------------------------------------- //============================================================================ + /// @brief Get the ID of the controller that best represents the peripheral's + /// appearance. + /// + /// @param[in] joystick The device's joystick properties; unknown values may + /// be left at their default + /// @param[out] controllerId The controller ID of the appearance, or empty + /// if the appearance is unknown + /// + /// @return @ref PERIPHERAL_NO_ERROR if successful + /// + virtual PERIPHERAL_ERROR GetAppearance(const kodi::addon::Joystick& joystick, + std::string& controllerId) + { + return PERIPHERAL_ERROR_NOT_IMPLEMENTED; + } + //---------------------------------------------------------------------------- + + //============================================================================ + /// @brief Set the ID of the controller that best represents the peripheral's + /// appearance. + /// + /// @param[in] joystick The device's joystick properties; unknown values may + /// be left at their default + /// @param[in] controllerId The controller ID of the appearance + /// + /// @return @ref PERIPHERAL_NO_ERROR if successful + /// + virtual PERIPHERAL_ERROR SetAppearance(const kodi::addon::Joystick& joystick, + const std::string& controllerId) + { + return PERIPHERAL_ERROR_NOT_IMPLEMENTED; + } + //---------------------------------------------------------------------------- + + //============================================================================ /// @brief Get the features that allow translating the joystick into the /// controller profile. /// @@ -612,6 +647,8 @@ private: instance->peripheral->toAddon->get_joystick_info = ADDON_GetJoystickInfo; instance->peripheral->toAddon->free_joystick_info = ADDON_FreeJoystickInfo; + instance->peripheral->toAddon->get_appearance = ADDON_GetAppearance; + instance->peripheral->toAddon->set_appearance = ADDON_SetAppearance; instance->peripheral->toAddon->get_features = ADDON_GetFeatures; instance->peripheral->toAddon->free_features = ADDON_FreeFeatures; instance->peripheral->toAddon->map_features = ADDON_MapFeatures; @@ -732,6 +769,42 @@ private: kodi::addon::Joystick::FreeStruct(*info); } + inline static PERIPHERAL_ERROR ADDON_GetAppearance(const AddonInstance_Peripheral* addonInstance, + const JOYSTICK_INFO* joystick, + char* buffer, + unsigned int bufferSize) + { + if (addonInstance == nullptr || joystick == nullptr || buffer == nullptr || bufferSize == 0) + return PERIPHERAL_ERROR_INVALID_PARAMETERS; + + kodi::addon::Joystick addonJoystick(*joystick); + std::string controllerId; + + PERIPHERAL_ERROR err = static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance) + ->GetAppearance(addonJoystick, controllerId); + if (err == PERIPHERAL_NO_ERROR) + { + std::strncpy(buffer, controllerId.c_str(), bufferSize - 1); + buffer[bufferSize - 1] = '\0'; + } + + return err; + } + + inline static PERIPHERAL_ERROR ADDON_SetAppearance(const AddonInstance_Peripheral* addonInstance, + const JOYSTICK_INFO* joystick, + const char* controllerId) + { + if (addonInstance == nullptr || joystick == nullptr || controllerId == nullptr) + return PERIPHERAL_ERROR_INVALID_PARAMETERS; + + kodi::addon::Joystick addonJoystick(*joystick); + std::string strControllerId(controllerId); + + return static_cast<CInstancePeripheral*>(addonInstance->toAddon->addonInstance) + ->SetAppearance(addonJoystick, strControllerId); + } + inline static PERIPHERAL_ERROR ADDON_GetFeatures(const AddonInstance_Peripheral* addonInstance, const JOYSTICK_INFO* joystick, const char* controller_id, diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/peripheral.h b/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/peripheral.h index 256a3d91b6..c70a90b25a 100644 --- a/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/peripheral.h +++ b/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/peripheral.h @@ -609,13 +609,17 @@ extern "C" typedef struct AddonToKodiFuncTable_Peripheral { KODI_HANDLE kodiInstance; + void (*trigger_scan)(void* kodiInstance); + void (*refresh_button_maps)(void* kodiInstance, const char* device_name, const char* controller_id); + unsigned int (*feature_count)(void* kodiInstance, const char* controller_id, JOYSTICK_FEATURE_TYPE type); + JOYSTICK_FEATURE_TYPE(*feature_type) (void* kodiInstance, const char* controller_id, const char* feature_name); } AddonToKodiFuncTable_Peripheral; @@ -628,20 +632,25 @@ extern "C" void(__cdecl* get_capabilities)(const struct AddonInstance_Peripheral* addonInstance, struct PERIPHERAL_CAPABILITIES* capabilities); + PERIPHERAL_ERROR(__cdecl* perform_device_scan) (const struct AddonInstance_Peripheral* addonInstance, unsigned int* peripheral_count, struct PERIPHERAL_INFO** scan_results); + void(__cdecl* free_scan_results)(const struct AddonInstance_Peripheral* addonInstance, unsigned int peripheral_count, struct PERIPHERAL_INFO* scan_results); + PERIPHERAL_ERROR(__cdecl* get_events) (const struct AddonInstance_Peripheral* addonInstance, unsigned int* event_count, struct PERIPHERAL_EVENT** events); + void(__cdecl* free_events)(const struct AddonInstance_Peripheral* addonInstance, unsigned int event_count, struct PERIPHERAL_EVENT* events); + bool(__cdecl* send_event)(const struct AddonInstance_Peripheral* addonInstance, const struct PERIPHERAL_EVENT* event); @@ -651,43 +660,65 @@ extern "C" (const struct AddonInstance_Peripheral* addonInstance, unsigned int index, struct JOYSTICK_INFO* info); + void(__cdecl* free_joystick_info)(const struct AddonInstance_Peripheral* addonInstance, struct JOYSTICK_INFO* info); + + PERIPHERAL_ERROR(__cdecl* get_appearance) + (const struct AddonInstance_Peripheral* addonInstance, + const struct JOYSTICK_INFO* joystick, + char* buffer, + unsigned int bufferSize); + + PERIPHERAL_ERROR(__cdecl* set_appearance) + (const struct AddonInstance_Peripheral* addonInstance, + const struct JOYSTICK_INFO* joystick, + const char* controller_id); + PERIPHERAL_ERROR(__cdecl* get_features) (const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick, const char* controller_id, unsigned int* feature_count, struct JOYSTICK_FEATURE** features); + void(__cdecl* free_features)(const struct AddonInstance_Peripheral* addonInstance, unsigned int feature_count, struct JOYSTICK_FEATURE* features); + PERIPHERAL_ERROR(__cdecl* map_features) (const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick, const char* controller_id, unsigned int feature_count, const struct JOYSTICK_FEATURE* features); + PERIPHERAL_ERROR(__cdecl* get_ignored_primitives) (const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick, unsigned int* feature_count, struct JOYSTICK_DRIVER_PRIMITIVE** primitives); + void(__cdecl* free_primitives)(const struct AddonInstance_Peripheral* addonInstance, unsigned int, struct JOYSTICK_DRIVER_PRIMITIVE* primitives); + PERIPHERAL_ERROR(__cdecl* set_ignored_primitives) (const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick, unsigned int primitive_count, const struct JOYSTICK_DRIVER_PRIMITIVE* primitives); + void(__cdecl* save_button_map)(const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick); + void(__cdecl* revert_button_map)(const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick); + void(__cdecl* reset_button_map)(const struct AddonInstance_Peripheral* addonInstance, const struct JOYSTICK_INFO* joystick, const char* controller_id); + void(__cdecl* power_off_joystick)(const struct AddonInstance_Peripheral* addonInstance, unsigned int index); ///} diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/versions.h b/xbmc/addons/kodi-dev-kit/include/kodi/versions.h index 0ee48bd08d..63793f7665 100644 --- a/xbmc/addons/kodi-dev-kit/include/kodi/versions.h +++ b/xbmc/addons/kodi-dev-kit/include/kodi/versions.h @@ -124,8 +124,8 @@ "addon-instance/inputstream/StreamCrypto.h" \ "addon-instance/inputstream/TimingConstants.h" -#define ADDON_INSTANCE_VERSION_PERIPHERAL "2.0.0" -#define ADDON_INSTANCE_VERSION_PERIPHERAL_MIN "2.0.0" +#define ADDON_INSTANCE_VERSION_PERIPHERAL "3.0.0" +#define ADDON_INSTANCE_VERSION_PERIPHERAL_MIN "3.0.0" #define ADDON_INSTANCE_VERSION_PERIPHERAL_XML_ID "kodi.binary.instance.peripheral" #define ADDON_INSTANCE_VERSION_PERIPHERAL_DEPENDS "addon-instance/Peripheral.h" \ "addon-instance/PeripheralUtils.h" diff --git a/xbmc/peripherals/addons/AddonButtonMap.cpp b/xbmc/peripherals/addons/AddonButtonMap.cpp index f2c894ca4d..a058417f5d 100644 --- a/xbmc/peripherals/addons/AddonButtonMap.cpp +++ b/xbmc/peripherals/addons/AddonButtonMap.cpp @@ -54,6 +54,7 @@ bool CAddonButtonMap::Load(void) bool bSuccess = false; if (auto addon = m_addon.lock()) { + bSuccess |= addon->GetAppearance(m_device, controllerAppearance); bSuccess |= addon->GetFeatures(m_device, m_strControllerId, features); bSuccess |= addon->GetIgnoredPrimitives(m_device, ignoredPrimitives); } @@ -102,6 +103,9 @@ std::string CAddonButtonMap::GetAppearance() const bool CAddonButtonMap::SetAppearance(const std::string& controllerId) const { + if (auto addon = m_addon.lock()) + return addon->SetAppearance(m_device, controllerId); + return false; } diff --git a/xbmc/peripherals/addons/PeripheralAddon.cpp b/xbmc/peripherals/addons/PeripheralAddon.cpp index 7a55bb37d4..d036cc8dd6 100644 --- a/xbmc/peripherals/addons/PeripheralAddon.cpp +++ b/xbmc/peripherals/addons/PeripheralAddon.cpp @@ -539,6 +539,68 @@ bool CPeripheralAddon::GetJoystickProperties(unsigned int index, CPeripheralJoys return false; } +bool CPeripheralAddon::GetAppearance(const CPeripheral* device, std::string& controllerId) +{ + if (!m_bProvidesButtonMaps) + return false; + + std::shared_lock<CSharedSection> lock(m_dllSection); + + if (!m_ifc.peripheral->toAddon->get_appearance) + return false; + + PERIPHERAL_ERROR retVal; + + kodi::addon::Joystick joystickInfo; + GetJoystickInfo(device, joystickInfo); + + JOYSTICK_INFO joystickStruct; + joystickInfo.ToStruct(joystickStruct); + + char strControllerId[1024]{}; + + LogError(retVal = m_ifc.peripheral->toAddon->get_appearance( + m_ifc.peripheral, &joystickStruct, strControllerId, sizeof(strControllerId)), + "GetAppearance()"); + + kodi::addon::Joystick::FreeStruct(joystickStruct); + + if (retVal == PERIPHERAL_NO_ERROR) + { + controllerId = strControllerId; + return true; + } + + return false; +} + +bool CPeripheralAddon::SetAppearance(const CPeripheral* device, const std::string& controllerId) +{ + if (!m_bProvidesButtonMaps) + return false; + + std::shared_lock<CSharedSection> lock(m_dllSection); + + if (!m_ifc.peripheral->toAddon->set_appearance) + return false; + + PERIPHERAL_ERROR retVal; + + kodi::addon::Joystick joystickInfo; + GetJoystickInfo(device, joystickInfo); + + JOYSTICK_INFO joystickStruct; + joystickInfo.ToStruct(joystickStruct); + + LogError(retVal = m_ifc.peripheral->toAddon->set_appearance(m_ifc.peripheral, &joystickStruct, + controllerId.c_str()), + "SetAppearance()"); + + kodi::addon::Joystick::FreeStruct(joystickStruct); + + return retVal == PERIPHERAL_NO_ERROR; +} + bool CPeripheralAddon::GetFeatures(const CPeripheral* device, const std::string& strControllerId, FeatureMap& features) diff --git a/xbmc/peripherals/addons/PeripheralAddon.h b/xbmc/peripherals/addons/PeripheralAddon.h index 2dd9d226ca..f967c20b1a 100644 --- a/xbmc/peripherals/addons/PeripheralAddon.h +++ b/xbmc/peripherals/addons/PeripheralAddon.h @@ -80,6 +80,8 @@ public: //@{ bool GetJoystickProperties(unsigned int index, CPeripheralJoystick& joystick); bool HasButtonMaps(void) const { return m_bProvidesButtonMaps; } + bool GetAppearance(const CPeripheral* device, std::string& controllerId); + bool SetAppearance(const CPeripheral* device, const std::string& controllerId); bool GetFeatures(const CPeripheral* device, const std::string& strControllerId, FeatureMap& features); |