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;
43 event.type = entry->i_event_type;
45 if (event.type == libvlc_VolumeChanged)
47 if(!strcmp(psz_cmd, "intf-change"))
49 input_thread_t * p_input = (input_thread_t *)p_this;
51 var_Get( p_input, "time", &val );
53 /* Only send event at a reasonable time precision (500ms) */
54 /* (FIXME: this should be configurable) */
55 if ((val.i_time % I64C(500000)) != 0)
57 /* Don't send this event */
61 event.u.input_position_changed.new_position = val.i_time;
64 event.u.input_position_changed.new_position = newval.i_time;
66 event.u.input_position_changed.new_position *= 1000LL;
68 else if (event.type == libvlc_InputPositionChanged)
70 event.u.volume_changed.new_volume = newval.i_int;
73 /* Call the client entry */
74 entry->f_callback( entry->p_instance, &event, entry->p_user_data );
79 /* Utility function: Object should be released by vlc_object_release afterwards */
80 static input_thread_t * get_input(libvlc_instance_t * p_instance)
82 libvlc_exception_t p_e_unused; /* FIXME: error checking here */
83 libvlc_input_t * p_libvlc_input = libvlc_playlist_get_input( p_instance, &p_e_unused );
84 input_thread_t * p_input;
89 p_input = libvlc_get_input_thread( p_libvlc_input, &p_e_unused );
91 libvlc_input_free(p_libvlc_input);
96 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
97 vlc_value_t oldval, vlc_value_t newval,
100 libvlc_instance_t * p_instance = p_data;
101 struct libvlc_callback_entry_list_t *p_listitem;
102 input_thread_t * p_input = get_input( p_instance );
104 vlc_mutex_lock( &p_instance->instance_lock );
106 p_listitem = p_instance->p_callback_list;
108 for( ; p_listitem ; p_listitem = p_listitem->next )
110 if (p_listitem->elmt->i_event_type == libvlc_InputPositionChanged)
112 /* FIXME: here we shouldn't listen on intf-change, we have to provide
113 * in vlc core a more accurate callback */
114 var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
115 var_AddCallback( p_input, "time", handle_event, p_listitem->elmt );
119 vlc_mutex_unlock( &p_instance->instance_lock );
120 vlc_object_release( p_input );
124 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
125 struct libvlc_callback_entry_list_t **list )
127 struct libvlc_callback_entry_list_t *new_listitem;
129 /* malloc/free strategy:
130 * - alloc-ded in add_callback_entry
131 * - free-ed by libvlc_event_remove_callback
132 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
133 * when entry is destroyed
135 new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
136 new_listitem->elmt = entry;
137 new_listitem->next = *list;
138 new_listitem->prev = NULL;
141 (*list)->prev = new_listitem;
143 *list = new_listitem;
146 static int remove_variable_callback( libvlc_instance_t *p_instance,
147 struct libvlc_callback_entry_t * p_entry )
149 input_thread_t * p_input = get_input( p_instance );
150 int res = VLC_SUCCESS;
152 /* Note: Appropriate lock should be held by the caller */
154 switch ( p_entry->i_event_type )
156 case libvlc_VolumeChanged:
157 res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
158 handle_event, p_entry );
160 case libvlc_InputPositionChanged:
161 /* We may not be deleting the right p_input callback, in this case this
163 var_DelCallback( p_input, "intf-change", handle_event, p_entry );
164 var_DelCallback( p_input, "position", handle_event, p_entry );
169 vlc_object_release( p_input );
175 * Internal libvlc functions
177 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
179 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
182 RAISEVOID ("Can't listen to input event");
184 /* Install a Callback for input changes, so
185 * so we can track input event */
186 var_AddCallback( p_playlist, "playlist-current",
187 install_input_event, p_instance );
190 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
192 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
193 libvlc_exception_t p_e_unused;
195 libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
198 RAISEVOID ("Can't unregister input events");
200 var_DelCallback( p_playlist, "playlist-current",
201 install_input_event, p_instance );
205 * Public libvlc functions
208 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
209 libvlc_event_type_t i_event_type,
210 libvlc_callback_t f_callback,
212 libvlc_exception_t *p_e )
214 struct libvlc_callback_entry_t *entry;
215 vlc_value_t unused1, unused2;
216 int res = VLC_SUCCESS;
219 RAISEVOID (" Callback function is null ");
221 /* malloc/free strategy:
222 * - alloc-ded in libvlc_event_add_callback
223 * - free-ed by libvlc_event_add_callback on error
224 * - free-ed by libvlc_event_remove_callback
225 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
226 * when entry is destroyed
228 entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
229 entry->f_callback = f_callback;
230 entry->i_event_type = i_event_type;
231 entry->p_user_data = user_data;
233 switch ( i_event_type )
235 case libvlc_VolumeChanged:
236 res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
237 handle_event, entry );
239 case libvlc_InputPositionChanged:
240 install_input_event( NULL, NULL, unused1, unused2, p_instance);
244 RAISEVOID( "Unsupported event." );
247 if (res != VLC_SUCCESS)
250 RAISEVOID("Internal callback registration was not successful. Callback not registered.");
253 vlc_mutex_lock( &p_instance->instance_lock );
254 add_callback_to_list( entry, &p_instance->p_callback_list );
255 vlc_mutex_unlock( &p_instance->instance_lock );
260 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
261 libvlc_exception_t *p_e )
263 struct libvlc_callback_entry_list_t *p_listitem;
265 vlc_mutex_lock( &p_instance->instance_lock );
267 p_listitem = p_instance->p_callback_list;
271 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
272 p_listitem = p_listitem->next;
275 p_instance->p_callback_list = NULL;
277 vlc_mutex_unlock( &p_instance->instance_lock );
280 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
281 libvlc_event_type_t i_event_type,
282 libvlc_callback_t f_callback,
284 libvlc_exception_t *p_e )
286 struct libvlc_callback_entry_list_t *p_listitem;
288 vlc_mutex_lock( &p_instance->instance_lock );
290 p_listitem = p_instance->p_callback_list;
294 if( p_listitem->elmt->f_callback == f_callback
295 && ( p_listitem->elmt->i_event_type == i_event_type )
296 && ( p_listitem->elmt->p_user_data == p_user_data )
300 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
302 if( p_listitem->prev )
303 p_listitem->prev->next = p_listitem->next;
305 p_instance->p_callback_list = p_listitem->next;
308 p_listitem->next->prev = p_listitem->prev;
310 free( p_listitem->elmt );
316 p_listitem = p_listitem->next;
318 vlc_mutex_unlock( &p_instance->instance_lock );