]> git.sesse.net Git - vlc/blob - src/input/control.c
* src/input/control.c: bookmarks support is back (will need some more work though).
[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 ) ) break;
142             }
143
144             if( i == p_input->input.p_item->i_categories )
145             {
146                 p_cat = malloc( sizeof( info_category_t ) );
147                 if( !p_cat )
148                 {
149                     vlc_mutex_lock( &p_input->input.p_item->lock );
150                     return VLC_EGENERIC;
151                 }
152                 p_cat->psz_name = strdup( psz_cat );
153                 p_cat->i_infos = 0;
154                 p_cat->pp_infos = NULL;
155                 INSERT_ELEM( p_input->input.p_item->pp_categories,
156                              p_input->input.p_item->i_categories,
157                              p_input->input.p_item->i_categories, p_cat );
158             }
159
160             p_cat = p_input->input.p_item->pp_categories[i];
161
162             for( i = 0; i < p_cat->i_infos; i++ )
163             {
164                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
165                 {
166                     if( p_cat->pp_infos[i]->psz_value )
167                         free( p_cat->pp_infos[i]->psz_value );
168                     break;
169                 }
170             }
171
172             if( i == p_cat->i_infos )
173             {
174                 p_info = malloc( sizeof( info_t ) );
175                 if( !p_info )
176                 {
177                     vlc_mutex_lock( &p_input->input.p_item->lock );
178                     return VLC_EGENERIC;
179                 }
180
181                 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
182                              p_cat->i_infos, p_info );
183                 p_info->psz_name = strdup( psz_name );
184             }
185
186             p_info = p_cat->pp_infos[i];
187             vasprintf( &p_info->psz_value, psz_format, args );
188
189             vlc_mutex_unlock( &p_input->input.p_item->lock );
190
191             NotifyPlaylist( p_input );
192         }
193         return VLC_SUCCESS;
194
195         case INPUT_GET_INFO:
196         {
197             char *psz_cat = (char *)va_arg( args, char * );
198             char *psz_name = (char *)va_arg( args, char * );
199             char **ppsz_value = (char **)va_arg( args, char ** );
200             int i_ret = VLC_EGENERIC;
201             int i;
202             *ppsz_value = NULL;
203
204             vlc_mutex_lock( &p_input->input.p_item->lock );
205             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
206             {
207                 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
208                              psz_cat ) ) break;
209             }
210
211             if( i != p_input->input.p_item->i_categories )
212             {
213                 info_category_t *p_cat;
214                 p_cat = p_input->input.p_item->pp_categories[i];
215
216                 for( i = 0; i < p_cat->i_infos; i++ )
217                 {
218                     if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
219                     {
220                         if( p_cat->pp_infos[i]->psz_value )
221                         {
222                             *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
223                             i_ret = VLC_SUCCESS;
224                         }
225                         break;
226                     }
227                 }
228             }
229             vlc_mutex_unlock( &p_input->input.p_item->lock );
230             return i_ret;
231         }
232
233         case INPUT_SET_NAME:
234         {
235             char *psz_name = (char *)va_arg( args, char * );
236
237             if( !psz_name ) return VLC_EGENERIC;
238
239             vlc_mutex_lock( &p_input->input.p_item->lock );
240             if( p_input->input.p_item->psz_name )
241                 free( p_input->input.p_item->psz_name );
242             p_input->input.p_item->psz_name = strdup( psz_name );
243             vlc_mutex_unlock( &p_input->input.p_item->lock );
244
245             NotifyPlaylist( p_input );
246
247             return VLC_SUCCESS;
248         }
249
250         case INPUT_ADD_BOOKMARK:
251             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
252             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
253
254             vlc_mutex_lock( &p_input->input.p_item->lock );
255             if( !p_bkmk->psz_name )
256             {
257                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
258                            p_input->i_bookmark );
259             }
260
261             TAB_APPEND( p_input->i_bookmark, p_input->bookmark, p_bkmk );
262
263             /* Reflect the changes on the object var */
264             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
265             {
266                 vlc_value_t val, text;
267                 int i;
268
269                 for( i = 0; i < p_input->i_bookmark; i++ )
270                 {
271                     val.i_int = i;
272                     text.psz_string = p_input->bookmark[i]->psz_name;
273                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
274                                 &val, &text );
275                 }
276             }
277             vlc_mutex_unlock( &p_input->input.p_item->lock );
278
279             UpdateBookmarksOption( p_input );
280
281             return VLC_SUCCESS;
282             break;
283
284         case INPUT_CHANGE_BOOKMARK:
285             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
286             i_bkmk = (int)va_arg( args, int );
287
288             vlc_mutex_lock( &p_input->input.p_item->lock );
289             if( i_bkmk < p_input->i_bookmark )
290             {
291                 vlc_value_t val, text;
292                 int i;
293
294                 p_input->bookmark[i_bkmk] = p_bkmk;
295
296                 /* Reflect the changes on the object var */
297                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
298                 for( i = 0; i < p_input->i_bookmark; i++ )
299                 {
300                     val.i_int = i;
301                     text.psz_string = p_input->bookmark[i]->psz_name;
302                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
303                                 &val, &text );
304                 }
305             }
306             vlc_mutex_unlock( &p_input->input.p_item->lock );
307
308             UpdateBookmarksOption( p_input );
309
310             return VLC_SUCCESS;
311             break;
312
313         case INPUT_DEL_BOOKMARK:
314             i_bkmk = (int)va_arg( args, int );
315
316             vlc_mutex_lock( &p_input->input.p_item->lock );
317             if( i_bkmk < p_input->i_bookmark )
318             {
319                 vlc_value_t val, text;
320                 int i;
321
322                 p_bkmk = p_input->bookmark[i_bkmk];
323                 TAB_REMOVE( p_input->i_bookmark, p_input->bookmark,
324                             p_bkmk );
325                 vlc_seekpoint_Delete( p_bkmk );
326
327                 /* Reflect the changes on the object var */
328                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
329                 for( i = 0; i < p_input->i_bookmark; i++ )
330                 {
331                     val.i_int = i;
332                     text.psz_string = p_input->bookmark[i]->psz_name;
333                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
334                                 &val, &text );
335                 }
336                 vlc_mutex_unlock( &p_input->input.p_item->lock );
337
338                 UpdateBookmarksOption( p_input );
339
340                 return VLC_SUCCESS;
341             }
342             vlc_mutex_unlock( &p_input->input.p_item->lock );
343
344             return VLC_EGENERIC;
345             break;
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             break;
400
401         case INPUT_SET_BOOKMARK:
402             i_bkmk = (int)va_arg( args, int );
403
404             vlc_mutex_lock( &p_input->input.p_item->lock );
405             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmark )
406             {
407                 vlc_value_t pos;
408                 int i_ret;
409
410                 if( p_input->bookmark[i_bkmk]->i_time_offset )
411                 {
412                     pos.i_time = p_input->bookmark[i_bkmk]->i_time_offset;
413                     i_ret = var_Set( p_input, "time", pos );
414                 }
415                 else if( p_input->bookmark[i_bkmk]->i_byte_offset )
416                 {
417                     pos.f_float = !p_input->input.p_stream ? 0 :
418                         p_input->bookmark[i_bkmk]->i_byte_offset /
419                         stream_Size( p_input->input.p_stream );
420                     i_ret = var_Set( p_input, "position", pos );
421                 }
422                 else
423                 {
424                     pos.f_float = 0;
425                     i_ret = var_Set( p_input, "position", pos );
426                 }
427
428                 vlc_mutex_unlock( &p_input->input.p_item->lock );
429                 return i_ret;
430             }
431             else
432             {
433                 vlc_mutex_unlock( &p_input->input.p_item->lock );
434                 return VLC_EGENERIC;
435             }
436
437             break;
438
439         case INPUT_ADD_OPTION:
440         {
441             char *psz_option = (char *)va_arg( args, char * );
442             char *psz_value = (char *)va_arg( args, char * );
443             int i;
444
445             vlc_mutex_lock( &p_input->input.p_item->lock );
446             /* Check if option already exists */            
447             for( i = 0; i < p_input->input.p_item->i_options; i++ )
448             {
449                 if( !strncmp( p_input->input.p_item->ppsz_options[i],
450                               psz_option, strlen( psz_option ) ) &&
451                     p_input->input.p_item->ppsz_options[i][strlen(psz_option)]
452                       == '=' )
453                 {
454                     free( p_input->input.p_item->ppsz_options[i] );
455                     break;
456                 }
457             }
458             if( i == p_input->input.p_item->i_options )
459             {
460                 p_input->input.p_item->i_options++;
461                 p_input->input.p_item->ppsz_options =
462                     realloc( p_input->input.p_item->ppsz_options,
463                              p_input->input.p_item->i_options *
464                              sizeof(char **) );
465             }
466
467             asprintf( &p_input->input.p_item->ppsz_options[i],
468                       "%s=%s", psz_option, psz_value ) ;
469             vlc_mutex_unlock( &p_input->input.p_item->lock );
470
471             return VLC_SUCCESS;
472             break;
473         }
474
475         case INPUT_GET_BYTE_POSITION:
476             pi_64 = (int64_t*)va_arg( args, int64_t * );
477             *pi_64 = !p_input->input.p_stream ? 0 :
478                 stream_Tell( p_input->input.p_stream );
479             return VLC_SUCCESS;
480             break;
481
482         case INPUT_SET_BYTE_SIZE:
483             pi_64 = (int64_t*)va_arg( args, int64_t * );
484             *pi_64 = !p_input->input.p_stream ? 0 :
485                 stream_Size( p_input->input.p_stream );
486             return VLC_SUCCESS;
487             break;
488
489         default:
490             msg_Err( p_input, "unknown query in input_vaControl" );
491             return VLC_EGENERIC;
492     }
493 }
494
495 static void NotifyPlaylist( input_thread_t *p_input )
496 {
497     playlist_t *p_playlist =
498         (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
499                                        FIND_PARENT );
500     if( p_playlist )
501     {
502         var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
503         vlc_object_release( p_playlist );
504     }
505 }
506
507 static void UpdateBookmarksOption( input_thread_t *p_input )
508 {
509     int i, i_len = 0;
510     char *psz_value = NULL, *psz_next = NULL;
511
512     vlc_mutex_lock( &p_input->input.p_item->lock );
513     for( i = 0; i < p_input->i_bookmark; i++ )
514     {
515         asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
516                   p_input->bookmark[i]->psz_name,
517                   p_input->bookmark[i]->i_byte_offset,
518                   p_input->bookmark[i]->i_time_offset/1000000 );
519         i_len += strlen( psz_value );
520         free( psz_value );
521     }
522     for( i = 0; i < p_input->i_bookmark; i++ )
523     {
524         if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmark );
525
526         sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
527                  p_input->bookmark[i]->psz_name,
528                  p_input->bookmark[i]->i_byte_offset,
529                  p_input->bookmark[i]->i_time_offset/1000000 );
530
531         psz_next += strlen( psz_next );
532         if( i < p_input->i_bookmark - 1)
533         {
534             *psz_next = ','; psz_next++;
535         }
536     }
537     vlc_mutex_unlock( &p_input->input.p_item->lock );
538
539     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
540                    psz_value ? psz_value : "" );
541 }