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