main.c 26.9 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3 4
 * main.c: main vlc source
 * Includes the main() function for vlc. Parses command line, start interface
 * and spawn threads.
5 6 7 8 9 10 11 12 13
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * 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.
14
 * 
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20 21 22
 * 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.
23
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
24

25
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
26
 * Preamble
27
 *****************************************************************************/
28 29
#include "defs.h"

30
#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
31
#include <getopt.h>                                              /* getopt() */
32 33 34 35 36
#include <stdio.h>                                              /* sprintf() */

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                  /* getenv(), strtol(),  */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
37

Michel Kaempf's avatar
Michel Kaempf committed
38 39
#include "config.h"
#include "common.h"
40
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
41
#include "mtime.h"
Sam Hocevar's avatar
Sam Hocevar committed
42
#include "tests.h"                                              /* TestMMX() */
43
#include "plugins.h"
44
#include "playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
45
#include "input_vlan.h"
Sam Hocevar's avatar
Sam Hocevar committed
46
#include "input_ps.h"
47

Vincent Seguin's avatar
Vincent Seguin committed
48
#include "intf_msg.h"
Michel Kaempf's avatar
Michel Kaempf committed
49
#include "interface.h"
50

Vincent Seguin's avatar
Vincent Seguin committed
51
#include "audio_output.h"
52

53 54 55 56
#ifdef SYS_BEOS
#include "beos_specific.h"
#endif

Vincent Seguin's avatar
Vincent Seguin committed
57
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
58

59
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
60 61
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
62
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
63 64 65 66

/* Long options return values - note that values corresponding to short options
 * chars, and in general any regular char, should be avoided */
#define OPT_NOAUDIO             150
67 68 69
#define OPT_AOUT                151
#define OPT_STEREO              152
#define OPT_MONO                153
Michel Kaempf's avatar
Michel Kaempf committed
70 71

#define OPT_NOVIDEO             160
72 73 74 75 76
#define OPT_VOUT                161
#define OPT_DISPLAY             162
#define OPT_WIDTH               163
#define OPT_HEIGHT              164
#define OPT_COLOR               165
Sam Hocevar's avatar
Sam Hocevar committed
77
#define OPT_YUV                 166
Michel Kaempf's avatar
Michel Kaempf committed
78

79
#define OPT_VLANS               170
80 81
#define OPT_SERVER              171
#define OPT_PORT                172
82
#define OPT_BROADCAST           173
83

84 85
#define OPT_SYNCHRO             180

86 87
#define OPT_WARNING             190

88
/* Usage fashion */
89 90 91
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
92

Michel Kaempf's avatar
Michel Kaempf committed
93 94
/* Long options */
static const struct option longopts[] =
95 96
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
97 98

    /* General/common options */
99
    {   "help",             0,          0,      'h' },
100
    {   "longhelp",         0,          0,      'H' },
101
    {   "version",          0,          0,      'v' },
Michel Kaempf's avatar
Michel Kaempf committed
102 103

    /* Audio options */
104
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
105
    {   "aout",             1,          0,      OPT_AOUT },
Michel Kaempf's avatar
Michel Kaempf committed
106
    {   "stereo",           0,          0,      OPT_STEREO },
107
    {   "mono",             0,          0,      OPT_MONO },
Michel Kaempf's avatar
Michel Kaempf committed
108 109

    /* Video options */
110
    {   "novideo",          0,          0,      OPT_NOVIDEO },
111
    {   "vout",             1,          0,      OPT_VOUT },
112
    {   "display",          1,          0,      OPT_DISPLAY },
113
    {   "width",            1,          0,      OPT_WIDTH },
114 115 116
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
Sam Hocevar committed
117
    {   "yuv",              1,          0,      OPT_YUV },
Michel Kaempf's avatar
Michel Kaempf committed
118

119 120 121 122 123
    /* DVD options */
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
124
    /* Input options */
125
    {   "vlans",            0,          0,      OPT_VLANS },
126 127
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
128
    {   "broadcast",        0,          0,      OPT_BROADCAST },
Michel Kaempf's avatar
Michel Kaempf committed
129

130 131 132
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },

133 134
    /* Interface messages */
    {   "warning",          1,          0,      OPT_WARNING },
Michel Kaempf's avatar
Michel Kaempf committed
135 136 137 138
    {   0,                  0,          0,      0 }
};

/* Short options */
139
static const char *psz_shortopts = "hHvga:s:c:";
Michel Kaempf's avatar
Michel Kaempf committed
140

