]> git.sesse.net Git - vlc/blob - plugins/text/intf_rc.c
* Made the remote command plugin usable even with no stream.
[vlc] / plugins / text / intf_rc.c
1 /*****************************************************************************
2  * intf_rc.cpp: remote control interface
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: intf_rc.cpp,v 0.1 2001/04/27 shurdeek
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 #define MODULE_NAME rc
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdlib.h>                                                /* free() */
34 #include <string.h>                                            /* strerror() */
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40
41 #include "config.h"
42 #include "common.h"
43 #include "threads.h"
44 #include "mtime.h"
45 #include "tests.h"
46
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
49
50 #include "intf_msg.h"
51 #include "intf_playlist.h"
52 #include "interface.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "modules.h"
58 #include "modules_export.h"
59
60 /*****************************************************************************
61  * intf_sys_t: description and status of rc interface
62  *****************************************************************************/
63 typedef struct intf_sys_s
64 {
65     vlc_mutex_t         change_lock;
66
67 } intf_sys_t;
68
69 #define MAX_LINE_LENGTH 256
70
71 /*****************************************************************************
72  * Local prototypes.
73  *****************************************************************************/
74 static int  intf_Probe     ( probedata_t *p_data );
75 static int  intf_Open      ( intf_thread_t *p_intf );
76 static void intf_Close     ( intf_thread_t *p_intf );
77 static void intf_Run       ( intf_thread_t *p_intf );
78
79 /*****************************************************************************
80  * Functions exported as capabilities. They are declared as static so that
81  * we don't pollute the namespace too much.
82  *****************************************************************************/
83 void _M( intf_getfunctions )( function_list_t * p_function_list )
84 {
85     p_function_list->pf_probe = intf_Probe;
86     p_function_list->functions.intf.pf_open  = intf_Open;
87     p_function_list->functions.intf.pf_close = intf_Close;
88     p_function_list->functions.intf.pf_run   = intf_Run;
89 }
90
91 /*****************************************************************************
92  * intf_Probe: probe the interface and return a score
93  *****************************************************************************
94  * This function tries to initialize rc and returns a score to the
95  * plugin manager so that it can select the best plugin.
96  *****************************************************************************/
97 static int intf_Probe( probedata_t *p_data )
98 {
99     if( TestMethod( INTF_METHOD_VAR, "rc" ) )
100     {
101         return( 999 );
102     }
103
104     return( 2 );
105 }
106
107 /*****************************************************************************
108  * intf_Open: initialize and create stuff
109  *****************************************************************************/
110 static int intf_Open( intf_thread_t *p_intf )
111 {
112     /* Non-buffered stdout */
113     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
114
115     /* Allocate instance and initialize some members */
116     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
117     if( p_intf->p_sys == NULL )
118     {
119         intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
120         return( 1 );
121     }
122
123     return( 0 );
124 }
125
126 /*****************************************************************************
127  * intf_Close: destroy interface stuff
128  *****************************************************************************/
129 static void intf_Close( intf_thread_t *p_intf )
130 {
131     /* Destroy structure */
132     free( p_intf->p_sys );
133 }
134
135 /*****************************************************************************
136  * intf_Run: rc thread
137  *****************************************************************************
138  * This part of the interface is in a separate thread so that we can call
139  * exec() from within it without annoying the rest of the program.
140  *****************************************************************************/
141 static void intf_Run( intf_thread_t *p_intf )
142 {
143     char      p_cmd[ MAX_LINE_LENGTH + 1 ];
144     int       i_cmd_pos;
145     boolean_t b_complete = 0;
146
147     int       i_dummy;
148     off_t     i_oldpos = 0;
149     off_t     i_newpos;
150     fd_set    fds;                                         /* stdin changed? */
151     struct timeval tv;                                   /* how long to wait */
152
153     double    f_cpos;
154     double    f_ratio = 1;
155
156     while( !p_intf->b_die )
157     {
158 #define S p_intf->p_input->stream
159         if( p_intf->p_input != NULL )
160         {
161             /* Get position */
162             if( S.i_mux_rate )
163             {
164                 f_ratio = 1.0 / ( 50 * S.i_mux_rate );
165                 i_newpos = S.p_selected_area->i_tell * f_ratio;
166
167                 if( i_oldpos != i_newpos )
168                 {
169                     i_oldpos = i_newpos;
170                     intf_Msg( "rc: pos: %li s / %li s", (long int)i_newpos,
171                               (long int)( f_ratio *
172                                           S.p_selected_area->i_size ) );
173                 }
174             }
175         }
176 #undef S
177
178         b_complete = 0;
179         i_cmd_pos = 0;
180
181         /* Check stdin */
182         tv.tv_sec = 0;
183         tv.tv_usec = 50000;
184         FD_ZERO( &fds );
185         FD_SET( STDIN_FILENO, &fds );
186
187         if( select( 32, &fds, NULL, NULL, &tv ) )
188         {
189             while( !p_intf->b_die
190                     && i_cmd_pos < MAX_LINE_LENGTH
191                     && read( STDIN_FILENO, p_cmd + i_cmd_pos, 1 ) > 0
192                     && p_cmd[ i_cmd_pos ] != '\r'
193                     && p_cmd[ i_cmd_pos ] != '\n' )
194             {
195                 i_cmd_pos++;
196             }
197
198             if( i_cmd_pos == MAX_LINE_LENGTH
199                  || p_cmd[ i_cmd_pos ] == '\r'
200                  || p_cmd[ i_cmd_pos ] == '\n' )
201             {
202                 p_cmd[ i_cmd_pos ] = 0;
203                 b_complete = 1;
204             }
205         }
206
207         /* Is there something to do? */
208         if( b_complete == 1 )
209         {
210             switch( p_cmd[ 0 ] )
211             {
212             case 'p':
213             case 'P':
214                 if( p_intf->p_input != NULL )
215                 {
216                     input_SetStatus( p_intf->p_input, INPUT_STATUS_PAUSE );
217                 }
218                 break;
219
220             case 'f':
221             case 'F':
222                 vlc_mutex_lock( &p_vout_bank->lock );
223                 /* XXX: only fullscreen the first video output */
224                 if( p_vout_bank->i_count )
225                 {
226                     p_vout_bank->pp_vout[0]->i_changes
227                                       |= VOUT_FULLSCREEN_CHANGE;
228                 }
229                 vlc_mutex_unlock( &p_vout_bank->lock );
230                 break;
231
232             case 'm':
233             case 'M':
234 #if 0
235                 double picratio = p_intf->p_input->p_default_vout->i_width 
236                     / p_intf->p_input->p_default_vout->i_height;
237                 if (picratio
238                 p_intf->p_input->p_default_vout->i_width=800
239                 p_intf->p_input->p_default_vout->i_changes |= 
240                     VOUT_FULLSCREEN_CHANGE;
241 #endif
242                 break;
243
244             case 's':
245             case 'S':
246                 ;
247                 break;
248
249             case 'q':
250             case 'Q':
251                 p_intf->b_die = 1;
252                 break;
253
254             case 'r':
255             case 'R':
256                 if( p_intf->p_input != NULL )
257                 {
258                     for( i_dummy = 1;
259                          i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
260                                                    && p_cmd[ i_dummy ] <= '9';
261                          i_dummy++ )
262                     {
263                         ;
264                     }
265
266                     p_cmd[ i_dummy ] = 0;
267                     f_cpos = atof( p_cmd + 1 );
268                     input_Seek( p_intf->p_input, (off_t) (f_cpos / f_ratio) );
269                     /* rcreseek(f_cpos); */
270                 }
271                 break;
272
273             default:
274                 intf_Msg( "rc: unknown command `%s'", p_cmd );
275                 break;
276             }
277         }
278
279         p_intf->pf_manage( p_intf );
280         msleep( INTF_IDLE_SLEEP );
281     }
282 }
283