main.c 37 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.95 2001/05/15 16:19:42 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 49
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
50
#include <unistd.h>
51 52 53
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                  /* getenv(), strtol(),  */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
54

Michel Kaempf's avatar
Michel Kaempf committed
55 56
#include "config.h"
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
57
#include "debug.h"
58
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
59
#include "mtime.h"
60
#include "tests.h"                                              /* TestCPU() */
Sam Hocevar's avatar
 
Sam Hocevar committed
61
#include "modules.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
62

63 64
#include "stream_control.h"
#include "input_ext-intf.h"
65

Vincent Seguin's avatar
Vincent Seguin committed
66
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
67
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
68
#include "interface.h"
69

Vincent Seguin's avatar
Vincent Seguin committed
70
#include "audio_output.h"
71

Sam Hocevar's avatar
 
Sam Hocevar committed
72 73 74
#include "video.h"
#include "video_output.h"

75
#ifdef SYS_BEOS
Sam Hocevar's avatar
 
Sam Hocevar committed
76 77 78 79 80
#   include "beos_specific.h"
#endif

#ifdef SYS_DARWIN1_3
#   include "darwin_specific.h"
81 82
#endif

Henri Fallon's avatar
 
Henri Fallon committed
83 84
#include "netutils.h"                                 /* network_ChannelJoin */

Vincent Seguin's avatar
Vincent Seguin committed
85
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
86

87
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
88 89
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
90
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
91 92 93 94

/* 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
95 96
#define OPT_STEREO              151
#define OPT_MONO                152
97
#define OPT_SPDIF               153
Michel Kaempf's avatar
Michel Kaempf committed
98 99

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
100 101 102 103 104 105
#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
106

Henri Fallon's avatar
 
Henri Fallon committed
107
#define OPT_CHANNELS            170
108 109
#define OPT_SERVER              171
#define OPT_PORT                172
110
#define OPT_BROADCAST           173
111

Sam Hocevar's avatar
 
Sam Hocevar committed
112 113 114 115
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
116 117
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
118

Sam Hocevar's avatar
 
Sam Hocevar committed
119 120
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
121
#define OPT_VERSION             192
122

123
/* Usage fashion */
124 125 126
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
127

Michel Kaempf's avatar
Michel Kaempf committed
128 129
/* Long options */
static const struct option longopts[] =
130 131
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
132 133

    /* General/common options */
134
    {   "help",             0,          0,      'h' },
135
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
136
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
137

Sam Hocevar's avatar
 
Sam Hocevar committed
138
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
139
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
140 141
    {   "warning",          1,          0,      OPT_WARNING },

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

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

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

180 181
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
182 183 184 185
    {   0,                  0,          0,      0 }
};

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

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

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

Michel Kaempf's avatar
Michel Kaempf committed
206
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
207 208
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
Michel Kaempf's avatar
Michel Kaempf committed
209

Sam Hocevar's avatar
 
Sam Hocevar committed
210 211
static int  CPUCapabilities         ( void );

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

Sam Hocevar's avatar
 
Sam Hocevar committed
229 230 231 232
    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
233 234

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
266 267
    intf_MsgImm( COPYRIGHT_MESSAGE );

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

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

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
290
     * Get input filenames given as commandline arguments
291
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
292
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
293

Sam Hocevar's avatar
 
Sam Hocevar committed
294
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
295
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
296
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
297
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
298 299
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
300

Michel Kaempf's avatar
Michel Kaempf committed
301 302 303
    /*
     * Initialize shared resources and libraries
     */
Henri Fallon's avatar
 
