wayland.shell.legacy: remove shell surface data

It is now redundant with the wl_surface user_data mechanism.
This commit is contained in:
Victor Berger 2019-04-23 23:08:24 +02:00 committed by Victor Berger
parent c604a48dce
commit 19432460cd
2 changed files with 37 additions and 65 deletions

View File

@ -36,17 +36,10 @@
//! use smithay::wayland::compositor::CompositorToken; //! use smithay::wayland::compositor::CompositorToken;
//! use smithay::wayland::shell::legacy::{wl_shell_init, ShellSurfaceRole, ShellRequest}; //! use smithay::wayland::shell::legacy::{wl_shell_init, ShellSurfaceRole, ShellRequest};
//! # use wayland_server::protocol::{wl_seat, wl_output}; //! # use wayland_server::protocol::{wl_seat, wl_output};
//! # #[derive(Default)] struct MySurfaceData;
//!
//! // define the metadata you want associated with the shell surfaces
//! #[derive(Default)]
//! pub struct MyShellSurfaceData {
//! /* ... */
//! }
//! //!
//! // define the roles type. You need to integrate the XdgSurface role: //! // define the roles type. You need to integrate the XdgSurface role:
//! define_roles!(MyRoles => //! define_roles!(MyRoles =>
//! [ShellSurface, ShellSurfaceRole<MyShellSurfaceData>] //! [ShellSurface, ShellSurfaceRole]
//! ); //! );
//! //!
//! # fn main() { //! # fn main() {
@ -62,7 +55,7 @@
//! // token from the compositor implementation //! // token from the compositor implementation
//! compositor_token, //! compositor_token,
//! // your implementation //! // your implementation
//! |event: ShellRequest<_, MyShellSurfaceData>| { /* ... */ }, //! |event: ShellRequest<_>| { /* ... */ },
//! None // put a logger if you want //! None // put a logger if you want
//! ); //! );
//! //!
@ -86,28 +79,24 @@ use wayland_server::{
mod wl_handlers; mod wl_handlers;
/// Metadata associated with the `wl_surface` role /// Metadata associated with the `wl_surface` role
pub struct ShellSurfaceRole<D: 'static> { pub struct ShellSurfaceRole {
/// Title of the surface /// Title of the surface
pub title: String, pub title: String,
/// Class of the surface /// Class of the surface
pub class: String, pub class: String,
pending_ping: u32, pending_ping: u32,
/// Some user data you may want to associate with the surface
pub user_data: D,
} }
/// A handle to a shell surface /// A handle to a shell surface
pub struct ShellSurface<R, D> { pub struct ShellSurface<R> {
wl_surface: wl_surface::WlSurface, wl_surface: wl_surface::WlSurface,
shell_surface: wl_shell_surface::WlShellSurface, shell_surface: wl_shell_surface::WlShellSurface,
token: CompositorToken<R>, token: CompositorToken<R>,
_d: ::std::marker::PhantomData<D>,
} }
impl<R, D> ShellSurface<R, D> impl<R> ShellSurface<R>
where where
R: Role<ShellSurfaceRole<D>> + 'static, R: Role<ShellSurfaceRole> + 'static,
D: 'static,
{ {
/// Is the shell surface referred by this handle still alive? /// Is the shell surface referred by this handle still alive?
pub fn alive(&self) -> bool { pub fn alive(&self) -> bool {
@ -168,16 +157,6 @@ where
pub fn send_popup_done(&self) { pub fn send_popup_done(&self) {
self.shell_surface.popup_done() self.shell_surface.popup_done()
} }
/// Access the user data you associated to this surface
pub fn with_user_data<F, T>(&self, f: F) -> Result<T, ()>
where
F: FnOnce(&mut D) -> T,
{
self.token
.with_role_data(&self.wl_surface, |data| f(&mut data.user_data))
.map_err(|_| ())
}
} }
/// Possible kinds of shell surface of the `wl_shell` protocol /// Possible kinds of shell surface of the `wl_shell` protocol
@ -234,13 +213,13 @@ pub enum ShellSurfaceKind {
} }
/// A request triggered by a `wl_shell_surface` /// A request triggered by a `wl_shell_surface`
pub enum ShellRequest<R, D> { pub enum ShellRequest<R> {
/// A new shell surface was created /// A new shell surface was created
/// ///
/// by default it has no kind and this should not be displayed /// by default it has no kind and this should not be displayed
NewShellSurface { NewShellSurface {
/// The created surface /// The created surface
surface: ShellSurface<R, D>, surface: ShellSurface<R>,
}, },
/// A pong event /// A pong event
/// ///
@ -248,14 +227,14 @@ pub enum ShellRequest<R, D> {
/// event, smithay has already checked that the responded serial was valid. /// event, smithay has already checked that the responded serial was valid.
Pong { Pong {
/// The surface that sent the pong /// The surface that sent the pong
surface: ShellSurface<R, D>, surface: ShellSurface<R>,
}, },
/// Start of an interactive move /// Start of an interactive move
/// ///
/// The surface requests that an interactive move is started on it /// The surface requests that an interactive move is started on it
Move { Move {
/// The surface requesting the move /// The surface requesting the move
surface: ShellSurface<R, D>, surface: ShellSurface<R>,
/// Serial of the implicit grab that initiated the move /// Serial of the implicit grab that initiated the move
serial: u32, serial: u32,
/// Seat associated with the move /// Seat associated with the move
@ -266,7 +245,7 @@ pub enum ShellRequest<R, D> {
/// The surface requests that an interactive resize is started on it /// The surface requests that an interactive resize is started on it
Resize { Resize {
/// The surface requesting the resize /// The surface requesting the resize
surface: ShellSurface<R, D>, surface: ShellSurface<R>,
/// Serial of the implicit grab that initiated the resize /// Serial of the implicit grab that initiated the resize
serial: u32, serial: u32,
/// Seat associated with the resize /// Seat associated with the resize
@ -277,7 +256,7 @@ pub enum ShellRequest<R, D> {
/// The surface changed its kind /// The surface changed its kind
SetKind { SetKind {
/// The surface /// The surface
surface: ShellSurface<R, D>, surface: ShellSurface<R>,
/// Its new kind /// Its new kind
kind: ShellSurfaceKind, kind: ShellSurfaceKind,
}, },
@ -287,14 +266,13 @@ pub enum ShellRequest<R, D> {
/// ///
/// This state allows you to retrieve a list of surfaces /// This state allows you to retrieve a list of surfaces
/// currently known to the shell global. /// currently known to the shell global.
pub struct ShellState<R, D> { pub struct ShellState<R> {
known_surfaces: Vec<ShellSurface<R, D>>, known_surfaces: Vec<ShellSurface<R>>,
} }
impl<R, D> ShellState<R, D> impl<R> ShellState<R>
where where
R: Role<ShellSurfaceRole<D>> + 'static, R: Role<ShellSurfaceRole> + 'static,
D: 'static,
{ {
/// Cleans the internal surface storage by removing all dead surfaces /// Cleans the internal surface storage by removing all dead surfaces
pub(crate) fn cleanup_surfaces(&mut self) { pub(crate) fn cleanup_surfaces(&mut self) {
@ -302,23 +280,22 @@ where
} }
/// Access all the shell surfaces known by this handler /// Access all the shell surfaces known by this handler
pub fn surfaces(&self) -> &[ShellSurface<R, D>] { pub fn surfaces(&self) -> &[ShellSurface<R>] {
&self.known_surfaces[..] &self.known_surfaces[..]
} }
} }
/// Create a new `wl_shell` global /// Create a new `wl_shell` global
pub fn wl_shell_init<R, D, L, Impl>( pub fn wl_shell_init<R, L, Impl>(
display: &mut Display, display: &mut Display,
ctoken: CompositorToken<R>, ctoken: CompositorToken<R>,
implementation: Impl, implementation: Impl,
logger: L, logger: L,
) -> (Arc<Mutex<ShellState<R, D>>>, Global<wl_shell::WlShell>) ) -> (Arc<Mutex<ShellState<R>>>, Global<wl_shell::WlShell>)
where where
D: Default + 'static, R: Role<ShellSurfaceRole> + 'static,
R: Role<ShellSurfaceRole<D>> + 'static,
L: Into<Option<::slog::Logger>>, L: Into<Option<::slog::Logger>>,
Impl: FnMut(ShellRequest<R, D>) + 'static, Impl: FnMut(ShellRequest<R>) + 'static,
{ {
let _log = crate::slog_or_stdlog(logger); let _log = crate::slog_or_stdlog(logger);

View File

@ -13,15 +13,14 @@ use crate::wayland::compositor::{roles::Role, CompositorToken};
use super::{ShellRequest, ShellState, ShellSurface, ShellSurfaceKind, ShellSurfaceRole}; use super::{ShellRequest, ShellState, ShellSurface, ShellSurfaceKind, ShellSurfaceRole};
pub(crate) fn implement_shell<R, D, Impl>( pub(crate) fn implement_shell<R, Impl>(
shell: NewResource<wl_shell::WlShell>, shell: NewResource<wl_shell::WlShell>,
ctoken: CompositorToken<R>, ctoken: CompositorToken<R>,
implementation: Rc<RefCell<Impl>>, implementation: Rc<RefCell<Impl>>,
state: Arc<Mutex<ShellState<R, D>>>, state: Arc<Mutex<ShellState<R>>>,
) where ) where
D: Default + 'static, R: Role<ShellSurfaceRole> + 'static,
R: Role<ShellSurfaceRole<D>> + 'static, Impl: FnMut(ShellRequest<R>) + 'static,
Impl: FnMut(ShellRequest<R, D>) + 'static,
{ {
shell.implement_closure( shell.implement_closure(
move |req, shell| { move |req, shell| {
@ -33,7 +32,6 @@ pub(crate) fn implement_shell<R, D, Impl>(
title: "".into(), title: "".into(),
class: "".into(), class: "".into(),
pending_ping: 0, pending_ping: 0,
user_data: Default::default(),
}; };
if ctoken.give_role_with(&surface, role_data).is_err() { if ctoken.give_role_with(&surface, role_data).is_err() {
shell shell
@ -58,49 +56,46 @@ pub(crate) fn implement_shell<R, D, Impl>(
); );
} }
fn make_handle<R, SD>( fn make_handle<R>(
shell_surface: &wl_shell_surface::WlShellSurface, shell_surface: &wl_shell_surface::WlShellSurface,
token: CompositorToken<R>, token: CompositorToken<R>,
) -> ShellSurface<R, SD> ) -> ShellSurface<R>
where where
R: Role<ShellSurfaceRole<SD>> + 'static, R: Role<ShellSurfaceRole> + 'static,
SD: 'static,
{ {
let data = shell_surface let data = shell_surface
.as_ref() .as_ref()
.user_data::<ShellSurfaceUserData<R, SD>>() .user_data::<ShellSurfaceUserData<R>>()
.unwrap(); .unwrap();
ShellSurface { ShellSurface {
wl_surface: data.surface.clone(), wl_surface: data.surface.clone(),
shell_surface: shell_surface.clone(), shell_surface: shell_surface.clone(),
token, token,
_d: ::std::marker::PhantomData,
} }
} }
pub(crate) struct ShellSurfaceUserData<R, SD> { pub(crate) struct ShellSurfaceUserData<R> {
surface: wl_surface::WlSurface, surface: wl_surface::WlSurface,
state: Arc<Mutex<ShellState<R, SD>>>, state: Arc<Mutex<ShellState<R>>>,
} }
fn implement_shell_surface<R, Impl, SD>( fn implement_shell_surface<R, Impl>(
shell_surface: NewResource<wl_shell_surface::WlShellSurface>, shell_surface: NewResource<wl_shell_surface::WlShellSurface>,
surface: wl_surface::WlSurface, surface: wl_surface::WlSurface,
implementation: Rc<RefCell<Impl>>, implementation: Rc<RefCell<Impl>>,
ctoken: CompositorToken<R>, ctoken: CompositorToken<R>,
state: Arc<Mutex<ShellState<R, SD>>>, state: Arc<Mutex<ShellState<R>>>,
) -> wl_shell_surface::WlShellSurface ) -> wl_shell_surface::WlShellSurface
where where
SD: 'static, R: Role<ShellSurfaceRole> + 'static,
R: Role<ShellSurfaceRole<SD>> + 'static, Impl: FnMut(ShellRequest<R>) + 'static,
Impl: FnMut(ShellRequest<R, SD>) + 'static,
{ {
use self::wl_shell_surface::Request; use self::wl_shell_surface::Request;
shell_surface.implement_closure( shell_surface.implement_closure(
move |req, shell_surface| { move |req, shell_surface| {
let data = shell_surface let data = shell_surface
.as_ref() .as_ref()
.user_data::<ShellSurfaceUserData<R, SD>>() .user_data::<ShellSurfaceUserData<R>>()
.unwrap(); .unwrap();
let mut user_impl = implementation.borrow_mut(); let mut user_impl = implementation.borrow_mut();
match req { match req {
@ -193,7 +188,7 @@ where
Some(|shell_surface: wl_shell_surface::WlShellSurface| { Some(|shell_surface: wl_shell_surface::WlShellSurface| {
let data = shell_surface let data = shell_surface
.as_ref() .as_ref()
.user_data::<ShellSurfaceUserData<R, SD>>() .user_data::<ShellSurfaceUserData<R>>()
.unwrap(); .unwrap();
data.state.lock().unwrap().cleanup_surfaces(); data.state.lock().unwrap().cleanup_surfaces();
}), }),