main.c 51 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-2001 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
7
 * $Id: main.c,v 1.138 2001/12/16 16:18:36 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

Sam Hocevar's avatar
 
Sam Hocevar committed
61 62 63 64
#ifdef HAVE_LOCALE_H
#    include <locale.h>
#endif

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

Michel Kaempf's avatar
Michel Kaempf committed
71
#include "common.h"
Christophe Massiot's avatar
Christophe Massiot committed
72
#include "debug.h"
73
#include "intf_msg.h"
74
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
75
#include "mtime.h"
76
#include "tests.h"                                              /* TestCPU() */
Sam Hocevar's avatar
 
Sam Hocevar committed
77
#include "modules.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
78

79 80
#include "stream_control.h"
#include "input_ext-intf.h"
81

Sam Hocevar's avatar
 
Sam Hocevar committed
82
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
83
#include "interface.h"
84

Vincent Seguin's avatar
Vincent Seguin committed
85
#include "audio_output.h"
86

Sam Hocevar's avatar
 
Sam Hocevar committed
87 88 89
#include "video.h"
#include "video_output.h"

90
#ifdef SYS_BEOS
Sam Hocevar's avatar
 
Sam Hocevar committed
91 92 93
#   include "beos_specific.h"
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
94
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
95
#   include "darwin_specific.h"
96 97
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
98 99 100 101
#ifdef WIN32
#   include "win32_specific.h"
#endif

Henri Fallon's avatar
 
Henri Fallon committed
102 103
#include "netutils.h"                                 /* network_ChannelJoin */

104
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
105 106
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
107
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
108 109 110 111

/* 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
112 113
#define OPT_STEREO              151
#define OPT_MONO                152
114
#define OPT_SPDIF               153
115
#define OPT_VOLUME              154
116
#define OPT_DESYNC              155
Michel Kaempf's avatar
Michel Kaempf committed
117 118

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
119 120 121 122 123 124
#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
125 126
#define OPT_XVADAPTOR           167
#define OPT_SMP                 168
Sam Hocevar's avatar
 
Sam Hocevar committed
127
#define OPT_FILTER              169
Michel Kaempf's avatar
Michel Kaempf committed
128

Henri Fallon's avatar
 
Henri Fallon committed
129
#define OPT_CHANNELS            170
130 131
#define OPT_SERVER              171
#define OPT_PORT                172
132
#define OPT_BROADCAST           173
133
#define OPT_CHANNELSERVER       174
134

Sam Hocevar's avatar
 
Sam Hocevar committed
135 136 137 138
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
139 140
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
Sam Hocevar's avatar
 
Sam Hocevar committed
141 142 143
#define OPT_MEMCPY              186
#define OPT_DVDCSS_METHOD       187
#define OPT_DVDCSS_VERBOSE      188
144

Sam Hocevar's avatar
 
Sam Hocevar committed
145 146
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
147
#define OPT_VERSION             192
gbazin's avatar
 
gbazin committed
148
#define OPT_STDOUT              193
149
#define OPT_STATS               194
150

151 152
#define OPT_MPEG_ADEC           200

153
/* Usage fashion */
154 155 156
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
157

Sam Hocevar's avatar
 
Sam Hocevar committed
158 159 160 161 162 163 164 165 166 167
/* 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
168 169
/* Long options */
static const struct option longopts[] =
170 171
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
172 173

    /* General/common options */
174
    {   "help",             0,          0,      'h' },
175
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
176
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
177

Sam Hocevar's avatar
 
Sam Hocevar committed
178
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
179
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
180
    {   "warning",          1,          0,      OPT_WARNING },
gbazin's avatar
 
gbazin committed
181
    {   "stdout",           1,          0,      OPT_STDOUT },
182
    {   "stats",            0,          0,      OPT_STATS },
Sam Hocevar's avatar
 
Sam Hocevar committed
183

Michel Kaempf's avatar
Michel Kaempf committed
184
    /* Audio options */
185
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
186
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
187
    {   "stereo",           0,          0,      OPT_STEREO },
188
    {   "mono",             0,          0,      OPT_MONO },
189
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
190 191
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
192
    {   "volume",           1,          0,      OPT_VOLUME },
193
    {   "desync",           1,          0,      OPT_DESYNC },
Michel Kaempf's avatar
Michel Kaempf committed
194 195

    /* Video options */
196
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
197
    {   "vout",             1,          0,      'V' },
