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.152 2002/02/19 00:50:19 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 <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
#include <stdio.h>                                              /* sprintf() */
32
#include <setjmp.h>                                       /* longjmp, setjmp */
33

Sam Hocevar's avatar
 
Sam Hocevar committed
34 35
#include <videolan/vlc.h>

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

Sam Hocevar's avatar
 
Sam Hocevar committed
71
#include "netutils.h"                                 /* network_ChannelJoin */
Sam Hocevar's avatar
 
Sam Hocevar committed
72

73 74
#include "stream_control.h"
#include "input_ext-intf.h"
75

Sam Hocevar's avatar
 
Sam Hocevar committed
76
#include "intf_playlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
77
#include "interface.h"
78

Vincent Seguin's avatar
Vincent Seguin committed
79
#include "audio_output.h"
80

Sam Hocevar's avatar
 
Sam Hocevar committed
81 82 83
#include "video.h"
#include "video_output.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
84
#include "debug.h"
Henri Fallon's avatar
 
Henri Fallon committed
85

86
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
87 88
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
89
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
90 91 92 93

/* 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
94 95
#define OPT_STEREO              151
#define OPT_MONO                152
96
#define OPT_SPDIF               153
97
#define OPT_VOLUME              154
98
#define OPT_DESYNC              155
Michel Kaempf's avatar
Michel Kaempf committed
99 100

#define OPT_NOVIDEO             160
Sam Hocevar's avatar
 
Sam Hocevar committed
101 102 103 104 105
#define OPT_DISPLAY             161
#define OPT_WIDTH               162
#define OPT_HEIGHT              163
#define OPT_COLOR               164
#define OPT_FULLSCREEN          165
gbazin's avatar
 
gbazin committed
106
#define OPT_NOOVERLAY           166
Sam Hocevar's avatar
 
Sam Hocevar committed
107 108
#define OPT_XVADAPTOR           167
#define OPT_SMP                 168
Sam Hocevar's avatar
 
Sam Hocevar committed
109
#define OPT_SPUMARGIN           169
Michel Kaempf's avatar
Michel Kaempf committed
110

Henri Fallon's avatar
 
Henri Fallon committed
111
#define OPT_CHANNELS            170
112 113
#define OPT_SERVER              171
#define OPT_PORT                172
114
#define OPT_BROADCAST           173
115
#define OPT_CHANNELSERVER       174
116

Sam Hocevar's avatar
 
Sam Hocevar committed
117 118 119 120
#define OPT_INPUT               180
#define OPT_MOTION              181
#define OPT_IDCT                182
#define OPT_YUV                 183
Sam Hocevar's avatar
 
Sam Hocevar committed
121 122
#define OPT_DOWNMIX             184
#define OPT_IMDCT               185
Sam Hocevar's avatar
 
Sam Hocevar committed
123
#define OPT_MEMCPY              186
Sam Hocevar's avatar
 
Sam Hocevar committed
124 125 126
#define OPT_FILTER              187
#define OPT_DVDCSS_METHOD       188
#define OPT_DVDCSS_VERBOSE      189
127

Sam Hocevar's avatar
 
Sam Hocevar committed
128 129
#define OPT_SYNCHRO             190
#define OPT_WARNING             191
Sam Hocevar's avatar
 
Sam Hocevar committed
130
#define OPT_VERSION             192
Sam Hocevar's avatar
 
Sam Hocevar committed
131
#define OPT_STATS               193
132

133
#define OPT_MPEG_ADEC           200
gbazin's avatar
 
gbazin committed
134
#define OPT_AC3_ADEC            201
135

gbazin's avatar
 
gbazin committed
136 137 138 139 140
#define OPT_NOMMX               210
#define OPT_NO3DNOW             211
#define OPT_NOMMXEXT            212
#define OPT_NOSSE               213
#define OPT_NOALTIVEC           214
Sam Hocevar's avatar
 
Sam Hocevar committed
141

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

Michel Kaempf's avatar
Michel Kaempf committed
147 148
/* Long options */
static const struct option longopts[] =
149 150
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
151 152

    /* General/common options */
153
    {   "help",             0,          0,      'h' },
154
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
155
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
156

Sam Hocevar's avatar
 
