1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "keyboarddevice_p.h" |
41 | |
42 | #include <Qt3DInput/QKeyEvent> |
43 | #include <Qt3DCore/qnode.h> |
44 | |
45 | #include <Qt3DInput/private/inputhandler_p.h> |
46 | #include <Qt3DInput/private/inputmanagers_p.h> |
47 | |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | namespace Qt3DInput { |
52 | namespace Input { |
53 | |
54 | namespace { |
55 | |
56 | QPair<int, int> getKeyPosition(int key) |
57 | { |
58 | QPair<int, int> position; |
59 | |
60 | switch (key) { |
61 | case Qt::Key_Escape: |
62 | return QPair<int, int>(0, 0); |
63 | case Qt::Key_Tab: |
64 | return QPair<int, int>(0, 1); |
65 | case Qt::Key_Backtab: |
66 | return QPair<int, int>(0, 2); |
67 | case Qt::Key_Backspace: |
68 | return QPair<int, int>(0, 3); |
69 | case Qt::Key_Return: |
70 | return QPair<int, int>(0, 4); |
71 | case Qt::Key_Enter: |
72 | return QPair<int, int>(0, 5); |
73 | case Qt::Key_Insert: |
74 | return QPair<int, int>(0, 6); |
75 | case Qt::Key_Delete: |
76 | return QPair<int, int>(0, 7); |
77 | case Qt::Key_Pause: |
78 | return QPair<int, int>(0, 8); |
79 | case Qt::Key_Print: |
80 | return QPair<int, int>(0, 9); |
81 | case Qt::Key_SysReq: |
82 | return QPair<int, int>(0, 10); |
83 | case Qt::Key_Clear: |
84 | return QPair<int, int>(0, 11); |
85 | case Qt::Key_Home: |
86 | return QPair<int, int>(0, 12); |
87 | case Qt::Key_End: |
88 | return QPair<int, int>(0, 13); |
89 | case Qt::Key_Left: |
90 | return QPair<int, int>(0, 14); |
91 | case Qt::Key_Right: |
92 | return QPair<int, int>(0, 16); |
93 | case Qt::Key_Up: |
94 | return QPair<int, int>(0, 15); |
95 | case Qt::Key_Down: |
96 | return QPair<int, int>(0, 17); |
97 | case Qt::Key_PageUp: |
98 | return QPair<int, int>(0, 18); |
99 | case Qt::Key_PageDown: |
100 | return QPair<int, int>(0, 19); |
101 | case Qt::Key_Shift: |
102 | return QPair<int, int>(0, 20); |
103 | case Qt::Key_Alt: |
104 | return QPair<int, int>(0, 23); |
105 | case Qt::Key_Control: |
106 | return QPair<int, int>(0, 21); |
107 | case Qt::Key_Meta: |
108 | return QPair<int, int>(0, 22); |
109 | case Qt::Key_CapsLock: |
110 | return QPair<int, int>(0, 24); |
111 | case Qt::Key_NumLock: |
112 | return QPair<int, int>(0, 25); |
113 | case Qt::Key_ScrollLock: |
114 | return QPair<int, int>(0, 26); |
115 | case Qt::Key_F1: |
116 | return QPair<int, int>(0, 27); |
117 | case Qt::Key_F2: |
118 | return QPair<int, int>(0, 28); |
119 | case Qt::Key_F3: |
120 | return QPair<int, int>(0, 29); |
121 | case Qt::Key_F4: |
122 | return QPair<int, int>(0, 30); |
123 | case Qt::Key_F5: |
124 | return QPair<int, int>(0, 31); |
125 | case Qt::Key_F6: |
126 | return QPair<int, int>(1, 0); |
127 | case Qt::Key_F7: |
128 | return QPair<int, int>(1, 1); |
129 | case Qt::Key_F8: |
130 | return QPair<int, int>(1, 2); |
131 | case Qt::Key_F9: |
132 | return QPair<int, int>(1, 3); |
133 | case Qt::Key_F10: |
134 | return QPair<int, int>(1, 4); |
135 | case Qt::Key_F11: |
136 | return QPair<int, int>(1, 5); |
137 | case Qt::Key_F12: |
138 | return QPair<int, int>(1, 6); |
139 | case Qt::Key_F13: |
140 | return QPair<int, int>(1, 7); |
141 | case Qt::Key_F14: |
142 | return QPair<int, int>(1, 8); |
143 | case Qt::Key_F15: |
144 | return QPair<int, int>(1, 9); |
145 | case Qt::Key_F16: |
146 | return QPair<int, int>(1, 10); |
147 | case Qt::Key_F17: |
148 | return QPair<int, int>(1, 11); |
149 | case Qt::Key_F18: |
150 | return QPair<int, int>(1, 12); |
151 | case Qt::Key_F19: |
152 | return QPair<int, int>(1, 13); |
153 | case Qt::Key_F20: |
154 | return QPair<int, int>(1, 14); |
155 | case Qt::Key_F21: |
156 | return QPair<int, int>(1, 15); |
157 | case Qt::Key_F22: |
158 | return QPair<int, int>(1, 16); |
159 | case Qt::Key_F23: |
160 | return QPair<int, int>(1, 17); |
161 | case Qt::Key_F24: |
162 | return QPair<int, int>(1, 18); |
163 | case Qt::Key_F25: |
164 | return QPair<int, int>(1, 19); |
165 | case Qt::Key_F26: |
166 | return QPair<int, int>(1, 20); |
167 | case Qt::Key_F27: |
168 | return QPair<int, int>(1, 21); |
169 | case Qt::Key_F28: |
170 | return QPair<int, int>(1, 22); |
171 | case Qt::Key_F29: |
172 | return QPair<int, int>(1, 23); |
173 | case Qt::Key_F30: |
174 | return QPair<int, int>(1, 24); |
175 | case Qt::Key_F31: |
176 | return QPair<int, int>(1, 25); |
177 | case Qt::Key_F32: |
178 | return QPair<int, int>(1, 26); |
179 | case Qt::Key_F33: |
180 | return QPair<int, int>(1, 27); |
181 | case Qt::Key_F34: |
182 | return QPair<int, int>(1, 28); |
183 | case Qt::Key_F35: |
184 | return QPair<int, int>(1, 29); |
185 | case Qt::Key_Super_L: |
186 | return QPair<int, int>(1, 30); |
187 | case Qt::Key_Super_R: |
188 | return QPair<int, int>(1, 31); |
189 | case Qt::Key_Menu: |
190 | return QPair<int, int>(2, 1); |
191 | case Qt::Key_Help: |
192 | return QPair<int, int>(2, 4); |
193 | case Qt::Key_Hyper_L: |
194 | return QPair<int, int>(2, 2); |
195 | case Qt::Key_Hyper_R: |
196 | return QPair<int, int>(2, 3); |
197 | case Qt::Key_Direction_L: |
198 | return QPair<int, int>(2, 5); |
199 | case Qt::Key_Direction_R: |
200 | return QPair<int, int>(2, 6); |
201 | case Qt::Key_Space: |
202 | return QPair<int, int>(2, 7); |
203 | case Qt::Key_Exclam: |
204 | return QPair<int, int>(2, 9); |
205 | case Qt::Key_QuoteDbl: |
206 | return QPair<int, int>(2, 10); |
207 | case Qt::Key_NumberSign: |
208 | return QPair<int, int>(2, 11); |
209 | case Qt::Key_Dollar: |
210 | return QPair<int, int>(2, 12); |
211 | case Qt::Key_Percent: |
212 | return QPair<int, int>(2, 13); |
213 | case Qt::Key_Ampersand: |
214 | return QPair<int, int>(2, 14); |
215 | case Qt::Key_Apostrophe: |
216 | return QPair<int, int>(2, 15); |
217 | case Qt::Key_ParenLeft: |
218 | return QPair<int, int>(2, 16); |
219 | case Qt::Key_ParenRight: |
220 | return QPair<int, int>(2, 17); |
221 | case Qt::Key_Asterisk: |
222 | return QPair<int, int>(2, 18); |
223 | case Qt::Key_Plus: |
224 | return QPair<int, int>(2, 19); |
225 | case Qt::Key_Comma: |
226 | return QPair<int, int>(2, 20); |
227 | case Qt::Key_Minus: |
228 | return QPair<int, int>(2, 21); |
229 | case Qt::Key_Period: |
230 | return QPair<int, int>(2, 22); |
231 | case Qt::Key_Slash: |
232 | return QPair<int, int>(2, 23); |
233 | case Qt::Key_0: |
234 | return QPair<int, int>(2, 24); |
235 | case Qt::Key_1: |
236 | return QPair<int, int>(2, 25); |
237 | case Qt::Key_2: |
238 | return QPair<int, int>(2, 26); |
239 | case Qt::Key_3: |
240 | return QPair<int, int>(2, 27); |
241 | case Qt::Key_4: |
242 | return QPair<int, int>(2, 28); |
243 | case Qt::Key_5: |
244 | return QPair<int, int>(2, 29); |
245 | case Qt::Key_6: |
246 | return QPair<int, int>(2, 30); |
247 | case Qt::Key_7: |
248 | return QPair<int, int>(2, 31); |
249 | case Qt::Key_8: |
250 | return QPair<int, int>(3, 0); |
251 | case Qt::Key_9: |
252 | return QPair<int, int>(3, 1); |
253 | case Qt::Key_Colon: |
254 | return QPair<int, int>(3, 2); |
255 | case Qt::Key_Semicolon: |
256 | return QPair<int, int>(3, 3); |
257 | case Qt::Key_Less: |
258 | return QPair<int, int>(3, 4); |
259 | case Qt::Key_Equal: |
260 | return QPair<int, int>(3, 5); |
261 | case Qt::Key_Greater: |
262 | return QPair<int, int>(3, 6); |
263 | case Qt::Key_Question: |
264 | return QPair<int, int>(3, 7); |
265 | case Qt::Key_At: |
266 | return QPair<int, int>(3, 8); |
267 | case Qt::Key_A: |
268 | return QPair<int, int>(3, 9); |
269 | case Qt::Key_B: |
270 | return QPair<int, int>(3, 10); |
271 | case Qt::Key_C: |
272 | return QPair<int, int>(3, 11); |
273 | case Qt::Key_D: |
274 | return QPair<int, int>(3, 12); |
275 | case Qt::Key_E: |
276 | return QPair<int, int>(3, 13); |
277 | case Qt::Key_F: |
278 | return QPair<int, int>(3, 14); |
279 | case Qt::Key_G: |
280 | return QPair<int, int>(3, 15); |
281 | case Qt::Key_H: |
282 | return QPair<int, int>(3, 16); |
283 | case Qt::Key_I: |
284 | return QPair<int, int>(3, 17); |
285 | case Qt::Key_J: |
286 | return QPair<int, int>(3, 18); |
287 | case Qt::Key_K: |
288 | return QPair<int, int>(3, 19); |
289 | case Qt::Key_L: |
290 | return QPair<int, int>(3, 20); |
291 | case Qt::Key_M: |
292 | return QPair<int, int>(3, 21); |
293 | case Qt::Key_N: |
294 | return QPair<int, int>(3, 22); |
295 | case Qt::Key_O: |
296 | return QPair<int, int>(3, 23); |
297 | case Qt::Key_P: |
298 | return QPair<int, int>(3, 24); |
299 | case Qt::Key_Q: |
300 | return QPair<int, int>(3, 25); |
301 | case Qt::Key_R: |
302 | return QPair<int, int>(3, 26); |
303 | case Qt::Key_S: |
304 | return QPair<int, int>(3, 27); |
305 | case Qt::Key_T: |
306 | return QPair<int, int>(3, 28); |
307 | case Qt::Key_U: |
308 | return QPair<int, int>(3, 29); |
309 | case Qt::Key_V: |
310 | return QPair<int, int>(3, 30); |
311 | case Qt::Key_W: |
312 | return QPair<int, int>(3, 31); |
313 | case Qt::Key_X: |
314 | return QPair<int, int>(4, 0); |
315 | case Qt::Key_Y: |
316 | return QPair<int, int>(4, 1); |
317 | case Qt::Key_Z: |
318 | return QPair<int, int>(4, 2); |
319 | case Qt::Key_BracketLeft: |
320 | return QPair<int, int>(4, 3); |
321 | case Qt::Key_Backslash: |
322 | return QPair<int, int>(4, 4); |
323 | case Qt::Key_BracketRight: |
324 | return QPair<int, int>(4, 5); |
325 | case Qt::Key_AsciiCircum: |
326 | return QPair<int, int>(4, 6); |
327 | case Qt::Key_Underscore: |
328 | return QPair<int, int>(4, 7); |
329 | case Qt::Key_QuoteLeft: |
330 | return QPair<int, int>(4, 8); |
331 | case Qt::Key_BraceLeft: |
332 | return QPair<int, int>(4, 9); |
333 | case Qt::Key_Bar: |
334 | return QPair<int, int>(4, 10); |
335 | case Qt::Key_BraceRight: |
336 | return QPair<int, int>(4, 11); |
337 | case Qt::Key_AsciiTilde: |
338 | return QPair<int, int>(4, 12); |
339 | case Qt::Key_plusminus: |
340 | return QPair<int, int>(4, 13); |
341 | case Qt::Key_onesuperior: |
342 | return QPair<int, int>(4, 14); |
343 | case Qt::Key_multiply: |
344 | return QPair<int, int>(4, 15); |
345 | case Qt::Key_division: |
346 | return QPair<int, int>(4, 16); |
347 | case Qt::Key_diaeresis: |
348 | return QPair<int, int>(4, 17); |
349 | default: |
350 | return QPair<int, int>(-1, -1); |
351 | } |
352 | return position; |
353 | } |
354 | |
355 | } // anonymous |
356 | |
357 | |
358 | // TO DO: Send change to frontend when activeInput changes |
359 | |
360 | KeyboardDevice::KeyboardDevice() |
361 | : QAbstractPhysicalDeviceBackendNode(QBackendNode::ReadOnly) |
362 | , m_inputHandler(nullptr) |
363 | { |
364 | m_keyStates.keys[0] = 0; |
365 | m_keyStates.keys[1] = 0; |
366 | m_keyStates.keys[2] = 0; |
367 | m_keyStates.keys[3] = 0; |
368 | m_keyStates.keys[4] = 0; |
369 | } |
370 | |
371 | void KeyboardDevice::cleanup() |
372 | { |
373 | QAbstractPhysicalDeviceBackendNode::cleanup(); |
374 | m_keyStates.keys[0] = 0; |
375 | m_keyStates.keys[1] = 0; |
376 | m_keyStates.keys[2] = 0; |
377 | m_keyStates.keys[3] = 0; |
378 | m_keyStates.keys[4] = 0; |
379 | } |
380 | |
381 | void KeyboardDevice::requestFocusForInput(Qt3DCore::QNodeId inputId) |
382 | { |
383 | // Saves the last inputId, this will then be used in an Aspect Job to determine which |
384 | // input will have the focus. This in turn will call KeyboardInput::setFocus which will |
385 | // decide if sending a notification to the frontend is necessary |
386 | m_lastRequester = inputId; |
387 | } |
388 | |
389 | void KeyboardDevice::setInputHandler(InputHandler *handler) |
390 | { |
391 | m_inputHandler = handler; |
392 | } |
393 | |
394 | void KeyboardDevice::setCurrentFocusItem(Qt3DCore::QNodeId input) |
395 | { |
396 | m_currentFocusItem = input; |
397 | } |
398 | |
399 | float KeyboardDevice::axisValue(int axisIdentifier) const |
400 | { |
401 | Q_UNUSED(axisIdentifier); |
402 | return 0.0f; |
403 | } |
404 | |
405 | bool KeyboardDevice::isButtonPressed(int buttonIdentifier) const |
406 | { |
407 | QPair<int, int> position = getKeyPosition(key: buttonIdentifier); |
408 | if (position.first != -1 && position.second != -1) |
409 | return m_keyStates.keys[position.first] & (1 << position.second); |
410 | return false; |
411 | } |
412 | |
413 | void KeyboardDevice::setButtonValue(int key, bool value) |
414 | { |
415 | QPair<int, int> position = getKeyPosition(key); |
416 | if (position.first != -1 && position.second != -1) { |
417 | if (value) |
418 | m_keyStates.keys[position.first] |= (1 << position.second); |
419 | else |
420 | m_keyStates.keys[position.first] &= ~(1 << position.second); |
421 | } |
422 | } |
423 | |
424 | void KeyboardDevice::updateKeyEvents(const QList<QT_PREPEND_NAMESPACE(QKeyEvent)> &events) |
425 | { |
426 | for (const QT_PREPEND_NAMESPACE(QKeyEvent) &e : events) |
427 | setButtonValue(key: e.key(), value: e.type() == QT_PREPEND_NAMESPACE(QKeyEvent)::KeyPress ? true : false); |
428 | } |
429 | |
430 | KeyboardDeviceFunctor::KeyboardDeviceFunctor(QInputAspect *inputaspect, InputHandler *handler) |
431 | : m_inputAspect(inputaspect) |
432 | , m_handler(handler) |
433 | { |
434 | } |
435 | |
436 | Qt3DCore::QBackendNode *KeyboardDeviceFunctor::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const |
437 | { |
438 | KeyboardDevice *keyboardDevice = m_handler->keyboardDeviceManager()->getOrCreateResource(id: change->subjectId()); |
439 | keyboardDevice->setInputAspect(m_inputAspect); |
440 | keyboardDevice->setInputHandler(m_handler); |
441 | m_handler->appendKeyboardDevice(device: m_handler->keyboardDeviceManager()->lookupHandle(id: change->subjectId())); |
442 | return keyboardDevice; |
443 | } |
444 | |
445 | Qt3DCore::QBackendNode *KeyboardDeviceFunctor::get(Qt3DCore::QNodeId id) const |
446 | { |
447 | return m_handler->keyboardDeviceManager()->lookupResource(id); |
448 | } |
449 | |
450 | void KeyboardDeviceFunctor::destroy(Qt3DCore::QNodeId id) const |
451 | { |
452 | m_handler->removeKeyboardDevice(device: m_handler->keyboardDeviceManager()->lookupHandle(id)); |
453 | m_handler->keyboardDeviceManager()->releaseResource(id); |
454 | } |
455 | |
456 | } // namespace Inputs |
457 | } // namespace Qt3DInput |
458 | |
459 | QT_END_NAMESPACE |
460 | |