Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwaylandtextinputv2.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qpa/qplatforminputcontext.h>
5
7
8#include "qwaylandwindow_p.h"
10
11#include <QtCore/qloggingcategory.h>
12#include <QtGui/QGuiApplication>
13#include <QtGui/private/qguiapplication_p.h>
14#include <QtGui/qpa/qplatformintegration.h>
15#include <QtGui/qevent.h>
16#include <QtGui/qwindow.h>
17#include <QTextCharFormat>
18#include <QList>
19#include <QRectF>
20#include <QLocale>
21
23
24Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)
25
26namespace QtWaylandClient {
27
28namespace {
29
30const Qt::InputMethodQueries supportedQueries2 = Qt::ImEnabled |
37}
38
39QWaylandTextInputv2::QWaylandTextInputv2(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input)
40 : QtWayland::zwp_text_input_v2(text_input)
41 , m_display(display)
42{
43}
44
46{
47 if (m_resetCallback)
48 wl_callback_destroy(m_resetCallback);
49}
50
52{
53 m_builder.reset();
54 m_preeditCommit = QString();
55 updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_reset);
56}
57
59{
61 if (!m_preeditCommit.isEmpty()) {
62
64 event.setCommitString(m_preeditCommit);
65 m_preeditCommit = QString();
66
68 }
69 }
70
71 reset();
72}
73
74const wl_callback_listener QWaylandTextInputv2::callbackListener = {
75 QWaylandTextInputv2::resetCallback
76};
77
78void QWaylandTextInputv2::resetCallback(void *data, wl_callback *, uint32_t)
79{
80 QWaylandTextInputv2 *self = static_cast<QWaylandTextInputv2*>(data);
81
82 if (self->m_resetCallback) {
83 wl_callback_destroy(self->m_resetCallback);
84 self->m_resetCallback = nullptr;
85 }
86}
87
88void QWaylandTextInputv2::updateState(Qt::InputMethodQueries queries, uint32_t flags)
89{
91 return;
92
94 return;
95
96 auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
97 auto *surface = window->wlSurface();
98 if (!surface || (surface != m_surface))
99 return;
100
101 queries &= supportedQueries2;
102
103 // Surrounding text, cursor and anchor positions are transferred together
104 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
106
109
110 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
111 QString text = event.value(Qt::ImSurroundingText).toString();
112 int cursor = event.value(Qt::ImCursorPosition).toInt();
113 int anchor = event.value(Qt::ImAnchorPosition).toInt();
114
115 // Make sure text is not too big
116 if (text.toUtf8().size() > 2048) {
117 int c = qAbs(cursor - anchor) <= 512 ? qMin(cursor, anchor) + qAbs(cursor - anchor) / 2: cursor;
118
119 const int offset = c - qBound(0, c, 512 - qMin(text.size() - c, 256));
120 text = text.mid(offset + c - 256, 512);
121 cursor -= offset;
122 anchor -= offset;
123 }
124
126 }
127
128 if (queries & Qt::ImHints) {
129 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
130 set_content_type(contentType.hint, contentType.purpose);
131 }
132
133 if (queries & Qt::ImCursorRectangle) {
134 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
135 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
136 const QMargins margins = window->frameMargins();
137 const QRect &surfaceRect = windowRect.translated(margins.left(), margins.top());
138 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
139 }
140
141 if (queries & Qt::ImPreferredLanguage) {
142 const QString &language = event.value(Qt::ImPreferredLanguage).toString();
143 set_preferred_language(language);
144 }
145
146 update_state(m_serial, flags);
147 if (flags != QtWayland::zwp_text_input_v2::update_state_change) {
148 if (m_resetCallback)
149 wl_callback_destroy(m_resetCallback);
150 m_resetCallback = wl_display_sync(m_display->wl_display());
151 wl_callback_add_listener(m_resetCallback, &QWaylandTextInputv2::callbackListener, this);
152 }
153}
154
156{
157 // Not supported yet
158}
159
161{
162 return m_inputPanelVisible;
163}
164
166{
167 return m_keyboardRectangle;
168}
169
171{
172 return m_locale;
173}
174
176{
177 return m_inputDirection;
178}
179
180void QWaylandTextInputv2::zwp_text_input_v2_enter(uint32_t serial, ::wl_surface *surface)
181{
182 m_serial = serial;
183 m_surface = surface;
184
185 updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_enter);
186}
187
188void QWaylandTextInputv2::zwp_text_input_v2_leave(uint32_t serial, ::wl_surface *surface)
189{
190 m_serial = serial;
191
192 if (m_surface != surface) {
193 qCDebug(qLcQpaInputMethods()) << Q_FUNC_INFO << "Got leave event for surface" << surface << "focused surface" << m_surface;
194 }
195
196 m_surface = nullptr;
197}
198
200{
201 const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
202
203 m_modifiersMap.clear();
204
205 for (const QByteArray &modifier : modifiersMap) {
206 if (modifier == "Shift")
207 m_modifiersMap.append(Qt::ShiftModifier);
208 else if (modifier == "Control")
209 m_modifiersMap.append(Qt::ControlModifier);
210 else if (modifier == "Alt")
211 m_modifiersMap.append(Qt::AltModifier);
212 else if (modifier == "Mod1")
213 m_modifiersMap.append(Qt::AltModifier);
214 else if (modifier == "Mod4")
215 m_modifiersMap.append(Qt::MetaModifier);
216 else
217 m_modifiersMap.append(Qt::NoModifier);
218 }
219}
220
221void QWaylandTextInputv2::zwp_text_input_v2_input_panel_state(uint32_t visible, int32_t x, int32_t y, int32_t width, int32_t height)
222{
223 const bool inputPanelVisible = (visible == input_panel_visibility_visible);
224 if (m_inputPanelVisible != inputPanelVisible) {
225 m_inputPanelVisible = inputPanelVisible;
227 }
228 const QRectF keyboardRectangle(x, y, width, height);
229 if (m_keyboardRectangle != keyboardRectangle) {
230 m_keyboardRectangle = keyboardRectangle;
232 }
233}
234
236{
237 if (m_resetCallback) {
238 qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
239 m_builder.reset();
240 return;
241 }
242
244 return;
245
246 QInputMethodEvent *event = m_builder.buildPreedit(text);
247
248 m_builder.reset();
249 m_preeditCommit = commit;
250
252 delete event;
253}
254
256{
257 m_builder.addPreeditStyling(index, length, style);
258}
259
261{
262 m_builder.setPreeditCursor(index);
263}
264
266{
267 if (m_resetCallback) {
268 qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
269 m_builder.reset();
270 return;
271 }
272
274 return;
275
276 QInputMethodEvent *event = m_builder.buildCommit(text);
277
278 m_builder.reset();
279
281 delete event;
282}
283
285{
286 m_builder.setCursorPosition(index, anchor);
287}
288
289void QWaylandTextInputv2::zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length)
290{
291 m_builder.setDeleteSurroundingText(before_length, after_length);
292}
293
294void QWaylandTextInputv2::zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
295{
296#if QT_CONFIG(xkbcommon)
297 if (m_resetCallback) {
298 qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
299 return;
300 }
301
303 return;
304
305 Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
306
307 QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
309 int qtkey = QXkbCommon::keysymToQtKey(sym, qtModifiers);
310
312 time, type, qtkey, qtModifiers, text);
313#else
314 Q_UNUSED(time);
315 Q_UNUSED(sym);
318#endif
319}
320
322{
323 if (m_resetCallback) {
324 qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
325 return;
326 }
327
328 const QLocale locale(language);
329 if (m_locale != locale) {
330 m_locale = locale;
332 }
333}
334
336{
337 if (m_resetCallback) {
338 qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
339 return;
340 }
341
342 const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
343 (direction == text_direction_ltr) ? Qt::LeftToRight :
344 (direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
345 if (m_inputDirection != inputDirection) {
346 m_inputDirection = inputDirection;
348 }
349}
350
352{
354
355 m_serial = serial;
356 updateState(Qt::ImQueryAll, QtWayland::zwp_text_input_v2::update_state_full);
357}
358
359Qt::KeyboardModifiers QWaylandTextInputv2::modifiersToQtModifiers(uint32_t modifiers)
360{
361 Qt::KeyboardModifiers ret = Qt::NoModifier;
362 for (int i = 0; i < m_modifiersMap.size(); ++i) {
363 if (modifiers & (1 << i)) {
364 ret |= m_modifiersMap[i];
365 }
366 }
367 return ret;
368}
369
370}
371
\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.
void emitKeyboardRectChanged()
Active QPlatformInputContext is responsible for providing keyboardRectangle 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
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
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
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)
struct wl_display * wl_display() const
void zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers) override
void zwp_text_input_v2_preedit_styling(uint32_t index, uint32_t length, uint32_t style) override
void updateState(Qt::InputMethodQueries queries, uint32_t flags) override
void zwp_text_input_v2_delete_surrounding_text(uint32_t before_length, uint32_t after_length) override
void setCursorInsidePreedit(int cursor) override
void zwp_text_input_v2_preedit_string(const QString &text, const QString &commit) override
void zwp_text_input_v2_input_method_changed(uint32_t serial, uint32_t flags) override
void zwp_text_input_v2_language(const QString &language) override
QWaylandTextInputv2(QWaylandDisplay *display, struct ::zwp_text_input_v2 *text_input)
Qt::LayoutDirection inputDirection() const override
void zwp_text_input_v2_enter(uint32_t serial, struct ::wl_surface *surface) override
void zwp_text_input_v2_text_direction(uint32_t direction) override
void zwp_text_input_v2_modifiers_map(wl_array *map) 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_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_cursor(int32_t index) override
void zwp_text_input_v2_leave(uint32_t serial, struct ::wl_surface *surface) 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 Q_FUNC_INFO
#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
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLuint index
[2]
GLenum GLuint GLenum GLsizei length
GLint GLint GLint GLint GLsizei GLsizei GLsizei GLboolean commit
GLint GLsizei width
GLenum type
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLint y
struct _cl_event * event
const GLubyte * c
#define Q_UNUSED(x)
aWidget window() -> setWindowTitle("New Window Title")
[2]
static QWaylandInputMethodContentType convert(Qt::InputMethodHints hints)