Sam Hocevar committed
157
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
158
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
159
    {   "warning",          1,          0,      OPT_WARNING },
160
    {   "stats",            0,          0,      OPT_STATS },
Sam Hocevar's avatar
 
Sam Hocevar committed
161

Michel Kaempf's avatar
Michel Kaempf committed
162
    /* Audio options */
163
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
164
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
165
    {   "stereo",           0,          0,      OPT_STEREO },
166
    {   "mono",             0,          0,      OPT_MONO },
167
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
168 169
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
170
    {   "volume",           1,          0,      OPT_VOLUME },
171
    {   "desync",           1,          0,      OPT_DESYNC },
Michel Kaempf's avatar
Michel Kaempf committed
172 173

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

190
    /* DVD options */
191 192
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
193
    {   "dvdangle",         1,          0,      'u' },
194 195 196
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
Sam Hocevar's avatar
 
Sam Hocevar committed
197 198
    {   "dvdcss-method",    1,          0,      OPT_DVDCSS_METHOD },
    {   "dvdcss-verbose",   1,          0,      OPT_DVDCSS_VERBOSE },
Sam Hocevar's avatar
 
Sam Hocevar committed
199

200
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
201
    {   "input",            1,          0,      OPT_INPUT },
202
    {   "channels",         0,          0,      OPT_CHANNELS },
203
    {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
Michel Kaempf's avatar
Michel Kaempf committed
204

Sam Hocevar's avatar
 
Sam Hocevar committed
205
    /* Misc options */
206
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Sam Hocevar's avatar
 
Sam Hocevar committed
207
    {   "memcpy",           1,          0,      OPT_MEMCPY },
Sam Hocevar's avatar
 
Sam Hocevar committed
208
    {   "filter",           1,          0,      OPT_FILTER },
209 210 211

    /* Decoder options */
    {   "mpeg_adec",        1,          0,      OPT_MPEG_ADEC },
gbazin's avatar
 
gbazin committed
212
    {   "ac3_adec",         1,          0,      OPT_AC3_ADEC },
213

Sam Hocevar's avatar
 
Sam Hocevar committed
214 215 216 217 218 219 220
    /* CPU options */
    {   "nommx",            0,          0,      OPT_NOMMX },
    {   "no3dnow",          0,          0,      OPT_NO3DNOW },
    {   "nommxext",         0,          0,      OPT_NOMMXEXT },
    {   "nosse",            0,          0,      OPT_NOSSE },
    {   "noaltivec",        0,          0,      OPT_NOALTIVEC },

Michel Kaempf's avatar
Michel Kaempf committed
221 222 223 224
    {   0,                  0,          0,      0 }
};

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

227
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
228
 * Global variables - these are the only ones, see main.h and modules.h
229
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
230 231
main_t        *p_main;
module_bank_t *p_module_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
232
input_bank_t  *p_input_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
233 234
aout_bank_t   *p_aout_bank;
vout_bank_t   *p_vout_bank;
Michel Kaempf's avatar
Michel Kaempf committed
235

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

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

static void ShowConsole             ( void );

253
static jmp_buf env;
Sam Hocevar's avatar
 
Sam Hocevar committed
254 255
static int     i_illegal;
static char   *psz_capability;
Sam Hocevar's avatar
 
Sam Hocevar committed
256

257
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
258
 * main: parse command line, start interface and spawn threads
259
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
260 261
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
262
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
263
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
264
 *      -closing of audio output device and some global modules
265
 * On error, the spawned threads are canceled, and the open devices closed.
266
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
267 268
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
269 270
    main_t        main_data;                /* root of all data - see main.h */
    module_bank_t module_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
271
    input_bank_t  input_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
272 273
    aout_bank_t   aout_bank;
    vout_bank_t   vout_bank;
274

Sam Hocevar's avatar
 
Sam Hocevar committed
275 276
    p_main        = &main_data;               /* set up the global variables */
    p_module_bank = &module_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
277
    p_input_bank  = &input_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
278 279
    p_aout_bank   = &aout_bank;
    p_vout_bank   = &vout_bank;
Sam Hocevar's avatar
 
Sam Hocevar committed
280

Sam Hocevar's avatar
 
Sam Hocevar committed
281
#if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
Sam Hocevar's avatar
 
