]> git.sesse.net Git - vlc/blob - src/input/control.c
* all: - added a boolean "seekable" object variable to p_input.
[vlc] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29 #include "vlc_playlist.h"
30
31
32 static void UpdateBookmarksOption( input_thread_t * );
33 static void NotifyPlaylist( input_thread_t * );
34
35 /****************************************************************************
36  * input_Control
37  ****************************************************************************/
38 /**
39  * Control function for inputs.
40  * \param p_input input handle
41  * \param i_query query type
42  * \return VLC_SUCESS if ok
43  */
44 int input_Control( input_thread_t *p_input, int i_query, ...  )
45 {
46     va_list args;
47     int     i_result;
48
49     va_start( args, i_query );
50     i_result = input_vaControl( p_input, i_query, args );
51     va_end( args );
52
53     return i_result;
54 }
55
56 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
57 {
58     /* seekpoint_t *p_bkmk, ***ppp_bkmk;
59     int i_bkmk = 0;
60     int *pi_bkmk;
61     */
62     int i_int, *pi_int;
63     double f, *pf;
64     int64_t i_64, *pi_64;
65
66     switch( i_query )
67     {
68         case INPUT_GET_POSITION:
69             pf = (double*)va_arg( args, double * );
70             *pf = var_GetFloat( p_input, "position" );
71             return VLC_SUCCESS;
72
73         case INPUT_SET_POSITION:
74             f = (double)va_arg( args, double );
75             return var_SetFloat( p_input, "position", f );
76
77         case INPUT_GET_LENGTH:
78             pi_64 = (int64_t*)va_arg( args, int64_t * );
79             *pi_64 = var_GetTime( p_input, "length" );
80             return VLC_SUCCESS;
81
82         case INPUT_GET_TIME:
83             pi_64 = (int64_t*)va_arg( args, int64_t * );
84             *pi_64 = var_GetTime( p_input, "time" );
85             return VLC_SUCCESS;
86
87         case INPUT_SET_TIME:
88             i_64 = (int64_t)va_arg( args, int64_t );
89             return var_SetTime( p_input, "time", i_64 );
90
91         case INPUT_GET_RATE:
92             pi_int = (int*)va_arg( args, int * );
93             *pi_int = var_GetInteger( p_input, "rate" );
94             return VLC_SUCCESS;
95
96         case INPUT_SET_RATE:
97             i_int = (int)va_arg( args, int );
98             return var_SetInteger( p_input, "rate", i_int );
99
100         case INPUT_GET_STATE:
101             pi_int = (int*)va_arg( args, int * );
102             *pi_int = var_GetInteger( p_input, "state" );
103             return VLC_SUCCESS;
104
105         case INPUT_SET_STATE:
106             i_int = (int)va_arg( args, int );
107             return var_SetInteger( p_input, "state", i_int );
108
109         case INPUT_GET_AUDIO_DELAY:
110             pi_64 = (int64_t*)va_arg( args, int64_t * );
111             *pi_64 = var_GetTime( p_input, "audio-delay" );
112             return VLC_SUCCESS;
113
114         case INPUT_GET_SPU_DELAY:
115             pi_64 = (int64_t*)va_arg( args, int64_t * );
116             *pi_64 = var_GetTime( p_input, "spu-delay" );
117             return VLC_SUCCESS;
118
119         case INPUT_SET_AUDIO_DELAY:
120             i_64 = (int64_t)va_arg( args, int64_t );
121             return var_SetTime( p_input, "audio-delay", i_64 );
122
123         case INPUT_SET_SPU_DELAY:
124             i_64 = (int64_t)va_arg( args, int64_t );
125             return var_SetTime( p_input, "spu-delay", i_64 );
126
127         case INPUT_ADD_INFO:
128         {
129             char *psz_cat = (char *)va_arg( args, char * );
130             char *psz_name = (char *)va_arg( args, char * );
131             char *psz_format = (char *)va_arg( args, char * );
132
133             info_category_t *p_cat;
134             info_t *p_info;
135             int i;
136
137             vlc_mutex_lock( &p_input->input.p_item->lock );
138             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
139             {
140                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
141                              psz_cat ) )
142                     return VLC_EGENERIC;
143             }
144
145             if( i == p_input->input.p_item->i_categories )
146             {
147                 p_cat = malloc( sizeof( info_category_t ) );
148                 if( !p_cat )
149                     return VLC_EGENERIC;
150                 p_cat->psz_name = strdup( psz_cat );
151                 p_cat->i_infos = 0;
152                 p_cat->pp_infos = NULL;
153                 INSERT_ELEM( p_input->input.p_item->pp_categories,
154                              p_input->input.p_item->i_categories,
155                              p_input->input.p_item->i_categories, p_cat );
156             }
157
158             p_cat = p_input->input.p_item->pp_categories[i];
159
160             for( i = 0; i < p_cat->i_infos; i++ )
161             {
162                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
163                 {
164                     if( p_cat->pp_infos[i]->psz_value )
165                         free( p_cat->pp_infos[i]->psz_value );
166                     return VLC_EGENERIC;
167                 }
168             }
169
170             if( i == p_cat->i_infos )
171             {
172                 p_info = malloc( sizeof( info_t ) );
173                 if( !p_info )
174                     return VLC_EGENERIC;
175                 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
176                              p_cat->i_infos, p_info );
177                 p_info->psz_name = strdup( psz_name );
178             }
179
180             p_info = p_cat->pp_infos[i];
181             vasprintf( &p_info->psz_value, psz_format, args );
182
183             vlc_mutex_unlock( &p_input->input.p_item->lock );
184
185             NotifyPlaylist( p_input );
186         }
187         return VLC_SUCCESS;
188
189         case INPUT_GET_INFO:
190         {
191             char *psz_cat = (char *)va_arg( args, char * );
192             char *psz_name = (char *)va_arg( args, char * );
193             char **ppsz_value = (char **)va_arg( args, char ** );
194             int i_ret = VLC_EGENERIC;
195             int i;
196             *ppsz_value = NULL;
197
198             vlc_mutex_lock( &p_input->input.p_item->lock );
199             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
200             {
201                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
202                              psz_cat ) )
203                     return VLC_EGENERIC;
204             }
205
206             if( i != p_input->input.p_item->i_categories )
207             {
208                 info_category_t *p_cat;
209                 p_cat = p_input->input.p_item->pp_categories[i];
210
211                 for( i = 0; i < p_cat->i_infos; i++ )
212                 {
213                     if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
214                     {
215                         if( p_cat->pp_infos[i]->psz_value )
216                         {
217                             *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
218                             i_ret = VLC_SUCCESS;
219                         }
220                         break;
221                     }
222                 }
223             }
224             vlc_mutex_unlock( &p_input->input.p_item->lock );
225             return i_ret;
226         }
227
228         case INPUT_SET_NAME:
229         {
230             char *psz_name = (char *)va_arg( args, char * );
231
232             if( !psz_name )
233                 return VLC_EGENERIC;
234
235             vlc_mutex_lock( &p_input->input.p_item->lock );
236             if( p_input->input.p_item->psz_name )
237                 free( p_input->input.p_item->psz_name );
238             p_input->input.p_item->psz_name = strdup( psz_name );
239             vlc_mutex_unlock( &p_input->input.p_item->lock );
240
241             NotifyPlaylist( p_input );
242
243             return VLC_SUCCESS;
244         }
245
246 #if 0
247         case INPUT_ADD_BOOKMARK:
248             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
249             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
250             if( !p_bkmk->psz_name )
251             {
252                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
253                            p_input->i_bookmarks );
254             }
255             TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
256
257             /* Reflect the changes on the object var */
258             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
259             {
260                 int i;
261                 for( i = 0; i < p_input->i_bookmarks; i++ )
262                 {
263                     val.i_int = i;
264                     text.psz_string = p_input->pp_bookmarks[i]->psz_name;
265                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
266                                 &val, &text );
267                 }
268             }
269
270             UpdateBookmarksOption( p_input );
271
272             i_ret = VLC_SUCCESS;
273             break;
274
275         case INPUT_CHANGE_BOOKMARK:
276              p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
277              i_bkmk = (int)va_arg( args, int );
278
279              if( i_bkmk < p_input->i_bookmarks )
280              {
281                  p_input->pp_bookmarks[i_bkmk] = p_bkmk;
282
283                  /* Reflect the changes on the object var */
284                  var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
285                  for( i = 0; i < p_input->i_bookmarks; i++ )
286                  {
287                      val.i_int = i;
288                      text.psz_string = p_input->pp_bookmarks[i]->psz_name;
289                      var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
290                                  &val, &text );
291                  }
292              }
293              UpdateBookmarksOption( p_input );
294
295              i_ret = VLC_SUCCESS;
296              break;
297
298         case INPUT_DEL_BOOKMARK:
299             i_bkmk = (int)va_arg( args, int );
300             if( i_bkmk < p_input->i_bookmarks )
301             {
302                 int i;
303                 p_bkmk = p_input->pp_bookmarks[i_bkmk];
304                 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
305                             p_bkmk );
306                 vlc_seekpoint_Delete( p_bkmk );
307
308                 /* Reflect the changes on the object var */
309                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
310                 for( i = 0; i < p_input->i_bookmarks; i++ )
311                 {
312                     val.i_int = i;
313                     text.psz_string = p_input->pp_bookmarks[i]->psz_name;
314                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
315                                 &val, &text );
316                 }
317
318                 UpdateBookmarksOption( p_input );
319
320                 i_ret = VLC_SUCCESS;
321             }
322             else i_ret = VLC_EGENERIC;
323             break;
324
325         case INPUT_GET_BOOKMARKS:
326             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
327             pi_bkmk = (int *)va_arg( args, int * );
328             if( p_input->i_bookmarks )
329             {
330                 int i;
331
332                 *pi_bkmk = p_input->i_bookmarks;
333                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
334                               p_input->i_bookmarks );
335                 for( i = 0; i < p_input->i_bookmarks; i++ )
336                 {
337                     (*ppp_bkmk)[i] =
338                         vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
339                 }
340                 i_ret = VLC_SUCCESS;
341             }
342             else
343             {
344                 *ppp_bkmk = NULL;
345                 *pi_bkmk = 0;
346                 i_ret = VLC_EGENERIC;
347             }
348             break;
349
350         case INPUT_CLEAR_BOOKMARKS:
351             if( p_input->i_bookmarks )
352             {
353                 int i;
354
355                 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
356                 {
357                     p_bkmk = p_input->pp_bookmarks[i];
358                     TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
359                                 p_bkmk );
360                     vlc_seekpoint_Delete( p_bkmk );
361                 }
362                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
363             }
364
365             UpdateBookmarksOption( p_input );
366
367             i_ret = VLC_SUCCESS;
368             break;
369
370         case INPUT_SET_BOOKMARK:
371             i_bkmk = (int)va_arg( args, int );
372             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
373             {
374                 vlc_value_t pos;
375                 vlc_mutex_unlock( &p_input->stream.stream_lock );
376                 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
377                     ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
378                       !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
379                 {
380                     pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
381                         (double)p_input->stream.p_selected_area->i_size;
382                     i_ret = var_Set( p_input, "position", pos );
383                 }
384                 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
385                 {
386                     pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
387                     i_ret = var_Set( p_input, "time", pos );
388                 }
389                 vlc_mutex_lock( &p_input->stream.stream_lock );
390             }
391             else
392             {
393                 i_ret = VLC_EGENERIC;
394             }
395             break;
396
397 #endif
398         case INPUT_GET_BOOKMARKS:
399         case INPUT_CLEAR_BOOKMARKS:
400         case INPUT_ADD_BOOKMARK:
401         case INPUT_CHANGE_BOOKMARK:
402         case INPUT_DEL_BOOKMARK:
403         case INPUT_SET_BOOKMARK:
404             /* FIXME */
405             msg_Err( p_input, "unimplemented query in input_vaControl" );
406         default:
407             msg_Err( p_input, "unknown query in input_vaControl" );
408             return VLC_EGENERIC;
409     }
410 }
411
412 static void NotifyPlaylist( input_thread_t *p_input )
413 {
414     playlist_t *p_playlist =
415         (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
416                                        FIND_PARENT );
417     if( p_playlist )
418     {
419         var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
420         vlc_object_release( p_playlist );
421     }
422 }
423
424 static void UpdateBookmarksOption( input_thread_t *p_input )
425 {
426 #if 0
427     int i, i_len = 0;
428     char *psz_value = NULL, *psz_next = NULL;
429     /* FIXME */
430     vlc_mutex_unlock( &p_input->stream.stream_lock );
431
432     for( i = 0; i < p_input->i_bookmarks; i++ )
433     {
434         asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
435                   p_input->pp_bookmarks[i]->psz_name,
436                   p_input->pp_bookmarks[i]->i_byte_offset,
437                   p_input->pp_bookmarks[i]->i_time_offset/1000000 );
438         i_len += strlen( psz_value );
439         free( psz_value );
440     }
441     for( i = 0; i < p_input->i_bookmarks; i++ )
442     {
443         if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmarks );
444
445         sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
446                  p_input->pp_bookmarks[i]->psz_name,
447                  p_input->pp_bookmarks[i]->i_byte_offset,
448                  p_input->pp_bookmarks[i]->i_time_offset/1000000 );
449
450         psz_next += strlen( psz_next );
451         if( i < p_input->i_bookmarks - 1)
452         {
453             *psz_next = ','; psz_next++;
454         }
455     }
456     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
457                    psz_value ? psz_value : "" );
458
459     vlc_mutex_lock( &p_input->stream.stream_lock );
460 #endif
461 }