]> git.sesse.net Git - vlc/blob - src/input/control.c
1fc129c736ec9aa8075b4efb6e93c7b269b1a658
[vlc] / src / input / control.c
1 /*****************************************************************************
2  * control.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: stream.c 7041 2004-03-11 16:48:27Z gbazin $
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 "ninput.h"
29 #include "../../modules/demux/util/sub.h"
30
31 struct input_thread_sys_t
32 {
33     /* subtitles */
34     int              i_sub;
35     subtitle_demux_t **sub;
36     int64_t          i_stop_time;
37 };
38
39 /****************************************************************************
40  * input_Control
41  ****************************************************************************/
42 /**
43  * Control function for inputs.
44  * \param p_input input handle
45  * \param i_query query type
46  * \return VLC_SUCESS if ok
47  */
48 int input_Control( input_thread_t *p_input, int i_query, ...  )
49 {
50     va_list args;
51     int     i_result;
52
53     va_start( args, i_query );
54     i_result = input_vaControl( p_input, i_query, args );
55     va_end( args );
56
57     return i_result;
58 }
59
60 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
61 {
62     int     i_ret;
63     seekpoint_t *p_bkmk, ***ppp_bkmk;
64     int i_bkmk, *pi_bkmk;
65     int i, *pi;
66     vlc_value_t val, text;
67
68     vlc_mutex_lock( &p_input->stream.stream_lock );
69     switch( i_query )
70     {
71         case INPUT_ADD_BOOKMARK:
72             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
73             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
74             if( !p_bkmk->psz_name )
75             {
76                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
77                            p_input->i_bookmarks );
78             }
79             TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
80
81             /* Reflect the changes on the object var */
82             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
83             {
84                 int i;
85                 for( i = 0; i < p_input->i_bookmarks; i++ )
86                 {
87                     val.i_int = i;
88                     text.psz_string = p_input->pp_bookmarks[i]->psz_name;
89                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
90                                 &val, &text );
91                 }
92             }
93
94             i_ret = VLC_SUCCESS;
95             break;
96
97         case INPUT_DEL_BOOKMARK:
98             i_bkmk = (int)va_arg( args, int );
99             if( i_bkmk < p_input->i_bookmarks )
100             {
101                 int i;
102                 p_bkmk = p_input->pp_bookmarks[i_bkmk];
103                 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
104                             p_bkmk );
105                 vlc_seekpoint_Delete( p_bkmk );
106
107                 /* Reflect the changes on the object var */
108                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
109                 for( i = 0; i < p_input->i_bookmarks; i++ )
110                 {
111                     val.i_int = i;
112                     text.psz_string = p_input->pp_bookmarks[i]->psz_name;
113                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
114                                 &val, &text );
115                 }
116                 i_ret = VLC_SUCCESS;
117             }
118             else i_ret = VLC_EGENERIC;
119             break;
120
121         case INPUT_GET_BOOKMARKS:
122             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
123             pi_bkmk = (int *)va_arg( args, int * );
124             if( p_input->i_bookmarks )
125             {
126                 int i;
127
128                 *pi_bkmk = p_input->i_bookmarks;
129                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
130                               p_input->i_bookmarks );
131                 for( i = 0; i < p_input->i_bookmarks; i++ )
132                 {
133                     (*ppp_bkmk)[i] =
134                         vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
135                 }
136                 i_ret = VLC_SUCCESS;
137             }
138             else
139             {
140                 *ppp_bkmk = NULL;
141                 *pi_bkmk = 0;
142                 i_ret = VLC_EGENERIC;
143             }
144             break;
145
146         case INPUT_CLEAR_BOOKMARKS:
147             if( p_input->i_bookmarks )
148             {
149                 int i;
150
151                 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
152                 {
153                     p_bkmk = p_input->pp_bookmarks[i];
154                     TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
155                                 p_bkmk );
156                     vlc_seekpoint_Delete( p_bkmk );
157                 }
158                 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
159             }
160             i_ret = VLC_SUCCESS;
161             break;
162
163         case INPUT_SET_BOOKMARK:
164             i_bkmk = (int)va_arg( args, int );
165             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
166             {
167                 vlc_value_t pos;
168                 vlc_mutex_unlock( &p_input->stream.stream_lock );
169                 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
170                     ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
171                       !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
172                 {
173                     pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
174                         (double)p_input->stream.p_selected_area->i_size;
175                     i_ret = var_Set( p_input, "position", pos );
176                 }
177                 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
178                 {
179                     pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
180                     i_ret = var_Set( p_input, "time", pos );
181                 }
182                 vlc_mutex_lock( &p_input->stream.stream_lock );
183             }
184             else
185             {
186                 i_ret = VLC_EGENERIC;
187             }
188             break;
189
190         case INPUT_GET_SUBDELAY:
191             pi = (int*)va_arg( args, int *);
192             /* We work on the first subtitle */
193             if( p_input->p_sys != NULL )
194             {
195                 if( p_input->p_sys->i_sub > 0 )
196                 {
197                     i_ret = var_Get( (vlc_object_t *)p_input->p_sys->sub[0],
198                                       "sub-delay", &val );
199                     *pi = val.i_int;
200                 }
201                 else
202                 {
203                     msg_Dbg( p_input,"no subtitle track");
204                     i_ret = VLC_EGENERIC;
205                 }
206             }
207             else
208             {
209                 i_ret = VLC_EGENERIC;
210             }
211             break;
212
213         case INPUT_SET_SUBDELAY:
214             i = (int)va_arg( args, int );
215             /* We work on the first subtitle */
216             if( p_input->p_sys )
217             {
218                 if( p_input->p_sys->i_sub > 0 )
219                 {
220                     val.i_int = i;
221                     i_ret = var_Set( (vlc_object_t *)p_input->p_sys->sub[0],
222                                       "sub-delay", val );
223                 }
224                 else
225                 {
226                     msg_Dbg( p_input,"no subtitle track");
227                     i_ret = VLC_EGENERIC;
228                 }
229             }
230             else
231             {
232                 i_ret = VLC_EGENERIC;
233             }
234             break;
235
236         default:
237             msg_Err( p_input, "unknown query in input_vaControl" );
238             i_ret = VLC_EGENERIC;
239             break;
240     }
241     vlc_mutex_unlock( &p_input->stream.stream_lock );
242
243     return i_ret;
244 }