wayland.compositor: DispatchData available in the callback

This commit is contained in:
Victor Berger 2021-05-30 23:31:14 +02:00 committed by Victor Berger
parent aaa6e625e9
commit 3f01f978f8
9 changed files with 33 additions and 28 deletions

View File

@ -36,7 +36,10 @@ use smithay::{
}, },
}; };
use crate::window_map::{Kind as SurfaceKind, WindowMap}; use crate::{
state::AnvilState,
window_map::{Kind as SurfaceKind, WindowMap},
};
#[cfg(feature = "xwayland")] #[cfg(feature = "xwayland")]
use crate::xwayland::X11SurfaceRole; use crate::xwayland::X11SurfaceRole;
@ -308,22 +311,18 @@ pub struct ShellHandles {
pub window_map: Rc<RefCell<MyWindowMap>>, pub window_map: Rc<RefCell<MyWindowMap>>,
} }
pub fn init_shell( pub fn init_shell<Backend: 'static>(
display: &mut Display, display: &mut Display,
#[cfg(feature = "egl")] egl_reader: Rc<RefCell<Option<EGLBufferReader>>>, #[cfg(feature = "egl")] egl_reader: Rc<RefCell<Option<EGLBufferReader>>>,
log: ::slog::Logger, log: ::slog::Logger,
) -> ShellHandles { ) -> ShellHandles {
// TODO: this is awkward...
let almost_window_map = Rc::new(RefCell::new(None::<Rc<RefCell<MyWindowMap>>>));
let almost_window_map_compositor = almost_window_map.clone();
// Create the compositor // Create the compositor
let (compositor_token, _, _) = compositor_init( let (compositor_token, _, _) = compositor_init(
display, display,
move |request, surface, ctoken| match request { move |request, surface, ctoken, mut ddata| match request {
SurfaceEvent::Commit => { SurfaceEvent::Commit => {
let window_map = almost_window_map_compositor.borrow(); let anvil_state = ddata.get::<AnvilState<Backend>>().unwrap();
let window_map = window_map.as_ref().unwrap(); let window_map = anvil_state.window_map.as_ref();
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
{ {
surface_commit(&surface, ctoken, egl_reader.borrow().as_ref(), &*window_map) surface_commit(&surface, ctoken, egl_reader.borrow().as_ref(), &*window_map)
@ -339,7 +338,6 @@ pub fn init_shell(
// Init a window map, to track the location of our windows // Init a window map, to track the location of our windows
let window_map = Rc::new(RefCell::new(WindowMap::new(compositor_token))); let window_map = Rc::new(RefCell::new(WindowMap::new(compositor_token)));
*almost_window_map.borrow_mut() = Some(window_map.clone());
// init the xdg_shell // init the xdg_shell
let xdg_window_map = window_map.clone(); let xdg_window_map = window_map.clone();

View File

@ -97,7 +97,7 @@ impl<Backend: Default + 'static> AnvilState<Backend> {
init_shm_global(&mut display.borrow_mut(), vec![], log.clone()); init_shm_global(&mut display.borrow_mut(), vec![], log.clone());
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
let shell_handles = init_shell(&mut display.borrow_mut(), egl_reader, log.clone()); let shell_handles = init_shell::<Backend>(&mut display.borrow_mut(), egl_reader, log.clone());
#[cfg(not(feature = "egl"))] #[cfg(not(feature = "egl"))]
let shell_handles = init_shell(&mut display.borrow_mut(), log.clone()); let shell_handles = init_shell(&mut display.borrow_mut(), log.clone());

View File

@ -2,7 +2,7 @@ use std::{cell::RefCell, ops::Deref as _, rc::Rc, sync::Mutex};
use wayland_server::{ use wayland_server::{
protocol::{wl_compositor, wl_region, wl_subcompositor, wl_subsurface, wl_surface}, protocol::{wl_compositor, wl_region, wl_subcompositor, wl_subsurface, wl_surface},
Filter, Main, DispatchData, Filter, Main,
}; };
use super::{ use super::{
@ -22,7 +22,7 @@ pub(crate) fn implement_compositor<R, Impl>(
) -> wl_compositor::WlCompositor ) -> wl_compositor::WlCompositor
where where
R: Default + Send + 'static, R: Default + Send + 'static,
Impl: FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>) + 'static, Impl: for<'a> FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>, DispatchData<'a>) + 'static,
{ {
compositor.quick_assign(move |_compositor, request, _| match request { compositor.quick_assign(move |_compositor, request, _| match request {
wl_compositor::Request::CreateSurface { id } => { wl_compositor::Request::CreateSurface { id } => {
@ -42,7 +42,8 @@ where
* wl_surface * wl_surface
*/ */
type SurfaceImplemFn<R> = dyn FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>); type SurfaceImplemFn<R> =
dyn for<'a> FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>, DispatchData<'a>);
// Internal implementation data of surfaces // Internal implementation data of surfaces
pub(crate) struct SurfaceImplem<R> { pub(crate) struct SurfaceImplem<R> {
@ -53,7 +54,8 @@ pub(crate) struct SurfaceImplem<R> {
impl<R> SurfaceImplem<R> { impl<R> SurfaceImplem<R> {
fn make<Impl>(log: ::slog::Logger, implem: Rc<RefCell<Impl>>) -> SurfaceImplem<R> fn make<Impl>(log: ::slog::Logger, implem: Rc<RefCell<Impl>>) -> SurfaceImplem<R>
where where
Impl: FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>) + 'static, Impl: for<'a> FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>, DispatchData<'a>)
+ 'static,
{ {
SurfaceImplem { log, implem } SurfaceImplem { log, implem }
} }
@ -63,7 +65,12 @@ impl<R> SurfaceImplem<R>
where where
R: 'static, R: 'static,
{ {
fn receive_surface_request(&mut self, req: wl_surface::Request, surface: wl_surface::WlSurface) { fn receive_surface_request<'a>(
&mut self,
req: wl_surface::Request,
surface: wl_surface::WlSurface,
ddata: DispatchData<'a>,
) {
match req { match req {
wl_surface::Request::Attach { buffer, x, y } => { wl_surface::Request::Attach { buffer, x, y } => {
SurfaceData::<R>::with_data(&surface, |d| { SurfaceData::<R>::with_data(&surface, |d| {
@ -103,7 +110,7 @@ where
wl_surface::Request::Commit => { wl_surface::Request::Commit => {
let mut user_impl = self.implem.borrow_mut(); let mut user_impl = self.implem.borrow_mut();
trace!(self.log, "Calling user implementation for wl_surface.commit"); trace!(self.log, "Calling user implementation for wl_surface.commit");
(&mut *user_impl)(SurfaceEvent::Commit, surface, CompositorToken::make()); (&mut *user_impl)(SurfaceEvent::Commit, surface, CompositorToken::make(), ddata);
} }
wl_surface::Request::SetBufferTransform { transform } => { wl_surface::Request::SetBufferTransform { transform } => {
SurfaceData::<R>::with_data(&surface, |d| d.buffer_transform = transform); SurfaceData::<R>::with_data(&surface, |d| d.buffer_transform = transform);
@ -131,11 +138,11 @@ fn implement_surface<R, Impl>(
) -> wl_surface::WlSurface ) -> wl_surface::WlSurface
where where
R: Default + Send + 'static, R: Default + Send + 'static,
Impl: FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>) + 'static, Impl: for<'a> FnMut(SurfaceEvent, wl_surface::WlSurface, CompositorToken<R>, DispatchData<'a>) + 'static,
{ {
surface.quick_assign({ surface.quick_assign({
let mut implem = SurfaceImplem::make(log, implem); let mut implem = SurfaceImplem::make(log, implem);
move |surface, req, _| implem.receive_surface_request(req, surface.deref().clone()) move |surface, req, ddata| implem.receive_surface_request(req, surface.deref().clone(), ddata)
}); });
surface.assign_destructor(Filter::new(|surface, _, _| SurfaceData::<R>::cleanup(&surface))); surface.assign_destructor(Filter::new(|surface, _, _| SurfaceData::<R>::cleanup(&surface)));
surface.as_ref().user_data().set_threadsafe(SurfaceData::<R>::new); surface.as_ref().user_data().set_threadsafe(SurfaceData::<R>::new);

View File

@ -39,7 +39,7 @@
//! // Call the init function: //! // Call the init function:
//! let (token, _, _) = compositor_init::<MyRoles, _, _>( //! let (token, _, _) = compositor_init::<MyRoles, _, _>(
//! &mut display, //! &mut display,
//! |request, surface, compositor_token| { //! |request, surface, compositor_token, dispatch_data| {
//! /* //! /*
//! Your handling of the user requests. //! Your handling of the user requests.
//! */ //! */
@ -84,7 +84,7 @@ use wayland_server::{
protocol::{ protocol::{
wl_buffer, wl_callback, wl_compositor, wl_output, wl_region, wl_subcompositor, wl_surface::WlSurface, wl_buffer, wl_callback, wl_compositor, wl_output, wl_region, wl_subcompositor, wl_surface::WlSurface,
}, },
Display, Filter, Global, UserDataMap, DispatchData, Display, Filter, Global, UserDataMap,
}; };
/// Description of a part of a surface that /// Description of a part of a surface that
@ -514,7 +514,7 @@ pub fn compositor_init<R, Impl, L>(
where where
L: Into<Option<::slog::Logger>>, L: Into<Option<::slog::Logger>>,
R: Default + RoleType + Role<SubsurfaceRole> + Send + 'static, R: Default + RoleType + Role<SubsurfaceRole> + Send + 'static,
Impl: FnMut(SurfaceEvent, WlSurface, CompositorToken<R>) + 'static, Impl: for<'a> FnMut(SurfaceEvent, WlSurface, CompositorToken<R>, DispatchData<'a>) + 'static,
{ {
let log = crate::slog_or_fallback(logger).new(o!("smithay_module" => "compositor_handler")); let log = crate::slog_or_fallback(logger).new(o!("smithay_module" => "compositor_handler"));
let implem = Rc::new(RefCell::new(implem)); let implem = Rc::new(RefCell::new(implem));

View File

@ -41,7 +41,7 @@
//! define_roles!(Roles => [DnDIcon, DnDIconRole]); //! define_roles!(Roles => [DnDIcon, DnDIconRole]);
//! //!
//! # let mut display = wayland_server::Display::new(); //! # let mut display = wayland_server::Display::new();
//! # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _| {}, None); //! # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _, _| {}, None);
//! // init the data device: //! // init the data device:
//! init_data_device( //! init_data_device(
//! &mut display, // the display //! &mut display, // the display

View File

@ -31,7 +31,7 @@
//! # let mut display = wayland_server::Display::new(); //! # let mut display = wayland_server::Display::new();
//! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>( //! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>(
//! # &mut display, //! # &mut display,
//! # |_, _, _| {}, //! # |_, _, _, _| {},
//! # None //! # None
//! # ); //! # );
//! init_explicit_synchronization_global( //! init_explicit_synchronization_global(

View File

@ -18,7 +18,7 @@
//! define_roles!(Roles => [CursorImage, CursorImageRole]); //! define_roles!(Roles => [CursorImage, CursorImageRole]);
//! //!
//! # let mut display = wayland_server::Display::new(); //! # let mut display = wayland_server::Display::new();
//! # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _| {}, None); //! # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _, _| {}, None);
//! // insert the seat: //! // insert the seat:
//! let (seat, seat_global) = Seat::new( //! let (seat, seat_global) = Seat::new(
//! &mut display, // the display //! &mut display, // the display
@ -204,7 +204,7 @@ impl Seat {
/// # define_roles!(Roles => [CursorImage, CursorImageRole]); /// # define_roles!(Roles => [CursorImage, CursorImageRole]);
/// # /// #
/// # let mut display = wayland_server::Display::new(); /// # let mut display = wayland_server::Display::new();
/// # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _| {}, None); /// # let (compositor_token, _, _) = compositor_init::<Roles, _, _>(&mut display, |_, _, _, _| {}, None);
/// # let (mut seat, seat_global) = Seat::new( /// # let (mut seat, seat_global) = Seat::new(
/// # &mut display, /// # &mut display,
/// # "seat-0".into(), /// # "seat-0".into(),

View File

@ -45,7 +45,7 @@
//! # let mut display = wayland_server::Display::new(); //! # let mut display = wayland_server::Display::new();
//! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>( //! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>(
//! # &mut display, //! # &mut display,
//! # |_, _, _| {}, //! # |_, _, _, _| {},
//! # None //! # None
//! # ); //! # );
//! let (shell_state, _) = wl_shell_init( //! let (shell_state, _) = wl_shell_init(

View File

@ -50,7 +50,7 @@
//! # let mut display = wayland_server::Display::new(); //! # let mut display = wayland_server::Display::new();
//! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>( //! # let (compositor_token, _, _) = smithay::wayland::compositor::compositor_init::<MyRoles, _, _>(
//! # &mut display, //! # &mut display,
//! # |_, _, _| {}, //! # |_, _, _, _| {},
//! # None //! # None
//! # ); //! # );
//! let (shell_state, _, _) = xdg_shell_init( //! let (shell_state, _, _) = xdg_shell_init(