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    #[cfg(all(feature = "v4_4", feature = "egl"))]
24    #[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
25    #[doc(alias = "gdk_wayland_display_get_egl_display")]
26    #[doc(alias = "get_egl_display")]
27    pub fn egl_display(&self) -> Option<egl::Display> {
28        unsafe {
29            let ptr = ffi::gdk_wayland_display_get_egl_display(self.to_glib_none().0);
30            if ptr.is_null() {
31                None
32            } else {
33                Some(egl::Display::from_ptr(ptr))
34            }
35        }
36    }
37
38    #[doc(alias = "gdk_wayland_display_get_wl_compositor")]
39    #[doc(alias = "get_wl_compositor")]
40    #[cfg(feature = "wayland_crate")]
41    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
42    pub fn wl_compositor(&self) -> Option<WlCompositor> {
43        unsafe {
44            let compositor_ptr = ffi::gdk_wayland_display_get_wl_compositor(self.to_glib_none().0);
45            if compositor_ptr.is_null() {
46                None
47            } else {
48                let cnx = self.connection();
49                let id = ObjectId::from_ptr(WlCompositor::interface(), compositor_ptr as *mut _)
50                    .unwrap();
51
52                WlCompositor::from_id(&cnx, id).ok()
53            }
54        }
55    }
56
57    #[doc(alias = "gdk_wayland_display_get_wl_display")]
58    #[doc(alias = "get_wl_display")]
59    #[cfg(feature = "wayland_crate")]
60    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
61    pub fn wl_display(&self) -> Option<WlDisplay> {
62        unsafe {
63            let display_ptr = ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0);
64            if display_ptr.is_null() {
65                None
66            } else {
67                let cnx = self.connection();
68                let id = ObjectId::from_ptr(WlDisplay::interface(), display_ptr as *mut _).unwrap();
69
70                WlDisplay::from_id(&cnx, id).ok()
71            }
72        }
73    }
74
75    #[cfg(feature = "wayland_crate")]
76    pub(crate) fn connection(&self) -> wayland_client::Connection {
77        use std::sync::OnceLock;
78        static QUARK: OnceLock<Quark> = OnceLock::new();
79        let quark =
80            *QUARK.get_or_init(|| Quark::from_str("gtk-rs-wayland-display-connection-quark"));
81
82        unsafe {
83            match self.qdata::<Option<wayland_client::Connection>>(quark) {
84                Some(conn) => conn.as_ref().clone().unwrap(),
85                None => {
86                    let display_ptr =
87                        ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0);
88                    let backend = wayland_backend::sys::client::Backend::from_foreign_display(
89                        display_ptr as *mut _,
90                    );
91                    let conn = wayland_client::Connection::from_backend(backend);
92                    self.set_qdata(quark, conn.clone());
93
94                    conn
95                }
96            }
97        }
98    }
99}