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