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::SetFullscreen(id, mode) => {
110 let rec: &mut LinuxWindowRecord = self
111 .get_mut_windows()
112 .get_mut(&id)
113 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
114 rec.get_mut_config().set_fullscreen(mode);
115 rec.get_mut_state().set_fullscreen(mode);
116 Ok(())
117 }
118 WindowCommand::SetPosition(id, _, _)
119 | WindowCommand::SetResizable(id, _)
120 | WindowCommand::SetDecorations(id, _)
121 | WindowCommand::SetAlwaysOnTop(id, _)
122 | WindowCommand::SetVisible(id, _)
123 | WindowCommand::SetCursor(id, _)
124 | WindowCommand::SetIme(id, _, _)
125 | WindowCommand::SetStatusBarStyle(id, _) => {
126 if !self.get_windows().contains_key(&id) {
127 return Err(PlatformError::WindowNotFound(id.get_value()));
128 }
129 Ok(())
130 }
131 }
132 }
133}
134
135/// `PlatformBackend` implementation for native Linux.
136impl PlatformBackend for LinuxPlatform {
137 /// Returns `PlatformKind::Linux`.
138 ///
139 /// # Arguments
140 ///
141 /// - `&Self` - Platform reference.
142 ///
143 /// # Returns
144 ///
145 /// - `PlatformKind` - `PlatformKind::Linux`.
146 fn kind(&self) -> PlatformKind {
147 PlatformKind::Linux
148 }
149
150 /// Returns Linux desktop capabilities.
151 ///
152 /// # Arguments
153 ///
154 /// - `&Self` - Platform reference.
155 ///
156 /// # Returns
157 ///
158 /// - `PlatformCapabilities` - Linux capability flags.
159 fn capabilities(&self) -> PlatformCapabilities {
160 PlatformCapabilities::for_platform(PlatformKind::Linux)
161 }
162
163 /// Opens an X11 or Wayland window via dynamic `dlopen`.
164 ///
165 /// # Arguments
166 ///
167 /// - `&Self` - Mutable Linux platform.
168 /// - `&WindowConfig` - Declarative window config.
169 ///
170 /// # Returns
171 ///
172 /// - `Result<WindowState, PlatformError>` - Initial window state.
173 fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
174 let native: LinuxNativeWindowHandle = linux_create_native_window(config)?;
175 if !native.get_is_wayland() {
176 self.set_x11_lib(native.get_lib());
177 }
178 let id: WindowId = WindowId::new(self.get_next_id());
179 self.set_next_id(self.get_next_id().wrapping_add(1));
180 let mut state: WindowState =
181 WindowState::from_config(id, config, native.get_scale_factor());
182 let hz: f32 = if native.get_is_wayland() {
183 linux_display_refresh_rate_hz(null_mut(), 0)
184 } else {
185 linux_display_refresh_rate_hz(
186 native.get_display() as *mut c_void,
187 native.get_window() as usize,
188 )
189 };
190 state
191 .get_mut_display()
192 .set_refresh_rate_hz(hz.round() as u32);
193 self.get_mut_windows().insert(
194 id,
195 LinuxWindowRecord {
196 config: config.clone(),
197 state,
198 is_wayland: native.get_is_wayland(),
199 display: native.get_display(),
200 window: native.get_window(),
201 gc: native.get_gc(),
202 visual: native.get_visual(),
203 depth: native.get_depth(),
204 wm_delete_atom: native.get_wm_delete_atom(),
205 },
206 );
207 Ok(state)
208 }
209
210 /// Queries live `WindowState` for `id`.
211 ///
212 /// # Arguments
213 ///
214 /// - `&Self` - Platform reference.
215 /// - `WindowId` - Window handle.
216 ///
217 /// # Returns
218 ///
219 /// - `Option<WindowState>` - Window state if open.
220 fn window_state(&self, id: WindowId) -> Option<WindowState> {
221 self.get_windows()
222 .get(&id)
223 .map(LinuxWindowRecord::get_state)
224 }
225
226 /// Returns `RawWindowHandle::Xlib` or `RawWindowHandle::Wayland` for Vulkan/EGL surface binding.
227 ///
228 /// # Arguments
229 ///
230 /// - `&Self` - Platform reference.
231 /// - `WindowId` - Window handle.
232 ///
233 /// # Returns
234 ///
235 /// - `RawWindowHandle` - Native Linux display/window handle.
236 fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
237 match self.get_windows().get(&id) {
238 Some(rec) => {
239 if rec.get_is_wayland() {
240 RawWindowHandle::Wayland {
241 surface: rec.get_window() as usize,
242 display: rec.get_display(),
243 }
244 } else {
245 RawWindowHandle::Xlib {
246 window: rec.get_window(),
247 display: rec.get_display(),
248 }
249 }
250 }
251 None => RawWindowHandle::Headless,
252 }
253 }
254
255 /// Pumps pending X11 events and drains queued Linux events.
256 ///
257 /// # Arguments
258 ///
259 /// - `&Self` - Mutable Linux platform.
260 ///
261 /// # Returns
262 ///
263 /// - `Vec<WindowEvent>` - Pending events.
264 fn poll_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 linux_x11_pump_events(lib, windows, pending_events);
272 take(pending_events)
273 }
274
275 /// Blocks on X11 `XNextEvent` with zero CPU usage until an OS event wakes the connection, then drains queued events.
276 ///
277 /// # Arguments
278 ///
279 /// - `&Self` - Mutable Linux platform.
280 ///
281 /// # Returns
282 ///
283 /// - `Vec<WindowEvent>` - Events received upon wake.
284 fn wait_for_events(&mut self) -> Vec<WindowEvent> {
285 let lib: usize = self.get_x11_lib();
286 let Self {
287 windows,
288 pending_events,
289 ..
290 } = self;
291 if pending_events.is_empty() && !windows.is_empty() {
292 linux_x11_wait_events(lib, windows, pending_events);
293 }
294 take(pending_events)
295 }
296
297 /// Updates Linux background frame-pump pause state.
298 ///
299 /// # Arguments
300 ///
301 /// - `&Self` - Mutable Linux platform.
302 /// - `bool` - `true` when paused in background; `false` when active.
303 fn set_frame_pump_paused(&mut self, paused: bool) {
304 self.set_pump_paused(paused);
305 }
306
307 /// Returns whether the Linux frame pump is currently paused for background freeze.
308 ///
309 /// # Arguments
310 ///
311 /// - `&Self` - Platform reference.
312 ///
313 /// # Returns
314 ///
315 /// - `bool` - `true` when paused in background.
316 fn is_frame_pump_paused(&self) -> bool {
317 self.get_pump_paused()
318 }
319
320 /// Presents `framebuffer` to the active Linux window surface via X11 `XCreateImage` + `XPutImage`.
321 ///
322 /// # Arguments
323 ///
324 /// - `&Self` - Mutable Linux platform.
325 /// - `WindowId` - Target window handle.
326 /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
327 ///
328 /// # Returns
329 ///
330 /// - `Result<(), PlatformError>` - Presentation status.
331 fn present_framebuffer(
332 &mut self,
333 id: WindowId,
334 framebuffer: &Framebuffer,
335 ) -> Result<(), PlatformError> {
336 let lib: usize = self.get_x11_lib();
337 let rec: &LinuxWindowRecord = self
338 .get_windows()
339 .get(&id)
340 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
341 linux_x11_present_framebuffer(lib, rec, framebuffer)
342 }
343
344 /// Reads cached clipboard text.
345 ///
346 /// # Arguments
347 ///
348 /// - `&Self` - Platform reference.
349 ///
350 /// # Returns
351 ///
352 /// - `Option<String>` - Clipboard contents.
353 fn read_clipboard(&self) -> Option<String> {
354 let Self {
355 clipboard_cache, ..
356 } = self;
357 clipboard_cache.clone()
358 }
359
360 /// Writes `text` to clipboard cache.
361 ///
362 /// # Arguments
363 ///
364 /// - `&Self` - Mutable platform.
365 /// - `&str` - Clipboard text.
366 ///
367 /// # Returns
368 ///
369 /// - `Result<(), PlatformError>` - Write status.
370 fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
371 self.set_clipboard_cache(Some(text.to_string()));
372 let _: bool = write_system_clipboard(text);
373 Ok(())
374 }
375
376 /// Returns active system theme mode.
377 ///
378 /// # Arguments
379 ///
380 /// - `&Self` - Platform reference.
381 ///
382 /// # Returns
383 ///
384 /// - `ThemeMode` - Active appearance mode.
385 fn system_theme(&self) -> ThemeMode {
386 ThemeMode::Light
387 }
388
389 /// Queries the native Linux display refresh rate via XRandR or `EUV_REFRESH_RATE_HZ`.
390 ///
391 /// # Arguments
392 ///
393 /// - `&Self` - Platform reference.
394 ///
395 /// # Returns
396 ///
397 /// - `f32` - Active display refresh rate in Hertz.
398 fn display_refresh_rate_hz(&self) -> f32 {
399 let (display, window): (*mut c_void, usize) = self
400 .get_windows()
401 .values()
402 .find(|rec: &&LinuxWindowRecord| !rec.get_is_wayland())
403 .map(|rec: &LinuxWindowRecord| {
404 (rec.get_display() as *mut c_void, rec.get_window() as usize)
405 })
406 .unwrap_or((null_mut(), 0));
407 linux_display_refresh_rate_hz(display, window)
408 }
409}