main.c 30.5 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 42
#include "config.h"
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
43
#include "debug.h"
44
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
45
#include "mtime.h"
46
#include "tests.h"                                              /* TestCPU() */
47
#include "plugins.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
48
#include "modules.h"
49
#include "playlist.h"
50 51
#include "stream_control.h"
#include "input_ext-intf.h"
52

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

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

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

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

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

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

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
76 77 78 79 80 81
#define OPT_DISPLAY             161
#define OPT_WIDTH               162
#define OPT_HEIGHT              163
#define OPT_COLOR               164
#define OPT_FULLSCREEN          165
#define OPT_OVERLAY             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
Stéphane Borel's avatar
Stéphane Borel committed
87
#define OPT_DVD                 174
88

Sam Hocevar's avatar
 
Sam Hocevar committed
89 90 91 92 93
#define OPT_AOUT                180
#define OPT_VOUT                181
#define OPT_MOTION              182
#define OPT_IDCT                183
#define OPT_YUV                 184
94

Sam Hocevar's avatar
 
Sam Hocevar committed
95 96
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
97

98
/* Usage fashion */
99 100 101
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
102

Michel Kaempf's avatar
Michel Kaempf committed
103
/* Long options */
Benoit Steiner's avatar
 
Benoit Steiner committed
104
#ifdef HAVE_GETOPT_H
Michel Kaempf's avatar
Michel Kaempf committed
105
static const struct option longopts[] =
106 107
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
108 109

    /* General/common options */
110
    {   "help",             0,          0,      'h' },
111
    {   "longhelp",         0,          0,      'H' },
112
    {   "version",          0,          0,      'v' },
Michel Kaempf's avatar
Michel Kaempf committed
113 114

    /* Audio options */
115
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
116
    {   "aout",             1,          0,      OPT_AOUT },
Michel Kaempf's avatar
Michel Kaempf committed
117
    {   "stereo",           0,          0,      OPT_STEREO },
118
    {   "mono",             0,          0,      OPT_MONO },
Michel Kaempf's avatar
Michel Kaempf committed
119 120

    /* Video options */
121
    {   "novideo",          0,          0,      OPT_NOVIDEO },
122
    {   "vout",             1,          0,      OPT_VOUT },
123
    {   "display",          1,          0,      OPT_DISPLAY },
124
    {   "width",            1,          0,      OPT_WIDTH },
125 126 127
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
128
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
129
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
130
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
131 132
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Michel Kaempf's avatar
Michel Kaempf committed
133

134 135 136 137 138
    /* DVD options */
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
139
    /* Input options */
140
    {   "vlans",            0,          0,      OPT_VLANS },
141 142
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
143
    {   "broadcast",        0,          0,      OPT_BROADCAST },
Stéphane Borel's avatar
Stéphane Borel committed
144
    {   "dvd",              0,          0,      OPT_DVD },
Michel Kaempf's avatar
Michel Kaempf committed
145

146 147 148
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },

149 150
    /* Interface messages */
    {   "warning",          1,          0,      OPT_WARNING },
Michel Kaempf's avatar
Michel Kaempf committed
151 152 153 154
    {   0,                  0,          0,      0 }
};

/* Short options */
155
static const char *psz_shortopts = "hHvga:s:c:";
Benoit Steiner's avatar
Benoit Steiner committed
156 157
#endif

Michel Kaempf's avatar
Michel Kaempf committed
158

159
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
160
 * Global variable program_data - this is the one and only, see main.h
161
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
162
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
163

164
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
165
 * Local prototypes
166
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
167
static void SetDefaultConfiguration ( void );
168 169
static int  GetConfiguration        ( int i_argc, char *ppsz_argv[],
                                      char *ppsz_env[] );
170
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
171
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
172

Michel Kaempf's avatar
Michel Kaempf committed
173
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
174 175
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
176

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

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

193 194 195
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
Sam Hocevar committed
196
#ifdef SYS_BEOS
197
    beos_Create();
198 199
#endif

200 201
    p_main->i_cpu_capabilities = CPUCapabilities();

Michel Kaempf's avatar
Michel Kaempf committed
202
    /*
203
     * Test if our code is likely to run on this CPU 
204
     */
