winit_wayland/
lib.rs

1//! # Winit's Wayland backend.
2//!
3//! **Note:** Windows don't appear on Wayland until you draw/present to them.
4//!
5//! By default, Winit loads system libraries using `dlopen`. This can be
6//! disabled by disabling the `"wayland-dlopen"` cargo feature.
7//!
8//! ## Client-side decorations
9//!
10//! Winit provides client-side decorations by default, but the behaviour can
11//! be controlled with the following feature flags:
12//!
13//! * `wayland-csd-adwaita` (default).
14//! * `wayland-csd-adwaita-crossfont`.
15//! * `wayland-csd-adwaita-notitle`.
16//! * `wayland-csd-adwaita-notitlebar`.
17use 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
44/// Additional methods on [`ActiveEventLoop`] that are specific to Wayland.
45pub trait ActiveEventLoopExtWayland {
46    /// True if the [`ActiveEventLoop`] uses Wayland.
47    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
57/// Additional methods on [`EventLoop`] that are specific to Wayland.
58pub trait EventLoopExtWayland {
59    /// True if the [`EventLoop`] uses Wayland.
60    fn is_wayland(&self) -> bool;
61}
62
63/// Additional methods when building event loop that are specific to Wayland.
64pub trait EventLoopBuilderExtWayland {
65    /// Force using Wayland.
66    fn with_wayland(&mut self) -> &mut Self;
67
68    /// Whether to allow the event loop to be created off of the main thread.
69    ///
70    /// By default, the window is only allowed to be created on the main
71    /// thread, to make platform compatibility easier.
72    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
73}
74
75/// Additional methods on [`Window`] that are specific to Wayland.
76///
77/// [`Window`]: crate::window::Window
78pub trait WindowExtWayland {
79    /// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
80    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/// Window attributes methods specific to Wayland.
97#[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    /// Build window with the given name.
106    ///
107    /// The `general` name sets an application ID, which should match the `.desktop`
108    /// file distributed with your program. The `instance` is a `no-op`.
109    ///
110    /// For details about application ID conventions, see the
111    /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
112    #[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    /// Builds the window with a given preference for client-side decorations.
125    ///
126    /// When set to `true`, the window will tell the compositor that it prefers
127    /// client-side decorations, even if server-side decorations are available.
128    /// When set to `false` (the default), the window will indicate a preference
129    /// for server-side decorations.
130    #[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/// Get the WindowId out of the surface.
144#[inline]
145fn make_wid(surface: &WlSurface) -> WindowId {
146    WindowId::from_raw(surface.id().as_ptr() as usize)
147}
148
149/// The default routine does floor, but we need round on Wayland.
150fn 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
156/// Converts an image buffer to a Wayland buffer (`wl_buffer`)
157fn 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        // Alpha in buffer is premultiplied.
168        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}