141
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
142
 * Global variable program_data - this is the one and only, see main.h
143
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
144
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
145

146
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
147
 * Local prototypes
148
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
149
static void SetDefaultConfiguration ( void );
150 151
static int  GetConfiguration        ( int i_argc, char *ppsz_argv[],
                                      char *ppsz_env[] );
152
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
153
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
154

Michel Kaempf's avatar
Michel Kaempf committed
155 156
static void InitSignalHandler       ( void );
static void SignalHandler           ( int i_signal );
157
#ifdef HAVE_MMX
158
       int  TestMMX                 ( void );
159
#endif
Michel Kaempf's avatar
Michel Kaempf committed
160

161
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
162
 * main: parse command line, start interface and spawn threads
163
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
164 165
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
166
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
167
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
168
 *      -closing of audio output device and some global modules
169
 * On error, the spawned threads are canceled, and the open devices closed.
170
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
171 172
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
173
    main_t  main_data;                      /* root of all data - see main.h */
174

175
    p_main = &main_data;                       /* set up the global variable */
Vincent Seguin's avatar
Vincent Seguin committed
176

177 178 179
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
Sam Hocevar committed
180
#ifdef SYS_BEOS
181
    beos_Create();
182 183
#endif

Michel Kaempf's avatar
Michel Kaempf committed
184
    /*
185
     * Test if our code is likely to run on this CPU 
186
     */
Sam Hocevar's avatar
Sam Hocevar committed
187
#ifdef HAVE_MMX
188 189
    if( !TestMMX() )
    {
190 191
        fprintf( stderr, "Sorry, this program needs an MMX processor. "
                         "Please run the non-MMX version.\n" );
192
        return( 1 );
193 194
    }
#endif
195 196 197 198

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
199
    p_main->p_msg = intf_MsgCreate();
200
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
201
    {
202
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
203 204
                strerror(errno) );
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
205
    }
206 207 208 209

    /*
     * Read configuration
     */
210
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
211
    {
Vincent Seguin's avatar
Vincent Seguin committed
212
        intf_MsgDestroy();
213
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
214
    }
215

216 217 218 219 220
    /*
     * Initialize playlist and get commandline files
     */
    p_main->p_playlist = playlist_Create( );
    if( !p_main->p_playlist )
221
    {
222 223 224
        intf_Msg( "Playlist initialization failed\n" );
        intf_MsgDestroy();
        return( errno );
225
    }
226 227 228 229 230 231 232
    playlist_Init( p_main->p_playlist, optind );

    /*
     * Initialize plugin bank
     */
    p_main->p_bank = bank_Create( );
    if( !p_main->p_bank )
233
    {
234 235 236 237
        intf_Msg( "Plugin bank initialization failed\n" );
        playlist_Destroy( p_main->p_playlist );
        intf_MsgDestroy();
        return( errno );
238
    }
239
    bank_Init( p_main->p_bank );
Michel Kaempf's avatar
Michel Kaempf committed
240 241 242 243

    /*
     * Initialize shared resources and libraries
     */
244
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
245
    {
246 247 248 249
        /* On error during vlans initialization, switch off vlans */
        intf_Msg( "Virtual LANs initialization failed : "
                  "vlans management is deactivated\n" );
        p_main->b_vlans = 0;
Michel Kaempf's avatar
Michel Kaempf committed
250
    }
251

Michel Kaempf's avatar
Michel Kaempf committed
252
    /*
Vincent Seguin's avatar
Vincent Seguin committed
253
     * Open audio device and start aout thread
Michel Kaempf's avatar
Michel Kaempf committed
254
     */
255
    if( p_main->b_audio )
Vincent Seguin's avatar
Vincent Seguin committed
256
    {
257 258
        p_main->p_aout = aout_CreateThread( NULL );
        if( p_main->p_aout == NULL )
259
        {
260
            /* On error during audio initialization, switch off audio */
261
            intf_Msg( "Audio initialization failed : audio is deactivated\n" );
262
            p_main->b_audio = 0;
263
        }
Michel Kaempf's avatar
Michel Kaempf committed
264
    }
265

Michel Kaempf's avatar
Michel Kaempf committed
266 267 268
    /*
     * Run interface
     */
269 270
    p_main->p_intf = intf_Create();
    if( p_main->p_intf != NULL )
Vincent Seguin's avatar
Vincent Seguin committed
271
    {
272
        InitSignalHandler();             /* prepare signals for interception */
273

Sam Hocevar's avatar
Sam Hocevar committed
274 275 276
        /*
         * This is the main loop
         */
277 278 279
        intf_Run( p_main->p_intf );

        intf_Destroy( p_main->p_intf );
Vincent Seguin's avatar
Vincent Seguin committed
280
    }
