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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
44
#ifdef SYS_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
110
#define OPT_VOLUME              154
Michel Kaempf's avatar
Michel Kaempf committed
111 112

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
113 114 115 116 117 118
#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
119 120
#define OPT_XVADAPTOR           167
#define OPT_SMP                 168
Michel Kaempf's avatar
Michel Kaempf committed
121

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

Sam Hocevar's avatar
 
Sam Hocevar committed
128 129 130 131
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
132 133
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
Sam Hocevar's avatar
 
Sam Hocevar committed
134 135
#define OPT_DVDCSS_METHOD       186
#define OPT_DVDCSS_VERBOSE      187
136

Sam Hocevar's avatar
 
Sam Hocevar committed
137 138
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
139
#define OPT_VERSION             192
gbazin's avatar
 
gbazin committed
140
#define OPT_STDOUT              193
141
#define OPT_STATS               194
142

143
/* Usage fashion */
144 145 146
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
147

Sam Hocevar's avatar
 
Sam Hocevar committed
148 149 150 151 152 153 154 155 156 157
/* 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
158 159
/* Long options */
static const struct option longopts[] =
160 161
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
162 163

    /* General/common options */
164
    {   "help",             0,          0,      'h' },
165
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
166
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
167

Sam Hocevar's avatar
 
Sam Hocevar committed
168
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
169
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
170
    {   "warning",          1,          0,      OPT_WARNING },
gbazin's avatar
 
gbazin committed
171
    {   "stdout",           1,          0,      OPT_STDOUT },
172
    {   "stats",            0,          0,      OPT_STATS },
Sam Hocevar's avatar
 
Sam Hocevar committed
173

Michel Kaempf's avatar
Michel Kaempf committed
174
    /* Audio options */
175
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
176
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
177
    {   "stereo",           0,          0,      OPT_STEREO },
178
    {   "mono",             0,          0,      OPT_MONO },
179
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
180 181
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
182
    {   "volume",           1,          0,      OPT_VOLUME },
Michel Kaempf's avatar
Michel Kaempf committed
183 184

    /* Video options */
185
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
186
    {   "vout",             1,          0,      'V' },
187
    {   "display",          1,          0,      OPT_DISPLAY },
188
    {   "width",            1,          0,      OPT_WIDTH },
189 190 191
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
192
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
193
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
194
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
195 196
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Sam Hocevar's avatar
 
Sam Hocevar committed
197
    {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
198
    {   "smp",              1,          0,      OPT_SMP },
Michel Kaempf's avatar
Michel Kaempf committed
199

200
    /* DVD options */
201 202
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
203
    {   "dvdangle",         1,          0,      'u' },
204 205 206
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
Sam Hocevar's avatar
 
Sam Hocevar committed
207 208
    {   "dvdcss-method",    1,          0,      OPT_DVDCSS_METHOD },
    {   "dvdcss-verbose",   1,          0,      OPT_DVDCSS_VERBOSE },
209
    
210
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
211
    {   "input",            1,          0,      OPT_INPUT },
212 213
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Henri Fallon's avatar
 
Henri Fallon committed
214
    {   "broadcast",        1,          0,      OPT_BROADCAST },
215
    {   "channels",         0,          0,      OPT_CHANNELS },
216
    {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
Michel Kaempf's avatar
Michel Kaempf committed
217

218 219
    /* Synchro options */
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Michel Kaempf's avatar
Michel Kaempf committed
220 221 222 223
    {   0,                  0,          0,      0 }
};

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

226
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
227 228
 * Global variable program_data - these are the only ones, see main.h and
 * modules.h
229
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
230 231 232 233
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
234

235
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
236
 * Local prototypes
237
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
238
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
239
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
240
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
241
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
242
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
243

Michel Kaempf's avatar
Michel Kaempf committed
244
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
245 246
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
247
static void InstructionSignalHandler( int i_signal );
Sam Hocevar's avatar
 
Sam Hocevar committed
248
static int  CPUCapabilities         ( void );
gbazin's avatar
 
gbazin committed
249 250 251 252

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

253 254
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
255

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

Sam Hocevar's avatar
 
Sam Hocevar committed
273 274 275 276
    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
277

Sam Hocevar's avatar
 
Sam Hocevar committed
278
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
279
     * Initialize threads system
Sam Hocevar's avatar
 
Sam Hocevar committed
280
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
281
    vlc_threads_init( );
Sam Hocevar's avatar
 
Sam Hocevar committed
282

Sam Hocevar's avatar
 
Sam Hocevar committed
283
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
284
     * Test if our code is likely to run on this CPU 
Sam Hocevar's avatar
 
Sam Hocevar committed
285 286 287
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
    
Sam Hocevar's avatar
 
Sam Hocevar committed
288 289 290
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
291
#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
Sam Hocevar's avatar
 
Sam Hocevar committed
292
    system_Init( &i_argc, ppsz_argv, ppsz_env );
293

gbazin's avatar
 
gbazin committed
294 295
#elif defined( WIN32 )
    _fmode = _O_BINARY;   /* sets the default file-translation mode on Win32 */
296 297 298 299 300 301

#elif defined( SYS_LINUX )
#   ifdef DEBUG
    /* Activate malloc checking routines to detect heap corruptions. */
    main_PutIntVariable( "MALLOC_CHECK_", 2 );
#   endif
302
#endif
303 304 305 306

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
307
    p_main->p_msg = intf_MsgCreate();
308
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
309
    {
310
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
311
                 strerror(errno) );
312
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
313
    }
314

315
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
Sam Hocevar's avatar
 
Sam Hocevar committed
316

317
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
318
     * Read configuration
319
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
320
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
321
    {
Vincent Seguin's avatar
Vincent Seguin committed
322
        intf_MsgDestroy();
323
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
324
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
325

gbazin's avatar
 
gbazin committed
326 327 328 329 330 331
    /*
     * Redirect the standard output if required by the user, and on Win32 we
     * also open a console to display the debug messages.
     */
    RedirectSTDOUT();

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    if( p_main->b_stats )
    {
        char          p_capabilities[200];
        p_capabilities[0] = '\0';

#define PRINT_CAPABILITY( capability, string )                              \
        if( p_main->i_cpu_capabilities & capability )                       \
        {                                                                   \
            strncat( p_capabilities, string " ",                            \
                     sizeof(p_capabilities) - strlen(p_capabilities) );     \
            p_capabilities[sizeof(p_capabilities) - 1] = '\0';              \
        }

        PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
        PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
        PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
        PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
        PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
        PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
        PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
        PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
        intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
    }

356
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
357
     * Initialize playlist and get commandline files
358
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
359
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
360
    if( !p_main->p_playlist )
361
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
362
        intf_ErrMsg( "playlist error: playlist initialization failed" );
363 364
        intf_MsgDestroy();
        return( errno );
365
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
366
    intf_PlaylistInit( p_main->p_playlist );
367 368

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
369
     * Get input filenames given as commandline arguments
370
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
371
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
372

Sam Hocevar's avatar
 
Sam Hocevar committed
373
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
374
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
375
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
376
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
377 378
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
379

Michel Kaempf's avatar
Michel Kaempf committed
380 381 382
    /*
     * Initialize shared resources and libraries
     */
383 384 385
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
386
    {
Henri Fallon's avatar
 
Henri Fallon committed
387 388 389
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
390
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
391
    }
392

Michel Kaempf's avatar
Michel Kaempf committed
393
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
394
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
395
     */
396
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
397
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
398 399 400
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
401
    else
402
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
        /*
         * 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
         */
421 422
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
423 424 425
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
426
    }
Michel Kaempf's avatar
Michel Kaempf committed
427

Sam Hocevar's avatar
 
Sam Hocevar committed
428
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
429
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
430
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
431 432
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
433
    module_EndBank();
434 435 436 437

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
438
    intf_PlaylistDestroy( p_main->p_playlist );
439 440 441 442

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
443
#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
Sam Hocevar's avatar
 
Sam Hocevar committed
444
    system_End();
445 446
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
447

Michel Kaempf's avatar
Michel Kaempf committed
448 449 450
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
451
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
452
    intf_MsgDestroy();
453

Sam Hocevar's avatar
 
Sam Hocevar committed
454 455 456 457
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
458

Sam Hocevar's avatar
 
Sam Hocevar committed
459
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
460 461
}

462
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
463
 * main_GetIntVariable: get the int value of an environment variable
464
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
465
 * This function is used to read some default parameters in modules.
466
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
467 468
int main_GetIntVariable( char *psz_name, int i_default )
{
469 470 471
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
472 473 474

    psz_env = getenv( psz_name );
    if( psz_env )
475
    {
476 477 478 479
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
480
        }
481
    }
Vincent Seguin's avatar
Vincent Seguin committed
482 483 484
    return( i_default );
}

485
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
486
 * main_GetPszVariable: get the string value of an environment variable
487
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
488
 * This function is used to read some default parameters in modules.
489
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
490 491 492 493 494 495 496
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
497
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
498
    }
499
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
500 501
}

502
/*****************************************************************************
503
 * main_PutPszVariable: set the string value of an environment variable
504
 *****************************************************************************
505 506 507
 * 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.
508
 *****************************************************************************/
509 510 511 512 513 514 515
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
516 517
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
518 519 520 521 522 523
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
524
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
525
        }
526 527 528
    }
}

529
/*****************************************************************************
530
 * main_PutIntVariable: set the integer value of an environment variable
531
 *****************************************************************************
532 533 534
 * 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.
535
 *****************************************************************************/
536 537
void main_PutIntVariable( char *psz_name, int i_value )
{
538
    char psz_value[ 256 ];                               /* buffer for value */
539

540
    sprintf( psz_value, "%d", i_value );
541
    main_PutPszVariable( psz_name, psz_value );
542 543
}

Michel Kaempf's avatar
Michel Kaempf committed
544 545
/* following functions are local */

546
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
547
 * GetConfiguration: parse command line
548
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
549 550
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
551
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
552 553
 * stage, but most structures are not allocated, so only environment should
 * be used.
554
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
555
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
556
{
Sam Hocevar's avatar
 
Sam Hocevar committed
557 558
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
559 560

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
561
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
562
    p_main->ppsz_argv = ppsz_argv;
563
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
564

Henri Fallon's avatar
 
Henri Fallon committed
565 566
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
567

568
    p_main->i_warning_level = 0;
569
    p_main->b_stats = 0;
Michel Kaempf's avatar
Michel Kaempf committed
570

571 572
    p_main->p_channel = NULL;

573
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
574 575
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
576
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
577
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
578
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
579
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
580 581 582
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
583
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
584 585 586
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
587
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
588 589 590
    /* 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
591 592
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
593
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
594 595 596 597 598 599 600 601 602
        /* 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--;
603 604 605 606
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
607
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
608
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
609 610
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
611
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
612
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
613
        {
614 615
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
616 617
            ShowConsole();
            RedirectSTDOUT();
618
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
619 620 621 622 623 624 625 626 627
#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
628 629
            return( -1 );
            break;
630
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
631 632
            ShowConsole();
            RedirectSTDOUT();
633
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
634 635 636 637 638 639 640 641 642
#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
643 644
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
645
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
646 647
            ShowConsole();
            RedirectSTDOUT();
Vincent Seguin's avatar
Vincent Seguin committed
648
            Version();
gbazin's avatar
 
gbazin committed
649 650 651 652 653 654 655 656 657
#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
658
            return( -1 );
659
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
660
        case 'v':                                           /* -v, --verbose */
661
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
662
            break;
Michel Kaempf's avatar
Michel Kaempf committed
663

Sam Hocevar's avatar
 
Sam Hocevar committed
664
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
665
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
666 667 668
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
669 670
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
671 672
            break;

gbazin's avatar
 
gbazin committed
673 674 675 676
        case OPT_STDOUT:                                         /* --stdout */
            main_PutPszVariable( INTF_STDOUT_VAR, optarg );
            break;

677 678 679 680
        case OPT_STATS:
            p_main->b_stats = 1;
            break;

Michel Kaempf's avatar
Michel Kaempf committed
681
        /* Audio options */
682
        case OPT_NOAUDIO:                                       /* --noaudio */
683
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
684
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
685
        case 'A':                                              /* -A, --aout */
686 687
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
688
        case OPT_STEREO:                                         /* --stereo */
689
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
690
            break;
691
        case OPT_MONO:                                             /* --mono */
692
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
693
            break;
694
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
695
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
696
            break;
gbazin's avatar
 
gbazin committed
697
        case OPT_DOWNMIX:                                       /* --downmix */
Sam Hocevar's avatar
 
Sam Hocevar committed
698 699 700 701 702
            main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
            break;
        case OPT_IMDCT:                                           /* --imdct */
            main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
            break;
703 704 705
        case OPT_VOLUME:                                         /* --volume */
            main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
706 707

        /* Video options */
708
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
709
            p_main->b_video = 0;
710
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
711
        case 'V':                                              /* -V, --vout */
712 713
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
714
        case OPT_DISPLAY:                                       /* --display */
715
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
716 717
            break;
        case OPT_WIDTH:                                           /* --width */
718
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
719 720 721 722 723
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
724
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
725 726
            break;
        case OPT_COLOR:                                           /* --color */
727
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
728
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
729
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
730 731
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
732
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
733 734
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
735 736 737
        case OPT_XVADAPTOR:                                   /* --xvadaptor */
            main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
738
        case OPT_MOTION:                                         /* --motion */
Sam Hocevar's avatar
 
Sam Hocevar committed
739 740
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
741
        case OPT_IDCT:                                             /* --idct */
Sam Hocevar's avatar
 
Sam Hocevar committed
742 743
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
744 745 746
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;
747 748 749
        case OPT_SMP:                                               /* --smp */
            main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
            break;
Michel Kaempf's avatar
Michel Kaempf committed
750

751
        /* DVD options */
752
        case 't':                                              /* --dvdtitle */
753 754
            main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
            break;
755
        case 'T':                                            /* --dvdchapter */
756 757
            main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
            break;
758
        case 'u':                                              /* --dvdangle */
759 760
            main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
            break;
761
        case 'a':                                              /* --dvdaudio */
762
            if ( ! strcmp(optarg, "ac3") )
Sam Hocevar's avatar
 
Sam Hocevar committed
763
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
764
            else if ( ! strcmp(optarg, "lpcm") )
Sam Hocevar's avatar
 
Sam Hocevar committed
765
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
766
            else if ( ! strcmp(optarg, "mpeg") )
Sam Hocevar's avatar
 
Sam Hocevar committed
767
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
768
            else
Sam Hocevar's avatar
 
Sam Hocevar committed
769
                main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
770
            break;
771
        case 'c':                                            /* --dvdchannel */
Sam Hocevar's avatar
 
Sam Hocevar committed
772
            main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
773
            break