main.c 45.7 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.114 2001/09/25 11:46:14 massiot Exp $
8
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
9 10
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
11 12 13 14 15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
16
 * 
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

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

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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
55
#ifdef HAVE_UNISTD_H
Sam Hocevar's avatar
 
Sam Hocevar committed
56 57 58
#   include <unistd.h>
#elif defined( _MSC_VER ) && defined( _WIN32 )
#   include <io.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
59 60
#endif

61 62 63
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                  /* getenv(), strtol(),  */
#include <string.h>                                            /* strerror() */
gbazin's avatar
 
gbazin committed
64 65
#include <fcntl.h>                                       /* open(), O_WRONLY */
#include <sys/stat.h>                                             /* S_IREAD */
Vincent Seguin's avatar
Vincent Seguin committed
66

Michel Kaempf's avatar
Michel Kaempf committed
67 68
#include "config.h"
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
69
#include "debug.h"
70
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
71
#include "mtime.h"
72
#include "tests.h"                                              /* TestCPU() */
Sam Hocevar's avatar
 
Sam Hocevar committed
73
#include "modules.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
74

75 76
#include "stream_control.h"
#include "input_ext-intf.h"
77

Vincent Seguin's avatar
Vincent Seguin committed
78
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
79
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
80
#include "interface.h"
81

Vincent Seguin's avatar
Vincent Seguin committed
82
#include "audio_output.h"
83

Sam Hocevar's avatar
 
Sam Hocevar committed
84 85 86
#include "video.h"
#include "video_output.h"

87
#ifdef SYS_BEOS
Sam Hocevar's avatar
 
Sam Hocevar committed
88 89 90
#   include "beos_specific.h"
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
91
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
92
#   include "darwin_specific.h"
93 94
#endif

Henri Fallon's avatar
 
Henri Fallon committed
95 96
#include "netutils.h"                                 /* network_ChannelJoin */

Vincent Seguin's avatar
Vincent Seguin committed
97
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
98

99
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
100 101
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
102
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
103 104 105 106

/* 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
107 108
#define OPT_STEREO              151
#define OPT_MONO                152
109
#define OPT_SPDIF               153
Michel Kaempf's avatar
Michel Kaempf committed
110 111

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
112 113 114 115 116 117
#define OPT_DISPLAY             161
#define OPT_WIDTH               162
#define OPT_HEIGHT              163
#define OPT_COLOR               164
#define OPT_FULLSCREEN          165
#define OPT_OVERLAY             166
Sam Hocevar's avatar
 
Sam Hocevar committed
118 119
#define OPT_XVADAPTOR           167
#define OPT_SMP                 168
Michel Kaempf's avatar
Michel Kaempf committed
120

Henri Fallon's avatar
 
Henri Fallon committed
121
#define OPT_CHANNELS            170
122 123
#define OPT_SERVER              171
#define OPT_PORT                172
124
#define OPT_BROADCAST           173
125

Sam Hocevar's avatar
 
Sam Hocevar committed
126 127 128 129
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
130 131
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
132

Sam Hocevar's avatar
 
Sam Hocevar committed
133 134
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
135
#define OPT_VERSION             192
gbazin's avatar
 
gbazin committed
136
#define OPT_STDOUT              193
137

138
/* Usage fashion */
139 140 141
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
142

Sam Hocevar's avatar
 
Sam Hocevar committed
143 144 145 146 147 148 149 150 151 152
/* Needed for x86 CPU capabilities detection */
#define cpuid( a )                 \
    asm volatile ( "cpuid"         \
                 : "=a" ( i_eax ), \
                   "=b" ( i_ebx ), \
                   "=c" ( i_ecx ), \
                   "=d" ( i_edx )  \
                 : "a"  ( a )      \
                 : "cc" );

Michel Kaempf's avatar
Michel Kaempf committed
153 154
/* Long options */
static const struct option longopts[] =
155 156
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
157 158

    /* General/common options */
159
    {   "help",             0,          0,      'h' },
160
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
161
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
162

Sam Hocevar's avatar
 
Sam Hocevar committed
163
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
164
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
165
    {   "warning",          1,          0,      OPT_WARNING },
gbazin's avatar
 
gbazin committed
166
    {   "stdout",           1,          0,      OPT_STDOUT },
Sam Hocevar's avatar
 
Sam Hocevar committed
167

Michel Kaempf's avatar
Michel Kaempf committed
168
    /* Audio options */
169
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
170
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
171
    {   "stereo",           0,          0,      OPT_STEREO },
172
    {   "mono",             0,          0,      OPT_MONO },
