forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

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