summaryrefslogtreecommitdiff
path: root/src/client/qwaylandinputmethodcontext.cpp
blob: 85cd427146bd0a34cf90b472cbeb82b07e7c8800 (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
// Copyright (C) 2020 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 "qwaylandinputmethodcontext_p.h"
#include "qwaylanddisplay_p.h"
#include "qwaylandinputdevice_p.h"

#include <QtGui/qguiapplication.h>
#include <QtGui/qtextformat.h>
#include <QtGui/private/qguiapplication_p.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)

namespace QtWaylandClient {

static constexpr int maxStringSize = 1000; // actual max is 4096/3

QWaylandTextInputMethod::QWaylandTextInputMethod(QWaylandDisplay *display, struct ::qt_text_input_method_v1 *textInputMethod)
    : QtWayland::qt_text_input_method_v1(textInputMethod)
{
    Q_UNUSED(display);
}

QWaylandTextInputMethod::~QWaylandTextInputMethod()
{
}

void QWaylandTextInputMethod::text_input_method_v1_visible_changed(int32_t visible)
{
    if (m_isVisible != visible) {
        m_isVisible = visible;
        QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputPanelVisibleChanged();
    }
}

void QWaylandTextInputMethod::text_input_method_v1_locale_changed(const QString &localeName)
{
    m_locale = QLocale(localeName);
}

void QWaylandTextInputMethod::text_input_method_v1_input_direction_changed(int32_t inputDirection)
{
    m_layoutDirection = Qt::LayoutDirection(inputDirection);
}

void QWaylandTextInputMethod::text_input_method_v1_keyboard_rectangle_changed(wl_fixed_t x, wl_fixed_t y, wl_fixed_t width, wl_fixed_t height)
{
    const QRectF keyboardRectangle(wl_fixed_to_double(x),
                                   wl_fixed_to_double(y),
                                   wl_fixed_to_double(width),
                                   wl_fixed_to_double(height));
    if (m_keyboardRect != keyboardRectangle) {
        m_keyboardRect = keyboardRectangle;
        QGuiApplicationPrivate::platformIntegration()->inputContext()->emitKeyboardRectChanged();
    }
}

void QWaylandTextInputMethod::text_input_method_v1_start_input_method_event(uint32_t serial, int32_t surrounding_text_offset)
{
    if (m_pendingInputMethodEvents.contains(serial)) {
        qCWarning(qLcQpaInputMethods) << "Input method event with serial" << serial << "already started";
        return;
    }

    m_pendingInputMethodEvents[serial] = QList<QInputMethodEvent::Attribute>{};
    m_offsetFromCompositor[serial] = surrounding_text_offset;
}

// We need to keep surrounding text below maxStringSize characters, with cursorPos centered in that substring

static int calculateOffset(const QString &text, int cursorPos)
{
    int size = text.size();
    int halfSize = maxStringSize/2;
    if (size <= maxStringSize || cursorPos < halfSize)
        return 0;
    if (cursorPos > size - halfSize)
        return size - maxStringSize;
    return  cursorPos - halfSize;
}

static QString mapSurroundingTextToCompositor(const QString &s, int offset)
{
    return s.mid(offset, maxStringSize);
}

static int mapPositionToCompositor(int pos, int offset)
{
    return pos - offset;
}

static int mapPositionFromCompositor(int pos, int offset)
{
    return pos + offset;
}

void QWaylandTextInputMethod::text_input_method_v1_input_method_event_attribute(uint32_t serial, int32_t type, int32_t start, int32_t length, const QString &value)
{
    if (!m_pendingInputMethodEvents.contains(serial)) {
        qCWarning(qLcQpaInputMethods) << "Input method event with serial" << serial << "does not exist";
        return;
    }

    int startMapped = mapPositionFromCompositor(start, m_offsetFromCompositor[serial]);
    QList<QInputMethodEvent::Attribute> &attributes = m_pendingInputMethodEvents[serial];
    switch (type) {
    case QInputMethodEvent::Selection:
        attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::AttributeType(type), startMapped, length));
        break;
    case QInputMethodEvent::Cursor:
        attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::AttributeType(type), start, length, QColor::fromString(value)));
        break;
    case QInputMethodEvent::TextFormat:
    {
        QTextCharFormat textFormat;
        textFormat.setProperty(QTextFormat::FontUnderline, true);
        textFormat.setProperty(QTextFormat::TextUnderlineStyle, QTextCharFormat::SingleUnderline);
        attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::AttributeType(type), start, length, textFormat));
        break;
    }
    case QInputMethodEvent::Language:
    case QInputMethodEvent::Ruby:
        attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::AttributeType(type), start, length, value));
        break;
    };
}

