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 VLC authors and VideoLAN
9 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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 *****************************************************************************/
45 /*****************************************************************************
47 *****************************************************************************/
49 typedef struct vlc_event_listener_t
52 vlc_event_callback_t pf_callback;
53 } vlc_event_listener_t;
55 typedef struct vlc_event_listeners_group_t
57 vlc_event_type_t event_type;
58 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
60 /* Used in vlc_event_send() to make sure to behave
61 Correctly when vlc_event_detach was called during
63 bool b_sublistener_removed;
65 } vlc_event_listeners_group_t;
68 listeners_are_equal( vlc_event_listener_t * listener1,
69 vlc_event_listener_t * listener2 )
71 return listener1->pf_callback == listener2->pf_callback &&
72 listener1->p_user_data == listener2->p_user_data;
76 group_contains_listener( vlc_event_listeners_group_t * group,
77 vlc_event_listener_t * searched_listener )
79 vlc_event_listener_t * listener;
80 FOREACH_ARRAY( listener, group->listeners )
81 if( listeners_are_equal(searched_listener, listener) )
87 /*****************************************************************************
89 *****************************************************************************/
91 #undef vlc_event_manager_init
93 * Initialize event manager object
94 * p_obj is the object that contains the event manager. But not
95 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
98 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj )
101 vlc_mutex_init( &p_em->object_lock );
103 /* We need a recursive lock here, because we need to be able
104 * to call libvlc_event_detach even if vlc_event_send is in
106 * This ensures that after libvlc_event_detach, the callback
107 * will never gets triggered.
109 vlc_mutex_init_recursive( &p_em->event_sending_lock );
110 ARRAY_INIT( p_em->listeners_groups );
115 * Destroy the event manager
117 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
119 struct vlc_event_listeners_group_t * listeners_group;
120 struct vlc_event_listener_t * listener;
122 vlc_mutex_destroy( &p_em->object_lock );
123 vlc_mutex_destroy( &p_em->event_sending_lock );
125 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
126 FOREACH_ARRAY( listener, listeners_group->listeners )
129 ARRAY_RESET( listeners_group->listeners );
130 free( listeners_group );
132 ARRAY_RESET( p_em->listeners_groups );
136 * Register the event manager
138 int vlc_event_manager_register_event_type(
139 vlc_event_manager_t * p_em,
140 vlc_event_type_t event_type )
142 vlc_event_listeners_group_t * listeners_group;
143 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
145 if( !listeners_group )
148 listeners_group->event_type = event_type;
149 ARRAY_INIT( listeners_group->listeners );
151 vlc_mutex_lock( &p_em->object_lock );
152 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
153 vlc_mutex_unlock( &p_em->object_lock );
159 * Send an event to the listener attached to this p_em.
161 void vlc_event_send( vlc_event_manager_t * p_em,
162 vlc_event_t * p_event )
164 vlc_event_listeners_group_t * listeners_group = NULL;
165 vlc_event_listener_t * listener;
166 vlc_event_listener_t * array_of_cached_listeners = NULL;
167 vlc_event_listener_t * cached_listener;
168 int i, i_cached_listeners = 0;
170 /* Fill event with the sending object now */
171 p_event->p_obj = p_em->p_obj;
173 vlc_mutex_lock( &p_em->event_sending_lock ) ;
174 vlc_mutex_lock( &p_em->object_lock );
176 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
177 if( listeners_group->event_type == p_event->type )
179 if( listeners_group->listeners.i_size <= 0 )
182 /* Save the function to call */
183 i_cached_listeners = listeners_group->listeners.i_size;
184 array_of_cached_listeners = malloc(
185 sizeof(vlc_event_listener_t)*i_cached_listeners );
186 if( !array_of_cached_listeners )
188 vlc_mutex_unlock( &p_em->object_lock );
189 vlc_mutex_unlock( &p_em->event_sending_lock ) ;
193 cached_listener = array_of_cached_listeners;
194 FOREACH_ARRAY( listener, listeners_group->listeners )
195 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
203 /* Track item removed from *this* thread, with a simple flag. Indeed
204 * event_sending_lock is a recursive lock. This has the advantage of
205 * allowing to remove an event listener from within a callback */
206 listeners_group->b_sublistener_removed = false;
208 vlc_mutex_unlock( &p_em->object_lock );
210 /* Call the function attached */
211 cached_listener = array_of_cached_listeners;
213 if( !listeners_group || !array_of_cached_listeners )
215 free( array_of_cached_listeners );
216 vlc_mutex_unlock( &p_em->event_sending_lock );
221 for( i = 0; i < i_cached_listeners; i++ )
223 if( listeners_group->b_sublistener_removed )
225 /* If a callback was removed inside one of our callback, this gets
228 vlc_mutex_lock( &p_em->object_lock );
229 valid_listener = group_contains_listener( listeners_group, cached_listener );
230 vlc_mutex_unlock( &p_em->object_lock );
231 if( !valid_listener )
237 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
240 vlc_mutex_unlock( &p_em->event_sending_lock );
242 free( array_of_cached_listeners );
245 #undef vlc_event_attach
247 * Add a callback for an event.
249 int vlc_event_attach( vlc_event_manager_t * p_em,
250 vlc_event_type_t event_type,
251 vlc_event_callback_t pf_callback,
254 vlc_event_listeners_group_t * listeners_group;
255 vlc_event_listener_t * listener;
256 listener = malloc(sizeof(vlc_event_listener_t));
260 listener->p_user_data = p_user_data;
261 listener->pf_callback = pf_callback;
263 vlc_mutex_lock( &p_em->object_lock );
264 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
265 if( listeners_group->event_type == event_type )
267 ARRAY_APPEND( listeners_group->listeners, listener );
268 vlc_mutex_unlock( &p_em->object_lock );
272 /* Unknown event = BUG */
277 * Remove a callback for an event.
280 void vlc_event_detach( vlc_event_manager_t *p_em,
281 vlc_event_type_t event_type,
282 vlc_event_callback_t pf_callback,
285 vlc_event_listeners_group_t * listeners_group;
286 struct vlc_event_listener_t * listener;
288 vlc_mutex_lock( &p_em->event_sending_lock );
289 vlc_mutex_lock( &p_em->object_lock );
290 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
291 if( listeners_group->event_type == event_type )
293 FOREACH_ARRAY( listener, listeners_group->listeners )
294 if( listener->pf_callback == pf_callback &&
295 listener->p_user_data == p_user_data )
297 /* Tell vlc_event_send, we did remove an item from that group,
298 in case vlc_event_send is in our caller stack */
299 listeners_group->b_sublistener_removed = true;
301 /* that's our listener */
302 ARRAY_REMOVE( listeners_group->listeners,
303 fe_idx /* This comes from the macro (and that's why
306 vlc_mutex_unlock( &p_em->event_sending_lock );
307 vlc_mutex_unlock( &p_em->object_lock );