main.c 37.5 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.99 2001/05/31 01:37:08 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 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
#ifdef HAVE_UNISTD_H
Sam Hocevar's avatar
 
Sam Hocevar committed
55
#include <unistd.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
56 57
#endif

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

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

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

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

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

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

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

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

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

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

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

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

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
107 108 109 110 111 112
#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
113

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

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

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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
145
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
146
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
147 148
    {   "warning",          1,          0,      OPT_WARNING },

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

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

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

187 188
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
189 190 191 192
    {   0,                  0,          0,      0 }
};

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

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

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

Michel Kaempf's avatar
Michel Kaempf committed
213
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
214 215
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
216

Sam Hocevar's avatar
 
Sam Hocevar committed
217 218
static int  CPUCapabilities         ( void );

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

Sam Hocevar's avatar
 
Sam Hocevar committed
236 237 238 239
    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
240 241

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
273 274
    intf_MsgImm( COPYRIGHT_MESSAGE );

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

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

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

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

Michel Kaempf's avatar
Michel Kaempf committed
308 309 310
    /*
     * Initialize shared resources and libraries
     */
311 312 313
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
314
    {
Henri Fallon's avatar
 
Henri Fallon committed
315 316 317
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
318
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
319
    }
320

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

Sam Hocevar's avatar
 
Sam Hocevar committed
356
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
357
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
358
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
359 360
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
361
    module_EndBank();
362 363 364 365

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
366
    intf_PlaylistDestroy( p_main->p_playlist );
367 368 369 370

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
371
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
Sam Hocevar's avatar
 
Sam Hocevar committed
372
    system_End();
373 374
#endif

Michel Kaempf's avatar
Michel Kaempf committed
375 376 377
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
378
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
379
    intf_MsgDestroy();
380

Sam Hocevar's avatar
 
Sam Hocevar committed
381
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
382 383
}

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

    psz_env = getenv( psz_name );
    if( psz_env )
397
    {
398 399 400 401
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
402
        }
403
    }
Vincent Seguin's avatar
Vincent Seguin committed
404 405 406
    return( i_default );
}

407
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
408
 * main_GetPszVariable: get the string value of an environment variable
409
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
410
 * This function is used to read some default parameters in modules.
411
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
412 413 414 415 416 417 418
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
419
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
420
    }
421
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
422 423
}

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

451
/*****************************************************************************
452
 * main_PutIntVariable: set the integer value of an environment variable
453
 *****************************************************************************
454 455 456
 * 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.
457
 *****************************************************************************/
458 459
void main_PutIntVariable( char *psz_name, int i_value )
{
460
    char psz_value[ 256 ];                               /* buffer for value */
461

462
    sprintf( psz_value, "%d", i_value );
463
    main_PutPszVariable( psz_name, psz_value );
464 465
}

Michel Kaempf's avatar
Michel Kaempf committed
466 467
/* following functions are local */

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

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
483
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
484
    p_main->ppsz_argv = ppsz_argv;
485
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
486

Henri Fallon's avatar
 
Henri Fallon committed
487 488
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
489

490
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
491

492 493
    p_main->p_channel = NULL;

494
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
495 496
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
497
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
498
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
499
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
500
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
501 502 503
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
504
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
505 506 507
        }
    }

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
552
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
553
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
554 555 556
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
557 558
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
559 560
            break;

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

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

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

649
        /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
650
        case OPT_INPUT:                                           /* --input */
Sam Hocevar's avatar
 
Sam Hocevar committed
651 652
            main_PutPszVariable( INPUT_METHOD_VAR, optarg );
            break;
Henri Fallon's avatar
 
Henri Fallon committed
653
        case OPT_CHANNELS:                                     /* --channels */
654
            main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
655 656
            break;
        case OPT_SERVER:                                         /* --server */
657
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
658 659
            break;
        case OPT_PORT:                                             /* --port */
660
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
661
            break;
662
        case OPT_BROADCAST:                                   /* --broadcast */
663 664
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
665
            break;
666

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
688 689 690 691 692 693 694 695 696 697 698
    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
699

Sam Hocevar's avatar
 
Sam Hocevar committed
700
    /* We assume that the remaining parameters are filenames */
Vincent Seguin's avatar
Vincent Seguin committed
701 702
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
703 704
        intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
                          ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
705
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
706

Michel Kaempf's avatar
Michel Kaempf committed
707 708 709
    return( 0 );
}

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

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
723 724
        intf_MsgImm( "Try `%s --help' for more information.",
                     p_main->psz_arg0 );
725 726 727
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
728
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
729
    intf_MsgImm( "\nOptions:"
Sam Hocevar's avatar
 
Sam Hocevar committed
730
          "\n  -I, --intf <module>            \tinterface method"
Sam Hocevar's avatar
 
Sam Hocevar committed
731
          "\n  -v, --verbose                  \tverbose mode (cumulative)"
Sam Hocevar's avatar
 
Sam Hocevar committed
732
          "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
733
          "\n      --noaudio                  \tdisable audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
734
          "\n  -A, --aout <module>            \taudio output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
735
          "\n      --stereo, --mono           \tstereo/mono audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
736
          "\n      --spdif                    \tAC3 pass-through mode"
Sam Hocevar's avatar
 
Sam Hocevar committed
737 738
          "\n      --downmix <module>         \tAC3 downmix method"
          "\n      --imdct <module>           \tAC3 IMDCT method"
Sam Hocevar's avatar
 
Sam Hocevar committed
739 740
          "\n"
          "\n      --novideo                  \tdisable video"
Sam Hocevar's avatar
 
Sam Hocevar committed
741
          "\n  -V, --vout <module>            \tvideo output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
742 743 744 745 746 747 748 749 750 751 752
          "\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"
753 754
          "\n  -t, --dvdtitle <num>           \tchoose DVD title"
          "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
755
          "\n  -u, --dvdangle <num>           \tchoose DVD angle"
Sam Hocevar's avatar
 
Sam Hocevar committed
756 757 758 759 760
          "\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"
761
          "\n      --channels                 \tenable channels"
Sam Hocevar's avatar
 
Sam Hocevar committed
762 763 764 765