main.c 39.6 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
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
7
 * $Id: main.c,v 1.103 2001/06/14 01:49:44 sam Exp $
8
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
9 10
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
11 12 13 14 15
 *
 * 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.
16
 * 
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22 23 24
 * 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.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

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

32 33
#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
#include <stdio.h>                                              /* sprintf() */
34
#include <setjmp.h>                                       /* longjmp, setjmp */
35

Sam Hocevar's avatar
 
Sam Hocevar committed
36 37 38 39 40 41
#ifdef HAVE_GETOPT_LONG
#   ifdef HAVE_GETOPT_H
#       include <getopt.h>                                       /* getopt() */
#   endif
#else
#   include "GNUgetopt/getopt.h"
Benoit Steiner's avatar
Benoit Steiner committed
42 43
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
44
#ifdef SYS_DARWIN1_3
Sam Hocevar's avatar
 
Sam Hocevar committed
45 46
#   include <mach/mach.h>                               /* Altivec detection */
#   include <mach/mach_error.h>       /* some day the header files||compiler *
Sam Hocevar's avatar
 
Sam Hocevar committed
47
                                                       will define it for us */
Sam Hocevar's avatar
 
Sam Hocevar committed
48
#   include <mach/bootstrap.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
49
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
50 51 52 53

#ifndef WIN32
#include <netinet/in.h>                               /* BSD: struct in_addr */
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
54

Sam Hocevar's avatar
 
Sam Hocevar committed
55
#ifdef HAVE_UNISTD_H
Sam Hocevar's avatar
 
Sam Hocevar committed
56
#include <unistd.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
57 58
#endif

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

Michel Kaempf's avatar
Michel Kaempf committed
63 64
#include "config.h"
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
65
#include "debug.h"
66
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
67
#include "mtime.h"
68
#include "tests.h"                                              /* TestCPU() */
Sam Hocevar's avatar
 
Sam Hocevar committed
69
#include "modules.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
70

71 72
#include "stream_control.h"
#include "input_ext-intf.h"
73

Vincent Seguin's avatar
Vincent Seguin committed
74
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
75
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
76
#include "interface.h"
77

Vincent Seguin's avatar
Vincent Seguin committed
78
#include "audio_output.h"
79

Sam Hocevar's avatar
 
Sam Hocevar committed
80 81 82
#include "video.h"
#include "video_output.h"

83
#ifdef SYS_BEOS
Sam Hocevar's avatar
 
Sam Hocevar committed
84 85 86 87 88
#   include "beos_specific.h"
#endif

#ifdef SYS_DARWIN1_3
#   include "darwin_specific.h"
89 90
#endif

Henri Fallon's avatar
 
Henri Fallon committed
91 92
#include "netutils.h"                                 /* network_ChannelJoin */

Vincent Seguin's avatar
Vincent Seguin committed
93
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
94

95
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
96 97
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
98
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
99 100 101 102

/* Long options return values - note that values corresponding to short options
 * chars, and in general any regular char, should be avoided */
#define OPT_NOAUDIO             150
Sam Hocevar's avatar
 
Sam Hocevar committed
103 104
#define OPT_STEREO              151
#define OPT_MONO                152
105
#define OPT_SPDIF               153
Michel Kaempf's avatar
Michel Kaempf committed
106 107

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
108 109 110 111 112 113
#define OPT_DISPLAY             161
#define OPT_WIDTH               162
#define OPT_HEIGHT              163
#define OPT_COLOR               164
#define OPT_FULLSCREEN          165
#define OPT_OVERLAY             166
Michel Kaempf's avatar
Michel Kaempf committed
114

Henri Fallon's avatar
 
Henri Fallon committed
115
#define OPT_CHANNELS            170
116 117
#define OPT_SERVER              171
#define OPT_PORT                172
118
#define OPT_BROADCAST           173
119

Sam Hocevar's avatar
 
Sam Hocevar committed
120 121 122 123
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
124 125
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
126

Sam Hocevar's avatar
 
Sam Hocevar committed
127 128
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
129
#define OPT_VERSION             192
130

131
/* Usage fashion */
132 133 134
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
135

Sam Hocevar's avatar
 
Sam Hocevar committed
136 137 138 139 140 141 142 143 144 145
/* Needed for x86 CPU capabilities detection */
#define cpuid( a )                 \
    asm volatile ( "cpuid"         \
                 : "=a" ( i_eax ), \
                   "=b" ( i_ebx ), \
                   "=c" ( i_ecx ), \
                   "=d" ( i_edx )  \
                 : "a"  ( a )      \
                 : "cc" );

Michel Kaempf's avatar
Michel Kaempf committed
146 147
/* Long options */
static const struct option longopts[] =
148 149
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
150 151

    /* General/common options */
152
    {   "help",             0,          0,      'h' },
153
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
154
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
155

Sam Hocevar's avatar
 
Sam Hocevar committed
156
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
157
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
158 159
    {   "warning",          1,          0,      OPT_WARNING },

Michel Kaempf's avatar
Michel Kaempf committed
160
    /* Audio options */
161
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
162
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
163
    {   "stereo",           0,          0,      OPT_STEREO },
164
    {   "mono",             0,          0,      OPT_MONO },
165
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
166 167
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
Michel Kaempf's avatar
Michel Kaempf committed
168 169

    /* Video options */
170
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
171
    {   "vout",             1,          0,      'V' },
172
    {   "display",          1,          0,      OPT_DISPLAY },
173
    {   "width",            1,          0,      OPT_WIDTH },
174 175 176
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
177
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
178
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
179
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
180 181
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Michel Kaempf's avatar
Michel Kaempf committed
182

183
    /* DVD options */
184 185
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
186
    {   "dvdangle",         1,          0,      'u' },
187 188 189 190
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
191
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
192
    {   "input",            1,          0,      OPT_INPUT },
193 194
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Henri Fallon's avatar
 
Henri Fallon committed
195
    {   "broadcast",        1,          0,      OPT_BROADCAST },
196
    {   "channels",         0,          0,      OPT_CHANNELS },
Michel Kaempf's avatar
Michel Kaempf committed
197

198 199
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
200 201 202 203
    {   0,                  0,          0,      0 }
};

/* Short options */
204
static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
Michel Kaempf's avatar
Michel Kaempf committed
205

206
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
207 208
 * Global variable program_data - these are the only ones, see main.h and
 * modules.h
209
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
210 211 212 213
main_t        *p_main;
module_bank_t *p_module_bank;
aout_bank_t   *p_aout_bank;
vout_bank_t   *p_vout_bank;
Michel Kaempf's avatar
Michel Kaempf committed
214

215
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
216
 * Local prototypes
217
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
218
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
219
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
220
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
221
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
222
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
223

Michel Kaempf's avatar
Michel Kaempf committed
224
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
225 226
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
227
static void InstructionSignalHandler( int i_signal );
Sam Hocevar's avatar
 
Sam Hocevar committed
228
static int  CPUCapabilities         ( void );
229 230
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
231

232
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
233
 * main: parse command line, start interface and spawn threads
234
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
235 236
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
237
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
238
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
239
 *      -closing of audio output device and some global modules
240
 * On error, the spawned threads are canceled, and the open devices closed.
241
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
242 243
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
244 245 246 247
    main_t        main_data;                /* root of all data - see main.h */
    module_bank_t module_bank;
    aout_bank_t   aout_bank;
    vout_bank_t   vout_bank;
248

Sam Hocevar's avatar
 
Sam Hocevar committed
249 250 251 252
    p_main        = &main_data;               /* set up the global variables */
    p_module_bank = &module_bank;
    p_aout_bank   = &aout_bank;
    p_vout_bank   = &vout_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
253

Sam Hocevar's avatar
 
Sam Hocevar committed
254 255 256 257 258 259 260
    /*
     * Initialize threads
     */
#if defined( PTH_INIT_IN_PTH_H )
    pth_init( );
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
261
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
262
     * Test if our code is likely to run on this CPU 
Sam Hocevar's avatar
 
Sam Hocevar committed
263 264 265
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
    
266 267 268
#if defined( __pentium__ ) || defined( __pentiumpro__ )
    if( ! TestCPU( CPU_CAPABILITY_586 ) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
269 270
        fprintf( stderr, "error: this program needs a Pentium CPU,\n"
                         "please try a version without Pentium support\n" );
271 272 273 274
        return( 1 );
    }
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
275 276 277 278 279
    /*
     * System specific initialization code
     */
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
    system_Init( &i_argc, ppsz_argv, ppsz_env );
280
#endif
281 282 283 284

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
285
    p_main->p_msg = intf_MsgCreate();
286
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
287
    {
288
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
289
                 strerror(errno) );
290
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
291
    }
292

Sam Hocevar's avatar
 
Sam Hocevar committed
293 294
    intf_MsgImm( COPYRIGHT_MESSAGE );

295
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
296
     * Read configuration
297
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
298
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
299
    {
Vincent Seguin's avatar
Vincent Seguin committed
300
        intf_MsgDestroy();
301
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
302
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
303

304
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
305
     * Initialize playlist and get commandline files
306
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
307
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
308
    if( !p_main->p_playlist )
309
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
310
        intf_ErrMsg( "playlist error: playlist initialization failed" );
311 312
        intf_MsgDestroy();
        return( errno );
313
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
314
    intf_PlaylistInit( p_main->p_playlist );
315 316

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
317
     * Get input filenames given as commandline arguments
318
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
319
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
320

Sam Hocevar's avatar
 
Sam Hocevar committed
321
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
322
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
323
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
324
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
325 326
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
327

Michel Kaempf's avatar
Michel Kaempf committed
328 329 330
    /*
     * Initialize shared resources and libraries
     */
331 332 333
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
334
    {
Henri Fallon's avatar
 
Henri Fallon committed
335 336 337
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
338
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
339
    }
340

Michel Kaempf's avatar
Michel Kaempf committed
341
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
342
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
343
     */
344
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
345
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
346 347 348
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
349
    else
350
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        /*
         * Set signal handling policy for all threads
         */
        InitSignalHandler();

        /*
         * This is the main loop
         */
        p_main->p_intf->pf_run( p_main->p_intf );

        /*
         * Finished, destroy the interface
         */
        intf_Destroy( p_main->p_intf );

        /*
         * Go back into channel 0 which is the network
         */
369 370
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
371 372 373
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
374
    }
Michel Kaempf's avatar
Michel Kaempf committed
375

Sam Hocevar's avatar
 
Sam Hocevar committed
376
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
377
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
378
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
379 380
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
381
    module_EndBank();
382 383 384 385

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
386
    intf_PlaylistDestroy( p_main->p_playlist );
387 388 389 390

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
391
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
Sam Hocevar's avatar
 
Sam Hocevar committed
392
    system_End();
393 394
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
395

Michel Kaempf's avatar
Michel Kaempf committed
396 397 398
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
399
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
400
    intf_MsgDestroy();
401

Sam Hocevar's avatar
 
Sam Hocevar committed
402 403 404 405
#if defined( PTH_INIT_IN_PTH_H )
    pth_kill( );
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
406
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
407 408
}

409
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
410
 * main_GetIntVariable: get the int value of an environment variable
411
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
412
 * This function is used to read some default parameters in modules.
413
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
414 415
int main_GetIntVariable( char *psz_name, int i_default )
{
416 417 418
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
419 420 421

    psz_env = getenv( psz_name );
    if( psz_env )
422
    {
423 424 425 426
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
427
        }
428
    }
Vincent Seguin's avatar
Vincent Seguin committed
429 430 431
    return( i_default );
}

432
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
433
 * main_GetPszVariable: get the string value of an environment variable
434
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
435
 * This function is used to read some default parameters in modules.
436
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
437 438 439 440 441 442 443
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
444
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
445
    }
446
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
447 448
}

449
/*****************************************************************************
450
 * main_PutPszVariable: set the string value of an environment variable
451
 *****************************************************************************
452 453 454
 * 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.
455
 *****************************************************************************/
456 457 458 459 460 461 462
void main_PutPszVariable( char *psz_name, char *psz_value )
{
    char *psz_env;

    psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
    if( psz_env == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
463 464
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
465 466 467 468 469 470
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
471
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
472
        }
473 474 475
    }
}

476
/*****************************************************************************
477
 * main_PutIntVariable: set the integer value of an environment variable
478
 *****************************************************************************
479 480 481
 * 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.
482
 *****************************************************************************/
