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 <vlc/libvlc.h>
27 #include "libvlc_internal.h"
28 #include "event_internal.h"
30 typedef struct libvlc_event_listeners_group_t
32 libvlc_event_type_t event_type;
33 vlc_array_t listeners;
34 bool b_sublistener_removed;
35 } libvlc_event_listeners_group_t;
42 group_contains_listener( libvlc_event_listeners_group_t * group,
43 libvlc_event_listener_t * searched_listener )
46 for( i = 0; i < vlc_array_count(&group->listeners); i++ )
48 if( listeners_are_equal(searched_listener, vlc_array_item_at_index(&group->listeners, i)) )
55 * Internal libvlc functions
58 /**************************************************************************
59 * libvlc_event_manager_new (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" );
78 p_em->async_event_queue = NULL;
79 p_em->p_libvlc_instance = p_libvlc_inst;
81 libvlc_retain( p_libvlc_inst );
82 vlc_array_init( &p_em->listeners_groups );
83 vlc_mutex_init( &p_em->object_lock );
84 vlc_mutex_init_recursive( &p_em->event_sending_lock );
88 /**************************************************************************
89 * libvlc_event_manager_release (internal) :
91 * Release an object's event manager.
92 **************************************************************************/
93 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
95 libvlc_event_listeners_group_t * p_lg;
98 libvlc_event_async_fini(p_em);
100 vlc_mutex_destroy( &p_em->event_sending_lock );
101 vlc_mutex_destroy( &p_em->object_lock );
103 for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
105 p_lg = vlc_array_item_at_index( &p_em->listeners_groups, i );
107 for( j = 0; j < vlc_array_count(&p_lg->listeners); j++)
108 free( vlc_array_item_at_index( &p_lg->listeners, j ) );
110 vlc_array_clear( &p_lg->listeners );
113 vlc_array_clear( &p_em->listeners_groups );
114 libvlc_release( p_em->p_libvlc_instance );
118 /**************************************************************************
119 * libvlc_event_manager_register_event_type (internal) :
121 * Init an object's event manager.
122 **************************************************************************/
123 void libvlc_event_manager_register_event_type(
124 libvlc_event_manager_t * p_em,
125 libvlc_event_type_t event_type,
126 libvlc_exception_t * p_e )
128 libvlc_event_listeners_group_t * listeners_group;
129 listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
130 if( !listeners_group )
132 libvlc_exception_raise( p_e, "No Memory left" );
136 listeners_group->event_type = event_type;
137 vlc_array_init( &listeners_group->listeners );
139 vlc_mutex_lock( &p_em->object_lock );
140 vlc_array_append( &p_em->listeners_groups, listeners_group );
141 vlc_mutex_unlock( &p_em->object_lock );
144 /**************************************************************************
145 * libvlc_event_send (internal) :
148 **************************************************************************/
149 void libvlc_event_send( libvlc_event_manager_t * p_em,
150 libvlc_event_t * p_event )
152 libvlc_event_listeners_group_t * listeners_group = NULL;
153 libvlc_event_listener_t * listener_cached;
154 libvlc_event_listener_t * listener;
155 libvlc_event_listener_t * array_listeners_cached = NULL;
156 int i, i_cached_listeners = 0;
158 /* Fill event with the sending object now */
159 p_event->p_obj = p_em->p_obj;
161 /* Here a read/write lock would be nice */
163 vlc_mutex_lock( &p_em->object_lock );
164 for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
166 listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
167 if( listeners_group->event_type == p_event->type )
169 if( vlc_array_count( &listeners_group->listeners ) <= 0 )
172 /* Cache a copy of the listener to avoid locking issues,
173 * and allow that edition of listeners during callbacks will garantee immediate effect. */
174 i_cached_listeners = vlc_array_count(&listeners_group->listeners);
175 array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
176 if( !array_listeners_cached )
178 vlc_mutex_unlock( &p_em->object_lock );
179 fprintf(stderr, "Can't alloc memory in libvlc_event_send" );
183 listener_cached = array_listeners_cached;
184 for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
186 listener = vlc_array_item_at_index(&listeners_group->listeners, i);
187 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t) );
194 if( !listeners_group )
196 free( array_listeners_cached );
197 vlc_mutex_unlock( &p_em->object_lock );
201 vlc_mutex_unlock( &p_em->object_lock );
203 vlc_mutex_lock( &p_em->event_sending_lock );
204 listener_cached = array_listeners_cached;
205 listeners_group->b_sublistener_removed = false;
206 for( i = 0; i < i_cached_listeners; i++ )
208 if(listener_cached->is_asynchronous)
210 /* The listener wants not to block the emitter during event callback */
211 libvlc_event_async_dispatch(p_em, listener_cached, p_event);
215 /* The listener wants to block the emitter during event callback */
217 listener_cached->pf_callback( p_event, listener_cached->p_user_data );
220 if( listeners_group->b_sublistener_removed )
222 /* If a callback was removed, this gets called */
224 vlc_mutex_lock( &p_em->object_lock );
225 valid_listener = group_contains_listener( listeners_group, listener_cached );
226 vlc_mutex_unlock( &p_em->object_lock );
227 if( !valid_listener )
235 vlc_mutex_unlock( &p_em->event_sending_lock );
237 free( array_listeners_cached );
241 * Public libvlc functions
244 /**************************************************************************
245 * libvlc_event_type_name (public) :
247 * Get the char * name of an event type.
248 **************************************************************************/
249 static const char event_type_to_name[][35] =
251 #define EVENT(a) [a]=#a
252 EVENT(libvlc_MediaMetaChanged),
253 EVENT(libvlc_MediaSubItemAdded),
254 EVENT(libvlc_MediaDurationChanged),
255 EVENT(libvlc_MediaPreparsedChanged),
256 EVENT(libvlc_MediaFreed),
257 EVENT(libvlc_MediaStateChanged),
259 EVENT(libvlc_MediaPlayerNothingSpecial),
260 EVENT(libvlc_MediaPlayerOpening),
261 EVENT(libvlc_MediaPlayerBuffering),
262 EVENT(libvlc_MediaPlayerPlaying),
263 EVENT(libvlc_MediaPlayerPaused),
264 EVENT(libvlc_MediaPlayerStopped),
265 EVENT(libvlc_MediaPlayerForward),
266 EVENT(libvlc_MediaPlayerBackward),
267 EVENT(libvlc_MediaPlayerEndReached),
268 EVENT(libvlc_MediaPlayerTimeChanged),
269 EVENT(libvlc_MediaPlayerTitleChanged),
270 EVENT(libvlc_MediaPlayerPositionChanged),
271 EVENT(libvlc_MediaPlayerSeekableChanged),
272 EVENT(libvlc_MediaPlayerPausableChanged),
274 EVENT(libvlc_MediaListItemAdded),
275 EVENT(libvlc_MediaListWillAddItem),
276 EVENT(libvlc_MediaListItemDeleted),
277 EVENT(libvlc_MediaListWillDeleteItem),
279 EVENT(libvlc_MediaListViewItemAdded),
280 EVENT(libvlc_MediaListViewWillAddItem),
281 EVENT(libvlc_MediaListViewItemDeleted),
282 EVENT(libvlc_MediaListViewWillDeleteItem),
284 EVENT(libvlc_MediaListPlayerPlayed),
285 EVENT(libvlc_MediaListPlayerNextItemSet),
286 EVENT(libvlc_MediaListPlayerStopped),
288 EVENT(libvlc_MediaDiscovererStarted),
289 EVENT(libvlc_MediaDiscovererEnded),
291 EVENT(libvlc_MediaPlayerSnapshotTaken),
295 static const char unkwown_event_name[] = "Unknown Event";
297 const char * libvlc_event_type_name( libvlc_event_type_t event_type )
299 if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
300 return unkwown_event_name;
301 return event_type_to_name[event_type];
304 /**************************************************************************
305 * event_attach (internal) :
307 * Add a callback for an event.
308 **************************************************************************/
310 void event_attach( libvlc_event_manager_t * p_event_manager,
311 libvlc_event_type_t event_type,
312 libvlc_callback_t pf_callback,
314 bool is_asynchronous,
315 libvlc_exception_t *p_e )
317 libvlc_event_listeners_group_t * listeners_group;
318 libvlc_event_listener_t * listener;
321 listener = malloc(sizeof(libvlc_event_listener_t));
324 libvlc_exception_raise( p_e, "No Memory left" );
328 listener->event_type = event_type;
329 listener->p_user_data = p_user_data;
330 listener->pf_callback = pf_callback;
331 listener->is_asynchronous = is_asynchronous;
333 vlc_mutex_lock( &p_event_manager->object_lock );
334 for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
336 listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
337 if( listeners_group->event_type == listener->event_type )
339 vlc_array_append( &listeners_group->listeners, listener );
340 vlc_mutex_unlock( &p_event_manager->object_lock );
344 vlc_mutex_unlock( &p_event_manager->object_lock );
347 libvlc_exception_raise( p_e,
348 "This object event manager doesn't know about '%s' events",
349 libvlc_event_type_name(event_type));
352 /**************************************************************************
353 * libvlc_event_attach (public) :
355 * Add a callback for an event.
356 **************************************************************************/
357 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
358 libvlc_event_type_t event_type,
359 libvlc_callback_t pf_callback,
361 libvlc_exception_t *p_e )
363 event_attach(p_event_manager, event_type, pf_callback, p_user_data, false /* synchronous */, p_e);
366 /**************************************************************************
367 * libvlc_event_attach (public) :
369 * Add a callback for an event.
370 **************************************************************************/
371 void libvlc_event_attach_async( libvlc_event_manager_t * p_event_manager,
372 libvlc_event_type_t event_type,
373 libvlc_callback_t pf_callback,
375 libvlc_exception_t *p_e )
377 event_attach(p_event_manager, event_type, pf_callback, p_user_data, true /* asynchronous */, p_e);
380 /**************************************************************************
381 * libvlc_event_detach (public) :
383 * Remove a callback for an event.
384 **************************************************************************/
385 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
386 libvlc_event_type_t event_type,
387 libvlc_callback_t pf_callback,
389 libvlc_exception_t *p_e )
391 libvlc_event_listeners_group_t * listeners_group;
392 libvlc_event_listener_t * listener;
396 vlc_mutex_lock( &p_event_manager->event_sending_lock );
397 vlc_mutex_lock( &p_event_manager->object_lock );
398 for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
400 listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
401 if( listeners_group->event_type == event_type )
403 for( j = 0; j < vlc_array_count(&listeners_group->listeners); j++)
405 listener = vlc_array_item_at_index(&listeners_group->listeners, j);
406 if( listener->event_type == event_type &&
407 listener->pf_callback == pf_callback &&
408 listener->p_user_data == p_user_data )
410 /* that's our listener */
412 /* Mark this group as edited so that libvlc_event_send
413 * will recheck what listener to call */
414 listeners_group->b_sublistener_removed = false;
417 vlc_array_remove( &listeners_group->listeners, j );
424 vlc_mutex_unlock( &p_event_manager->object_lock );
425 vlc_mutex_unlock( &p_event_manager->event_sending_lock );
427 /* Now make sure any pending async event won't get fired after that point */
428 libvlc_event_listener_t listener_to_remove;
429 listener_to_remove.event_type = event_type;
430 listener_to_remove.pf_callback = pf_callback;
431 listener_to_remove.p_user_data = p_user_data;
432 listener_to_remove.is_asynchronous = true;
434 libvlc_event_async_ensure_listener_removal(p_event_manager, &listener_to_remove);
438 libvlc_exception_raise( p_e,
439 "This object event manager doesn't know about '%s,%p,%p' event observer",
440 libvlc_event_type_name(event_type), pf_callback, p_user_data );