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: Arc<dyn Client>,
53 inner: Arc<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 = Arc::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 = Arc::new(WaylandClient::new(Arc::clone(&inner), Arc::clone(&conn)));
105 Self {
106 client,
107 inner: Arc::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(None).unwrap();
119 let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
120 let xcb_connection = Arc::new(xcb_connection);
121 let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
122 Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
123 let dispatcher = Arc::new(LinuxDispatcher::new(main_sender, &client_dispatcher));
124 let inner = Arc::new(LinuxPlatformInner {
125 background_executor: BackgroundExecutor::new(dispatcher.clone()),
126 foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
127 main_receiver,
128 text_system,
129 callbacks,
130 state,
131 });
132 let client = Arc::new(X11Client::new(
133 Arc::clone(&inner),
134 xcb_connection,
135 x_root_index,
136 atoms,
137 ));
138 Self {
139 client,
140 inner: Arc::clone(&inner),
141 }
142 }
143}
144
145impl Platform for LinuxPlatform {
146 fn background_executor(&self) -> BackgroundExecutor {
147 self.inner.background_executor.clone()
148 }
149
150 fn foreground_executor(&self) -> ForegroundExecutor {
151 self.inner.foreground_executor.clone()
152 }
153
154 fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
155 self.inner.text_system.clone()
156 }
157
158 fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
159 self.client.run(on_finish_launching)
160 }
161
162 fn quit(&self) {
163 self.inner.state.lock().quit_requested = true;
164 }
165
166 //todo!(linux)
167 fn restart(&self) {}
168
169 //todo!(linux)
170 fn activate(&self, ignoring_other_apps: bool) {}
171
172 //todo!(linux)
173 fn hide(&self) {}
174
175 //todo!(linux)
176 fn hide_other_apps(&self) {}
177
178 //todo!(linux)
179 fn unhide_other_apps(&self) {}
180
181 fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
182 self.client.displays()
183 }
184
185 fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
186 self.client.display(id)
187 }
188
189 //todo!(linux)
190 fn active_window(&self) -> Option<AnyWindowHandle> {
191 None
192 }
193
194 fn open_window(
195 &self,
196 handle: AnyWindowHandle,
197 options: WindowOptions,
198 ) -> Box<dyn PlatformWindow> {
199 self.client.open_window(handle, options)
200 }
201
202 fn open_url(&self, url: &str) {
203 unimplemented!()
204 }
205
206 fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {
207 self.inner.callbacks.lock().open_urls = Some(callback);
208 }
209
210 fn prompt_for_paths(
211 &self,
212 options: PathPromptOptions,
213 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
214 unimplemented!()
215 }
216
217 fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
218 unimplemented!()
219 }
220
221 fn reveal_path(&self, path: &Path) {
222 unimplemented!()
223 }
224
225 fn on_become_active(&self, callback: Box<dyn FnMut()>) {
226 self.inner.callbacks.lock().become_active = Some(callback);
227 }
228
229 fn on_resign_active(&self, callback: Box<dyn FnMut()>) {
230 self.inner.callbacks.lock().resign_active = Some(callback);
231 }
232
233 fn on_quit(&self, callback: Box<dyn FnMut()>) {
234 self.inner.callbacks.lock().quit = Some(callback);
235 }
236
237 fn on_reopen(&self, callback: Box<dyn FnMut()>) {
238 self.inner.callbacks.lock().reopen = Some(callback);
239 }
240
241 fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>) {
242 self.inner.callbacks.lock().event = Some(callback);
243 }
244
245 fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
246 self.inner.callbacks.lock().app_menu_action = Some(callback);
247 }
248
249 fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>) {
250 self.inner.callbacks.lock().will_open_app_menu = Some(callback);
251 }
252
253 fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
254 self.inner.callbacks.lock().validate_app_menu_command = Some(callback);
255 }
256
257 fn os_name(&self) -> &'static str {
258 "Linux"
259 }
260
261 fn double_click_interval(&self) -> Duration {
262 Duration::default()
263 }
264
265 fn os_version(&self) -> Result<SemanticVersion> {
266 Ok(SemanticVersion {
267 major: 1,
268 minor: 0,
269 patch: 0,
270 })
271 }
272
273 fn app_version(&self) -> Result<SemanticVersion> {
274 Ok(SemanticVersion {
275 major: 1,
276 minor: 0,
277 patch: 0,
278 })
279 }
280
281 fn app_path(&self) -> Result<PathBuf> {
282 unimplemented!()
283 }
284
285 //todo!(linux)
286 fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap) {}
287
288 fn local_timezone(&self) -> UtcOffset {
289 UtcOffset::UTC
290 }
291
292 fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
293 unimplemented!()
294 }
295
296 //todo!(linux)
297 fn set_cursor_style(&self, style: CursorStyle) {}
298
299 //todo!(linux)
300 fn should_auto_hide_scrollbars(&self) -> bool {
301 false
302 }
303
304 //todo!(linux)
305 fn write_to_clipboard(&self, item: ClipboardItem) {}
306
307 //todo!(linux)
308 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
309 None
310 }
311
312 fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>> {
313 unimplemented!()
314 }
315
316 fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> {
317 unimplemented!()
318 }
319
320 fn delete_credentials(&self, url: &str) -> Task<Result<()>> {
321 unimplemented!()
322 }
323
324 fn window_appearance(&self) -> crate::WindowAppearance {
325 crate::WindowAppearance::Light
326 }
327}
328
329#[cfg(test)]
330mod tests {
331 use super::*;
332
333 fn build_platform() -> LinuxPlatform {
334 let platform = LinuxPlatform::new();
335 platform
336 }
337}