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

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

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

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

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

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

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

#define OPT_NOVIDEO             160
71
72
73
74
75
#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
76
77

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

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

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

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

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

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

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

    {   0,                  0,          0,      0 }
};

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

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

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

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

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

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

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

170
#ifdef HAVE_MMX
Michel Kaempf's avatar
Michel Kaempf committed
171
    /*
172
     * Test if our code is likely to run on this CPU 
173
     */
174
175
    if( !TestMMX() )
    {
176
177
        fprintf( stderr, "Sorry, this program needs an MMX processor. "
                         "Please run the non-MMX version.\n" );
178
        return( 1 );
179
180
    }
#endif
181
182
183
184

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
185
    p_main->p_msg = intf_MsgCreate();
186
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
187
    {
188
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
189
190
                strerror(errno) );
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
191
    }
192
193
194
195

    /*
     * Read configuration
     */
196
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
197
    {
Vincent Seguin's avatar
Vincent Seguin committed
198
        intf_MsgDestroy();
199
200
        fprintf( stderr, "error: can't read configuration (%s)\n",
                strerror(errno) );
201
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
202
    }
203

204
205
206
207
208
    /*
     * Initialize playlist and get commandline files
     */
    p_main->p_playlist = playlist_Create( );
    if( !p_main->p_playlist )
209
    {
210
211
212
        intf_Msg( "Playlist initialization failed\n" );
        intf_MsgDestroy();
        return( errno );
213
    }
214
215
216
217
218
219
220
    playlist_Init( p_main->p_playlist, optind );

    /*
     * Initialize plugin bank
     */
    p_main->p_bank = bank_Create( );
    if( !p_main->p_bank )
221
    {
222
223
224
225
        intf_Msg( "Plugin bank initialization failed\n" );
        playlist_Destroy( p_main->p_playlist );
        intf_MsgDestroy();
        return( errno );
226
    }
227
    bank_Init( p_main->p_bank );
Michel Kaempf's avatar
Michel Kaempf committed
228
229
230
231

    /*
     * Initialize shared resources and libraries
     */
232
    if( p_main->b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
233
    {
234
235
236
237
        /* 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
238
    }
239

Michel Kaempf's avatar
Michel Kaempf committed
240
    /*
Vincent Seguin's avatar
Vincent Seguin committed
241
     * Open audio device and start aout thread
Michel Kaempf's avatar
Michel Kaempf committed
242
     */
243
    if( p_main->b_audio )
Vincent Seguin's avatar
Vincent Seguin committed
244
    {
245
246
        p_main->p_aout = aout_CreateThread( NULL );
        if( p_main->p_aout == NULL )
247
        {
248
            /* On error during audio initialization, switch off audio */
249
            intf_Msg( "Audio initialization failed : audio is deactivated\n" );
250
            p_main->b_audio = 0;
251
        }
Michel Kaempf's avatar
Michel Kaempf committed
252
    }
253

Michel Kaempf's avatar
Michel Kaempf committed
254
255
256
    /*
     * Run interface
     */
257
258
    p_main->p_intf = intf_Create();
    if( p_main->p_intf != NULL )
Vincent Seguin's avatar
Vincent Seguin committed
259
    {
260
        InitSignalHandler();             /* prepare signals for interception */
261

262
263
264
        intf_Run( p_main->p_intf );

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

Michel Kaempf's avatar
Michel Kaempf committed
267
    /*
Michel Kaempf's avatar
Michel Kaempf committed
268
269
     * Close audio device
     */
270
    if( p_main->b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
271
    {
272
        aout_DestroyThread( p_main->p_aout, NULL );
Michel Kaempf's avatar
Michel Kaempf committed
273
    }
Michel Kaempf's avatar
Michel Kaempf committed
274
275
276
277

    /*
     * Free shared resources and libraries
     */
278
    if( p_main->b_vlans )
279
280
281
    {
        input_VlanDestroy();
    }
Michel Kaempf's avatar
Michel Kaempf committed
282

283
    /*
284
     * Free plugin bank
285
     */
286
287
288
289
290
291
292
    bank_Destroy( p_main->p_bank );

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

293
#ifdef SYS_BEOS
294
295
296
297
    /*
     * System specific cleaning code
     */
    beos_Destroy();
298
299
#endif

Michel Kaempf's avatar
Michel Kaempf committed
300
301
302
    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
303
304
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
305

Michel Kaempf's avatar
Michel Kaempf committed
306
307
308
    return( 0 );
}

309
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
310
 * main_GetIntVariable: get the int value of an environment variable
311
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
312
 * This function is used to read some default parameters in modules.
313
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
314
315
int main_GetIntVariable( char *psz_name, int i_default )
{
316
317
318
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
319
320
321

    psz_env = getenv( psz_name );
    if( psz_env )
322
    {
323
324
325
326
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
327
        }
328
    }
Vincent Seguin's avatar
Vincent Seguin committed
329
330
331
    return( i_default );
}

332
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
333
 * main_GetPszVariable: get the string value of an environment variable
334
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
335
 * This function is used to read some default parameters in modules.
336
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
337
338
339
340
341
342
343
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
344
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
345
    }
346
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
347
348
}

