1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_playlist.h>
36 * Internal libvlc functions
39 /**************************************************************************
40 * libvlc_event_init (internal) :
42 * initialization function.
43 **************************************************************************/
44 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
46 /* Will certainly be used to install libvlc_instance event */
49 /**************************************************************************
50 * libvlc_event_fini (internal) :
52 * finalization function.
53 **************************************************************************/
54 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
58 /**************************************************************************
59 * libvlc_event_manager_init (internal) :
61 * Init an object's event manager.
62 **************************************************************************/
63 libvlc_event_manager_t *
64 libvlc_event_manager_new( void * p_obj, libvlc_instance_t * p_libvlc_inst,
65 libvlc_exception_t *p_e )
67 libvlc_event_manager_t * p_em;
69 p_em = malloc(sizeof( libvlc_event_manager_t ));
72 libvlc_exception_raise( p_e, "No Memory left" );
77 p_em->p_libvlc_instance = p_libvlc_inst;
78 libvlc_retain( p_libvlc_inst );
79 ARRAY_INIT( p_em->listeners_groups );
80 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
81 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
85 /**************************************************************************
86 * libvlc_event_manager_release (internal) :
88 * Init an object's event manager.
89 **************************************************************************/
90 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
92 libvlc_event_listeners_group_t * listeners_group;
93 libvlc_event_listener_t * listener;
95 vlc_mutex_destroy( &p_em->event_sending_lock );
96 vlc_mutex_destroy( &p_em->object_lock );
98 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
99 FOREACH_ARRAY( listener, listeners_group->listeners )
102 ARRAY_RESET( listeners_group->listeners );
103 free( listeners_group );
105 ARRAY_RESET( p_em->listeners_groups );
107 libvlc_release( p_em->p_libvlc_instance, NULL );
111 /**************************************************************************
112 * libvlc_event_manager_register_event_type (internal) :
114 * Init an object's event manager.
115 **************************************************************************/
116 void libvlc_event_manager_register_event_type(
117 libvlc_event_manager_t * p_em,
118 libvlc_event_type_t event_type,
119 libvlc_exception_t * p_e )
121 libvlc_event_listeners_group_t * listeners_group;
122 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
123 if( !listeners_group )
125 libvlc_exception_raise( p_e, "No Memory left" );
129 listeners_group->event_type = event_type;
130 ARRAY_INIT( listeners_group->listeners );
132 vlc_mutex_lock( &p_em->object_lock );
133 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
134 vlc_mutex_unlock( &p_em->object_lock );
137 /**************************************************************************
138 * libvlc_event_send (internal) :
141 **************************************************************************/
142 void libvlc_event_send( libvlc_event_manager_t * p_em,
143 libvlc_event_t * p_event )
145 libvlc_event_listeners_group_t * listeners_group;
146 libvlc_event_listener_t * listener_cached;
147 libvlc_event_listener_t * listener;
148 libvlc_event_listener_t * array_listeners_cached = NULL;
149 int i, i_cached_listeners = 0;
151 /* Fill event with the sending object now */
152 p_event->p_obj = p_em->p_obj;
154 /* Here a read/write lock would be nice */
156 vlc_mutex_lock( &p_em->object_lock );
157 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
158 if( listeners_group->event_type == p_event->type )
160 if( listeners_group->listeners.i_size <= 0 )
163 /* Cache a copy of the listener to avoid locking issues */
164 i_cached_listeners = listeners_group->listeners.i_size;
165 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
166 if( !array_listeners_cached )
168 printf( "Can't alloc memory in libvlc_event_send" );
172 listener_cached = array_listeners_cached;
173 FOREACH_ARRAY( listener, listeners_group->listeners )
174 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
181 vlc_mutex_unlock( &p_em->object_lock );
183 /* Here a read/write lock would be *especially* nice,
184 * We would be able to send event without blocking */
185 /* The only reason for this lock is to be able to wait the
186 * End of events sending with libvlc_event_manager_lock_event_sending.
187 * This can be useful to make sure a callback is completely detached. */
188 vlc_mutex_lock( &p_em->event_sending_lock );
189 listener_cached = array_listeners_cached;
190 for( i = 0; i < i_cached_listeners; i++ )
192 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
195 vlc_mutex_unlock( &p_em->event_sending_lock );
197 free( array_listeners_cached );
201 * Public libvlc functions
204 /**************************************************************************
205 * libvlc_event_attach (public) :
207 * Add a callback for an event.
208 **************************************************************************/
209 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
210 libvlc_event_type_t event_type,
211 libvlc_callback_t pf_callback,
213 libvlc_exception_t *p_e )
215 libvlc_event_listeners_group_t * listeners_group;
216 libvlc_event_listener_t * listener;
217 listener = malloc(sizeof(libvlc_event_listener_t));
220 libvlc_exception_raise( p_e, "No Memory left" );
224 listener->event_type = event_type;
225 listener->p_user_data = p_user_data;
226 listener->pf_callback = pf_callback;
228 vlc_mutex_lock( &p_event_manager->object_lock );
229 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
230 if( listeners_group->event_type == listener->event_type )
232 ARRAY_APPEND( listeners_group->listeners, listener );
233 vlc_mutex_unlock( &p_event_manager->object_lock );
237 vlc_mutex_unlock( &p_event_manager->object_lock );
240 libvlc_exception_raise( p_e,
241 "This object event manager doesn't know about '%s' events",
242 libvlc_event_type_name(a));
245 /**************************************************************************
246 * libvlc_event_detach (public) :
248 * Remove a callback for an event.
249 **************************************************************************/
250 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
251 libvlc_event_type_t event_type,
252 libvlc_callback_t pf_callback,
254 libvlc_exception_t *p_e )
256 libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
257 p_user_data, libvlc_UnLocked, p_e );
260 /**************************************************************************
261 * libvlc_event_detach_no_lock (internal) :
263 * Remove a callback for an event.
264 **************************************************************************/
265 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
266 libvlc_event_type_t event_type,
267 libvlc_callback_t pf_callback,
269 libvlc_lock_state_t lockstate,
270 libvlc_exception_t *p_e )
272 libvlc_event_listeners_group_t * listeners_group;
273 libvlc_event_listener_t * listener;
275 if( lockstate == libvlc_UnLocked )
276 vlc_mutex_lock( &p_event_manager->event_sending_lock );
277 vlc_mutex_lock( &p_event_manager->object_lock );
278 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
279 if( listeners_group->event_type == event_type )
281 FOREACH_ARRAY( listener, listeners_group->listeners )
282 if( listener->event_type == event_type &&
283 listener->pf_callback == pf_callback &&
284 listener->p_user_data == p_user_data )
286 /* that's our listener */
288 ARRAY_REMOVE( listeners_group->listeners,
289 fe_idx /* This comes from the macro (and that's why
291 vlc_mutex_unlock( &p_event_manager->object_lock );
292 if( lockstate == libvlc_UnLocked )
293 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
299 vlc_mutex_unlock( &p_event_manager->object_lock );
300 if( lockstate == libvlc_UnLocked )
301 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
303 libvlc_exception_raise( p_e,
304 "This object event manager doesn't know about '%i,%p,%p' event observer",
305 event_type, pf_callback, p_user_data );