main.c 27.8 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 31 32
#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
#include <stdio.h>                                              /* sprintf() */

Benoit Steiner's avatar
 
Benoit Steiner committed
33
#ifdef HAVE_GETOPT_H
Benoit Steiner's avatar
Benoit Steiner committed
34 35 36
#include <getopt.h>                                              /* getopt() */
#endif

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

Michel Kaempf's avatar
Michel Kaempf committed
41
#include "config.h"
Benoit Steiner's avatar
 
Benoit Steiner committed
42
#include "debug.h"
Michel Kaempf's avatar
Michel Kaempf committed
43
#include "common.h"
44
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
45
#include "mtime.h"
Sam Hocevar's avatar
Sam Hocevar committed
46
#include "tests.h"                                              /* TestMMX() */
47
#include "plugins.h"
48
#include "playlist.h"
49 50
#include "stream_control.h"
#include "input_ext-intf.h"
51

Vincent Seguin's avatar
Vincent Seguin committed
52
#include "intf_msg.h"
Michel Kaempf's avatar
Michel Kaempf committed
53
#include "interface.h"
54

Vincent Seguin's avatar
Vincent Seguin committed
55
#include "audio_output.h"
56

57 58 59 60
#ifdef SYS_BEOS
#include "beos_specific.h"
#endif

Vincent Seguin's avatar
Vincent Seguin committed
61
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
62

63
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
64 65
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
66
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
67 68 69 70

/* 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
71 72 73
#define OPT_AOUT                151
#define OPT_STEREO              152
#define OPT_MONO                153
Michel Kaempf's avatar
Michel Kaempf committed
74 75

#define OPT_NOVIDEO             160
76 77 78 79 80
#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
81
#define OPT_YUV                 166
Michel Kaempf's avatar
Michel Kaempf committed
82

83
#define OPT_VLANS               170
84 85
#define OPT_SERVER              171
#define OPT_PORT                172
86
#define OPT_BROADCAST           173
87

88 89
#define OPT_SYNCHRO             180

90 91
#define OPT_WARNING             190

92
/* Usage fashion */
93 94 95
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
96

Michel Kaempf's avatar
Michel Kaempf committed
97
/* Long options */
Benoit Steiner's avatar
 
Benoit Steiner committed
98
#ifdef HAVE_GETOPT_H
Michel Kaempf's avatar
Michel Kaempf committed
99
static const struct option longopts[] =
100 101
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
102 103

    /* General/common options */
104
    {   "help",             0,          0,      'h' },
105
    {   "longhelp",         0,          0,      'H' },
106
    {   "version",          0,          0,      'v' },
Michel Kaempf's avatar
Michel Kaempf committed
107 108

    /* Audio options */
109
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
110
    {   "aout",             1,          0,      OPT_AOUT },
Michel Kaempf's avatar
Michel Kaempf committed
111
    {   "stereo",           0,          0,      OPT_STEREO },
112
    {   "mono",             0,          0,      OPT_MONO },
Michel Kaempf's avatar
Michel Kaempf committed
113 114

    /* Video options */
115
    {   "novideo",          0,          0,      OPT_NOVIDEO },
116
    {   "vout",             1,          0,      OPT_VOUT },
117
    {   "display",          1,          0,      OPT_DISPLAY },
118
    {   "width",            1,          0,      OPT_WIDTH },
119 120 121
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
Sam Hocevar committed
122
    {   "yuv",              1,          0,      OPT_YUV },
Michel Kaempf's avatar
Michel Kaempf committed
123

124 125 126 127 128
    /* DVD options */
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
129
    /* Input options */
130
    {   "vlans",            0,          0,      OPT_VLANS },
131 132
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
133
    {   "broadcast",        0,          0,      OPT_BROADCAST },
Michel Kaempf's avatar
Michel Kaempf committed
134

135 136 137
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },

138 139
    /* Interface messages */
    {   "warning",          1,          0,      OPT_WARNING },
Michel Kaempf's avatar
Michel Kaempf committed
140 141 142 143
    {   0,                  0,          0,      0 }
};

/* Short options */
144
static const char *psz_shortopts = "hHvga:s:c:";
Benoit Steiner's avatar
Benoit Steiner committed
145 146
#endif

Michel Kaempf's avatar
Michel Kaempf committed
147

148
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
149
 * Global variable program_data - this is the one and only, see main.h