349
/*****************************************************************************
350
 * main_PutPszVariable: set the string value of an environment variable
351
 *****************************************************************************
352
353
354
 * 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.
355
 *****************************************************************************/
356
357
358
359
360
361
362
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 )
    {
363
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
364
365
366
367
368
369
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
370
            intf_ErrMsg( "error: %s\n", strerror(errno) );
371
        }
372
373
374
    }
}

375
/*****************************************************************************
376
 * main_PutIntVariable: set the integer value of an environment variable
377
 *****************************************************************************
378
379
380
 * 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.
381
 *****************************************************************************/
382
383
void main_PutIntVariable( char *psz_name, int i_value )
{
384
    char psz_value[ 256 ];                               /* buffer for value */
385

386
    sprintf( psz_value, "%d", i_value );
387
    main_PutPszVariable( psz_name, psz_value );
388
389
}

Michel Kaempf's avatar
Michel Kaempf committed
390
391
/* following functions are local */

392
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
393
 * SetDefaultConfiguration: set default options
394
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
395
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
396
397
398
 * 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.
399
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
400
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
401
402
{
    /*
Vincent Seguin's avatar
Vincent Seguin committed
403
     * All features are activated by default
Michel Kaempf's avatar
Michel Kaempf committed
404
     */
Vincent Seguin's avatar
Vincent Seguin committed
405
406
407
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
    p_main->b_vlans  = 1;
Michel Kaempf's avatar
Michel Kaempf committed
408
409
}

410
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
411
 * GetConfiguration: parse command line
412
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
413
414
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
415
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
416
417
 * stage, but most structures are not allocated, so only environment should
 * be used.
418
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
419
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
420
{
Vincent Seguin's avatar
Vincent Seguin committed
421
    int c, i_opt;
Michel Kaempf's avatar
Michel Kaempf committed
422
423

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
424
425
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
426
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
427
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
428

Sam Hocevar's avatar
Sam Hocevar committed
429
430
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );

Vincent Seguin's avatar
Vincent Seguin committed
431
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
432
433
434
435
436
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
437
438
        /* General/common options */
        case 'h':                                              /* -h, --help */
439
440
441
            Usage( SHORT_HELP );
            return( -1 );
            break;
442
        case 'H':                                          /* -H, --longhelp */
443
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
444
445
            return( -1 );
            break;
446
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
447
448
            Version();
            return( -1 );
449
            break;
Michel Kaempf's avatar
Michel Kaempf committed
450
451

        /* Audio options */
452
        case OPT_NOAUDIO:                                       /* --noaudio */
453
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
454
            break;
455
456
457
        case OPT_AOUT:                                             /* --aout */
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
458
        case OPT_STEREO:                                         /* --stereo */
459
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
460
            break;
461
        case OPT_MONO:                                             /* --mono */
462
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
463
464
465
            break;

        /* Video options */
466
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
467
            p_main->b_video = 0;
468
            break;
469
470
471
        case OPT_VOUT:                                             /* --vout */
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
472
        case OPT_DISPLAY:                                       /* --display */
473
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
474
475
            break;
        case OPT_WIDTH:                                           /* --width */
476
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
477
478
479
480
481
482
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;

        case 'g':                                         /* -g, --grayscale */
483
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
484
485
            break;
        case OPT_COLOR:                                           /* --color */
486
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
487
            break;
Michel Kaempf's avatar
Michel Kaempf committed
488

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
        /* 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;

507
        /* Input options */
508
        case OPT_NOVLANS:                                       /* --novlans */
Vincent Seguin's avatar
Vincent Seguin committed
509
            p_main->b_vlans = 0;
510
511
            break;
        case OPT_SERVER:                                         /* --server */
512
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
513
514
            break;
        case OPT_PORT:                                             /* --port */
515
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
516
            break;
517

Michel Kaempf's avatar
Michel Kaempf committed
518
        /* Internal error: unknown option */
519
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
520
        default:
521
522
            intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
523
524
525
526
527
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
528
529
530
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
531
        putenv( ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
532
    }
Michel Kaempf's avatar
Michel Kaempf committed
533
534
535
    return( 0 );
}

536
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
537
 * Usage: print program usage
