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::{WaylandToplevel, ffi};
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 unsafe {
52 let toplevel = from_glib_borrow(toplevel);
53 let handle: Borrowed<Option<glib::GString>> = from_glib_borrow(handle);
54 let callback = &*(user_data as *mut P);
55 if let Some(handle) = handle.as_ref() {
56 (*callback)(&toplevel, Ok(handle.as_str()))
57 } else {
58 (*callback)(
59 &toplevel,
60 Err(glib::bool_error!(
61 "Failed to export a handle. The compositor probably doesn't implement the xdg-foreign protocol"
62 )),
63 )
64 }
65 }
66 }
67 let callback = Some(callback_func::<P> as _);
68 unsafe extern "C" fn destroy_func_func<
69 P: Fn(&WaylandToplevel, Result<&str, glib::BoolError>) + 'static,
70 >(
71 data: glib::ffi::gpointer,
72 ) {
73 unsafe {
74 let _callback = Box_::from_raw(data as *mut P);
75 }
76 }
77 let destroy_call3 = Some(destroy_func_func::<P> as _);
78 let super_callback0: Box_<P> = callback_data;
79 unsafe {
80 from_glib(ffi::gdk_wayland_toplevel_export_handle(
81 self.to_glib_none().0,
82 callback,
83 Box_::into_raw(super_callback0) as *mut _,
84 destroy_call3,
85 ))
86 }
87 }
88}