150
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
151
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
152

153
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
154
 * Local prototypes
155
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
156
static void SetDefaultConfiguration ( void );
157 158
static int  GetConfiguration        ( int i_argc, char *ppsz_argv[],
                                      char *ppsz_env[] );
159
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
160
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
161

Michel Kaempf's avatar
Michel Kaempf committed
162
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
163 164
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
165
#ifdef HAVE_MMX
166
       int  TestMMX                 ( void );
167
#endif
Michel Kaempf's avatar
Michel Kaempf committed
168

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

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

185 186 187
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
Sam Hocevar committed
188
#ifdef SYS_BEOS
189
    beos_Create();
190 191
#endif

Michel Kaempf's avatar
Michel Kaempf committed
192
    /*
193
     * Test if our code is likely to run on this CPU 
194
     */
Sam Hocevar's avatar
Sam Hocevar committed
195
#ifdef HAVE_MMX
196 197
    if( !TestMMX() )
    {
198 199
        fprintf( stderr, "Sorry, this program needs an MMX processor. "
                         "Please run the non-MMX version.\n" );
200
        return( 1 );
201 202
    }
#endif
203 204 205 206

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
207
    p_main->p_msg = intf_MsgCreate();
208
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
209
    {
210
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
211
                 strerror(errno) );
212
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
213
    }
214

Benoit Steiner's avatar
 
Benoit Steiner committed
215 216 217 218 219
    /*
     * Set signal handling policy up for all the threads that will be created
     */
    InitSignalHandler();                 /* prepare signals for interception */

220 221 222
    /*
     * Read configuration
     */
223
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
224
    {
Vincent Seguin's avatar
Vincent Seguin committed
225
        intf_MsgDestroy();
226
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
227
    }
228

229 230 231 232 233
    /*
     * Initialize playlist and get commandline files
     */
    p_main->p_playlist = playlist_Create( );
    if( !p_main->p_playlist )
234
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
235
        intf_Msg( "Playlist initialization failed" );
236 237
        intf_MsgDestroy();
        return( errno );
238
    }
239 240 241 242 243 244 245
    playlist_Init( p_main->p_playlist, optind );

    /*
     * Initialize plugin bank
     */
    p_main->p_bank = bank_Create( );
    if( !p_main->p_bank )
246
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
247
        intf_Msg( "Plugin bank initialization failed" );
248 249 250
        playlist_Destroy( p_main->p_playlist );
        intf_MsgDestroy();
        return( errno );
251
    }
252
    bank_Init( p_main->p_bank );
Michel Kaempf's avatar
Michel Kaempf committed
253 254 255 256

    /*
     * Initialize shared resources and libraries
     */
257 258
    /* FIXME: no VLANs */
#if 0
259
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
260
    {
261 262
        /* On error during vlans initialization, switch off vlans */
        intf_Msg( "Virtual LANs initialization failed : "
Sam Hocevar's avatar
 
Sam Hocevar committed
263
                  "vlans management is deactivated" );
264
        p_main->b_vlans = 0;
Michel Kaempf's avatar
Michel Kaempf committed
265
    }
266
#endif
267

Michel Kaempf's avatar
Michel Kaempf committed
268
    /*
Vincent Seguin's avatar
Vincent Seguin committed
269
     * Open audio device and start aout thread
Michel Kaempf's avatar
Michel Kaempf committed
270
     */
271
    if( p_main->b_audio )
Vincent Seguin's avatar
Vincent Seguin committed
272
    {
273 274
        p_main->p_aout = aout_CreateThread( NULL );
        if( p_main->p_aout == NULL )
275
        {
276
            /* On error during audio initialization, switch off audio */
Sam Hocevar's avatar
 
Sam Hocevar committed
277
            intf_Msg( "Audio initialization failed : audio is deactivated" );
278
            p_main->b_audio = 0;
279
        }
Michel Kaempf's avatar
Michel Kaempf committed
280
    }
281

Michel Kaempf's avatar
Michel Kaempf committed
282 283 284
    /*
     * Run interface
     */
285 286
    p_main->p_intf = intf_Create();
    if( p_main->p_intf != NULL )
