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,32 +334,29 @@ 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| {
let state = ddata.get::<AnvilState<BackendData>>().unwrap();
match shell_event {
XdgRequest::NewToplevel { surface } => { XdgRequest::NewToplevel { surface } => {
// place the window at a random location on the primary output // 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 // 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 = xdg_output_map let output_geometry = state
.output_map
.borrow() .borrow()
.with_primary() .with_primary()
.map(|o| o.geometry()) .map(|o| o.geometry())
.unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800))); .unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800)));
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 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 x_range = Uniform::new(output_geometry.loc.x, max_x);
let y_range = Uniform::new(output_geometry.loc.y, max_y); let y_range = Uniform::new(output_geometry.loc.y, max_y);
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -370,10 +365,12 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
// Do not send a configure here, the initial configure // Do not send a configure here, the initial configure
// of a xdg_surface has to be sent during the commit if // of a xdg_surface has to be sent during the commit if
// the surface is not already configured // the surface is not already configured
xdg_window_map state
.window_map
.borrow_mut() .borrow_mut()
.insert(SurfaceKind::Xdg(surface), (x, y).into()); .insert(SurfaceKind::Xdg(surface), (x, y).into());
} }
XdgRequest::NewPopup { surface, positioner } => { XdgRequest::NewPopup { surface, positioner } => {
// Do not send a configure here, the initial configure // Do not send a configure here, the initial configure
// of a xdg_surface has to be sent during the commit if // of a xdg_surface has to be sent during the commit if
@ -389,8 +386,12 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
state.geometry = positioner.get_geometry(); state.geometry = positioner.get_geometry();
}) })
.unwrap(); .unwrap();
xdg_window_map.borrow_mut().insert_popup(PopupKind::Xdg(surface)); state
.window_map
.borrow_mut()
.insert_popup(PopupKind::Xdg(surface));
} }
XdgRequest::RePosition { XdgRequest::RePosition {
surface, surface,
positioner, positioner,
@ -410,6 +411,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
surface.send_repositioned(token); surface.send_repositioned(token);
} }
} }
XdgRequest::Move { XdgRequest::Move {
surface, surface,
seat, seat,
@ -440,7 +442,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
let toplevel = SurfaceKind::Xdg(surface.clone()); let toplevel = SurfaceKind::Xdg(surface.clone());
let mut initial_window_location = xdg_window_map.borrow().location(&toplevel).unwrap(); let mut initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
// If surface is maximized then unmaximize it // If surface is maximized then unmaximize it
if let Some(current_state) = surface.current_state() { if let Some(current_state) = surface.current_state() {
@ -472,13 +474,14 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
let grab = MoveSurfaceGrab { let grab = MoveSurfaceGrab {
start_data, start_data,
window_map: xdg_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);
} }
XdgRequest::Resize { XdgRequest::Resize {
surface, surface,
seat, seat,
@ -510,8 +513,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
let toplevel = SurfaceKind::Xdg(surface.clone()); let toplevel = SurfaceKind::Xdg(surface.clone());
let initial_window_location = xdg_window_map.borrow().location(&toplevel).unwrap(); let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
let geometry = xdg_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| {
@ -538,6 +541,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
pointer.set_grab(grab, serial); pointer.set_grab(grab, serial);
} }
XdgRequest::AckConfigure { XdgRequest::AckConfigure {
surface, surface,
configure: Configure::Toplevel(configure), configure: Configure::Toplevel(configure),
@ -595,6 +599,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
} }
} }
XdgRequest::Fullscreen { surface, output, .. } => { XdgRequest::Fullscreen { surface, output, .. } => {
// NOTE: This is only one part of the solution. We can set the // 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 // location and configure size here, but the surface should be rendered fullscreen
@ -609,13 +614,14 @@ 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(),
&xdg_window_map.borrow(), &state.window_map.borrow(),
&xdg_output_map.borrow(), &state.output_map.borrow(),
); );
if let Some(geometry) = output_geometry { if let Some(geometry) = output_geometry {
if let Some(surface) = surface.get_surface() { if let Some(surface) = surface.get_surface() {
let mut xdg_window_map = xdg_window_map.borrow_mut(); let mut xdg_window_map = state.window_map.borrow_mut();
if let Some(kind) = xdg_window_map.find(surface) { if let Some(kind) = xdg_window_map.find(surface) {
xdg_window_map.set_location(&kind, geometry.loc); xdg_window_map.set_location(&kind, geometry.loc);
} }
@ -631,6 +637,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
} }
} }
} }
XdgRequest::UnFullscreen { surface } => { XdgRequest::UnFullscreen { surface } => {
let ret = surface.with_pending_state(|state| { let ret = surface.with_pending_state(|state| {
state.states.unset(xdg_toplevel::State::Fullscreen); state.states.unset(xdg_toplevel::State::Fullscreen);
@ -641,17 +648,19 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
surface.send_configure(); surface.send_configure();
} }
} }
XdgRequest::Maximize { surface } => { XdgRequest::Maximize { surface } => {
// NOTE: This should use layer-shell when it is implemented to // NOTE: This should use layer-shell when it is implemented to
// get the correct maximum size // get the correct maximum size
let output_geometry = { let output_geometry = {
let xdg_window_map = xdg_window_map.borrow(); let xdg_window_map = state.window_map.borrow();
surface surface
.get_surface() .get_surface()
.and_then(|s| xdg_window_map.find(s)) .and_then(|s| xdg_window_map.find(s))
.and_then(|k| xdg_window_map.location(&k)) .and_then(|k| xdg_window_map.location(&k))
.and_then(|position| { .and_then(|position| {
xdg_output_map state
.output_map
.borrow() .borrow()
.find_by_position(position) .find_by_position(position)
.map(|o| o.geometry()) .map(|o| o.geometry())
@ -660,15 +669,18 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
if let Some(geometry) = output_geometry { if let Some(geometry) = output_geometry {
if let Some(surface) = surface.get_surface() { if let Some(surface) = surface.get_surface() {
let mut xdg_window_map = xdg_window_map.borrow_mut(); let mut xdg_window_map = state.window_map.borrow_mut();
if let Some(kind) = xdg_window_map.find(surface) { if let Some(kind) = xdg_window_map.find(surface) {
xdg_window_map.set_location(&kind, geometry.loc); xdg_window_map.set_location(&kind, geometry.loc);
} }
} }
let ret = surface.with_pending_state(|state| { let ret = surface.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Maximized); state.states.set(xdg_toplevel::State::Maximized);
state.size = Some(geometry.size); state.size = Some(geometry.size);
}); });
if ret.is_ok() { if ret.is_ok() {
surface.send_configure(); surface.send_configure();
} }
@ -679,21 +691,23 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
state.states.unset(xdg_toplevel::State::Maximized); state.states.unset(xdg_toplevel::State::Maximized);
state.size = None; state.size = None;
}); });
if ret.is_ok() { if ret.is_ok() {
surface.send_configure(); 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,