Skip to content

Commit

Permalink
gdk: Manually implement Cursor.new_from_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Apr 26, 2024
1 parent 85e6727 commit 7a987a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 60 deletions.
3 changes: 3 additions & 0 deletions gdk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
60 changes: 0 additions & 60 deletions gdk4/src/auto/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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<P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static>(
callback: P,
fallback: Option<&Cursor>,
) -> Option<Cursor> {
assert_initialized_main_thread!();
let callback_data: Box_<P> = 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::<P> 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::<P> as _);
let super_callback0: Box_<P> = 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<Cursor> {
Expand Down
66 changes: 66 additions & 0 deletions gdk4/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -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<Cursor> {
assert_initialized_main_thread!();
let callback_data: Box_<P> = 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::<P> 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::<P> as _);
let super_callback0: Box_<P> = 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,
))
}
}
}
3 changes: 3 additions & 0 deletions gdk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 7a987a7

Please sign in to comment.