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(
&mut *x11_state.renderer.borrow_mut(),
state.spaces.active_space_mut(&output_ref),
&state.start_time,
state.common.spaces.active_space_mut(&output_ref),
&state.common.start_time,
) {
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")?,
));
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 {
handle,
@ -205,9 +205,9 @@ pub fn init_backend(event_loop: &mut EventLoop<State>, state: &mut State) -> Res
let output = state
.backend
.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")?;
state.spaces.map_output(&output);
state.common.spaces.map_output(&output);
event_loop
.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)
{
surface.window.unmap();
state.spaces.unmap_output(&surface.output);
state.common.spaces.unmap_output(&surface.output);
}
state
.backend
@ -314,7 +314,7 @@ impl State {
.unwrap();
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();
if devices.has_device(&device) {
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())
.unwrap_or_else(|| {
state
.common
.spaces
.outputs()
.next()
@ -100,7 +101,7 @@ impl State {
match event {
InputEvent::DeviceAdded { device } => {
let seat = &mut self.last_active_seat;
let seat = &mut self.common.last_active_seat;
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
for cap in devices.add_device(&device) {
@ -116,6 +117,7 @@ impl State {
}
DeviceCapability::Pointer => {
let output = self
.common
.spaces
.outputs()
.next()
@ -137,7 +139,7 @@ impl State {
}
}
InputEvent::DeviceRemoved { device } => {
for seat in &mut self.seats {
for seat in &mut self.common.seats {
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
@ -160,7 +162,7 @@ impl State {
use smithay::backend::input::KeyboardKeyEvent;
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 devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
@ -189,7 +191,7 @@ impl State {
use smithay::backend::input::PointerMotionEvent;
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 devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
@ -199,10 +201,12 @@ impl State {
position += event.delta();
let output = self
.common
.spaces
.outputs()
.find(|output| {
self.spaces
self.common
.spaces
.output_geometry(output)
.to_f64()
.contains(position)
@ -212,7 +216,7 @@ impl State {
if output != current_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
.max(position.x)
@ -222,7 +226,7 @@ impl State {
.min((output_geometry.loc.y + output_geometry.size.h) as f64);
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);
handle_window_movement(under.as_ref().map(|(s, _)| s), space);
seat.get_pointer()
@ -237,13 +241,13 @@ impl State {
use smithay::backend::input::PointerMotionAbsoluteEvent;
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 devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
let output = active_output(seat, &self);
let geometry = self.spaces.output_geometry(&output);
let space = self.spaces.active_space_mut(&output);
let geometry = self.common.spaces.output_geometry(&output);
let space = self.common.spaces.active_space_mut(&output);
let position =
geometry.loc.to_f64() + event.position_transformed(geometry.size);
let serial = SERIAL_COUNTER.next_serial();
@ -263,7 +267,7 @@ impl State {
};
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 devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
@ -275,8 +279,8 @@ impl State {
if !seat.get_pointer().unwrap().is_grabbed() {
let output = active_output(seat, &self);
let mut pos = seat.get_pointer().unwrap().current_location();
let output_geo = self.spaces.output_geometry(&output);
let space = self.spaces.active_space_mut(&output);
let output_geo = self.common.spaces.output_geometry(&output);
let space = self.common.spaces.active_space_mut(&output);
let layers = layer_map_for_output(&output);
pos -= output_geo.loc.to_f64();
let mut under = None;
@ -335,7 +339,7 @@ impl State {
};
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 devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {

View file

@ -32,7 +32,7 @@ fn main() -> Result<()> {
let signal = event_loop.get_signal();
event_loop.run(None, &mut state, |state| {
// 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");
signal.stop();
signal.wakeup();
@ -40,10 +40,10 @@ fn main() -> Result<()> {
}
// trigger routines
state.spaces.refresh();
state.common.spaces.refresh();
// send out events
let display = state.display.clone();
let display = state.common.display.clone();
display.borrow_mut().flush_clients(state);
})?;
@ -87,13 +87,13 @@ fn init_wayland_display(event_loop: &mut EventLoop<state::State>) -> Result<Disp
.insert_source(
Generic::from_fd(display.get_poll_fd(), Interest::READ, Mode::Level),
move |_, _, state: &mut state::State| {
let display = state.display.clone();
let display = state.common.display.clone();
let mut display = display.borrow_mut();
match display.dispatch(std::time::Duration::from_millis(0), state) {
Ok(_) => Ok(PostAction::Continue),
Err(e) => {
slog_scope::error!("I/O error on the Wayland display: {}", e);
state.should_stop = true;
state.common.should_stop = true;
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| {
on_commit_buffer_handler(&surface);
let state = ddata.get::<State>().unwrap();
state.spaces.commit(&surface);
state.shell.popups.commit(&surface);
state.common.spaces.commit(&surface);
state.common.shell.popups.commit(&surface);
commit(&surface, state)
},
None,
@ -55,17 +55,18 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
match event {
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 space = state.spaces.active_space_mut(&output);
let space = state.common.spaces.active_space_mut(&output);
let window = Window::new(Kind::Xdg(surface));
space.map_window(&window, (0, 0), true);
// We will position the window after the first commit, when we know its size
}
XdgRequest::NewPopup { surface, .. } => {
state
.common
.shell
.popups
.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)
{
let space = state
.common
.spaces
.space_for_surface(surface.get_surface().unwrap())
.unwrap();
@ -162,6 +164,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
check_grab_preconditions(&seat, surface.get_surface(), serial)
{
let space = state
.common
.spaces
.space_for_surface(surface.get_surface().unwrap())
.unwrap();
@ -182,6 +185,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
configure: Configure::Toplevel(configure),
} => {
let window = state
.common
.spaces
.space_for_surface(&surface)
.unwrap()
@ -190,9 +194,9 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
grabs::ResizeSurfaceGrab::ack_configure(window, configure)
}
XdgRequest::Maximize { surface } => {
let seat = &state.last_active_seat;
let seat = &state.common.last_active_seat;
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
.window_for_surface(surface.get_surface().unwrap())
.unwrap()
@ -236,7 +240,7 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
..
} => {
let state = ddata.get::<State>().unwrap();
let seat = &state.last_active_seat;
let seat = &state.common.last_active_seat;
let output = wl_output
.as_ref()
.and_then(Output::from_resource)
@ -296,6 +300,8 @@ fn check_grab_preconditions(
}
fn commit(surface: &WlSurface, state: &mut State) {
let state = &mut state.common;
if let Some(toplevel) = state.pending_toplevels.iter().find(|toplevel| {
toplevel
.get_surface()

View file

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