main.c 26.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
#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"
42
#include "plugins.h"
Michel Kaempf's avatar
Michel Kaempf committed
43
#include "input_vlan.h"
Sam Hocevar's avatar
Sam Hocevar committed
44
#include "input_ps.h"
45

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

Vincent Seguin's avatar
Vincent Seguin committed
49
#include "audio_output.h"
50

51
52
53
54
#ifdef SYS_BEOS
#include "beos_specific.h"
#endif

Vincent Seguin's avatar
Vincent Seguin committed
55
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
56

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

/* 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
65
66
67
#define OPT_AOUT                151
#define OPT_STEREO              152
#define OPT_MONO                153
Michel Kaempf's avatar
Michel Kaempf committed
68
69

#define OPT_NOVIDEO             160
70
71
72
73
74
#define OPT_VOUT                161
#define OPT_DISPLAY             162
#define OPT_WIDTH               163
#define OPT_HEIGHT              164
#define OPT_COLOR               165
Michel Kaempf's avatar
Michel Kaempf committed
75
76

#define OPT_NOVLANS             170
77
78
#define OPT_SERVER              171
#define OPT_PORT                172
79

80
/* Usage fashion */
81
82
83
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
84

Michel Kaempf's avatar
Michel Kaempf committed
85
86
/* Long options */
static const struct option longopts[] =
87
88
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
89
90

    /* General/common options */
91
    {   "help",             0,          0,      'h' },
92
    {   "longhelp",         0,          0,      'H' },
93
    {   "version",          0,          0,      'v' },
Michel Kaempf's avatar
Michel Kaempf committed
94
95

    /* Audio options */
96
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
97
    {   "aout",             1,          0,      OPT_AOUT },
Michel Kaempf's avatar
Michel Kaempf committed
98
    {   "stereo",           0,          0,      OPT_STEREO },
99
    {   "mono",             0,          0,      OPT_MONO },
Michel Kaempf's avatar
Michel Kaempf committed
100
101

    /* Video options */
102
    {   "novideo",          0,          0,      OPT_NOVIDEO },
103
    {   "vout",             1,          0,      OPT_VOUT },
104
    {   "display",          1,          0,      OPT_DISPLAY },
105
    {   "width",            1,          0,      OPT_WIDTH },
106
107
108
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Michel Kaempf's avatar
Michel Kaempf committed
109

110
111
112
113
114
    /* DVD options */
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
115
    /* Input options */
Michel Kaempf's avatar
Michel Kaempf committed
116
    {   "novlans",          0,          0,      OPT_NOVLANS },
117
118
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Michel Kaempf's avatar
Michel Kaempf committed
119
120
121
122
123

    {   0,                  0,          0,      0 }
};

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

126
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
127
 * Global variable program_data - this is the one and only, see main.h
128
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
129
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
130

131
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
132
 * Local prototypes
133
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
134
135
static void SetDefaultConfiguration ( void );
static int  GetConfiguration        ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
136
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
137
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
138

Michel Kaempf's avatar
Michel Kaempf committed
139
140
static void InitSignalHandler       ( void );
static void SignalHandler           ( int i_signal );
141
#ifdef HAVE_MMX
142
static int  TestMMX                 ( void );
143
#endif
Michel Kaempf's avatar
Michel Kaempf committed
144

145
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
146
 * main: parse command line, start interface and spawn threads
147
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
148
149
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
Vincent Seguin's avatar
Vincent Seguin committed
150
 *      -openning of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
151
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
152
 *      -closing of audio output device and some global modules
153
 * On error, the spawned threads are cancelled, and the open devices closed.
154
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
155
156
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
157
    main_t  main_data;                      /* root of all data - see main.h */
158
159
160
    char **p_playlist;
    int i_list_index;

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

163
164
165
166
167
168
169
    /*
     * System specific initialization code
     */
#ifdef SYS_BEOS
    beos_Init();
#endif

Michel Kaempf's avatar
Michel Kaempf committed
170
171
    /*
     * Read configuration, initialize messages interface and set up program
172
     */
173
174
175
176
#ifdef HAVE_MMX
    if( !TestMMX() )
    {
        fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
177
        return( 1 );
178
179
    }
#endif
Vincent Seguin's avatar
Vincent Seguin committed
180
    p_main->p_msg = intf_MsgCreate();
181
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
182
    {
183
184
185
        fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
                strerror(errno) );
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
186
    }
