intf_rc.c 8.78 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
2
 * intf_rc.cpp: remote control interface
Sam Hocevar's avatar
 
Sam Hocevar committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
 * $Id: intf_rc.cpp,v 0.1 2001/04/27 shurdeek
 *
 * 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.
 *****************************************************************************/

#define MODULE_NAME rc
#include "modules_inner.h"

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "tests.h"
#include "modules.h"

#include "stream_control.h"
#include "input_ext-intf.h"

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

#include "video.h"
#include "video_output.h"

#include "main.h"


/*****************************************************************************
 * intf_sys_t: description and status of rc interface
 *****************************************************************************/
typedef struct intf_sys_s
{
    vlc_mutex_t         change_lock;

} intf_sys_t;

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  intf_Probe     ( probedata_t *p_data );
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 );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
void _M( intf_getfunctions )( function_list_t * p_function_list )
{
    p_function_list->pf_probe = intf_Probe;
    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_Probe: probe the interface and return a score
 *****************************************************************************
 * This function tries to initialize rc and returns a score to the
 * plugin manager so that it can select the best plugin.
 *****************************************************************************/
static int intf_Probe( probedata_t *p_data )
{
    if( TestMethod( INTF_METHOD_VAR, "rc" ) )
    {
        return( 999 );
    }

    return( 2 );
}

/*****************************************************************************
 * 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 );
    }

    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[ 32 ];
    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 )
    {
        if( p_intf->p_input != NULL )
        {
            /* Get position */
#define S p_intf->p_input->stream
            if( S.i_mux_rate )
            {
                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 ) );
                }
            }
#undef S

            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( i_cmd_pos < 32
                       && 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 == 31 || p_cmd[ i_cmd_pos ] == '\r'
                                    || p_cmd[ i_cmd_pos ] == '\n' )
                {
                    p_cmd[ i_cmd_pos ] = 0;
                    b_complete = 1;
                }
            }

            /* Is there something to do? */
            if( b_complete == 1 )
            {
                switch( p_cmd[ 0 ] )
                {
                case 'p':
                case 'P':
                    input_SetStatus( p_intf->p_input, INPUT_STATUS_PAUSE );
                    break;

                case 'f':
                case 'F':
                    p_intf->p_input->p_default_vout->i_changes |= 
                        VOUT_FULLSCREEN_CHANGE;
                    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':
                    for( i_dummy = 1; i_dummy < 32 && p_cmd[ i_dummy ] >= '0'
                         && p_cmd[ i_dummy ] <= '9'; ++i_dummy )
                    {
                        ;
                    }

                    p_cmd[ i_dummy ] = 0;
                    f_cpos = atof( p_cmd + 1 );
                    input_Seek( p_intf->p_input, (off_t) (f_cpos / f_ratio) );
                    /* rcreseek(f_cpos); */
                    break;

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

        p_intf->pf_manage( p_intf );
        msleep( INTF_IDLE_SLEEP );
    }
}