]> git.sesse.net Git - vlc/blob - plugins/text/rc.c
* Replacement header for aout_common.h.
[vlc] / plugins / text / 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.10 2002/02/15 13:32:53 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 <unistd.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36
37 #include <videolan/vlc.h>
38
39 #if defined( WIN32 )
40 #include <winsock2.h>                                            /* select() */
41 #endif
42
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
45
46 #include "intf_playlist.h"
47 #include "interface.h"
48
49 #include "video.h"
50 #include "video_output.h"
51
52 /*****************************************************************************
53  * intf_sys_t: description and status of rc interface
54  *****************************************************************************/
55 typedef struct intf_sys_s
56 {
57     vlc_mutex_t         change_lock;
58
59 } intf_sys_t;
60
61 #define MAX_LINE_LENGTH 256
62
63 /*****************************************************************************
64  * Local prototypes.
65  *****************************************************************************/
66 static void intf_getfunctions ( function_list_t * p_function_list );
67 static int  intf_Open         ( intf_thread_t *p_intf );
68 static void intf_Close        ( intf_thread_t *p_intf );
69 static void intf_Run          ( intf_thread_t *p_intf );
70
71 /*****************************************************************************
72  * Build configuration tree.
73  *****************************************************************************/
74 MODULE_CONFIG_START
75 MODULE_CONFIG_STOP
76
77 MODULE_INIT_START
78     SET_DESCRIPTION( "remote control interface module" )
79     ADD_CAPABILITY( INTF, 20 )
80     ADD_SHORTCUT( "rc" )
81 MODULE_INIT_STOP
82
83 MODULE_ACTIVATE_START
84     intf_getfunctions( &p_module->p_functions->intf );
85 MODULE_ACTIVATE_STOP
86
87 MODULE_DEACTIVATE_START
88 MODULE_DEACTIVATE_STOP
89
90 /*****************************************************************************
91  * Functions exported as capabilities. They are declared as static so that
92  * we don't pollute the namespace too much.
93  *****************************************************************************/
94 static void intf_getfunctions( function_list_t * p_function_list )
95 {
96     p_function_list->functions.intf.pf_open  = intf_Open;
97     p_function_list->functions.intf.pf_close = intf_Close;
98     p_function_list->functions.intf.pf_run   = intf_Run;
99 }
100
101 /*****************************************************************************
102  * intf_Open: initialize and create stuff
103  *****************************************************************************/
104 static int intf_Open( intf_thread_t *p_intf )
105 {
106     /* Non-buffered stdout */
107     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
108
109     /* Allocate instance and initialize some members */
110     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
111     if( p_intf->p_sys == NULL )
112     {
113         intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
114         return( 1 );
115     }
116
117     intf_Msg( "rc: remote control interface initialized, `h' for help" );
118     return( 0 );
119 }
120
121 /*****************************************************************************
122  * intf_Close: destroy interface stuff
123  *****************************************************************************/
124 static void intf_Close( intf_thread_t *p_intf )
125 {
126     /* Destroy structure */
127     free( p_intf->p_sys );
128 }
129
130 /*****************************************************************************
131  * intf_Run: rc thread
132  *****************************************************************************
133  * This part of the interface is in a separate thread so that we can call
134  * exec() from within it without annoying the rest of the program.
135  *****************************************************************************/
136 static void intf_Run( intf_thread_t *p_intf )
137 {
138     char      p_cmd[ MAX_LINE_LENGTH + 1 ];
139     int       i_cmd_pos;
140     boolean_t b_complete = 0;
141
142     int       i_dummy;
143     off_t     i_oldpos = 0;
144     off_t     i_newpos;
145     fd_set    fds;                                         /* stdin changed? */
146     struct timeval tv;                                   /* how long to wait */
147
148     double    f_cpos;
149     double    f_ratio = 1;
150
151     while( !p_intf->b_die )
152     {
153         vlc_mutex_lock( &p_input_bank->lock );
154 #define S p_input_bank->pp_input[0]->stream
155         if( p_input_bank->pp_input[0] != NULL )
156         {
157             /* Get position */
158             if( S.i_mux_rate )
159             {
160                 f_ratio = 1.0 / ( 50 * S.i_mux_rate );
161                 i_newpos = S.p_selected_area->i_tell * f_ratio;
162
163                 if( i_oldpos != i_newpos )
164                 {
165                     i_oldpos = i_newpos;
166                     intf_Msg( "rc: pos: %li s / %li s", (long int)i_newpos,
167                               (long int)( f_ratio *
168                                           S.p_selected_area->i_size ) );
169                 }
170             }
171         }
172 #undef S
173         vlc_mutex_unlock( &p_input_bank->lock );
174
175         b_complete = 0;
176         i_cmd_pos = 0;
177
178         /* Check stdin */
179         tv.tv_sec = 0;
180         tv.tv_usec = 50000;
181         FD_ZERO( &fds );
182         FD_SET( STDIN_FILENO, &fds );
183
184         if( select( 32, &fds, NULL, NULL, &tv ) )
185         {
186             while( !p_intf->b_die
187                     && i_cmd_pos < MAX_LINE_LENGTH
188                     && read( STDIN_FILENO, p_cmd + i_cmd_pos, 1 ) > 0
189                     && p_cmd[ i_cmd_pos ] != '\r'
190                     && p_cmd[ i_cmd_pos ] != '\n' )
191             {
192                 i_cmd_pos++;
193             }
194
195             if( i_cmd_pos == MAX_LINE_LENGTH
196                  || p_cmd[ i_cmd_pos ] == '\r'
197                  || p_cmd[ i_cmd_pos ] == '\n' )
198             {
199                 p_cmd[ i_cmd_pos ] = 0;
200                 b_complete = 1;
201             }
202         }
203
204         vlc_mutex_lock( &p_input_bank->lock );
205
206         /* Is there something to do? */
207         if( b_complete == 1 )
208         {
209             switch( p_cmd[ 0 ] )
210             {
211             case 'a':
212             case 'A':
213                 if( p_cmd[ 1 ] == ' ' )
214                 {
215                     intf_PlaylistAdd( p_main->p_playlist,
216                                       PLAYLIST_END, p_cmd + 2 );
217                     if( p_input_bank->pp_input[0] != NULL )
218                     {
219                         p_input_bank->pp_input[0]->b_eof = 1;
220                     }
221                     intf_PlaylistJumpto( p_main->p_playlist,
222                                          p_main->p_playlist->i_size - 2 );
223                 }
224                 break;
225
226             case 'p':
227             case 'P':
228                 if( p_input_bank->pp_input[0] != NULL )
229                 {
230                     input_SetStatus( p_input_bank->pp_input[0],
231                                      INPUT_STATUS_PAUSE );
232                 }
233                 break;
234
235             case 'f':
236             case 'F':
237                 vlc_mutex_lock( &p_vout_bank->lock );
238                 /* XXX: only fullscreen the first video output */
239                 if( p_vout_bank->i_count )
240                 {
241                     p_vout_bank->pp_vout[0]->i_changes
242                                       |= VOUT_FULLSCREEN_CHANGE;
243                 }
244                 vlc_mutex_unlock( &p_vout_bank->lock );
245                 break;
246
247             case 'm':
248             case 'M':
249 #if 0
250                 double picratio = p_intf->p_input->p_default_vout->i_width 
251                     / p_intf->p_input->p_default_vout->i_height;
252                 if (picratio
253                 p_intf->p_input->p_default_vout->i_width=800
254                 p_intf->p_input->p_default_vout->i_changes |= 
255                     VOUT_FULLSCREEN_CHANGE;
256 #endif
257                 break;
258
259             case 's':
260             case 'S':
261                 ;
262                 break;
263
264             case 'q':
265             case 'Q':
266                 p_intf->b_die = 1;
267                 break;
268
269             case 'r':
270             case 'R':
271                 if( p_input_bank->pp_input[0] != NULL )
272                 {
273                     for( i_dummy = 1;
274                          i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
275                                                    && p_cmd[ i_dummy ] <= '9';
276                          i_dummy++ )
277                     {
278                         ;
279                     }
280
281                     p_cmd[ i_dummy ] = 0;
282                     f_cpos = atof( p_cmd + 1 );
283                     input_Seek( p_input_bank->pp_input[0],
284                                 (off_t) (f_cpos / f_ratio) );
285                     /* rcreseek(f_cpos); */
286                 }
287                 break;
288
289             case '?':
290             case 'h':
291             case 'H':
292                 intf_Msg( "rc: help for remote control commands" );
293                 intf_Msg( "rc: h                                       help" );
294                 intf_Msg( "rc: a XYZ                 append XYZ to playlist" );
295                 intf_Msg( "rc: p                               toggle pause" );
296                 intf_Msg( "rc: f                          toggle fullscreen" );
297                 intf_Msg( "rc: r X    seek in seconds, for instance `r 3.5'" );
298                 intf_Msg( "rc: q                                       quit" );
299                 intf_Msg( "rc: end of help" );
300                 break;
301
302             default:
303                 intf_Msg( "rc: unknown command `%s'", p_cmd );
304                 break;
305             }
306         }
307
308         vlc_mutex_unlock( &p_input_bank->lock );
309
310         p_intf->pf_manage( p_intf );
311         msleep( INTF_IDLE_SLEEP );
312     }
313 }
314