From 4121d45c42df00052b79076da7858f72d7a2aa04 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Wed, 20 Jul 2022 11:38:03 +0200 Subject: [PATCH] backends/libinput: remove global state --- src/backends/libinput/connection.cpp | 64 ++++++----------------- src/backends/libinput/connection.h | 14 +++-- src/backends/libinput/context.cpp | 5 +- src/backends/libinput/context.h | 3 +- src/backends/libinput/libinputbackend.cpp | 22 ++++---- src/backends/libinput/libinputbackend.h | 4 +- 6 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/backends/libinput/connection.cpp b/src/backends/libinput/connection.cpp index 41b380e634..ab1f40502b 100644 --- a/src/backends/libinput/connection.cpp +++ b/src/backends/libinput/connection.cpp @@ -80,53 +80,30 @@ Q_SIGNALS: void deviceRemoved(QString sysName); }; -Connection *Connection::s_self = nullptr; - -static ConnectionAdaptor *s_adaptor = nullptr; -static Context *s_context = nullptr; - -Connection::Connection(QObject *parent) - : Connection(nullptr, parent) +std::unique_ptr Connection::create() { - // only here to fix build, using will crash, BUG 343529 -} - -Connection *Connection::create(QObject *parent) -{ - Q_ASSERT(!s_self); - static Udev s_udev; - if (!s_udev.isValid()) { + std::unique_ptr udev = std::make_unique(); + if (!udev->isValid()) { qCWarning(KWIN_LIBINPUT) << "Failed to initialize udev"; return nullptr; } - if (!s_context) { - s_context = new Context(s_udev); - if (!s_context->isValid()) { - qCWarning(KWIN_LIBINPUT) << "Failed to create context from udev"; - delete s_context; - s_context = nullptr; - return nullptr; - } - const QString seat = kwinApp()->platform()->session()->seat(); - if (!s_context->assignSeat(seat.toUtf8().constData())) { - qCWarning(KWIN_LIBINPUT) << "Failed to assign seat" << seat; - delete s_context; - s_context = nullptr; - return nullptr; - } + std::unique_ptr context = std::make_unique(std::move(udev)); + if (!context->isValid()) { + qCWarning(KWIN_LIBINPUT) << "Failed to create context from udev"; + return nullptr; } - s_self = new Connection(s_context); - if (!s_adaptor) { - s_adaptor = new ConnectionAdaptor(s_self); + const QString seat = kwinApp()->platform()->session()->seat(); + if (!context->assignSeat(seat.toUtf8().constData())) { + qCWarning(KWIN_LIBINPUT) << "Failed to assign seat" << seat; + return nullptr; } - - return s_self; + return std::unique_ptr(new Connection(std::move(context))); } -Connection::Connection(Context *input, QObject *parent) - : QObject(parent) - , m_input(input) - , m_notifier(nullptr) +Connection::Connection(std::unique_ptr &&input) + : m_notifier(nullptr) + , m_connectionAdaptor(std::make_unique(this)) + , m_input(std::move(input)) { Q_ASSERT(m_input); // need to connect to KGlobalSettings as the mouse KCM does not emit a dedicated signal @@ -134,14 +111,7 @@ Connection::Connection(Context *input, QObject *parent) QStringLiteral("notifyChange"), this, SLOT(slotKGlobalSettingsNotifyChange(int, int))); } -Connection::~Connection() -{ - delete s_adaptor; - s_adaptor = nullptr; - s_self = nullptr; - delete s_context; - s_context = nullptr; -} +Connection::~Connection() = default; void Connection::setup() { diff --git a/src/backends/libinput/connection.h b/src/backends/libinput/connection.h index 1dfec939c1..f50033b643 100644 --- a/src/backends/libinput/connection.h +++ b/src/backends/libinput/connection.h @@ -26,12 +26,16 @@ class QThread; namespace KWin { + +class Udev; + namespace LibInput { class Event; class Device; class Context; +class ConnectionAdaptor; class KWIN_EXPORT Connection : public QObject { @@ -52,6 +56,8 @@ public: QStringList devicesSysNames() const; + static std::unique_ptr create(); + Q_SIGNALS: void deviceAdded(KWin::LibInput::Device *); void deviceRemoved(KWin::LibInput::Device *); @@ -63,18 +69,18 @@ private Q_SLOTS: void slotKGlobalSettingsNotifyChange(int type, int arg); private: - Connection(Context *input, QObject *parent = nullptr); + Connection(std::unique_ptr &&input); void handleEvent(); void applyDeviceConfig(Device *device); void applyScreenToDevice(Device *device); - Context *m_input; QSocketNotifier *m_notifier; QRecursiveMutex m_mutex; std::deque> m_eventQueue; QVector m_devices; KSharedConfigPtr m_config; - - KWIN_SINGLETON(Connection) + std::unique_ptr m_connectionAdaptor; + std::unique_ptr m_input; + std::unique_ptr m_udev; }; } diff --git a/src/backends/libinput/context.cpp b/src/backends/libinput/context.cpp index 99fd78e3d6..4b6a20d03b 100644 --- a/src/backends/libinput/context.cpp +++ b/src/backends/libinput/context.cpp @@ -46,9 +46,10 @@ static void libinputLogHandler(libinput *libinput, libinput_log_priority priorit } } -Context::Context(const Udev &udev) - : m_libinput(libinput_udev_create_context(&Context::s_interface, this, udev)) +Context::Context(std::unique_ptr &&udev) + : m_libinput(libinput_udev_create_context(&Context::s_interface, this, *udev.get())) , m_suspended(false) + , m_udev(std::move(udev)) { libinput_log_set_priority(m_libinput, LIBINPUT_LOG_PRIORITY_DEBUG); libinput_log_set_handler(m_libinput, &libinputLogHandler); diff --git a/src/backends/libinput/context.h b/src/backends/libinput/context.h index efcb231cf3..74da9eda9b 100644 --- a/src/backends/libinput/context.h +++ b/src/backends/libinput/context.h @@ -25,7 +25,7 @@ class Event; class Context { public: - Context(const Udev &udev); + Context(std::unique_ptr &&udev); ~Context(); bool assignSeat(const char *seat); bool isValid() const @@ -65,6 +65,7 @@ private: void closeRestricted(int fd); struct libinput *m_libinput; bool m_suspended; + std::unique_ptr m_udev; }; } diff --git a/src/backends/libinput/libinputbackend.cpp b/src/backends/libinput/libinputbackend.cpp index 303f1158af..b7e6f37d5a 100644 --- a/src/backends/libinput/libinputbackend.cpp +++ b/src/backends/libinput/libinputbackend.cpp @@ -14,34 +14,30 @@ namespace KWin LibinputBackend::LibinputBackend(QObject *parent) : InputBackend(parent) { - m_thread = new QThread(); - m_thread->setObjectName(QStringLiteral("libinput-connection")); - m_thread->start(); + m_thread.setObjectName(QStringLiteral("libinput-connection")); + m_thread.start(); - m_connection = LibInput::Connection::create(this); - m_connection->moveToThread(m_thread); + m_connection = LibInput::Connection::create(); + m_connection->moveToThread(&m_thread); connect( - m_connection, &LibInput::Connection::eventsRead, this, [this]() { + m_connection.get(), &LibInput::Connection::eventsRead, this, [this]() { m_connection->processEvents(); }, Qt::QueuedConnection); // Direct connection because the deviceAdded() and the deviceRemoved() signals are emitted // from the main thread. - connect(m_connection, &LibInput::Connection::deviceAdded, + connect(m_connection.get(), &LibInput::Connection::deviceAdded, this, &InputBackend::deviceAdded, Qt::DirectConnection); - connect(m_connection, &LibInput::Connection::deviceRemoved, + connect(m_connection.get(), &LibInput::Connection::deviceRemoved, this, &InputBackend::deviceRemoved, Qt::DirectConnection); } LibinputBackend::~LibinputBackend() { - m_connection->deleteLater(); - - m_thread->quit(); - m_thread->wait(); - delete m_thread; + m_thread.quit(); + m_thread.wait(); } void LibinputBackend::initialize() diff --git a/src/backends/libinput/libinputbackend.h b/src/backends/libinput/libinputbackend.h index 7f8f2d05e6..628d048640 100644 --- a/src/backends/libinput/libinputbackend.h +++ b/src/backends/libinput/libinputbackend.h @@ -29,8 +29,8 @@ public: void initialize() override; private: - QThread *m_thread = nullptr; - LibInput::Connection *m_connection = nullptr; + QThread m_thread; + std::unique_ptr m_connection; }; } // namespace KWin