187
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
188
    {
Vincent Seguin's avatar
Vincent Seguin committed
189
        intf_MsgDestroy();
190
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
191
    }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

    /* get command line files */
    i_list_index = 0;

    if( optind < i_argc )
    {
        int i_index = 0;
        p_playlist = malloc( (i_list_index = i_argc - optind)
                                        * sizeof(int) );

        while( i_argc - i_index > optind )
        {
            p_playlist[ i_index ] = ppsz_argv[ i_argc - i_index - 1];
            i_index++;
        }
    }
    else
    {
        p_playlist = malloc( sizeof(int) );
        p_playlist[ 0 ] = "-";
        i_list_index = 1;
    }

215
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );          /* print welcome message */
Michel Kaempf's avatar
Michel Kaempf committed
216
217
218
219

    /*
     * Initialize shared resources and libraries
     */
Vincent Seguin's avatar
Vincent Seguin committed
220
    if( main_data.b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
221
222
    {
        /* On error during vlans initialization, switch of vlans */
223
        intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
Vincent Seguin's avatar
Vincent Seguin committed
224
        main_data.b_vlans = 0;
Michel Kaempf's avatar
Michel Kaempf committed
225
    }
226

Michel Kaempf's avatar
Michel Kaempf committed
227
    /*
Vincent Seguin's avatar
Vincent Seguin committed
228
     * Open audio device and start aout thread
Michel Kaempf's avatar
Michel Kaempf committed
229
     */
Vincent Seguin's avatar
Vincent Seguin committed
230
231
232
    if( main_data.b_audio )
    {
        main_data.p_aout = aout_CreateThread( NULL );
233
234
235
        if( main_data.p_aout == NULL )
        {
            /* On error during audio initialization, switch of audio */
236
            intf_Msg( "Audio initialization failed : audio is deactivated\n" );
Vincent Seguin's avatar
Vincent Seguin committed
237
            main_data.b_audio = 0;
238
        }
Michel Kaempf's avatar
Michel Kaempf committed
239
    }
240

Michel Kaempf's avatar
Michel Kaempf committed
241
242
243
    /*
     * Run interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
244
245
246
    main_data.p_intf = intf_Create();
    if( main_data.p_intf != NULL )
    {
247
248
249
        main_data.p_intf->p_playlist = p_playlist;
        main_data.p_intf->i_list_index = i_list_index;

250
        InitSignalHandler();             /* prepare signals for interception */
251

252
253
        intf_Run( main_data.p_intf );
        intf_Destroy( main_data.p_intf );
Vincent Seguin's avatar
Vincent Seguin committed
254
    }
Michel Kaempf's avatar
Michel Kaempf committed
255

Michel Kaempf's avatar
Michel Kaempf committed
256
    /*
Michel Kaempf's avatar
Michel Kaempf committed
257
258
     * Close audio device
     */
Vincent Seguin's avatar
Vincent Seguin committed
259
    if( main_data.b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
260
    {
Vincent Seguin's avatar
Vincent Seguin committed
261
        aout_DestroyThread( main_data.p_aout, NULL );
Michel Kaempf's avatar
Michel Kaempf committed
262
    }
Michel Kaempf's avatar
Michel Kaempf committed
263
264
265
266

    /*
     * Free shared resources and libraries
     */
Vincent Seguin's avatar
Vincent Seguin committed
267
    if( main_data.b_vlans )
268
269
270
    {
        input_VlanDestroy();
    }
Michel Kaempf's avatar
Michel Kaempf committed
271

272
273
274
275
276
277
278
    /*
     * System specific cleaning code
     */
#ifdef SYS_BEOS
    beos_Clean();
#endif

Michel Kaempf's avatar
Michel Kaempf committed
279
280
281
    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
282
283
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
Michel Kaempf's avatar
Michel Kaempf committed
284
285
286
    return( 0 );
}

287
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
288
 * main_GetIntVariable: get the int value of an environment variable
289
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
290
 * This function is used to read some default parameters in modules.
291
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
292
293
int main_GetIntVariable( char *psz_name, int i_default )
{
294
295
296
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
297
298
299

    psz_env = getenv( psz_name );
    if( psz_env )
300
    {
301
302
303
304
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
305
        }
306
    }
Vincent Seguin's avatar
Vincent Seguin committed
307
308
309
    return( i_default );
}

310
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
311
 * main_GetPszVariable: get the string value of an environment variable
312
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
313
 * This function is used to read some default parameters in modules.
314
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
315
316
317
318
319
320
321
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
322
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
323
    }
324
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
325
326
}

327
/*****************************************************************************
328
 * main_PutPszVariable: set the string value of an environment variable
329
 *****************************************************************************
330
331
332
 * 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.
333
 *****************************************************************************/
334
335
336
337
338
339
340
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 )
    {
341
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
342
343
344
345
346
347
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
348
            intf_ErrMsg( "error: %s\n", strerror(errno) );
349
        }
