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 ARRAY_INIT( p_em->listeners_groups );
79 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
80 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
84 /**************************************************************************
85 * libvlc_event_manager_release (internal) :
87 * Init an object's event manager.
88 **************************************************************************/
89 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
91 libvlc_event_listeners_group_t * listeners_group;
92 libvlc_event_listener_t * listener;
94 vlc_mutex_destroy( &p_em->event_sending_lock );
95 vlc_mutex_destroy( &p_em->object_lock );
97 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
98 FOREACH_ARRAY( listener, listeners_group->listeners )
101 ARRAY_RESET( listeners_group->listeners );
102 free( listeners_group );
104 ARRAY_RESET( p_em->listeners_groups );
108 /**************************************************************************
109 * libvlc_event_manager_register_event_type (internal) :
111 * Init an object's event manager.
112 **************************************************************************/
113 void libvlc_event_manager_register_event_type(
114 libvlc_event_manager_t * p_em,
115 libvlc_event_type_t event_type,
116 libvlc_exception_t * p_e )
118 libvlc_event_listeners_group_t * listeners_group;
119 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
120 if( !listeners_group )
122 libvlc_exception_raise( p_e, "No Memory left" );
126 listeners_group->event_type = event_type;
127 ARRAY_INIT( listeners_group->listeners );
129 vlc_mutex_lock( &p_em->object_lock );
130 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
131 vlc_mutex_unlock( &p_em->object_lock );
134 /**************************************************************************
135 * libvlc_event_send (internal) :
138 **************************************************************************/
139 void libvlc_event_send( libvlc_event_manager_t * p_em,
140 libvlc_event_t * p_event )
142 libvlc_event_listeners_group_t * listeners_group;
143 libvlc_event_listener_t * listener_cached;
144 libvlc_event_listener_t * listener;
145 libvlc_event_listener_t * array_listeners_cached = NULL;
146 int i, i_cached_listeners = 0;
148 /* Fill event with the sending object now */
149 p_event->p_obj = p_em->p_obj;
151 /* Here a read/write lock would be nice */
153 vlc_mutex_lock( &p_em->object_lock );
154 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
155 if( listeners_group->event_type == p_event->type )
157 if( listeners_group->listeners.i_size <= 0 )
160 /* Cache a copy of the listener to avoid locking issues */
161 i_cached_listeners = listeners_group->listeners.i_size;
162 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
163 if( !array_listeners_cached )
165 printf( "Can't alloc memory in libvlc_event_send" );
169 listener_cached = array_listeners_cached;
170 FOREACH_ARRAY( listener, listeners_group->listeners )
171 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
178 vlc_mutex_unlock( &p_em->object_lock );
180 /* Here a read/write lock would be *especially* nice,
181 * We would be able to send event without blocking */
182 /* The only reason for this lock is to be able to wait the
183 * End of events sending with libvlc_event_manager_lock_event_sending.
184 * This can be useful to make sure a callback is completely detached. */
185 vlc_mutex_lock( &p_em->event_sending_lock );
186 listener_cached = array_listeners_cached;
187 for( i = 0; i < i_cached_listeners; i++ )
189 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
192 vlc_mutex_unlock( &p_em->event_sending_lock );
194 free( array_listeners_cached );
198 * Public libvlc functions
201 /**************************************************************************
202 * libvlc_event_attach (public) :
204 * Add a callback for an event.
205 **************************************************************************/
206 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
207 libvlc_event_type_t event_type,
208 libvlc_callback_t pf_callback,
210 libvlc_exception_t *p_e )
212 libvlc_event_listeners_group_t * listeners_group;
213 libvlc_event_listener_t * listener;
214 listener = malloc(sizeof(libvlc_event_listener_t));
217 libvlc_exception_raise( p_e, "No Memory left" );
221 listener->event_type = event_type;
222 listener->p_user_data = p_user_data;
223 listener->pf_callback = pf_callback;
225 vlc_mutex_lock( &p_event_manager->object_lock );
226 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
227 if( listeners_group->event_type == listener->event_type )
229 ARRAY_APPEND( listeners_group->listeners, listener );
230 vlc_mutex_unlock( &p_event_manager->object_lock );
234 vlc_mutex_unlock( &p_event_manager->object_lock );
237 libvlc_exception_raise( p_e,
238 "This object event manager doesn't know about '%s' events",
239 libvlc_event_type_name(a));
242 /**************************************************************************
243 * libvlc_event_detach (public) :
245 * Remove a callback for an event.
246 **************************************************************************/
247 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
248 libvlc_event_type_t event_type,
249 libvlc_callback_t pf_callback,
251 libvlc_exception_t *p_e )
253 libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
254 p_user_data, libvlc_UnLocked, p_e );
257 /**************************************************************************
258 * libvlc_event_detach_no_lock (internal) :
260 * Remove a callback for an event.
261 **************************************************************************/
262 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
263 libvlc_event_type_t event_type,
264 libvlc_callback_t pf_callback,
266 libvlc_lock_state_t lockstate,
267 libvlc_exception_t *p_e )
269 libvlc_event_listeners_group_t * listeners_group;
270 libvlc_event_listener_t * listener;
272 if( lockstate == libvlc_UnLocked )
273 vlc_mutex_lock( &p_event_manager->event_sending_lock );
274 vlc_mutex_lock( &p_event_manager->object_lock );
275 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
276 if( listeners_group->event_type == event_type )
278 FOREACH_ARRAY( listener, listeners_group->listeners )
279 if( listener->event_type == event_type &&
280 listener->pf_callback == pf_callback &&
281 listener->p_user_data == p_user_data )
283 /* that's our listener */
285 ARRAY_REMOVE( listeners_group->listeners,
286 fe_idx /* This comes from the macro (and that's why
288 vlc_mutex_unlock( &p_event_manager->object_lock );
289 if( lockstate == libvlc_UnLocked )
290 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
296 vlc_mutex_unlock( &p_event_manager->object_lock );
297 if( lockstate == libvlc_UnLocked )
298 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
300 libvlc_exception_raise( p_e,
301 "This object event manager doesn't know about '%i,%p,%p' event observer",
302 event_type, pf_callback, p_user_data );