main.c 50.5 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.141 2001/12/30 07:09:56 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 106
#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
107 108
#define OPT_XVADAPTOR           167
#define OPT_SMP                 168
Sam Hocevar's avatar
 
Sam Hocevar committed
109
#define OPT_FILTER              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 124 125
#define OPT_MEMCPY              186
#define OPT_DVDCSS_METHOD       187
#define OPT_DVDCSS_VERBOSE      188
126

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

133 134
#define OPT_MPEG_ADEC           200

135
/* Usage fashion */
136 137 138
#define USAGE                     0
#define SHORT_HELP                1
#define LONG_HELP                 2
139

Michel Kaempf's avatar
Michel Kaempf committed
140 141
/* Long options */
static const struct option longopts[] =
142 143
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
144 145

    /* General/common options */
146
    {   "help",             0,          0,      'h' },
147
    {   "longhelp",         0,          0,      'H' },
Sam Hocevar's avatar
 
Sam Hocevar committed
148
    {   "version",          0,          0,      OPT_VERSION },
Michel Kaempf's avatar
Michel Kaempf committed
149

Sam Hocevar's avatar
 
Sam Hocevar committed
150
    /* Interface options */
Sam Hocevar's avatar
 
Sam Hocevar committed
151
    {   "intf",             1,          0,      'I' },
Sam Hocevar's avatar
 
Sam Hocevar committed
152
    {   "warning",          1,          0,      OPT_WARNING },
gbazin's avatar
 
gbazin committed
153
    {   "stdout",           1,          0,      OPT_STDOUT },
154
    {   "stats",            0,          0,      OPT_STATS },
Sam Hocevar's avatar
 
Sam Hocevar committed
155

Michel Kaempf's avatar
Michel Kaempf committed
156
    /* Audio options */
157
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Sam Hocevar's avatar
 
Sam Hocevar committed
158
    {   "aout",             1,          0,      'A' },
Michel Kaempf's avatar
Michel Kaempf committed
159
    {   "stereo",           0,          0,      OPT_STEREO },
160
    {   "mono",             0,          0,      OPT_MONO },
161
    {   "spdif",            0,          0,      OPT_SPDIF },
Sam Hocevar's avatar
 
Sam Hocevar committed
162 163
    {   "downmix",          1,          0,      OPT_DOWNMIX },
    {   "imdct",            1,          0,      OPT_IMDCT },
164
    {   "volume",           1,          0,      OPT_VOLUME },
165
    {   "desync",           1,          0,      OPT_DESYNC },
Michel Kaempf's avatar
Michel Kaempf committed
166 167

    /* Video options */
168
    {   "novideo",          0,          0,      OPT_NOVIDEO },
Sam Hocevar's avatar
 
Sam Hocevar committed
169
    {   "vout",             1,          0,      'V' },
170
    {   "display",          1,          0,      OPT_DISPLAY },
171
    {   "width",            1,          0,      OPT_WIDTH },
172 173 174
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Sam Hocevar's avatar
 
Sam Hocevar committed
175
    {   "motion",           1,          0,      OPT_MOTION },
Sam Hocevar's avatar
 
Sam Hocevar committed
176
    {   "idct",             1,          0,      OPT_IDCT },
Sam Hocevar's avatar
Sam Hocevar committed
177
    {   "yuv",              1,          0,      OPT_YUV },
Sam Hocevar's avatar
 
