Debian Codegraph @

xbmc/input/keymaps/joysticks/KeymapHandler.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 "KeymapHandler.h"
10
11 #include "games/controllers/Controller.h"
12 #include "input/InputTranslator.h"
13 #include "input/joysticks/JoystickEasterEgg.h"
14 #include "input/joysticks/JoystickTranslator.h"
15 #include "input/joysticks/JoystickUtils.h"
16 #include "input/keymaps/generic/KeyHandler.h"
17 #include "input/keymaps/interfaces/IKeyHandler.h"
18 #include "input/keymaps/interfaces/IKeymap.h"
19 #include "input/keymaps/interfaces/IKeymapEnvironment.h"
20
21 #include <algorithm>
22 #include <assert.h>
23 #include <cmath>
24 #include <memory>
25 #include <utility>
26
27 using namespace KODI;
28 using namespace JOYSTICK;
29 using namespace KEYMAP;
30
31 CKeymapHandler::CKeymapHandler(ACTION::IActionListener* actionHandler, const IKeymap* keymap)
32 : m_actionHandler(actionHandler), m_keymap(keymap)
33 {
34 assert(m_actionHandler != nullptr);
35 assert(m_keymap != nullptr);
36
37 if (m_keymap->Environment()->UseEasterEgg())
38 m_easterEgg = std::make_unique<CJoystickEasterEgg>(ControllerID());
39 }
40
41 bool CKeymapHandler::HotkeysPressed(const std::set<std::string>& keyNames) const
42 {
43 bool bHotkeysPressed = true;
44
45 for (const auto& hotkey : keyNames)
46 {
47 auto it = m_keyHandlers.find(hotkey);
48 if (it == m_keyHandlers.end() || !it->second->IsPressed())
49 {
50 bHotkeysPressed = false;
51 break;
52 }
53 }
54
55 return bHotkeysPressed;
56 }
57
58 std::string CKeymapHandler::ControllerID() const
59 {
60 return m_keymap->ControllerID();
61 }
62
63 bool CKeymapHandler::AcceptsInput(const FeatureName& feature) const
64 {
65 if (HasAction(CJoystickUtils::MakeKeyName(feature)))
66 return true;
67
68 for (auto dir : CJoystickUtils::GetAnalogStickDirections())
69 {
70 if (HasAction(CJoystickUtils::MakeKeyName(feature, dir)))
71 return true;
72 }
73
74 return false;
75 }
76
77 bool CKeymapHandler::OnButtonPress(const FeatureName& feature, bool bPressed)
78 {
79 if (bPressed && m_easterEgg && m_easterEgg->OnButtonPress(feature))
80 return true;
81
82 const std::string keyName = CJoystickUtils::MakeKeyName(feature);
83
84 IKeyHandler* handler = GetKeyHandler(keyName);
85 return handler->OnDigitalMotion(bPressed, 0);
86 }
87
88 void CKeymapHandler::OnButtonHold(const FeatureName& feature, unsigned int holdTimeMs)
89 {
90 if (m_easterEgg && m_easterEgg->IsCapturing())
91 return;
92
93 const std::string keyName = CJoystickUtils::MakeKeyName(feature);
94
95 IKeyHandler* handler = GetKeyHandler(keyName);
96 handler->OnDigitalMotion(true, holdTimeMs);
97 }
98
99 bool CKeymapHandler::OnButtonMotion(const FeatureName& feature,
100 float magnitude,
101 unsigned int motionTimeMs)
102 {
103 const std::string keyName = CJoystickUtils::MakeKeyName(feature);
104
105 IKeyHandler* handler = GetKeyHandler(keyName);
106 return handler->OnAnalogMotion(magnitude, motionTimeMs);
107 }
108
109 bool CKeymapHandler::OnAnalogStickMotion(const FeatureName& feature,
110 float x,
111 float y,
112 unsigned int motionTimeMs)
113 {
114 using namespace INPUT;
115
116 bool bHandled = false;
117
118 // Calculate the direction of the stick's position
119 const ANALOG_STICK_DIRECTION analogStickDir = CInputTranslator::VectorToCardinalDirection(x, y);
120
121 // Calculate the magnitude projected onto that direction
122 const float magnitude = std::max(std::fabs(x), std::fabs(y));
123
124 // Deactivate directions in which the stick is not pointing first
125 for (auto dir : CJoystickUtils::GetAnalogStickDirections())
126 {
127 if (dir != analogStickDir)
128 DeactivateDirection(feature, dir);
129 }
130
131 // Now activate direction the analog stick is pointing
132 if (analogStickDir != ANALOG_STICK_DIRECTION::NONE)
133 bHandled = ActivateDirection(feature, magnitude, analogStickDir, motionTimeMs);
134
135 return bHandled;
136 }
137
138 bool CKeymapHandler::OnWheelMotion(const FeatureName& feature,
139 float position,
140 unsigned int motionTimeMs)
141 {
142 bool bHandled = false;
143
144 // Calculate the direction of the wheel's position
145 const WHEEL_DIRECTION direction = CJoystickTranslator::PositionToWheelDirection(position);
146
147 // Calculate the magnitude projected onto that direction
148 const float magnitude = std::fabs(position);
149
150 // Deactivate directions in which the wheel is not pointing first
151 for (auto dir : CJoystickUtils::GetWheelDirections())
152 {
153 if (dir != direction)
154 DeactivateDirection(feature, dir);
155 }
156
157 // Now activate direction in which the wheel is positioned
158 if (direction != WHEEL_DIRECTION::NONE)
159 bHandled = ActivateDirection(feature, magnitude, direction, motionTimeMs);
160
161 return bHandled;
162 }
163
164 bool CKeymapHandler::OnThrottleMotion(const FeatureName& feature,
165 float position,
166 unsigned int motionTimeMs)
167 {
168 bool bHandled = false;
169
170 // Calculate the direction of the throttle's position
171 const THROTTLE_DIRECTION direction = CJoystickTranslator::PositionToThrottleDirection(position);
172
173 // Calculate the magnitude projected onto that direction
174 const float magnitude = std::fabs(position);
175
176 // Deactivate directions in which the throttle is not pointing first
177 for (auto dir : CJoystickUtils::GetThrottleDirections())
178 {
179 if (dir != direction)
180 DeactivateDirection(feature, dir);
181 }
182
183 // Now activate direction in which the throttle is positioned
184 if (direction != THROTTLE_DIRECTION::NONE)
185 bHandled = ActivateDirection(feature, magnitude, direction, motionTimeMs);
186
187 return bHandled;
188 }
189
190 bool CKeymapHandler::OnAccelerometerMotion(const FeatureName& feature, float x, float y, float z)
191 {
192 return false; //! @todo implement
193 }
194
195 bool CKeymapHandler::ActivateDirection(const FeatureName& feature,
196 float magnitude,
197 ANALOG_STICK_DIRECTION dir,
198 unsigned int motionTimeMs)
199 {
200 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
201
202 IKeyHandler* handler = GetKeyHandler(keyName);
203 return handler->OnAnalogMotion(magnitude, motionTimeMs);
204 }
205
206 void CKeymapHandler::DeactivateDirection(const FeatureName& feature, ANALOG_STICK_DIRECTION dir)
207 {
208 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
209
210 IKeyHandler* handler = GetKeyHandler(keyName);
211 handler->OnAnalogMotion(0.0f, 0);
212 }
213
214 bool CKeymapHandler::ActivateDirection(const FeatureName& feature,
215 float magnitude,
216 WHEEL_DIRECTION dir,
217 unsigned int motionTimeMs)
218 {
219 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
220
221 IKeyHandler* handler = GetKeyHandler(keyName);
222 return handler->OnAnalogMotion(magnitude, motionTimeMs);
223 }
224
225 void CKeymapHandler::DeactivateDirection(const FeatureName& feature, WHEEL_DIRECTION dir)
226 {
227 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
228
229 IKeyHandler* handler = GetKeyHandler(keyName);
230 handler->OnAnalogMotion(0.0f, 0);
231 }
232
233 bool CKeymapHandler::ActivateDirection(const FeatureName& feature,
234 float magnitude,
235 THROTTLE_DIRECTION dir,
236 unsigned int motionTimeMs)
237 {
238 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
239
240 IKeyHandler* handler = GetKeyHandler(keyName);
241 return handler->OnAnalogMotion(magnitude, motionTimeMs);
242 }
243
244 void CKeymapHandler::DeactivateDirection(const FeatureName& feature, THROTTLE_DIRECTION dir)
245 {
246 const std::string keyName = CJoystickUtils::MakeKeyName(feature, dir);
247
248 IKeyHandler* handler = GetKeyHandler(keyName);
249 handler->OnAnalogMotion(0.0f, 0);
250 }
251
252 IKeyHandler* CKeymapHandler::GetKeyHandler(const std::string& keyName)
253 {
254 auto it = m_keyHandlers.find(keyName);
255 if (it == m_keyHandlers.end())
256 {
257 std::unique_ptr<IKeyHandler> handler(new CKeyHandler(keyName, m_actionHandler, m_keymap, this));
258 m_keyHandlers.insert(std::make_pair(keyName, std::move(handler)));
259 it = m_keyHandlers.find(keyName);
260 }
261
262 return it->second.get();
263 }
264
265 bool CKeymapHandler::HasAction(const std::string& keyName) const
266 {
267 bool bHasAction = false;
268
269 const auto& actions = m_keymap->GetActions(keyName).actions;
270 for (const auto& action : actions)
271 {
272 if (HotkeysPressed(action.hotkeys))
273 {
274 bHasAction = true;
275 break;
276 }
277 }
278
279 return bHasAction;
280 }