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 )
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_type_name (public) :
207 * Get the char * name of an event type.
208 **************************************************************************/
209 static const char * event_type_to_name[] =
211 #define EVENT(a) [a]=#a
212 EVENT(libvlc_MediaDescriptorMetaChanged),
213 EVENT(libvlc_MediaDescriptorSubItemAdded),
214 EVENT(libvlc_MediaDescriptorDurationChanged),
215 EVENT(libvlc_MediaDescriptorPreparsedChanged),
216 EVENT(libvlc_MediaDescriptorFreed),
218 EVENT(libvlc_MediaInstancePlayed),
219 EVENT(libvlc_MediaInstancePaused),
220 EVENT(libvlc_MediaInstanceReachedEnd),
221 EVENT(libvlc_MediaInstancePositionChanged),
223 EVENT(libvlc_MediaListItemAdded),
224 EVENT(libvlc_MediaListWillAddItem),
225 EVENT(libvlc_MediaListItemDeleted),
226 EVENT(libvlc_MediaListWillDeleteItem),
228 EVENT(libvlc_MediaListViewItemAdded),
229 EVENT(libvlc_MediaListViewWillAddItem),
230 EVENT(libvlc_MediaListViewItemDeleted),
231 EVENT(libvlc_MediaListViewWillDeleteItem),
233 EVENT(libvlc_MediaListPlayerPlayed),
234 EVENT(libvlc_MediaListPlayerNextItemSet),
235 EVENT(libvlc_MediaListPlayerStopped)
238 static const char * unkwown_event_name = "Unknown Event";
240 const char * libvlc_event_type_name( libvlc_event_type_t event_type )
242 if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
243 return unkwown_event_name;
244 return strdup(event_type_to_name[event_type]);
247 /**************************************************************************
248 * libvlc_event_attach (public) :
250 * Add a callback for an event.
251 **************************************************************************/
252 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
253 libvlc_event_type_t event_type,
254 libvlc_callback_t pf_callback,
256 libvlc_exception_t *p_e )
258 libvlc_event_listeners_group_t * listeners_group;
259 libvlc_event_listener_t * listener;
260 listener = malloc(sizeof(libvlc_event_listener_t));
263 libvlc_exception_raise( p_e, "No Memory left" );
267 listener->event_type = event_type;
268 listener->p_user_data = p_user_data;
269 listener->pf_callback = pf_callback;
271 vlc_mutex_lock( &p_event_manager->object_lock );
272 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
273 if( listeners_group->event_type == listener->event_type )
275 ARRAY_APPEND( listeners_group->listeners, listener );
276 vlc_mutex_unlock( &p_event_manager->object_lock );
280 vlc_mutex_unlock( &p_event_manager->object_lock );
283 libvlc_exception_raise( p_e,
284 "This object event manager doesn't know about '%s' events",
285 libvlc_event_type_name(event_type));
288 /**************************************************************************
289 * libvlc_event_detach (public) :
291 * Remove a callback for an event.
292 **************************************************************************/
293 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
294 libvlc_event_type_t event_type,
295 libvlc_callback_t pf_callback,
297 libvlc_exception_t *p_e )
299 libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
300 p_user_data, libvlc_UnLocked, p_e );
303 /**************************************************************************
304 * libvlc_event_detach_no_lock (internal) :
306 * Remove a callback for an event.
307 **************************************************************************/
308 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
309 libvlc_event_type_t event_type,
310 libvlc_callback_t pf_callback,
312 libvlc_lock_state_t lockstate,
313 libvlc_exception_t *p_e )
315 libvlc_event_listeners_group_t * listeners_group;
316 libvlc_event_listener_t * listener;
318 if( lockstate == libvlc_UnLocked )
319 vlc_mutex_lock( &p_event_manager->event_sending_lock );
320 vlc_mutex_lock( &p_event_manager->object_lock );
321 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
322 if( listeners_group->event_type == event_type )
324 FOREACH_ARRAY( listener, listeners_group->listeners )
325 if( listener->event_type == event_type &&
326 listener->pf_callback == pf_callback &&
327 listener->p_user_data == p_user_data )
329 /* that's our listener */
331 ARRAY_REMOVE( listeners_group->listeners,
332 fe_idx /* This comes from the macro (and that's why
334 vlc_mutex_unlock( &p_event_manager->object_lock );
335 if( lockstate == libvlc_UnLocked )
336 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
342 vlc_mutex_unlock( &p_event_manager->object_lock );
343 if( lockstate == libvlc_UnLocked )
344 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
346 libvlc_exception_raise( p_e,
347 "This object event manager doesn't know about '%i,%p,%p' event observer",
348 event_type, pf_callback, p_user_data );