Sam Hocevar committed
178 179
    {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
    {   "overlay",          0,          0,      OPT_OVERLAY },
Sam Hocevar's avatar
 
Sam Hocevar committed
180
    {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
181
    {   "smp",              1,          0,      OPT_SMP },
Sam Hocevar's avatar
 
Sam Hocevar committed
182
    {   "filter",           1,          0,      OPT_FILTER },
Michel Kaempf's avatar
Michel Kaempf committed
183

184
    /* DVD options */
185 186
    {   "dvdtitle",         1,          0,      't' },
    {   "dvdchapter",       1,          0,      'T' },
187
    {   "dvdangle",         1,          0,      'u' },
188 189 190
    {   "dvdaudio",         1,          0,      'a' },
    {   "dvdchannel",       1,          0,      'c' },
    {   "dvdsubtitle",      1,          0,      's' },
Sam Hocevar's avatar
 
Sam Hocevar committed
191 192
    {   "dvdcss-method",    1,          0,      OPT_DVDCSS_METHOD },
    {   "dvdcss-verbose",   1,          0,      OPT_DVDCSS_VERBOSE },
Sam Hocevar's avatar
 
Sam Hocevar committed
193

194
    /* Input options */
Sam Hocevar's avatar
 
Sam Hocevar committed
195
    {   "input",            1,          0,      OPT_INPUT },
196
    {   "channels",         0,          0,      OPT_CHANNELS },
197
    {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
Michel Kaempf's avatar
Michel Kaempf committed
198

Sam Hocevar's avatar
 
Sam Hocevar committed
199
    /* Misc options */
200
    {   "synchro",          1,          0,      OPT_SYNCHRO },
Sam Hocevar's avatar
 
Sam Hocevar committed
201
    {   "memcpy",           1,          0,      OPT_MEMCPY },
202 203 204 205

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

Michel Kaempf's avatar
Michel Kaempf committed
206 207 208 209
    {   0,                  0,          0,      0 }
};

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

212
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
213
 * Global variables - these are the only ones, see main.h and modules.h
214
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
215 216 217 218
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
219

220
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
221
 * Local prototypes
222
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
223
static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
224
                                      char *ppsz_env[] );
Sam Hocevar's avatar
 
Sam Hocevar committed
225
static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
226
static void Usage                   ( int i_fashion );
Vincent Seguin's avatar
Vincent Seguin committed
227
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
228

Michel Kaempf's avatar
Michel Kaempf committed
229
static void InitSignalHandler       ( void );
Benoit Steiner's avatar
 
Benoit Steiner committed
230 231
static void SimpleSignalHandler     ( int i_signal );
static void FatalSignalHandler      ( int i_signal );
232
static void InstructionSignalHandler( int i_signal );
Sam Hocevar's avatar
 
Sam Hocevar committed
233
static u32  CPUCapabilities         ( void );
gbazin's avatar
 
gbazin committed
234 235 236 237

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

238 239
static jmp_buf env;
static int  i_illegal;
Sam Hocevar's avatar
 
Sam Hocevar committed
240

241
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
242
 * main: parse command line, start interface and spawn threads
243
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
244 245
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
246
 *      -opening of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
247
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
248
 *      -closing of audio output device and some global modules
249
 * On error, the spawned threads are canceled, and the open devices closed.
250
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
251 252
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
253 254 255 256
    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;
257

Sam Hocevar's avatar
 
Sam Hocevar committed
258 259 260 261
    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
262

Sam Hocevar's avatar
 
Sam Hocevar committed
263
#ifdef ENABLE_NLS
Sam Hocevar's avatar
 
Sam Hocevar committed
264
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
265 266
     * Support for getext
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
267 268
#if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
    if( !setlocale( LC_MESSAGES, "" ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
269 270 271
    {
        fprintf( stderr, "warning: unsupported locale.\n" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
272
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
273

Sam Hocevar's avatar
 
Sam Hocevar committed
274
    if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
275 276 277 278 279 280 281
    {
        fprintf( stderr, "warning: no domain %s in directory %s\n",
                 PACKAGE, LOCALEDIR );
    }

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

Sam Hocevar's avatar
 
Sam Hocevar committed
283
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
284
     * Initialize threads system
Sam Hocevar's avatar
 
Sam Hocevar committed
285
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
286
    vlc_threads_init( );
Sam Hocevar's avatar
 
Sam Hocevar committed
287

Sam Hocevar's avatar
 
Sam Hocevar committed
288
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
289
     * Test if our code is likely to run on this CPU
Sam Hocevar's avatar
 
Sam Hocevar committed
290 291
     */
    p_main->i_cpu_capabilities = CPUCapabilities();
Sam Hocevar's avatar
 
Sam Hocevar committed
292

Sam Hocevar's avatar
 
Sam Hocevar committed
293 294 295
    /*
     * System specific initialization code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
296
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
297
    system_Init( &i_argc, ppsz_argv, ppsz_env );
298 299 300 301 302 303

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

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

317
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
Sam Hocevar's avatar
 
Sam Hocevar committed
318

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

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

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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" );
Christophe Massiot's avatar
Christophe Massiot committed
355
	PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
Sam Hocevar's avatar
 
Sam Hocevar committed
356
        intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
357 358
    }

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
383 384 385
    /*
     * Choose the best memcpy module
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
386
    if( module_NeedMemcpy( &p_main->memcpy ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
387 388 389
    {
        intf_ErrMsg( "intf error: no suitable memcpy module, "
                     "using libc default" );
Sam Hocevar's avatar
 
Sam Hocevar committed
390
        p_main->memcpy.pf_memcpy = memcpy;
Sam Hocevar's avatar
 
Sam Hocevar committed
391
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
392

Michel Kaempf's avatar
Michel Kaempf committed
393 394 395
    /*
     * Initialize shared resources and libraries
     */
396 397 398
    if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                             INPUT_NETWORK_CHANNEL_DEFAULT ) &&
        network_ChannelCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
399
    {
Henri Fallon's avatar
 
Henri Fallon committed
400 401 402
        /* On error during Channels initialization, switch off channels */
        intf_Msg( "Channels initialization failed : "
                  "Channel management is deactivated" );
403
        main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
404
    }
405

Michel Kaempf's avatar
Michel Kaempf committed
406
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
407
     * Try to run the interface
Michel Kaempf's avatar
Michel Kaempf committed
408
     */
409
    p_main->p_intf = intf_Create();
Sam Hocevar's avatar
 
Sam Hocevar committed
410
    if( p_main->p_intf == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
411 412 413
    {
        intf_ErrMsg( "intf error: interface initialization failed" );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
414
    else
415
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
        /*
         * 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
         */
434 435
        if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                                 INPUT_NETWORK_CHANNEL_DEFAULT ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
436 437 438
        {
            network_ChannelJoin( COMMON_CHANNEL );
        }
439
    }
Michel Kaempf's avatar
Michel Kaempf committed
440

Sam Hocevar's avatar
 
Sam Hocevar committed
441
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
442
     * Free memcpy module if it was allocated
Sam Hocevar's avatar
 
Sam Hocevar committed
443
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
444
    if( p_main->memcpy.p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
445
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
446
        module_UnneedMemcpy( &p_main->memcpy );
Sam Hocevar's avatar
 
Sam Hocevar committed
447
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
448

Sam Hocevar's avatar
 
Sam Hocevar committed
449
    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
450
     * Free module, aout and vout banks
Sam Hocevar's avatar
 
Sam Hocevar committed
451
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
452 453
    vout_EndBank();
    aout_EndBank();
Sam Hocevar's avatar
 
Sam Hocevar committed
454
    module_EndBank();
455 456 457 458

    /*
     * Free playlist
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
459
    intf_PlaylistDestroy( p_main->p_playlist );
460 461 462 463

    /*
     * System specific cleaning code
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
464
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
465
    system_End();
466 467
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
468

Michel Kaempf's avatar
Michel Kaempf committed
469 470 471
    /*
     * Terminate messages interface and program
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
472
    intf_Msg( "intf: program terminated" );
Vincent Seguin's avatar
Vincent Seguin committed
473
    intf_MsgDestroy();
474

Sam Hocevar's avatar
 
Sam Hocevar committed
475 476 477 478
    /*
     * Stop threads system
     */
    vlc_threads_end( );
Sam Hocevar's avatar
 
Sam Hocevar committed
479

Sam Hocevar's avatar
 
Sam Hocevar committed
480
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
481 482
}

483
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
484
 * main_GetIntVariable: get the int value of an environment variable
485
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
486
 * This function is used to read some default parameters in modules.
487
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
488 489
int main_GetIntVariable( char *psz_name, int i_default )
{
490 491 492
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
493 494 495

    psz_env = getenv( psz_name );
    if( psz_env )
496
    {
497 498 499 500
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
501
        }
502
    }
Vincent Seguin's avatar
Vincent Seguin committed
503 504 505
    return( i_default );
}

506
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
507
 * main_GetPszVariable: get the string 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 513 514 515 516 517
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
518
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
519
    }
520
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
521 522
}

523
/*****************************************************************************
524
 * main_PutPszVariable: set the string value of an environment variable
525
 *****************************************************************************
526 527 528
 * 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.
529
 *****************************************************************************/
530 531 532 533 534 535 536
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
537 538
        intf_ErrMsg( "intf error: cannot create psz_env (%s)",
                     strerror(ENOMEM) );
539 540 541 542 543 544
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
545
            intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
546
        }
547 548 549
    }
}

