]> git.sesse.net Git - vlc/blob - modules/control/rc/rc.c
075a55389eab46080b507a7c8b7eafa8a5d1095b
[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.4 2002/08/20 18:08:51 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 vlc_module_begin();
67     add_category_hint( N_("Remote control"), NULL );
68     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
69     set_description( _("remote control interface module") );
70     set_capability( "interface", 20 );
71     set_callbacks( Activate, NULL );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * Activate: initialize and create stuff
76  *****************************************************************************/
77 static int Activate( vlc_object_t *p_this )
78 {
79     intf_thread_t *p_intf = (intf_thread_t*)p_this;
80
81 #ifdef HAVE_ISATTY
82     /* Check that stdin is a TTY */
83     if( !isatty( 0 ) )
84     {
85         msg_Warn( p_intf, "fd 0 is not a TTY" );
86         return 1;
87     }
88 #endif
89
90     /* Non-buffered stdout */
91     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
92
93     p_intf->pf_run = Run;
94
95 #ifdef WIN32
96     AllocConsole();
97     freopen( "CONOUT$", "w", stdout );
98     freopen( "CONOUT$", "w", stderr );
99     freopen( "CONIN$", "r", stdin );
100     printf( VERSION_MESSAGE "\n" );
101 #endif
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     while( !p_intf->b_die )
136     {
137         b_complete = 0;
138
139         /* Check stdin */
140         tv.tv_sec = 0;
141         tv.tv_usec = 50000;
142         FD_ZERO( &fds );
143         FD_SET( STDIN_FILENO, &fds );
144
145         i_dummy = select( 32, &fds, NULL, NULL, &tv );
146         if( i_dummy > 0 )
147         {
148             int i_size = 0;
149
150             while( !p_intf->b_die
151                     && i_size < MAX_LINE_LENGTH
152                     && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
153                     && p_buffer[ i_size ] != '\r'
154                     && p_buffer[ i_size ] != '\n' )
155             {
156                 i_size++;
157             }
158
159             if( i_size == MAX_LINE_LENGTH
160                  || p_buffer[ i_size ] == '\r'
161                  || p_buffer[ i_size ] == '\n' )
162             {
163                 p_buffer[ i_size ] = 0;
164                 b_complete = 1;
165             }
166         }
167
168         /* Manage the input part */
169         if( p_input == NULL )
170         {
171             if( p_playlist )
172             {
173                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
174                                                        FIND_CHILD );
175             }
176             else
177             {
178                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
179                                                    FIND_ANYWHERE );
180                 if( p_input )
181                 {
182                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
183                                                            FIND_PARENT );
184                 }
185             }
186         }
187         else if( p_input->b_dead )
188         {
189             vlc_object_release( p_input );
190             p_input = NULL;
191         }
192
193         if( p_input && b_showpos )
194         {
195             /* Get position */
196             vlc_mutex_lock( &p_input->stream.stream_lock );
197             if( !p_input->b_die && p_input->stream.i_mux_rate )
198             {
199 #define A p_input->stream.p_selected_area
200                 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
201                 i_newpos = A->i_tell * f_ratio;
202
203                 if( i_oldpos != i_newpos )
204                 {
205                     i_oldpos = i_newpos;
206                     printf( "pos: %li s / %li s\n", (long int)i_newpos,
207                             (long int)(f_ratio * A->i_size) );
208                 }
209 #undef S
210             }
211             vlc_mutex_unlock( &p_input->stream.stream_lock );
212         }
213
214         /* Is there something to do? */
215         if( b_complete == 1 )
216         {
217             char *p_cmd = p_buffer;
218             //char *p_tmp;
219
220             if( !strcmp( p_cmd, "quit" ) )
221             {
222                 p_intf->p_vlc->b_die = VLC_TRUE;
223             }
224             else if( !strcmp( p_cmd, "segfault" ) )
225             {
226                 raise( SIGSEGV );
227             }
228             else if( !strcmp( p_cmd, "prev" ) )
229             {
230                 if( p_playlist ) playlist_Prev( p_playlist );
231             }
232             else if( !strcmp( p_cmd, "next" ) )
233             {
234                 if( p_playlist ) playlist_Next( p_playlist );
235             }
236             else if( !strcmp( p_cmd, "play" ) )
237             {
238                 if( p_playlist ) playlist_Play( p_playlist );
239             }
240             else if( !strcmp( p_cmd, "stop" ) )
241             {
242                 if( p_playlist ) playlist_Stop( p_playlist );
243             }
244             else if( !strcmp( p_cmd, "pause" ) )
245             {
246                 if( p_input ) input_SetStatus( p_input, INPUT_STATUS_PAUSE );
247             }
248             else if( !strcmp( p_cmd, "tree" ) )
249             {
250                 vlc_dumpstructure( p_intf->p_vlc );
251             }
252             else if( !strcmp( p_cmd, "list" ) )
253             {
254                 vlc_liststructure( p_intf->p_vlc );
255             }
256             else if( !strncmp( p_cmd, "set ", 4 ) )
257             {
258 #if 0
259 //                vlc_set_r( p_intf->p_vlc, p_cmd + 4, strstr( p_cmd + 4, " " ) );
260                 p_tmp = strstr( p_cmd + 4, " " );
261                 p_tmp[0] = '\0';
262                 config_PutPsz( p_intf->p_vlc, p_cmd + 4, p_tmp + 1 );
263                 config_PutInt( p_intf->p_vlc, p_cmd + 4, atoi(p_tmp + 1) );
264 #endif
265             }
266             else if( !strncmp( p_cmd, "intf ", 5 ) )
267             {
268                 intf_thread_t *p_newintf;
269                 char *psz_oldmodule = config_GetPsz( p_intf->p_vlc, "intf" );
270
271                 config_PutPsz( p_intf->p_vlc, "intf", p_cmd + 5 );
272                 p_newintf = intf_Create( p_intf->p_vlc );
273                 config_PutPsz( p_intf->p_vlc, "intf", psz_oldmodule );
274
275                 if( psz_oldmodule )
276                 {
277                     free( psz_oldmodule );
278                 }
279
280                 if( p_newintf )
281                 {
282                     p_newintf->b_block = VLC_FALSE;
283                     if( intf_RunThread( p_newintf ) )
284                     {
285                         vlc_object_detach( p_newintf );
286                         intf_Destroy( p_newintf );
287                     }
288                 }
289             }
290             else if( !strcmp( p_cmd, "info" ) )
291             {
292                 if ( p_input )
293                 {
294                     vlc_mutex_lock( &p_input->stream.stream_lock );
295                     p_category = p_input->stream.p_info;
296                     while ( p_category )
297                     {
298                         printf( "+----[ %s ]\n", p_category->psz_name );
299                         printf( "| \n" );
300                         p_info = p_category->p_info;
301                         while ( p_info )
302                         {
303                             printf( "| %s: %s\n", p_info->psz_name,
304                                     p_info->psz_value );
305                             p_info = p_info->p_next;
306                         }
307                         p_category = p_category->p_next;
308                         printf( "| \n" );
309                     }
310                     printf( "+----[ end of stream info ]\n" );
311                     vlc_mutex_unlock( &p_input->stream.stream_lock );
312                 }
313                 else
314                 {
315                     printf( "no input\n" );
316                 }
317             }
318             else switch( p_cmd[0] )
319             {
320             case 'a':
321             case 'A':
322                 if( p_cmd[1] == ' ' && p_playlist )
323                 {
324                     playlist_Add( p_playlist, p_cmd + 2,
325                                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
326                 }
327                 break;
328
329             case 'f':
330             case 'F':
331                 if( p_input )
332                 {
333                     vout_thread_t *p_vout;
334                     p_vout = vlc_object_find( p_input,
335                                               VLC_OBJECT_VOUT, FIND_CHILD );
336
337                     if( p_vout )
338                     {
339                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
340                         vlc_object_release( p_vout );
341                     }
342                 }
343                 break;
344
345             case 's':
346             case 'S':
347                 ;
348                 break;
349
350             case 'r':
351             case 'R':
352                 if( p_input )
353                 {
354                     for( i_dummy = 1;
355                          i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
356                                                    && p_cmd[ i_dummy ] <= '9';
357                          i_dummy++ )
358                     {
359                         ;
360                     }
361
362                     p_cmd[ i_dummy ] = 0;
363                     input_Seek( p_input, (off_t)atoi( p_cmd + 1 ),
364                                 INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
365                     /* rcreseek(f_cpos); */
366                 }
367                 break;
368
369             case '?':
370             case 'h':
371             case 'H':
372                 printf("+----[ remote control commands ]\n");
373                 printf("| \n");
374                 printf("| a XYZ  . . . . . . . . . . . add XYZ to playlist\n");
375                 printf("| play . . . . . . . . . . . . . . . . play stream\n");
376                 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
377                 printf("| next . . . . . . . . . . . .  next playlist item\n");
378                 printf("| prev . . . . . . . . . .  previous playlist item\n");
379                 printf("| \n");
380                 printf("| r X  . . . seek in seconds, for instance `r 3.5'\n");
381                 printf("| pause  . . . . . . . . . . . . . .  toggle pause\n");
382                 printf("| f  . . . . . . . . . . . . . . toggle fullscreen\n");
383                 printf("| info . . .  information about the current stream\n");
384                 printf("| \n");
385                 printf("| help . . . . . . . . . . . . . this help message\n");
386                 printf("| quit . . . . . . . . . . . . . . . . .  quit vlc\n");
387                 printf("| \n");
388                 printf("+----[ end of help ]\n");
389                 break;
390             case '\0':
391                 /* Ignore empty lines */
392                 break;
393             default:
394                 printf( "unknown command `%s', type `help' for help\n", p_cmd );
395                 break;
396             }
397         }
398
399         msleep( INTF_IDLE_SLEEP );
400     }
401
402     if( p_input )
403     {
404         vlc_object_release( p_input );
405         p_input = NULL;
406
407         if( p_playlist )
408         {
409             vlc_object_release( p_playlist );
410             p_playlist = NULL;
411         }
412     }
413 }
414