Michel Kaempf's avatar
Michel Kaempf committed
281

Michel Kaempf's avatar
Michel Kaempf committed
282
    /*
Michel Kaempf's avatar
Michel Kaempf committed
283 284
     * Close audio device
     */
285
    if( p_main->b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
286
    {
287
        aout_DestroyThread( p_main->p_aout, NULL );
Michel Kaempf's avatar
Michel Kaempf committed
288
    }
Michel Kaempf's avatar
Michel Kaempf committed
289 290 291 292

    /*
     * Free shared resources and libraries
     */
293
    if( p_main->b_vlans )
294 295 296
    {
        input_VlanDestroy();
    }
Michel Kaempf's avatar
Michel Kaempf committed
297

298
    /*
299
     * Free plugin bank
300
     */
301 302 303 304 305 306 307
    bank_Destroy( p_main->p_bank );

    /*
     * Free playlist
     */
    playlist_Destroy( p_main->p_playlist );

308
#ifdef SYS_BEOS
309 310 311 312
    /*
     * System specific cleaning code
     */
    beos_Destroy();
313 314
#endif

Michel Kaempf's avatar
Michel Kaempf committed
315 316 317
    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
318 319
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
320

Michel Kaempf's avatar
Michel Kaempf committed
321 322 323
    return( 0 );
}

324
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
325
 * main_GetIntVariable: get the int value of an environment variable
326
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
327
 * This function is used to read some default parameters in modules.
328
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
329 330
int main_GetIntVariable( char *psz_name, int i_default )
{
331 332 333
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
334 335 336

    psz_env = getenv( psz_name );
    if( psz_env )
337
    {
338 339 340 341
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
342
        }
343
    }
Vincent Seguin's avatar
Vincent Seguin committed
344 345 346
    return( i_default );
}

347
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
348
 * main_GetPszVariable: get the string value of an environment variable
349
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
350
 * This function is used to read some default parameters in modules.
351
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
352 353 354 355 356 357 358
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
359
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
360
    }
361
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
362 363
}

364
/*****************************************************************************
365
 * main_PutPszVariable: set the string value of an environment variable
366
 *****************************************************************************
367 368 369
 * This function is used to set some default parameters in modules. The use of
 * this function will cause some memory leak: since some systems use the pointer
 * passed to putenv to store the environment string, it can't be freed.
370
 *****************************************************************************/
371 372 373 374 375 376 377
void main_PutPszVariable( char *psz_name, char *psz_value )
{
    char *psz_env;

    psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
    if( psz_env == NULL )
    {
378
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
379 380 381 382 383 384
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
385
            intf_ErrMsg( "error: %s\n", strerror(errno) );
386
        }
387 388 389
    }
}

390
/*****************************************************************************
391
 * main_PutIntVariable: set the integer value of an environment variable
392
 *****************************************************************************
393 394 395
 * This function is used to set some default parameters in modules. The use of
 * this function will cause some memory leak: since some systems use the pointer
 * passed to putenv to store the environment string, it can't be freed.
396
 *****************************************************************************/
397 398
void main_PutIntVariable( char *psz_name, int i_value )
{
399
    char psz_value[ 256 ];                               /* buffer for value */
400

401
    sprintf( psz_value, "%d", i_value );
402
    main_PutPszVariable( psz_name, psz_value );
403 404
}

Michel Kaempf's avatar
Michel Kaempf committed
405 406
/* following functions are local */

407
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
408
 * SetDefaultConfiguration: set default options
409
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
410
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
411 412 413
 * It sets all the default values required later by the program. At this stage,
 * most structure are not yet allocated, so initialization must be done using
 * environment.
414
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
415
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
416 417
{
    /*
418
     * All features are activated by default execpted vlans
Michel Kaempf's avatar
Michel Kaempf committed
419
     */
Vincent Seguin's avatar
Vincent Seguin committed
420 421
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
422
    p_main->b_vlans  = 0;
Michel Kaempf's avatar
Michel Kaempf committed
423 424
}

425
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
426
 * GetConfiguration: parse command line
427
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
428 429
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
430
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
431 432
 * stage, but most structures are not allocated, so only environment should
 * be used.
