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