]> git.sesse.net Git - vlc/blob - src/misc/events.c
Removed variable callback when needed in the projectm module.
[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 the VideoLAN team
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
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 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 General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, 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->object_lock );
174     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
175         if( listeners_group->event_type == p_event->type )
176         {
177             if( listeners_group->listeners.i_size <= 0 )
178                 break;
179
180             /* Save the function to call */
181             i_cached_listeners = listeners_group->listeners.i_size;
182             array_of_cached_listeners = malloc(
183                     sizeof(vlc_event_listener_t)*i_cached_listeners );
184             if( !array_of_cached_listeners )
185             {
186                 vlc_mutex_unlock( &p_em->object_lock );
187                 return;
188             }
189
190             cached_listener = array_of_cached_listeners;
191             FOREACH_ARRAY( listener, listeners_group->listeners )
192                 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
193                 cached_listener++;
194             FOREACH_END()
195
196             break;
197         }
198     FOREACH_END()
199     vlc_mutex_unlock( &p_em->object_lock );
200  
201     /* Call the function attached */
202     cached_listener = array_of_cached_listeners;
203
204     if( !listeners_group || !array_of_cached_listeners )
205     {
206         free( array_of_cached_listeners );
207         return;
208     }
209
210     vlc_mutex_lock( &p_em->event_sending_lock ) ;
211
212     /* Track item removed from *this* thread, with a simple flag */
213     listeners_group->b_sublistener_removed = false;
214
215     for( i = 0; i < i_cached_listeners; i++ )
216     {
217         /* No need to lock on listeners_group, a listener group can't be removed */
218         if( listeners_group->b_sublistener_removed )
219         {
220             /* If a callback was removed, this gets called */
221             bool valid_listener;
222             vlc_mutex_lock( &p_em->object_lock );
223             valid_listener = group_contains_listener( listeners_group, cached_listener );
224             vlc_mutex_unlock( &p_em->object_lock );
225             if( !valid_listener )
226             {
227                 cached_listener++;
228                 continue;
229             }
230         }
231         cached_listener->pf_callback( p_event, cached_listener->p_user_data );
232         cached_listener++;
233     }
234     vlc_mutex_unlock( &p_em->event_sending_lock );
235
236     free( array_of_cached_listeners );
237 }
238
239 #undef vlc_event_attach
240 /**
241  * Add a callback for an event.
242  */
243 int vlc_event_attach( vlc_event_manager_t * p_em,
244                       vlc_event_type_t event_type,
245                       vlc_event_callback_t pf_callback,
246                       void *p_user_data )
247 {
248     vlc_event_listeners_group_t * listeners_group;
249     vlc_event_listener_t * listener;
250     listener = malloc(sizeof(vlc_event_listener_t));
251     if( !listener )
252         return VLC_ENOMEM;
253  
254     listener->p_user_data = p_user_data;
255     listener->pf_callback = pf_callback;
256
257     vlc_mutex_lock( &p_em->object_lock );
258     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
259         if( listeners_group->event_type == event_type )
260         {
261             ARRAY_APPEND( listeners_group->listeners, listener );
262             vlc_mutex_unlock( &p_em->object_lock );
263             return VLC_SUCCESS;
264         }
265     FOREACH_END()
266     /* Unknown event = BUG */
267     assert( 0 );
268 }
269
270 /**
271  * Remove a callback for an event.
272  */
273
274 void vlc_event_detach( vlc_event_manager_t *p_em,
275                        vlc_event_type_t event_type,
276                        vlc_event_callback_t pf_callback,
277                        void *p_user_data )
278 {
279     vlc_event_listeners_group_t * listeners_group;
280     struct vlc_event_listener_t * listener;
281
282     vlc_mutex_lock( &p_em->event_sending_lock );
283     vlc_mutex_lock( &p_em->object_lock );
284     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
285         if( listeners_group->event_type == event_type )
286         {
287             FOREACH_ARRAY( listener, listeners_group->listeners )
288                 if( listener->pf_callback == pf_callback &&
289                     listener->p_user_data == p_user_data )
290                 {
291                     /* Tell vlc_event_send, we did remove an item from that group,
292                        in case vlc_event_send is in our caller stack  */
293                     listeners_group->b_sublistener_removed = true;
294
295                     /* that's our listener */
296                     ARRAY_REMOVE( listeners_group->listeners,
297                         fe_idx /* This comes from the macro (and that's why
298                                   I hate macro) */ );
299                     free( listener );
300                     vlc_mutex_unlock( &p_em->event_sending_lock );
301                     vlc_mutex_unlock( &p_em->object_lock );
302                     return;
303                 }
304             FOREACH_END()
305         }
306     FOREACH_END()
307
308     assert( 0 );
309 }