Add a Headless backend for tests
Browse files at this commitfbb0054232fb859636ea7e32bdc45c71e185f0c6Parent: 2d3c36e4 changed files +174 -2
- src/backend/headless.rs +139 -0
- src/backend/mod.rs +28 -0
- src/main.rs +1 -0
- src/niri.rs +6 -2
src/backend/headless.rsadded+139 -0
| @@ -0,0 +1,139 @@ | |||
| 1 | +//! Headless backend for tests. | ||
| 2 | +//! | ||
| 3 | +//! This can eventually grow into a more complete backend if needed, but for now it's missing some | ||
| 4 | +//! crucial parts like rendering. | ||
| 5 | + | ||
| 6 | +use std::mem; | ||
| 7 | +use std::sync::{Arc, Mutex}; | ||
| 8 | + | ||
| 9 | +use niri_config::OutputName; | ||
| 10 | +use smithay::backend::allocator::dmabuf::Dmabuf; | ||
| 11 | +use smithay::backend::renderer::element::RenderElementStates; | ||
| 12 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 13 | +use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; | ||
| 14 | +use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; | ||
| 15 | +use smithay::utils::Size; | ||
| 16 | + | ||
| 17 | +use super::{IpcOutputMap, OutputId, RenderResult}; | ||
| 18 | +use crate::niri::{Niri, RedrawState}; | ||
| 19 | +use crate::utils::{get_monotonic_time, logical_output}; | ||
| 20 | + | ||
| 21 | +pub struct Headless { | ||
| 22 | + ipc_outputs: Arc<Mutex<IpcOutputMap>>, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +impl Headless { | ||
| 26 | + pub fn new() -> Self { | ||
| 27 | + Self { | ||
| 28 | + ipc_outputs: Default::default(), | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + pub fn init(&mut self, _niri: &mut Niri) {} | ||
| 33 | + | ||
| 34 | + pub fn add_output(&mut self, niri: &mut Niri, n: u8, size: (u16, u16)) { | ||
| 35 | + let connector = format!("headless-{n}"); | ||
| 36 | + let make = "niri".to_string(); | ||
| 37 | + let model = "headless".to_string(); | ||
| 38 | + let serial = n.to_string(); | ||
| 39 | + | ||
| 40 | + let output = Output::new( | ||
| 41 | + connector.clone(), | ||
| 42 | + PhysicalProperties { | ||
| 43 | + size: (0, 0).into(), | ||
| 44 | + subpixel: Subpixel::Unknown, | ||
| 45 | + make: make.clone(), | ||
| 46 | + model: model.clone(), | ||
| 47 | + }, | ||
| 48 | + ); | ||
| 49 | + | ||
| 50 | + let mode = Mode { | ||
| 51 | + size: Size::from((i32::from(size.0), i32::from(size.1))), | ||
| 52 | + refresh: 60_000, | ||
| 53 | + }; | ||
| 54 | + output.change_current_state(Some(mode), None, None, None); | ||
| 55 | + output.set_preferred(mode); | ||
| 56 | + | ||
| 57 | + output.user_data().insert_if_missing(|| OutputName { | ||
| 58 | + connector, | ||
| 59 | + make: Some(make), | ||
| 60 | + model: Some(model), | ||
| 61 | + serial: Some(serial), | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + let physical_properties = output.physical_properties(); | ||
| 65 | + self.ipc_outputs.lock().unwrap().insert( | ||
| 66 | + OutputId::next(), | ||
| 67 | + niri_ipc::Output { | ||
| 68 | + name: output.name(), | ||
| 69 | + make: physical_properties.make, | ||
| 70 | + model: physical_properties.model, | ||
| 71 | + serial: None, | ||
| 72 | + physical_size: None, | ||
| 73 | + modes: vec![niri_ipc::Mode { | ||
| 74 | + width: size.0, | ||
| 75 | + height: size.1, | ||
| 76 | + refresh_rate: 60_000, | ||
| 77 | + is_preferred: true, | ||
| 78 | + }], | ||
| 79 | + current_mode: Some(0), | ||
| 80 | + vrr_supported: false, | ||
| 81 | + vrr_enabled: false, | ||
| 82 | + logical: Some(logical_output(&output)), | ||
| 83 | + }, | ||
| 84 | + ); | ||
| 85 | + | ||
| 86 | + niri.add_output(output, None, false); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + pub fn seat_name(&self) -> String { | ||
| 90 | + "headless".to_owned() | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + pub fn with_primary_renderer<T>( | ||
| 94 | + &mut self, | ||
| 95 | + _f: impl FnOnce(&mut GlesRenderer) -> T, | ||
| 96 | + ) -> Option<T> { | ||
| 97 | + None | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult { | ||
| 101 | + let states = RenderElementStates::default(); | ||
| 102 | + let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &states); | ||
| 103 | + presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( | ||
| 104 | + get_monotonic_time(), | ||
| 105 | + std::time::Duration::ZERO, | ||
| 106 | + 0, | ||
| 107 | + wp_presentation_feedback::Kind::empty(), | ||
| 108 | + ); | ||
| 109 | + | ||
| 110 | + let output_state = niri.output_state.get_mut(output).unwrap(); | ||
| 111 | + match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { | ||
| 112 | + RedrawState::Idle => unreachable!(), | ||
| 113 | + RedrawState::Queued => (), | ||
| 114 | + RedrawState::WaitingForVBlank { .. } => unreachable!(), | ||
| 115 | + RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(), | ||
| 116 | + RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(), | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1); | ||
| 120 | + | ||
| 121 | + // FIXME: request redraw on unfinished animations remain | ||
| 122 | + | ||
| 123 | + RenderResult::Submitted | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + pub fn import_dmabuf(&mut self, _dmabuf: &Dmabuf) -> bool { | ||
| 127 | + unimplemented!() | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { | ||
| 131 | + self.ipc_outputs.clone() | ||
| 132 | + } | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +impl Default for Headless { | ||
| 136 | + fn default() -> Self { | ||
| 137 | + Self::new() | ||
| 138 | + } | ||
| 139 | +} | ||
src/backend/mod.rsmodified+28 -0
| @@ -17,9 +17,13 @@ pub use tty::Tty; | |||
| 17 | pub mod winit; | 17 | pub mod winit; |
| 18 | pub use winit::Winit; | 18 | pub use winit::Winit; |
| 19 | 19 | ||
| 20 | +pub mod headless; | ||
| 21 | +pub use headless::Headless; | ||
| 22 | + | ||
| 20 | pub enum Backend { | 23 | pub enum Backend { |
| 21 | Tty(Tty), | 24 | Tty(Tty), |
| 22 | Winit(Winit), | 25 | Winit(Winit), |
| 26 | + Headless(Headless), | ||
| 23 | } | 27 | } |
| 24 | 28 | ||
| 25 | #[derive(PartialEq, Eq)] | 29 | #[derive(PartialEq, Eq)] |
| @@ -54,6 +58,7 @@ impl Backend { | |||
| 54 | match self { | 58 | match self { |
| 55 | Backend::Tty(tty) => tty.init(niri), | 59 | Backend::Tty(tty) => tty.init(niri), |
| 56 | Backend::Winit(winit) => winit.init(niri), | 60 | Backend::Winit(winit) => winit.init(niri), |
| 61 | + Backend::Headless(headless) => headless.init(niri), | ||
| 57 | } | 62 | } |
| 58 | } | 63 | } |
| 59 | 64 | ||
| @@ -61,6 +66,7 @@ impl Backend { | |||
| 61 | match self { | 66 | match self { |
| 62 | Backend::Tty(tty) => tty.seat_name(), | 67 | Backend::Tty(tty) => tty.seat_name(), |
| 63 | Backend::Winit(winit) => winit.seat_name(), | 68 | Backend::Winit(winit) => winit.seat_name(), |
| 69 | + Backend::Headless(headless) => headless.seat_name(), | ||
| 64 | } | 70 | } |
| 65 | } | 71 | } |
| 66 | 72 | ||
| @@ -71,6 +77,7 @@ impl Backend { | |||
| 71 | match self { | 77 | match self { |
| 72 | Backend::Tty(tty) => tty.with_primary_renderer(f), | 78 | Backend::Tty(tty) => tty.with_primary_renderer(f), |
| 73 | Backend::Winit(winit) => winit.with_primary_renderer(f), | 79 | Backend::Winit(winit) => winit.with_primary_renderer(f), |
| 80 | + Backend::Headless(headless) => headless.with_primary_renderer(f), | ||
| 74 | } | 81 | } |
| 75 | } | 82 | } |
| 76 | 83 | ||
| @@ -83,6 +90,7 @@ impl Backend { | |||
| 83 | match self { | 90 | match self { |
| 84 | Backend::Tty(tty) => tty.render(niri, output, target_presentation_time), | 91 | Backend::Tty(tty) => tty.render(niri, output, target_presentation_time), |
| 85 | Backend::Winit(winit) => winit.render(niri, output), | 92 | Backend::Winit(winit) => winit.render(niri, output), |
| 93 | + Backend::Headless(headless) => headless.render(niri, output), | ||
| 86 | } | 94 | } |
| 87 | } | 95 | } |
| 88 | 96 | ||
| @@ -90,6 +98,7 @@ impl Backend { | |||
| 90 | match self { | 98 | match self { |
| 91 | Backend::Tty(_) => CompositorMod::Super, | 99 | Backend::Tty(_) => CompositorMod::Super, |
| 92 | Backend::Winit(_) => CompositorMod::Alt, | 100 | Backend::Winit(_) => CompositorMod::Alt, |
| 101 | + Backend::Headless(_) => CompositorMod::Super, | ||
| 93 | } | 102 | } |
| 94 | } | 103 | } |
| 95 | 104 | ||
| @@ -97,6 +106,7 @@ impl Backend { | |||
| 97 | match self { | 106 | match self { |
| 98 | Backend::Tty(tty) => tty.change_vt(vt), | 107 | Backend::Tty(tty) => tty.change_vt(vt), |
| 99 | Backend::Winit(_) => (), | 108 | Backend::Winit(_) => (), |
| 109 | + Backend::Headless(_) => (), | ||
| 100 | } | 110 | } |
| 101 | } | 111 | } |
| 102 | 112 | ||
| @@ -104,6 +114,7 @@ impl Backend { | |||
| 104 | match self { | 114 | match self { |
| 105 | Backend::Tty(tty) => tty.suspend(), | 115 | Backend::Tty(tty) => tty.suspend(), |
| 106 | Backend::Winit(_) => (), | 116 | Backend::Winit(_) => (), |
| 117 | + Backend::Headless(_) => (), | ||
| 107 | } | 118 | } |
| 108 | } | 119 | } |
| 109 | 120 | ||
| @@ -111,6 +122,7 @@ impl Backend { | |||
| 111 | match self { | 122 | match self { |
| 112 | Backend::Tty(tty) => tty.toggle_debug_tint(), | 123 | Backend::Tty(tty) => tty.toggle_debug_tint(), |
| 113 | Backend::Winit(winit) => winit.toggle_debug_tint(), | 124 | Backend::Winit(winit) => winit.toggle_debug_tint(), |
| 125 | + Backend::Headless(_) => (), | ||
| 114 | } | 126 | } |
| 115 | } | 127 | } |
| 116 | 128 | ||
| @@ -118,6 +130,7 @@ impl Backend { | |||
| 118 | match self { | 130 | match self { |
| 119 | Backend::Tty(tty) => tty.import_dmabuf(dmabuf), | 131 | Backend::Tty(tty) => tty.import_dmabuf(dmabuf), |
| 120 | Backend::Winit(winit) => winit.import_dmabuf(dmabuf), | 132 | Backend::Winit(winit) => winit.import_dmabuf(dmabuf), |
| 133 | + Backend::Headless(headless) => headless.import_dmabuf(dmabuf), | ||
| 121 | } | 134 | } |
| 122 | } | 135 | } |
| 123 | 136 | ||
| @@ -125,6 +138,7 @@ impl Backend { | |||
| 125 | match self { | 138 | match self { |
| 126 | Backend::Tty(tty) => tty.early_import(surface), | 139 | Backend::Tty(tty) => tty.early_import(surface), |
| 127 | Backend::Winit(_) => (), | 140 | Backend::Winit(_) => (), |
| 141 | + Backend::Headless(_) => (), | ||
| 128 | } | 142 | } |
| 129 | } | 143 | } |
| 130 | 144 | ||
| @@ -132,6 +146,7 @@ impl Backend { | |||
| 132 | match self { | 146 | match self { |
| 133 | Backend::Tty(tty) => tty.ipc_outputs(), | 147 | Backend::Tty(tty) => tty.ipc_outputs(), |
| 134 | Backend::Winit(winit) => winit.ipc_outputs(), | 148 | Backend::Winit(winit) => winit.ipc_outputs(), |
| 149 | + Backend::Headless(headless) => headless.ipc_outputs(), | ||
| 135 | } | 150 | } |
| 136 | } | 151 | } |
| 137 | 152 | ||
| @@ -143,6 +158,7 @@ impl Backend { | |||
| 143 | match self { | 158 | match self { |
| 144 | Backend::Tty(tty) => tty.primary_gbm_device(), | 159 | Backend::Tty(tty) => tty.primary_gbm_device(), |
| 145 | Backend::Winit(_) => None, | 160 | Backend::Winit(_) => None, |
| 161 | + Backend::Headless(_) => None, | ||
| 146 | } | 162 | } |
| 147 | } | 163 | } |
| 148 | 164 | ||
| @@ -150,6 +166,7 @@ impl Backend { | |||
| 150 | match self { | 166 | match self { |
| 151 | Backend::Tty(tty) => tty.set_monitors_active(active), | 167 | Backend::Tty(tty) => tty.set_monitors_active(active), |
| 152 | Backend::Winit(_) => (), | 168 | Backend::Winit(_) => (), |
| 169 | + Backend::Headless(_) => (), | ||
| 153 | } | 170 | } |
| 154 | } | 171 | } |
| 155 | 172 | ||
| @@ -157,6 +174,7 @@ impl Backend { | |||
| 157 | match self { | 174 | match self { |
| 158 | Backend::Tty(tty) => tty.set_output_on_demand_vrr(niri, output, enable_vrr), | 175 | Backend::Tty(tty) => tty.set_output_on_demand_vrr(niri, output, enable_vrr), |
| 159 | Backend::Winit(_) => (), | 176 | Backend::Winit(_) => (), |
| 177 | + Backend::Headless(_) => (), | ||
| 160 | } | 178 | } |
| 161 | } | 179 | } |
| 162 | 180 | ||
| @@ -164,6 +182,7 @@ impl Backend { | |||
| 164 | match self { | 182 | match self { |
| 165 | Backend::Tty(tty) => tty.on_output_config_changed(niri), | 183 | Backend::Tty(tty) => tty.on_output_config_changed(niri), |
| 166 | Backend::Winit(_) => (), | 184 | Backend::Winit(_) => (), |
| 185 | + Backend::Headless(_) => (), | ||
| 167 | } | 186 | } |
| 168 | } | 187 | } |
| 169 | 188 | ||
| @@ -171,6 +190,7 @@ impl Backend { | |||
| 171 | match self { | 190 | match self { |
| 172 | Backend::Tty(tty) => tty.on_debug_config_changed(), | 191 | Backend::Tty(tty) => tty.on_debug_config_changed(), |
| 173 | Backend::Winit(_) => (), | 192 | Backend::Winit(_) => (), |
| 193 | + Backend::Headless(_) => (), | ||
| 174 | } | 194 | } |
| 175 | } | 195 | } |
| 176 | 196 | ||
| @@ -197,4 +217,12 @@ impl Backend { | |||
| 197 | panic!("backend is not Winit") | 217 | panic!("backend is not Winit") |
| 198 | } | 218 | } |
| 199 | } | 219 | } |
| 220 | + | ||
| 221 | + pub fn headless(&mut self) -> &mut Headless { | ||
| 222 | + if let Self::Headless(v) = self { | ||
| 223 | + v | ||
| 224 | + } else { | ||
| 225 | + panic!("backend is not Headless") | ||
| 226 | + } | ||
| 227 | + } | ||
| 200 | } | 228 | } |
src/main.rsmodified+1 -0
| @@ -176,6 +176,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 176 | event_loop.handle(), | 176 | event_loop.handle(), |
| 177 | event_loop.get_signal(), | 177 | event_loop.get_signal(), |
| 178 | display, | 178 | display, |
| 179 | + false, | ||
| 179 | ) | 180 | ) |
| 180 | .unwrap(); | 181 | .unwrap(); |
| 181 | 182 | ||
src/niri.rsmodified+6 -2
| @@ -102,7 +102,7 @@ use smithay::wayland::xdg_foreign::XdgForeignState; | |||
| 102 | 102 | ||
| 103 | use crate::animation::Clock; | 103 | use crate::animation::Clock; |
| 104 | use crate::backend::tty::SurfaceDmabufFeedback; | 104 | use crate::backend::tty::SurfaceDmabufFeedback; |
| 105 | -use crate::backend::{Backend, RenderResult, Tty, Winit}; | 105 | +use crate::backend::{Backend, Headless, RenderResult, Tty, Winit}; |
| 106 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; | 106 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; |
| 107 | #[cfg(feature = "dbus")] | 107 | #[cfg(feature = "dbus")] |
| 108 | use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect}; | 108 | use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect}; |
| @@ -520,6 +520,7 @@ impl State { | |||
| 520 | event_loop: LoopHandle<'static, State>, | 520 | event_loop: LoopHandle<'static, State>, |
| 521 | stop_signal: LoopSignal, | 521 | stop_signal: LoopSignal, |
| 522 | display: Display<State>, | 522 | display: Display<State>, |
| 523 | + headless: bool, | ||
| 523 | ) -> Result<Self, Box<dyn std::error::Error>> { | 524 | ) -> Result<Self, Box<dyn std::error::Error>> { |
| 524 | let _span = tracy_client::span!("State::new"); | 525 | let _span = tracy_client::span!("State::new"); |
| 525 | 526 | ||
| @@ -528,7 +529,10 @@ impl State { | |||
| 528 | let has_display = | 529 | let has_display = |
| 529 | env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some(); | 530 | env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some(); |
| 530 | 531 | ||
| 531 | - let mut backend = if has_display { | 532 | + let mut backend = if headless { |
| 533 | + let headless = Headless::new(); | ||
| 534 | + Backend::Headless(headless) | ||
| 535 | + } else if has_display { | ||
| 532 | let winit = Winit::new(config.clone(), event_loop.clone())?; | 536 | let winit = Winit::new(config.clone(), event_loop.clone())?; |
| 533 | Backend::Winit(winit) | 537 | Backend::Winit(winit) |
| 534 | } else { | 538 | } else { |