| 1 | use std::collections::HashMap; |
| 2 | use std::sync::{Arc, Mutex}; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use niri_config::{Config, ModKey}; |
| 6 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 7 | use smithay::backend::renderer::gles::GlesRenderer; |
| 8 | use smithay::output::Output; |
| 9 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 10 | |
| 11 | use crate::niri::Niri; |
| 12 | use crate::utils::id::IdCounter; |
| 13 | |
| 14 | pub mod tty; |
| 15 | pub use tty::Tty; |
| 16 | |
| 17 | pub mod winit; |
| 18 | pub use winit::Winit; |
| 19 | |
| 20 | pub mod headless; |
| 21 | pub use headless::Headless; |
| 22 | |
| 23 | pub enum Backend { |
| 24 | Tty(Tty), |
| 25 | Winit(Winit), |
| 26 | Headless(Headless), |
| 27 | } |
| 28 | |
| 29 | #[derive(PartialEq, Eq)] |
| 30 | pub enum RenderResult { |
| 31 | |
| 32 | Submitted, |
| 33 | |
| 34 | NoDamage, |
| 35 | |
| 36 | Skipped, |
| 37 | } |
| 38 | |
| 39 | pub type IpcOutputMap = HashMap<OutputId, niri_ipc::Output>; |
| 40 | |
| 41 | static OUTPUT_ID_COUNTER: IdCounter = IdCounter::new(); |
| 42 | |
| 43 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 44 | pub struct OutputId(u64); |
| 45 | |
| 46 | impl OutputId { |
| 47 | fn next() -> OutputId { |
| 48 | OutputId(OUTPUT_ID_COUNTER.next()) |
| 49 | } |
| 50 | |
| 51 | pub fn get(self) -> u64 { |
| 52 | self.0 |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl Backend { |
| 57 | pub fn init(&mut self, niri: &mut Niri) { |
| 58 | match self { |
| 59 | Backend::Tty(tty) => tty.init(niri), |
| 60 | Backend::Winit(winit) => winit.init(niri), |
| 61 | Backend::Headless(headless) => headless.init(niri), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn seat_name(&self) -> String { |
| 66 | match self { |
| 67 | Backend::Tty(tty) => tty.seat_name(), |
| 68 | Backend::Winit(winit) => winit.seat_name(), |
| 69 | Backend::Headless(headless) => headless.seat_name(), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn with_primary_renderer<T>( |
| 74 | &mut self, |
| 75 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 76 | ) -> Option<T> { |
| 77 | match self { |
| 78 | Backend::Tty(tty) => tty.with_primary_renderer(f), |
| 79 | Backend::Winit(winit) => winit.with_primary_renderer(f), |
| 80 | Backend::Headless(headless) => headless.with_primary_renderer(f), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn render( |
| 85 | &mut self, |
| 86 | niri: &mut Niri, |
| 87 | output: &Output, |
| 88 | target_presentation_time: Duration, |
| 89 | ) -> RenderResult { |
| 90 | match self { |
| 91 | Backend::Tty(tty) => tty.render(niri, output, target_presentation_time), |
| 92 | Backend::Winit(winit) => winit.render(niri, output), |
| 93 | Backend::Headless(headless) => headless.render(niri, output), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn mod_key(&self, config: &Config) -> ModKey { |
| 98 | match self { |
| 99 | Backend::Winit(_) => config.input.mod_key_nested.unwrap_or({ |
| 100 | if let Some(ModKey::Alt) = config.input.mod_key { |
| 101 | ModKey::Super |
| 102 | } else { |
| 103 | ModKey::Alt |
| 104 | } |
| 105 | }), |
| 106 | Backend::Tty(_) | Backend::Headless(_) => config.input.mod_key.unwrap_or(ModKey::Super), |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn change_vt(&mut self, vt: i32) { |
| 111 | match self { |
| 112 | Backend::Tty(tty) => tty.change_vt(vt), |
| 113 | Backend::Winit(_) => (), |
| 114 | Backend::Headless(_) => (), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn suspend(&mut self) { |
| 119 | match self { |
| 120 | Backend::Tty(tty) => tty.suspend(), |
| 121 | Backend::Winit(_) => (), |
| 122 | Backend::Headless(_) => (), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | pub fn toggle_debug_tint(&mut self) { |
| 127 | match self { |
| 128 | Backend::Tty(tty) => tty.toggle_debug_tint(), |
| 129 | Backend::Winit(winit) => winit.toggle_debug_tint(), |
| 130 | Backend::Headless(_) => (), |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool { |
| 135 | match self { |
| 136 | Backend::Tty(tty) => tty.import_dmabuf(dmabuf), |
| 137 | Backend::Winit(winit) => winit.import_dmabuf(dmabuf), |
| 138 | Backend::Headless(headless) => headless.import_dmabuf(dmabuf), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | pub fn early_import(&mut self, surface: &WlSurface) { |
| 143 | match self { |
| 144 | Backend::Tty(tty) => tty.early_import(surface), |
| 145 | Backend::Winit(_) => (), |
| 146 | Backend::Headless(_) => (), |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 151 | match self { |
| 152 | Backend::Tty(tty) => tty.ipc_outputs(), |
| 153 | Backend::Winit(winit) => winit.ipc_outputs(), |
| 154 | Backend::Headless(headless) => headless.ipc_outputs(), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[cfg(feature = "xdp-gnome-screencast")] |
| 159 | pub fn gbm_device( |
| 160 | &self, |
| 161 | ) -> Option<smithay::backend::allocator::gbm::GbmDevice<smithay::backend::drm::DrmDeviceFd>> |
| 162 | { |
| 163 | match self { |
| 164 | Backend::Tty(tty) => tty.primary_gbm_device(), |
| 165 | Backend::Winit(_) => None, |
| 166 | Backend::Headless(_) => None, |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | pub fn set_monitors_active(&mut self, active: bool) { |
| 171 | match self { |
| 172 | Backend::Tty(tty) => tty.set_monitors_active(active), |
| 173 | Backend::Winit(_) => (), |
| 174 | Backend::Headless(_) => (), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | pub fn set_output_on_demand_vrr(&mut self, niri: &mut Niri, output: &Output, enable_vrr: bool) { |
| 179 | match self { |
| 180 | Backend::Tty(tty) => tty.set_output_on_demand_vrr(niri, output, enable_vrr), |
| 181 | Backend::Winit(_) => (), |
| 182 | Backend::Headless(_) => (), |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | pub fn on_output_config_changed(&mut self, niri: &mut Niri) { |
| 187 | match self { |
| 188 | Backend::Tty(tty) => tty.on_output_config_changed(niri), |
| 189 | Backend::Winit(_) => (), |
| 190 | Backend::Headless(_) => (), |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | pub fn tty_checked(&mut self) -> Option<&mut Tty> { |
| 195 | if let Self::Tty(v) = self { |
| 196 | Some(v) |
| 197 | } else { |
| 198 | None |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | pub fn tty(&mut self) -> &mut Tty { |
| 203 | if let Self::Tty(v) = self { |
| 204 | v |
| 205 | } else { |
| 206 | panic!("backend is not Tty"); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | pub fn winit(&mut self) -> &mut Winit { |
| 211 | if let Self::Winit(v) = self { |
| 212 | v |
| 213 | } else { |
| 214 | panic!("backend is not Winit") |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | pub fn headless(&mut self) -> &mut Headless { |
| 219 | if let Self::Headless(v) = self { |
| 220 | v |
| 221 | } else { |
| 222 | panic!("backend is not Headless") |
| 223 | } |
| 224 | } |
| 225 | } |