Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
androidjniinput.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
3// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:significant reason:default
6
7#include <QtGui/qtguiglobal.h>
8
10#include "androidjnimain.h"
12
13#include <qpa/qplatformwindow.h>
14#include <qpa/qwindowsysteminterface.h>
15#include <QTouchEvent>
16#include <QPointer>
17
18#include <QGuiApplication>
19#include <QtMath>
20
22
23Q_LOGGING_CATEGORY(lcQpaInputMethods, "qt.qpa.input.methods");
24
25using namespace QtAndroid;
26
27Q_DECLARE_JNI_CLASS(QtInputInterface, "org/qtproject/qt/android/QtInputInterface")
28
30{
31 static bool m_ignoreMouseEvents = false;
33
35
37
38 static QPointer<QWindow> m_mouseGrabber;
39
40
41 void updateSelection(int selStart, int selEnd, int candidatesStart, int candidatesEnd)
42 {
43 qCDebug(lcQpaInputMethods) << ">>> UPDATESELECTION" << selStart << selEnd << candidatesStart << candidatesEnd;
45 reg->callInterface<QtJniTypes::QtInputInterface, void>("updateSelection", selStart, selEnd,
46 candidatesStart, candidatesEnd);
47 }
48
49 void showSoftwareKeyboard(int left, int top, int width, int height, int inputHints, int enterKeyType)
50 {
52 reg->callInterface<QtJniTypes::QtInputInterface, void>(
53 "showSoftwareKeyboard", QtAndroidPrivate::activity(),
54 left, top, width, height, inputHints,
55 enterKeyType);
56 qCDebug(lcQpaInputMethods) << "@@@ SHOWSOFTWAREKEYBOARD" << left << top << width << height << inputHints << enterKeyType;
57 }
58
60 {
62 reg->callInterface<QtJniTypes::QtInputInterface>("resetSoftwareKeyboard");
63 qCDebug(lcQpaInputMethods) << "@@@ RESETSOFTWAREKEYBOARD";
64 }
65
67 {
69 reg->callInterface<QtJniTypes::QtInputInterface>("hideSoftwareKeyboard");
70 qCDebug(lcQpaInputMethods) << "@@@ HIDESOFTWAREKEYBOARD";
71 }
72
74 {
76 return reg->callInterface<QtJniTypes::QtInputInterface, jboolean>(
77 "isSoftwareKeyboardVisible");
78 }
79
81 {
82 return m_softwareKeyboardRect;
83 }
84
86 {
88 return reg->callInterface<QtJniTypes::QtInputInterface, jint>("getSelectionHandleWidth");
89 }
90
91 void updateHandles(int mode, QPoint editMenuPos, uint32_t editButtons, QPoint cursor, QPoint anchor, bool rtl)
92 {
94 reg->callInterface<QtJniTypes::QtInputInterface, void>(
95 "updateHandles", mode, editMenuPos.x(), editMenuPos.y(),
96 editButtons, cursor.x(), cursor.y(), anchor.x(), anchor.y(), rtl);
97 }
98
99 // from https://developer.android.com/reference/android/view/MotionEvent#getButtonState()
101 BUTTON_PRIMARY = 0x00000001,
102 BUTTON_SECONDARY = 0x00000002,
103 BUTTON_TERTIARY = 0x00000004,
104 BUTTON_BACK = 0x00000008,
105 BUTTON_FORWARD = 0x00000010,
108 };
109 Q_DECLARE_FLAGS(AndroidMouseButtons, AndroidMouseButton)
110
111 static Qt::MouseButtons toMouseButtons(jint j_buttons)
112 {
113 const auto buttons = static_cast<AndroidMouseButtons>(j_buttons);
114 Qt::MouseButtons mouseButtons;
115 if (buttons.testFlag(BUTTON_PRIMARY))
116 mouseButtons.setFlag(Qt::LeftButton);
117
118 if (buttons.testFlag(BUTTON_SECONDARY))
119 mouseButtons.setFlag(Qt::RightButton);
120
121 if (buttons.testFlag(BUTTON_TERTIARY))
122 mouseButtons.setFlag(Qt::MiddleButton);
123
124 if (buttons.testFlag(BUTTON_BACK))
125 mouseButtons.setFlag(Qt::BackButton);
126
127 if (buttons.testFlag(BUTTON_FORWARD))
128 mouseButtons.setFlag(Qt::ForwardButton);
129
130 if (buttons.testFlag(BUTTON_STYLUS_PRIMARY))
131 mouseButtons.setFlag(Qt::LeftButton);
132
133 if (buttons.testFlag(BUTTON_STYLUS_SECONDARY))
134 mouseButtons.setFlag(Qt::RightButton);
135
136 // Fall back to left button
137 if (Q_UNLIKELY(buttons != 0 && mouseButtons == Qt::NoButton)) {
138 qWarning() << "Unhandled button value:" << buttons << "Falling back to Qt::LeftButton";
139 mouseButtons = Qt::LeftButton;
140 }
141 return mouseButtons;
142 }
143
144 static void sendMouseButtonEvents(QWindow *topLevel, QPoint localPos, QPoint globalPos,
145 jint mouseButtonState, QEvent::Type type)
146 {
147 const Qt::MouseButtons qtButtons = toMouseButtons(mouseButtonState);
148 const bool mouseReleased = type == QEvent::MouseButtonRelease && qtButtons == Qt::NoButton;
149 const Qt::MouseButtons eventButtons = mouseReleased ? m_lastSeenButtons : qtButtons;
150 m_lastSeenButtons = qtButtons;
151
152 static_assert (sizeof(eventButtons) <= sizeof(uint), "Qt::MouseButtons size changed. Adapt code.");
153
154 if (eventButtons == Qt::NoButton)
155 return;
156
157 for (uint buttonInt = 0x1; static_cast<uint>(eventButtons) >= buttonInt; buttonInt <<= 1) {
158 const auto button = static_cast<Qt::MouseButton>(buttonInt);
159 if (eventButtons.testFlag(button)) {
160 QWindowSystemInterface::handleMouseEvent(topLevel, localPos, globalPos,
161 qtButtons, button, type);
162 }
163 }
164 }
165
166 static void mouseDown(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y, jint mouseButtonState)
167 {
169 return;
170
171 const QPoint localPos(x,y);
172 QWindow *window = windowFromId(winId);
173 m_mouseGrabber = window;
174 const QPoint globalPos = window && window->handle() ?
175 window->handle()->mapToGlobal(localPos) : localPos;
176 sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, QEvent::MouseButtonPress);
177 }
178
179 static void mouseUp(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y, jint mouseButtonState)
180 {
181 const QPoint localPos(x,y);
182 QWindow *window = m_mouseGrabber.data();
183 if (!window)
184 window = windowFromId(winId);
185
186 const QPoint globalPos = window && window->handle() ?
187 window->handle()->mapToGlobal(localPos) : localPos;
188
189 sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, QEvent::MouseButtonRelease);
190 m_ignoreMouseEvents = false;
191 m_mouseGrabber.clear();
192 }
193
194 static void mouseMove(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y, jint mouseButtonState)
195 {
197 return;
198
199 const QPoint localPos(x,y);
200 QWindow *window = m_mouseGrabber.data();
201 if (!window)
202 window = windowFromId(winId);
203 const QPoint globalPos = window && window->handle() ?
204 window->handle()->mapToGlobal(localPos) : localPos;
205 const Qt::MouseButtons qtButtons = toMouseButtons(mouseButtonState);
206 m_lastSeenButtons = qtButtons;
207 QWindowSystemInterface::handleMouseEvent(window, localPos, globalPos,
208 qtButtons, Qt::NoButton, QEvent::MouseMove);
209 }
210
211 static void mouseWheel(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y, jfloat hdelta, jfloat vdelta)
212 {
214 return;
215
216 const QPoint localPos(x,y);
217 QWindow *window = m_mouseGrabber.data();
218 if (!window)
219 window = windowFromId(winId);
220 const QPoint globalPos = window && window->handle() ?
221 window->handle()->mapToGlobal(localPos) : localPos;
222 const QPoint angleDelta(hdelta * 120, vdelta * 120);
223
224 QWindowSystemInterface::handleWheelEvent(window,
225 localPos,
226 globalPos,
227 QPoint(),
228 angleDelta);
229 }
230
231 static void longPress(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y)
232 {
234
235 const QPoint localPos(x,y);
236 QWindow *window = windowFromId(winId);
237 const QPoint globalPos = window && window->handle() ?
238 window->handle()->mapToGlobal(localPos) : localPos;
239
240 if (inputContext && qGuiApp)
241 QMetaObject::invokeMethod(inputContext, "longPress", Q_ARG(int, globalPos.x()), Q_ARG(int, globalPos.y()));
242
243 //### TODO: add proper API for Qt 5.2
244 static bool rightMouseFromLongPress = qEnvironmentVariableIntValue("QT_ANDROID_ENABLE_RIGHT_MOUSE_FROM_LONG_PRESS");
245 if (!rightMouseFromLongPress)
246 return;
247 m_ignoreMouseEvents = true;
248
249
250 // Click right button if no other button is already pressed.
251 if (!m_mouseGrabber) {
252 QWindowSystemInterface::handleMouseEvent(window, localPos, globalPos,
253 Qt::MouseButtons(Qt::RightButton), Qt::RightButton,
254 QEvent::MouseButtonPress);
255 QWindowSystemInterface::handleMouseEvent(window, localPos, globalPos,
256 Qt::MouseButtons(Qt::NoButton), Qt::RightButton,
257 QEvent::MouseButtonRelease);
258 }
259 }
260
261 static void touchBegin(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/)
262 {
263 m_touchPoints.clear();
264 }
265
266 static void touchAdd(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint id, jint action, jboolean /*primary*/, jint x, jint y,
267 jfloat major, jfloat minor, jfloat rotation, jfloat pressure)
268 {
269 QEventPoint::State state = QEventPoint::State::Stationary;
270 switch (action) {
271 case 0:
272 state = QEventPoint::State::Pressed;
273 break;
274 case 1:
275 state = QEventPoint::State::Updated;
276 break;
277 case 2:
278 state = QEventPoint::State::Stationary;
279 break;
280 case 3:
281 state = QEventPoint::State::Released;
282 break;
283 }
284
285
286 QSize availableSize;
287 if (auto *platformIntegration = QtAndroid::androidPlatformIntegration())
288 availableSize = platformIntegration->screen()->availableGeometry().size();
289 else
291
292 QWindow *window = QtAndroid::windowFromId(winId);
293 if (!window) {
294 qCWarning(lcQpaInputMethods, "Touch event received for non-existing window %d", winId);
295 return;
296 }
297
298 QPointF mappedTouchPoint;
299 if (window->handle())
300 mappedTouchPoint = window->handle()->mapToGlobalF(QPointF(x, y));
301 else
302 mappedTouchPoint = window->mapToGlobal(QPointF(x, y));
303
304 QWindowSystemInterface::TouchPoint touchPoint;
305 // Start numbering touch points from 1
306 touchPoint.id = id + 1;
307 touchPoint.pressure = pressure;
308 touchPoint.rotation = qRadiansToDegrees(rotation);
309 touchPoint.normalPosition = QPointF((mappedTouchPoint.x() / availableSize.width()),
310 (mappedTouchPoint.y() / availableSize.height()));
311 touchPoint.state = state;
312 touchPoint.area = QRectF(mappedTouchPoint.x() - double(minor * 0.5f),
313 mappedTouchPoint.y() - double(major * 0.5f),
314 double(minor),
315 double(major));
316
317 m_touchPoints.push_back(touchPoint);
318 if (state == QEventPoint::State::Pressed) {
320 if (inputContext && qGuiApp)
321 QMetaObject::invokeMethod(inputContext, "touchDown", Q_ARG(int, mappedTouchPoint.x()), Q_ARG(int, mappedTouchPoint.y()));
322 }
323 }
324
326 {
328 if (!platformIntegration)
329 return nullptr;
330
331 QPointingDevice *touchDevice = platformIntegration->touchDevice();
332 if (!touchDevice) {
333 touchDevice = new QPointingDevice("Android touchscreen", 1,
334 QInputDevice::DeviceType::TouchScreen,
335 QPointingDevice::PointerType::Finger,
336 QPointingDevice::Capability::Position
337 | QPointingDevice::Capability::Area
338 | QPointingDevice::Capability::Pressure
339 | QPointingDevice::Capability::NormalizedPosition,
340 10, 0);
341 QWindowSystemInterface::registerInputDevice(touchDevice);
342 platformIntegration->setTouchDevice(touchDevice);
343 }
344
345 return touchDevice;
346 }
347
348 static void touchEnd(JNIEnv * /*env*/, jobject /*thiz*/, jint winId, jint /*action*/)
349 {
350 if (m_touchPoints.isEmpty())
351 return;
352
353 QMutexLocker lock(QtAndroid::platformInterfaceMutex());
354 const QPointingDevice *touchDevice = getTouchDevice();
355 if (!touchDevice)
356 return;
357
358 QWindow *window = QtAndroid::windowFromId(winId);
359 if (!window)
360 return;
361 QWindowSystemInterface::handleTouchEvent(window, touchDevice, m_touchPoints);
362 }
363
364 static void touchCancel(JNIEnv * /*env*/, jobject /*thiz*/, jint winId)
365 {
366 if (m_touchPoints.isEmpty())
367 return;
368
369 QMutexLocker lock(QtAndroid::platformInterfaceMutex());
370 const QPointingDevice *touchDevice = getTouchDevice();
371 if (!touchDevice)
372 return;
373
374 QWindow *window = QtAndroid::windowFromId(winId);
375 if (!window)
376 return;
377 QWindowSystemInterface::handleTouchCancelEvent(window, touchDevice);
378 }
379
380 static bool isTabletEventSupported(JNIEnv */*env*/, jobject /*thiz*/)
381 {
382#if QT_CONFIG(tabletevent)
383 return true;
384#else
385 return false;
386#endif // QT_CONFIG(tabletevent)
387 }
388
389 static void tabletEvent(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint deviceId, jlong time, jint action,
390 jint pointerType, jint buttonState, jfloat x, jfloat y, jfloat pressure)
391 {
392#if QT_CONFIG(tabletevent)
393 const QPointF localPos(x, y);
394 QWindow *window = windowFromId(winId);
395 const QPointF globalPosF = window && window->handle() ?
396 window->handle()->mapFromGlobalF(localPos) : localPos;
397
398 // Galaxy Note with plain Android:
399 // 0 1 0 stylus press
400 // 2 1 0 stylus drag
401 // 1 1 0 stylus release
402 // 0 1 2 stylus press with side-button held
403 // 2 1 2 stylus drag with side-button held
404 // 1 1 2 stylus release with side-button held
405 // Galaxy Note 4 with Samsung firmware:
406 // 0 1 0 stylus press
407 // 2 1 0 stylus drag
408 // 1 1 0 stylus release
409 // 211 1 2 stylus press with side-button held
410 // 213 1 2 stylus drag with side-button held
411 // 212 1 2 stylus release with side-button held
412 // when action == ACTION_UP (1) it's a release; otherwise we say which button is pressed
413 Qt::MouseButtons buttons = Qt::NoButton;
414 switch (action) {
415 case 1: // ACTION_UP
416 case 6: // ACTION_POINTER_UP, happens if stylus is not the primary pointer
417 case 212: // stylus release while side-button held on Galaxy Note 4
418 buttons = Qt::NoButton;
419 break;
420 default: // action is press or drag
421 if (buttonState == 0)
422 buttons = Qt::LeftButton;
423 else // 2 means RightButton
424 buttons = Qt::MouseButtons(buttonState);
425 break;
426 }
427
428 qCDebug(lcQpaInputMethods) << action << pointerType << buttonState << '@' << x << y << "pressure" << pressure << ": buttons" << buttons;
429
430 QWindowSystemInterface::handleTabletEvent(window, ulong(time),
431 localPos, globalPosF, int(QInputDevice::DeviceType::Stylus), pointerType,
432 buttons, pressure, 0, 0, 0., 0., 0, deviceId, Qt::NoModifier);
433#endif // QT_CONFIG(tabletevent)
434 }
435
437 {
438 // 0--9 0x00000007 -- 0x00000010
439 if (key >= 0x00000007 && key <= 0x00000010)
440 return QKeyCombination::fromCombined(Qt::Key_0 + key - 0x00000007);
441
442 // A--Z 0x0000001d -- 0x00000036
443 if (key >= 0x0000001d && key <= 0x00000036)
444 return QKeyCombination::fromCombined(Qt::Key_A + key - 0x0000001d);
445
446 // F1--F12 0x00000083 -- 0x0000008e
447 if (key >= 0x00000083 && key <= 0x0000008e)
448 return QKeyCombination::fromCombined(Qt::Key_F1 + key - 0x00000083);
449
450 // NUMPAD_0--NUMPAD_9 0x00000090 -- 0x00000099
451 if (key >= 0x00000090 && key <= 0x00000099)
452 return QKeyCombination::fromCombined(Qt::KeypadModifier | Qt::Key_0 + key - 0x00000090);
453
454 // BUTTON_1--KEYCODE_BUTTON_16 0x000000bc -- 0x000000cb
455
456 switch (key) {
457 case 0x00000000: // KEYCODE_UNKNOWN
458 return Qt::Key_unknown;
459
460 case 0x00000001: // KEYCODE_SOFT_LEFT
461 return Qt::Key_Left;
462
463 case 0x00000002: // KEYCODE_SOFT_RIGHT
464 return Qt::Key_Right;
465
466 // 0x00000003: // KEYCODE_HOME is never delivered to applications.
467
468 case 0x00000004: // KEYCODE_BACK
469 return Qt::Key_Back;
470
471 case 0x00000005: // KEYCODE_CALL
472 return Qt::Key_Call;
473
474 case 0x00000006: // KEYCODE_ENDCALL
475 return Qt::Key_Hangup;
476
477 // 0--9 0x00000007 -- 0x00000010
478
479 case 0x00000011: // KEYCODE_STAR
480 return Qt::Key_Asterisk;
481
482 case 0x00000012: // KEYCODE_POUND
483 return Qt::Key_NumberSign;
484
485 case 0x00000013: //KEYCODE_DPAD_UP
486 return Qt::Key_Up;
487
488 case 0x00000014: // KEYCODE_DPAD_DOWN
489 return Qt::Key_Down;
490
491 case 0x00000015: //KEYCODE_DPAD_LEFT
492 return Qt::Key_Left;
493
494 case 0x00000016: //KEYCODE_DPAD_RIGHT
495 return Qt::Key_Right;
496
497 case 0x00000017: // KEYCODE_DPAD_CENTER
498 return Qt::Key_Enter;
499
500 case 0x00000018: // KEYCODE_VOLUME_UP
501 return Qt::Key_VolumeUp;
502
503 case 0x00000019: // KEYCODE_VOLUME_DOWN
504 return Qt::Key_VolumeDown;
505
506 case 0x0000001a:
507 return Qt::Key_PowerOff;
508
509 case 0x0000001b: // KEYCODE_CAMERA
510 return Qt::Key_Camera;
511
512 case 0x0000001c: // KEYCODE_CLEAR
513 return Qt::Key_Clear;
514
515 // A--Z 0x0000001d -- 0x00000036
516
517 case 0x00000037: // KEYCODE_COMMA
518 return Qt::Key_Comma;
519
520 case 0x00000038: // KEYCODE_PERIOD
521 return Qt::Key_Period;
522
523 case 0x00000039: // KEYCODE_ALT_LEFT
524 case 0x0000003a: // KEYCODE_ALT_RIGHT
525 return Qt::Key_Alt;
526
527 case 0x0000003b: // KEYCODE_SHIFT_LEFT
528 case 0x0000003c: // KEYCODE_SHIFT_RIGHT
529 return Qt::Key_Shift;
530
531 case 0x0000003d: // KEYCODE_TAB
532 return Qt::Key_Tab;
533
534 case 0x0000003e: // KEYCODE_SPACE
535 return Qt::Key_Space;
536
537 case 0x0000003f: // KEYCODE_SYM
538 return Qt::Key_Meta;
539
540 case 0x00000040: // KEYCODE_EXPLORER
541 return Qt::Key_Explorer;
542
543 case 0x00000041: //KEYCODE_ENVELOPE
544 return Qt::Key_LaunchMail;
545
546 case 0x00000042: // KEYCODE_ENTER
547 return Qt::Key_Return;
548
549 case 0x00000043: // KEYCODE_DEL
550 return Qt::Key_Backspace;
551
552 case 0x00000044: // KEYCODE_GRAVE
553 return Qt::Key_QuoteLeft;
554
555 case 0x00000045: // KEYCODE_MINUS
556 return Qt::Key_Minus;
557
558 case 0x00000046: // KEYCODE_EQUALS
559 return Qt::Key_Equal;
560
561 case 0x00000047: // KEYCODE_LEFT_BRACKET
562 return Qt::Key_BracketLeft;
563
564 case 0x00000048: // KEYCODE_RIGHT_BRACKET
565 return Qt::Key_BracketRight;
566
567 case 0x00000049: // KEYCODE_BACKSLASH
568 return Qt::Key_Backslash;
569
570 case 0x0000004a: // KEYCODE_SEMICOLON
571 return Qt::Key_Semicolon;
572
573 case 0x0000004b: // KEYCODE_APOSTROPHE
574 return Qt::Key_Apostrophe;
575
576 case 0x0000004c: // KEYCODE_SLASH
577 return Qt::Key_Slash;
578
579 case 0x0000004d: // KEYCODE_AT
580 return Qt::Key_At;
581
582 case 0x0000004e: // KEYCODE_NUM
583 return Qt::Key_Alt;
584
585 case 0x0000004f: // KEYCODE_HEADSETHOOK
586 return QKeyCombination::fromCombined(0);
587
588 case 0x00000050: // KEYCODE_FOCUS
589 return Qt::Key_CameraFocus;
590
591 case 0x00000051: // KEYCODE_PLUS
592 return Qt::Key_Plus;
593
594 case 0x00000052: // KEYCODE_MENU
595 return Qt::Key_Menu;
596
597 case 0x00000053: // KEYCODE_NOTIFICATION
598 return QKeyCombination::fromCombined(0);
599
600 case 0x00000054: // KEYCODE_SEARCH
601 return Qt::Key_Search;
602
603 case 0x00000055: // KEYCODE_MEDIA_PLAY_PAUSE
604 return Qt::Key_MediaTogglePlayPause;
605
606 case 0x00000056: // KEYCODE_MEDIA_STOP
607 return Qt::Key_MediaStop;
608
609 case 0x00000057: // KEYCODE_MEDIA_NEXT
610 return Qt::Key_MediaNext;
611
612 case 0x00000058: // KEYCODE_MEDIA_PREVIOUS
613 return Qt::Key_MediaPrevious;
614
615 case 0x00000059: // KEYCODE_MEDIA_REWIND
616 return Qt::Key_AudioRewind;
617
618 case 0x0000005a: // KEYCODE_MEDIA_FAST_FORWARD
619 return Qt::Key_AudioForward;
620
621 case 0x0000005b: // KEYCODE_MUTE
622 return Qt::Key_MicMute;
623
624 case 0x0000005c: // KEYCODE_PAGE_UP
625 return Qt::Key_PageUp;
626
627 case 0x0000005d: // KEYCODE_PAGE_DOWN
628 return Qt::Key_PageDown;
629
630 case 0x0000005e: // KEYCODE_PICTSYMBOLS
631 return QKeyCombination::fromCombined(0);
632
633 case 0x00000060: // KEYCODE_BUTTON_A
634 case 0x00000061: // KEYCODE_BUTTON_B
635 case 0x00000062: // KEYCODE_BUTTON_B
636 case 0x00000063: // KEYCODE_BUTTON_X
637 case 0x00000064: // KEYCODE_BUTTON_Y
638 case 0x00000065: // KEYCODE_BUTTON_Z
639 case 0x00000066: // KEYCODE_BUTTON_L1
640 case 0x00000067: // KEYCODE_BUTTON_R1
641 case 0x00000068: // KEYCODE_BUTTON_L2
642 case 0x00000069: // KEYCODE_BUTTON_R2
643 case 0x0000006a: // KEYCODE_BUTTON_THUMBL
644 case 0x0000006b: // KEYCODE_BUTTON_THUMBR
645 case 0x0000006c: // KEYCODE_BUTTON_START
646 case 0x0000006d: // KEYCODE_BUTTON_SELECT
647 case 0x0000006e: // KEYCODE_BUTTON_MODE
648 return QKeyCombination::fromCombined(0);
649
650 case 0x0000006f: // KEYCODE_ESCAPE
651 return Qt::Key_Escape;
652
653 case 0x00000070: // KEYCODE_FORWARD_DEL
654 return Qt::Key_Delete;
655
656 case 0x00000071: // KEYCODE_CTRL_LEFT
657 case 0x00000072: // KEYCODE_CTRL_RIGHT
658 return Qt::Key_Control;
659
660 case 0x00000073: // KEYCODE_CAPS_LOCK
661 return Qt::Key_CapsLock;
662
663 case 0x00000074: // KEYCODE_SCROLL_LOCK
664 return Qt::Key_ScrollLock;
665
666 case 0x00000075: // KEYCODE_META_LEFT
667 case 0x00000076: // KEYCODE_META_RIGHT
668 return Qt::Key_Meta;
669
670 case 0x00000077: // KEYCODE_FUNCTION
671 return QKeyCombination::fromCombined(0);
672
673 case 0x00000078: // KEYCODE_SYSRQ
674 return Qt::Key_Print;
675
676 case 0x00000079: // KEYCODE_BREAK
677 return Qt::Key_Pause;
678
679 case 0x0000007a: // KEYCODE_MOVE_HOME
680 return Qt::Key_Home;
681
682 case 0x0000007b: // KEYCODE_MOVE_END
683 return Qt::Key_End;
684
685 case 0x0000007c: // KEYCODE_MOVE_INSERT
686 return Qt::Key_Insert;
687
688 case 0x0000007d: // KEYCODE_FORWARD
689 return Qt::Key_Forward;
690
691 case 0x0000007e: // KEYCODE_MEDIA_PLAY
692 return Qt::Key_MediaPlay;
693
694 case 0x0000007f: // KEYCODE_MEDIA_PAUSE
695 return Qt::Key_MediaPause;
696
697 case 0x00000080: // KEYCODE_MEDIA_CLOSE
698 case 0x00000081: // KEYCODE_MEDIA_EJECT
699 return Qt::Key_Eject;
700
701 case 0x00000082: // KEYCODE_MEDIA_RECORD
702 return Qt::Key_MediaRecord;
703
704 // F1--F12 0x00000083 -- 0x0000008e
705
706 case 0x0000008f: // KEYCODE_NUM_LOCK
707 return Qt::Key_NumLock;
708
709 // NUMPAD_0--NUMPAD_9 0x00000090 -- 0x00000099
710
711 case 0x0000009a: // KEYCODE_NUMPAD_DIVIDE
712 return Qt::KeypadModifier | Qt::Key_Slash;
713
714 case 0x0000009b: // KEYCODE_NUMPAD_MULTIPLY
715 return Qt::KeypadModifier | Qt::Key_Asterisk;
716
717 case 0x0000009c: // KEYCODE_NUMPAD_SUBTRACT
718 return Qt::KeypadModifier | Qt::Key_Minus;
719
720 case 0x0000009d: // KEYCODE_NUMPAD_ADD
721 return Qt::KeypadModifier | Qt::Key_Plus;
722
723 case 0x0000009e: // KEYCODE_NUMPAD_DOT
724 return Qt::KeypadModifier | Qt::Key_Period;
725
726 case 0x0000009f: // KEYCODE_NUMPAD_COMMA
727 return Qt::KeypadModifier | Qt::Key_Comma;
728
729 case 0x000000a0: // KEYCODE_NUMPAD_ENTER
730 return Qt::Key_Enter;
731
732 case 0x000000a1: // KEYCODE_NUMPAD_EQUALS
733 return Qt::KeypadModifier | Qt::Key_Equal;
734
735 case 0x000000a2: // KEYCODE_NUMPAD_LEFT_PAREN
736 return Qt::Key_ParenLeft;
737
738 case 0x000000a3: // KEYCODE_NUMPAD_RIGHT_PAREN
739 return Qt::Key_ParenRight;
740
741 case 0x000000a4: // KEYCODE_VOLUME_MUTE
742 return Qt::Key_VolumeMute;
743
744 case 0x000000a5: // KEYCODE_INFO
745 return Qt::Key_Info;
746
747 case 0x000000a6: // KEYCODE_CHANNEL_UP
748 return Qt::Key_ChannelUp;
749
750 case 0x000000a7: // KEYCODE_CHANNEL_DOWN
751 return Qt::Key_ChannelDown;
752
753 case 0x000000a8: // KEYCODE_ZOOM_IN
754 return Qt::Key_ZoomIn;
755
756 case 0x000000a9: // KEYCODE_ZOOM_OUT
757 return Qt::Key_ZoomOut;
758
759 case 0x000000aa: // KEYCODE_TV
760 case 0x000000ab: // KEYCODE_WINDOW
761 return QKeyCombination::fromCombined(0);
762
763 case 0x000000ac: // KEYCODE_GUIDE
764 return Qt::Key_Guide;
765
766 case 0x000000ad: // KEYCODE_DVR
767 return QKeyCombination::fromCombined(0);
768
769 case 0x000000ae: // KEYCODE_BOOKMARK
770 return Qt::Key_AddFavorite;
771
772 case 0x000000af: // KEYCODE_CAPTIONS
773 return Qt::Key_Subtitle;
774
775 case 0x000000b0: // KEYCODE_SETTINGS
776 return Qt::Key_Settings;
777
778 case 0x000000b1: // KEYCODE_TV_POWER
779 case 0x000000b2: // KEYCODE_TV_INPUT
780 case 0x000000b3: // KEYCODE_STB_POWER
781 case 0x000000b4: // KEYCODE_STB_INPUT
782 case 0x000000b5: // KEYCODE_AVR_POWER
783 case 0x000000b6: // KEYCODE_AVR_INPUT
784 return QKeyCombination::fromCombined(0);
785
786 case 0x000000b7: // KEYCODE_PROG_RED
787 return Qt::Key_Red;
788
789 case 0x000000b8: // KEYCODE_PROG_GREEN
790 return Qt::Key_Green;
791
792 case 0x000000b9: // KEYCODE_PROG_YELLOW
793 return Qt::Key_Yellow;
794
795 case 0x000000ba: // KEYCODE_PROG_BLUE
796 return Qt::Key_Blue;
797
798 // 0x000000bb: // KEYCODE_APP_SWITCH is not sent by the Android O.S.
799
800 // BUTTON_1--KEYCODE_BUTTON_16 0x000000bc -- 0x000000cb
801
802 case 0x000000cc: // KEYCODE_LANGUAGE_SWITCH
803 case 0x000000cd: // KEYCODE_MANNER_MODE do we need such a thing?
804 case 0x000000ce: // KEYCODE_3D_MODE
805 case 0x000000cf: // KEYCODE_CONTACTS
806 return QKeyCombination::fromCombined(0);
807
808 case 0x000000d0: // KEYCODE_CALENDAR
809 return Qt::Key_Calendar;
810
811 case 0x000000d1: // KEYCODE_MUSIC
812 return Qt::Key_Music;
813
814 case 0x000000d2: // KEYCODE_CALCULATOR
815 return Qt::Key_Calculator;
816
817 // 0x000000d3 -- 0x000000da some japanese specific keys, someone who understand what is about should check !
818
819 // 0x000000db: // KEYCODE_ASSIST not delivered to applications.
820
821 case 0x000000dc: // KEYCODE_BRIGHTNESS_DOWN
822 return Qt::Key_KeyboardBrightnessDown;
823
824 case 0x000000dd: // KEYCODE_BRIGHTNESS_UP
825 return Qt::Key_KeyboardBrightnessUp;
826
827 case 0x000000de: // KEYCODE_MEDIA_AUDIO_TRACK
828 return Qt::Key_AudioCycleTrack;
829
830 default:
831 qWarning() << "Unhandled key code " << key << '!';
832 return QKeyCombination::fromCombined(0);
833 }
834 }
835
837 {
838 Qt::KeyboardModifiers qmodifiers;
839
840 if (modifiers & 0x00000001) // META_SHIFT_ON
841 qmodifiers |= Qt::ShiftModifier;
842
843 if (modifiers & 0x00000002) // META_ALT_ON
844 qmodifiers |= Qt::AltModifier;
845
846 if (modifiers & 0x00000004) // META_SYM_ON
847 qmodifiers |= Qt::MetaModifier;
848
849 if (modifiers & 0x00001000) // META_CTRL_ON
850 qmodifiers |= Qt::ControlModifier;
851
852 return qmodifiers;
853 }
854
855 // maps 0 to the empty string, and anything else to a single-character string
856 static inline QString toString(jint unicode)
857 {
858 return unicode ? QString(QChar(unicode)) : QString();
859 }
860
861 static void keyDown(JNIEnv */*env*/, jobject /*thiz*/, jint key, jint unicode, jint modifier, jboolean autoRepeat)
862 {
863 QWindowSystemInterface::handleKeyEvent(0,
864 QEvent::KeyPress,
865 mapAndroidKey(key).toCombined(),
866 mapAndroidModifiers(modifier),
867 toString(unicode),
868 autoRepeat);
869 }
870
871 static void keyUp(JNIEnv */*env*/, jobject /*thiz*/, jint key, jint unicode, jint modifier, jboolean autoRepeat)
872 {
873 QWindowSystemInterface::handleKeyEvent(0,
874 QEvent::KeyRelease,
875 mapAndroidKey(key).toCombined(),
876 mapAndroidModifiers(modifier),
877 toString(unicode),
878 autoRepeat);
879 }
880
881 static void keyboardVisibilityChanged(JNIEnv */*env*/, jobject /*thiz*/, jboolean visibility)
882 {
883 if (!visibility)
884 m_softwareKeyboardRect = QRect();
885
887 if (inputContext && qGuiApp) {
888 inputContext->emitInputPanelVisibleChanged();
889 if (!visibility) {
890 inputContext->emitKeyboardRectChanged();
891 QMetaObject::invokeMethod(inputContext, "hideSelectionHandles", Qt::QueuedConnection);
892 }
893 }
894 qCDebug(lcQpaInputMethods) << "@@@ KEYBOARDVISIBILITYCHANGED" << inputContext;
895 }
896
897 static void keyboardGeometryChanged(JNIEnv */*env*/, jobject /*thiz*/, jint x, jint y, jint w, jint h)
898 {
899 QRect r = QRect(x, y, w, h);
900 if (r == m_softwareKeyboardRect)
901 return;
904 if (inputContext && qGuiApp)
905 inputContext->emitKeyboardRectChanged();
906
907 qCDebug(lcQpaInputMethods) << "@@@ KEYBOARDRECTCHANGED" << m_softwareKeyboardRect;
908 }
909
910 static void handleLocationChanged(JNIEnv */*env*/, jobject /*thiz*/, int id, int x, int y)
911 {
912 qCDebug(lcQpaInputMethods) << "@@@ handleLocationChanged" << id << x << y;
914 if (inputContext && qGuiApp)
915 QMetaObject::invokeMethod(inputContext, "handleLocationChanged", Qt::BlockingQueuedConnection,
916 Q_ARG(int, id), Q_ARG(int, x), Q_ARG(int, y));
917
918 }
919
920
921 static const JNINativeMethod methods[] = {
922 {"touchBegin","(I)V",(void*)touchBegin},
923 {"touchAdd","(IIIZIIFFFF)V",(void*)touchAdd},
924 {"touchEnd","(II)V",(void*)touchEnd},
925 {"touchCancel", "(I)V", (void *)touchCancel},
926 {"mouseDown", "(IIII)V", (void *)mouseDown},
927 {"mouseUp", "(IIII)V", (void *)mouseUp},
928 {"mouseMove", "(IIII)V", (void *)mouseMove},
929 {"mouseWheel", "(IIIFF)V", (void *)mouseWheel},
930 {"longPress", "(III)V", (void *)longPress},
931 {"isTabletEventSupported", "()Z", (void *)isTabletEventSupported},
932 {"tabletEvent", "(IIJIIIFFF)V", (void *)tabletEvent},
933 {"keyDown", "(IIIZ)V", (void *)keyDown},
934 {"keyUp", "(IIIZ)V", (void *)keyUp},
935 {"keyboardVisibilityChanged", "(Z)V", (void *)keyboardVisibilityChanged},
936 {"keyboardGeometryChanged", "(IIII)V", (void *)keyboardGeometryChanged},
937 {"handleLocationChanged", "(III)V", (void *)handleLocationChanged},
938 };
939
940 bool registerNatives(QJniEnvironment &env)
941 {
942 if (!env.registerNativeMethods(QtJniTypes::Traits<QtJniTypes::QtInputDelegate>::className(),
943 methods, sizeof(methods) / sizeof(methods[0]))) {
944 __android_log_print(ANDROID_LOG_FATAL,"Qt", "RegisterNatives failed");
945 return false;
946 }
947
948 return true;
949 }
950}
951
952QT_END_NAMESPACE
static QAndroidInputContext * androidInputContext()
static QRect & defaultAvailableGeometry()
\inmodule QtCore\reentrant
Definition qpoint.h:30
Combined button and popup list for selecting options.
static QPointer< QWindow > m_mouseGrabber
static void touchCancel(JNIEnv *, jobject, jint winId)
static void mouseWheel(JNIEnv *, jobject, jint winId, jint x, jint y, jfloat hdelta, jfloat vdelta)
static bool isTabletEventSupported(JNIEnv *, jobject)
void updateSelection(int selStart, int selEnd, int candidatesStart, int candidatesEnd)
static void touchBegin(JNIEnv *, jobject, jint)
bool registerNatives(QJniEnvironment &env)
static QString toString(jint unicode)
static void sendMouseButtonEvents(QWindow *topLevel, QPoint localPos, QPoint globalPos, jint mouseButtonState, QEvent::Type type)
static bool m_ignoreMouseEvents
static QKeyCombination mapAndroidKey(int key)
static void mouseDown(JNIEnv *, jobject, jint winId, jint x, jint y, jint mouseButtonState)
static void keyUp(JNIEnv *, jobject, jint key, jint unicode, jint modifier, jboolean autoRepeat)
void showSoftwareKeyboard(int left, int top, int width, int height, int inputHints, int enterKeyType)
static QPointingDevice * getTouchDevice()
static Qt::MouseButtons m_lastSeenButtons
static Qt::KeyboardModifiers mapAndroidModifiers(jint modifiers)
bool isSoftwareKeyboardVisible()
static void touchAdd(JNIEnv *, jobject, jint winId, jint id, jint action, jboolean, jint x, jint y, jfloat major, jfloat minor, jfloat rotation, jfloat pressure)
static void keyboardGeometryChanged(JNIEnv *, jobject, jint x, jint y, jint w, jint h)
static QRect m_softwareKeyboardRect
static void keyboardVisibilityChanged(JNIEnv *, jobject, jboolean visibility)
static void tabletEvent(JNIEnv *, jobject, jint winId, jint deviceId, jlong time, jint action, jint pointerType, jint buttonState, jfloat x, jfloat y, jfloat pressure)
static void keyDown(JNIEnv *, jobject, jint key, jint unicode, jint modifier, jboolean autoRepeat)
static void mouseMove(JNIEnv *, jobject, jint winId, jint x, jint y, jint mouseButtonState)
void updateHandles(int mode, QPoint editMenuPos, uint32_t editButtons, QPoint cursor, QPoint anchor, bool rtl)
static void handleLocationChanged(JNIEnv *, jobject, int id, int x, int y)
QRect softwareKeyboardRect()
static void longPress(JNIEnv *, jobject, jint winId, jint x, jint y)
static QList< QWindowSystemInterface::TouchPoint > m_touchPoints
static Qt::MouseButtons toMouseButtons(jint j_buttons)
static void mouseUp(JNIEnv *, jobject, jint winId, jint x, jint y, jint mouseButtonState)
static const JNINativeMethod methods[]
static void touchEnd(JNIEnv *, jobject, jint winId, jint)
QBasicMutex * platformInterfaceMutex()
QAndroidPlatformIntegration * androidPlatformIntegration()
AndroidBackendRegister * backendRegister()
QWindow * windowFromId(int windowId)
Q_DECLARE_JNI_CLASS(MotionEvent, "android/view/MotionEvent")