]> git.sesse.net Git - vlc/blob - src/control/event.c
c976629a7acb66d5cb78c001086f4f6e800a0fab
[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, libvlc_exception_t *p_e )
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     ARRAY_INIT( p_em->listeners_groups );
79     vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
80     vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
81     return p_em;
82 }
83
84 /**************************************************************************
85  *       libvlc_event_manager_release (internal) :
86  *
87  * Init an object's event manager.
88  **************************************************************************/
89 void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
90 {
91     libvlc_event_listeners_group_t * listeners_group;
92     libvlc_event_listener_t * listener;
93
94     vlc_mutex_destroy( &p_em->event_sending_lock );
95     vlc_mutex_destroy( &p_em->object_lock );
96
97     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
98         FOREACH_ARRAY( listener, listeners_group->listeners )
99             free( listener );
100         FOREACH_END()
101         ARRAY_RESET( listeners_group->listeners );
102         free( listeners_group );
103     FOREACH_END()
104     ARRAY_RESET( p_em->listeners_groups );
105     free( p_em );
106 }
107
108 /**************************************************************************
109  *       libvlc_event_manager_register_event_type (internal) :
110  *
111  * Init an object's event manager.
112  **************************************************************************/
113 void libvlc_event_manager_register_event_type(
114         libvlc_event_manager_t * p_em,
115         libvlc_event_type_t event_type,
116         libvlc_exception_t * p_e )
117 {
118     libvlc_event_listeners_group_t * listeners_group;
119     listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
120     if( !listeners_group )
121     {
122         libvlc_exception_raise( p_e, "No Memory left" );
123         return;
124     }
125
126     listeners_group->event_type = event_type;
127     ARRAY_INIT( listeners_group->listeners );
128
129     vlc_mutex_lock( &p_em->object_lock );
130     ARRAY_APPEND( p_em->listeners_groups, listeners_group );
131     vlc_mutex_unlock( &p_em->object_lock );
132 }
133
134 /**************************************************************************
135  *       libvlc_event_send (internal) :
136  *
137  * Send a callback.
138  **************************************************************************/
139 void libvlc_event_send( libvlc_event_manager_t * p_em,
140                         libvlc_event_t * p_event )
141 {
142     libvlc_event_listeners_group_t * listeners_group;
143     libvlc_event_listener_t * listener_cached;
144     libvlc_event_listener_t * listener;
145     libvlc_event_listener_t * array_listeners_cached = NULL;
146     int i, i_cached_listeners = 0;
147
148     /* Fill event with the sending object now */
149     p_event->p_obj = p_em->p_obj;
150
151     /* Here a read/write lock would be nice */
152
153     vlc_mutex_lock( &p_em->object_lock );
154     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
155         if( listeners_group->event_type == p_event->type )
156         {
157             if( listeners_group->listeners.i_size <= 0 )
158                 break;
159
160             /* Cache a copy of the listener to avoid locking issues */
161             i_cached_listeners = listeners_group->listeners.i_size;
162             array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
163             if( !array_listeners_cached )
164             {
165                 printf( "Can't alloc memory in libvlc_event_send" );
166                 break;
167             }
168
169             listener_cached = array_listeners_cached;
170             FOREACH_ARRAY( listener, listeners_group->listeners )
171                 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t));
172                 listener_cached++;
173             FOREACH_END()
174             break;
175         }
176     FOREACH_END()
177
178     vlc_mutex_unlock( &p_em->object_lock );
179
180     /* Here a read/write lock would be *especially* nice,
181      * We would be able to send event without blocking */
182     /* The only reason for this lock is to be able to wait the
183      * End of events sending with libvlc_event_manager_lock_event_sending.
184      * This can be useful to make sure a callback is completely detached. */
185     vlc_mutex_lock( &p_em->event_sending_lock );
186     listener_cached = array_listeners_cached;
187     for( i = 0; i < i_cached_listeners; i++ )
188     {
189         listener_cached->pf_callback( p_event, listener_cached->p_user_data );
190         listener_cached++;
191     }
192     vlc_mutex_unlock( &p_em->event_sending_lock );
193
194     free( array_listeners_cached );
195 }
196
197 /*
198  * Public libvlc functions
199  */
200
201 /**************************************************************************
202  *       libvlc_event_attach (public) :
203  *
204  * Add a callback for an event.
205  **************************************************************************/
206 void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
207                           libvlc_event_type_t event_type,
208                           libvlc_callback_t pf_callback,
209                           void *p_user_data,
210                           libvlc_exception_t *p_e )
211 {
212     libvlc_event_listeners_group_t * listeners_group;
213     libvlc_event_listener_t * listener;
214     listener = malloc(sizeof(libvlc_event_listener_t));
215     if( !listener )
216     {
217         libvlc_exception_raise( p_e, "No Memory left" );
218         return;
219     }
220     
221     listener->event_type = event_type;
222     listener->p_user_data = p_user_data;
223     listener->pf_callback = pf_callback;
224
225     vlc_mutex_lock( &p_event_manager->object_lock );
226     FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
227         if( listeners_group->event_type == listener->event_type )
228         {
229             ARRAY_APPEND( listeners_group->listeners, listener );
230             vlc_mutex_unlock( &p_event_manager->object_lock );
231             return;
232         }
233     FOREACH_END()
234     vlc_mutex_unlock( &p_event_manager->object_lock );
235
236     free(listener);
237     libvlc_exception_raise( p_e,
238             "This object event manager doesn't know about '%s' events",
239             libvlc_event_type_name(a));
240 }
241
242 /**************************************************************************
243  *       libvlc_event_detach (public) :
244  *
245  * Remove a callback for an event.
246  **************************************************************************/
247 void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
248                           libvlc_event_type_t event_type,
249                           libvlc_callback_t pf_callback,
250                           void *p_user_data,
251                           libvlc_exception_t *p_e )
252 {
253     libvlc_event_detach_lock_state( p_event_manager, event_type, pf_callback,
254                                     p_user_data, libvlc_UnLocked, p_e );
255 }
256
257 /**************************************************************************
258  *       libvlc_event_detach_no_lock (internal) :
259  *
260  * Remove a callback for an event.
261  **************************************************************************/
262 void libvlc_event_detach_lock_state( libvlc_event_manager_t *p_event_manager,
263                                      libvlc_event_type_t event_type,
264                                      libvlc_callback_t pf_callback,
265                                      void *p_user_data,
266                                      libvlc_lock_state_t lockstate,
267                                      libvlc_exception_t *p_e )
268 {
269     libvlc_event_listeners_group_t * listeners_group;
270     libvlc_event_listener_t * listener;
271
272     if( lockstate == libvlc_UnLocked )
273         vlc_mutex_lock( &p_event_manager->event_sending_lock );
274     vlc_mutex_lock( &p_event_manager->object_lock );
275     FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
276         if( listeners_group->event_type == event_type )
277         {
278             FOREACH_ARRAY( listener, listeners_group->listeners )
279                 if( listener->event_type == event_type &&
280                     listener->pf_callback == pf_callback &&
281                     listener->p_user_data == p_user_data )
282                 {
283                     /* that's our listener */
284                     free( listener );
285                     ARRAY_REMOVE( listeners_group->listeners,
286                         fe_idx /* This comes from the macro (and that's why
287                                   I hate macro) */ );
288                     vlc_mutex_unlock( &p_event_manager->object_lock );
289                     if( lockstate == libvlc_UnLocked )
290                         vlc_mutex_unlock( &p_event_manager->event_sending_lock );
291                     return;
292                 }
293             FOREACH_END()
294         }
295     FOREACH_END()
296     vlc_mutex_unlock( &p_event_manager->object_lock );
297     if( lockstate == libvlc_UnLocked )
298         vlc_mutex_unlock( &p_event_manager->event_sending_lock );
299
300     libvlc_exception_raise( p_e,
301             "This object event manager doesn't know about '%i,%p,%p' event observer",
302             event_type, pf_callback, p_user_data );
303 }