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_this 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
71 int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj )
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 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
87 FOREACH_ARRAY( listener, listeners_group->listeners )
90 free( listeners_group );
95 * Destroy the event manager
97 int vlc_event_manager_register_event_type(
98 vlc_event_manager_t * p_em,
99 vlc_event_type_t event_type )
101 vlc_event_listeners_group_t * listeners_group;
102 listeners_group = malloc(sizeof(vlc_event_listeners_group_t));
104 if( !listeners_group )
107 listeners_group->event_type = event_type;
108 ARRAY_INIT( listeners_group->listeners );
110 ARRAY_APPEND( p_em->listeners_groups, listeners_group );
116 * Send an event to the listener attached to this p_em.
118 void vlc_event_send( vlc_event_manager_t * p_em,
119 vlc_event_t * p_event )
121 vlc_event_listeners_group_t * listeners_group;
122 vlc_event_listener_t * listener;
124 /* Fill event with the sending object now */
125 p_event->p_obj = p_em->p_obj;
127 FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
128 if( listeners_group->event_type == p_event->type )
130 /* We found the group, now send every one the event */
131 FOREACH_ARRAY( listener, listeners_group->listeners )
132 listener->pf_callback( p_event, listener->p_user_data );
140 * Add a callback for an event.
142 int vlc_event_attach( vlc_event_manager_t * p_event_manager,
143 vlc_event_type_t event_type,
144 vlc_event_callback_t pf_callback,
147 vlc_event_listeners_group_t * listeners_group;
148 vlc_event_listener_t * listener;
149 listener = malloc(sizeof(vlc_event_listener_t));
153 listener->p_user_data = p_user_data;
154 listener->pf_callback = pf_callback;
156 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
157 if( listeners_group->event_type == event_type )
159 ARRAY_APPEND( listeners_group->listeners, listener );
169 * Remove a callback for an event.
171 int vlc_event_detach( vlc_event_manager_t *p_event_manager,
172 vlc_event_type_t event_type,
173 vlc_event_callback_t pf_callback,
176 vlc_event_listeners_group_t * listeners_group;
177 struct vlc_event_listener_t * listener;
178 FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
179 if( listeners_group->event_type == event_type )
181 FOREACH_ARRAY( listener, listeners_group->listeners )
182 if( listener->pf_callback == pf_callback &&
183 listener->p_user_data == p_user_data )
185 /* that's our listener */
187 ARRAY_REMOVE( listeners_group->listeners,
188 fe_idx /* This comes from the macro (and that's why