void QWaylandTextInputMethod::sendInputState(QInputMethodQueryEvent *event, Qt::InputMethodQueries queries)
{
    int cursorPosition = event->value(Qt::ImCursorPosition).toInt();
    int anchorPosition = event->value(Qt::ImAnchorPosition).toInt();
    QString surroundingText = event->value(Qt::ImSurroundingText).toString();
    int offset = calculateOffset(surroundingText, cursorPosition);

    if (queries & Qt::ImCursorPosition)
        update_cursor_position(mapPositionToCompositor(cursorPosition, offset));
    if (queries & Qt::ImSurroundingText)
        update_surrounding_text(mapSurroundingTextToCompositor(surroundingText, offset), offset);
    if (queries & Qt::ImAnchorPosition)
        update_anchor_position(mapPositionToCompositor(anchorPosition, offset));
    if (queries & Qt::ImAbsolutePosition)
        update_absolute_position(event->value(Qt::ImAbsolutePosition).toInt()); // do not map: this is the position in the whole document
}


void QWaylandTextInputMethod::text_input_method_v1_end_input_method_event(uint32_t serial, const QString &commitString, const QString &preeditString, int32_t replacementStart, int32_t replacementLength)
{
    if (!m_pendingInputMethodEvents.contains(serial)) {
        qCWarning(qLcQpaInputMethods) << "Input method event with serial" << serial << "does not exist";
        return;
    }

    QList<QInputMethodEvent::Attribute> attributes = m_pendingInputMethodEvents.take(serial);
    m_offsetFromCompositor.remove(serial);
    if (QGuiApplication::focusObject() != nullptr) {
        QInputMethodEvent event(preeditString, attributes);
        event.setCommitString(commitString, replacementStart, replacementLength);
        QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
    }

    // Send current state to make sure it matches
    if (QGuiApplication::focusObject() != nullptr) {
        QInputMethodQueryEvent event(Qt::ImCursorPosition | Qt::ImSurroundingText | Qt::ImAnchorPosition | Qt::ImAbsolutePosition);
        QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
        sendInputState(&event);
    }

    acknowledge_input_method();
}

void QWaylandTextInputMethod::text_input_method_v1_key(int32_t type,
                                                    int32_t key,
                                                    int32_t modifiers,
                                                    int32_t autoRepeat,
                                                    int32_t count,
                                                    int32_t nativeScanCode,
                                                    int32_t nativeVirtualKey,
                                                    int32_t nativeModifiers,
                                                    const QString &text)
{
    if (QGuiApplication::focusObject() != nullptr) {
        QKeyEvent event(QKeyEvent::Type(type),
                        key,
                        Qt::KeyboardModifiers(modifiers),
                        nativeScanCode,
                        nativeVirtualKey,
                        nativeModifiers,
                        text,
                        autoRepeat,
                        count);
        QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
    }
}

void QWaylandTextInputMethod::text_input_method_v1_enter(struct ::wl_surface *surface)
{
    m_surface = surface;
}

void QWaylandTextInputMethod::text_input_method_v1_leave(struct ::wl_surface *surface)
{
    if (surface != m_surface) {
        qCWarning(qLcQpaInputMethods) << "Got leave event for surface without corresponding enter";
    } else {
        m_surface = nullptr;
    }
}

QWaylandInputMethodContext::QWaylandInputMethodContext(QWaylandDisplay *display)
    : m_display(display)
{
}

QWaylandInputMethodContext::~QWaylandInputMethodContext()
{
}

bool QWaylandInputMethodContext::isValid() const
{
    return m_display->textInputMethodManager() != nullptr;
}

void QWaylandInputMethodContext::reset()
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        inputMethod->reset();
}

void QWaylandInputMethodContext::commit()
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        inputMethod->commit();

    m_display->forceRoundTrip();
}