538
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
539
 * Print a short inline help. Message interface is initialized at this stage.
540
 *****************************************************************************/
541
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
542
{
Vincent Seguin's avatar
Vincent Seguin committed
543
    /* Usage */
544
    intf_Msg( "Usage: vlc [options] [parameters] [file]...\n" );
545
546
547
548
549
550
551

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

Vincent Seguin's avatar
Vincent Seguin committed
552
    /* Options */
553
554
555
556
557
558
    intf_Msg( "\n"
              "Options:\n"
              "      --noaudio                  \tdisable audio\n"
              "      --aout <plugin>            \taudio output method\n"
              "      --stereo, --mono           \tstereo/mono audio\n"
              "\n"
559
              "      --novideo                  \tdisable video\n"
560
561
562
563
564
565
              "      --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"
566
567
568
569
              "  -a, --dvdaudio                 \tchoose DVD audio type\n"
              "  -c, --dvdchannel               \tchoose DVD audio channel\n"
              "  -s, --dvdsubtitle              \tchoose DVD subtitle channel\n"
              "\n"
570
571
572
573
574
575
576
              "      --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
577

578
579
580
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
581
    /* Interface parameters */
582
583
584
585
    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
586
587

    /* Audio parameters */
588
589
590
591
592
593
    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
594
595

    /* Video parameters */
596
597
598
599
600
601
602
603
    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
604

605
606
607
608
609
610
611
    /* 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" );

612
    /* Input parameters */
613
614
615
616
617
618
619
    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
620
621
}

622
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
623
 * Version: print complete program version
624
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
625
 * Print complete program version and build number.
626
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
627
628
static void Version( void )
{
629
    intf_Msg( VERSION_MESSAGE
630
631
632
633
              "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" );
634

635
}
Vincent Seguin's avatar
Vincent Seguin committed
636

637
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
638
 * InitSignalHandler: system signal handler initialization
639
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
640
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
641
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
642
 * when we don't want to use SIGKILL.
643
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
644
645
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
646
647
648
649
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
650
651
}

652
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
653
 * SignalHandler: system signal handler
654
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
655
656
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
657
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
658
659
static void SignalHandler( int i_signal )
{
660
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
661
662
663
664
665
666
667
668
669
     * 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 );

670
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
671
672
673
674
     * interface thread */
    p_main->p_intf->b_die = 1;
}

675
676
677
678
679
#ifdef HAVE_MMX
/*****************************************************************************
 * TestMMX: tests if the processor has MMX support.
 *****************************************************************************
 * This function is called if HAVE_MMX is enabled, to check whether the
680
 * CPU really supports MMX.
681
682
683
 *****************************************************************************/
static int TestMMX( void )
{
684
/* FIXME: under beos, gcc does not support the following inline assembly */ 
685
686
687
#ifdef SYS_BEOS
    return( 1 );
#else
688

689
    int i_reg, i_dummy = 0;
690

691
    /* test for a 386 CPU */
692
693
694
695
696
697
698
699
700
701
    asm volatile ( "pushfl
                    popl %%eax
                    movl %%eax, %%ecx
                    xorl $0x40000, %%eax
                    pushl %%eax
                    popfl
                    pushfl
                    popl %%eax
                    xorl %%ecx, %%eax
                    andl $0x40000, %%eax"
702
                 : "=a" ( i_reg ) );
703

704
    if( !i_reg )
705
706
        return( 0 );

707
    /* test for a 486 CPU */
708
709
710
711
712
713
714
    asm volatile ( "movl %%ecx, %%eax
                    xorl $0x200000, %%eax
                    pushl %%eax
                    popfl
                    pushfl
                    popl %%eax
                    xorl %%ecx, %%eax
715
                    pushl %%ecx
716
717
                    popfl
                    andl $0x200000, %%eax"
718
                 : "=a" ( i_reg ) );
719

720
    if( !i_reg )
721
722
        return( 0 );

723
    /* the CPU supports the CPUID instruction - get its level */
724
    asm volatile ( "cpuid"
725
726
727
728
729
730
                 : "=a" ( i_reg ),
                   "=b" ( i_dummy ),
                   "=c" ( i_dummy ),
                   "=d" ( i_dummy )
                 : "a"  ( 0 ),       /* level 0 */
                   "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
731

732
    /* this shouldn't happen on a normal CPU */
733
    if( !i_reg )
734
735
736
737
738
        return( 0 );

    /* test for the MMX flag */
    asm volatile ( "cpuid
                    andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
739
740
741
742
743
744
745
746
                 : "=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 )
747
748
749
        return( 0 );

    return( 1 );
750

751
#endif
752
753
}
#endif