Sam Hocevar committed
282
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
283 284
     * Support for getext
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
285 286
#if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
    if( !setlocale( LC_MESSAGES, "" ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
287 288 289
    {
        fprintf( stderr, "warning: unsupported locale.\n" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
290
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
291

Sam Hocevar's avatar
 
Sam Hocevar committed
292
    if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
293 294 295 296 297 298 299
    {
        fprintf( stderr, "warning: no domain %s in directory %s\n",
                 PACKAGE, LOCALEDIR );
    }

    textdomain( PACKAGE );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
300

Sam Hocevar's avatar
 
Sam Hocevar committed
301
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
302
     * Initialize threads system
Sam Hocevar's avatar
 
Sam Hocevar committed
303
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
304
    vlc_threads_init( );
Sam Hocevar's avatar
 
Sam Hocevar committed
305

Sam Hocevar's avatar
 
Sam Hocevar committed
306
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
307
     * Test if our code is likely to run on this CPU
Sam Hocevar's avatar
 
Sam Hocevar committed
308 309
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
Sam Hocevar's avatar
 
Sam Hocevar committed
310

Sam Hocevar's avatar
 
Sam Hocevar committed
311 312 313
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
314
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
315
    system_Init( &i_argc, ppsz_argv, ppsz_env );
316 317 318 319 320 321

#elif defined( SYS_LINUX )
#   ifdef DEBUG
    /* Activate malloc checking routines to detect heap corruptions. */
    main_PutIntVariable( "MALLOC_CHECK_", 2 );
#   endif
322
#endif
323 324 325 326

    /*
     * Initialize messages interface
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
327
    intf_MsgCreate();
328

Sam Hocevar's avatar
 
Sam Hocevar committed
329
    intf_Msg( COPYRIGHT_MESSAGE "\n" );
Sam Hocevar's avatar
 
Sam Hocevar committed
330

331
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
332
     * Read configuration
333
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
334
    if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
Michel Kaempf's avatar
Michel Kaempf committed
335
    {
Vincent Seguin's avatar
Vincent Seguin committed
336
        intf_MsgDestroy();
337
        return( errno );
Michel Kaempf's avatar
Michel Kaempf committed
338
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
339

gbazin's avatar
 
gbazin committed
340 341 342 343
    /*
     * Redirect the standard output if required by the user, and on Win32 we
     * also open a console to display the debug messages.
     */
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    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" );
Sam Hocevar's avatar
 
Sam Hocevar committed
365
        PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
Sam Hocevar's avatar
 
Sam Hocevar committed
366
        intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
367 368
    }

369
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
370
     * Initialize playlist and get commandline files
371
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
372
    p_main->p_playlist = intf_PlaylistCreate();
Sam Hocevar's avatar
 
Sam Hocevar committed
373
    if( !p_main->p_playlist )
374
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
375
        intf_ErrMsg( "playlist error: playlist initialization failed" );
376 377
        intf_MsgDestroy();
        return( errno );
378
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
379
    intf_PlaylistInit( p_main->p_playlist );
380 381

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
382
     * Get input filenames given as commandline arguments
383
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
384
    GetFilenames( i_argc, ppsz_argv );
Michel Kaempf's avatar
Michel Kaempf committed
385

Sam Hocevar's avatar
 
Sam Hocevar committed
386
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
387
     * Initialize module, input, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
388
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
389
    module_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
390
    input_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
391 392
    aout_InitBank();
    vout_InitBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
393

Sam Hocevar's avatar
 
Sam Hocevar committed
394 395 396
    /*
     * Choose the best memcpy module
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
397 398 399
    p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL,
                                           NULL );
    if( p_main->p_memcpy_module == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
400 401 402
    {
        intf_ErrMsg( "intf error: no suitable memcpy module, "
                     "using libc default" );
Sam Hocevar's avatar
 
Sam Hocevar committed
403 404 405 406 407 408
        p_main->pf_memcpy = memcpy;
    }
    else
    {
        p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
                                  ->memcpy.functions.memcpy.pf_memcpy;
Sam Hocevar's avatar
 
Sam Hocevar committed
409
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
410

Michel Kaempf's avatar
Michel Kaempf committed
411 412 413
    /*
     * Initialize shared resources and libraries
     */
414 415 416
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
417
    {
Henri Fallon's avatar
 
Henri Fallon committed
418
        /* On error during Channels initialization, switch off channels */
Sam Hocevar's avatar
 
Sam Hocevar committed
419 420
        intf_ErrMsg( "intf error: channels initialization failed, " 
                                 "deactivating channels" );
421
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
422
    }
423

Michel Kaempf's avatar
Michel Kaempf committed
424
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
425
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
426
     */
427
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
428
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
429 430 431
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
432
    else
433
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        /*
         * 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
         */
452 453
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
454 455 456
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
457
    }
Michel Kaempf's avatar
Michel Kaempf committed
458

Sam Hocevar's avatar
 
Sam Hocevar committed
459
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
460
     * Free input, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
461
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
462
    input_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
463 464 465 466 467 468 469 470
    vout_EndBank();
    aout_EndBank();

    /*
     * Free playlist
     */
    intf_PlaylistDestroy( p_main->p_playlist );

Sam Hocevar's avatar
 
Sam Hocevar committed
471
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
472
     * Free memcpy module if it was allocated
Sam Hocevar's avatar
 
Sam Hocevar committed
473
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
474
    if( p_main->p_memcpy_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
475
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
476
        module_Unneed( p_main->p_memcpy_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
477
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
478

Sam Hocevar's avatar
 
Sam Hocevar committed
479
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
480
     * Free module bank
Sam Hocevar's avatar
 
Sam Hocevar committed
481
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
482
    module_EndBank();
483 484 485 486

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
487
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
488
    system_End();
489 490
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
491

Michel Kaempf's avatar
Michel Kaempf committed
492 493 494
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
495
    intf_WarnMsg( 1, "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
496
    intf_MsgDestroy();
497

Sam Hocevar's avatar
 
Sam Hocevar committed
498 499 500 501
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
502

Sam Hocevar's avatar
 
Sam Hocevar committed
503
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
504 505
}

506
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
507
 * main_GetIntVariable: get the int value of an environment variable
508
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
509
 * This function is used to read some default parameters in modules.
510
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
511 512
int main_GetIntVariable( char *psz_name, int i_default )
{
513 514 515
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
516 517 518

    psz_env = getenv( psz_name );
    if( psz_env )
519
    {
520 521 522 523
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
524
        }
525
    }
Vincent Seguin's avatar
Vincent Seguin committed
526 527 528
    return( i_default );
}

529
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
530
 * main_GetPszVariable: get the string value of an environment variable
531
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
532
 * This function is used to read some default parameters in modules.
533
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
534 535 536 537 538 539 540
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
541
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
542
    }
543
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
544 545
}

546
/*****************************************************************************
547
 * main_PutPszVariable: set the string value of an environment variable
548
 *****************************************************************************
549 550 551
 * 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.
552
 *****************************************************************************/
553 554 555 556 557 558 559
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
560 561
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
562 563 564 565 566 567
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
568
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
569
        }
