From 93d1349d787d65d3100f228368d1cb60cdce0f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 19 Dec 2018 01:15:23 +0200 Subject: [PATCH] Use new GLib API for generically implementing GObject interfaces --- gstreamer/src/subclass/child_proxy.rs | 47 ++++++------------ gstreamer/src/subclass/uri_handler.rs | 68 ++++++++++----------------- 2 files changed, 40 insertions(+), 75 deletions(-) diff --git a/gstreamer/src/subclass/child_proxy.rs b/gstreamer/src/subclass/child_proxy.rs index 83555f555..272d60b4a 100644 --- a/gstreamer/src/subclass/child_proxy.rs +++ b/gstreamer/src/subclass/child_proxy.rs @@ -16,8 +16,6 @@ use glib::translate::*; use libc; -use std::ptr; - use ChildProxy; pub trait ChildProxyImpl: super::element::ElementImpl + Send + Sync + 'static { @@ -46,6 +44,21 @@ pub trait ChildProxyImpl: super::element::ElementImpl + Send + Sync + 'static { fn child_removed(&self, object: &ChildProxy, child: &glib::Object, name: &str); } +unsafe impl IsImplementable for ChildProxy { + unsafe extern "C" fn interface_init( + iface: glib_ffi::gpointer, + _iface_data: glib_ffi::gpointer, + ) { + let child_proxy_iface = &mut *(iface as *mut ffi::GstChildProxyInterface); + + child_proxy_iface.get_child_by_name = Some(child_proxy_get_child_by_name::); + child_proxy_iface.get_child_by_index = Some(child_proxy_get_child_by_index::); + child_proxy_iface.get_children_count = Some(child_proxy_get_children_count::); + child_proxy_iface.child_added = Some(child_proxy_child_added::); + child_proxy_iface.child_removed = Some(child_proxy_child_removed::); + } +} + unsafe extern "C" fn child_proxy_get_child_by_name( child_proxy: *mut ffi::GstChildProxy, name: *const libc::c_char, @@ -127,33 +140,3 @@ unsafe extern "C" fn child_proxy_child_removed( String::from_glib_none(name).as_str(), ) } - -unsafe extern "C" fn child_proxy_init( - iface: glib_ffi::gpointer, - _iface_data: glib_ffi::gpointer, -) where - T: ChildProxyImpl, -{ - let child_proxy_iface = &mut *(iface as *mut ffi::GstChildProxyInterface); - - child_proxy_iface.get_child_by_name = Some(child_proxy_get_child_by_name::); - child_proxy_iface.get_child_by_index = Some(child_proxy_get_child_by_index::); - child_proxy_iface.get_children_count = Some(child_proxy_get_children_count::); - child_proxy_iface.child_added = Some(child_proxy_child_added::); - child_proxy_iface.child_removed = Some(child_proxy_child_removed::); -} - -pub fn register(type_: glib::subclass::InitializingType) { - unsafe { - let iface_info = gobject_ffi::GInterfaceInfo { - interface_init: Some(child_proxy_init::), - interface_finalize: None, - interface_data: ptr::null_mut(), - }; - gobject_ffi::g_type_add_interface_static( - type_.to_glib(), - ffi::gst_child_proxy_get_type(), - &iface_info, - ); - } -} diff --git a/gstreamer/src/subclass/uri_handler.rs b/gstreamer/src/subclass/uri_handler.rs index f748e63c9..d963571f7 100644 --- a/gstreamer/src/subclass/uri_handler.rs +++ b/gstreamer/src/subclass/uri_handler.rs @@ -8,7 +8,6 @@ use ffi; use glib_ffi; -use gobject_ffi; use glib; use glib::prelude::*; @@ -18,8 +17,6 @@ use glib::subclass::prelude::*; use libc; -use std::ptr; - use URIHandler; use URIType; @@ -30,6 +27,31 @@ pub trait URIHandlerImpl: super::element::ElementImpl + Send + Sync + 'static { fn get_protocols() -> Vec; } +unsafe impl IsImplementable for URIHandler { + unsafe extern "C" fn interface_init( + iface: glib_ffi::gpointer, + _iface_data: glib_ffi::gpointer, + ) { + let uri_handler_iface = &mut *(iface as *mut ffi::GstURIHandlerInterface); + + // Store the protocols in the interface data for later use + let mut data = T::type_data(); + let protocols = T::get_protocols(); + let protocols: *mut *const libc::c_char = protocols.to_glib_full(); + let data = data.as_mut(); + if data.interface_data.is_null() { + data.interface_data = Box::into_raw(Box::new(Vec::new())); + } + (*(data.interface_data as *mut Vec<(glib_ffi::GType, glib_ffi::gpointer)>)) + .push((URIHandler::static_type().to_glib(), protocols as *mut _)); + + uri_handler_iface.get_type = Some(uri_handler_get_type::); + uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::); + uri_handler_iface.get_uri = Some(uri_handler_get_uri::); + uri_handler_iface.set_uri = Some(uri_handler_set_uri::); + } +} + unsafe extern "C" fn uri_handler_get_type( _type_: glib_ffi::GType, ) -> ffi::GstURIType @@ -83,43 +105,3 @@ where } } } - -unsafe extern "C" fn uri_handler_init( - iface: glib_ffi::gpointer, - _iface_data: glib_ffi::gpointer, -) where - T: URIHandlerImpl, -{ - let uri_handler_iface = &mut *(iface as *mut ffi::GstURIHandlerInterface); - - // Store the protocols in the interface data for later use - let mut data = T::type_data(); - let protocols = T::get_protocols(); - let protocols: *mut *const libc::c_char = protocols.to_glib_full(); - let data = &mut *data.as_mut(); - if data.interface_data.is_null() { - data.interface_data = Box::into_raw(Box::new(Vec::new())); - } - (*(data.interface_data as *mut Vec<(glib_ffi::GType, glib_ffi::gpointer)>)) - .push((URIHandler::static_type().to_glib(), protocols as *mut _)); - - uri_handler_iface.get_type = Some(uri_handler_get_type::); - uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::); - uri_handler_iface.get_uri = Some(uri_handler_get_uri::); - uri_handler_iface.set_uri = Some(uri_handler_set_uri::); -} - -pub fn register(type_: &glib::subclass::InitializingType) { - unsafe { - let iface_info = gobject_ffi::GInterfaceInfo { - interface_init: Some(uri_handler_init::), - interface_finalize: None, - interface_data: ptr::null_mut(), - }; - gobject_ffi::g_type_add_interface_static( - type_.to_glib(), - ffi::gst_uri_handler_get_type(), - &iface_info, - ); - } -}