Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwaylandtextinputv1.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include <qpa/qplatforminputcontext.h>
4
6
7#include "qwaylandwindow_p.h"
9
10#include <QtCore/qloggingcategory.h>
11#include <QtGui/QGuiApplication>
12#include <QtGui/private/qguiapplication_p.h>
13#include <QtGui/qpa/qplatformintegration.h>
14#include <QtGui/qevent.h>
15#include <QtGui/qwindow.h>
16#include <QTextCharFormat>
17#include <QList>
18#include <QRectF>
19#include <QLocale>
20
22
23Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)
24
25namespace QtWaylandClient {
26
27namespace {
28
29const Qt::InputMethodQueries supportedQueries1 = Qt::ImEnabled |
36}
37
38QWaylandTextInputv1::QWaylandTextInputv1(QWaylandDisplay *display, struct ::zwp_text_input_v1 *text_input)
39 : QtWayland::zwp_text_input_v1(text_input)
40{
42}
43
45{
46 if (m_resetCallback)
47 wl_callback_destroy(m_resetCallback);
48}
49
51{
52 m_builder.reset();
53 m_preeditCommit = QString();
55}
56
58{
61 event.setCommitString(m_preeditCommit);
63 }
64
65 reset();
66}
67
68const wl_callback_listener QWaylandTextInputv1::callbackListener = {
69 QWaylandTextInputv1::resetCallback
70};
71
72void QWaylandTextInputv1::resetCallback(void *data, wl_callback *, uint32_t)
73{
74 QWaylandTextInputv1 *self = static_cast<QWaylandTextInputv1*>(data);
75
76 if (self->m_resetCallback) {
77 wl_callback_destroy(self->m_resetCallback);
78 self->m_resetCallback = nullptr;
79 }
80}
81
82void QWaylandTextInputv1::updateState(Qt::InputMethodQueries queries, uint32_t flags)
83{
85 return;
86
88 return;
89
90 auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
91 auto *surface = window->wlSurface();
92 if (!surface || (surface != m_surface))
93 return;
94
95 queries &= supportedQueries1;
96
97 // Surrounding text, cursor and anchor positions are transferred together
98 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
100
103
104 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
105 QString text = event.value(Qt::ImSurroundingText).toString();
106 int cursor = event.value(Qt::ImCursorPosition).toInt();
107 int anchor = event.value(Qt::ImAnchorPosition).toInt();
108
109 // Make sure text is not too big
110 if (text.toUtf8().size() > 2048) {
111 int c = qAbs(cursor - anchor) <= 512 ? qMin(cursor, anchor) + qAbs(cursor - anchor) / 2: cursor;
112
113 const int offset = c - qBound(0, c, 512 - qMin(text.size() - c, 256));
114 text = text.mid(offset + c - 256, 512);
115 cursor -= offset;
116 anchor -= offset;
117 }
118
120 }
121
122 if (queries & Qt::ImHints) {
123 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
124 set_content_type(contentType.hint, contentType.purpose);
125 }
126
127 if (queries & Qt::ImCursorRectangle) {
128 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
129 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
130 const QMargins margins = window->frameMargins();
131 const QRect &surfaceRect = windowRect.translated(margins.left(), margins.top());
132 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
133 }
134
135 if (queries & Qt::ImPreferredLanguage) {
136 const QString &language = event.value(Qt::ImPreferredLanguage).toString();
137 set_preferred_language(language);
138 }
139
141 QtWayland::zwp_text_input_v1::reset();
142 else
143 commit_state(m_serial);
144}
145
147{
148 // Not supported yet
149}
150
152{
153 return m_inputPanelVisible;
154}
155
157{
158 return m_keyboardRectangle;
159}
160
162{
163 return m_locale;
164}
165
167{
168 return m_inputDirection;
169}
170
172{
173 m_surface = surface;
174
176}
177
179{
180 m_surface = nullptr;
181}
182
184{
185 const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
186
187 m_modifiersMap.clear();
188
189 for (const QByteArray &modifier : modifiersMap) {
190 if (modifier == "Shift")
191 m_modifiersMap.append(Qt::ShiftModifier);
192 else if (modifier == "Control")
193 m_modifiersMap.append(Qt::ControlModifier);
194 else if (modifier == "Alt")
195 m_modifiersMap.append(Qt::AltModifier);
196 else if (modifier == "Mod1")
197 m_modifiersMap.append(Qt::AltModifier);
198 else if (modifier == "Mod4")
199 m_modifiersMap.append(Qt::MetaModifier);
200 else
201 m_modifiersMap.append(Qt::NoModifier);
202 }
203}
204
206{
207 const bool inputPanelVisible = (visible == 1);
208 if (m_inputPanelVisible != inputPanelVisible) {
209 m_inputPanelVisible = inputPanelVisible;
211 }
212}
213
215{
216 m_serial = serial;
217
218 if (m_resetCallback) {
219 qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
220 m_builder.reset();
221 return;
222 }
223
225 return;
226
227 QInputMethodEvent *event = m_builder.buildPreedit(text);
228
229 m_builder.reset();
230 m_preeditCommit = commit;
231
233 delete event;
234}
235
237{
238 m_builder.addPreeditStyling(index, length, style);
239}
240
242{
243 m_builder.setPreeditCursor(index);
244}
245
247{
248 m_serial = serial;
249
250 if (m_resetCallback) {
251 qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
252 m_builder.reset();
253 return;
254 }
255
257 return;
258
259 // When committing the text, the preeditString needs to be reset, to prevent it to be
260 // send again in the commit() function
261 m_preeditCommit.clear();
262
263 QInputMethodEvent *event = m_builder.buildCommit(text);
264
265 m_builder.reset();
266
268 delete event;
269}
270
272{
273 m_builder.setCursorPosition(index, anchor);
274}
275
276void QWaylandTextInputv1::zwp_text_input_v1_delete_surrounding_text(int32_t before_length, uint32_t after_length)
277{
278 //before_length is negative, but the builder expects it to be positive
279 m_builder.setDeleteSurroundingText(-before_length, after_length);
280}
281
282void QWaylandTextInputv1::zwp_text_input_v1_keysym(uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
283{
284 m_serial = serial;
285
286#if QT_CONFIG(xkbcommon)
287 if (m_resetCallback) {
288 qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
289 return;
290 }
291
293 return;
294
295 Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
296
297 QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
299 int qtkey = QXkbCommon::keysymToQtKey(sym, qtModifiers);
300
302 time, type, qtkey, qtModifiers, text);
303#else
304 Q_UNUSED(time);
305 Q_UNUSED(sym);
308#endif
309}
310
312{
313 m_serial = serial;
314
315 if (m_resetCallback) {
316 qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
317 return;
318 }
319
320 const QLocale locale(language);
321 if (m_locale != locale) {
322 m_locale = locale;
324 }
325}
326
328{
329 m_serial = serial;
330
331 if (m_resetCallback) {
332 qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
333 return;
334 }
335
336 const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
337 (direction == text_direction_ltr) ? Qt::LeftToRight :
338 (direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
339 if (m_inputDirection != inputDirection) {
340 m_inputDirection = inputDirection;
342 }
343}
344
345Qt::KeyboardModifiers QWaylandTextInputv1::modifiersToQtModifiers(uint32_t modifiers)
346{
347 Qt::KeyboardModifiers ret = Qt::NoModifier;
348 for (int i = 0; i < m_modifiersMap.size(); ++i) {
349 if (modifiers & (1 << i)) {
350 ret |= m_modifiersMap[i];
351 }
352 }
353 return ret;
354}
355
356}
357
359
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays.
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:394
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
@ KeyRelease
Definition qcoreevent.h:65
@ KeyPress
Definition qcoreevent.h:64
static QPlatformIntegration * platformIntegration()
static QObject * focusObject()
Returns the QObject in currently active window that will be final receiver of events tied to focus,...
static QWindow * focusWindow()
Returns the QWindow that receives events tied to focus, such as key events.
static QInputMethod * inputMethod()
returns the input method.
The QInputMethodEvent class provides parameters for input method events.
Definition qevent.h:624
The QInputMethodQueryEvent class provides an event sent by the input context to input objects.
Definition qevent.h:678
QTransform inputItemTransform() const
Returns the transformation from input item coordinates to the window coordinates.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
size_type size() const
Definition qmap.h:266
\inmodule QtCore
Definition qmargins.h:23
constexpr int left() const noexcept
Returns the left margin.
Definition qmargins.h:110
constexpr int top() const noexcept
Returns the top margin.
Definition qmargins.h:113
\inmodule QtCore
Definition qobject.h:90
void emitInputDirectionChanged(Qt::LayoutDirection newDirection)
void emitInputPanelVisibleChanged()
Active QPlatformInputContext is responsible for providing visible property to QInputMethod.
virtual QPlatformInputContext * inputContext() const
Returns the platforms input context.
\inmodule QtCore\reentrant
Definition qrect.h:483
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:238
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:184
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:235
constexpr QRect translated(int dx, int dy) const noexcept
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis,...
Definition qrect.h:260
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:187
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1107
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
QByteArray toUtf8() const &
Definition qstring.h:563
QRect mapRect(const QRect &) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void addPreeditStyling(uint32_t index, uint32_t length, uint32_t style)
QInputMethodEvent * buildCommit(const QString &text)
QInputMethodEvent * buildPreedit(const QString &text)
void setDeleteSurroundingText(uint32_t beforeLength, uint32_t afterLength)
void setCursorPosition(int32_t index, int32_t anchor)
static int indexToWayland(const QString &text, int length, int base=0)
static bool handleKeyEvent(QWindow *window, QEvent::Type t, int k, Qt::KeyboardModifiers mods, const QString &text=QString(), bool autorep=false, ushort count=1)
static QString lookupStringNoKeysymTransformations(xkb_keysym_t keysym)
static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers)
void zwp_text_input_v1_language(uint32_t serial, const QString &language) override
void zwp_text_input_v1_keysym(uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers) override
QWaylandTextInputv1(QWaylandDisplay *display, struct ::zwp_text_input_v1 *text_input)
void zwp_text_input_v1_enter(struct ::wl_surface *surface) override
void zwp_text_input_v1_input_panel_state(uint32_t state) override
Qt::LayoutDirection inputDirection() const override
void zwp_text_input_v1_cursor_position(int32_t index, int32_t anchor) override
void zwp_text_input_v1_modifiers_map(wl_array *map) override
void zwp_text_input_v1_preedit_string(uint32_t serial, const QString &text, const QString &commit) override
void zwp_text_input_v1_commit_string(uint32_t serial, const QString &text) override
void zwp_text_input_v1_delete_surrounding_text(int32_t before_length, uint32_t after_length) override
void updateState(Qt::InputMethodQueries queries, uint32_t flags) override
void zwp_text_input_v1_preedit_cursor(int32_t index) override
void zwp_text_input_v1_text_direction(uint32_t serial, uint32_t direction) override
void zwp_text_input_v1_preedit_styling(uint32_t index, uint32_t length, uint32_t style) override
void setCursorInsidePreedit(int cursor) override
EGLImageKHR int int EGLuint64KHR * modifiers
QMap< QString, QString > map
[6]
QString text
QCursor cursor
direction
else opt state
[0]
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
@ ImSurroundingText
@ ImCursorPosition
@ ImPreferredLanguage
@ ImAnchorPosition
@ ImCursorRectangle
@ ImHints
@ ImEnabled
@ ImQueryAll
LayoutDirection
@ LeftToRight
@ LayoutDirectionAuto
@ RightToLeft
@ ShiftModifier
@ ControlModifier
@ MetaModifier
@ NoModifier
@ AltModifier
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
return ret
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLuint64 GLenum void * handle
GLuint index
[2]
GLenum GLuint GLenum GLsizei length
GLint GLint GLint GLint GLsizei GLsizei GLsizei GLboolean commit
GLenum type
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
struct _cl_event * event
const GLubyte * c
#define Q_UNUSED(x)
aWidget window() -> setWindowTitle("New Window Title")
[2]
static QWaylandInputMethodContentType convert(Qt::InputMethodHints hints)