Skip to main content

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        }
18    }
19}
20
21/// `WindowController` implementation for Linux X11 / Wayland.
22impl WindowController for LinuxPlatform {
23    /// Applies a `WindowCommand` onto the native X11 or Wayland window.
24    ///
25    /// # Arguments
26    ///
27    /// - `&Self` - Mutable Linux platform.
28    /// - `WindowCommand` - Command to apply.
29    ///
30    /// # Returns
31    ///
32    /// - `Result<(), PlatformError>` - Execution status.
33    fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
34        let lib: usize = self.get_x11_lib();
35        match command {
36            WindowCommand::SetTitle(id, title) => {
37                let rec: &mut LinuxWindowRecord = self
38                    .get_mut_windows()
39                    .get_mut(&id)
40                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
41                if !rec.get_is_wayland() {
42                    linux_x11_set_title(lib, rec.get_display(), rec.get_window(), &title);
43                }
44                rec.get_mut_config().set_title(title);
45                Ok(())
46            }
47            WindowCommand::SetSize(id, width, height) => {
48                let rec: &mut LinuxWindowRecord = self
49                    .get_mut_windows()
50                    .get_mut(&id)
51                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
52                let w: u32 = width.max(1);
53                let h: u32 = height.max(1);
54                if !rec.get_is_wayland() {
55                    linux_x11_resize(lib, rec.get_display(), rec.get_window(), w, h);
56                }
57                let scale: f32 = rec.get_state().get_scale_factor();
58                rec.get_mut_config().set_width(w);
59                rec.get_mut_config().set_height(h);
60                rec.get_mut_state().set_logical_width(w as f32);
61                rec.get_mut_state().set_logical_height(h as f32);
62                rec.get_mut_state()
63                    .set_physical_width(((w as f32) * scale).round() as u32);
64                rec.get_mut_state()
65                    .set_physical_height(((h as f32) * scale).round() as u32);
66                Ok(())
67            }
68            WindowCommand::Close(id) => {
69                let rec: LinuxWindowRecord = self
70                    .get_mut_windows()
71                    .remove(&id)
72                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
73                if !rec.get_is_wayland() {
74                    linux_x11_destroy(lib, rec.get_display(), rec.get_window());
75                }
76                Ok(())
77            }
78            WindowCommand::SetRenderMode(id, mode) => {
79                let rec: &mut LinuxWindowRecord = self
80                    .get_mut_windows()
81                    .get_mut(&id)
82                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
83                rec.get_mut_config().get_mut_render().set_mode(mode);
84                Ok(())
85            }
86            WindowCommand::RequestRedraw(id) => {
87                if !self.get_windows().contains_key(&id) {
88                    return Err(PlatformError::WindowNotFound(id.get_value()));
89                }
90                self.get_mut_pending_events()
91                    .push(WindowEvent::RedrawRequested(id));
92                Ok(())
93            }
94            WindowCommand::SetPosition(id, _, _)
95            | WindowCommand::SetResizable(id, _)
96            | WindowCommand::SetDecorations(id, _)
97            | WindowCommand::SetAlwaysOnTop(id, _)
98            | WindowCommand::SetFullscreen(id, _)
99            | WindowCommand::SetVisible(id, _)
100            | WindowCommand::SetCursor(id, _)
101            | WindowCommand::SetIme(id, _, _)
102            | WindowCommand::SetStatusBarStyle(id, _) => {
103                if !self.get_windows().contains_key(&id) {
104                    return Err(PlatformError::WindowNotFound(id.get_value()));
105                }
106                Ok(())
107            }
108        }
109    }
110}
111
112/// `PlatformBackend` implementation for native Linux.
113impl PlatformBackend for LinuxPlatform {
114    /// Returns `PlatformKind::Linux`.
115    ///
116    /// # Arguments
117    ///
118    /// - `&Self` - Platform reference.
119    ///
120    /// # Returns
121    ///
122    /// - `PlatformKind` - `PlatformKind::Linux`.
123    fn kind(&self) -> PlatformKind {
124        PlatformKind::Linux
125    }
126
127    /// Returns Linux desktop capabilities.
128    ///
129    /// # Arguments
130    ///
131    /// - `&Self` - Platform reference.
132    ///
133    /// # Returns
134    ///
135    /// - `PlatformCapabilities` - Linux capability flags.
136    fn capabilities(&self) -> PlatformCapabilities {
137        PlatformCapabilities::for_platform(PlatformKind::Linux)
138    }
139
140    /// Opens an X11 or Wayland window via dynamic `dlopen`.
141    ///
142    /// # Arguments
143    ///
144    /// - `&Self` - Mutable Linux platform.
145    /// - `&WindowConfig` - Declarative window config.
146    ///
147    /// # Returns
148    ///
149    /// - `Result<WindowState, PlatformError>` - Initial window state.
150    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
151        let native: LinuxNativeWindowHandle = linux_create_native_window(config)?;
152        if !native.get_is_wayland() {
153            self.set_x11_lib(native.get_lib());
154        }
155        let id: WindowId = WindowId::new(self.get_next_id());
156        self.set_next_id(self.get_next_id().wrapping_add(1));
157        let state: WindowState = WindowState::from_config(id, config, native.get_scale_factor());
158        self.get_mut_windows().insert(
159            id,
160            LinuxWindowRecord {
161                config: config.clone(),
162                state,
163                is_wayland: native.get_is_wayland(),
164                display: native.get_display(),
165                window: native.get_window(),
166                gc: native.get_gc(),
167                visual: native.get_visual(),
168                depth: native.get_depth(),
169                wm_delete_atom: native.get_wm_delete_atom(),
170            },
171        );
172        Ok(state)
173    }
174
175    /// Queries live `WindowState` for `id`.
176    ///
177    /// # Arguments
178    ///
179    /// - `&Self` - Platform reference.
180    /// - `WindowId` - Window handle.
181    ///
182    /// # Returns
183    ///
184    /// - `Option<WindowState>` - Window state if open.
185    fn window_state(&self, id: WindowId) -> Option<WindowState> {
186        self.get_windows()
187            .get(&id)
188            .map(LinuxWindowRecord::get_state)
189    }
190
191    /// Returns `RawWindowHandle::Xlib` or `RawWindowHandle::Wayland` for Vulkan/EGL surface binding.
192    ///
193    /// # Arguments
194    ///
195    /// - `&Self` - Platform reference.
196    /// - `WindowId` - Window handle.
197    ///
198    /// # Returns
199    ///
200    /// - `RawWindowHandle` - Native Linux display/window handle.
201    fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
202        match self.get_windows().get(&id) {
203            Some(rec) => {
204                if rec.get_is_wayland() {
205                    RawWindowHandle::Wayland {
206                        surface: rec.get_window() as usize,
207                        display: rec.get_display(),
208                    }
209                } else {
210                    RawWindowHandle::Xlib {
211                        window: rec.get_window(),
212                        display: rec.get_display(),
213                    }
214                }
215            }
216            None => RawWindowHandle::Headless,
217        }
218    }
219
220    /// Pumps pending X11 events and drains queued Linux events.
221    ///
222    /// # Arguments
223    ///
224    /// - `&Self` - Mutable Linux platform.
225    ///
226    /// # Returns
227    ///
228    /// - `Vec<WindowEvent>` - Pending events.
229    fn poll_events(&mut self) -> Vec<WindowEvent> {
230        let lib: usize = self.get_x11_lib();
231        let Self {
232            windows,
233            pending_events,
234            ..
235        } = self;
236        linux_x11_pump_events(lib, windows, pending_events);
237        take(pending_events)
238    }
239
240    /// Presents `framebuffer` to the active Linux window surface via X11 `XCreateImage` + `XPutImage`.
241    ///
242    /// # Arguments
243    ///
244    /// - `&Self` - Mutable Linux platform.
245    /// - `WindowId` - Target window handle.
246    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
247    ///
248    /// # Returns
249    ///
250    /// - `Result<(), PlatformError>` - Presentation status.
251    fn present_framebuffer(
252        &mut self,
253        id: WindowId,
254        framebuffer: &Framebuffer,
255    ) -> Result<(), PlatformError> {
256        let lib: usize = self.get_x11_lib();
257        let rec: &LinuxWindowRecord = self
258            .get_windows()
259            .get(&id)
260            .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
261        linux_x11_present_framebuffer(lib, rec, framebuffer)
262    }
263
264    /// Reads cached clipboard text.
265    ///
266    /// # Arguments
267    ///
268    /// - `&Self` - Platform reference.
269    ///
270    /// # Returns
271    ///
272    /// - `Option<String>` - Clipboard contents.
273    fn read_clipboard(&self) -> Option<String> {
274        let Self {
275            clipboard_cache, ..
276        } = self;
277        clipboard_cache.clone()
278    }
279
280    /// Writes `text` to clipboard cache.
281    ///
282    /// # Arguments
283    ///
284    /// - `&Self` - Mutable platform.
285    /// - `&str` - Clipboard text.
286    ///
287    /// # Returns
288    ///
289    /// - `Result<(), PlatformError>` - Write status.
290    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
291        self.set_clipboard_cache(Some(text.to_string()));
292        Ok(())
293    }
294
295    /// Returns active system theme mode.
296    ///
297    /// # Arguments
298    ///
299    /// - `&Self` - Platform reference.
300    ///
301    /// # Returns
302    ///
303    /// - `ThemeMode` - Active appearance mode.
304    fn system_theme(&self) -> ThemeMode {
305        ThemeMode::Light
306    }
307}