2025-05-25 16:48:07 +02:00
|
|
|
//! # Winit's Wayland backend.
|
2024-02-23 15:35:18 +01:00
|
|
|
//!
|
|
|
|
|
//! **Note:** Windows don't appear on Wayland until you draw/present to them.
|
|
|
|
|
//!
|
|
|
|
|
//! By default, Winit loads system libraries using `dlopen`. This can be
|
|
|
|
|
//! disabled by disabling the `"wayland-dlopen"` cargo feature.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Client-side decorations
|
|
|
|
|
//!
|
|
|
|
|
//! Winit provides client-side decorations by default, but the behaviour can
|
|
|
|
|
//! be controlled with the following feature flags:
|
|
|
|
|
//!
|
|
|
|
|
//! * `wayland-csd-adwaita` (default).
|
|
|
|
|
//! * `wayland-csd-adwaita-crossfont`.
|
|
|
|
|
//! * `wayland-csd-adwaita-notitle`.
|
2025-04-29 13:31:49 +09:00
|
|
|
use std::ffi::c_void;
|
|
|
|
|
use std::ptr::NonNull;
|
|
|
|
|
|
2025-05-25 16:48:07 +02:00
|
|
|
use dpi::{LogicalSize, PhysicalSize};
|
|
|
|
|
use sctk::reexports::client::protocol::wl_surface::WlSurface;
|
|
|
|
|
use sctk::reexports::client::Proxy;
|
|
|
|
|
use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
|
|
|
|
|
use winit_core::window::{
|
|
|
|
|
ActivationToken, PlatformWindowAttributes, Window as CoreWindow, WindowId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
macro_rules! os_error {
|
|
|
|
|
($error:expr) => {{
|
|
|
|
|
winit_core::error::OsError::new(line!(), file!(), $error)
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod event_loop;
|
|
|
|
|
mod output;
|
|
|
|
|
mod seat;
|
|
|
|
|
mod state;
|
|
|
|
|
mod types;
|
|
|
|
|
mod window;
|
2025-05-03 18:24:44 +09:00
|
|
|
|
2025-05-25 16:48:07 +02:00
|
|
|
pub use self::event_loop::{ActiveEventLoop, EventLoop};
|
|
|
|
|
pub use self::window::Window;
|
2022-09-01 07:05:32 +02:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
/// Additional methods on [`ActiveEventLoop`] that are specific to Wayland.
|
|
|
|
|
pub trait ActiveEventLoopExtWayland {
|
|
|
|
|
/// True if the [`ActiveEventLoop`] uses Wayland.
|
2022-09-01 07:05:32 +02:00
|
|
|
fn is_wayland(&self) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 16:48:07 +02:00
|
|
|
impl ActiveEventLoopExtWayland for dyn CoreActiveEventLoop + '_ {
|
2022-09-01 07:05:32 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn is_wayland(&self) -> bool {
|
2025-05-25 16:48:07 +02:00
|
|
|
self.cast_ref::<ActiveEventLoop>().is_some()
|
2022-09-01 07:05:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 14:38:10 +01:00
|
|
|
/// Additional methods on [`EventLoop`] that are specific to Wayland.
|
|
|
|
|
pub trait EventLoopExtWayland {
|
|
|
|
|
/// True if the [`EventLoop`] uses Wayland.
|
|
|
|
|
fn is_wayland(&self) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 07:05:32 +02:00
|
|
|
/// Additional methods on [`EventLoopBuilder`] that are specific to Wayland.
|
|
|
|
|
pub trait EventLoopBuilderExtWayland {
|
|
|
|
|
/// Force using Wayland.
|
|
|
|
|
fn with_wayland(&mut self) -> &mut Self;
|
|
|
|
|
|
|
|
|
|
/// Whether to allow the event loop to be created off of the main thread.
|
|
|
|
|
///
|
|
|
|
|
/// By default, the window is only allowed to be created on the main
|
|
|
|
|
/// thread, to make platform compatibility easier.
|
|
|
|
|
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Additional methods on [`Window`] that are specific to Wayland.
|
2024-08-23 23:40:27 +03:00
|
|
|
///
|
|
|
|
|
/// [`Window`]: crate::window::Window
|
2025-04-29 13:31:49 +09:00
|
|
|
pub trait WindowExtWayland {
|
|
|
|
|
/// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
|
|
|
|
|
fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
|
|
|
|
|
}
|
2022-09-01 07:05:32 +02:00
|
|
|
|
2025-04-29 13:31:49 +09:00
|
|
|
impl WindowExtWayland for dyn CoreWindow + '_ {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
|
|
|
|
|
self.cast_ref::<Window>()?.xdg_toplevel()
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-01 07:05:32 +02:00
|
|
|
|
2025-05-25 16:48:07 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub(crate) struct ApplicationName {
|
|
|
|
|
pub(crate) general: String,
|
|
|
|
|
pub(crate) instance: String,
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:24:44 +09:00
|
|
|
/// Window attributes methods specific to Wayland.
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
|
pub struct WindowAttributesWayland {
|
|
|
|
|
pub(crate) name: Option<ApplicationName>,
|
|
|
|
|
pub(crate) activation_token: Option<ActivationToken>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WindowAttributesWayland {
|
2022-09-01 07:05:32 +02:00
|
|
|
/// Build window with the given name.
|
|
|
|
|
///
|
|
|
|
|
/// The `general` name sets an application ID, which should match the `.desktop`
|
2024-02-19 11:58:44 +07:00
|
|
|
/// file distributed with your program. The `instance` is a `no-op`.
|
2022-09-01 07:05:32 +02:00
|
|
|
///
|
|
|
|
|
/// For details about application ID conventions, see the
|
|
|
|
|
/// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
|
|
|
|
|
#[inline]
|
2025-05-03 18:24:44 +09:00
|
|
|
pub fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
|
2025-05-25 16:48:07 +02:00
|
|
|
self.name = Some(ApplicationName { general: general.into(), instance: instance.into() });
|
2022-09-01 07:05:32 +02:00
|
|
|
self
|
|
|
|
|
}
|
2025-05-03 18:24:44 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn with_activation_token(mut self, token: ActivationToken) -> Self {
|
|
|
|
|
self.activation_token = Some(token);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PlatformWindowAttributes for WindowAttributesWayland {
|
|
|
|
|
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
|
|
|
|
Box::from(self.clone())
|
|
|
|
|
}
|
2022-09-01 07:05:32 +02:00
|
|
|
}
|
2025-05-25 16:48:07 +02:00
|
|
|
|
|
|
|
|
/// Get the WindowId out of the surface.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn make_wid(surface: &WlSurface) -> WindowId {
|
|
|
|
|
WindowId::from_raw(surface.id().as_ptr() as usize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The default routine does floor, but we need round on Wayland.
|
|
|
|
|
fn logical_to_physical_rounded(size: LogicalSize<u32>, scale_factor: f64) -> PhysicalSize<u32> {
|
|
|
|
|
let width = size.width as f64 * scale_factor;
|
|
|
|
|
let height = size.height as f64 * scale_factor;
|
|
|
|
|
(width.round(), height.round()).into()
|
|
|
|
|
}
|