Vincent Seguin's avatar
Vincent Seguin committed
287
    {
Sam Hocevar's avatar
Sam Hocevar committed
288 289 290
        /*
         * This is the main loop
         */
291 292 293
        intf_Run( p_main->p_intf );

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

Michel Kaempf's avatar
Michel Kaempf committed
296
    /*
Michel Kaempf's avatar
Michel Kaempf committed
297 298
     * Close audio device
     */
299
    if( p_main->b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
300
    {
301
        aout_DestroyThread( p_main->p_aout, NULL );
Michel Kaempf's avatar
Michel Kaempf committed
302
    }
Michel Kaempf's avatar
Michel Kaempf committed
303 304 305 306

    /*
     * Free shared resources and libraries
     */
307 308
    /* FIXME */
#if 0
309
    if( p_main->b_vlans )
310 311 312
    {
        input_VlanDestroy();
    }
313
#endif
Michel Kaempf's avatar
Michel Kaempf committed
314

315
    /*
316
     * Free plugin bank
317
     */
318 319 320 321 322 323 324
    bank_Destroy( p_main->p_bank );

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

325
#ifdef SYS_BEOS
326 327 328 329
    /*
     * System specific cleaning code
     */
    beos_Destroy();
330 331
#endif

Michel Kaempf's avatar
Michel Kaempf committed
332 333 334
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
335
    intf_Msg( "Program terminated." );
Vincent Seguin's avatar
Vincent Seguin committed
336
    intf_MsgDestroy();
337

Michel Kaempf's avatar
Michel Kaempf committed
338 339 340
    return( 0 );
}

341
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
342
 * main_GetIntVariable: get the int value of an environment variable
343
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
344
 * This function is used to read some default parameters in modules.
345
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
346 347
int main_GetIntVariable( char *psz_name, int i_default )
{
348 349 350
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
351 352 353

    psz_env = getenv( psz_name );
    if( psz_env )
354
    {
355 356 357 358
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
359
        }
360
    }
Vincent Seguin's avatar
Vincent Seguin committed
361 362 363
    return( i_default );
}

364
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
365
 * main_GetPszVariable: get the string value of an environment variable
366
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
367
 * This function is used to read some default parameters in modules.
368
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
369 370 371 372 373 374 375
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
376
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
377
    }
378
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
379 380
}

381
/*****************************************************************************
382
 * main_PutPszVariable: set the string value of an environment variable
383
 *****************************************************************************
384 385 386
 * 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.
387
 *****************************************************************************/
388 389 390 391 392 393 394
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 )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
395 396
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
397 398 399 400 401 402
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
403
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
404
        }
405 406 407
    }
}

408
/*****************************************************************************
409
 * main_PutIntVariable: set the integer value of an environment variable
410
 *****************************************************************************
411 412 413
 * 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.
414
 *****************************************************************************/
415 416
void main_PutIntVariable( char *psz_name, int i_value )
{
417
    char psz_value[ 256 ];                               /* buffer for value */
418

419
    sprintf( psz_value, "%d", i_value );
420
    main_PutPszVariable( psz_name, psz_value );
421 422
}

Michel Kaempf's avatar
Michel Kaempf committed
423 424
/* following functions are local */

425
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
426
 * SetDefaultConfiguration: set default options
427
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
428
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
429 430 431
 * 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.
432
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
433
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
434 435
{
    /*
436
     * All features are activated by default except vlans
Michel Kaempf's avatar
Michel Kaempf committed
437
     */
Vincent Seguin's avatar
Vincent Seguin committed
438 439
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
440
    p_main->b_vlans  = 0;
Michel Kaempf's avatar
Michel Kaempf committed
441 442
}

443
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
444
 * GetConfiguration: parse command line
445
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
446 447
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
448
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
449 450
 * stage, but most structures are not allocated, so only environment should
 * be used.
