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