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.
6 *****************************************************************************
7 * Copyright (C) 1998-2005 the VideoLAN team
10 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
39 #include <vlc_events.h>
40 #include <vlc_arrays.h>
42 /*****************************************************************************
43 * Documentation : Read vlc_events.h
44 *****************************************************************************/
48 /*****************************************************************************
50 *****************************************************************************/
52 typedef struct vlc_event_listener_t
55 vlc_event_callback_t pf_callback;
57 char * psz_debug_name;
59 } vlc_event_listener_t;
61 typedef struct vlc_event_listeners_group_t
63 vlc_event_type_t event_type;
64 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
66 /* Used in vlc_event_send() to make sure to behave
67 Correctly when vlc_event_detach was called during
69 vlc_bool_t b_sublistener_removed;
71 } vlc_event_listeners_group_t;
74 static const char * ppsz_event_type_to_name[] =
76 [vlc_InputItemMetaChanged] = "vlc_InputItemMetaChanged",
77 [vlc_InputItemSubItemAdded] = "vlc_InputItemSubItemAdded",
78 [vlc_ServicesDiscoveryItemAdded] = "vlc_ServicesDiscoveryItemAdded",
79 [vlc_ServicesDiscoveryItemRemoved] = "vlc_ServicesDiscoveryItemRemoved"
84 listeners_are_equal( vlc_event_listener_t * listener1,
85 vlc_event_listener_t * listener2 )
87 return listener1->pf_callback == listener2->pf_callback &&
88 listener1->p_user_data == listener2->p_user_data;
92 group_contains_listener( vlc_event_listeners_group_t * group,
93 vlc_event_listener_t * searched_listener )
95 vlc_event_listener_t * listener;
96 FOREACH_ARRAY( listener, group->listeners )
97 if( listeners_are_equal(searched_listener, listener) )
103 /*****************************************************************************
105 *****************************************************************************/
108 * Initialize event manager object
109 * p_obj is the object that contains the event manager. But not
110 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
112 * p_parent_obj gives a libvlc instance
114 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
115 vlc_object_t * p_parent_obj )
118 p_em->p_parent_object = p_parent_obj;
119 vlc_mutex_init( p_parent_obj, &p_em->object_lock );
121 /* We need a recursive lock here, because we need to be able
122 * to call libvlc_event_detach even if vlc_event_send is in
124 * This ensures that after libvlc_event_detach, the callback
125 * will never gets triggered.
127 vlc_mutex_init_recursive( p_parent_obj, &p_em->event_sending_lock );
128 ARRAY_INIT( p_em->listeners_groups );
133 * Destroy the event manager
135 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
137 struct vlc_event_listeners_group_t * listeners_group;
138 struct vlc_event_listener_t * listener;
140 vlc_mutex_destroy( &p_em->object_lock );
141 vlc_mutex_destroy( &p_em->event_sending_lock );
143 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
144 FOREACH_ARRAY( listener, listeners_group->listeners )
147 ARRAY_RESET( listeners_group->listeners );
148 free( listeners_group );
150 ARRAY_RESET( p_em->listeners_groups );
154 * Destroy the event manager
156 int vlc_event_manager_register_event_type(
157 vlc_event_manager_t * p_em,
158 vlc_event_type_t event_type )
160 vlc_event_listeners_group_t * listeners_group;
161 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
163 if( !listeners_group )
166 listeners_group->event_type = event_type;
167 ARRAY_INIT( listeners_group->listeners );
169 vlc_mutex_lock( &p_em->object_lock );
170 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
171 vlc_mutex_unlock( &p_em->object_lock );
177 * Send an event to the listener attached to this p_em.
179 void vlc_event_send( vlc_event_manager_t * p_em,
180 vlc_event_t * p_event )
182 vlc_event_listeners_group_t * listeners_group = NULL;
183 vlc_event_listener_t * listener;
184 vlc_event_listener_t * array_of_cached_listeners = NULL;
185 vlc_event_listener_t * cached_listener;
186 int i, i_cached_listeners = 0;
188 /* Fill event with the sending object now */
189 p_event->p_obj = p_em->p_obj;
191 vlc_mutex_lock( &p_em->object_lock );
192 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
193 if( listeners_group->event_type == p_event->type )
195 if( listeners_group->listeners.i_size <= 0 )
198 /* Save the function to call */
199 i_cached_listeners = listeners_group->listeners.i_size;
200 array_of_cached_listeners = malloc(
201 sizeof(vlc_event_listener_t)*i_cached_listeners );
202 if( !array_of_cached_listeners )
204 msg_Err( p_em->p_parent_object, "Not enough memory in vlc_event_send" );
205 vlc_mutex_unlock( &p_em->object_lock );
209 cached_listener = array_of_cached_listeners;
210 FOREACH_ARRAY( listener, listeners_group->listeners )
211 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
213 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
221 vlc_mutex_unlock( &p_em->object_lock );
223 /* Call the function attached */
224 cached_listener = array_of_cached_listeners;
226 if( !listeners_group || !array_of_cached_listeners )
228 free( array_of_cached_listeners );
232 vlc_mutex_lock( &p_em->event_sending_lock ) ;
234 /* Track item removed from *this* thread, with a simple flag */
235 listeners_group->b_sublistener_removed = VLC_FALSE;
237 for( i = 0; i < i_cached_listeners; i++ )
240 msg_Dbg( p_em->p_parent_object,
241 "Calling '%s' with a '%s' event (data %p)",
242 cached_listener->psz_debug_name,
243 ppsz_event_type_to_name[p_event->type],
244 cached_listener->p_user_data );
245 free(cached_listener->psz_debug_name);
247 /* No need to lock on listeners_group, a listener group can't be removed */
248 if( listeners_group->b_sublistener_removed )
250 /* If a callback was removed, this gets called */
251 vlc_bool_t valid_listener;
252 vlc_mutex_lock( &p_em->object_lock );
253 valid_listener = group_contains_listener( listeners_group, cached_listener );
254 vlc_mutex_unlock( &p_em->object_lock );
255 if( !valid_listener )
258 msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
264 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
267 vlc_mutex_unlock( &p_em->event_sending_lock );
269 free( array_of_cached_listeners );
273 * Add a callback for an event.
275 int __vlc_event_attach( vlc_event_manager_t * p_em,
276 vlc_event_type_t event_type,
277 vlc_event_callback_t pf_callback,
279 const char * psz_debug_name )
281 vlc_event_listeners_group_t * listeners_group;
282 vlc_event_listener_t * listener;
283 listener = malloc(sizeof(vlc_event_listener_t));
287 listener->p_user_data = p_user_data;
288 listener->pf_callback = pf_callback;
290 listener->psz_debug_name = strdup( psz_debug_name );
292 (void)psz_debug_name;
295 vlc_mutex_lock( &p_em->object_lock );
296 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
297 if( listeners_group->event_type == event_type )
299 ARRAY_APPEND( listeners_group->listeners, listener );
301 msg_Dbg( p_em->p_parent_object,
302 "Listening to '%s' event with '%s' (data %p)",
303 ppsz_event_type_to_name[event_type],
304 listener->psz_debug_name,
305 listener->p_user_data );
307 vlc_mutex_unlock( &p_em->object_lock );
311 vlc_mutex_unlock( &p_em->object_lock );
313 msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
319 * Remove a callback for an event.
322 int vlc_event_detach( vlc_event_manager_t *p_em,
323 vlc_event_type_t event_type,
324 vlc_event_callback_t pf_callback,
327 vlc_event_listeners_group_t * listeners_group;
328 struct vlc_event_listener_t * listener;
330 vlc_mutex_lock( &p_em->object_lock );
331 vlc_mutex_lock( &p_em->event_sending_lock );
332 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
333 if( listeners_group->event_type == event_type )
335 FOREACH_ARRAY( listener, listeners_group->listeners )
336 if( listener->pf_callback == pf_callback &&
337 listener->p_user_data == p_user_data )
339 /* Tell vlc_event_send, we did remove an item from that group,
340 in case vlc_event_send is in our caller stack */
341 listeners_group->b_sublistener_removed = VLC_TRUE;
343 /* that's our listener */
344 ARRAY_REMOVE( listeners_group->listeners,
345 fe_idx /* This comes from the macro (and that's why
348 msg_Dbg( p_em->p_parent_object,
349 "Detaching '%s' from '%s' event (data %p)",
350 listener->psz_debug_name,
351 ppsz_event_type_to_name[event_type],
352 listener->p_user_data );
354 free( listener->psz_debug_name );
357 vlc_mutex_unlock( &p_em->event_sending_lock );
358 vlc_mutex_unlock( &p_em->object_lock );
364 vlc_mutex_unlock( &p_em->event_sending_lock );
365 vlc_mutex_unlock( &p_em->object_lock );
367 msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );