main.c 50.6 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.136 2001/12/12 02:13:50 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
Michel Kaempf's avatar
Michel Kaempf committed
127

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

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

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

150 151
#define OPT_MPEG_ADEC           200

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

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

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

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

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

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

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

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

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

Michel Kaempf's avatar
Michel Kaempf committed
232 233 234 235
    {   0,                  0,          0,      0 }
};

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

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

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

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

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

264 265
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
266

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
289 290 291 292
#ifdef ENABLE_NLS
    /* 
     * Support for getext
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
293
#ifdef HAVE_LOCALE_H
Sam Hocevar's avatar
 
Sam Hocevar committed
294 295 296 297
    if( ! setlocale(LC_MESSAGES, "") )
    {
        fprintf( stderr, "warning: unsupported locale.\n" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
298
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
299 300 301 302 303 304 305 306 307 308

    if( ! bindtextdomain(PACKAGE, LOCALEDIR) )
    {
        fprintf( stderr, "warning: no domain %s in directory %s\n",
                 PACKAGE, LOCALEDIR );
    }

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

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

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

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

343
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
Sam Hocevar's avatar
 
Sam Hocevar committed
344

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
501

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
513
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
514 515
}

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

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

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

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

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

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

594
    sprintf( psz_value, "%d", i_value );
595
    main_PutPszVariable( psz_name, psz_value );
596 597
}

Michel Kaempf's avatar
Michel Kaempf committed
598 599
/* following functions are local */

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

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

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

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

626 627
    p_main->p_channel = NULL;

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

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

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

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

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

732 733 734 735
        case OPT_STATS:
            p_main->b_stats = 1;
            break;

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

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