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>
35 * Internal libvlc functions
38 /**************************************************************************
39 * libvlc_event_init (internal) :
41 * initialization function.
42 **************************************************************************/
43 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
45 (void)p_instance;(void)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 )
59 /**************************************************************************
60 * libvlc_event_manager_init (internal) :
62 * Init an object's event manager.
63 **************************************************************************/
64 libvlc_event_manager_t *
65 libvlc_event_manager_new( void * p_obj, libvlc_instance_t * p_libvlc_inst,
66 libvlc_exception_t *p_e )
68 libvlc_event_manager_t * p_em;
70 p_em = malloc(sizeof( libvlc_event_manager_t ));
73 libvlc_exception_raise( p_e, "No Memory left" );
78 p_em->p_libvlc_instance = p_libvlc_inst;
79 libvlc_retain( p_libvlc_inst );
80 vlc_array_init( &p_em->listeners_groups );
81 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
82 vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
86 /**************************************************************************
87 * libvlc_event_manager_release (internal) :
89 * Init an object's event manager.
90 **************************************************************************/
91 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
93 libvlc_event_listeners_group_t * p_lg;
96 vlc_mutex_destroy( &p_em->event_sending_lock );
97 vlc_mutex_destroy( &p_em->object_lock );
99 for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
101 p_lg = vlc_array_item_at_index( &p_em->listeners_groups, i );
103 for( j = 0; j < vlc_array_count(&p_lg->listeners); j++)
104 free( vlc_array_item_at_index( &p_em->listeners_groups, i ) );
106 vlc_array_clear( &p_lg->listeners );
109 vlc_array_clear( &p_em->listeners_groups );
110 libvlc_release( p_em->p_libvlc_instance );
114 /**************************************************************************
115 * libvlc_event_manager_register_event_type (internal) :
117 * Init an object's event manager.
118 **************************************************************************/
119 void libvlc_event_manager_register_event_type(
120 libvlc_event_manager_t * p_em,
121 libvlc_event_type_t event_type,
122 libvlc_exception_t * p_e )
124 libvlc_event_listeners_group_t * listeners_group;
125 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
126 if( !listeners_group )
128 libvlc_exception_raise( p_e, "No Memory left" );
132 listeners_group->event_type = event_type;
133 vlc_array_init( &listeners_group->listeners );
135 vlc_mutex_lock( &p_em->object_lock );
136 vlc_array_append( &p_em->listeners_groups, listeners_group );
137 vlc_mutex_unlock( &p_em->object_lock );
140 /**************************************************************************
141 * libvlc_event_send (internal) :
144 **************************************************************************/
145 void libvlc_event_send( libvlc_event_manager_t * p_em,
146 libvlc_event_t * p_event )
148 libvlc_event_listeners_group_t * listeners_group;
149 libvlc_event_listener_t * listener_cached;
150 libvlc_event_listener_t * listener;
151 libvlc_event_listener_t * array_listeners_cached = NULL;
152 int i, i_cached_listeners = 0;
154 /* Fill event with the sending object now */
155 p_event->p_obj = p_em->p_obj;
157 /* Here a read/write lock would be nice */
159 vlc_mutex_lock( &p_em->object_lock );
160 for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
162 listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
163 if( listeners_group->event_type == p_event->type )
165 if( vlc_array_count( &listeners_group->listeners ) <= 0 )
168 /* Cache a copy of the listener to avoid locking issues */
169 i_cached_listeners = vlc_array_count(&listeners_group->listeners);
170 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
171 if( !array_listeners_cached )
173 printf( "Can't alloc memory in libvlc_event_send" );
177 listener_cached = array_listeners_cached;
178 for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
180 listener = vlc_array_item_at_index(&listeners_group->listeners, i);
181 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t) );
188 vlc_mutex_unlock( &p_em->object_lock );
190 /* Here a read/write lock would be *especially* nice,
191 * We would be able to send event without blocking */
192 /* The only reason for this lock is to be able to wait the
193 * End of events sending with libvlc_event_manager_lock_event_sending.
194 * This can be useful to make sure a callback is completely detached. */
195 vlc_mutex_lock( &p_em->event_sending_lock );
196 listener_cached = array_listeners_cached;
197 for( i = 0; i < i_cached_listeners; i++ )
199 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
202 vlc_mutex_unlock( &p_em->event_sending_lock );
204 free( array_listeners_cached );
208 * Public libvlc functions
211 /**************************************************************************
212 * libvlc_event_type_name (public) :
214 * Get the char * name of an event type.
215 **************************************************************************/
216 static const char * event_type_to_name[] =
218 #define EVENT(a) [a]=#a
219 EVENT(libvlc_MediaDescriptorMetaChanged),
220 EVENT(libvlc_MediaDescriptorSubItemAdded),
221 EVENT(libvlc_MediaDescriptorDurationChanged),
222 EVENT(libvlc_MediaDescriptorPreparsedChanged),
223 EVENT(libvlc_MediaDescriptorFreed),
224 EVENT(libvlc_MediaDescriptorStateChanged),
226 EVENT(libvlc_MediaInstancePlayed),
227 EVENT(libvlc_MediaInstancePaused),
228 EVENT(libvlc_MediaInstanceReachedEnd),
229 EVENT(libvlc_MediaInstanceTimeChanged),
230 EVENT(libvlc_MediaInstancePositionChanged),
232 EVENT(libvlc_MediaListItemAdded),
233 EVENT(libvlc_MediaListWillAddItem),
234 EVENT(libvlc_MediaListItemDeleted),
235 EVENT(libvlc_MediaListWillDeleteItem),
237 EVENT(libvlc_MediaListViewItemAdded),
238 EVENT(libvlc_MediaListViewWillAddItem),
239 EVENT(libvlc_MediaListViewItemDeleted),
240 EVENT(libvlc_MediaListViewWillDeleteItem),
242 EVENT(libvlc_MediaListPlayerPlayed),
243 EVENT(libvlc_MediaListPlayerNextItemSet),
244 EVENT(libvlc_MediaListPlayerStopped),
246 EVENT(libvlc_MediaDiscovererStarted),
247 EVENT(libvlc_MediaDiscovererEnded)
250 static const char * unkwown_event_name = "Unknown Event";
252 const char * libvlc_event_type_name( libvlc_event_type_t event_type )
254 if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
255 return unkwown_event_name;
256 return event_type_to_name[event_type];
259 /**************************************************************************
260 * libvlc_event_attach (public) :
262 * Add a callback for an event.
263 **************************************************************************/
264 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
265 libvlc_event_type_t event_type,
266 libvlc_callback_t pf_callback,
268 libvlc_exception_t *p_e )
270 libvlc_event_listeners_group_t * listeners_group;
271 libvlc_event_listener_t * listener;
274 listener = malloc(sizeof(libvlc_event_listener_t));
277 libvlc_exception_raise( p_e, "No Memory left" );
281 listener->event_type = event_type;
282 listener->p_user_data = p_user_data;
283 listener->pf_callback = pf_callback;
285 vlc_mutex_lock( &p_event_manager->object_lock );
286 for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
288 listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
289 if( listeners_group->event_type == listener->event_type )
291 vlc_array_append( &listeners_group->listeners, listener );
292 vlc_mutex_unlock( &p_event_manager->object_lock );
296 vlc_mutex_unlock( &p_event_manager->object_lock );
299 libvlc_exception_raise( p_e,
300 "This object event manager doesn't know about '%s' events",
301 libvlc_event_type_name(event_type));
304 /**************************************************************************
305 * libvlc_event_detach (public) :
307 * Remove a callback for an event.
308 **************************************************************************/
309 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
310 libvlc_event_type_t event_type,
311 libvlc_callback_t pf_callback,
313 libvlc_exception_t *p_e )
315 libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
316 p_user_data, libvlc_UnLocked, p_e );
319 /**************************************************************************
320 * libvlc_event_detach_no_lock (internal) :
322 * Remove a callback for an event.
323 **************************************************************************/
324 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
325 libvlc_event_type_t event_type,
326 libvlc_callback_t pf_callback,
328 libvlc_lock_state_t lockstate,
329 libvlc_exception_t *p_e )
331 libvlc_event_listeners_group_t * listeners_group;
332 libvlc_event_listener_t * listener;
335 if( lockstate == libvlc_UnLocked )
336 vlc_mutex_lock( &p_event_manager->event_sending_lock );
337 vlc_mutex_lock( &p_event_manager->object_lock );
338 for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
340 listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
341 if( listeners_group->event_type == event_type )
343 for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
345 listener = vlc_array_item_at_index(&listeners_group->listeners, i);
346 if( listener->event_type == event_type &&
347 listener->pf_callback == pf_callback &&
348 listener->p_user_data == p_user_data )
350 /* that's our listener */
352 vlc_array_remove( &listeners_group->listeners, i );
353 vlc_mutex_unlock( &p_event_manager->object_lock );
354 if( lockstate == libvlc_UnLocked )
355 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
361 vlc_mutex_unlock( &p_event_manager->object_lock );
362 if( lockstate == libvlc_UnLocked )
363 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
365 libvlc_exception_raise( p_e,
366 "This object event manager doesn't know about '%s,%p,%p' event observer",
367 libvlc_event_type_name(event_type), pf_callback, p_user_data );