rc.c 10.8 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
2
 * rc.c : remote control stdin/stdout plugin for vlc
Sam Hocevar's avatar
 
Sam Hocevar committed
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: rc.c,v 1.13 2002/05/18 07:30:03 sam Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * Authors: Peter Surda <shurdeek@panorama.sth.ac.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
27
28
#include <videolan/vlc.h>

Sam Hocevar's avatar
 
Sam Hocevar committed
29
#include <stdlib.h>                                      /* malloc(), free() */
30
#include <string.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
31

Sam Hocevar's avatar
   
Sam Hocevar committed
32
33
34
35
#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <ctype.h>

gbazin's avatar
   
gbazin committed
36
37
38
39
40
41
42
43
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#include <sys/types.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
44
45
46
47

#if defined( WIN32 )
#include <winsock2.h>                                            /* select() */
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
48

Sam Hocevar's avatar
   
Sam Hocevar committed
49
50
51
52
53
54
55
56
#include "stream_control.h"
#include "input_ext-intf.h"

#include "intf_playlist.h"
#include "interface.h"

#include "video.h"
#include "video_output.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
57
58

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
59
 * intf_sys_t: description and status of rc interface
Sam Hocevar's avatar
 
Sam Hocevar committed
60
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
typedef struct intf_sys_s
{
    vlc_mutex_t         change_lock;

} intf_sys_t;

#define MAX_LINE_LENGTH 256

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static void intf_getfunctions ( function_list_t * p_function_list );
static int  intf_Open         ( intf_thread_t *p_intf );
static void intf_Close        ( intf_thread_t *p_intf );
static void intf_Run          ( intf_thread_t *p_intf );
Sam Hocevar's avatar
 
Sam Hocevar committed
76
77

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
78
 * Build configuration tree.
Sam Hocevar's avatar
 
Sam Hocevar committed
79
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
80
81
MODULE_CONFIG_START
MODULE_CONFIG_STOP
Sam Hocevar's avatar
 
Sam Hocevar committed
82

Sam Hocevar's avatar
   
Sam Hocevar committed
83
MODULE_INIT_START
Sam Hocevar's avatar
   
Sam Hocevar committed
84
    SET_DESCRIPTION( _("remote control interface module") )
Sam Hocevar's avatar
   
Sam Hocevar committed
85
86
    ADD_CAPABILITY( INTF, 20 )
    ADD_SHORTCUT( "rc" )
Sam Hocevar's avatar
   
Sam Hocevar committed
87
MODULE_INIT_STOP
Sam Hocevar's avatar
 
Sam Hocevar committed
88

Sam Hocevar's avatar
   
Sam Hocevar committed
89
MODULE_ACTIVATE_START
Sam Hocevar's avatar
   
Sam Hocevar committed
90
    intf_getfunctions( &p_module->p_functions->intf );
Sam Hocevar's avatar
   
Sam Hocevar committed
91
MODULE_ACTIVATE_STOP
Sam Hocevar's avatar
 
Sam Hocevar committed
92

Sam Hocevar's avatar
   
Sam Hocevar committed
93
94
MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP
Sam Hocevar's avatar
 
Sam Hocevar committed
95

Sam Hocevar's avatar
   
Sam Hocevar committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
static void intf_getfunctions( function_list_t * p_function_list )
{
    p_function_list->functions.intf.pf_open  = intf_Open;
    p_function_list->functions.intf.pf_close = intf_Close;
    p_function_list->functions.intf.pf_run   = intf_Run;
}

/*****************************************************************************
 * intf_Open: initialize and create stuff
 *****************************************************************************/
static int intf_Open( intf_thread_t *p_intf )
{
    /* Non-buffered stdout */
    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );

    /* Allocate instance and initialize some members */
    p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
        return( 1 );
    }

    intf_Msg( "rc: remote control interface initialized, `h' for help" );
    return( 0 );
}

/*****************************************************************************
 * intf_Close: destroy interface stuff
 *****************************************************************************/
static void intf_Close( intf_thread_t *p_intf )
{
    /* Destroy structure */
    free( p_intf->p_sys );
}

/*****************************************************************************
 * intf_Run: rc thread
 *****************************************************************************
 * This part of the interface is in a separate thread so that we can call
 * exec() from within it without annoying the rest of the program.
 *****************************************************************************/
