Debian Codegraph @

xbmc/games/controllers/input/PhysicalFeature.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 "PhysicalFeature.h"
10
11 #include "games/controllers/Controller.h"
12 #include "games/controllers/ControllerDefinitions.h"
13 #include "games/controllers/ControllerTranslator.h"
14 #include "guilib/LocalizeStrings.h"
15 #include "input/keyboard/KeyboardTranslator.h"
16 #include "utils/XMLUtils.h"
17 #include "utils/log.h"
18
19 #include <sstream>
20
21 #include <tinyxml2.h>
22
23 using namespace KODI;
24 using namespace GAME;
25 using namespace JOYSTICK;
26
27 CPhysicalFeature::CPhysicalFeature(int labelId)
28 {
29 Reset();
30 m_labelId = labelId;
31 }
32
33 void CPhysicalFeature::Reset(void)
34 {
35 *this = CPhysicalFeature();
36 }
37
38 CPhysicalFeature& CPhysicalFeature::operator=(const CPhysicalFeature& rhs)
39 {
40 if (this != &rhs)
41 {
42 m_controller = rhs.m_controller;
43 m_type = rhs.m_type;
44 m_category = rhs.m_category;
45 m_categoryLabelId = rhs.m_categoryLabelId;
46 m_strName = rhs.m_strName;
47 m_labelId = rhs.m_labelId;
48 m_inputType = rhs.m_inputType;
49 m_keycode = rhs.m_keycode;
50 }
51 return *this;
52 }
53
54 std::string CPhysicalFeature::CategoryLabel() const
55 {
56 std::string categoryLabel;
57
58 if (m_categoryLabelId >= 0 && m_controller != nullptr)
59 categoryLabel = g_localizeStrings.GetAddonString(m_controller->ID(), m_categoryLabelId);
60
61 if (categoryLabel.empty())
62 categoryLabel = g_localizeStrings.Get(m_categoryLabelId);
63
64 return categoryLabel;
65 }
66
67 std::string CPhysicalFeature::Label() const
68 {
69 std::string label;
70
71 if (m_labelId >= 0 && m_controller != nullptr)
72 label = g_localizeStrings.GetAddonString(m_controller->ID(), m_labelId);
73
74 if (label.empty())
75 label = g_localizeStrings.Get(m_labelId);
76
77 return label;
78 }
79
80 bool CPhysicalFeature::Deserialize(const tinyxml2::XMLElement* pElement,
81 const CController* controller,
82 FEATURE_CATEGORY category,
83 int categoryLabelId)
84 {
85 Reset();
86
87 if (!pElement)
88 return false;
89
90 std::string strType(pElement->Value());
91
92 // Type
93 m_type = CControllerTranslator::TranslateFeatureType(strType);
94 if (m_type == FEATURE_TYPE::UNKNOWN)
95 {
96 CLog::Log(LOGDEBUG, "Invalid feature: <{}> ", pElement->Value());
97 return false;
98 }
99
100 // Category was obtained from parent XML node
101 m_category = category;
102 m_categoryLabelId = categoryLabelId;
103
104 // Name
105 m_strName = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_FEATURE_NAME);
106 if (m_strName.empty())
107 {
108 CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_FEATURE_NAME);
109 return false;
110 }
111
112 // Label ID
113 std::string strLabel = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_FEATURE_LABEL);
114 if (strLabel.empty())
115 CLog::Log(LOGDEBUG, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_FEATURE_LABEL);
116 else
117 std::istringstream(strLabel) >> m_labelId;
118
119 // Input type
120 if (m_type == FEATURE_TYPE::SCALAR)
121 {
122 std::string strInputType = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_INPUT_TYPE);
123 if (strInputType.empty())
124 {
125 CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_INPUT_TYPE);
126 return false;
127 }
128 else
129 {
130 m_inputType = CControllerTranslator::TranslateInputType(strInputType);
131 if (m_inputType == INPUT_TYPE::UNKNOWN)
132 {
133 CLog::Log(LOGERROR, "<{}> tag - attribute \"{}\" is invalid: \"{}\"", strType,
134 LAYOUT_XML_ATTR_INPUT_TYPE, strInputType);
135 return false;
136 }
137 }
138 }
139
140 // Keycode
141 if (m_type == FEATURE_TYPE::KEY)
142 {
143 std::string strSymbol = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_KEY_SYMBOL);
144 if (strSymbol.empty())
145 {
146 CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_KEY_SYMBOL);
147 return false;
148 }
149 else
150 {
151 m_keycode = KEYBOARD::CKeyboardTranslator::TranslateKeysym(strSymbol);
152 if (m_keycode == XBMCK_UNKNOWN)
153 {
154 CLog::Log(LOGERROR, "<{}> tag - attribute \"{}\" is invalid: \"{}\"", strType,
155 LAYOUT_XML_ATTR_KEY_SYMBOL, strSymbol);
156 return false;
157 }
158 }
159 }
160
161 // Save controller for string translation
162 m_controller = controller;
163
164 return true;
165 }