Skip to content

Commit 3b0339d

Browse files
committed
backend: Add destroy_object method
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`.
1 parent da5fde8 commit 3b0339d

File tree

8 files changed

+117
-25
lines changed

8 files changed

+117
-25
lines changed

wayland-backend/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
### Additions
6+
- backend: Added a `destroy_object` method
7+
58
## 0.3.3 -- 2024-01-29
69

710
### Additions

wayland-backend/src/client_api.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ impl Backend {
214214
self.backend.info(id)
215215
}
216216

217+
/// Destroy an object
218+
///
219+
/// For most protocols, this is handled automatically when a destructor
220+
/// message is sent or received.
221+
///
222+
/// This corresponds to `wl_proxy_destroy` in the C API. Or a `_destroy`
223+
/// method generated for an object without a destructor request.
224+
pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
225+
self.backend.destroy_object(id)
226+
}
227+
217228
/// Sends a request to the server
218229
///
219230
/// Returns an error if the sender ID of the provided message is no longer valid.

wayland-backend/src/rs/client_impl/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ impl InnerBackend {
298298
ObjectId { id: InnerObjectId { serial: 0, id: 0, interface: &ANONYMOUS_INTERFACE } }
299299
}
300300

301+
pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
302+
let mut guard = self.state.lock_protocol();
303+
let object = guard.get_object(id.id.clone())?;
304+
guard
305+
.map
306+
.with(id.id.id, |obj| {
307+
obj.data.client_destroyed = true;
308+
})
309+
.unwrap();
310+
object.data.user_data.destroyed(id.clone());
311+
Ok(())
312+
}
313+
301314
pub fn send_request(
302315
&self,
303316
Message { sender_id: ObjectId { id }, opcode, args }: Message<ObjectId, RawFd>,

wayland-backend/src/rs/server_impl/client.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ impl<D> Client<D> {
101101
InnerObjectId { id, serial, client_id: self.id.clone(), interface }
102102
}
103103

104+
pub(crate) fn destroy_object(
105+
&mut self,
106+
id: InnerObjectId,
107+
pending_destructors: &mut Vec<super::handle::PendingDestructor<D>>,
108+
) -> Result<(), InvalidId> {
109+
let object = self.get_object(id.clone())?;
110+
pending_destructors.push((object.data.user_data.clone(), self.id.clone(), id.clone()));
111+
self.send_delete_id(id.clone());
112+
Ok(())
113+
}
114+
104115
pub(crate) fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId> {
105116
let object = self.get_object(id.clone())?;
106117
Ok(ObjectInfo { id: id.id, interface: object.interface, version: object.version })
@@ -201,7 +212,6 @@ impl<D> Client<D> {
201212

202213
// Handle destruction if relevant
203214
if message_desc.is_destructor {
204-
self.map.remove(object_id.id.id);
205215
if let Some(vec) = pending_destructors {
206216
vec.push((object.data.user_data.clone(), self.id.clone(), object_id.id.clone()));
207217
}
@@ -378,7 +388,7 @@ impl<D> Client<D> {
378388
}
379389
}
380390

381-
fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
391+
pub(crate) fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
382392
let object = self.map.find(id.id).ok_or(InvalidId)?;
383393
if object.data.serial != id.serial {
384394
return Err(InvalidId);

wayland-backend/src/rs/server_impl/handle.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ impl InnerHandle {
164164
Ok(ObjectId { id: client.create_object(interface, version, data) })
165165
}
166166

167+
pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
168+
let mut state = self.state.lock().unwrap();
169+
let state = (&mut *state as &mut dyn ErasedState)
170+
.downcast_mut::<State<D>>()
171+
.expect("Wrong type parameter passed to Handle::destroy_object().");
172+
let client = state.clients.get_client_mut(id.id.client_id.clone())?;
173+
client.destroy_object(id.id.clone(), &mut state.pending_destructors)
174+
}
175+
167176
pub fn null_id() -> ObjectId {
168177
ObjectId {
169178
id: InnerObjectId {

wayland-backend/src/server_api.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,21 @@ impl Handle {
359359
self.handle.create_object(client_id.id, interface, version, data)
360360
}
361361

362+
/// Destroy an object
363+
///
364+
/// For most protocols, this is handled automatically when a destructor
365+
/// message is sent or received.
366+
///
367+
/// This corresponds to `wl_resource_destroy` in the C API.
368+
///
369+
/// # Panics
370+
///
371+
/// This method will panic if the type parameter `D` is not same to the same type as the
372+
/// one the backend was initialized with.
373+
pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
374+
self.handle.destroy_object::<D>(id)
375+
}
376+
362377
/// Send an event to the client
363378
///
364379
/// Returns an error if the sender ID of the provided message is no longer valid.

wayland-backend/src/sys/client_impl/mod.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,41 @@ impl InnerBackend {
515515
}
516516
}
517517

518+
fn destroy_object_inner(&self, guard: &mut MutexGuard<ConnectionState>, id: &ObjectId) {
519+
if let Some(ref alive) = id.id.alive {
520+
let udata = unsafe {
521+
Box::from_raw(ffi_dispatch!(
522+
wayland_client_handle(),
523+
wl_proxy_get_user_data,
524+
id.id.ptr
525+
) as *mut ProxyUserData)
526+
};
527+
unsafe {
528+
ffi_dispatch!(
529+
wayland_client_handle(),
530+
wl_proxy_set_user_data,
531+
id.id.ptr,
532+
std::ptr::null_mut()
533+
);
534+
}
535+
alive.store(false, Ordering::Release);
536+
udata.data.destroyed(id.clone());
537+
}
538+
guard.known_proxies.remove(&id.id.ptr);
539+
unsafe {
540+
ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, id.id.ptr);
541+
}
542+
}
543+
544+
pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
545+
if !id.id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(false) {
546+
return Err(InvalidId);
547+
}
548+
549+
self.destroy_object_inner(&mut self.lock_state(), id);
550+
Ok(())
551+
}
552+
518553
pub fn send_request(
519554
&self,
520555
Message { sender_id: ObjectId { id }, opcode, args }: Message<ObjectId, RawFd>,
@@ -731,29 +766,7 @@ impl InnerBackend {
731766
};
732767

733768
if message_desc.is_destructor {
734-
if let Some(ref alive) = id.alive {
735-
let udata = unsafe {
736-
Box::from_raw(ffi_dispatch!(
737-
wayland_client_handle(),
738-
wl_proxy_get_user_data,
739-
id.ptr
740-
) as *mut ProxyUserData)
741-
};
742-
unsafe {
743-
ffi_dispatch!(
744-
wayland_client_handle(),
745-
wl_proxy_set_user_data,
746-
id.ptr,
747-
std::ptr::null_mut()
748-
);
749-
}
750-
alive.store(false, Ordering::Release);
751-
udata.data.destroyed(ObjectId { id: id.clone() });
752-
}
753-
guard.known_proxies.remove(&id.ptr);
754-
unsafe {
755-
ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, id.ptr);
756-
}
769+
self.destroy_object_inner(&mut guard, &ObjectId { id })
757770
}
758771

759772
Ok(child_id)

wayland-backend/src/sys/server_impl/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,24 @@ impl InnerHandle {
564564
Ok(ObjectId { id: unsafe { init_resource(resource, interface, Some(data)).0 } })
565565
}
566566

567+
pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
568+
let mut state = self.state.lock().unwrap();
569+
// Keep this guard alive while the code is run to protect the C state
570+
let state = (&mut *state as &mut dyn ErasedState)
571+
.downcast_mut::<State<D>>()
572+
.expect("Wrong type parameter passed to Handle::destroy_object().");
573+
574+
if !id.id.alive.load(Ordering::Acquire) {
575+
return Err(InvalidId);
576+
}
577+
578+
PENDING_DESTRUCTORS.set(&(&mut state.pending_destructors as *mut _ as *mut _), || unsafe {
579+
ffi_dispatch!(wayland_server_handle(), wl_resource_destroy, id.id.ptr);
580+
});
581+
582+
Ok(())
583+
}
584+
567585
pub fn null_id() -> ObjectId {
568586
ObjectId {
569587
id: InnerObjectId {

0 commit comments

Comments
 (0)