Debian Codegraph @

xbmc/input/joysticks/generic/InputHandling.cpp

raw
1 /*
2 * Copyright (C) 2014-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 "InputHandling.h"
10
11 #include "input/joysticks/DriverPrimitive.h"
12 #include "input/joysticks/JoystickUtils.h"
13 #include "input/joysticks/dialogs/GUIDialogNewJoystick.h"
14 #include "input/joysticks/interfaces/IButtonMap.h"
15 #include "input/joysticks/interfaces/IInputHandler.h"
16 #include "utils/log.h"
17
18 #include <array>
19 #include <cmath>
20 #include <tuple>
21
22 using namespace KODI;
23 using namespace JOYSTICK;
24
25 CGUIDialogNewJoystick* const CInputHandling::m_dialog = new CGUIDialogNewJoystick;
26
27 CInputHandling::CInputHandling(IInputHandler* handler, IButtonMap* buttonMap)
28 : m_handler(handler), m_buttonMap(buttonMap)
29 {
30 }
31
32 CInputHandling::~CInputHandling(void) = default;
33
34 bool CInputHandling::OnButtonMotion(unsigned int buttonIndex, bool bPressed)
35 {
36 return OnDigitalMotion(CDriverPrimitive(PRIMITIVE_TYPE::BUTTON, buttonIndex), bPressed);
37 }
38
39 bool CInputHandling::OnHatMotion(unsigned int hatIndex, HAT_STATE state)
40 {
41 bool bHandled = false;
42
43 bHandled |=
44 OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::UP), state & HAT_DIRECTION::UP);
45 bHandled |= OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::RIGHT),
46 state & HAT_DIRECTION::RIGHT);
47 bHandled |=
48 OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::DOWN), state & HAT_DIRECTION::DOWN);
49 bHandled |=
50 OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::LEFT), state & HAT_DIRECTION::LEFT);
51
52 return bHandled;
53 }
54
55 bool CInputHandling::OnAxisMotion(unsigned int axisIndex,
56 float position,
57 int center,
58 unsigned int range)
59 {
60 bool bHandled = false;
61
62 if (center != 0)
63 {
64 float translatedPostion = std::min((position - center) / range, 1.0f);
65
66 // Calculate the direction the trigger travels from the center point
67 SEMIAXIS_DIRECTION dir;
68 if (center > 0)
69 dir = SEMIAXIS_DIRECTION::NEGATIVE;
70 else
71 dir = SEMIAXIS_DIRECTION::POSITIVE;
72
73 CDriverPrimitive offsetSemiaxis(axisIndex, center, dir, range);
74
75 bHandled = OnAnalogMotion(offsetSemiaxis, translatedPostion);
76 }
77 else
78 {
79 CDriverPrimitive positiveSemiaxis(axisIndex, 0, SEMIAXIS_DIRECTION::POSITIVE, 1);
80 CDriverPrimitive negativeSemiaxis(axisIndex, 0, SEMIAXIS_DIRECTION::NEGATIVE, 1);
81
82 bHandled |= OnAnalogMotion(positiveSemiaxis, position > 0.0f ? position : 0.0f);
83 bHandled |= OnAnalogMotion(negativeSemiaxis, position < 0.0f ? -position : 0.0f);
84 }
85
86 return bHandled;
87 }
88
89 void CInputHandling::OnInputFrame(void)
90 {
91 // Handle driver input
92 for (auto& it : m_features)
93 it.second->ProcessMotions();
94
95 // Handle higher-level controller input
96 m_handler->OnInputFrame();
97 }
98
99 bool CInputHandling::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
100 {
101 bool bHandled = false;
102
103 FeatureName featureName;
104 if (m_buttonMap->GetFeature(source, featureName))
105 {
106 auto it = m_features.find(featureName);
107 if (it == m_features.end())
108 {
109 FeaturePtr feature(CreateFeature(featureName));
110 if (feature)
111 std::tie(it, std::ignore) = m_features.insert({featureName, std::move(feature)});
112 }
113
114 if (it != m_features.end())
115 bHandled = it->second->OnDigitalMotion(source, bPressed);
116 }
117 else if (bPressed)
118 {
119 // If button didn't resolve to a feature, check if the button map is empty
120 // and ask the user if they would like to start mapping the controller
121 if (m_buttonMap->IsEmpty())
122 {
123 CLog::Log(LOGDEBUG, "Empty button map detected for {}", m_buttonMap->ControllerID());
124 m_dialog->ShowAsync();
125 }
126 }
127
128 return bHandled;
129 }
130
131 bool CInputHandling::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
132 {
133 bool bHandled = false;
134
135 FeatureName featureName;
136 if (m_buttonMap->GetFeature(source, featureName))
137 {
138 auto it = m_features.find(featureName);
139 if (it == m_features.end())
140 {
141 FeaturePtr feature(CreateFeature(featureName));
142 if (feature)
143 std::tie(it, std::ignore) = m_features.insert({featureName, std::move(feature)});
144 }
145
146 if (it != m_features.end())
147 bHandled = it->second->OnAnalogMotion(source, magnitude);
148 }
149
150 return bHandled;
151 }
152
153 CJoystickFeature* CInputHandling::CreateFeature(const FeatureName& featureName)
154 {
155 CJoystickFeature* feature = nullptr;
156
157 switch (m_buttonMap->GetFeatureType(featureName))
158 {
159 case FEATURE_TYPE::SCALAR:
160 {
161 feature = new CScalarFeature(featureName, m_handler, m_buttonMap);
162 break;
163 }
164 case FEATURE_TYPE::ANALOG_STICK:
165 {
166 feature = new CAnalogStick(featureName, m_handler, m_buttonMap);
167 break;
168 }
169 case FEATURE_TYPE::ACCELEROMETER:
170 {
171 feature = new CAccelerometer(featureName, m_handler, m_buttonMap);
172 break;
173 }
174 default:
175 break;
176 }
177
178 return feature;
179 }