1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rc.c,v 1.15 2002/12/07 15:25:26 gbazin 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 */
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
47 #include <sys/types.h>
50 #include <winsock2.h> /* select() */
55 #define MAX_LINE_LENGTH 256
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Activate ( vlc_object_t * );
61 static void Run ( intf_thread_t *p_intf );
63 static int Playlist ( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int Quit ( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int Intf ( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
69 static int Volume ( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
71 static int VolumeMove ( vlc_object_t *, char const *,
72 vlc_value_t, vlc_value_t, void * );
73 static int AudioConfig ( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
76 /*****************************************************************************
78 *****************************************************************************/
79 #define POS_TEXT N_("show stream position")
80 #define POS_LONGTEXT N_("Show the current position in seconds within the stream from time to time.")
82 #define TTY_TEXT N_("fake TTY")
83 #define TTY_LONGTEXT N_("Force the rc plugin to use stdin as if it was a TTY.")
86 add_category_hint( N_("Remote control"), NULL );
87 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
89 add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
91 set_description( _("remote control interface module") );
92 set_capability( "interface", 20 );
93 set_callbacks( Activate, NULL );
96 /*****************************************************************************
97 * Activate: initialize and create stuff
98 *****************************************************************************/
99 static int Activate( vlc_object_t *p_this )
101 intf_thread_t *p_intf = (intf_thread_t*)p_this;
104 /* Check that stdin is a TTY */
105 if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
107 msg_Warn( p_intf, "fd 0 is not a TTY" );
112 /* Non-buffered stdout */
113 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
115 p_intf->pf_run = Run;
119 printf( "remote control interface initialized, `h' for help\n" );
123 /*****************************************************************************
125 *****************************************************************************
126 * This part of the interface is in a separate thread so that we can call
127 * exec() from within it without annoying the rest of the program.
128 *****************************************************************************/
129 static void Run( intf_thread_t *p_intf )
131 input_thread_t * p_input;
132 playlist_t * p_playlist;
134 char p_buffer[ MAX_LINE_LENGTH + 1 ];
135 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
136 input_info_category_t * p_category;
137 input_info_t * p_info;
143 double f_ratio = 1.0;
148 /* Register commands that will be cleaned up upon object destruction */
149 var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
150 var_AddCallback( p_intf, "quit", Quit, NULL );
151 var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
152 var_AddCallback( p_intf, "intf", Intf, NULL );
154 var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
155 var_AddCallback( p_intf, "play", Playlist, NULL );
156 var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
157 var_AddCallback( p_intf, "stop", Playlist, NULL );
158 var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
159 var_AddCallback( p_intf, "pause", Playlist, NULL );
160 var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
161 var_AddCallback( p_intf, "seek", Playlist, NULL );
162 var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
163 var_AddCallback( p_intf, "prev", Playlist, NULL );
164 var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
165 var_AddCallback( p_intf, "next", Playlist, NULL );
167 var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
168 var_AddCallback( p_intf, "title", Playlist, NULL );
169 var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
170 var_AddCallback( p_intf, "title_n", Playlist, NULL );
171 var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
172 var_AddCallback( p_intf, "title_p", Playlist, NULL );
173 var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
174 var_AddCallback( p_intf, "chapter", Playlist, NULL );
175 var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
176 var_AddCallback( p_intf, "chapter_n", Playlist, NULL );
177 var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
178 var_AddCallback( p_intf, "chapter_p", Playlist, NULL );
180 var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_intf, "volume", Volume, NULL );
182 var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_intf, "volup", VolumeMove, NULL );
184 var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185 var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
186 var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_intf, "adev", AudioConfig, NULL );
188 var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_intf, "achan", AudioConfig, NULL );
191 while( !p_intf->b_die )
195 vlc_bool_t b_complete = VLC_FALSE;
201 FD_SET( STDIN_FILENO, &fds );
203 i_dummy = select( 32, &fds, NULL, NULL, &tv );
208 while( !p_intf->b_die
209 && i_size < MAX_LINE_LENGTH
210 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
211 && p_buffer[ i_size ] != '\r'
212 && p_buffer[ i_size ] != '\n' )
217 if( i_size == MAX_LINE_LENGTH
218 || p_buffer[ i_size ] == '\r'
219 || p_buffer[ i_size ] == '\n' )
221 p_buffer[ i_size ] = 0;
222 b_complete = VLC_TRUE;
226 /* Manage the input part */
227 if( p_input == NULL )
231 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
236 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
240 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
245 else if( p_input->b_dead )
247 vlc_object_release( p_input );
251 if( p_input && b_showpos )
254 vlc_mutex_lock( &p_input->stream.stream_lock );
255 if( !p_input->b_die && p_input->stream.i_mux_rate )
257 #define A p_input->stream.p_selected_area
258 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
259 i_newpos = A->i_tell * f_ratio;
261 if( i_oldpos != i_newpos )
264 printf( "pos: %li s / %li s\n", (long int)i_newpos,
265 (long int)(f_ratio * A->i_size) );
269 vlc_mutex_unlock( &p_input->stream.stream_lock );
272 /* Is there something to do? */
275 char *psz_cmd, *psz_arg;
277 /* Skip heading spaces */
279 while( *psz_cmd == ' ' )
284 /* Split psz_cmd at the first space and make sure that
285 * psz_arg is valid */
286 psz_arg = strchr( psz_cmd, ' ' );
290 while( *psz_arg == ' ' )
300 /* If the user typed a registered local command, try it */
301 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
306 val.psz_string = psz_arg;
307 i_ret = var_Set( p_intf, psz_cmd, val );
308 printf( "%s: returned %i (%s)\n",
309 psz_cmd, i_ret, vlc_error( i_ret ) );
311 /* Or maybe it's a global command */
312 else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
317 val.psz_string = psz_arg;
318 /* FIXME: it's a global command, but we should pass the
319 * local object as an argument, not p_intf->p_libvlc. */
320 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
321 printf( "%s: returned %i (%s)\n",
322 psz_cmd, i_ret, vlc_error( i_ret ) );
324 else if( !strcmp( psz_cmd, "info" ) )
328 vlc_mutex_lock( &p_input->stream.stream_lock );
329 p_category = p_input->stream.p_info;
332 printf( "+----[ %s ]\n", p_category->psz_name );
334 p_info = p_category->p_info;
337 printf( "| %s: %s\n", p_info->psz_name,
339 p_info = p_info->p_next;
341 p_category = p_category->p_next;
344 printf( "+----[ end of stream info ]\n" );
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
349 printf( "no input\n" );
352 else switch( psz_cmd[0] )
356 if( psz_cmd[1] == ' ' && p_playlist )
358 playlist_Add( p_playlist, psz_cmd + 2,
359 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
367 vout_thread_t *p_vout;
368 p_vout = vlc_object_find( p_input,
369 VLC_OBJECT_VOUT, FIND_CHILD );
373 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
374 vlc_object_release( p_vout );
387 printf("+----[ remote control commands ]\n");
389 printf("| a XYZ . . . . . . . . . . . add XYZ to playlist\n");
390 printf("| play . . . . . . . . . . . . . . . . play stream\n");
391 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
392 printf("| next . . . . . . . . . . . . next playlist item\n");
393 printf("| prev . . . . . . . . . . previous playlist item\n");
394 printf("| title [X] . . . . set/get title in current item\n");
395 printf("| title_n . . . . . . next title in current item\n");
396 printf("| title_p . . . . previous title in current item\n");
397 printf("| chapter [X] . . set/get chapter in current item\n");
398 printf("| chapter_n . . . . next chapter in current item\n");
399 printf("| chapter_p . . previous chapter in current item\n");
401 printf("| seek X . seek in seconds, for instance `seek 12'\n");
402 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
403 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
404 printf("| info . . . information about the current stream\n");
406 printf("| volume [X] . . . . . . . . set/get audio volume\n");
407 printf("| volup [X] . . . . . raise audio volume X steps\n");
408 printf("| voldown [X] . . . . lower audio volume X steps\n");
409 printf("| adev [X] . . . . . . . . . set/get audio device\n");
410 printf("| achan [X]. . . . . . . . set/get audio channels\n");
412 printf("| help . . . . . . . . . . . . . this help message\n");
413 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
415 printf("+----[ end of help ]\n");
418 /* Ignore empty lines */
421 printf( "unknown command `%s', type `help' for help\n", psz_cmd );
429 vlc_object_release( p_input );
435 vlc_object_release( p_playlist );
440 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
441 vlc_value_t oldval, vlc_value_t newval, void *p_data )
443 input_thread_t * p_input;
444 playlist_t * p_playlist;
446 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
453 /* Parse commands that only require an input */
454 if( !strcmp( psz_cmd, "pause" ) )
456 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
457 vlc_object_release( p_input );
460 else if( !strcmp( psz_cmd, "seek" ) )
462 input_Seek( p_input, atoi( newval.psz_string ),
463 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
465 else if( !strcmp( psz_cmd, "chapter" ) ||
466 !strcmp( psz_cmd, "chapter_n" ) ||
467 !strcmp( psz_cmd, "chapter_p" ) )
469 unsigned int i_chapter = 0;
471 if( !strcmp( psz_cmd, "chapter" ) )
473 if ( *newval.psz_string )
476 i_chapter = atoi( newval.psz_string );
481 vlc_mutex_lock( &p_input->stream.stream_lock );
482 printf( "Currently playing chapter %d\n",
483 p_input->stream.p_selected_area->i_part );
484 vlc_mutex_unlock( &p_input->stream.stream_lock );
486 vlc_object_release( p_input );
490 else if( !strcmp( psz_cmd, "chapter_n" ) )
492 vlc_mutex_lock( &p_input->stream.stream_lock );
493 i_chapter = p_input->stream.p_selected_area->i_part + 1;
494 vlc_mutex_unlock( &p_input->stream.stream_lock );
496 else if( !strcmp( psz_cmd, "chapter_p" ) )
498 vlc_mutex_lock( &p_input->stream.stream_lock );
499 i_chapter = p_input->stream.p_selected_area->i_part - 1;
500 vlc_mutex_unlock( &p_input->stream.stream_lock );
503 vlc_mutex_lock( &p_input->stream.stream_lock );
504 if( ( i_chapter > 0 ) && ( i_chapter <=
505 p_input->stream.p_selected_area->i_part_nb ) )
507 p_input->stream.p_selected_area->i_part = i_chapter;
508 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 input_ChangeArea( p_input,
510 (input_area_t*)p_input->stream.p_selected_area );
511 input_SetStatus( p_input, INPUT_STATUS_PLAY );
512 vlc_mutex_lock( &p_input->stream.stream_lock );
514 vlc_mutex_unlock( &p_input->stream.stream_lock );
516 vlc_object_release( p_input );
519 else if( !strcmp( psz_cmd, "title" ) ||
520 !strcmp( psz_cmd, "title_n" ) ||
521 !strcmp( psz_cmd, "title_p" ) )
523 unsigned int i_title = 0;
525 if( !strcmp( psz_cmd, "title" ) )
527 if ( *newval.psz_string )
530 i_title = atoi( newval.psz_string );
535 vlc_mutex_lock( &p_input->stream.stream_lock );
536 printf( "Currently playing title %d\n",
537 p_input->stream.p_selected_area->i_id );
538 vlc_mutex_unlock( &p_input->stream.stream_lock );
540 vlc_object_release( p_input );
544 else if( !strcmp( psz_cmd, "title_n" ) )
546 vlc_mutex_lock( &p_input->stream.stream_lock );
547 i_title = p_input->stream.p_selected_area->i_id + 1;
548 vlc_mutex_unlock( &p_input->stream.stream_lock );
550 else if( !strcmp( psz_cmd, "title_p" ) )
552 vlc_mutex_lock( &p_input->stream.stream_lock );
553 i_title = p_input->stream.p_selected_area->i_id - 1;
554 vlc_mutex_unlock( &p_input->stream.stream_lock );
557 vlc_mutex_lock( &p_input->stream.stream_lock );
558 if( ( i_title > 0 ) && ( i_title <=
559 p_input->stream.p_selected_area->i_part_nb ) )
561 p_input->stream.p_selected_area->i_part = i_title;
562 vlc_mutex_unlock( &p_input->stream.stream_lock );
563 input_ChangeArea( p_input,
564 (input_area_t*)p_input->stream.pp_areas[i_title] );
565 input_SetStatus( p_input, INPUT_STATUS_PLAY );
566 vlc_mutex_lock( &p_input->stream.stream_lock );
568 vlc_mutex_unlock( &p_input->stream.stream_lock );
570 vlc_object_release( p_input );
575 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
577 vlc_object_release( p_input );
584 /* Parse commands that require a playlist */
585 if( !strcmp( psz_cmd, "prev" ) )
587 playlist_Prev( p_playlist );
589 else if( !strcmp( psz_cmd, "next" ) )
591 playlist_Next( p_playlist );
593 else if( !strcmp( psz_cmd, "play" ) )
595 playlist_Play( p_playlist );
597 else if( !strcmp( psz_cmd, "stop" ) )
599 playlist_Stop( p_playlist );
602 vlc_object_release( p_playlist );
606 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
607 vlc_value_t oldval, vlc_value_t newval, void *p_data )
609 p_this->p_vlc->b_die = VLC_TRUE;
613 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
614 vlc_value_t oldval, vlc_value_t newval, void *p_data )
616 intf_thread_t *p_newintf;
617 char *psz_oldmodule = config_GetPsz( p_this->p_vlc, "intf" );
619 config_PutPsz( p_this->p_vlc, "intf", newval.psz_string );
620 p_newintf = intf_Create( p_this->p_vlc );
621 config_PutPsz( p_this->p_vlc, "intf", psz_oldmodule );
625 free( psz_oldmodule );
630 p_newintf->b_block = VLC_FALSE;
631 if( intf_RunThread( p_newintf ) )
633 vlc_object_detach( p_newintf );
634 intf_Destroy( p_newintf );
641 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
642 vlc_value_t oldval, vlc_value_t newval, void *p_data )
644 aout_instance_t * p_aout;
646 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
647 if ( p_aout == NULL ) return VLC_ENOOBJ;
649 if ( *newval.psz_string )
652 audio_volume_t i_volume = atoi( newval.psz_string );
653 if ( i_volume > AOUT_VOLUME_MAX )
655 printf( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
657 i_error = VLC_EBADVAR;
659 else i_error = aout_VolumeSet( p_aout, i_volume );
664 audio_volume_t i_volume;
665 if ( aout_VolumeGet( p_aout, &i_volume ) < 0 )
667 i_error = VLC_EGENERIC;
671 printf( "Volume is %d\n", i_volume );
672 i_error = VLC_SUCCESS;
675 vlc_object_release( (vlc_object_t *)p_aout );
680 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
681 vlc_value_t oldval, vlc_value_t newval, void *p_data )
683 aout_instance_t * p_aout;
684 audio_volume_t i_volume;
685 int i_nb_steps = atoi(newval.psz_string);
686 int i_error = VLC_SUCCESS;
688 if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
693 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
694 if ( p_aout == NULL ) return VLC_ENOOBJ;
696 if ( !strcmp(psz_cmd, "volup") )
698 if ( aout_VolumeUp( p_aout, i_nb_steps, &i_volume ) < 0 )
699 i_error = VLC_EGENERIC;
703 if ( aout_VolumeDown( p_aout, i_nb_steps, &i_volume ) < 0 )
704 i_error = VLC_EGENERIC;
706 vlc_object_release( (vlc_object_t *)p_aout );
708 if ( !i_error ) printf( "Volume is %d\n", i_volume );
712 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
713 vlc_value_t oldval, vlc_value_t newval, void *p_data )
715 aout_instance_t * p_aout;
716 const char * psz_variable;
717 const char * psz_name;
720 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
721 if ( p_aout == NULL ) return VLC_ENOOBJ;
723 if ( !strcmp( psz_cmd, "adev" ) )
725 psz_variable = "audio-device";
726 psz_name = "audio devices";
730 psz_variable = "audio-channels";
731 psz_name = "audio channels";
734 if ( !*newval.psz_string )
736 /* Retrieve all registered ***. */
739 vlc_value_t * p_vals;
742 if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
744 vlc_object_release( (vlc_object_t *)p_aout );
747 psz_value = val.psz_string;
749 if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
750 VLC_VAR_GETLIST, &val ) < 0 )
753 vlc_object_release( (vlc_object_t *)p_aout );
757 printf( "+----[ %s ]\n", psz_name );
758 i_vals = ((vlc_value_t *)val.p_address)[0].i_int;
759 p_vals = &((vlc_value_t *)val.p_address)[1]; /* Starts at index 1 */
760 for ( i = 0; i < i_vals; i++ )
762 if ( !strcmp( psz_value, p_vals[i].psz_string ) )
763 printf( "| %s *\n", p_vals[i].psz_string );
765 printf( "| %s\n", p_vals[i].psz_string );
767 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
769 printf( "+----[ end of %s ]\n", psz_name );
772 i_error = VLC_SUCCESS;
777 val.psz_string = newval.psz_string;
779 i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
781 vlc_object_release( (vlc_object_t *)p_aout );