433
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
434
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
435
{
Vincent Seguin's avatar
Vincent Seguin committed
436
    int c, i_opt;
Sam Hocevar's avatar
Sam Hocevar committed
437
    char * p_pointer;
Michel Kaempf's avatar
Michel Kaempf committed
438 439

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
440 441
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
442
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
443
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
444

Sam Hocevar's avatar
Sam Hocevar committed
445 446
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );

447
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
Sam Hocevar committed
448 449 450 451 452 453 454 455 456 457 458 459 460
    p_main->psz_arg0 = p_pointer = ppsz_argv[ 0 ];
    while( *p_pointer )
    {
        if( *p_pointer == '/' )
        {
            p_main->psz_arg0 = ++p_pointer;
        }
        else
        {
            ++p_pointer;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
461
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
462 463 464 465 466
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
467 468
        /* General/common options */
        case 'h':                                              /* -h, --help */
469 470 471
            Usage( SHORT_HELP );
            return( -1 );
            break;
472
        case 'H':                                          /* -H, --longhelp */
473
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
474 475
            return( -1 );
            break;
476
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
477 478
            Version();
            return( -1 );
479
            break;
Michel Kaempf's avatar
Michel Kaempf committed
480 481

        /* Audio options */
482
        case OPT_NOAUDIO:                                       /* --noaudio */
483
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
484
            break;
485 486 487
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
488
        case OPT_STEREO:                                         /* --stereo */
489
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
490
            break;
491
        case OPT_MONO:                                             /* --mono */
492
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
493 494 495
            break;

        /* Video options */
496
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
497
            p_main->b_video = 0;
498
            break;
499 500 501
        case OPT_VOUT:                                             /* --vout */
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
502
        case OPT_DISPLAY:                                       /* --display */
503
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
504 505
            break;
        case OPT_WIDTH:                                           /* --width */
506
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
507 508 509 510 511 512
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;

        case 'g':                                         /* -g, --grayscale */
513
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
514 515
            break;
        case OPT_COLOR:                                           /* --color */
516
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
517
            break;
Sam Hocevar's avatar
Sam Hocevar committed
518 519 520
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        /* DVD options */
        case 'a':
            if ( ! strcmp(optarg, "mpeg") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
            else if ( ! strcmp(optarg, "lpcm") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
            else if ( ! strcmp(optarg, "off") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
            else
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
            break;
        case 'c':
            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
            break;
        case 's':
            main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
            break;

540
        /* Input options */
541
        case OPT_VLANS:                                           /* --vlans */
542
            p_main->b_vlans = 1;
543 544
            break;
        case OPT_SERVER:                                         /* --server */
545
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
546 547
            break;
        case OPT_PORT:                                             /* --port */
548
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
549
            break;
550 551 552
        case OPT_BROADCAST:                                   /* --broadcast */
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            break;
553

554
        /* Synchro options */
555
        case OPT_SYNCHRO:                                      
556 557 558
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;

559 560 561 562 563
        /* Interface warning messages level */
        case OPT_WARNING:                                       /* --warning */
            main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
            break;
            
Michel Kaempf's avatar
Michel Kaempf committed
564
        /* Internal error: unknown option */
565
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
566
        default:
567 568
            intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
569 570 571 572 573
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
574 575 576
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
577
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
578
    }
Michel Kaempf's avatar
Michel Kaempf committed
579 580 581
    return( 0 );
}

582
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
583
 * Usage: print program usage
584
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
585
 * Print a short inline help. Message interface is initialized at this stage.
586
 *****************************************************************************/
587
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
588
{
Vincent Seguin's avatar
Vincent Seguin committed
589
    /* Usage */
Sam Hocevar's avatar
Sam Hocevar committed
590 591
    intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
              p_main->psz_arg0 );
592 593 594

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
Sam Hocevar committed
595 596
        intf_Msg( "Try `%s --help' for more information.\n",
                  p_main->psz_arg0 );
597 598 599
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
600
    /* Options */
601 602 603 604 605 606
    intf_Msg( "\n"
              "Options:\n"
              "      --noaudio                  \tdisable audio\n"
              "      --aout <plugin>            \taudio output method\n"
              "      --stereo, --mono           \tstereo/mono audio\n"
              "\n"
607
              "      --novideo                  \tdisable video\n"
608 609 610 611 612 613
              "      --vout <plugin>            \tvideo output method\n"
              "      --display <display>        \tdisplay string\n"
              "      --width <w>, --height <h>  \tdisplay dimensions\n"
              "  -g, --grayscale                \tgrayscale output\n"
              "      --color                    \tcolor output\n"
              "\n"
614 615 616
              "  -a, --dvdaudio <type>          \tchoose DVD audio type\n"
              "  -c, --dvdchannel <channel>     \tchoose DVD audio channel\n"
              "  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel\n"
617
              "\n"
618
              "      --vlans                    \tenable vlans\n"
619 620
              "      --server <host>            \tvideo server address\n"
              "      --port <port>              \tvideo server port\n"
621
              "      --broadcast                \tlisten to a broadcast\n"
622
              "\n"
623 624
              "      --synchro <type>           \tforce synchro algorithm\n"
              "\n"
625 626
              "      --warning <level>          \tdisplay warning messages\n"
              "\n"
627 628 629
              "  -h, --help                     \tprint help and exit\n"
              "  -H, --longhelp                 \tprint long help and exit\n"
              "  -v, --version                  \toutput version information and exit\n" );
Michel Kaempf's avatar
Michel Kaempf committed
630

631 632 633
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
634
    /* Interface parameters */
635 636
    intf_Msg( "\n"
              "Interface parameters:\n"
637
              "  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script\n"
638 639
              "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n"
              "  " INTF_WARNING_VAR "=<level>                \twarning level\n" );
Vincent Seguin's avatar
Vincent Seguin committed
640 641

    /* Audio parameters */
642 643 644 645 646 647
    intf_Msg( "\n"
              "Audio parameters:\n"
              "  " AOUT_METHOD_VAR "=<method name>        \taudio method\n"
              "  " AOUT_DSP_VAR "=<filename>              \tdsp device path\n"
              "  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output\n"
              "  " AOUT_RATE_VAR "=<rate>             \toutput rate\n" );
Vincent Seguin's avatar
Vincent Seguin committed
648 649

    /* Video parameters */
650 651 652 653 654 655 656 657
    intf_Msg( "\n"
              "Video parameters:\n"
              "  " VOUT_METHOD_VAR "=<method name>        \tdisplay method\n"
              "  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used\n"
              "  " VOUT_WIDTH_VAR "=<width>               \tdisplay width\n"
              "  " VOUT_HEIGHT_VAR "=<height>             \tdislay height\n"
              "  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path\n"
              "  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output\n" );
Vincent Seguin's avatar
Vincent Seguin committed
658

659 660 661 662 663 664 665
    /* DVD parameters */
    intf_Msg( "\n"
              "DVD parameters:\n"
              "  " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type\n"
              "  " INPUT_DVD_CHANNEL_VAR "=[0-15]            \taudio channel\n"
              "  " INPUT_DVD_SUBTITLE_VAR "=[0-31]           \tsubtitle channel\n" );

666
    /* Input parameters */
667 668 669 670 671
    intf_Msg( "\n"
              "Input parameters:\n"
              "  " INPUT_SERVER_VAR "=<hostname>          \tvideo server\n"
              "  " INPUT_PORT_VAR "=<port>            \tvideo server port\n"
              "  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface\n"
672
              "  " INPUT_BROADCAST_VAR "={1|0}            \tbroadcast mode\n"
673 674
              "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
              "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
675 676 677 678

    /* Synchro parameters */
    intf_Msg( "\n"
              "Synchro parameters:\n"
679
              "  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm\n");
Michel Kaempf's avatar
Michel Kaempf committed
680 681
}

682
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
683
 * Version: print complete program version
684
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
685
 * Print complete program version and build number.
686
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
687 688
static void Version( void )
{
689
    intf_Msg( VERSION_MESSAGE
690 691 692 693
              "This program comes with NO WARRANTY, to the extent permitted by law.\n"
              "You may redistribute it under the terms of the GNU General Public License;\n"
              "see the file named COPYING for details.\n"
              "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
694

695
}
Vincent Seguin's avatar
Vincent Seguin committed
696

697
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
698
 * InitSignalHandler: system signal handler initialization
699
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
700
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
701
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
702
 * when we don't want to use SIGKILL.
703
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
704 705
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
706 707 708 709
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
710 711
}

712
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
713
 * SignalHandler: system signal handler
714
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
715 716
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
717
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
718 719
static void SignalHandler( int i_signal )
{
720
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
721 722 723 724 725 726 727 728 729
     * following signals will be ignored to avoid sending messages to an interface
     * having been destroyed */
    signal( SIGHUP,  SIG_IGN );
    signal( SIGINT,  SIG_IGN );
    signal( SIGQUIT, SIG_IGN );

    /* Acknowledge the signal received */
    intf_ErrMsgImm("intf: signal %d received\n", i_signal );

730
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
731 732 733 734
     * interface thread */
    p_main->p_intf->b_die = 1;
}