Skip to main content

gdk4_wayland/
wayland_display.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use crate::ffi;
7use glib::translate::*;
8#[cfg(feature = "wayland_crate")]
9#[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
10use glib::{Quark, prelude::*};
11
12#[cfg(all(feature = "v4_4", feature = "egl"))]
13#[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
14use khronos_egl as egl;
15#[cfg(feature = "wayland_crate")]
16#[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
17use wayland_client::{
18    Proxy,
19    backend::ObjectId,
20    protocol::{wl_compositor::WlCompositor, wl_display::WlDisplay},
21};
22
23use crate::WaylandDisplay;
24
25impl WaylandDisplay {
26    #[cfg(feature = "v4_4")]
27    #[cfg_attr(docsrs, doc(cfg(feature = "v4_4")))]
28    #[doc(alias = "gdk_wayland_display_get_egl_display")]
29    #[doc(alias = "get_egl_display")]
30    pub fn egl_display_raw(&self) -> Option<NonNull<c_void>> {
31        NonNull::new(unsafe { ffi::gdk_wayland_display_get_egl_display(self.to_glib_none().0) })
32    }
33
34    /// Retrieves the EGL display connection object for the given GDK display.
35    ///
36    /// # Returns
37    ///
38    /// the EGL display
39    #[cfg(all(feature = "v4_4", feature = "egl"))]
40    #[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
41    #[doc(alias = "gdk_wayland_display_get_egl_display")]
42    #[doc(alias = "get_egl_display")]
43    pub fn egl_display(&self) -> Option<egl::Display> {
44        let ptr = self.egl_display_raw()?;
45        Some(unsafe { egl::Display::from_ptr(ptr.as_ptr()) })
46    }
47
48    #[doc(alias = "gdk_wayland_display_get_wl_compositor")]
49    #[doc(alias = "get_wl_compositor")]
50    pub fn wl_compositor_raw(&self) -> Option<NonNull<c_void>> {
51        NonNull::new(unsafe { ffi::gdk_wayland_display_get_wl_compositor(self.to_glib_none().0) })
52    }
53
54    /// Returns the Wayland `wl_compositor` of a display.
55    ///
56    /// # Returns
57    ///
58    /// a Wayland `wl_compositor`
59    #[doc(alias = "gdk_wayland_display_get_wl_compositor")]
60    #[doc(alias = "get_wl_compositor")]
61    #[cfg(feature = "wayland_crate")]
62    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
63    pub fn wl_compositor(&self) -> Option<WlCompositor> {
64        let ptr = self.wl_compositor_raw()?;
65        unsafe {
66            let cnx = self.connection();
67            let id = ObjectId::from_ptr(WlCompositor::interface(), ptr.as_ptr() as *mut _).unwrap();
68
69            WlCompositor::from_id(&cnx, id).ok()
70        }
71    }
72
73    #[doc(alias = "gdk_wayland_display_get_wl_display")]
74    #[doc(alias = "get_wl_display")]
75    pub fn wl_display_raw(&self) -> Option<NonNull<c_void>> {
76        NonNull::new(unsafe { ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0) })
77    }
78
79    /// Returns the Wayland `wl_display` of a display.
80    ///
81    /// # Returns
82    ///
83    /// a Wayland `wl_display`
84    #[doc(alias = "gdk_wayland_display_get_wl_display")]
85    #[doc(alias = "get_wl_display")]
86    #[cfg(feature = "wayland_crate")]
87    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
88    pub fn wl_display(&self) -> Option<WlDisplay> {
89        let ptr = self.wl_display_raw()?;
90        unsafe {
91            let cnx = self.connection();
92            let id = ObjectId::from_ptr(WlDisplay::interface(), ptr.as_ptr() as *mut _).unwrap();
93
94            WlDisplay::from_id(&cnx, id).ok()
95        }
96    }
97
98    #[cfg(feature = "wayland_crate")]
99    pub(crate) fn connection(&self) -> wayland_client::Connection {
100        use std::sync::OnceLock;
101        static QUARK: OnceLock<Quark> = OnceLock::new();
102        let quark =
103            *QUARK.get_or_init(|| Quark::from_str("gtk-rs-wayland-display-connection-quark"));
104
105        unsafe {
106            match self.qdata::<Option<wayland_client::Connection>>(quark) {
107                Some(conn) => conn.as_ref().clone().unwrap(),
108                None => {
109                    let display_ptr =
110                        ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0);
111                    let backend = wayland_backend::sys::client::Backend::from_foreign_display(
112                        display_ptr as *mut _,
113                    );
114                    let conn = wayland_client::Connection::from_backend(backend);
115                    self.set_qdata(quark, conn.clone());
116
117                    conn
118                }
119            }
120        }
121    }
122}