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 *****************************************************************************/
35 #include <vlc_events.h>
36 #include <vlc_arrays.h>
38 /*****************************************************************************
39 * Documentation : Read vlc_events.h
40 *****************************************************************************/
44 /*****************************************************************************
46 *****************************************************************************/
48 typedef struct vlc_event_listener_t
51 vlc_event_callback_t pf_callback;
53 char * psz_debug_name;
55 } vlc_event_listener_t;
57 typedef struct vlc_event_listeners_group_t
59 vlc_event_type_t event_type;
60 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
62 /* Used in vlc_event_send() to make sure to behave
63 Correctly when vlc_event_detach was called during
65 vlc_bool_t b_sublistener_removed;
67 } vlc_event_listeners_group_t;
70 static const char * ppsz_event_type_to_name[] =
72 [vlc_InputItemMetaChanged] = "vlc_InputItemMetaChanged",
73 [vlc_InputItemSubItemAdded] = "vlc_InputItemSubItemAdded",
74 [vlc_ServicesDiscoveryItemAdded] = "vlc_ServicesDiscoveryItemAdded",
75 [vlc_ServicesDiscoveryItemRemoved] = "vlc_ServicesDiscoveryItemRemoved"
80 group_contains_listener( vlc_event_listeners_group_t * group,
81 vlc_event_listener_t * searched_listener )
83 vlc_event_listener_t * listener;
84 FOREACH_ARRAY( listener, group->listeners )
85 if( searched_listener == listener )
91 /*****************************************************************************
93 *****************************************************************************/
96 * Initialize event manager object
97 * p_obj is the object that contains the event manager. But not
98 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
100 * p_parent_obj gives a libvlc instance
102 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
103 vlc_object_t * p_parent_obj )
106 p_em->p_parent_object = p_parent_obj;
107 vlc_mutex_init( p_parent_obj, &p_em->object_lock );
109 /* We need a recursive lock here, because we need to be able
110 * to call libvlc_event_detach even if vlc_event_send is in
112 * This ensures that after libvlc_event_detach, the callback
113 * will never gets triggered.
115 vlc_mutex_init_recursive( p_parent_obj, &p_em->event_sending_lock );
116 ARRAY_INIT( p_em->listeners_groups );
121 * Destroy the event manager
123 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
125 struct vlc_event_listeners_group_t * listeners_group;
126 struct vlc_event_listener_t * listener;
128 vlc_mutex_destroy( &p_em->object_lock );
129 vlc_mutex_destroy( &p_em->event_sending_lock );
131 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
132 FOREACH_ARRAY( listener, listeners_group->listeners )
135 ARRAY_RESET( listeners_group->listeners );
136 free( listeners_group );
138 ARRAY_RESET( p_em->listeners_groups );
142 * Destroy the event manager
144 int vlc_event_manager_register_event_type(
145 vlc_event_manager_t * p_em,
146 vlc_event_type_t event_type )
148 vlc_event_listeners_group_t * listeners_group;
149 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
151 if( !listeners_group )
154 listeners_group->event_type = event_type;
155 ARRAY_INIT( listeners_group->listeners );
157 vlc_mutex_lock( &p_em->object_lock );
158 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
159 vlc_mutex_unlock( &p_em->object_lock );
165 * Send an event to the listener attached to this p_em.
167 void vlc_event_send( vlc_event_manager_t * p_em,
168 vlc_event_t * p_event )
170 vlc_event_listeners_group_t * listeners_group = NULL;
171 vlc_event_listener_t * listener;
172 vlc_event_listener_t * array_of_cached_listeners = NULL;
173 vlc_event_listener_t * cached_listener;
174 int i, i_cached_listeners = 0;
176 /* Fill event with the sending object now */
177 p_event->p_obj = p_em->p_obj;
179 vlc_mutex_lock( &p_em->object_lock );
180 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
181 if( listeners_group->event_type == p_event->type )
183 if( listeners_group->listeners.i_size <= 0 )
186 /* Save the function to call */
187 i_cached_listeners = listeners_group->listeners.i_size;
188 array_of_cached_listeners = malloc(
189 sizeof(vlc_event_listener_t)*i_cached_listeners );
190 if( !array_of_cached_listeners )
192 msg_Err( p_em->p_parent_object, "Not enough memory in vlc_event_send" );
193 vlc_mutex_unlock( &p_em->object_lock );
197 cached_listener = array_of_cached_listeners;
198 FOREACH_ARRAY( listener, listeners_group->listeners )
199 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
201 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
209 vlc_mutex_unlock( &p_em->object_lock );
211 /* Call the function attached */
212 cached_listener = array_of_cached_listeners;
213 vlc_mutex_lock( &p_em->event_sending_lock );
215 /* Track item removed from *this* thread, with a simple flag */
216 listeners_group->b_sublistener_removed = VLC_FALSE;
218 for( i = 0; i < i_cached_listeners; i++ )
221 msg_Dbg( p_em->p_parent_object,
222 "Calling '%s' with a '%s' event (data %p)",
223 cached_listener->psz_debug_name,
224 ppsz_event_type_to_name[p_event->type],
225 cached_listener->p_user_data );
226 free(cached_listener->psz_debug_name);
228 /* No need to lock on listeners_group, a listener group can't be removed */
229 if( listeners_group->b_sublistener_removed )
231 /* If a callback was removed, this gets called */
232 vlc_bool_t valid_listener;
233 vlc_mutex_lock( &p_em->object_lock );
234 valid_listener = group_contains_listener( listeners_group, cached_listener );
235 vlc_mutex_unlock( &p_em->object_lock );
236 if( !valid_listener )
239 msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
245 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
248 vlc_mutex_unlock( &p_em->event_sending_lock );
250 free( array_of_cached_listeners );
254 * Add a callback for an event.
256 int __vlc_event_attach( vlc_event_manager_t * p_em,
257 vlc_event_type_t event_type,
258 vlc_event_callback_t pf_callback,
260 const char * psz_debug_name )
262 vlc_event_listeners_group_t * listeners_group;
263 vlc_event_listener_t * listener;
264 listener = malloc(sizeof(vlc_event_listener_t));
268 listener->p_user_data = p_user_data;
269 listener->pf_callback = pf_callback;
271 listener->psz_debug_name = strdup( psz_debug_name );
273 (void)psz_debug_name;
276 vlc_mutex_lock( &p_em->object_lock );
277 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
278 if( listeners_group->event_type == event_type )
280 ARRAY_APPEND( listeners_group->listeners, listener );
282 msg_Dbg( p_em->p_parent_object,
283 "Listening to '%s' event with '%s' (data %p)",
284 ppsz_event_type_to_name[event_type],
285 listener->psz_debug_name,
286 listener->p_user_data );
288 vlc_mutex_unlock( &p_em->object_lock );
292 vlc_mutex_unlock( &p_em->object_lock );
294 msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
300 * Remove a callback for an event.
303 int vlc_event_detach( vlc_event_manager_t *p_em,
304 vlc_event_type_t event_type,
305 vlc_event_callback_t pf_callback,
308 vlc_event_listeners_group_t * listeners_group;
309 struct vlc_event_listener_t * listener;
311 vlc_mutex_lock( &p_em->object_lock );
312 vlc_mutex_lock( &p_em->event_sending_lock );
313 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
314 if( listeners_group->event_type == event_type )
316 FOREACH_ARRAY( listener, listeners_group->listeners )
317 if( listener->pf_callback == pf_callback &&
318 listener->p_user_data == p_user_data )
320 /* Tell vlc_event_send, we did remove an item from that group,
321 in case vlc_event_send is in our caller stack */
322 listeners_group->b_sublistener_removed = VLC_TRUE;
324 /* that's our listener */
325 ARRAY_REMOVE( listeners_group->listeners,
326 fe_idx /* This comes from the macro (and that's why
329 msg_Dbg( p_em->p_parent_object,
330 "Detaching '%s' from '%s' event (data %p)",
331 listener->psz_debug_name,
332 ppsz_event_type_to_name[event_type],
333 listener->p_user_data );
335 free( listener->psz_debug_name );
338 vlc_mutex_unlock( &p_em->event_sending_lock );
339 vlc_mutex_unlock( &p_em->object_lock );
345 vlc_mutex_unlock( &p_em->event_sending_lock );
346 vlc_mutex_unlock( &p_em->object_lock );
348 msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );