diff --git a/autotests/client/test_wayland_connection_thread.cpp b/autotests/client/test_wayland_connection_thread.cpp --- a/autotests/client/test_wayland_connection_thread.cpp +++ b/autotests/client/test_wayland_connection_thread.cpp @@ -78,7 +78,9 @@ void TestWaylandConnectionThread::testInitConnectionNoThread() { + QVERIFY(KWayland::Client::ConnectionThread::connections().isEmpty()); QScopedPointer connection(new KWayland::Client::ConnectionThread); + QVERIFY(KWayland::Client::ConnectionThread::connections().contains(connection.data())); QCOMPARE(connection->socketName(), QStringLiteral("wayland-0")); connection->setSocketName(s_socketName); QCOMPARE(connection->socketName(), s_socketName); @@ -90,6 +92,9 @@ QCOMPARE(connectedSpy.count(), 1); QCOMPARE(failedSpy.count(), 0); QVERIFY(connection->display()); + + connection.reset(); + QVERIFY(KWayland::Client::ConnectionThread::connections().isEmpty()); } void TestWaylandConnectionThread::testConnectionFailure() diff --git a/src/client/connection_thread.h b/src/client/connection_thread.h --- a/src/client/connection_thread.h +++ b/src/client/connection_thread.h @@ -21,6 +21,7 @@ #define WAYLAND_CONNECTION_THREAD_H #include +#include #include @@ -198,6 +199,12 @@ **/ int errorCode() const; + /** + * @returns all connections created in this application + * @since 5.37 + **/ + static QVector connections(); + public Q_SLOTS: /** * Initializes the connection in an asynchronous way. diff --git a/src/client/connection_thread.cpp b/src/client/connection_thread.cpp --- a/src/client/connection_thread.cpp +++ b/src/client/connection_thread.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include // Wayland @@ -55,22 +57,36 @@ bool foreign = false; QMetaObject::Connection eventDispatcherConnection; int error = 0; + static QVector connections; + static QMutex mutex; private: ConnectionThread *q; }; +QVector ConnectionThread::Private::connections = QVector{}; +QMutex ConnectionThread::Private::mutex = QMutex(QMutex::Recursive); + + ConnectionThread::Private::Private(ConnectionThread *q) : socketName(QString::fromUtf8(qgetenv("WAYLAND_DISPLAY"))) , runtimeDir(QString::fromUtf8(qgetenv("XDG_RUNTIME_DIR"))) , q(q) { if (socketName.isEmpty()) { socketName = QStringLiteral("wayland-0"); } + { + QMutexLocker lock(&mutex); + connections << q; + } } ConnectionThread::Private::~Private() { + { + QMutexLocker lock(&mutex); + connections.removeOne(q); + } if (display && !foreign) { wl_display_flush(display); wl_display_disconnect(display); @@ -280,5 +296,10 @@ return d->error; } +QVector ConnectionThread::connections() +{ + return Private::connections; +} + } }