]> git.sesse.net Git - vlc/blob - src/interface/main.c
New --channelserver switch.
[vlc] / src / interface / main.c
1 /*****************************************************************************
2  * main.c: main vlc source
3  * Includes the main() function for vlc. Parses command line, start interface
4  * and spawn threads.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  * $Id: main.c,v 1.122 2001/11/12 12:54:16 massiot Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
33 #include <stdio.h>                                              /* sprintf() */
34 #include <setjmp.h>                                       /* longjmp, setjmp */
35
36 #ifdef HAVE_GETOPT_LONG
37 #   ifdef HAVE_GETOPT_H
38 #       include <getopt.h>                                       /* getopt() */
39 #   endif
40 #else
41 #   include "GNUgetopt/getopt.h"
42 #endif
43
44 #ifdef SYS_DARWIN
45 #   include <mach/mach.h>                               /* Altivec detection */
46 #   include <mach/mach_error.h>       /* some day the header files||compiler *
47                                                        will define it for us */
48 #   include <mach/bootstrap.h>
49 #endif
50
51 #ifndef WIN32
52 #   include <netinet/in.h>                            /* BSD: struct in_addr */
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h>
57 #elif defined( _MSC_VER ) && defined( _WIN32 )
58 #   include <io.h>
59 #endif
60
61 #include <errno.h>                                                 /* ENOMEM */
62 #include <stdlib.h>                                  /* getenv(), strtol(),  */
63 #include <string.h>                                            /* strerror() */
64 #include <fcntl.h>                                       /* open(), O_WRONLY */
65 #include <sys/stat.h>                                             /* S_IREAD */
66
67 #include "config.h"
68 #include "common.h"
69 #include "debug.h"
70 #include "threads.h"
71 #include "mtime.h"
72 #include "tests.h"                                              /* TestCPU() */
73 #include "modules.h"
74
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77
78 #include "intf_msg.h"
79 #include "intf_playlist.h"
80 #include "interface.h"
81
82 #include "audio_output.h"
83
84 #include "video.h"
85 #include "video_output.h"
86
87 #ifdef SYS_BEOS
88 #   include "beos_specific.h"
89 #endif
90
91 #ifdef SYS_DARWIN
92 #   include "darwin_specific.h"
93 #endif
94
95 #include "netutils.h"                                 /* network_ChannelJoin */
96
97 #include "main.h"
98
99 /*****************************************************************************
100  * Command line options constants. If something is changed here, be sure that
101  * GetConfiguration and Usage are also changed.
102  *****************************************************************************/
103
104 /* Long options return values - note that values corresponding to short options
105  * chars, and in general any regular char, should be avoided */
106 #define OPT_NOAUDIO             150
107 #define OPT_STEREO              151
108 #define OPT_MONO                152
109 #define OPT_SPDIF               153
110 #define OPT_VOLUME              154
111
112 #define OPT_NOVIDEO             160
113 #define OPT_DISPLAY             161
114 #define OPT_WIDTH               162
115 #define OPT_HEIGHT              163
116 #define OPT_COLOR               164
117 #define OPT_FULLSCREEN          165
118 #define OPT_OVERLAY             166
119 #define OPT_XVADAPTOR           167
120 #define OPT_SMP                 168
121
122 #define OPT_CHANNELS            170
123 #define OPT_SERVER              171
124 #define OPT_PORT                172
125 #define OPT_BROADCAST           173
126 #define OPT_CHANNELSERVER       174
127
128 #define OPT_INPUT               180
129 #define OPT_MOTION              181
130 #define OPT_IDCT                182
131 #define OPT_YUV                 183
132 #define OPT_DOWNMIX             184
133 #define OPT_IMDCT               185
134 #define OPT_DVDCSS              186
135
136 #define OPT_SYNCHRO             190
137 #define OPT_WARNING             191
138 #define OPT_VERSION             192
139 #define OPT_STDOUT              193
140 #define OPT_STATS               194
141
142 /* Usage fashion */
143 #define USAGE                     0
144 #define SHORT_HELP                1
145 #define LONG_HELP                 2
146
147 /* Needed for x86 CPU capabilities detection */
148 #define cpuid( a )                 \
149     asm volatile ( "cpuid"         \
150                  : "=a" ( i_eax ), \
151                    "=b" ( i_ebx ), \
152                    "=c" ( i_ecx ), \
153                    "=d" ( i_edx )  \
154                  : "a"  ( a )      \
155                  : "cc" );
156
157 /* Long options */
158 static const struct option longopts[] =
159 {
160     /*  name,               has_arg,    flag,   val */
161
162     /* General/common options */
163     {   "help",             0,          0,      'h' },
164     {   "longhelp",         0,          0,      'H' },
165     {   "version",          0,          0,      OPT_VERSION },
166
167     /* Interface options */
168     {   "intf",             1,          0,      'I' },
169     {   "warning",          1,          0,      OPT_WARNING },
170     {   "stdout",           1,          0,      OPT_STDOUT },
171     {   "stats",            0,          0,      OPT_STATS },
172
173     /* Audio options */
174     {   "noaudio",          0,          0,      OPT_NOAUDIO },
175     {   "aout",             1,          0,      'A' },
176     {   "stereo",           0,          0,      OPT_STEREO },
177     {   "mono",             0,          0,      OPT_MONO },
178     {   "spdif",            0,          0,      OPT_SPDIF },
179     {   "downmix",          1,          0,      OPT_DOWNMIX },
180     {   "imdct",            1,          0,      OPT_IMDCT },
181     {   "volume",           1,          0,      OPT_VOLUME },
182
183     /* Video options */
184     {   "novideo",          0,          0,      OPT_NOVIDEO },
185     {   "vout",             1,          0,      'V' },
186     {   "display",          1,          0,      OPT_DISPLAY },
187     {   "width",            1,          0,      OPT_WIDTH },
188     {   "height",           1,          0,      OPT_HEIGHT },
189     {   "grayscale",        0,          0,      'g' },
190     {   "color",            0,          0,      OPT_COLOR },
191     {   "motion",           1,          0,      OPT_MOTION },
192     {   "idct",             1,          0,      OPT_IDCT },
193     {   "yuv",              1,          0,      OPT_YUV },
194     {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
195     {   "overlay",          0,          0,      OPT_OVERLAY },
196     {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
197     {   "smp",              1,          0,      OPT_SMP },
198
199     /* DVD options */
200     {   "dvdtitle",         1,          0,      't' },
201     {   "dvdchapter",       1,          0,      'T' },
202     {   "dvdangle",         1,          0,      'u' },
203     {   "dvdaudio",         1,          0,      'a' },
204     {   "dvdchannel",       1,          0,      'c' },
205     {   "dvdsubtitle",      1,          0,      's' },
206     {   "dvdcss_method",    1,          0,      OPT_DVDCSS },
207     
208     /* Input options */
209     {   "input",            1,          0,      OPT_INPUT },
210     {   "server",           1,          0,      OPT_SERVER },
211     {   "port",             1,          0,      OPT_PORT },
212     {   "broadcast",        1,          0,      OPT_BROADCAST },
213     {   "channels",         0,          0,      OPT_CHANNELS },
214     {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
215
216     /* Synchro options */
217     {   "synchro",          1,          0,      OPT_SYNCHRO },
218     {   0,                  0,          0,      0 }
219 };
220
221 /* Short options */
222 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
223
224 /*****************************************************************************
225  * Global variable program_data - these are the only ones, see main.h and
226  * modules.h
227  *****************************************************************************/
228 main_t        *p_main;
229 module_bank_t *p_module_bank;
230 aout_bank_t   *p_aout_bank;
231 vout_bank_t   *p_vout_bank;
232
233 /*****************************************************************************
234  * Local prototypes
235  *****************************************************************************/
236 static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
237                                       char *ppsz_env[] );
238 static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
239 static void Usage                   ( int i_fashion );
240 static void Version                 ( void );
241
242 static void InitSignalHandler       ( void );
243 static void SimpleSignalHandler     ( int i_signal );
244 static void FatalSignalHandler      ( int i_signal );
245 static void InstructionSignalHandler( int i_signal );
246 static int  CPUCapabilities         ( void );
247
248 static int  RedirectSTDOUT          ( void );
249 static void ShowConsole             ( void );
250
251 static jmp_buf env;
252 static int  i_illegal;
253
254 /*****************************************************************************
255  * main: parse command line, start interface and spawn threads
256  *****************************************************************************
257  * Steps during program execution are:
258  *      -configuration parsing and messages interface initialization
259  *      -opening of audio output device and some global modules
260  *      -execution of interface, which exit on error or on user request
261  *      -closing of audio output device and some global modules
262  * On error, the spawned threads are canceled, and the open devices closed.
263  *****************************************************************************/
264 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
265 {
266     main_t        main_data;                /* root of all data - see main.h */
267     module_bank_t module_bank;
268     aout_bank_t   aout_bank;
269     vout_bank_t   vout_bank;
270
271     p_main        = &main_data;               /* set up the global variables */
272     p_module_bank = &module_bank;
273     p_aout_bank   = &aout_bank;
274     p_vout_bank   = &vout_bank;
275
276     /*
277      * Initialize threads system
278      */
279     vlc_threads_init( );
280
281     /*
282      * Test if our code is likely to run on this CPU 
283      */
284     p_main->i_cpu_capabilities = CPUCapabilities();
285     
286     /*
287      * System specific initialization code
288      */
289 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
290     system_Init( &i_argc, ppsz_argv, ppsz_env );
291
292 #elif defined( WIN32 )
293     _fmode = _O_BINARY;   /* sets the default file-translation mode on Win32 */
294
295 #elif defined( SYS_LINUX )
296 #   ifdef DEBUG
297     /* Activate malloc checking routines to detect heap corruptions. */
298     main_PutIntVariable( "MALLOC_CHECK_", 2 );
299 #   endif
300 #endif
301
302     /*
303      * Initialize messages interface
304      */
305     p_main->p_msg = intf_MsgCreate();
306     if( !p_main->p_msg )                         /* start messages interface */
307     {
308         fprintf( stderr, "error: can't initialize messages interface (%s)\n",
309                  strerror(errno) );
310         return( errno );
311     }
312
313     intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
314
315     /*
316      * Read configuration
317      */
318     if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
319     {
320         intf_MsgDestroy();
321         return( errno );
322     }
323
324     /*
325      * Redirect the standard output if required by the user, and on Win32 we
326      * also open a console to display the debug messages.
327      */
328     RedirectSTDOUT();
329
330     if( p_main->b_stats )
331     {
332         char          p_capabilities[200];
333         p_capabilities[0] = '\0';
334
335 #define PRINT_CAPABILITY( capability, string )                              \
336         if( p_main->i_cpu_capabilities & capability )                       \
337         {                                                                   \
338             strncat( p_capabilities, string " ",                            \
339                      sizeof(p_capabilities) - strlen(p_capabilities) );     \
340             p_capabilities[sizeof(p_capabilities) - 1] = '\0';              \
341         }
342
343         PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
344         PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
345         PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
346         PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
347         PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
348         PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
349         PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
350         PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
351         intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
352     }
353
354     /*
355      * Initialize playlist and get commandline files
356      */
357     p_main->p_playlist = intf_PlaylistCreate();
358     if( !p_main->p_playlist )
359     {
360         intf_ErrMsg( "playlist error: playlist initialization failed" );
361         intf_MsgDestroy();
362         return( errno );
363     }
364     intf_PlaylistInit( p_main->p_playlist );
365
366     /*
367      * Get input filenames given as commandline arguments
368      */
369     GetFilenames( i_argc, ppsz_argv );
370
371     /*
372      * Initialize module, aout and vout banks
373      */
374     module_InitBank();
375     aout_InitBank();
376     vout_InitBank();
377
378     /*
379      * Initialize shared resources and libraries
380      */
381     if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
382                              INPUT_NETWORK_CHANNEL_DEFAULT ) &&
383         network_ChannelCreate() )
384     {
385         /* On error during Channels initialization, switch off channels */
386         intf_Msg( "Channels initialization failed : "
387                   "Channel management is deactivated" );
388         main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
389     }
390
391     /*
392      * Try to run the interface
393      */
394     p_main->p_intf = intf_Create();
395     if( p_main->p_intf == NULL )
396     {
397         intf_ErrMsg( "intf error: interface initialization failed" );
398     }
399     else
400     {
401         /*
402          * Set signal handling policy for all threads
403          */
404         InitSignalHandler();
405
406         /*
407          * This is the main loop
408          */
409         p_main->p_intf->pf_run( p_main->p_intf );
410
411         /*
412          * Finished, destroy the interface
413          */
414         intf_Destroy( p_main->p_intf );
415
416         /*
417          * Go back into channel 0 which is the network
418          */
419         if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
420                                  INPUT_NETWORK_CHANNEL_DEFAULT ) )
421         {
422             network_ChannelJoin( COMMON_CHANNEL );
423         }
424     }
425
426     /*
427      * Free module, aout and vout banks
428      */
429     vout_EndBank();
430     aout_EndBank();
431     module_EndBank();
432
433     /*
434      * Free playlist
435      */
436     intf_PlaylistDestroy( p_main->p_playlist );
437
438     /*
439      * System specific cleaning code
440      */
441 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
442     system_End();
443 #endif
444
445
446     /*
447      * Terminate messages interface and program
448      */
449     intf_Msg( "intf: program terminated" );
450     intf_MsgDestroy();
451
452     /*
453      * Stop threads system
454      */
455     vlc_threads_end( );
456
457     return 0;
458 }
459
460 /*****************************************************************************
461  * main_GetIntVariable: get the int value of an environment variable
462  *****************************************************************************
463  * This function is used to read some default parameters in modules.
464  *****************************************************************************/
465 int main_GetIntVariable( char *psz_name, int i_default )
466 {
467     char *      psz_env;                                /* environment value */
468     char *      psz_end;                             /* end of parsing index */
469     long int    i_value;                                            /* value */
470
471     psz_env = getenv( psz_name );
472     if( psz_env )
473     {
474         i_value = strtol( psz_env, &psz_end, 0 );
475         if( (*psz_env != '\0') && (*psz_end == '\0') )
476         {
477             return( i_value );
478         }
479     }
480     return( i_default );
481 }
482
483 /*****************************************************************************
484  * main_GetPszVariable: get the string value of an environment variable
485  *****************************************************************************
486  * This function is used to read some default parameters in modules.
487  *****************************************************************************/
488 char * main_GetPszVariable( char *psz_name, char *psz_default )
489 {
490     char *psz_env;
491
492     psz_env = getenv( psz_name );
493     if( psz_env )
494     {
495         return( psz_env );
496     }
497     return( psz_default );
498 }
499
500 /*****************************************************************************
501  * main_PutPszVariable: set the string value of an environment variable
502  *****************************************************************************
503  * This function is used to set some default parameters in modules. The use of
504  * this function will cause some memory leak: since some systems use the pointer
505  * passed to putenv to store the environment string, it can't be freed.
506  *****************************************************************************/
507 void main_PutPszVariable( char *psz_name, char *psz_value )
508 {
509     char *psz_env;
510
511     psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
512     if( psz_env == NULL )
513     {
514         intf_ErrMsg( "intf error: cannot create psz_env (%s)",
515                      strerror(ENOMEM) );
516     }
517     else
518     {
519         sprintf( psz_env, "%s=%s", psz_name, psz_value );
520         if( putenv( psz_env ) )
521         {
522             intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
523         }
524     }
525 }
526
527 /*****************************************************************************
528  * main_PutIntVariable: set the integer value of an environment variable
529  *****************************************************************************
530  * This function is used to set some default parameters in modules. The use of
531  * this function will cause some memory leak: since some systems use the pointer
532  * passed to putenv to store the environment string, it can't be freed.
533  *****************************************************************************/
534 void main_PutIntVariable( char *psz_name, int i_value )
535 {
536     char psz_value[ 256 ];                               /* buffer for value */
537
538     sprintf( psz_value, "%d", i_value );
539     main_PutPszVariable( psz_name, psz_value );
540 }
541
542 /* following functions are local */
543
544 /*****************************************************************************
545  * GetConfiguration: parse command line
546  *****************************************************************************
547  * Parse command line and configuration file for configuration. If the inline
548  * help is requested, the function Usage() is called and the function returns
549  * -1 (causing main() to exit). The messages interface is initialized at this
550  * stage, but most structures are not allocated, so only environment should
551  * be used.
552  *****************************************************************************/
553 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
554 {
555     int   i_cmd;
556     char *p_tmp;
557
558     /* Set default configuration and copy arguments */
559     p_main->i_argc    = *pi_argc;
560     p_main->ppsz_argv = ppsz_argv;
561     p_main->ppsz_env  = ppsz_env;
562
563     p_main->b_audio     = 1;
564     p_main->b_video     = 1;
565
566     p_main->i_warning_level = 0;
567     p_main->b_stats = 0;
568
569     p_main->p_channel = NULL;
570
571     /* Get the executable name (similar to the basename command) */
572     p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
573     while( *p_tmp )
574     {
575         if( *p_tmp == '/' )
576         {
577             p_main->psz_arg0 = ++p_tmp;
578         }
579         else
580         {
581             ++p_tmp;
582         }
583     }
584
585 #ifdef SYS_DARWIN
586     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
587      * is the PSN - process serial number (a unique PID-ish thingie)
588      * still ok for real Darwin & when run from command line */
589     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
590                                         /* for example -psn_0_9306113 */
591     {
592         /* GDMF!... I can't do this or else the MacOSX window server will
593          * not pick up the PSN and not register the app and we crash...
594          * hence the following kludge otherwise we'll get confused w/ argv[1]
595          * being an input file name */
596 #if 0
597         ppsz_argv[ 1 ] = NULL;
598 #endif
599         *pi_argc = *pi_argc - 1;
600         pi_argc--;
601         return( 0 );
602     }
603 #endif
604
605     /* Parse command line options */
606     opterr = 0;
607     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
608                                    psz_shortopts, longopts, 0 ) ) != EOF )
609     {
610         switch( i_cmd )
611         {
612         /* General/common options */
613         case 'h':                                              /* -h, --help */
614             ShowConsole();
615             RedirectSTDOUT();
616             Usage( SHORT_HELP );
617 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
618             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
619                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
620             {
621                 /* No stdout redirection has been asked for */
622                 intf_MsgImm( "\nPress the RETURN key to continue..." );
623                 getchar();
624             }
625 #endif
626             return( -1 );
627             break;
628         case 'H':                                          /* -H, --longhelp */
629             ShowConsole();
630             RedirectSTDOUT();
631             Usage( LONG_HELP );
632 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
633             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
634                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
635             {
636                 /* No stdout redirection has been asked for */
637                 intf_MsgImm( "\nPress the RETURN key to continue..." );
638                 getchar();
639             }
640 #endif
641             return( -1 );
642             break;
643         case OPT_VERSION:                                       /* --version */
644             ShowConsole();
645             RedirectSTDOUT();
646             Version();
647 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
648             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
649                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
650             {
651                 /* No stdout redirection has been asked for */
652                 intf_MsgImm( "\nPress the RETURN key to continue..." );
653                 getchar();
654             }
655 #endif
656             return( -1 );
657             break;
658         case 'v':                                           /* -v, --verbose */
659             p_main->i_warning_level++;
660             break;
661
662         /* Interface warning messages level */
663         case 'I':                                              /* -I, --intf */
664             main_PutPszVariable( INTF_METHOD_VAR, optarg );
665             break;
666         case OPT_WARNING:                                       /* --warning */
667             intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
668             p_main->i_warning_level = atoi(optarg);
669             break;
670
671         case OPT_STDOUT:                                         /* --stdout */
672             main_PutPszVariable( INTF_STDOUT_VAR, optarg );
673             break;
674
675         case OPT_STATS:
676             p_main->b_stats = 1;
677             break;
678
679         /* Audio options */
680         case OPT_NOAUDIO:                                       /* --noaudio */
681             p_main->b_audio = 0;
682             break;
683         case 'A':                                              /* -A, --aout */
684             main_PutPszVariable( AOUT_METHOD_VAR, optarg );
685             break;
686         case OPT_STEREO:                                         /* --stereo */
687             main_PutIntVariable( AOUT_STEREO_VAR, 1 );
688             break;
689         case OPT_MONO:                                             /* --mono */
690             main_PutIntVariable( AOUT_STEREO_VAR, 0 );
691             break;
692         case OPT_SPDIF:                                           /* --spdif */
693             main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
694             break;
695         case OPT_DOWNMIX:                                       /* --downmix */
696             main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
697             break;
698         case OPT_IMDCT:                                           /* --imdct */
699             main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
700             break;
701         case OPT_VOLUME:                                         /* --volume */
702             main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
703             break;
704
705         /* Video options */
706         case OPT_NOVIDEO:                                       /* --novideo */
707             p_main->b_video = 0;
708             break;
709         case 'V':                                              /* -V, --vout */
710             main_PutPszVariable( VOUT_METHOD_VAR, optarg );
711             break;
712         case OPT_DISPLAY:                                       /* --display */
713             main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
714             break;
715         case OPT_WIDTH:                                           /* --width */
716             main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
717             break;
718         case OPT_HEIGHT:                                         /* --height */
719             main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
720             break;
721         case 'g':                                         /* -g, --grayscale */
722             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
723             break;
724         case OPT_COLOR:                                           /* --color */
725             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
726             break;
727         case OPT_FULLSCREEN:                                 /* --fullscreen */
728             main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
729             break;
730         case OPT_OVERLAY:                                       /* --overlay */
731             main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
732             break;
733         case OPT_XVADAPTOR:                                   /* --xvadaptor */
734             main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
735             break;
736         case OPT_MOTION:                                         /* --motion */
737             main_PutPszVariable( MOTION_METHOD_VAR, optarg );
738             break;
739         case OPT_IDCT:                                             /* --idct */
740             main_PutPszVariable( IDCT_METHOD_VAR, optarg );
741             break;
742         case OPT_YUV:                                               /* --yuv */
743             main_PutPszVariable( YUV_METHOD_VAR, optarg );
744             break;
745         case OPT_SMP:                                               /* --smp */
746             main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
747             break;
748
749         /* DVD options */
750         case 't':                                              /* --dvdtitle */
751             main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
752             break;
753         case 'T':                                            /* --dvdchapter */
754             main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
755             break;
756         case 'u':                                              /* --dvdangle */
757             main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
758             break;
759         case 'a':                                              /* --dvdaudio */
760             if ( ! strcmp(optarg, "ac3") )
761                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
762             else if ( ! strcmp(optarg, "lpcm") )
763                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
764             else if ( ! strcmp(optarg, "mpeg") )
765                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
766             else
767                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
768             break;
769         case 'c':                                            /* --dvdchannel */
770             main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
771             break;
772         case 's':                                           /* --dvdsubtitle */
773             main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
774             break;
775         case OPT_DVDCSS:                                         /* --dvdcss */
776                 main_PutPszVariable( INPUT_DVDCSS_VAR, optarg );
777             break;
778
779         /* Input options */
780         case OPT_INPUT:                                           /* --input */
781             main_PutPszVariable( INPUT_METHOD_VAR, optarg );
782             break;
783         case OPT_CHANNELS:                                     /* --channels */
784             main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
785             break;
786         case OPT_CHANNELSERVER:                           /* --channelserver */
787             main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
788             break;
789         case OPT_SERVER:                                         /* --server */
790             main_PutPszVariable( INPUT_SERVER_VAR, optarg );
791             break;
792         case OPT_PORT:                                             /* --port */
793             main_PutPszVariable( INPUT_PORT_VAR, optarg );
794             break;
795         case OPT_BROADCAST:                                   /* --broadcast */
796             main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
797             main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
798             break;
799
800         /* Synchro options */
801         case OPT_SYNCHRO:                                      
802             main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
803             break;
804             
805         /* Internal error: unknown option */
806         case '?':
807         default:
808             ShowConsole();
809             RedirectSTDOUT();
810             intf_ErrMsg( "intf error: unknown option `%s'",
811                          ppsz_argv[optind] );
812             Usage( USAGE );
813 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
814             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
815                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
816             {
817                 /* No stdout redirection has been asked for */
818                 intf_MsgImm( "\nPress the RETURN key to continue..." );
819                 getchar();
820             }
821 #endif
822             return( EINVAL );
823             break;
824         }
825     }
826
827     if( p_main->i_warning_level < 0 )
828     {
829         p_main->i_warning_level = 0;
830     }
831
832     return( 0 );
833 }
834
835 /*****************************************************************************
836  * GetFilenames: parse command line options which are not flags
837  *****************************************************************************
838  * Parse command line for input files.
839  *****************************************************************************/
840 static int GetFilenames( int i_argc, char *ppsz_argv[] )
841 {
842     int i_opt;
843
844     /* We assume that the remaining parameters are filenames */
845     for( i_opt = optind; i_opt < i_argc; i_opt++ )
846     {
847         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
848                           ppsz_argv[ i_opt ] );
849     }
850
851     return( 0 );
852 }
853
854 /*****************************************************************************
855  * Usage: print program usage
856  *****************************************************************************
857  * Print a short inline help. Message interface is initialized at this stage.
858  *****************************************************************************/
859 static void Usage( int i_fashion )
860 {
861     /* Usage */
862     intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
863                  p_main->psz_arg0 );
864
865     if( i_fashion == USAGE )
866     {
867         intf_MsgImm( "Try `%s --help' for more information.",
868                      p_main->psz_arg0 );
869         return;
870     }
871
872     /* Options */
873     intf_MsgImm( "\nOptions:"
874           "\n  -I, --intf <module>            \tinterface method"
875           "\n  -v, --verbose                  \tverbose mode (cumulative)"
876           "\n      --stdout <filename>        \tredirect console stdout"
877           "\n"
878           "\n      --noaudio                  \tdisable audio"
879           "\n  -A, --aout <module>            \taudio output method"
880           "\n      --stereo, --mono           \tstereo/mono audio"
881           "\n      --spdif                    \tAC3 pass-through mode"
882           "\n      --downmix <module>         \tAC3 downmix method"
883           "\n      --imdct <module>           \tAC3 IMDCT method"
884           "\n      --volume [0..1024]         \tVLC output volume"
885           "\n"
886           "\n      --novideo                  \tdisable video"
887           "\n  -V, --vout <module>            \tvideo output method"
888           "\n      --display <display>        \tdisplay string"
889           "\n      --width <w>, --height <h>  \tdisplay dimensions"
890           "\n  -g, --grayscale                \tgrayscale output"
891           "\n      --fullscreen               \tfullscreen output"
892           "\n      --overlay                  \taccelerated display"
893           "\n      --xvadaptor <adaptor>      \tXVideo adaptor"
894           "\n      --color                    \tcolor output"
895           "\n      --motion <module>          \tmotion compensation method"
896           "\n      --idct <module>            \tIDCT method"
897           "\n      --yuv <module>             \tYUV method"
898           "\n      --synchro <type>           \tforce synchro algorithm"
899           "\n      --smp <number of threads>  \tuse several processors"
900           "\n"
901           "\n  -t, --dvdtitle <num>           \tchoose DVD title"
902           "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
903           "\n  -u, --dvdangle <num>           \tchoose DVD angle"
904           "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
905           "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
906           "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
907           "\n    , --dvdcss <method>          \tselect DVDCSS decryption method"
908           "\n"
909           "\n      --input                    \tinput method"
910           "\n      --channels                 \tenable channels"
911           "\n      --channelserver <host>     \tchannel server address"
912           "\n      --server <host>            \tvideo server address"
913           "\n      --port <port>              \tvideo server port"
914           "\n      --broadcast                \tlisten to a broadcast"
915           "\n"
916           "\n  -h, --help                     \tprint help and exit"
917           "\n  -H, --longhelp                 \tprint long help and exit"
918           "\n      --version                  \toutput version information and exit" );
919
920     if( i_fashion == SHORT_HELP )
921         return;
922
923     /* Interface parameters */
924     intf_MsgImm( "\nInterface parameters:"
925         "\n  " INTF_METHOD_VAR "=<method name>        \tinterface method"
926         "\n  " INTF_INIT_SCRIPT_VAR "=<filename>              \tinitialization script"
927         "\n  " INTF_CHANNELS_VAR "=<filename>         \tchannels list"
928         "\n  " INTF_STDOUT_VAR "=<filename>           \tredirect console stdout" );
929
930     /* Audio parameters */
931     intf_MsgImm( "\nAudio parameters:"
932         "\n  " AOUT_METHOD_VAR "=<method name>        \taudio method"
933         "\n  " AOUT_DSP_VAR "=<filename>              \tdsp device path"
934         "\n  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output"
935         "\n  " AOUT_SPDIF_VAR "={1|0}                 \tAC3 pass-through mode"
936         "\n  " DOWNMIX_METHOD_VAR "=<method name>     \tAC3 downmix method"
937         "\n  " IMDCT_METHOD_VAR "=<method name>       \tAC3 IMDCT method"
938         "\n  " AOUT_VOLUME_VAR "=[0..1024]            \tVLC output volume"
939         "\n  " AOUT_RATE_VAR "=<rate>                 \toutput rate" );
940
941     /* Video parameters */
942     intf_MsgImm( "\nVideo parameters:"
943         "\n  " VOUT_METHOD_VAR "=<method name>        \tdisplay method"
944         "\n  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used"
945         "\n  " VOUT_WIDTH_VAR "=<width>               \tdisplay width"
946         "\n  " VOUT_HEIGHT_VAR "=<height>             \tdislay height"
947         "\n  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path"
948         "\n  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output"
949         "\n  " VOUT_FULLSCREEN_VAR "={1|0}            \tfullscreen"
950         "\n  " VOUT_OVERLAY_VAR "={1|0}               \toverlay"
951         "\n  " VOUT_XVADAPTOR_VAR "=<adaptor>         \tXVideo adaptor"
952         "\n  " MOTION_METHOD_VAR "=<method name>      \tmotion compensation method"
953         "\n  " IDCT_METHOD_VAR "=<method name>        \tIDCT method"
954         "\n  " YUV_METHOD_VAR "=<method name>         \tYUV method"
955         "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm"
956         "\n  " VDEC_SMP_VAR "=<number of threads>     \tuse several processors" );
957
958     /* DVD parameters */
959     intf_MsgImm( "\nDVD parameters:"
960         "\n  " INPUT_DVD_DEVICE_VAR "=<device>        \tDVD device"
961         "\n  " INPUT_TITLE_VAR "=<title>              \ttitle number"
962         "\n  " INPUT_CHAPTER_VAR "=<chapter>          \tchapter number"
963         "\n  " INPUT_ANGLE_VAR "=<angle>              \tangle number"
964         "\n  " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off}  \taudio type"
965         "\n  " INPUT_CHANNEL_VAR "=[0-15]             \taudio channel"
966         "\n  " INPUT_SUBTITLE_VAR "=[0-31]            \tsubtitle channel"
967         "\n  " INPUT_DVDCSS_VAR "={csskey|disc|title} \tdvdcss method" );
968
969     /* Input parameters */
970     intf_MsgImm( "\nInput parameters:"
971         "\n  " INPUT_SERVER_VAR "=<hostname>          \tvideo server"
972         "\n  " INPUT_PORT_VAR "=<port>                \tvideo server port"
973         "\n  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface"
974         "\n  " INPUT_BCAST_ADDR_VAR "=<addr>          \tbroadcast mode"
975         "\n  " INPUT_CHANNEL_SERVER_VAR "=<hostname>  \tchannel server"
976         "\n  " INPUT_CHANNEL_PORT_VAR "=<port>        \tchannel server port" );
977
978 }
979
980 /*****************************************************************************
981  * Version: print complete program version
982  *****************************************************************************
983  * Print complete program version and build number.
984  *****************************************************************************/
985 static void Version( void )
986 {
987     intf_MsgImm( VERSION_MESSAGE
988         "This program comes with NO WARRANTY, to the extent permitted by law.\n"
989         "You may redistribute it under the terms of the GNU General Public License;\n"
990         "see the file named COPYING for details.\n"
991         "Written by the VideoLAN team at Ecole Centrale, Paris." );
992 }
993
994 /*****************************************************************************
995  * InitSignalHandler: system signal handler initialization
996  *****************************************************************************
997  * Set the signal handlers. SIGTERM is not intercepted, because we need at
998  * at least a method to kill the program when all other methods failed, and
999  * when we don't want to use SIGKILL.
1000  *****************************************************************************/
1001 static void InitSignalHandler( void )
1002 {
1003     /* Termination signals */
1004 #ifndef WIN32
1005     signal( SIGINT,  FatalSignalHandler );
1006     signal( SIGHUP,  FatalSignalHandler );
1007     signal( SIGQUIT, FatalSignalHandler );
1008
1009     /* Other signals */
1010     signal( SIGALRM, SimpleSignalHandler );
1011     signal( SIGPIPE, SimpleSignalHandler );
1012 #endif
1013 }
1014
1015
1016 /*****************************************************************************
1017  * SimpleSignalHandler: system signal handler
1018  *****************************************************************************
1019  * This function is called when a non fatal signal is received by the program.
1020  *****************************************************************************/
1021 static void SimpleSignalHandler( int i_signal )
1022 {
1023     /* Acknowledge the signal received */
1024     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1025 }
1026
1027
1028 /*****************************************************************************
1029  * FatalSignalHandler: system signal handler
1030  *****************************************************************************
1031  * This function is called when a fatal signal is received by the program.
1032  * It tries to end the program in a clean way.
1033  *****************************************************************************/
1034 static void FatalSignalHandler( int i_signal )
1035 {
1036     /* Once a signal has been trapped, the termination sequence will be
1037      * armed and following signals will be ignored to avoid sending messages
1038      * to an interface having been destroyed */
1039 #ifndef WIN32
1040     signal( SIGINT,  SIG_IGN );
1041     signal( SIGHUP,  SIG_IGN );
1042     signal( SIGQUIT, SIG_IGN );
1043 #endif
1044
1045     /* Acknowledge the signal received */
1046     intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1047
1048     /* Try to terminate everything - this is done by requesting the end of the
1049      * interface thread */
1050     p_main->p_intf->b_die = 1;
1051 }
1052
1053 /*****************************************************************************
1054  * InstructionSignalHandler: system signal handler
1055  *****************************************************************************
1056  * This function is called when a illegal instruction signal is received by
1057  * the program.
1058  * We use this function to test OS and CPU_Capabilities
1059  *****************************************************************************/
1060 static void InstructionSignalHandler( int i_signal )
1061 {
1062     /* Once a signal has been trapped, the termination sequence will be
1063      * armed and following signals will be ignored to avoid sending messages
1064      * to an interface having been destroyed */
1065
1066     /* Acknowledge the signal received */
1067     i_illegal = 1;
1068     
1069 #ifdef HAVE_SIGRELSE
1070     sigrelse( i_signal );
1071 #endif
1072     longjmp( env, 1 );
1073 }
1074
1075 /*****************************************************************************
1076  * CPUCapabilities: list the processors MMX support and other capabilities
1077  *****************************************************************************
1078  * This function is called to list extensions the CPU may have.
1079  *****************************************************************************/
1080 static int CPUCapabilities( void )
1081 {
1082     volatile int i_capabilities = CPU_CAPABILITY_NONE;
1083
1084 #if defined( SYS_BEOS )
1085     i_capabilities |= CPU_CAPABILITY_486
1086                       | CPU_CAPABILITY_586
1087                       | CPU_CAPABILITY_MMX;
1088
1089     return( i_capabilities );
1090
1091 #elif defined( SYS_DARWIN )
1092     struct host_basic_info hi;
1093     kern_return_t          ret;
1094     host_name_port_t       host;
1095
1096     int i_size;
1097     char *psz_name, *psz_subname;
1098
1099     /* Should 'never' fail? */
1100     host = mach_host_self();
1101
1102     i_size = sizeof( hi ) / sizeof( int );
1103     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1104
1105     if( ret != KERN_SUCCESS )
1106     {
1107         fprintf( stderr, "error: couldn't get CPU information\n" );
1108         return( i_capabilities );
1109     }
1110
1111     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1112     /* FIXME: need better way to detect newer proccessors.
1113      * could do strncmp(a,b,5), but that's real ugly */
1114     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1115     {
1116         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1117     }
1118
1119     return( i_capabilities );
1120
1121 #elif defined( __i386__ )
1122     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
1123     volatile boolean_t     b_amd;
1124
1125     signal( SIGILL, InstructionSignalHandler );
1126     
1127     /* test for a 486 CPU */
1128     asm volatile ( "pushfl\n\t"
1129                    "popl %%eax\n\t"
1130                    "movl %%eax, %%ebx\n\t"
1131                    "xorl $0x200000, %%eax\n\t"
1132                    "pushl %%eax\n\t"
1133                    "popfl\n\t"
1134                    "pushfl\n\t"
1135                    "popl %%eax"
1136                  : "=a" ( i_eax ),
1137                    "=b" ( i_ebx )
1138                  :
1139                  : "cc" );
1140
1141     if( i_eax == i_ebx )
1142     {
1143         signal( SIGILL, NULL );     
1144         return( i_capabilities );
1145     }
1146
1147     i_capabilities |= CPU_CAPABILITY_486;
1148
1149     /* the CPU supports the CPUID instruction - get its level */
1150     cpuid( 0x00000000 );
1151
1152     if( !i_eax )
1153     {
1154         signal( SIGILL, NULL );     
1155         return( i_capabilities );
1156     }
1157
1158     /* FIXME: this isn't correct, since some 486s have cpuid */
1159     i_capabilities |= CPU_CAPABILITY_586;
1160
1161     /* borrowed from mpeg2dec */
1162     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1163                     && ( i_edx == 0x69746e65 );
1164
1165     /* test for the MMX flag */
1166     cpuid( 0x00000001 );
1167
1168     if( ! (i_edx & 0x00800000) )
1169     {
1170         signal( SIGILL, NULL );     
1171         return( i_capabilities );
1172     }
1173
1174     i_capabilities |= CPU_CAPABILITY_MMX;
1175
1176     if( i_edx & 0x02000000 )
1177     {
1178         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1179
1180 #ifdef HAVE_SSE
1181         /* We test if OS support the SSE instructions */
1182         i_illegal = 0;
1183         if( setjmp( env ) == 0 )
1184         {
1185             /* Test a SSE instruction */
1186             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1187         }
1188
1189         if( i_illegal == 0 )
1190         {
1191             i_capabilities |= CPU_CAPABILITY_SSE;
1192         }
1193         else
1194         {
1195             fprintf( stderr, "warning: your OS doesn't have support for "
1196                              "SSE instructions, "
1197                              "some optimizations\nwill be disabled\n" );
1198 #ifdef SYS_LINUX
1199             fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1200 #endif
1201         }
1202 #endif
1203     }
1204     
1205     /* test for additional capabilities */
1206     cpuid( 0x80000000 );
1207
1208     if( i_eax < 0x80000001 )
1209     {
1210         signal( SIGILL, NULL );     
1211         return( i_capabilities );
1212     }
1213
1214     /* list these additional capabilities */
1215     cpuid( 0x80000001 );
1216
1217 #ifdef HAVE_3DNOW
1218     if( i_edx & 0x80000000 )
1219     {
1220         i_illegal = 0;
1221         if( setjmp( env ) == 0 )
1222         {
1223             /* Test a 3D Now! instruction */
1224             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1225         }
1226
1227         if( i_illegal == 0 ) 
1228         {
1229             i_capabilities |= CPU_CAPABILITY_3DNOW;
1230         }
1231     }
1232 #endif
1233
1234     if( b_amd && ( i_edx & 0x00400000 ) )
1235     {
1236         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1237     }
1238
1239     signal( SIGILL, NULL );     
1240     return( i_capabilities );
1241
1242 #elif defined( __powerpc__ )
1243
1244     /* Test for Altivec */
1245     signal( SIGILL, InstructionSignalHandler );
1246
1247 #ifdef HAVE_ALTIVEC
1248     i_illegal = 0;
1249     if( setjmp( env ) == 0 )
1250     {
1251         asm volatile ("mtspr 256,%0" : : "r" (-1));
1252     }
1253
1254     if( i_illegal == 0 )
1255     {
1256         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1257     }
1258 #endif
1259
1260     signal( SIGILL, NULL );     
1261     return( i_capabilities );
1262
1263 #else
1264     /* default behaviour */
1265     return( i_capabilities );
1266
1267 #endif
1268 }
1269
1270 /*****************************************************************************
1271  * RedirectSTDOUT: redirect stdout and stderr to a file
1272  *****************************************************************************
1273  * This function will redirect stdout and stderr to a file if the user has
1274  * specified so.
1275  *****************************************************************************/
1276 static int RedirectSTDOUT( void )
1277 {
1278     int  i_stdout_filedesc;
1279     char *psz_stdout_filename;
1280
1281     psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1282                                                INTF_STDOUT_DEFAULT );
1283     if( strcmp( "", psz_stdout_filename ) != 0 )
1284     {
1285         ShowConsole();
1286         i_stdout_filedesc = open( psz_stdout_filename,
1287                                   O_CREAT | O_TRUNC | O_RDWR,
1288                                   S_IREAD | S_IWRITE );
1289
1290         if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1291         {
1292             intf_ErrMsg( "warning: unable to redirect stdout" );
1293         }
1294
1295         if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1296         {
1297             intf_ErrMsg( "warning: unable to redirect stderr" );
1298         }
1299
1300         close( i_stdout_filedesc );
1301     }
1302     else
1303     {
1304         /* No stdout redirection has been asked so open a console */
1305         if( p_main->i_warning_level )
1306         {
1307             ShowConsole();
1308         }
1309
1310     }
1311
1312     return 0;
1313 }
1314
1315 /*****************************************************************************
1316  * ShowConsole: On Win32, create an output console for debug messages
1317  *****************************************************************************
1318  * This function is usefull only on Win32.
1319  *****************************************************************************/
1320 static void ShowConsole( void )
1321 {
1322 #ifdef WIN32 /*  */
1323     AllocConsole();
1324     freopen( "CONOUT$", "w", stdout );
1325     freopen( "CONOUT$", "w", stderr );
1326     freopen( "CONIN$", "r", stdin );
1327 #endif
1328     return;
1329 }
1330