Debian Codegraph @

xbmc/input/joysticks/DriverPrimitive.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 "DriverPrimitive.h"
10
11 #include "input/keyboard/KeyboardTranslator.h"
12 #include "utils/StringUtils.h"
13
14 #include <utility>
15
16 using namespace KODI;
17 using namespace JOYSTICK;
18
19 CDriverPrimitive::CDriverPrimitive(void) = default;
20
21 CDriverPrimitive::CDriverPrimitive(PRIMITIVE_TYPE type, unsigned int index)
22 : m_type(type), m_driverIndex(index)
23 {
24 }
25
26 CDriverPrimitive::CDriverPrimitive(unsigned int hatIndex, HAT_DIRECTION direction)
27 : m_type(PRIMITIVE_TYPE::HAT), m_driverIndex(hatIndex), m_hatDirection(direction)
28 {
29 }
30
31 CDriverPrimitive::CDriverPrimitive(unsigned int axisIndex,
32 int center,
33 SEMIAXIS_DIRECTION direction,
34 unsigned int range)
35 : m_type(PRIMITIVE_TYPE::SEMIAXIS),
36 m_driverIndex(axisIndex),
37 m_center(center),
38 m_semiAxisDirection(direction),
39 m_range(range)
40 {
41 }
42
43 CDriverPrimitive::CDriverPrimitive(XBMCKey keycode)
44 : m_type(PRIMITIVE_TYPE::KEY), m_keycode(keycode)
45 {
46 }
47
48 CDriverPrimitive::CDriverPrimitive(MOUSE::BUTTON_ID index)
49 : m_type(PRIMITIVE_TYPE::MOUSE_BUTTON), m_driverIndex(static_cast<unsigned int>(index))
50 {
51 }
52
53 CDriverPrimitive::CDriverPrimitive(RELATIVE_POINTER_DIRECTION direction)
54 : m_type(PRIMITIVE_TYPE::RELATIVE_POINTER), m_pointerDirection(direction)
55 {
56 }
57
58 bool CDriverPrimitive::operator==(const CDriverPrimitive& rhs) const
59 {
60 if (m_type == rhs.m_type)
61 {
62 switch (m_type)
63 {
64 case PRIMITIVE_TYPE::BUTTON:
65 case PRIMITIVE_TYPE::MOTOR:
66 case PRIMITIVE_TYPE::MOUSE_BUTTON:
67 return m_driverIndex == rhs.m_driverIndex;
68 case PRIMITIVE_TYPE::HAT:
69 return m_driverIndex == rhs.m_driverIndex && m_hatDirection == rhs.m_hatDirection;
70 case PRIMITIVE_TYPE::SEMIAXIS:
71 return m_driverIndex == rhs.m_driverIndex && m_center == rhs.m_center &&
72 m_semiAxisDirection == rhs.m_semiAxisDirection && m_range == rhs.m_range;
73 case PRIMITIVE_TYPE::KEY:
74 return m_keycode == rhs.m_keycode;
75 case PRIMITIVE_TYPE::RELATIVE_POINTER:
76 return m_pointerDirection == rhs.m_pointerDirection;
77 default:
78 return true;
79 }
80 }
81 return false;
82 }
83
84 bool CDriverPrimitive::operator<(const CDriverPrimitive& rhs) const
85 {
86 if (m_type < rhs.m_type)
87 return true;
88 if (m_type > rhs.m_type)
89 return false;
90
91 if (m_type == PRIMITIVE_TYPE::BUTTON || m_type == PRIMITIVE_TYPE::HAT ||
92 m_type == PRIMITIVE_TYPE::SEMIAXIS || m_type == PRIMITIVE_TYPE::MOTOR ||
93 m_type == PRIMITIVE_TYPE::MOUSE_BUTTON)
94 {
95 if (m_driverIndex < rhs.m_driverIndex)
96 return true;
97 if (m_driverIndex > rhs.m_driverIndex)
98 return false;
99 }
100
101 if (m_type == PRIMITIVE_TYPE::HAT)
102 {
103 if (m_hatDirection < rhs.m_hatDirection)
104 return true;
105 if (m_hatDirection > rhs.m_hatDirection)
106 return false;
107 }
108
109 if (m_type == PRIMITIVE_TYPE::SEMIAXIS)
110 {
111 if (m_center < rhs.m_center)
112 return true;
113 if (m_center > rhs.m_center)
114 return false;
115
116 if (m_semiAxisDirection < rhs.m_semiAxisDirection)
117 return true;
118 if (m_semiAxisDirection > rhs.m_semiAxisDirection)
119 return false;
120
121 if (m_range < rhs.m_range)
122 return true;
123 if (m_range > rhs.m_range)
124 return false;
125 }
126
127 if (m_type == PRIMITIVE_TYPE::KEY)
128 {
129 if (m_keycode < rhs.m_keycode)
130 return true;
131 if (m_keycode > rhs.m_keycode)
132 return false;
133 }
134
135 if (m_type == PRIMITIVE_TYPE::RELATIVE_POINTER)
136 {
137 if (m_pointerDirection < rhs.m_pointerDirection)
138 return true;
139 if (m_pointerDirection > rhs.m_pointerDirection)
140 return false;
141 }
142
143 return false;
144 }
145
146 bool CDriverPrimitive::IsValid(void) const
147 {
148 if (m_type == PRIMITIVE_TYPE::BUTTON || m_type == PRIMITIVE_TYPE::MOTOR ||
149 m_type == PRIMITIVE_TYPE::MOUSE_BUTTON)
150 return true;
151
152 if (m_type == PRIMITIVE_TYPE::HAT)
153 {
154 return m_hatDirection == HAT_DIRECTION::UP || m_hatDirection == HAT_DIRECTION::DOWN ||
155 m_hatDirection == HAT_DIRECTION::RIGHT || m_hatDirection == HAT_DIRECTION::LEFT;
156 }
157
158 if (m_type == PRIMITIVE_TYPE::SEMIAXIS)
159 {
160 unsigned int maxRange = 1;
161
162 switch (m_center)
163 {
164 case -1:
165 {
166 if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE)
167 return false;
168 maxRange = 2;
169 break;
170 }
171 case 0:
172 {
173 if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE &&
174 m_semiAxisDirection != SEMIAXIS_DIRECTION::NEGATIVE)
175 return false;
176 break;
177 }
178 case 1:
179 {
180 if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE)
181 return false;
182 maxRange = 2;
183 break;
184 }
185 default:
186 break;
187 }
188
189 return 1 <= m_range && m_range <= maxRange;
190 }
191
192 if (m_type == PRIMITIVE_TYPE::KEY)
193 return m_keycode != XBMCK_UNKNOWN;
194
195 if (m_type == PRIMITIVE_TYPE::RELATIVE_POINTER)
196 {
197 return m_pointerDirection == RELATIVE_POINTER_DIRECTION::UP ||
198 m_pointerDirection == RELATIVE_POINTER_DIRECTION::DOWN ||
199 m_pointerDirection == RELATIVE_POINTER_DIRECTION::RIGHT ||
200 m_pointerDirection == RELATIVE_POINTER_DIRECTION::LEFT;
201 }
202
203 return false;
204 }
205
206 std::string CDriverPrimitive::ToString() const
207 {
208 switch (m_type)
209 {
210 case PRIMITIVE_TYPE::BUTTON:
211 return StringUtils::Format("button {}", m_driverIndex);
212 case PRIMITIVE_TYPE::MOTOR:
213 return StringUtils::Format("motor {}", m_driverIndex);
214 case PRIMITIVE_TYPE::MOUSE_BUTTON:
215 return StringUtils::Format("mouse button {}", m_driverIndex);
216 case PRIMITIVE_TYPE::HAT:
217 {
218 switch (m_hatDirection)
219 {
220 case HAT_DIRECTION::UP:
221 return StringUtils::Format("hat {} up", m_driverIndex);
222 case HAT_DIRECTION::DOWN:
223 return StringUtils::Format("hat {} down", m_driverIndex);
224 case HAT_DIRECTION::RIGHT:
225 return StringUtils::Format("hat {} right", m_driverIndex);
226 case HAT_DIRECTION::LEFT:
227 return StringUtils::Format("hat {} left", m_driverIndex);
228 default:
229 break;
230 }
231 break;
232 }
233 case PRIMITIVE_TYPE::SEMIAXIS:
234 {
235 switch (m_semiAxisDirection)
236 {
237 case SEMIAXIS_DIRECTION::POSITIVE:
238 return StringUtils::Format("semiaxis +{}", m_driverIndex);
239 case SEMIAXIS_DIRECTION::NEGATIVE:
240 return StringUtils::Format("semiaxis -{}", m_driverIndex);
241 default:
242 break;
243 }
244 break;
245 }
246 case PRIMITIVE_TYPE::KEY:
247 return StringUtils::Format("key {}",
248 KEYBOARD::CKeyboardTranslator::TranslateKeycode(m_keycode));
249 case PRIMITIVE_TYPE::RELATIVE_POINTER:
250 {
251 switch (m_pointerDirection)
252 {
253 case RELATIVE_POINTER_DIRECTION::UP:
254 return StringUtils::Format("pointer {} up", m_driverIndex);
255 case RELATIVE_POINTER_DIRECTION::DOWN:
256 return StringUtils::Format("pointer {} down", m_driverIndex);
257 case RELATIVE_POINTER_DIRECTION::RIGHT:
258 return StringUtils::Format("pointer {} right", m_driverIndex);
259 case RELATIVE_POINTER_DIRECTION::LEFT:
260 return StringUtils::Format("pointer {} left", m_driverIndex);
261 default:
262 break;
263 }
264 break;
265 }
266 default:
267 break;
268 }
269
270 return "";
271 }