1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rc.c,v 1.6 2002/10/03 13:21:55 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 );
72 add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
73 set_description( _("remote control interface module") );
74 set_capability( "interface", 20 );
75 set_callbacks( Activate, NULL );
78 /*****************************************************************************
79 * Activate: initialize and create stuff
80 *****************************************************************************/
81 static int Activate( vlc_object_t *p_this )
83 intf_thread_t *p_intf = (intf_thread_t*)p_this;
86 /* Check that stdin is a TTY */
87 if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
89 msg_Warn( p_intf, "fd 0 is not a TTY" );
94 /* Non-buffered stdout */
95 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
101 freopen( "CONOUT$", "w", stdout );
102 freopen( "CONOUT$", "w", stderr );
103 freopen( "CONIN$", "r", stdin );
104 printf( VERSION_MESSAGE "\n" );
107 printf( "remote control interface initialized, `h' for help\n" );
111 /*****************************************************************************
113 *****************************************************************************
114 * This part of the interface is in a separate thread so that we can call
115 * exec() from within it without annoying the rest of the program.
116 *****************************************************************************/
117 static void Run( intf_thread_t *p_intf )
119 input_thread_t * p_input;
120 playlist_t * p_playlist;
122 char p_buffer[ MAX_LINE_LENGTH + 1 ];
123 vlc_bool_t b_complete = 0;
124 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
125 input_info_category_t * p_category;
126 input_info_t * p_info;
131 fd_set fds; /* stdin changed? */
132 struct timeval tv; /* how long to wait */
139 while( !p_intf->b_die )
147 FD_SET( STDIN_FILENO, &fds );
149 i_dummy = select( 32, &fds, NULL, NULL, &tv );
154 while( !p_intf->b_die
155 && i_size < MAX_LINE_LENGTH
156 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
157 && p_buffer[ i_size ] != '\r'
158 && p_buffer[ i_size ] != '\n' )
163 if( i_size == MAX_LINE_LENGTH
164 || p_buffer[ i_size ] == '\r'
165 || p_buffer[ i_size ] == '\n' )
167 p_buffer[ i_size ] = 0;
172 /* Manage the input part */
173 if( p_input == NULL )
177 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
182 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
186 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
191 else if( p_input->b_dead )
193 vlc_object_release( p_input );
197 if( p_input && b_showpos )
200 vlc_mutex_lock( &p_input->stream.stream_lock );
201 if( !p_input->b_die && p_input->stream.i_mux_rate )
203 #define A p_input->stream.p_selected_area
204 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
205 i_newpos = A->i_tell * f_ratio;
207 if( i_oldpos != i_newpos )
210 printf( "pos: %li s / %li s\n", (long int)i_newpos,
211 (long int)(f_ratio * A->i_size) );
215 vlc_mutex_unlock( &p_input->stream.stream_lock );
218 /* Is there something to do? */
219 if( b_complete == 1 )
221 char *p_cmd = p_buffer;
224 if( !strcmp( p_cmd, "quit" ) )
226 p_intf->p_vlc->b_die = VLC_TRUE;
228 else if( !strcmp( p_cmd, "segfault" ) )
232 else if( !strcmp( p_cmd, "prev" ) )
234 if( p_playlist ) playlist_Prev( p_playlist );
236 else if( !strcmp( p_cmd, "next" ) )
238 if( p_playlist ) playlist_Next( p_playlist );
240 else if( !strcmp( p_cmd, "play" ) )
242 if( p_playlist ) playlist_Play( p_playlist );
244 else if( !strcmp( p_cmd, "stop" ) )
246 if( p_playlist ) playlist_Stop( p_playlist );
248 else if( !strcmp( p_cmd, "pause" ) )
250 if( p_input ) input_SetStatus( p_input, INPUT_STATUS_PAUSE );
252 else if( !strcmp( p_cmd, "tree" ) )
254 vlc_dumpstructure( p_intf->p_vlc );
256 else if( !strcmp( p_cmd, "list" ) )
258 vlc_liststructure( p_intf->p_vlc );
260 else if( !strncmp( p_cmd, "set ", 4 ) )
262 // vlc_set_r( p_intf->p_vlc, p_cmd + 4, strstr( p_cmd + 4, " " ) );
263 p_tmp = strstr( p_cmd + 4, " " );
265 config_PutPsz( p_intf->p_vlc, p_cmd + 4, p_tmp + 1 );
266 config_PutInt( p_intf->p_vlc, p_cmd + 4, atoi(p_tmp + 1) );
268 else if( !strncmp( p_cmd, "intf ", 5 ) )
270 intf_thread_t *p_newintf;
271 char *psz_oldmodule = config_GetPsz( p_intf->p_vlc, "intf" );
273 config_PutPsz( p_intf->p_vlc, "intf", p_cmd + 5 );
274 p_newintf = intf_Create( p_intf->p_vlc );
275 config_PutPsz( p_intf->p_vlc, "intf", psz_oldmodule );
279 free( psz_oldmodule );
284 p_newintf->b_block = VLC_FALSE;
285 if( intf_RunThread( p_newintf ) )
287 vlc_object_detach( p_newintf );
288 intf_Destroy( p_newintf );
292 else if( !strcmp( p_cmd, "info" ) )
296 vlc_mutex_lock( &p_input->stream.stream_lock );
297 p_category = p_input->stream.p_info;
300 printf( "+----[ %s ]\n", p_category->psz_name );
302 p_info = p_category->p_info;
305 printf( "| %s: %s\n", p_info->psz_name,
307 p_info = p_info->p_next;
309 p_category = p_category->p_next;
312 printf( "+----[ end of stream info ]\n" );
313 vlc_mutex_unlock( &p_input->stream.stream_lock );
317 printf( "no input\n" );
320 else switch( p_cmd[0] )
324 if( p_cmd[1] == ' ' && p_playlist )
326 playlist_Add( p_playlist, p_cmd + 2,
327 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
335 vout_thread_t *p_vout;
336 p_vout = vlc_object_find( p_input,
337 VLC_OBJECT_VOUT, FIND_CHILD );
341 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
342 vlc_object_release( p_vout );
357 i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
358 && p_cmd[ i_dummy ] <= '9';
364 p_cmd[ i_dummy ] = 0;
365 input_Seek( p_input, (off_t)atoi( p_cmd + 1 ),
366 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
367 /* rcreseek(f_cpos); */
374 printf("+----[ remote control commands ]\n");
376 printf("| a XYZ . . . . . . . . . . . add XYZ to playlist\n");
377 printf("| play . . . . . . . . . . . . . . . . play stream\n");
378 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
379 printf("| next . . . . . . . . . . . . next playlist item\n");
380 printf("| prev . . . . . . . . . . previous playlist item\n");
382 printf("| r X . . . seek in seconds, for instance `r 3.5'\n");
383 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
384 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
385 printf("| info . . . information about the current stream\n");
387 printf("| help . . . . . . . . . . . . . this help message\n");
388 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
390 printf("+----[ end of help ]\n");
393 /* Ignore empty lines */
396 printf( "unknown command `%s', type `help' for help\n", p_cmd );
401 msleep( INTF_IDLE_SLEEP );
406 vlc_object_release( p_input );
412 vlc_object_release( p_playlist );