main.c 37.3 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3 4
 * main.c: main vlc source
 * Includes the main() function for vlc. Parses command line, start interface
 * and spawn threads.
5 6
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
7
 * $Id: main.c,v 1.97 2001/05/30 05:19:03 stef 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 34
#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
#include <stdio.h>                                              /* sprintf() */

Sam Hocevar's avatar
 
Sam Hocevar committed
35 36 37 38 39 40
#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
41 42
#endif

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
54
#include <unistd.h>
55 56 57
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                  /* getenv(), strtol(),  */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
58

Michel Kaempf's avatar
Michel Kaempf committed
59 60
#include "config.h"
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
61
#include "debug.h"
62
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
63
#include "mtime.h"
64
#include "tests.h"                                              /* TestCPU() */
Sam Hocevar's avatar
 
Sam Hocevar committed
65
#include "modules.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
66

67 68
#include "stream_control.h"
#include "input_ext-intf.h"
69

Vincent Seguin's avatar
Vincent Seguin committed
70
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
71
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
72
#include "interface.h"
73

Vincent Seguin's avatar
Vincent Seguin committed
74
#include "audio_output.h"
75

Sam Hocevar's avatar
 
Sam Hocevar committed
76 77 78
#include "video.h"
#include "video_output.h"

79
#ifdef SYS_BEOS
Sam Hocevar's avatar
 
Sam Hocevar committed
80 81 82 83 84
#   include "beos_specific.h"
#endif

#ifdef SYS_DARWIN1_3
#   include "darwin_specific.h"
85 86
#endif

Henri Fallon's avatar
 
Henri Fallon committed
87 88
#include "netutils.h"                                 /* network_ChannelJoin */

Vincent Seguin's avatar
Vincent Seguin committed
89
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
90

91
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
92 93
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
94
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
95 96 97 98

/* 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
99 100
#define OPT_STEREO              151
#define OPT_MONO                152
101
#define OPT_SPDIF               153
Michel Kaempf's avatar
Michel Kaempf committed
102 103

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
104 105 106 107 108 109
#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
110

Henri Fallon's avatar
 
Henri Fallon committed
111
#define OPT_CHANNELS            170
112 113
#define OPT_SERVER              171
#define OPT_PORT                172
114
#define OPT_BROADCAST           173
115

Sam Hocevar's avatar
 
Sam Hocevar committed
116 117 118 119
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
120 121
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
122

Sam Hocevar's avatar
 
Sam Hocevar committed
123 124
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
125
#define OPT_VERSION             192
126

127
/* Usage fashion */
128 129 130
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
131

Michel Kaempf's avatar
Michel Kaempf committed
132 133
/* Long options */
static const struct option longopts[] =
134 135
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
136 137

    /* General/common options */
138
    {   "help",             0,          0,      'h' },
139
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
140
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
141

Sam Hocevar's avatar
 
Sam Hocevar committed
142
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
143
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
144 145
    {   "warning",          1,          0,      OPT_WARNING },

Michel Kaempf's avatar
Michel Kaempf committed
146
    /* Audio options */
147
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
148
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
149
    {   "stereo",           0,          0,      OPT_STEREO },
150
    {   "mono",             0,          0,      OPT_MONO },
151
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
152 153
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
Michel Kaempf's avatar
Michel Kaempf committed
154 155

    /* Video options */
156
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
157
    {   "vout",             1,          0,      'V' },
158
    {   "display",          1,          0,      OPT_DISPLAY },
159
    {   "width",            1,          0,      OPT_WIDTH },
160 161 162
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
163
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
164
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
165
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
166 167
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Michel Kaempf's avatar
Michel Kaempf committed
168

169
    /* DVD options */
170 171
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
172
    {   "dvdangle",         1,          0,      'u' },
173 174 175 176
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
177
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
178
    {   "input",            1,          0,      OPT_INPUT },
Henri Fallon's avatar
 
Henri Fallon committed
179
    {   "channels",         0,          0,      OPT_CHANNELS },
180 181
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Henri Fallon's avatar
 
Henri Fallon committed
182
    {   "broadcast",        1,          0,      OPT_BROADCAST },
Michel Kaempf's avatar
Michel Kaempf committed
183

184 185
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
186 187 188 189
    {   0,                  0,          0,      0 }
};

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

192
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
193 194
 * Global variable program_data - these are the only ones, see main.h and
 * modules.h
