main.c 26.3 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
3
4
 * main.c: main vlc source
 * Includes the main() function for vlc. Parses command line, start interface
 * and spawn threads.
5
6
7
8
9
10
11
12
13
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 * 
15
16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20
21
22
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
24

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

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

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

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

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

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

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

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

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

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

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

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

84
85
#define OPT_SYNCHRO             180

86
/* Usage fashion */
87
88
89
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
90

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

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

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

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

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

128
129
130
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },

Michel Kaempf's avatar
Michel Kaempf committed
131
132
133
134
    {   0,                  0,          0,      0 }
};

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

137
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
138
 * Global variable program_data - this is the one and only, see main.h
139
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
140
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
141

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

Michel Kaempf's avatar
Michel Kaempf committed
151
152
static void InitSignalHandler       ( void );
static void SignalHandler           ( int i_signal );
153
#ifdef HAVE_MMX
154
       int  TestMMX                 ( void );
155
#endif
Michel Kaempf's avatar
Michel Kaempf committed
156

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

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

173
174
175
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
Sam Hocevar committed
176
#ifdef SYS_BEOS
177
    beos_Create();
178
179
#endif

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

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

    /*
     * Read configuration
     */
206
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
207
    {
Vincent Seguin's avatar
Vincent Seguin committed
208
        intf_MsgDestroy();
209
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
210
    }
211

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

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

    /*
     * Initialize shared resources and libraries
     */
240
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
241
    {
242
243
244
245
        /* 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
246
    }
247

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

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

Sam Hocevar's avatar
Sam Hocevar committed
270
271
272
        /*
         * This is the main loop
         */
273
274
275
        intf_Run( p_main->p_intf );

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

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

    /*
     * Free shared resources and libraries
     */
289
    if( p_main->b_vlans )
290
291
292
    {
        input_VlanDestroy();
    }
Michel Kaempf's avatar
Michel Kaempf committed
293

294
    /*
295
     * Free plugin bank
296
     */
297
298
299
300
301
302
303
    bank_Destroy( p_main->p_bank );

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

304
#ifdef SYS_BEOS
305
306
307
308
    /*
     * System specific cleaning code
     */
    beos_Destroy();
309
310
#endif

Michel Kaempf's avatar
Michel Kaempf committed
311
312
313
    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
314
315
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
316

Michel Kaempf's avatar
Michel Kaempf committed
317
318
319
    return( 0 );
}

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

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

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

    psz_env = getenv( psz_name );
    if( psz_env )
    {
355
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
356
    }
357
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
358
359
}

360
/*****************************************************************************
361
 * main_PutPszVariable: set the string value of an environment variable
362
 *****************************************************************************
363
364
365
 * 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.
366
 *****************************************************************************/
367
368
369
370
371
372
373
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 )
    {
374
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
375
376
377
378
379
380
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
381
            intf_ErrMsg( "error: %s\n", strerror(errno) );
382
        }
383
384
385
    }
}

386
/*****************************************************************************
387
 * main_PutIntVariable: set the integer value of an environment variable
388
 *****************************************************************************
389
390
391
 * 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.
392
 *****************************************************************************/
393
394
void main_PutIntVariable( char *psz_name, int i_value )
{
395
    char psz_value[ 256 ];                               /* buffer for value */
396

397
    sprintf( psz_value, "%d", i_value );
398
    main_PutPszVariable( psz_name, psz_value );
399
400
}

Michel Kaempf's avatar
Michel Kaempf committed
401
402
/* following functions are local */

403
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
404
 * SetDefaultConfiguration: set default options
405
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
406
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
407
408
409
 * 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.
410
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
411
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
412
413
{
    /*
Vincent Seguin's avatar
Vincent Seguin committed
414
     * All features are activated by default
Michel Kaempf's avatar
Michel Kaempf committed
415
     */
Vincent Seguin's avatar
Vincent Seguin committed
416
417
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
418
    p_main->b_vlans  = 0;
Michel Kaempf's avatar
Michel Kaempf committed
419
420
}

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

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
436
437
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
438
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
439
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
440

Sam Hocevar's avatar
Sam Hocevar committed
441
442
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );

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

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

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

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

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

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

550
551
552
553
554
        /* Synchro options */
        case OPT_SYNCHRO:
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;

Michel Kaempf's avatar
Michel Kaempf committed
555
        /* Internal error: unknown option */
556
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
557
        default:
558
559
            intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
560
561
562
563
564
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
565
566
567
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
568
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
569
    }
Michel Kaempf's avatar
Michel Kaempf committed
570
571
572
    return( 0 );
}

573
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
574
 * Usage: print program usage
575
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
576
 * Print a short inline help. Message interface is initialized at this stage.
577
 *****************************************************************************/
578
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
579
{
Vincent Seguin's avatar
Vincent Seguin committed
580
    /* Usage */
Sam Hocevar's avatar
Sam Hocevar committed
581
582
    intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
              p_main->psz_arg0 );
583
584
585

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
Sam Hocevar committed
586
587
        intf_Msg( "Try `%s --help' for more information.\n",
                  p_main->psz_arg0 );
588
589
590
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
591
    /* Options */
592
593
594
595
596
597
    intf_Msg( "\n"
              "Options:\n"
              "      --noaudio                  \tdisable audio\n"
              "      --aout <plugin>            \taudio output method\n"
              "      --stereo, --mono           \tstereo/mono audio\n"
              "\n"
598
              "      --novideo                  \tdisable video\n"
599
600
601
602
603
604
              "      --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"
605
606
607
              "  -a, --dvdaudio <type>          \tchoose DVD audio type\n"
              "  -c, --dvdchannel <channel>     \tchoose DVD audio channel\n"
              "  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel\n"
608
              "\n"
609
              "      --vlans                    \tenable vlans\n"
610
611
              "      --server <host>            \tvideo server address\n"
              "      --port <port>              \tvideo server port\n"
612
              "      --broadcast                \tlisten to a broadcast\n"
613
              "\n"
614
615
              "      --synchro <type>           \tforce synchro algorithm\n"
              "\n"
616
617
618
              "  -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
619

620
621
622
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
623
    /* Interface parameters */
624
625
    intf_Msg( "\n"
              "Interface parameters:\n"
626
              "  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script\n"
627
              "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n" );
Vincent Seguin's avatar
Vincent Seguin committed
628
629

    /* Audio parameters */
630
631
632
633
634
635
    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
636
637

    /* Video parameters */
638
639
640
641
642
643
644
645
    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
646

647
648
649
650
651
652
653
    /* 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" );

654
    /* Input parameters */
655
656
657
658
659
    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"
660
              "  " INPUT_BROADCAST_VAR "={1|0}            \tbroadcast mode\n"
661
662
              "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
              "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
663
664
665
666

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

670
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
671
 * Version: print complete program version
672
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
673
 * Print complete program version and build number.
674
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
675
676
static void Version( void )
{
677
    intf_Msg( VERSION_MESSAGE
678
679
680
681
              "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" );
682

683
}
Vincent Seguin's avatar
Vincent Seguin committed
684

685
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
686
 * InitSignalHandler: system signal handler initialization
687
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
688
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
689
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
690
 * when we don't want to use SIGKILL.
691
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
692
693
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
694
695
696
697
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
698
699
}

700
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
701
 * SignalHandler: system signal handler
702
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
703
704
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
705
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
706
707
static void SignalHandler( int i_signal )
{
708
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
709
710
711
712
713
714
715
716
717
     * 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 );

718
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
719
720
721
722
     * interface thread */
    p_main->p_intf->b_die = 1;
}