483 484
void main_PutIntVariable( char *psz_name, int i_value )
{
485
    char psz_value[ 256 ];                               /* buffer for value */
486

487
    sprintf( psz_value, "%d", i_value );
488
    main_PutPszVariable( psz_name, psz_value );
489 490
}

Michel Kaempf's avatar
Michel Kaempf committed
491 492
/* following functions are local */

493
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
494
 * GetConfiguration: parse command line
495
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
496 497
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
498
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
499 500
 * stage, but most structures are not allocated, so only environment should
 * be used.
501
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
502
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
503
{
Sam Hocevar's avatar
 
Sam Hocevar committed
504 505
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
506 507

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
508
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
509
    p_main->ppsz_argv = ppsz_argv;
510
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
511

Henri Fallon's avatar
 
Henri Fallon committed
512 513
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
514

515
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
516

517 518
    p_main->p_channel = NULL;

519
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
520 521
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
522
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
523
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
524
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
525
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
526 527 528
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
529
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
530 531 532
        }
    }

533
#ifdef SYS_DARWIN1_3
Sam Hocevar's avatar
 
Sam Hocevar committed
534 535 536
    /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
     * is the PSN - process serial number (a unique PID-ish thingie)
     * still ok for real Darwin & when run from command line */
Sam Hocevar's avatar
 
Sam Hocevar committed
537 538
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
539
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
540 541 542 543 544 545 546 547 548
        /* GDMF!... I can't do this or else the MacOSX window server will
         * not pick up the PSN and not register the app and we crash...
         * hence the following kludge otherwise we'll get confused w/ argv[1]
         * being an input file name */
#if 0
        ppsz_argv[ 1 ] = NULL;
#endif
        *pi_argc = *pi_argc - 1;
        pi_argc--;
549 550 551 552
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
553
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
554
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
555 556
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
557
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
558
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
559
        {
560 561
        /* General/common options */
        case 'h':                                              /* -h, --help */
562 563 564
            Usage( SHORT_HELP );
            return( -1 );
            break;
565
        case 'H':                                          /* -H, --longhelp */
566
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
567 568
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
569
        case OPT_VERSION:                                       /* --version */
Vincent Seguin's avatar
Vincent Seguin committed
570 571
            Version();
            return( -1 );
572
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
573
        case 'v':                                           /* -v, --verbose */
574
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
575
            break;
Michel Kaempf's avatar
Michel Kaempf committed
576

Sam Hocevar's avatar
 
Sam Hocevar committed
577
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
578
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
579 580 581
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
582 583
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
584 585
            break;

Michel Kaempf's avatar
Michel Kaempf committed
586
        /* Audio options */
587
        case OPT_NOAUDIO:                                       /* --noaudio */
588
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
589
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
590
        case 'A':                                              /* -A, --aout */
591 592
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
593
        case OPT_STEREO:                                         /* --stereo */
594
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
595
            break;
596
        case OPT_MONO:                                             /* --mono */
597
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
598
            break;
599
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
600
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
601
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
602 603 604 605 606 607
	case OPT_DOWNMIX:                                       /* --downmix */
            main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
            break;
        case OPT_IMDCT:                                           /* --imdct */
            main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
608 609

        /* Video options */
610
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
611
            p_main->b_video = 0;
612
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
613
        case 'V':                                              /* -V, --vout */
614 615
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
616
        case OPT_DISPLAY:                                       /* --display */
617
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
618 619
            break;
        case OPT_WIDTH:                                           /* --width */
620
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
621 622 623 624 625
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
626
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
627 628
            break;
        case OPT_COLOR:                                           /* --color */
629
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
630
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
631
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
632 633
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
634
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
635 636
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
637
        case OPT_MOTION:                                         /* --motion */
Sam Hocevar's avatar
 
Sam Hocevar committed
638 639
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
640
        case OPT_IDCT:                                             /* --idct */
Sam Hocevar's avatar
 
Sam Hocevar committed
641 642
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
643 644 645
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
646

647
        /* DVD options */
648 649 650 651 652 653
        case 't':
            main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
            break;
        case 'T':
            main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
            break;
654 655 656
        case 'u':
            main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
            break;
657
        case 'a':
658
            if ( ! strcmp(optarg, "ac3") )
Sam Hocevar's avatar
 
Sam Hocevar committed
659
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
660
            else if ( ! strcmp(optarg, "lpcm") )
Sam Hocevar's avatar
 
Sam Hocevar committed
661
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
662
            else if ( ! strcmp(optarg, "mpeg") )
Sam Hocevar's avatar
 
Sam Hocevar committed
663
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
664
            else
Sam Hocevar's avatar
 
Sam Hocevar committed
665
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
666 667
            break;
        case 'c':
Sam Hocevar's avatar
 
Sam Hocevar committed
668
            main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
669 670
            break;
        case 's':
Sam Hocevar's avatar
 
Sam Hocevar committed
671
            main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
672 673
            break;

674
        /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
675
        case OPT_INPUT:                                           /* --input */
Sam Hocevar's avatar
 
Sam Hocevar committed
676 677
            main_PutPszVariable( INPUT_METHOD_VAR, optarg );
            break;
Henri Fallon's avatar
 
Henri Fallon committed
678
        case OPT_CHANNELS:                                     /* --channels */
679
            main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
680 681
            break;
        case OPT_SERVER:                                         /* --server */
682
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
683 684
            break;
        case OPT_PORT:                                             /* --port */
685
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
686
            break;
687
        case OPT_BROADCAST:                                   /* --broadcast */
688 689
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
690
            break;
691

692
        /* Synchro options */
693
        case OPT_SYNCHRO:                                      
694 695
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;
696
            
Michel Kaempf's avatar
Michel Kaempf committed
697
        /* Internal error: unknown option */
698
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
699
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
700 701
            intf_ErrMsg( "intf error: unknown option `%s'",
                         ppsz_argv[optind - 1] );
702
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
703 704 705 706
            return( EINVAL );
            break;
        }
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
707 708 709 710 711 712

    if( p_main->i_warning_level < 0 )
    {
        p_main->i_warning_level = 0;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
713 714 715 716 717 718 719 720 721 722 723
    return( 0 );
}

