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.
6 *****************************************************************************
7 * Copyright (C) 1998-2005 the VideoLAN team
10 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_events.h>
36 #include <vlc_arrays.h>
38 /*****************************************************************************
39 * Documentation : Read vlc_events.h
40 *****************************************************************************/
44 /*****************************************************************************
46 *****************************************************************************/
48 typedef struct vlc_event_listener_t
51 vlc_event_callback_t pf_callback;
53 char * psz_debug_name;
55 } vlc_event_listener_t;
57 typedef struct vlc_event_listeners_group_t
59 vlc_event_type_t event_type;
60 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
61 } vlc_event_listeners_group_t;
64 static const char * ppsz_event_type_to_name[] =
66 [vlc_InputItemMetaChanged] = "vlc_InputItemMetaChanged",
67 [vlc_InputItemSubItemAdded] = "vlc_InputItemSubItemAdded",
68 [vlc_ServicesDiscoveryItemAdded] = "vlc_ServicesDiscoveryItemAdded",
69 [vlc_ServicesDiscoveryItemRemoved] = "vlc_ServicesDiscoveryItemRemoved"
73 /*****************************************************************************
75 *****************************************************************************/
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
82 * p_parent_obj gives a libvlc instance
84 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
85 vlc_object_t * p_parent_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 );
95 * Destroy the event manager
97 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
99 struct vlc_event_listeners_group_t * listeners_group;
100 struct vlc_event_listener_t * listener;
102 vlc_mutex_destroy( &p_em->object_lock );
104 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
105 FOREACH_ARRAY( listener, listeners_group->listeners )
108 ARRAY_RESET( listeners_group->listeners );
109 free( listeners_group );
111 ARRAY_RESET( p_em->listeners_groups );
115 * Destroy the event manager
117 int vlc_event_manager_register_event_type(
118 vlc_event_manager_t * p_em,
119 vlc_event_type_t event_type )
121 vlc_event_listeners_group_t * listeners_group;
122 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
124 if( !listeners_group )
127 listeners_group->event_type = event_type;
128 ARRAY_INIT( listeners_group->listeners );
130 vlc_mutex_lock( &p_em->object_lock );
131 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
132 vlc_mutex_unlock( &p_em->object_lock );
138 * Send an event to the listener attached to this p_em.
140 void vlc_event_send( vlc_event_manager_t * p_em,
141 vlc_event_t * p_event )
143 vlc_event_listeners_group_t * listeners_group;
144 vlc_event_listener_t * listener;
145 vlc_event_listener_t * array_of_cached_listeners = NULL;
146 vlc_event_listener_t * cached_listener;
147 int i, i_cached_listeners = 0;
149 /* Fill event with the sending object now */
150 p_event->p_obj = p_em->p_obj;
152 vlc_mutex_lock( &p_em->object_lock );
153 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
154 if( listeners_group->event_type == p_event->type )
156 if( listeners_group->listeners.i_size <= 0 )
159 /* Save the function to call */
160 i_cached_listeners = listeners_group->listeners.i_size;
161 array_of_cached_listeners = malloc(
162 sizeof(vlc_event_listener_t)*i_cached_listeners );
163 if( !array_of_cached_listeners )
165 msg_Err( p_em->p_parent_object, "Not enough memory in vlc_event_send" );
166 vlc_mutex_unlock( &p_em->object_lock );
170 cached_listener = array_of_cached_listeners;
171 FOREACH_ARRAY( listener, listeners_group->listeners )
172 memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
174 cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
182 vlc_mutex_unlock( &p_em->object_lock );
184 /* Call the function attached */
185 cached_listener = array_of_cached_listeners;
186 for( i = 0; i < i_cached_listeners; i++ )
189 msg_Dbg( p_em->p_parent_object,
190 "Calling '%s' with a '%s' event (data %p)",
191 cached_listener->psz_debug_name,
192 ppsz_event_type_to_name[p_event->type],
193 cached_listener->p_user_data );
194 free(cached_listener->psz_debug_name);
197 cached_listener->pf_callback( p_event, cached_listener->p_user_data );
200 free( array_of_cached_listeners );
205 * Add a callback for an event.
207 int __vlc_event_attach( vlc_event_manager_t * p_em,
208 vlc_event_type_t event_type,
209 vlc_event_callback_t pf_callback,
211 const char * psz_debug_name )
213 vlc_event_listeners_group_t * listeners_group;
214 vlc_event_listener_t * listener;
215 listener = malloc(sizeof(vlc_event_listener_t));
219 listener->p_user_data = p_user_data;
220 listener->pf_callback = pf_callback;
222 listener->psz_debug_name = strdup( psz_debug_name );
224 (void)psz_debug_name;
227 vlc_mutex_lock( &p_em->object_lock );
228 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
229 if( listeners_group->event_type == event_type )
231 ARRAY_APPEND( listeners_group->listeners, listener );
233 msg_Dbg( p_em->p_parent_object,
234 "Listening to '%s' event with '%s' (data %p)",
235 ppsz_event_type_to_name[event_type],
236 listener->psz_debug_name,
237 listener->p_user_data );
239 vlc_mutex_unlock( &p_em->object_lock );
243 vlc_mutex_unlock( &p_em->object_lock );
245 msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
251 * Remove a callback for an event.
253 int vlc_event_detach( vlc_event_manager_t *p_em,
254 vlc_event_type_t event_type,
255 vlc_event_callback_t pf_callback,
258 vlc_event_listeners_group_t * listeners_group;
259 struct vlc_event_listener_t * listener;
261 vlc_mutex_lock( &p_em->object_lock );
262 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
263 if( listeners_group->event_type == event_type )
265 FOREACH_ARRAY( listener, listeners_group->listeners )
266 if( listener->pf_callback == pf_callback &&
267 listener->p_user_data == p_user_data )
269 /* that's our listener */
270 ARRAY_REMOVE( listeners_group->listeners,
271 fe_idx /* This comes from the macro (and that's why
274 msg_Dbg( p_em->p_parent_object,
275 "Detaching '%s' from '%s' event (data %p)",
276 listener->psz_debug_name,
277 ppsz_event_type_to_name[event_type],
278 listener->p_user_data );
280 free( listener->psz_debug_name );
283 vlc_mutex_unlock( &p_em->object_lock );
289 vlc_mutex_unlock( &p_em->object_lock );
291 msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );