gdk4_wayland/
wayland_display.rs

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