formatting commit

This commit is contained in:
Victoria Brekenfeld 2022-01-18 19:42:56 +01:00
parent c46f327384
commit d78a06123f
6 changed files with 131 additions and 97 deletions

View file

@ -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>, 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")
}
}
}

View file

@ -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>, 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>, 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>, state: &mut State) -> Res
#[cfg(feature = "debug")]
fps: Fps::default(),
});
Ok(())
}
fn init_egl_client_side(display: &mut Display, renderer: Rc<RefCell<WinitGraphicsBackend>>) -> Result<()> {
fn init_egl_client_side(
display: &mut Display,
renderer: Rc<RefCell<WinitGraphicsBackend>>,
) -> 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<RefCell<WinitGraphic
init_dmabuf_global(
display,
dmabuf_formats,
move |buffer, _| renderer.borrow_mut().renderer().import_dmabuf(buffer).is_ok(),
move |buffer, _| {
renderer
.borrow_mut()
.renderer()
.import_dmabuf(buffer)
.is_ok()
},
None,
);
}
@ -215,10 +226,10 @@ impl State {
output.set_preferred(mode);
layer_map_for_output(output).arrange();
render_ping.ping();
},
}
WinitEvent::Refresh => render_ping.ping(),
WinitEvent::Input(event) => self.common.process_input_event(event),
_ => {},
_ => {}
};
}
}

View file

@ -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);
}
}

View file

@ -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<i32, Logical>, scale: f64, primary: bool) -> EguiFrame {
pub fn debug_ui(
state: &mut Common,
fps: &Fps,
area: Rectangle<i32, Logical>,
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<i32, Logical>, 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<i32, Logical>, 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<i32, Logical>, 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<i32, Logical>, 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<i32, Logical>, 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<i32, Logical>, 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,

View file

@ -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<Output>);
@ -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::<SupressedKeys>().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()
{

View file

@ -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<Item=&'a Duration>) -> f64 {
pub fn avg_fps<'a>(iter: impl Iterator<Item = &'a Duration>) -> f64 {
let sum_secs = iter.map(|d| d.as_secs_f64()).sum::<f64>();
1.0 / (sum_secs / Fps::WINDOW_SIZE as f64)
}
}