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 *****************************************************************************/
42 /*****************************************************************************
44 *****************************************************************************/
46 typedef struct vlc_event_listener_t
49 vlc_event_callback_t pf_callback;
50 } vlc_event_listener_t;
52 typedef struct vlc_event_listeners_group_t
54 vlc_event_type_t event_type;
55 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
56 } vlc_event_listeners_group_t;
58 /*****************************************************************************
60 *****************************************************************************/
63 * Initialize event manager object
64 * p_obj is the object that contains the event manager. But not
65 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
67 * p_parent_obj gives a libvlc instance
69 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
70 vlc_object_t * p_parent_obj )
73 vlc_mutex_init( p_parent_obj, &p_em->object_lock );
74 ARRAY_INIT( p_em->listeners_groups );
79 * Destroy the event manager
81 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
83 struct vlc_event_listeners_group_t * listeners_group;
84 struct vlc_event_listener_t * listener;
86 vlc_mutex_destroy( &p_em->object_lock );
88 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
89 FOREACH_ARRAY( listener, listeners_group->listeners )
92 free( listeners_group );
97 * Destroy the event manager
99 int vlc_event_manager_register_event_type(
100 vlc_event_manager_t * p_em,
101 vlc_event_type_t event_type )
103 vlc_event_listeners_group_t * listeners_group;
104 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
106 if( !listeners_group )
109 listeners_group->event_type = event_type;
110 ARRAY_INIT( listeners_group->listeners );
112 vlc_mutex_lock( &p_em->object_lock );
113 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
114 vlc_mutex_unlock( &p_em->object_lock );
120 * Send an event to the listener attached to this p_em.
122 void vlc_event_send( vlc_event_manager_t * p_em,
123 vlc_event_t * p_event )
125 vlc_event_listeners_group_t * listeners_group;
126 vlc_event_listener_t * listener;
127 vlc_event_callback_t func = NULL;
128 void * user_data = NULL;
130 /* Fill event with the sending object now */
131 p_event->p_obj = p_em->p_obj;
133 vlc_mutex_lock( &p_em->object_lock );
134 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
135 if( listeners_group->event_type == p_event->type )
137 /* We found the group, now send every one the event */
138 FOREACH_ARRAY( listener, listeners_group->listeners )
139 func = listener->pf_callback;
140 user_data = listener->p_user_data;
141 /* This is safe to do that because we are sure
142 * that there will be no object owned references
143 * used after the lock. */
144 vlc_mutex_unlock( &p_em->object_lock );
145 func( p_event, user_data );
146 vlc_mutex_lock( &p_em->object_lock );
151 vlc_mutex_unlock( &p_em->object_lock );
155 * Add a callback for an event.
157 int vlc_event_attach( vlc_event_manager_t * p_em,
158 vlc_event_type_t event_type,
159 vlc_event_callback_t pf_callback,
162 vlc_event_listeners_group_t * listeners_group;
163 vlc_event_listener_t * listener;
164 listener = malloc(sizeof(vlc_event_listener_t));
168 listener->p_user_data = p_user_data;
169 listener->pf_callback = pf_callback;
171 vlc_mutex_lock( &p_em->object_lock );
172 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
173 if( listeners_group->event_type == event_type )
175 ARRAY_APPEND( listeners_group->listeners, listener );
176 vlc_mutex_unlock( &p_em->object_lock );
180 vlc_mutex_unlock( &p_em->object_lock );
187 * Remove a callback for an event.
189 int vlc_event_detach( vlc_event_manager_t *p_em,
190 vlc_event_type_t event_type,
191 vlc_event_callback_t pf_callback,
194 vlc_event_listeners_group_t * listeners_group;
195 struct vlc_event_listener_t * listener;
197 vlc_mutex_lock( &p_em->object_lock );
198 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
199 if( listeners_group->event_type == event_type )
201 FOREACH_ARRAY( listener, listeners_group->listeners )
202 if( listener->pf_callback == pf_callback &&
203 listener->p_user_data == p_user_data )
205 /* that's our listener */
206 ARRAY_REMOVE( listeners_group->listeners,
207 fe_idx /* This comes from the macro (and that's why
210 vlc_mutex_unlock( &p_em->object_lock );
216 vlc_mutex_unlock( &p_em->object_lock );