main.c 45.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
7
 * $Id: main.c,v 1.115 2001/10/01 12:48:01 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 );
292

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
314 315
    intf_MsgImm( COPYRIGHT_MESSAGE );

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

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

331
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
332
     * Initialize playlist and get commandline files
333
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
334
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
335
    if( !p_main->p_playlist )
336
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
337
        intf_ErrMsg( "playlist error: playlist initialization failed" );
338 339
        intf_MsgDestroy();
        return( errno );
340
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
341
    intf_PlaylistInit( p_main->p_playlist );
342 343

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
344
     * Get input filenames given as commandline arguments
345
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
346
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
347

Sam Hocevar's avatar
 
Sam Hocevar committed
348
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
349
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
350
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
351
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
352 353
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
354

Michel Kaempf's avatar
Michel Kaempf committed
355 356 357
    /*
     * Initialize shared resources and libraries
     */
358 359 360
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
361
    {
Henri Fallon's avatar
 
Henri Fallon committed
362 363 364
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
365
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
366
    }
367

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

Sam Hocevar's avatar
 
Sam Hocevar committed
403
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
404
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
405
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
406 407
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
408
    module_EndBank();
409 410 411 412

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
413
    intf_PlaylistDestroy( p_main->p_playlist );
414 415 416 417

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
418
#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
Sam Hocevar's avatar
 
Sam Hocevar committed
419
    system_End();
420 421
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
422

Michel Kaempf's avatar
Michel Kaempf committed
423 424 425
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
426
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
427
    intf_MsgDestroy();
428

Sam Hocevar's avatar
 
Sam Hocevar committed
429 430 431 432
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
433

Sam Hocevar's avatar
 
Sam Hocevar committed
434
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
435 436
}

437
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
438
 * main_GetIntVariable: get the int value of an environment variable
439
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
440
 * This function is used to read some default parameters in modules.
441
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
442 443
int main_GetIntVariable( char *psz_name, int i_default )
{
444 445 446
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
447 448 449

    psz_env = getenv( psz_name );
    if( psz_env )
450
    {
451 452 453 454
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
455
        }
456
    }
Vincent Seguin's avatar
Vincent Seguin committed
457 458 459
    return( i_default );
}

460
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
461
 * main_GetPszVariable: get the string value of an environment variable
462
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
463
 * This function is used to read some default parameters in modules.
464
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
465 466 467 468 469 470 471
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
472
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
473
    }
474
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
475 476
}

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

504
/*****************************************************************************
505
 * main_PutIntVariable: set the integer value of an environment variable
506
 *****************************************************************************
507 508 509
 * 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.
510
 *****************************************************************************/
511 512
void main_PutIntVariable( char *psz_name, int i_value )
{
513
    char psz_value[ 256 ];                               /* buffer for value */
514

515
    sprintf( psz_value, "%d", i_value );
516
    main_PutPszVariable( psz_name, psz_value );
517 518
}

Michel Kaempf's avatar
Michel Kaempf committed
519 520
/* following functions are local */

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

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
536
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
537
    p_main->ppsz_argv = ppsz_argv;
538
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
539

Henri Fallon's avatar
 
Henri Fallon committed
540 541
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
542

543
    p_main->i_warning_level = 0;
Michel Kaempf's avatar
Michel Kaempf committed
544

545 546
    p_main->p_channel = NULL;

547
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
548 549
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
550
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
551
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
552
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
553
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
554 555 556
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
557
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
558 559 560
        }
    }

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

Vincent Seguin's avatar
Vincent Seguin committed
581
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
582
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
583 584
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
585
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
586
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
587
        {
588 589
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
590 591
            ShowConsole();
            RedirectSTDOUT();
592
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
593 594 595 596 597 598 599 600 601
#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
602 603
            return( -1 );
            break;
604
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
605 606
            ShowConsole();
            RedirectSTDOUT();
607
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
608 609 610 611 612 613 614 615 616
#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
617 618
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
619
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
620 621
            ShowConsole();
            RedirectSTDOUT();
Vincent Seguin's avatar
Vincent Seguin committed
622
            Version();
gbazin's avatar
 
gbazin committed
623 624 625 626 627 628 629 630 631
#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
632
            return( -1 );
633
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
634
        case 'v':                                           /* -v, --verbose */
635
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
636
            break;
Michel Kaempf's avatar
Michel Kaempf committed
637

Sam Hocevar's avatar
 
Sam Hocevar committed
638
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
639
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
640 641 642
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
643 644
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
645 646
            break;

gbazin's avatar
 
gbazin committed
647 648 649 650
        case OPT_STDOUT:                                         /* --stdout */
            main_PutPszVariable( INTF_STDOUT_VAR, optarg );
            break;

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

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

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

745
        /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
746
        case OPT_INPUT:                                           /* --input */
Sam Hocevar's avatar
 
Sam Hocevar committed
747 748
            main_PutPszVariable( INPUT_METHOD_VAR, optarg );
            break;
Henri Fallon's avatar
 
Henri Fallon committed
749
        case OPT_CHANNELS:                                     /* --channels */
750
            main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
751 752
            break;
        case OPT_SERVER:                                         /* --server */
753
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
754 755
            break;
        case OPT_PORT:                                             /* --port */
756
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
757
            break;
758
        case OPT_BROADCAST:                                   /* --broadcast */
759 760
            main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
            main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
761
            break;
762

763
        /* Synchro options */
764
        case OPT_SYNCHRO:                                      
765 766
            main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
            break;