Skip to main content

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::{SignalHandlerId, connect_raw};
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::{X11Display, ffi, prelude::*};
22#[cfg(not(feature = "xlib"))]
23use crate::{XCursor, XWindow};
24
25impl X11Display {
26    /// Retrieves the EGL display connection object for the given GDK display.
27    ///
28    /// This function returns `NULL` if GDK is using GLX.
29    ///
30    /// # Deprecated since 4.18
31    ///
32    ///
33    /// # Returns
34    ///
35    /// the EGL display object
36    #[cfg(all(feature = "v4_4", feature = "egl"))]
37    #[cfg_attr(docsrs, doc(cfg(all(feature = "v4_4", feature = "egl"))))]
38    #[doc(alias = "gdk_x11_display_get_egl_display")]
39    #[doc(alias = "get_egl_display")]
40    pub fn egl_display(&self) -> Option<egl::Display> {
41        unsafe {
42            let ptr = ffi::gdk_x11_display_get_egl_display(self.to_glib_none().0);
43            if ptr.is_null() {
44                None
45            } else {
46                Some(egl::Display::from_ptr(ptr))
47            }
48        }
49    }
50
51    /// Returns the X cursor belonging to a [`gdk::Cursor`][crate::gdk::Cursor], potentially
52    /// creating the cursor.
53    ///
54    /// Be aware that the returned cursor may not be unique to @cursor.
55    /// It may for example be shared with its fallback cursor. On old
56    /// X servers that don't support the XCursor extension, all cursors
57    /// may even fall back to a few default cursors.
58    ///
59    /// # Deprecated since 4.18
60    ///
61    /// ## `cursor`
62    /// a [`gdk::Cursor`][crate::gdk::Cursor]
63    ///
64    /// # Returns
65    ///
66    /// an Xlib Cursor.
67    #[doc(alias = "gdk_x11_display_get_xcursor")]
68    #[doc(alias = "get_xcursor")]
69    pub fn xcursor(&self, cursor: &gdk::Cursor) -> XCursor {
70        unsafe { ffi::gdk_x11_display_get_xcursor(self.to_glib_none().0, cursor.to_glib_none().0) }
71    }
72
73    /// Returns the root X window used by [`gdk::Display`][crate::gdk::Display].
74    ///
75    /// # Deprecated since 4.18
76    ///
77    ///
78    /// # Returns
79    ///
80    /// an X Window
81    #[doc(alias = "gdk_x11_display_get_xrootwindow")]
82    #[doc(alias = "get_xrootwindow")]
83    pub fn xrootwindow(&self) -> XWindow {
84        unsafe { ffi::gdk_x11_display_get_xrootwindow(self.to_glib_none().0) }
85    }
86
87    /// Returns the X display of a [`gdk::Display`][crate::gdk::Display].
88    ///
89    /// # Deprecated since 4.18
90    ///
91    ///
92    /// # Returns
93    ///
94    /// an X display
95    #[cfg(feature = "xlib")]
96    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
97    #[doc(alias = "gdk_x11_display_get_xdisplay")]
98    #[doc(alias = "get_xdisplay")]
99    #[allow(clippy::missing_safety_doc)]
100    pub unsafe fn xdisplay(&self) -> *mut xlib::Display {
101        unsafe { ffi::gdk_x11_display_get_xdisplay(self.to_glib_none().0) as *mut xlib::Display }
102    }
103
104    /// Returns the X Screen used by [`gdk::Display`][crate::gdk::Display].
105    ///
106    /// # Deprecated since 4.18
107    ///
108    ///
109    /// # Returns
110    ///
111    /// an X Screen
112    #[cfg(feature = "xlib")]
113    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
114    #[doc(alias = "gdk_x11_display_get_xscreen")]
115    #[doc(alias = "get_xscreen")]
116    #[allow(clippy::missing_safety_doc)]
117    pub unsafe fn xscreen(&self) -> *mut xlib::Screen {
118        unsafe { ffi::gdk_x11_display_get_xscreen(self.to_glib_none().0) as *mut xlib::Screen }
119    }
120
121    /// The ::xevent signal is a low level signal that is emitted
122    /// whenever an XEvent has been received.
123    ///
124    /// When handlers to this signal return [`true`], no other handlers will be
125    /// invoked. In particular, the default handler for this function is
126    /// GDK's own event handling mechanism, so by returning [`true`] for an event
127    /// that GDK expects to translate, you may break GDK and/or GTK+ in
128    /// interesting ways. You have been warned.
129    ///
130    /// If you want this signal handler to queue a [`gdk::Event`][crate::gdk::Event], you can use
131    /// gdk_display_put_event().
132    ///
133    /// If you are interested in X GenericEvents, bear in mind that
134    /// XGetEventData() has been already called on the event, and
135    /// XFreeEventData() will be called afterwards.
136    /// ## `xevent`
137    /// a pointer to the XEvent to process
138    ///
139    /// # Returns
140    ///
141    /// [`true`] to stop other handlers from being invoked for the event.
142    ///   [`false`] to propagate the event further.
143    #[cfg(feature = "xlib")]
144    #[cfg_attr(docsrs, doc(cfg(feature = "xlib")))]
145    #[doc(alias = "xevent")]
146    #[allow(clippy::missing_safety_doc)]
147    pub unsafe fn connect_xevent<F: Fn(&Self, *mut xlib::XEvent) -> glib::Propagation + 'static>(
148        &self,
149        f: F,
150    ) -> SignalHandlerId {
151        unsafe {
152            unsafe extern "C" fn xevent_trampoline<
153                F: Fn(&X11Display, *mut xlib::XEvent) -> glib::Propagation + 'static,
154            >(
155                this: *mut ffi::GdkX11Display,
156                xevent: glib::ffi::gpointer,
157                f: glib::ffi::gpointer,
158            ) -> glib::ffi::gboolean {
159                unsafe {
160                    let f: &F = &*(f as *const F);
161                    f(&from_glib_borrow(this), xevent as *mut xlib::XEvent).into_glib()
162                }
163            }
164            let f: Box_<F> = Box_::new(f);
165            connect_raw(
166                self.as_ptr() as *mut _,
167                c"xevent".as_ptr() as *const _,
168                Some(transmute::<*const (), unsafe extern "C" fn()>(
169                    xevent_trampoline::<F> as *const (),
170                )),
171                Box_::into_raw(f),
172            )
173        }
174    }
175
176    /// Sets the program class.
177    ///
178    /// The X11 backend uses the program class to set the class name part
179    /// of the `WM_CLASS` property on toplevel windows; see the ICCCM.
180    ///
181    /// # Deprecated since 4.18
182    ///
183    /// ## `display`
184    /// a [`gdk::Display`][crate::gdk::Display]
185    /// ## `program_class`
186    /// a string
187    #[doc(alias = "gdk_x11_display_set_program_class")]
188    pub fn set_program_class(&self, program_class: impl IntoGStr) {
189        assert_initialized_main_thread!();
190        unsafe {
191            program_class.run_with_gstr(|program_class| {
192                ffi::gdk_x11_display_set_program_class(
193                    self.upcast_ref::<gdk::Display>().to_glib_none().0,
194                    program_class.as_ptr(),
195                );
196            });
197        }
198    }
199}