198
    {   "display",          1,          0,      OPT_DISPLAY },
199
    {   "width",            1,          0,      OPT_WIDTH },
200 201 202
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
203
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
204
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
205
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
206 207
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Sam Hocevar's avatar
 
Sam Hocevar committed
208
    {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
209
    {   "smp",              1,          0,      OPT_SMP },
Sam Hocevar's avatar
 
Sam Hocevar committed
210
    {   "filter",           1,          0,      OPT_FILTER },
Michel Kaempf's avatar
Michel Kaempf committed
211

212
    /* DVD options */
213 214
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
215
    {   "dvdangle",         1,          0,      'u' },
216 217 218
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
Sam Hocevar's avatar
 
Sam Hocevar committed
219 220
    {   "dvdcss-method",    1,          0,      OPT_DVDCSS_METHOD },
    {   "dvdcss-verbose",   1,          0,      OPT_DVDCSS_VERBOSE },
221
    
222
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
223
    {   "input",            1,          0,      OPT_INPUT },
224
    {   "channels",         0,          0,      OPT_CHANNELS },
225
    {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
Michel Kaempf's avatar
Michel Kaempf committed
226

Sam Hocevar's avatar
 
Sam Hocevar committed
227
    /* Misc options */
228
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Sam Hocevar's avatar
 
Sam Hocevar committed
229
    {   "memcpy",           1,          0,      OPT_MEMCPY },
230 231 232 233

    /* Decoder options */
    {   "mpeg_adec",        1,          0,      OPT_MPEG_ADEC },

Michel Kaempf's avatar
Michel Kaempf committed
234 235 236 237
    {   0,                  0,          0,      0 }
};

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

240
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
241
 * Global variables - these are the only ones, see main.h and modules.h
242
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
243 244 245 246
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
247

248
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
249
 * Local prototypes
250
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
251
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
252
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
253
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
254
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
255
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
256

Michel Kaempf's avatar
Michel Kaempf committed
257
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
258 259
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
260
static void InstructionSignalHandler( int i_signal );
Sam Hocevar's avatar
 
Sam Hocevar committed
261
static int  CPUCapabilities         ( void );
gbazin's avatar
 
gbazin committed
262 263 264 265

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

266 267
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
268

269
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
270
 * main: parse command line, start interface and spawn threads
271
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
272 273
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
274
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
275
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
276
 *      -closing of audio output device and some global modules
277
 * On error, the spawned threads are canceled, and the open devices closed.
278
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
279 280
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
281 282 283 284
    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;
285

Sam Hocevar's avatar
 
Sam Hocevar committed
286 287 288 289
    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
290

Sam Hocevar's avatar
 
Sam Hocevar committed
291 292 293 294
#ifdef ENABLE_NLS
    /* 
     * Support for getext
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
295 296
#if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
    if( !setlocale( LC_MESSAGES, "" ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
297 298 299
    {
        fprintf( stderr, "warning: unsupported locale.\n" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
300
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
301

Sam Hocevar's avatar
 
Sam Hocevar committed
302
    if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
303 304 305 306 307 308 309 310
    {
        fprintf( stderr, "warning: no domain %s in directory %s\n",
                 PACKAGE, LOCALEDIR );
    }

    textdomain( PACKAGE );
#endif
        
Sam Hocevar's avatar
 
Sam Hocevar committed
311
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
312
     * Initialize threads system
Sam Hocevar's avatar
 
Sam Hocevar committed
313
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
314
    vlc_threads_init( );
Sam Hocevar's avatar
 
Sam Hocevar committed
315

Sam Hocevar's avatar
 
Sam Hocevar committed
316
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
317
     * Test if our code is likely to run on this CPU 
Sam Hocevar's avatar
 
Sam Hocevar committed
318 319 320
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
    
Sam Hocevar's avatar
 
Sam Hocevar committed
321 322 323
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
324
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
325
    system_Init( &i_argc, ppsz_argv, ppsz_env );
326 327 328 329 330 331

#elif defined( SYS_LINUX )
#   ifdef DEBUG
    /* Activate malloc checking routines to detect heap corruptions. */
    main_PutIntVariable( "MALLOC_CHECK_", 2 );
#   endif
332
#endif
333 334 335 336

    /*
     * Initialize messages interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
337
    p_main->p_msg = intf_MsgCreate();
338
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
339
    {
340
        fprintf( stderr, "error: can't initialize messages interface (%s)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
341
                 strerror(errno) );
342
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
343
    }
344

345
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
Sam Hocevar's avatar
 
Sam Hocevar committed
346

347
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
348
     * Read configuration
349
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
350
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
351
    {
Vincent Seguin's avatar
Vincent Seguin committed
352
        intf_MsgDestroy();
353
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
354
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
355

gbazin's avatar
 
gbazin committed
356 357 358 359 360 361
    /*
     * Redirect the standard output if required by the user, and on Win32 we
     * also open a console to display the debug messages.
     */
    RedirectSTDOUT();

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    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 );
    }

