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 free( listeners_group );
113 * Destroy the event manager
115 int vlc_event_manager_register_event_type(
116 vlc_event_manager_t * p_em,
117 vlc_event_type_t event_type )
119 vlc_event_listeners_group_t * listeners_group;
120 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
122 if( !listeners_group )
125 listeners_group->event_type = event_type;
126 ARRAY_INIT( listeners_group->listeners );
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 );
136 * Send an event to the listener attached to this p_em.
138 void vlc_event_send( vlc_event_manager_t * p_em,
139 vlc_event_t * p_event )
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;
146 /* Fill event with the sending object now */
147 p_event->p_obj = p_em->p_obj;
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 )
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;
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 );
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 );
173 vlc_mutex_unlock( &p_em->object_lock );
177 * Add a callback for an event.
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,
183 const char * psz_debug_name )
185 vlc_event_listeners_group_t * listeners_group;
186 vlc_event_listener_t * listener;
187 listener = malloc(sizeof(vlc_event_listener_t));
191 listener->p_user_data = p_user_data;
192 listener->pf_callback = pf_callback;
194 listener->psz_debug_name = strdup( psz_debug_name );
196 (void)psz_debug_name;
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 )
203 ARRAY_APPEND( listeners_group->listeners, listener );
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 );
210 vlc_mutex_unlock( &p_em->object_lock );
214 vlc_mutex_unlock( &p_em->object_lock );
216 msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
222 * Remove a callback for an event.
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,
229 vlc_event_listeners_group_t * listeners_group;
230 struct vlc_event_listener_t * listener;
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 )
236 FOREACH_ARRAY( listener, listeners_group->listeners )
237 if( listener->pf_callback == pf_callback &&
238 listener->p_user_data == p_user_data )
240 /* that's our listener */
241 ARRAY_REMOVE( listeners_group->listeners,
242 fe_idx /* This comes from the macro (and that's why
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 );
250 free( listener->psz_debug_name );
253 vlc_mutex_unlock( &p_em->object_lock );
259 vlc_mutex_unlock( &p_em->object_lock );
261 msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );