gdk4_x11/
x11_display.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(feature = "xlib")]
4#[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
5use std::{boxed::Box as Box_, mem::transmute};
6
7#[cfg(feature = "xlib")]
8#[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
9use glib::signal::{connect_raw, SignalHandlerId};
10use glib::translate::*;
11#[cfg(all(feature = "v4_4", feature = "egl"))]
12#[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
13use khronos_egl as egl;
14#[cfg(feature = "xlib")]
15#[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
16use x11::xlib;
17#[cfg(feature = "xlib")]
18#[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
19use x11::xlib::{Cursor as XCursor, Window as XWindow};
20
21use crate::{ffi, prelude::*, X11Display};
22#[cfg(not(feature = "xlib"))]
23use crate::{XCursor, XWindow};
24
25impl X11Display {
26    #[cfg(all(feature = "v4_4", feature = "egl"))]
27    #[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
28    #[doc(alias = "gdk_x11_display_get_egl_display")]
29    #[doc(alias = "get_egl_display")]
30    pub fn egl_display(&self) -> Option<egl::Display> {
31        unsafe {
32            let ptr = ffi::gdk_x11_display_get_egl_display(self.to_glib_none().0);
33            if ptr.is_null() {
34                None
35            } else {
36                Some(egl::Display::from_ptr(ptr))
37            }
38        }
39    }
40
41    #[doc(alias = "gdk_x11_display_get_xcursor")]
42    #[doc(alias = "get_xcursor")]
43    pub fn xcursor(&self, cursor: &gdk::Cursor) -> XCursor {
44        unsafe { ffi::gdk_x11_display_get_xcursor(self.to_glib_none().0, cursor.to_glib_none().0) }
45    }
46
47    #[doc(alias = "gdk_x11_display_get_xrootwindow")]
48    #[doc(alias = "get_xrootwindow")]
49    pub fn xrootwindow(&self) -> XWindow {
50        unsafe { ffi::gdk_x11_display_get_xrootwindow(self.to_glib_none().0) }
51    }
52
53    #[cfg(feature = "xlib")]
54    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
55    #[doc(alias = "gdk_x11_display_get_xdisplay")]
56    #[doc(alias = "get_xdisplay")]
57    #[allow(clippy::missing_safety_doc)]
58    pub unsafe fn xdisplay(&self) -> *mut xlib::Display {
59        ffi::gdk_x11_display_get_xdisplay(self.to_glib_none().0) as *mut xlib::Display
60    }
61
62    #[cfg(feature = "xlib")]
63    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
64    #[doc(alias = "gdk_x11_display_get_xscreen")]
65    #[doc(alias = "get_xscreen")]
66    #[allow(clippy::missing_safety_doc)]
67    pub unsafe fn xscreen(&self) -> *mut xlib::Screen {
68        ffi::gdk_x11_display_get_xscreen(self.to_glib_none().0) as *mut xlib::Screen
69    }
70
71    #[cfg(feature = "xlib")]
72    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
73    #[doc(alias = "xevent")]
74    #[allow(clippy::missing_safety_doc)]
75    pub unsafe fn connect_xevent<F: Fn(&Self, *mut xlib::XEvent) -> glib::Propagation + 'static>(
76        &self,
77        f: F,
78    ) -> SignalHandlerId {
79        unsafe extern "C" fn xevent_trampoline<
80            F: Fn(&X11Display, *mut xlib::XEvent) -> glib::Propagation + 'static,
81        >(
82            this: *mut ffi::GdkX11Display,
83            xevent: glib::ffi::gpointer,
84            f: glib::ffi::gpointer,
85        ) -> glib::ffi::gboolean {
86            let f: &F = &*(f as *const F);
87            f(&from_glib_borrow(this), xevent as *mut xlib::XEvent).into_glib()
88        }
89        let f: Box_<F> = Box_::new(f);
90        connect_raw(
91            self.as_ptr() as *mut _,
92            b"xevent\0".as_ptr() as *const _,
93            Some(transmute::<*const (), unsafe extern "C" fn()>(
94                xevent_trampoline::<F> as *const (),
95            )),
96            Box_::into_raw(f),
97        )
98    }
99
100    #[doc(alias = "gdk_x11_display_set_program_class")]
101    pub fn set_program_class(&self, program_class: impl IntoGStr) {
102        assert_initialized_main_thread!();
103        unsafe {
104            program_class.run_with_gstr(|program_class| {
105                ffi::gdk_x11_display_set_program_class(
106                    self.upcast_ref::<gdk::Display>().to_glib_none().0,
107                    program_class.as_ptr(),
108                );
109            });
110        }
111    }
112}