205 206 207 208 209 210 211 212 213
#if defined( __pentium__ ) || defined( __pentiumpro__ )
    if( ! TestCPU( CPU_CAPABILITY_586 ) )
    {
        fprintf( stderr, "Sorry, this program needs a Pentium CPU.\n"
                         "Please try a version without Pentium support.\n" );
        return( 1 );
    }
#endif

Sam Hocevar's avatar
Sam Hocevar committed
214
#ifdef HAVE_MMX
215
    if( ! TestCPU( CPU_CAPABILITY_MMX ) )
216
    {
217 218
        fprintf( stderr, "Sorry, this program needs MMX extensions.\n"
                         "Please try a version without MMX support.\n" );
219
        return( 1 );
220 221
    }
#endif
222 223 224 225

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
226
    p_main->p_msg = intf_MsgCreate();
227
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
228
    {
229
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
230
                 strerror(errno) );
231
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
232
    }
233 234 235 236

    /*
     * Read configuration
     */
237
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
238
    {
Vincent Seguin's avatar
Vincent Seguin committed
239
        intf_MsgDestroy();
240
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
241
    }
242

243 244 245 246 247
    /*
     * Initialize playlist and get commandline files
     */
    p_main->p_playlist = playlist_Create( );
    if( !p_main->p_playlist )
248
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
249
        intf_ErrMsg( "playlist error: playlist initialization failed" );
250 251
        intf_MsgDestroy();
        return( errno );
252
    }
253 254 255 256 257 258 259
    playlist_Init( p_main->p_playlist, optind );

    /*
     * Initialize plugin bank
     */
    p_main->p_bank = bank_Create( );
    if( !p_main->p_bank )
260
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
261
        intf_ErrMsg( "plugin error: plugin bank initialization failed" );
262 263 264
        playlist_Destroy( p_main->p_playlist );
        intf_MsgDestroy();
        return( errno );
265
    }
266
    bank_Init( p_main->p_bank );
Michel Kaempf's avatar
Michel Kaempf committed
267

Sam Hocevar's avatar
 
Sam Hocevar committed
268 269 270 271 272 273
    /*
     * Initialize module bank
     */
    p_main->p_module_bank = module_CreateBank( );
    if( !p_main->p_module_bank )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
274
        intf_ErrMsg( "module error: module bank initialization failed" );
Sam Hocevar's avatar
 
Sam Hocevar committed
275 276 277 278 279 280 281
        bank_Destroy( p_main->p_bank );
        playlist_Destroy( p_main->p_playlist );
        intf_MsgDestroy();
        return( errno );
    }
    module_InitBank( p_main->p_module_bank );

Michel Kaempf's avatar
Michel Kaempf committed
282 283 284
    /*
     * Initialize shared resources and libraries
     */
285 286
    /* FIXME: no VLANs */
#if 0
287
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
288
    {
289 290
        /* On error during vlans initialization, switch off vlans */
        intf_Msg( "Virtual LANs initialization failed : "
Sam Hocevar's avatar
 
Sam Hocevar committed
291
                  "vlans management is deactivated" );
292
        p_main->b_vlans = 0;
Michel Kaempf's avatar
Michel Kaempf committed
293
    }
294
#endif
295

Michel Kaempf's avatar
Michel Kaempf committed
296 297 298
    /*
     * Run interface
     */
299
    p_main->p_intf = intf_Create();
300

301
    if( p_main->p_intf != NULL )