451
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
452
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
453
{
Vincent Seguin's avatar
Vincent Seguin committed
454
    int c, i_opt;
Sam Hocevar's avatar
Sam Hocevar committed
455
    char * p_pointer;
Michel Kaempf's avatar
Michel Kaempf committed
456 457

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
458 459
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
460
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
461
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
462

Sam Hocevar's avatar
 
Sam Hocevar committed
463
    intf_MsgImm( COPYRIGHT_MESSAGE );
Sam Hocevar's avatar
Sam Hocevar committed
464

465
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
Sam Hocevar committed
466 467 468 469 470 471 472 473 474 475 476 477 478
    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
479
    /* Parse command line options */
Benoit Steiner's avatar
 
Benoit Steiner committed
480
#ifdef HAVE_GETOPT_H
Michel Kaempf's avatar
Michel Kaempf committed
481 482 483 484 485
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
486 487
        /* General/common options */
        case 'h':                                              /* -h, --help */
488 489 490
            Usage( SHORT_HELP );
            return( -1 );
            break;
491
        case 'H':                                          /* -H, --longhelp */
492
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
493 494
            return( -1 );
            break;
495
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
496 497
            Version();
            return( -1 );
498
            break;
Michel Kaempf's avatar
Michel Kaempf committed
499 500

        /* Audio options */
501
        case OPT_NOAUDIO:                                       /* --noaudio */
502
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
503
            break;
504 505 506
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
507
        case OPT_STEREO:                                         /* --stereo */
508
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
509
            break;
510
        case OPT_MONO:                                             /* --mono */
511
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
512 513 514
            break;

        /* Video options */
515
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
516
            p_main->b_video = 0;
517
            break;
518 519 520
        case OPT_VOUT:                                             /* --vout */
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
521
        case OPT_DISPLAY:                                       /* --display */
522
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
523 524
            break;
        case OPT_WIDTH:                                           /* --width */
525
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
526 527 528 529 530 531
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;

        case 'g':                                         /* -g, --grayscale */
532
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
533 534
            break;
        case OPT_COLOR:                                           /* --color */
535
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
536
            break;
Sam Hocevar's avatar
Sam Hocevar committed
537 538 539
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
540

541 542
        /* DVD options */
        case 'a':
543 544
            if ( ! strcmp(optarg, "ac3") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
545 546 547 548 549
            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
550
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
551 552 553 554 555 556 557 558
            break;
        case 'c':
            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
            break;
        case 's':
            main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
            break;

559
        /* Input options */
560
        case OPT_VLANS:                                           /* --vlans */
561
            p_main->b_vlans = 1;
562 563
            break;
        case OPT_SERVER:                                         /* --server */
564
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
565 566
            break;
        case OPT_PORT:                                             /* --port */
567
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
568
            break;
569 570 571
        case OPT_BROADCAST:                                   /* --broadcast */
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            break;
572

573
        /* Synchro options */
574
        case OPT_SYNCHRO:                                      
575 576 577
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;

578 579 580 581 582
        /* Interface warning messages level */
        case OPT_WARNING:                                       /* --warning */
            main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
            break;
            
Michel Kaempf's avatar
Michel Kaempf committed
583
        /* Internal error: unknown option */
584
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
585
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
586
            intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
587
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
588 589 590 591
            return( EINVAL );
            break;
        }
    }
Benoit Steiner's avatar
Benoit Steiner committed
592
#endif
Michel Kaempf's avatar
Michel Kaempf committed
593

Vincent Seguin's avatar
Vincent Seguin committed
594 595 596
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
597
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
598
    }
Michel Kaempf's avatar
Michel Kaempf committed
599 600 601
    return( 0 );
}

602
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
603
 * Usage: print program usage
604
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
605
 * Print a short inline help. Message interface is initialized at this stage.
606
 *****************************************************************************/