350
351
352
    }
}

353
/*****************************************************************************
354
 * main_PutIntVariable: set the integer value of an environment variable
355
 *****************************************************************************
356
357
358
 * 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.
359
 *****************************************************************************/
360
361
void main_PutIntVariable( char *psz_name, int i_value )
{
362
    char psz_value[ 256 ];                               /* buffer for value */
363

364
    sprintf( psz_value, "%d", i_value );
365
    main_PutPszVariable( psz_name, psz_value );
366
367
}

Michel Kaempf's avatar
Michel Kaempf committed
368
369
/* following functions are local */

370
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
371
 * SetDefaultConfiguration: set default options
372
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
373
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
374
375
376
 * 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.
377
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
378
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
379
380
{
    /*
Vincent Seguin's avatar
Vincent Seguin committed
381
     * All features are activated by default
Michel Kaempf's avatar
Michel Kaempf committed
382
     */
Vincent Seguin's avatar
Vincent Seguin committed
383
384
385
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
    p_main->b_vlans  = 1;
Michel Kaempf's avatar
Michel Kaempf committed
386
387
}

388
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
389
 * GetConfiguration: parse command line
390
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
391
392
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
393
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
394
395
 * stage, but most structures are not allocated, so only environment should
 * be used.
396
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
397
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
398
{
Vincent Seguin's avatar
Vincent Seguin committed
399
    int c, i_opt;
Michel Kaempf's avatar
Michel Kaempf committed
400
401

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
402
403
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
404
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
405
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
406

Vincent Seguin's avatar
Vincent Seguin committed
407
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
408
409
410
411
412
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
413
414
        /* General/common options */
        case 'h':                                              /* -h, --help */
415
416
417
            Usage( SHORT_HELP );
            return( -1 );
            break;
418
        case 'H':                                          /* -H, --longhelp */
419
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
420
421
            return( -1 );
            break;
422
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
423
424
            Version();
            return( -1 );
425
            break;
Michel Kaempf's avatar
Michel Kaempf committed
426
427

        /* Audio options */
428
        case OPT_NOAUDIO:                                       /* --noaudio */
429
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
430
            break;
431
432
433
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
434
        case OPT_STEREO:                                         /* --stereo */
435
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
436
            break;
437
        case OPT_MONO:                                             /* --mono */
438
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
439
440
441
            break;

        /* Video options */
442
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
443
            p_main->b_video = 0;
444
            break;
445
446
447
        case OPT_VOUT:                                             /* --vout */
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
448
        case OPT_DISPLAY:                                       /* --display */
449
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
450
451
            break;
        case OPT_WIDTH:                                           /* --width */
452
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
453
454
455
456
457
458
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;

        case 'g':                                         /* -g, --grayscale */
459
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
460
461
            break;
        case OPT_COLOR:                                           /* --color */
462
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
463
            break;
Michel Kaempf's avatar
Michel Kaempf committed
464

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
        /* 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;

483
        /* Input options */
484
        case OPT_NOVLANS:                                       /* --novlans */
Vincent Seguin's avatar
Vincent Seguin committed
485
            p_main->b_vlans = 0;
486
487
            break;
        case OPT_SERVER:                                         /* --server */
488
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
489
490
            break;
        case OPT_PORT:                                             /* --port */
491
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
492
            break;
493

Michel Kaempf's avatar
Michel Kaempf committed
494
        /* Internal error: unknown option */
495
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
496
        default:
497
498
            intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
499
500
501
502
503
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
504
505
506
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
507
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
508
    }
Michel Kaempf's avatar
Michel Kaempf committed
509
510
511
    return( 0 );
}

512
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
513
 * Usage: print program usage
514
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
515
 * Print a short inline help. Message interface is initialized at this stage.
516
 *****************************************************************************/
