diff --git a/src/wayland/autotests/client/test_wayland_surface.cpp b/src/wayland/autotests/client/test_wayland_surface.cpp index cea3de5d90..3c8de2992d 100644 --- a/src/wayland/autotests/client/test_wayland_surface.cpp +++ b/src/wayland/autotests/client/test_wayland_surface.cpp @@ -165,6 +165,10 @@ void TestWaylandSurface::testStaticAccessor() QCOMPARE(KWayland::Client::Surface::all().first(), s1); QCOMPARE(KWayland::Client::Surface::get(*s1), s1); QVERIFY(serverSurfaceCreated.wait()); + auto serverSurface1 = serverSurfaceCreated.first().first().value(); + QVERIFY(serverSurface1); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->resource()), serverSurface1); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->id()), serverSurface1); QVERIFY(!s1->size().isValid()); QSignalSpy sizeChangedSpy(s1, SIGNAL(sizeChanged(QSize))); @@ -185,6 +189,12 @@ void TestWaylandSurface::testStaticAccessor() QCOMPARE(KWayland::Client::Surface::get(*s2), s2); serverSurfaceCreated.clear(); QVERIFY(serverSurfaceCreated.wait()); + auto serverSurface2 = serverSurfaceCreated.first().first().value(); + QVERIFY(serverSurface2); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->resource()), serverSurface1); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->id()), serverSurface1); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface2->resource()), serverSurface2); + QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface2->id()), serverSurface2); // delete s2 again delete s2; diff --git a/src/wayland/server/resource_p.h b/src/wayland/server/resource_p.h index cc365dc9f1..c410bdb1cd 100644 --- a/src/wayland/server/resource_p.h +++ b/src/wayland/server/resource_p.h @@ -54,6 +54,20 @@ public: } return reinterpret_cast((*it)->q); } + template + static ResourceDerived *get(quint32 id) { + static_assert(std::is_base_of::value, + "ResourceDerived must be derived from Resource"); + auto it = std::find_if(s_allResources.constBegin(), s_allResources.constEnd(), + [id](Private *p) { + return wl_resource_get_id(p->resource) == id; + } + ); + if (it == s_allResources.constEnd()) { + return nullptr; + } + return reinterpret_cast((*it)->q); + } protected: explicit Private(Resource *q, Global *g, wl_resource *parentResource, const wl_interface *interface, const void *implementation); diff --git a/src/wayland/surface_interface.cpp b/src/wayland/surface_interface.cpp index 8cf56d9e6b..5820dd680b 100644 --- a/src/wayland/surface_interface.cpp +++ b/src/wayland/surface_interface.cpp @@ -419,6 +419,11 @@ SurfaceInterface *SurfaceInterface::get(wl_resource *native) return Private::get(native); } +SurfaceInterface *SurfaceInterface::get(quint32 id) +{ + return Private::get(id); +} + QList< QPointer< SubSurfaceInterface > > SurfaceInterface::childSubSurfaces() const { Q_D(); diff --git a/src/wayland/surface_interface.h b/src/wayland/surface_interface.h index 6153c9c73a..a9b855c3ae 100644 --- a/src/wayland/surface_interface.h +++ b/src/wayland/surface_interface.h @@ -69,6 +69,11 @@ public: QList> childSubSurfaces() const; static SurfaceInterface *get(wl_resource *native); + /** + * @returns The SurfaceInterface with given @p id, if it exists, otherwise @c nullptr. + * @since 5.3 + **/ + static SurfaceInterface *get(quint32 id); Q_SIGNALS: void damaged(const QRegion&);