main.c 27 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"
45 46
#include "stream_control.h"
#include "input_ext-intf.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 245
    /* FIXME: no VLANs */
#if 0
246
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
247
    {
248 249 250 251
        /* 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
252
    }
253
#endif
254

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

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

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

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

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

    /*
     * Free shared resources and libraries
     */
296 297
    /* FIXME */
#if 0
298
    if( p_main->b_vlans )
299 300 301
    {
        input_VlanDestroy();
    }
302
#endif
Michel Kaempf's avatar
Michel Kaempf committed
303

304
    /*
305
     * Free plugin bank
306
     */
307 308 309 310 311 312 313
    bank_Destroy( p_main->p_bank );

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

314
#ifdef SYS_BEOS
315 316 317 318
    /*
     * System specific cleaning code
     */
    beos_Destroy();
319 320
#endif

Michel Kaempf's avatar
Michel Kaempf committed
321 322 323
    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
324 325
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
326

Michel Kaempf's avatar
Michel Kaempf committed
327 328 329
    return( 0 );
}

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

    psz_env = getenv( psz_name );
    if( psz_env )
343
    {
344 345 346 347
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
348
        }
349
    }
Vincent Seguin's avatar
Vincent Seguin committed
350 351 352
    return( i_default );
}

353
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
354
 * main_GetPszVariable: get the string value of an environment variable
355
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
356
 * This function is used to read some default parameters in modules.
357
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
358 359 360 361 362 363 364
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
365
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
366
    }
367
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
368 369
}

370
/*****************************************************************************
371
 * main_PutPszVariable: set the string value of an environment variable
372
 *****************************************************************************
373 374 375
 * 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.
376
 *****************************************************************************/
377 378 379 380 381 382 383
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 )
    {
384
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
385 386 387 388 389 390
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
391
            intf_ErrMsg( "error: %s\n", strerror(errno) );
392
        }
393 394 395
    }
}

396
/*****************************************************************************
397
 * main_PutIntVariable: set the integer value of an environment variable
398
 *****************************************************************************
399 400 401
 * 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.
402
 *****************************************************************************/
403 404
void main_PutIntVariable( char *psz_name, int i_value )
{
405
    char psz_value[ 256 ];                               /* buffer for value */
406

407
    sprintf( psz_value, "%d", i_value );
408
    main_PutPszVariable( psz_name, psz_value );
409 410
}

Michel Kaempf's avatar
Michel Kaempf committed
411 412
/* following functions are local */

413
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
414
 * SetDefaultConfiguration: set default options
415
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
416
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
417 418 419
 * 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.
420
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
421
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
422 423
{
    /*
424
     * All features are activated by default execpted vlans
Michel Kaempf's avatar
Michel Kaempf committed
425
     */
Vincent Seguin's avatar
Vincent Seguin committed
426 427
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
428
    p_main->b_vlans  = 0;
Michel Kaempf's avatar
Michel Kaempf committed
429 430
}

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

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
446 447
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
448
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
449
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
450

Sam Hocevar's avatar
Sam Hocevar committed
451 452
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );

453
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
Sam Hocevar committed
454 455 456 457 458 459 460 461 462 463 464 465 466
    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
467
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
468 469 470 471 472
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
473 474
        /* General/common options */
        case 'h':                                              /* -h, --help */
475 476 477
            Usage( SHORT_HELP );
            return( -1 );
            break;
478
        case 'H':                                          /* -H, --longhelp */
479
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
480 481
            return( -1 );
            break;
482
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
483 484
            Version();
            return( -1 );
485
            break;
Michel Kaempf's avatar
Michel Kaempf committed
486 487

        /* Audio options */
488
        case OPT_NOAUDIO:                                       /* --noaudio */
489
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
490
            break;
491 492 493
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
494
        case OPT_STEREO:                                         /* --stereo */
495
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
496
            break;
497
        case OPT_MONO:                                             /* --mono */
498
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
499 500 501
            break;

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

        case 'g':                                         /* -g, --grayscale */
519
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
520 521
            break;
        case OPT_COLOR:                                           /* --color */
522
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
523
            break;
Sam Hocevar's avatar
Sam Hocevar committed
524 525 526
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
527

528 529
            /* FIXME */
#if 0
530 531 532 533 534 535 536 537 538 539 540
        /* 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;
541
#endif
542 543 544 545 546 547 548
        case 'c':
            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
            break;
        case 's':
            main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
            break;

549
        /* Input options */
550
        case OPT_VLANS:                                           /* --vlans */
551
            p_main->b_vlans = 1;
552 553
            break;
        case OPT_SERVER:                                         /* --server */
554
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
555 556
            break;
        case OPT_PORT:                                             /* --port */
557
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
558
            break;
559 560 561
        case OPT_BROADCAST:                                   /* --broadcast */
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            break;
562

563
        /* Synchro options */
564
        case OPT_SYNCHRO:                                      
565 566 567
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;

568 569 570 571 572
        /* Interface warning messages level */
        case OPT_WARNING:                                       /* --warning */
            main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
            break;
            
Michel Kaempf's avatar
Michel Kaempf committed
573
        /* Internal error: unknown option */
574
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
575
        default:
576 577
            intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
578 579 580 581 582
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
583 584 585
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
586
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
587
    }
Michel Kaempf's avatar
Michel Kaempf committed
588 589 590
    return( 0 );
}

591
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
592
 * Usage: print program usage
593
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
594
 * Print a short inline help. Message interface is initialized at this stage.
595
 *****************************************************************************/
596
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
597
{
Vincent Seguin's avatar
Vincent Seguin committed
598
    /* Usage */
Sam Hocevar's avatar
Sam Hocevar committed
599 600
    intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
              p_main->psz_arg0 );
601 602 603

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
Sam Hocevar committed
604 605
        intf_Msg( "Try `%s --help' for more information.\n",
                  p_main->psz_arg0 );
606 607 608
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
609
    /* Options */
610 611 612 613 614 615
    intf_Msg( "\n"
              "Options:\n"
              "      --noaudio                  \tdisable audio\n"
              "      --aout <plugin>            \taudio output method\n"
              "      --stereo, --mono           \tstereo/mono audio\n"
              "\n"
616
              "      --novideo                  \tdisable video\n"
617 618 619 620 621 622
              "      --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"
623 624 625
              "  -a, --dvdaudio <type>          \tchoose DVD audio type\n"
              "  -c, --dvdchannel <channel>     \tchoose DVD audio channel\n"
              "  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel\n"
626
              "\n"
627
              "      --vlans                    \tenable vlans\n"
628 629
              "      --server <host>            \tvideo server address\n"
              "      --port <port>              \tvideo server port\n"
630
              "      --broadcast                \tlisten to a broadcast\n"
631
              "\n"
632 633
              "      --synchro <type>           \tforce synchro algorithm\n"
              "\n"
634 635
              "      --warning <level>          \tdisplay warning messages\n"
              "\n"
636 637 638
              "  -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
639

640 641 642
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
643
    /* Interface parameters */
644 645
    intf_Msg( "\n"
              "Interface parameters:\n"
646
              "  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script\n"
647 648
              "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n"
              "  " INTF_WARNING_VAR "=<level>                \twarning level\n" );
Vincent Seguin's avatar
Vincent Seguin committed
649 650

    /* Audio parameters */
651 652 653 654 655 656
    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
657 658

    /* Video parameters */
659 660 661 662 663 664 665 666
    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
667

668 669 670 671 672 673 674
    /* 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" );

675
    /* Input parameters */
676 677 678 679 680
    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"
681
              "  " INPUT_BROADCAST_VAR "={1|0}            \tbroadcast mode\n"
682 683
              "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
              "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
684 685 686 687

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

691
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
692
 * Version: print complete program version
693
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
694
 * Print complete program version and build number.
695
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
696 697
static void Version( void )
{
698
    intf_Msg( VERSION_MESSAGE
699 700 701 702
              "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" );
703

704
}
Vincent Seguin's avatar
Vincent Seguin committed
705

706
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
707
 * InitSignalHandler: system signal handler initialization
708
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
709
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
710
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
711
 * when we don't want to use SIGKILL.
712
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
713 714
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
715 716 717 718
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
719 720
}

721
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
722
 * SignalHandler: system signal handler
723
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
724 725
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
726
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
727 728
static void SignalHandler( int i_signal )
{
729
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
730 731 732 733 734 735 736 737 738
     * 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 );

739
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
740 741 742 743
     * interface thread */
    p_main->p_intf->b_die = 1;
}