]> git.sesse.net Git - vlc/blob - src/input/control.c
all: implemented INPUT_ADD_SLAVE.
[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_SUCCESS 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     char *psz;
67     vlc_value_t val;
68
69     switch( i_query )
70     {
71         case INPUT_GET_POSITION:
72             pf = (double*)va_arg( args, double * );
73             *pf = var_GetFloat( p_input, "position" );
74             return VLC_SUCCESS;
75
76         case INPUT_SET_POSITION:
77             f = (double)va_arg( args, double );
78             return var_SetFloat( p_input, "position", f );
79
80         case INPUT_GET_LENGTH:
81             pi_64 = (int64_t*)va_arg( args, int64_t * );
82             *pi_64 = var_GetTime( p_input, "length" );
83             return VLC_SUCCESS;
84
85         case INPUT_GET_TIME:
86             pi_64 = (int64_t*)va_arg( args, int64_t * );
87             *pi_64 = var_GetTime( p_input, "time" );
88             return VLC_SUCCESS;
89
90         case INPUT_SET_TIME:
91             i_64 = (int64_t)va_arg( args, int64_t );
92             return var_SetTime( p_input, "time", i_64 );
93
94         case INPUT_GET_RATE:
95             pi_int = (int*)va_arg( args, int * );
96             *pi_int = var_GetInteger( p_input, "rate" );
97             return VLC_SUCCESS;
98
99         case INPUT_SET_RATE:
100             i_int = (int)va_arg( args, int );
101             return var_SetInteger( p_input, "rate", i_int );
102
103         case INPUT_GET_STATE:
104             pi_int = (int*)va_arg( args, int * );
105             *pi_int = var_GetInteger( p_input, "state" );
106             return VLC_SUCCESS;
107
108         case INPUT_SET_STATE:
109             i_int = (int)va_arg( args, int );
110             return var_SetInteger( p_input, "state", i_int );
111
112         case INPUT_GET_AUDIO_DELAY:
113             pi_64 = (int64_t*)va_arg( args, int64_t * );
114             *pi_64 = var_GetTime( p_input, "audio-delay" );
115             return VLC_SUCCESS;
116
117         case INPUT_GET_SPU_DELAY:
118             pi_64 = (int64_t*)va_arg( args, int64_t * );
119             *pi_64 = var_GetTime( p_input, "spu-delay" );
120             return VLC_SUCCESS;
121
122         case INPUT_SET_AUDIO_DELAY:
123             i_64 = (int64_t)va_arg( args, int64_t );
124             return var_SetTime( p_input, "audio-delay", i_64 );
125
126         case INPUT_SET_SPU_DELAY:
127             i_64 = (int64_t)va_arg( args, int64_t );
128             return var_SetTime( p_input, "spu-delay", i_64 );
129
130         case INPUT_ADD_INFO:
131         {
132             char *psz_cat = (char *)va_arg( args, char * );
133             char *psz_name = (char *)va_arg( args, char * );
134             char *psz_format = (char *)va_arg( args, char * );
135
136             info_category_t *p_cat;
137             info_t *p_info;
138             int i;
139
140             vlc_mutex_lock( &p_input->input.p_item->lock );
141             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
142             {
143                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
144                              psz_cat ) ) break;
145             }
146
147             if( i == p_input->input.p_item->i_categories )
148             {
149                 p_cat = malloc( sizeof( info_category_t ) );
150                 if( !p_cat )
151                 {
152                     vlc_mutex_lock( &p_input->input.p_item->lock );
153                     return VLC_EGENERIC;
154                 }
155                 p_cat->psz_name = strdup( psz_cat );
156                 p_cat->i_infos = 0;
157                 p_cat->pp_infos = NULL;
158                 INSERT_ELEM( p_input->input.p_item->pp_categories,
159                              p_input->input.p_item->i_categories,
160                              p_input->input.p_item->i_categories, p_cat );
161             }
162
163             p_cat = p_input->input.p_item->pp_categories[i];
164
165             for( i = 0; i < p_cat->i_infos; i++ )
166             {
167                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
168                 {
169                     if( p_cat->pp_infos[i]->psz_value )
170                         free( p_cat->pp_infos[i]->psz_value );
171                     break;
172                 }
173             }
174
175             if( i == p_cat->i_infos )
176             {
177                 p_info = malloc( sizeof( info_t ) );
178                 if( !p_info )
179                 {
180                     vlc_mutex_lock( &p_input->input.p_item->lock );
181                     return VLC_EGENERIC;
182                 }
183
184                 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
185                              p_cat->i_infos, p_info );
186                 p_info->psz_name = strdup( psz_name );
187             }
188
189             p_info = p_cat->pp_infos[i];
190             vasprintf( &p_info->psz_value, psz_format, args );
191
192             vlc_mutex_unlock( &p_input->input.p_item->lock );
193
194             NotifyPlaylist( p_input );
195         }
196         return VLC_SUCCESS;
197
198         case INPUT_GET_INFO:
199         {
200             char *psz_cat = (char *)va_arg( args, char * );
201             char *psz_name = (char *)va_arg( args, char * );
202             char **ppsz_value = (char **)va_arg( args, char ** );
203             int i_ret = VLC_EGENERIC;
204             int i;
205             *ppsz_value = NULL;
206
207             vlc_mutex_lock( &p_input->input.p_item->lock );
208             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
209             {
210                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
211                              psz_cat ) ) break;
212             }
213
214             if( i != p_input->input.p_item->i_categories )
215             {
216                 info_category_t *p_cat;
217                 p_cat = p_input->input.p_item->pp_categories[i];
218
219                 for( i = 0; i < p_cat->i_infos; i++ )
220                 {
221                     if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
222                     {
223                         if( p_cat->pp_infos[i]->psz_value )
224                         {
225                             *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
226                             i_ret = VLC_SUCCESS;
227                         }
228                         break;
229                     }
230                 }
231             }
232             vlc_mutex_unlock( &p_input->input.p_item->lock );
233             return i_ret;
234         }
235
236         case INPUT_SET_NAME:
237         {
238             char *psz_name = (char *)va_arg( args, char * );
239
240             if( !psz_name ) return VLC_EGENERIC;
241
242             vlc_mutex_lock( &p_input->input.p_item->lock );
243             if( p_input->input.p_item->psz_name )
244                 free( p_input->input.p_item->psz_name );
245             p_input->input.p_item->psz_name = strdup( psz_name );
246             vlc_mutex_unlock( &p_input->input.p_item->lock );
247
248             NotifyPlaylist( p_input );
249
250             return VLC_SUCCESS;
251         }
252
253         case INPUT_ADD_BOOKMARK:
254             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
255             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
256
257             vlc_mutex_lock( &p_input->input.p_item->lock );
258             if( !p_bkmk->psz_name )
259             {
260                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
261                            p_input->i_bookmark );
262             }
263
264             TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
265
266             /* Reflect the changes on the object var */
267             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
268             {
269                 vlc_value_t val, text;
270                 int i;
271
272                 for( i = 0; i < p_input->i_bookmark; i++ )
273                 {
274                     val.i_int = i;
275                     text.psz_string = p_input->bookmark[i]->psz_name;
276                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
277                                 &val, &text );
278                 }
279             }
280             vlc_mutex_unlock( &p_input->input.p_item->lock );
281
282             UpdateBookmarksOption( p_input );
283
284             return VLC_SUCCESS;
285
286         case INPUT_CHANGE_BOOKMARK:
287             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
288             i_bkmk = (int)va_arg( args, int );
289
290             vlc_mutex_lock( &p_input->input.p_item->lock );
291             if( i_bkmk < p_input->i_bookmark )
292             {
293                 vlc_value_t val, text;
294                 int i;
295
296                 p_input->bookmark[i_bkmk] = p_bkmk;
297
298                 /* Reflect the changes on the object var */
299                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
300                 for( i = 0; i < p_input->i_bookmark; i++ )
301                 {
302                     val.i_int = i;
303                     text.psz_string = p_input->bookmark[i]->psz_name;
304                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
305                                 &val, &text );
306                 }
307             }
308             vlc_mutex_unlock( &p_input->input.p_item->lock );
309
310             UpdateBookmarksOption( p_input );
311
312             return VLC_SUCCESS;
313
314         case INPUT_DEL_BOOKMARK:
315             i_bkmk = (int)va_arg( args, int );
316
317             vlc_mutex_lock( &p_input->input.p_item->lock );
318             if( i_bkmk < p_input->i_bookmark )
319             {
320                 vlc_value_t val, text;
321                 int i;
322
323                 p_bkmk = p_input->bookmark[i_bkmk];
324                 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
325                             p_bkmk );
326                 vlc_seekpoint_Delete( p_bkmk );
327
328                 /* Reflect the changes on the object var */
329                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
330                 for( i = 0; i < p_input->i_bookmark; i++ )
331                 {
332                     val.i_int = i;
333                     text.psz_string = p_input->bookmark[i]->psz_name;
334                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
335                                 &val, &text );
336                 }
337                 vlc_mutex_unlock( &p_input->input.p_item->lock );
338
339                 UpdateBookmarksOption( p_input );
340
341                 return VLC_SUCCESS;
342             }
343             vlc_mutex_unlock( &p_input->input.p_item->lock );
344
345             return VLC_EGENERIC;
346
347         case INPUT_GET_BOOKMARKS:
348             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
349             pi_bkmk = (int *)va_arg( args, int * );
350
351             vlc_mutex_lock( &p_input->input.p_item->lock );
352             if( p_input->i_bookmark )
353             {
354                 int i;
355
356                 *pi_bkmk = p_input->i_bookmark;
357                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
358                                     p_input->i_bookmark );
359                 for( i = 0; i < p_input->i_bookmark; i++ )
360                 {
361                     (*ppp_bkmk)[i] =
362                         vlc_seekpoint_Duplicate(p_input->bookmark[i]);
363                 }
364
365                 vlc_mutex_unlock( &p_input->input.p_item->lock );
366                 return VLC_SUCCESS;
367             }
368             else
369             {
370                 *ppp_bkmk = NULL;
371                 *pi_bkmk = 0;
372
373                 vlc_mutex_unlock( &p_input->input.p_item->lock );
374                 return VLC_EGENERIC;
375             }
376             break;
377
378         case INPUT_CLEAR_BOOKMARKS:
379
380             vlc_mutex_lock( &p_input->input.p_item->lock );
381             if( p_input->i_bookmark )
382             {
383                 int i;
384
385                 for( i = p_input->i_bookmark - 1; i >= 0; i-- )
386                 {
387                     p_bkmk = p_input->bookmark[i];
388                     TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
389                                 p_bkmk );
390                     vlc_seekpoint_Delete( p_bkmk );
391                 }
392                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
393             }
394             vlc_mutex_unlock( &p_input->input.p_item->lock );
395
396             UpdateBookmarksOption( p_input );
397
398             return VLC_SUCCESS;
399
400         case INPUT_SET_BOOKMARK:
401             i_bkmk = (int)va_arg( args, int );
402
403             vlc_mutex_lock( &p_input->input.p_item->lock );
404             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
405             {
406                 vlc_value_t pos;
407                 int i_ret;
408
409                 if( p_input->bookmark[i_bkmk]->i_time_offset )
410                 {
411                     pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
412                     i_ret = var_Set( p_input, "time", pos );
413                 }
414                 else if( p_input->bookmark[i_bkmk]->i_byte_offset )
415                 {
416                     pos.f_float = !p_input->input.p_stream ? 0 :
417                         p_input->bookmark[i_bkmk]->i_byte_offset /
418                         stream_Size( p_input->input.p_stream );
419                     i_ret = var_Set( p_input, "position", pos );
420                 }
421                 else
422                 {
423                     pos.f_float = 0;
424                     i_ret = var_Set( p_input, "position", pos );
425                 }
426
427                 vlc_mutex_unlock( &p_input->input.p_item->lock );
428                 return i_ret;
429             }
430             else
431             {
432                 vlc_mutex_unlock( &p_input->input.p_item->lock );
433                 return VLC_EGENERIC;
434             }
435
436             break;
437
438         case INPUT_ADD_OPTION:
439         {
440             char *psz_option = (char *)va_arg( args, char * );
441             char *psz_value = (char *)va_arg( args, char * );
442             int i;
443
444             vlc_mutex_lock( &p_input->input.p_item->lock );
445             /* Check if option already exists */            
446             for( i = 0; i < p_input->input.p_item->i_options; i++ )
447             {
448                 if( !strncmp( p_input->input.p_item->ppsz_options[i],
449                               psz_option, strlen( psz_option ) ) &&
450                     p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
451                       == '=' )
452                 {
453                     free( p_input->input.p_item->ppsz_options[i] );
454                     break;
455                 }
456             }
457             if( i == p_input->input.p_item->i_options )
458             {
459                 p_input->input.p_item->i_options++;
460                 p_input->input.p_item->ppsz_options =
461                     realloc( p_input->input.p_item->ppsz_options,
462                              p_input->input.p_item->i_options *
463                              sizeof(char **) );
464             }
465
466             asprintf( &p_input->input.p_item->ppsz_options[i],
467                       "%s=%s", psz_option, psz_value ) ;
468             vlc_mutex_unlock( &p_input->input.p_item->lock );
469
470             return VLC_SUCCESS;
471         }
472
473         case INPUT_GET_BYTE_POSITION:
474             pi_64 = (int64_t*)va_arg( args, int64_t * );
475             *pi_64 = !p_input->input.p_stream ? 0 :
476                 stream_Tell( p_input->input.p_stream );
477             return VLC_SUCCESS;
478
479         case INPUT_SET_BYTE_SIZE:
480             pi_64 = (int64_t*)va_arg( args, int64_t * );
481             *pi_64 = !p_input->input.p_stream ? 0 :
482                 stream_Size( p_input->input.p_stream );
483             return VLC_SUCCESS;
484
485         case INPUT_ADD_SLAVE:
486             psz = (char*)va_arg( args, char * );
487             if( psz && *psz )
488             {
489                 val.psz_string = strdup( psz );
490                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
491             }
492             return VLC_SUCCESS;
493
494         default:
495             msg_Err( p_input, "unknown query in input_vaControl" );
496             return VLC_EGENERIC;
497     }
498 }
499
500 static void NotifyPlaylist( input_thread_t *p_input )
501 {
502     playlist_t *p_playlist =
503         (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
504                                        FIND_PARENT );
505     if( p_playlist )
506     {
507         var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
508         vlc_object_release( p_playlist );
509     }
510 }
511
512 static void UpdateBookmarksOption( input_thread_t *p_input )
513 {
514     int i, i_len = 0;
515     char *psz_value = NULL, *psz_next = NULL;
516
517     vlc_mutex_lock( &p_input->input.p_item->lock );
518     for( i = 0; i < p_input->i_bookmark; i++ )
519     {
520         asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
521                   p_input->bookmark[i]->psz_name,
522                   p_input->bookmark[i]->i_byte_offset,
523                   p_input->bookmark[i]->i_time_offset/1000000 );
524         i_len += strlen( psz_value );
525         free( psz_value );
526     }
527     for( i = 0; i < p_input->i_bookmark; i++ )
528     {
529         if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
530
531         sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
532                  p_input->bookmark[i]->psz_name,
533                  p_input->bookmark[i]->i_byte_offset,
534                  p_input->bookmark[i]->i_time_offset/1000000 );
535
536         psz_next += strlen( psz_next );
537         if( i < p_input->i_bookmark - 1)
538         {
539             *psz_next = ','; psz_next++;
540         }
541     }
542     vlc_mutex_unlock( &p_input->input.p_item->lock );
543
544     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
545                    psz_value ? psz_value : "" );
546 }