From 4c015edec43b255136c28939e1d1d800deaf01ed Mon Sep 17 00:00:00 2001
From: Rene Richter <richterrettich@gmail.com>
Date: Mon, 25 Oct 2021 12:53:39 +0200
Subject: [PATCH] Add EventManager::detach method

---
 src/core.rs | 20 +++++++++++++++++---
 src/sys.rs  |  3 +++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/core.rs b/src/core.rs
index 236de3c..3da9e5f 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -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(())
         }
diff --git a/src/sys.rs b/src/sys.rs
index 7221369..8994ecc 100644
--- a/src/sys.rs
+++ b/src/sys.rs
@@ -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,