Debian Codegraph @

xbmc/games/controllers/ControllerLayout.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 "ControllerLayout.h"
10
11 #include "Controller.h"
12 #include "ControllerDefinitions.h"
13 #include "ControllerTranslator.h"
14 #include "games/controllers/input/PhysicalTopology.h"
15 #include "guilib/LocalizeStrings.h"
16 #include "utils/URIUtils.h"
17 #include "utils/XMLUtils.h"
18 #include "utils/log.h"
19
20 #include <cstring>
21 #include <sstream>
22
23 #include <tinyxml2.h>
24
25 using namespace KODI;
26 using namespace GAME;
27
28 CControllerLayout::CControllerLayout() : m_topology(new CPhysicalTopology)
29 {
30 }
31
32 CControllerLayout::CControllerLayout(const CControllerLayout& other)
33 : m_controller(other.m_controller),
34 m_labelId(other.m_labelId),
35 m_icon(other.m_icon),
36 m_strImage(other.m_strImage),
37 m_topology(new CPhysicalTopology(*other.m_topology))
38 {
39 }
40
41 CControllerLayout::~CControllerLayout() = default;
42
43 void CControllerLayout::Reset(void)
44 {
45 m_controller = nullptr;
46 m_labelId = -1;
47 m_icon.clear();
48 m_strImage.clear();
49 m_topology->Reset();
50 }
51
52 bool CControllerLayout::IsValid(bool bLog) const
53 {
54 if (m_labelId < 0)
55 {
56 if (bLog)
57 CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", LAYOUT_XML_ROOT,
58 LAYOUT_XML_ATTR_LAYOUT_LABEL);
59 return false;
60 }
61
62 if (m_strImage.empty())
63 {
64 if (bLog)
65 CLog::Log(LOGDEBUG, "<{}> tag has no \"{}\" attribute", LAYOUT_XML_ROOT,
66 LAYOUT_XML_ATTR_LAYOUT_IMAGE);
67 return false;
68 }
69
70 return true;
71 }
72
73 std::string CControllerLayout::Label(void) const
74 {
75 std::string label;
76
77 if (m_labelId >= 0 && m_controller != nullptr)
78 label = g_localizeStrings.GetAddonString(m_controller->ID(), m_labelId);
79
80 return label;
81 }
82
83 std::string CControllerLayout::ImagePath(void) const
84 {
85 std::string path;
86
87 if (!m_strImage.empty() && m_controller != nullptr)
88 return URIUtils::AddFileToFolder(URIUtils::GetDirectory(m_controller->LibPath()), m_strImage);
89
90 return path;
91 }
92
93 void CControllerLayout::Deserialize(const tinyxml2::XMLElement* pElement,
94 const CController* controller,
95 std::vector<CPhysicalFeature>& features)
96 {
97 if (pElement == nullptr || controller == nullptr)
98 return;
99
100 // Controller (used for string lookup and path translation)
101 m_controller = controller;
102
103 // Label
104 std::string strLabel = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_LABEL);
105 if (!strLabel.empty())
106 std::istringstream(strLabel) >> m_labelId;
107
108 // Icon
109 std::string icon = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_ICON);
110 if (!icon.empty())
111 m_icon = icon;
112
113 // Fallback icon, use add-on icon
114 if (m_icon.empty())
115 m_icon = controller->Icon();
116
117 // Image
118 std::string image = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_IMAGE);
119 if (!image.empty())
120 m_strImage = image;
121
122 for (const auto* pChild = pElement->FirstChildElement(); pChild != nullptr;
123 pChild = pChild->NextSiblingElement())
124 {
125 if (std::strcmp(pChild->Value(), LAYOUT_XML_ELM_CATEGORY) == 0)
126 {
127 // Category
128 std::string strCategory = XMLUtils::GetAttribute(pChild, LAYOUT_XML_ATTR_CATEGORY_NAME);
129 JOYSTICK::FEATURE_CATEGORY category =
130 CControllerTranslator::TranslateFeatureCategory(strCategory);
131
132 // Category label
133 int categoryLabelId = -1;
134
135 std::string strCategoryLabelId =
136 XMLUtils::GetAttribute(pChild, LAYOUT_XML_ATTR_CATEGORY_LABEL);
137 if (!strCategoryLabelId.empty())
138 std::istringstream(strCategoryLabelId) >> categoryLabelId;
139
140 // Features
141 for (const auto* pFeature = pChild->FirstChildElement(); pFeature != nullptr;
142 pFeature = pFeature->NextSiblingElement())
143 {
144 CPhysicalFeature feature;
145
146 if (feature.Deserialize(pFeature, controller, category, categoryLabelId))
147 features.push_back(feature);
148 }
149 }
150 else if (std::strcmp(pChild->Value(), LAYOUT_XML_ELM_TOPOLOGY) == 0)
151 {
152 // Topology
153 CPhysicalTopology topology;
154 if (topology.Deserialize(pChild))
155 *m_topology = std::move(topology);
156 }
157 else
158 {
159 CLog::Log(LOGDEBUG, "Ignoring <{}> tag", pChild->Value());
160 }
161 }
162 }