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