550
/*****************************************************************************
551
 * main_PutIntVariable: set the integer value of an environment variable
552
 *****************************************************************************
553 554 555
 * 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.
556
 *****************************************************************************/
557 558
void main_PutIntVariable( char *psz_name, int i_value )
{
559
    char psz_value[ 256 ];                               /* buffer for value */
560

561
    sprintf( psz_value, "%d", i_value );
562
    main_PutPszVariable( psz_name, psz_value );
563 564
}

Michel Kaempf's avatar
Michel Kaempf committed
565 566
/* following functions are local */

567
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
568
 * GetConfiguration: parse command line
569
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
570 571
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
572
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
573 574
 * stage, but most structures are not allocated, so only environment should
 * be used.
575
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
576
static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
577
{
Sam Hocevar's avatar
 
Sam Hocevar committed
578 579
    int   i_cmd;
    char *p_tmp;
Michel Kaempf's avatar
Michel Kaempf committed
580 581

    /* Set default configuration and copy arguments */
Sam Hocevar's avatar
 
Sam Hocevar committed
582
    p_main->i_argc    = *pi_argc;
Vincent Seguin's avatar
Vincent Seguin committed
583
    p_main->ppsz_argv = ppsz_argv;
584
    p_main->ppsz_env  = ppsz_env;
Sam Hocevar's avatar
 
Sam Hocevar committed
585

Henri Fallon's avatar
 
Henri Fallon committed
586 587
    p_main->b_audio     = 1;
    p_main->b_video     = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
588

589
    p_main->i_warning_level = 0;
590
    p_main->b_stats = 0;
591
    p_main->i_desync = 0; /* No desynchronization by default */
Michel Kaempf's avatar
Michel Kaempf committed
592

593 594
    p_main->p_channel = NULL;

595
    /* Get the executable name (similar to the basename command) */
Sam Hocevar's avatar
 
Sam Hocevar committed
596 597
    p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
    while( *p_tmp )
Sam Hocevar's avatar
Sam Hocevar committed
598
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
599
        if( *p_tmp == '/' )
Sam Hocevar's avatar
Sam Hocevar committed
600
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
601
            p_main->psz_arg0 = ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
602 603 604
        }
        else
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
605
            ++p_tmp;
