Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Generate “open” signal for g_application #31

Closed
daa84 opened this issue Jun 5, 2017 · 7 comments
Closed

Generate “open” signal for g_application #31

daa84 opened this issue Jun 5, 2017 · 7 comments

Comments

@daa84
Copy link

daa84 commented Jun 5, 2017

I try to generate https://developer.gnome.org/gio/stable/GApplication.html#GApplication-open
When add Gio.File, Gir generate next closure:

Fn(&P, &[File], i32, &str)

But looks like this is wrong, because as i understand &[File] array can't be generated properly without n_files field, so looks like it is need to write this code manually? To make proper conversion between array of pointers to rust slice? Or any other way exists?
Thanks

@GuillaumeGomez
Copy link
Member

What is the i32 for in here?

@daa84
Copy link
Author

daa84 commented Jun 5, 2017

i32 is n_files - count of files in [File] array
as i understand **GFile is not finished with null value

@GuillaumeGomez
Copy link
Member

Yes, that's my point. n_files is useless in Rust since it's supposed to be the length of the slice.

@daa84
Copy link
Author

daa84 commented Jun 5, 2017

Ok, but how to implement this properly? Gir generator does not support such kind of thing? Does it need to implement this manually? Or maybe some converter already exists?

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Jun 5, 2017

I wonder. I think we already did it but not sure if it was in gir or manually...

cc @EPashkin

@EPashkin
Copy link
Member

EPashkin commented Jun 5, 2017

Yes, this need be manual. Something like this:

impl<O: IsA<Application> + IsA<glib::object::Object>> ApplicationExtManual for O {
   fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> u64 {
        unsafe {
            let f: Box_<Box_<Fn(&Self, &[File], &str) + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "open",
                transmute(open_trampoline::<Self> as usize), Box_::into_raw(f) as *mut _)
        }
    }
}
unsafe extern "C" fn open_trampoline<P>(this: *mut ffi::GApplication, files: *const *mut ffi::GFile, n_files: libc::c_int, hint: *mut libc::c_char, f: glib_ffi::gpointer)
where P: IsA<Application> {
    callback_guard!();
    let f: &Box_<Fn(&P, &[File], &str) + 'static> = transmute(f);
    let files: Vec<File> = FromGlibPtrContainer::from_glib_none_num(files, n_files as usize);
    f(&Application::from_glib_none(this).downcast_unchecked(), &files[..], &String::from_glib_none(hint))
}

@daa84
Copy link
Author

daa84 commented Jun 6, 2017

Thanks, i try 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants