Skip to content

Commit

Permalink
backend/server: Add destroy_object method
Browse files Browse the repository at this point in the history
This is needed for implementing the `wl_fixes` protocol. It should also
make it possible to handle the requirement in
`zwlr_output_configuration_v1::destroy` to destroy the
`wlr_output_configuration_head`.
  • Loading branch information
ids1024 committed May 15, 2024
1 parent 987c544 commit 34b5cd4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions wayland-backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Additions
- backend/server: Added a `destroy_object` method

## 0.3.3 -- 2024-01-29

### Additions
Expand Down
14 changes: 12 additions & 2 deletions wayland-backend/src/rs/server_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ impl<D> Client<D> {
InnerObjectId { id, serial, client_id: self.id.clone(), interface }
}

pub(crate) fn destroy_object(
&mut self,
id: InnerObjectId,
pending_destructors: &mut Vec<super::handle::PendingDestructor<D>>,
) -> Result<(), InvalidId> {
let object = self.get_object(id.clone())?;
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<ObjectInfo, InvalidId> {
let object = self.get_object(id.clone())?;
Ok(ObjectInfo { id: id.id, interface: object.interface, version: object.version })
Expand Down Expand Up @@ -199,7 +210,6 @@ impl<D> Client<D> {

// Handle destruction if relevant
if message_desc.is_destructor {
self.map.remove(object_id.id.id);
if let Some(vec) = pending_destructors {
vec.push((object.data.user_data.clone(), self.id.clone(), object_id.id.clone()));
}
Expand Down Expand Up @@ -376,7 +386,7 @@ impl<D> Client<D> {
}
}

fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
pub(crate) fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
let object = self.map.find(id.id).ok_or(InvalidId)?;
if object.data.serial != id.serial {
return Err(InvalidId);
Expand Down
9 changes: 9 additions & 0 deletions wayland-backend/src/rs/server_impl/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ impl InnerHandle {
Ok(ObjectId { id: client.create_object(interface, version, data) })
}

pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
let mut state = self.state.lock().unwrap();
let state = (&mut *state as &mut dyn ErasedState)
.downcast_mut::<State<D>>()
.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 {
Expand Down
15 changes: 15 additions & 0 deletions wayland-backend/src/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,21 @@ impl Handle {
self.handle.create_object(client_id.id, interface, version, data)
}

/// Destroy an object
///
/// For most protocols, this is handled automatically when a destructor
/// message is sent or received.
///
/// This corresponds to `wl_resource_destroy` in the C API.
///
/// # Panics
///
/// This method will panic if the type parameter `D` is not same to the same type as the
/// one the backend was initialized with.
pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
self.handle.destroy_object::<D>(id)
}

/// Send an event to the client
///
/// Returns an error if the sender ID of the provided message is no longer valid.
Expand Down
14 changes: 14 additions & 0 deletions wayland-backend/src/sys/server_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,20 @@ impl InnerHandle {
Ok(ObjectId { id: unsafe { init_resource(resource, interface, Some(data)).0 } })
}

pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
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::<State<D>>()
.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);
});

Ok(())
}

pub fn null_id() -> ObjectId {
ObjectId {
id: InnerObjectId {
Expand Down

0 comments on commit 34b5cd4

Please sign in to comment.