1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 #include <vlc/input.h>
28 #include "input_internal.h"
29 #include "vlc_playlist.h"
32 static void UpdateBookmarksOption( input_thread_t * );
33 static void NotifyPlaylist( input_thread_t * );
35 /****************************************************************************
37 ****************************************************************************/
39 * Control function for inputs.
40 * \param p_input input handle
41 * \param i_query query type
42 * \return VLC_SUCESS if ok
44 int input_Control( input_thread_t *p_input, int i_query, ... )
49 va_start( args, i_query );
50 i_result = input_vaControl( p_input, i_query, args );
56 int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
58 /* seekpoint_t *p_bkmk, ***ppp_bkmk;
68 case INPUT_GET_POSITION:
69 pf = (double*)va_arg( args, double * );
70 *pf = var_GetFloat( p_input, "position" );
73 case INPUT_SET_POSITION:
74 f = (double)va_arg( args, double );
75 return var_SetFloat( p_input, "position", f );
77 case INPUT_GET_LENGTH:
78 pi_64 = (int64_t*)va_arg( args, int64_t * );
79 *pi_64 = var_GetTime( p_input, "length" );
83 pi_64 = (int64_t*)va_arg( args, int64_t * );
84 *pi_64 = var_GetTime( p_input, "time" );
88 i_64 = (int64_t)va_arg( args, int64_t );
89 return var_SetTime( p_input, "time", i_64 );
92 pi_int = (int*)va_arg( args, int * );
93 *pi_int = var_GetInteger( p_input, "rate" );
97 i_int = (int)va_arg( args, int );
98 return var_SetInteger( p_input, "rate", i_int );
100 case INPUT_GET_STATE:
101 pi_int = (int*)va_arg( args, int * );
102 *pi_int = var_GetInteger( p_input, "state" );
105 case INPUT_SET_STATE:
106 i_int = (int)va_arg( args, int );
107 return var_SetInteger( p_input, "state", i_int );
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" );
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" );
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 );
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 );
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 * );
133 info_category_t *p_cat;
137 vlc_mutex_lock( &p_input->input.p_item->lock );
138 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
140 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
145 if( i == p_input->input.p_item->i_categories )
147 p_cat = malloc( sizeof( info_category_t ) );
150 p_cat->psz_name = strdup( psz_cat );
152 p_cat->pp_infos = NULL;
153 INSERT_ELEM( p_input->input.p_item->pp_categories,
154 p_input->input.p_item->i_categories,
155 p_input->input.p_item->i_categories, p_cat );
158 p_cat = p_input->input.p_item->pp_categories[i];
160 for( i = 0; i < p_cat->i_infos; i++ )
162 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
164 if( p_cat->pp_infos[i]->psz_value )
165 free( p_cat->pp_infos[i]->psz_value );
170 if( i == p_cat->i_infos )
172 p_info = malloc( sizeof( info_t ) );
175 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
176 p_cat->i_infos, p_info );
177 p_info->psz_name = strdup( psz_name );
180 p_info = p_cat->pp_infos[i];
181 vasprintf( &p_info->psz_value, psz_format, args );
183 vlc_mutex_unlock( &p_input->input.p_item->lock );
185 NotifyPlaylist( p_input );
191 char *psz_cat = (char *)va_arg( args, char * );
192 char *psz_name = (char *)va_arg( args, char * );
193 char **ppsz_value = (char **)va_arg( args, char ** );
194 int i_ret = VLC_EGENERIC;
198 vlc_mutex_lock( &p_input->input.p_item->lock );
199 for( i = 0; i < p_input->input.p_item->i_categories; i++ )
201 if( !strcmp( p_input->input.p_item->pp_categories[i]->psz_name,
206 if( i != p_input->input.p_item->i_categories )
208 info_category_t *p_cat;
209 p_cat = p_input->input.p_item->pp_categories[i];
211 for( i = 0; i < p_cat->i_infos; i++ )
213 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
215 if( p_cat->pp_infos[i]->psz_value )
217 *ppsz_value =strdup(p_cat->pp_infos[i]->psz_value);
224 vlc_mutex_unlock( &p_input->input.p_item->lock );
230 char *psz_name = (char *)va_arg( args, char * );
235 vlc_mutex_lock( &p_input->input.p_item->lock );
236 if( p_input->input.p_item->psz_name )
237 free( p_input->input.p_item->psz_name );
238 p_input->input.p_item->psz_name = strdup( psz_name );
239 vlc_mutex_unlock( &p_input->input.p_item->lock );
241 NotifyPlaylist( p_input );
247 case INPUT_ADD_BOOKMARK:
248 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
249 p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
250 if( !p_bkmk->psz_name )
252 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
253 p_input->i_bookmarks );
255 TAB_APPEND( p_input->i_bookmarks, p_input->pp_bookmarks, p_bkmk );
257 /* Reflect the changes on the object var */
258 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
261 for( i = 0; i < p_input->i_bookmarks; i++ )
264 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
265 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
270 UpdateBookmarksOption( p_input );
275 case INPUT_CHANGE_BOOKMARK:
276 p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
277 i_bkmk = (int)va_arg( args, int );
279 if( i_bkmk < p_input->i_bookmarks )
281 p_input->pp_bookmarks[i_bkmk] = p_bkmk;
283 /* Reflect the changes on the object var */
284 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
285 for( i = 0; i < p_input->i_bookmarks; i++ )
288 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
289 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
293 UpdateBookmarksOption( p_input );
298 case INPUT_DEL_BOOKMARK:
299 i_bkmk = (int)va_arg( args, int );
300 if( i_bkmk < p_input->i_bookmarks )
303 p_bkmk = p_input->pp_bookmarks[i_bkmk];
304 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
306 vlc_seekpoint_Delete( p_bkmk );
308 /* Reflect the changes on the object var */
309 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
310 for( i = 0; i < p_input->i_bookmarks; i++ )
313 text.psz_string = p_input->pp_bookmarks[i]->psz_name;
314 var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
318 UpdateBookmarksOption( p_input );
322 else i_ret = VLC_EGENERIC;
325 case INPUT_GET_BOOKMARKS:
326 ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
327 pi_bkmk = (int *)va_arg( args, int * );
328 if( p_input->i_bookmarks )
332 *pi_bkmk = p_input->i_bookmarks;
333 *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
334 p_input->i_bookmarks );
335 for( i = 0; i < p_input->i_bookmarks; i++ )
338 vlc_seekpoint_Duplicate(p_input->pp_bookmarks[i]);
346 i_ret = VLC_EGENERIC;
350 case INPUT_CLEAR_BOOKMARKS:
351 if( p_input->i_bookmarks )
355 for( i = p_input->i_bookmarks - 1; i >= 0; i-- )
357 p_bkmk = p_input->pp_bookmarks[i];
358 TAB_REMOVE( p_input->i_bookmarks, p_input->pp_bookmarks,
360 vlc_seekpoint_Delete( p_bkmk );
362 var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
365 UpdateBookmarksOption( p_input );
370 case INPUT_SET_BOOKMARK:
371 i_bkmk = (int)va_arg( args, int );
372 if( i_bkmk >= 0 && i_bkmk < p_input->i_bookmarks )
375 vlc_mutex_unlock( &p_input->stream.stream_lock );
376 if( p_input->pp_bookmarks[i_bkmk]->i_byte_offset ||
377 ( !p_input->pp_bookmarks[i_bkmk]->i_byte_offset &&
378 !p_input->pp_bookmarks[i_bkmk]->i_time_offset ) )
380 pos.f_float = p_input->pp_bookmarks[i_bkmk]->i_byte_offset/
381 (double)p_input->stream.p_selected_area->i_size;
382 i_ret = var_Set( p_input, "position", pos );
384 else if( p_input->pp_bookmarks[i_bkmk]->i_time_offset )
386 pos.i_time = p_input->pp_bookmarks[i_bkmk]->i_time_offset;
387 i_ret = var_Set( p_input, "time", pos );
389 vlc_mutex_lock( &p_input->stream.stream_lock );
393 i_ret = VLC_EGENERIC;
398 case INPUT_GET_BOOKMARKS:
399 case INPUT_CLEAR_BOOKMARKS:
400 case INPUT_ADD_BOOKMARK:
401 case INPUT_CHANGE_BOOKMARK:
402 case INPUT_DEL_BOOKMARK:
403 case INPUT_SET_BOOKMARK:
405 msg_Err( p_input, "unimplemented query in input_vaControl" );
407 msg_Err( p_input, "unknown query in input_vaControl" );
412 static void NotifyPlaylist( input_thread_t *p_input )
414 playlist_t *p_playlist =
415 (playlist_t *)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
419 var_SetInteger( p_playlist, "item-change", p_playlist->i_index );
420 vlc_object_release( p_playlist );
424 static void UpdateBookmarksOption( input_thread_t *p_input )
428 char *psz_value = NULL, *psz_next = NULL;
430 vlc_mutex_unlock( &p_input->stream.stream_lock );
432 for( i = 0; i < p_input->i_bookmarks; i++ )
434 asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
435 p_input->pp_bookmarks[i]->psz_name,
436 p_input->pp_bookmarks[i]->i_byte_offset,
437 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
438 i_len += strlen( psz_value );
441 for( i = 0; i < p_input->i_bookmarks; i++ )
443 if( !i ) psz_value = psz_next = malloc( i_len + p_input->i_bookmarks );
445 sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
446 p_input->pp_bookmarks[i]->psz_name,
447 p_input->pp_bookmarks[i]->i_byte_offset,
448 p_input->pp_bookmarks[i]->i_time_offset/1000000 );
450 psz_next += strlen( psz_next );
451 if( i < p_input->i_bookmarks - 1)
453 *psz_next = ','; psz_next++;
456 input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
457 psz_value ? psz_value : "" );
459 vlc_mutex_lock( &p_input->stream.stream_lock );