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 == INPUT_POSITION_CHANGED && !strcmp(psz_cmd, "intf-change"))
47 input_thread_t * p_input = (input_thread_t *)p_this;
49 var_Get( p_input, "position", &val );
51 /* Only send event at a reasonable time precision (500ms) */
52 /* (FIXME: this should be configurable) */
53 if ((val.i_time % I64C(500000)) != 0)
55 /* Don't send this event */
60 /* Call the client entry */
61 entry->f_callback( entry->p_instance, &event, entry->p_user_data );
66 /* Utility function: Object should be released by vlc_object_release afterwards */
67 static input_thread_t * get_input(libvlc_instance_t * p_instance)
69 libvlc_exception_t p_e_unused; /* FIXME: error checking here */
70 libvlc_input_t * p_libvlc_input = libvlc_playlist_get_input( p_instance, &p_e_unused );
71 input_thread_t * p_input;
76 p_input = libvlc_get_input_thread( p_libvlc_input, &p_e_unused );
78 libvlc_input_free(p_libvlc_input);
83 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
84 vlc_value_t oldval, vlc_value_t newval,
87 libvlc_instance_t * p_instance = p_data;
88 struct libvlc_callback_entry_list_t *p_listitem;
89 input_thread_t * p_input = get_input( p_instance );
91 vlc_mutex_lock( &p_instance->instance_lock );
93 p_listitem = p_instance->p_callback_list;
95 for( ; p_listitem ; p_listitem = p_listitem->next )
97 if (p_listitem->elmt->i_event_type == INPUT_POSITION_CHANGED)
99 /* FIXME: here we shouldn't listen on intf-change, we have to provide
100 * in vlc core a more accurate callback */
101 var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
102 var_AddCallback( p_input, "position", handle_event, p_listitem->elmt );
106 vlc_mutex_unlock( &p_instance->instance_lock );
107 vlc_object_release( p_input );
111 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
112 struct libvlc_callback_entry_list_t **list )
114 struct libvlc_callback_entry_list_t *new_listitem;
116 /* malloc/free strategy:
117 * - alloc-ded in add_callback_entry
118 * - free-ed by libvlc_event_remove_callback
119 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
120 * when entry is destroyed
122 new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
123 new_listitem->elmt = entry;
124 new_listitem->next = *list;
125 new_listitem->prev = NULL;
128 (*list)->prev = new_listitem;
130 *list = new_listitem;
133 static int remove_variable_callback( libvlc_instance_t *p_instance,
134 struct libvlc_callback_entry_t * p_entry )
136 input_thread_t * p_input = get_input( p_instance );
137 int res = VLC_SUCCESS;
139 /* Note: Appropriate lock should be held by the caller */
141 switch ( p_entry->i_event_type )
144 res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
145 handle_event, p_entry );
147 case INPUT_POSITION_CHANGED:
148 /* We may not be deleting the right p_input callback, in this case this
150 var_DelCallback( p_input, "intf-change",
151 handle_event, p_entry );
152 var_DelCallback( p_input, "position",
153 handle_event, p_entry );
158 vlc_object_release( p_input );
164 * Internal libvlc functions
166 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
168 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
171 RAISEVOID ("Can't listen to input event");
173 /* Install a Callback for input changes, so
174 * so we can track input event */
175 var_AddCallback( p_playlist, "playlist-current",
176 install_input_event, p_instance );
179 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
181 playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
182 libvlc_exception_t p_e_unused;
184 libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
187 RAISEVOID ("Can't unregister input events");
189 var_DelCallback( p_playlist, "playlist-current",
190 install_input_event, p_instance );
194 * Public libvlc functions
197 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
198 libvlc_event_type_t i_event_type,
199 libvlc_callback_t f_callback,
201 libvlc_exception_t *p_e )
203 struct libvlc_callback_entry_t *entry;
204 vlc_value_t unused1, unused2;
205 int res = VLC_SUCCESS;
208 RAISEVOID (" Callback function is null ");
210 /* malloc/free strategy:
211 * - alloc-ded in libvlc_event_add_callback
212 * - free-ed by libvlc_event_add_callback on error
213 * - free-ed by libvlc_event_remove_callback
214 * - free-ed in libvlc_destroy threw libvlc_event_remove_callback
215 * when entry is destroyed
217 entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
218 entry->f_callback = f_callback;
219 entry->i_event_type = i_event_type;
220 entry->p_user_data = user_data;
222 switch ( i_event_type )
225 res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
226 handle_event, entry );
228 case INPUT_POSITION_CHANGED:
229 install_input_event( NULL, NULL, unused1, unused2, p_instance);
233 RAISEVOID( "Unsupported event." );
236 if (res != VLC_SUCCESS)
239 RAISEVOID("Internal callback registration was not successful. Callback not registered.");
242 vlc_mutex_lock( &p_instance->instance_lock );
243 add_callback_to_list( entry, &p_instance->p_callback_list );
244 vlc_mutex_unlock( &p_instance->instance_lock );
249 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
250 libvlc_exception_t *p_e )
252 struct libvlc_callback_entry_list_t *p_listitem;
254 vlc_mutex_lock( &p_instance->instance_lock );
256 p_listitem = p_instance->p_callback_list;
260 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
261 p_listitem = p_listitem->next;
264 p_instance->p_callback_list = NULL;
266 vlc_mutex_unlock( &p_instance->instance_lock );
269 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
270 libvlc_event_type_t i_event_type,
271 libvlc_callback_t f_callback,
273 libvlc_exception_t *p_e )
275 struct libvlc_callback_entry_list_t *p_listitem;
277 vlc_mutex_lock( &p_instance->instance_lock );
279 p_listitem = p_instance->p_callback_list;
283 if( p_listitem->elmt->f_callback == f_callback
284 && ( p_listitem->elmt->i_event_type == i_event_type )
285 && ( p_listitem->elmt->p_user_data == p_user_data )
289 remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
291 if( p_listitem->prev )
292 p_listitem->prev->next = p_listitem->next;
294 p_instance->p_callback_list = p_listitem->next;
297 p_listitem->next->prev = p_listitem->prev;
299 free( p_listitem->elmt );
305 p_listitem = p_listitem->next;
307 vlc_mutex_unlock( &p_instance->instance_lock );