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