add abstract class QWaylandTextInputInterface in client side

There are several protocols for text-input and it will be an abstract
class for them. It is not related with qt_text_input_method_v1.
It will help to implement zwp_text_input_* protocols.

Change-Id: I2207887d84d416469217cff7d011648402a53664
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Inho Lee 2021-06-15 10:54:00 +02:00 committed by Inho Lee
parent b2b710ba7a
commit 0c434b3537
10 changed files with 713 additions and 422 deletions

View File

@ -29,6 +29,8 @@ qt_internal_add_module(WaylandClient
qwaylanddisplay.cpp qwaylanddisplay_p.h
qwaylandextendedsurface.cpp qwaylandextendedsurface_p.h
qwaylandinputcontext.cpp qwaylandinputcontext_p.h
qwaylandtextinputv2.cpp qwaylandtextinputv2_p.h
qwaylandtextinputinterface.cpp qwaylandtextinputinterface_p.h
qwaylandinputdevice.cpp qwaylandinputdevice_p.h
qwaylandinputmethodcontext.cpp qwaylandinputmethodcontext_p.h
qwaylandintegration.cpp qwaylandintegration_p.h

View File

@ -60,6 +60,7 @@
#include <wayland-cursor.h>
#endif
#include "qwaylandhardwareintegration_p.h"
#include "qwaylandtextinputv2_p.h"
#include "qwaylandinputcontext_p.h"
#include "qwaylandinputmethodcontext_p.h"
@ -510,7 +511,7 @@ bool QWaylandDisplay::registerTextInputManager(const QStringList &protocols, int
qCDebug(lcQpaWayland) << "text input: register zwp_text_input_manager_v2";
mTextInputManager.reset(new QtWayland::zwp_text_input_manager_v2(global.registry, global.id, 1));
for (QWaylandInputDevice *inputDevice : qAsConst(mInputDevices))
inputDevice->setTextInput(new QWaylandTextInput(this, mTextInputManager->get_text_input(inputDevice->wl_seat())));
inputDevice->setTextInput(new QWaylandTextInputv2(this, mTextInputManager->get_text_input(inputDevice->wl_seat())));
mWaylandIntegration->reconfigureInputContext();
return true;
}

View File

@ -40,15 +40,13 @@
#include "qwaylandinputcontext_p.h"
#include <QLoggingCategory>
#include <QtGui/QGuiApplication>
#include <QtGui/QTextCharFormat>
#include <QtGui/QWindow>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformintegration.h>
#include "qwaylanddisplay_p.h"
#include "qwaylandinputdevice_p.h"
#include "qwaylandinputmethodeventbuilder_p.h"
#include "qwaylandwindow_p.h"
#if QT_CONFIG(xkbcommon)
@ -61,342 +59,6 @@ Q_LOGGING_CATEGORY(qLcQpaInputMethods, "qt.qpa.input.methods")
namespace QtWaylandClient {
namespace {
const Qt::InputMethodQueries supportedQueries = Qt::ImEnabled |
Qt::ImSurroundingText |
Qt::ImCursorPosition |
Qt::ImAnchorPosition |
Qt::ImHints |
Qt::ImCursorRectangle |
Qt::ImPreferredLanguage;
}
QWaylandTextInput::QWaylandTextInput(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input)
: QtWayland::zwp_text_input_v2(text_input)
, m_display(display)
{
}
QWaylandTextInput::~QWaylandTextInput()
{
if (m_resetCallback)
wl_callback_destroy(m_resetCallback);
}
void QWaylandTextInput::reset()
{
m_builder.reset();
m_preeditCommit = QString();
updateState(Qt::ImQueryAll, update_state_reset);
}
void QWaylandTextInput::commit()
{
if (QObject *o = QGuiApplication::focusObject()) {
QInputMethodEvent event;
event.setCommitString(m_preeditCommit);
QCoreApplication::sendEvent(o, &event);
}
reset();
}
const wl_callback_listener QWaylandTextInput::callbackListener = {
QWaylandTextInput::resetCallback
};
void QWaylandTextInput::resetCallback(void *data, wl_callback *, uint32_t)
{
QWaylandTextInput *self = static_cast<QWaylandTextInput*>(data);
if (self->m_resetCallback) {
wl_callback_destroy(self->m_resetCallback);
self->m_resetCallback = nullptr;
}
}
void QWaylandTextInput::updateState(Qt::InputMethodQueries queries, uint32_t flags)
{
if (!QGuiApplication::focusObject())
return;
if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
return;
auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
auto *surface = window->wlSurface();
if (!surface || (surface != m_surface))
return;
queries &= supportedQueries;
// Surrounding text, cursor and anchor positions are transferred together
if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
queries |= Qt::ImSurroundingText | Qt::ImCursorPosition | Qt::ImAnchorPosition;
QInputMethodQueryEvent event(queries);
QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
QString text = event.value(Qt::ImSurroundingText).toString();
int cursor = event.value(Qt::ImCursorPosition).toInt();
int anchor = event.value(Qt::ImAnchorPosition).toInt();
// Make sure text is not too big
if (text.toUtf8().size() > 2048) {
int c = qAbs(cursor - anchor) <= 512 ? qMin(cursor, anchor) + qAbs(cursor - anchor) / 2: cursor;
const int offset = c - qBound(0, c, 512 - qMin(text.size() - c, 256));
text = text.mid(offset + c - 256, 512);
cursor -= offset;
anchor -= offset;
}
set_surrounding_text(text, QWaylandInputMethodEventBuilder::indexToWayland(text, cursor), QWaylandInputMethodEventBuilder::indexToWayland(text, anchor));
}
if (queries & Qt::ImHints) {
QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
set_content_type(contentType.hint, contentType.purpose);
}
if (queries & Qt::ImCursorRectangle) {
const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
const QMargins margins = window->frameMargins();
const QRect &surfaceRect = windowRect.translated(margins.left(), margins.top());
set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
}
if (queries & Qt::ImPreferredLanguage) {
const QString &language = event.value(Qt::ImPreferredLanguage).toString();
set_preferred_language(language);
}
update_state(m_serial, flags);
if (flags != update_state_change) {
if (m_resetCallback)
wl_callback_destroy(m_resetCallback);
m_resetCallback = wl_display_sync(m_display->wl_display());
wl_callback_add_listener(m_resetCallback, &QWaylandTextInput::callbackListener, this);
}
}
void QWaylandTextInput::setCursorInsidePreedit(int)
{
// Not supported yet
}
bool QWaylandTextInput::isInputPanelVisible() const
{
return m_inputPanelVisible;
}
QRectF QWaylandTextInput::keyboardRect() const
{
return m_keyboardRectangle;
}
QLocale QWaylandTextInput::locale() const
{
return m_locale;
}
Qt::LayoutDirection QWaylandTextInput::inputDirection() const
{
return m_inputDirection;
}
void QWaylandTextInput::zwp_text_input_v2_enter(uint32_t serial, ::wl_surface *surface)
{
m_serial = serial;
m_surface = surface;
updateState(Qt::ImQueryAll, update_state_enter);
}
void QWaylandTextInput::zwp_text_input_v2_leave(uint32_t serial, ::wl_surface *surface)
{
m_serial = serial;
if (m_surface != surface) {
qCDebug(qLcQpaInputMethods()) << Q_FUNC_INFO << "Got leave event for surface" << surface << "focused surface" << m_surface;
}
m_surface = nullptr;
}
void QWaylandTextInput::zwp_text_input_v2_modifiers_map(wl_array *map)
{
const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
m_modifiersMap.clear();
for (const QByteArray &modifier : modifiersMap) {
if (modifier == "Shift")
m_modifiersMap.append(Qt::ShiftModifier);
else if (modifier == "Control")
m_modifiersMap.append(Qt::ControlModifier);
else if (modifier == "Alt")
m_modifiersMap.append(Qt::AltModifier);
else if (modifier == "Mod1")
m_modifiersMap.append(Qt::AltModifier);
else if (modifier == "Mod4")
m_modifiersMap.append(Qt::MetaModifier);
else
m_modifiersMap.append(Qt::NoModifier);
}
}
void QWaylandTextInput::zwp_text_input_v2_input_panel_state(uint32_t visible, int32_t x, int32_t y, int32_t width, int32_t height)
{
const bool inputPanelVisible = (visible == input_panel_visibility_visible);
if (m_inputPanelVisible != inputPanelVisible) {
m_inputPanelVisible = inputPanelVisible;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputPanelVisibleChanged();
}
const QRectF keyboardRectangle(x, y, width, height);
if (m_keyboardRectangle != keyboardRectangle) {
m_keyboardRectangle = keyboardRectangle;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitKeyboardRectChanged();
}
}
void QWaylandTextInput::zwp_text_input_v2_preedit_string(const QString &text, const QString &commit)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
m_builder.reset();
return;
}
if (!QGuiApplication::focusObject())
return;
QInputMethodEvent *event = m_builder.buildPreedit(text);
m_builder.reset();
m_preeditCommit = commit;
QCoreApplication::sendEvent(QGuiApplication::focusObject(), event);
delete event;
}
void QWaylandTextInput::zwp_text_input_v2_preedit_styling(uint32_t index, uint32_t length, uint32_t style)
{
m_builder.addPreeditStyling(index, length, style);
}
void QWaylandTextInput::zwp_text_input_v2_preedit_cursor(int32_t index)
{
m_builder.setPreeditCursor(index);
}
void QWaylandTextInput::zwp_text_input_v2_commit_string(const QString &text)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
m_builder.reset();
return;
}
if (!QGuiApplication::focusObject())
return;
QInputMethodEvent *event = m_builder.buildCommit(text);
m_builder.reset();
QCoreApplication::sendEvent(QGuiApplication::focusObject(), event);
delete event;
}
void QWaylandTextInput::zwp_text_input_v2_cursor_position(int32_t index, int32_t anchor)
{
m_builder.setCursorPosition(index, anchor);
}
void QWaylandTextInput::zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length)
{
m_builder.setDeleteSurroundingText(before_length, after_length);
}
void QWaylandTextInput::zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
{
#if QT_CONFIG(xkbcommon)
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
return;
}
if (!QGuiApplication::focusWindow())
return;
Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
QString text = QXkbCommon::lookupStringNoKeysymTransformations(sym);
int qtkey = QXkbCommon::keysymToQtKey(sym, qtModifiers);
QWindowSystemInterface::handleKeyEvent(QGuiApplication::focusWindow(),
time, type, qtkey, qtModifiers, text);
#else
Q_UNUSED(time);
Q_UNUSED(sym);
Q_UNUSED(state);
Q_UNUSED(modifiers);
#endif
}
void QWaylandTextInput::zwp_text_input_v2_language(const QString &language)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
return;
}
const QLocale locale(language);
if (m_locale != locale) {
m_locale = locale;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitLocaleChanged();
}
}
void QWaylandTextInput::zwp_text_input_v2_text_direction(uint32_t direction)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
return;
}
const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
(direction == text_direction_ltr) ? Qt::LeftToRight :
(direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
if (m_inputDirection != inputDirection) {
m_inputDirection = inputDirection;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputDirectionChanged(m_inputDirection);
}
}
void QWaylandTextInput::zwp_text_input_v2_input_method_changed(uint32_t serial, uint32_t flags)
{
Q_UNUSED(flags);
m_serial = serial;
updateState(Qt::ImQueryAll, update_state_full);
}
Qt::KeyboardModifiers QWaylandTextInput::modifiersToQtModifiers(uint32_t modifiers)
{
Qt::KeyboardModifiers ret = Qt::NoModifier;
for (int i = 0; i < m_modifiersMap.size(); ++i) {
if (modifiers & (1 << i)) {
ret |= m_modifiersMap[i];
}
}
return ret;
}
QWaylandInputContext::QWaylandInputContext(QWaylandDisplay *display)
: mDisplay(display)
{
@ -456,17 +118,17 @@ void QWaylandInputContext::update(Qt::InputMethodQueries queries)
auto *currentSurface = surfaceForWindow(mCurrentWindow);
if (currentSurface && !inputMethodAccepted()) {
textInput()->disable(currentSurface);
textInput()->disableSurface(currentSurface);
mCurrentWindow.clear();
} else if (!currentSurface && inputMethodAccepted()) {
QWindow *window = QGuiApplication::focusWindow();
if (auto *focusSurface = surfaceForWindow(window)) {
textInput()->enable(focusSurface);
textInput()->enableSurface(focusSurface);
mCurrentWindow = window;
}
}
textInput()->updateState(queries, QtWayland::zwp_text_input_v2::update_state_change);
textInput()->updateState(queries, QWaylandTextInputInterface::update_state_change);
}
void QWaylandInputContext::invokeAction(QInputMethod::Action action, int cursorPostion)
@ -485,7 +147,7 @@ void QWaylandInputContext::showInputPanel()
if (!textInput())
return;
textInput()->show_input_panel();
textInput()->showInputPanel();
}
void QWaylandInputContext::hideInputPanel()
@ -495,7 +157,7 @@ void QWaylandInputContext::hideInputPanel()
if (!textInput())
return;
textInput()->hide_input_panel();
textInput()->hideInputPanel();
}
bool QWaylandInputContext::isInputPanelVisible() const
@ -556,7 +218,7 @@ void QWaylandInputContext::setFocusObject(QObject *object)
if (mCurrentWindow.data() != window || !inputMethodAccepted()) {
auto *surface = static_cast<QWaylandWindow *>(mCurrentWindow->handle())->wlSurface();
if (surface)
textInput()->disable(surface);
textInput()->disableSurface(surface);
mCurrentWindow.clear();
}
}
@ -565,15 +227,15 @@ void QWaylandInputContext::setFocusObject(QObject *object)
if (mCurrentWindow.data() != window) {
auto *surface = static_cast<QWaylandWindow *>(window->handle())->wlSurface();
if (surface) {
textInput()->enable(surface);
textInput()->enableSurface(surface);
mCurrentWindow = window;
}
}
textInput()->updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_enter);
textInput()->updateState(Qt::ImQueryAll, QWaylandTextInputInterface::update_state_enter);
}
}
QWaylandTextInput *QWaylandInputContext::textInput() const
QWaylandTextInputInterface *QWaylandInputContext::textInput() const
{
return mDisplay->defaultInputDevice()->textInput();
}

View File

@ -54,14 +54,9 @@
#include <qpa/qplatforminputcontext.h>
#include <QList>
#include <QLoggingCategory>
#include <QPointer>
#include <QRectF>
#include <QLocale>
#include <QtWaylandClient/private/qwayland-text-input-unstable-v2.h>
#include <qwaylandinputmethodeventbuilder_p.h>
#include "qwaylandtextinputinterface_p.h"
#include <qtwaylandclientglobal_p.h>
#if QT_CONFIG(xkbcommon)
#include <xkbcommon/xkbcommon-compose.h>
@ -72,69 +67,10 @@ struct wl_callback_listener;
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)
namespace QtWaylandClient {
class QWaylandDisplay;
class QWaylandTextInput : public QtWayland::zwp_text_input_v2
{
public:
QWaylandTextInput(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input);
~QWaylandTextInput() override;
void reset();
void commit();
void updateState(Qt::InputMethodQueries queries, uint32_t flags);
void setCursorInsidePreedit(int cursor);
bool isInputPanelVisible() const;
QRectF keyboardRect() const;
QLocale locale() const;
Qt::LayoutDirection inputDirection() const;
protected:
void zwp_text_input_v2_enter(uint32_t serial, struct ::wl_surface *surface) override;
void zwp_text_input_v2_leave(uint32_t serial, struct ::wl_surface *surface) override;
void zwp_text_input_v2_modifiers_map(wl_array *map) override;
void zwp_text_input_v2_input_panel_state(uint32_t state, int32_t x, int32_t y, int32_t width, int32_t height) override;
void zwp_text_input_v2_preedit_string(const QString &text, const QString &commit) override;
void zwp_text_input_v2_preedit_styling(uint32_t index, uint32_t length, uint32_t style) override;
void zwp_text_input_v2_preedit_cursor(int32_t index) override;
void zwp_text_input_v2_commit_string(const QString &text) override;
void zwp_text_input_v2_cursor_position(int32_t index, int32_t anchor) override;
void zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length) override;
void zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers) override;
void zwp_text_input_v2_language(const QString &language) override;
void zwp_text_input_v2_text_direction(uint32_t direction) override;
void zwp_text_input_v2_input_method_changed(uint32_t serial, uint32_t flags) override;
private:
Qt::KeyboardModifiers modifiersToQtModifiers(uint32_t modifiers);
QWaylandDisplay *m_display = nullptr;
QWaylandInputMethodEventBuilder m_builder;
QList<Qt::KeyboardModifier> m_modifiersMap;
uint32_t m_serial = 0;
struct ::wl_surface *m_surface = nullptr;
QString m_preeditCommit;
bool m_inputPanelVisible = false;
QRectF m_keyboardRectangle;
QLocale m_locale;
Qt::LayoutDirection m_inputDirection = Qt::LayoutDirectionAuto;
struct ::wl_callback *m_resetCallback = nullptr;
static const wl_callback_listener callbackListener;
static void resetCallback(void *data, struct wl_callback *wl_callback, uint32_t time);
};
class QWaylandInputContext : public QPlatformInputContext
{
Q_OBJECT
@ -168,7 +104,7 @@ public:
#endif
private:
QWaylandTextInput *textInput() const;
QWaylandTextInputInterface *textInput() const;
QWaylandDisplay *mDisplay = nullptr;
QPointer<QWindow> mCurrentWindow;

View File

@ -57,6 +57,8 @@
#include "qwaylandcursor_p.h"
#include "qwaylanddisplay_p.h"
#include "qwaylandshmbackingstore_p.h"
#include "qwaylandtextinputv2_p.h"
#include "qwaylandtextinputinterface_p.h"
#include "qwaylandinputcontext_p.h"
#include "qwaylandinputmethodcontext_p.h"
@ -420,7 +422,7 @@ QWaylandInputDevice::QWaylandInputDevice(QWaylandDisplay *display, int version,
#endif
if (mQDisplay->textInputManager())
mTextInput.reset(new QWaylandTextInput(mQDisplay, mQDisplay->textInputManager()->get_text_input(wl_seat())));
mTextInput.reset(new QWaylandTextInputv2(mQDisplay, mQDisplay->textInputManager()->get_text_input(wl_seat())));
if (mQDisplay->textInputMethodManager())
mTextInputMethod.reset(new QWaylandTextInputMethod(mQDisplay, mQDisplay->textInputMethodManager()->get_text_input_method(wl_seat())));
@ -555,7 +557,7 @@ QWaylandPrimarySelectionDeviceV1 *QWaylandInputDevice::primarySelectionDevice()
}
#endif
void QWaylandInputDevice::setTextInput(QWaylandTextInput *textInput)
void QWaylandInputDevice::setTextInput(QWaylandTextInputInterface *textInput)
{
mTextInput.reset(textInput);
}
@ -565,7 +567,7 @@ void QWaylandInputDevice::setTextInputMethod(QWaylandTextInputMethod *textInputM
mTextInputMethod.reset(textInputMethod);
}
QWaylandTextInput *QWaylandInputDevice::textInput() const
QWaylandTextInputInterface *QWaylandInputDevice::textInput() const
{
return mTextInput.data();
}

View File

@ -94,7 +94,7 @@ class QWaylandTabletSeatV2;
class QWaylandPointerGestures;
class QWaylandPointerGestureSwipe;
class QWaylandPointerGesturePinch;
class QWaylandTextInput;
class QWaylandTextInputInterface;
class QWaylandTextInputMethod;
#if QT_CONFIG(cursor)
class QWaylandCursorTheme;
@ -138,8 +138,8 @@ public:
void setTabletSeat(QWaylandTabletSeatV2 *tabletSeat);
QWaylandTabletSeatV2* tabletSeat() const;
void setTextInput(QWaylandTextInput *textInput);
QWaylandTextInput *textInput() const;
void setTextInput(QWaylandTextInputInterface *textInput);
QWaylandTextInputInterface *textInput() const;
void setTextInputMethod(QWaylandTextInputMethod *textInputMethod);
QWaylandTextInputMethod *textInputMethod() const;
@ -199,7 +199,7 @@ protected:
QScopedPointer<QWaylandPointerGesturePinch> mPointerGesturePinch;
QScopedPointer<Touch> mTouch;
QScopedPointer<QWaylandTextInput> mTextInput;
QScopedPointer<QWaylandTextInputInterface> mTextInput;
QScopedPointer<QWaylandTextInputMethod> mTextInputMethod;
QScopedPointer<QWaylandTabletSeatV2> mTabletSeat;

View File

@ -0,0 +1,44 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qwaylandtextinputinterface_p.h"
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE

View File

@ -0,0 +1,95 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QWAYLANDTEXTINPUTINTERFACE_P_H
#define QWAYLANDTEXTINPUTINTERFACE_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/qlocale.h>
#include <QtCore/qrect.h>
struct wl_surface;
QT_BEGIN_NAMESPACE
namespace QtWaylandClient {
class QWaylandTextInputInterface
{
public:
virtual ~QWaylandTextInputInterface() {}
virtual void reset() = 0;
virtual void commit() = 0;
virtual void disableSurface(::wl_surface *surface) = 0;
virtual void enableSurface(::wl_surface *surface) = 0;
virtual void updateState(Qt::InputMethodQueries queries, uint32_t flags) = 0;
virtual void showInputPanel() = 0;
virtual void hideInputPanel() = 0;
virtual bool isInputPanelVisible() const = 0;
virtual QRectF keyboardRect() const = 0;
virtual QLocale locale() const = 0;
virtual Qt::LayoutDirection inputDirection() const = 0;
virtual void setCursorInsidePreedit(int cursor) = 0;
// This enum should be compatible with update_state of text-input-unstable-v2.
// Higher versions of text-input-* protocol may not use it directly
// but QtWaylandClient can determine clients' states based on the values
enum TextInputState {
update_state_change = 0, // updated state because it changed
update_state_full = 1, // full state after enter or input_method_changed event
update_state_reset = 2, // full state after reset
update_state_enter = 3, // full state after switching focus to a different widget on client side
};
};
}
QT_END_NAMESPACE
#endif // QWAYLANDTEXTINPUTINTERFACE_P_H

View File

@ -0,0 +1,403 @@
/****************************************************************************
**
** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWaylandClient module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qpa/qplatforminputcontext.h>
#include "qwaylandtextinputv2_p.h"
#include "qwaylandwindow_p.h"
#include "qwaylandinputmethodeventbuilder_p.h"
#include <QtCore/qloggingcategory.h>
#include <QtGui/QGuiApplication>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformintegration.h>
#include <QtGui/qevent.h>
#include <QtGui/qwindow.h>
#include <QTextCharFormat>
#include <QList>
#include <QRectF>
#include <QLocale>
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)
namespace QtWaylandClient {
namespace {
const Qt::InputMethodQueries supportedQueries = Qt::ImEnabled |
Qt::ImSurroundingText |
Qt::ImCursorPosition |
Qt::ImAnchorPosition |
Qt::ImHints |
Qt::ImCursorRectangle |
Qt::ImPreferredLanguage;
}
QWaylandTextInputv2::QWaylandTextInputv2(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input)
: QtWayland::zwp_text_input_v2(text_input)
, m_display(display)
{
}
QWaylandTextInputv2::~QWaylandTextInputv2()
{
if (m_resetCallback)
wl_callback_destroy(m_resetCallback);
}
void QWaylandTextInputv2::reset()
{
m_builder.reset();
m_preeditCommit = QString();
updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_reset);
}
void QWaylandTextInputv2::commit()
{
if (QObject *o = QGuiApplication::focusObject()) {
QInputMethodEvent event;
event.setCommitString(m_preeditCommit);
QCoreApplication::sendEvent(o, &event);
}
reset();
}
const wl_callback_listener QWaylandTextInputv2::callbackListener = {
QWaylandTextInputv2::resetCallback
};
void QWaylandTextInputv2::resetCallback(void *data, wl_callback *, uint32_t)
{
QWaylandTextInputv2 *self = static_cast<QWaylandTextInputv2*>(data);
if (self->m_resetCallback) {
wl_callback_destroy(self->m_resetCallback);
self->m_resetCallback = nullptr;
}
}
void QWaylandTextInputv2::updateState(Qt::InputMethodQueries queries, uint32_t flags)
{
if (!QGuiApplication::focusObject())
return;
if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
return;
auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
auto *surface = window->wlSurface();
if (!surface || (surface != m_surface))
return;
queries &= supportedQueries;
// Surrounding text, cursor and anchor positions are transferred together
if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
queries |= Qt::ImSurroundingText | Qt::ImCursorPosition | Qt::ImAnchorPosition;
QInputMethodQueryEvent event(queries);
QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
QString text = event.value(Qt::ImSurroundingText).toString();
int cursor = event.value(Qt::ImCursorPosition).toInt();
int anchor = event.value(Qt::ImAnchorPosition).toInt();
// Make sure text is not too big
if (text.toUtf8().size() > 2048) {
int c = qAbs(cursor - anchor) <= 512 ? qMin(cursor, anchor) + qAbs(cursor - anchor) / 2: cursor;
const int offset = c - qBound(0, c, 512 - qMin(text.size() - c, 256));
text = text.mid(offset + c - 256, 512);
cursor -= offset;
anchor -= offset;
}
set_surrounding_text(text, QWaylandInputMethodEventBuilder::indexToWayland(text, cursor), QWaylandInputMethodEventBuilder::indexToWayland(text, anchor));
}
if (queries & Qt::ImHints) {
QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
set_content_type(contentType.hint, contentType.purpose);
}
if (queries & Qt::ImCursorRectangle) {
const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
const QMargins margins = window->frameMargins();
const QRect &surfaceRect = windowRect.translated(margins.left(), margins.top());
set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
}
if (queries & Qt::ImPreferredLanguage) {
const QString &language = event.value(Qt::ImPreferredLanguage).toString();
set_preferred_language(language);
}
update_state(m_serial, flags);
if (flags != QtWayland::zwp_text_input_v2::update_state_change) {
if (m_resetCallback)
wl_callback_destroy(m_resetCallback);
m_resetCallback = wl_display_sync(m_display->wl_display());
wl_callback_add_listener(m_resetCallback, &QWaylandTextInputv2::callbackListener, this);
}
}
void QWaylandTextInputv2::setCursorInsidePreedit(int)
{
// Not supported yet
}
bool QWaylandTextInputv2::isInputPanelVisible() const
{
return m_inputPanelVisible;
}
QRectF QWaylandTextInputv2::keyboardRect() const
{
return m_keyboardRectangle;
}
QLocale QWaylandTextInputv2::locale() const
{
return m_locale;
}
Qt::LayoutDirection QWaylandTextInputv2::inputDirection() const
{
return m_inputDirection;
}
void QWaylandTextInputv2::zwp_text_input_v2_enter(uint32_t serial, ::wl_surface *surface)
{
m_serial = serial;
m_surface = surface;
updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_enter);
}
void QWaylandTextInputv2::zwp_text_input_v2_leave(uint32_t serial, ::wl_surface *surface)
{
m_serial = serial;
if (m_surface != surface) {
qCDebug(qLcQpaInputMethods()) << Q_FUNC_INFO << "Got leave event for surface" << surface << "focused surface" << m_surface;
}
m_surface = nullptr;
}
void QWaylandTextInputv2::zwp_text_input_v2_modifiers_map(wl_array *map)
{
const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
m_modifiersMap.clear();
for (const QByteArray &modifier : modifiersMap) {
if (modifier == "Shift")
m_modifiersMap.append(Qt::ShiftModifier);
else if (modifier == "Control")
m_modifiersMap.append(Qt::ControlModifier);
else if (modifier == "Alt")
m_modifiersMap.append(Qt::AltModifier);
else if (modifier == "Mod1")
m_modifiersMap.append(Qt::AltModifier);
else if (modifier == "Mod4")
m_modifiersMap.append(Qt::MetaModifier);
else
m_modifiersMap.append(Qt::NoModifier);
}
}
void QWaylandTextInputv2::zwp_text_input_v2_input_panel_state(uint32_t visible, int32_t x, int32_t y, int32_t width, int32_t height)
{
const bool inputPanelVisible = (visible == input_panel_visibility_visible);
if (m_inputPanelVisible != inputPanelVisible) {
m_inputPanelVisible = inputPanelVisible;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputPanelVisibleChanged();
}
const QRectF keyboardRectangle(x, y, width, height);
if (m_keyboardRectangle != keyboardRectangle) {
m_keyboardRectangle = keyboardRectangle;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitKeyboardRectChanged();
}
}
void QWaylandTextInputv2::zwp_text_input_v2_preedit_string(const QString &text, const QString &commit)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
m_builder.reset();
return;
}
if (!QGuiApplication::focusObject())
return;
QInputMethodEvent *event = m_builder.buildPreedit(text);
m_builder.reset();
m_preeditCommit = commit;
QCoreApplication::sendEvent(QGuiApplication::focusObject(), event);
delete event;
}
void QWaylandTextInputv2::zwp_text_input_v2_preedit_styling(uint32_t index, uint32_t length, uint32_t style)
{
m_builder.addPreeditStyling(index, length, style);
}
void QWaylandTextInputv2::zwp_text_input_v2_preedit_cursor(int32_t index)
{
m_builder.setPreeditCursor(index);
}
void QWaylandTextInputv2::zwp_text_input_v2_commit_string(const QString &text)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
m_builder.reset();
return;
}
if (!QGuiApplication::focusObject())
return;
QInputMethodEvent *event = m_builder.buildCommit(text);
m_builder.reset();
QCoreApplication::sendEvent(QGuiApplication::focusObject(), event);
delete event;
}
void QWaylandTextInputv2::zwp_text_input_v2_cursor_position(int32_t index, int32_t anchor)
{
m_builder.setCursorPosition(index, anchor);
}
void QWaylandTextInputv2::zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length)
{
m_builder.setDeleteSurroundingText(before_length, after_length);
}
void QWaylandTextInputv2::zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
{
#if QT_CONFIG(xkbcommon)
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
return;
}
if (!QGuiApplication::focusWindow())
return;
Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
QString text = QXkbCommon::lookupStringNoKeysymTransformations(sym);
int qtkey = QXkbCommon::keysymToQtKey(sym, qtModifiers);
QWindowSystemInterface::handleKeyEvent(QGuiApplication::focusWindow(),
time, type, qtkey, qtModifiers, text);
#else
Q_UNUSED(time);
Q_UNUSED(sym);
Q_UNUSED(state);
Q_UNUSED(modifiers);
#endif
}
void QWaylandTextInputv2::zwp_text_input_v2_language(const QString &language)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
return;
}
const QLocale locale(language);
if (m_locale != locale) {
m_locale = locale;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitLocaleChanged();
}
}
void QWaylandTextInputv2::zwp_text_input_v2_text_direction(uint32_t direction)
{
if (m_resetCallback) {
qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
return;
}
const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
(direction == text_direction_ltr) ? Qt::LeftToRight :
(direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
if (m_inputDirection != inputDirection) {
m_inputDirection = inputDirection;
QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputDirectionChanged(m_inputDirection);
}
}
void QWaylandTextInputv2::zwp_text_input_v2_input_method_changed(uint32_t serial, uint32_t flags)
{
Q_UNUSED(flags);
m_serial = serial;
updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_full);
}
Qt::KeyboardModifiers QWaylandTextInputv2::modifiersToQtModifiers(uint32_t modifiers)
{
Qt::KeyboardModifiers ret = Qt::NoModifier;
for (int i = 0; i < m_modifiersMap.size(); ++i) {
if (modifiers & (1 << i)) {
ret |= m_modifiersMap[i];
}
}
return ret;
}
}
QT_END_NAMESPACE

View File

@ -0,0 +1,146 @@
/****************************************************************************
**
** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWaylandClient module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QWAYLANDINPUTV2_P_H
#define QWAYLANDINPUTV2_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "qwaylandtextinputinterface_p.h"
#include <QtWaylandClient/private/qwayland-text-input-unstable-v2.h>
#include <qwaylandinputmethodeventbuilder_p.h>
struct wl_callback;
struct wl_callback_listener;
QT_BEGIN_NAMESPACE
namespace QtWaylandClient {
class QWaylandDisplay;
class QWaylandTextInputv2 : public QtWayland::zwp_text_input_v2, public QWaylandTextInputInterface
{
public:
QWaylandTextInputv2(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input);
~QWaylandTextInputv2() override;
void reset() override;
void commit() override;
void updateState(Qt::InputMethodQueries queries, uint32_t flags) override;
void setCursorInsidePreedit(int cursor) override;
bool isInputPanelVisible() const override;
QRectF keyboardRect() const override;
QLocale locale() const override;
Qt::LayoutDirection inputDirection() const override;
void showInputPanel() override
{
show_input_panel();
}
void hideInputPanel() override
{
hide_input_panel();
}
void enableSurface(::wl_surface *surface) override
{
enable(surface);
}
void disableSurface(::wl_surface *surface) override
{
disable(surface);
}
protected:
void zwp_text_input_v2_enter(uint32_t serial, struct ::wl_surface *surface) override;
void zwp_text_input_v2_leave(uint32_t serial, struct ::wl_surface *surface) override;
void zwp_text_input_v2_modifiers_map(wl_array *map) override;
void zwp_text_input_v2_input_panel_state(uint32_t state, int32_t x, int32_t y, int32_t width, int32_t height) override;
void zwp_text_input_v2_preedit_string(const QString &text, const QString &commit) override;
void zwp_text_input_v2_preedit_styling(uint32_t index, uint32_t length, uint32_t style) override;
void zwp_text_input_v2_preedit_cursor(int32_t index) override;
void zwp_text_input_v2_commit_string(const QString &text) override;
void zwp_text_input_v2_cursor_position(int32_t index, int32_t anchor) override;
void zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length) override;
void zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers) override;
void zwp_text_input_v2_language(const QString &language) override;
void zwp_text_input_v2_text_direction(uint32_t direction) override;
void zwp_text_input_v2_input_method_changed(uint32_t serial, uint32_t flags) override;
private:
Qt::KeyboardModifiers modifiersToQtModifiers(uint32_t modifiers);
QWaylandDisplay *m_display = nullptr;
QWaylandInputMethodEventBuilder m_builder;
QList<Qt::KeyboardModifier> m_modifiersMap;
uint32_t m_serial = 0;
struct ::wl_surface *m_surface = nullptr;
QString m_preeditCommit;
bool m_inputPanelVisible = false;
QRectF m_keyboardRectangle;
QLocale m_locale;
Qt::LayoutDirection m_inputDirection = Qt::LayoutDirectionAuto;
struct ::wl_callback *m_resetCallback = nullptr;
static const wl_callback_listener callbackListener;
static void resetCallback(void *data, struct wl_callback *wl_callback, uint32_t time);
};
}
QT_END_NAMESPACE
#endif // QWAYLANDTEXTINPUTV2_P_H