/*****************************************************************************
 * GetFilenames: parse command line options which are not flags
 *****************************************************************************
 * Parse command line for input files.
 *****************************************************************************/
static int GetFilenames( int i_argc, char *ppsz_argv[] )
{
    int i_opt;
Michel Kaempf's avatar
Michel Kaempf committed
724

Sam Hocevar's avatar
 
Sam Hocevar committed
725
    /* We assume that the remaining parameters are filenames */
Vincent Seguin's avatar
Vincent Seguin committed
726 727
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
728 729
        intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
                          ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
730
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
731

Michel Kaempf's avatar
Michel Kaempf committed
732 733 734
    return( 0 );
}

735
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
736
 * Usage: print program usage
737
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
738
 * Print a short inline help. Message interface is initialized at this stage.
739
 *****************************************************************************/
740
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
741
{
Vincent Seguin's avatar
Vincent Seguin committed
742
    /* Usage */
Sam Hocevar's avatar
 
Sam Hocevar committed
743 744
    intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
                 p_main->psz_arg0 );
745 746 747

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
748 749
        intf_MsgImm( "Try `%s --help' for more information.",
                     p_main->psz_arg0 );
750 751 752
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
753
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
754
    intf_MsgImm( "\nOptions:"
Sam Hocevar's avatar
 
Sam Hocevar committed
755
          "\n  -I, --intf <module>            \tinterface method"
Sam Hocevar's avatar
 
Sam Hocevar committed
756
          "\n  -v, --verbose                  \tverbose mode (cumulative)"
Sam Hocevar's avatar
 
Sam Hocevar committed
757
          "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
758
          "\n      --noaudio                  \tdisable audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
759
          "\n  -A, --aout <module>            \taudio output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
760
          "\n      --stereo, --mono           \tstereo/mono audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
761
          "\n      --spdif                    \tAC3 pass-through mode"
Sam Hocevar's avatar
 
Sam Hocevar committed
762 763
          "\n      --downmix <module>         \tAC3 downmix method"
          "\n      --imdct <module>           \tAC3 IMDCT method"
Sam Hocevar's avatar