Debian Codegraph @

xbmc/games/controllers/Controller.cpp

raw
1 /*
2 * Copyright (C) 2015-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 "Controller.h"
10
11 #include "ControllerDefinitions.h"
12 #include "ControllerLayout.h"
13 #include "URL.h"
14 #include "addons/addoninfo/AddonType.h"
15 #include "games/controllers/input/PhysicalTopology.h"
16 #include "utils/XBMCTinyXML2.h"
17 #include "utils/XMLUtils.h"
18 #include "utils/log.h"
19
20 #include <algorithm>
21 #include <cstring>
22
23 using namespace KODI;
24 using namespace GAME;
25
26 // --- FeatureTypeEqual --------------------------------------------------------
27
28 struct FeatureTypeEqual
29 {
30 FeatureTypeEqual(FEATURE_TYPE type, JOYSTICK::INPUT_TYPE inputType)
31 : type(type), inputType(inputType)
32 {
33 }
34
35 bool operator()(const CPhysicalFeature& feature) const
36 {
37 if (type == FEATURE_TYPE::UNKNOWN)
38 return true; // Match all feature types
39
40 if (type == FEATURE_TYPE::SCALAR && feature.Type() == FEATURE_TYPE::SCALAR)
41 {
42 if (inputType == JOYSTICK::INPUT_TYPE::UNKNOWN)
43 return true; // Match all input types
44
45 return inputType == feature.InputType();
46 }
47
48 return type == feature.Type();
49 }
50
51 const FEATURE_TYPE type;
52 const JOYSTICK::INPUT_TYPE inputType;
53 };
54
55 // --- CController -------------------------------------------------------------
56
57 const ControllerPtr CController::EmptyPtr;
58
59 CController::CController(const ADDON::AddonInfoPtr& addonInfo)
60 : CAddon(addonInfo, ADDON::AddonType::GAME_CONTROLLER), m_layout(new CControllerLayout)
61 {
62 }
63
64 CController::~CController() = default;
65
66 const CPhysicalFeature& CController::GetFeature(const std::string& name) const
67 {
68 auto it =
69 std::find_if(m_features.begin(), m_features.end(),
70 [&name](const CPhysicalFeature& feature) { return name == feature.Name(); });
71
72 if (it != m_features.end())
73 return *it;
74
75 static const CPhysicalFeature invalid{};
76 return invalid;
77 }
78
79 unsigned int CController::FeatureCount(
80 FEATURE_TYPE type /* = FEATURE_TYPE::UNKNOWN */,
81 JOYSTICK::INPUT_TYPE inputType /* = JOYSTICK::INPUT_TYPE::UNKNOWN */) const
82 {
83 auto featureCount =
84 std::count_if(m_features.begin(), m_features.end(), FeatureTypeEqual(type, inputType));
85 return static_cast<unsigned int>(featureCount);
86 }
87
88 void CController::GetFeatures(std::vector<std::string>& features,
89 FEATURE_TYPE type /* = FEATURE_TYPE::UNKNOWN */) const
90 {
91 for (const CPhysicalFeature& feature : m_features)
92 {
93 if (type == FEATURE_TYPE::UNKNOWN || type == feature.Type())
94 features.push_back(feature.Name());
95 }
96 }
97
98 JOYSTICK::FEATURE_TYPE CController::FeatureType(const std::string& feature) const
99 {
100 for (auto it = m_features.begin(); it != m_features.end(); ++it)
101 {
102 if (feature == it->Name())
103 return it->Type();
104 }
105 return JOYSTICK::FEATURE_TYPE::UNKNOWN;
106 }
107
108 JOYSTICK::INPUT_TYPE CController::GetInputType(const std::string& feature) const
109 {
110 for (auto it = m_features.begin(); it != m_features.end(); ++it)
111 {
112 if (feature == it->Name())
113 return it->InputType();
114 }
115 return JOYSTICK::INPUT_TYPE::UNKNOWN;
116 }
117
118 bool CController::LoadLayout(void)
119 {
120 if (!m_bLoaded)
121 {
122 std::string strLayoutXmlPath = LibPath();
123
124 CLog::Log(LOGINFO, "Loading controller layout: {}", CURL::GetRedacted(strLayoutXmlPath));
125
126 CXBMCTinyXML2 xmlDoc;
127 if (!xmlDoc.LoadFile(strLayoutXmlPath))
128 {
129 CLog::Log(LOGDEBUG, "Unable to load file: {} at line {}", xmlDoc.ErrorStr(),
130 xmlDoc.ErrorLineNum());
131 return false;
132 }
133
134 auto* pRootElement = xmlDoc.RootElement();
135 if (pRootElement == nullptr || std::strcmp(pRootElement->Value(), LAYOUT_XML_ROOT) != 0)
136 {
137 CLog::Log(LOGERROR, "Can't find root <{}> tag", LAYOUT_XML_ROOT);
138 return false;
139 }
140
141 m_layout->Deserialize(pRootElement, this, m_features);
142 if (m_layout->IsValid(true))
143 {
144 m_bLoaded = true;
145 }
146 else
147 {
148 m_layout->Reset();
149 }
150 }
151
152 return m_bLoaded;
153 }
154
155 const CPhysicalTopology& CController::Topology() const
156 {
157 return m_layout->Topology();
158 }