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 *****************************************************************************/
33 #include <stdio.h> /* required */
34 #include <stdlib.h> /* malloc() */
38 #include <vlc_events.h>
39 #include <vlc_arrays.h>
41 /*****************************************************************************
42 * Documentation : Read vlc_events.h
43 *****************************************************************************/
45 /*****************************************************************************
47 *****************************************************************************/
49 typedef struct vlc_event_listener_t
52 vlc_event_callback_t pf_callback;
53 } vlc_event_listener_t;
55 typedef struct vlc_event_listeners_group_t
57 vlc_event_type_t event_type;
58 DECL_ARRAY(struct vlc_event_listener_t *) listeners;
59 } vlc_event_listeners_group_t;
61 /*****************************************************************************
63 *****************************************************************************/
66 * Initialize event manager object
67 * p_obj is the object that contains the event manager. But not
68 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
70 * p_parent_obj gives a libvlc instance
72 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
73 vlc_object_t * p_parent_obj )
76 vlc_mutex_init( p_parent_obj, &p_em->object_lock );
77 ARRAY_INIT( p_em->listeners_groups );
82 * Destroy the event manager
84 void vlc_event_manager_fini( vlc_event_manager_t * p_em )
86 struct vlc_event_listeners_group_t * listeners_group;
87 struct vlc_event_listener_t * listener;
89 vlc_mutex_destroy( &p_em->object_lock );
91 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
92 FOREACH_ARRAY( listener, listeners_group->listeners )
95 free( listeners_group );
100 * Destroy the event manager
102 int vlc_event_manager_register_event_type(
103 vlc_event_manager_t * p_em,
104 vlc_event_type_t event_type )
106 vlc_event_listeners_group_t * listeners_group;
107 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
109 if( !listeners_group )
112 listeners_group->event_type = event_type;
113 ARRAY_INIT( listeners_group->listeners );
115 vlc_mutex_lock( &p_em->object_lock );
116 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
117 vlc_mutex_unlock( &p_em->object_lock );
123 * Send an event to the listener attached to this p_em.
125 void vlc_event_send( vlc_event_manager_t * p_em,
126 vlc_event_t * p_event )
128 vlc_event_listeners_group_t * listeners_group;
129 vlc_event_listener_t * listener;
130 vlc_event_callback_t func = NULL;
133 /* Fill event with the sending object now */
134 p_event->p_obj = p_em->p_obj;
136 vlc_mutex_lock( &p_em->object_lock );
137 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
138 if( listeners_group->event_type == p_event->type )
140 /* We found the group, now send every one the event */
141 FOREACH_ARRAY( listener, listeners_group->listeners )
142 func = listener->pf_callback;
143 user_data = listener->p_user_data;
148 vlc_mutex_unlock( &p_em->object_lock );
150 func( p_event, user_data );
154 * Add a callback for an event.
156 int vlc_event_attach( vlc_event_manager_t * p_em,
157 vlc_event_type_t event_type,
158 vlc_event_callback_t pf_callback,
161 vlc_event_listeners_group_t * listeners_group;
162 vlc_event_listener_t * listener;
163 listener = malloc(sizeof(vlc_event_listener_t));
167 listener->p_user_data = p_user_data;
168 listener->pf_callback = pf_callback;
170 vlc_mutex_lock( &p_em->object_lock );
171 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
172 if( listeners_group->event_type == event_type )
174 ARRAY_APPEND( listeners_group->listeners, listener );
175 vlc_mutex_unlock( &p_em->object_lock );
179 vlc_mutex_unlock( &p_em->object_lock );
186 * Remove a callback for an event.
188 int vlc_event_detach( vlc_event_manager_t *p_em,
189 vlc_event_type_t event_type,
190 vlc_event_callback_t pf_callback,
193 vlc_event_listeners_group_t * listeners_group;
194 struct vlc_event_listener_t * listener;
196 vlc_mutex_lock( &p_em->object_lock );
197 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
198 if( listeners_group->event_type == event_type )
200 FOREACH_ARRAY( listener, listeners_group->listeners )
201 if( listener->pf_callback == pf_callback &&
202 listener->p_user_data == p_user_data )
204 /* that's our listener */
205 ARRAY_REMOVE( listeners_group->listeners,
206 fe_idx /* This comes from the macro (and that's why
209 vlc_mutex_unlock( &p_em->object_lock );
215 vlc_mutex_unlock( &p_em->object_lock );