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 /**************************************************************************
35 * handle_event (private)
37 * Callback from the vlc variables
38 **************************************************************************/
39 static int handle_event( vlc_object_t *p_this, char const *psz_cmd,
40 vlc_value_t oldval, vlc_value_t newval,
43 /* This is thread safe, as the var_*Callback already provide the locking
44 * facility for p_data */
45 struct libvlc_callback_entry_t *entry = p_data;
48 event.type = entry->i_event_type;
50 if (event.type == libvlc_VolumeChanged)
52 if(!strcmp(psz_cmd, "intf-change"))
54 input_thread_t * p_input = (input_thread_t *)p_this;
56 var_Get( p_input, "time", &val );
58 /* Only send event at a reasonable time precision (500ms) */
59 /* (FIXME: this should be configurable) */
60 if ((val.i_time % I64C(500000)) != 0)
62 /* Don't send this event */
66 event.u.input_position_changed.new_position = val.i_time;
69 event.u.input_position_changed.new_position = newval.i_time;
71 event.u.input_position_changed.new_position *= 1000LL;
73 else if (event.type == libvlc_InputPositionChanged)
75 event.u.volume_changed.new_volume = newval.i_int;
78 /* Call the client entry */
79 entry->f_callback( entry->p_instance, &event, entry->p_user_data );
84 /**************************************************************************
85 * get_input (private) :
87 * Utility function, Object should be released by vlc_object_release
89 **************************************************************************/
90 static input_thread_t * get_input(libvlc_instance_t * p_instance)
92 libvlc_exception_t p_e_unused; /* FIXME: error checking here */
93 libvlc_media_instance_t * p_mi;
94 input_thread_t * p_input;
96 p_mi = libvlc_playlist_get_media_instance( p_instance, &p_e_unused );
101 p_input = libvlc_get_input_thread( p_mi, &p_e_unused );
103 libvlc_media_instance_destroy( p_mi );
108 /**************************************************************************
109 * install_input_event (private) :
111 * vlc variables callback, used to install input event.
112 * Can be called manually though.
113 **************************************************************************/
114 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
115 vlc_value_t oldval, vlc_value_t newval,
118 libvlc_instance_t * p_instance = p_data;
119 struct libvlc_callback_entry_list_t *p_listitem;
120 input_thread_t * p_input = get_input( p_instance );
122 vlc_mutex_lock( &p_instance->instance_lock );
124 p_listitem = p_instance->p_callback_list;
126 for( ; p_listitem ; p_listitem = p_listitem->next )
128 if (p_listitem->elmt->i_event_type == libvlc_InputPositionChanged)
130 /* FIXME: here we shouldn't listen on intf-change, we have to provide
131 * in vlc core a more accurate callback */
132 var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
133 var_AddCallback( p_input, "time", handle_event, p_listitem->elmt );
137 vlc_mutex_unlock( &p_instance->instance_lock );
138 vlc_object_release( p_input );
142 /**************************************************************************
143 * add_callback_to_list (private) :
145 * callback list utility function.
146 **************************************************************************/
147 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
148 struct libvlc_callback_entry_list_t **list )
150 struct libvlc_callback_entry_list_t *new_listitem;
152 /* malloc/free strategy:
153 * - alloc-ded in add_callback_entry
154 * - free-ed by libvlc_event_remove_callback
155 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
156 * when entry is destroyed
158 new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
159 new_listitem->elmt = entry;
160 new_listitem->next = *list;
161 new_listitem->prev = NULL;
164 (*list)->prev = new_listitem;
166 *list = new_listitem;
169 /**************************************************************************
170 * remove_variable_callback (private) :
172 * Delete the appropriate vlc variables callback for an event.
173 **************************************************************************/
174 static int remove_variable_callback( libvlc_instance_t *p_instance,
175 struct libvlc_callback_entry_t * p_entry )
177 input_thread_t * p_input = get_input( p_instance );
178 int res = VLC_SUCCESS;
180 /* Note: Appropriate lock should be held by the caller */
182 switch ( p_entry->i_event_type )
184 case libvlc_VolumeChanged:
185 res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
186 handle_event, p_entry );
188 case libvlc_InputPositionChanged:
189 /* We may not be deleting the right p_input callback, in this case this
191 var_DelCallback( p_input, "intf-change", handle_event, p_entry );
192 var_DelCallback( p_input, "position", handle_event, p_entry );
197 vlc_object_release( p_input );
203 * Internal libvlc functions
206 /**************************************************************************
207 * libvlc_event_init (internal) :
209 * initialization function.
210 **************************************************************************/
211 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
213 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
216 RAISEVOID ("Can't listen to input event");
218 /* Install a Callback for input changes, so
219 * so we can track input event */
220 var_AddCallback( p_playlist, "playlist-current",
221 install_input_event, p_instance );
224 /**************************************************************************
225 * libvlc_event_fini (internal) :
227 * finalization function.
228 **************************************************************************/
229 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
231 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
232 libvlc_exception_t p_e_unused;
234 libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
237 RAISEVOID ("Can't unregister input events");
239 var_DelCallback( p_playlist, "playlist-current",
240 install_input_event, p_instance );
244 * Public libvlc functions
247 /**************************************************************************
248 * libvlc_event_add_callback (public) :
250 * Add a callback for an event.
251 **************************************************************************/
252 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
253 libvlc_event_type_t i_event_type,
254 libvlc_callback_t f_callback,
256 libvlc_exception_t *p_e )
258 struct libvlc_callback_entry_t *entry;
259 vlc_value_t unused1, unused2;
260 int res = VLC_SUCCESS;
263 RAISEVOID (" Callback function is null ");
265 /* malloc/free strategy:
266 * - alloc-ded in libvlc_event_add_callback
267 * - free-ed by libvlc_event_add_callback on error
268 * - free-ed by libvlc_event_remove_callback
269 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
270 * when entry is destroyed
272 entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
273 entry->f_callback = f_callback;
274 entry->i_event_type = i_event_type;
275 entry->p_user_data = user_data;
277 switch ( i_event_type )
279 case libvlc_VolumeChanged:
280 res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
281 handle_event, entry );
283 case libvlc_InputPositionChanged:
284 install_input_event( NULL, NULL, unused1, unused2, p_instance);
288 RAISEVOID( "Unsupported event." );
291 if (res != VLC_SUCCESS)
294 RAISEVOID("Internal callback registration was not successful. Callback not registered.");
297 vlc_mutex_lock( &p_instance->instance_lock );
298 add_callback_to_list( entry, &p_instance->p_callback_list );
299 vlc_mutex_unlock( &p_instance->instance_lock );
304 /**************************************************************************
305 * libvlc_event_remove_callback (public) :
307 * Remove a callback for an event.
308 **************************************************************************/
309 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
310 libvlc_event_type_t i_event_type,
311 libvlc_callback_t f_callback,
313 libvlc_exception_t *p_e )
315 struct libvlc_callback_entry_list_t *p_listitem;
317 vlc_mutex_lock( &p_instance->instance_lock );
319 p_listitem = p_instance->p_callback_list;
323 if( p_listitem->elmt->f_callback == f_callback
324 && ( p_listitem->elmt->i_event_type == i_event_type )
325 && ( p_listitem->elmt->p_user_data == p_user_data )
329 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
331 if( p_listitem->prev )
332 p_listitem->prev->next = p_listitem->next;
334 p_instance->p_callback_list = p_listitem->next;
337 p_listitem->next->prev = p_listitem->prev;
339 free( p_listitem->elmt );
345 p_listitem = p_listitem->next;
347 vlc_mutex_unlock( &p_instance->instance_lock );
350 /**************************************************************************
351 * libvlc_event_remove_all_callbacks (public) :
353 * Remove all callbacks for all events.
354 **************************************************************************/
355 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
356 libvlc_exception_t *p_e )
358 struct libvlc_callback_entry_list_t *p_listitem;
360 vlc_mutex_lock( &p_instance->instance_lock );
362 p_listitem = p_instance->p_callback_list;
366 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
367 p_listitem = p_listitem->next;
370 p_instance->p_callback_list = NULL;
372 vlc_mutex_unlock( &p_instance->instance_lock );