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

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