gui: return first default device when seatName isNull

for QInputDevice::primaryKeyboard() and
QPointingDevice::primaryPointingDevice().

This also reverts ae9fefe3c8.

Fixes: QTBUG-100790
Pick-to: 6.3
Change-Id: Id02f277db25f823eb29e939e25801325df8e4076
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
bb10
Liang Qi 2021-10-25 17:07:32 +02:00
parent 7382e5735e
commit 9c6b986d96
3 changed files with 51 additions and 10 deletions

View File

@ -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<const QInputDevice *>(d->parent()))
return d;

View File

@ -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)

View File

@ -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<const QInputDevice *> 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<const QInputDevice *>(d->parent()))
return d;
if (!ret)
ret = d;
}
}
return ret;
}
const QPointingDevice *tst_QInputDevice::getPrimaryPointingDevice(const QString& seatName)
{
QList<const QInputDevice *> 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<const QPointingDevice *>(dev);
// the core pointer is likely a mouse, and its parent is not another input device
if (!mouse->parent() || !qobject_cast<const QInputDevice *>(mouse->parent()))
return mouse;
} else if (dev->type() == QInputDevice::DeviceType::TouchPad) {
if (!touchpad || !dev->parent() || dev->parent()->metaObject() != dev->metaObject())
touchpad = static_cast<const QPointingDevice *>(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"));