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