]> git.sesse.net Git - vlc/blob - src/input/control.c
Rewrite a useful tooltip for Windows DShow.
[vlc] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "input_internal.h"
34
35
36 static void UpdateBookmarksOption( input_thread_t * );
37
38 /****************************************************************************
39  * input_Control
40  ****************************************************************************/
41 /**
42  * Control function for inputs.
43  * \param p_input input handle
44  * \param i_query query type
45  * \return VLC_SUCCESS if ok
46  */
47 int input_Control( input_thread_t *p_input, int i_query, ...  )
48 {
49     va_list args;
50     int     i_result;
51
52     va_start( args, i_query );
53     i_result = input_vaControl( p_input, i_query, args );
54     va_end( args );
55
56     return i_result;
57 }
58
59 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
60 {
61     seekpoint_t *p_bkmk, ***ppp_bkmk;
62     int i_bkmk = 0;
63     int *pi_bkmk;
64
65     int i_int, *pi_int;
66     double f, *pf;
67     int64_t i_64, *pi_64;
68
69     char *psz;
70     vlc_value_t val;
71
72     switch( i_query )
73     {
74         case INPUT_GET_POSITION:
75             pf = (double*)va_arg( args, double * );
76             *pf = var_GetFloat( p_input, "position" );
77             return VLC_SUCCESS;
78
79         case INPUT_SET_POSITION:
80             f = (double)va_arg( args, double );
81             return var_SetFloat( p_input, "position", f );
82
83         case INPUT_GET_LENGTH:
84             pi_64 = (int64_t*)va_arg( args, int64_t * );
85             *pi_64 = var_GetTime( p_input, "length" );
86             return VLC_SUCCESS;
87
88         case INPUT_GET_TIME:
89             pi_64 = (int64_t*)va_arg( args, int64_t * );
90             *pi_64 = var_GetTime( p_input, "time" );
91             return VLC_SUCCESS;
92
93         case INPUT_SET_TIME:
94             i_64 = (int64_t)va_arg( args, int64_t );
95             return var_SetTime( p_input, "time", i_64 );
96
97         case INPUT_GET_RATE:
98             pi_int = (int*)va_arg( args, int * );
99             *pi_int = var_GetInteger( p_input, "rate" );
100             return VLC_SUCCESS;
101
102         case INPUT_SET_RATE:
103             i_int = (int)va_arg( args, int );
104             return var_SetInteger( p_input, "rate", i_int );
105
106         case INPUT_GET_STATE:
107             pi_int = (int*)va_arg( args, int * );
108             *pi_int = var_GetInteger( p_input, "state" );
109             return VLC_SUCCESS;
110
111         case INPUT_SET_STATE:
112             i_int = (int)va_arg( args, int );
113             return var_SetInteger( p_input, "state", i_int );
114
115         case INPUT_GET_AUDIO_DELAY:
116             pi_64 = (int64_t*)va_arg( args, int64_t * );
117             *pi_64 = var_GetTime( p_input, "audio-delay" );
118             return VLC_SUCCESS;
119
120         case INPUT_GET_SPU_DELAY:
121             pi_64 = (int64_t*)va_arg( args, int64_t * );
122             *pi_64 = var_GetTime( p_input, "spu-delay" );
123             return VLC_SUCCESS;
124
125         case INPUT_SET_AUDIO_DELAY:
126             i_64 = (int64_t)va_arg( args, int64_t );
127             return var_SetTime( p_input, "audio-delay", i_64 );
128
129         case INPUT_SET_SPU_DELAY:
130             i_64 = (int64_t)va_arg( args, int64_t );
131             return var_SetTime( p_input, "spu-delay", i_64 );
132
133         case INPUT_ADD_INFO:
134         {
135             /* FIXME : Impossible to use input_ItemAddInfo because of
136              * the ... problem ? */
137             char *psz_cat = (char *)va_arg( args, char * );
138             char *psz_name = (char *)va_arg( args, char * );
139             char *psz_format = (char *)va_arg( args, char * );
140
141             info_category_t *p_cat;
142             info_t *p_info;
143             int i;
144
145             vlc_mutex_lock( &p_input->p->input.p_item->lock );
146             for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
147             {
148                 if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
149                              psz_cat ) ) break;
150             }
151
152             if( i == p_input->p->input.p_item->i_categories )
153             {
154                 p_cat = malloc( sizeof( info_category_t ) );
155                 if( !p_cat )
156                 {
157                     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
158                     return VLC_EGENERIC;
159                 }
160                 p_cat->psz_name = strdup( psz_cat );
161                 p_cat->i_infos = 0;
162                 p_cat->pp_infos = NULL;
163                 INSERT_ELEM( p_input->p->input.p_item->pp_categories,
164                              p_input->p->input.p_item->i_categories,
165                              p_input->p->input.p_item->i_categories, p_cat );
166             }
167
168             p_cat = p_input->p->input.p_item->pp_categories[i];
169
170             for( i = 0; i < p_cat->i_infos; i++ )
171             {
172                 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
173                 {
174                     if( p_cat->pp_infos[i]->psz_value )
175                         free( p_cat->pp_infos[i]->psz_value );
176                     break;
177                 }
178             }
179
180             if( i == p_cat->i_infos )
181             {
182                 p_info = malloc( sizeof( info_t ) );
183                 if( !p_info )
184                 {
185                     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
186                     return VLC_EGENERIC;
187                 }
188
189                 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
190                              p_cat->i_infos, p_info );
191                 p_info->psz_name = strdup( psz_name );
192             }
193
194             p_info = p_cat->pp_infos[i];
195             if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
196                 p_info->psz_value = NULL;
197
198             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
199
200             if( !p_input->b_preparsing )
201             {
202                 vlc_event_t event;
203                 event.type = vlc_InputItemInfoChanged;
204                 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
205             }
206         }
207         return VLC_SUCCESS;
208
209         case INPUT_DEL_INFO:
210         {
211             char *psz_cat = (char *)va_arg( args, char * );
212             char *psz_name = (char *)va_arg( args, char * );
213
214             info_category_t *p_cat = NULL;
215             int i_cat;
216             int i;
217
218             vlc_mutex_lock( &p_input->p->input.p_item->lock );
219             for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
220             {
221                 if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
222                              psz_cat ) )
223                 {
224                     p_cat = p_input->p->input.p_item->pp_categories[i_cat];
225                     break;
226                 }
227             }
228             if( p_cat == NULL )
229             {
230                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
231                 return VLC_EGENERIC;
232             }
233
234             if( psz_name )
235             {
236                 /* Remove a specific info */
237                 for( i = 0; i < p_cat->i_infos; i++ )
238                 {
239                     if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
240                     {
241                         free( p_cat->pp_infos[i]->psz_name );
242                         if( p_cat->pp_infos[i]->psz_value )
243                             free( p_cat->pp_infos[i]->psz_value );
244                         free( p_cat->pp_infos[i] );
245                         REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
246                         break;
247                     }
248                 }
249                 if( i >= p_cat->i_infos )
250                 {
251                     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
252                     return VLC_EGENERIC;
253                 }
254             }
255             else
256             {
257                 /* Remove the complete categorie */
258                 for( i = 0; i < p_cat->i_infos; i++ )
259                 {
260                     free( p_cat->pp_infos[i]->psz_name );
261                     if( p_cat->pp_infos[i]->psz_value )
262                         free( p_cat->pp_infos[i]->psz_value );
263                     free( p_cat->pp_infos[i] );
264                 }
265                 if( p_cat->pp_infos )
266                     free( p_cat->pp_infos );
267                 REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
268             }
269             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
270
271             if( !p_input->b_preparsing )
272             {
273                 vlc_event_t event;
274                 event.type = vlc_InputItemInfoChanged;
275                 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
276             }
277             return VLC_SUCCESS;
278         }
279
280
281         case INPUT_GET_INFO:
282         {
283             char *psz_cat = (char *)va_arg( args, char * );
284             char *psz_name = (char *)va_arg( args, char * );
285             char **ppsz_value = (char **)va_arg( args, char ** );
286             int i_ret = VLC_EGENERIC;
287             *ppsz_value = NULL;
288
289             *ppsz_value = input_ItemGetInfo( p_input->p->input.p_item,
290                                                   psz_cat, psz_name );
291             return i_ret;
292         }
293
294         case INPUT_SET_NAME:
295         {
296             char *psz_name = (char *)va_arg( args, char * );
297
298             if( !psz_name ) return VLC_EGENERIC;
299
300             vlc_mutex_lock( &p_input->p->input.p_item->lock );
301             if( p_input->p->input.p_item->psz_name )
302                 free( p_input->p->input.p_item->psz_name );
303             p_input->p->input.p_item->psz_name = strdup( psz_name );
304             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
305
306             if( !p_input->b_preparsing )
307             {
308                 vlc_event_t event;
309                 event.type = vlc_InputItemNameChanged;
310                 event.u.input_item_name_changed.new_name = psz_name;
311                 vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
312             }
313             return VLC_SUCCESS;
314         }
315
316         case INPUT_ADD_BOOKMARK:
317             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
318             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
319
320             vlc_mutex_lock( &p_input->p->input.p_item->lock );
321             if( !p_bkmk->psz_name )
322             {
323                  if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
324                                p_input->p->i_bookmark ) == -1 )
325                      p_bkmk->psz_name = NULL;
326             }
327
328             TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
329
330             /* Reflect the changes on the object var */
331             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
332             {
333                 vlc_value_t val, text;
334                 int i;
335
336                 for( i = 0; i < p_input->p->i_bookmark; i++ )
337                 {
338                     val.i_int = i;
339                     text.psz_string = p_input->p->bookmark[i]->psz_name;
340                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
341                                 &val, &text );
342                 }
343             }
344             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
345
346             UpdateBookmarksOption( p_input );
347
348             return VLC_SUCCESS;
349
350         case INPUT_CHANGE_BOOKMARK:
351             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
352             i_bkmk = (int)va_arg( args, int );
353
354             vlc_mutex_lock( &p_input->p->input.p_item->lock );
355             if( i_bkmk < p_input->p->i_bookmark )
356             {
357                 vlc_value_t val, text;
358                 int i;
359
360                 p_input->p->bookmark[i_bkmk] = p_bkmk;
361
362                 /* Reflect the changes on the object var */
363                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
364                 for( i = 0; i < p_input->p->i_bookmark; i++ )
365                 {
366                     val.i_int = i;
367                     text.psz_string = p_input->p->bookmark[i]->psz_name;
368                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
369                                 &val, &text );
370                 }
371             }
372             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
373
374             UpdateBookmarksOption( p_input );
375
376             return VLC_SUCCESS;
377
378         case INPUT_DEL_BOOKMARK:
379             i_bkmk = (int)va_arg( args, int );
380
381             vlc_mutex_lock( &p_input->p->input.p_item->lock );
382             if( i_bkmk < p_input->p->i_bookmark )
383             {
384                 vlc_value_t val, text;
385                 int i;
386
387                 p_bkmk = p_input->p->bookmark[i_bkmk];
388                 TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
389                             p_bkmk );
390                 vlc_seekpoint_Delete( p_bkmk );
391
392                 /* Reflect the changes on the object var */
393                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
394                 for( i = 0; i < p_input->p->i_bookmark; i++ )
395                 {
396                     val.i_int = i;
397                     text.psz_string = p_input->p->bookmark[i]->psz_name;
398                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
399                                 &val, &text );
400                 }
401                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
402
403                 UpdateBookmarksOption( p_input );
404
405                 return VLC_SUCCESS;
406             }
407             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
408
409             return VLC_EGENERIC;
410
411         case INPUT_GET_BOOKMARKS:
412             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
413             pi_bkmk = (int *)va_arg( args, int * );
414
415             vlc_mutex_lock( &p_input->p->input.p_item->lock );
416             if( p_input->p->i_bookmark )
417             {
418                 int i;
419
420                 *pi_bkmk = p_input->p->i_bookmark;
421                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
422                                     p_input->p->i_bookmark );
423                 for( i = 0; i < p_input->p->i_bookmark; i++ )
424                 {
425                     (*ppp_bkmk)[i] =
426                         vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
427                 }
428
429                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
430                 return VLC_SUCCESS;
431             }
432             else
433             {
434                 *ppp_bkmk = NULL;
435                 *pi_bkmk = 0;
436
437                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
438                 return VLC_EGENERIC;
439             }
440             break;
441
442         case INPUT_CLEAR_BOOKMARKS:
443
444             vlc_mutex_lock( &p_input->p->input.p_item->lock );
445             if( p_input->p->i_bookmark )
446             {
447                 int i;
448
449                 for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
450                 {
451                     p_bkmk = p_input->p->bookmark[i];
452                     TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
453                                 p_bkmk );
454                     vlc_seekpoint_Delete( p_bkmk );
455                 }
456                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
457             }
458             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
459
460             UpdateBookmarksOption( p_input );
461
462             return VLC_SUCCESS;
463
464         case INPUT_SET_BOOKMARK:
465             i_bkmk = (int)va_arg( args, int );
466
467             vlc_mutex_lock( &p_input->p->input.p_item->lock );
468             if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
469             {
470                 vlc_value_t pos;
471                 int i_ret;
472
473                 if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
474                 {
475                     pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
476                     i_ret = var_Set( p_input, "time", pos );
477                 }
478                 else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
479                 {
480                     // don't crash on bookmarks in live streams
481                     if( stream_Size( p_input->p->input.p_stream ) == 0 )
482                     {
483                         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
484                         return VLC_EGENERIC;
485                     }
486                     pos.f_float = !p_input->p->input.p_stream ? 0 :
487                         p_input->p->bookmark[i_bkmk]->i_byte_offset /
488                         stream_Size( p_input->p->input.p_stream );
489                     i_ret = var_Set( p_input, "position", pos );
490                 }
491                 else
492                 {
493                     pos.f_float = 0;
494                     i_ret = var_Set( p_input, "position", pos );
495                 }
496
497                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
498                 return i_ret;
499             }
500             else
501             {
502                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
503                 return VLC_EGENERIC;
504             }
505
506             break;
507
508         case INPUT_ADD_OPTION:
509         {
510             const char *psz_option = va_arg( args, const char * );
511             const char *psz_value = va_arg( args, const char * );
512             char *str;
513             int i;
514
515             if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
516                 return VLC_ENOMEM;
517
518             i = input_ItemAddOpt( p_input->p->input.p_item, str,
519                                   VLC_INPUT_OPTION_UNIQUE );
520             free( str );
521             return i;
522         }
523
524         case INPUT_GET_BYTE_POSITION:
525             pi_64 = (int64_t*)va_arg( args, int64_t * );
526             *pi_64 = !p_input->p->input.p_stream ? 0 :
527                 stream_Tell( p_input->p->input.p_stream );
528             return VLC_SUCCESS;
529
530         case INPUT_SET_BYTE_SIZE:
531             pi_64 = (int64_t*)va_arg( args, int64_t * );
532             *pi_64 = !p_input->p->input.p_stream ? 0 :
533                 stream_Size( p_input->p->input.p_stream );
534             return VLC_SUCCESS;
535
536         case INPUT_GET_VIDEO_FPS:
537         {
538             int i;
539             pf = (double*)va_arg( args, double * );
540             vlc_mutex_lock( &p_input->p->input.p_item->lock );
541             *pf = p_input->p->input.f_fps;
542             for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
543                 *pf = p_input->p->slave[i]->f_fps;
544             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
545             return VLC_SUCCESS;
546         }
547
548         case INPUT_ADD_SLAVE:
549             psz = (char*)va_arg( args, char * );
550             if( psz && *psz )
551             {
552                 val.psz_string = strdup( psz );
553                 input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
554             }
555             return VLC_SUCCESS;
556
557         case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int*  res=can fail */
558         {
559             input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
560             int *pi_attachment = (int*)va_arg( args, int * );
561             int i;
562
563             vlc_mutex_lock( &p_input->p->input.p_item->lock );
564             if( p_input->p->i_attachment <= 0 )
565             {
566                 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
567                 *ppp_attachment = NULL;
568                 *pi_attachment = 0;
569                 return VLC_EGENERIC;
570             }
571             *pi_attachment = p_input->p->i_attachment;
572             *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
573             for( i = 0; i < p_input->p->i_attachment; i++ )
574                 (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
575
576             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
577             return VLC_SUCCESS;
578         }
579
580         case INPUT_GET_ATTACHMENT:  /* arg1=input_attachment_t**, arg2=char*  res=can fail */
581         {
582             input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
583             const char *psz_name = (const char*)va_arg( args, const char * );
584             int i;
585
586             vlc_mutex_lock( &p_input->p->input.p_item->lock );
587             for( i = 0; i < p_input->p->i_attachment; i++ )
588             {
589                 if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
590                 {
591                     *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
592                     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
593                     return VLC_SUCCESS;
594                 }
595             }
596             *pp_attachment = NULL;
597             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
598             return VLC_EGENERIC;
599         }
600
601
602         default:
603             msg_Err( p_input, "unknown query in input_vaControl" );
604             return VLC_EGENERIC;
605     }
606 }
607
608 static void UpdateBookmarksOption( input_thread_t *p_input )
609 {
610     int i, i_len = 0;
611     char *psz_value = NULL, *psz_next = NULL;
612
613     vlc_mutex_lock( &p_input->p->input.p_item->lock );
614     if( p_input->p->i_bookmark > 0 )
615     {
616         for( i = 0; i < p_input->p->i_bookmark; i++ )
617         {
618             i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
619                                p_input->p->bookmark[i]->psz_name,
620                                p_input->p->bookmark[i]->i_byte_offset,
621                                p_input->p->bookmark[i]->i_time_offset/1000000 );
622         }
623         psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
624
625         for( i = 0; i < p_input->p->i_bookmark; i++ )
626         {
627             sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
628                      p_input->p->bookmark[i]->psz_name,
629                      p_input->p->bookmark[i]->i_byte_offset,
630                      p_input->p->bookmark[i]->i_time_offset/1000000 );
631
632             psz_next += strlen( psz_next );
633             if( i < p_input->p->i_bookmark - 1)
634                 *psz_next = ','; psz_next++;
635         }
636     }
637     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
638
639     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
640                    psz_value ? psz_value : "" );
641     free( psz_value );
642 }
643