195
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
196 197 198 199
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
200

201
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
202
 * Local prototypes
203
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
204
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
205
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
206
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
207
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
208
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
209

Michel Kaempf's avatar
Michel Kaempf committed
210
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
211 212
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
213

Sam Hocevar's avatar
 
Sam Hocevar committed
214 215
static int  CPUCapabilities         ( void );

216
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
217
 * main: parse command line, start interface and spawn threads
218
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
219 220
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
221
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
222
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
223
 *      -closing of audio output device and some global modules
224
 * On error, the spawned threads are canceled, and the open devices closed.
225
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
226 227
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
228 229 230 231
    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;
232

Sam Hocevar's avatar
 
Sam Hocevar committed
233 234 235 236
    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
237 238

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
239
     * Test if our code is likely to run on this CPU 
Sam Hocevar's avatar
 
Sam Hocevar committed
240 241 242
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
    
243 244 245
#if defined( __pentium__ ) || defined( __pentiumpro__ )
    if( ! TestCPU( CPU_CAPABILITY_586 ) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
246 247
        fprintf( stderr, "error: this program needs a Pentium CPU,\n"
                         "please try a version without Pentium support\n" );
248 249 250 251
        return( 1 );
    }
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
252 253 254 255 256
    /*
     * System specific initialization code
     */
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
    system_Init( &i_argc, ppsz_argv, ppsz_env );
257
#endif
258 259 260 261

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
262
    p_main->p_msg = intf_MsgCreate();
263
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
264
    {
265
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
266
                 strerror(errno) );
267
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
268
    }
269

Sam Hocevar's avatar
 
Sam Hocevar committed
270 271
    intf_MsgImm( COPYRIGHT_MESSAGE );

272
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
273
     * Read configuration
274
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
275
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
276
    {
Vincent Seguin's avatar
Vincent Seguin committed
277
        intf_MsgDestroy();
278
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
279
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
280

281
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
282
     * Initialize playlist and get commandline files
283
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
284
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
285
    if( !p_main->p_playlist )
286
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
287
        intf_ErrMsg( "playlist error: playlist initialization failed" );
288 289
        intf_MsgDestroy();
        return( errno );
290
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
291
    intf_PlaylistInit( p_main->p_playlist );
292 293

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
294
     * Get input filenames given as commandline arguments
295
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
296
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
297

Sam Hocevar's avatar
 
Sam Hocevar committed
298
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
299
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
300
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
301
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
302 303
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
304

Michel Kaempf's avatar
Michel Kaempf committed
305 306 307
    /*
     * Initialize shared resources and libraries
     */
Henri Fallon's avatar
 
Henri Fallon committed
308
    if( p_main->b_channels && network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
309
    {
Henri Fallon's avatar
 
Henri Fallon committed
310 311 312 313
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
        p_main->b_channels = 0;
Michel Kaempf's avatar
Michel Kaempf committed
314
    }
315

Michel Kaempf's avatar
Michel Kaempf committed
316
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
317
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
318
     */
319
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
320
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
321 322 323
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
324
    else
325
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        /*
         * 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
         */
        if( p_main->b_channels )
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
348
    }
Michel Kaempf's avatar
Michel Kaempf committed
349

Sam Hocevar's avatar
 
Sam Hocevar committed
350
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
351
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
352
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
353 354
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
355
    module_EndBank();
356 357 358 359

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
360
    intf_PlaylistDestroy( p_main->p_playlist );
361 362 363 364

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
365
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
Sam Hocevar's avatar
 
Sam Hocevar committed
366
    system_End();
367 368
#endif

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

Sam Hocevar's avatar
 
Sam Hocevar committed
375
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
376 377
}

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

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

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

    psz_env = getenv( psz_name );
    if( psz_env )
    {
413
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
414
    }
415
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
416 417
}

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

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

456
    sprintf( psz_value, "%d", i_value );
457
    main_PutPszVariable( psz_name, psz_value );
458 459
}

Michel Kaempf's avatar
Michel Kaempf committed
460 461
/* following functions are local */

462
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
463
 * GetConfiguration: parse command line
464
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
465 466
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
467
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
468 469
 * stage, but most structures are not allocated, so only environment should
 * be used.
