| 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 | |
| 13 | pub mod tty; |
| 14 | pub use tty::Tty; |
| 15 | |
| 16 | pub mod winit; |
| 17 | pub use winit::Winit; |
| 18 | |
| 19 | pub enum Backend { |
| 20 | Tty(Tty), |
| 21 | Winit(Winit), |
| 22 | } |
| 23 | |
| 24 | #[derive(PartialEq, Eq)] |
| 25 | pub enum RenderResult { |
| 26 | |
| 27 | Submitted, |
| 28 | |
| 29 | NoDamage, |
| 30 | |
| 31 | Skipped, |
| 32 | } |
| 33 | |
| 34 | pub type IpcOutputMap = HashMap<String, niri_ipc::Output>; |
| 35 | |
| 36 | impl Backend { |
| 37 | pub fn init(&mut self, niri: &mut Niri) { |
| 38 | match self { |
| 39 | Backend::Tty(tty) => tty.init(niri), |
| 40 | Backend::Winit(winit) => winit.init(niri), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn seat_name(&self) -> String { |
| 45 | match self { |
| 46 | Backend::Tty(tty) => tty.seat_name(), |
| 47 | Backend::Winit(winit) => winit.seat_name(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn with_primary_renderer<T>( |
| 52 | &mut self, |
| 53 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 54 | ) -> Option<T> { |
| 55 | match self { |
| 56 | Backend::Tty(tty) => tty.with_primary_renderer(f), |
| 57 | Backend::Winit(winit) => winit.with_primary_renderer(f), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn render( |
| 62 | &mut self, |
| 63 | niri: &mut Niri, |
| 64 | output: &Output, |
| 65 | target_presentation_time: Duration, |
| 66 | ) -> RenderResult { |
| 67 | match self { |
| 68 | Backend::Tty(tty) => tty.render(niri, output, target_presentation_time), |
| 69 | Backend::Winit(winit) => winit.render(niri, output), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn mod_key(&self) -> CompositorMod { |
| 74 | match self { |
| 75 | Backend::Tty(_) => CompositorMod::Super, |
| 76 | Backend::Winit(_) => CompositorMod::Alt, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub fn change_vt(&mut self, vt: i32) { |
| 81 | match self { |
| 82 | Backend::Tty(tty) => tty.change_vt(vt), |
| 83 | Backend::Winit(_) => (), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn suspend(&mut self) { |
| 88 | match self { |
| 89 | Backend::Tty(tty) => tty.suspend(), |
| 90 | Backend::Winit(_) => (), |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn toggle_debug_tint(&mut self) { |
| 95 | match self { |
| 96 | Backend::Tty(tty) => tty.toggle_debug_tint(), |
| 97 | Backend::Winit(winit) => winit.toggle_debug_tint(), |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool { |
| 102 | match self { |
| 103 | Backend::Tty(tty) => tty.import_dmabuf(dmabuf), |
| 104 | Backend::Winit(winit) => winit.import_dmabuf(dmabuf), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn early_import(&mut self, surface: &WlSurface) { |
| 109 | match self { |
| 110 | Backend::Tty(tty) => tty.early_import(surface), |
| 111 | Backend::Winit(_) => (), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 116 | match self { |
| 117 | Backend::Tty(tty) => tty.ipc_outputs(), |
| 118 | Backend::Winit(winit) => winit.ipc_outputs(), |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[cfg(feature = "xdp-gnome-screencast")] |
| 123 | pub fn gbm_device( |
| 124 | &self, |
| 125 | ) -> Option<smithay::backend::allocator::gbm::GbmDevice<smithay::backend::drm::DrmDeviceFd>> |
| 126 | { |
| 127 | match self { |
| 128 | Backend::Tty(tty) => tty.primary_gbm_device(), |
| 129 | Backend::Winit(_) => None, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn set_monitors_active(&mut self, active: bool) { |
| 134 | match self { |
| 135 | Backend::Tty(tty) => tty.set_monitors_active(active), |
| 136 | Backend::Winit(_) => (), |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | pub fn on_output_config_changed(&mut self, niri: &mut Niri) { |
| 141 | match self { |
| 142 | Backend::Tty(tty) => tty.on_output_config_changed(niri), |
| 143 | Backend::Winit(_) => (), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | pub fn tty(&mut self) -> &mut Tty { |
| 148 | if let Self::Tty(v) = self { |
| 149 | v |
| 150 | } else { |
| 151 | panic!("backend is not Tty"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | pub fn winit(&mut self) -> &mut Winit { |
| 156 | if let Self::Winit(v) = self { |
| 157 | v |
| 158 | } else { |
| 159 | panic!("backend is not Winit") |
| 160 | } |
| 161 | } |
| 162 | } |