]> git.sesse.net Git - vlc/blob - src/input/control.c
* src/input/*:
[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
30 /****************************************************************************
31  * input_Control
32  ****************************************************************************/
33 /**
34  * Control function for inputs.
35  * \param p_input input handle
36  * \param i_query query type
37  * \return VLC_SUCESS if ok
38  */
39 int input_Control( input_thread_t *p_input, int i_query, ...  )
40 {
41     va_list args;
42     int     i_result;
43
44     va_start( args, i_query );
45     i_result = input_vaControl( p_input, i_query, args );
46     va_end( args );
47
48     return i_result;
49 }
50
51 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
52 {
53     int     i_ret;
54     seekpoint_t *p_bkmk, ***ppp_bkmk;
55     int i_bkmk, *pi_bkmk;
56     vlc_value_t val, text;
57
58     vlc_mutex_lock( &p_input->stream.stream_lock );
59     switch( i_query )
60     {
61         case INPUT_ADD_BOOKMARK:
62             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
63             p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
64             if( !p_bkmk->psz_name )
65             {
66                  asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
67                            p_input->i_bookmarks );
68             }
69             TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
70
71             /* Reflect the changes on the object var */
72             var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
73             {
74                 int i;
75                 for( i = 0; i < p_input->i_bookmarks; i++ )
76                 {
77                     val.i_int = i;
78                     text.psz_string = p_input->pp_bookmarks[i]->psz_name;
79                     var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
80                                 &val, &text );
81                 }
82             }
83
84             i_ret = VLC_SUCCESS;
85             break;
86
87         case INPUT_DEL_BOOKMARK:
88             p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
89             if( p_input->i_bookmarks )
90             {
91                 int i;
92                 for( i = 0; i < p_input->i_bookmarks; i++ )
93                 {
94                     if( ( p_bkmk->i_byte_offset &&
95                           p_input->pp_bookmarks[i]->i_byte_offset ==
96                             p_bkmk->i_byte_offset ) ||
97                         ( p_bkmk->i_time_offset &&
98                           p_input->pp_bookmarks[i]->i_time_offset ==
99                             p_bkmk->i_time_offset ) ||
100                         ( !p_bkmk->i_byte_offset && !p_bkmk->i_time_offset &&
101                           p_input->pp_bookmarks[i]->i_byte_offset ==
102                             p_bkmk->i_byte_offset ) )
103                     {
104                         p_bkmk = p_input->pp_bookmarks[i];
105                         break;
106                     }
107                 }
108                 if( i < p_input->i_bookmarks )
109                 {
110                     TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
111                                 p_bkmk );
112                     vlc_seekpoint_Delete( p_bkmk );
113
114                     /* Reflect the changes on the object var */
115                     var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES,
116                                 0, 0 );
117                     for( i = 0; i < p_input->i_bookmarks; i++ )
118                     {
119                         val.i_int = i;
120                         text.psz_string = p_input->pp_bookmarks[i]->psz_name;
121                         var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
122                                     &val, &text );
123                     }
124                 }
125             }
126             i_ret = VLC_SUCCESS;
127             break;
128
129         case INPUT_GET_BOOKMARKS:
130             ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
131             pi_bkmk = (int *)va_arg( args, int * );
132             if( p_input->i_bookmarks )
133             {
134                 int i;
135
136                 *pi_bkmk = p_input->i_bookmarks;
137                 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
138                               p_input->i_bookmarks );
139                 for( i = 0; i < p_input->i_bookmarks; i++ )
140                 {
141                     (*ppp_bkmk)[i] =
142                         vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
143                 }
144                 i_ret = VLC_SUCCESS;
145             }
146             else
147             {
148                 *ppp_bkmk = NULL;
149                 *pi_bkmk = 0;
150                 i_ret = VLC_EGENERIC;
151             }
152             break;
153
154         case INPUT_CLEAR_BOOKMARKS:
155             if( p_input->i_bookmarks )
156             {
157                 int i;
158
159                 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
160                 {
161                     p_bkmk = p_input->pp_bookmarks[i];
162                     TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
163                                 p_bkmk );
164                     vlc_seekpoint_Delete( p_bkmk );
165                 }
166             }
167             i_ret = VLC_SUCCESS;
168             break;
169
170         case INPUT_SET_BOOKMARK:
171             i_bkmk = (int)va_arg( args, int );
172             if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
173             {
174                 vlc_value_t pos;
175                 vlc_mutex_unlock( &p_input->stream.stream_lock );
176                 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
177                     ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
178                       !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
179                 {
180                     pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
181                         (double)p_input->stream.p_selected_area->i_size;
182                     i_ret = var_Set( p_input, "position", pos );
183                 }
184                 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
185                 {
186                     pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
187                     i_ret = var_Set( p_input, "time", pos );
188                 }
189                 vlc_mutex_lock( &p_input->stream.stream_lock );
190             }
191             else
192             {
193                 i_ret = VLC_EGENERIC;
194             }
195             break;
196
197         default:
198             msg_Err( p_input, "unknown query in input_vaControl" );
199             i_ret = VLC_EGENERIC;
200             break;
201     }
202     vlc_mutex_unlock( &p_input->stream.stream_lock );
203
204     return i_ret;
205 }