]> git.sesse.net Git - vlc/blob - src/control/event.c
Libvlc Event: Add support for input event.
[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 static int handle_event( vlc_object_t *p_this, char const *psz_cmd,
35                          vlc_value_t oldval, vlc_value_t newval,
36                          void *p_data )
37 {
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;
41     libvlc_event_t event;
42     event.type = entry->i_event_type;
43     switch ( event.type )
44     {
45         case VOLUME_CHANGED:
46             event.value_type = BOOLEAN_EVENT;
47             break;
48         case INPUT_POSITION_CHANGED:
49             break;
50         default:
51             break;
52     }
53     event.old_value = oldval;
54     event.new_value = newval;
55
56     /* Call the client entry */
57     entry->f_callback( entry->p_instance, &event, entry->p_user_data );
58
59     return VLC_SUCCESS;
60 }
61
62 /* Utility function: Object should be released by vlc_object_release afterwards */
63 static input_thread_t * get_input(libvlc_instance_t * p_instance)
64 {
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;
68
69     if( !p_libvlc_input )
70         return NULL;
71     
72     p_input = libvlc_get_input_thread( p_libvlc_input, &p_e_unused );
73
74     libvlc_input_free(p_libvlc_input);
75
76     return p_input;
77 }
78
79 static int install_input_event( vlc_object_t *p_this, char const *psz_cmd,
80                                 vlc_value_t oldval, vlc_value_t newval,
81                                 void *p_data )
82 {
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 );
86
87     vlc_mutex_lock( &p_instance->instance_lock );
88
89     p_listitem = p_instance->p_callback_list;
90
91     for( ; p_listitem ; p_listitem = p_listitem->next )
92     {
93         if (p_listitem->elmt->i_event_type == INPUT_POSITION_CHANGED)
94         {
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 );
98         }
99     }
100
101     vlc_mutex_unlock( &p_instance->instance_lock );
102     vlc_object_release( p_input );
103     return VLC_SUCCESS;
104 }
105
106 static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry,
107                                          struct libvlc_callback_entry_list_t **list )
108 {
109     struct libvlc_callback_entry_list_t *new_listitem;
110
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
116      */
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;
121
122     if(*list)
123         (*list)->prev = new_listitem;
124
125     *list = new_listitem;
126 }
127
128 static int remove_variable_callback( libvlc_instance_t *p_instance, 
129                                      struct libvlc_callback_entry_t * p_entry )
130 {
131     input_thread_t * p_input = get_input( p_instance );
132     int res = VLC_SUCCESS;
133
134     /* Note: Appropriate lock should be held by the caller */
135
136     switch ( p_entry->i_event_type )
137     {
138         case VOLUME_CHANGED:
139             res = var_DelCallback( p_instance->p_libvlc_int, "volume-change",
140                              handle_event, p_entry );
141             break;
142         case INPUT_POSITION_CHANGED:
143             /* We may not be deleting the right p_input callback, in this case this
144              * will be a no-op */
145             var_DelCallback( p_input, "intf-change",
146                              handle_event, p_entry );
147             break;
148     }
149     
150     if (p_input)
151         vlc_object_release( p_input );
152
153     return res;
154 }
155
156 /*
157  * Internal libvlc functions
158  */
159 void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
160 {
161     playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
162
163     if( !p_playlist )
164         RAISEVOID ("Can't listen to input event");
165
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 );
170 }
171
172 void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
173 {
174     playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist;
175     libvlc_exception_t p_e_unused;
176
177     libvlc_event_remove_all_callbacks( p_instance, &p_e_unused );
178
179     if( !p_playlist )
180         RAISEVOID ("Can't unregister input events");
181
182     var_DelCallback( p_playlist, "playlist-current",
183                      install_input_event, p_instance );
184 }
185
186 /*
187  * Public libvlc functions
188  */
189
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,
193                                 void *user_data,
194                                 libvlc_exception_t *p_e )
195 {
196     struct libvlc_callback_entry_t *entry;
197     vlc_value_t unused1, unused2;
198     int res = VLC_SUCCESS;
199
200     if ( !f_callback )
201         RAISEVOID (" Callback function is null ");
202
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
209      */
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;
214     
215     switch ( i_event_type )
216     {
217         case VOLUME_CHANGED:
218             res = var_AddCallback( p_instance->p_libvlc_int, "volume-change",
219                            handle_event, entry );
220             break;
221         case INPUT_POSITION_CHANGED:
222             install_input_event( NULL, NULL, unused1, unused2, p_instance);
223             break;
224         default:
225             free( entry );
226             RAISEVOID( "Unsupported event." );
227     }
228
229     if (res != VLC_SUCCESS)
230     {
231         free ( entry );
232         RAISEVOID("Internal callback registration was not successful. Callback not registered.");
233     }
234     
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 );
238
239     return;
240 }
241
242 void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
243                                        libvlc_exception_t *p_e )
244 {
245     struct libvlc_callback_entry_list_t *p_listitem;
246
247     vlc_mutex_lock( &p_instance->instance_lock );
248
249     p_listitem = p_instance->p_callback_list;
250
251     while( p_listitem )
252     {
253         remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
254         p_listitem = p_listitem->next;
255
256     }
257     p_instance->p_callback_list = NULL;
258
259     vlc_mutex_unlock( &p_instance->instance_lock );
260 }
261
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,
265                                    void *p_user_data,
266                                    libvlc_exception_t *p_e )
267 {
268     struct libvlc_callback_entry_list_t *p_listitem;
269
270     vlc_mutex_lock( &p_instance->instance_lock );
271
272     p_listitem = p_instance->p_callback_list;
273
274     while( p_listitem )
275     {
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 )
279         
280         )
281         {
282             remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */
283
284             if( p_listitem->prev )
285                 p_listitem->prev->next = p_listitem->next;
286             else
287                 p_instance->p_callback_list = p_listitem->next;
288
289
290             p_listitem->next->prev = p_listitem->prev;
291
292             free( p_listitem->elmt );
293
294             free( p_listitem );
295             break;
296         }
297         
298         p_listitem = p_listitem->next;
299     }
300     vlc_mutex_unlock( &p_instance->instance_lock );
301 }