607
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
608
{
Vincent Seguin's avatar
Vincent Seguin committed
609
    /* Usage */
Sam Hocevar's avatar
 
Sam Hocevar committed
610
    intf_Msg( "Usage: %s [options] [parameters] [file]...",
Sam Hocevar's avatar
Sam Hocevar committed
611
              p_main->psz_arg0 );
612 613 614

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
615
        intf_Msg( "Try `%s --help' for more information.",
Sam Hocevar's avatar
Sam Hocevar committed
616
                  p_main->psz_arg0 );
617 618 619
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
620
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
621 622 623 624
    intf_Msg( "\nOptions:"
              "\n      --noaudio                  \tdisable audio"
              "\n      --aout <plugin>            \taudio output method"
              "\n      --stereo, --mono           \tstereo/mono audio"
625
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
626 627 628 629 630 631
              "\n      --novideo                  \tdisable video"
              "\n      --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"
632
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
633 634 635
              "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
              "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
              "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
636
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
637 638 639 640
              "\n      --vlans                    \tenable vlans"
              "\n      --server <host>            \tvideo server address"
              "\n      --port <port>              \tvideo server port"
              "\n      --broadcast                \tlisten to a broadcast"
641
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
642
              "\n      --synchro <type>           \tforce synchro algorithm"
643
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
644
              "\n      --warning <level>          \tdisplay warning messages"
645
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
646 647 648
              "\n  -h, --help                     \tprint help and exit"
              "\n  -H, --longhelp                 \tprint long help and exit"
              "\n  -v, --version                  \toutput version information and exit" );
Michel Kaempf's avatar
Michel Kaempf committed
649

650 651 652
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
653
    /* Interface parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
654 655 656 657
    intf_Msg( "\nInterface parameters:\n"
              "\n  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script"
              "\n  " INTF_CHANNELS_VAR "=<filename>            \tchannels list"
              "\n  " INTF_WARNING_VAR "=<level>                \twarning level" );
Vincent Seguin's avatar
Vincent Seguin committed
658 659

    /* Audio parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
660 661 662 663 664
    intf_Msg( "\nAudio 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" );
Vincent Seguin's avatar
Vincent Seguin committed
665 666

    /* Video parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
667 668 669 670 671 672 673
    intf_Msg( "\nVideo 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" );
Vincent Seguin's avatar
Vincent Seguin committed
674

675
    /* DVD parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
676 677 678 679
    intf_Msg( "\nDVD 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" );
680

681
    /* Input parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
682 683 684 685 686 687 688
    intf_Msg( "\nInput parameters:\n"
              "\n  " INPUT_SERVER_VAR "=<hostname>          \tvideo server"
              "\n  " INPUT_PORT_VAR "=<port>            \tvideo server port"
              "\n  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface"
              "\n  " INPUT_BROADCAST_VAR "={1|0}            \tbroadcast mode"
              "\n  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server"
              "\n  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port" );
689 690

    /* Synchro parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
691 692
    intf_Msg( "\nSynchro parameters:"
              "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm");
Michel Kaempf's avatar
Michel Kaempf committed
693 694
}

695
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
696
 * Version: print complete program version
697
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
698
 * Print complete program version and build number.
699
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
700 701
static void Version( void )
{
702
    intf_Msg( VERSION_MESSAGE
703 704 705
              "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"
Sam Hocevar's avatar
 
Sam Hocevar committed
706
              "Written by the VideoLAN team at Ecole Centrale, Paris." );
707

708
}
Vincent Seguin's avatar
Vincent Seguin committed
709

710
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
711
 * InitSignalHandler: system signal handler initialization
712
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
713
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
714
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
715
 * when we don't want to use SIGKILL.
716
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
717 718
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
719
    /* Termination signals */
Benoit Steiner's avatar
 
Benoit Steiner committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    signal( SIGHUP,  FatalSignalHandler );
    signal( SIGINT,  FatalSignalHandler );
    signal( SIGQUIT, FatalSignalHandler );

    /* Other signals */
    signal( SIGALRM, SimpleSignalHandler );
    signal( SIGPIPE, SimpleSignalHandler );
}


/*****************************************************************************
 * SimpleSignalHandler: system signal handler
 *****************************************************************************
 * This function is called when a non fatal signal is received by the program.
 *****************************************************************************/
static void SimpleSignalHandler( int i_signal )
{
    /* Acknowledge the signal received */
Sam Hocevar's avatar
 
Sam Hocevar committed
738
    intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
739 740
}

Benoit Steiner's avatar
 
Benoit Steiner committed
741

742
/*****************************************************************************
Benoit Steiner's avatar
 
Benoit Steiner committed
743
 * FatalSignalHandler: system signal handler
744
 *****************************************************************************
Benoit Steiner's avatar
 
Benoit Steiner committed
745 746
 * This function is called when a fatal signal is received by the program.
 * It tries to end the program in a clean way.
747
 *****************************************************************************/
Benoit Steiner's avatar
 
Benoit Steiner committed
748
static void FatalSignalHandler( int i_signal )
Michel Kaempf's avatar
Michel Kaempf committed
749
{
750
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
751 752 753 754 755 756 757
     * 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 */
Sam Hocevar's avatar
 
Sam Hocevar committed
758
    intf_ErrMsgImm("intf error: signal %d received, exiting", i_signal );
Vincent Seguin's avatar
Vincent Seguin committed
759

760
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
761 762 763 764
     * interface thread */
    p_main->p_intf->b_die = 1;
}