1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Author: 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 */
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
47 #include <sys/types.h>
49 #include "vlc_error.h"
51 #define MAX_LINE_LENGTH 256
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Activate ( vlc_object_t * );
57 static void Run ( intf_thread_t *p_intf );
59 static int Input ( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
61 static int Playlist ( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
63 static int Quit ( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int Intf ( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int Volume ( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
69 static int VolumeMove ( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
71 static int AudioConfig ( vlc_object_t *, char const *,
72 vlc_value_t, vlc_value_t, void * );
74 /*****************************************************************************
76 *****************************************************************************/
77 #define POS_TEXT N_("Show stream position")
78 #define POS_LONGTEXT N_("Show the current position in seconds within the stream from time to time.")
80 #define TTY_TEXT N_("Fake TTY")
81 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
84 set_description( _("Remote control interface") );
85 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
87 add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
89 set_capability( "interface", 20 );
90 set_callbacks( Activate, NULL );
93 /*****************************************************************************
94 * Activate: initialize and create stuff
95 *****************************************************************************/
96 static int Activate( vlc_object_t *p_this )
98 intf_thread_t *p_intf = (intf_thread_t*)p_this;
100 #if defined(HAVE_ISATTY) && !defined(WIN32)
101 /* Check that stdin is a TTY */
102 if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
104 msg_Warn( p_intf, "fd 0 is not a TTY" );
109 /* Non-buffered stdout */
110 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
112 p_intf->pf_run = Run;
116 printf( "Remote control interface initialized, `h' for help\n" );
120 /*****************************************************************************
122 *****************************************************************************
123 * This part of the interface is in a separate thread so that we can call
124 * exec() from within it without annoying the rest of the program.
125 *****************************************************************************/
126 static void Run( intf_thread_t *p_intf )
128 input_thread_t * p_input;
129 playlist_t * p_playlist;
131 char p_buffer[ MAX_LINE_LENGTH + 1 ];
132 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
133 input_info_category_t * p_category;
134 input_info_t * p_info;
140 double f_ratio = 1.0;
144 INPUT_RECORD input_record;
151 /* Register commands that will be cleaned up upon object destruction */
152 var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
153 var_AddCallback( p_intf, "quit", Quit, NULL );
154 var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
155 var_AddCallback( p_intf, "intf", Intf, NULL );
157 var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
158 var_AddCallback( p_intf, "add", Playlist, NULL );
159 var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
160 var_AddCallback( p_intf, "playlist", Playlist, NULL );
161 var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
162 var_AddCallback( p_intf, "play", Playlist, NULL );
163 var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
164 var_AddCallback( p_intf, "stop", Playlist, NULL );
165 var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
166 var_AddCallback( p_intf, "prev", Playlist, NULL );
167 var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
168 var_AddCallback( p_intf, "next", Playlist, NULL );
170 var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_intf, "pause", Input, NULL );
172 var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
173 var_AddCallback( p_intf, "seek", Input, NULL );
174 var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
175 var_AddCallback( p_intf, "title", Input, NULL );
176 var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
177 var_AddCallback( p_intf, "title_n", Input, NULL );
178 var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
179 var_AddCallback( p_intf, "title_p", Input, NULL );
180 var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_intf, "chapter", Input, NULL );
182 var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_intf, "chapter_n", Input, NULL );
184 var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
185 var_AddCallback( p_intf, "chapter_p", Input, NULL );
187 var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
188 var_AddCallback( p_intf, "volume", Volume, NULL );
189 var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_intf, "volup", VolumeMove, NULL );
191 var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
193 var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_intf, "adev", AudioConfig, NULL );
195 var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196 var_AddCallback( p_intf, "achan", AudioConfig, NULL );
199 /* Get the file descriptor of the console input */
200 hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
201 if( hConsoleIn == INVALID_HANDLE_VALUE )
203 msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
204 p_intf->b_die = VLC_TRUE;
208 while( !p_intf->b_die )
210 vlc_bool_t b_complete = VLC_FALSE;
218 tv.tv_usec = (long)INTF_IDLE_SLEEP;
220 FD_SET( STDIN_FILENO, &fds );
222 i_dummy = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
224 /* On Win32, select() only works on socket descriptors */
225 i_dummy = ( WaitForSingleObject( hConsoleIn, INTF_IDLE_SLEEP/1000 )
232 while( !p_intf->b_die
233 && i_size < MAX_LINE_LENGTH
235 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
237 && ReadConsoleInput( hConsoleIn, &input_record, 1,
243 if( input_record.EventType != KEY_EVENT ||
244 !input_record.Event.KeyEvent.bKeyDown ||
245 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
246 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
247 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
248 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
250 /* nothing interesting */
255 input_record.Event.KeyEvent.uChar.AsciiChar;
257 /* Echo out the command */
258 putc( p_buffer[ i_size ], stdout );
260 /* Handle special keys */
261 if( p_buffer[ i_size ] == '\r' || p_buffer[ i_size ] == '\n' )
263 putc( '\n', stdout );
266 switch( p_buffer[ i_size ] )
273 putc( '\b', stdout );
284 if( p_buffer[ i_size ] == '\r' || p_buffer[ i_size ] == '\n' )
293 if( i_size == MAX_LINE_LENGTH
294 || p_buffer[ i_size ] == '\r'
295 || p_buffer[ i_size ] == '\n' )
297 p_buffer[ i_size ] = 0;
298 b_complete = VLC_TRUE;
302 /* Manage the input part */
303 if( p_input == NULL )
307 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
312 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
316 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
321 else if( p_input->b_dead )
323 vlc_object_release( p_input );
327 if( p_input && b_showpos )
330 vlc_mutex_lock( &p_input->stream.stream_lock );
331 if( !p_input->b_die && p_input->stream.i_mux_rate )
333 #define A p_input->stream.p_selected_area
334 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
335 i_newpos = A->i_tell * f_ratio;
337 if( i_oldpos != i_newpos )
340 printf( "pos: %li s / %li s\n", (long int)i_newpos,
341 (long int)(f_ratio * A->i_size) );
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
348 /* Is there something to do? */
351 char *psz_cmd, *psz_arg;
353 /* Skip heading spaces */
355 while( *psz_cmd == ' ' )
360 /* Split psz_cmd at the first space and make sure that
361 * psz_arg is valid */
362 psz_arg = strchr( psz_cmd, ' ' );
366 while( *psz_arg == ' ' )
376 /* If the user typed a registered local command, try it */
377 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
382 val.psz_string = psz_arg;
383 i_ret = var_Set( p_intf, psz_cmd, val );
384 printf( "%s: returned %i (%s)\n",
385 psz_cmd, i_ret, vlc_error( i_ret ) );
387 /* Or maybe it's a global command */
388 else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
393 val.psz_string = psz_arg;
394 /* FIXME: it's a global command, but we should pass the
395 * local object as an argument, not p_intf->p_libvlc. */
396 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
397 printf( "%s: returned %i (%s)\n",
398 psz_cmd, i_ret, vlc_error( i_ret ) );
400 else if( !strcmp( psz_cmd, "info" ) )
404 vlc_mutex_lock( &p_input->stream.stream_lock );
405 p_category = p_input->stream.p_info;
408 printf( "+----[ %s ]\n", p_category->psz_name );
410 p_info = p_category->p_info;
413 printf( "| %s: %s\n", p_info->psz_name,
415 p_info = p_info->p_next;
417 p_category = p_category->p_next;
420 printf( "+----[ end of stream info ]\n" );
421 vlc_mutex_unlock( &p_input->stream.stream_lock );
425 printf( "no input\n" );
428 else switch( psz_cmd[0] )
434 vout_thread_t *p_vout;
435 p_vout = vlc_object_find( p_input,
436 VLC_OBJECT_VOUT, FIND_CHILD );
440 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
441 vlc_object_release( p_vout );
454 printf("+----[ Remote control commands ]\n");
456 printf("| add XYZ . . . . . . . . . . add XYZ to playlist\n");
457 printf("| playlist . . . show items currently in playlist\n");
458 printf("| play . . . . . . . . . . . . . . . . play stream\n");
459 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
460 printf("| next . . . . . . . . . . . . next playlist item\n");
461 printf("| prev . . . . . . . . . . previous playlist item\n");
462 printf("| title [X] . . . . set/get title in current item\n");
463 printf("| title_n . . . . . . next title in current item\n");
464 printf("| title_p . . . . previous title in current item\n");
465 printf("| chapter [X] . . set/get chapter in current item\n");
466 printf("| chapter_n . . . . next chapter in current item\n");
467 printf("| chapter_p . . previous chapter in current item\n");
469 printf("| seek X . seek in seconds, for instance `seek 12'\n");
470 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
471 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
472 printf("| info . . . information about the current stream\n");
474 printf("| volume [X] . . . . . . . . set/get audio volume\n");
475 printf("| volup [X] . . . . . raise audio volume X steps\n");
476 printf("| voldown [X] . . . . lower audio volume X steps\n");
477 printf("| adev [X] . . . . . . . . . set/get audio device\n");
478 printf("| achan [X]. . . . . . . . set/get audio channels\n");
480 printf("| help . . . . . . . . . . . . . this help message\n");
481 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
483 printf("+----[ end of help ]\n");
486 /* Ignore empty lines */
489 printf( "unknown command `%s', type `help' for help\n", psz_cmd );
497 vlc_object_release( p_input );
503 vlc_object_release( p_playlist );
508 static int Input( vlc_object_t *p_this, char const *psz_cmd,
509 vlc_value_t oldval, vlc_value_t newval, void *p_data )
511 input_thread_t * p_input;
514 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
521 /* Parse commands that only require an input */
522 if( !strcmp( psz_cmd, "pause" ) )
526 var_Set( p_input, "state", val );
527 vlc_object_release( p_input );
530 else if( !strcmp( psz_cmd, "seek" ) )
532 if( strlen( newval.psz_string ) > 0 &&
533 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
535 val.f_float = (float)atoi( newval.psz_string ) / 100.0;
536 var_Set( p_input, "position", val );
540 val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
541 var_Set( p_input, "time", val );
543 vlc_object_release( p_input );
546 else if( !strcmp( psz_cmd, "chapter" ) ||
547 !strcmp( psz_cmd, "chapter_n" ) ||
548 !strcmp( psz_cmd, "chapter_p" ) )
550 if( !strcmp( psz_cmd, "chapter" ) )
552 if ( *newval.psz_string )
555 val.i_int = atoi( newval.psz_string );
556 var_Set( p_input, "chapter", val );
560 vlc_value_t val_list;
563 var_Get( p_input, "chapter", &val );
564 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
565 printf( "Currently playing chapter %d/%d\n", val.i_int, val_list.p_list->i_count );
566 var_Change( p_this, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
569 else if( !strcmp( psz_cmd, "chapter_n" ) )
571 val.b_bool = VLC_TRUE;
572 var_Set( p_input, "next-chapter", val );
574 else if( !strcmp( psz_cmd, "chapter_p" ) )
576 val.b_bool = VLC_TRUE;
577 var_Set( p_input, "prev-chapter", val );
580 vlc_object_release( p_input );
583 else if( !strcmp( psz_cmd, "title" ) ||
584 !strcmp( psz_cmd, "title_n" ) ||
585 !strcmp( psz_cmd, "title_p" ) )
587 if( !strcmp( psz_cmd, "title" ) )
589 if ( *newval.psz_string )
592 val.i_int = atoi( newval.psz_string );
593 var_Set( p_input, "title", val );
597 vlc_value_t val_list;
600 var_Get( p_input, "title", &val );
601 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
602 printf( "Currently playing title %d/%d\n", val.i_int, val_list.p_list->i_count );
603 var_Change( p_this, "title", VLC_VAR_FREELIST, &val_list, NULL );
606 else if( !strcmp( psz_cmd, "title_n" ) )
608 val.b_bool = VLC_TRUE;
609 var_Set( p_input, "next-title", val );
611 else if( !strcmp( psz_cmd, "title_p" ) )
613 val.b_bool = VLC_TRUE;
614 var_Set( p_input, "prev-title", val );
617 vlc_object_release( p_input );
625 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
626 vlc_value_t oldval, vlc_value_t newval, void *p_data )
628 playlist_t * p_playlist;
630 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
637 /* Parse commands that require a playlist */
638 if( !strcmp( psz_cmd, "prev" ) )
640 playlist_Prev( p_playlist );
642 else if( !strcmp( psz_cmd, "next" ) )
644 playlist_Next( p_playlist );
646 else if( !strcmp( psz_cmd, "play" ) )
648 playlist_Play( p_playlist );
650 else if( !strcmp( psz_cmd, "stop" ) )
652 playlist_Stop( p_playlist );
654 else if( !strcmp( psz_cmd, "add" ) )
656 printf( "trying to add %s to playlist\n", newval.psz_string );
657 playlist_Add( p_playlist, newval.psz_string, newval.psz_string,
658 PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
660 else if( !strcmp( psz_cmd, "playlist" ) )
663 for ( i = 0; i < p_playlist->i_size; i++ )
665 printf( "|%s%s %s|\n", i == p_playlist->i_index?"*":" ",
666 p_playlist->pp_items[i]->psz_name,
667 p_playlist->pp_items[i]->psz_uri );
671 printf( "| no entries\n" );
679 printf( "unknown command!\n" );
682 vlc_object_release( p_playlist );
686 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
687 vlc_value_t oldval, vlc_value_t newval, void *p_data )
689 p_this->p_vlc->b_die = VLC_TRUE;
693 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
694 vlc_value_t oldval, vlc_value_t newval, void *p_data )
696 intf_thread_t *p_newintf;
698 p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
702 p_newintf->b_block = VLC_FALSE;
703 if( intf_RunThread( p_newintf ) )
705 vlc_object_detach( p_newintf );
706 intf_Destroy( p_newintf );
713 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
714 vlc_value_t oldval, vlc_value_t newval, void *p_data )
718 if ( *newval.psz_string )
721 audio_volume_t i_volume = atoi( newval.psz_string );
722 if ( i_volume > AOUT_VOLUME_MAX )
724 printf( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
726 i_error = VLC_EBADVAR;
728 else i_error = aout_VolumeSet( p_this, i_volume );
733 audio_volume_t i_volume;
734 if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
736 i_error = VLC_EGENERIC;
740 printf( "Volume is %d\n", i_volume );
741 i_error = VLC_SUCCESS;
748 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
749 vlc_value_t oldval, vlc_value_t newval, void *p_data )
751 audio_volume_t i_volume;
752 int i_nb_steps = atoi(newval.psz_string);
753 int i_error = VLC_SUCCESS;
755 if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
760 if ( !strcmp(psz_cmd, "volup") )
762 if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
763 i_error = VLC_EGENERIC;
767 if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
768 i_error = VLC_EGENERIC;
771 if ( !i_error ) printf( "Volume is %d\n", i_volume );
775 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
776 vlc_value_t oldval, vlc_value_t newval, void *p_data )
778 aout_instance_t * p_aout;
779 const char * psz_variable;
780 vlc_value_t val_name;
783 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
784 if ( p_aout == NULL ) return VLC_ENOOBJ;
786 if ( !strcmp( psz_cmd, "adev" ) )
788 psz_variable = "audio-device";
792 psz_variable = "audio-channels";
795 /* Get the descriptive name of the variable */
796 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
798 if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
800 if ( !*newval.psz_string )
802 /* Retrieve all registered ***. */
803 vlc_value_t val, text;
806 if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
808 vlc_object_release( (vlc_object_t *)p_aout );
813 if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
814 VLC_VAR_GETLIST, &val, &text ) < 0 )
816 vlc_object_release( (vlc_object_t *)p_aout );
820 printf( "+----[ %s ]\n", val_name.psz_string );
821 for ( i = 0; i < val.p_list->i_count; i++ )
823 if ( i_value == val.p_list->p_values[i].i_int )
824 printf( "| %i - %s *\n", val.p_list->p_values[i].i_int,
825 text.p_list->p_values[i].psz_string );
827 printf( "| %i - %s\n", val.p_list->p_values[i].i_int,
828 text.p_list->p_values[i].psz_string );
830 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
832 printf( "+----[ end of %s ]\n", val_name.psz_string );
834 if( val_name.psz_string ) free( val_name.psz_string );
835 i_error = VLC_SUCCESS;
840 val.i_int = atoi( newval.psz_string );
842 i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
844 vlc_object_release( (vlc_object_t *)p_aout );