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
5#include "qwaylandtextinputv1_p.h"
6
7#include "qwaylandwindow_p.h"
8#include "qwaylandinputmethodeventbuilder_p.h"
9
10#include <QtCore/qloggingcategory.h>
11#include <QtGui/QGuiApplication>
12#include <QtGui/private/qguiapplication_p.h>
13#include <QtGui/qpa/qplatformintegration.h>
14#include <QtGui/qevent.h>
15#include <QtGui/qwindow.h>
16#include <QTextCharFormat>
17#include <QList>
18#include <QRectF>
19#include <QLocale>
20
21QT_BEGIN_NAMESPACE
22
23Q_DECLARE_LOGGING_CATEGORY(qLcQpaInputMethods)
24
25namespace QtWaylandClient {
26
27namespace {
28
29const Qt::InputMethodQueries supportedQueries1 = Qt::ImEnabled |
30 Qt::ImSurroundingText |
31 Qt::ImCursorPosition |
32 Qt::ImAnchorPosition |
33 Qt::ImHints |
34 Qt::ImCursorRectangle |
35 Qt::ImPreferredLanguage;
36}
37
38QWaylandTextInputv1::QWaylandTextInputv1(QWaylandDisplay *display, struct ::zwp_text_input_v1 *text_input)
39 : QtWayland::zwp_text_input_v1(text_input)
40{
41 Q_UNUSED(display);
42}
43
44QWaylandTextInputv1::~QWaylandTextInputv1()
45{
46 if (m_resetCallback)
47 wl_callback_destroy(m_resetCallback);
48}
49
50void QWaylandTextInputv1::reset()
51{
52 m_builder.reset();
53 m_preeditCommit = QString();
54 updateState(queries: Qt::ImQueryAll, flags: QWaylandTextInputInterface::update_state_reset);
55}
56
57void QWaylandTextInputv1::commit()
58{
59 if (QObject *o = QGuiApplication::focusObject()) {
60 QInputMethodEvent event;
61 event.setCommitString(commitString: m_preeditCommit);
62 QCoreApplication::sendEvent(receiver: o, event: &event);
63 }
64
65 reset();
66}
67
68const wl_callback_listener QWaylandTextInputv1::callbackListener = {
69 QWaylandTextInputv1::resetCallback
70};
71
72void QWaylandTextInputv1::resetCallback(void *data, wl_callback *, uint32_t)
73{
74 QWaylandTextInputv1 *self = static_cast<QWaylandTextInputv1*>(data);
75
76 if (self->m_resetCallback) {
77 wl_callback_destroy(self->m_resetCallback);
78 self->m_resetCallback = nullptr;
79 }
80}
81
82void QWaylandTextInputv1::updateState(Qt::InputMethodQueries queries, uint32_t flags)
83{
84 if (!QGuiApplication::focusObject())
85 return;
86
87 if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
88 return;
89
90 auto *window = static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
91 auto *surface = window->wlSurface();
92 if (!surface || (surface != m_surface))
93 return;
94
95 queries &= supportedQueries1;
96
97 // Surrounding text, cursor and anchor positions are transferred together
98 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition))
99 queries |= Qt::ImSurroundingText | Qt::ImCursorPosition | Qt::ImAnchorPosition;
100
101 QInputMethodQueryEvent event(queries);
102 QCoreApplication::sendEvent(receiver: QGuiApplication::focusObject(), event: &event);
103
104 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
105 QString text = event.value(query: Qt::ImSurroundingText).toString();
106 int cursor = event.value(query: Qt::ImCursorPosition).toInt();
107 int anchor = event.value(query: Qt::ImAnchorPosition).toInt();
108
109 // Make sure text is not too big
110 if (text.toUtf8().size() > 2048) {
111 int c = qAbs(t: cursor - anchor) <= 512 ? qMin(a: cursor, b: anchor) + qAbs(t: cursor - anchor) / 2: cursor;
112
113 const int offset = c - qBound(min: 0, val: c, max: 512 - qMin(a: text.size() - c, b: 256));
114 text = text.mid(position: offset + c - 256, n: 512);
115 cursor -= offset;
116 anchor -= offset;
117 }
118
119 set_surrounding_text(text, QWaylandInputMethodEventBuilder::indexToWayland(text, length: cursor), QWaylandInputMethodEventBuilder::indexToWayland(text, length: anchor));
120 }
121
122 if (queries & Qt::ImHints) {
123 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convert(hints: static_cast<Qt::InputMethodHints>(event.value(query: Qt::ImHints).toInt()));
124 set_content_type(contentType.hint, contentType.purpose);
125 }
126
127 if (queries & Qt::ImCursorRectangle) {
128 const QRect &cRect = event.value(query: Qt::ImCursorRectangle).toRect();
129 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
130 const QMargins margins = window->frameMargins();
131 const QRect &surfaceRect = windowRect.translated(dx: margins.left(), dy: margins.top());
132 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
133 }
134
135 if (queries & Qt::ImPreferredLanguage) {
136 const QString &language = event.value(query: Qt::ImPreferredLanguage).toString();
137 set_preferred_language(language);
138 }
139
140 if (flags == QWaylandTextInputInterface::update_state_reset)
141 QtWayland::zwp_text_input_v1::reset();
142 else
143 commit_state(m_serial);
144}
145
146void QWaylandTextInputv1::setCursorInsidePreedit(int)
147{
148 // Not supported yet
149}
150
151bool QWaylandTextInputv1::isInputPanelVisible() const
152{
153 return m_inputPanelVisible;
154}
155
156QRectF QWaylandTextInputv1::keyboardRect() const
157{
158 return m_keyboardRectangle;
159}
160
161QLocale QWaylandTextInputv1::locale() const
162{
163 return m_locale;
164}
165
166Qt::LayoutDirection QWaylandTextInputv1::inputDirection() const
167{
168 return m_inputDirection;
169}
170
171void QWaylandTextInputv1::zwp_text_input_v1_enter(::wl_surface *surface)
172{
173 m_surface = surface;
174
175 updateState(queries: Qt::ImQueryAll, flags: QWaylandTextInputInterface::update_state_reset);
176}
177
178void QWaylandTextInputv1::zwp_text_input_v1_leave()
179{
180 m_surface = nullptr;
181}
182
183void QWaylandTextInputv1::zwp_text_input_v1_modifiers_map(wl_array *map)
184{
185 const QList<QByteArray> modifiersMap = QByteArray::fromRawData(data: static_cast<const char*>(map->data), size: map->size).split('\0');
186
187 m_modifiersMap.clear();
188
189 for (const QByteArray &modifier : modifiersMap) {
190 if (modifier == "Shift")
191 m_modifiersMap.append(Qt::ShiftModifier);
192 else if (modifier == "Control")
193 m_modifiersMap.append(Qt::ControlModifier);
194 else if (modifier == "Alt")
195 m_modifiersMap.append(Qt::AltModifier);
196 else if (modifier == "Mod1")
197 m_modifiersMap.append(Qt::AltModifier);
198 else if (modifier == "Mod4")
199 m_modifiersMap.append(Qt::MetaModifier);
200 else
201 m_modifiersMap.append(Qt::NoModifier);
202 }
203}
204
205void QWaylandTextInputv1::zwp_text_input_v1_input_panel_state(uint32_t visible)
206{
207 const bool inputPanelVisible = (visible == 1);
208 if (m_inputPanelVisible != inputPanelVisible) {
209 m_inputPanelVisible = inputPanelVisible;
210 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputPanelVisibleChanged();
211 }
212}
213
214void QWaylandTextInputv1::zwp_text_input_v1_preedit_string(uint32_t serial, const QString &text, const QString &commit)
215{
216 m_serial = serial;
217
218 if (m_resetCallback) {
219 qCDebug(qLcQpaInputMethods()) << "discard preedit_string: reset not confirmed";
220 m_builder.reset();
221 return;
222 }
223
224 if (!QGuiApplication::focusObject())
225 return;
226
227 QInputMethodEvent *event = m_builder.buildPreedit(text);
228
229 m_builder.reset();
230 m_preeditCommit = commit;
231
232 QCoreApplication::sendEvent(receiver: QGuiApplication::focusObject(), event);
233 delete event;
234}
235
236void QWaylandTextInputv1::zwp_text_input_v1_preedit_styling(uint32_t index, uint32_t length, uint32_t style)
237{
238 m_builder.addPreeditStyling(index, length, style);
239}
240
241void QWaylandTextInputv1::zwp_text_input_v1_preedit_cursor(int32_t index)
242{
243 m_builder.setPreeditCursor(index);
244}
245
246void QWaylandTextInputv1::zwp_text_input_v1_commit_string(uint32_t serial, const QString &text)
247{
248 m_serial = serial;
249
250 if (m_resetCallback) {
251 qCDebug(qLcQpaInputMethods()) << "discard commit_string: reset not confirmed";
252 m_builder.reset();
253 return;
254 }
255
256 if (!QGuiApplication::focusObject())
257 return;
258
259 // When committing the text, the preeditString needs to be reset, to prevent it to be
260 // send again in the commit() function
261 m_preeditCommit.clear();
262
263 QInputMethodEvent *event = m_builder.buildCommit(text);
264
265 m_builder.reset();
266
267 QCoreApplication::sendEvent(receiver: QGuiApplication::focusObject(), event);
268 delete event;
269}
270
271void QWaylandTextInputv1::zwp_text_input_v1_cursor_position(int32_t index, int32_t anchor)
272{
273 m_builder.setCursorPosition(index, anchor);
274}
275
276void QWaylandTextInputv1::zwp_text_input_v1_delete_surrounding_text(int32_t before_length, uint32_t after_length)
277{
278 //before_length is negative, but the builder expects it to be positive
279 m_builder.setDeleteSurroundingText(beforeLength: -before_length, afterLength: after_length);
280}
281
282void QWaylandTextInputv1::zwp_text_input_v1_keysym(uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers)
283{
284 m_serial = serial;
285
286#if QT_CONFIG(xkbcommon)
287 if (m_resetCallback) {
288 qCDebug(qLcQpaInputMethods()) << "discard keysym: reset not confirmed";
289 return;
290 }
291
292 if (!QGuiApplication::focusWindow())
293 return;
294
295 Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers);
296
297 QEvent::Type type = state == WL_KEYBOARD_KEY_STATE_PRESSED ? QEvent::KeyPress : QEvent::KeyRelease;
298 QString text = QXkbCommon::lookupStringNoKeysymTransformations(keysym: sym);
299 int qtkey = QXkbCommon::keysymToQtKey(keysym: sym, modifiers: qtModifiers);
300
301 QWindowSystemInterface::handleKeyEvent(window: QGuiApplication::focusWindow(),
302 timestamp: time, t: type, k: qtkey, mods: qtModifiers, text);
303#else
304 Q_UNUSED(time);
305 Q_UNUSED(sym);
306 Q_UNUSED(state);
307 Q_UNUSED(modifiers);
308#endif
309}
310
311void QWaylandTextInputv1::zwp_text_input_v1_language(uint32_t serial, const QString &language)
312{
313 m_serial = serial;
314
315 if (m_resetCallback) {
316 qCDebug(qLcQpaInputMethods()) << "discard language: reset not confirmed";
317 return;
318 }
319
320 const QLocale locale(language);
321 if (m_locale != locale) {
322 m_locale = locale;
323 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitLocaleChanged();
324 }
325}
326
327void QWaylandTextInputv1::zwp_text_input_v1_text_direction(uint32_t serial, uint32_t direction)
328{
329 m_serial = serial;
330
331 if (m_resetCallback) {
332 qCDebug(qLcQpaInputMethods()) << "discard text_direction: reset not confirmed";
333 return;
334 }
335
336 const Qt::LayoutDirection inputDirection = (direction == text_direction_auto) ? Qt::LayoutDirectionAuto :
337 (direction == text_direction_ltr) ? Qt::LeftToRight :
338 (direction == text_direction_rtl) ? Qt::RightToLeft : Qt::LayoutDirectionAuto;
339 if (m_inputDirection != inputDirection) {
340 m_inputDirection = inputDirection;
341 QGuiApplicationPrivate::platformIntegration()->inputContext()->emitInputDirectionChanged(newDirection: m_inputDirection);
342 }
343}
344
345Qt::KeyboardModifiers QWaylandTextInputv1::modifiersToQtModifiers(uint32_t modifiers)
346{
347 Qt::KeyboardModifiers ret = Qt::NoModifier;
348 for (int i = 0; i < m_modifiersMap.size(); ++i) {
349 if (modifiers & (1 << i)) {
350 ret |= m_modifiersMap[i];
351 }
352 }
353 return ret;
354}
355
356}
357
358QT_END_NAMESPACE
359
360

source code of qtwayland/src/client/qwaylandtextinputv1.cpp