1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rc.c,v 1.8 2002/10/11 22:32:56 sam Exp $
7 * Authors: Peter Surda <shurdeek@panorama.sth.ac.at>
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
46 #include <sys/types.h>
49 #include <winsock2.h> /* select() */
52 #define MAX_LINE_LENGTH 256
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Activate ( vlc_object_t * );
58 static void Run ( intf_thread_t *p_intf );
60 /*****************************************************************************
62 *****************************************************************************/
63 #define POS_TEXT N_("show stream position")
64 #define POS_LONGTEXT N_("Show the current position in seconds within the stream from time to time.")
66 #define TTY_TEXT N_("fake TTY")
67 #define TTY_LONGTEXT N_("Force the rc plugin to use stdin as if it was a TTY.")
70 add_category_hint( N_("Remote control"), NULL );
71 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
73 add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
75 set_description( _("remote control interface module") );
76 set_capability( "interface", 20 );
77 set_callbacks( Activate, NULL );
80 /*****************************************************************************
81 * Activate: initialize and create stuff
82 *****************************************************************************/
83 static int Activate( vlc_object_t *p_this )
85 intf_thread_t *p_intf = (intf_thread_t*)p_this;
88 /* Check that stdin is a TTY */
89 if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
91 msg_Warn( p_intf, "fd 0 is not a TTY" );
96 /* Non-buffered stdout */
97 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
103 printf( "remote control interface initialized, `h' for help\n" );
107 /*****************************************************************************
109 *****************************************************************************
110 * This part of the interface is in a separate thread so that we can call
111 * exec() from within it without annoying the rest of the program.
112 *****************************************************************************/
113 static void Run( intf_thread_t *p_intf )
115 input_thread_t * p_input;
116 playlist_t * p_playlist;
118 char p_buffer[ MAX_LINE_LENGTH + 1 ];
119 vlc_bool_t b_complete = 0;
120 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
121 input_info_category_t * p_category;
122 input_info_t * p_info;
127 fd_set fds; /* stdin changed? */
128 struct timeval tv; /* how long to wait */
135 var_Create( p_intf, "foo", VLC_VAR_STRING );
136 var_Set( p_intf, "foo", (vlc_value_t)"test" );
138 while( !p_intf->b_die )
146 FD_SET( STDIN_FILENO, &fds );
148 i_dummy = select( 32, &fds, NULL, NULL, &tv );
153 while( !p_intf->b_die
154 && i_size < MAX_LINE_LENGTH
155 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
156 && p_buffer[ i_size ] != '\r'
157 && p_buffer[ i_size ] != '\n' )
162 if( i_size == MAX_LINE_LENGTH
163 || p_buffer[ i_size ] == '\r'
164 || p_buffer[ i_size ] == '\n' )
166 p_buffer[ i_size ] = 0;
171 /* Manage the input part */
172 if( p_input == NULL )
176 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
181 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
185 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
190 else if( p_input->b_dead )
192 vlc_object_release( p_input );
196 if( p_input && b_showpos )
199 vlc_mutex_lock( &p_input->stream.stream_lock );
200 if( !p_input->b_die && p_input->stream.i_mux_rate )
202 #define A p_input->stream.p_selected_area
203 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
204 i_newpos = A->i_tell * f_ratio;
206 if( i_oldpos != i_newpos )
209 printf( "pos: %li s / %li s\n", (long int)i_newpos,
210 (long int)(f_ratio * A->i_size) );
214 vlc_mutex_unlock( &p_input->stream.stream_lock );
217 /* Is there something to do? */
218 if( b_complete == 1 )
220 char *p_cmd = p_buffer;
222 if( !strcmp( p_cmd, "quit" ) )
224 p_intf->p_vlc->b_die = VLC_TRUE;
226 else if( !strcmp( p_cmd, "segfault" ) )
230 else if( !strcmp( p_cmd, "prev" ) )
232 if( p_playlist ) playlist_Prev( p_playlist );
234 else if( !strcmp( p_cmd, "next" ) )
236 if( p_playlist ) playlist_Next( p_playlist );
238 else if( !strcmp( p_cmd, "play" ) )
240 if( p_playlist ) playlist_Play( p_playlist );
242 else if( !strcmp( p_cmd, "stop" ) )
244 if( p_playlist ) playlist_Stop( p_playlist );
246 else if( !strcmp( p_cmd, "pause" ) )
248 if( p_input ) input_SetStatus( p_input, INPUT_STATUS_PAUSE );
250 else if( !strcmp( p_cmd, "tree" ) )
252 vlc_dumpstructure( p_intf->p_vlc );
254 else if( !strcmp( p_cmd, "list" ) )
256 vlc_liststructure( p_intf->p_vlc );
258 else if( !strncmp( p_cmd, "setfoo ", 7 ) )
261 value.psz_string = p_cmd + 7;
262 var_Set( p_intf, "foo", value );
264 else if( !strncmp( p_cmd, "getfoo", 6 ) )
267 var_Get( p_intf, "foo", &value );
268 printf( "current value is '%s'\n", value.psz_string );
270 else if( !strncmp( p_cmd, "intf ", 5 ) )
272 intf_thread_t *p_newintf;
273 char *psz_oldmodule = config_GetPsz( p_intf->p_vlc, "intf" );
275 config_PutPsz( p_intf->p_vlc, "intf", p_cmd + 5 );
276 p_newintf = intf_Create( p_intf->p_vlc );
277 config_PutPsz( p_intf->p_vlc, "intf", psz_oldmodule );
281 free( psz_oldmodule );
286 p_newintf->b_block = VLC_FALSE;
287 if( intf_RunThread( p_newintf ) )
289 vlc_object_detach( p_newintf );
290 intf_Destroy( p_newintf );
294 else if( !strcmp( p_cmd, "info" ) )
298 vlc_mutex_lock( &p_input->stream.stream_lock );
299 p_category = p_input->stream.p_info;
302 printf( "+----[ %s ]\n", p_category->psz_name );
304 p_info = p_category->p_info;
307 printf( "| %s: %s\n", p_info->psz_name,
309 p_info = p_info->p_next;
311 p_category = p_category->p_next;
314 printf( "+----[ end of stream info ]\n" );
315 vlc_mutex_unlock( &p_input->stream.stream_lock );
319 printf( "no input\n" );
322 else switch( p_cmd[0] )
326 if( p_cmd[1] == ' ' && p_playlist )
328 playlist_Add( p_playlist, p_cmd + 2,
329 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
337 vout_thread_t *p_vout;
338 p_vout = vlc_object_find( p_input,
339 VLC_OBJECT_VOUT, FIND_CHILD );
343 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
344 vlc_object_release( p_vout );
359 i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
360 && p_cmd[ i_dummy ] <= '9';
366 p_cmd[ i_dummy ] = 0;
367 input_Seek( p_input, (off_t)atoi( p_cmd + 1 ),
368 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
369 /* rcreseek(f_cpos); */
376 printf("+----[ remote control commands ]\n");
378 printf("| a XYZ . . . . . . . . . . . add XYZ to playlist\n");
379 printf("| play . . . . . . . . . . . . . . . . play stream\n");
380 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
381 printf("| next . . . . . . . . . . . . next playlist item\n");
382 printf("| prev . . . . . . . . . . previous playlist item\n");
384 printf("| r X . . . seek in seconds, for instance `r 3.5'\n");
385 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
386 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
387 printf("| info . . . information about the current stream\n");
389 printf("| help . . . . . . . . . . . . . this help message\n");
390 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
392 printf("+----[ end of help ]\n");
395 /* Ignore empty lines */
398 printf( "unknown command `%s', type `help' for help\n", p_cmd );
406 vlc_object_release( p_input );
412 vlc_object_release( p_playlist );
416 var_Destroy( p_intf, "foo" );