]> git.sesse.net Git - vlc/blob - src/misc/events.c
misc/events.c: Fix event debugging.
[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 } vlc_event_listeners_group_t;
62
63 #ifdef DEBUG_EVENT
64 static const char * ppsz_event_type_to_name[] = 
65 {
66     [vlc_InputItemMetaChanged]          = "vlc_InputItemMetaChanged",
67     [vlc_InputItemSubItemAdded]         = "vlc_InputItemSubItemAdded",
68     [vlc_ServicesDiscoveryItemAdded]    = "vlc_ServicesDiscoveryItemAdded",
69     [vlc_ServicesDiscoveryItemRemoved]  = "vlc_ServicesDiscoveryItemRemoved"
70 };
71 #endif
72
73 /*****************************************************************************
74  * 
75  *****************************************************************************/
76
77 /**
78  * Initialize event manager object
79  * p_obj is the object that contains the event manager. But not
80  * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
81  * for instance).
82  * p_parent_obj gives a libvlc instance
83  */
84 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
85                             vlc_object_t * p_parent_obj )
86 {
87     p_em->p_obj = p_obj;
88     p_em->p_parent_object = p_parent_obj;
89     vlc_mutex_init( p_parent_obj, &p_em->object_lock );
90     ARRAY_INIT( p_em->listeners_groups );
91     return VLC_SUCCESS;
92 }
93
94 /**
95  * Destroy the event manager
96  */
97 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
98 {
99     struct vlc_event_listeners_group_t * listeners_group;
100     struct vlc_event_listener_t * listener;
101
102     vlc_mutex_destroy( &p_em->object_lock );
103
104     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
105         FOREACH_ARRAY( listener, listeners_group->listeners )
106             free( listener );
107         FOREACH_END()
108         free( listeners_group );
109     FOREACH_END()
110 }
111
112 /**
113  * Destroy the event manager
114  */
115 int vlc_event_manager_register_event_type(
116         vlc_event_manager_t * p_em,
117         vlc_event_type_t event_type )
118 {
119     vlc_event_listeners_group_t * listeners_group;
120     listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
121
122     if( !listeners_group )
123         return VLC_ENOMEM;
124
125     listeners_group->event_type = event_type;
126     ARRAY_INIT( listeners_group->listeners );
127     
128     vlc_mutex_lock( &p_em->object_lock );
129     ARRAY_APPEND( p_em->listeners_groups, listeners_group );
130     vlc_mutex_unlock( &p_em->object_lock );
131
132     return VLC_SUCCESS;
133 }
134
135 /**
136  * Send an event to the listener attached to this p_em.
137  */
138 void vlc_event_send( vlc_event_manager_t * p_em,
139                      vlc_event_t * p_event )
140 {
141     vlc_event_listeners_group_t * listeners_group;
142     vlc_event_listener_t * listener;
143     vlc_event_callback_t func = NULL;
144     void * user_data = NULL;
145
146     /* Fill event with the sending object now */
147     p_event->p_obj = p_em->p_obj;
148
149     vlc_mutex_lock( &p_em->object_lock );
150     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
151         if( listeners_group->event_type == p_event->type )
152         {
153             /* We found the group, now send every one the event */
154             FOREACH_ARRAY( listener, listeners_group->listeners )
155                 func = listener->pf_callback;
156                 user_data = listener->p_user_data;
157 #ifdef DEBUG_EVENT
158                 msg_Dbg( p_em->p_parent_object,
159                     "Calling '%s' with a '%s' event (data %p)",
160                     listener->psz_debug_name,
161                     ppsz_event_type_to_name[p_event->type],
162                     listener->p_user_data );
163 #endif
164                 /* This is safe to do that because we are sure 
165                  * that there will be no object owned references
166                  * used after the lock. */
167                 vlc_mutex_unlock( &p_em->object_lock );
168                 func( p_event, user_data );
169                 vlc_mutex_lock( &p_em->object_lock );
170             FOREACH_END()
171             break;
172         }
173     FOREACH_END()
174     vlc_mutex_unlock( &p_em->object_lock );
175 }
176
177 /**
178  * Add a callback for an event.
179  */
180 int __vlc_event_attach( vlc_event_manager_t * p_em,
181                         vlc_event_type_t event_type,
182                         vlc_event_callback_t pf_callback,
183                         void *p_user_data,
184                         const char * psz_debug_name )
185 {
186     vlc_event_listeners_group_t * listeners_group;
187     vlc_event_listener_t * listener;
188     listener = malloc(sizeof(vlc_event_listener_t));
189     if( !listener )
190         return VLC_ENOMEM;
191     
192     listener->p_user_data = p_user_data;
193     listener->pf_callback = pf_callback;
194 #ifdef DEBUG_EVENT
195     listener->psz_debug_name = strdup( psz_debug_name );
196 #else
197     (void)psz_debug_name;
198 #endif
199
200     vlc_mutex_lock( &p_em->object_lock );
201     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
202         if( listeners_group->event_type == event_type )
203         {
204             ARRAY_APPEND( listeners_group->listeners, listener );
205 #ifdef DEBUG_EVENT
206                 msg_Dbg( p_em->p_parent_object,
207                     "Listening to '%s' event with '%s' (data %p)",
208                     ppsz_event_type_to_name[event_type],
209                     listener->psz_debug_name,
210                     listener->p_user_data );
211 #endif
212             vlc_mutex_unlock( &p_em->object_lock );
213             return VLC_SUCCESS;
214         }
215     FOREACH_END()
216     vlc_mutex_unlock( &p_em->object_lock );
217
218     msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
219     free(listener);
220     return VLC_EGENERIC;
221 }
222
223 /**
224  * Remove a callback for an event.
225  */
226 int vlc_event_detach( vlc_event_manager_t *p_em,
227                       vlc_event_type_t event_type,
228                       vlc_event_callback_t pf_callback,
229                       void *p_user_data )
230 {
231     vlc_event_listeners_group_t * listeners_group;
232     struct vlc_event_listener_t * listener;
233
234     vlc_mutex_lock( &p_em->object_lock );
235     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
236         if( listeners_group->event_type == event_type )
237         {
238             FOREACH_ARRAY( listener, listeners_group->listeners )
239                 if( listener->pf_callback == pf_callback &&
240                     listener->p_user_data == p_user_data )
241                 {
242                     /* that's our listener */
243                     ARRAY_REMOVE( listeners_group->listeners,
244                         fe_idx /* This comes from the macro (and that's why
245                                   I hate macro) */ );
246 #ifdef DEBUG_EVENT
247                     msg_Dbg( p_em->p_parent_object,
248                         "Detaching '%s' from '%s' event (data %p)\n",
249                         listener->psz_debug_name,
250                         ppsz_event_type_to_name[event_type],
251                         listener->p_user_data );
252
253                     free( listener->psz_debug_name );
254 #endif
255                     free( listener );
256                     vlc_mutex_unlock( &p_em->object_lock );
257                     return VLC_SUCCESS;
258                 }
259             FOREACH_END()
260         }
261     FOREACH_END()
262     vlc_mutex_unlock( &p_em->object_lock );
263
264     msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );
265
266     return VLC_EGENERIC;
267 }