Vincent Seguin's avatar
Vincent Seguin committed
302
    {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        /*
         * Set signal handling policy for all threads
         */
        InitSignalHandler();

        /*
         * Open audio device and start aout thread
         */
        if( p_main->b_audio )
        {
            p_main->p_aout = aout_CreateThread( NULL );
            if( p_main->p_aout == NULL )
            {
                /* On error during audio initialization, switch off audio */
                intf_ErrMsg( "aout error: audio initialization failed,"
                             " audio is deactivated" );
                p_main->b_audio = 0;
            }
        }

Sam Hocevar's avatar
Sam Hocevar committed
323 324 325
        /*
         * This is the main loop
         */
326 327 328
        intf_Run( p_main->p_intf );

        intf_Destroy( p_main->p_intf );
Michel Kaempf's avatar
Michel Kaempf committed
329

330 331 332 333 334 335 336
        /*
         * Close audio device
         */
        if( p_main->b_audio )
        {
            aout_DestroyThread( p_main->p_aout, NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
337
    }
Michel Kaempf's avatar
Michel Kaempf committed
338 339 340 341

    /*
     * Free shared resources and libraries
     */
342 343
    /* FIXME */
#if 0
344
    if( p_main->b_vlans )
345 346 347
    {
        input_VlanDestroy();
    }
348
#endif
Michel Kaempf's avatar
Michel Kaempf committed
349

Sam Hocevar's avatar
 
Sam Hocevar committed
350 351 352 353 354
    /*
     * Free module bank
     */
    module_DestroyBank( p_main->p_module_bank );

355
    /*
356
     * Free plugin bank
357
     */
358 359 360 361 362 363 364
    bank_Destroy( p_main->p_bank );

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

365
#ifdef SYS_BEOS
366 367 368 369
    /*
     * System specific cleaning code
     */
    beos_Destroy();
370 371
#endif

Michel Kaempf's avatar
Michel Kaempf committed
372 373 374
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
375
    intf_Msg( "intf: program terminated." );
Vincent Seguin's avatar
Vincent Seguin committed
376
    intf_MsgDestroy();
377

Michel Kaempf's avatar
Michel Kaempf committed
378 379 380
    return( 0 );
}

381
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
382
 * main_GetIntVariable: get the int value of an environment variable
383
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
384
 * This function is used to read some default parameters in modules.
385
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
386 387
int main_GetIntVariable( char *psz_name, int i_default )
{
388 389 390
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
391 392 393

    psz_env = getenv( psz_name );
    if( psz_env )
394
    {
395 396 397 398
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
399
        }
400
    }
Vincent Seguin's avatar
Vincent Seguin committed
401 402 403
    return( i_default );
}

404
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
405
 * main_GetPszVariable: get the string value of an environment variable
406
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
407
 * This function is used to read some default parameters in modules.
408
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
409 410 411 412 413 414 415
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
416
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
417
    }
418
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
419 420
}

421
/*****************************************************************************
422
 * main_PutPszVariable: set the string value of an environment variable
423
 *****************************************************************************
424 425 426
 * 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.
427
 *****************************************************************************/
428 429 430 431 432 433 434
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
435 436
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
437 438 439 440 441 442
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
443
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
444
        }
445 446 447
    }
}

448
/*****************************************************************************
449
 * main_PutIntVariable: set the integer value of an environment variable
450
 *****************************************************************************
451 452 453
 * 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.
454
 *****************************************************************************/
455 456
void main_PutIntVariable( char *psz_name, int i_value )
{
457
    char psz_value[ 256 ];                               /* buffer for value */
458

459
    sprintf( psz_value, "%d", i_value );
460
    main_PutPszVariable( psz_name, psz_value );
461 462
}

Michel Kaempf's avatar
Michel Kaempf committed
463 464
/* following functions are local */

465
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
466
 * SetDefaultConfiguration: set default options
467
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
468
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
469 470 471
 * 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.
472
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
473
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
474 475
{
    /*
476
     * All features are activated by default except vlans
Michel Kaempf's avatar
Michel Kaempf committed
477
     */
Vincent Seguin's avatar
Vincent Seguin committed
478 479
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
480
    p_main->b_vlans  = 0;
Stéphane Borel's avatar
Stéphane Borel committed
481
    p_main->b_dvd    = 0;
Michel Kaempf's avatar
Michel Kaempf committed
482 483
}

484
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
485
 * GetConfiguration: parse command line
486
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
487 488
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
489
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
490 491
 * stage, but most structures are not allocated, so only environment should
 * be used.