517
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
518
{
Vincent Seguin's avatar
Vincent Seguin committed
519
    /* Usage */
520
521
522
523
524
525
526
527
528
    intf_Msg( "Usage: vlc [options] [parameters]\n" );

    if( i_fashion == USAGE )
    {
        intf_Msg( "Try `vlc --help' for more information.\n" );
        return;
    }

    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
Vincent Seguin's avatar
Vincent Seguin committed
529
530

    /* Options */
531
532
533
534
535
536
    intf_Msg( "\n"
              "Options:\n"
              "      --noaudio                  \tdisable audio\n"
              "      --aout <plugin>            \taudio output method\n"
              "      --stereo, --mono           \tstereo/mono audio\n"
              "\n"
537
              "      --novideo                  \tdisable video\n"
538
539
540
541
542
543
              "      --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"
544
545
546
547
              "  -a, --dvdaudio                 \tchoose DVD audio type\n"
              "  -c, --dvdchannel               \tchoose DVD audio channel\n"
              "  -s, --dvdsubtitle              \tchoose DVD subtitle channel\n"
              "\n"
548
549
550
551
552
553
554
              "      --novlans                  \tdisable vlans\n"
              "      --server <host>            \tvideo server address\n"
              "      --port <port>              \tvideo server port\n"
              "\n"
              "  -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
555

556
557
558
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
559
    /* Interface parameters */
560
561
562
563
    intf_Msg( "\n"
              "Interface parameters:\n"
              "  " INTF_INIT_SCRIPT_VAR "=<filename>             \tinitialization script\n"
              "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n" );
Vincent Seguin's avatar
Vincent Seguin committed
564
565

    /* Audio parameters */
566
567
568
569
570
571
    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
572
573

    /* Video parameters */
574
575
576
577
578
579
580
581
    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
582

583
584
585
586
587
588
589
    /* 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" );

590
    /* Input parameters */
591
592
593
594
595
596
597
    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"
              "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
              "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
Michel Kaempf's avatar
Michel Kaempf committed
598
599
}

600
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
601
 * Version: print complete program version
602
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
603
 * Print complete program version and build number.
604
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
605
606
static void Version( void )
{
607
    intf_Msg( VERSION_MESSAGE
608
609
610
611
              "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" );
612

613
}
Vincent Seguin's avatar
Vincent Seguin committed
614

615
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
616
 * InitSignalHandler: system signal handler initialization
617
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
618
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
619
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
620
 * when we don't want to use SIGKILL.
621
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
622
623
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
624
625
626
627
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
628
629
}

630
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
631
 * SignalHandler: system signal handler
632
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
633
634
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
635
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
636
637
static void SignalHandler( int i_signal )
{
638
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
639
640
641
642
643
644
645
646
647
     * 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 );

648
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
649
650
651
652
     * interface thread */
    p_main->p_intf->b_die = 1;
}

653
654
655
656
657
658
659
660
661
#ifdef HAVE_MMX
/*****************************************************************************
 * TestMMX: tests if the processor has MMX support.
 *****************************************************************************
 * This function is called if HAVE_MMX is enabled, to check whether the
 * cpu really supports MMX.
 *****************************************************************************/
static int TestMMX( void )
{
662
/* FIXME: under beos, gcc does not support the foolowing inline assembly */ 
663
664
665
#ifdef SYS_BEOS
    return( 1 );
#else
666

667
    int i_reg, i_dummy = 0;
668
669
670
671
672
673
674
675
676
677
678
679

    /* test for a 386 cpu */
    asm volatile ( "pushfl
                    popl %%eax
                    movl %%eax, %%ecx
                    xorl $0x40000, %%eax
                    pushl %%eax
                    popfl
                    pushfl
                    popl %%eax
                    xorl %%ecx, %%eax
                    andl $0x40000, %%eax"
680
                 : "=a" ( i_reg ) );
681

682
    if( !i_reg )
683
684
685
686
687
688
689
690
691
692
        return( 0 );

    /* test for a 486 cpu */
    asm volatile ( "movl %%ecx, %%eax
                    xorl $0x200000, %%eax
                    pushl %%eax
                    popfl
                    pushfl
                    popl %%eax
                    xorl %%ecx, %%eax
693
                    pushl %%ecx
694
695
                    popfl
                    andl $0x200000, %%eax"
696
                 : "=a" ( i_reg ) );
697

698
    if( !i_reg )
699
700
701
702
        return( 0 );

    /* the cpu supports the CPUID instruction - get its level */
    asm volatile ( "cpuid"
703
704
705
706
707
708
                 : "=a" ( i_reg ),
                   "=b" ( i_dummy ),
                   "=c" ( i_dummy ),
                   "=d" ( i_dummy )
                 : "a"  ( 0 ),       /* level 0 */
                   "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
709
710

    /* this shouldn't happen on a normal cpu */
711
    if( !i_reg )
712
713
714
715
716
        return( 0 );

    /* test for the MMX flag */
    asm volatile ( "cpuid
                    andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
717
718
719
720
721
722
723
724
                 : "=a" ( i_dummy ),
                   "=b" ( i_dummy ),
                   "=c" ( i_dummy ),
                   "=d" ( i_reg )
                 : "a"  ( 1 ),       /* level 1 */
                   "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */

    if( !i_reg )
725
726
727
        return( 0 );

    return( 1 );
728

729
#endif
730
731
}
#endif