173
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
174 175
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
Michel Kaempf's avatar
Michel Kaempf committed
176 177

    /* Video options */
178
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
179
    {   "vout",             1,          0,      'V' },
180
    {   "display",          1,          0,      OPT_DISPLAY },
181
    {   "width",            1,          0,      OPT_WIDTH },
182 183 184
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
185
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
186
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
187
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
188 189
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Sam Hocevar's avatar
 
Sam Hocevar committed
190
    {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
191
    {   "smp",              1,          0,      OPT_SMP },
Michel Kaempf's avatar
Michel Kaempf committed
192

193
    /* DVD options */
194 195
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
196
    {   "dvdangle",         1,          0,      'u' },
197 198 199 200
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
    
201
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
202
    {   "input",            1,          0,      OPT_INPUT },
203 204
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Henri Fallon's avatar
 
Henri Fallon committed
205
    {   "broadcast",        1,          0,      OPT_BROADCAST },
206
    {   "channels",         0,          0,      OPT_CHANNELS },
Michel Kaempf's avatar
Michel Kaempf committed
207

208 209
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
210 211 212 213
    {   0,                  0,          0,      0 }
};

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

216
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
217 218
 * Global variable program_data - these are the only ones, see main.h and
 * modules.h
219
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
220 221 222 223
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
224

225
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
226
 * Local prototypes
227
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
228
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
229
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
230
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
231
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
232
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
233

Michel Kaempf's avatar
Michel Kaempf committed
234
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
235 236
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
237
static void InstructionSignalHandler( int i_signal );
Sam Hocevar's avatar
 
Sam Hocevar committed
238
static int  CPUCapabilities         ( void );
gbazin's avatar
 
gbazin committed
239 240 241 242

static int  RedirectSTDOUT          ( void );
static void ShowConsole             ( void );

243 244
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
245

246
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
247
 * main: parse command line, start interface and spawn threads
248
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
249 250
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
251
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
252
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
253
 *      -closing of audio output device and some global modules
254
 * On error, the spawned threads are canceled, and the open devices closed.
255
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
256 257
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
258 259 260 261
    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;
262

Sam Hocevar's avatar
 
Sam Hocevar committed
263 264 265 266
    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
267

Sam Hocevar's avatar
 
Sam Hocevar committed
268
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
269
     * Initialize threads system
Sam Hocevar's avatar
 
Sam Hocevar committed
270
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
271
    vlc_threads_init( );
Sam Hocevar's avatar
 
Sam Hocevar committed
272

Sam Hocevar's avatar
 
Sam Hocevar committed
273
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
274
     * Test if our code is likely to run on this CPU 
Sam Hocevar's avatar
 
Sam Hocevar committed
275 276 277
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
    
278 279 280
#if defined( __pentium__ ) || defined( __pentiumpro__ )
    if( ! TestCPU( CPU_CAPABILITY_586 ) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
281 282
        fprintf( stderr, "error: this program needs a Pentium CPU,\n"
                         "please try a version without Pentium support\n" );
283 284 285 286
        return( 1 );
    }
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
287 288 289
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
290
#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
Sam Hocevar's avatar
 
Sam Hocevar committed
291
    system_Init( &i_argc, ppsz_argv, ppsz_env );
gbazin's avatar
 
gbazin committed
292 293
#elif defined( WIN32 )
    _fmode = _O_BINARY;   /* sets the default file-translation mode on Win32 */
294
#endif
295 296 297 298

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
299
    p_main->p_msg = intf_MsgCreate();
300
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
301
    {
302
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
303
                 strerror(errno) );
304
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
305
    }
306

Sam Hocevar's avatar
 
Sam Hocevar committed
307 308
    intf_MsgImm( COPYRIGHT_MESSAGE );

309
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
310
     * Read configuration
311
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
312
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
313
    {
Vincent Seguin's avatar
Vincent Seguin committed
314
        intf_MsgDestroy();
315
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
316
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
317

gbazin's avatar
 
gbazin committed
318 319 320 321 322 323
    /*
     * Redirect the standard output if required by the user, and on Win32 we
     * also open a console to display the debug messages.
     */
    RedirectSTDOUT();

324
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
325
     * Initialize playlist and get commandline files
326
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
327
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
328
    if( !p_main->p_playlist )
329
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
330
        intf_ErrMsg( "playlist error: playlist initialization failed" );
331 332
        intf_MsgDestroy();
        return( errno );
333
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
334
    intf_PlaylistInit( p_main->p_playlist );
335 336

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
337
     * Get input filenames given as commandline arguments
338
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
339
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
340

Sam Hocevar's avatar
 
Sam Hocevar committed
341
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
342
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
343
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
344
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
345 346
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
347

Michel Kaempf's avatar
Michel Kaempf committed
348 349 350
    /*
     * Initialize shared resources and libraries
     */
351 352 353
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
354
    {
Henri Fallon's avatar
 
Henri Fallon committed
355 356 357
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
358
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
359
    }
360

Michel Kaempf's avatar
Michel Kaempf committed
361
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
362
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
363
     */
364
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
365
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
366 367 368
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
369
    else
370
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        /*
         * 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
         */
389 390
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
391 392 393
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
394
    }
