euv_core/platform/linux/impl.rs
1use super::*;
2
3/// Default initialization for `LinuxPlatform`.
4impl Default for LinuxPlatform {
5 /// Creates an empty Linux X11 / Wayland platform backend.
6 ///
7 /// # Returns
8 ///
9 /// - `Self` - Initialized Linux backend.
10 fn default() -> Self {
11 Self {
12 next_id: 1,
13 x11_lib: 0,
14 windows: HashMap::new(),
15 pending_events: Vec::new(),
16 clipboard_cache: None,
17 pump_paused: false,
18 }
19 }
20}
21
22/// X11/Wayland system event injection helpers for `LinuxPlatform`.
23impl LinuxPlatform {
24 /// Enqueues a normalized Linux `SystemEvent` to be drained on the next `poll_events()` tick.
25 ///
26 /// # Arguments
27 ///
28 /// - `&Self` - Mutable Linux platform backend.
29 /// - `SystemEvent` - System event to enqueue.
30 pub fn push_system_event(&mut self, event: SystemEvent) {
31 self.get_mut_pending_events()
32 .push(WindowEvent::System(event));
33 }
34}
35
36/// `WindowController` implementation for Linux X11 / Wayland.
37impl WindowController for LinuxPlatform {
38 /// Applies a `WindowCommand` onto the native X11 or Wayland window.
39 ///
40 /// # Arguments
41 ///
42 /// - `&Self` - Mutable Linux platform.
43 /// - `WindowCommand` - Command to apply.
44 ///
45 /// # Returns
46 ///
47 /// - `Result<(), PlatformError>` - Execution status.
48 fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
49 let lib: usize = self.get_x11_lib();
50 match command {
51 WindowCommand::SetTitle(id, title) => {
52 let rec: &mut LinuxWindowRecord = self
53 .get_mut_windows()
54 .get_mut(&id)
55 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
56 if !rec.get_is_wayland() {
57 linux_x11_set_title(lib, rec.get_display(), rec.get_window(), &title);
58 }
59 rec.get_mut_config().set_title(title);
60 Ok(())
61 }
62 WindowCommand::SetSize(id, width, height) => {
63 let rec: &mut LinuxWindowRecord = self
64 .get_mut_windows()
65 .get_mut(&id)
66 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
67 let w: u32 = width.max(1);
68 let h: u32 = height.max(1);
69 if !rec.get_is_wayland() {
70 linux_x11_resize(lib, rec.get_display(), rec.get_window(), w, h);
71 }
72 let scale: f32 = rec.get_state().get_scale_factor();
73 rec.get_mut_config().set_width(w);
74 rec.get_mut_config().set_height(h);
75 rec.get_mut_state().set_logical_width(w as f32);
76 rec.get_mut_state().set_logical_height(h as f32);
77 rec.get_mut_state()
78 .set_physical_width(((w as f32) * scale).round() as u32);
79 rec.get_mut_state()
80 .set_physical_height(((h as f32) * scale).round() as u32);
81 Ok(())
82 }
83 WindowCommand::Close(id) => {
84 let rec: LinuxWindowRecord = self
85 .get_mut_windows()
86 .remove(&id)
87 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
88 if !rec.get_is_wayland() {
89 linux_x11_destroy(lib, rec.get_display(), rec.get_window());
90 }
91 Ok(())
92 }
93 WindowCommand::SetRenderMode(id, mode) => {
94 let rec: &mut LinuxWindowRecord = self
95 .get_mut_windows()
96 .get_mut(&id)
97 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
98 rec.get_mut_config().get_mut_render().set_mode(mode);
99 Ok(())
100 }
101 WindowCommand::RequestRedraw(id) => {
102 if !self.get_windows().contains_key(&id) {
103 return Err(PlatformError::WindowNotFound(id.get_value()));
104 }
105 self.get_mut_pending_events()
106 .push(WindowEvent::RedrawRequested(id));
107 Ok(())
108 }
109 WindowCommand::SetPosition(id, _, _)
110 | WindowCommand::SetResizable(id, _)
111 | WindowCommand::SetDecorations(id, _)
112 | WindowCommand::SetAlwaysOnTop(id, _)
113 | WindowCommand::SetFullscreen(id, _)
114 | WindowCommand::SetVisible(id, _)
115 | WindowCommand::SetCursor(id, _)
116 | WindowCommand::SetIme(id, _, _)
117 | WindowCommand::SetStatusBarStyle(id, _) => {
118 if !self.get_windows().contains_key(&id) {
119 return Err(PlatformError::WindowNotFound(id.get_value()));
120 }
121 Ok(())
122 }
123 }
124 }
125}
126
127/// `PlatformBackend` implementation for native Linux.
128impl PlatformBackend for LinuxPlatform {
129 /// Returns `PlatformKind::Linux`.
130 ///
131 /// # Arguments
132 ///
133 /// - `&Self` - Platform reference.
134 ///
135 /// # Returns
136 ///
137 /// - `PlatformKind` - `PlatformKind::Linux`.
138 fn kind(&self) -> PlatformKind {
139 PlatformKind::Linux
140 }
141
142 /// Returns Linux desktop capabilities.
143 ///
144 /// # Arguments
145 ///
146 /// - `&Self` - Platform reference.
147 ///
148 /// # Returns
149 ///
150 /// - `PlatformCapabilities` - Linux capability flags.
151 fn capabilities(&self) -> PlatformCapabilities {
152 PlatformCapabilities::for_platform(PlatformKind::Linux)
153 }
154
155 /// Opens an X11 or Wayland window via dynamic `dlopen`.
156 ///
157 /// # Arguments
158 ///
159 /// - `&Self` - Mutable Linux platform.
160 /// - `&WindowConfig` - Declarative window config.
161 ///
162 /// # Returns
163 ///
164 /// - `Result<WindowState, PlatformError>` - Initial window state.
165 fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
166 let native: LinuxNativeWindowHandle = linux_create_native_window(config)?;
167 if !native.get_is_wayland() {
168 self.set_x11_lib(native.get_lib());
169 }
170 let id: WindowId = WindowId::new(self.get_next_id());
171 self.set_next_id(self.get_next_id().wrapping_add(1));
172 let state: WindowState = WindowState::from_config(id, config, native.get_scale_factor());
173 self.get_mut_windows().insert(
174 id,
175 LinuxWindowRecord {
176 config: config.clone(),
177 state,
178 is_wayland: native.get_is_wayland(),
179 display: native.get_display(),
180 window: native.get_window(),
181 gc: native.get_gc(),
182 visual: native.get_visual(),
183 depth: native.get_depth(),
184 wm_delete_atom: native.get_wm_delete_atom(),
185 },
186 );
187 Ok(state)
188 }
189
190 /// Queries live `WindowState` for `id`.
191 ///
192 /// # Arguments
193 ///
194 /// - `&Self` - Platform reference.
195 /// - `WindowId` - Window handle.
196 ///
197 /// # Returns
198 ///
199 /// - `Option<WindowState>` - Window state if open.
200 fn window_state(&self, id: WindowId) -> Option<WindowState> {
201 self.get_windows()
202 .get(&id)
203 .map(LinuxWindowRecord::get_state)
204 }
205
206 /// Returns `RawWindowHandle::Xlib` or `RawWindowHandle::Wayland` for Vulkan/EGL surface binding.
207 ///
208 /// # Arguments
209 ///
210 /// - `&Self` - Platform reference.
211 /// - `WindowId` - Window handle.
212 ///
213 /// # Returns
214 ///
215 /// - `RawWindowHandle` - Native Linux display/window handle.
216 fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
217 match self.get_windows().get(&id) {
218 Some(rec) => {
219 if rec.get_is_wayland() {
220 RawWindowHandle::Wayland {
221 surface: rec.get_window() as usize,
222 display: rec.get_display(),
223 }
224 } else {
225 RawWindowHandle::Xlib {
226 window: rec.get_window(),
227 display: rec.get_display(),
228 }
229 }
230 }
231 None => RawWindowHandle::Headless,
232 }
233 }
234
235 /// Pumps pending X11 events and drains queued Linux events.
236 ///
237 /// # Arguments
238 ///
239 /// - `&Self` - Mutable Linux platform.
240 ///
241 /// # Returns
242 ///
243 /// - `Vec<WindowEvent>` - Pending events.
244 fn poll_events(&mut self) -> Vec<WindowEvent> {
245 let lib: usize = self.get_x11_lib();
246 let Self {
247 windows,
248 pending_events,
249 ..
250 } = self;
251 linux_x11_pump_events(lib, windows, pending_events);
252 take(pending_events)
253 }
254
255 /// Blocks on X11 `XNextEvent` with zero CPU usage until an OS event wakes the connection, then drains queued events.
256 ///
257 /// # Arguments
258 ///
259 /// - `&Self` - Mutable Linux platform.
260 ///
261 /// # Returns
262 ///
263 /// - `Vec<WindowEvent>` - Events received upon wake.
264 fn wait_for_events(&mut self) -> Vec<WindowEvent> {
265 let lib: usize = self.get_x11_lib();
266 let Self {
267 windows,
268 pending_events,
269 ..
270 } = self;
271 if pending_events.is_empty() && !windows.is_empty() {
272 linux_x11_wait_events(lib, windows, pending_events);
273 }
274 take(pending_events)
275 }
276
277 /// Updates Linux background frame-pump pause state.
278 ///
279 /// # Arguments
280 ///
281 /// - `&Self` - Mutable Linux platform.
282 /// - `bool` - `true` when paused in background; `false` when active.
283 fn set_frame_pump_paused(&mut self, paused: bool) {
284 self.set_pump_paused(paused);
285 }
286
287 /// Returns whether the Linux frame pump is currently paused for background freeze.
288 ///
289 /// # Arguments
290 ///
291 /// - `&Self` - Platform reference.
292 ///
293 /// # Returns
294 ///
295 /// - `bool` - `true` when paused in background.
296 fn is_frame_pump_paused(&self) -> bool {
297 self.get_pump_paused()
298 }
299
300 /// Presents `framebuffer` to the active Linux window surface via X11 `XCreateImage` + `XPutImage`.
301 ///
302 /// # Arguments
303 ///
304 /// - `&Self` - Mutable Linux platform.
305 /// - `WindowId` - Target window handle.
306 /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
307 ///
308 /// # Returns
309 ///
310 /// - `Result<(), PlatformError>` - Presentation status.
311 fn present_framebuffer(
312 &mut self,
313 id: WindowId,
314 framebuffer: &Framebuffer,
315 ) -> Result<(), PlatformError> {
316 let lib: usize = self.get_x11_lib();
317 let rec: &LinuxWindowRecord = self
318 .get_windows()
319 .get(&id)
320 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
321 linux_x11_present_framebuffer(lib, rec, framebuffer)
322 }
323
324 /// Reads cached clipboard text.
325 ///
326 /// # Arguments
327 ///
328 /// - `&Self` - Platform reference.
329 ///
330 /// # Returns
331 ///
332 /// - `Option<String>` - Clipboard contents.
333 fn read_clipboard(&self) -> Option<String> {
334 let Self {
335 clipboard_cache, ..
336 } = self;
337 clipboard_cache.clone()
338 }
339
340 /// Writes `text` to clipboard cache.
341 ///
342 /// # Arguments
343 ///
344 /// - `&Self` - Mutable platform.
345 /// - `&str` - Clipboard text.
346 ///
347 /// # Returns
348 ///
349 /// - `Result<(), PlatformError>` - Write status.
350 fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
351 self.set_clipboard_cache(Some(text.to_string()));
352 let _: bool = write_system_clipboard(text);
353 Ok(())
354 }
355
356 /// Returns active system theme mode.
357 ///
358 /// # Arguments
359 ///
360 /// - `&Self` - Platform reference.
361 ///
362 /// # Returns
363 ///
364 /// - `ThemeMode` - Active appearance mode.
365 fn system_theme(&self) -> ThemeMode {
366 ThemeMode::Light
367 }
368
369 /// Queries the native Linux display refresh rate via XRandR or `EUV_REFRESH_RATE_HZ`.
370 ///
371 /// # Arguments
372 ///
373 /// - `&Self` - Platform reference.
374 ///
375 /// # Returns
376 ///
377 /// - `f32` - Active display refresh rate in Hertz.
378 fn display_refresh_rate_hz(&self) -> f32 {
379 let (display, window): (*mut c_void, usize) = self
380 .get_windows()
381 .values()
382 .find(|rec: &&LinuxWindowRecord| !rec.get_is_wayland())
383 .map(|rec: &LinuxWindowRecord| {
384 (rec.get_display() as *mut c_void, rec.get_window() as usize)
385 })
386 .unwrap_or((null_mut(), 0));
387 linux_display_refresh_rate_hz(display, window)
388 }
389}