void QWaylandInputMethodContext::update(Qt::InputMethodQueries queries)
{
    wl_surface *currentSurface = m_currentWindow != nullptr && m_currentWindow->handle() != nullptr
                ? static_cast<QWaylandWindow *>(m_currentWindow->handle())->wlSurface()
                : nullptr;
    if (currentSurface != nullptr && !inputMethodAccepted()) {
        textInputMethod()->disable(currentSurface);
        m_currentWindow.clear();
    } else if (currentSurface == nullptr && inputMethodAccepted()) {
        QWindow *window = QGuiApplication::focusWindow();
        currentSurface = window != nullptr && window->handle() != nullptr
                ? static_cast<QWaylandWindow *>(window->handle())->wlSurface()
                : nullptr;
        if (currentSurface != nullptr) {
            textInputMethod()->disable(currentSurface);
            m_currentWindow = window;
        }
    }

    queries &= (Qt::ImEnabled
                | Qt::ImHints
                | Qt::ImCursorRectangle
                | Qt::ImCursorPosition
                | Qt::ImSurroundingText
                | Qt::ImCurrentSelection
                | Qt::ImAnchorPosition
                | Qt::ImTextAfterCursor
                | Qt::ImTextBeforeCursor
                | Qt::ImPreferredLanguage);

    const Qt::InputMethodQueries queriesNeedingOffset = Qt::ImCursorPosition | Qt::ImSurroundingText | Qt::ImAnchorPosition;
    if (queries & queriesNeedingOffset)
        queries |= queriesNeedingOffset;

    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr && QGuiApplication::focusObject() != nullptr) {
        QInputMethodQueryEvent event(queries);
        QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);

        inputMethod->start_update(int(queries));

        if (queries & Qt::ImHints)
            inputMethod->update_hints(event.value(Qt::ImHints).toInt());

        if (queries & Qt::ImCursorRectangle) {
            QRect rect = event.value(Qt::ImCursorRectangle).toRect();
            inputMethod->update_cursor_rectangle(rect.x(), rect.y(), rect.width(), rect.height());
        }

        inputMethod->sendInputState(&event, queries);

        if (queries & Qt::ImPreferredLanguage)
            inputMethod->update_preferred_language(event.value(Qt::ImPreferredLanguage).toString());

        inputMethod->end_update();

        // ### Should we do a display sync here and ignore all events until it is received?
    }
}

void QWaylandInputMethodContext::invokeAction(QInputMethod::Action action, int cursorPosition)
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        inputMethod->invoke_action(int(action), cursorPosition);
}

void QWaylandInputMethodContext::showInputPanel()
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        inputMethod->show_input_panel();
}

void QWaylandInputMethodContext::hideInputPanel()
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        inputMethod->hide_input_panel();
}

bool QWaylandInputMethodContext::isInputPanelVisible() const
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        return inputMethod->isVisible();
    else
        return false;
}

QRectF QWaylandInputMethodContext::keyboardRect() const
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        return inputMethod->keyboardRect();
    else
        return QRectF();
}

QLocale QWaylandInputMethodContext::locale() const
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        return inputMethod->locale();
    else
        return QLocale();
}

Qt::LayoutDirection QWaylandInputMethodContext::inputDirection() const
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod != nullptr)
        return inputMethod->inputDirection();
    else
        return Qt::LeftToRight;
}

void QWaylandInputMethodContext::setFocusObject(QObject *)
{
    QWaylandTextInputMethod *inputMethod = textInputMethod();
    if (inputMethod == nullptr)
        return;

    QWindow *window = QGuiApplication::focusWindow();

    if (m_currentWindow != nullptr && m_currentWindow->handle() != nullptr) {
        if (m_currentWindow.data() != window || !inputMethodAccepted()) {
            auto *surface = static_cast<QWaylandWindow *>(m_currentWindow->handle())->wlSurface();
            if (surface)
                inputMethod->disable(surface);
            m_currentWindow.clear();
        }
    }

    if (window != nullptr && window->handle() != nullptr && inputMethodAccepted()) {
        if (m_currentWindow.data() != window) {
            auto *surface = static_cast<QWaylandWindow *>(window->handle())->wlSurface();
            if (surface != nullptr) {
                inputMethod->enable(surface);
                m_currentWindow = window;
            }
        }

        update(Qt::ImQueryAll);
    }
}

QWaylandTextInputMethod *QWaylandInputMethodContext::textInputMethod() const
{
    return m_display->defaultInputDevice()->textInputMethod();
}

} // QtWaylandClient

QT_END_NAMESPACE

#include "moc_qwaylandinputmethodcontext_p.cpp"