]> git.sesse.net Git - vlc/blobdiff - src/misc/events.c
es_format: avoid copy in video_format_IsSimilar() if possible
[vlc] / src / misc / events.c
index 3bad1d8bcdc46ab22a42be6af14ec9f2197462ec..fb2a04df3accfd0cdac885211dce36c37aa5fdb3 100644 (file)
@@ -2,26 +2,25 @@
  * events.c: events interface
  * This library provides an interface to the send and receive events.
  * It is more lightweight than variable based callback.
- * Methode
  *****************************************************************************
- * Copyright (C) 1998-2005 the VideoLAN team
+ * Copyright (C) 1998-2005 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -32,7 +31,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 
 #include <assert.h>
 
@@ -43,8 +42,6 @@
  * Documentation : Read vlc_events.h
  *****************************************************************************/
 
-//#define DEBUG_EVENT
-
 /*****************************************************************************
  *  Private types.
  *****************************************************************************/
@@ -53,9 +50,6 @@ typedef struct vlc_event_listener_t
 {
     void *               p_user_data;
     vlc_event_callback_t pf_callback;
-#ifdef DEBUG_EVENT
-    char *               psz_debug_name;
-#endif
 } vlc_event_listener_t;
 
 typedef struct vlc_event_listeners_group_t
@@ -66,49 +60,45 @@ typedef struct vlc_event_listeners_group_t
    /* Used in vlc_event_send() to make sure to behave
       Correctly when vlc_event_detach was called during
       a callback */
-    vlc_bool_t          b_sublistener_removed;
+    bool          b_sublistener_removed;
                                          
 } vlc_event_listeners_group_t;
 
-#ifdef DEBUG_EVENT
-static const char * ppsz_event_type_to_name[] =
+static bool
+listeners_are_equal( vlc_event_listener_t * listener1,
+                     vlc_event_listener_t * listener2 )
 {
-    [vlc_InputItemMetaChanged]          = "vlc_InputItemMetaChanged",
-    [vlc_InputItemSubItemAdded]         = "vlc_InputItemSubItemAdded",
-    [vlc_ServicesDiscoveryItemAdded]    = "vlc_ServicesDiscoveryItemAdded",
-    [vlc_ServicesDiscoveryItemRemoved]  = "vlc_ServicesDiscoveryItemRemoved"
-};
-#endif
+    return listener1->pf_callback == listener2->pf_callback &&
+           listener1->p_user_data == listener2->p_user_data;
+}
 
-static vlc_bool_t
+static bool
 group_contains_listener( vlc_event_listeners_group_t * group,
                          vlc_event_listener_t * searched_listener )
 {
     vlc_event_listener_t * listener;
     FOREACH_ARRAY( listener, group->listeners )
-        if( searched_listener == listener )
-            return VLC_TRUE;
+        if( listeners_are_equal(searched_listener, listener) )
+            return true;
     FOREACH_END()
-    return VLC_FALSE;
+    return false;
 }
 
 /*****************************************************************************
  *
  *****************************************************************************/
 
+#undef vlc_event_manager_init
 /**
  * Initialize event manager object
  * p_obj is the object that contains the event manager. But not
  * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
  * for instance).
- * p_parent_obj gives a libvlc instance
  */
-int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
-                            vlc_object_t * p_parent_obj )
+int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj )
 {
     p_em->p_obj = p_obj;
-    p_em->p_parent_object = p_parent_obj;
-    vlc_mutex_init( p_parent_obj, &p_em->object_lock );
+    vlc_mutex_init( &p_em->object_lock );
 
     /* We need a recursive lock here, because we need to be able
      * to call libvlc_event_detach even if vlc_event_send is in
@@ -116,7 +106,7 @@ int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
      * This ensures that after libvlc_event_detach, the callback
      * will never gets triggered.
      * */
-    vlc_mutex_init_recursive( p_parent_obj, &p_em->event_sending_lock );
+    vlc_mutex_init_recursive( &p_em->event_sending_lock );
     ARRAY_INIT( p_em->listeners_groups );
     return VLC_SUCCESS;
 }
