[libinput] Track all created Devices in Device

Summary:
Device has a static QVector<Device*> into which each created Device
is added and provides a static method to match a libinput_device* to
the already created Device.

This can be used by the the libinput Event class wrapper to properly
reference the Device the event is for.

Reviewers: #plasma

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D1665
icc-effect-5.14.5
Martin Gräßlin 2016-05-24 09:57:07 +02:00
parent 9cdd76f20d
commit 82d2a2f9f1
3 changed files with 75 additions and 0 deletions

View File

@ -28,6 +28,7 @@ class TestLibinputDevice : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testStaticGetter();
void testDeviceType_data();
void testDeviceType();
void testGestureSupport_data();
@ -59,6 +60,48 @@ private Q_SLOTS:
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<bool>("keyboard");

View File

@ -52,6 +52,21 @@ static bool checkAlphaNumericKeyboard(libinput_device *device)
return true;
}
QVector<Device*> 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 @@ Device::Device(libinput_device *device, QObject *parent)
if (m_keyboard) {
m_alphaNumericKeyboard = checkAlphaNumericKeyboard(m_device);
}
s_devices << this;
}
Device::~Device()
{
s_devices.removeOne(this);
libinput_device_unref(m_device);
}

View File

@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QObject>
#include <QSizeF>
#include <QVector>
struct libinput_device;
@ -155,6 +156,17 @@ public:
return m_device;
}
/**
* All created Devices
**/
static QVector<Device*> 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 @@ private:
bool m_leftHanded;
qreal m_pointerAcceleration;
bool m_enabled;
static QVector<Device*> s_devices;
};
}