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