492
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
493
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
494
{
Vincent Seguin's avatar
Vincent Seguin committed
495
    int c, i_opt;
Sam Hocevar's avatar
Sam Hocevar committed
496
    char * p_pointer;
Michel Kaempf's avatar
Michel Kaempf committed
497 498

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
499 500
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
501
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
502
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
503

Sam Hocevar's avatar
 
Sam Hocevar committed
504
    intf_MsgImm( COPYRIGHT_MESSAGE );
Sam Hocevar's avatar
Sam Hocevar committed
505

506
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
Sam Hocevar committed
507 508 509 510 511 512 513 514 515 516 517 518 519
    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
520
    /* Parse command line options */
Benoit Steiner's avatar
 
Benoit Steiner committed
521
#ifdef HAVE_GETOPT_H
Michel Kaempf's avatar
Michel Kaempf committed
522 523 524 525 526
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
527 528
        /* General/common options */
        case 'h':                                              /* -h, --help */
529 530 531
            Usage( SHORT_HELP );
            return( -1 );
            break;
532
        case 'H':                                          /* -H, --longhelp */
533
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
534 535
            return( -1 );
            break;
536
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
537 538
            Version();
            return( -1 );
539
            break;
Michel Kaempf's avatar
Michel Kaempf committed
540 541

        /* Audio options */
542
        case OPT_NOAUDIO:                                       /* --noaudio */
543
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
544
            break;
545 546 547
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
548
        case OPT_STEREO:                                         /* --stereo */
549
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
550
            break;
551
        case OPT_MONO:                                             /* --mono */
552
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
553 554 555
            break;

        /* Video options */
556
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
557
            p_main->b_video = 0;
558
            break;
559 560 561
        case OPT_VOUT:                                             /* --vout */
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
562
        case OPT_DISPLAY:                                       /* --display */
563
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
564 565
            break;
        case OPT_WIDTH:                                           /* --width */
566
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
567 568 569 570 571
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
572
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
573 574
            break;
        case OPT_COLOR:                                           /* --color */
575
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
576
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
577 578 579 580 581 582
	case OPT_FULLSCREEN:                                 /* --fullscreen */
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
	case OPT_OVERLAY:                                       /* --overlay */
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
583 584 585
	case OPT_MOTION:                                         /* --motion */
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
586 587 588
	case OPT_IDCT:                                             /* --idct */
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
589 590 591
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
592

593 594
        /* DVD options */
        case 'a':
595 596
            if ( ! strcmp(optarg, "ac3") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
597 598
            else if ( ! strcmp(optarg, "lpcm") )
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
599
            else if ( ! strcmp(optarg, "mpeg") )
600
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
601 602
            else
                main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
603 604 605 606 607 608 609 610
            break;
        case 'c':
            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
            break;
        case 's':
            main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
            break;

611
        /* Input options */
612
        case OPT_VLANS:                                           /* --vlans */
613
            p_main->b_vlans = 1;
614 615
            break;
        case OPT_SERVER:                                         /* --server */
616
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
617 618
            break;
        case OPT_PORT:                                             /* --port */
619
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
620
            break;
621 622 623
        case OPT_BROADCAST:                                   /* --broadcast */
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            break;
Stéphane Borel's avatar
Stéphane Borel committed
624 625 626
        case OPT_DVD:                                               /* --dvd */
            p_main->b_dvd = 1;
            break;
627

628
        /* Synchro options */
629
        case OPT_SYNCHRO:                                      
630 631 632
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;

633 634 635 636 637
        /* Interface warning messages level */
        case OPT_WARNING:                                       /* --warning */
            main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
            break;
            
Michel Kaempf's avatar
Michel Kaempf committed
638
        /* Internal error: unknown option */
639
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
640
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
641
            intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
642
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
643 644 645 646
            return( EINVAL );
            break;
        }
    }
Benoit Steiner's avatar
Benoit Steiner committed
647
#endif
Michel Kaempf's avatar
Michel Kaempf committed
648

Vincent Seguin's avatar
Vincent Seguin committed
649 650 651
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
652
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
653
    }
Michel Kaempf's avatar
Michel Kaempf committed
654 655 656
    return( 0 );
}

657
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
658
 * Usage: print program usage
659
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
660
 * Print a short inline help. Message interface is initialized at this stage.
661
 *****************************************************************************/
