euv_core/platform/linux/
impl.rs1use super::*;
2
3impl Default for LinuxPlatform {
5 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
21impl WindowController for LinuxPlatform {
23 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
112impl PlatformBackend for LinuxPlatform {
114 fn kind(&self) -> PlatformKind {
124 PlatformKind::Linux
125 }
126
127 fn capabilities(&self) -> PlatformCapabilities {
137 PlatformCapabilities::for_platform(PlatformKind::Linux)
138 }
139
140 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 fn window_state(&self, id: WindowId) -> Option<WindowState> {
186 self.get_windows()
187 .get(&id)
188 .map(LinuxWindowRecord::get_state)
189 }
190
191 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 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 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 fn read_clipboard(&self) -> Option<String> {
274 let Self {
275 clipboard_cache, ..
276 } = self;
277 clipboard_cache.clone()
278 }
279
280 fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
291 self.set_clipboard_cache(Some(text.to_string()));
292 Ok(())
293 }
294
295 fn system_theme(&self) -> ThemeMode {
305 ThemeMode::Light
306 }
307
308 fn display_refresh_rate_hz(&self) -> f32 {
318 let (display, window): (*mut c_void, usize) = self
319 .get_windows()
320 .values()
321 .find(|rec: &&LinuxWindowRecord| !rec.get_is_wayland())
322 .map(|rec: &LinuxWindowRecord| {
323 (rec.get_display() as *mut c_void, rec.get_window() as usize)
324 })
325 .unwrap_or((null_mut(), 0));
326 linux_display_refresh_rate_hz(display, window)
327 }
328}