]> git.sesse.net Git - vlc/blob - src/misc/events.c
decoder: inline DecoderSignalWait()
[vlc] / src / misc / events.c
1 /*****************************************************************************
2  * events.c: events interface
3  * This library provides an interface to the send and receive events.
4  * It is more lightweight than variable based callback.
5  *****************************************************************************
6  * Copyright (C) 1998-2005 VLC authors and VideoLAN
7  * $Id$
8  *
9  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35
36 #include <assert.h>
37
38 #include <vlc_events.h>
39 #include <vlc_arrays.h>
40
41 /*****************************************************************************
42  * Documentation : Read vlc_events.h
43  *****************************************************************************/
44
45 /*****************************************************************************
46  *  Private types.
47  *****************************************************************************/
48
49 typedef struct vlc_event_listener_t
50 {
51     void *               p_user_data;
52     vlc_event_callback_t pf_callback;
53 } vlc_event_listener_t;
54
55 typedef struct vlc_event_listeners_group_t
56 {
57     vlc_event_type_t    event_type;
58     DECL_ARRAY(struct vlc_event_listener_t *) listeners;
59
60    /* Used in vlc_event_send() to make sure to behave
61       Correctly when vlc_event_detach was called during
62       a callback */
63     bool          b_sublistener_removed;
64                                          
65 } vlc_event_listeners_group_t;
66
67 static bool
68 listeners_are_equal( vlc_event_listener_t * listener1,
69                      vlc_event_listener_t * listener2 )
70 {
71     return listener1->pf_callback == listener2->pf_callback &&
72            listener1->p_user_data == listener2->p_user_data;
73 }
74
75 static bool
76 group_contains_listener( vlc_event_listeners_group_t * group,
77                          vlc_event_listener_t * searched_listener )
78 {
79     vlc_event_listener_t * listener;
80     FOREACH_ARRAY( listener, group->listeners )
81         if( listeners_are_equal(searched_listener, listener) )
82             return true;
83     FOREACH_END()
84     return false;
85 }
86
87 /*****************************************************************************
88  *
89  *****************************************************************************/
90
91 #undef vlc_event_manager_init
92 /**
93  * Initialize event manager object
94  * p_obj is the object that contains the event manager. But not
95  * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
96  * for instance).
97  */
98 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj )
99 {
100     p_em->p_obj = p_obj;
101     vlc_mutex_init( &p_em->object_lock );
102
103     /* We need a recursive lock here, because we need to be able
104      * to call libvlc_event_detach even if vlc_event_send is in
105      * the call frame.
106      * This ensures that after libvlc_event_detach, the callback
107      * will never gets triggered.
108      * */
109     vlc_mutex_init_recursive( &p_em->event_sending_lock );
110     ARRAY_INIT( p_em->listeners_groups );
111     return VLC_SUCCESS;
112 }
113
114 /**
115  * Destroy the event manager
116  */
117 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
118 {
119     struct vlc_event_listeners_group_t * listeners_group;
120     struct vlc_event_listener_t * listener;
121
122     vlc_mutex_destroy( &p_em->object_lock );
123     vlc_mutex_destroy( &p_em->event_sending_lock );
124
125     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
126         FOREACH_ARRAY( listener, listeners_group->listeners )
127             free( listener );
128         FOREACH_END()
129         ARRAY_RESET( listeners_group->listeners );
130         free( listeners_group );
131     FOREACH_END()
132     ARRAY_RESET( p_em->listeners_groups );
133 }
134
135 /**
136  * Register the event manager
137  */
138 int vlc_event_manager_register_event_type(
139         vlc_event_manager_t * p_em,
140         vlc_event_type_t event_type )
141 {
142     vlc_event_listeners_group_t * listeners_group;
143     listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
144
145     if( !listeners_group )
146         return VLC_ENOMEM;
147
148     listeners_group->event_type = event_type;
149     ARRAY_INIT( listeners_group->listeners );
150  
151     vlc_mutex_lock( &p_em->object_lock );
152     ARRAY_APPEND( p_em->listeners_groups, listeners_group );
153     vlc_mutex_unlock( &p_em->object_lock );
154
155     return VLC_SUCCESS;
156 }
157
158 /**
159  * Send an event to the listener attached to this p_em.
160  */
161 void vlc_event_send( vlc_event_manager_t * p_em,
162                      vlc_event_t * p_event )
163 {
164     vlc_event_listeners_group_t * listeners_group = NULL;
165     vlc_event_listener_t * listener;
166     vlc_event_listener_t * array_of_cached_listeners = NULL;
167     vlc_event_listener_t * cached_listener;
168     int i, i_cached_listeners = 0;
169
170     /* Fill event with the sending object now */
171     p_event->p_obj = p_em->p_obj;
172
173     vlc_mutex_lock( &p_em->event_sending_lock ) ;
174     vlc_mutex_lock( &p_em->object_lock );
175
176     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
177         if( listeners_group->event_type == p_event->type )
178         {
179             if( listeners_group->listeners.i_size <= 0 )
180                 break;
181
182             /* Save the function to call */
183             i_cached_listeners = listeners_group->listeners.i_size;
184             array_of_cached_listeners = malloc(
185                     sizeof(vlc_event_listener_t)*i_cached_listeners );
186             if( !array_of_cached_listeners )
187             {
188                 vlc_mutex_unlock( &p_em->object_lock );
189                 vlc_mutex_unlock( &p_em->event_sending_lock ) ;
190                 return;
191             }
192
193             cached_listener = array_of_cached_listeners;
194             FOREACH_ARRAY( listener, listeners_group->listeners )
195                 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
196                 cached_listener++;
197             FOREACH_END()
198
199             break;
200         }
201     FOREACH_END()
202
203     /* Track item removed from *this* thread, with a simple flag. Indeed
204      * event_sending_lock is a recursive lock. This has the advantage of
205      * allowing to remove an event listener from within a callback */
206     listeners_group->b_sublistener_removed = false;
207
208     vlc_mutex_unlock( &p_em->object_lock );
209
210     /* Call the function attached */
211     cached_listener = array_of_cached_listeners;
212
213     if( !listeners_group || !array_of_cached_listeners )
214     {
215         free( array_of_cached_listeners );
216         vlc_mutex_unlock( &p_em->event_sending_lock );
217         return;
218     }
219
220
221     for( i = 0; i < i_cached_listeners; i++ )
222     {
223         if( listeners_group->b_sublistener_removed )
224         {
225             /* If a callback was removed inside one of our callback, this gets
226              * called */
227             bool valid_listener;
228             vlc_mutex_lock( &p_em->object_lock );
229             valid_listener = group_contains_listener( listeners_group, cached_listener );
230             vlc_mutex_unlock( &p_em->object_lock );
231             if( !valid_listener )
232             {
233                 cached_listener++;
234                 continue;
235             }
236         }
237         cached_listener->pf_callback( p_event, cached_listener->p_user_data );
238         cached_listener++;
239     }
240     vlc_mutex_unlock( &p_em->event_sending_lock );
241
242     free( array_of_cached_listeners );
243 }
244
245 #undef vlc_event_attach
246 /**
247  * Add a callback for an event.
248  */
249 int vlc_event_attach( vlc_event_manager_t * p_em,
250                       vlc_event_type_t event_type,
251                       vlc_event_callback_t pf_callback,
252                       void *p_user_data )
253 {
254     vlc_event_listeners_group_t * listeners_group;
255     vlc_event_listener_t * listener;
256     listener = malloc(sizeof(vlc_event_listener_t));
257     if( !listener )
258         return VLC_ENOMEM;
259  
260     listener->p_user_data = p_user_data;
261     listener->pf_callback = pf_callback;
262
263     vlc_mutex_lock( &p_em->object_lock );
264     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
265         if( listeners_group->event_type == event_type )
266         {
267             ARRAY_APPEND( listeners_group->listeners, listener );
268             vlc_mutex_unlock( &p_em->object_lock );
269             return VLC_SUCCESS;
270         }
271     FOREACH_END()
272     /* Unknown event = BUG */
273     vlc_assert_unreachable();
274 }
275
276 /**
277  * Remove a callback for an event.
278  */
279
280 void vlc_event_detach( vlc_event_manager_t *p_em,
281                        vlc_event_type_t event_type,
282                        vlc_event_callback_t pf_callback,
283                        void *p_user_data )
284 {
285     vlc_event_listeners_group_t * listeners_group;
286     struct vlc_event_listener_t * listener;
287
288     vlc_mutex_lock( &p_em->event_sending_lock );
289     vlc_mutex_lock( &p_em->object_lock );
290     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
291         if( listeners_group->event_type == event_type )
292         {
293             FOREACH_ARRAY( listener, listeners_group->listeners )
294                 if( listener->pf_callback == pf_callback &&
295                     listener->p_user_data == p_user_data )
296                 {
297                     /* Tell vlc_event_send, we did remove an item from that group,
298                        in case vlc_event_send is in our caller stack  */
299                     listeners_group->b_sublistener_removed = true;
300
301                     /* that's our listener */
302                     ARRAY_REMOVE( listeners_group->listeners,
303                         fe_idx /* This comes from the macro (and that's why
304                                   I hate macro) */ );
305                     free( listener );
306                     vlc_mutex_unlock( &p_em->event_sending_lock );
307                     vlc_mutex_unlock( &p_em->object_lock );
308                     return;
309                 }
310             FOREACH_END()
311         }
312     FOREACH_END()
313
314     vlc_assert_unreachable();
315 }