Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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/private/qhighdpiscaling_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 supportedQueries1 = Qt::ImEnabled |
37}
38
39QWaylandTextInputv1::QWaylandTextInputv1(QWaylandDisplay *display, struct ::zwp_text_input_v1 *text_input)
40 : QtWayland::zwp_text_input_v1(text_input)
41{
43}
44
46{
47 if (m_resetCallback)
48 wl_callback_destroy(m_resetCallback);
49 zwp_text_input_v1_destroy(object());
50}
51
58
60{
63 event.setCommitString(m_preeditCommit);
65 }
66
67 reset();
68}
69
70const wl_callback_listener QWaylandTextInputv1::callbackListener = {
71 QWaylandTextInputv1::resetCallback
72};
73
74void QWaylandTextInputv1::resetCallback(void *data, wl_callback *, uint32_t)
75{
76 QWaylandTextInputv1 *self = static_cast<QWaylandTextInputv1*>(data);
77
78 if (self->m_resetCallback) {
79 wl_callback_destroy(self->m_resetCallback);
80 self->m_resetCallback = nullptr;
81 }
82}
83
84void QWaylandTextInputv1::updateState(Qt::InputMethodQueries queries, uint32_t flags)
85{
87 return;
88
90 return;
91
92 auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
93 auto *surface = window->wlSurface();
94 if (!surface || (surface != m_surface))
95 return;
96
97 queries &= supportedQueries1;
98
99 // Surrounding text, cursor and anchor positions are transferred together
100 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
102
105
106 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
107 QString text = event.value(Qt::ImSurroundingText).toString();
108 int cursor = event.value(Qt::ImCursorPosition).toInt();
109 int anchor = event.value(Qt::ImAnchorPosition).toInt();
110
111 // Make sure text is not too big
112 if (text.toUtf8().size() > 2048) {
113 int c = qAbs(cursor - anchor) <= 512 ? qMin(cursor, anchor) + qAbs(cursor - anchor) / 2: cursor;
114
115 const int offset = c - qBound(0, c, 512 - qMin(text.size() - c, 256));
116 text = text.mid(offset + c - 256, 512);
117 cursor -= offset;
118 anchor -= offset;
119 }
120
122 }
123
124 if (queries & Qt::ImHints) {
125 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
126 set_content_type(contentType.hint, contentType.purpose);
127 }
128
129 if (queries & Qt::ImCursorRectangle) {
130 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
131 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
132 const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
133 const QMargins margins = window->clientSideMargins();
134 const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
135 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
136 }
137
138 if (queries & Qt::ImPreferredLanguage) {
139 const QString &language = event.value(Qt::ImPreferredLanguage).toString();
140 set_preferred_language(language);
141 }
142
144 QtWayland::zwp_text_input_v1::reset();
145 else
146 commit_state(m_serial);
147}
148
150{
151 // Not supported yet
152}
153
155{
156 return m_inputPanelVisible;
157}
158
160{
161 return m_keyboardRectangle;
162}
163
165{
166 return m_locale;
167}
168
170{
171 return m_inputDirection;
172}
173
180
182{
183 m_surface = nullptr;
184}
185
187{
188 const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
189
190 m_modifiersMap.clear();
191
192 for (const QByteArray &modifier : modifiersMap) {
193 if (modifier == "Shift")
194 m_modifiersMap.append(Qt::ShiftModifier);
195 else if (modifier == "Control")
196 m_modifiersMap.append(Qt::ControlModifier);
197 else if (modifier == "Alt")
198 m_modifiersMap.append(Qt::AltModifier);
199 else if (modifier == "Mod1")
200 m_modifiersMap.append(Qt::AltModifier);
201 else if (modifier == "Mod4")
202 m_modifiersMap.append(Qt::MetaModifier);
203 else
204 m_modifiersMap.append(Qt::NoModifier);
205 }
206}
207
209{
210 const bool inputPanelVisible = (visible == 1);
211 if (m_inputPanelVisible != inputPanelVisible) {
212 m_inputPanelVisible = inputPanelVisible;
213 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputPanelVisibleChanged();
214 }
215}
216
218{
219 m_serial = serial;
220
221 if (m_resetCallback) {
222 qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
223 m_builder.reset();
224 return;
225 }
226
228 return;
229
230 QInputMethodEvent *event = m_builder.buildPreedit(text);
231
232 m_builder.reset();
233 m_preeditCommit = commit;
234
236 delete event;
237}
238
240{
241 m_builder.addPreeditStyling(index, length, style);
242}
243
248
250{
251 m_serial = serial;
252
253 if (m_resetCallback) {
254 qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
255 m_builder.reset();
256 return;
257 }
258
260 return;
261
262 // When committing the text, the preeditString needs to be reset, to prevent it to be
263 // send again in the commit() function
264 m_preeditCommit.clear();
265
266 QInputMethodEvent *event = m_builder.buildCommit(text);
267
268 m_builder.reset();
269
271 delete event;
272}
273
275{
276 m_builder.setCursorPosition(index, anchor);
277}
278
279void QWaylandTextInputv1::zwp_text_input_v1_delete_surrounding_text(int32_t before_length, uint32_t after_length)
280{
281 //before_length is negative, but the builder expects it to be positive
282 m_builder.setDeleteSurroundingText(-before_length, after_length);
283}
284
285void QWaylandTextInputv1::zwp_text_input_v1_keysym(uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
286{
287 m_serial = serial;
288
289#if QT_CONFIG(xkbcommon)
290 if (m_resetCallback) {
291 qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
292 return;
293 }
294
296 return;
297
298 Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
299
300 QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
302 int qtkey = QXkbCommon::keysymToQtKey(sym, qtModifiers);
303
305 time, type, qtkey, qtModifiers, text);
306#else
307 Q_UNUSED(time);
308 Q_UNUSED(sym);
311#endif
312}
313
315{
316 m_serial = serial;
317
318 if (m_resetCallback) {
319 qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
320 return;
321 }
322
323 const QLocale locale(language);
324 if (m_locale != locale) {
325 m_locale = locale;
326 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitLocaleChanged();
327 }
328}
329
331{
332 m_serial = serial;
333
334 if (m_resetCallback) {
335 qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
336 return;
337 }
338
339 const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
340 (direction == text_direction_ltr) ? Qt::LeftToRight :
341 (direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
342 if (m_inputDirection != inputDirection) {
343 m_inputDirection = inputDirection;
344 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputDirectionChanged(m_inputDirection);
345 }
346}
347
348Qt::KeyboardModifiers QWaylandTextInputv1::modifiersToQtModifiers(uint32_t modifiers)
349{
350 Qt::KeyboardModifiers ret = Qt::NoModifier;
351 for (int i = 0; i < m_modifiersMap.size(); ++i) {
352 if (modifiers & (1 << i)) {
353 ret |= m_modifiersMap[i];
354 }
355 }
356 return ret;
357}
358
359}
360
362
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
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:625
The QInputMethodQueryEvent class provides an event sent by the input context to input objects.
Definition qevent.h:679
qsizetype size() const noexcept
Definition qlist.h:397
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
size_type size() const
Definition qmap.h:267
\inmodule QtCore
Definition qmargins.h:24
constexpr int left() const noexcept
Returns the left margin.
Definition qmargins.h:106
constexpr int top() const noexcept
Returns the top margin.
Definition qmargins.h:109
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
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:261
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
QByteArray toUtf8() const &
Definition qstring.h:634
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
T toNativePixels(const T &value, const C *context)
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
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLbitfield flags
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)