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