From c04dcc2e7d834218ef2d4194331e383402495ae1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 20:07:22 +0200 Subject: Adding upstream version 2:20.4+dfsg. Signed-off-by: Daniel Baumann --- xbmc/games/controllers/ControllerManager.cpp | 122 +++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 xbmc/games/controllers/ControllerManager.cpp (limited to 'xbmc/games/controllers/ControllerManager.cpp') diff --git a/xbmc/games/controllers/ControllerManager.cpp b/xbmc/games/controllers/ControllerManager.cpp new file mode 100644 index 0000000..707733b --- /dev/null +++ b/xbmc/games/controllers/ControllerManager.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2017-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "ControllerManager.h" + +#include "Controller.h" +#include "ControllerIDs.h" +#include "addons/AddonEvents.h" +#include "addons/AddonManager.h" +#include "addons/addoninfo/AddonType.h" + +#include + +using namespace KODI; +using namespace GAME; + +CControllerManager::CControllerManager(ADDON::CAddonMgr& addonManager) + : m_addonManager(addonManager) +{ + m_addonManager.Events().Subscribe(this, &CControllerManager::OnEvent); +} + +CControllerManager::~CControllerManager() +{ + m_addonManager.Events().Unsubscribe(this); +} + +ControllerPtr CControllerManager::GetController(const std::string& controllerId) +{ + using namespace ADDON; + + std::lock_guard lock(m_mutex); + + ControllerPtr& cachedController = m_cache[controllerId]; + + if (!cachedController && m_failedControllers.find(controllerId) == m_failedControllers.end()) + { + AddonPtr addon; + if (m_addonManager.GetAddon(controllerId, addon, AddonType::GAME_CONTROLLER, + OnlyEnabled::CHOICE_NO)) + cachedController = LoadController(addon); + } + + return cachedController; +} + +ControllerPtr CControllerManager::GetDefaultController() +{ + return GetController(DEFAULT_CONTROLLER_ID); +} + +ControllerPtr CControllerManager::GetDefaultKeyboard() +{ + return GetController(DEFAULT_KEYBOARD_ID); +} + +ControllerPtr CControllerManager::GetDefaultMouse() +{ + return GetController(DEFAULT_MOUSE_ID); +} + +ControllerVector CControllerManager::GetControllers() +{ + using namespace ADDON; + + ControllerVector controllers; + + std::lock_guard lock(m_mutex); + + VECADDONS addons; + if (m_addonManager.GetInstalledAddons(addons, AddonType::GAME_CONTROLLER)) + { + for (auto& addon : addons) + { + ControllerPtr& cachedController = m_cache[addon->ID()]; + if (!cachedController && m_failedControllers.find(addon->ID()) == m_failedControllers.end()) + cachedController = LoadController(addon); + + if (cachedController) + controllers.emplace_back(cachedController); + } + } + + return controllers; +} + +void CControllerManager::OnEvent(const ADDON::AddonEvent& event) +{ + if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // Also called on install + typeid(event) == typeid(ADDON::AddonEvents::ReInstalled)) + { + std::lock_guard lock(m_mutex); + + const std::string& addonId = event.addonId; + + // Clear caches for add-on + auto it = m_cache.find(addonId); + if (it != m_cache.end()) + m_cache.erase(it); + + auto it2 = m_failedControllers.find(addonId); + if (it2 != m_failedControllers.end()) + m_failedControllers.erase(it2); + } +} + +ControllerPtr CControllerManager::LoadController(const ADDON::AddonPtr& addon) +{ + ControllerPtr controller = std::static_pointer_cast(addon); + if (!controller->LoadLayout()) + { + m_failedControllers.insert(addon->ID()); + controller.reset(); + } + + return controller; +} -- cgit v1.2.3