1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rc.c,v 1.4 2002/08/20 18:08:51 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.")
67 add_category_hint( N_("Remote control"), NULL );
68 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
69 set_description( _("remote control interface module") );
70 set_capability( "interface", 20 );
71 set_callbacks( Activate, NULL );
74 /*****************************************************************************
75 * Activate: initialize and create stuff
76 *****************************************************************************/
77 static int Activate( vlc_object_t *p_this )
79 intf_thread_t *p_intf = (intf_thread_t*)p_this;
82 /* Check that stdin is a TTY */
85 msg_Warn( p_intf, "fd 0 is not a TTY" );
90 /* Non-buffered stdout */
91 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
97 freopen( "CONOUT$", "w", stdout );
98 freopen( "CONOUT$", "w", stderr );
99 freopen( "CONIN$", "r", stdin );
100 printf( VERSION_MESSAGE "\n" );
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 while( !p_intf->b_die )
143 FD_SET( STDIN_FILENO, &fds );
145 i_dummy = select( 32, &fds, NULL, NULL, &tv );
150 while( !p_intf->b_die
151 && i_size < MAX_LINE_LENGTH
152 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
153 && p_buffer[ i_size ] != '\r'
154 && p_buffer[ i_size ] != '\n' )
159 if( i_size == MAX_LINE_LENGTH
160 || p_buffer[ i_size ] == '\r'
161 || p_buffer[ i_size ] == '\n' )
163 p_buffer[ i_size ] = 0;
168 /* Manage the input part */
169 if( p_input == NULL )
173 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
178 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
182 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
187 else if( p_input->b_dead )
189 vlc_object_release( p_input );
193 if( p_input && b_showpos )
196 vlc_mutex_lock( &p_input->stream.stream_lock );
197 if( !p_input->b_die && p_input->stream.i_mux_rate )
199 #define A p_input->stream.p_selected_area
200 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
201 i_newpos = A->i_tell * f_ratio;
203 if( i_oldpos != i_newpos )
206 printf( "pos: %li s / %li s\n", (long int)i_newpos,
207 (long int)(f_ratio * A->i_size) );
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
214 /* Is there something to do? */
215 if( b_complete == 1 )
217 char *p_cmd = p_buffer;
220 if( !strcmp( p_cmd, "quit" ) )
222 p_intf->p_vlc->b_die = VLC_TRUE;
224 else if( !strcmp( p_cmd, "segfault" ) )
228 else if( !strcmp( p_cmd, "prev" ) )
230 if( p_playlist ) playlist_Prev( p_playlist );
232 else if( !strcmp( p_cmd, "next" ) )
234 if( p_playlist ) playlist_Next( p_playlist );
236 else if( !strcmp( p_cmd, "play" ) )
238 if( p_playlist ) playlist_Play( p_playlist );
240 else if( !strcmp( p_cmd, "stop" ) )
242 if( p_playlist ) playlist_Stop( p_playlist );
244 else if( !strcmp( p_cmd, "pause" ) )
246 if( p_input ) input_SetStatus( p_input, INPUT_STATUS_PAUSE );
248 else if( !strcmp( p_cmd, "tree" ) )
250 vlc_dumpstructure( p_intf->p_vlc );
252 else if( !strcmp( p_cmd, "list" ) )
254 vlc_liststructure( p_intf->p_vlc );
256 else if( !strncmp( p_cmd, "set ", 4 ) )
259 // vlc_set_r( p_intf->p_vlc, p_cmd + 4, strstr( p_cmd + 4, " " ) );
260 p_tmp = strstr( p_cmd + 4, " " );
262 config_PutPsz( p_intf->p_vlc, p_cmd + 4, p_tmp + 1 );
263 config_PutInt( p_intf->p_vlc, p_cmd + 4, atoi(p_tmp + 1) );
266 else if( !strncmp( p_cmd, "intf ", 5 ) )
268 intf_thread_t *p_newintf;
269 char *psz_oldmodule = config_GetPsz( p_intf->p_vlc, "intf" );
271 config_PutPsz( p_intf->p_vlc, "intf", p_cmd + 5 );
272 p_newintf = intf_Create( p_intf->p_vlc );
273 config_PutPsz( p_intf->p_vlc, "intf", psz_oldmodule );
277 free( psz_oldmodule );
282 p_newintf->b_block = VLC_FALSE;
283 if( intf_RunThread( p_newintf ) )
285 vlc_object_detach( p_newintf );
286 intf_Destroy( p_newintf );
290 else if( !strcmp( p_cmd, "info" ) )
294 vlc_mutex_lock( &p_input->stream.stream_lock );
295 p_category = p_input->stream.p_info;
298 printf( "+----[ %s ]\n", p_category->psz_name );
300 p_info = p_category->p_info;
303 printf( "| %s: %s\n", p_info->psz_name,
305 p_info = p_info->p_next;
307 p_category = p_category->p_next;
310 printf( "+----[ end of stream info ]\n" );
311 vlc_mutex_unlock( &p_input->stream.stream_lock );
315 printf( "no input\n" );
318 else switch( p_cmd[0] )
322 if( p_cmd[1] == ' ' && p_playlist )
324 playlist_Add( p_playlist, p_cmd + 2,
325 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
333 vout_thread_t *p_vout;
334 p_vout = vlc_object_find( p_input,
335 VLC_OBJECT_VOUT, FIND_CHILD );
339 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
340 vlc_object_release( p_vout );
355 i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
356 && p_cmd[ i_dummy ] <= '9';
362 p_cmd[ i_dummy ] = 0;
363 input_Seek( p_input, (off_t)atoi( p_cmd + 1 ),
364 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
365 /* rcreseek(f_cpos); */
372 printf("+----[ remote control commands ]\n");
374 printf("| a XYZ . . . . . . . . . . . add XYZ to playlist\n");
375 printf("| play . . . . . . . . . . . . . . . . play stream\n");
376 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
377 printf("| next . . . . . . . . . . . . next playlist item\n");
378 printf("| prev . . . . . . . . . . previous playlist item\n");
380 printf("| r X . . . seek in seconds, for instance `r 3.5'\n");
381 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
382 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
383 printf("| info . . . information about the current stream\n");
385 printf("| help . . . . . . . . . . . . . this help message\n");
386 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
388 printf("+----[ end of help ]\n");
391 /* Ignore empty lines */
394 printf( "unknown command `%s', type `help' for help\n", p_cmd );
399 msleep( INTF_IDLE_SLEEP );
404 vlc_object_release( p_input );
409 vlc_object_release( p_playlist );