Sam Hocevar's avatar
Sam Hocevar committed
606 607 608
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
609
#ifdef SYS_DARWIN
Sam Hocevar's avatar
 
Sam Hocevar committed
610 611 612
    /* 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
613 614
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
615
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
616 617 618 619 620 621 622 623 624
        /* 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--;
625 626 627 628
        return( 0 );
    }
#endif

Vincent Seguin's avatar
Vincent Seguin committed
629
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
630
    opterr = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
631 632
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
                                   psz_shortopts, longopts, 0 ) ) != EOF )
Michel Kaempf's avatar
Michel Kaempf committed
633
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
634
        switch( i_cmd )
Michel Kaempf's avatar
Michel Kaempf committed
635
        {
636 637
        /* General/common options */
        case 'h':                                              /* -h, --help */
gbazin's avatar
 
gbazin committed
638 639
            ShowConsole();
            RedirectSTDOUT();
640
            Usage( SHORT_HELP );
gbazin's avatar
 
gbazin committed
641 642 643 644 645 646 647 648 649
#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
650 651
            return( -1 );
            break;
652
        case 'H':                                          /* -H, --longhelp */
gbazin's avatar
 
gbazin committed
653 654
            ShowConsole();
            RedirectSTDOUT();
655
            Usage( LONG_HELP );
gbazin's avatar
 
gbazin committed
656 657 658 659 660 661 662 663 664
#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
665 666
            return( -1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
667
        case OPT_VERSION:                                       /* --version */
gbazin's avatar
 
gbazin committed
668 669
            ShowConsole();
            RedirectSTDOUT();
Vincent Seguin's avatar
Vincent Seguin committed
670
            Version();
gbazin's avatar
 
gbazin committed
671 672 673 674 675 676 677 678 679
#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
680
            return( -1 );
681
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
682
        case 'v':                                           /* -v, --verbose */
683
            p_main->i_warning_level++;
Sam Hocevar's avatar
 
Sam Hocevar committed
684
            break;
Michel Kaempf's avatar
Michel Kaempf committed
685

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

gbazin's avatar
 
gbazin committed
695 696 697 698
        case OPT_STDOUT:                                         /* --stdout */
            main_PutPszVariable( INTF_STDOUT_VAR, optarg );
            break;

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

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

        /* Video options */
733
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
734
            p_main->b_video = 0;
735
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
736
        case 'V':                                              /* -V, --vout */
737 738
            main_PutPszVariable( VOUT_METHOD_VAR, optarg );
            break;
739
        case OPT_DISPLAY:                                       /* --display */
740
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
741 742
            break;
        case OPT_WIDTH:                                           /* --width */
743
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
744 745 746 747 748
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;
        case 'g':                                         /* -g, --grayscale */
749
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
750 751
            break;
        case OPT_COLOR:                                           /* --color */
752
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
753
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
754
        case OPT_FULLSCREEN:                                 /* --fullscreen */
Sam Hocevar's avatar
 
Sam Hocevar committed
755 756
            main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
757
        case OPT_OVERLAY:                                       /* --overlay */
Sam Hocevar's avatar
 
Sam Hocevar committed
758 759
            main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
760 761 762
        case OPT_XVADAPTOR:                                   /* --xvadaptor */
            main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
763
        case OPT_MOTION:                                         /* --motion */
Sam Hocevar's avatar
 
Sam Hocevar committed
764 765
            main_PutPszVariable( MOTION_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
766
        case OPT_IDCT:                                             /* --idct */
Sam Hocevar's avatar
 
Sam Hocevar committed
767 768
            main_PutPszVariable( IDCT_METHOD_VAR, optarg );
            break;
Sam Hocevar's avatar
Sam Hocevar committed
769 770 771
        case OPT_YUV:                                               /* --yuv */
            main_PutPszVariable( YUV_METHOD_VAR, optarg );
            break;