Skip to main content

gdkwayland/
wayland_display.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::once_cell::sync::Lazy;
4use glib::translate::*;
5use glib::{ObjectExt, Quark};
6
7use wayland_client::backend::ObjectId;
8use wayland_client::protocol::{wl_compositor::WlCompositor, wl_display::WlDisplay};
9use wayland_client::Proxy;
10
11static WAYLAND_DISPLAY_CONNECTION_QUARK: Lazy<Quark> =
12    Lazy::new(|| Quark::from_str("gtk-rs-wayland-display-connection-quark"));
13
14glib::wrapper! {
15    #[doc(alias = "GdkWaylandDisplay")]
16    pub struct WaylandDisplay(Object<ffi::GdkWaylandDisplay>) @extends gdk::Display;
17
18    match fn {
19        type_ => || ffi::gdk_wayland_display_get_type(),
20    }
21}
22
23impl WaylandDisplay {
24    #[doc(alias = "gdk_wayland_display_get_wl_compositor")]
25    #[doc(alias = "get_wl_compositor")]
26    pub fn wl_compositor(&self) -> Option<WlCompositor> {
27        unsafe {
28            let compositor_ptr = ffi::gdk_wayland_display_get_wl_compositor(self.to_glib_none().0);
29            if compositor_ptr.is_null() {
30                None
31            } else {
32                let cnx = self.connection();
33                let id = ObjectId::from_ptr(WlCompositor::interface(), compositor_ptr as *mut _)
34                    .unwrap();
35
36                WlCompositor::from_id(&cnx, id).ok()
37            }
38        }
39    }
40
41    #[doc(alias = "gdk_wayland_display_get_wl_display")]
42    #[doc(alias = "get_wl_display")]
43    pub fn wl_display(&self) -> Option<WlDisplay> {
44        unsafe {
45            let display_ptr = ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0);
46            if display_ptr.is_null() {
47                None
48            } else {
49                let cnx = self.connection();
50                let id = ObjectId::from_ptr(WlDisplay::interface(), display_ptr as *mut _).unwrap();
51
52                WlDisplay::from_id(&cnx, id).ok()
53            }
54        }
55    }
56
57    #[doc(alias = "gdk_wayland_display_set_cursor_theme")]
58    pub fn set_cursor_theme(&self, theme: &str, size: isize) {
59        unsafe {
60            ffi::gdk_wayland_display_set_cursor_theme(
61                self.to_glib_none().0,
62                theme.to_glib_none().0,
63                size as _,
64            )
65        }
66    }
67
68    #[doc(alias = "gdk_wayland_display_set_startup_notification_id")]
69    pub fn set_startup_notification_id(&self, startup_id: &str) {
70        unsafe {
71            ffi::gdk_wayland_display_set_startup_notification_id(
72                self.to_glib_none().0,
73                startup_id.to_glib_none().0,
74            )
75        }
76    }
77
78    #[doc(alias = "gdk_wayland_display_prefers_ssd")]
79    pub fn prefers_ssd(&self) -> bool {
80        unsafe { from_glib(ffi::gdk_wayland_display_prefers_ssd(self.to_glib_none().0)) }
81    }
82
83    #[doc(alias = "gdk_wayland_display_query_registry")]
84    pub fn query_registry(&self, global: &str) -> bool {
85        unsafe {
86            from_glib(ffi::gdk_wayland_display_query_registry(
87                self.to_glib_none().0,
88                global.to_glib_none().0,
89            ))
90        }
91    }
92
93    pub(crate) fn connection(&self) -> wayland_client::Connection {
94        unsafe {
95            match self
96                .qdata::<Option<wayland_client::Connection>>(*WAYLAND_DISPLAY_CONNECTION_QUARK)
97            {
98                Some(conn) => conn.as_ref().clone().unwrap(),
99                None => {
100                    let display_ptr =
101                        ffi::gdk_wayland_display_get_wl_display(self.to_glib_none().0);
102                    let backend = wayland_backend::sys::client::Backend::from_foreign_display(
103                        display_ptr as *mut _,
104                    );
105                    let conn = wayland_client::Connection::from_backend(backend);
106                    self.set_qdata(*WAYLAND_DISPLAY_CONNECTION_QUARK, conn.clone());
107
108                    conn
109                }
110            }
111        }
112    }
113}