Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwaylandtextinputv3.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
5
7#include "qwaylandinputmethodeventbuilder_p.h"
8
9#include <QtCore/qloggingcategory.h>
10#include <QtGui/qguiapplication.h>
11#include <QtGui/private/qhighdpiscaling_p.h>
12#include <QtGui/qevent.h>
13#include <QtGui/qwindow.h>
14#include <QTextCharFormat>
15
17
18Q_LOGGING_CATEGORY(qLcQpaWaylandTextInput, "qt.qpa.wayland.textinput")
19
20namespace QtWaylandClient {
21
22QWaylandTextInputv3::QWaylandTextInputv3(QWaylandDisplay *display,
23 struct ::zwp_text_input_v3 *text_input)
24 : QtWayland::zwp_text_input_v3(text_input)
25{
26 Q_UNUSED(display)
27}
28
30{
31 destroy();
32}
33
34namespace {
35const Qt::InputMethodQueries supportedQueries3 = Qt::ImEnabled |
36 Qt::ImSurroundingText |
37 Qt::ImCursorPosition |
38 Qt::ImAnchorPosition |
39 Qt::ImHints |
40 Qt::ImCursorRectangle;
41}
42
44{
45}
46
48{
49}
50
51void QWaylandTextInputv3::zwp_text_input_v3_enter(struct ::wl_surface *surface)
52{
53 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "Trying to enable surface" << surface << "with focusing surface" << m_surface;
54
55 if (m_surface == surface)
56 return; // already enabled
57
58 m_surface = surface;
59 m_pendingPreeditString.clear();
60 m_pendingCommitString.clear();
61 m_pendingDeleteBeforeText = 0;
62 m_pendingDeleteAfterText = 0;
63 m_surroundingText.clear();
64 m_cursor = 0;
65 m_cursorPos = 0;
66 m_anchorPos = 0;
67 m_contentHint = 0;
68 m_contentPurpose = 0;
69 m_cursorRect = QRect();
70
71 enable();
72 updateState(supportedQueries3, update_state_enter);
73}
74
75void QWaylandTextInputv3::zwp_text_input_v3_leave(struct ::wl_surface *surface)
76{
77 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;
78
79 if (!m_surface)
80 return; // Nothing to leave
81
82 if (m_surface != surface)
83 qCWarning(qLcQpaWaylandTextInput()) << Q_FUNC_INFO << "Got leave event for surface" << surface << "with focusing surface" << m_surface;
84
85 m_currentPreeditString.clear();
86 m_surface = nullptr;
87 disable();
89}
90
91void QWaylandTextInputv3::zwp_text_input_v3_preedit_string(const QString &text, int32_t cursorBegin, int32_t cursorEnd)
92{
93 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << text << cursorBegin << cursorEnd;
94 if (!m_surface) {
95 qCWarning(qLcQpaWaylandTextInput) << "Got preedit_string event without entering a surface";
96 return;
97 }
98
99 if (!QGuiApplication::focusObject())
100 return;
101
102 m_pendingPreeditString.text = text;
103 m_pendingPreeditString.cursorBegin = QWaylandInputMethodEventBuilder::indexFromWayland(text, cursorBegin);
104 m_pendingPreeditString.cursorEnd = QWaylandInputMethodEventBuilder::indexFromWayland(text, cursorEnd);
105}
106
108{
109 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << text;
110 if (!m_surface) {
111 qCWarning(qLcQpaWaylandTextInput) << "Got commit_string event without entering a surface";
112 return;
113 }
114
115 if (!QGuiApplication::focusObject())
116 return;
117
118 m_pendingCommitString = text;
119}
120
121void QWaylandTextInputv3::zwp_text_input_v3_delete_surrounding_text(uint32_t beforeText, uint32_t afterText)
122{
123 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << beforeText << afterText;
124 if (!m_surface) {
125 qCWarning(qLcQpaWaylandTextInput) << "Got delete_surrounding_text event without entering a surface";
126 return;
127 }
128
129 if (!QGuiApplication::focusObject())
130 return;
131
132 m_pendingDeleteBeforeText = beforeText;
133 m_pendingDeleteAfterText = afterText;
134}
135
137{
138 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "with serial" << serial << m_currentSerial;
139
140 if (!m_surface)
141 return;
142
143 // This is a case of double click.
144 // text_input_v3 will ignore this done signal and just keep the selection of the clicked word.
145 if (m_cursorPos != m_anchorPos && (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0)) {
146 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "Ignore done";
147 m_pendingDeleteBeforeText = 0;
148 m_pendingDeleteAfterText = 0;
149 m_pendingPreeditString.clear();
150 m_pendingCommitString.clear();
151 return;
152 }
153
154 QObject *focusObject = QGuiApplication::focusObject();
155 if (!focusObject)
156 return;
157
158 if (!m_surface) {
159 qCWarning(qLcQpaWaylandTextInput) << Q_FUNC_INFO << serial << "Surface is not enabled yet";
160 return;
161 }
162
163 if ((m_pendingPreeditString == m_currentPreeditString)
164 && (m_pendingCommitString.isEmpty() && m_pendingDeleteBeforeText == 0
165 && m_pendingDeleteAfterText == 0)) {
166 // Current done doesn't need additional updates
167 m_pendingPreeditString.clear();
168 return;
169 }
170
171 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "PREEDIT" << m_pendingPreeditString.text << m_pendingPreeditString.cursorBegin;
172
173 QList<QInputMethodEvent::Attribute> attributes;
174 {
175 if (m_pendingPreeditString.cursorBegin != -1 ||
176 m_pendingPreeditString.cursorEnd != -1) {
177 // Current supported cursor shape is just line.
178 // It means, cursorEnd and cursorBegin are the same.
179 QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
180 m_pendingPreeditString.cursorBegin,
181 1);
182 attributes.append(attribute1);
183 }
184
185 // only use single underline style for now
186 QTextCharFormat format;
187 format.setFontUnderline(true);
188 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
189 QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
190 0,
191 m_pendingPreeditString.text.length(), format);
192 attributes.append(attribute2);
193 }
194 QInputMethodEvent event(m_pendingPreeditString.text, attributes);
195
196 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "DELETE" << m_pendingDeleteBeforeText << m_pendingDeleteAfterText;
197 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "COMMIT" << m_pendingCommitString;
198
199 int replaceFrom = 0;
200 int replaceLength = 0;
201 if (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0) {
202 // A workaround for reselection
203 // It will disable redundant commit after reselection
204 m_condReselection = true;
205 const QByteArray &utf8 = QStringView{m_surroundingText}.toUtf8();
206 if (m_cursorPos < int(m_pendingDeleteBeforeText)) {
207 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText)).size();
208 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
209 } else {
210 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText)).size();
211 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
212 }
213 }
214
215 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "DELETE from " << replaceFrom << " length " << replaceLength;
216 event.setCommitString(m_pendingCommitString,
217 replaceFrom,
218 replaceLength);
219 m_currentPreeditString = m_pendingPreeditString;
220 m_pendingPreeditString.clear();
221 m_pendingCommitString.clear();
222 m_pendingDeleteBeforeText = 0;
223 m_pendingDeleteAfterText = 0;
224 QCoreApplication::sendEvent(focusObject, &event);
225
226 if (serial == m_currentSerial)
227 updateState(supportedQueries3, update_state_full);
228}
229
231{
232 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;
233
234 m_pendingPreeditString.clear();
235}
236
238{
239 m_currentSerial = (m_currentSerial < UINT_MAX) ? m_currentSerial + 1U: 0U;
240
241 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "with serial" << m_currentSerial;
242 QtWayland::zwp_text_input_v3::commit();
243}
244
245void QWaylandTextInputv3::updateState(Qt::InputMethodQueries queries, uint32_t flags)
246{
247 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << queries << flags;
248
249 if (!QGuiApplication::focusObject())
250 return;
251
252 if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
253 return;
254
255 auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
256 auto *surface = window->wlSurface();
257 if (!surface || (surface != m_surface))
258 return;
259
260 queries &= supportedQueries3;
261 bool needsCommit = false;
262
263 QInputMethodQueryEvent event(queries);
264 QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
265
266 // For some reason, a query for Qt::ImSurroundingText gives an empty string even though it is not.
267 if (!(queries & Qt::ImSurroundingText) && event.value(Qt::ImSurroundingText).toString().isEmpty()) {
268 return;
269 }
270
271 if (queries & Qt::ImCursorRectangle) {
272 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
273 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
274 const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
275 const QMargins margins = window->clientSideMargins();
276 const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
277 if (surfaceRect != m_cursorRect) {
278 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
279 m_cursorRect = surfaceRect;
280 needsCommit = true;
281 }
282 }
283
284 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
285 QString text = event.value(Qt::ImSurroundingText).toString();
286 int cursor = event.value(Qt::ImCursorPosition).toInt();
287 int anchor = event.value(Qt::ImAnchorPosition).toInt();
288
289 qCDebug(qLcQpaWaylandTextInput) << "Original surrounding_text from InputMethodQuery: " << text << cursor << anchor;
290
291 // Make sure text is not too big
292 // surround_text cannot exceed 4000byte in wayland protocol
293 // The worst case will be supposed here.
294 const int MAX_MESSAGE_SIZE = 4000;
295
296 const QByteArray utf8 = text.toUtf8();
297 const int textSize = utf8.size();
298 if (textSize > MAX_MESSAGE_SIZE) {
299 qCDebug(qLcQpaWaylandTextInput) << "SurroundText size is over "
300 << MAX_MESSAGE_SIZE
301 << " byte, some text will be clipped.";
302 const int selectionStart = qMin(cursor, anchor);
303 const int selectionEnd = qMax(cursor, anchor);
304 const int selectionLength = selectionEnd - selectionStart;
305 QByteArray selection = QStringView{text}.sliced(selectionStart, selectionLength).toUtf8();
306 const int selectionSize = selection.size();
307 // If selection is bigger than 4000 byte, it is fixed to 4000 byte.
308 // anchor will be moved in the 4000 byte boundary.
309 if (selectionSize > MAX_MESSAGE_SIZE) {
310 if (anchor > cursor) {
311 cursor = 0;
312 anchor = MAX_MESSAGE_SIZE;
313 text = QString::fromUtf8(QByteArrayView{selection}.sliced(0, MAX_MESSAGE_SIZE));
314 } else {
315 anchor = 0;
316 cursor = MAX_MESSAGE_SIZE;
317 text = QString::fromUtf8(QByteArrayView{selection}.sliced(selectionSize - MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE));
318 }
319 } else {
320 // This is not optimal in some cases.
321 // For examples, if the cursor position and
322 // the selectionEnd are close to the end of the surround text,
323 // the tail of the text might always be clipped.
324 // However all the cases of over 4000 byte are just exceptions.
325 int selEndSize = QStringView{text}.first(selectionEnd).toUtf8().size();
326 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
327 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
328 if (selEndSize < MAX_MESSAGE_SIZE) {
329 text = QString::fromUtf8(QByteArrayView{utf8}.first(MAX_MESSAGE_SIZE));
330 } else {
331 const int startOffset = selEndSize - MAX_MESSAGE_SIZE;
332 text = QString::fromUtf8(QByteArrayView{utf8}.sliced(startOffset, MAX_MESSAGE_SIZE));
333 cursor -= startOffset;
334 anchor -= startOffset;
335 }
336 }
337 } else {
338 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
339 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
340 }
341 qCDebug(qLcQpaWaylandTextInput) << "Modified surrounding_text: " << text << cursor << anchor;
342
343 if (m_surroundingText != text || m_cursorPos != cursor || m_anchorPos != anchor) {
344 qCDebug(qLcQpaWaylandTextInput) << "Current surrounding_text: " << m_surroundingText << m_cursorPos << m_anchorPos;
345 qCDebug(qLcQpaWaylandTextInput) << "New surrounding_text: " << text << cursor << anchor;
346
347 set_surrounding_text(text, cursor, anchor);
348
349 // A workaround in the case of reselection
350 // It will work when re-clicking a preedit text
351 if (m_condReselection) {
352 qCDebug(qLcQpaWaylandTextInput) << "\"commit\" is disabled when Reselection by changing focus";
353 m_condReselection = false;
354 needsCommit = false;
355
356 }
357
358 m_surroundingText = text;
359 m_cursorPos = cursor;
360 m_anchorPos = anchor;
361 m_cursor = cursor;
362 }
363 }
364
365 if (queries & Qt::ImHints) {
366 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convertV3(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
367 qCDebug(qLcQpaWaylandTextInput) << m_contentHint << contentType.hint;
368 qCDebug(qLcQpaWaylandTextInput) << m_contentPurpose << contentType.purpose;
369
370 if (m_contentHint != contentType.hint || m_contentPurpose != contentType.purpose) {
371 qCDebug(qLcQpaWaylandTextInput) << "set_content_type: " << contentType.hint << contentType.purpose;
372 set_content_type(contentType.hint, contentType.purpose);
373
374 m_contentHint = contentType.hint;
375 m_contentPurpose = contentType.purpose;
376 needsCommit = true;
377 }
378 }
379
380 if (needsCommit)
381 commit();
382}
383
385{
386 Q_UNUSED(cursor);
387}
388
390{
391 return false;
392}
393
395{
396 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;
397 return m_cursorRect;
398}
399
401{
402 return QLocale();
403}
404
406{
407 return Qt::LeftToRight;
408}
409
410}
411
412QT_END_NAMESPACE
void disableSurface(::wl_surface *) override
void zwp_text_input_v3_commit_string(const QString &text) override
Qt::LayoutDirection inputDirection() const override
void updateState(Qt::InputMethodQueries queries, uint32_t flags) override
void zwp_text_input_v3_enter(struct ::wl_surface *surface) override
void zwp_text_input_v3_preedit_string(const QString &text, int32_t cursor_begin, int32_t cursor_end) override
void zwp_text_input_v3_delete_surrounding_text(uint32_t before_length, uint32_t after_length) override
void zwp_text_input_v3_leave(struct ::wl_surface *surface) override
void enableSurface(::wl_surface *) override
void setCursorInsidePreedit(int cursor) override
void zwp_text_input_v3_done(uint32_t serial) override
Combined button and popup list for selecting options.