Commit 066ac320 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

backends/libinput: Rework Device getter

libinput_device_get_user_data() can be used to get the associated Device
object with libinput_device. That way, we won't need to maintain a
private list of all input devices.
parent eb058a4a
Pipeline #169748 passed with stage
in 25 minutes and 55 seconds
......@@ -26,7 +26,6 @@ class TestLibinputDevice : public QObject
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testStaticGetter();
void testDeviceType_data();
void testDeviceType();
void testGestureSupport_data();
......@@ -181,48 +180,6 @@ void TestLibinputDevice::initTestCase()
QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kwin.tests.libinputdevice"));
}
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");
......
......@@ -956,3 +956,14 @@ void libinput_device_led_update(struct libinput_device *device,
Q_UNUSED(device)
Q_UNUSED(leds)
}
void libinput_device_set_user_data(struct libinput_device *device, void *user_data)
{
device->userData = user_data;
}
void *
libinput_device_get_user_data(struct libinput_device *device)
{
return device->userData;
}
......@@ -19,6 +19,7 @@
struct libinput_device
{
void *userData = nullptr;
bool keyboard = false;
bool pointer = false;
bool touch = false;
......
......@@ -77,20 +77,6 @@ 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;
}
enum class ConfigKey {
Enabled,
LeftHanded,
......@@ -336,6 +322,7 @@ Device::Device(libinput_device *device, QObject *parent)
, m_clickMethod(libinput_device_config_click_get_method(m_device))
{
libinput_device_ref(m_device);
libinput_device_set_user_data(m_device, this);
qreal width = 0;
qreal height = 0;
......@@ -385,7 +372,6 @@ Device::Device(libinput_device *device, QObject *parent)
qDBusRegisterMetaType<QMatrix4x4>();
s_devices << this;
QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin/InputDevice/") + m_sysName,
QStringLiteral("org.kde.KWin.InputDevice"),
this,
......@@ -394,11 +380,16 @@ Device::Device(libinput_device *device, QObject *parent)
Device::~Device()
{
s_devices.removeOne(this);
QDBusConnection::sessionBus().unregisterObject(QStringLiteral("/org/kde/KWin/InputDevice/") + m_sysName);
libinput_device_set_user_data(m_device, nullptr);
libinput_device_unref(m_device);
}
Device *Device::get(libinput_device *native)
{
return static_cast<Device *>(libinput_device_get_user_data(native));
}
template<typename T>
void Device::writeEntry(const ConfigKey &key, const T &value)
{
......
......@@ -619,17 +619,10 @@ public:
LEDs leds() const override;
void setLeds(LEDs leds) override;
/**
* 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);
static Device *get(libinput_device *native);
Q_SIGNALS:
void tapButtonMapChanged();
......@@ -736,7 +729,6 @@ private:
enum libinput_config_click_method m_clickMethod;
LEDs m_leds;
static QVector<Device *> s_devices;
};
}
......
......@@ -83,7 +83,7 @@ Event::~Event()
Device *Event::device() const
{
if (!m_device) {
m_device = Device::getDevice(libinput_event_get_device(m_event));
m_device = Device::get(libinput_event_get_device(m_event));
}
return m_device;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment