diff --git a/autotests/libinput/device_test.cpp b/autotests/libinput/device_test.cpp --- a/autotests/libinput/device_test.cpp +++ b/autotests/libinput/device_test.cpp @@ -28,6 +28,7 @@ { Q_OBJECT private Q_SLOTS: + void testStaticGetter(); void testDeviceType_data(); void testDeviceType(); void testGestureSupport_data(); @@ -59,6 +60,48 @@ void testLeftHanded(); }; +void TestLibinputDevice::testStaticGetter() +{ + // this test verifies that the static getter for Device works as expected + QVERIFY(Device::devices().isEmpty()); + + // create some device + libinput_device device1; + libinput_device device2; + // at the moment not yet known to Device + QVERIFY(!Device::getDevice(&device1)); + QVERIFY(!Device::getDevice(&device2)); + QVERIFY(Device::devices().isEmpty()); + + // now create a Device for one + Device *d1 = new Device(&device1); + QCOMPARE(Device::devices().count(), 1); + QCOMPARE(Device::devices().first(), d1); + QCOMPARE(Device::getDevice(&device1), d1); + QVERIFY(!Device::getDevice(&device2)); + + // and a second Device + Device *d2 = new Device(&device2); + QCOMPARE(Device::devices().count(), 2); + QCOMPARE(Device::devices().first(), d1); + QCOMPARE(Device::devices().last(), d2); + QCOMPARE(Device::getDevice(&device1), d1); + QCOMPARE(Device::getDevice(&device2), d2); + + // now delete d1 + delete d1; + QCOMPARE(Device::devices().count(), 1); + QCOMPARE(Device::devices().first(), d2); + QCOMPARE(Device::getDevice(&device2), d2); + QVERIFY(!Device::getDevice(&device1)); + + // and delete d2 + delete d2; + QVERIFY(!Device::getDevice(&device1)); + QVERIFY(!Device::getDevice(&device2)); + QVERIFY(Device::devices().isEmpty()); +} + void TestLibinputDevice::testDeviceType_data() { QTest::addColumn("keyboard"); diff --git a/libinput/device.h b/libinput/device.h --- a/libinput/device.h +++ b/libinput/device.h @@ -22,6 +22,7 @@ #include #include +#include struct libinput_device; @@ -155,6 +156,17 @@ return m_device; } + /** + * All created Devices + **/ + static QVector devices() { + return s_devices; + } + /** + * Gets the Device for @p native. @c null if there is no Device for @p native. + **/ + static Device *getDevice(libinput_device *native); + Q_SIGNALS: void leftHandedChanged(); void pointerAccelerationChanged(); @@ -187,6 +199,8 @@ bool m_leftHanded; qreal m_pointerAcceleration; bool m_enabled; + + static QVector s_devices; }; } diff --git a/libinput/device.cpp b/libinput/device.cpp --- a/libinput/device.cpp +++ b/libinput/device.cpp @@ -52,6 +52,21 @@ return true; } +QVector Device::s_devices; + +Device *Device::getDevice(libinput_device *native) +{ + auto it = std::find_if(s_devices.constBegin(), s_devices.constEnd(), + [native] (const Device *d) { + return d->device() == native; + } + ); + if (it != s_devices.constEnd()) { + return *it; + } + return nullptr; +} + Device::Device(libinput_device *device, QObject *parent) : QObject(parent) , m_device(device) @@ -120,10 +135,13 @@ if (m_keyboard) { m_alphaNumericKeyboard = checkAlphaNumericKeyboard(m_device); } + + s_devices << this; } Device::~Device() { + s_devices.removeOne(this); libinput_device_unref(m_device); }