Michel Kaempf's avatar
Michel Kaempf committed
395

Sam Hocevar's avatar
 
Sam Hocevar committed
396
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
397
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
398
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
399 400
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
401
    module_EndBank();
402 403 404 405

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
406
    intf_PlaylistDestroy( p_main->p_playlist );
407 408 409 410

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
411
#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
Sam Hocevar's avatar
 
Sam Hocevar committed
412
    system_End();
413 414
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
415

Michel Kaempf's avatar
Michel Kaempf committed
416 417 418
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
419
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
420
    intf_MsgDestroy();
421

Sam Hocevar's avatar
 
Sam Hocevar committed
422 423 424 425
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
426

Sam Hocevar's avatar
 
Sam Hocevar committed
427
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
428 429
}

430
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
431
 * main_GetIntVariable: get the int value of an environment variable
432
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
433
 * This function is used to read some default parameters in modules.
434
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
435 436
int main_GetIntVariable( char *psz_name, int i_default )
{
437 438 439
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
440 441 442

    psz_env = getenv( psz_name );
    if( psz_env )
443
    {
444 445 446 447
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
448
        }
449
    }
Vincent Seguin's avatar
Vincent Seguin committed
450 451 452
    return( i_default );
}

453
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
454
 * main_GetPszVariable: get the string value of an environment variable
455
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
456
 * This function is used to read some default parameters in modules.
457
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
458 459 460 461 462 463 464
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
465
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
466
    }
467
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
468 469
}

470
/*****************************************************************************
471
 * main_PutPszVariable: set the string value of an environment variable
472
 *****************************************************************************
473 474 475
 * 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.
476
 *****************************************************************************/
477 478 479 480 481 482 483
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
484 485
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
486 487 488 489 490 491
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
492
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
493
        }
494 495 496
    }
}

497
/*****************************************************************************
498
 * main_PutIntVariable: set the integer value of an environment variable
499
 *****************************************************************************
500 501 502
 * 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.
503
 *****************************************************************************/
504 505
void main_PutIntVariable( char *psz_name, int i_value )
{
506
    char psz_value[ 256 ];                               /* buffer for value */
507

508
    sprintf( psz_value, "%d", i_value );
509
    main_PutPszVariable( psz_name, psz_value );
510 511
}

Michel Kaempf's avatar
Michel Kaempf committed
512 513
/* following functions are local */

514
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
515
 * GetConfiguration: parse command line
516
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
517 518
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
519
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
520 521
 * stage, but most structures are not allocated, so only environment should
 * be used.
522
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
523
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
524
{
Sam Hocevar's avatar
 
Sam Hocevar committed
525 526
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
527 528

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
529
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
530
    p_main->ppsz_argv = ppsz_argv;
531
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
532

Henri Fallon's avatar
 
Henri Fallon committed
533 534
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
535

536
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
537

538 539
    p_main->p_channel = NULL;

540
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
541 542
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
543
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
544
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
545
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
546
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
547 548 549
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
550
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
551 552 553
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
554
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
555 556 557
    /* 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
558 559
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
560
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
561 562 563 564 565 566 567 568 569
        /* 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--;
570 571 572 573
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
574
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
575
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
576 577
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
578
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
579
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
580
        {
581 582
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
583 584
            ShowConsole();
            RedirectSTDOUT();
585
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
586 587 588 589 590 591 592 593 594
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
            if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
                                                 INTF_STDOUT_DEFAULT ) ) == 0 )
            {
                /* No stdout redirection has been asked for */
                intf_MsgImm( "\nPress the RETURN key to continue..." );
                getchar();
            }
#endif
595 596
            return( -1 );
            break;
597
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
598 599
            ShowConsole();
            RedirectSTDOUT();
600
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
601 602 603 604 605 606 607 608 609
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
            if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
                                                 INTF_STDOUT_DEFAULT ) ) == 0 )
            {
                /* No stdout redirection has been asked for */
                intf_MsgImm( "\nPress the RETURN key to continue..." );
                getchar();
            }
