winit/platform_impl/linux/wayland/
mod.rs

1//! Winit's Wayland backend.
2
3use std::fmt::Display;
4use std::sync::Arc;
5
6use sctk::reexports::client::globals::{BindError, GlobalError};
7use sctk::reexports::client::protocol::wl_surface::WlSurface;
8use sctk::reexports::client::{self, ConnectError, DispatchError, Proxy};
9
10pub(super) use crate::cursor::OnlyCursorImage as CustomCursor;
11use crate::dpi::{LogicalSize, PhysicalSize};
12pub use crate::platform_impl::platform::{OsError, WindowId};
13pub use event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy};
14pub use output::{MonitorHandle, VideoModeHandle};
15pub use window::Window;
16
17mod event_loop;
18mod output;
19mod seat;
20mod state;
21mod types;
22mod window;
23
24#[derive(Debug)]
25pub enum WaylandError {
26    /// Error connecting to the socket.
27    Connection(ConnectError),
28
29    /// Error binding the global.
30    Global(GlobalError),
31
32    // Bind error.
33    Bind(BindError),
34
35    /// Error during the dispatching the event queue.
36    Dispatch(DispatchError),
37
38    /// Calloop error.
39    Calloop(calloop::Error),
40
41    /// Wayland
42    Wire(client::backend::WaylandError),
43}
44
45impl Display for WaylandError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        match self {
48            WaylandError::Connection(error) => error.fmt(f),
49            WaylandError::Global(error) => error.fmt(f),
50            WaylandError::Bind(error) => error.fmt(f),
51            WaylandError::Dispatch(error) => error.fmt(f),
52            WaylandError::Calloop(error) => error.fmt(f),
53            WaylandError::Wire(error) => error.fmt(f),
54        }
55    }
56}
57
58impl From<WaylandError> for OsError {
59    fn from(value: WaylandError) -> Self {
60        Self::WaylandError(Arc::new(value))
61    }
62}
63
64/// Dummy device id, since Wayland doesn't have device events.
65#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66pub struct DeviceId;
67
68impl DeviceId {
69    pub const fn dummy() -> Self {
70        DeviceId
71    }
72}
73
74/// Get the WindowId out of the surface.
75#[inline]
76fn make_wid(surface: &WlSurface) -> WindowId {
77    WindowId(surface.id().as_ptr() as u64)
78}
79
80/// The default routine does floor, but we need round on Wayland.
81fn logical_to_physical_rounded(size: LogicalSize<u32>, scale_factor: f64) -> PhysicalSize<u32> {
82    let width = size.width as f64 * scale_factor;
83    let height = size.height as f64 * scale_factor;
84    (width.round(), height.round()).into()
85}