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" );
140 INPUT_RECORD input_record;
147 /* Register commands that will be cleaned up upon object destruction */
148 var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
149 var_AddCallback( p_intf, "quit", Quit, NULL );
150 var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
151 var_AddCallback( p_intf, "intf", Intf, NULL );
153 var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
154 var_AddCallback( p_intf, "add", Playlist, NULL );
155 var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
156 var_AddCallback( p_intf, "playlist", Playlist, NULL );
157 var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
158 var_AddCallback( p_intf, "play", Playlist, NULL );
159 var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
160 var_AddCallback( p_intf, "stop", Playlist, NULL );
161 var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
162 var_AddCallback( p_intf, "prev", Playlist, NULL );
163 var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
164 var_AddCallback( p_intf, "next", Playlist, NULL );
166 var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
167 var_AddCallback( p_intf, "pause", Input, NULL );
168 var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
169 var_AddCallback( p_intf, "seek", Input, NULL );
170 var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_intf, "title", Input, NULL );
172 var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
173 var_AddCallback( p_intf, "title_n", Input, NULL );
174 var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
175 var_AddCallback( p_intf, "title_p", Input, NULL );
176 var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
177 var_AddCallback( p_intf, "chapter", Input, NULL );
178 var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
179 var_AddCallback( p_intf, "chapter_n", Input, NULL );
180 var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_intf, "chapter_p", Input, NULL );
183 var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
184 var_AddCallback( p_intf, "volume", Volume, NULL );
185 var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
186 var_AddCallback( p_intf, "volup", VolumeMove, NULL );
187 var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
188 var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
189 var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_intf, "adev", AudioConfig, NULL );
191 var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_intf, "achan", AudioConfig, NULL );
195 /* Get the file descriptor of the console input */
196 hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
197 if( hConsoleIn == INVALID_HANDLE_VALUE )
199 msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
200 p_intf->b_die = VLC_TRUE;
204 while( !p_intf->b_die )
206 vlc_bool_t b_complete = VLC_FALSE;
214 tv.tv_usec = (long)INTF_IDLE_SLEEP;
216 FD_SET( STDIN_FILENO, &fds );
218 i_dummy = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
220 /* On Win32, select() only works on socket descriptors */
221 i_dummy = ( WaitForSingleObject( hConsoleIn, INTF_IDLE_SLEEP/1000 )
228 while( !p_intf->b_die
229 && i_size < MAX_LINE_LENGTH
231 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
233 && ReadConsoleInput( hConsoleIn, &input_record, 1,
239 if( input_record.EventType != KEY_EVENT ||
240 !input_record.Event.KeyEvent.bKeyDown ||
241 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
242 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
243 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
244 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
246 /* nothing interesting */
251 input_record.Event.KeyEvent.uChar.AsciiChar;
253 /* Echo out the command */
254 putc( p_buffer[ i_size ], stdout );
256 /* Handle special keys */
257 if( p_buffer[ i_size ] == '\r' || p_buffer[ i_size ] == '\n' )
259 putc( '\n', stdout );
262 switch( p_buffer[ i_size ] )
269 putc( '\b', stdout );
280 if( p_buffer[ i_size ] == '\r' || p_buffer[ i_size ] == '\n' )
289 if( i_size == MAX_LINE_LENGTH
290 || p_buffer[ i_size ] == '\r'
291 || p_buffer[ i_size ] == '\n' )
293 p_buffer[ i_size ] = 0;
294 b_complete = VLC_TRUE;
298 /* Manage the input part */
299 if( p_input == NULL )
303 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
308 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
312 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
317 else if( p_input->b_dead )
319 vlc_object_release( p_input );
323 if( p_input && b_showpos )
325 i_newpos = 100 * var_GetFloat( p_input, "position" );
326 if( i_oldpos != i_newpos )
329 printf( "pos: %d%%\n", i_newpos );
333 /* Is there something to do? */
336 char *psz_cmd, *psz_arg;
338 /* Skip heading spaces */
340 while( *psz_cmd == ' ' )
345 /* Split psz_cmd at the first space and make sure that
346 * psz_arg is valid */
347 psz_arg = strchr( psz_cmd, ' ' );
351 while( *psz_arg == ' ' )
361 /* If the user typed a registered local command, try it */
362 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
367 val.psz_string = psz_arg;
368 i_ret = var_Set( p_intf, psz_cmd, val );
369 printf( _("%s: returned %i (%s)\n"),
370 psz_cmd, i_ret, vlc_error( i_ret ) );
372 /* Or maybe it's a global command */
373 else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
378 val.psz_string = psz_arg;
379 /* FIXME: it's a global command, but we should pass the
380 * local object as an argument, not p_intf->p_libvlc. */
381 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
382 printf( _("%s: returned %i (%s)\n"),
383 psz_cmd, i_ret, vlc_error( i_ret ) );
385 else if( !strcmp( psz_cmd, "info" ) )
390 vlc_mutex_lock( &p_input->input.p_item->lock );
391 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
393 info_category_t *p_category =
394 p_input->input.p_item->pp_categories[i];
396 printf( "+----[ %s ]\n", p_category->psz_name );
398 for ( j = 0; j < p_category->i_infos; j++ )
400 info_t *p_info = p_category->pp_infos[j];
401 printf( "| %s: %s\n", p_info->psz_name,
406 printf( _("+----[ end of stream info ]\n") );
407 vlc_mutex_unlock( &p_input->input.p_item->lock );
411 printf( _("no input\n") );
414 else switch( psz_cmd[0] )
420 vout_thread_t *p_vout;
421 p_vout = vlc_object_find( p_input,
422 VLC_OBJECT_VOUT, FIND_CHILD );
426 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
427 vlc_object_release( p_vout );
440 printf(_("+----[ Remote control commands ]\n"));
442 printf(_("| add XYZ . . . . . . . . . . add XYZ to playlist\n"));
443 printf(_("| playlist . . . show items currently in playlist\n"));
444 printf(_("| play . . . . . . . . . . . . . . . . play stream\n"));
445 printf(_("| stop . . . . . . . . . . . . . . . . stop stream\n"));
446 printf(_("| next . . . . . . . . . . . . next playlist item\n"));
447 printf(_("| prev . . . . . . . . . . previous playlist item\n"));
448 printf(_("| title [X] . . . . set/get title in current item\n"));
449 printf(_("| title_n . . . . . . next title in current item\n"));
450 printf(_("| title_p . . . . previous title in current item\n"));
451 printf(_("| chapter [X] . . set/get chapter in current item\n"));
452 printf(_("| chapter_n . . . . next chapter in current item\n"));
453 printf(_("| chapter_p . . previous chapter in current item\n"));
455 printf(_("| seek X . seek in seconds, for instance `seek 12'\n"));
456 printf(_("| pause . . . . . . . . . . . . . . toggle pause\n"));
457 printf(_("| f . . . . . . . . . . . . . . toggle fullscreen\n"));
458 printf(_("| info . . . information about the current stream\n"));
460 printf(_("| volume [X] . . . . . . . . set/get audio volume\n"));
461 printf(_("| volup [X] . . . . . raise audio volume X steps\n"));
462 printf(_("| voldown [X] . . . . lower audio volume X steps\n"));
463 printf(_("| adev [X] . . . . . . . . . set/get audio device\n"));
464 printf(_("| achan [X]. . . . . . . . set/get audio channels\n"));
466 printf(_("| help . . . . . . . . . . . . . this help message\n"));
467 printf(_("| quit . . . . . . . . . . . . . . . . . quit vlc\n"));
469 printf(_("+----[ end of help ]\n"));
472 /* Ignore empty lines */
475 printf( _("unknown command `%s', type `help' for help\n"), psz_cmd );
483 vlc_object_release( p_input );
489 vlc_object_release( p_playlist );
494 static int Input( vlc_object_t *p_this, char const *psz_cmd,
495 vlc_value_t oldval, vlc_value_t newval, void *p_data )
497 input_thread_t * p_input;
500 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
507 /* Parse commands that only require an input */
508 if( !strcmp( psz_cmd, "pause" ) )
512 var_Set( p_input, "state", val );
513 vlc_object_release( p_input );
516 else if( !strcmp( psz_cmd, "seek" ) )
518 if( strlen( newval.psz_string ) > 0 &&
519 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
521 val.f_float = (float)atoi( newval.psz_string ) / 100.0;
522 var_Set( p_input, "position", val );
526 val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
527 var_Set( p_input, "time", val );
529 vlc_object_release( p_input );
532 else if( !strcmp( psz_cmd, "chapter" ) ||
533 !strcmp( psz_cmd, "chapter_n" ) ||
534 !strcmp( psz_cmd, "chapter_p" ) )
536 if( !strcmp( psz_cmd, "chapter" ) )
538 if ( *newval.psz_string )
541 val.i_int = atoi( newval.psz_string );
542 var_Set( p_input, "chapter", val );
546 vlc_value_t val_list;
549 var_Get( p_input, "chapter", &val );
550 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
551 printf( _("Currently playing chapter %d/%d\n"), val.i_int, val_list.p_list->i_count );
552 var_Change( p_this, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
555 else if( !strcmp( psz_cmd, "chapter_n" ) )
557 val.b_bool = VLC_TRUE;
558 var_Set( p_input, "next-chapter", val );
560 else if( !strcmp( psz_cmd, "chapter_p" ) )
562 val.b_bool = VLC_TRUE;
563 var_Set( p_input, "prev-chapter", val );
566 vlc_object_release( p_input );
569 else if( !strcmp( psz_cmd, "title" ) ||
570 !strcmp( psz_cmd, "title_n" ) ||
571 !strcmp( psz_cmd, "title_p" ) )
573 if( !strcmp( psz_cmd, "title" ) )
575 if ( *newval.psz_string )
578 val.i_int = atoi( newval.psz_string );
579 var_Set( p_input, "title", val );
583 vlc_value_t val_list;
586 var_Get( p_input, "title", &val );
587 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
588 printf( _("Currently playing title %d/%d\n"), val.i_int, val_list.p_list->i_count );
589 var_Change( p_this, "title", VLC_VAR_FREELIST, &val_list, NULL );
592 else if( !strcmp( psz_cmd, "title_n" ) )
594 val.b_bool = VLC_TRUE;
595 var_Set( p_input, "next-title", val );
597 else if( !strcmp( psz_cmd, "title_p" ) )
599 val.b_bool = VLC_TRUE;
600 var_Set( p_input, "prev-title", val );
603 vlc_object_release( p_input );
611 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
612 vlc_value_t oldval, vlc_value_t newval, void *p_data )
614 playlist_t * p_playlist;
616 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
623 /* Parse commands that require a playlist */
624 if( !strcmp( psz_cmd, "prev" ) )
626 playlist_Prev( p_playlist );
628 else if( !strcmp( psz_cmd, "next" ) )
630 playlist_Next( p_playlist );
632 else if( !strcmp( psz_cmd, "play" ) )
634 playlist_Play( p_playlist );
636 else if( !strcmp( psz_cmd, "stop" ) )
638 playlist_Stop( p_playlist );
640 else if( !strcmp( psz_cmd, "add" ) )
642 printf( _("trying to add %s to playlist\n"), newval.psz_string );
643 playlist_Add( p_playlist, newval.psz_string, newval.psz_string,
644 PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
646 else if( !strcmp( psz_cmd, "playlist" ) )
649 for ( i = 0; i < p_playlist->i_size; i++ )
651 printf( "|%s%s %s|\n", i == p_playlist->i_index?"*":" ",
652 p_playlist->pp_items[i]->input.psz_name,
653 p_playlist->pp_items[i]->input.psz_uri );
657 printf( _("| no entries\n") );
665 printf( _("unknown command!\n") );
668 vlc_object_release( p_playlist );
672 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
673 vlc_value_t oldval, vlc_value_t newval, void *p_data )
675 p_this->p_vlc->b_die = VLC_TRUE;
679 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
680 vlc_value_t oldval, vlc_value_t newval, void *p_data )
682 intf_thread_t *p_newintf;
684 p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
688 p_newintf->b_block = VLC_FALSE;
689 if( intf_RunThread( p_newintf ) )
691 vlc_object_detach( p_newintf );
692 intf_Destroy( p_newintf );
699 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
700 vlc_value_t oldval, vlc_value_t newval, void *p_data )
704 if ( *newval.psz_string )
707 audio_volume_t i_volume = atoi( newval.psz_string );
708 if ( i_volume > AOUT_VOLUME_MAX )
710 printf( _("Volume must be in the range %d-%d\n"), AOUT_VOLUME_MIN,
712 i_error = VLC_EBADVAR;
714 else i_error = aout_VolumeSet( p_this, i_volume );
719 audio_volume_t i_volume;
720 if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
722 i_error = VLC_EGENERIC;
726 printf( _("Volume is %d\n"), i_volume );
727 i_error = VLC_SUCCESS;
734 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
735 vlc_value_t oldval, vlc_value_t newval, void *p_data )
737 audio_volume_t i_volume;
738 int i_nb_steps = atoi(newval.psz_string);
739 int i_error = VLC_SUCCESS;
741 if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
746 if ( !strcmp(psz_cmd, "volup") )
748 if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
749 i_error = VLC_EGENERIC;
753 if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
754 i_error = VLC_EGENERIC;
757 if ( !i_error ) printf( _("Volume is %d\n"), i_volume );
761 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
762 vlc_value_t oldval, vlc_value_t newval, void *p_data )
764 aout_instance_t * p_aout;
765 const char * psz_variable;
766 vlc_value_t val_name;
769 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
770 if ( p_aout == NULL ) return VLC_ENOOBJ;
772 if ( !strcmp( psz_cmd, "adev" ) )
774 psz_variable = "audio-device";
778 psz_variable = "audio-channels";
781 /* Get the descriptive name of the variable */
782 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
784 if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
786 if ( !*newval.psz_string )
788 /* Retrieve all registered ***. */
789 vlc_value_t val, text;
792 if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
794 vlc_object_release( (vlc_object_t *)p_aout );
799 if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
800 VLC_VAR_GETLIST, &val, &text ) < 0 )
802 vlc_object_release( (vlc_object_t *)p_aout );
806 printf( "+----[ %s ]\n", val_name.psz_string );
807 for ( i = 0; i < val.p_list->i_count; i++ )
809 if ( i_value == val.p_list->p_values[i].i_int )
810 printf( "| %i - %s *\n", val.p_list->p_values[i].i_int,
811 text.p_list->p_values[i].psz_string );
813 printf( "| %i - %s\n", val.p_list->p_values[i].i_int,
814 text.p_list->p_values[i].psz_string );
816 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
818 printf( _("+----[ end of %s ]\n"), val_name.psz_string );
820 if( val_name.psz_string ) free( val_name.psz_string );
821 i_error = VLC_SUCCESS;
826 val.i_int = atoi( newval.psz_string );
828 i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
830 vlc_object_release( (vlc_object_t *)p_aout );