Henri Fallon committed
304
    if( p_main->b_channels && network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
305
    {
Henri Fallon's avatar
 
Henri Fallon committed
306 307 308 309
        /* 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
310
    }
311

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

Sam Hocevar's avatar
 
Sam Hocevar committed
346
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
347
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
348
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
349 350
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
351
    module_EndBank();
352 353 354 355

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
356
    intf_PlaylistDestroy( p_main->p_playlist );
357 358 359 360

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
361
#if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
Sam Hocevar's avatar
 
Sam Hocevar committed
362
    system_End();
363 364
#endif

Michel Kaempf's avatar
Michel Kaempf committed
365 366 367
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
368
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
369
    intf_MsgDestroy();
370

Sam Hocevar's avatar
 
Sam Hocevar committed
371
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
372 373
}

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

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

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

    psz_env = getenv( psz_name );
    if( psz_env )
    {
409
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
410
    }
411
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
412 413
}

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

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

452
    sprintf( psz_value, "%d", i_value );
453
    main_PutPszVariable( psz_name, psz_value );
454 455
}

Michel Kaempf's avatar
Michel Kaempf committed
456 457
/* following functions are local */

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

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
473
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
474
    p_main->ppsz_argv = ppsz_argv;
475
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
476

Henri Fallon's avatar
 
Henri Fallon committed
477 478 479
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
    p_main->b_channels  = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
480

481
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
482

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

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

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

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

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

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

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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
676 677 678 679 680 681 682 683 684 685 686
    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
687

Sam Hocevar's avatar
 
Sam Hocevar committed
688
    /* We assume that the remaining parameters are filenames */
Vincent Seguin's avatar
Vincent Seguin committed
689 690
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
691 692
        intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
                          ppsz_argv[ i_opt ] );
Vincent Seguin's avatar
Vincent Seguin committed
693
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
694

Michel Kaempf's avatar
Michel Kaempf committed
695 696 697
    return( 0 );
}

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

    if( i_fashion == USAGE )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
711 712
        intf_MsgImm( "Try `%s --help' for more information.",
                     p_main->psz_arg0 );
713 714 715
        return;
    }

Vincent Seguin's avatar
Vincent Seguin committed
716
    /* Options */
Sam Hocevar's avatar
 
Sam Hocevar committed
717
    intf_MsgImm( "\nOptions:"
Sam Hocevar's avatar
 
Sam Hocevar committed
718
          "\n  -I, --intf <module>            \tinterface method"
Sam Hocevar's avatar
 
Sam Hocevar committed
719
          "\n  -v, --verbose                  \tverbose mode (cumulative)"
Sam Hocevar's avatar
 
Sam Hocevar committed
720
          "\n"
Sam Hocevar's avatar
 
Sam Hocevar committed
721
          "\n      --noaudio                  \tdisable audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
722
          "\n  -A, --aout <module>            \taudio output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
723
          "\n      --stereo, --mono           \tstereo/mono audio"
Sam Hocevar's avatar
 
Sam Hocevar committed
724
          "\n      --spdif                    \tAC3 pass-through mode"
Sam Hocevar's avatar
 
Sam Hocevar committed
725 726
          "\n      --downmix <module>         \tAC3 downmix method"
          "\n      --imdct <module>           \tAC3 IMDCT method"
Sam Hocevar's avatar
 
Sam Hocevar committed
727 728
          "\n"
          "\n      --novideo                  \tdisable video"
Sam Hocevar's avatar
 
Sam Hocevar committed
729
          "\n  -V, --vout <module>            \tvideo output method"
Sam Hocevar's avatar
 
Sam Hocevar committed
730 731 732 733 734 735 736 737 738 739 740
          "\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"
741 742
          "\n  -t, --dvdtitle <num>           \tchoose DVD title"
          "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
743
          "\n  -u, --dvdangle <num>           \tchoose DVD angle"
Sam Hocevar's avatar
 
Sam Hocevar committed
744 745 746 747 748
          "\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"
749
          "\n      --channels                 \tenable channels"
Sam Hocevar's avatar
 
Sam Hocevar committed
750 751 752 753 754 755
          "\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
756
          "\n      --version                  \toutput version information and exit" );
Michel Kaempf's avatar
Michel Kaempf committed
757

758 759 760
    if( i_fashion == SHORT_HELP )
        return;

Vincent Seguin's avatar
Vincent Seguin committed
761
    /* Interface parameters */
Sam Hocevar's avatar
 
Sam Hocevar committed
762
    intf_MsgImm( "\nInterface parameters:"
Sam Hocevar's avatar
 
Sam Hocevar committed
763
        "\n  " INTF_METHOD_VAR "=<method name>          \tinterface method"
Sam Hocevar's avatar
 
Sam Hocevar committed
764
        "\n  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script"
Sam Hocevar's avatar
 
Sam Hocevar committed
765
        "\n  " INTF_CHANNELS_VAR "=<filename>            \tchannels list" );