state: split off backend from rest

This commit is contained in:
Victoria Brekenfeld 2022-01-11 17:00:04 +01:00
parent a14591a217
commit 57d94515d5
6 changed files with 59 additions and 43 deletions

View file

@ -98,8 +98,8 @@ impl X11State {
{ {
if let Err(err) = surface.render_from_space( if let Err(err) = surface.render_from_space(
&mut *x11_state.renderer.borrow_mut(), &mut *x11_state.renderer.borrow_mut(),
state.spaces.active_space_mut(&output_ref), state.common.spaces.active_space_mut(&output_ref),
&state.start_time, &state.common.start_time,
) { ) {
slog_scope::error!("Error rendering: {}", err); slog_scope::error!("Error rendering: {}", err);
} }
@ -192,7 +192,7 @@ pub fn init_backend(event_loop: &mut EventLoop<State>, state: &mut State) -> Res
.with_context(|| "Failed to initialize renderer")?, .with_context(|| "Failed to initialize renderer")?,
)); ));
init_egl_client_side(&mut *state.display.borrow_mut(), renderer.clone())?; init_egl_client_side(&mut *state.common.display.borrow_mut(), renderer.clone())?;
state.backend = BackendData::X11(X11State { state.backend = BackendData::X11(X11State {
handle, handle,
@ -205,9 +205,9 @@ pub fn init_backend(event_loop: &mut EventLoop<State>, state: &mut State) -> Res
let output = state let output = state
.backend .backend
.x11() .x11()
.add_window(&mut *state.display.borrow_mut(), event_loop.handle()) .add_window(&mut *state.common.display.borrow_mut(), event_loop.handle())
.with_context(|| "Failed to create wl_output")?; .with_context(|| "Failed to create wl_output")?;
state.spaces.map_output(&output); state.common.spaces.map_output(&output);
event_loop event_loop
.handle() .handle()
@ -222,7 +222,7 @@ pub fn init_backend(event_loop: &mut EventLoop<State>, state: &mut State) -> Res
.filter(|s| s.window.id() == window_id) .filter(|s| s.window.id() == window_id)
{ {
surface.window.unmap(); surface.window.unmap();
state.spaces.unmap_output(&surface.output); state.common.spaces.unmap_output(&surface.output);
} }
state state
.backend .backend
@ -314,7 +314,7 @@ impl State {
.unwrap(); .unwrap();
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let devices = seat.user_data().get::<Devices>().unwrap(); let devices = seat.user_data().get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
set_active_output(seat, &output); set_active_output(seat, &output);

View file

@ -72,6 +72,7 @@ pub fn active_output(seat: &Seat, state: &State) -> Output {
.map(|x| x.0.borrow().clone()) .map(|x| x.0.borrow().clone())
.unwrap_or_else(|| { .unwrap_or_else(|| {
state state
.common
.spaces .spaces
.outputs() .outputs()
.next() .next()
@ -100,7 +101,7 @@ impl State {
match event { match event {
InputEvent::DeviceAdded { device } => { InputEvent::DeviceAdded { device } => {
let seat = &mut self.last_active_seat; let seat = &mut self.common.last_active_seat;
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
for cap in devices.add_device(&device) { for cap in devices.add_device(&device) {
@ -116,6 +117,7 @@ impl State {
} }
DeviceCapability::Pointer => { DeviceCapability::Pointer => {
let output = self let output = self
.common
.spaces .spaces
.outputs() .outputs()
.next() .next()
@ -137,7 +139,7 @@ impl State {
} }
} }
InputEvent::DeviceRemoved { device } => { InputEvent::DeviceRemoved { device } => {
for seat in &mut self.seats { for seat in &mut self.common.seats {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -160,7 +162,7 @@ impl State {
use smithay::backend::input::KeyboardKeyEvent; use smithay::backend::input::KeyboardKeyEvent;
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -189,7 +191,7 @@ impl State {
use smithay::backend::input::PointerMotionEvent; use smithay::backend::input::PointerMotionEvent;
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -199,10 +201,12 @@ impl State {
position += event.delta(); position += event.delta();
let output = self let output = self
.common
.spaces .spaces
.outputs() .outputs()
.find(|output| { .find(|output| {
self.spaces self.common
.spaces
.output_geometry(output) .output_geometry(output)
.to_f64() .to_f64()
.contains(position) .contains(position)
@ -212,7 +216,7 @@ impl State {
if output != current_output { if output != current_output {
set_active_output(seat, &output); set_active_output(seat, &output);
} }
let output_geometry = self.spaces.output_geometry(&output); let output_geometry = self.common.spaces.output_geometry(&output);
position.x = 0.0f64 position.x = 0.0f64
.max(position.x) .max(position.x)
@ -222,7 +226,7 @@ impl State {
.min((output_geometry.loc.y + output_geometry.size.h) as f64); .min((output_geometry.loc.y + output_geometry.size.h) as f64);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
let space = self.spaces.active_space_mut(&output); let space = self.common.spaces.active_space_mut(&output);
let under = State::surface_under(position, &output, space); let under = State::surface_under(position, &output, space);
handle_window_movement(under.as_ref().map(|(s, _)| s), space); handle_window_movement(under.as_ref().map(|(s, _)| s), space);
seat.get_pointer() seat.get_pointer()
@ -237,13 +241,13 @@ impl State {
use smithay::backend::input::PointerMotionAbsoluteEvent; use smithay::backend::input::PointerMotionAbsoluteEvent;
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
let output = active_output(seat, &self); let output = active_output(seat, &self);
let geometry = self.spaces.output_geometry(&output); let geometry = self.common.spaces.output_geometry(&output);
let space = self.spaces.active_space_mut(&output); let space = self.common.spaces.active_space_mut(&output);
let position = let position =
geometry.loc.to_f64() + event.position_transformed(geometry.size); geometry.loc.to_f64() + event.position_transformed(geometry.size);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
@ -263,7 +267,7 @@ impl State {
}; };
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -275,8 +279,8 @@ impl State {
if !seat.get_pointer().unwrap().is_grabbed() { if !seat.get_pointer().unwrap().is_grabbed() {
let output = active_output(seat, &self); let output = active_output(seat, &self);
let mut pos = seat.get_pointer().unwrap().current_location(); let mut pos = seat.get_pointer().unwrap().current_location();
let output_geo = self.spaces.output_geometry(&output); let output_geo = self.common.spaces.output_geometry(&output);
let space = self.spaces.active_space_mut(&output); let space = self.common.spaces.active_space_mut(&output);
let layers = layer_map_for_output(&output); let layers = layer_map_for_output(&output);
pos -= output_geo.loc.to_f64(); pos -= output_geo.loc.to_f64();
let mut under = None; let mut under = None;
@ -335,7 +339,7 @@ impl State {
}; };
let device = event.device(); let device = event.device();
for seat in self.seats.clone().iter() { for seat in self.common.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {

View file

@ -32,7 +32,7 @@ fn main() -> Result<()> {
let signal = event_loop.get_signal(); let signal = event_loop.get_signal();
event_loop.run(None, &mut state, |state| { event_loop.run(None, &mut state, |state| {
// shall we shut down? // shall we shut down?
if state.spaces.outputs().next().is_none() || state.should_stop { if state.common.spaces.outputs().next().is_none() || state.common.should_stop {
slog_scope::info!("Shutting down"); slog_scope::info!("Shutting down");
signal.stop(); signal.stop();
signal.wakeup(); signal.wakeup();
@ -40,10 +40,10 @@ fn main() -> Result<()> {
} }
// trigger routines // trigger routines
state.spaces.refresh(); state.common.spaces.refresh();
// send out events // send out events
let display = state.display.clone(); let display = state.common.display.clone();
display.borrow_mut().flush_clients(state); display.borrow_mut().flush_clients(state);
})?; })?;
@ -87,13 +87,13 @@ fn init_wayland_display(event_loop: &mut EventLoop<state::State>) -> Result<Disp
.insert_source( .insert_source(
Generic::from_fd(display.get_poll_fd(), Interest::READ, Mode::Level), Generic::from_fd(display.get_poll_fd(), Interest::READ, Mode::Level),
move |_, _, state: &mut state::State| { move |_, _, state: &mut state::State| {
let display = state.display.clone(); let display = state.common.display.clone();
let mut display = display.borrow_mut(); let mut display = display.borrow_mut();
match display.dispatch(std::time::Duration::from_millis(0), state) { match display.dispatch(std::time::Duration::from_millis(0), state) {
Ok(_) => Ok(PostAction::Continue), Ok(_) => Ok(PostAction::Continue),
Err(e) => { Err(e) => {
slog_scope::error!("I/O error on the Wayland display: {}", e); slog_scope::error!("I/O error on the Wayland display: {}", e);
state.should_stop = true; state.common.should_stop = true;
Err(e) Err(e)
} }
} }

0
src/rendering/debug.rs Normal file
View file

View file

@ -41,8 +41,8 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
move |surface, mut ddata| { move |surface, mut ddata| {
on_commit_buffer_handler(&surface); on_commit_buffer_handler(&surface);
let state = ddata.get::<State>().unwrap(); let state = ddata.get::<State>().unwrap();
state.spaces.commit(&surface); state.common.spaces.commit(&surface);
state.shell.popups.commit(&surface); state.common.shell.popups.commit(&surface);
commit(&surface, state) commit(&surface, state)
}, },
None, None,
@ -55,17 +55,18 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
match event { match event {
XdgRequest::NewToplevel { surface } => { XdgRequest::NewToplevel { surface } => {
state.pending_toplevels.push(surface.clone()); state.common.pending_toplevels.push(surface.clone());
let seat = &state.last_active_seat; let seat = &state.common.last_active_seat;
let output = active_output(seat, &state); let output = active_output(seat, &state);
let space = state.spaces.active_space_mut(&output); let space = state.common.spaces.active_space_mut(&output);
let window = Window::new(Kind::Xdg(surface)); let window = Window::new(Kind::Xdg(surface));
space.map_window(&window, (0, 0), true); space.map_window(&window, (0, 0), true);
// We will position the window after the first commit, when we know its size // We will position the window after the first commit, when we know its size
} }
XdgRequest::NewPopup { surface, .. } => { XdgRequest::NewPopup { surface, .. } => {
state state
.common
.shell .shell
.popups .popups
.track_popup(PopupKind::from(surface)) .track_popup(PopupKind::from(surface))
@ -100,6 +101,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
check_grab_preconditions(&seat, surface.get_surface(), serial) check_grab_preconditions(&seat, surface.get_surface(), serial)
{ {
let space = state let space = state
.common
.spaces .spaces
.space_for_surface(surface.get_surface().unwrap()) .space_for_surface(surface.get_surface().unwrap())
.unwrap(); .unwrap();
@ -162,6 +164,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
check_grab_preconditions(&seat, surface.get_surface(), serial) check_grab_preconditions(&seat, surface.get_surface(), serial)
{ {
let space = state let space = state
.common
.spaces .spaces
.space_for_surface(surface.get_surface().unwrap()) .space_for_surface(surface.get_surface().unwrap())
.unwrap(); .unwrap();
@ -182,6 +185,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
configure: Configure::Toplevel(configure), configure: Configure::Toplevel(configure),
} => { } => {
let window = state let window = state
.common
.spaces .spaces
.space_for_surface(&surface) .space_for_surface(&surface)
.unwrap() .unwrap()
@ -190,9 +194,9 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
grabs::ResizeSurfaceGrab::ack_configure(window, configure) grabs::ResizeSurfaceGrab::ack_configure(window, configure)
} }
XdgRequest::Maximize { surface } => { XdgRequest::Maximize { surface } => {
let seat = &state.last_active_seat; let seat = &state.common.last_active_seat;
let output = active_output(seat, &state); let output = active_output(seat, &state);
let space = state.spaces.active_space_mut(&output); let space = state.common.spaces.active_space_mut(&output);
let window = space let window = space
.window_for_surface(surface.get_surface().unwrap()) .window_for_surface(surface.get_surface().unwrap())
.unwrap() .unwrap()
@ -236,7 +240,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
.. ..
} => { } => {
let state = ddata.get::<State>().unwrap(); let state = ddata.get::<State>().unwrap();
let seat = &state.last_active_seat; let seat = &state.common.last_active_seat;
let output = wl_output let output = wl_output
.as_ref() .as_ref()
.and_then(Output::from_resource) .and_then(Output::from_resource)
@ -296,6 +300,8 @@ fn check_grab_preconditions(
} }
fn commit(surface: &WlSurface, state: &mut State) { fn commit(surface: &WlSurface, state: &mut State) {
let state = &mut state.common;
if let Some(toplevel) = state.pending_toplevels.iter().find(|toplevel| { if let Some(toplevel) = state.pending_toplevels.iter().find(|toplevel| {
toplevel toplevel
.get_surface() .get_surface()

View file

@ -17,6 +17,11 @@ use smithay::{
use std::{cell::RefCell, rc::Rc, time::Instant}; use std::{cell::RefCell, rc::Rc, time::Instant};
pub struct State { pub struct State {
pub common: Common,
pub backend: BackendData,
}
pub struct Common {
pub display: Rc<RefCell<Display>>, pub display: Rc<RefCell<Display>>,
pub spaces: Workspaces, pub spaces: Workspaces,
@ -28,7 +33,6 @@ pub struct State {
pub start_time: Instant, pub start_time: Instant,
pub should_stop: bool, pub should_stop: bool,
pub backend: BackendData,
} }
pub enum BackendData { pub enum BackendData {
@ -62,17 +66,19 @@ impl State {
); );
State { State {
display: Rc::new(RefCell::new(display)), common: Common {
display: Rc::new(RefCell::new(display)),
spaces: Workspaces::new(), spaces: Workspaces::new(),
shell: shell_handles, shell: shell_handles,
pending_toplevels: Vec::new(), pending_toplevels: Vec::new(),
seats: vec![initial_seat.clone()], seats: vec![initial_seat.clone()],
last_active_seat: initial_seat, last_active_seat: initial_seat,
start_time: Instant::now(), start_time: Instant::now(),
should_stop: false, should_stop: false,
},
backend: BackendData::Unset, backend: BackendData::Unset,
} }
} }