570 571 572
    }
}

573
/*****************************************************************************
574
 * main_PutIntVariable: set the integer value of an environment variable
575
 *****************************************************************************
576 577 578
 * 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.
579
 *****************************************************************************/
580 581
void main_PutIntVariable( char *psz_name, int i_value )
{
582
    char psz_value[ 256 ];                               /* buffer for value */
583

584
    sprintf( psz_value, "%d", i_value );
585
    main_PutPszVariable( psz_name, psz_value );
586 587
}

Michel Kaempf's avatar
Michel Kaempf committed
588 589
/* following functions are local */

590
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
591
 * GetConfiguration: parse command line
592
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
593 594
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
595
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
596 597
 * stage, but most structures are not allocated, so only environment should
 * be used.
598
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
599
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
600
{
Sam Hocevar's avatar
 
Sam Hocevar committed
601 602
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
603 604

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
605
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
606
    p_main->ppsz_argv = ppsz_argv;
607
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
608

Henri Fallon's avatar
 
Henri Fallon committed
609 610
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
611

612
    p_main->i_warning_level = 0;
613
    p_main->b_stats = 0;
614
    p_main->i_desync = 0; /* No desynchronization by default */
Michel Kaempf's avatar
Michel Kaempf committed
615

616 617
    p_main->p_channel = NULL;

618
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
619 620
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
621
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
622
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
623
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
624
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
625 626 627
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
628
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
629 630 631
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
632
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
633 634 635
    /* 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
636 637
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
638
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
639 640 641 642 643 644 645 646 647
        /* 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--;
648 649 650 651
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
652
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
653
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
654 655
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
656
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
657
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
658
        {
659 660
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
661
            ShowConsole();
662
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
663
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
Sam Hocevar's avatar
 
Sam Hocevar committed
664 665
            intf_Msg( "\nPress the RETURN key to continue..." );
            getchar();
gbazin's avatar
 
gbazin committed
666
#endif
667 668
            return( -1 );
            break;
669
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
670
            ShowConsole();
671
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
672
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
Sam Hocevar's avatar
 
Sam Hocevar committed
673 674
            intf_Msg( "\nPress the RETURN key to continue..." );
            getchar();
gbazin's avatar
 
gbazin committed
675
#endif
Michel Kaempf's avatar
Michel Kaempf committed
676 677
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
678
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
679
            ShowConsole();
Vincent Seguin's avatar
Vincent Seguin committed
680
            Version();
gbazin's avatar
 
gbazin committed
681
#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
Sam Hocevar's avatar
 
Sam Hocevar committed
682 683
            intf_Msg( "\nPress the RETURN key to continue..." );
            getchar();
gbazin's avatar
 
gbazin committed
684
#endif
Vincent Seguin's avatar
Vincent Seguin committed
685
            return( -1 );
686
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
687
        case 'v':                                           /* -v, --verbose */
688
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
689
            break;
Michel Kaempf's avatar
Michel Kaempf committed
690

Sam Hocevar's avatar
 
Sam Hocevar committed
691
        /* Interface warning messages level */
Sam Hocevar's avatar
 
Sam Hocevar committed
692
        case 'I':                                              /* -I, --intf */
Sam Hocevar's avatar
 
Sam Hocevar committed
693 694 695
            main_PutPszVariable( INTF_METHOD_VAR, optarg );
            break;
        case OPT_WARNING:                                       /* --warning */
Sam Hocevar's avatar
 
Sam Hocevar committed
696 697
            intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
            p_main->i_warning_level = atoi(optarg);
Sam Hocevar's avatar
 
Sam Hocevar committed
698 699
            break;

700 701 702 703
        case OPT_STATS:
            p_main->b_stats = 1;
            break;

Michel Kaempf's avatar
Michel Kaempf committed
704
        /* Audio options */
705
        case OPT_NOAUDIO:                                       /* --noaudio */
706
            p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
707
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
708
        case 'A':                                              /* -A, --aout */
709 710
            main_PutPszVariable( AOUT_METHOD_VAR, optarg );
            break;
711
        case OPT_STEREO:                                         /* --stereo */
712
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
713
            break;
714
        case OPT_MONO:                                             /* --mono */
715
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
716
            break;
717
        case OPT_SPDIF:                                           /* --spdif */
Sam Hocevar's avatar
 
Sam Hocevar committed
718
            main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
719
            break;
gbazin's avatar
 
gbazin committed
720
        case OPT_DOWNMIX:                                       /* --downmix */
Sam Hocevar's avatar
 
Sam Hocevar committed
721 722 723 724 725
            main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
            break;
        case OPT_IMDCT:                                           /* --imdct */
            main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
            break;
726 727 728
        case OPT_VOLUME:                                         /* --volume */
            main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
            break;
729
        case OPT_DESYNC:                                         /* --desync */
Sam Hocevar's avatar
 
Sam Hocevar committed
730
            p_main->i_desync = (mtime_t)atoi(optarg) * (mtime_t)1000;
731
            break;
Michel Kaempf's avatar
Michel Kaempf committed
732 733

        /* Video options */
734
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
735
            p_main->b_video = 0;
736
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
737
        case 'V':                                              /* -V, --vout */
738 739
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
740
        case OPT_DISPLAY:                                       /* --display */
741
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
742 743
            break;
        case OPT_WIDTH:                                           /* --width */
744
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
745 746 747 748 749
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
750
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
Sam Hocevar's avatar