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