| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 |
#include "ControllerManager.h" |
| 10 |
|
| 11 |
#include "Controller.h" |
| 12 |
#include "ControllerIDs.h" |
| 13 |
#include "addons/AddonEvents.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "addons/addoninfo/AddonType.h" |
| 16 |
#include "games/controllers/input/PhysicalFeature.h" |
| 17 |
|
| 18 |
#include <mutex> |
| 19 |
|
| 20 |
using namespace KODI; |
| 21 |
using namespace GAME; |
| 22 |
|
| 23 |
CControllerManager::CControllerManager(ADDON::CAddonMgr& addonManager) |
| 24 |
: m_addonManager(addonManager) |
| 25 |
{ |
| 26 |
m_addonManager.Events().Subscribe(this, &CControllerManager::OnEvent); |
| 27 |
} |
| 28 |
|
| 29 |
CControllerManager::~CControllerManager() |
| 30 |
{ |
| 31 |
m_addonManager.Events().Unsubscribe(this); |
| 32 |
} |
| 33 |
|
| 34 |
ControllerPtr CControllerManager::GetController(const std::string& controllerId) |
| 35 |
{ |
| 36 |
using namespace ADDON; |
| 37 |
|
| 38 |
if (controllerId.empty()) |
| 39 |
return {}; |
| 40 |
|
| 41 |
std::lock_guard<CCriticalSection> lock(m_mutex); |
| 42 |
|
| 43 |
ControllerPtr& cachedController = m_cache[controllerId]; |
| 44 |
|
| 45 |
if (!cachedController && m_failedControllers.find(controllerId) == m_failedControllers.end()) |
| 46 |
{ |
| 47 |
AddonPtr addon; |
| 48 |
if (m_addonManager.GetAddon(controllerId, addon, AddonType::GAME_CONTROLLER, |
| 49 |
OnlyEnabled::CHOICE_NO)) |
| 50 |
cachedController = LoadController(addon); |
| 51 |
} |
| 52 |
|
| 53 |
return cachedController; |
| 54 |
} |
| 55 |
|
| 56 |
ControllerPtr CControllerManager::GetDefaultController() |
| 57 |
{ |
| 58 |
return GetController(DEFAULT_CONTROLLER_ID); |
| 59 |
} |
| 60 |
|
| 61 |
ControllerPtr CControllerManager::GetDefaultKeyboard() |
| 62 |
{ |
| 63 |
return GetController(DEFAULT_KEYBOARD_ID); |
| 64 |
} |
| 65 |
|
| 66 |
ControllerPtr CControllerManager::GetDefaultMouse() |
| 67 |
{ |
| 68 |
return GetController(DEFAULT_MOUSE_ID); |
| 69 |
} |
| 70 |
|
| 71 |
ControllerVector CControllerManager::GetControllers() |
| 72 |
{ |
| 73 |
using namespace ADDON; |
| 74 |
|
| 75 |
ControllerVector controllers; |
| 76 |
|
| 77 |
std::lock_guard<CCriticalSection> lock(m_mutex); |
| 78 |
|
| 79 |
VECADDONS addons; |
| 80 |
if (m_addonManager.GetInstalledAddons(addons, AddonType::GAME_CONTROLLER)) |
| 81 |
{ |
| 82 |
for (auto& addon : addons) |
| 83 |
{ |
| 84 |
ControllerPtr& cachedController = m_cache[addon->ID()]; |
| 85 |
if (!cachedController && m_failedControllers.find(addon->ID()) == m_failedControllers.end()) |
| 86 |
cachedController = LoadController(addon); |
| 87 |
|
| 88 |
if (cachedController) |
| 89 |
controllers.emplace_back(cachedController); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return controllers; |
| 94 |
} |
| 95 |
|
| 96 |
std::string CControllerManager::TranslateFeature(const std::string& controllerId, |
| 97 |
const std::string& featureName) |
| 98 |
{ |
| 99 |
ControllerPtr controller = GetController(controllerId); |
| 100 |
if (controller) |
| 101 |
{ |
| 102 |
const std::vector<CPhysicalFeature>& features = controller->Features(); |
| 103 |
|
| 104 |
auto it = std::find_if(features.begin(), features.end(), |
| 105 |
[&featureName](const CPhysicalFeature& feature) |
| 106 |
{ return feature.Name() == featureName; }); |
| 107 |
|
| 108 |
if (it != features.end()) |
| 109 |
return it->Label(); |
| 110 |
} |
| 111 |
|
| 112 |
return ""; |
| 113 |
} |
| 114 |
|
| 115 |
void CControllerManager::OnEvent(const ADDON::AddonEvent& event) |
| 116 |
{ |
| 117 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // Also called on install |
| 118 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled)) |
| 119 |
{ |
| 120 |
std::lock_guard<CCriticalSection> lock(m_mutex); |
| 121 |
|
| 122 |
const std::string& addonId = event.addonId; |
| 123 |
|
| 124 |
// Clear caches for add-on |
| 125 |
auto it = m_cache.find(addonId); |
| 126 |
if (it != m_cache.end()) |
| 127 |
m_cache.erase(it); |
| 128 |
|
| 129 |
auto it2 = m_failedControllers.find(addonId); |
| 130 |
if (it2 != m_failedControllers.end()) |
| 131 |
m_failedControllers.erase(it2); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
ControllerPtr CControllerManager::LoadController(const ADDON::AddonPtr& addon) |
| 136 |
{ |
| 137 |
ControllerPtr controller = std::static_pointer_cast<CController>(addon); |
| 138 |
if (!controller->LoadLayout()) |
| 139 |
{ |
| 140 |
m_failedControllers.insert(addon->ID()); |
| 141 |
controller.reset(); |
| 142 |
} |
| 143 |
|
| 144 |
return controller; |
| 145 |
} |