Skip to main content

egui_pop_wayland/
sys.rs

1//! The one place that touches raw Wayland pointers: joining winit's
2//! connection, naming winit's surface, and handing wgpu a handle to a
3//! surface of ours.
4#![allow(unsafe_code, reason = "the crate's one module of raw Wayland pointers")]
5
6use std::ffi::c_void;
7use std::fmt;
8use std::ptr::NonNull;
9use std::sync::Arc;
10
11use raw_window_handle::{
12    DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle,
13    RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle, WindowHandle,
14};
15use wayland_client::backend::{Backend, ObjectId};
16use wayland_client::protocol::wl_surface::WlSurface;
17use wayland_client::{Connection, Proxy};
18
19/// A connection on winit's `wl_display`, in guest mode: it never closes
20/// the display.
21///
22/// The display must outlive the connection: it is the window's event
23/// loop's, which eframe drops after the application and its renderer,
24/// where egui-pop keeps the connection.
25pub(crate) fn connection(display: NonNull<c_void>) -> Connection {
26    // SAFETY: `display` is the live `wl_display` of the window's handle;
27    // the event loop owning it outlives the egui context and renderer
28    // that hold this connection (see above).
29    let backend = unsafe { Backend::from_foreign_display(display.as_ptr().cast()) };
30    Connection::from_backend(backend)
31}
32
33/// winit's `wl_surface`, as a proxy on `conn` (used only as an argument:
34/// its events stay winit's).
35pub(crate) fn foreign_surface(
36    conn: &Connection,
37    surface: NonNull<c_void>,
38) -> Result<WlSurface, String> {
39    // SAFETY: `surface` is the live `wl_surface` of the window's handle,
40    // on the display `conn` joined; it is used while the window lives
41    // (to make subsurfaces and the preferred-description feedback).
42    let id = unsafe { ObjectId::from_ptr(WlSurface::interface(), surface.as_ptr().cast()) }
43        .map_err(|_| "the window's surface is not a wl_surface".to_string())?;
44    WlSurface::from_id(conn, id).map_err(|_| "the window's surface is not alive".to_string())
45}
46
47/// A surface of ours and its connection, destroyed when the last holder
48/// goes (the subsurface, or the wgpu surface made on its handle).
49pub(crate) struct Keep {
50    pub(crate) surface: WlSurface,
51    pub(crate) conn: Connection,
52}
53
54impl Drop for Keep {
55    fn drop(&mut self) {
56        self.surface.destroy();
57        let _ = self.conn.flush();
58    }
59}
60
61/// The raw handles of an image surface, for wgpu
62/// (`Instance::create_surface`): the window's display and the
63/// subsurface's `wl_surface`, kept alive as long as the handle is.
64#[derive(Clone)]
65pub struct SurfaceHandle {
66    pub(crate) keep: Arc<Keep>,
67}
68
69impl fmt::Debug for SurfaceHandle {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.debug_struct("SurfaceHandle")
72            .field("surface", &self.keep.surface.id())
73            .finish()
74    }
75}
76
77impl HasWindowHandle for SurfaceHandle {
78    fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
79        let surface =
80            NonNull::new(self.keep.surface.id().as_ptr().cast()).ok_or(HandleError::Unavailable)?;
81        let raw = RawWindowHandle::Wayland(WaylandWindowHandle::new(surface));
82        // SAFETY: the `wl_surface` is ours and is destroyed only when the
83        // last clone of this handle (and of its subsurface) is dropped.
84        Ok(unsafe { WindowHandle::borrow_raw(raw) })
85    }
86}
87
88impl HasDisplayHandle for SurfaceHandle {
89    fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
90        let display = NonNull::new(self.keep.conn.backend().display_ptr().cast())
91            .ok_or(HandleError::Unavailable)?;
92        let raw = RawDisplayHandle::Wayland(WaylandDisplayHandle::new(display));
93        // SAFETY: the display is winit's, alive as long as the connection
94        // this handle keeps (see `connection`).
95        Ok(unsafe { DisplayHandle::borrow_raw(raw) })
96    }
97}