Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wl_fixes protocol #726

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Added a `destroy_object` method

### Bugfixes

- backend/rs: Prevent a potential deadlock during client cleanup
Expand Down
11 changes: 11 additions & 0 deletions wayland-backend/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ impl Backend {
self.backend.info(id)
}

/// Destroy an object
///
/// For most protocols, this is handled automatically when a destructor
/// message is sent or received.
///
/// This corresponds to `wl_proxy_destroy` in the C API. Or a `_destroy`
/// method generated for an object without a destructor request.
pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
self.backend.destroy_object(id)
}

/// Sends a request to the server
///
/// Returns an error if the sender ID of the provided message is no longer valid.
Expand Down
13 changes: 13 additions & 0 deletions wayland-backend/src/rs/client_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ impl InnerBackend {
ObjectId { id: InnerObjectId { serial: 0, id: 0, interface: &ANONYMOUS_INTERFACE } }
}

pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
let mut guard = self.state.lock_protocol();
let object = guard.get_object(id.id.clone())?;
guard
.map
.with(id.id.id, |obj| {
obj.data.client_destroyed = true;
})
.unwrap();
object.data.user_data.destroyed(id.clone());
Ok(())
}

pub fn send_request(
&self,
Message { sender_id: ObjectId { id }, opcode, args }: Message<ObjectId, RawFd>,
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 @@ -101,6 +101,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 @@ -201,7 +212,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 @@ -378,7 +388,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 @@ -165,6 +165,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 @@ -359,6 +359,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
63 changes: 38 additions & 25 deletions wayland-backend/src/sys/client_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,43 @@ impl InnerBackend {
}
}

fn destroy_object_inner(&self, guard: &mut MutexGuard<ConnectionState>, id: &ObjectId) {
if let Some(ref alive) = id.id.alive {
let udata = unsafe {
Box::from_raw(ffi_dispatch!(
wayland_client_handle(),
wl_proxy_get_user_data,
id.id.ptr
) as *mut ProxyUserData)
};
unsafe {
ffi_dispatch!(
wayland_client_handle(),
wl_proxy_set_user_data,
id.id.ptr,
std::ptr::null_mut()
);
}
alive.store(false, Ordering::Release);
udata.data.destroyed(id.clone());
}

guard.known_proxies.remove(&id.id.ptr);

unsafe {
ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, id.id.ptr);
}
}

pub fn destroy_object(&self, id: &ObjectId) -> Result<(), InvalidId> {
if !id.id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(false) {
return Err(InvalidId);
}

self.destroy_object_inner(&mut self.lock_state(), id);
Ok(())
}

pub fn send_request(
&self,
Message { sender_id: ObjectId { id }, opcode, args }: Message<ObjectId, RawFd>,
Expand Down Expand Up @@ -685,31 +722,7 @@ impl InnerBackend {
};

if message_desc.is_destructor {
if let Some(ref alive) = id.alive {
let udata = unsafe {
Box::from_raw(ffi_dispatch!(
wayland_client_handle(),
wl_proxy_get_user_data,
id.ptr
) as *mut ProxyUserData)
};
unsafe {
ffi_dispatch!(
wayland_client_handle(),
wl_proxy_set_user_data,
id.ptr,
std::ptr::null_mut()
);
}
alive.store(false, Ordering::Release);
udata.data.destroyed(ObjectId { id: id.clone() });
}

guard.known_proxies.remove(&id.ptr);

unsafe {
ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, id.ptr);
}
self.destroy_object_inner(&mut guard, &ObjectId { id })
}

Ok(child_id)
Expand Down
18 changes: 18 additions & 0 deletions wayland-backend/src/sys/server_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,24 @@ 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().");

if !id.id.alive.load(Ordering::Acquire) {
return Err(InvalidId);
}

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
55 changes: 51 additions & 4 deletions wayland-client/wayland.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,9 @@
x and y, combined with the new surface size define in which
directions the surface's size changes.

The exact semantics of wl_surface.offset are role-specific. Refer to
the documentation of specific roles for more information.

Surface location offset is double-buffered state, see
wl_surface.commit.

Expand Down Expand Up @@ -1880,7 +1883,7 @@
</event>
</interface>

<interface name="wl_seat" version="9">
<interface name="wl_seat" version="10">
<description summary="group of input devices">
A seat is a group of keyboards, pointer and touch devices. This
object is published as a global during start up, or when such a
Expand Down Expand Up @@ -2013,7 +2016,7 @@

</interface>

<interface name="wl_pointer" version="9">
<interface name="wl_pointer" version="10">
<description summary="pointer input device">
The wl_pointer interface represents one or more input devices,
such as mice, which control the pointer location and pointer_focus
Expand Down Expand Up @@ -2426,7 +2429,7 @@
</event>
</interface>

<interface name="wl_keyboard" version="9">
<interface name="wl_keyboard" version="10">
<description summary="keyboard input device">
The wl_keyboard interface represents one or more keyboards
associated with a seat.
Expand Down Expand Up @@ -2479,6 +2482,9 @@
the surface argument and the keys currently logically down to the keys
in the keys argument. The compositor must not send this event if the
wl_keyboard already had an active surface immediately before this event.

Clients should not use the list of pressed keys to emulate key-press
events. The order of keys in the list is unspecified.
</description>
<arg name="serial" type="uint" summary="serial number of the enter event"/>
<arg name="surface" type="object" interface="wl_surface" summary="surface gaining keyboard focus"/>
Expand All @@ -2505,9 +2511,18 @@
<enum name="key_state">
<description summary="physical key state">
Describes the physical state of a key that produced the key event.

Since version 10, the key can be in a "repeated" pseudo-state which
means the same as "pressed", but is used to signal repetition in the
key event.

The key may only enter the repeated state after entering the pressed
state and before entering the released state. This event may be
generated multiple times while the key is down.
</description>
<entry name="released" value="0" summary="key is not pressed"/>
<entry name="pressed" value="1" summary="key is pressed"/>
<entry name="repeated" value="2" summary="key was repeated" since="10"/>
</enum>

<event name="key">
Expand All @@ -2530,6 +2545,11 @@
compositor must not send this event if state is pressed (resp. released)
and the key was already logically down (resp. was not logically down)
immediately before this event.

Since version 10, compositors may send key events with the "repeated"
key state when a wl_keyboard.repeat_info event with a rate argument of
0 has been received. This allows the compositor to take over the
responsibility of key repetition.
</description>
<arg name="serial" type="uint" summary="serial number of the key event"/>
<arg name="time" type="uint" summary="timestamp with millisecond granularity"/>
Expand Down Expand Up @@ -2590,7 +2610,7 @@
</event>
</interface>

<interface name="wl_touch" version="9">
<interface name="wl_touch" version="10">
<description summary="touchscreen input device">
The wl_touch interface represents a touchscreen
associated with a seat.
Expand Down Expand Up @@ -3245,4 +3265,31 @@
</request>
</interface>

<interface name="wl_fixes" version="1">
<description summary="wayland protocol fixes">
This global fixes problems with other core-protocol interfaces that
cannot be fixed in these interfaces themselves.
</description>

<request name="destroy" type="destructor">
<description summary="destroys this object"/>
</request>

<request name="destroy_registry">
<description summary="destroy a wl_registry">
This request destroys a wl_registry object.

The client should no longer use the wl_registry after making this
request.

The compositor will emit a wl_display.delete_id event with the object ID
of the registry and will no longer emit any events on the registry. The
client should re-use the object ID once it receives the
wl_display.delete_id event.
</description>
<arg name="registry" type="object" interface="wl_registry"
summary="the registry to destroy"/>
</request>
</interface>

</protocol>
8 changes: 6 additions & 2 deletions wayland-scanner/src/server_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn generate_server_objects(protocol: &Protocol) -> TokenStream {
protocol
.interfaces
.iter()
.filter(|iface| iface.name != "wl_display" && iface.name != "wl_registry")
.filter(|iface| iface.name != "wl_display")
.map(generate_objects_for)
.collect()
}
Expand All @@ -39,7 +39,11 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
&interface.events,
);

let parse_body = crate::common::gen_parse_body(interface, Side::Server);
let parse_body = if interface.name == "wl_registry" {
quote! { unimplemented!("`wl_registry` is implemented internally in `wayland-server`") }
} else {
crate::common::gen_parse_body(interface, Side::Server)
};
let write_body = crate::common::gen_write_body(interface, Side::Server);
let methods = gen_methods(interface);

Expand Down
Loading
Loading