662
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
663
{
Vincent Seguin's avatar
Vincent Seguin committed
664
    /* Usage */
Sam Hocevar's avatar
 
Sam Hocevar committed
665
    intf_Msg( "Usage: %s [options] [parameters] [file]...",
Sam Hocevar's avatar
Sam Hocevar committed
666
              p_main->psz_arg0 );
667 668 669

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
670
        intf_Msg( "Try `%s --help' for more information.",
Sam Hocevar's avatar
Sam Hocevar committed
671
                  p_main->psz_arg0 );
672 673 674
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
675
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
676 677
    intf_Msg( "\nOptions:"
              "\n      --noaudio                  \tdisable audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
678
              "\n      --aout <module>            \taudio output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
679
              "\n      --stereo, --mono           \tstereo/mono audio"
680
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
681
              "\n      --novideo                  \tdisable video"
Sam Hocevar's avatar
 
Sam Hocevar committed
682
              "\n      --vout <module>            \tvideo output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
683 684 685 686
              "\n      --display <display>        \tdisplay string"
              "\n      --width <w>, --height <h>  \tdisplay dimensions"
              "\n  -g, --grayscale                \tgrayscale output"
              "\n      --color                    \tcolor output"
Sam Hocevar's avatar
 
Sam Hocevar committed
687
              "\n      --motion <module>          \tmotion compensation method"
Sam Hocevar's avatar
 
Sam Hocevar committed
688 689 690
              "\n      --idct <module>            \tIDCT method"
              "\n      --yuv <module>             \tYUV method"
              "\n      --synchro <type>           \tforce synchro algorithm"
691
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
692
              "\n      --dvd                      \tDVD mode"
Sam Hocevar's avatar
 
Sam Hocevar committed
693 694 695
              "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
              "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
              "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
696
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
697 698 699 700
              "\n      --vlans                    \tenable vlans"
              "\n      --server <host>            \tvideo server address"
              "\n      --port <port>              \tvideo server port"
              "\n      --broadcast                \tlisten to a broadcast"
701
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
702
              "\n      --warning <level>          \tdisplay warning messages"
703
              "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
704 705 706
              "\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
707

708 709 710
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
711
    /* Interface parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
712 713 714 715
    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
716 717

    /* Audio parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
718 719 720 721 722
    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
723 724

    /* Video parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
725 726 727 728 729 730
    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"
Sam Hocevar's avatar
 
Sam Hocevar committed
731
              "\n  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output"
Sam Hocevar's avatar
 
Sam Hocevar committed
732 733
              "\n  " VOUT_FULLSCREEN_VAR "={1|0}            \tfullscreen"
              "\n  " VOUT_OVERLAY_VAR "={1|0}               \toverlay"
Sam Hocevar's avatar
 
Sam Hocevar committed
734
              "\n  " MOTION_METHOD_VAR "=<method name>      \tmotion compensation method"
Sam Hocevar's avatar
 
Sam Hocevar committed
735 736
              "\n  " IDCT_METHOD_VAR "=<method name>        \tIDCT method"
              "\n  " YUV_METHOD_VAR "=<method name>         \tYUV method"
Sam Hocevar's avatar
 
Sam Hocevar committed
737 738
              "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm"
 );
Vincent Seguin's avatar
Vincent Seguin committed
739

740
    /* DVD parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
741
    intf_Msg( "\nDVD parameters:"
Sam Hocevar's avatar
 
Sam Hocevar committed
742
              "\n  " INPUT_DVD_DEVICE_VAR "=<device>           \tDVD device"
Sam Hocevar's avatar
 
Sam Hocevar committed
743 744 745
              "\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" );
746

747
    /* Input parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
748 749 750 751 752 753
    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"
Stéphane Borel's avatar
Stéphane Borel committed
754 755
              "\n  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port"
 );
756

Michel Kaempf's avatar
Michel Kaempf committed
757 758
}

759
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
760
 * Version: print complete program version
761
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
762
 * Print complete program version and build number.
763
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
764 765
static void Version( void )
{
766
    intf_Msg( VERSION_MESSAGE
767 768 769
              "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
770
              "Written by the VideoLAN team at Ecole Centrale, Paris." );
771

772
}
Vincent Seguin's avatar
Vincent Seguin committed
773

774
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
775
 * InitSignalHandler: system signal handler initialization
776
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
777
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
778
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
779
 * when we don't want to use SIGKILL.
780
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
781 782
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
783
    /* Termination signals */
Benoit Steiner's avatar
 
Benoit Steiner committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    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
802
    intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
803 804
}

Benoit Steiner's avatar
 
Benoit Steiner committed
805