1 /*****************************************************************************
2 * events.c: events interface
3 * This library provides an interface to the send and receive events.
4 * It is more lightweight than variable based callback.
5 *****************************************************************************
6 * Copyright (C) 1998-2005 the VideoLAN team
9 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
38 #include <vlc_events.h>
39 #include <vlc_arrays.h>
41 /*****************************************************************************
42 * Documentation : Read vlc_events.h
43 *****************************************************************************/
47 /*****************************************************************************
49 *****************************************************************************/
51 typedef struct vlc_event_listener_t
54 vlc_event_callback_t pf_callback;
56 char * psz_debug_name;
58 } vlc_event_listener_t;
60 typedef struct vlc_event_listeners_group_t
62 vlc_event_type_t event_type;
63 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
65 /* Used in vlc_event_send() to make sure to behave
66 Correctly when vlc_event_detach was called during
68 bool b_sublistener_removed;
70 } vlc_event_listeners_group_t;
73 static const char ppsz_event_type_to_name[][33] =
75 [vlc_InputItemMetaChanged] = "vlc_InputItemMetaChanged",
76 [vlc_InputItemSubItemAdded] = "vlc_InputItemSubItemAdded",
77 [vlc_ServicesDiscoveryItemAdded] = "vlc_ServicesDiscoveryItemAdded",
78 [vlc_ServicesDiscoveryItemRemoved] = "vlc_ServicesDiscoveryItemRemoved"
83 listeners_are_equal( vlc_event_listener_t * listener1,
84 vlc_event_listener_t * listener2 )
86 return listener1->pf_callback == listener2->pf_callback &&
87 listener1->p_user_data == listener2->p_user_data;
91 group_contains_listener( vlc_event_listeners_group_t * group,
92 vlc_event_listener_t * searched_listener )
94 vlc_event_listener_t * listener;
95 FOREACH_ARRAY( listener, group->listeners )
96 if( listeners_are_equal(searched_listener, listener) )
102 /*****************************************************************************
104 *****************************************************************************/
107 * Initialize event manager object
108 * p_obj is the object that contains the event manager. But not
109 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
111 * p_parent_obj gives a libvlc instance
113 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
114 vlc_object_t * p_parent_obj )
117 p_em->p_parent_object = p_parent_obj;
118 vlc_mutex_init( &p_em->object_lock );
120 /* We need a recursive lock here, because we need to be able
121 * to call libvlc_event_detach even if vlc_event_send is in
123 * This ensures that after libvlc_event_detach, the callback
124 * will never gets triggered.
126 vlc_mutex_init_recursive( &p_em->event_sending_lock );
127 ARRAY_INIT( p_em->listeners_groups );
132 * Destroy the event manager
134 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
136 struct vlc_event_listeners_group_t * listeners_group;
137 struct vlc_event_listener_t * listener;
139 vlc_mutex_destroy( &p_em->object_lock );
140 vlc_mutex_destroy( &p_em->event_sending_lock );
142 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
143 FOREACH_ARRAY( listener, listeners_group->listeners )
146 ARRAY_RESET( listeners_group->listeners );
147 free( listeners_group );
149 ARRAY_RESET( p_em->listeners_groups );
153 * Destroy the event manager
155 int vlc_event_manager_register_event_type(
156 vlc_event_manager_t * p_em,
157 vlc_event_type_t event_type )
159 vlc_event_listeners_group_t * listeners_group;
160 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
162 if( !listeners_group )
165 listeners_group->event_type = event_type;
166 ARRAY_INIT( listeners_group->listeners );
168 vlc_mutex_lock( &p_em->object_lock );
169 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
170 vlc_mutex_unlock( &p_em->object_lock );
176 * Send an event to the listener attached to this p_em.
178 void vlc_event_send( vlc_event_manager_t * p_em,
179 vlc_event_t * p_event )
181 vlc_event_listeners_group_t * listeners_group = NULL;
182 vlc_event_listener_t * listener;
183 vlc_event_listener_t * array_of_cached_listeners = NULL;
184 vlc_event_listener_t * cached_listener;
185 int i, i_cached_listeners = 0;
187 /* Fill event with the sending object now */
188 p_event->p_obj = p_em->p_obj;
190 vlc_mutex_lock( &p_em->object_lock );
191 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
192 if( listeners_group->event_type == p_event->type )
194 if( listeners_group->listeners.i_size <= 0 )
197 /* Save the function to call */
198 i_cached_listeners = listeners_group->listeners.i_size;
199 array_of_cached_listeners = malloc(
200 sizeof(vlc_event_listener_t)*i_cached_listeners );
201 if( !array_of_cached_listeners )
203 msg_Err( p_em->p_parent_object, "Not enough memory in vlc_event_send" );
204 vlc_mutex_unlock( &p_em->object_lock );
208 cached_listener = array_of_cached_listeners;
209 FOREACH_ARRAY( listener, listeners_group->listeners )
210 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
212 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
220 vlc_mutex_unlock( &p_em->object_lock );
222 /* Call the function attached */
223 cached_listener = array_of_cached_listeners;
225 if( !listeners_group || !array_of_cached_listeners )
227 free( array_of_cached_listeners );
231 vlc_mutex_lock( &p_em->event_sending_lock ) ;
233 /* Track item removed from *this* thread, with a simple flag */
234 listeners_group->b_sublistener_removed = false;
236 for( i = 0; i < i_cached_listeners; i++ )
239 msg_Dbg( p_em->p_parent_object,
240 "Calling '%s' with a '%s' event (data %p)",
241 cached_listener->psz_debug_name,
242 ppsz_event_type_to_name[p_event->type],
243 cached_listener->p_user_data );
244 free(cached_listener->psz_debug_name);
246 /* No need to lock on listeners_group, a listener group can't be removed */
247 if( listeners_group->b_sublistener_removed )
249 /* If a callback was removed, this gets called */
251 vlc_mutex_lock( &p_em->object_lock );
252 valid_listener = group_contains_listener( listeners_group, cached_listener );
253 vlc_mutex_unlock( &p_em->object_lock );
254 if( !valid_listener )
257 msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
263 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
266 vlc_mutex_unlock( &p_em->event_sending_lock );
268 free( array_of_cached_listeners );
272 * Add a callback for an event.
274 int __vlc_event_attach( vlc_event_manager_t * p_em,
275 vlc_event_type_t event_type,
276 vlc_event_callback_t pf_callback,
278 const char * psz_debug_name )
280 vlc_event_listeners_group_t * listeners_group;
281 vlc_event_listener_t * listener;
282 listener = malloc(sizeof(vlc_event_listener_t));
286 listener->p_user_data = p_user_data;
287 listener->pf_callback = pf_callback;
289 listener->psz_debug_name = strdup( psz_debug_name );
291 (void)psz_debug_name;
294 vlc_mutex_lock( &p_em->object_lock );
295 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
296 if( listeners_group->event_type == event_type )
298 ARRAY_APPEND( listeners_group->listeners, listener );
300 msg_Dbg( p_em->p_parent_object,
301 "Listening to '%s' event with '%s' (data %p)",
302 ppsz_event_type_to_name[event_type],
303 listener->psz_debug_name,
304 listener->p_user_data );
306 vlc_mutex_unlock( &p_em->object_lock );
310 vlc_mutex_unlock( &p_em->object_lock );
312 msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
318 * Remove a callback for an event.
321 int vlc_event_detach( vlc_event_manager_t *p_em,
322 vlc_event_type_t event_type,
323 vlc_event_callback_t pf_callback,
326 vlc_event_listeners_group_t * listeners_group;
327 struct vlc_event_listener_t * listener;
329 vlc_mutex_lock( &p_em->object_lock );
330 vlc_mutex_lock( &p_em->event_sending_lock );
331 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
332 if( listeners_group->event_type == event_type )
334 FOREACH_ARRAY( listener, listeners_group->listeners )
335 if( listener->pf_callback == pf_callback &&
336 listener->p_user_data == p_user_data )
338 /* Tell vlc_event_send, we did remove an item from that group,
339 in case vlc_event_send is in our caller stack */
340 listeners_group->b_sublistener_removed = true;
342 /* that's our listener */
343 ARRAY_REMOVE( listeners_group->listeners,
344 fe_idx /* This comes from the macro (and that's why
347 msg_Dbg( p_em->p_parent_object,
348 "Detaching '%s' from '%s' event (data %p)",
349 listener->psz_debug_name,
350 ppsz_event_type_to_name[event_type],
351 listener->p_user_data );
353 free( listener->psz_debug_name );
356 vlc_mutex_unlock( &p_em->event_sending_lock );
357 vlc_mutex_unlock( &p_em->object_lock );
363 vlc_mutex_unlock( &p_em->event_sending_lock );
364 vlc_mutex_unlock( &p_em->object_lock );
366 msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );