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 xdg_state: Arc<Mutex<XdgShellState>>,
pub wl_state: Arc<Mutex<WlShellState>>,
pub window_map: Rc<RefCell<WindowMap>>,
pub output_map: Rc<RefCell<OutputMap>>,
}
fn fullscreen_output_geometry(
@ -336,32 +334,29 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
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
let xdg_window_map = window_map.clone();
let xdg_output_map = output_map.clone();
let (xdg_shell_state, _, _) = xdg_shell_init(
&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 } => {
// 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
let output_geometry = state
.output_map
.borrow()
.with_primary()
.map(|o| o.geometry())
.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 y_range = Uniform::new(output_geometry.loc.y, max_y);
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
// of a xdg_surface has to be sent during the commit if
// the surface is not already configured
xdg_window_map
state
.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
@ -389,8 +386,12 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
state.geometry = positioner.get_geometry();
})
.unwrap();
xdg_window_map.borrow_mut().insert_popup(PopupKind::Xdg(surface));
state
.window_map
.borrow_mut()
.insert_popup(PopupKind::Xdg(surface));
}
XdgRequest::RePosition {
surface,
positioner,
@ -410,6 +411,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
surface.send_repositioned(token);
}
}
XdgRequest::Move {
surface,
seat,
@ -440,7 +442,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
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 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 {
start_data,
window_map: xdg_window_map.clone(),
window_map: state.window_map.clone(),
toplevel,
initial_window_location,
};
pointer.set_grab(grab, serial);
}
XdgRequest::Resize {
surface,
seat,
@ -510,8 +513,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
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_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| {
@ -538,6 +541,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
pointer.set_grab(grab, serial);
}
XdgRequest::AckConfigure {
surface,
configure: Configure::Toplevel(configure),
@ -595,6 +599,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
}
}
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
@ -609,13 +614,14 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
let output_geometry = fullscreen_output_geometry(
wl_surface,
output.as_ref(),
&xdg_window_map.borrow(),
&xdg_output_map.borrow(),
&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 = xdg_window_map.borrow_mut();
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);
}
@ -631,6 +637,7 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
}
}
XdgRequest::UnFullscreen { surface } => {
let ret = surface.with_pending_state(|state| {
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();
}
}
XdgRequest::Maximize { surface } => {
// NOTE: This should use layer-shell when it is implemented to
// get the correct maximum size
let output_geometry = {
let xdg_window_map = xdg_window_map.borrow();
let xdg_window_map = state.window_map.borrow();
surface
.get_surface()
.and_then(|s| xdg_window_map.find(s))
.and_then(|k| xdg_window_map.location(&k))
.and_then(|position| {
xdg_output_map
state
.output_map
.borrow()
.find_by_position(position)
.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(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) {
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();
}
@ -679,21 +691,23 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
state.states.unset(xdg_toplevel::State::Maximized);
state.size = None;
});
if ret.is_ok() {
surface.send_configure();
}
}
_ => (),
}
},
log.clone(),
);
// 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(
&mut *display.borrow_mut(),
move |req: ShellRequest, _dispatch_data| {
move |req: ShellRequest, mut ddata| {
let state = ddata.get::<AnvilState<BackendData>>().unwrap();
match req {
ShellRequest::SetKind {
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
use rand::distributions::{Distribution, Uniform};
let output_geometry = shell_output_map
let output_geometry = state
.output_map
.borrow()
.with_primary()
.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 x = x_range.sample(&mut rng);
let y = y_range.sample(&mut rng);
shell_window_map
state
.window_map
.borrow_mut()
.insert(SurfaceKind::Wl(surface), (x, y).into());
}
ShellRequest::SetKind {
surface,
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(
wl_surface,
output.as_ref(),
&shell_window_map.borrow(),
&shell_output_map.borrow(),
&state.window_map.borrow(),
&state.output_map.borrow(),
);
if let Some(geometry) = output_geometry {
shell_window_map
state
.window_map
.borrow_mut()
.insert(SurfaceKind::Wl(surface), geometry.loc);
}
}
ShellRequest::Move {
surface,
seat,
@ -778,17 +797,18 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
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 {
start_data,
window_map: shell_window_map.clone(),
window_map: state.window_map.clone(),
toplevel,
initial_window_location,
};
pointer.set_grab(grab, serial);
}
ShellRequest::Resize {
surface,
seat,
@ -820,8 +840,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
}
let toplevel = SurfaceKind::Wl(surface.clone());
let initial_window_location = shell_window_map.borrow().location(&toplevel).unwrap();
let geometry = shell_window_map.borrow().geometry(&toplevel).unwrap();
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| {
@ -854,8 +874,6 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
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(
&mut *display.borrow_mut(),
move |event, mut ddata| match event {
@ -865,8 +883,8 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
layer,
..
} => {
let output_map = layer_output_map.borrow();
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.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() {
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 { .. } => {}
},
log.clone(),
@ -889,8 +908,6 @@ pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::sl
ShellHandles {
xdg_state: xdg_shell_state,
wl_state: wl_shell_state,
window_map,
output_map,
}
}

View File

@ -26,7 +26,7 @@ use smithay::{
#[cfg(feature = "xwayland")]
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 backend_data: BackendData,
@ -79,11 +79,20 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
)
.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_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_activation_global(
@ -192,8 +201,8 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
running: Arc::new(AtomicBool::new(true)),
display,
handle,
window_map: shell_handles.window_map,
output_map: shell_handles.output_map,
window_map,
output_map,
dnd_icon,
log,
socket_name,