1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_playlist.h>
34 static int handle_event( vlc_object_t *p_this, char const *psz_cmd,
35 vlc_value_t oldval, vlc_value_t newval,
38 /* This is thread safe, as the var_*Callback already provide the locking
39 * facility for p_data */
40 struct libvlc_callback_entry_t *entry = p_data;
42 event.type = entry->i_event_type;
46 event.value_type = BOOLEAN_EVENT;
48 case INPUT_POSITION_CHANGED:
53 event.old_value = oldval;
54 event.new_value = newval;
56 /* Call the client entry */
57 entry->f_callback( entry->p_instance, &event, entry->p_user_data );
62 /* Utility function: Object should be released by vlc_object_release afterwards */
63 static input_thread_t * get_input(libvlc_instance_t * p_instance)
65 libvlc_exception_t p_e_unused; /* FIXME: error checking here */
66 libvlc_input_t * p_libvlc_input = libvlc_playlist_get_input( p_instance, &p_e_unused );
67 input_thread_t * p_input;
72 p_input = libvlc_get_input_thread( p_libvlc_input, &p_e_unused );
74 libvlc_input_free(p_libvlc_input);
79 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
80 vlc_value_t oldval, vlc_value_t newval,
83 libvlc_instance_t * p_instance = p_data;
84 struct libvlc_callback_entry_list_t *p_listitem;
85 input_thread_t * p_input = get_input( p_instance );
87 vlc_mutex_lock( &p_instance->instance_lock );
89 p_listitem = p_instance->p_callback_list;
91 for( ; p_listitem ; p_listitem = p_listitem->next )
93 if (p_listitem->elmt->i_event_type == INPUT_POSITION_CHANGED)
95 /* FIXME: here we shouldn't listen on intf-change, we have to provide
96 * in vlc core a more accurate callback */
97 var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
101 vlc_mutex_unlock( &p_instance->instance_lock );
102 vlc_object_release( p_input );
106 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
107 struct libvlc_callback_entry_list_t **list )
109 struct libvlc_callback_entry_list_t *new_listitem;
111 /* malloc/free strategy:
112 * - alloc-ded in add_callback_entry
113 * - free-ed by libvlc_event_remove_callback
114 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
115 * when entry is destroyed
117 new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
118 new_listitem->elmt = entry;
119 new_listitem->next = *list;
120 new_listitem->prev = NULL;
123 (*list)->prev = new_listitem;
125 *list = new_listitem;
128 static int remove_variable_callback( libvlc_instance_t *p_instance,
129 struct libvlc_callback_entry_t * p_entry )
131 input_thread_t * p_input = get_input( p_instance );
132 int res = VLC_SUCCESS;
134 /* Note: Appropriate lock should be held by the caller */
136 switch ( p_entry->i_event_type )
139 res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
140 handle_event, p_entry );
142 case INPUT_POSITION_CHANGED:
143 /* We may not be deleting the right p_input callback, in this case this
145 var_DelCallback( p_input, "intf-change",
146 handle_event, p_entry );
151 vlc_object_release( p_input );
157 * Internal libvlc functions
159 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
161 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
164 RAISEVOID ("Can't listen to input event");
166 /* Install a Callback for input changes, so
167 * so we can track input event */
168 var_AddCallback( p_playlist, "playlist-current",
169 install_input_event, p_instance );
172 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
174 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
175 libvlc_exception_t p_e_unused;
177 libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
180 RAISEVOID ("Can't unregister input events");
182 var_DelCallback( p_playlist, "playlist-current",
183 install_input_event, p_instance );
187 * Public libvlc functions
190 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
191 libvlc_event_type_t i_event_type,
192 libvlc_callback_t f_callback,
194 libvlc_exception_t *p_e )
196 struct libvlc_callback_entry_t *entry;
197 vlc_value_t unused1, unused2;
198 int res = VLC_SUCCESS;
201 RAISEVOID (" Callback function is null ");
203 /* malloc/free strategy:
204 * - alloc-ded in libvlc_event_add_callback
205 * - free-ed by libvlc_event_add_callback on error
206 * - free-ed by libvlc_event_remove_callback
207 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
208 * when entry is destroyed
210 entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
211 entry->f_callback = f_callback;
212 entry->i_event_type = i_event_type;
213 entry->p_user_data = user_data;
215 switch ( i_event_type )
218 res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
219 handle_event, entry );
221 case INPUT_POSITION_CHANGED:
222 install_input_event( NULL, NULL, unused1, unused2, p_instance);
226 RAISEVOID( "Unsupported event." );
229 if (res != VLC_SUCCESS)
232 RAISEVOID("Internal callback registration was not successful. Callback not registered.");
235 vlc_mutex_lock( &p_instance->instance_lock );
236 add_callback_to_list( entry, &p_instance->p_callback_list );
237 vlc_mutex_unlock( &p_instance->instance_lock );
242 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
243 libvlc_exception_t *p_e )
245 struct libvlc_callback_entry_list_t *p_listitem;
247 vlc_mutex_lock( &p_instance->instance_lock );
249 p_listitem = p_instance->p_callback_list;
253 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
254 p_listitem = p_listitem->next;
257 p_instance->p_callback_list = NULL;
259 vlc_mutex_unlock( &p_instance->instance_lock );
262 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
263 libvlc_event_type_t i_event_type,
264 libvlc_callback_t f_callback,
266 libvlc_exception_t *p_e )
268 struct libvlc_callback_entry_list_t *p_listitem;
270 vlc_mutex_lock( &p_instance->instance_lock );
272 p_listitem = p_instance->p_callback_list;
276 if( p_listitem->elmt->f_callback == f_callback
277 && ( p_listitem->elmt->i_event_type == i_event_type )
278 && ( p_listitem->elmt->p_user_data == p_user_data )
282 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
284 if( p_listitem->prev )
285 p_listitem->prev->next = p_listitem->next;
287 p_instance->p_callback_list = p_listitem->next;
290 p_listitem->next->prev = p_listitem->prev;
292 free( p_listitem->elmt );
298 p_listitem = p_listitem->next;
300 vlc_mutex_unlock( &p_instance->instance_lock );