]> git.sesse.net Git - vlc/blob - src/control/event.c
control: Remove libvlc_event_fini exception handling, as we don't need it.
[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
35 /*
36  * Internal libvlc functions
37  */
38
39 /**************************************************************************
40  *       libvlc_event_init (internal) :
41  *
42  * initialization function.
43  **************************************************************************/
44 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
45 {
46     /* Will certainly be used to install libvlc_instance event */
47 }
48
49 /**************************************************************************
50  *       libvlc_event_fini (internal) :
51  *
52  * finalization function.
53  **************************************************************************/
54 void libvlc_event_fini( libvlc_instance_t *p_instance )
55 {
56 }
57
58 /**************************************************************************
59  *       libvlc_event_manager_init (internal) :
60  *
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 )
66 {
67     libvlc_event_manager_t * p_em;
68
69     p_em = malloc(sizeof( libvlc_event_manager_t ));
70     if( !p_em )
71     {
72         libvlc_exception_raise( p_e, "No Memory left" );
73         return NULL;
74     }
75
76     p_em->p_obj = p_obj;
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 );
82     return p_em;
83 }
84
85 /**************************************************************************
86  *       libvlc_event_manager_release (internal) :
87  *
88  * Init an object's event manager.
89  **************************************************************************/
90 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
91 {
92     libvlc_event_listeners_group_t * listeners_group;
93     libvlc_event_listener_t * listener;
94
95     vlc_mutex_destroy( &p_em->event_sending_lock );
96     vlc_mutex_destroy( &p_em->object_lock );
97
98     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
99         FOREACH_ARRAY( listener, listeners_group->listeners )
100             free( listener );
101         FOREACH_END()
102         ARRAY_RESET( listeners_group->listeners );
103         free( listeners_group );
104     FOREACH_END()
105     ARRAY_RESET( p_em->listeners_groups );
106
107     libvlc_release( p_em->p_libvlc_instance, NULL );
108     free( p_em );
109 }
110
111 /**************************************************************************
112  *       libvlc_event_manager_register_event_type (internal) :
113  *
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 )
120 {
121     libvlc_event_listeners_group_t * listeners_group;
122     listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
123     if( !listeners_group )
124     {
125         libvlc_exception_raise( p_e, "No Memory left" );
126         return;
127     }
128
129     listeners_group->event_type = event_type;
130     ARRAY_INIT( listeners_group->listeners );
131
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 );
135 }
136
137 /**************************************************************************
138  *       libvlc_event_send (internal) :
139  *
140  * Send a callback.
141  **************************************************************************/
142 void libvlc_event_send( libvlc_event_manager_t * p_em,
143                         libvlc_event_t * p_event )
144 {
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;
150
151     /* Fill event with the sending object now */
152     p_event->p_obj = p_em->p_obj;
153
154     /* Here a read/write lock would be nice */
155
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 )
159         {
160             if( listeners_group->listeners.i_size <= 0 )
161                 break;
162
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 )
167             {
168                 printf( "Can't alloc memory in libvlc_event_send" );
169                 break;
170             }
171
172             listener_cached = array_listeners_cached;
173             FOREACH_ARRAY( listener, listeners_group->listeners )
174                 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
175                 listener_cached++;
176             FOREACH_END()
177             break;
178         }
179     FOREACH_END()
180
181     vlc_mutex_unlock( &p_em->object_lock );
182
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++ )
191     {
192         listener_cached->pf_callback( p_event, listener_cached->p_user_data );
193         listener_cached++;
194     }
195     vlc_mutex_unlock( &p_em->event_sending_lock );
196
197     free( array_listeners_cached );
198 }
199
200 /*
201  * Public libvlc functions
202  */
203
204 /**************************************************************************
205  *       libvlc_event_attach (public) :
206  *
207  * Add a callback for an event.
208  **************************************************************************/
209 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
210                           libvlc_event_type_t event_type,
211                           libvlc_callback_t pf_callback,
212                           void *p_user_data,
213                           libvlc_exception_t *p_e )
214 {
215     libvlc_event_listeners_group_t * listeners_group;
216     libvlc_event_listener_t * listener;
217     listener = malloc(sizeof(libvlc_event_listener_t));
218     if( !listener )
219     {
220         libvlc_exception_raise( p_e, "No Memory left" );
221         return;
222     }
223  
224     listener->event_type = event_type;
225     listener->p_user_data = p_user_data;
226     listener->pf_callback = pf_callback;
227
228     vlc_mutex_lock( &p_event_manager->object_lock );
229     FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
230         if( listeners_group->event_type == listener->event_type )
231         {
232             ARRAY_APPEND( listeners_group->listeners, listener );
233             vlc_mutex_unlock( &p_event_manager->object_lock );
234             return;
235         }
236     FOREACH_END()
237     vlc_mutex_unlock( &p_event_manager->object_lock );
238
239     free(listener);
240     libvlc_exception_raise( p_e,
241             "This object event manager doesn't know about '%s' events",
242             libvlc_event_type_name(a));
243 }
244
245 /**************************************************************************
246  *       libvlc_event_detach (public) :
247  *
248  * Remove a callback for an event.
249  **************************************************************************/
250 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
251                           libvlc_event_type_t event_type,
252                           libvlc_callback_t pf_callback,
253                           void *p_user_data,
254                           libvlc_exception_t *p_e )
255 {
256     libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
257                                     p_user_data, libvlc_UnLocked, p_e );
258 }
259
260 /**************************************************************************
261  *       libvlc_event_detach_no_lock (internal) :
262  *
263  * Remove a callback for an event.
264  **************************************************************************/
265 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
266                                      libvlc_event_type_t event_type,
267                                      libvlc_callback_t pf_callback,
268                                      void *p_user_data,
269                                      libvlc_lock_state_t lockstate,
270                                      libvlc_exception_t *p_e )
271 {
272     libvlc_event_listeners_group_t * listeners_group;
273     libvlc_event_listener_t * listener;
274
275     if( lockstate == libvlc_UnLocked )
276         vlc_mutex_lock( &p_event_manager->event_sending_lock );
277     vlc_mutex_lock( &p_event_manager->object_lock );
278     FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
279         if( listeners_group->event_type == event_type )
280         {
281             FOREACH_ARRAY( listener, listeners_group->listeners )
282                 if( listener->event_type == event_type &&
283                     listener->pf_callback == pf_callback &&
284                     listener->p_user_data == p_user_data )
285                 {
286                     /* that's our listener */
287                     free( listener );
288                     ARRAY_REMOVE( listeners_group->listeners,
289                         fe_idx /* This comes from the macro (and that's why
290                                   I hate macro) */ );
291                     vlc_mutex_unlock( &p_event_manager->object_lock );
292                     if( lockstate == libvlc_UnLocked )
293                         vlc_mutex_unlock( &p_event_manager->event_sending_lock );
294                     return;
295                 }
296             FOREACH_END()
297         }
298     FOREACH_END()
299     vlc_mutex_unlock( &p_event_manager->object_lock );
300     if( lockstate == libvlc_UnLocked )
301         vlc_mutex_unlock( &p_event_manager->event_sending_lock );
302
303     libvlc_exception_raise( p_e,
304             "This object event manager doesn't know about '%i,%p,%p' event observer",
305             event_type, pf_callback, p_user_data );
306 }