7#include <QtGui/qtguiglobal.h>
15#include <qpa/qplatformwindow.h>
16#include <qpa/qwindowsysteminterface.h>
17#include <QtGui/private/qpointingdevice_p.h>
21#include <QGuiApplication>
27Q_LOGGING_CATEGORY(lcQpaInputMethods,
"qt.qpa.input.methods");
47 void updateSelection(
int selStart,
int selEnd,
int candidatesStart,
int candidatesEnd)
49 qCDebug(lcQpaInputMethods) <<
">>> UPDATESELECTION" << selStart << selEnd << candidatesStart << candidatesEnd;
51 reg->callInterface<QtJniTypes::QtInputInterface,
void>(
"updateSelection", selStart, selEnd,
52 candidatesStart, candidatesEnd);
58 reg->callInterface<QtJniTypes::QtInputInterface,
void>(
59 "showSoftwareKeyboard", QtAndroidPrivate::activity(),
60 left, top, width, height, inputHints,
62 qCDebug(lcQpaInputMethods) <<
"@@@ SHOWSOFTWAREKEYBOARD" << left << top << width << height << inputHints << enterKeyType;
68 reg->callInterface<QtJniTypes::QtInputInterface>(
"resetSoftwareKeyboard");
69 qCDebug(lcQpaInputMethods) <<
"@@@ RESETSOFTWAREKEYBOARD";
75 reg->callInterface<QtJniTypes::QtInputInterface>(
"hideSoftwareKeyboard");
76 qCDebug(lcQpaInputMethods) <<
"@@@ HIDESOFTWAREKEYBOARD";
82 return reg->callInterface<QtJniTypes::QtInputInterface, jboolean>(
83 "isSoftwareKeyboardVisible");
88 return m_softwareKeyboardRect;
94 return reg->callInterface<QtJniTypes::QtInputInterface, jint>(
"getSelectionHandleWidth");
100 reg->callInterface<QtJniTypes::QtInputInterface,
void>(
101 "updateHandles", mode, editMenuPos.x(), editMenuPos.y(),
102 editButtons, cursor.x(), cursor.y(), anchor.x(), anchor.y(), rtl);
119 const auto buttons =
static_cast<AndroidMouseButtons>(j_buttons);
120 Qt::MouseButtons mouseButtons;
122 mouseButtons.setFlag(Qt::LeftButton);
125 mouseButtons.setFlag(Qt::RightButton);
128 mouseButtons.setFlag(Qt::MiddleButton);
131 mouseButtons.setFlag(Qt::BackButton);
134 mouseButtons.setFlag(Qt::ForwardButton);
137 mouseButtons.setFlag(Qt::LeftButton);
140 mouseButtons.setFlag(Qt::RightButton);
143 if (Q_UNLIKELY(buttons != 0 && mouseButtons == Qt::NoButton)) {
144 qWarning() <<
"Unhandled button value:" << buttons <<
"Falling back to Qt::LeftButton";
145 mouseButtons = Qt::LeftButton;
152 Qt::KeyboardModifiers qmodifiers;
154 if (modifiers & 0x00000001)
155 qmodifiers |= Qt::ShiftModifier;
157 if (modifiers & 0x00000002)
158 qmodifiers |= Qt::AltModifier;
160 if (modifiers & 0x00000004)
161 qmodifiers |= Qt::MetaModifier;
163 if (modifiers & 0x00001000)
164 qmodifiers |= Qt::ControlModifier;
166 if (modifiers & 0x00010000)
167 qmodifiers |= Qt::MetaModifier;
173 jint mouseButtonState, jint metaState, QEvent::Type type)
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;
180 static_assert (
sizeof(eventButtons) <=
sizeof(uint),
"Qt::MouseButtons size changed. Adapt code.");
182 if (eventButtons == Qt::NoButton)
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);
196 jint mouseButtonState, jint metaState)
201 const QPoint localPos(x,y);
202 QWindow *window = windowFromId(winId);
204 const QPoint globalPos = window && window->handle() ?
205 window->handle()->mapToGlobal(localPos) : localPos;
206 sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, metaState,
207 QEvent::MouseButtonPress);
211 jint mouseButtonState, jint metaState)
213 const QPoint localPos(x,y);
214 QWindow *window = m_mouseGrabber.data();
216 window = windowFromId(winId);
218 const QPoint globalPos = window && window->handle() ?
219 window->handle()->mapToGlobal(localPos) : localPos;
221 sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, metaState,
222 QEvent::MouseButtonRelease);
228 jint mouseButtonState, jint metaState)
233 const QPoint localPos(x,y);
234 QWindow *window = m_mouseGrabber.data();
236 window = windowFromId(winId);
237 const QPoint globalPos = window && window->handle() ?
238 window->handle()->mapToGlobal(localPos) : localPos;
239 const Qt::MouseButtons qtButtons = toMouseButtons(mouseButtonState);
241 QWindowSystemInterface::handleMouseEvent(window, localPos, globalPos,
242 qtButtons, Qt::NoButton, QEvent::MouseMove,
243 mapAndroidModifiers(metaState));
247 jfloat hdelta, jfloat vdelta, jint metaState)
252 const QPoint localPos(x,y);
253 QWindow *window = m_mouseGrabber.data();
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);
260 QWindowSystemInterface::handleWheelEvent(window,
265 mapAndroidModifiers(metaState));
273 const QPoint localPos(x,y);
274 QWindow *window = windowFromId(winId);
275 const QPoint globalPos = window && window->handle() ?
276 window->handle()->mapToGlobal(localPos) : localPos;
278 if (inputContext && qGuiApp)
279 QMetaObject::invokeMethod(inputContext,
"longPress", Q_ARG(
int, globalPos.x()), Q_ARG(
int, globalPos.y()));
282 static bool rightMouseFromLongPress = qEnvironmentVariableIntValue(
"QT_ANDROID_ENABLE_RIGHT_MOUSE_FROM_LONG_PRESS");
283 if (!rightMouseFromLongPress)
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);
306 jfloat major, jfloat minor, jfloat rotation, jfloat pressure)
308 QEventPoint::State state = QEventPoint::State::Stationary;
311 state = QEventPoint::State::Pressed;
314 state = QEventPoint::State::Updated;
317 state = QEventPoint::State::Stationary;
320 state = QEventPoint::State::Released;
327 qCWarning(lcQpaInputMethods,
"Touch event received for non-existing window %d", winId);
331 QPointF mappedTouchPoint;
332 if (window->handle())
333 mappedTouchPoint = window->handle()->mapToGlobalF(QPointF(x, y));
335 mappedTouchPoint = window->mapToGlobal(QPointF(x, y));
337 QWindowSystemInterface::TouchPoint touchPoint;
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),
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()));
359 if (!platformIntegration)
362 QPointingDevice *touchDevice = platformIntegration->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,
371 QWindowSystemInterface::registerInputDevice(touchDevice);
372 platformIntegration->setTouchDevice(touchDevice);
375 QPointingDevicePrivate::get(touchDevice)->setAvailableVirtualGeometry(
376 platformIntegration->screen()->availableGeometry());
388 const QPointingDevice *touchDevice = getTouchDevice();
395 QWindowSystemInterface::handleTouchEvent(window, touchDevice,
396 m_touchPoints, mapAndroidModifiers(metaState));
405 const QPointingDevice *touchDevice = getTouchDevice();
412 QWindowSystemInterface::handleTouchCancelEvent(window, touchDevice,
413 mapAndroidModifiers(metaState));
418#if QT_CONFIG(tabletevent)
426 jint pointerType, jint buttonState, jfloat x, jfloat y, jfloat pressure, jint metaState)
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;
449 Qt::MouseButtons buttons = Qt::NoButton;
454 buttons = Qt::NoButton;
457 if (buttonState == 0)
458 buttons = Qt::LeftButton;
460 buttons = Qt::MouseButtons(buttonState);
464 qCDebug(lcQpaInputMethods) << action << pointerType << buttonState <<
'@' << x << y <<
"pressure" << pressure <<
": buttons" << buttons;
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));
475 if (key >= 0x00000007 && key <= 0x00000010)
476 return QKeyCombination::fromCombined(Qt::Key_0 + key - 0x00000007);
479 if (key >= 0x0000001d && key <= 0x00000036)
480 return QKeyCombination::fromCombined(Qt::Key_A + key - 0x0000001d);
483 if (key >= 0x00000083 && key <= 0x0000008e)
484 return QKeyCombination::fromCombined(Qt::Key_F1 + key - 0x00000083);
487 if (key >= 0x00000090 && key <= 0x00000099)
488 return QKeyCombination::fromCombined(Qt::KeypadModifier | Qt::Key_0 + key - 0x00000090);
494 return Qt::Key_unknown;
500 return Qt::Key_Right;
511 return Qt::Key_Hangup;
516 return Qt::Key_Asterisk;
519 return Qt::Key_NumberSign;
531 return Qt::Key_Right;
534 return Qt::Key_Enter;
537 return Qt::Key_VolumeUp;
540 return Qt::Key_VolumeDown;
543 return Qt::Key_PowerOff;
546 return Qt::Key_Camera;
549 return Qt::Key_Clear;
554 return Qt::Key_Comma;
557 return Qt::Key_Period;
565 return Qt::Key_Shift;
571 return Qt::Key_Space;
577 return Qt::Key_Explorer;
580 return Qt::Key_LaunchMail;
583 return Qt::Key_Return;
586 return Qt::Key_Backspace;
589 return Qt::Key_QuoteLeft;
592 return Qt::Key_Minus;
595 return Qt::Key_Equal;
598 return Qt::Key_BracketLeft;
601 return Qt::Key_BracketRight;
604 return Qt::Key_Backslash;
607 return Qt::Key_Semicolon;
610 return Qt::Key_Apostrophe;
613 return Qt::Key_Slash;
622 return QKeyCombination::fromCombined(0);
625 return Qt::Key_CameraFocus;
634 return QKeyCombination::fromCombined(0);
637 return Qt::Key_Search;
640 return Qt::Key_MediaTogglePlayPause;
643 return Qt::Key_MediaStop;
646 return Qt::Key_MediaNext;
649 return Qt::Key_MediaPrevious;
652 return Qt::Key_AudioRewind;
655 return Qt::Key_AudioForward;
658 return Qt::Key_MicMute;
661 return Qt::Key_PageUp;
664 return Qt::Key_PageDown;
667 return QKeyCombination::fromCombined(0);
684 return QKeyCombination::fromCombined(0);
687 return Qt::Key_Escape;
690 return Qt::Key_Delete;
694 return Qt::Key_Control;
697 return Qt::Key_CapsLock;
700 return Qt::Key_ScrollLock;
707 return QKeyCombination::fromCombined(0);
710 return Qt::Key_Print;
713 return Qt::Key_Pause;
722 return Qt::Key_Insert;
725 return Qt::Key_Forward;
728 return Qt::Key_MediaPlay;
731 return Qt::Key_MediaPause;
735 return Qt::Key_Eject;
738 return Qt::Key_MediaRecord;
743 return Qt::Key_NumLock;
748 return Qt::KeypadModifier | Qt::Key_Slash;
751 return Qt::KeypadModifier | Qt::Key_Asterisk;
754 return Qt::KeypadModifier | Qt::Key_Minus;
757 return Qt::KeypadModifier | Qt::Key_Plus;
760 return Qt::KeypadModifier | Qt::Key_Period;
763 return Qt::KeypadModifier | Qt::Key_Comma;
766 return Qt::Key_Enter;
769 return Qt::KeypadModifier | Qt::Key_Equal;
772 return Qt::Key_ParenLeft;
775 return Qt::Key_ParenRight;
778 return Qt::Key_VolumeMute;
784 return Qt::Key_ChannelUp;
787 return Qt::Key_ChannelDown;
790 return Qt::Key_ZoomIn;
793 return Qt::Key_ZoomOut;
797 return QKeyCombination::fromCombined(0);
800 return Qt::Key_Guide;
803 return QKeyCombination::fromCombined(0);
806 return Qt::Key_AddFavorite;
809 return Qt::Key_Subtitle;
812 return Qt::Key_Settings;
820 return QKeyCombination::fromCombined(0);
826 return Qt::Key_Green;
829 return Qt::Key_Yellow;
842 return QKeyCombination::fromCombined(0);
845 return Qt::Key_Calendar;
848 return Qt::Key_Music;
851 return Qt::Key_Calculator;
858 return Qt::Key_KeyboardBrightnessDown;
861 return Qt::Key_KeyboardBrightnessUp;
864 return Qt::Key_AudioCycleTrack;
867 qWarning() <<
"Unhandled key code " << key <<
'!';
868 return QKeyCombination::fromCombined(0);
875 return unicode ? QString(QChar(unicode)) : QString();
890 qWarning(
"Dropping Android key event while the Qt GUI thread is stopped");
898 qWarning(
"Failed to deliver Android key event on the GUI thread");
903 static void keyDown(JNIEnv *,
jobject , jint key, jint unicode, jint modifier, jboolean autoRepeat)
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);
911 bool accepted =
false;
912 if (isBackKey || isMenuKey) {
914 qtModifiers, text, autoRepeat);
916 QWindowSystemInterface::handleKeyEvent(
917 nullptr, QEvent::KeyPress, qtKeyCombination, qtModifiers, text, autoRepeat);
924 auto iface = qGuiApp->nativeInterface<QNativeInterface::QAndroidApplication>();
925 iface->runOnAndroidMainThread([iface]() {
926 if (!iface->isActivityContext())
928 iface->context().callMethod<jboolean>(
"moveTaskToBack",
true);
932 static void keyUp(JNIEnv *,
jobject , jint key, jint unicode, jint modifier, jboolean autoRepeat)
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);
940 if (!isBackKey && !isMenuKey) {
941 QWindowSystemInterface::handleKeyEvent(
942 nullptr, QEvent::KeyRelease, qtKeyCombination, qtModifiers, text, autoRepeat);
947 qtModifiers, text, autoRepeat);
961 if (inputContext && qGuiApp) {
962 inputContext->emitInputPanelVisibleChanged();
964 inputContext->emitKeyboardRectChanged();
965 QMetaObject::invokeMethod(inputContext,
"hideSelectionHandles", Qt::QueuedConnection);
968 qCDebug(lcQpaInputMethods) <<
"@@@ KEYBOARDVISIBILITYCHANGED" << inputContext;
973 QRect r = QRect(x, y, w, h);
974 if (r == m_softwareKeyboardRect)
978 if (inputContext && qGuiApp)
979 inputContext->emitKeyboardRectChanged();
981 qCDebug(lcQpaInputMethods) <<
"@@@ KEYBOARDRECTCHANGED" << m_softwareKeyboardRect;
986 qCDebug(lcQpaInputMethods) <<
"@@@ handleLocationChanged" << id << x << y;
989 if (inputContext && qGuiApp)
990 QMetaObject::invokeMethod(inputContext,
"handleLocationChanged", Qt::QueuedConnection,
991 Q_ARG(
int, id), Q_ARG(
int, x), Q_ARG(
int, y));
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},
1017 if (!env.registerNativeMethods(QtJniTypes::Traits<QtJniTypes::QtInputDelegate>::className(),
1019 __android_log_print(ANDROID_LOG_FATAL,
"Qt",
"RegisterNatives failed");
static QAndroidInputContext * androidInputContext()
\inmodule QtCore\reentrant
Combined button and popup list for selecting options.
QBasicMutex * platformInterfaceMutex()
QAndroidPlatformIntegration * androidPlatformIntegration()
AndroidBackendRegister * backendRegister()
QWindow * windowFromId(int windowId)