1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rc.c,v 1.14 2002/12/06 16:34:06 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 */
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 *, char * );
64 static int Quit ( vlc_object_t *, char *, char * );
65 static int Intf ( vlc_object_t *, char *, char * );
66 static int Volume ( vlc_object_t *, char *, char * );
67 static int VolumeMove ( vlc_object_t *, char *, char * );
68 static int AudioConfig ( vlc_object_t *, char *, char * );
70 /*****************************************************************************
72 *****************************************************************************/
73 #define POS_TEXT N_("show stream position")
74 #define POS_LONGTEXT N_("Show the current position in seconds within the stream from time to time.")
76 #define TTY_TEXT N_("fake TTY")
77 #define TTY_LONGTEXT N_("Force the rc plugin to use stdin as if it was a TTY.")
80 add_category_hint( N_("Remote control"), NULL );
81 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
83 add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
85 set_description( _("remote control interface module") );
86 set_capability( "interface", 20 );
87 set_callbacks( Activate, NULL );
90 /*****************************************************************************
91 * Activate: initialize and create stuff
92 *****************************************************************************/
93 static int Activate( vlc_object_t *p_this )
95 intf_thread_t *p_intf = (intf_thread_t*)p_this;
98 /* Check that stdin is a TTY */
99 if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
101 msg_Warn( p_intf, "fd 0 is not a TTY" );
106 /* Non-buffered stdout */
107 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
109 p_intf->pf_run = Run;
113 printf( "remote control interface initialized, `h' for help\n" );
117 /*****************************************************************************
119 *****************************************************************************
120 * This part of the interface is in a separate thread so that we can call
121 * exec() from within it without annoying the rest of the program.
122 *****************************************************************************/
123 static void Run( intf_thread_t *p_intf )
125 input_thread_t * p_input;
126 playlist_t * p_playlist;
128 char p_buffer[ MAX_LINE_LENGTH + 1 ];
129 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
130 input_info_category_t * p_category;
131 input_info_t * p_info;
137 double f_ratio = 1.0;
142 /* Register commands that will be cleaned up upon object destruction */
143 var_Create( p_intf, "quit", VLC_VAR_COMMAND );
144 var_Set( p_intf, "quit", (vlc_value_t)(void*)Quit );
145 var_Create( p_intf, "intf", VLC_VAR_COMMAND );
146 var_Set( p_intf, "intf", (vlc_value_t)(void*)Intf );
148 var_Create( p_intf, "play", VLC_VAR_COMMAND );
149 var_Set( p_intf, "play", (vlc_value_t)(void*)Playlist );
150 var_Create( p_intf, "stop", VLC_VAR_COMMAND );
151 var_Set( p_intf, "stop", (vlc_value_t)(void*)Playlist );
152 var_Create( p_intf, "pause", VLC_VAR_COMMAND );
153 var_Set( p_intf, "pause", (vlc_value_t)(void*)Playlist );
154 var_Create( p_intf, "seek", VLC_VAR_COMMAND );
155 var_Set( p_intf, "seek", (vlc_value_t)(void*)Playlist );
156 var_Create( p_intf, "prev", VLC_VAR_COMMAND );
157 var_Set( p_intf, "prev", (vlc_value_t)(void*)Playlist );
158 var_Create( p_intf, "next", VLC_VAR_COMMAND );
159 var_Set( p_intf, "next", (vlc_value_t)(void*)Playlist );
161 var_Create( p_intf, "title", VLC_VAR_COMMAND );
162 var_Set( p_intf, "title", (vlc_value_t)(void*)Playlist );
163 var_Create( p_intf, "title_n", VLC_VAR_COMMAND );
164 var_Set( p_intf, "title_n", (vlc_value_t)(void*)Playlist );
165 var_Create( p_intf, "title_p", VLC_VAR_COMMAND );
166 var_Set( p_intf, "title_p", (vlc_value_t)(void*)Playlist );
167 var_Create( p_intf, "chapter", VLC_VAR_COMMAND );
168 var_Set( p_intf, "chapter", (vlc_value_t)(void*)Playlist );
169 var_Create( p_intf, "chapter_n", VLC_VAR_COMMAND );
170 var_Set( p_intf, "chapter_n", (vlc_value_t)(void*)Playlist );
171 var_Create( p_intf, "chapter_p", VLC_VAR_COMMAND );
172 var_Set( p_intf, "chapter_p", (vlc_value_t)(void*)Playlist );
174 var_Create( p_intf, "volume", VLC_VAR_COMMAND );
175 var_Set( p_intf, "volume", (vlc_value_t)(void*)Volume );
176 var_Create( p_intf, "volup", VLC_VAR_COMMAND );
177 var_Set( p_intf, "volup", (vlc_value_t)(void*)VolumeMove );
178 var_Create( p_intf, "voldown", VLC_VAR_COMMAND );
179 var_Set( p_intf, "voldown", (vlc_value_t)(void*)VolumeMove );
180 var_Create( p_intf, "adev", VLC_VAR_COMMAND );
181 var_Set( p_intf, "adev", (vlc_value_t)(void*)AudioConfig );
182 var_Create( p_intf, "achan", VLC_VAR_COMMAND );
183 var_Set( p_intf, "achan", (vlc_value_t)(void*)AudioConfig );
185 while( !p_intf->b_die )
189 vlc_bool_t b_complete = VLC_FALSE;
195 FD_SET( STDIN_FILENO, &fds );
197 i_dummy = select( 32, &fds, NULL, NULL, &tv );
202 while( !p_intf->b_die
203 && i_size < MAX_LINE_LENGTH
204 && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
205 && p_buffer[ i_size ] != '\r'
206 && p_buffer[ i_size ] != '\n' )
211 if( i_size == MAX_LINE_LENGTH
212 || p_buffer[ i_size ] == '\r'
213 || p_buffer[ i_size ] == '\n' )
215 p_buffer[ i_size ] = 0;
216 b_complete = VLC_TRUE;
220 /* Manage the input part */
221 if( p_input == NULL )
225 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
230 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
234 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
239 else if( p_input->b_dead )
241 vlc_object_release( p_input );
245 if( p_input && b_showpos )
248 vlc_mutex_lock( &p_input->stream.stream_lock );
249 if( !p_input->b_die && p_input->stream.i_mux_rate )
251 #define A p_input->stream.p_selected_area
252 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
253 i_newpos = A->i_tell * f_ratio;
255 if( i_oldpos != i_newpos )
258 printf( "pos: %li s / %li s\n", (long int)i_newpos,
259 (long int)(f_ratio * A->i_size) );
263 vlc_mutex_unlock( &p_input->stream.stream_lock );
266 /* Is there something to do? */
269 char *psz_cmd, *psz_arg;
271 /* Skip heading spaces */
273 while( *psz_cmd == ' ' )
278 /* Split psz_cmd at the first space and make sure that
279 * psz_arg is valid */
280 psz_arg = strchr( psz_cmd, ' ' );
284 while( *psz_arg == ' ' )
294 /* If the user typed a registered local command, try it */
295 if( var_Type( p_intf, psz_cmd ) == VLC_VAR_COMMAND )
300 val.psz_string = psz_arg;
301 i_ret = var_Get( p_intf, psz_cmd, &val );
302 printf( "%s: returned %i (%s)\n",
303 psz_cmd, i_ret, vlc_error( i_ret ) );
305 /* Or maybe it's a global command */
306 else if( var_Type( p_intf->p_libvlc, psz_cmd ) == VLC_VAR_COMMAND )
311 val.psz_string = psz_arg;
312 /* FIXME: it's a global command, but we should pass the
313 * local object as an argument, not p_intf->p_libvlc. */
314 i_ret = var_Get( p_intf->p_libvlc, psz_cmd, &val );
315 printf( "%s: returned %i (%s)\n",
316 psz_cmd, i_ret, vlc_error( i_ret ) );
318 else if( !strcmp( psz_cmd, "info" ) )
322 vlc_mutex_lock( &p_input->stream.stream_lock );
323 p_category = p_input->stream.p_info;
326 printf( "+----[ %s ]\n", p_category->psz_name );
328 p_info = p_category->p_info;
331 printf( "| %s: %s\n", p_info->psz_name,
333 p_info = p_info->p_next;
335 p_category = p_category->p_next;
338 printf( "+----[ end of stream info ]\n" );
339 vlc_mutex_unlock( &p_input->stream.stream_lock );
343 printf( "no input\n" );
346 else switch( psz_cmd[0] )
350 if( psz_cmd[1] == ' ' && p_playlist )
352 playlist_Add( p_playlist, psz_cmd + 2,
353 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
361 vout_thread_t *p_vout;
362 p_vout = vlc_object_find( p_input,
363 VLC_OBJECT_VOUT, FIND_CHILD );
367 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
368 vlc_object_release( p_vout );
381 printf("+----[ remote control commands ]\n");
383 printf("| a XYZ . . . . . . . . . . . add XYZ to playlist\n");
384 printf("| play . . . . . . . . . . . . . . . . play stream\n");
385 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
386 printf("| next . . . . . . . . . . . . next playlist item\n");
387 printf("| prev . . . . . . . . . . previous playlist item\n");
388 printf("| title [X] . . . . set/get title in current item\n");
389 printf("| title_n . . . . . . next title in current item\n");
390 printf("| title_p . . . . previous title in current item\n");
391 printf("| chapter [X] . . set/get chapter in current item\n");
392 printf("| chapter_n . . . . next chapter in current item\n");
393 printf("| chapter_p . . previous chapter in current item\n");
395 printf("| seek X . seek in seconds, for instance `seek 12'\n");
396 printf("| pause . . . . . . . . . . . . . . toggle pause\n");
397 printf("| f . . . . . . . . . . . . . . toggle fullscreen\n");
398 printf("| info . . . information about the current stream\n");
400 printf("| volume [X] . . . . . . . . set/get audio volume\n");
401 printf("| volup [X] . . . . . raise audio volume X steps\n");
402 printf("| voldown [X] . . . . lower audio volume X steps\n");
403 printf("| adev [X] . . . . . . . . . set/get audio device\n");
404 printf("| achan [X]. . . . . . . . set/get audio channels\n");
406 printf("| help . . . . . . . . . . . . . this help message\n");
407 printf("| quit . . . . . . . . . . . . . . . . . quit vlc\n");
409 printf("+----[ end of help ]\n");
412 /* Ignore empty lines */
415 printf( "unknown command `%s', type `help' for help\n", psz_cmd );
423 vlc_object_release( p_input );
429 vlc_object_release( p_playlist );
434 static int Playlist( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
436 input_thread_t * p_input;
437 playlist_t * p_playlist;
439 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
446 /* Parse commands that only require an input */
447 if( !strcmp( psz_cmd, "pause" ) )
449 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
450 vlc_object_release( p_input );
453 else if( !strcmp( psz_cmd, "seek" ) )
455 input_Seek( p_input, atoi( psz_arg ),
456 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
458 else if( !strcmp( psz_cmd, "chapter" ) ||
459 !strcmp( psz_cmd, "chapter_n" ) ||
460 !strcmp( psz_cmd, "chapter_p" ) )
462 unsigned int i_chapter = 0;
464 if( !strcmp( psz_cmd, "chapter" ) )
469 i_chapter = atoi( psz_arg );
474 vlc_mutex_lock( &p_input->stream.stream_lock );
475 printf( "Currently playing chapter %d\n",
476 p_input->stream.p_selected_area->i_part );
477 vlc_mutex_unlock( &p_input->stream.stream_lock );
479 vlc_object_release( p_input );
483 else if( !strcmp( psz_cmd, "chapter_n" ) )
485 vlc_mutex_lock( &p_input->stream.stream_lock );
486 i_chapter = p_input->stream.p_selected_area->i_part + 1;
487 vlc_mutex_unlock( &p_input->stream.stream_lock );
489 else if( !strcmp( psz_cmd, "chapter_p" ) )
491 vlc_mutex_lock( &p_input->stream.stream_lock );
492 i_chapter = p_input->stream.p_selected_area->i_part - 1;
493 vlc_mutex_unlock( &p_input->stream.stream_lock );
496 vlc_mutex_lock( &p_input->stream.stream_lock );
497 if( ( i_chapter > 0 ) && ( i_chapter <=
498 p_input->stream.p_selected_area->i_part_nb ) )
500 p_input->stream.p_selected_area->i_part = i_chapter;
501 vlc_mutex_unlock( &p_input->stream.stream_lock );
502 input_ChangeArea( p_input,
503 (input_area_t*)p_input->stream.p_selected_area );
504 input_SetStatus( p_input, INPUT_STATUS_PLAY );
505 vlc_mutex_lock( &p_input->stream.stream_lock );
507 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 vlc_object_release( p_input );
512 else if( !strcmp( psz_cmd, "title" ) ||
513 !strcmp( psz_cmd, "title_n" ) ||
514 !strcmp( psz_cmd, "title_p" ) )
516 unsigned int i_title = 0;
518 if( !strcmp( psz_cmd, "title" ) )
523 i_title = atoi( psz_arg );
528 vlc_mutex_lock( &p_input->stream.stream_lock );
529 printf( "Currently playing title %d\n",
530 p_input->stream.p_selected_area->i_id );
531 vlc_mutex_unlock( &p_input->stream.stream_lock );
533 vlc_object_release( p_input );
537 else if( !strcmp( psz_cmd, "title_n" ) )
539 vlc_mutex_lock( &p_input->stream.stream_lock );
540 i_title = p_input->stream.p_selected_area->i_id + 1;
541 vlc_mutex_unlock( &p_input->stream.stream_lock );
543 else if( !strcmp( psz_cmd, "title_p" ) )
545 vlc_mutex_lock( &p_input->stream.stream_lock );
546 i_title = p_input->stream.p_selected_area->i_id - 1;
547 vlc_mutex_unlock( &p_input->stream.stream_lock );
550 vlc_mutex_lock( &p_input->stream.stream_lock );
551 if( ( i_title > 0 ) && ( i_title <=
552 p_input->stream.p_selected_area->i_part_nb ) )
554 p_input->stream.p_selected_area->i_part = i_title;
555 vlc_mutex_unlock( &p_input->stream.stream_lock );
556 input_ChangeArea( p_input,
557 (input_area_t*)p_input->stream.pp_areas[i_title] );
558 input_SetStatus( p_input, INPUT_STATUS_PLAY );
559 vlc_mutex_lock( &p_input->stream.stream_lock );
561 vlc_mutex_unlock( &p_input->stream.stream_lock );
563 vlc_object_release( p_input );
568 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
570 vlc_object_release( p_input );
577 /* Parse commands that require a playlist */
578 if( !strcmp( psz_cmd, "prev" ) )
580 playlist_Prev( p_playlist );
582 else if( !strcmp( psz_cmd, "next" ) )
584 playlist_Next( p_playlist );
586 else if( !strcmp( psz_cmd, "play" ) )
588 playlist_Play( p_playlist );
590 else if( !strcmp( psz_cmd, "stop" ) )
592 playlist_Stop( p_playlist );
595 vlc_object_release( p_playlist );
599 static int Quit( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
601 p_this->p_vlc->b_die = VLC_TRUE;
605 static int Intf( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
607 intf_thread_t *p_newintf;
608 char *psz_oldmodule = config_GetPsz( p_this->p_vlc, "intf" );
610 config_PutPsz( p_this->p_vlc, "intf", psz_arg );
611 p_newintf = intf_Create( p_this->p_vlc );
612 config_PutPsz( p_this->p_vlc, "intf", psz_oldmodule );
616 free( psz_oldmodule );
621 p_newintf->b_block = VLC_FALSE;
622 if( intf_RunThread( p_newintf ) )
624 vlc_object_detach( p_newintf );
625 intf_Destroy( p_newintf );
632 static int Signal( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
634 raise( atoi(psz_arg) );
638 static int Volume( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
640 aout_instance_t * p_aout;
642 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
643 if ( p_aout == NULL ) return VLC_ENOOBJ;
648 audio_volume_t i_volume = atoi( psz_arg );
649 if ( i_volume > AOUT_VOLUME_MAX )
651 printf( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
653 i_error = VLC_EBADVAR;
655 else i_error = aout_VolumeSet( p_aout, i_volume );
660 audio_volume_t i_volume;
661 if ( aout_VolumeGet( p_aout, &i_volume ) < 0 )
663 i_error = VLC_EGENERIC;
667 printf( "Volume is %d\n", i_volume );
668 i_error = VLC_SUCCESS;
671 vlc_object_release( (vlc_object_t *)p_aout );
676 static int VolumeMove( vlc_object_t * p_this, char * psz_cmd, char * psz_arg )
678 aout_instance_t * p_aout;
679 audio_volume_t i_volume;
680 int i_nb_steps = atoi(psz_arg);
681 int i_error = VLC_SUCCESS;
683 if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
688 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
689 if ( p_aout == NULL ) return VLC_ENOOBJ;
691 if ( !strcmp(psz_cmd, "volup") )
693 if ( aout_VolumeUp( p_aout, i_nb_steps, &i_volume ) < 0 )
694 i_error = VLC_EGENERIC;
698 if ( aout_VolumeDown( p_aout, i_nb_steps, &i_volume ) < 0 )
699 i_error = VLC_EGENERIC;
701 vlc_object_release( (vlc_object_t *)p_aout );
703 if ( !i_error ) printf( "Volume is %d\n", i_volume );
707 static int AudioConfig( vlc_object_t * p_this, char * psz_cmd, char * psz_arg )
709 aout_instance_t * p_aout;
710 const char * psz_variable;
711 const char * psz_name;
714 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
715 if ( p_aout == NULL ) return VLC_ENOOBJ;
717 if ( !strcmp( psz_cmd, "adev" ) )
719 psz_variable = "audio-device";
720 psz_name = "audio devices";
724 psz_variable = "audio-channels";
725 psz_name = "audio channels";
730 /* Retrieve all registered ***. */
733 vlc_value_t * p_vals;
736 if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
738 vlc_object_release( (vlc_object_t *)p_aout );
741 psz_value = val.psz_string;
743 if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
744 VLC_VAR_GETLIST, &val ) < 0 )
747 vlc_object_release( (vlc_object_t *)p_aout );
751 printf( "+----[ %s ]\n", psz_name );
752 i_vals = ((vlc_value_t *)val.p_address)[0].i_int;
753 p_vals = &((vlc_value_t *)val.p_address)[1]; /* Starts at index 1 */
754 for ( i = 0; i < i_vals; i++ )
756 if ( !strcmp( psz_value, p_vals[i].psz_string ) )
757 printf( "| %s *\n", p_vals[i].psz_string );
759 printf( "| %s\n", p_vals[i].psz_string );
761 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
763 printf( "+----[ end of %s ]\n", psz_name );
766 i_error = VLC_SUCCESS;
771 val.psz_string = psz_arg;
773 i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
775 vlc_object_release( (vlc_object_t *)p_aout );