]> git.sesse.net Git - vlc/blob - modules/control/rc/rc.c
7faf52e29dd3333d0b5896acc229c48c3252f15c
[vlc] / modules / control / rc / rc.c
1 /*****************************************************************************
2  * rc.c : remote control stdin/stdout plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: rc.c,v 1.8 2002/10/11 22:32:56 sam Exp $
6  *
7  * Authors: Peter Surda <shurdeek@panorama.sth.ac.at>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37 #include <vlc/vout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46 #include <sys/types.h>
47
48 #if defined( WIN32 )
49 #include <winsock2.h>                                            /* select() */
50 #endif
51
52 #define MAX_LINE_LENGTH 256
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int  Activate     ( vlc_object_t * );
58 static void Run          ( intf_thread_t *p_intf );
59
60 /*****************************************************************************
61  * Module descriptor
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.")
65
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.")
68
69 vlc_module_begin();
70     add_category_hint( N_("Remote control"), NULL );
71     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
72 #ifdef HAVE_ISATTY
73     add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
74 #endif
75     set_description( _("remote control interface module") );
76     set_capability( "interface", 20 );
77     set_callbacks( Activate, NULL );
78 vlc_module_end();
79
80 /*****************************************************************************
81  * Activate: initialize and create stuff
82  *****************************************************************************/
83 static int Activate( vlc_object_t *p_this )
84 {
85     intf_thread_t *p_intf = (intf_thread_t*)p_this;
86
87 #ifdef HAVE_ISATTY
88     /* Check that stdin is a TTY */
89     if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
90     {
91         msg_Warn( p_intf, "fd 0 is not a TTY" );
92         return 1;
93     }
94 #endif
95
96     /* Non-buffered stdout */
97     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
98
99     p_intf->pf_run = Run;
100
101     CONSOLE_INTRO_MSG;
102
103     printf( "remote control interface initialized, `h' for help\n" );
104     return 0;
105 }
106
107 /*****************************************************************************
108  * Run: rc thread
109  *****************************************************************************
110  * This part of the interface is in a separate thread so that we can call
111  * exec() from within it without annoying the rest of the program.
112  *****************************************************************************/
113 static void Run( intf_thread_t *p_intf )
114 {
115     input_thread_t * p_input;
116     playlist_t *     p_playlist;
117
118     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
119     vlc_bool_t b_complete = 0;
120     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
121     input_info_category_t * p_category;
122     input_info_t * p_info;
123
124     int        i_dummy;
125     off_t      i_oldpos = 0;
126     off_t      i_newpos;
127     fd_set     fds;                                        /* stdin changed? */
128     struct timeval tv;                                   /* how long to wait */
129
130     double     f_ratio = 1;
131
132     p_input = NULL;
133     p_playlist = NULL;
134
135     var_Create( p_intf, "foo", VLC_VAR_STRING );
136     var_Set( p_intf, "foo", (vlc_value_t)"test" );
137
138     while( !p_intf->b_die )
139     {
140         b_complete = 0;
141
142         /* Check stdin */
143         tv.tv_sec = 0;
144         tv.tv_usec = 50000;
145         FD_ZERO( &fds );
146         FD_SET( STDIN_FILENO, &fds );
147
148         i_dummy = select( 32, &fds, NULL, NULL, &tv );
149         if( i_dummy > 0 )
150         {
151             int i_size = 0;
152
153             while( !p_intf->b_die
154                     && i_size < MAX_LINE_LENGTH
155                     && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
156                     && p_buffer[ i_size ] != '\r'
157                     && p_buffer[ i_size ] != '\n' )
158             {
159                 i_size++;
160             }
161
162             if( i_size == MAX_LINE_LENGTH
163                  || p_buffer[ i_size ] == '\r'
164                  || p_buffer[ i_size ] == '\n' )
165             {
166                 p_buffer[ i_size ] = 0;
167                 b_complete = 1;
168             }
169         }
170
171         /* Manage the input part */
172         if( p_input == NULL )
173         {
174             if( p_playlist )
175             {
176                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
177                                                        FIND_CHILD );
178             }
179             else
180             {
181                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
182                                                    FIND_ANYWHERE );
183                 if( p_input )
184                 {
185                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
186                                                            FIND_PARENT );
187                 }
188             }
189         }
190         else if( p_input->b_dead )
191         {
192             vlc_object_release( p_input );
193             p_input = NULL;
194         }
195
196         if( p_input && b_showpos )
197         {
198             /* Get position */
199             vlc_mutex_lock( &p_input->stream.stream_lock );
200             if( !p_input->b_die && p_input->stream.i_mux_rate )
201             {
202 #define A p_input->stream.p_selected_area
203                 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
204                 i_newpos = A->i_tell * f_ratio;
205
206                 if( i_oldpos != i_newpos )
207                 {
208                     i_oldpos = i_newpos;
209                     printf( "pos: %li s / %li s\n", (long int)i_newpos,
210                             (long int)(f_ratio * A->i_size) );
211                 }
212 #undef S
213             }
214             vlc_mutex_unlock( &p_input->stream.stream_lock );
215         }
216
217         /* Is there something to do? */
218         if( b_complete == 1 )
219         {
220             char *p_cmd = p_buffer;
221
222             if( !strcmp( p_cmd, "quit" ) )
223             {
224                 p_intf->p_vlc->b_die = VLC_TRUE;
225             }
226             else if( !strcmp( p_cmd, "segfault" ) )
227             {
228                 raise( SIGSEGV );
229             }
230             else if( !strcmp( p_cmd, "prev" ) )
231             {
232                 if( p_playlist ) playlist_Prev( p_playlist );
233             }
234             else if( !strcmp( p_cmd, "next" ) )
235             {
236                 if( p_playlist ) playlist_Next( p_playlist );
237             }
238             else if( !strcmp( p_cmd, "play" ) )
239             {
240                 if( p_playlist ) playlist_Play( p_playlist );
241             }
242             else if( !strcmp( p_cmd, "stop" ) )
243             {
244                 if( p_playlist ) playlist_Stop( p_playlist );
245             }
246             else if( !strcmp( p_cmd, "pause" ) )
247             {
248                 if( p_input ) input_SetStatus( p_input, INPUT_STATUS_PAUSE );
249             }
250             else if( !strcmp( p_cmd, "tree" ) )
251             {
252                 vlc_dumpstructure( p_intf->p_vlc );
253             }
254             else if( !strcmp( p_cmd, "list" ) )
255             {
256                 vlc_liststructure( p_intf->p_vlc );
257             }
258             else if( !strncmp( p_cmd, "setfoo ", 7 ) )
259             {
260                 vlc_value_t value;
261                 value.psz_string = p_cmd + 7;
262                 var_Set( p_intf, "foo", value );
263             }
264             else if( !strncmp( p_cmd, "getfoo", 6 ) )
265             {
266                 vlc_value_t value;
267                 var_Get( p_intf, "foo", &value );
268                 printf( "current value is '%s'\n", value.psz_string );
269             }
270             else if( !strncmp( p_cmd, "intf ", 5 ) )
271             {
272                 intf_thread_t *p_newintf;
273                 char *psz_oldmodule = config_GetPsz( p_intf->p_vlc, "intf" );
274
275                 config_PutPsz( p_intf->p_vlc, "intf", p_cmd + 5 );
276                 p_newintf = intf_Create( p_intf->p_vlc );
277                 config_PutPsz( p_intf->p_vlc, "intf", psz_oldmodule );
278
279                 if( psz_oldmodule )
280                 {
281                     free( psz_oldmodule );
282                 }
283
284                 if( p_newintf )
285                 {
286                     p_newintf->b_block = VLC_FALSE;
287                     if( intf_RunThread( p_newintf ) )
288                     {
289                         vlc_object_detach( p_newintf );
290                         intf_Destroy( p_newintf );
291                     }
292                 }
293             }
294             else if( !strcmp( p_cmd, "info" ) )
295             {
296                 if ( p_input )
297                 {
298                     vlc_mutex_lock( &p_input->stream.stream_lock );
299                     p_category = p_input->stream.p_info;
300                     while ( p_category )
301                     {
302                         printf( "+----[ %s ]\n", p_category->psz_name );
303                         printf( "| \n" );
304                         p_info = p_category->p_info;
305                         while ( p_info )
306                         {
307                             printf( "| %s: %s\n", p_info->psz_name,
308                                     p_info->psz_value );
309                             p_info = p_info->p_next;
310                         }
311                         p_category = p_category->p_next;
312                         printf( "| \n" );
313                     }
314                     printf( "+----[ end of stream info ]\n" );
315                     vlc_mutex_unlock( &p_input->stream.stream_lock );
316                 }
317                 else
318                 {
319                     printf( "no input\n" );
320                 }
321             }
322             else switch( p_cmd[0] )
323             {
324             case 'a':
325             case 'A':
326                 if( p_cmd[1] == ' ' && p_playlist )
327                 {
328                     playlist_Add( p_playlist, p_cmd + 2,
329                                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
330                 }
331                 break;
332
333             case 'f':
334             case 'F':
335                 if( p_input )
336                 {
337                     vout_thread_t *p_vout;
338                     p_vout = vlc_object_find( p_input,
339                                               VLC_OBJECT_VOUT, FIND_CHILD );
340
341                     if( p_vout )
342                     {
343                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
344                         vlc_object_release( p_vout );
345                     }
346                 }
347                 break;
348
349             case 's':
350             case 'S':
351                 ;
352                 break;
353
354             case 'r':
355             case 'R':
356                 if( p_input )
357                 {
358                     for( i_dummy = 1;
359                          i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
360                                                    && p_cmd[ i_dummy ] <= '9';
361                          i_dummy++ )
362                     {
363                         ;
364                     }
365
366                     p_cmd[ i_dummy ] = 0;
367                     input_Seek( p_input, (off_t)atoi( p_cmd + 1 ),
368                                 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
369                     /* rcreseek(f_cpos); */
370                 }
371                 break;
372
373             case '?':
374             case 'h':
375             case 'H':
376                 printf("+----[ remote control commands ]\n");
377                 printf("| \n");
378                 printf("| a XYZ  . . . . . . . . . . . add XYZ to playlist\n");
379                 printf("| play . . . . . . . . . . . . . . . . play stream\n");
380                 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
381                 printf("| next . . . . . . . . . . . .  next playlist item\n");
382                 printf("| prev . . . . . . . . . .  previous playlist item\n");
383                 printf("| \n");
384                 printf("| r X  . . . seek in seconds, for instance `r 3.5'\n");
385                 printf("| pause  . . . . . . . . . . . . . .  toggle pause\n");
386                 printf("| f  . . . . . . . . . . . . . . toggle fullscreen\n");
387                 printf("| info . . .  information about the current stream\n");
388                 printf("| \n");
389                 printf("| help . . . . . . . . . . . . . this help message\n");
390                 printf("| quit . . . . . . . . . . . . . . . . .  quit vlc\n");
391                 printf("| \n");
392                 printf("+----[ end of help ]\n");
393                 break;
394             case '\0':
395                 /* Ignore empty lines */
396                 break;
397             default:
398                 printf( "unknown command `%s', type `help' for help\n", p_cmd );
399                 break;
400             }
401         }
402     }
403
404     if( p_input )
405     {
406         vlc_object_release( p_input );
407         p_input = NULL;
408     }
409
410     if( p_playlist )
411     {
412         vlc_object_release( p_playlist );
413         p_playlist = NULL;
414     }
415
416     var_Destroy( p_intf, "foo" );
417 }
418