@@ -143,7 +133,7 @@ void vlc_event_manager_fini( vlc_event_manager_t * p_em )
 }
 
 /**
- * Destroy the event manager
+ * Register the event manager
  */
 int vlc_event_manager_register_event_type(
         vlc_event_manager_t * p_em,
@@ -180,7 +170,9 @@ void vlc_event_send( vlc_event_manager_t * p_em,
     /* Fill event with the sending object now */
     p_event->p_obj = p_em->p_obj;
 
+    vlc_mutex_lock( &p_em->event_sending_lock ) ;
     vlc_mutex_lock( &p_em->object_lock );
+
     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
         if( listeners_group->event_type == p_event->type )
         {
@@ -193,62 +185,51 @@ void vlc_event_send( vlc_event_manager_t * p_em,
                     sizeof(vlc_event_listener_t)*i_cached_listeners );
             if( !array_of_cached_listeners )
             {
-                msg_Err( p_em->p_parent_object, "Not enough memory in vlc_event_send" );
                 vlc_mutex_unlock( &p_em->object_lock );
+                vlc_mutex_unlock( &p_em->event_sending_lock ) ;
                 return;
             }
 
             cached_listener = array_of_cached_listeners;
             FOREACH_ARRAY( listener, listeners_group->listeners )
                 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
-#ifdef DEBUG_EVENT
-                cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
-#endif
                 cached_listener++;
             FOREACH_END()
 
             break;
         }
     FOREACH_END()
+
+    /* Track item removed from *this* thread, with a simple flag. Indeed
+     * event_sending_lock is a recursive lock. This has the advantage of
+     * allowing to remove an event listener from within a callback */
+    listeners_group->b_sublistener_removed = false;
+
     vlc_mutex_unlock( &p_em->object_lock );
+
     /* Call the function attached */
     cached_listener = array_of_cached_listeners;
 
     if( !listeners_group || !array_of_cached_listeners )
     {
         free( array_of_cached_listeners );
+        vlc_mutex_unlock( &p_em->event_sending_lock );
         return;
     }
 
-    vlc_mutex_lock( &p_em->event_sending_lock ) ;
-
-    /* Track item removed from *this* thread, with a simple flag */
-    listeners_group->b_sublistener_removed = VLC_FALSE;
 
     for( i = 0; i < i_cached_listeners; i++ )
     {
-#ifdef DEBUG_EVENT
-        msg_Dbg( p_em->p_parent_object,
-                    "Calling '%s' with a '%s' event (data %p)",
-                    cached_listener->psz_debug_name,
-                    ppsz_event_type_to_name[p_event->type],
-                    cached_listener->p_user_data );
-        free(cached_listener->psz_debug_name);
-#endif
-        /* No need to lock on listeners_group, a listener group can't be removed */
         if( listeners_group->b_sublistener_removed )
         {
-            /* If a callback was removed, this gets called */
-            vlc_bool_t valid_listener;
+            /* If a callback was removed inside one of our callback, this gets
+            * called */
+            bool valid_listener;
             vlc_mutex_lock( &p_em->object_lock );
             valid_listener = group_contains_listener( listeners_group, cached_listener );
             vlc_mutex_unlock( &p_em->object_lock );
             if( !valid_listener )
             {
-#ifdef DEBUG_EVENT
-                msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
-#endif
                 cached_listener++;
                 continue;
             }
@@ -261,14 +242,14 @@ void vlc_event_send( vlc_event_manager_t * p_em,
     free( array_of_cached_listeners );
 }
 
+#undef vlc_event_attach
 /**
  * Add a callback for an event.
  */
-int __vlc_event_attach( vlc_event_manager_t * p_em,
-                        vlc_event_type_t event_type,
-                        vlc_event_callback_t pf_callback,
-                        void *p_user_data,
-                        const char * psz_debug_name )
+int vlc_event_attach( vlc_event_manager_t * p_em,
+                      vlc_event_type_t event_type,
+                      vlc_event_callback_t pf_callback,
+                      void *p_user_data )
 {
     vlc_event_listeners_group_t * listeners_group;
     vlc_event_listener_t * listener;
@@ -278,49 +259,34 @@ int __vlc_event_attach( vlc_event_manager_t * p_em,
  
     listener->p_user_data = p_user_data;
     listener->pf_callback = pf_callback;
-#ifdef DEBUG_EVENT
-    listener->psz_debug_name = strdup( psz_debug_name );
-#else
-    (void)psz_debug_name;
-#endif
 
     vlc_mutex_lock( &p_em->object_lock );
     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
         if( listeners_group->event_type == event_type )
         {
             ARRAY_APPEND( listeners_group->listeners, listener );
-#ifdef DEBUG_EVENT
-                msg_Dbg( p_em->p_parent_object,
-                    "Listening to '%s' event with '%s' (data %p)",
-                    ppsz_event_type_to_name[event_type],
-                    listener->psz_debug_name,
-                    listener->p_user_data );
-#endif
             vlc_mutex_unlock( &p_em->object_lock );
             return VLC_SUCCESS;
         }
     FOREACH_END()
-    vlc_mutex_unlock( &p_em->object_lock );
-
-    msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
-    free(listener);
-    return VLC_EGENERIC;
+    /* Unknown event = BUG */
+    assert( 0 );
 }
 
 /**
  * Remove a callback for an event.
  */
 
-int vlc_event_detach( vlc_event_manager_t *p_em,
-                      vlc_event_type_t event_type,
-                      vlc_event_callback_t pf_callback,
-                      void *p_user_data )
+void vlc_event_detach( vlc_event_manager_t *p_em,
+                       vlc_event_type_t event_type,
+                       vlc_event_callback_t pf_callback,
+                       void *p_user_data )
 {
     vlc_event_listeners_group_t * listeners_group;
     struct vlc_event_listener_t * listener;
 
-    vlc_mutex_lock( &p_em->object_lock );
     vlc_mutex_lock( &p_em->event_sending_lock );
+    vlc_mutex_lock( &p_em->object_lock );
     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
         if( listeners_group->event_type == event_type )
         {
@@ -330,34 +296,20 @@ int vlc_event_detach( vlc_event_manager_t *p_em,
                 {
                     /* Tell vlc_event_send, we did remove an item from that group,
                        in case vlc_event_send is in our caller stack  */
-                    listeners_group->b_sublistener_removed = VLC_TRUE;
+                    listeners_group->b_sublistener_removed = true;
 
                     /* that's our listener */
                     ARRAY_REMOVE( listeners_group->listeners,
                         fe_idx /* This comes from the macro (and that's why
                                   I hate macro) */ );
-#ifdef DEBUG_EVENT
-                    msg_Dbg( p_em->p_parent_object,
-                        "Detaching '%s' from '%s' event (data %p)",
-                        listener->psz_debug_name,
-                        ppsz_event_type_to_name[event_type],
-                        listener->p_user_data );
-
-                    free( listener->psz_debug_name );
-#endif
                     free( listener );
                     vlc_mutex_unlock( &p_em->event_sending_lock );
                     vlc_mutex_unlock( &p_em->object_lock );
-                    return VLC_SUCCESS;
+                    return;
                 }
             FOREACH_END()
         }
     FOREACH_END()
-    vlc_mutex_unlock( &p_em->event_sending_lock );
-    vlc_mutex_unlock( &p_em->object_lock );
 
-    msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );
-
-    return VLC_EGENERIC;
+    assert( 0 );
 }
-