386
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
387
     * Initialize playlist and get commandline files
388
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
389
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
390
    if( !p_main->p_playlist )
391
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
392
        intf_ErrMsg( "playlist error: playlist initialization failed" );
393 394
        intf_MsgDestroy();
        return( errno );
395
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
396
    intf_PlaylistInit( p_main->p_playlist );
397 398

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
399
     * Get input filenames given as commandline arguments
400
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
401
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
402

Sam Hocevar's avatar
 
Sam Hocevar committed
403
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
404
     * Initialize module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
405
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
406
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
407 408
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
409

Sam Hocevar's avatar
 
Sam Hocevar committed
410 411 412 413 414
    /*
     * Choose the best memcpy module
     */
    p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );

Sam Hocevar's avatar
 
Sam Hocevar committed
415
    if( p_main->p_memcpy_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
416 417
    {
#define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
Sam Hocevar's avatar
 
Sam Hocevar committed
418
        p_main->fast_memcpy = f.fast_memcpy;
Sam Hocevar's avatar
 
Sam Hocevar committed
419 420
#undef f
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
421 422 423 424 425 426
    else
    {
        intf_ErrMsg( "intf error: no suitable memcpy module, "
                     "using libc default" );
        p_main->fast_memcpy = memcpy;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
427

Michel Kaempf's avatar
Michel Kaempf committed
428 429 430
    /*
     * Initialize shared resources and libraries
     */
431 432 433
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
434
    {
Henri Fallon's avatar
 
Henri Fallon committed
435 436 437
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
438
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
439
    }
440

Michel Kaempf's avatar
Michel Kaempf committed
441
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
442
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
443
     */
444
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
445
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
446 447 448
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
449
    else
450
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        /*
         * 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
         */
469 470
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
471 472 473
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
474
    }
Michel Kaempf's avatar
Michel Kaempf committed
475

Sam Hocevar's avatar
 
Sam Hocevar committed
476 477 478
    /*
     * Free memcpy module
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
479 480 481 482
    if( p_main->p_memcpy_module != NULL )
    {
        module_Unneed( p_main->p_memcpy_module );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
483

Sam Hocevar's avatar
 
Sam Hocevar committed
484
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
485
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
486
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
487 488
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
489
    module_EndBank();
490 491 492 493

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
494
    intf_PlaylistDestroy( p_main->p_playlist );
495 496 497 498

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
499
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
500
    system_End();
501 502
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
503

Michel Kaempf's avatar
Michel Kaempf committed
504 505 506
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
507
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
508
    intf_MsgDestroy();
509

Sam Hocevar's avatar
 
Sam Hocevar committed
510 511 512 513
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
514

Sam Hocevar's avatar
 
Sam Hocevar committed
515
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
516 517
}

518
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
519
 * main_GetIntVariable: get the int value of an environment variable
520
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
521
 * This function is used to read some default parameters in modules.
522
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
523 524
int main_GetIntVariable( char *psz_name, int i_default )
{
525 526 527
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
528 529 530

    psz_env = getenv( psz_name );
    if( psz_env )
531
    {
532 533 534 535
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
536
        }
537
    }
Vincent Seguin's avatar
Vincent Seguin committed
538 539 540
    return( i_default );
}

541
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
542
 * main_GetPszVariable: get the string value of an environment variable
543
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
544
 * This function is used to read some default parameters in modules.
545
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
546 547 548 549 550 551 552
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
553
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
554
    }
555
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
556 557
}

558
/*****************************************************************************
559
 * main_PutPszVariable: set the string value of an environment variable
560
 *****************************************************************************
561 562 563
 * 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.
564
 *****************************************************************************/
565 566 567 568 569 570 571
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
572 573
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
574 575 576 577 578 579
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
580
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
581
        }
582 583 584
    }
}

585
/*****************************************************************************
586
 * main_PutIntVariable: set the integer value of an environment variable
587
 *****************************************************************************
588 589 590
 * 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.
591
 *****************************************************************************/
592 593
void main_PutIntVariable( char *psz_name, int i_value )
{
594
    char psz_value[ 256 ];                               /* buffer for value */
595

596
    sprintf( psz_value, "%d", i_value );
597
    main_PutPszVariable( psz_name, psz_value );
598 599
}

Michel Kaempf's avatar
Michel Kaempf committed
600 601
/* following functions are local */

602
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
603
 * GetConfiguration: parse command line
604
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
605 606
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
607
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
608 609
 * stage, but most structures are not allocated, so only environment should
 * be used.
610
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
611
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
612
{
Sam Hocevar's avatar
 
Sam Hocevar committed
613 614
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
615 616

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
617
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
618
    p_main->ppsz_argv = ppsz_argv;
619
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
620

Henri Fallon's avatar
 
Henri Fallon committed
621 622
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
623

624
    p_main->i_warning_level = 0;
625
    p_main->b_stats = 0;
626
    p_main->i_desync = 0; /* No desynchronization by default */
Michel Kaempf's avatar
Michel Kaempf committed
627

628 629
    p_main->p_channel = NULL;

630
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
631 632
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
633
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
634
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
635
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
636
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
637 638 639
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
640
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
641 642 643
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
644
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
645 646 647
    /* 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
648 649
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
650
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
651 652 653 654 655 656 657 658 659
        /* 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--;
660 661 662 663
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
664
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
665
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
666 667
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
668
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
669
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
670
        {
671 672
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
673 674
            ShowConsole();
            RedirectSTDOUT();
675
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
676 677 678 679 680 681 682 683 684
#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
685 686
            return( -1 );
            break;
687
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
688 689
            ShowConsole();
            RedirectSTDOUT();
690
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
691 692 693 694 695 696 697 698 699
#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
700 701
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
702
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
703 704
            ShowConsole();
            RedirectSTDOUT();
Vincent Seguin's avatar
Vincent Seguin committed
705
            Version();
gbazin's avatar
 
gbazin committed
706 707 708 709 710 711 712 713 714
#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
715
            return( -1 );
716
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
717
        case 'v':                                           /* -v, --verbose */
718
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
719
            break;
Michel Kaempf's avatar
Michel Kaempf committed
720

Sam Hocevar's avatar
 
Sam Hocevar committed
721
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
722
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
723 724 725
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
726 727
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
728 729
            break;

gbazin's avatar
 
gbazin committed
730 731 732 733
        case OPT_STDOUT:                                         /* --stdout */
            main_PutPszVariable( INTF_STDOUT_VAR, optarg );
            break;

734 735 736 737
        case OPT_STATS:
            p_main->b_stats = 1;
            break;

Michel Kaempf's avatar
Michel Kaempf committed
738
        /* Audio options */
739
        case OPT_NOAUDIO:                                       /* --noaudio */
740
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
741
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
742
        case 'A':                                              /* -A, --aout */
743 744
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
745
        case OPT_STEREO:                                         /* --stereo */
746
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
747
            break;
748
        case OPT_MONO:                                             /* --mono */
749
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
750
            break;
751
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
752
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
753
            break;
gbazin's avatar
 
gbazin committed
754
        case OPT_DOWNMIX:                                       /* --downmix */
Sam Hocevar's avatar
 
Sam Hocevar committed
755 756 757 758 759
            main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
            break;
        case OPT_IMDCT:                                           /* --imdct */
            main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
            break;
760 761 762
        case OPT_VOLUME:                                         /* --volume */
            main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
            break;
763 764 765
        case OPT_DESYNC:                                         /* --desync */
            p_main->i_desync = atoi(optarg);
            break;
Michel Kaempf's avatar
Michel Kaempf committed
766 767

        /* Video options */
768
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
769
            p_main->b_video = 0;
770
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
771
        case 'V':                                              /* -V, --vout */
772 773
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
774
        case OPT_DISPLAY:                                       /* --display */
775
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
776 777
            break;
        case OPT_WIDTH:                                           /* --width */
778
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
779 780 781 782 783
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
784
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
785 786
            break;
        case OPT_COLOR:                                           /* --color */
787
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
788
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
789
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
790 791
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
792
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
793 794
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar