]> git.sesse.net Git - vlc/blob - src/control/event.c
update module LIST file.
[vlc] / src / control / event.c
1 /*****************************************************************************
2  * event.c: New libvlc event control API
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id $
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_playlist.h>
28
29
30 /*
31  * Private functions
32  */
33
34 static vlc_bool_t
35 listeners_are_equal( libvlc_event_listener_t * listener1,
36                      libvlc_event_listener_t * listener2 )
37 {
38     return listener1->event_type  == listener2->event_type &&
39            listener1->pf_callback == listener2->pf_callback &&
40            listener1->p_user_data == listener2->p_user_data;
41 }
42
43 static vlc_bool_t
44 group_contains_listener( libvlc_event_listeners_group_t * group,
45                          libvlc_event_listener_t * searched_listener )
46 {
47     int i;
48     for( i = 0; i < vlc_array_count(&group->listeners); i++ )
49     {
50         if( listeners_are_equal(searched_listener, vlc_array_item_at_index(&group->listeners, i)) )
51             return VLC_TRUE;
52     }
53     return VLC_FALSE;
54 }
55
56 /*
57  * Internal libvlc functions
58  */
59
60 /**************************************************************************
61  *       libvlc_event_init (internal) :
62  *
63  * initialization function.
64  **************************************************************************/
65 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
66 {
67     (void)p_instance;(void)p_e;
68     /* Will certainly be used to install libvlc_instance event */
69 }
70
71 /**************************************************************************
72  *       libvlc_event_fini (internal) :
73  *
74  * finalization function.
75  **************************************************************************/
76 void libvlc_event_fini( libvlc_instance_t *p_instance )
77 {
78     (void)p_instance;
79 }
80
81 /**************************************************************************
82  *       libvlc_event_manager_init (internal) :
83  *
84  * Init an object's event manager.
85  **************************************************************************/
86 libvlc_event_manager_t *
87 libvlc_event_manager_new( void * p_obj, libvlc_instance_t * p_libvlc_inst,
88                            libvlc_exception_t *p_e )
89 {
90     libvlc_event_manager_t * p_em;
91
92     p_em = malloc(sizeof( libvlc_event_manager_t ));
93     if( !p_em )
94     {
95         libvlc_exception_raise( p_e, "No Memory left" );
96         return NULL;
97     }
98
99     p_em->p_obj = p_obj;
100     p_em->p_libvlc_instance = p_libvlc_inst;
101     libvlc_retain( p_libvlc_inst );
102     vlc_array_init( &p_em->listeners_groups );
103     vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
104     vlc_mutex_init_recursive( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
105     return p_em;
106 }
107
108 /**************************************************************************
109  *       libvlc_event_manager_release (internal) :
110  *
111  * Init an object's event manager.
112  **************************************************************************/
113 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
114 {
115     libvlc_event_listeners_group_t * p_lg;
116     int i,j ;
117
118     vlc_mutex_destroy( &p_em->event_sending_lock );
119     vlc_mutex_destroy( &p_em->object_lock );
120
121     for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
122     {
123         p_lg = vlc_array_item_at_index( &p_em->listeners_groups, i );
124
125         for( j = 0; j < vlc_array_count(&p_lg->listeners); j++)
126             free( vlc_array_item_at_index( &p_em->listeners_groups, i ) );
127
128         vlc_array_clear( &p_lg->listeners );
129         free( p_lg );
130     }
131     vlc_array_clear( &p_em->listeners_groups );
132     libvlc_release( p_em->p_libvlc_instance );
133     free( p_em );
134 }
135
136 /**************************************************************************
137  *       libvlc_event_manager_register_event_type (internal) :
138  *
139  * Init an object's event manager.
140  **************************************************************************/
141 void libvlc_event_manager_register_event_type(
142         libvlc_event_manager_t * p_em,
143         libvlc_event_type_t event_type,
144         libvlc_exception_t * p_e )
145 {
146     libvlc_event_listeners_group_t * listeners_group;
147     listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
148     if( !listeners_group )
149     {
150         libvlc_exception_raise( p_e, "No Memory left" );
151         return;
152     }
153
154     listeners_group->event_type = event_type;
155     vlc_array_init( &listeners_group->listeners );
156
157     vlc_mutex_lock( &p_em->object_lock );
158     vlc_array_append( &p_em->listeners_groups, listeners_group );
159     vlc_mutex_unlock( &p_em->object_lock );
160 }
161
162 /**************************************************************************
163  *       libvlc_event_send (internal) :
164  *
165  * Send a callback.
166  **************************************************************************/
167 void libvlc_event_send( libvlc_event_manager_t * p_em,
168                         libvlc_event_t * p_event )
169 {
170     libvlc_event_listeners_group_t * listeners_group = NULL;
171     libvlc_event_listener_t * listener_cached;
172     libvlc_event_listener_t * listener;
173     libvlc_event_listener_t * array_listeners_cached = NULL;
174     int i, i_cached_listeners = 0;
175
176     /* Fill event with the sending object now */
177     p_event->p_obj = p_em->p_obj;
178
179     /* Here a read/write lock would be nice */
180
181     vlc_mutex_lock( &p_em->object_lock );
182     for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
183     {
184         listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
185         if( listeners_group->event_type == p_event->type )
186         {
187             if( vlc_array_count( &listeners_group->listeners ) <= 0 )
188                 break;
189
190             /* Cache a copy of the listener to avoid locking issues */
191             i_cached_listeners = vlc_array_count(&listeners_group->listeners);
192             array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
193             if( !array_listeners_cached )
194             {
195                 printf( "Can't alloc memory in libvlc_event_send" );
196                 break;
197             }
198
199             listener_cached = array_listeners_cached;
200             for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
201             {
202                 listener = vlc_array_item_at_index(&listeners_group->listeners, i);
203                 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t) );
204                 listener_cached++;
205             }
206             break;
207         }
208     }
209
210     if( !listeners_group )
211     {
212         free( array_listeners_cached );
213         return;
214     }
215
216     vlc_mutex_unlock( &p_em->object_lock );
217
218     vlc_mutex_lock( &p_em->event_sending_lock );
219     listener_cached = array_listeners_cached;
220     listeners_group->b_sublistener_removed = VLC_FALSE;
221     for( i = 0; i < i_cached_listeners; i++ )
222     {
223         if( listeners_group->b_sublistener_removed )
224         {
225             /* If a callback was removed, this gets called */
226             vlc_bool_t valid_listener;
227             vlc_mutex_lock( &p_em->object_lock );
228             valid_listener = group_contains_listener( listeners_group, listener_cached );
229             vlc_mutex_unlock( &p_em->object_lock );
230             if( !valid_listener )
231             {
232                 listener_cached++;
233                 continue;
234             }
235         }
236         
237         listener_cached->pf_callback( p_event, listener_cached->p_user_data );
238         listener_cached++;
239     }
240     vlc_mutex_unlock( &p_em->event_sending_lock );
241
242     free( array_listeners_cached );
243 }
244
245 /*
246  * Public libvlc functions
247  */
248
249 /**************************************************************************
250  *       libvlc_event_type_name (public) :
251  *
252  * Get the char * name of an event type.
253  **************************************************************************/
254 static const char * event_type_to_name[] =
255 {
256 #define EVENT(a) [a]=#a
257     EVENT(libvlc_MediaMetaChanged),
258     EVENT(libvlc_MediaSubItemAdded),
259     EVENT(libvlc_MediaDurationChanged),
260     EVENT(libvlc_MediaPreparsedChanged),
261     EVENT(libvlc_MediaFreed),
262     EVENT(libvlc_MediaStateChanged),
263
264     EVENT(libvlc_MediaPlayerPlayed),
265     EVENT(libvlc_MediaPlayerPaused),
266     EVENT(libvlc_MediaPlayerEndReached),
267     EVENT(libvlc_MediaPlayerStopped),
268     EVENT(libvlc_MediaPlayerTimeChanged),
269     EVENT(libvlc_MediaPlayerPositionChanged),
270
271     EVENT(libvlc_MediaListItemAdded),
272     EVENT(libvlc_MediaListWillAddItem),
273     EVENT(libvlc_MediaListItemDeleted),
274     EVENT(libvlc_MediaListWillDeleteItem),
275
276     EVENT(libvlc_MediaListViewItemAdded),
277     EVENT(libvlc_MediaListViewWillAddItem),
278     EVENT(libvlc_MediaListViewItemDeleted),
279     EVENT(libvlc_MediaListViewWillDeleteItem),
280
281     EVENT(libvlc_MediaListPlayerPlayed),
282     EVENT(libvlc_MediaListPlayerNextItemSet),
283     EVENT(libvlc_MediaListPlayerStopped),
284
285     EVENT(libvlc_MediaDiscovererStarted),
286     EVENT(libvlc_MediaDiscovererEnded)
287 #undef EVENT
288 };
289 static const char * unkwown_event_name = "Unknown Event";
290
291 const char * libvlc_event_type_name( libvlc_event_type_t event_type )
292 {
293     if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
294         return unkwown_event_name;
295     return event_type_to_name[event_type];
296 }
297
298 /**************************************************************************
299  *       libvlc_event_attach (public) :
300  *
301  * Add a callback for an event.
302  **************************************************************************/
303 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
304                           libvlc_event_type_t event_type,
305                           libvlc_callback_t pf_callback,
306                           void *p_user_data,
307                           libvlc_exception_t *p_e )
308 {
309     libvlc_event_listeners_group_t * listeners_group;
310     libvlc_event_listener_t * listener;
311     int i;
312
313     listener = malloc(sizeof(libvlc_event_listener_t));
314     if( !listener )
315     {
316         libvlc_exception_raise( p_e, "No Memory left" );
317         return;
318     }
319  
320     listener->event_type = event_type;
321     listener->p_user_data = p_user_data;
322     listener->pf_callback = pf_callback;
323
324     vlc_mutex_lock( &p_event_manager->object_lock );
325     for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
326     {
327         listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
328         if( listeners_group->event_type == listener->event_type )
329         {
330             vlc_array_append( &listeners_group->listeners, listener );
331             vlc_mutex_unlock( &p_event_manager->object_lock );
332             return;
333         }
334     }
335     vlc_mutex_unlock( &p_event_manager->object_lock );
336
337     free(listener);
338     libvlc_exception_raise( p_e,
339             "This object event manager doesn't know about '%s' events",
340             libvlc_event_type_name(event_type));
341 }
342
343 /**************************************************************************
344  *       libvlc_event_detach (public) :
345  *
346  * Remove a callback for an event.
347  **************************************************************************/
348 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
349                                      libvlc_event_type_t event_type,
350                                      libvlc_callback_t pf_callback,
351                                      void *p_user_data,
352                                      libvlc_exception_t *p_e )
353 {
354     libvlc_event_listeners_group_t * listeners_group;
355     libvlc_event_listener_t * listener;
356     int i, j;
357
358     vlc_mutex_lock( &p_event_manager->event_sending_lock );
359     vlc_mutex_lock( &p_event_manager->object_lock );
360     for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
361     {
362         listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
363         if( listeners_group->event_type == event_type )
364         {
365             for( j = 0; j < vlc_array_count(&listeners_group->listeners); j++)
366             {
367                 listener = vlc_array_item_at_index(&listeners_group->listeners, j);
368                 if( listener->event_type == event_type &&
369                     listener->pf_callback == pf_callback &&
370                     listener->p_user_data == p_user_data )
371                 {
372                     /* that's our listener */
373                     
374                     /* Mark this group as edited so that libvlc_event_send
375                      * will recheck what listener to call */
376                     listeners_group->b_sublistener_removed = VLC_FALSE;
377
378                     free( listener );
379                     vlc_array_remove( &listeners_group->listeners, j );
380                     vlc_mutex_unlock( &p_event_manager->object_lock );
381                     vlc_mutex_unlock( &p_event_manager->event_sending_lock );
382                     return;
383                 }
384             }
385         }
386     }
387     vlc_mutex_unlock( &p_event_manager->object_lock );
388     vlc_mutex_unlock( &p_event_manager->event_sending_lock );
389
390     libvlc_exception_raise( p_e,
391             "This object event manager doesn't know about '%s,%p,%p' event observer",
392             libvlc_event_type_name(event_type), pf_callback, p_user_data );
393 }