static void intf_Run( intf_thread_t *p_intf )
{
    char      p_cmd[ MAX_LINE_LENGTH + 1 ];
    int       i_cmd_pos;
    boolean_t b_complete = 0;

    int       i_dummy;
    off_t     i_oldpos = 0;
    off_t     i_newpos;
    fd_set    fds;                                         /* stdin changed? */
    struct timeval tv;                                   /* how long to wait */

    double    f_cpos;
    double    f_ratio = 1;

    while( !p_intf->b_die )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
159
160
161
        vlc_mutex_lock( &p_input_bank->lock );
#define S p_input_bank->pp_input[0]->stream
        if( p_input_bank->pp_input[0] != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
162
163
        {
            /* Get position */
164
165
            vlc_mutex_lock( &S.stream_lock );
            if( !p_input_bank->pp_input[0]->b_die && S.i_mux_rate )
Sam Hocevar's avatar
   
Sam Hocevar committed
166
167
168
169
170
171
172
173
174
175
176
177
            {
                f_ratio = 1.0 / ( 50 * S.i_mux_rate );
                i_newpos = S.p_selected_area->i_tell * f_ratio;

                if( i_oldpos != i_newpos )
                {
                    i_oldpos = i_newpos;
                    intf_Msg( "rc: pos: %li s / %li s", (long int)i_newpos,
                              (long int)( f_ratio *
                                          S.p_selected_area->i_size ) );
                }
            }
178
            vlc_mutex_unlock( &S.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
179
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
180
#undef S
Sam Hocevar's avatar
   
Sam Hocevar committed
181
        vlc_mutex_unlock( &p_input_bank->lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

        b_complete = 0;
        i_cmd_pos = 0;

        /* Check stdin */
        tv.tv_sec = 0;
        tv.tv_usec = 50000;
        FD_ZERO( &fds );
        FD_SET( STDIN_FILENO, &fds );

        if( select( 32, &fds, NULL, NULL, &tv ) )
        {
            while( !p_intf->b_die
                    && i_cmd_pos < MAX_LINE_LENGTH
                    && read( STDIN_FILENO, p_cmd + i_cmd_pos, 1 ) > 0
                    && p_cmd[ i_cmd_pos ] != '\r'
                    && p_cmd[ i_cmd_pos ] != '\n' )
            {
                i_cmd_pos++;
            }

            if( i_cmd_pos == MAX_LINE_LENGTH
                 || p_cmd[ i_cmd_pos ] == '\r'
                 || p_cmd[ i_cmd_pos ] == '\n' )
            {
                p_cmd[ i_cmd_pos ] = 0;
                b_complete = 1;
            }
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
212
213
        vlc_mutex_lock( &p_input_bank->lock );

Sam Hocevar's avatar
   
Sam Hocevar committed
214
215
216
217
218
219
220
221
222
223
224
        /* Is there something to do? */
        if( b_complete == 1 )
        {
            switch( p_cmd[ 0 ] )
            {
            case 'a':
            case 'A':
                if( p_cmd[ 1 ] == ' ' )
                {
                    intf_PlaylistAdd( p_main->p_playlist,
                                      PLAYLIST_END, p_cmd + 2 );
Sam Hocevar's avatar
   
Sam Hocevar committed
225
                    if( p_input_bank->pp_input[0] != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
226
                    {
Sam Hocevar's avatar
   
Sam Hocevar committed
227
                        p_input_bank->pp_input[0]->b_eof = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
228
229
230
231
232
233
234
235
                    }
                    intf_PlaylistJumpto( p_main->p_playlist,
                                         p_main->p_playlist->i_size - 2 );
                }
                break;

            case 'p':
            case 'P':
Sam Hocevar's avatar
   
Sam Hocevar committed
236
                if( p_input_bank->pp_input[0] != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
237
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
238
239
                    input_SetStatus( p_input_bank->pp_input[0],
                                     INPUT_STATUS_PAUSE );
Sam Hocevar's avatar
   
Sam Hocevar committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
                }
                break;

            case 'f':
            case 'F':
                vlc_mutex_lock( &p_vout_bank->lock );
                /* XXX: only fullscreen the first video output */
                if( p_vout_bank->i_count )
                {
                    p_vout_bank->pp_vout[0]->i_changes
                                      |= VOUT_FULLSCREEN_CHANGE;
                }
                vlc_mutex_unlock( &p_vout_bank->lock );
                break;

            case 'm':
            case 'M':
#if 0
                double picratio = p_intf->p_input->p_default_vout->i_width 
                    / p_intf->p_input->p_default_vout->i_height;
                if (picratio
                p_intf->p_input->p_default_vout->i_width=800
                p_intf->p_input->p_default_vout->i_changes |= 
                    VOUT_FULLSCREEN_CHANGE;
#endif
                break;

            case 's':
            case 'S':
                ;
                break;

            case 'q':
            case 'Q':
                p_intf->b_die = 1;
                break;

            case 'r':
            case 'R':
Sam Hocevar's avatar
   
Sam Hocevar committed
279
                if( p_input_bank->pp_input[0] != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
280
281
282
283
284
285
286
287
288
289
290
                {
                    for( i_dummy = 1;
                         i_dummy < MAX_LINE_LENGTH && p_cmd[ i_dummy ] >= '0'
                                                   && p_cmd[ i_dummy ] <= '9';
                         i_dummy++ )
                    {
                        ;
                    }

                    p_cmd[ i_dummy ] = 0;
                    f_cpos = atof( p_cmd + 1 );
Sam Hocevar's avatar
   
Sam Hocevar committed
291
292
                    input_Seek( p_input_bank->pp_input[0],
                                (off_t) (f_cpos / f_ratio) );
Sam Hocevar's avatar
   
Sam Hocevar committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
                    /* rcreseek(f_cpos); */
                }
                break;

            case '?':
            case 'h':
            case 'H':
                intf_Msg( "rc: help for remote control commands" );
                intf_Msg( "rc: h                                       help" );
                intf_Msg( "rc: a XYZ                 append XYZ to playlist" );
                intf_Msg( "rc: p                               toggle pause" );
                intf_Msg( "rc: f                          toggle fullscreen" );
                intf_Msg( "rc: r X    seek in seconds, for instance `r 3.5'" );
                intf_Msg( "rc: q                                       quit" );
                intf_Msg( "rc: end of help" );
                break;

            default:
                intf_Msg( "rc: unknown command `%s'", p_cmd );
                break;
            }
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
316
317
        vlc_mutex_unlock( &p_input_bank->lock );

Sam Hocevar's avatar
   
Sam Hocevar committed
318
319
320
321
322
        p_intf->pf_manage( p_intf );
        msleep( INTF_IDLE_SLEEP );
    }
}