Add EventManager::detach method

master
Rene Richter 2021-10-25 12:53:39 +02:00 committed by Martin Finkel
parent 6133d47ca7
commit 6c4a081099
2 changed files with 20 additions and 3 deletions

View File

@ -326,21 +326,35 @@ pub struct EventManager<'a> {
}
impl<'a> EventManager<'a> {
pub fn attach<F>(&self, event_type: EventType, callback: F) -> Result<(), ()>
pub fn detach(&self, event_type: EventType, registered_callback: *mut c_void) -> Result<(),()> {
let result = unsafe {
sys::libvlc_event_detach(self.ptr, event_type as i32, event_manager_callback, registered_callback)
};
if result == 0 {
Ok(())
} else {
Err(())
}
}
pub fn attach<F>(&self, event_type: EventType, callback: F) -> Result<*mut c_void, ()>
where F: Fn(Event, VLCObject) + Send + 'static
{
// Explicit type annotation is needed
let callback: Box<Box<dyn Fn(Event, VLCObject) + Send + 'static>> =
Box::new(Box::new(callback));
let raw = Box::into_raw(callback) as *mut c_void;
let result = unsafe{
sys::libvlc_event_attach(
self.ptr, event_type as i32, event_manager_callback,
Box::into_raw(callback) as *mut c_void)
raw)
};
if result == 0 {
Ok(())
Ok(raw)
}else{
Err(())
}

View File

@ -74,6 +74,9 @@ extern "C" {
pub fn libvlc_event_attach(
p_event_manager: *mut libvlc_event_manager_t, i_event_type: libvlc_event_type_t,
f_callback: libvlc_callback_t, user_data: *mut c_void) -> c_int;
pub fn libvlc_event_detach(
p_event_manager: *mut libvlc_event_manager_t, i_event_type: libvlc_event_type_t,
f_callback: libvlc_callback_t, p_user_data: *mut c_void) -> c_int;
pub fn libvlc_event_type_name(event_type: libvlc_event_type_t) -> *const c_char;
pub fn libvlc_log_get_context(
ctx: *const libvlc_log_t, module: *const *const c_char, file: *const *const c_char,