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[][40] =
75 [vlc_InputStateChanged] = "vlc_InputStateChanged",
76 [vlc_InputSelectedStreamChanged] = "vlc_InputSelectedStreamChanged",
78 [vlc_InputItemMetaChanged] = "vlc_InputItemMetaChanged",
79 [vlc_InputItemSubItemAdded] = "vlc_InputItemSubItemAdded",
80 [vlc_InputItemDurationChanged] = "vlc_InputItemDurationChanged",
81 [vlc_InputItemPreparsedChanged] = "vlc_InputItemPreparsedChanged",
82 [vlc_InputItemNameChanged] = "vlc_InputItemNameChanged",
83 [vlc_InputItemInfoChanged] = "vlc_InputItemInfoChanged",
84 [vlc_InputItemErrorWhenReadingChanged] = "vlc_InputItemErrorWhenReadingChanged",
86 [vlc_ServicesDiscoveryItemAdded] = "vlc_ServicesDiscoveryItemAdded",
87 [vlc_ServicesDiscoveryItemRemoved] = "vlc_ServicesDiscoveryItemRemoved",
88 [vlc_ServicesDiscoveryStarted] = "vlc_ServicesDiscoveryStarted",
89 [vlc_ServicesDiscoveryEnded] = "vlc_ServicesDiscoveryEnded"
94 listeners_are_equal( vlc_event_listener_t * listener1,
95 vlc_event_listener_t * listener2 )
97 return listener1->pf_callback == listener2->pf_callback &&
98 listener1->p_user_data == listener2->p_user_data;
102 group_contains_listener( vlc_event_listeners_group_t * group,
103 vlc_event_listener_t * searched_listener )
105 vlc_event_listener_t * listener;
106 FOREACH_ARRAY( listener, group->listeners )
107 if( listeners_are_equal(searched_listener, listener) )
113 /*****************************************************************************
115 *****************************************************************************/
118 * Initialize event manager object
119 * p_obj is the object that contains the event manager. But not
120 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
122 * p_parent_obj gives a libvlc instance
124 int __vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
125 vlc_object_t * p_parent_obj )
128 p_em->p_parent_object = p_parent_obj;
129 vlc_mutex_init( &p_em->object_lock );
131 /* We need a recursive lock here, because we need to be able
132 * to call libvlc_event_detach even if vlc_event_send is in
134 * This ensures that after libvlc_event_detach, the callback
135 * will never gets triggered.
137 vlc_mutex_init_recursive( &p_em->event_sending_lock );
138 ARRAY_INIT( p_em->listeners_groups );
143 * Destroy the event manager
145 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
147 struct vlc_event_listeners_group_t * listeners_group;
148 struct vlc_event_listener_t * listener;
150 vlc_mutex_destroy( &p_em->object_lock );
151 vlc_mutex_destroy( &p_em->event_sending_lock );
153 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
154 FOREACH_ARRAY( listener, listeners_group->listeners )
157 ARRAY_RESET( listeners_group->listeners );
158 free( listeners_group );
160 ARRAY_RESET( p_em->listeners_groups );
164 * Register the event manager
166 int vlc_event_manager_register_event_type(
167 vlc_event_manager_t * p_em,
168 vlc_event_type_t event_type )
170 vlc_event_listeners_group_t * listeners_group;
171 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
173 if( !listeners_group )
176 listeners_group->event_type = event_type;
177 ARRAY_INIT( listeners_group->listeners );
179 vlc_mutex_lock( &p_em->object_lock );
180 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
181 vlc_mutex_unlock( &p_em->object_lock );
187 * Send an event to the listener attached to this p_em.
189 void vlc_event_send( vlc_event_manager_t * p_em,
190 vlc_event_t * p_event )
192 vlc_event_listeners_group_t * listeners_group = NULL;
193 vlc_event_listener_t * listener;
194 vlc_event_listener_t * array_of_cached_listeners = NULL;
195 vlc_event_listener_t * cached_listener;
196 int i, i_cached_listeners = 0;
198 /* Fill event with the sending object now */
199 p_event->p_obj = p_em->p_obj;
201 vlc_mutex_lock( &p_em->object_lock );
202 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
203 if( listeners_group->event_type == p_event->type )
205 if( listeners_group->listeners.i_size <= 0 )
208 /* Save the function to call */
209 i_cached_listeners = listeners_group->listeners.i_size;
210 array_of_cached_listeners = malloc(
211 sizeof(vlc_event_listener_t)*i_cached_listeners );
212 if( !array_of_cached_listeners )
214 vlc_mutex_unlock( &p_em->object_lock );
218 cached_listener = array_of_cached_listeners;
219 FOREACH_ARRAY( listener, listeners_group->listeners )
220 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
222 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
230 vlc_mutex_unlock( &p_em->object_lock );
232 /* Call the function attached */
233 cached_listener = array_of_cached_listeners;
235 if( !listeners_group || !array_of_cached_listeners )
237 free( array_of_cached_listeners );
241 vlc_mutex_lock( &p_em->event_sending_lock ) ;
243 /* Track item removed from *this* thread, with a simple flag */
244 listeners_group->b_sublistener_removed = false;
246 for( i = 0; i < i_cached_listeners; i++ )
249 msg_Dbg( p_em->p_parent_object,
250 "Calling '%s' with a '%s' event (data %p)",
251 cached_listener->psz_debug_name,
252 ppsz_event_type_to_name[p_event->type],
253 cached_listener->p_user_data );
254 free(cached_listener->psz_debug_name);
256 /* No need to lock on listeners_group, a listener group can't be removed */
257 if( listeners_group->b_sublistener_removed )
259 /* If a callback was removed, this gets called */
261 vlc_mutex_lock( &p_em->object_lock );
262 valid_listener = group_contains_listener( listeners_group, cached_listener );
263 vlc_mutex_unlock( &p_em->object_lock );
264 if( !valid_listener )
267 msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
273 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
276 vlc_mutex_unlock( &p_em->event_sending_lock );
278 free( array_of_cached_listeners );
282 * Add a callback for an event.
284 int __vlc_event_attach( vlc_event_manager_t * p_em,
285 vlc_event_type_t event_type,
286 vlc_event_callback_t pf_callback,
288 const char * psz_debug_name )
290 vlc_event_listeners_group_t * listeners_group;
291 vlc_event_listener_t * listener;
292 listener = malloc(sizeof(vlc_event_listener_t));
296 listener->p_user_data = p_user_data;
297 listener->pf_callback = pf_callback;
299 listener->psz_debug_name = strdup( psz_debug_name );
301 (void)psz_debug_name;
304 vlc_mutex_lock( &p_em->object_lock );
305 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
306 if( listeners_group->event_type == event_type )
308 ARRAY_APPEND( listeners_group->listeners, listener );
310 msg_Dbg( p_em->p_parent_object,
311 "Listening to '%s' event with '%s' (data %p)",
312 ppsz_event_type_to_name[event_type],
313 listener->psz_debug_name,
314 listener->p_user_data );
316 vlc_mutex_unlock( &p_em->object_lock );
320 vlc_mutex_unlock( &p_em->object_lock );
322 msg_Err( p_em->p_parent_object, "cannot attach to an object event" );
328 * Remove a callback for an event.
331 int vlc_event_detach( vlc_event_manager_t *p_em,
332 vlc_event_type_t event_type,
333 vlc_event_callback_t pf_callback,
336 vlc_event_listeners_group_t * listeners_group;
337 struct vlc_event_listener_t * listener;
339 vlc_mutex_lock( &p_em->object_lock );
340 vlc_mutex_lock( &p_em->event_sending_lock );
341 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
342 if( listeners_group->event_type == event_type )
344 FOREACH_ARRAY( listener, listeners_group->listeners )
345 if( listener->pf_callback == pf_callback &&
346 listener->p_user_data == p_user_data )
348 /* Tell vlc_event_send, we did remove an item from that group,
349 in case vlc_event_send is in our caller stack */
350 listeners_group->b_sublistener_removed = true;
352 /* that's our listener */
353 ARRAY_REMOVE( listeners_group->listeners,
354 fe_idx /* This comes from the macro (and that's why
357 msg_Dbg( p_em->p_parent_object,
358 "Detaching '%s' from '%s' event (data %p)",
359 listener->psz_debug_name,
360 ppsz_event_type_to_name[event_type],
361 listener->p_user_data );
363 free( listener->psz_debug_name );
366 vlc_mutex_unlock( &p_em->event_sending_lock );
367 vlc_mutex_unlock( &p_em->object_lock );
373 vlc_mutex_unlock( &p_em->event_sending_lock );
374 vlc_mutex_unlock( &p_em->object_lock );
376 msg_Warn( p_em->p_parent_object, "cannot detach from an object event" );