anvil: Pass window and output maps using dispatch data AnvilState

This commit is contained in:
i509VCB 2021-09-02 17:34:55 -05:00 committed by Victor Berger
parent 60fdbaebc5
commit fdaf1859b0
2 changed files with 366 additions and 340 deletions

View File

@ -289,8 +289,6 @@ impl PointerGrab for ResizeSurfaceGrab {
pub struct ShellHandles { pub struct ShellHandles {
pub xdg_state: Arc<Mutex<XdgShellState>>, pub xdg_state: Arc<Mutex<XdgShellState>>,
pub wl_state: Arc<Mutex<WlShellState>>, pub wl_state: Arc<Mutex<WlShellState>>,
pub window_map: Rc<RefCell<WindowMap>>,
pub output_map: Rc<RefCell<OutputMap>>,
} }
fn fullscreen_output_geometry( fn fullscreen_output_geometry(
@ -336,364 +334,380 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
log.clone(), log.clone(),
); );
// Init a window map, to track the location of our windows
let window_map = Rc::new(RefCell::new(WindowMap::default()));
let output_map = Rc::new(RefCell::new(OutputMap::new(
display.clone(),
window_map.clone(),
log.clone(),
)));
// init the xdg_shell // init the xdg_shell
let xdg_window_map = window_map.clone();
let xdg_output_map = output_map.clone();
let (xdg_shell_state, _, _) = xdg_shell_init( let (xdg_shell_state, _, _) = xdg_shell_init(
&mut *display.borrow_mut(), &mut *display.borrow_mut(),
move |shell_event, _dispatch_data| match shell_event { move |shell_event, mut ddata| {
XdgRequest::NewToplevel { surface } => { let state = ddata.get::<AnvilState<BackendData>>().unwrap();
// place the window at a random location on the primary output
// or if there is not output in a [0;800]x[0;800] square
use rand::distributions::{Distribution, Uniform};
let output_geometry = xdg_output_map match shell_event {
.borrow() XdgRequest::NewToplevel { surface } => {
.with_primary() // place the window at a random location on the primary output
.map(|o| o.geometry()) // or if there is not output in a [0;800]x[0;800] square
.unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800))); use rand::distributions::{Distribution, Uniform};
let max_x = output_geometry.loc.x + (((output_geometry.size.w as f32) / 3.0) * 2.0) as i32;
let max_y = output_geometry.loc.y + (((output_geometry.size.h as f32) / 3.0) * 2.0) as i32;
let x_range = Uniform::new(output_geometry.loc.x, max_x);
let y_range = Uniform::new(output_geometry.loc.y, max_y);
let mut rng = rand::thread_rng();
let x = x_range.sample(&mut rng);
let y = y_range.sample(&mut rng);
// Do not send a configure here, the initial configure
// of a xdg_surface has to be sent during the commit if
// the surface is not already configured
xdg_window_map
.borrow_mut()
.insert(SurfaceKind::Xdg(surface), (x, y).into());
}
XdgRequest::NewPopup { surface, positioner } => {
// Do not send a configure here, the initial configure
// of a xdg_surface has to be sent during the commit if
// the surface is not already configured
// TODO: properly recompute the geometry with the whole of positioner state let output_geometry = state
surface .output_map
.with_pending_state(|state| { .borrow()
// NOTE: This is not really necessary as the default geometry .with_primary()
// is already set the same way, but for demonstrating how .map(|o| o.geometry())
// to set the initial popup geometry this code is left as .unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800)));
// an example
state.geometry = positioner.get_geometry();
})
.unwrap();
xdg_window_map.borrow_mut().insert_popup(PopupKind::Xdg(surface));
}
XdgRequest::RePosition {
surface,
positioner,
token,
} => {
let result = surface.with_pending_state(|state| {
// NOTE: This is again a simplification, a proper compositor would
// calculate the geometry of the popup here. For simplicity we just
// use the default implementation here that does not take the
// window position and output constraints into account.
let geometry = positioner.get_geometry();
state.geometry = geometry;
state.positioner = positioner;
});
if result.is_ok() { let max_x =
surface.send_repositioned(token); output_geometry.loc.x + (((output_geometry.size.w as f32) / 3.0) * 2.0) as i32;
} let max_y =
} output_geometry.loc.y + (((output_geometry.size.h as f32) / 3.0) * 2.0) as i32;
XdgRequest::Move { let x_range = Uniform::new(output_geometry.loc.x, max_x);
surface, let y_range = Uniform::new(output_geometry.loc.y, max_y);
seat, let mut rng = rand::thread_rng();
serial, let x = x_range.sample(&mut rng);
} => { let y = y_range.sample(&mut rng);
let seat = Seat::from_resource(&seat).unwrap(); // Do not send a configure here, the initial configure
// TODO: touch move. // of a xdg_surface has to be sent during the commit if
let pointer = seat.get_pointer().unwrap(); // the surface is not already configured
state
// Check that this surface has a click grab. .window_map
if !pointer.has_grab(serial) {
return;
}
let start_data = pointer.grab_start_data().unwrap();
// If the focus was for a different surface, ignore the request.
if start_data.focus.is_none()
|| !start_data
.focus
.as_ref()
.unwrap()
.0
.as_ref()
.same_client_as(surface.get_surface().unwrap().as_ref())
{
return;
}
let toplevel = SurfaceKind::Xdg(surface.clone());
let mut initial_window_location = xdg_window_map.borrow().location(&toplevel).unwrap();
// If surface is maximized then unmaximize it
if let Some(current_state) = surface.current_state() {
if current_state.states.contains(xdg_toplevel::State::Maximized) {
let fs_changed = surface.with_pending_state(|state| {
state.states.unset(xdg_toplevel::State::Maximized);
state.size = None;
});
if fs_changed.is_ok() {
surface.send_configure();
// NOTE: In real compositor mouse location should be mapped to a new window size
// For example, you could:
// 1) transform mouse pointer position from compositor space to window space (location relative)
// 2) divide the x coordinate by width of the window to get the percentage
// - 0.0 would be on the far left of the window
// - 0.5 would be in middle of the window
// - 1.0 would be on the far right of the window
// 3) multiply the percentage by new window width
// 4) by doing that, drag will look a lot more natural
//
// but for anvil needs setting location to pointer location is fine
let pos = pointer.current_location();
initial_window_location = (pos.x as i32, pos.y as i32).into();
}
}
}
let grab = MoveSurfaceGrab {
start_data,
window_map: xdg_window_map.clone(),
toplevel,
initial_window_location,
};
pointer.set_grab(grab, serial);
}
XdgRequest::Resize {
surface,
seat,
serial,
edges,
} => {
let seat = Seat::from_resource(&seat).unwrap();
// TODO: touch resize.
let pointer = seat.get_pointer().unwrap();
// Check that this surface has a click grab.
if !pointer.has_grab(serial) {
return;
}
let start_data = pointer.grab_start_data().unwrap();
// If the focus was for a different surface, ignore the request.
if start_data.focus.is_none()
|| !start_data
.focus
.as_ref()
.unwrap()
.0
.as_ref()
.same_client_as(surface.get_surface().unwrap().as_ref())
{
return;
}
let toplevel = SurfaceKind::Xdg(surface.clone());
let initial_window_location = xdg_window_map.borrow().location(&toplevel).unwrap();
let geometry = xdg_window_map.borrow().geometry(&toplevel).unwrap();
let initial_window_size = geometry.size;
with_states(surface.get_surface().unwrap(), move |states| {
states
.data_map
.get::<RefCell<SurfaceData>>()
.unwrap()
.borrow_mut() .borrow_mut()
.resize_state = ResizeState::Resizing(ResizeData { .insert(SurfaceKind::Xdg(surface), (x, y).into());
edges: edges.into(), }
initial_window_location,
initial_window_size,
});
})
.unwrap();
let grab = ResizeSurfaceGrab { XdgRequest::NewPopup { surface, positioner } => {
start_data, // Do not send a configure here, the initial configure
toplevel, // of a xdg_surface has to be sent during the commit if
edges: edges.into(), // the surface is not already configured
initial_window_size,
last_window_size: initial_window_size,
};
pointer.set_grab(grab, serial); // TODO: properly recompute the geometry with the whole of positioner state
} surface
XdgRequest::AckConfigure { .with_pending_state(|state| {
surface, // NOTE: This is not really necessary as the default geometry
configure: Configure::Toplevel(configure), // is already set the same way, but for demonstrating how
.. // to set the initial popup geometry this code is left as
} => { // an example
let waiting_for_serial = with_states(&surface, |states| { state.geometry = positioner.get_geometry();
if let Some(data) = states.data_map.get::<RefCell<SurfaceData>>() {
if let ResizeState::WaitingForFinalAck(_, serial) = data.borrow().resize_state {
return Some(serial);
}
}
None
})
.unwrap();
if let Some(serial) = waiting_for_serial {
// When the resize grab is released the surface
// resize state will be set to WaitingForFinalAck
// and the client will receive a configure request
// without the resize state to inform the client
// resizing has finished. Here we will wait for
// the client to acknowledge the end of the
// resizing. To check if the surface was resizing
// before sending the configure we need to use
// the current state as the received acknowledge
// will no longer have the resize state set
let is_resizing = with_states(&surface, |states| {
states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap()
.current
.states
.contains(xdg_toplevel::State::Resizing)
})
.unwrap();
if configure.serial >= serial && is_resizing {
with_states(&surface, |states| {
let mut data = states
.data_map
.get::<RefCell<SurfaceData>>()
.unwrap()
.borrow_mut();
if let ResizeState::WaitingForFinalAck(resize_data, _) = data.resize_state {
data.resize_state = ResizeState::WaitingForCommit(resize_data);
} else {
unreachable!()
}
}) })
.unwrap(); .unwrap();
state
.window_map
.borrow_mut()
.insert_popup(PopupKind::Xdg(surface));
}
XdgRequest::RePosition {
surface,
positioner,
token,
} => {
let result = surface.with_pending_state(|state| {
// NOTE: This is again a simplification, a proper compositor would
// calculate the geometry of the popup here. For simplicity we just
// use the default implementation here that does not take the
// window position and output constraints into account.
let geometry = positioner.get_geometry();
state.geometry = geometry;
state.positioner = positioner;
});
if result.is_ok() {
surface.send_repositioned(token);
} }
} }
}
XdgRequest::Fullscreen { surface, output, .. } => {
// NOTE: This is only one part of the solution. We can set the
// location and configure size here, but the surface should be rendered fullscreen
// independently from its buffer size
let wl_surface = if let Some(surface) = surface.get_surface() {
surface
} else {
// If there is no underlying surface just ignore the request
return;
};
let output_geometry = fullscreen_output_geometry( XdgRequest::Move {
wl_surface, surface,
output.as_ref(), seat,
&xdg_window_map.borrow(), serial,
&xdg_output_map.borrow(), } => {
); let seat = Seat::from_resource(&seat).unwrap();
// TODO: touch move.
let pointer = seat.get_pointer().unwrap();
if let Some(geometry) = output_geometry { // Check that this surface has a click grab.
if let Some(surface) = surface.get_surface() { if !pointer.has_grab(serial) {
let mut xdg_window_map = xdg_window_map.borrow_mut(); return;
if let Some(kind) = xdg_window_map.find(surface) { }
xdg_window_map.set_location(&kind, geometry.loc);
let start_data = pointer.grab_start_data().unwrap();
// If the focus was for a different surface, ignore the request.
if start_data.focus.is_none()
|| !start_data
.focus
.as_ref()
.unwrap()
.0
.as_ref()
.same_client_as(surface.get_surface().unwrap().as_ref())
{
return;
}
let toplevel = SurfaceKind::Xdg(surface.clone());
let mut initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
// If surface is maximized then unmaximize it
if let Some(current_state) = surface.current_state() {
if current_state.states.contains(xdg_toplevel::State::Maximized) {
let fs_changed = surface.with_pending_state(|state| {
state.states.unset(xdg_toplevel::State::Maximized);
state.size = None;
});
if fs_changed.is_ok() {
surface.send_configure();
// NOTE: In real compositor mouse location should be mapped to a new window size
// For example, you could:
// 1) transform mouse pointer position from compositor space to window space (location relative)
// 2) divide the x coordinate by width of the window to get the percentage
// - 0.0 would be on the far left of the window
// - 0.5 would be in middle of the window
// - 1.0 would be on the far right of the window
// 3) multiply the percentage by new window width
// 4) by doing that, drag will look a lot more natural
//
// but for anvil needs setting location to pointer location is fine
let pos = pointer.current_location();
initial_window_location = (pos.x as i32, pos.y as i32).into();
}
} }
} }
let ret = surface.with_pending_state(|state| { let grab = MoveSurfaceGrab {
state.states.set(xdg_toplevel::State::Fullscreen); start_data,
state.size = Some(geometry.size); window_map: state.window_map.clone(),
state.fullscreen_output = output; toplevel,
}); initial_window_location,
if ret.is_ok() { };
surface.send_configure();
pointer.set_grab(grab, serial);
}
XdgRequest::Resize {
surface,
seat,
serial,
edges,
} => {
let seat = Seat::from_resource(&seat).unwrap();
// TODO: touch resize.
let pointer = seat.get_pointer().unwrap();
// Check that this surface has a click grab.
if !pointer.has_grab(serial) {
return;
} }
let start_data = pointer.grab_start_data().unwrap();
// If the focus was for a different surface, ignore the request.
if start_data.focus.is_none()
|| !start_data
.focus
.as_ref()
.unwrap()
.0
.as_ref()
.same_client_as(surface.get_surface().unwrap().as_ref())
{
return;
}
let toplevel = SurfaceKind::Xdg(surface.clone());
let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
let geometry = state.window_map.borrow().geometry(&toplevel).unwrap();
let initial_window_size = geometry.size;
with_states(surface.get_surface().unwrap(), move |states| {
states
.data_map
.get::<RefCell<SurfaceData>>()
.unwrap()
.borrow_mut()
.resize_state = ResizeState::Resizing(ResizeData {
edges: edges.into(),
initial_window_location,
initial_window_size,
});
})
.unwrap();
let grab = ResizeSurfaceGrab {
start_data,
toplevel,
edges: edges.into(),
initial_window_size,
last_window_size: initial_window_size,
};
pointer.set_grab(grab, serial);
} }
}
XdgRequest::UnFullscreen { surface } => { XdgRequest::AckConfigure {
let ret = surface.with_pending_state(|state| { surface,
state.states.unset(xdg_toplevel::State::Fullscreen); configure: Configure::Toplevel(configure),
state.size = None; ..
state.fullscreen_output = None; } => {
}); let waiting_for_serial = with_states(&surface, |states| {
if ret.is_ok() { if let Some(data) = states.data_map.get::<RefCell<SurfaceData>>() {
surface.send_configure(); if let ResizeState::WaitingForFinalAck(_, serial) = data.borrow().resize_state {
} return Some(serial);
} }
XdgRequest::Maximize { surface } => { }
// NOTE: This should use layer-shell when it is implemented to
// get the correct maximum size None
let output_geometry = { })
let xdg_window_map = xdg_window_map.borrow(); .unwrap();
surface
.get_surface() if let Some(serial) = waiting_for_serial {
.and_then(|s| xdg_window_map.find(s)) // When the resize grab is released the surface
.and_then(|k| xdg_window_map.location(&k)) // resize state will be set to WaitingForFinalAck
.and_then(|position| { // and the client will receive a configure request
xdg_output_map // without the resize state to inform the client
.borrow() // resizing has finished. Here we will wait for
.find_by_position(position) // the client to acknowledge the end of the
.map(|o| o.geometry()) // resizing. To check if the surface was resizing
// before sending the configure we need to use
// the current state as the received acknowledge
// will no longer have the resize state set
let is_resizing = with_states(&surface, |states| {
states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap()
.current
.states
.contains(xdg_toplevel::State::Resizing)
}) })
}; .unwrap();
if let Some(geometry) = output_geometry { if configure.serial >= serial && is_resizing {
if let Some(surface) = surface.get_surface() { with_states(&surface, |states| {
let mut xdg_window_map = xdg_window_map.borrow_mut(); let mut data = states
if let Some(kind) = xdg_window_map.find(surface) { .data_map
xdg_window_map.set_location(&kind, geometry.loc); .get::<RefCell<SurfaceData>>()
.unwrap()
.borrow_mut();
if let ResizeState::WaitingForFinalAck(resize_data, _) = data.resize_state {
data.resize_state = ResizeState::WaitingForCommit(resize_data);
} else {
unreachable!()
}
})
.unwrap();
} }
} }
}
XdgRequest::Fullscreen { surface, output, .. } => {
// NOTE: This is only one part of the solution. We can set the
// location and configure size here, but the surface should be rendered fullscreen
// independently from its buffer size
let wl_surface = if let Some(surface) = surface.get_surface() {
surface
} else {
// If there is no underlying surface just ignore the request
return;
};
let output_geometry = fullscreen_output_geometry(
wl_surface,
output.as_ref(),
&state.window_map.borrow(),
&state.output_map.borrow(),
);
if let Some(geometry) = output_geometry {
if let Some(surface) = surface.get_surface() {
let mut xdg_window_map = state.window_map.borrow_mut();
if let Some(kind) = xdg_window_map.find(surface) {
xdg_window_map.set_location(&kind, geometry.loc);
}
}
let ret = surface.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Fullscreen);
state.size = Some(geometry.size);
state.fullscreen_output = output;
});
if ret.is_ok() {
surface.send_configure();
}
}
}
XdgRequest::UnFullscreen { surface } => {
let ret = surface.with_pending_state(|state| { let ret = surface.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Maximized); state.states.unset(xdg_toplevel::State::Fullscreen);
state.size = Some(geometry.size); state.size = None;
state.fullscreen_output = None;
}); });
if ret.is_ok() { if ret.is_ok() {
surface.send_configure(); surface.send_configure();
} }
} }
}
XdgRequest::UnMaximize { surface } => { XdgRequest::Maximize { surface } => {
let ret = surface.with_pending_state(|state| { // NOTE: This should use layer-shell when it is implemented to
state.states.unset(xdg_toplevel::State::Maximized); // get the correct maximum size
state.size = None; let output_geometry = {
}); let xdg_window_map = state.window_map.borrow();
if ret.is_ok() { surface
surface.send_configure(); .get_surface()
.and_then(|s| xdg_window_map.find(s))
.and_then(|k| xdg_window_map.location(&k))
.and_then(|position| {
state
.output_map
.borrow()
.find_by_position(position)
.map(|o| o.geometry())
})
};
if let Some(geometry) = output_geometry {
if let Some(surface) = surface.get_surface() {
let mut xdg_window_map = state.window_map.borrow_mut();
if let Some(kind) = xdg_window_map.find(surface) {
xdg_window_map.set_location(&kind, geometry.loc);
}
}
let ret = surface.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Maximized);
state.size = Some(geometry.size);
});
if ret.is_ok() {
surface.send_configure();
}
}
} }
XdgRequest::UnMaximize { surface } => {
let ret = surface.with_pending_state(|state| {
state.states.unset(xdg_toplevel::State::Maximized);
state.size = None;
});
if ret.is_ok() {
surface.send_configure();
}
}
_ => (),
} }
_ => (),
}, },
log.clone(), log.clone(),
); );
// init the wl_shell // init the wl_shell
let shell_window_map = window_map.clone();
let shell_output_map = output_map.clone();
let (wl_shell_state, _) = wl_shell_init( let (wl_shell_state, _) = wl_shell_init(
&mut *display.borrow_mut(), &mut *display.borrow_mut(),
move |req: ShellRequest, _dispatch_data| { move |req: ShellRequest, mut ddata| {
let state = ddata.get::<AnvilState<BackendData>>().unwrap();
match req { match req {
ShellRequest::SetKind { ShellRequest::SetKind {
surface, surface,
@ -703,7 +717,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
// or if there is not output in a [0;800]x[0;800] square // or if there is not output in a [0;800]x[0;800] square
use rand::distributions::{Distribution, Uniform}; use rand::distributions::{Distribution, Uniform};
let output_geometry = shell_output_map let output_geometry = state
.output_map
.borrow() .borrow()
.with_primary() .with_primary()
.map(|o| o.geometry()) .map(|o| o.geometry())
@ -717,10 +732,12 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let x = x_range.sample(&mut rng); let x = x_range.sample(&mut rng);
let y = y_range.sample(&mut rng); let y = y_range.sample(&mut rng);
shell_window_map state
.window_map
.borrow_mut() .borrow_mut()
.insert(SurfaceKind::Wl(surface), (x, y).into()); .insert(SurfaceKind::Wl(surface), (x, y).into());
} }
ShellRequest::SetKind { ShellRequest::SetKind {
surface, surface,
kind: ShellSurfaceKind::Fullscreen { output, .. }, kind: ShellSurfaceKind::Fullscreen { output, .. },
@ -738,16 +755,18 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
let output_geometry = fullscreen_output_geometry( let output_geometry = fullscreen_output_geometry(
wl_surface, wl_surface,
output.as_ref(), output.as_ref(),
&shell_window_map.borrow(), &state.window_map.borrow(),
&shell_output_map.borrow(), &state.output_map.borrow(),
); );
if let Some(geometry) = output_geometry { if let Some(geometry) = output_geometry {
shell_window_map state
.window_map
.borrow_mut() .borrow_mut()
.insert(SurfaceKind::Wl(surface), geometry.loc); .insert(SurfaceKind::Wl(surface), geometry.loc);
} }
} }
ShellRequest::Move { ShellRequest::Move {
surface, surface,
seat, seat,
@ -778,17 +797,18 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
let toplevel = SurfaceKind::Wl(surface); let toplevel = SurfaceKind::Wl(surface);
let initial_window_location = shell_window_map.borrow().location(&toplevel).unwrap(); let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
let grab = MoveSurfaceGrab { let grab = MoveSurfaceGrab {
start_data, start_data,
window_map: shell_window_map.clone(), window_map: state.window_map.clone(),
toplevel, toplevel,
initial_window_location, initial_window_location,
}; };
pointer.set_grab(grab, serial); pointer.set_grab(grab, serial);
} }
ShellRequest::Resize { ShellRequest::Resize {
surface, surface,
seat, seat,
@ -820,8 +840,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
let toplevel = SurfaceKind::Wl(surface.clone()); let toplevel = SurfaceKind::Wl(surface.clone());
let initial_window_location = shell_window_map.borrow().location(&toplevel).unwrap(); let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
let geometry = shell_window_map.borrow().geometry(&toplevel).unwrap(); let geometry = state.window_map.borrow().geometry(&toplevel).unwrap();
let initial_window_size = geometry.size; let initial_window_size = geometry.size;
with_states(surface.get_surface().unwrap(), move |states| { with_states(surface.get_surface().unwrap(), move |states| {
@ -854,8 +874,6 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
log.clone(), log.clone(),
); );
let layer_window_map = window_map.clone();
let layer_output_map = output_map.clone();
smithay::wayland::shell::wlr_layer::wlr_layer_shell_init( smithay::wayland::shell::wlr_layer::wlr_layer_shell_init(
&mut *display.borrow_mut(), &mut *display.borrow_mut(),
move |event, mut ddata| match event { move |event, mut ddata| match event {
@ -865,8 +883,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
layer, layer,
.. ..
} => { } => {
let output_map = layer_output_map.borrow();
let anvil_state = ddata.get::<AnvilState<BackendData>>().unwrap(); let anvil_state = ddata.get::<AnvilState<BackendData>>().unwrap();
let output_map = anvil_state.output_map.borrow();
let output = output.and_then(|output| output_map.find_by_output(&output)); let output = output.and_then(|output| output_map.find_by_output(&output));
let output = output.unwrap_or_else(|| { let output = output.unwrap_or_else(|| {
@ -878,9 +896,10 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
if let Some(wl_surface) = surface.get_surface() { if let Some(wl_surface) = surface.get_surface() {
output.add_layer_surface(wl_surface.clone()); output.add_layer_surface(wl_surface.clone());
layer_window_map.borrow_mut().layers.insert(surface, layer); anvil_state.window_map.borrow_mut().layers.insert(surface, layer);
} }
} }
LayerShellRequest::AckConfigure { .. } => {} LayerShellRequest::AckConfigure { .. } => {}
}, },
log.clone(), log.clone(),
@ -889,8 +908,6 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
ShellHandles { ShellHandles {
xdg_state: xdg_shell_state, xdg_state: xdg_shell_state,
wl_state: wl_shell_state, wl_state: wl_shell_state,
window_map,
output_map,
} }
} }

View File

@ -26,7 +26,7 @@ use smithay::{
#[cfg(feature = "xwayland")] #[cfg(feature = "xwayland")]
use smithay::xwayland::{XWayland, XWaylandEvent}; use smithay::xwayland::{XWayland, XWaylandEvent};
use crate::shell::init_shell; use crate::{output_map::OutputMap, shell::init_shell, window_map::WindowMap};
pub struct AnvilState<BackendData> { pub struct AnvilState<BackendData> {
pub backend_data: BackendData, pub backend_data: BackendData,
@ -79,11 +79,20 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
) )
.expect("Failed to init the wayland event source."); .expect("Failed to init the wayland event source.");
// Init a window map, to track the location of our windows
let window_map = Rc::new(RefCell::new(WindowMap::default()));
let output_map = Rc::new(RefCell::new(OutputMap::new(
display.clone(),
window_map.clone(),
log.clone(),
)));
// Init the basic compositor globals // Init the basic compositor globals
init_shm_global(&mut (*display).borrow_mut(), vec![], log.clone()); init_shm_global(&mut (*display).borrow_mut(), vec![], log.clone());
let shell_handles = init_shell::<BackendData>(display.clone(), log.clone()); // Init the shell states
init_shell::<BackendData>(display.clone(), log.clone());
init_xdg_output_manager(&mut display.borrow_mut(), log.clone()); init_xdg_output_manager(&mut display.borrow_mut(), log.clone());
init_xdg_activation_global( init_xdg_activation_global(
@ -192,8 +201,8 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
running: Arc::new(AtomicBool::new(true)), running: Arc::new(AtomicBool::new(true)),
display, display,
handle, handle,
window_map: shell_handles.window_map, window_map,
output_map: shell_handles.output_map, output_map,
dnd_icon, dnd_icon,
log, log,
socket_name, socket_name,