gdk4_wayland/
wayland_toplevel.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ffi, WaylandToplevel};
4use glib::translate::*;
5use std::boxed::Box as Box_;
6
7impl WaylandToplevel {
8    /// Asynchronously obtains a handle for a surface that can be passed
9    /// to other processes.
10    ///
11    /// When the handle has been obtained, @callback will be called.
12    ///
13    /// It is an error to call this function on a surface that is already
14    /// exported.
15    ///
16    /// When the handle is no longer needed, [`unexport_handle()`][Self::unexport_handle()]
17    /// should be called to clean up resources.
18    ///
19    /// The main purpose for obtaining a handle is to mark a surface
20    /// from another surface as transient for this one, see
21    /// [`set_transient_for_exported()`][Self::set_transient_for_exported()].
22    ///
23    /// Before 4.12, this API could not safely be used multiple times,
24    /// since there was no reference counting for handles. Starting with
25    /// 4.12, every call to this function obtains a new handle, and every
26    /// call to [`drop_exported_handle()`][Self::drop_exported_handle()] drops
27    /// just the handle that it is given.
28    ///
29    /// Note that this API depends on an unstable Wayland protocol,
30    /// and thus may require changes in the future.
31    /// ## `callback`
32    /// callback to call with the handle
33    ///
34    /// # Returns
35    ///
36    /// [`true`] if the handle has been requested, [`false`] if
37    ///   an error occurred.
38    #[doc(alias = "gdk_wayland_toplevel_export_handle")]
39    pub fn export_handle<P: Fn(&WaylandToplevel, Result<&str, glib::BoolError>) + 'static>(
40        &self,
41        callback: P,
42    ) -> bool {
43        let callback_data: Box_<P> = Box_::new(callback);
44        unsafe extern "C" fn callback_func<
45            P: Fn(&WaylandToplevel, Result<&str, glib::BoolError>) + 'static,
46        >(
47            toplevel: *mut ffi::GdkWaylandToplevel,
48            handle: *const libc::c_char,
49            user_data: glib::ffi::gpointer,
50        ) {
51            let toplevel = from_glib_borrow(toplevel);
52            let handle: Borrowed<Option<glib::GString>> = from_glib_borrow(handle);
53            let callback = &*(user_data as *mut P);
54            if let Some(handle) = handle.as_ref() {
55                (*callback)(&toplevel, Ok(handle.as_str()))
56            } else {
57                (*callback)(&toplevel, Err(glib::bool_error!("Failed to export a handle. The compositor probably doesn't implement the xdg-foreign protocol")))
58            }
59        }
60        let callback = Some(callback_func::<P> as _);
61        unsafe extern "C" fn destroy_func_func<
62            P: Fn(&WaylandToplevel, Result<&str, glib::BoolError>) + 'static,
63        >(
64            data: glib::ffi::gpointer,
65        ) {
66            let _callback = Box_::from_raw(data as *mut P);
67        }
68        let destroy_call3 = Some(destroy_func_func::<P> as _);
69        let super_callback0: Box_<P> = callback_data;
70        unsafe {
71            from_glib(ffi::gdk_wayland_toplevel_export_handle(
72                self.to_glib_none().0,
73                callback,
74                Box_::into_raw(super_callback0) as *mut _,
75                destroy_call3,
76            ))
77        }
78    }
79}