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 ARRAY_RESET( listeners_group->listeners );
101 free( listeners_group );
103 ARRAY_RESET( p_em->listeners_groups );
107 /**************************************************************************
108 * libvlc_event_manager_register_event_type (internal) :
110 * Init an object's event manager.
111 **************************************************************************/
112 void libvlc_event_manager_register_event_type(
113 libvlc_event_manager_t * p_em,
114 libvlc_event_type_t event_type,
115 libvlc_exception_t * p_e )
117 libvlc_event_listeners_group_t * listeners_group;
118 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
119 if( !listeners_group )
121 libvlc_exception_raise( p_e, "No Memory left" );
125 listeners_group->event_type = event_type;
126 ARRAY_INIT( listeners_group->listeners );
128 vlc_mutex_lock( &p_em->object_lock );
129 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
130 vlc_mutex_unlock( &p_em->object_lock );
133 /**************************************************************************
134 * libvlc_event_send (internal) :
137 **************************************************************************/
138 void libvlc_event_send( libvlc_event_manager_t * p_em,
139 libvlc_event_t * p_event )
141 libvlc_event_listeners_group_t * listeners_group;
142 libvlc_event_listener_t * listener_cached;
143 libvlc_event_listener_t * listener;
144 libvlc_event_listener_t * array_listeners_cached = NULL;
145 int i, i_cached_listeners = 0;
147 /* Fill event with the sending object now */
148 p_event->p_obj = p_em->p_obj;
150 vlc_mutex_lock( &p_em->object_lock );
151 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
152 if( listeners_group->event_type == p_event->type )
154 if( listeners_group->listeners.i_size <= 0 )
157 /* Cache a copy of the listener to avoid locking issues */
158 i_cached_listeners = listeners_group->listeners.i_size;
159 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
160 if( !array_listeners_cached )
162 printf( "Can't alloc memory in libvlc_event_send" );
166 listener_cached = array_listeners_cached;
167 FOREACH_ARRAY( listener, listeners_group->listeners )
168 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
174 vlc_mutex_unlock( &p_em->object_lock );
176 listener_cached = array_listeners_cached;
177 for( i = 0; i < i_cached_listeners; i++ )
179 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
182 free( array_listeners_cached );
186 * Public libvlc functions
189 /**************************************************************************
190 * libvlc_event_attach (public) :
192 * Add a callback for an event.
193 **************************************************************************/
194 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
195 libvlc_event_type_t event_type,
196 libvlc_callback_t pf_callback,
198 libvlc_exception_t *p_e )
200 libvlc_event_listeners_group_t * listeners_group;
201 libvlc_event_listener_t * listener;
202 listener = malloc(sizeof(libvlc_event_listener_t));
205 libvlc_exception_raise( p_e, "No Memory left" );
209 listener->event_type = event_type;
210 listener->p_user_data = p_user_data;
211 listener->pf_callback = pf_callback;
213 vlc_mutex_lock( &p_event_manager->object_lock );
214 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
215 if( listeners_group->event_type == listener->event_type )
217 ARRAY_APPEND( listeners_group->listeners, listener );
218 vlc_mutex_unlock( &p_event_manager->object_lock );
222 vlc_mutex_unlock( &p_event_manager->object_lock );
225 libvlc_exception_raise( p_e,
226 "This object event manager doesn't know about '%s' events",
227 libvlc_event_type_name(a));
230 /**************************************************************************
231 * libvlc_event_detach (public) :
233 * Remove a callback for an event.
234 **************************************************************************/
235 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
236 libvlc_event_type_t event_type,
237 libvlc_callback_t pf_callback,
239 libvlc_exception_t *p_e )
241 libvlc_event_listeners_group_t * listeners_group;
242 libvlc_event_listener_t * listener;
244 vlc_mutex_lock( &p_event_manager->object_lock );
245 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
246 if( listeners_group->event_type == event_type )
248 FOREACH_ARRAY( listener, listeners_group->listeners )
249 if( listener->event_type == event_type &&
250 listener->pf_callback == pf_callback &&
251 listener->p_user_data == p_user_data )
253 /* that's our listener */
255 ARRAY_REMOVE( listeners_group->listeners,
256 fe_idx /* This comes from the macro (and that's why
258 vlc_mutex_unlock( &p_event_manager->object_lock );
264 vlc_mutex_unlock( &p_event_manager->object_lock );
266 libvlc_exception_raise( p_e,
267 "This object event manager doesn't know about '%i,%p,%p' event observer",
268 event_type, pf_callback, p_user_data );