From f758b77805a44eb8d697d4fecab39ef2c2e6107f Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 25 Apr 2024 13:55:43 +0200 Subject: [PATCH] gdk: Manually implement Cursor.new_from_callback --- gdk4/Gir.toml | 3 ++ gdk4/src/auto/cursor.rs | 60 ------------------------------------- gdk4/src/cursor.rs | 66 +++++++++++++++++++++++++++++++++++++++++ gdk4/src/lib.rs | 3 ++ 4 files changed, 72 insertions(+), 60 deletions(-) create mode 100644 gdk4/src/cursor.rs diff --git a/gdk4/Gir.toml b/gdk4/Gir.toml index f3d2d3924a4c..bcd0f7996fbd 100644 --- a/gdk4/Gir.toml +++ b/gdk4/Gir.toml @@ -376,6 +376,9 @@ status = "generate" name = "Gdk.Cursor" status = "generate" generate_builder = true + [[object.function]] + name = "new_from_callback" + manual = true # handle &mut in the callback [[object]] name = "Gdk.Device" diff --git a/gdk4/src/auto/cursor.rs b/gdk4/src/auto/cursor.rs index 40eeb58e2ef0..72ae9db76bbb 100644 --- a/gdk4/src/auto/cursor.rs +++ b/gdk4/src/auto/cursor.rs @@ -4,9 +4,6 @@ use crate::Texture; use glib::{prelude::*, translate::*}; -#[cfg(feature = "v4_16")] -#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))] -use std::boxed::Box as Box_; glib::wrapper! { #[doc(alias = "GdkCursor")] @@ -18,63 +15,6 @@ glib::wrapper! { } impl Cursor { - #[cfg(feature = "v4_16")] - #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))] - #[doc(alias = "gdk_cursor_new_from_callback")] - #[doc(alias = "new_from_callback")] - pub fn from_callback Texture + 'static>( - callback: P, - fallback: Option<&Cursor>, - ) -> Option { - assert_initialized_main_thread!(); - let callback_data: Box_

= Box_::new(callback); - unsafe extern "C" fn callback_func< - P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static, - >( - cursor: *mut ffi::GdkCursor, - cursor_size: libc::c_int, - scale: libc::c_double, - width: *mut libc::c_int, - height: *mut libc::c_int, - hotspot_x: *mut libc::c_int, - hotspot_y: *mut libc::c_int, - data: glib::ffi::gpointer, - ) -> *mut ffi::GdkTexture { - let cursor = from_glib_borrow(cursor); - let callback = &*(data as *mut P); - (*callback)( - &cursor, - cursor_size, - scale, - width, - height, - hotspot_x, - hotspot_y, - ) - /*Not checked*/ - .to_glib_none() - .0 - } - let callback = Some(callback_func::

as _); - unsafe extern "C" fn destroy_func< - P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static, - >( - data: glib::ffi::gpointer, - ) { - let _callback = Box_::from_raw(data as *mut P); - } - let destroy_call2 = Some(destroy_func::

as _); - let super_callback0: Box_

= callback_data; - unsafe { - from_glib_full(ffi::gdk_cursor_new_from_callback( - callback, - Box_::into_raw(super_callback0) as *mut _, - destroy_call2, - fallback.to_glib_none().0, - )) - } - } - #[doc(alias = "gdk_cursor_new_from_name")] #[doc(alias = "new_from_name")] pub fn from_name(name: &str, fallback: Option<&Cursor>) -> Option { diff --git a/gdk4/src/cursor.rs b/gdk4/src/cursor.rs new file mode 100644 index 000000000000..ce68889d4b6f --- /dev/null +++ b/gdk4/src/cursor.rs @@ -0,0 +1,66 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use crate::{Cursor, Texture}; +use glib::translate::*; +use std::boxed::Box as Box_; + +impl Cursor { + #[cfg(feature = "v4_16")] + #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))] + #[doc(alias = "gdk_cursor_new_from_callback")] + #[doc(alias = "new_from_callback")] + pub fn from_callback< + P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static, + >( + callback: P, + fallback: Option<&Cursor>, + ) -> Option { + assert_initialized_main_thread!(); + let callback_data: Box_

= Box_::new(callback); + unsafe extern "C" fn callback_func< + P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static, + >( + cursor: *mut ffi::GdkCursor, + cursor_size: libc::c_int, + scale: libc::c_double, + width: *mut libc::c_int, + height: *mut libc::c_int, + hotspot_x: *mut libc::c_int, + hotspot_y: *mut libc::c_int, + data: glib::ffi::gpointer, + ) -> *mut ffi::GdkTexture { + let cursor = from_glib_borrow(cursor); + let callback = &*(data as *mut P); + (*callback)( + &cursor, + cursor_size, + scale, + &mut *width, + &mut *height, + &mut *hotspot_x, + &mut *hotspot_y, + ) + /*Not checked*/ + .to_glib_none() + .0 + } + let callback = Some(callback_func::

as _); + unsafe extern "C" fn destroy_func< + P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static, + >( + data: glib::ffi::gpointer, + ) { + let _callback = Box_::from_raw(data as *mut P); + } + let destroy_call2 = Some(destroy_func::

as _); + let super_callback0: Box_

= callback_data; + unsafe { + from_glib_full(ffi::gdk_cursor_new_from_callback( + callback, + Box_::into_raw(super_callback0) as *mut _, + destroy_call2, + fallback.to_glib_none().0, + )) + } + } +} diff --git a/gdk4/src/lib.rs b/gdk4/src/lib.rs index 65e67dce62ee..d3c4f4d89697 100644 --- a/gdk4/src/lib.rs +++ b/gdk4/src/lib.rs @@ -70,6 +70,9 @@ mod content_formats_builder; mod content_provider; mod content_serializer; mod crossing_event; +#[cfg(feature = "v4_16")] +#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))] +mod cursor; mod delete_event; mod display; #[cfg(feature = "v4_14")]