From 066ac3200a60d7dc0ddfe270512f2ef820c67ab3 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 29 Apr 2022 11:23:23 +0300 Subject: [PATCH] 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. --- autotests/libinput/device_test.cpp | 43 ---------------------------- autotests/libinput/mock_libinput.cpp | 11 +++++++ autotests/libinput/mock_libinput.h | 1 + src/backends/libinput/device.cpp | 23 +++++---------- src/backends/libinput/device.h | 10 +------ src/backends/libinput/events.cpp | 2 +- 6 files changed, 21 insertions(+), 69 deletions(-) diff --git a/autotests/libinput/device_test.cpp b/autotests/libinput/device_test.cpp index ad6df4c6ea..e3ebbb6e80 100644 --- a/autotests/libinput/device_test.cpp +++ b/autotests/libinput/device_test.cpp @@ -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("keyboard"); diff --git a/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp index 703ab70eda..d83ea2162c 100644 --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -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; +} diff --git a/autotests/libinput/mock_libinput.h b/autotests/libinput/mock_libinput.h index f9f76966ee..23e23d00e1 100644 --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -19,6 +19,7 @@ struct libinput_device { + void *userData = nullptr; bool keyboard = false; bool pointer = false; bool touch = false; diff --git a/src/backends/libinput/device.cpp b/src/backends/libinput/device.cpp index f7e0d389cc..19fe0349a7 100644 --- a/src/backends/libinput/device.cpp +++ b/src/backends/libinput/device.cpp @@ -77,20 +77,6 @@ static bool checkAlphaNumericKeyboard(libinput_device *device) 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; -} - 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(); - 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(libinput_device_get_user_data(native)); +} + template void Device::writeEntry(const ConfigKey &key, const T &value) { diff --git a/src/backends/libinput/device.h b/src/backends/libinput/device.h index aa64c2c011..6c9a6ccd44 100644 --- a/src/backends/libinput/device.h +++ b/src/backends/libinput/device.h @@ -619,17 +619,10 @@ public: LEDs leds() const override; void setLeds(LEDs leds) override; - /** - * 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); + 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 s_devices; }; } diff --git a/src/backends/libinput/events.cpp b/src/backends/libinput/events.cpp index c9f779b27f..2f637ba88c 100644 --- a/src/backends/libinput/events.cpp +++ b/src/backends/libinput/events.cpp @@ -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; }