#endif
Michel Kaempf's avatar
Michel Kaempf committed
610 611
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
612
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
613 614
            ShowConsole();
            RedirectSTDOUT();
Vincent Seguin's avatar
Vincent Seguin committed
615
            Version();
gbazin's avatar
 
gbazin committed
616 617 618 619 620 621 622 623 624
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
            if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
                                                 INTF_STDOUT_DEFAULT ) ) == 0 )
            {
                /* No stdout redirection has been asked for */
                intf_MsgImm( "\nPress the RETURN key to continue..." );
                getchar();
            }
#endif
Vincent Seguin's avatar
Vincent Seguin committed
625
            return( -1 );
626
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
627
        case 'v':                                           /* -v, --verbose */
628
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
629
            break;
Michel Kaempf's avatar
Michel Kaempf committed
630

Sam Hocevar's avatar
 
Sam Hocevar committed
631
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
632
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
633 634 635
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
636 637
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
638 639
            break;

gbazin's avatar
 
gbazin committed
640 641 642 643
        case OPT_STDOUT:                                         /* --stdout */
            main_PutPszVariable( INTF_STDOUT_VAR, optarg );
            break;

Michel Kaempf's avatar
Michel Kaempf committed
644
        /* Audio options */
645
        case OPT_NOAUDIO:                                       /* --noaudio */
646
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
647
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
648
        case 'A':                                              /* -A, --aout */
649 650
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
651
        case OPT_STEREO:                                         /* --stereo */
652
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
653
            break;
654
        case OPT_MONO:                                             /* --mono */
655
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
656
            break;
657
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
658
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
659
            break;
gbazin's avatar
 
gbazin committed
660
        case OPT_DOWNMIX:                                       /* --downmix */
Sam Hocevar's avatar
 
Sam Hocevar committed
661 662 663 664 665
            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
666 667

        /* Video options */
668
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
669
            p_main->b_video = 0;
670
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
671
        case 'V':                                              /* -V, --vout */
672 673
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
674
        case OPT_DISPLAY:                                       /* --display */
675
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
676 677
            break;
        case OPT_WIDTH:                                           /* --width */
678
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
679 680 681 682 683
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
684
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
685 686
            break;
        case OPT_COLOR:                                           /* --color */
687
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
688
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
689
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
690 691
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
692
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
693 694
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
695 696 697
        case OPT_XVADAPTOR:                                   /* --xvadaptor */
            main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
698
        case OPT_MOTION:                                         /* --motion */
Sam Hocevar's avatar
 
Sam Hocevar committed
699 700
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
701
        case OPT_IDCT:                                             /* --idct */
Sam Hocevar's avatar
 
Sam Hocevar committed
702 703
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
704 705 706
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
707 708 709
        case OPT_SMP:                                               /* --smp */
            main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
710

711
        /* DVD options */
712 713 714 715 716 717
        case 't':
            main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
            break;
        case 'T':
            main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
            break;
718 719 720
        case 'u':
            main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
            break;
721
        case 'a':
722
            if ( ! strcmp(optarg, "ac3") )
Sam Hocevar's avatar
 
Sam Hocevar committed
723
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
724
            else if ( ! strcmp(optarg, "lpcm") )
Sam Hocevar's avatar
 
Sam Hocevar committed
725
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
726
            else if ( ! strcmp(optarg, "mpeg") )
Sam Hocevar's avatar
 
Sam Hocevar committed
727
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
728
            else
Sam Hocevar's avatar
 
Sam Hocevar committed
729
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
730 731
            break;
        case 'c':
Sam Hocevar's avatar
 
Sam Hocevar committed
732
            main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
733 734
            break;
        case 's':
Sam Hocevar's avatar
 
Sam Hocevar committed
735
            main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
736 737
            break;

738
        /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
739
        case OPT_INPUT:                                           /* --input */
Sam Hocevar's avatar
 
Sam Hocevar committed
740 741
            main_PutPszVariable( INPUT_METHOD_VAR, optarg );
            break;
Henri Fallon's avatar
 
Henri Fallon committed
742
        case OPT_CHANNELS:                                     /* --channels */
743
            main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
744 745
            break;
        case OPT_SERVER:                                         /* --server */
746
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
747 748
            break;
        case OPT_PORT:                                             /* --port */
749
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
750
            break;
751
        case OPT_BROADCAST:                                   /* --broadcast */
752 753
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
754
            break;
755

756
        /* Synchro options */
757
        case OPT_SYNCHRO:                                      
758 759
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;
760
            
Michel Kaempf's avatar
Michel Kaempf committed
761
        /* Internal error: unknown option */
762
        case '?':