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 );
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->object_lock );
96 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
97 FOREACH_ARRAY( listener, listeners_group->listeners )
100 free( listeners_group );
105 /**************************************************************************
106 * libvlc_event_manager_register_event_type (internal) :
108 * Init an object's event manager.
109 **************************************************************************/
110 void libvlc_event_manager_register_event_type(
111 libvlc_event_manager_t * p_em,
112 libvlc_event_type_t event_type,
113 libvlc_exception_t * p_e )
115 libvlc_event_listeners_group_t * listeners_group;
116 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
117 if( !listeners_group )
119 libvlc_exception_raise( p_e, "No Memory left" );
123 listeners_group->event_type = event_type;
124 ARRAY_INIT( listeners_group->listeners );
126 vlc_mutex_lock( &p_em->object_lock );
127 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
128 vlc_mutex_unlock( &p_em->object_lock );
131 /**************************************************************************
132 * libvlc_event_send (internal) :
135 **************************************************************************/
136 void libvlc_event_send( libvlc_event_manager_t * p_em,
137 libvlc_event_t * p_event )
139 libvlc_event_listeners_group_t * listeners_group;
140 libvlc_event_listener_t * listener_cached;
141 libvlc_event_listener_t * listener;
142 libvlc_event_listener_t * array_listeners_cached = NULL;
143 int i, i_cached_listeners = 0;
145 /* Fill event with the sending object now */
146 p_event->p_obj = p_em->p_obj;
148 vlc_mutex_lock( &p_em->object_lock );
149 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
150 if( listeners_group->event_type == p_event->type )
152 if( listeners_group->listeners.i_size <= 0 )
155 /* Cache a copy of the listener to avoid locking issues */
156 i_cached_listeners = listeners_group->listeners.i_size;
157 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
158 if( !array_listeners_cached )
160 printf( "Can't alloc memory in libvlc_event_send" );
164 listener_cached = array_listeners_cached;
165 FOREACH_ARRAY( listener, listeners_group->listeners )
166 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
167 listener_cached += sizeof(libvlc_event_listener_t) ;
172 vlc_mutex_unlock( &p_em->object_lock );
174 listener_cached = array_listeners_cached;
175 for( i = 0; i < i_cached_listeners; i++ )
177 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
178 listener_cached += sizeof(libvlc_event_listener_t) ;
180 free( array_listeners_cached );
184 * Public libvlc functions
187 /**************************************************************************
188 * libvlc_event_attach (public) :
190 * Add a callback for an event.
191 **************************************************************************/
192 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
193 libvlc_event_type_t event_type,
194 libvlc_callback_t pf_callback,
196 libvlc_exception_t *p_e )
198 libvlc_event_listeners_group_t * listeners_group;
199 libvlc_event_listener_t * listener;
200 listener = malloc(sizeof(libvlc_event_listener_t));
203 libvlc_exception_raise( p_e, "No Memory left" );
207 listener->event_type = event_type;
208 listener->p_user_data = p_user_data;
209 listener->pf_callback = pf_callback;
211 vlc_mutex_lock( &p_event_manager->object_lock );
212 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
213 if( listeners_group->event_type == listener->event_type )
215 ARRAY_APPEND( listeners_group->listeners, listener );
216 vlc_mutex_unlock( &p_event_manager->object_lock );
220 vlc_mutex_unlock( &p_event_manager->object_lock );
223 libvlc_exception_raise( p_e,
224 "This object event manager doesn't know about '%s' events",
225 libvlc_event_type_name(a));
228 /**************************************************************************
229 * libvlc_event_detach (public) :
231 * Remove a callback for an event.
232 **************************************************************************/
233 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
234 libvlc_event_type_t event_type,
235 libvlc_callback_t pf_callback,
237 libvlc_exception_t *p_e )
239 libvlc_event_listeners_group_t * listeners_group;
240 libvlc_event_listener_t * listener;
242 vlc_mutex_lock( &p_event_manager->object_lock );
243 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
244 if( listeners_group->event_type == event_type )
246 FOREACH_ARRAY( listener, listeners_group->listeners )
247 if( listener->event_type == event_type &&
248 listener->pf_callback == pf_callback &&
249 listener->p_user_data == p_user_data )
251 /* that's our listener */
253 ARRAY_REMOVE( listeners_group->listeners,
254 fe_idx /* This comes from the macro (and that's why
256 vlc_mutex_unlock( &p_event_manager->object_lock );
262 vlc_mutex_unlock( &p_event_manager->object_lock );
264 libvlc_exception_raise( p_e,
265 "This object event manager doesn't know about '%i,%p,%p' event observer",
266 event_type, pf_callback, p_user_data );