niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 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 | #[allow(clippy::large_enum_variant)] |
| 24 | pub enum Backend { |
| 25 | Tty(Tty), |
| 26 | Winit(Winit), |
| 27 | Headless(Headless), |
| 28 | } |
| 29 | |
| 30 | #[derive(PartialEq, Eq)] |
| 31 | pub enum RenderResult { |
| 32 | /// The frame was submitted to the backend for presentation. |
| 33 | Submitted, |
| 34 | /// Rendering succeeded, but there was no damage. |
| 35 | NoDamage, |
| 36 | /// The frame was not rendered and submitted, due to an error or otherwise. |
| 37 | Skipped, |
| 38 | } |
| 39 | |
| 40 | pub type IpcOutputMap = HashMap<OutputId, niri_ipc::Output>; |
| 41 | |
| 42 | static OUTPUT_ID_COUNTER: IdCounter = IdCounter::new(); |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 45 | pub struct OutputId(u64); |
| 46 | |
| 47 | impl OutputId { |
| 48 | fn next() -> OutputId { |
| 49 | OutputId(OUTPUT_ID_COUNTER.next()) |
| 50 | } |
| 51 | |
| 52 | pub fn get(self) -> u64 { |
| 53 | self.0 |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Backend { |
| 58 | pub fn init(&mut self, niri: &mut Niri) { |
| 59 | match self { |
| 60 | Backend::Tty(tty) => tty.init(niri), |
| 61 | Backend::Winit(winit) => winit.init(niri), |
| 62 | Backend::Headless(headless) => headless.init(niri), |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn seat_name(&self) -> String { |
| 67 | match self { |
| 68 | Backend::Tty(tty) => tty.seat_name(), |
| 69 | Backend::Winit(winit) => winit.seat_name(), |
| 70 | Backend::Headless(headless) => headless.seat_name(), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn with_primary_renderer<T>( |
| 75 | &mut self, |
| 76 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 77 | ) -> Option<T> { |
| 78 | match self { |
| 79 | Backend::Tty(tty) => tty.with_primary_renderer(f), |
| 80 | Backend::Winit(winit) => winit.with_primary_renderer(f), |
| 81 | Backend::Headless(headless) => headless.with_primary_renderer(f), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub fn render( |
| 86 | &mut self, |
| 87 | niri: &mut Niri, |
| 88 | output: &Output, |
| 89 | target_presentation_time: Duration, |
| 90 | ) -> RenderResult { |
| 91 | match self { |
| 92 | Backend::Tty(tty) => tty.render(niri, output, target_presentation_time), |
| 93 | Backend::Winit(winit) => winit.render(niri, output), |
| 94 | Backend::Headless(headless) => headless.render(niri, output), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn mod_key(&self, config: &Config) -> ModKey { |
| 99 | match self { |
| 100 | Backend::Winit(_) => config.input.mod_key_nested.unwrap_or({ |
| 101 | if let Some(ModKey::Alt) = config.input.mod_key { |
| 102 | ModKey::Super |
| 103 | } else { |
| 104 | ModKey::Alt |
| 105 | } |
| 106 | }), |
| 107 | Backend::Tty(_) | Backend::Headless(_) => config.input.mod_key.unwrap_or(ModKey::Super), |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | pub fn change_vt(&mut self, vt: i32) { |
| 112 | match self { |
| 113 | Backend::Tty(tty) => tty.change_vt(vt), |
| 114 | Backend::Winit(_) => (), |
| 115 | Backend::Headless(_) => (), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub fn suspend(&mut self) { |
| 120 | match self { |
| 121 | Backend::Tty(tty) => tty.suspend(), |
| 122 | Backend::Winit(_) => (), |
| 123 | Backend::Headless(_) => (), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn toggle_debug_tint(&mut self) { |
| 128 | match self { |
| 129 | Backend::Tty(tty) => tty.toggle_debug_tint(), |
| 130 | Backend::Winit(winit) => winit.toggle_debug_tint(), |
| 131 | Backend::Headless(_) => (), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool { |
| 136 | match self { |
| 137 | Backend::Tty(tty) => tty.import_dmabuf(dmabuf), |
| 138 | Backend::Winit(winit) => winit.import_dmabuf(dmabuf), |
| 139 | Backend::Headless(headless) => headless.import_dmabuf(dmabuf), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub fn early_import(&mut self, surface: &WlSurface) { |
| 144 | match self { |
| 145 | Backend::Tty(tty) => tty.early_import(surface), |
| 146 | Backend::Winit(_) => (), |
| 147 | Backend::Headless(_) => (), |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 152 | match self { |
| 153 | Backend::Tty(tty) => tty.ipc_outputs(), |
| 154 | Backend::Winit(winit) => winit.ipc_outputs(), |
| 155 | Backend::Headless(headless) => headless.ipc_outputs(), |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[cfg(feature = "xdp-gnome-screencast")] |
| 160 | pub fn gbm_device( |
| 161 | &self, |
| 162 | ) -> Option<smithay::backend::allocator::gbm::GbmDevice<smithay::backend::drm::DrmDeviceFd>> |
| 163 | { |
| 164 | match self { |
| 165 | Backend::Tty(tty) => tty.primary_gbm_device(), |
| 166 | Backend::Winit(_) => None, |
| 167 | Backend::Headless(_) => None, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | pub fn set_monitors_active(&mut self, active: bool) { |
| 172 | match self { |
| 173 | Backend::Tty(tty) => tty.set_monitors_active(active), |
| 174 | Backend::Winit(_) => (), |
| 175 | Backend::Headless(_) => (), |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | pub fn set_output_on_demand_vrr(&mut self, niri: &mut Niri, output: &Output, enable_vrr: bool) { |
| 180 | match self { |
| 181 | Backend::Tty(tty) => tty.set_output_on_demand_vrr(niri, output, enable_vrr), |
| 182 | Backend::Winit(_) => (), |
| 183 | Backend::Headless(_) => (), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | pub fn on_output_config_changed(&mut self, niri: &mut Niri) { |
| 188 | match self { |
| 189 | Backend::Tty(tty) => tty.on_output_config_changed(niri), |
| 190 | Backend::Winit(_) => (), |
| 191 | Backend::Headless(_) => (), |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | pub fn tty_checked(&mut self) -> Option<&mut Tty> { |
| 196 | if let Self::Tty(v) = self { |
| 197 | Some(v) |
| 198 | } else { |
| 199 | None |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | pub fn tty(&mut self) -> &mut Tty { |
| 204 | if let Self::Tty(v) = self { |
| 205 | v |
| 206 | } else { |
| 207 | panic!("backend is not Tty"); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | pub fn winit(&mut self) -> &mut Winit { |
| 212 | if let Self::Winit(v) = self { |
| 213 | v |
| 214 | } else { |
| 215 | panic!("backend is not Winit") |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | pub fn headless(&mut self) -> &mut Headless { |
| 220 | if let Self::Headless(v) = self { |
| 221 | v |
| 222 | } else { |
| 223 | panic!("backend is not Headless") |
| 224 | } |
| 225 | } |
| 226 | } |