summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wayland/qwaylandtextinputv3.cpp
blob: 42ee5cde2ff6a3537393d188688496a959e91a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qwaylandtextinputv3_p.h"

#include "qwaylandwindow_p.h"
#include "qwaylandinputmethodeventbuilder_p.h"

#include <QtCore/qloggingcategory.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/private/qhighdpiscaling_p.h>
#include <QtGui/qevent.h>
#include <QtGui/qwindow.h>
#include <QTextCharFormat>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcQpaWaylandTextInput, "qt.qpa.wayland.textinput")

namespace QtWaylandClient {

QWaylandTextInputv3::QWaylandTextInputv3(QWaylandDisplay *display,
                                         struct ::zwp_text_input_v3 *text_input)
    : QtWayland::zwp_text_input_v3(text_input)
{
    Q_UNUSED(display)
}

QWaylandTextInputv3::~QWaylandTextInputv3()
{
    destroy();
}

namespace {
const Qt::InputMethodQueries supportedQueries3 = Qt::ImEnabled |
                                                Qt::ImSurroundingText |
                                                Qt::ImCursorPosition |
                                                Qt::ImAnchorPosition |
                                                Qt::ImHints |
                                                Qt::ImCursorRectangle;
}

void QWaylandTextInputv3::enableSurface(::wl_surface *surface)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << surface;

    if (m_surface == surface)
        return; // already enabled
    if (m_surface)
        qCWarning(qLcQpaWaylandTextInput()) << Q_FUNC_INFO << "Try to enable surface" << surface << "with focusing surface" << m_surface;

    m_surface = surface;
    m_pendingPreeditString.clear();
    m_pendingCommitString.clear();
    m_pendingDeleteBeforeText = 0;
    m_pendingDeleteAfterText = 0;
    m_surroundingText.clear();
    m_cursor = 0;
    m_cursorPos = 0;
    m_anchorPos = 0;
    m_contentHint = 0;
    m_contentPurpose = 0;
    m_cursorRect = QRect();

    enable();
    updateState(supportedQueries3, update_state_enter);
}

void QWaylandTextInputv3::disableSurface(::wl_surface *surface)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << surface;

    if (!m_surface)
        return; // already disabled
    if (m_surface != surface)
        qCWarning(qLcQpaWaylandTextInput()) << Q_FUNC_INFO << "Try to disable surface" << surface << "with focusing surface" << m_surface;

    m_currentPreeditString.clear();
    m_surface = nullptr;
    disable();
    commit();
}

void QWaylandTextInputv3::zwp_text_input_v3_enter(struct ::wl_surface *surface)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << m_surface << surface;

    if (m_surface)
        qCWarning(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "Got enter event without leaving a surface " << m_surface;

    enableSurface(surface);
}

void QWaylandTextInputv3::zwp_text_input_v3_leave(struct ::wl_surface *surface)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;

    if (!m_surface)
        return; // Nothing to leave

    if (m_surface != surface)
        qCWarning(qLcQpaWaylandTextInput()) << Q_FUNC_INFO << "Got leave event for surface" << surface << "with focusing surface" << m_surface;

    disableSurface(surface);
}

void QWaylandTextInputv3::zwp_text_input_v3_preedit_string(const QString &text, int32_t cursorBegin, int32_t cursorEnd)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << text << cursorBegin << cursorEnd;
    if (!m_surface) {
        qCWarning(qLcQpaWaylandTextInput) << "Got preedit_string event without entering a surface";
        return;
    }

    if (!QGuiApplication::focusObject())
        return;

    m_pendingPreeditString.text = text;
    m_pendingPreeditString.cursorBegin = QWaylandInputMethodEventBuilder::indexFromWayland(text, cursorBegin);
    m_pendingPreeditString.cursorEnd = QWaylandInputMethodEventBuilder::indexFromWayland(text, cursorEnd);
}

void QWaylandTextInputv3::zwp_text_input_v3_commit_string(const QString &text)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << text;
    if (!m_surface) {
        qCWarning(qLcQpaWaylandTextInput) << "Got commit_string event without entering a surface";
        return;
    }

    if (!QGuiApplication::focusObject())
        return;

    m_pendingCommitString = text;
}

void QWaylandTextInputv3::zwp_text_input_v3_delete_surrounding_text(uint32_t beforeText, uint32_t afterText)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << beforeText << afterText;
    if (!m_surface) {
        qCWarning(qLcQpaWaylandTextInput) << "Got delete_surrounding_text event without entering a surface";
        return;
    }

    if (!QGuiApplication::focusObject())
        return;

    m_pendingDeleteBeforeText = beforeText;
    m_pendingDeleteAfterText = afterText;
}

