]> git.sesse.net Git - vlc/blob - src/misc/events.c
089b5e85f87a82f08d261d8597fdc46f390425d5
[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 //#define DEBUG_EVENT
46
47 /*****************************************************************************
48  *  Private types.
49  *****************************************************************************/
50
51 typedef struct vlc_event_listener_t
52 {
53     void *               p_user_data;
54     vlc_event_callback_t pf_callback;
55 #ifdef DEBUG_EVENT
56     char *               psz_debug_name;
57 #endif
58 } vlc_event_listener_t;
59
60 typedef struct vlc_event_listeners_group_t
61 {
62     vlc_event_type_t    event_type;
63     DECL_ARRAY(struct vlc_event_listener_t *) listeners;
64
65    /* Used in vlc_event_send() to make sure to behave
66       Correctly when vlc_event_detach was called during
67       a callback */
68     bool          b_sublistener_removed;
69                                          
70 } vlc_event_listeners_group_t;
71
72 #ifdef DEBUG_EVENT
73 static const char ppsz_event_type_to_name[][33] =
74 {
75     [vlc_InputStateChanged]             = "vlc_InputStateChanged",
76     [vlc_InputSelectedStreamChanged]    = "vlc_InputSelectedStreamChanged",
77
78     [vlc_InputItemMetaChanged]          = "vlc_InputItemMetaChanged",
79     [vlc_InputItemSubItemAdded]         = "vlc_InputItemSubItemAdded",
80     [vlc_InputItemDurationChanged]      = "vlc_InputItemDurationChanged",
81     [vlc_InputItemPreparsedChanged]     = "vlc_InputItemPreparsedChanged",
82     [vlc_InputItemNameChanged]          = "vlc_InputItemNameChanged",
83     [vlc_InputItemInfoChanged]          = "vlc_InputItemInfoChanged",
84
85     [vlc_ServicesDiscoveryItemAdded]    = "vlc_ServicesDiscoveryItemAdded",
86     [vlc_ServicesDiscoveryItemRemoved]  = "vlc_ServicesDiscoveryItemRemoved"
87     [vlc_ServicesDiscoveryStarted]      = "vlc_ServicesDiscoveryStarted"
88     [vlc_ServicesDiscoveryEnded]        = "vlc_ServicesDiscoveryEnded"
89 };
90 #endif
91
92 static bool
93 listeners_are_equal( vlc_event_listener_t * listener1,
94                      vlc_event_listener_t * listener2 )
95 {
96     return listener1->pf_callback == listener2->pf_callback &&
97            listener1->p_user_data == listener2->p_user_data;
98 }
99
100 static bool
101 group_contains_listener( vlc_event_listeners_group_t * group,
102                          vlc_event_listener_t * searched_listener )
103 {
104     vlc_event_listener_t * listener;
105     FOREACH_ARRAY( listener, group->listeners )
106         if( listeners_are_equal(searched_listener, listener) )
107             return true;
108     FOREACH_END()
109     return false;
110 }
111
112 /*****************************************************************************
113  *
114  *****************************************************************************/
115
116 /**
117  * Initialize event manager object
118  * p_obj is the object that contains the event manager. But not
119  * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
120  * for instance).
121  * p_parent_obj gives a libvlc instance
122  */
123 int __vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
124                               vlc_object_t * p_parent_obj )
125 {
126     p_em->p_obj = p_obj;
127     p_em->p_parent_object = p_parent_obj;
128     vlc_mutex_init( &p_em->object_lock );
129
130     /* We need a recursive lock here, because we need to be able
131      * to call libvlc_event_detach even if vlc_event_send is in
132      * the call frame.
133      * This ensures that after libvlc_event_detach, the callback
134      * will never gets triggered.
135      * */
136     vlc_mutex_init_recursive( &p_em->event_sending_lock );
137     ARRAY_INIT( p_em->listeners_groups );
138     return VLC_SUCCESS;
139 }
140
141 /**
142  * Destroy the event manager
143  */
144 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
145 {
146     struct vlc_event_listeners_group_t * listeners_group;
147     struct vlc_event_listener_t * listener;
148
149     vlc_mutex_destroy( &p_em->object_lock );
150     vlc_mutex_destroy( &p_em->event_sending_lock );
151
152     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
153         FOREACH_ARRAY( listener, listeners_group->listeners )
154             free( listener );
155         FOREACH_END()
156         ARRAY_RESET( listeners_group->listeners );
157         free( listeners_group );
158     FOREACH_END()
159     ARRAY_RESET( p_em->listeners_groups );
160 }
161
162 /**
163  * Destroy the event manager
164  */
165 int vlc_event_manager_register_event_type(
166         vlc_event_manager_t * p_em,
167         vlc_event_type_t event_type )
168 {
169     vlc_event_listeners_group_t * listeners_group;
170     listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
171
172     if( !listeners_group )
173         return VLC_ENOMEM;
174
175     listeners_group->event_type = event_type;
176     ARRAY_INIT( listeners_group->listeners );
177  
178     vlc_mutex_lock( &p_em->object_lock );
179     ARRAY_APPEND( p_em->listeners_groups, listeners_group );
180     vlc_mutex_unlock( &p_em->object_lock );
181
182     return VLC_SUCCESS;
183 }
184
185 /**
186  * Send an event to the listener attached to this p_em.
187  */
188 void vlc_event_send( vlc_event_manager_t * p_em,
189                      vlc_event_t * p_event )
190 {
191     vlc_event_listeners_group_t * listeners_group = NULL;
192     vlc_event_listener_t * listener;
193     vlc_event_listener_t * array_of_cached_listeners = NULL;
194     vlc_event_listener_t * cached_listener;
195     int i, i_cached_listeners = 0;
196
197     /* Fill event with the sending object now */
198     p_event->p_obj = p_em->p_obj;
199
200     vlc_mutex_lock( &p_em->object_lock );
201     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
202         if( listeners_group->event_type == p_event->type )
203         {
204             if( listeners_group->listeners.i_size <= 0 )
205                 break;
206
207             /* Save the function to call */
208             i_cached_listeners = listeners_group->listeners.i_size;
209             array_of_cached_listeners = malloc(
210                     sizeof(vlc_event_listener_t)*i_cached_listeners );
211             if( !array_of_cached_listeners )
212             {
213                 vlc_mutex_unlock( &p_em->object_lock );
214                 return;
215             }
216
217             cached_listener = array_of_cached_listeners;
218             FOREACH_ARRAY( listener, listeners_group->listeners )
219                 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
220 #ifdef DEBUG_EVENT
221                 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
222 #endif
223                 cached_listener++;
224             FOREACH_END()
225
226             break;
227         }
228     FOREACH_END()
229     vlc_mutex_unlock( &p_em->object_lock );
230  
231     /* Call the function attached */
232     cached_listener = array_of_cached_listeners;
233
234     if( !listeners_group || !array_of_cached_listeners )
235     {
236         free( array_of_cached_listeners );
237         return;
238     }
239
240     vlc_mutex_lock( &p_em->event_sending_lock ) ;
241
242     /* Track item removed from *this* thread, with a simple flag */
243     listeners_group->b_sublistener_removed = false;
244
245     for( i = 0; i < i_cached_listeners; i++ )
246     {
247 #ifdef DEBUG_EVENT
248         msg_Dbg( p_em->p_parent_object,
249                     "Calling '%s' with a '%s' event (data %p)",
250                     cached_listener->psz_debug_name,
251                     ppsz_event_type_to_name[p_event->type],
252                     cached_listener->p_user_data );
253         free(cached_listener->psz_debug_name);
254 #endif
255         /* No need to lock on listeners_group, a listener group can't be removed */
256         if( listeners_group->b_sublistener_removed )
257         {
258             /* If a callback was removed, this gets called */
259             bool valid_listener;
260             vlc_mutex_lock( &p_em->object_lock );
261             valid_listener = group_contains_listener( listeners_group, cached_listener );
262             vlc_mutex_unlock( &p_em->object_lock );
263             if( !valid_listener )
264             {
265 #ifdef DEBUG_EVENT
266                 msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
267 #endif
268                 cached_listener++;
269                 continue;
270             }
271         }
272         cached_listener->pf_callback( p_event, cached_listener->p_user_data );
273         cached_listener++;
274     }
275     vlc_mutex_unlock( &p_em->event_sending_lock );
276
277     free( array_of_cached_listeners );
278 }
279
280 /**
281  * Add a callback for an event.
282  */
283 int __vlc_event_attach( vlc_event_manager_t * p_em,
284                         vlc_event_type_t event_type,
285                         vlc_event_callback_t pf_callback,
286                         void *p_user_data,
287                         const char * psz_debug_name )
288 {
289     vlc_event_listeners_group_t * listeners_group;
290     vlc_event_listener_t * listener;
291     listener = malloc(sizeof(vlc_event_listener_t));
292     if( !listener )
293         return VLC_ENOMEM;
294  
295     listener->p_user_data = p_user_data;
296     listener->pf_callback = pf_callback;
297 #ifdef DEBUG_EVENT
298     listener->psz_debug_name = strdup( psz_debug_name );
299 #else
300     (void)psz_debug_name;
301 #endif
302
303     vlc_mutex_lock( &p_em->object_lock );
304     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
305         if( listeners_group->event_type == event_type )
306         {
307             ARRAY_APPEND( listeners_group->listeners, listener );
308 #ifdef DEBUG_EVENT
309                 msg_Dbg( p_em->p_parent_object,
310                     "Listening to '%s' event with '%s' (data %p)",
311                     ppsz_event_type_to_name[event_type],
312                     listener->psz_debug_name,
313                     listener->p_user_data );
314 #endif
315             vlc_mutex_unlock( &p_em->object_lock );
316             return VLC_SUCCESS;
317         }
318     FOREACH_END()
319     vlc_mutex_unlock( &p_em->object_lock );
320
321     msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
322     free(listener);
323     return VLC_EGENERIC;
324 }
325
326 /**
327  * Remove a callback for an event.
328  */
329
330 int vlc_event_detach( vlc_event_manager_t *p_em,
331                       vlc_event_type_t event_type,
332                       vlc_event_callback_t pf_callback,
333                       void *p_user_data )
334 {
335     vlc_event_listeners_group_t * listeners_group;
336     struct vlc_event_listener_t * listener;
337
338     vlc_mutex_lock( &p_em->object_lock );
339     vlc_mutex_lock( &p_em->event_sending_lock );
340     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
341         if( listeners_group->event_type == event_type )
342         {
343             FOREACH_ARRAY( listener, listeners_group->listeners )
344                 if( listener->pf_callback == pf_callback &&
345                     listener->p_user_data == p_user_data )
346                 {
347                     /* Tell vlc_event_send, we did remove an item from that group,
348                        in case vlc_event_send is in our caller stack  */
349                     listeners_group->b_sublistener_removed = true;
350
351                     /* that's our listener */
352                     ARRAY_REMOVE( listeners_group->listeners,
353                         fe_idx /* This comes from the macro (and that's why
354                                   I hate macro) */ );
355 #ifdef DEBUG_EVENT
356                     msg_Dbg( p_em->p_parent_object,
357                         "Detaching '%s' from '%s' event (data %p)",
358                         listener->psz_debug_name,
359                         ppsz_event_type_to_name[event_type],
360                         listener->p_user_data );
361
362                     free( listener->psz_debug_name );
363 #endif
364                     free( listener );
365                     vlc_mutex_unlock( &p_em->event_sending_lock );
366                     vlc_mutex_unlock( &p_em->object_lock );
367                     return VLC_SUCCESS;
368                 }
369             FOREACH_END()
370         }
371     FOREACH_END()
372     vlc_mutex_unlock( &p_em->event_sending_lock );
373     vlc_mutex_unlock( &p_em->object_lock );
374
375     msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );
376
377     return VLC_EGENERIC;
378 }
379