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