Debian Codegraph @

xbmc/input/keymaps/joysticks/JoystickMapper.cpp

raw
1 /*
2 * Copyright (C) 2017-2024 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 "JoystickMapper.h"
10
11 #include "input/actions/ActionIDs.h"
12 #include "input/actions/ActionTranslator.h"
13 #include "input/joysticks/JoystickTranslator.h"
14 #include "input/joysticks/JoystickUtils.h"
15 #include "input/keymaps/WindowKeymap.h"
16 #include "utils/StringUtils.h"
17
18 #include <algorithm>
19 #include <memory>
20 #include <sstream>
21 #include <utility>
22
23 #include <tinyxml2.h>
24
25 using namespace KODI;
26 using namespace KEYMAP;
27
28 #define JOYSTICK_XML_NODE_PROFILE "profile"
29 #define JOYSTICK_XML_ATTR_DIRECTION "direction"
30 #define JOYSTICK_XML_ATTR_HOLDTIME "holdtime"
31 #define JOYSTICK_XML_ATTR_HOTKEY "hotkey"
32
33 void CJoystickMapper::MapActions(int windowID, const tinyxml2::XMLNode* pDevice)
34 {
35 std::string controllerId;
36 DeserializeJoystickNode(pDevice, controllerId);
37 if (controllerId.empty())
38 return;
39
40 // Update Controller IDs
41 if (std::find(m_controllerIds.begin(), m_controllerIds.end(), controllerId) ==
42 m_controllerIds.end())
43 m_controllerIds.emplace_back(controllerId);
44
45 // Create/overwrite keymap
46 auto& keymap = m_joystickKeymaps[controllerId];
47 if (!keymap)
48 keymap = std::make_shared<CWindowKeymap>(controllerId);
49
50 const auto* pButton = pDevice->FirstChildElement();
51 while (pButton != nullptr)
52 {
53 std::string feature;
54 JOYSTICK::ANALOG_STICK_DIRECTION dir;
55 unsigned int holdtimeMs;
56 std::set<std::string> hotkeys;
57 std::string actionString;
58 if (DeserializeButton(pButton, feature, dir, holdtimeMs, hotkeys, actionString))
59 {
60 // Update keymap
61 unsigned int actionId = ACTION_NONE;
62 if (ACTION::CActionTranslator::TranslateString(actionString, actionId))
63 {
64 KeymapAction action = {
65 actionId,
66 std::move(actionString),
67 holdtimeMs,
68 std::move(hotkeys),
69 };
70 keymap->MapAction(windowID, JOYSTICK::CJoystickUtils::MakeKeyName(feature, dir),
71 std::move(action));
72 }
73 }
74 pButton = pButton->NextSiblingElement();
75 }
76 }
77
78 void CJoystickMapper::Clear()
79 {
80 m_joystickKeymaps.clear();
81 m_controllerIds.clear();
82 }
83
84 std::vector<std::shared_ptr<const IWindowKeymap>> CJoystickMapper::GetJoystickKeymaps() const
85 {
86 std::vector<std::shared_ptr<const IWindowKeymap>> keymaps;
87
88 for (const auto& controllerId : m_controllerIds)
89 {
90 auto it = m_joystickKeymaps.find(controllerId);
91 if (it != m_joystickKeymaps.end())
92 keymaps.emplace_back(it->second);
93 }
94
95 return keymaps;
96 }
97
98 void CJoystickMapper::DeserializeJoystickNode(const tinyxml2::XMLNode* pDevice,
99 std::string& controllerId)
100 {
101 const auto* deviceElem = pDevice->ToElement();
102 if (deviceElem != nullptr)
103 {
104 const char* controller;
105 if (deviceElem->QueryStringAttribute(JOYSTICK_XML_NODE_PROFILE, &controller) ==
106 tinyxml2::XML_SUCCESS)
107 controllerId = controller;
108 }
109 }
110
111 bool CJoystickMapper::DeserializeButton(const tinyxml2::XMLElement* pButton,
112 std::string& feature,
113 JOYSTICK::ANALOG_STICK_DIRECTION& dir,
114 unsigned int& holdtimeMs,
115 std::set<std::string>& hotkeys,
116 std::string& actionStr)
117 {
118 const char* szButton = pButton->Value();
119 if (szButton != nullptr)
120 {
121 const char* szAction = nullptr;
122
123 const auto* actionNode = pButton->FirstChild();
124 if (actionNode != nullptr)
125 szAction = actionNode->Value();
126
127 if (szAction != nullptr)
128 {
129 feature = szButton;
130 StringUtils::ToLower(feature);
131 actionStr = szAction;
132 }
133 }
134
135 if (!feature.empty() && !actionStr.empty())
136 {
137 // Handle direction
138 dir = JOYSTICK::ANALOG_STICK_DIRECTION::NONE;
139 const char* szDirection = pButton->Attribute(JOYSTICK_XML_ATTR_DIRECTION);
140 if (szDirection != nullptr)
141 dir = JOYSTICK::CJoystickTranslator::TranslateAnalogStickDirection(szDirection);
142
143 // Process holdtime parameter
144 holdtimeMs = 0;
145 const char* strHoldTime;
146 if (pButton->QueryStringAttribute(JOYSTICK_XML_ATTR_HOLDTIME, &strHoldTime) ==
147 tinyxml2::XML_SUCCESS)
148 {
149 std::istringstream ss(strHoldTime);
150 ss >> holdtimeMs;
151 }
152
153 // Process hotkeys
154 hotkeys.clear();
155 const char* strHotkeys;
156 if (pButton->QueryStringAttribute(JOYSTICK_XML_ATTR_HOTKEY, &strHotkeys) ==
157 tinyxml2::XML_SUCCESS)
158 {
159 std::vector<std::string> vecHotkeys = StringUtils::Split(strHotkeys, ",");
160 for (auto& hotkey : vecHotkeys)
161 hotkeys.insert(std::move(hotkey));
162 }
163
164 return true;
165 }
166
167 return false;
168 }