From 6c60391ad32445e31938fab2fc65af5a5821a6fb Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 27 Sep 2021 13:10:37 +0300 Subject: [PATCH 2/4] wayland: Move ownership of the libinput thread to InputRedirection When libinput tears down, it may access the Session object. This change re-jitters the shut down logic so the Session object is guaranteed to be valid when libinput stuff gets destroyed. BUG: 442104 (cherry picked from commit d7d1c6600ab9c95fb852a66d2a8fb745caa5d716) --- src/input.cpp | 16 ++++++++++++++++ src/input.h | 1 + src/libinput/connection.cpp | 19 +------------------ src/libinput/connection.h | 3 --- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 7ebad3052..29648b375 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -53,6 +53,7 @@ #include // Qt #include +#include #include @@ -2082,6 +2083,14 @@ InputRedirection::InputRedirection(QObject *parent) InputRedirection::~InputRedirection() { + if (m_libInput) { + m_libInput->deleteLater(); + + m_libInputThread->quit(); + m_libInputThread->wait(); + delete m_libInputThread; + } + s_self = nullptr; qDeleteAll(m_filters); qDeleteAll(m_spies); @@ -2319,8 +2328,15 @@ void InputRedirection::setupLibInput() if (m_libInput) { return; } + + m_libInputThread = new QThread(); + m_libInputThread->setObjectName(QStringLiteral("libinput-connection")); + m_libInputThread->start(); + LibInput::Connection *conn = LibInput::Connection::create(this); m_libInput = conn; + m_libInput->moveToThread(m_libInputThread); + if (conn) { if (waylandServer()) { diff --git a/src/input.h b/src/input.h index 8d9d40ce4..8b69dee5a 100644 --- a/src/input.h +++ b/src/input.h @@ -325,6 +325,7 @@ private: GlobalShortcutsManager *m_shortcuts; LibInput::Connection *m_libInput = nullptr; + QThread *m_libInputThread = nullptr; WindowSelectorFilter *m_windowSelector = nullptr; diff --git a/src/libinput/connection.cpp b/src/libinput/connection.cpp index 9cca61a3b..104f2ea94 100644 --- a/src/libinput/connection.cpp +++ b/src/libinput/connection.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -81,7 +80,6 @@ Q_SIGNALS: }; Connection *Connection::s_self = nullptr; -QPointer Connection::s_thread; static ConnectionAdaptor *s_adaptor = nullptr; static Context *s_context = nullptr; @@ -107,16 +105,6 @@ Connection::Connection(QObject *parent) // only here to fix build, using will crash, BUG 343529 } -void Connection::createThread() -{ - if (s_thread) { - return; - } - s_thread = new QThread(); - s_thread->setObjectName(QStringLiteral("libinput-connection")); - s_thread->start(); -} - Connection *Connection::create(QObject *parent) { Q_ASSERT(!s_self); @@ -141,12 +129,7 @@ Connection *Connection::create(QObject *parent) return nullptr; } } - Connection::createThread(); s_self = new Connection(s_context); - s_self->moveToThread(s_thread); - QObject::connect(s_thread, &QThread::finished, s_self, &QObject::deleteLater); - QObject::connect(s_thread, &QThread::finished, s_thread, &QObject::deleteLater); - QObject::connect(parent, &QObject::destroyed, s_thread, &QThread::quit); if (!s_adaptor) { s_adaptor = new ConnectionAdaptor(s_self); } @@ -335,7 +318,7 @@ void Connection::processEvents() switch (event->type()) { case LIBINPUT_EVENT_DEVICE_ADDED: { auto device = new Device(event->nativeDevice()); - device->moveToThread(s_thread); + device->moveToThread(thread()); m_devices << device; if (device->isKeyboard()) { m_keyboard++; diff --git a/src/libinput/connection.h b/src/libinput/connection.h index 61dbd2488..f4f1d523c 100644 --- a/src/libinput/connection.h +++ b/src/libinput/connection.h @@ -87,8 +87,6 @@ public: void updateLEDs(KWin::Xkb::LEDs leds); - static void createThread(); - Q_SIGNALS: void keyChanged(quint32 key, KWin::InputRedirection::KeyboardKeyState, quint32 time, KWin::LibInput::Device *device); void pointerButtonChanged(quint32 button, KWin::InputRedirection::PointerButtonState state, quint32 time, KWin::LibInput::Device *device); @@ -163,7 +161,6 @@ private: Xkb::LEDs m_leds; KWIN_SINGLETON(Connection) - static QPointer s_thread; }; } -- 2.32.0