Blame SOURCES/0019-client-Allow-QWaylandInputContext-to-accept-composed.patch

de89c0
From 91c48320633e493b4cd519e5d73b836a878b2b77 Mon Sep 17 00:00:00 2001
de89c0
From: Aleix Pol <aleixpol@kde.org>
de89c0
Date: Wed, 10 Mar 2021 01:09:13 +0100
de89c0
Subject: [PATCH 19/36] client: Allow QWaylandInputContext to accept composed
de89c0
 key combinations
de89c0
de89c0
At the moment, we are forcing user to choose to either compose or use
de89c0
the text-input channel. This patch brings some of the QComposeInputContext
de89c0
functionality in order to let applications understand dead key
de89c0
combinations like they are supposed to.
de89c0
de89c0
Having it in QWaylandInputContext rather than in QWaylandInputDevice
de89c0
should solve the problems 3aedd01271dc4f4a13103d632df224971ab2b6df had
de89c0
with 57c4af2b18c0fb1d266b245a107fa6cb876b9d9e, because we are doing it
de89c0
in the input context rather than before. This way, if the user is
de89c0
overriding the input method (e.g. by setting QT_IM_MODULE), all the key
de89c0
strokes will still be properly forwarded to the module to use.
de89c0
de89c0
This in turn allows us to solve https://bugs.kde.org/show_bug.cgi?id=411729
de89c0
and https://bugs.kde.org/show_bug.cgi?id=405388 since we don't need to
de89c0
choose anymore between physical and virual keyboards anymore.
de89c0
de89c0
Pick-to: 5.15
de89c0
Change-Id: I8601f5d7ae21edf4b3a1191fa75877286e505588
de89c0
Reviewed-by: David Edmundson <davidedmundson@kde.org>
de89c0
---
de89c0
 src/client/qwaylanddisplay_p.h      |  3 -
de89c0
 src/client/qwaylandinputcontext.cpp | 95 ++++++++++++++++++++++++++++-
de89c0
 src/client/qwaylandinputcontext_p.h | 21 +++++++
de89c0
 src/client/qwaylandinputdevice.cpp  |  2 +-
de89c0
 src/client/qwaylandintegration.cpp  |  8 +--
de89c0
 5 files changed, 119 insertions(+), 10 deletions(-)
de89c0
de89c0
diff --git a/src/client/qwaylanddisplay_p.h b/src/client/qwaylanddisplay_p.h
de89c0
index 188e9131..3b092bc8 100644
de89c0
--- a/src/client/qwaylanddisplay_p.h
de89c0
+++ b/src/client/qwaylanddisplay_p.h
de89c0
@@ -175,8 +175,6 @@ public:
de89c0
     QWaylandHardwareIntegration *hardwareIntegration() const { return mHardwareIntegration.data(); }
de89c0
     QWaylandXdgOutputManagerV1 *xdgOutputManager() const { return mXdgOutputManager.data(); }
de89c0
 
de89c0
-    bool usingInputContextFromCompositor() const { return mUsingInputContextFromCompositor; }
de89c0
-
de89c0
     struct RegistryGlobal {
de89c0
         uint32_t id;
de89c0
         QString interface;
de89c0
@@ -282,7 +280,6 @@ private:
de89c0
     QReadWriteLock m_frameQueueLock;
de89c0
 
de89c0
     bool mClientSideInputContextRequested = !QPlatformInputContextFactory::requested().isNull();
de89c0
-    bool mUsingInputContextFromCompositor = false;
de89c0
 
de89c0
     void registry_global(uint32_t id, const QString &interface, uint32_t version) override;
de89c0
     void registry_global_remove(uint32_t id) override;
de89c0
diff --git a/src/client/qwaylandinputcontext.cpp b/src/client/qwaylandinputcontext.cpp
de89c0
index e9afe05e..ef5aa375 100644
de89c0
--- a/src/client/qwaylandinputcontext.cpp
de89c0
+++ b/src/client/qwaylandinputcontext.cpp
de89c0
@@ -406,6 +406,8 @@ bool QWaylandInputContext::isValid() const
de89c0
 void QWaylandInputContext::reset()
de89c0
 {
de89c0
     qCDebug(qLcQpaInputMethods) << Q_FUNC_INFO;
de89c0
+    if (m_composeState)
de89c0
+        xkb_compose_state_reset(m_composeState);
de89c0
 
de89c0
     QPlatformInputContext::reset();
de89c0
 
de89c0
@@ -526,9 +528,14 @@ Qt::LayoutDirection QWaylandInputContext::inputDirection() const
de89c0
     return textInput()->inputDirection();
de89c0
 }
de89c0
 
de89c0
-void QWaylandInputContext::setFocusObject(QObject *)
de89c0
+void QWaylandInputContext::setFocusObject(QObject *object)
de89c0
 {
de89c0
     qCDebug(qLcQpaInputMethods) << Q_FUNC_INFO;
de89c0
+#if QT_CONFIG(xkbcommon)
de89c0
+    m_focusObject = object;
de89c0
+#else
de89c0
+    Q_UNUSED(object);
de89c0
+#endif
de89c0
 
de89c0
     if (!textInput())
de89c0
         return;
de89c0
@@ -561,6 +568,92 @@ QWaylandTextInput *QWaylandInputContext::textInput() const
de89c0
     return mDisplay->defaultInputDevice()->textInput();
de89c0
 }
de89c0
 
de89c0
+#if QT_CONFIG(xkbcommon)
de89c0
+
de89c0
+void QWaylandInputContext::ensureInitialized()
de89c0
+{
de89c0
+    if (m_initialized)
de89c0
+        return;
de89c0
+
de89c0
+    if (!m_XkbContext) {
de89c0
+        qCWarning(qLcQpaInputMethods) << "error: xkb context has not been set on" << metaObject()->className();
de89c0
+        return;
de89c0
+    }
de89c0
+
de89c0
+    m_initialized = true;
de89c0
+    const char *locale = setlocale(LC_CTYPE, "");
de89c0
+    if (!locale)
de89c0
+        locale = setlocale(LC_CTYPE, nullptr);
de89c0
+    qCDebug(qLcQpaInputMethods) << "detected locale (LC_CTYPE):" << locale;
de89c0
+
de89c0
+    m_composeTable = xkb_compose_table_new_from_locale(m_XkbContext, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
de89c0
+    if (m_composeTable)
de89c0
+        m_composeState = xkb_compose_state_new(m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS);
de89c0
+
de89c0
+    if (!m_composeTable) {
de89c0
+        qCWarning(qLcQpaInputMethods, "failed to create compose table");
de89c0
+        return;
de89c0
+    }
de89c0
+    if (!m_composeState) {
de89c0
+        qCWarning(qLcQpaInputMethods, "failed to create compose state");
de89c0
+        return;
de89c0
+    }
de89c0
+}
de89c0
+
de89c0
+bool QWaylandInputContext::filterEvent(const QEvent *event)
de89c0
+{
de89c0
+    auto keyEvent = static_cast<const QKeyEvent *>(event);
de89c0
+    if (keyEvent->type() != QEvent::KeyPress)
de89c0
+        return false;
de89c0
+
de89c0
+    if (!inputMethodAccepted())
de89c0
+        return false;
de89c0
+
de89c0
+    // lazy initialization - we don't want to do this on an app startup
de89c0
+    ensureInitialized();
de89c0
+
de89c0
+    if (!m_composeTable || !m_composeState)
de89c0
+        return false;
de89c0
+
de89c0
+    xkb_compose_state_feed(m_composeState, keyEvent->nativeVirtualKey());
de89c0
+
de89c0
+    switch (xkb_compose_state_get_status(m_composeState)) {
de89c0
+    case XKB_COMPOSE_COMPOSING:
de89c0
+        return true;
de89c0
+    case XKB_COMPOSE_CANCELLED:
de89c0
+        reset();
de89c0
+        return false;
de89c0
+    case XKB_COMPOSE_COMPOSED:
de89c0
+    {
de89c0
+        const int size = xkb_compose_state_get_utf8(m_composeState, nullptr, 0);
de89c0
+        QVarLengthArray<char, 32> buffer(size + 1);
de89c0
+        xkb_compose_state_get_utf8(m_composeState, buffer.data(), buffer.size());
de89c0
+        QString composedText = QString::fromUtf8(buffer.constData());
de89c0
+
de89c0
+        QInputMethodEvent event;
de89c0
+        event.setCommitString(composedText);
de89c0
+
de89c0
+        if (!m_focusObject && qApp)
de89c0
+            m_focusObject = qApp->focusObject();
de89c0
+
de89c0
+        if (m_focusObject)
de89c0
+            QCoreApplication::sendEvent(m_focusObject, &event);
de89c0
+        else
de89c0
+            qCWarning(qLcQpaInputMethods, "no focus object");
de89c0
+
de89c0
+        reset();
de89c0
+        return true;
de89c0
+    }
de89c0
+    case XKB_COMPOSE_NOTHING:
de89c0
+        return false;
de89c0
+    default:
de89c0
+        Q_UNREACHABLE();
de89c0
+        return false;
de89c0
+    }
de89c0
+}
de89c0
+
de89c0
+#endif
de89c0
+
de89c0
 }
de89c0
 
de89c0
 QT_END_NAMESPACE
de89c0
diff --git a/src/client/qwaylandinputcontext_p.h b/src/client/qwaylandinputcontext_p.h
de89c0
index 10132dfe..50db6344 100644
de89c0
--- a/src/client/qwaylandinputcontext_p.h
de89c0
+++ b/src/client/qwaylandinputcontext_p.h
de89c0
@@ -61,6 +61,10 @@
de89c0
 
de89c0
 #include <QtWaylandClient/private/qwayland-text-input-unstable-v2.h>
de89c0
 #include <qwaylandinputmethodeventbuilder_p.h>
de89c0
+#include <qtwaylandclientglobal_p.h>
de89c0
+#if QT_CONFIG(xkbcommon)
de89c0
+#include <xkbcommon/xkbcommon-compose.h>
de89c0
+#endif
de89c0
 
de89c0
 struct wl_callback;
de89c0
 struct wl_callback_listener;
de89c0
@@ -155,11 +159,28 @@ public:
de89c0
 
de89c0
     void setFocusObject(QObject *object) override;
de89c0
 
de89c0
+#if QT_CONFIG(xkbcommon)
de89c0
+    bool filterEvent(const QEvent *event) override;
de89c0
+
de89c0
+    // This invokable is called from QXkbCommon::setXkbContext().
de89c0
+    Q_INVOKABLE void setXkbContext(struct xkb_context *context) { m_XkbContext = context; }
de89c0
+#endif
de89c0
+
de89c0
 private:
de89c0
     QWaylandTextInput *textInput() const;
de89c0
 
de89c0
     QWaylandDisplay *mDisplay = nullptr;
de89c0
     QPointer<QWindow> mCurrentWindow;
de89c0
+
de89c0
+#if QT_CONFIG(xkbcommon)
de89c0
+    void ensureInitialized();
de89c0
+
de89c0
+    bool m_initialized = false;
de89c0
+    QObject *m_focusObject = nullptr;
de89c0
+    xkb_compose_table *m_composeTable = nullptr;
de89c0
+    xkb_compose_state *m_composeState = nullptr;
de89c0
+    struct xkb_context *m_XkbContext = nullptr;
de89c0
+#endif
de89c0
 };
de89c0
 
de89c0
 }
de89c0
diff --git a/src/client/qwaylandinputdevice.cpp b/src/client/qwaylandinputdevice.cpp
de89c0
index ed4a0eb4..ae045f4f 100644
de89c0
--- a/src/client/qwaylandinputdevice.cpp
de89c0
+++ b/src/client/qwaylandinputdevice.cpp
de89c0
@@ -1201,7 +1201,7 @@ void QWaylandInputDevice::Keyboard::handleKey(ulong timestamp, QEvent::Type type
de89c0
     QPlatformInputContext *inputContext = QGuiApplicationPrivate::platformIntegration()->inputContext();
de89c0
     bool filtered = false;
de89c0
 
de89c0
-    if (inputContext && !mParent->mQDisplay->usingInputContextFromCompositor()) {
de89c0
+    if (inputContext) {
de89c0
         QKeyEvent event(type, key, modifiers, nativeScanCode, nativeVirtualKey,
de89c0
                         nativeModifiers, text, autorepeat, count);
de89c0
         event.setTimestamp(timestamp);
de89c0
diff --git a/src/client/qwaylandintegration.cpp b/src/client/qwaylandintegration.cpp
de89c0
index 7ad8e05e..c53ccb78 100644
de89c0
--- a/src/client/qwaylandintegration.cpp
de89c0
+++ b/src/client/qwaylandintegration.cpp
de89c0
@@ -474,13 +474,11 @@ void QWaylandIntegration::reconfigureInputContext()
de89c0
 
de89c0
 #if QT_CONFIG(xkbcommon)
de89c0
     QXkbCommon::setXkbContext(mInputContext.data(), mDisplay->xkbContext());
de89c0
+    if (QWaylandInputContext* waylandInput = qobject_cast<QWaylandInputContext*>(mInputContext.get())) {
de89c0
+        waylandInput->setXkbContext(mDisplay->xkbContext());
de89c0
+    }
de89c0
 #endif
de89c0
 
de89c0
-    // Even if compositor-side input context handling has been requested, we fallback to
de89c0
-    // client-side handling if compositor does not provide the text-input extension. This
de89c0
-    // is why we need to check here which input context actually is being used.
de89c0
-    mDisplay->mUsingInputContextFromCompositor = qobject_cast<QWaylandInputContext *>(mInputContext.data());
de89c0
-
de89c0
     qCDebug(lcQpaWayland) << "using input method:" << inputContext()->metaObject()->className();
de89c0
 }
de89c0
 
de89c0
-- 
de89c0
2.33.1
de89c0