]> git.sesse.net Git - vlc/blob - src/control/event.c
control/event.c: Add some comments.
[vlc] / src / control / event.c
1 /*****************************************************************************
2  * event.c: New libvlc event control API
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id $
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_playlist.h>
28
29
30 /*
31  * Private functions
32  */
33
34 /**************************************************************************
35  *       handle_event (private)
36  *
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,
41                          void *p_data )
42 {
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;
46     libvlc_event_t event;
47
48     event.type = entry->i_event_type;    
49
50     if (event.type == libvlc_VolumeChanged)
51     {
52         if(!strcmp(psz_cmd, "intf-change"))
53         {
54             input_thread_t * p_input = (input_thread_t *)p_this;
55             vlc_value_t val;
56             var_Get( p_input, "time", &val );
57
58             /* Only send event at a reasonable time precision (500ms) */
59             /* (FIXME: this should be configurable) */
60             if ((val.i_time % I64C(500000)) != 0)
61             {
62                 /* Don't send this event */
63                 return VLC_SUCCESS;
64             }
65
66             event.u.input_position_changed.new_position = val.i_time;
67         }
68         else
69             event.u.input_position_changed.new_position = newval.i_time;
70
71         event.u.input_position_changed.new_position *= 1000LL;
72     }
73     else if (event.type == libvlc_InputPositionChanged)
74     {
75         event.u.volume_changed.new_volume = newval.i_int;
76     }
77
78     /* Call the client entry */
79     entry->f_callback( entry->p_instance, &event, entry->p_user_data );
80
81     return VLC_SUCCESS;
82 }
83
84 /**************************************************************************
85  *       get_input (private) :
86  *
87  * Utility function, Object should be released by vlc_object_release
88  * afterwards
89  **************************************************************************/
90 static input_thread_t * get_input(libvlc_instance_t * p_instance)
91 {
92     libvlc_exception_t p_e_unused; /* FIXME: error checking here */
93     libvlc_input_t * p_libvlc_input = libvlc_playlist_get_input( p_instance, &p_e_unused );
94     input_thread_t * p_input;
95
96     if( !p_libvlc_input )
97         return NULL;
98     
99     p_input = libvlc_get_input_thread( p_libvlc_input, &p_e_unused );
100
101     libvlc_input_free(p_libvlc_input);
102
103     return p_input;
104 }
105
106 /**************************************************************************
107  *       install_input_event (private) :
108  *
109  * vlc variables callback, used to install input event.
110  * Can be called manually though.
111  **************************************************************************/
112 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
113                                 vlc_value_t oldval, vlc_value_t newval,
114                                 void *p_data )
115 {
116     libvlc_instance_t * p_instance = p_data;
117     struct libvlc_callback_entry_list_t *p_listitem;
118     input_thread_t * p_input = get_input( p_instance );
119
120     vlc_mutex_lock( &p_instance->instance_lock );
121
122     p_listitem = p_instance->p_callback_list;
123
124     for( ; p_listitem ; p_listitem = p_listitem->next )
125     {
126         if (p_listitem->elmt->i_event_type == libvlc_InputPositionChanged)
127         {
128             /* FIXME: here we shouldn't listen on intf-change, we have to provide
129              * in vlc core a more accurate callback */
130             var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
131             var_AddCallback( p_input, "time", handle_event, p_listitem->elmt );
132         }
133     }
134
135     vlc_mutex_unlock( &p_instance->instance_lock );
136     vlc_object_release( p_input );
137     return VLC_SUCCESS;
138 }
139
140 /**************************************************************************
141  *       add_callback_to_list (private) :
142  *
143  * callback list utility function.
144  **************************************************************************/
145 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
146                                          struct libvlc_callback_entry_list_t **list )
147 {
148     struct libvlc_callback_entry_list_t *new_listitem;
149
150     /* malloc/free strategy:
151      *  - alloc-ded in add_callback_entry
152      *  - free-ed by libvlc_event_remove_callback
153      *  - free-ed in libvlc_destroy threw libvlc_event_remove_callback
154      *    when entry is destroyed
155      */
156     new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
157     new_listitem->elmt = entry;
158     new_listitem->next = *list;
159     new_listitem->prev = NULL;
160
161     if(*list)
162         (*list)->prev = new_listitem;
163
164     *list = new_listitem;
165 }
166
167 /**************************************************************************
168  *       remove_variable_callback (private) :
169  *
170  * Delete the appropriate vlc variables callback for an event.
171  **************************************************************************/
172 static int remove_variable_callback( libvlc_instance_t *p_instance, 
173                                      struct libvlc_callback_entry_t * p_entry )
174 {
175     input_thread_t * p_input = get_input( p_instance );
176     int res = VLC_SUCCESS;
177
178     /* Note: Appropriate lock should be held by the caller */
179
180     switch ( p_entry->i_event_type )
181     {
182         case libvlc_VolumeChanged:
183             res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
184                              handle_event, p_entry );
185             break;
186         case libvlc_InputPositionChanged:
187             /* We may not be deleting the right p_input callback, in this case this
188              * will be a no-op */
189             var_DelCallback( p_input, "intf-change", handle_event, p_entry );
190             var_DelCallback( p_input, "position", handle_event, p_entry );
191             break;
192     }
193     
194     if (p_input)
195         vlc_object_release( p_input );
196
197     return res;
198 }
199
200 /*
201  * Internal libvlc functions
202  */
203
204 /**************************************************************************
205  *       libvlc_event_init (internal) :
206  *
207  * initialization function.
208  **************************************************************************/
209 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
210 {
211     playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
212
213     if( !p_playlist )
214         RAISEVOID ("Can't listen to input event");
215
216     /* Install a Callback for input changes, so
217      * so we can track input event */
218      var_AddCallback( p_playlist, "playlist-current",
219                       install_input_event, p_instance );
220 }
221
222 /**************************************************************************
223  *       libvlc_event_fini (internal) :
224  *
225  * finalization function.
226  **************************************************************************/
227 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
228 {
229     playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
230     libvlc_exception_t p_e_unused;
231
232     libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
233
234     if( !p_playlist )
235         RAISEVOID ("Can't unregister input events");
236
237     var_DelCallback( p_playlist, "playlist-current",
238                      install_input_event, p_instance );
239 }
240
241 /*
242  * Public libvlc functions
243  */
244
245 /**************************************************************************
246  *       libvlc_event_add_callback (public) :
247  *
248  * Add a callback for an event.
249  **************************************************************************/
250 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
251                                 libvlc_event_type_t i_event_type,
252                                 libvlc_callback_t f_callback,
253                                 void *user_data,
254                                 libvlc_exception_t *p_e )
255 {
256     struct libvlc_callback_entry_t *entry;
257     vlc_value_t unused1, unused2;
258     int res = VLC_SUCCESS;
259
260     if ( !f_callback )
261         RAISEVOID (" Callback function is null ");
262
263     /* malloc/free strategy:
264      *  - alloc-ded in libvlc_event_add_callback
265      *  - free-ed by libvlc_event_add_callback on error
266      *  - free-ed by libvlc_event_remove_callback
267      *  - free-ed in libvlc_destroy threw libvlc_event_remove_callback
268      *    when entry is destroyed
269      */
270     entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
271     entry->f_callback = f_callback;
272     entry->i_event_type = i_event_type;
273     entry->p_user_data = user_data;
274     
275     switch ( i_event_type )
276     {
277         case libvlc_VolumeChanged:
278             res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
279                            handle_event, entry );
280             break;
281         case libvlc_InputPositionChanged:
282             install_input_event( NULL, NULL, unused1, unused2, p_instance);
283             break;
284         default:
285             free( entry );
286             RAISEVOID( "Unsupported event." );
287     }
288
289     if (res != VLC_SUCCESS)
290     {
291         free ( entry );
292         RAISEVOID("Internal callback registration was not successful. Callback not registered.");
293     }
294     
295     vlc_mutex_lock( &p_instance->instance_lock );
296     add_callback_to_list( entry, &p_instance->p_callback_list );
297     vlc_mutex_unlock( &p_instance->instance_lock );
298
299     return;
300 }
301
302 /**************************************************************************
303  *       libvlc_event_remove_callback (public) :
304  *
305  * Remove a callback for an event.
306  **************************************************************************/
307 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
308                                    libvlc_event_type_t i_event_type,
309                                    libvlc_callback_t f_callback,
310                                    void *p_user_data,
311                                    libvlc_exception_t *p_e )
312 {
313     struct libvlc_callback_entry_list_t *p_listitem;
314
315     vlc_mutex_lock( &p_instance->instance_lock );
316
317     p_listitem = p_instance->p_callback_list;
318
319     while( p_listitem )
320     {
321         if( p_listitem->elmt->f_callback == f_callback
322             && ( p_listitem->elmt->i_event_type == i_event_type )
323             && ( p_listitem->elmt->p_user_data == p_user_data )
324         
325         )
326         {
327             remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
328
329             if( p_listitem->prev )
330                 p_listitem->prev->next = p_listitem->next;
331             else
332                 p_instance->p_callback_list = p_listitem->next;
333
334
335             p_listitem->next->prev = p_listitem->prev;
336
337             free( p_listitem->elmt );
338
339             free( p_listitem );
340             break;
341         }
342         
343         p_listitem = p_listitem->next;
344     }
345     vlc_mutex_unlock( &p_instance->instance_lock );
346 }
347
348 /**************************************************************************
349  *       libvlc_event_remove_all_callbacks (public) :
350  *
351  * Remove all callbacks for all events.
352  **************************************************************************/
353 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
354                                        libvlc_exception_t *p_e )
355 {
356     struct libvlc_callback_entry_list_t *p_listitem;
357
358     vlc_mutex_lock( &p_instance->instance_lock );
359
360     p_listitem = p_instance->p_callback_list;
361
362     while( p_listitem )
363     {
364         remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
365         p_listitem = p_listitem->next;
366
367     }
368     p_instance->p_callback_list = NULL;
369
370     vlc_mutex_unlock( &p_instance->instance_lock );
371 }