From 9c6b986d967b42bea5faca0d9fa1000c7edb023e Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Mon, 25 Oct 2021 17:07:32 +0200 Subject: [PATCH] gui: return first default device when seatName isNull for QInputDevice::primaryKeyboard() and QPointingDevice::primaryPointingDevice(). This also reverts ae9fefe3c89fd5720ccaad0687f125e32f7c3fe6. Fixes: QTBUG-100790 Pick-to: 6.3 Change-Id: Id02f277db25f823eb29e939e25801325df8e4076 Reviewed-by: Qt CI Bot Reviewed-by: Shawn Rutledge --- src/gui/kernel/qinputdevice.cpp | 8 +-- src/gui/kernel/qpointingdevice.cpp | 2 +- .../kernel/qinputdevice/tst_qinputdevice.cpp | 51 ++++++++++++++++--- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/gui/kernel/qinputdevice.cpp b/src/gui/kernel/qinputdevice.cpp index 13bd672f3d..8aea2f6e2e 100644 --- a/src/gui/kernel/qinputdevice.cpp +++ b/src/gui/kernel/qinputdevice.cpp @@ -305,11 +305,13 @@ QStringList QInputDevice::seatNames() const QInputDevice *QInputDevice::primaryKeyboard(const QString& seatName) { QMutexLocker locker(&devicesMutex); - InputDevicesList v = *deviceList(); + const InputDevicesList devices = *deviceList(); locker.unlock(); const QInputDevice *ret = nullptr; - for (const QInputDevice *d : v) { - if (d->type() == DeviceType::Keyboard && d->seatName() == seatName) { + for (const QInputDevice *d : devices) { + if (d->type() != DeviceType::Keyboard) + continue; + if (seatName.isNull() || d->seatName() == seatName) { // the master keyboard's parent is not another input device if (!d->parent() || !qobject_cast(d->parent())) return d; diff --git a/src/gui/kernel/qpointingdevice.cpp b/src/gui/kernel/qpointingdevice.cpp index e2f17f821c..750be2b728 100644 --- a/src/gui/kernel/qpointingdevice.cpp +++ b/src/gui/kernel/qpointingdevice.cpp @@ -286,7 +286,7 @@ const QPointingDevice *QPointingDevice::primaryPointingDevice(const QString& sea const QPointingDevice *mouse = nullptr; const QPointingDevice *touchpad = nullptr; for (const QInputDevice *dev : v) { - if (dev->seatName() != seatName) + if (!seatName.isNull() && dev->seatName() != seatName) continue; if (dev->type() == QInputDevice::DeviceType::Mouse) { if (!mouse) diff --git a/tests/auto/gui/kernel/qinputdevice/tst_qinputdevice.cpp b/tests/auto/gui/kernel/qinputdevice/tst_qinputdevice.cpp index 06887420cf..2722583a83 100644 --- a/tests/auto/gui/kernel/qinputdevice/tst_qinputdevice.cpp +++ b/tests/auto/gui/kernel/qinputdevice/tst_qinputdevice.cpp @@ -43,15 +43,54 @@ private slots: void multiSeatDevices(); private: + const QInputDevice *getPrimaryKeyboard(const QString& seatName = QString()); + const QPointingDevice *getPrimaryPointingDevice(const QString& seatName = QString()); }; void tst_QInputDevice::initTestCase() { } -static bool isPlatformWayland() +const QInputDevice *tst_QInputDevice::getPrimaryKeyboard(const QString& seatName) { - return !QGuiApplication::platformName().compare(QLatin1String("wayland"), Qt::CaseInsensitive); + QList devices = QInputDevice::devices(); + const QInputDevice *ret = nullptr; + for (const QInputDevice *d : devices) { + if (d->type() != QInputDevice::DeviceType::Keyboard) + continue; + if (seatName.isNull() || d->seatName() == seatName) { + // the master keyboard's parent is not another input device + if (!d->parent() || !qobject_cast(d->parent())) + return d; + if (!ret) + ret = d; + } + } + return ret; +} + +const QPointingDevice *tst_QInputDevice::getPrimaryPointingDevice(const QString& seatName) +{ + QList devices = QInputDevice::devices(); + const QPointingDevice *mouse = nullptr; + const QPointingDevice *touchpad = nullptr; + for (const QInputDevice *dev : devices) { + if (!seatName.isNull() && dev->seatName() != seatName) + continue; + if (dev->type() == QInputDevice::DeviceType::Mouse) { + if (!mouse) + mouse = static_cast(dev); + // the core pointer is likely a mouse, and its parent is not another input device + if (!mouse->parent() || !qobject_cast(mouse->parent())) + return mouse; + } else if (dev->type() == QInputDevice::DeviceType::TouchPad) { + if (!touchpad || !dev->parent() || dev->parent()->metaObject() != dev->metaObject()) + touchpad = static_cast(dev); + } + } + if (mouse) + return mouse; + return touchpad; } void tst_QInputDevice::multiSeatDevices() @@ -70,11 +109,11 @@ void tst_QInputDevice::multiSeatDevices() QVERIFY(QInputDevicePrivate::fromId(2010)); QVERIFY(!QInputDevicePrivate::fromId(2010)->hasCapability(QInputDevice::Capability::Scroll)); QVERIFY(QInputDevice::primaryKeyboard()); - if (isPlatformWayland()) - QEXPECT_FAIL("", "This fails on Wayland, see QTBUG-100790.", Abort); - QCOMPARE(QInputDevice::primaryKeyboard()->systemId(), qint64(1) << 33); + if (!getPrimaryKeyboard()) + QCOMPARE(QInputDevice::primaryKeyboard()->systemId(), qint64(1) << 33); QVERIFY(QPointingDevice::primaryPointingDevice()); - QCOMPARE(QPointingDevice::primaryPointingDevice()->systemId(), 1); + if (!getPrimaryPointingDevice()) + QCOMPARE(QPointingDevice::primaryPointingDevice()->systemId(), 1); QVERIFY(QInputDevice::primaryKeyboard("seat 1")); QCOMPARE(QInputDevice::primaryKeyboard("seat 1")->systemId(), 1000); QVERIFY(QPointingDevice::primaryPointingDevice("seat 1"));