1#![allow(unused)]
2
3use std::env;
4use std::{
5 path::{Path, PathBuf},
6 rc::Rc,
7 sync::Arc,
8 time::Duration,
9};
10
11use async_task::Runnable;
12use flume::{Receiver, Sender};
13use futures::channel::oneshot;
14use parking_lot::Mutex;
15use time::UtcOffset;
16use wayland_client::Connection;
17
18use crate::platform::linux::client::Client;
19use crate::platform::linux::client_dispatcher::ClientDispatcher;
20use crate::platform::linux::wayland::{WaylandClient, WaylandClientDispatcher};
21use crate::platform::{X11Client, X11ClientDispatcher, XcbAtoms};
22use crate::{
23 Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId,
24 ForegroundExecutor, Keymap, LinuxDispatcher, LinuxTextSystem, Menu, PathPromptOptions,
25 Platform, PlatformDisplay, PlatformInput, PlatformTextSystem, PlatformWindow, Result,
26 SemanticVersion, Task, WindowOptions,
27};
28
29#[derive(Default)]
30pub(crate) struct Callbacks {
31 open_urls: Option<Box<dyn FnMut(Vec<String>)>>,
32 become_active: Option<Box<dyn FnMut()>>,
33 resign_active: Option<Box<dyn FnMut()>>,
34 pub(crate) quit: Option<Box<dyn FnMut()>>,
35 reopen: Option<Box<dyn FnMut()>>,
36 event: Option<Box<dyn FnMut(PlatformInput) -> bool>>,
37 app_menu_action: Option<Box<dyn FnMut(&dyn Action)>>,
38 will_open_app_menu: Option<Box<dyn FnMut()>>,
39 validate_app_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
40}
41
42pub(crate) struct LinuxPlatformInner {
43 pub(crate) background_executor: BackgroundExecutor,
44 pub(crate) foreground_executor: ForegroundExecutor,
45 pub(crate) main_receiver: flume::Receiver<Runnable>,
46 pub(crate) text_system: Arc<LinuxTextSystem>,
47 pub(crate) callbacks: Mutex<Callbacks>,
48 pub(crate) state: Mutex<LinuxPlatformState>,
49}
50
51pub(crate) struct LinuxPlatform {
52 client: Rc<dyn Client>,
53 inner: Rc<LinuxPlatformInner>,
54}
55
56pub(crate) struct LinuxPlatformState {
57 pub(crate) quit_requested: bool,
58}
59
60impl Default for LinuxPlatform {
61 fn default() -> Self {
62 Self::new()
63 }
64}
65
66impl LinuxPlatform {
67 pub(crate) fn new() -> Self {
68 let wayland_display = env::var_os("WAYLAND_DISPLAY");
69 let use_wayland = wayland_display.is_some() && !wayland_display.unwrap().is_empty();
70
71 let (main_sender, main_receiver) = flume::unbounded::<Runnable>();
72 let text_system = Arc::new(LinuxTextSystem::new());
73 let callbacks = Mutex::new(Callbacks::default());
74 let state = Mutex::new(LinuxPlatformState {
75 quit_requested: false,
76 });
77
78 if use_wayland {
79 Self::new_wayland(main_sender, main_receiver, text_system, callbacks, state)
80 } else {
81 Self::new_x11(main_sender, main_receiver, text_system, callbacks, state)
82 }
83 }
84
85 fn new_wayland(
86 main_sender: Sender<Runnable>,
87 main_receiver: Receiver<Runnable>,
88 text_system: Arc<LinuxTextSystem>,
89 callbacks: Mutex<Callbacks>,
90 state: Mutex<LinuxPlatformState>,
91 ) -> Self {
92 let conn = Arc::new(Connection::connect_to_env().unwrap());
93 let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
94 Arc::new(WaylandClientDispatcher::new(&conn));
95 let dispatcher = Arc::new(LinuxDispatcher::new(main_sender, &client_dispatcher));
96 let inner = Rc::new(LinuxPlatformInner {
97 background_executor: BackgroundExecutor::new(dispatcher.clone()),
98 foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
99 main_receiver,
100 text_system,
101 callbacks,
102 state,
103 });
104 let client = Rc::new(WaylandClient::new(Rc::clone(&inner), Arc::clone(&conn)));
105 Self {
106 client,
107 inner: Rc::clone(&inner),
108 }
109 }
110
111 fn new_x11(
112 main_sender: Sender<Runnable>,
113 main_receiver: Receiver<Runnable>,
114 text_system: Arc<LinuxTextSystem>,
115 callbacks: Mutex<Callbacks>,
116 state: Mutex<LinuxPlatformState>,
117 ) -> Self {
118 let (xcb_connection, x_root_index) = xcb::Connection::connect_with_extensions(
119 None,
120 &[xcb::Extension::Present, xcb::Extension::Xkb],
121 &[],
122 )
123 .unwrap();
124
125 let xkb_ver = xcb_connection
126 .wait_for_reply(xcb_connection.send_request(&xcb::xkb::UseExtension {
127 wanted_major: xcb::xkb::MAJOR_VERSION as u16,
128 wanted_minor: xcb::xkb::MINOR_VERSION as u16,
129 }))
130 .unwrap();
131 assert!(xkb_ver.supported());
132
133 let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
134 let xcb_connection = Arc::new(xcb_connection);
135 let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
136 Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
137 let dispatcher = Arc::new(LinuxDispatcher::new(main_sender, &client_dispatcher));
138 let inner = Rc::new(LinuxPlatformInner {
139 background_executor: BackgroundExecutor::new(dispatcher.clone()),
140 foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
141 main_receiver,
142 text_system,
143 callbacks,
144 state,
145 });
146 let client = Rc::new(X11Client::new(
147 Rc::clone(&inner),
148 xcb_connection,
149 x_root_index,
150 atoms,
151 ));
152 Self {
153 client,
154 inner: Rc::clone(&inner),
155 }
156 }
157}
158
159impl Platform for LinuxPlatform {
160 fn background_executor(&self) -> BackgroundExecutor {
161 self.inner.background_executor.clone()
162 }
163
164 fn foreground_executor(&self) -> ForegroundExecutor {
165 self.inner.foreground_executor.clone()
166 }
167
168 fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
169 self.inner.text_system.clone()
170 }
171
172 fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
173 self.client.run(on_finish_launching)
174 }
175
176 fn quit(&self) {
177 self.inner.state.lock().quit_requested = true;
178 }
179
180 //todo!(linux)
181 fn restart(&self) {}
182
183 //todo!(linux)
184 fn activate(&self, ignoring_other_apps: bool) {}
185
186 //todo!(linux)
187 fn hide(&self) {}
188
189 //todo!(linux)
190 fn hide_other_apps(&self) {}
191
192 //todo!(linux)
193 fn unhide_other_apps(&self) {}
194
195 fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
196 self.client.displays()
197 }
198
199 fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
200 self.client.display(id)
201 }
202
203 //todo!(linux)
204 fn active_window(&self) -> Option<AnyWindowHandle> {
205 None
206 }
207
208 fn open_window(
209 &self,
210 handle: AnyWindowHandle,
211 options: WindowOptions,
212 ) -> Box<dyn PlatformWindow> {
213 self.client.open_window(handle, options)
214 }
215
216 fn open_url(&self, url: &str) {
217 unimplemented!()
218 }
219
220 fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {
221 self.inner.callbacks.lock().open_urls = Some(callback);
222 }
223
224 fn prompt_for_paths(
225 &self,
226 options: PathPromptOptions,
227 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
228 unimplemented!()
229 }
230
231 fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
232 unimplemented!()
233 }
234
235 fn reveal_path(&self, path: &Path) {
236 unimplemented!()
237 }
238
239 fn on_become_active(&self, callback: Box<dyn FnMut()>) {
240 self.inner.callbacks.lock().become_active = Some(callback);
241 }
242
243 fn on_resign_active(&self, callback: Box<dyn FnMut()>) {
244 self.inner.callbacks.lock().resign_active = Some(callback);
245 }
246
247 fn on_quit(&self, callback: Box<dyn FnMut()>) {
248 self.inner.callbacks.lock().quit = Some(callback);
249 }
250
251 fn on_reopen(&self, callback: Box<dyn FnMut()>) {
252 self.inner.callbacks.lock().reopen = Some(callback);
253 }
254
255 fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>) {
256 self.inner.callbacks.lock().event = Some(callback);
257 }
258
259 fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
260 self.inner.callbacks.lock().app_menu_action = Some(callback);
261 }
262
263 fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>) {
264 self.inner.callbacks.lock().will_open_app_menu = Some(callback);
265 }
266
267 fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
268 self.inner.callbacks.lock().validate_app_menu_command = Some(callback);
269 }
270
271 fn os_name(&self) -> &'static str {
272 "Linux"
273 }
274
275 fn double_click_interval(&self) -> Duration {
276 Duration::default()
277 }
278
279 fn os_version(&self) -> Result<SemanticVersion> {
280 Ok(SemanticVersion {
281 major: 1,
282 minor: 0,
283 patch: 0,
284 })
285 }
286
287 fn app_version(&self) -> Result<SemanticVersion> {
288 Ok(SemanticVersion {
289 major: 1,
290 minor: 0,
291 patch: 0,
292 })
293 }
294
295 fn app_path(&self) -> Result<PathBuf> {
296 unimplemented!()
297 }
298
299 //todo!(linux)
300 fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap) {}
301
302 fn local_timezone(&self) -> UtcOffset {
303 UtcOffset::UTC
304 }
305
306 fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
307 unimplemented!()
308 }
309
310 //todo!(linux)
311 fn set_cursor_style(&self, style: CursorStyle) {}
312
313 //todo!(linux)
314 fn should_auto_hide_scrollbars(&self) -> bool {
315 false
316 }
317
318 //todo!(linux)
319 fn write_to_clipboard(&self, item: ClipboardItem) {}
320
321 //todo!(linux)
322 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
323 None
324 }
325
326 fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>> {
327 unimplemented!()
328 }
329
330 fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> {
331 unimplemented!()
332 }
333
334 fn delete_credentials(&self, url: &str) -> Task<Result<()>> {
335 unimplemented!()
336 }
337
338 fn window_appearance(&self) -> crate::WindowAppearance {
339 crate::WindowAppearance::Light
340 }
341}
342
343#[cfg(test)]
344mod tests {
345 use super::*;
346
347 fn build_platform() -> LinuxPlatform {
348 let platform = LinuxPlatform::new();
349 platform
350 }
351}