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