forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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