From f8975f7c2ea31578fdd4411955f9f3b0ab757fdb Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 10 May 2024 17:50:51 -0700 Subject: [PATCH] WIP `destroy_object` --- wayland-backend/src/rs/server_impl/client.rs | 14 +++++++++++++- wayland-backend/src/rs/server_impl/handle.rs | 9 +++++++++ wayland-backend/src/server_api.rs | 4 ++++ wayland-backend/src/sys/server_impl/mod.rs | 12 ++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/wayland-backend/src/rs/server_impl/client.rs b/wayland-backend/src/rs/server_impl/client.rs index 44c216db6de..e6697638306 100644 --- a/wayland-backend/src/rs/server_impl/client.rs +++ b/wayland-backend/src/rs/server_impl/client.rs @@ -99,6 +99,18 @@ impl Client { InnerObjectId { id, serial, client_id: self.id.clone(), interface } } + pub(crate) fn destroy_object( + &mut self, + id: InnerObjectId, + pending_destructors: &mut Vec>, + ) -> Result<(), InvalidId> { + let object = self.get_object(id.clone())?; + self.map.remove(id.id); + pending_destructors.push((object.data.user_data.clone(), self.id.clone(), id.clone())); + self.send_delete_id(id.clone()); + Ok(()) + } + pub(crate) fn object_info(&self, id: InnerObjectId) -> Result { let object = self.get_object(id.clone())?; Ok(ObjectInfo { id: id.id, interface: object.interface, version: object.version }) @@ -376,7 +388,7 @@ impl Client { } } - fn get_object(&self, id: InnerObjectId) -> Result>, InvalidId> { + pub(crate) fn get_object(&self, id: InnerObjectId) -> Result>, InvalidId> { let object = self.map.find(id.id).ok_or(InvalidId)?; if object.data.serial != id.serial { return Err(InvalidId); diff --git a/wayland-backend/src/rs/server_impl/handle.rs b/wayland-backend/src/rs/server_impl/handle.rs index 7eb284acb91..107a2865a7b 100644 --- a/wayland-backend/src/rs/server_impl/handle.rs +++ b/wayland-backend/src/rs/server_impl/handle.rs @@ -162,6 +162,15 @@ impl InnerHandle { Ok(ObjectId { id: client.create_object(interface, version, data) }) } + pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> { + let mut state = self.state.lock().unwrap(); + let state = (&mut *state as &mut dyn ErasedState) + .downcast_mut::>() + .expect("Wrong type parameter passed to Handle::destroy_object()."); + let client = state.clients.get_client_mut(id.id.client_id.clone())?; + client.destroy_object(id.id.clone(), &mut state.pending_destructors) + } + pub fn null_id() -> ObjectId { ObjectId { id: InnerObjectId { diff --git a/wayland-backend/src/server_api.rs b/wayland-backend/src/server_api.rs index 6e3565db1c0..7754c89b807 100644 --- a/wayland-backend/src/server_api.rs +++ b/wayland-backend/src/server_api.rs @@ -355,6 +355,10 @@ impl Handle { self.handle.create_object(client_id.id, interface, version, data) } + pub fn destroy_object(&self, id: &ObjectId) { + self.handle.destroy_object::(id); + } + /// Send an event to the client /// /// Returns an error if the sender ID of the provided message is no longer valid. diff --git a/wayland-backend/src/sys/server_impl/mod.rs b/wayland-backend/src/sys/server_impl/mod.rs index f0d9c7c545f..f1705d9c1ca 100644 --- a/wayland-backend/src/sys/server_impl/mod.rs +++ b/wayland-backend/src/sys/server_impl/mod.rs @@ -565,6 +565,18 @@ impl InnerHandle { Ok(ObjectId { id: unsafe { init_resource(resource, interface, Some(data)).0 } }) } + pub fn destroy_object(&self, id: &ObjectId) { + let mut state = self.state.lock().unwrap(); + // Keep this guard alive while the code is run to protect the C state + let state = (&mut *state as &mut dyn ErasedState) + .downcast_mut::>() + .expect("Wrong type parameter passed to Handle::destroy_object()."); + + PENDING_DESTRUCTORS.set(&(&mut state.pending_destructors as *mut _ as *mut _), || unsafe { + ffi_dispatch!(wayland_server_handle(), wl_resource_destroy, id.id.ptr); + }); + } + pub fn null_id() -> ObjectId { ObjectId { id: InnerObjectId {