void QWaylandTextInputv3::zwp_text_input_v3_done(uint32_t serial)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "with serial" << serial << m_currentSerial;

    if (!m_surface)
        return;

    // This is a case of double click.
    // text_input_v3 will ignore this done signal and just keep the selection of the clicked word.
    if (m_cursorPos != m_anchorPos && (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0)) {
        qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "Ignore done";
        m_pendingDeleteBeforeText = 0;
        m_pendingDeleteAfterText = 0;
        m_pendingPreeditString.clear();
        m_pendingCommitString.clear();
        return;
    }

    QObject *focusObject = QGuiApplication::focusObject();
    if (!focusObject)
        return;

    if (!m_surface) {
        qCWarning(qLcQpaWaylandTextInput) << Q_FUNC_INFO << serial << "Surface is not enabled yet";
        return;
    }

    if ((m_pendingPreeditString == m_currentPreeditString)
           && (m_pendingCommitString.isEmpty() && m_pendingDeleteBeforeText == 0
                                               && m_pendingDeleteAfterText == 0)) {
        // Current done doesn't need additional updates
        m_pendingPreeditString.clear();
        return;
    }

    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "PREEDIT" << m_pendingPreeditString.text << m_pendingPreeditString.cursorBegin;

    QList<QInputMethodEvent::Attribute> attributes;
    {
        if (m_pendingPreeditString.cursorBegin != -1 ||
                m_pendingPreeditString.cursorEnd != -1) {
            // Current supported cursor shape is just line.
            // It means, cursorEnd and cursorBegin are the same.
            QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
                                                    m_pendingPreeditString.cursorBegin,
                                                    1);
            attributes.append(attribute1);
        }

        // only use single underline style for now
        QTextCharFormat format;
        format.setFontUnderline(true);
        format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
        QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
                                                0,
                                                m_pendingPreeditString.text.length(), format);
        attributes.append(attribute2);
    }
    QInputMethodEvent event(m_pendingPreeditString.text, attributes);

    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "DELETE" << m_pendingDeleteBeforeText << m_pendingDeleteAfterText;
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "COMMIT" << m_pendingCommitString;

    int replaceFrom = 0;
    int replaceLength = 0;
    if (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0) {
        // A workaround for reselection
        // It will disable redundant commit after reselection
        m_condReselection = true;
        const QByteArray &utf8 = QStringView{m_surroundingText}.toUtf8();
        if (m_cursorPos < int(m_pendingDeleteBeforeText)) {
            replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText)).size();
            replaceLength = QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
        } else {
            replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText)).size();
            replaceLength = QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
        }
    }

    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "DELETE from " << replaceFrom << " length " << replaceLength;
    event.setCommitString(m_pendingCommitString,
                          replaceFrom,
                          replaceLength);
    m_currentPreeditString = m_pendingPreeditString;
    m_pendingPreeditString.clear();
    m_pendingCommitString.clear();
    m_pendingDeleteBeforeText = 0;
    m_pendingDeleteAfterText = 0;
    QCoreApplication::sendEvent(focusObject, &event);

    if (serial == m_currentSerial)
        updateState(supportedQueries3, update_state_full);
}

void QWaylandTextInputv3::reset()
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;

    m_pendingPreeditString.clear();
}

void QWaylandTextInputv3::commit()
{
    m_currentSerial = (m_currentSerial < UINT_MAX) ? m_currentSerial + 1U: 0U;

    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << "with serial" << m_currentSerial;
    QtWayland::zwp_text_input_v3::commit();
}