470
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
471
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
472
{
Sam Hocevar's avatar
 
Sam Hocevar committed
473 474
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
475 476

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
477
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
478
    p_main->ppsz_argv = ppsz_argv;
479
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
480

Henri Fallon's avatar
 
Henri Fallon committed
481 482 483
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
    p_main->b_channels  = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
484

485
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
486

487
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
488 489
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
490
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
491
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
492
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
493
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
494 495 496
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
497
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
498 499 500
        }
    }

501
#ifdef SYS_DARWIN1_3
Sam Hocevar's avatar
 
Sam Hocevar committed
502 503 504
    /* 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
505 506
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
507
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
508 509 510 511 512 513 514 515 516
        /* 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--;
517 518 519 520
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
521
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
522
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
523 524
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
525
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
526
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
527
        {
528 529
        /* General/common options */
        case 'h':                                              /* -h, --help */
530 531 532
            Usage( SHORT_HELP );
            return( -1 );
            break;
533
        case 'H':                                          /* -H, --longhelp */
534
            Usage( LONG_HELP );
Michel Kaempf's avatar
Michel Kaempf committed
535 536
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
537
        case OPT_VERSION:                                       /* --version */
Vincent Seguin's avatar
Vincent Seguin committed
538 539
            Version();
            return( -1 );
540
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
541
        case 'v':                                           /* -v, --verbose */
542
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
543
            break;
Michel Kaempf's avatar
Michel Kaempf committed
544

Sam Hocevar's avatar
 
Sam Hocevar committed
545
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
546
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
547 548 549
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
550 551
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
552 553
            break;

Michel Kaempf's avatar
Michel Kaempf committed
554
        /* Audio options */
555
        case OPT_NOAUDIO:                                       /* --noaudio */
556
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
557
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
558
        case 'A':                                              /* -A, --aout */
559 560
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
561
        case OPT_STEREO:                                         /* --stereo */
562
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
563
            break;
564
        case OPT_MONO:                                             /* --mono */
565
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
566
            break;
567
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
568
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
569
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
570 571 572 573 574 575
	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
576 577

        /* Video options */
578
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
579
            p_main->b_video = 0;
580
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
581
        case 'V':                                              /* -V, --vout */
582 583
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
584
        case OPT_DISPLAY:                                       /* --display */
585
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
586 587
            break;
        case OPT_WIDTH:                                           /* --width */
588
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
589 590 591 592 593
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
594
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
595 596
            break;
        case OPT_COLOR:                                           /* --color */
597
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
598
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
599
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
600 601
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
602
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
603 604
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
605
        case OPT_MOTION:                                         /* --motion */
Sam Hocevar's avatar
 
Sam Hocevar committed
606 607
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
608
        case OPT_IDCT:                                             /* --idct */
Sam Hocevar's avatar
 
Sam Hocevar committed
609 610
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
611 612 613
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
614

615
        /* DVD options */
616 617 618 619 620 621
        case 't':
            main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
            break;
        case 'T':
            main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
            break;
622 623 624
        case 'u':
            main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
            break;
625
        case 'a':
626
            if ( ! strcmp(optarg, "ac3") )
Sam Hocevar's avatar
 
Sam Hocevar committed
627
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
628
            else if ( ! strcmp(optarg, "lpcm") )
Sam Hocevar's avatar
 
Sam Hocevar committed
629
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
630
            else if ( ! strcmp(optarg, "mpeg") )
Sam Hocevar's avatar
 
Sam Hocevar committed
631
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
632
            else
Sam Hocevar's avatar
 
Sam Hocevar committed
633
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
634 635
            break;
        case 'c':
Sam Hocevar's avatar
 
Sam Hocevar committed
636
            main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
637 638
            break;
        case 's':
Sam Hocevar's avatar
 
Sam Hocevar committed
639
            main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
640 641
            break;

642
        /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
643
        case OPT_INPUT:                                           /* --input */
Sam Hocevar's avatar
 
Sam Hocevar committed
644 645
            main_PutPszVariable( INPUT_METHOD_VAR, optarg );
            break;
Henri Fallon's avatar
 
Henri Fallon committed
646 647
        case OPT_CHANNELS:                                     /* --channels */
            p_main->b_channels = 1;
648
            main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
649 650
            break;
        case OPT_SERVER:                                         /* --server */
651
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
652 653
            break;
        case OPT_PORT:                                             /* --port */
654
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
655
            break;
656
        case OPT_BROADCAST:                                   /* --broadcast */
657 658
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
659
            break;
660

661
        /* Synchro options */
662
        case OPT_SYNCHRO:                                      
663 664
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;
665
            
Michel Kaempf's avatar
Michel Kaempf committed
666
        /* Internal error: unknown option */
667
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
668
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
669 670
            intf_ErrMsg( "intf error: unknown option `%s'",
                         ppsz_argv[optind - 1] );
671
            Usage( USAGE );
Michel Kaempf's avatar
Michel Kaempf committed
672 673 674 675
            return( EINVAL );
            break;
        }
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
676 677 678 679 680 681

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

Sam Hocevar's avatar
 
Sam Hocevar committed
682 683 684 685 686 687 688 689 690 691 692
    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
693

Sam Hocevar's avatar
 
Sam Hocevar committed
694
    /* We assume that the remaining parameters are filenames */
Vincent Seguin's avatar
Vincent Seguin committed
695 696
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
697 698
        intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
                          ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
699
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
700

Michel Kaempf's avatar
Michel Kaempf committed
701 702 703
    return( 0 );
}

704
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
705
 * Usage: print program usage
706
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
707
 * Print a short inline help. Message interface is initialized at this stage.
708
 *****************************************************************************/
709
static void Usage( int i_fashion )
Michel Kaempf's avatar
Michel Kaempf committed
710
{
Vincent Seguin's avatar
Vincent Seguin committed
711
    /* Usage */
Sam Hocevar's avatar
 
Sam Hocevar committed
712 713
    intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
                 p_main->psz_arg0 );
714 715 716

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
717 718
        intf_MsgImm( "Try `%s --help' for more information.",
                     p_main->psz_arg0 );
719 720 721
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
722
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
723
    intf_MsgImm( "\nOptions:"
Sam Hocevar's avatar
 
Sam Hocevar committed
724
          "\n  -I, --intf <module>            \tinterface method"
Sam Hocevar's avatar
 
Sam Hocevar committed
725
          "\n  -v, --verbose                  \tverbose mode (cumulative)"
Sam Hocevar's avatar
 
Sam Hocevar committed
726
          "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
727
          "\n      --noaudio                  \tdisable audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
728
          "\n  -A, --aout <module>            \taudio output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
729
          "\n      --stereo, --mono           \tstereo/mono audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
730
          "\n      --spdif                    \tAC3 pass-through mode"
Sam Hocevar's avatar
 
Sam Hocevar committed
731 732
          "\n      --downmix <module>         \tAC3 downmix method"
          "\n      --imdct <module>           \tAC3 IMDCT method"
Sam Hocevar's avatar
 
Sam Hocevar committed
733 734
          "\n"
          "\n      --novideo                  \tdisable video"
Sam Hocevar's avatar
 
Sam Hocevar committed
735
          "\n  -V, --vout <module>            \tvideo output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
736 737 738 739 740 741 742 743 744 745 746
          "\n      --display <display>        \tdisplay string"
          "\n      --width <w>, --height <h>  \tdisplay dimensions"
          "\n  -g, --grayscale                \tgrayscale output"
          "\n      --fullscreen               \tfullscreen output"
          "\n      --overlay                  \taccelerated display"
          "\n      --color                    \tcolor output"
          "\n      --motion <module>          \tmotion compensation method"
          "\n      --idct <module>            \tIDCT method"
          "\n      --yuv <module>             \tYUV method"
          "\n      --synchro <type>           \tforce synchro algorithm"
          "\n"
747 748
          "\n  -t, --dvdtitle <num>           \tchoose DVD title"
          "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
749
          "\n  -u, --dvdangle <num>           \tchoose DVD angle"
Sam Hocevar's avatar
 
Sam Hocevar committed
750 751 752 753 754
          "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
          "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
          "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
          "\n"
          "\n      --input                    \tinput method"
755
          "\n      --channels                 \tenable channels"
Sam Hocevar's avatar
 
Sam Hocevar committed
756 757 758 759 760 761
          "\n      --server <host>            \tvideo server address"
          "\n      --port <port>              \tvideo server port"
          "\n      --broadcast                \tlisten to a broadcast"
          "\n"
          "\n  -h, --help                     \tprint help and exit"
          "\n  -H, --longhelp                 \tprint long help and exit"
Sam Hocevar's avatar
 
Sam Hocevar committed
762
          "\n      --version                  \toutput version information and exit" );
Michel Kaempf's avatar
Michel Kaempf committed
763

764 765 766
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
767
    /* Interface parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed