From d78a06123f97925a531080f8e109e87f4bf7e4bb Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Tue, 18 Jan 2022 19:42:56 +0100 Subject: [PATCH] formatting commit --- src/backend/mod.rs | 24 +++++----- src/backend/winit.rs | 101 ++++++++++++++++++++++++------------------- src/backend/x11.rs | 7 ++- src/debug.rs | 74 +++++++++++++++++++++---------- src/input/mod.rs | 13 ++---- src/state.rs | 9 ++-- 6 files changed, 131 insertions(+), 97 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7b86e7b5..981dd132 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -4,8 +4,8 @@ use crate::state::State; use anyhow::Result; use smithay::reexports::calloop::EventLoop; -pub mod x11; pub mod winit; +pub mod x11; // TODO // pub mod wayland; // tbd in smithay // pub mod udev; @@ -15,17 +15,21 @@ pub fn init_backend_auto(event_loop: &mut EventLoop, state: &mut State) - Ok(x) if x == "x11" => x11::init_backend(event_loop, state), Ok(x) if x == "winit" => winit::init_backend(event_loop, state), Ok(_) => unimplemented!("There is no backend with this identifier"), - Err(_) => if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() { - match x11::init_backend(event_loop, state) { - Ok(_) => Ok(()), - Err(err) => { - slog_scope::warn!("X11 Backend failed with error: {}", err); - slog_scope::info!("Falling back to winit backend."); - winit::init_backend(event_loop, state) + Err(_) => { + if std::env::var_os("DISPLAY").is_some() + || std::env::var_os("WAYLAND_DISPLAY").is_some() + { + match x11::init_backend(event_loop, state) { + Ok(_) => Ok(()), + Err(err) => { + slog_scope::warn!("X11 Backend failed with error: {}", err); + slog_scope::info!("Falling back to winit backend."); + winit::init_backend(event_loop, state) + } } + } else { + unimplemented!("Currently this runs only nested") } - } else { - unimplemented!("Currently this runs only nested") } } } diff --git a/src/backend/winit.rs b/src/backend/winit.rs index 4b3e0984..d530919a 100644 --- a/src/backend/winit.rs +++ b/src/backend/winit.rs @@ -9,7 +9,7 @@ use anyhow::{Context, Result}; use smithay::{ backend::{ renderer::{ImportDma, ImportEgl}, - winit::{self, WinitGraphicsBackend, WinitEvent, WinitVirtualDevice}, + winit::{self, WinitEvent, WinitGraphicsBackend, WinitVirtualDevice}, }, desktop::layer_map_for_output, reexports::{ @@ -24,15 +24,12 @@ use smithay::{ output::{Mode, Output, PhysicalProperties}, }, }; -use std::{ - cell::RefCell, - rc::Rc, -}; +use std::{cell::RefCell, rc::Rc}; -#[cfg(feature = "debug")] -use smithay::backend::renderer::gles2::Gles2Renderer; #[cfg(feature = "debug")] use crate::{debug::debug_ui, state::Fps}; +#[cfg(feature = "debug")] +use smithay::backend::renderer::gles2::Gles2Renderer; pub struct WinitState { // The winit backend currently has no notion of multiple windows @@ -44,10 +41,7 @@ pub struct WinitState { } impl WinitState { - pub fn render_output( - &mut self, - state: &mut Common, - ) -> Result<()> { + pub fn render_output(&mut self, state: &mut Common) -> Result<()> { #[allow(unused_mut)] let mut custom_elements = Vec::new(); @@ -94,11 +88,12 @@ impl WinitState { } pub fn init_backend(event_loop: &mut EventLoop, state: &mut State) -> Result<()> { - let (backend, mut input) = winit::init(None).with_context(|| "Failed to initilize winit backend")?; + let (backend, mut input) = + winit::init(None).with_context(|| "Failed to initilize winit backend")?; let backend = Rc::new(RefCell::new(backend)); init_egl_client_side(&mut *state.common.display.borrow_mut(), backend.clone())?; - + let name = format!("WINIT-0"); let size = backend.borrow().window_size(); let props = PhysicalProperties { @@ -113,47 +108,54 @@ pub fn init_backend(event_loop: &mut EventLoop, state: &mut State) -> Res }; let (output, _global) = Output::new(&mut *state.common.display.borrow_mut(), name, props, None); //let _global = global.into(); - output.change_current_state(Some(mode), Some(Transform::Flipped180), None, Some((0, 0).into())); + output.change_current_state( + Some(mode), + Some(Transform::Flipped180), + None, + Some((0, 0).into()), + ); output.set_preferred(mode); state.common.spaces.map_output(&output); - let (event_ping, event_source) = ping::make_ping().with_context(|| "Failed to init eventloop timer for winit")?; - let (render_ping, render_source) = ping::make_ping().with_context(|| "Failed to init eventloop timer for winit")?; + let (event_ping, event_source) = + ping::make_ping().with_context(|| "Failed to init eventloop timer for winit")?; + let (render_ping, render_source) = + ping::make_ping().with_context(|| "Failed to init eventloop timer for winit")?; let event_ping_handle = event_ping.clone(); let render_ping_handle = render_ping.clone(); - let mut token = Some(event_loop - .handle() - .insert_source( - render_source, - move |_, _, state| { + let mut token = Some( + event_loop + .handle() + .insert_source(render_source, move |_, _, state| { if let Err(err) = state.backend.winit().render_output(&mut state.common) { slog_scope::error!("Failed to render frame: {}", err); render_ping.ping(); } - } - ).map_err(|_| anyhow::anyhow!("Failed to init eventloop timer for winit"))?); + }) + .map_err(|_| anyhow::anyhow!("Failed to init eventloop timer for winit"))?, + ); let event_loop_handle = event_loop.handle(); event_loop .handle() - .insert_source( - event_source, - move |_, _, state| { - match input.dispatch_new_events(|event| state.process_winit_event(event, &render_ping_handle)) { - Ok(_) => { - event_ping_handle.ping(); - render_ping_handle.ping(); - }, - Err(winit::WinitError::WindowClosed) => { - let winit_state = state.backend.winit(); - state.common.spaces.unmap_output(&winit_state.output); - if let Some(token) = token.take() { - event_loop_handle.remove(token); - } + .insert_source(event_source, move |_, _, state| { + match input + .dispatch_new_events(|event| state.process_winit_event(event, &render_ping_handle)) + { + Ok(_) => { + event_ping_handle.ping(); + render_ping_handle.ping(); + } + Err(winit::WinitError::WindowClosed) => { + let winit_state = state.backend.winit(); + state.common.spaces.unmap_output(&winit_state.output); + if let Some(token) = token.take() { + event_loop_handle.remove(token); } - }; - } - ).map_err(|_| anyhow::anyhow!("Failed to init eventloop timer for winit"))?; + } + }; + }) + .map_err(|_| anyhow::anyhow!("Failed to init eventloop timer for winit"))?; event_ping.ping(); state.backend = BackendData::Winit(WinitState { @@ -162,11 +164,14 @@ pub fn init_backend(event_loop: &mut EventLoop, state: &mut State) -> Res #[cfg(feature = "debug")] fps: Fps::default(), }); - + Ok(()) } -fn init_egl_client_side(display: &mut Display, renderer: Rc>) -> Result<()> { +fn init_egl_client_side( + display: &mut Display, + renderer: Rc>, +) -> Result<()> { let bind_result = renderer.borrow_mut().renderer().bind_wl_display(display); match bind_result { Ok(_) => { @@ -180,7 +185,13 @@ fn init_egl_client_side(display: &mut Display, renderer: Rc render_ping.ping(), WinitEvent::Input(event) => self.common.process_input_event(event), - _ => {}, + _ => {} }; } } diff --git a/src/backend/x11.rs b/src/backend/x11.rs index f8ace282..a4ee857d 100644 --- a/src/backend/x11.rs +++ b/src/backend/x11.rs @@ -99,10 +99,9 @@ impl X11State { .iter_mut() .find(|s| s.output == output_ref) { - if let Err(err) = surface.render_output( - &mut *x11_state.renderer.borrow_mut(), - &mut state.common, - ) { + if let Err(err) = surface + .render_output(&mut *x11_state.renderer.borrow_mut(), &mut state.common) + { slog_scope::error!("Error rendering: {}", err); } } diff --git a/src/debug.rs b/src/debug.rs index d137bcb2..51493de0 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,10 +1,16 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::state::{Common, Fps}; +use smithay::utils::{Logical, Rectangle}; use smithay_egui::EguiFrame; -use smithay::utils::{Rectangle, Logical}; -pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, scale: f64, primary: bool) -> EguiFrame { +pub fn debug_ui( + state: &mut Common, + fps: &Fps, + area: Rectangle, + scale: f64, + primary: bool, +) -> EguiFrame { let size = area.size; let alpha = state.egui.alpha; @@ -32,7 +38,7 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc ui.separator(); // FPS - + let (max, min, avg, avg_fps) = ( fps.max_frametime().as_secs_f64(), fps.min_frametime().as_secs_f64(), @@ -48,11 +54,14 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc let fps_chart = BarChart::new( fps.frames .iter() - .rev().take(30).rev() + .rev() + .take(30) + .rev() .enumerate() .map(|(i, d)| { let value = d.as_secs_f64(); - let transformed = ((value - min) / (max - min) * 255.0).round() as u8; + let transformed = + ((value - min) / (max - min) * 255.0).round() as u8; Bar::new(i as f64, value).fill(egui::Color32::from_rgb( transformed, 255 - transformed, @@ -74,21 +83,23 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc .show(ui, |plot_ui| { plot_ui.bar_chart(fps_chart); plot_ui.hline( - HLine::new(avg) - .highlight() - .color(egui::Color32::LIGHT_BLUE), + HLine::new(avg).highlight().color(egui::Color32::LIGHT_BLUE), ); }); - + ui.separator(); // Toggles and stuff - ui.add(egui::Slider::new(&mut state.egui.alpha, 0.1..=1.0).clamp_to_range(true).text("Opacity")); + ui.add( + egui::Slider::new(&mut state.egui.alpha, 0.1..=1.0) + .clamp_to_range(true) + .text("Opacity"), + ); ui.checkbox(&mut state.egui.spaces, "Workspace UI"); //TODO: ui.checkbox(&mut state.egui.outputs, "Outputs UI"); } }); - + // don't show these one others then the primary monitor if primary { egui::Window::new("Workspaces") @@ -99,11 +110,15 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc ui.set_min_width(250.0); - // Mode + // Mode ui.label(egui::RichText::new("Mode").heading()); let mut mode = *state.spaces.mode(); - let active = if let Mode::Global { active } = mode { active } else { 0 }; + let active = if let Mode::Global { active } = mode { + active + } else { + 0 + }; ui.radio_value(&mut mode, Mode::OutputBound, "Output bound"); ui.radio_value(&mut mode, Mode::Global { active }, "Global"); state.spaces.set_mode(mode); @@ -121,20 +136,29 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc .unwrap(); let mut active_val = active as f64; ui.label(output.name()); - ui.add(egui::DragValue::new(&mut active_val).clamp_range(0..=(MAX_WORKSPACES-1)).speed(1.0)); + ui.add( + egui::DragValue::new(&mut active_val) + .clamp_range(0..=(MAX_WORKSPACES - 1)) + .speed(1.0), + ); if active != active_val as usize { state.spaces.activate(&output, active_val as usize); } }); } - }, + } Mode::Global { active } => { ui.horizontal(|ui| { let mut active_val = active as f64; ui.label("Workspace:"); - ui.add(egui::DragValue::new(&mut active_val).clamp_range(0..=(MAX_WORKSPACES-1)).speed(1.0)); + ui.add( + egui::DragValue::new(&mut active_val) + .clamp_range(0..=(MAX_WORKSPACES - 1)) + .speed(1.0), + ); if active != active_val as usize { - let output = state.spaces.outputs().next().cloned().unwrap(); + let output = + state.spaces.outputs().next().cloned().unwrap(); state.spaces.activate(&output, active_val as usize); } }); @@ -147,21 +171,25 @@ pub fn debug_ui(state: &mut Common, fps: &Fps, area: Rectangle, sc ui.collapsing(format!("Windows"), |ui| { for window in space.windows() { ui.collapsing(format!("{:?}", window.toplevel()), |ui| { - ui.label(format!("Rect: {:?}", space.window_geometry(window))); - ui.label(format!("Bounding box: {:?}", space.window_bbox(window))); + ui.label(format!( + "Rect: {:?}", + space.window_geometry(window) + )); + ui.label(format!( + "Bounding box: {:?}", + space.window_bbox(window) + )); }); } }) }); } }); - + egui::Window::new("Outputs") .open(&mut state.egui.outputs) .hscroll(true) - .show(ctx, |ui| { - - }); + .show(ctx, |ui| {}); } }, area, diff --git a/src/input/mod.rs b/src/input/mod.rs index aaed308c..5a5a7c65 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::state::Common; +#[cfg(feature = "debug")] +use smithay::{backend::input::KeyState, wayland::seat::keysyms}; use smithay::{ backend::input::{Device, DeviceCapability, InputBackend, InputEvent}, desktop::{layer_map_for_output, Space}, @@ -14,11 +16,6 @@ use smithay::{ SERIAL_COUNTER, }, }; -#[cfg(feature = "debug")] -use smithay::{ - backend::input::KeyState, - wayland::seat::keysyms, -}; use std::{cell::RefCell, collections::HashMap}; pub struct ActiveOutput(pub RefCell); @@ -217,8 +214,7 @@ impl Common { #[cfg(feature = "debug")] { self.egui.modifiers = modifiers.clone(); - if self.seats.iter().position(|x| x == seat).unwrap() - == 0 + if self.seats.iter().position(|x| x == seat).unwrap() == 0 && modifiers.logo && handle.raw_syms().contains(&keysyms::KEY_Escape) && state == KeyState::Pressed @@ -227,8 +223,7 @@ impl Common { userdata.get::().unwrap().add(&handle); return FilterResult::Intercept(()); } - if self.seats.iter().position(|x| x == seat).unwrap() - == 0 + if self.seats.iter().position(|x| x == seat).unwrap() == 0 && self.egui.active && self.egui.state.wants_keyboard() { diff --git a/src/state.rs b/src/state.rs index 736dfff0..86a0ee09 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,10 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::{ - backend::{ - x11::X11State, - winit::WinitState, - }, + backend::{winit::WinitState, x11::X11State}, shell::{init_shell, workspaces::Workspaces, ShellStates}, }; use smithay::{ @@ -177,7 +174,7 @@ impl Default for Fps { } #[cfg(feature = "debug")] -pub fn avg_fps<'a>(iter: impl Iterator) -> f64 { +pub fn avg_fps<'a>(iter: impl Iterator) -> f64 { let sum_secs = iter.map(|d| d.as_secs_f64()).sum::(); 1.0 / (sum_secs / Fps::WINDOW_SIZE as f64) -} \ No newline at end of file +}