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