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