]> git.sesse.net Git - vlc/blob - src/misc/events.c
misc/events.c: Add some debug code. (turnable on and off via #ifdef)
[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("Calling '%s' with a '%s' event (data %p)\n",
159                     listener->psz_debug_name,
160                     ppsz_event_type_to_name[event_type],
161                     listener->p_user_data );
162 #endif
163                 /* This is safe to do that because we are sure 
164                  * that there will be no object owned references
165                  * used after the lock. */
166                 vlc_mutex_unlock( &p_em->object_lock );
167                 func( p_event, user_data );
168                 vlc_mutex_lock( &p_em->object_lock );
169             FOREACH_END()
170             break;
171         }
172     FOREACH_END()
173     vlc_mutex_unlock( &p_em->object_lock );
174 }
175
176 /**
177  * Add a callback for an event.
178  */
179 int __vlc_event_attach( vlc_event_manager_t * p_em,
180                         vlc_event_type_t event_type,
181                         vlc_event_callback_t pf_callback,
182                         void *p_user_data,
183                         const char * psz_debug_name )
184 {
185     vlc_event_listeners_group_t * listeners_group;
186     vlc_event_listener_t * listener;
187     listener = malloc(sizeof(vlc_event_listener_t));
188     if( !listener )
189         return VLC_ENOMEM;
190     
191     listener->p_user_data = p_user_data;
192     listener->pf_callback = pf_callback;
193 #ifdef DEBUG_EVENT
194     listener->psz_debug_name = strdup( psz_debug_name );
195 #else
196     (void)psz_debug_name;
197 #endif
198
199     vlc_mutex_lock( &p_em->object_lock );
200     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
201         if( listeners_group->event_type == event_type )
202         {
203             ARRAY_APPEND( listeners_group->listeners, listener );
204 #ifdef DEBUG_EVENT
205                 msg_Dbg("Listening to '%s' event with '%s' (data %p)\n",
206                     ppsz_event_type_to_name[event_type],
207                     listener->psz_debug_name,
208                     listener->p_user_data );
209 #endif
210             vlc_mutex_unlock( &p_em->object_lock );
211             return VLC_SUCCESS;
212         }
213     FOREACH_END()
214     vlc_mutex_unlock( &p_em->object_lock );
215
216     msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
217     free(listener);
218     return VLC_EGENERIC;
219 }
220
221 /**
222  * Remove a callback for an event.
223  */
224 int vlc_event_detach( vlc_event_manager_t *p_em,
225                       vlc_event_type_t event_type,
226                       vlc_event_callback_t pf_callback,
227                       void *p_user_data )
228 {
229     vlc_event_listeners_group_t * listeners_group;
230     struct vlc_event_listener_t * listener;
231
232     vlc_mutex_lock( &p_em->object_lock );
233     FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
234         if( listeners_group->event_type == event_type )
235         {
236             FOREACH_ARRAY( listener, listeners_group->listeners )
237                 if( listener->pf_callback == pf_callback &&
238                     listener->p_user_data == p_user_data )
239                 {
240                     /* that's our listener */
241                     ARRAY_REMOVE( listeners_group->listeners,
242                         fe_idx /* This comes from the macro (and that's why
243                                   I hate macro) */ );
244 #ifdef DEBUG_EVENT
245                     msg_Dbg("Detaching '%s' from '%s' event (data %p)\n",
246                         listener->psz_debug_name,
247                         ppsz_event_type_to_name[event_type],
248                         listener->p_user_data );
249
250                     free( listener->psz_debug_name );
251 #endif
252                     free( listener );
253                     vlc_mutex_unlock( &p_em->object_lock );
254                     return VLC_SUCCESS;
255                 }
256             FOREACH_END()
257         }
258     FOREACH_END()
259     vlc_mutex_unlock( &p_em->object_lock );
260
261     msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );
262
263     return VLC_EGENERIC;
264 }