void QWaylandTextInputv3::updateState(Qt::InputMethodQueries queries, uint32_t flags)
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << queries << 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 &= supportedQueries3;
    bool needsCommit = false;

    QInputMethodQueryEvent event(queries);
    QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);

    // For some reason, a query for Qt::ImSurroundingText gives an empty string even though it is not.
    if (!(queries & Qt::ImSurroundingText) && event.value(Qt::ImSurroundingText).toString().isEmpty()) {
        return;
    }

    if (queries & Qt::ImCursorRectangle) {
        const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
        const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
        const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
        const QMargins margins = window->clientSideMargins();
        const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
        if (surfaceRect != m_cursorRect) {
            set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
            m_cursorRect = surfaceRect;
            needsCommit = true;
        }
    }

    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();

        qCDebug(qLcQpaWaylandTextInput) << "Original surrounding_text from InputMethodQuery: " << text << cursor << anchor;

        // Make sure text is not too big
        // surround_text cannot exceed 4000byte in wayland protocol
        // The worst case will be supposed here.
        const int MAX_MESSAGE_SIZE = 4000;

        const QByteArray utf8 = text.toUtf8();
        const int textSize = utf8.size();
        if (textSize > MAX_MESSAGE_SIZE) {
            qCDebug(qLcQpaWaylandTextInput) << "SurroundText size is over "
                                            << MAX_MESSAGE_SIZE
                                            << " byte, some text will be clipped.";
            const int selectionStart = qMin(cursor, anchor);
            const int selectionEnd = qMax(cursor, anchor);
            const int selectionLength = selectionEnd - selectionStart;
            QByteArray selection = QStringView{text}.sliced(selectionStart, selectionLength).toUtf8();
            const int selectionSize = selection.size();
            // If selection is bigger than 4000 byte, it is fixed to 4000 byte.
            // anchor will be moved in the 4000 byte boundary.
            if (selectionSize > MAX_MESSAGE_SIZE) {
                if (anchor > cursor) {
                    cursor = 0;
                    anchor = MAX_MESSAGE_SIZE;
                    text = QString::fromUtf8(QByteArrayView{selection}.sliced(0, MAX_MESSAGE_SIZE));
                } else {
                    anchor = 0;
                    cursor = MAX_MESSAGE_SIZE;
                    text = QString::fromUtf8(QByteArrayView{selection}.sliced(selectionSize - MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE));
                }
            } else {
                // This is not optimal in some cases.
                // For examples, if the cursor position and
                // the selectionEnd are close to the end of the surround text,
                // the tail of the text might always be clipped.
                // However all the cases of over 4000 byte are just exceptions.
                int selEndSize = QStringView{text}.first(selectionEnd).toUtf8().size();
                cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
                anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
                if (selEndSize < MAX_MESSAGE_SIZE) {
                    text = QString::fromUtf8(QByteArrayView{utf8}.first(MAX_MESSAGE_SIZE));
                } else {
                    const int startOffset = selEndSize - MAX_MESSAGE_SIZE;
                    text = QString::fromUtf8(QByteArrayView{utf8}.sliced(startOffset, MAX_MESSAGE_SIZE));
                    cursor -= startOffset;
                    anchor -= startOffset;
                }
            }
        } else {
            cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
            anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
        }
        qCDebug(qLcQpaWaylandTextInput) << "Modified surrounding_text: " << text << cursor << anchor;

        if (m_surroundingText != text || m_cursorPos != cursor || m_anchorPos != anchor) {
            qCDebug(qLcQpaWaylandTextInput) << "Current surrounding_text: " << m_surroundingText << m_cursorPos << m_anchorPos;
            qCDebug(qLcQpaWaylandTextInput) << "New surrounding_text: " << text << cursor << anchor;

            set_surrounding_text(text, cursor, anchor);

            // A workaround in the case of reselection
            // It will work when re-clicking a preedit text
            if (m_condReselection) {
                qCDebug(qLcQpaWaylandTextInput) << "\"commit\" is disabled when Reselection by changing focus";
                m_condReselection = false;
                needsCommit = false;

            }

            m_surroundingText = text;
            m_cursorPos = cursor;
            m_anchorPos = anchor;
            m_cursor = cursor;
        }
    }

    if (queries & Qt::ImHints) {
        QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convertV3(static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
        qCDebug(qLcQpaWaylandTextInput) << m_contentHint << contentType.hint;
        qCDebug(qLcQpaWaylandTextInput) << m_contentPurpose << contentType.purpose;

        if (m_contentHint != contentType.hint || m_contentPurpose != contentType.purpose) {
            qCDebug(qLcQpaWaylandTextInput) << "set_content_type: " << contentType.hint << contentType.purpose;
            set_content_type(contentType.hint, contentType.purpose);

            m_contentHint = contentType.hint;
            m_contentPurpose = contentType.purpose;
            needsCommit = true;
        }
    }

    if (needsCommit)
        commit();
}

void QWaylandTextInputv3::setCursorInsidePreedit(int cursor)
{
    Q_UNUSED(cursor);
}

bool QWaylandTextInputv3::isInputPanelVisible() const
{
    return false;
}

QRectF QWaylandTextInputv3::keyboardRect() const
{
    qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO;
    return m_cursorRect;
}

QLocale QWaylandTextInputv3::locale() const
{
    return QLocale();
}

Qt::LayoutDirection QWaylandTextInputv3::inputDirection() const
{
    return Qt::LeftToRight;
}

}

QT_END_NAMESPACE