1use std::any::Any;
2use std::ffi::c_void;
3use std::rc::Rc;
4use std::sync::Arc;
5
6use blade_graphics as gpu;
7use blade_rwh::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
8use futures::channel::oneshot::Receiver;
9use parking_lot::Mutex;
10use raw_window_handle::{
11 DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle,
12};
13use wayland_client::{protocol::wl_surface, Proxy};
14use wayland_protocols::wp::viewporter::client::wp_viewport;
15use wayland_protocols::xdg::shell::client::xdg_toplevel;
16
17use crate::platform::blade::BladeRenderer;
18use crate::platform::linux::wayland::display::WaylandDisplay;
19use crate::platform::{PlatformAtlas, PlatformInputHandler, PlatformWindow};
20use crate::scene::Scene;
21use crate::{
22 px, size, Bounds, Modifiers, Pixels, PlatformDisplay, PlatformInput, Point, PromptLevel, Size,
23 WindowAppearance, WindowBounds, WindowOptions,
24};
25
26#[derive(Default)]
27pub(crate) struct Callbacks {
28 request_frame: Option<Box<dyn FnMut()>>,
29 input: Option<Box<dyn FnMut(crate::PlatformInput) -> bool>>,
30 active_status_change: Option<Box<dyn FnMut(bool)>>,
31 resize: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
32 fullscreen: Option<Box<dyn FnMut(bool)>>,
33 moved: Option<Box<dyn FnMut()>>,
34 should_close: Option<Box<dyn FnMut() -> bool>>,
35 close: Option<Box<dyn FnOnce()>>,
36 appearance_changed: Option<Box<dyn FnMut()>>,
37}
38
39struct WaylandWindowInner {
40 renderer: BladeRenderer,
41 bounds: Bounds<i32>,
42 scale: f32,
43 input_handler: Option<PlatformInputHandler>,
44}
45
46struct RawWindow {
47 window: *mut c_void,
48 display: *mut c_void,
49}
50
51unsafe impl HasRawWindowHandle for RawWindow {
52 fn raw_window_handle(&self) -> RawWindowHandle {
53 let mut wh = blade_rwh::WaylandWindowHandle::empty();
54 wh.surface = self.window;
55 wh.into()
56 }
57}
58
59unsafe impl HasRawDisplayHandle for RawWindow {
60 fn raw_display_handle(&self) -> RawDisplayHandle {
61 let mut dh = blade_rwh::WaylandDisplayHandle::empty();
62 dh.display = self.display;
63 dh.into()
64 }
65}
66
67impl WaylandWindowInner {
68 fn new(
69 conn: &Arc<wayland_client::Connection>,
70 wl_surf: &Arc<wl_surface::WlSurface>,
71 bounds: Bounds<i32>,
72 ) -> Self {
73 let raw = RawWindow {
74 window: wl_surf.id().as_ptr().cast::<c_void>(),
75 display: conn.backend().display_ptr().cast::<c_void>(),
76 };
77 let gpu = Arc::new(
78 unsafe {
79 gpu::Context::init_windowed(
80 &raw,
81 gpu::ContextDesc {
82 validation: false,
83 capture: false,
84 },
85 )
86 }
87 .unwrap(),
88 );
89 let extent = gpu::Extent {
90 width: bounds.size.width as u32,
91 height: bounds.size.height as u32,
92 depth: 1,
93 };
94 Self {
95 renderer: BladeRenderer::new(gpu, extent),
96 bounds,
97 scale: 1.0,
98 input_handler: None,
99 }
100 }
101}
102
103pub(crate) struct WaylandWindowState {
104 conn: Arc<wayland_client::Connection>,
105 inner: Mutex<WaylandWindowInner>,
106 pub(crate) callbacks: Mutex<Callbacks>,
107 pub(crate) surface: Arc<wl_surface::WlSurface>,
108 pub(crate) toplevel: Arc<xdg_toplevel::XdgToplevel>,
109 viewport: Option<wp_viewport::WpViewport>,
110}
111
112impl WaylandWindowState {
113 pub(crate) fn new(
114 conn: &Arc<wayland_client::Connection>,
115 wl_surf: Arc<wl_surface::WlSurface>,
116 viewport: Option<wp_viewport::WpViewport>,
117 toplevel: Arc<xdg_toplevel::XdgToplevel>,
118 options: WindowOptions,
119 ) -> Self {
120 if options.bounds == WindowBounds::Maximized {
121 toplevel.set_maximized();
122 } else if options.bounds == WindowBounds::Fullscreen {
123 toplevel.set_fullscreen(None);
124 }
125
126 let bounds: Bounds<i32> = match options.bounds {
127 WindowBounds::Fullscreen | WindowBounds::Maximized => Bounds {
128 origin: Point::default(),
129 size: Size {
130 width: 500,
131 height: 500,
132 }, //todo!(implement)
133 },
134 WindowBounds::Fixed(bounds) => bounds.map(|p| p.0 as i32),
135 };
136
137 Self {
138 conn: Arc::clone(conn),
139 surface: Arc::clone(&wl_surf),
140 inner: Mutex::new(WaylandWindowInner::new(&Arc::clone(conn), &wl_surf, bounds)),
141 callbacks: Mutex::new(Callbacks::default()),
142 toplevel,
143 viewport,
144 }
145 }
146
147 pub fn update(&self) {
148 let mut cb = self.callbacks.lock();
149 if let Some(mut fun) = cb.request_frame.take() {
150 drop(cb);
151 fun();
152 self.callbacks.lock().request_frame = Some(fun);
153 }
154 }
155
156 pub fn set_size_and_scale(&self, width: i32, height: i32, scale: f32) {
157 self.inner.lock().scale = scale;
158 self.inner.lock().bounds.size.width = width;
159 self.inner.lock().bounds.size.height = height;
160 self.inner.lock().renderer.update_drawable_size(size(
161 width as f64 * scale as f64,
162 height as f64 * scale as f64,
163 ));
164
165 if let Some(ref mut fun) = self.callbacks.lock().resize {
166 fun(
167 Size {
168 width: px(width as f32),
169 height: px(height as f32),
170 },
171 scale,
172 );
173 }
174
175 if let Some(viewport) = &self.viewport {
176 viewport.set_destination(width, height);
177 }
178 }
179
180 pub fn resize(&self, width: i32, height: i32) {
181 let scale = self.inner.lock().scale;
182 self.set_size_and_scale(width, height, scale);
183 }
184
185 pub fn rescale(&self, scale: f32) {
186 let bounds = self.inner.lock().bounds;
187 self.set_size_and_scale(bounds.size.width, bounds.size.height, scale)
188 }
189
190 pub fn close(&self) {
191 let mut callbacks = self.callbacks.lock();
192 if let Some(fun) = callbacks.close.take() {
193 fun()
194 }
195 self.toplevel.destroy();
196 }
197
198 pub fn handle_input(&self, input: PlatformInput) {
199 if let Some(ref mut fun) = self.callbacks.lock().input {
200 if fun(input.clone()) {
201 return;
202 }
203 }
204 if let PlatformInput::KeyDown(event) = input {
205 let mut inner = self.inner.lock();
206 if let Some(ref mut input_handler) = inner.input_handler {
207 if let Some(ime_key) = &event.keystroke.ime_key {
208 input_handler.replace_text_in_range(None, ime_key);
209 }
210 }
211 }
212 }
213}
214
215#[derive(Clone)]
216pub(crate) struct WaylandWindow(pub(crate) Rc<WaylandWindowState>);
217
218impl HasWindowHandle for WaylandWindow {
219 fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
220 unimplemented!()
221 }
222}
223
224impl HasDisplayHandle for WaylandWindow {
225 fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
226 unimplemented!()
227 }
228}
229
230impl PlatformWindow for WaylandWindow {
231 //todo!(linux)
232 fn bounds(&self) -> WindowBounds {
233 WindowBounds::Maximized
234 }
235
236 fn content_size(&self) -> Size<Pixels> {
237 let inner = self.0.inner.lock();
238 Size {
239 width: Pixels(inner.bounds.size.width as f32),
240 height: Pixels(inner.bounds.size.height as f32),
241 }
242 }
243
244 fn scale_factor(&self) -> f32 {
245 self.0.inner.lock().scale
246 }
247
248 //todo!(linux)
249 fn titlebar_height(&self) -> Pixels {
250 unimplemented!()
251 }
252
253 // todo!(linux)
254 fn appearance(&self) -> WindowAppearance {
255 WindowAppearance::Light
256 }
257
258 // todo!(linux)
259 fn display(&self) -> Rc<dyn PlatformDisplay> {
260 Rc::new(WaylandDisplay {})
261 }
262
263 // todo!(linux)
264 fn mouse_position(&self) -> Point<Pixels> {
265 Point::default()
266 }
267
268 //todo!(linux)
269 fn modifiers(&self) -> Modifiers {
270 crate::Modifiers::default()
271 }
272
273 //todo!(linux)
274 fn as_any_mut(&mut self) -> &mut dyn Any {
275 unimplemented!()
276 }
277
278 fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
279 self.0.inner.lock().input_handler = Some(input_handler);
280 }
281
282 fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
283 self.0.inner.lock().input_handler.take()
284 }
285
286 //todo!(linux)
287 fn prompt(
288 &self,
289 level: PromptLevel,
290 msg: &str,
291 detail: Option<&str>,
292 answers: &[&str],
293 ) -> Receiver<usize> {
294 unimplemented!()
295 }
296
297 fn activate(&self) {
298 //todo!(linux)
299 }
300
301 fn set_title(&mut self, title: &str) {
302 self.0.toplevel.set_title(title.to_string());
303 }
304
305 fn set_edited(&mut self, edited: bool) {
306 //todo!(linux)
307 }
308
309 fn show_character_palette(&self) {
310 //todo!(linux)
311 }
312
313 fn minimize(&self) {
314 //todo!(linux)
315 }
316
317 fn zoom(&self) {
318 //todo!(linux)
319 }
320
321 fn toggle_full_screen(&self) {
322 //todo!(linux)
323 }
324
325 fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
326 self.0.callbacks.lock().request_frame = Some(callback);
327 }
328
329 fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>) {
330 self.0.callbacks.lock().input = Some(callback);
331 }
332
333 fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
334 //todo!(linux)
335 }
336
337 fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
338 self.0.callbacks.lock().resize = Some(callback);
339 }
340
341 fn on_fullscreen(&self, callback: Box<dyn FnMut(bool)>) {
342 //todo!(linux)
343 }
344
345 fn on_moved(&self, callback: Box<dyn FnMut()>) {
346 self.0.callbacks.lock().moved = Some(callback);
347 }
348
349 fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
350 self.0.callbacks.lock().should_close = Some(callback);
351 }
352
353 fn on_close(&self, callback: Box<dyn FnOnce()>) {
354 self.0.callbacks.lock().close = Some(callback);
355 }
356
357 fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
358 //todo!(linux)
359 }
360
361 // todo!(linux)
362 fn is_topmost_for_position(&self, position: Point<Pixels>) -> bool {
363 false
364 }
365
366 fn draw(&self, scene: &Scene) {
367 let mut inner = self.0.inner.lock();
368 inner.renderer.draw(scene);
369 }
370
371 fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
372 let inner = self.0.inner.lock();
373 inner.renderer.sprite_atlas().clone()
374 }
375
376 fn set_graphics_profiler_enabled(&self, enabled: bool) {
377 //todo!(linux)
378 }
379}