1use std::ffi::c_void;
18use std::ptr::NonNull;
19
20use dpi::{LogicalSize, PhysicalSize};
21use sctk::reexports::client::Proxy;
22use sctk::reexports::client::protocol::wl_surface::WlSurface;
23use sctk::shm::slot::{Buffer, CreateBufferError, SlotPool};
24use wayland_client::protocol::wl_shm::Format;
25use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
26use winit_core::window::{
27 ActivationToken, PlatformWindowAttributes, Window as CoreWindow, WindowId,
28};
29
30macro_rules! os_error {
31 ($error:expr) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }};
32}
33
34mod event_loop;
35mod output;
36mod seat;
37mod state;
38mod types;
39mod window;
40
41pub use self::event_loop::{ActiveEventLoop, EventLoop};
42pub use self::window::Window;
43
44pub trait ActiveEventLoopExtWayland {
46 fn is_wayland(&self) -> bool;
48}
49
50impl ActiveEventLoopExtWayland for dyn CoreActiveEventLoop + '_ {
51 #[inline]
52 fn is_wayland(&self) -> bool {
53 self.cast_ref::<ActiveEventLoop>().is_some()
54 }
55}
56
57pub trait EventLoopExtWayland {
59 fn is_wayland(&self) -> bool;
61}
62
63pub trait EventLoopBuilderExtWayland {
65 fn with_wayland(&mut self) -> &mut Self;
67
68 fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
73}
74
75pub trait WindowExtWayland {
79 fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
81}
82
83impl WindowExtWayland for dyn CoreWindow + '_ {
84 #[inline]
85 fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
86 self.cast_ref::<Window>()?.xdg_toplevel()
87 }
88}
89
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub(crate) struct ApplicationName {
92 pub(crate) general: String,
93 pub(crate) instance: String,
94}
95
96#[derive(Debug, Default, Clone)]
98pub struct WindowAttributesWayland {
99 pub(crate) name: Option<ApplicationName>,
100 pub(crate) activation_token: Option<ActivationToken>,
101 pub(crate) prefer_csd: bool,
102}
103
104impl WindowAttributesWayland {
105 #[inline]
113 pub fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
114 self.name = Some(ApplicationName { general: general.into(), instance: instance.into() });
115 self
116 }
117
118 #[inline]
119 pub fn with_activation_token(mut self, token: ActivationToken) -> Self {
120 self.activation_token = Some(token);
121 self
122 }
123
124 #[inline]
131 pub fn with_prefer_csd(mut self, prefer_csd: bool) -> Self {
132 self.prefer_csd = prefer_csd;
133 self
134 }
135}
136
137impl PlatformWindowAttributes for WindowAttributesWayland {
138 fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
139 Box::from(self.clone())
140 }
141}
142
143#[inline]
145fn make_wid(surface: &WlSurface) -> WindowId {
146 WindowId::from_raw(surface.id().as_ptr() as usize)
147}
148
149fn logical_to_physical_rounded(size: LogicalSize<u32>, scale_factor: f64) -> PhysicalSize<u32> {
151 let width = size.width as f64 * scale_factor;
152 let height = size.height as f64 * scale_factor;
153 (width.round(), height.round()).into()
154}
155
156fn image_to_buffer(
158 width: i32,
159 height: i32,
160 data: &[u8],
161 format: Format,
162 pool: &mut SlotPool,
163) -> Result<Buffer, CreateBufferError> {
164 let (buffer, canvas) = pool.create_buffer(width, height, 4 * width, format)?;
165
166 for (canvas_chunk, rgba) in canvas.chunks_exact_mut(4).zip(data.chunks_exact(4)) {
167 let alpha = rgba[3] as f32 / 255.;
169 let r = (rgba[0] as f32 * alpha) as u32;
170 let g = (rgba[1] as f32 * alpha) as u32;
171 let b = (rgba[2] as f32 * alpha) as u32;
172 let color = ((rgba[3] as u32) << 24) + (r << 16) + (g << 8) + b;
173 let array: &mut [u8; 4] = canvas_chunk.try_into().unwrap();
174 *array = color.to_le_bytes();
175 }
176
177 Ok(buffer)
178}