| 1 | // Copyright (C) 2016 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 | |
| 4 | |
| 5 | #include "qspiapplicationadaptor_p.h" |
| 6 | |
| 7 | #include <QtCore/qcoreapplication.h> |
| 8 | #include <QtDBus/qdbuspendingreply.h> |
| 9 | #include <qdebug.h> |
| 10 | |
| 11 | #if QT_CONFIG(accessibility) |
| 12 | #include "deviceeventcontroller_adaptor.h" |
| 13 | #include "atspi/atspi-constants.h" |
| 14 | |
| 15 | #if __has_include(<xcb/xproto.h>) |
| 16 | #include <xcb/xproto.h> |
| 17 | #endif |
| 18 | |
| 19 | //#define KEYBOARD_DEBUG |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | using namespace Qt::Literals::StringLiterals; |
| 24 | |
| 25 | /*! |
| 26 | \class QSpiApplicationAdaptor |
| 27 | \internal |
| 28 | |
| 29 | \brief QSpiApplicationAdaptor |
| 30 | |
| 31 | QSpiApplicationAdaptor |
| 32 | */ |
| 33 | |
| 34 | QSpiApplicationAdaptor::QSpiApplicationAdaptor(const QDBusConnection &connection, QObject *parent) |
| 35 | : QObject(parent), dbusConnection(connection) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | enum QSpiKeyEventType { |
| 40 | QSPI_KEY_EVENT_PRESS, |
| 41 | QSPI_KEY_EVENT_RELEASE, |
| 42 | QSPI_KEY_EVENT_LAST_DEFINED |
| 43 | }; |
| 44 | |
| 45 | void QSpiApplicationAdaptor::sendEvents(bool active) |
| 46 | { |
| 47 | if (active) { |
| 48 | qApp->installEventFilter(filterObj: this); |
| 49 | } else { |
| 50 | qApp->removeEventFilter(obj: this); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | bool QSpiApplicationAdaptor::eventFilter(QObject *target, QEvent *event) |
| 56 | { |
| 57 | if (!event->spontaneous()) |
| 58 | return false; |
| 59 | |
| 60 | switch (event->type()) { |
| 61 | case QEvent::WindowActivate: |
| 62 | emit windowActivated(window: target, active: true); |
| 63 | break; |
| 64 | case QEvent::WindowDeactivate: |
| 65 | emit windowActivated(window: target, active: false); |
| 66 | break; |
| 67 | case QEvent::KeyPress: |
| 68 | case QEvent::KeyRelease: { |
| 69 | QKeyEvent *keyEvent = static_cast <QKeyEvent *>(event); |
| 70 | QSpiDeviceEvent de; |
| 71 | |
| 72 | if (event->type() == QEvent::KeyPress) |
| 73 | de.type = QSPI_KEY_EVENT_PRESS; |
| 74 | else |
| 75 | de.type = QSPI_KEY_EVENT_RELEASE; |
| 76 | |
| 77 | de.id = keyEvent->nativeVirtualKey(); |
| 78 | de.hardwareCode = keyEvent->nativeScanCode(); |
| 79 | |
| 80 | de.timestamp = QDateTime::currentMSecsSinceEpoch(); |
| 81 | |
| 82 | if (keyEvent->key() == Qt::Key_Tab) |
| 83 | de.text = QStringLiteral("Tab" ); |
| 84 | else if (keyEvent->key() == Qt::Key_Backtab) |
| 85 | de.text = QStringLiteral("Backtab" ); |
| 86 | else if (keyEvent->key() == Qt::Key_Control) |
| 87 | de.text = QStringLiteral("Control_L" ); |
| 88 | else if (keyEvent->key() == Qt::Key_Left) |
| 89 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Left" ) : QStringLiteral("Left" ); |
| 90 | else if (keyEvent->key() == Qt::Key_Right) |
| 91 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Right" ) : QStringLiteral("Right" ); |
| 92 | else if (keyEvent->key() == Qt::Key_Up) |
| 93 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Up" ) : QStringLiteral("Up" ); |
| 94 | else if (keyEvent->key() == Qt::Key_Down) |
| 95 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Down" ) : QStringLiteral("Down" ); |
| 96 | else if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) |
| 97 | de.text = QStringLiteral("Return" ); |
| 98 | else if (keyEvent->key() == Qt::Key_Backspace) |
| 99 | de.text = QStringLiteral("BackSpace" ); |
| 100 | else if (keyEvent->key() == Qt::Key_Delete) |
| 101 | de.text = QStringLiteral("Delete" ); |
| 102 | else if (keyEvent->key() == Qt::Key_PageUp) |
| 103 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Page_Up" ) : QStringLiteral("Page_Up" ); |
| 104 | else if (keyEvent->key() == Qt::Key_PageDown) |
| 105 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Page_Up" ) : QStringLiteral("Page_Down" ); |
| 106 | else if (keyEvent->key() == Qt::Key_Home) |
| 107 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_Home" ) : QStringLiteral("Home" ); |
| 108 | else if (keyEvent->key() == Qt::Key_End) |
| 109 | de.text = (keyEvent->modifiers() & Qt::KeypadModifier) ? QStringLiteral("KP_End" ) : QStringLiteral("End" ); |
| 110 | else if (keyEvent->key() == Qt::Key_Clear && (keyEvent->modifiers() & Qt::KeypadModifier)) |
| 111 | de.text = QStringLiteral("KP_Begin" ); // Key pad 5 |
| 112 | else if (keyEvent->key() == Qt::Key_Escape) |
| 113 | de.text = QStringLiteral("Escape" ); |
| 114 | else if (keyEvent->key() == Qt::Key_Space) |
| 115 | de.text = QStringLiteral("space" ); |
| 116 | else if (keyEvent->key() == Qt::Key_CapsLock) |
| 117 | de.text = QStringLiteral("Caps_Lock" ); |
| 118 | else if (keyEvent->key() == Qt::Key_NumLock) |
| 119 | de.text = QStringLiteral("Num_Lock" ); |
| 120 | else if (keyEvent->key() == Qt::Key_Insert) |
| 121 | de.text = QStringLiteral("Insert" ); |
| 122 | else |
| 123 | de.text = keyEvent->text(); |
| 124 | |
| 125 | // This is a bit dubious, Gnome uses some gtk function here. |
| 126 | // Long term the spec will hopefully change to just use keycodes. |
| 127 | de.isText = !de.text.isEmpty(); |
| 128 | |
| 129 | de.modifiers = 0; |
| 130 | if ((keyEvent->modifiers() & Qt::ShiftModifier) && (keyEvent->key() != Qt::Key_Shift)) |
| 131 | de.modifiers |= 1 << ATSPI_MODIFIER_SHIFT; |
| 132 | #ifdef XCB_MOD_MASK_LOCK |
| 133 | if (QGuiApplication::platformName().startsWith("xcb"_L1 )) { |
| 134 | // TODO rather introduce Qt::CapslockModifier into KeyboardModifier |
| 135 | if (keyEvent->nativeModifiers() & XCB_MOD_MASK_LOCK ) |
| 136 | de.modifiers |= 1 << ATSPI_MODIFIER_SHIFTLOCK; |
| 137 | } |
| 138 | #endif |
| 139 | if ((keyEvent->modifiers() & Qt::ControlModifier) && (keyEvent->key() != Qt::Key_Control)) |
| 140 | de.modifiers |= 1 << ATSPI_MODIFIER_CONTROL; |
| 141 | if ((keyEvent->modifiers() & Qt::AltModifier) && (keyEvent->key() != Qt::Key_Alt)) |
| 142 | de.modifiers |= 1 << ATSPI_MODIFIER_ALT; |
| 143 | if ((keyEvent->modifiers() & Qt::MetaModifier) && (keyEvent->key() != Qt::Key_Meta)) |
| 144 | de.modifiers |= 1 << ATSPI_MODIFIER_META; |
| 145 | |
| 146 | #ifdef KEYBOARD_DEBUG |
| 147 | qDebug() << "Key event text:" << event->type() << de.text |
| 148 | << "native virtual key:" << de.id |
| 149 | << "hardware code/scancode:" << de.hardwareCode |
| 150 | << "modifiers:" << de.modifiers |
| 151 | << "text:" << de.text; |
| 152 | #endif |
| 153 | |
| 154 | QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.a11y.atspi.Registry" ), |
| 155 | QStringLiteral("/org/a11y/atspi/registry/deviceeventcontroller" ), |
| 156 | QStringLiteral("org.a11y.atspi.DeviceEventController" ), QStringLiteral("NotifyListenersSync" )); |
| 157 | m.setArguments(QVariantList() << QVariant::fromValue(de)); |
| 158 | |
| 159 | // FIXME: this is critical, the timeout should probably be pretty low to allow normal processing |
| 160 | int timeout = 100; |
| 161 | bool sent = dbusConnection.callWithCallback(message: m, receiver: this, SLOT(notifyKeyboardListenerCallback(QDBusMessage)), |
| 162 | SLOT(notifyKeyboardListenerError(QDBusError,QDBusMessage)), timeout); |
| 163 | if (sent) { |
| 164 | //queue the event and send it after callback |
| 165 | keyEvents.enqueue(t: QPair<QPointer<QObject>, QKeyEvent*> (QPointer<QObject>(target), copyKeyEvent(keyEvent))); |
| 166 | return true; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | QKeyEvent* QSpiApplicationAdaptor::copyKeyEvent(QKeyEvent* old) |
| 177 | { |
| 178 | return new QKeyEvent(old->type(), old->key(), old->modifiers(), |
| 179 | old->nativeScanCode(), old->nativeVirtualKey(), old->nativeModifiers(), |
| 180 | old->text(), old->isAutoRepeat(), old->count()); |
| 181 | } |
| 182 | |
| 183 | void QSpiApplicationAdaptor::notifyKeyboardListenerCallback(const QDBusMessage& message) |
| 184 | { |
| 185 | if (!keyEvents.size()) { |
| 186 | qWarning(msg: "QSpiApplication::notifyKeyboardListenerCallback with no queued key called" ); |
| 187 | return; |
| 188 | } |
| 189 | Q_ASSERT(message.arguments().size() == 1); |
| 190 | if (message.arguments().at(i: 0).toBool() == true) { |
| 191 | QPair<QPointer<QObject>, QKeyEvent*> event = keyEvents.dequeue(); |
| 192 | delete event.second; |
| 193 | } else { |
| 194 | QPair<QPointer<QObject>, QKeyEvent*> event = keyEvents.dequeue(); |
| 195 | if (event.first) |
| 196 | QCoreApplication::postEvent(receiver: event.first.data(), event: event.second); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void QSpiApplicationAdaptor::notifyKeyboardListenerError(const QDBusError& error, const QDBusMessage& /*message*/) |
| 201 | { |
| 202 | qWarning() << "QSpiApplication::keyEventError " << error.name() << error.message(); |
| 203 | while (!keyEvents.isEmpty()) { |
| 204 | QPair<QPointer<QObject>, QKeyEvent*> event = keyEvents.dequeue(); |
| 205 | if (event.first) |
| 206 | QCoreApplication::postEvent(receiver: event.first.data(), event: event.second); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | QT_END_NAMESPACE |
| 211 | |
| 212 | #include "moc_qspiapplicationadaptor_p.cpp" |
| 213 | |
| 214 | #endif // QT_CONFIG(accessibility) |
| 215 | |