]> git.sesse.net Git - vlc/blob - src/interface/main.c
Translation of user interface messages to Netherlands.
[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.171 2002/03/26 23:08:40 gbazin Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *          Gildas Bazin <gbazin@netcourrier.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <setjmp.h>                                       /* longjmp, setjmp */
34
35 #include <videolan/vlc.h>
36
37 #ifdef HAVE_GETOPT_LONG
38 #   ifdef HAVE_GETOPT_H
39 #       include <getopt.h>                                       /* getopt() */
40 #   endif
41 #else
42 #   include "GNUgetopt/getopt.h"
43 #endif
44
45 #ifdef SYS_DARWIN
46 #   include <mach/mach.h>                               /* Altivec detection */
47 #   include <mach/mach_error.h>       /* some day the header files||compiler *
48                                                        will define it for us */
49 #   include <mach/bootstrap.h>
50 #endif
51
52 #ifndef WIN32
53 #   include <netinet/in.h>                            /* BSD: struct in_addr */
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( _MSC_VER ) && defined( _WIN32 )
59 #   include <io.h>
60 #endif
61
62 #ifdef HAVE_LOCALE_H
63 #    include <locale.h>
64 #endif
65
66 #include <errno.h>                                                 /* ENOMEM */
67 #include <stdlib.h>                                  /* getenv(), strtol(),  */
68 #include <string.h>                                            /* strerror() */
69
70 #include "netutils.h"                                 /* network_ChannelJoin */
71
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
74
75 #include "intf_playlist.h"
76 #include "interface.h"
77
78 #include "audio_output.h"
79
80 #include "video.h"
81 #include "video_output.h"
82
83 #include "debug.h"
84
85 /*****************************************************************************
86  * Configuration options for the main program. Each plugin will also separatly
87  * define its own configuration options.
88  * Look into configuration.h if you need to know more about the following
89  * macros.
90  *
91  *****************************************************************************/
92 #define BUILTIN
93 #define MODULE_NAME main
94 #include "modules_inner.h"                        /* for configuration stuff */
95
96
97 #define INTF_TEXT "interface method"
98 #define INTF_LONGTEXT "This option allows you to select the interface used by"\
99                       " vlc.\nNote that the default behaviour is to" \
100                       " automatically select the best method available"
101
102 #define WARNING_TEXT "warning level (or use -v, -vv, etc...)"
103 #define WARNING_LONGTEXT "Increasing the warning level will allow you to see" \
104                          " more debug messages and can sometimes help you to" \
105                          " troubleshoot a problem"
106
107 #define STATS_TEXT "output statistics"
108 #define STATS_LONGTEXT "Enabling the stats mode will flood your log console" \
109                        " with various statistics messages"
110
111 #define INTF_PATH_TEXT "interface default search path"
112 #define INTF_PATH_LONGTEXT "This option allows you to set the default path" \
113                            " that the interface will open when looking for a" \
114                            " file"
115
116 #define AOUT_TEXT "audio output method"
117 #define AOUT_LONGTEXT "This option allows you to select the audio" \
118                       " audio output method used by vlc.\nNote that the" \
119                       " default behaviour is to automatically select the best"\
120                       " method available"
121
122 #define NOAUDIO_TEXT "disable audio"
123 #define NOAUDIO_LONGTEXT "This will completely disable the audio output. The" \
124                          " audio decoding stage shouldn't even be done, so it"\
125                          " can allow you to save some processing power"
126
127 #define MONO_TEXT "mono audio"
128 #define MONO_LONGTEXT "This will force a mono audio output"
129
130 #define VOLUME_TEXT "audio output volume"
131 #define VOLUME_LONGTEXT "You can set the default audio output volume here," \
132                         " in a range from 0 to 1024"
133
134 #define FORMAT_TEXT "audio output format"
135 #define FORMAT_LONGTEXT "You can force the audio output format here.\n" \
136                         "0 -> 16 bits signed native endian (default)\n" \
137                         "1 ->  8 bits unsigned\n"                       \
138                         "2 -> 16 bits signed little endian\n"           \
139                         "3 -> 16 bits signed big endian\n"              \
140                         "4 ->  8 bits signed\n"                         \
141                         "5 -> 16 bits unsigned little endian\n"         \
142                         "6 -> 16 bits unsigned big endian\n"            \
143                         "7 -> mpeg2 audio (unsupported)\n"              \
144                         "8 -> ac3 pass-through"
145
146 #define RATE_TEXT "audio output frequency (Hz)"
147 #define RATE_LONGTEXT "You can force the audio output frequency here.\n"    \
148                       "Common values are 48000, 44100, 32000, 22050,"       \
149                       " 16000, 11025, 8000"
150
151 #define DESYNC_TEXT "Compensate desynchronization of audio (in ms)"
152 #define DESYNC_LONGTEXT "This option allows you to delay the audio output."  \
153                         " This can be handy if you notice a lag between the" \
154                         " video and the audio"
155
156 #define VOUT_TEXT "video output method"
157 #define VOUT_LONGTEXT "This option allows you to select the video output"     \
158                       " method used by vlc.\nNote that the default behaviour" \
159                       " is to automatically select the best method available"
160
161 #define NOVIDEO_TEXT "disable video"
162 #define NOVIDEO_LONGTEXT "This will completely disable the video output. The" \
163                          " video decoding stage shouldn't even be done, so"   \
164                          " it can allow you to save some processing power"
165
166 #define DISPLAY_TEXT "display identifier"
167 #define DISPLAY_LONGTEXT NULL
168
169 #define WIDTH_TEXT "video width"
170 #define WIDTH_LONGTEXT "You can enforce the video width here.\nNote"  \
171                        " that by default vlc will adapt to the video" \
172                        " characteristics"
173
174 #define HEIGHT_TEXT "video height"
175 #define HEIGHT_LONGTEXT "You can enforce the video height here.\nNote"  \
176                         " that by default vlc will adapt to the video " \
177                         " characteristics"
178
179 #define GRAYSCALE_TEXT "grayscale video output"
180 #define GRAYSCALE_LONGTEXT "Using this option, vlc will not decode the color "\
181                            "information from the video (this can also allow " \
182                            "you to save some processing power)"
183
184 #define FULLSCREEN_TEXT "fullscreen video output"
185 #define FULLSCREEN_LONGTEXT "If this option is enabled, vlc will always " \
186                             "start a video in fullscreen mode"
187
188 #define NOOVERLAY_TEXT "disable hardware acceleration for the video output"
189 #define NOOVERLAY_LONGTEXT "By default vlc will try to take advantage of the "\
190                            "overlay capabilities of you graphics card.\n"
191
192 #define SPUMARGIN_TEXT "force SPU position"
193 #define SPUMARGIN_LONGTEXT NULL
194
195 #define FILTER_TEXT "video filter module"
196 #define FILTER_LONGTEXT NULL
197
198 #define INPUT_TEXT "input method"
199 #define INPUT_LONGTEXT NULL
200
201 #define SERVER_PORT_TEXT "server port"
202 #define SERVER_PORT_LONGTEXT NULL
203
204 #define NETCHANNEL_TEXT "enable network channel mode"
205 #define NETCHANNEL_LONGTEXT NULL
206
207 #define CHAN_SERV_TEXT "channel server address"
208 #define CHAN_SERV_LONGTEXT NULL
209
210 #define CHAN_PORT_TEXT "channel server port"
211 #define CHAN_PORT_LONGTEXT NULL
212
213 #define IFACE_TEXT "network interface"
214 #define IFACE_LONGTEXT NULL
215
216 #define INPUT_AUDIO_TEXT "choose audio"
217 #define INPUT_AUDIO_LONGTEXT NULL
218
219 #define INPUT_CHAN_TEXT "choose channel"
220 #define INPUT_CHAN_LONGTEXT NULL
221
222 #define INPUT_SUBT_TEXT "choose subtitles"
223 #define INPUT_SUBT_LONGTEXT NULL
224
225 #define DVD_DEV_TEXT "DVD device"
226 #define DVD_DEV_LONGTEXT NULL
227
228 #define VCD_DEV_TEXT "VCD device"
229 #define VCD_DEV_LONGTEXT NULL
230
231 #define SAT_FREQ_TEXT "Satellite transponder frequency"
232 #define SAT_FREQ_LONGTEXT NULL
233
234 #define SAT_POL_TEXT "Satellite transponder polarization"
235 #define SAT_POL_LONGTEXT NULL
236
237 #define SAT_SRATE_TEXT "Satellite transponder symbol rate"
238 #define SAT_SRATE_LONGTEXT NULL
239
240 #define IPV6_TEXT "force IPv6"
241 #define IPV6_LONGTEXT NULL
242
243 #define IPV4_TEXT "force IPv4"
244 #define IPV4_LONGTEXT NULL
245
246 #define ADEC_MPEG_TEXT "choose MPEG audio decoder"
247 #define ADEC_MPEG_LONGTEXT NULL
248
249 #define ADEC_AC3_TEXT "choose AC3 audio decoder"
250 #define ADEC_AC3_LONGTEXT NULL
251
252 #define VDEC_SMP_TEXT "use additional processors"
253 #define VDEC_SMP_LONGTEXT NULL
254
255 #define VPAR_SYNCHRO_TEXT "force synchro algorithm {I|I+|IP|IP+|IPB}"
256 #define VPAR_SYNCHRO_LONGTEXT NULL
257
258 #define NOMMX_TEXT "disable CPU's MMX support"
259 #define NOMMX_LONGTEXT NULL
260
261 #define NO3DN_TEXT "disable CPU's 3D Now! support"
262 #define NO3DN_LONGTEXT NULL
263
264 #define NOMMXEXT_TEXT "disable CPU's MMX EXT support"
265 #define NOMMXEXT_LONGTEXT NULL
266
267 #define NOSSE_TEXT "disable CPU's SSE support"
268 #define NOSSE_LONGTEXT NULL
269
270 #define NOALTIVEC_TEXT "disable CPU's AltiVec support"
271 #define NOALTIVEC_LONGTEXT NULL
272
273 #define PLAYLIST_LAUNCH_TEXT "launch playlist on startup"
274 #define PLAYLIST_LAUNCH_LONGTEXT NULL
275
276 #define PLAYLIST_ENQUEUE_TEXT "enqueue playlist as default"
277 #define PLAYLIST_ENQUEUE_LONGTEXT NULL
278
279 #define PLAYLIST_LOOP_TEXT "loop playlist on end"
280 #define PLAYLIST_LOOP_LONGTEXT NULL
281
282 #define MEMCPY_TEXT "memory copy method"
283 #define MEMCPY_LONGTEXT NULL
284
285 /* Quick usage guide
286 MODULE_CONFIG_START
287 MODULE_CONFIG_STOP
288 ADD_CATEGORY_HINT( text, longtext )
289 ADD_SUBCATEGORY_HINT( text, longtext )
290 ADD_STRING( option_name, value, p_callback, text, longtext )
291 ADD_FILE( option_name, psz_value, p_callback, text, longtext )
292 ADD_PLUGIN( option_name, psz_value, i_capability, p_callback, text, longtext )
293 ADD_INTEGER( option_name, i_value, p_callback, text, longtext )
294 ADD_BOOL( option_name, p_callback, text, longtext )
295 */
296
297 MODULE_CONFIG_START
298
299 /* Interface options */
300 ADD_CATEGORY_HINT( "Interface", NULL)
301 ADD_PLUGIN  ( "intf", MODULE_CAPABILITY_INTF, NULL, NULL, INTF_TEXT, INTF_LONGTEXT )
302 ADD_INTEGER ( "warning", 0, NULL, WARNING_TEXT, WARNING_LONGTEXT )
303 ADD_BOOL    ( "stats", NULL, STATS_TEXT, STATS_LONGTEXT )
304 ADD_STRING  ( "search_path", NULL, NULL, INTF_PATH_TEXT, INTF_PATH_LONGTEXT )
305
306 /* Audio options */
307 ADD_CATEGORY_HINT( "Audio", NULL)
308 ADD_PLUGIN  ( "aout", MODULE_CAPABILITY_AOUT, NULL, NULL, AOUT_TEXT, AOUT_LONGTEXT )
309 ADD_BOOL    ( "noaudio", NULL, NOAUDIO_TEXT, NOAUDIO_LONGTEXT )
310 ADD_BOOL    ( "mono", NULL, MONO_TEXT, MONO_LONGTEXT )
311 ADD_INTEGER ( "volume", VOLUME_DEFAULT, NULL, VOLUME_TEXT, VOLUME_LONGTEXT )
312 ADD_INTEGER ( "rate", 44100, NULL, RATE_TEXT, RATE_LONGTEXT )
313 ADD_INTEGER ( "desync", 0, NULL, DESYNC_TEXT, DESYNC_LONGTEXT )
314 ADD_INTEGER ( "aout_format", 0, NULL, FORMAT_TEXT,
315               FORMAT_LONGTEXT )
316
317 /* Video options */
318 ADD_CATEGORY_HINT( "Video", NULL )
319 ADD_PLUGIN  ( "vout", MODULE_CAPABILITY_VOUT, NULL, NULL, VOUT_TEXT, VOUT_LONGTEXT )
320 ADD_BOOL    ( "novideo", NULL, NOVIDEO_TEXT, NOVIDEO_LONGTEXT )
321 ADD_INTEGER ( "width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT )
322 ADD_INTEGER ( "height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT )
323 ADD_BOOL    ( "grayscale", NULL, GRAYSCALE_TEXT, GRAYSCALE_LONGTEXT )
324 ADD_BOOL    ( "fullscreen", NULL, FULLSCREEN_TEXT, FULLSCREEN_LONGTEXT )
325 ADD_BOOL    ( "nooverlay", NULL, NOOVERLAY_TEXT, NOOVERLAY_LONGTEXT )
326 ADD_INTEGER ( "spumargin", -1, NULL, SPUMARGIN_TEXT, SPUMARGIN_LONGTEXT )
327 ADD_PLUGIN  ( "filter", MODULE_CAPABILITY_VOUT, NULL, NULL, FILTER_TEXT, FILTER_LONGTEXT )
328
329 /* Input options */
330 ADD_CATEGORY_HINT( "Input", NULL )
331 ADD_STRING  ( "input", NULL, NULL, INPUT_TEXT, INPUT_LONGTEXT )
332 ADD_INTEGER ( "server_port", 1234, NULL, SERVER_PORT_TEXT, SERVER_PORT_LONGTEXT )
333 ADD_BOOL    ( "network_channel", NULL, NETCHANNEL_TEXT, NETCHANNEL_LONGTEXT )
334 ADD_STRING  ( "channel_server", "localhost", NULL, CHAN_SERV_TEXT, CHAN_SERV_LONGTEXT )
335 ADD_INTEGER ( "channel_port", 6010, NULL, CHAN_PORT_TEXT, CHAN_PORT_LONGTEXT )
336 ADD_STRING  ( "iface", "eth0", NULL, IFACE_TEXT, IFACE_LONGTEXT )
337
338 ADD_INTEGER ( "input_audio", -1, NULL, INPUT_AUDIO_TEXT, INPUT_AUDIO_LONGTEXT )
339 ADD_INTEGER ( "input_channel", -1, NULL, INPUT_CHAN_TEXT, INPUT_CHAN_LONGTEXT )
340 ADD_INTEGER ( "input_subtitle", -1, NULL, INPUT_SUBT_TEXT, INPUT_SUBT_LONGTEXT )
341
342 ADD_STRING  ( "dvd_device", "/dev/dvd", NULL, DVD_DEV_TEXT, DVD_DEV_LONGTEXT )
343 ADD_STRING  ( "vcd_device", "/dev/cdrom", NULL, VCD_DEV_TEXT, VCD_DEV_LONGTEXT )
344 #ifdef HAVE_SATELLITE
345 ADD_INTEGER ( "sat_frequency", 12553, NULL, SAT_FREQ_TEXT, SAT_FREQ_LONGTEXT )
346 ADD_INTEGER ( "sat_polarization", 0, NULL, SAT_POL_TEXT, SAT_POL_LONGTEXT )
347 ADD_INTEGER ( "sat_symbol_rate", 27500, NULL, SAT_SRATE_TEXT,
348             SAT_SRATE_LONGTEXT )
349 #endif
350
351 ADD_BOOL    ( "ipv6", NULL, IPV6_TEXT, IPV6_LONGTEXT )
352 ADD_BOOL    ( "ipv4", NULL, IPV4_TEXT, IPV4_LONGTEXT )
353
354 /* Decoder options */
355 ADD_CATEGORY_HINT( "Decoders", NULL )
356 ADD_PLUGIN  ( "mpeg_adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_MPEG_TEXT, ADEC_MPEG_LONGTEXT )
357 ADD_PLUGIN  ( "ac3_adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_AC3_TEXT, ADEC_AC3_LONGTEXT )
358 ADD_INTEGER ( "vdec_smp", 0, NULL, VDEC_SMP_TEXT, VDEC_SMP_LONGTEXT )
359 ADD_STRING  ( "vpar_synchro", NULL, NULL, VPAR_SYNCHRO_TEXT, VPAR_SYNCHRO_LONGTEXT )
360
361 /* CPU options */
362 ADD_CATEGORY_HINT( "CPU", NULL )
363 ADD_BOOL    ( "nommx", NULL, NOMMX_TEXT, NOMMX_LONGTEXT )
364 ADD_BOOL    ( "no3dn", NULL, NO3DN_TEXT, NO3DN_LONGTEXT )
365 ADD_BOOL    ( "nommxext", NULL, NOMMXEXT_TEXT, NOMMXEXT_LONGTEXT )
366 ADD_BOOL    ( "nosse", NULL, NOSSE_TEXT, NOSSE_LONGTEXT )
367 ADD_BOOL    ( "noaltivec", NULL, NOALTIVEC_TEXT, NOALTIVEC_LONGTEXT )
368
369 /* Playlist options */
370 ADD_CATEGORY_HINT( "Playlist", NULL )
371 ADD_BOOL    ( "playlist_launch", NULL, PLAYLIST_LAUNCH_TEXT, PLAYLIST_LAUNCH_LONGTEXT )
372 ADD_BOOL    ( "playlist_enqueue", NULL, PLAYLIST_ENQUEUE_TEXT, PLAYLIST_ENQUEUE_LONGTEXT )
373 ADD_BOOL    ( "playlist_loop", NULL, PLAYLIST_LOOP_TEXT, PLAYLIST_LOOP_LONGTEXT )
374
375 /* Misc options */
376 ADD_CATEGORY_HINT( "Miscellaneous", NULL )
377 ADD_PLUGIN  ( "memcpy", MODULE_CAPABILITY_MEMCPY, NULL, NULL, MEMCPY_TEXT, MEMCPY_LONGTEXT )
378
379 MODULE_CONFIG_STOP
380
381 MODULE_INIT_START
382     SET_DESCRIPTION( "Main program" )
383     ADD_CAPABILITY( MAIN, 100/*whatever*/ )
384 MODULE_INIT_STOP
385
386 MODULE_ACTIVATE_START
387 MODULE_ACTIVATE_STOP
388
389 MODULE_DEACTIVATE_START
390 MODULE_DEACTIVATE_STOP
391
392 /* Hack for help options */
393 static module_t help_module;
394 static module_config_t p_help_config[] = {
395     { MODULE_CONFIG_ITEM_BOOL, "help", "print help (or use -h)",
396       NULL, NULL, 0, NULL, NULL, 0 },
397     { MODULE_CONFIG_ITEM_BOOL, "longhelp", "print detailed help (or use -H)",
398       NULL, NULL, 0, NULL, NULL, 0 },
399     { MODULE_CONFIG_ITEM_BOOL, "list", "print a list of available plugins "
400       "(or use -l)", NULL, NULL, 0, NULL, NULL, 0 },
401     { MODULE_CONFIG_ITEM_STRING, "plugin", "print help on plugin (or use -p)",
402       NULL, NULL, 0, NULL, &help_module.config_lock, 0 },
403     { MODULE_CONFIG_ITEM_BOOL, "version", "print version information",
404       NULL, NULL, 0, NULL, NULL, 0 },
405     { MODULE_CONFIG_HINT_END, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 } };
406
407
408 /*****************************************************************************
409  * End configuration.
410  *****************************************************************************/
411
412 /*****************************************************************************
413  * Global variables - these are the only ones, see main.h and modules.h
414  *****************************************************************************/
415 main_t        *p_main;
416 module_bank_t *p_module_bank;
417 input_bank_t  *p_input_bank;
418 aout_bank_t   *p_aout_bank;
419 vout_bank_t   *p_vout_bank;
420
421 /*****************************************************************************
422  * Local prototypes
423  *****************************************************************************/
424 static int  GetFilenames                ( int i_argc, char *ppsz_argv[] );
425 static void Usage                       ( const char *psz_module_name );
426 static void ListModules                 ( void );
427 static void Version                     ( void );
428
429 static void InitSignalHandler           ( void );
430 static void SimpleSignalHandler         ( int i_signal );
431 static void FatalSignalHandler          ( int i_signal );
432 static void IllegalSignalHandler        ( int i_signal );
433 static u32  CPUCapabilities             ( void );
434
435 #ifdef WIN32
436 static void ShowConsole                 ( void );
437 #endif
438
439 static jmp_buf env;
440 static int     i_illegal;
441 static char   *psz_capability;
442
443 /*****************************************************************************
444  * main: parse command line, start interface and spawn threads
445  *****************************************************************************
446  * Steps during program execution are:
447  *      -configuration parsing and messages interface initialization
448  *      -opening of audio output device and some global modules
449  *      -execution of interface, which exit on error or on user request
450  *      -closing of audio output device and some global modules
451  * On error, the spawned threads are canceled, and the open devices closed.
452  *****************************************************************************/
453 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
454 {
455     main_t        main_data;                /* root of all data - see main.h */
456     module_bank_t module_bank;
457     input_bank_t  input_bank;
458     aout_bank_t   aout_bank;
459     vout_bank_t   vout_bank;
460     char *psz_plugin;
461     char *p_tmp;
462
463     p_main        = &main_data;               /* set up the global variables */
464     p_module_bank = &module_bank;
465     p_input_bank  = &input_bank;
466     p_aout_bank   = &aout_bank;
467     p_vout_bank   = &vout_bank;
468
469     p_main->i_warning_level = 0;
470
471 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
472     /*
473      * Support for getext
474      */
475 #if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
476     if( !setlocale( LC_MESSAGES, "" ) )
477     {
478         fprintf( stderr, "warning: unsupported locale.\n" );
479     }
480 #endif
481
482     if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
483     {
484         fprintf( stderr, "warning: no domain %s in directory %s\n",
485                  PACKAGE, LOCALEDIR );
486     }
487
488     textdomain( PACKAGE );
489 #endif
490
491     /*
492      * Initialize threads system
493      */
494     vlc_threads_init();
495
496     /*
497      * Test if our code is likely to run on this CPU
498      */
499     p_main->i_cpu_capabilities = CPUCapabilities();
500
501     /*
502      * System specific initialization code
503      */
504 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
505     system_Init( &i_argc, ppsz_argv, ppsz_env );
506
507 #elif defined( SYS_LINUX )
508 #   ifdef DEBUG
509     /* Activate malloc checking routines to detect heap corruptions. */
510     putenv( "MALLOC_CHECK_=2" );
511     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
512 #   endif
513 #endif
514
515     /*
516      * Initialize messages interface
517      */
518     intf_MsgCreate();
519
520     intf_Msg( COPYRIGHT_MESSAGE "\n" );
521
522
523     /* Get the executable name (similar to the basename command) */
524     p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
525     while( *p_tmp )
526     {
527         if( *p_tmp == '/' ) p_main->psz_arg0 = ++p_tmp;
528         else ++p_tmp;
529     }
530
531     /*
532      * Initialize the module bank and and load the configuration of the main
533      * module. We need to do this at this stage to be able to display a short
534      * help if required by the user. (short help == main module options)
535      */
536     module_InitBank();
537     module_LoadMain();
538
539     /* Hack: insert the help module here */
540     help_module.psz_name = "help";
541     help_module.i_config_lines = sizeof(p_help_config) /
542                                      sizeof(module_config_t);
543     help_module.i_config_items = help_module.i_config_lines - 1;
544     vlc_mutex_init( &help_module.config_lock );
545     help_module.p_config = p_help_config;
546     help_module.next = p_module_bank->first;
547     p_module_bank->first = &help_module;
548     /* end hack */
549
550     if( config_LoadCmdLine( &i_argc, ppsz_argv, 1 ) )
551     {
552         intf_MsgDestroy();
553         return( errno );
554     }
555
556     /* Check for short help option */
557     if( config_GetIntVariable( "help" ) )
558     {
559         intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
560                   p_main->psz_arg0 );
561
562         Usage( "help" );
563         Usage( "main" );
564         return( -1 );
565     }
566
567     /* Check for version option */
568     if( config_GetIntVariable( "version" ) )
569     {
570         Version();
571         return( -1 );
572     }
573
574     /* Hack: remove the help module here */
575     p_module_bank->first = help_module.next;
576     /* end hack */
577
578     /*
579      * Load the builtins and plugins into the module_bank.
580      * We have to do it before config_Load*() because this also gets the
581      * list of configuration options exported by each plugin and loads their
582      * default values.
583      */
584     module_LoadBuiltins();
585     module_LoadPlugins();
586     intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
587                   p_module_bank->i_count );
588
589     /* Hack: insert the help module here */
590     help_module.next = p_module_bank->first;
591     p_module_bank->first = &help_module;
592     /* end hack */
593
594     /* Check for help on plugins */
595     if( (p_tmp = config_GetPszVariable( "plugin" )) )
596     {
597         Usage( p_tmp );
598         free( p_tmp );
599         return( -1 );
600     }
601
602     /* Check for long help option */
603     if( config_GetIntVariable( "longhelp" ) )
604     {
605         Usage( NULL );
606         return( -1 );
607     }
608
609     /* Check for plugin list option */
610     if( config_GetIntVariable( "list" ) )
611     {
612         ListModules();
613         return( -1 );
614     }
615
616     /* Hack: remove the help module here */
617     p_module_bank->first = help_module.next;
618     /* end hack */
619
620
621     /*
622      * Override default configuration with config file settings
623      */
624     vlc_mutex_init( &p_main->config_lock );
625     p_main->psz_homedir = config_GetHomeDir();
626     config_LoadConfigFile( NULL );
627
628     /*
629      * Override configuration with command line settings
630      */
631     if( config_LoadCmdLine( &i_argc, ppsz_argv, 0 ) )
632     {
633 #ifdef WIN32
634         ShowConsole();
635         /* Pause the console because it's destroyed when we exit */
636         intf_Msg( "The command line options couldn't be loaded, check that "
637                   "they are valid.\nPress the RETURN key to continue..." );
638         getchar();
639 #endif
640         intf_MsgDestroy();
641         return( errno );
642     }
643
644
645     /* p_main inititalization. FIXME ? */
646     p_main->i_desync = (mtime_t)config_GetIntVariable( "desync" )
647       * (mtime_t)1000;
648     p_main->b_stats = config_GetIntVariable( "stats" );
649     p_main->b_audio = !config_GetIntVariable( "noaudio" );
650     p_main->b_stereo= !config_GetIntVariable( "mono" );
651     p_main->b_video = !config_GetIntVariable( "novideo" );
652     if( config_GetIntVariable( "nommx" ) )
653         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
654     if( config_GetIntVariable( "no3dn" ) )
655         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
656     if( config_GetIntVariable( "nommxext" ) )
657         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
658     if( config_GetIntVariable( "nosse" ) )
659         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
660     if( config_GetIntVariable( "noaltivec" ) )
661         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
662
663
664     if( p_main->b_stats )
665     {
666         char          p_capabilities[200];
667         p_capabilities[0] = '\0';
668
669 #define PRINT_CAPABILITY( capability, string )                              \
670         if( p_main->i_cpu_capabilities & capability )                       \
671         {                                                                   \
672             strncat( p_capabilities, string " ",                            \
673                      sizeof(p_capabilities) - strlen(p_capabilities) );     \
674             p_capabilities[sizeof(p_capabilities) - 1] = '\0';              \
675         }
676
677         PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
678         PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
679         PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
680         PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
681         PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
682         PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
683         PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
684         PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
685         PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
686         intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
687     }
688
689     /*
690      * Initialize playlist and get commandline files
691      */
692     p_main->p_playlist = intf_PlaylistCreate();
693     if( !p_main->p_playlist )
694     {
695         intf_ErrMsg( "playlist error: playlist initialization failed" );
696         intf_MsgDestroy();
697         return( errno );
698     }
699     intf_PlaylistInit( p_main->p_playlist );
700
701     /*
702      * Get input filenames given as commandline arguments
703      */
704     GetFilenames( i_argc, ppsz_argv );
705
706     /*
707      * Initialize input, aout and vout banks
708      */
709     input_InitBank();
710     aout_InitBank();
711     vout_InitBank();
712
713     /*
714      * Choose the best memcpy module
715      */
716     psz_plugin = config_GetPszVariable( "memcpy" );
717     p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL,
718                                            psz_plugin );
719     if( psz_plugin ) free( psz_plugin );
720     if( p_main->p_memcpy_module == NULL )
721     {
722         intf_ErrMsg( "intf error: no suitable memcpy module, "
723                      "using libc default" );
724         p_main->pf_memcpy = memcpy;
725     }
726     else
727     {
728         p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
729                                   ->memcpy.functions.memcpy.pf_memcpy;
730     }
731
732     /*
733      * Initialize shared resources and libraries
734      */
735     if( config_GetIntVariable( "network_channel" ) &&
736         network_ChannelCreate() )
737     {
738         /* On error during Channels initialization, switch off channels */
739         intf_ErrMsg( "intf error: channels initialization failed, "
740                                  "deactivating channels" );
741         config_PutIntVariable( "network_channel", 0 );
742     }
743
744     /*
745      * Try to run the interface
746      */
747     p_main->p_intf = intf_Create();
748     if( p_main->p_intf == NULL )
749     {
750         intf_ErrMsg( "intf error: interface initialization failed" );
751     }
752     else
753     {
754         /*
755          * Set signal handling policy for all threads
756          */
757         InitSignalHandler();
758
759         /*
760          * This is the main loop
761          */
762         p_main->p_intf->pf_run( p_main->p_intf );
763
764         /*
765          * Finished, destroy the interface
766          */
767         intf_Destroy( p_main->p_intf );
768
769         /*
770          * Go back into channel 0 which is the network
771          */
772         if( config_GetIntVariable( "network_channel" ) )
773         {
774             network_ChannelJoin( COMMON_CHANNEL );
775         }
776     }
777
778     /*
779      * Free input, aout and vout banks
780      */
781     input_EndBank();
782     vout_EndBank();
783     aout_EndBank();
784
785     /*
786      * Free playlist
787      */
788     intf_PlaylistDestroy( p_main->p_playlist );
789
790     /*
791      * Free memcpy module if it was allocated
792      */
793     if( p_main->p_memcpy_module != NULL )
794     {
795         module_Unneed( p_main->p_memcpy_module );
796     }
797
798     /*
799      * Free module bank
800      */
801     module_EndBank();
802
803     /*
804      * System specific cleaning code
805      */
806 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
807     system_End();
808 #endif
809
810
811     /*
812      * Terminate messages interface and program
813      */
814     intf_WarnMsg( 1, "intf: program terminated" );
815     intf_MsgDestroy();
816
817     /*
818      * Stop threads system
819      */
820     vlc_threads_end( );
821
822     return 0;
823 }
824
825
826 /* following functions are local */
827
828 /*****************************************************************************
829  * GetFilenames: parse command line options which are not flags
830  *****************************************************************************
831  * Parse command line for input files.
832  *****************************************************************************/
833 static int GetFilenames( int i_argc, char *ppsz_argv[] )
834 {
835     int i_opt;
836
837     /* We assume that the remaining parameters are filenames */
838     for( i_opt = optind; i_opt < i_argc; i_opt++ )
839     {
840         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
841                           ppsz_argv[ i_opt ] );
842     }
843
844     return( 0 );
845 }
846
847 /*****************************************************************************
848  * Usage: print program usage
849  *****************************************************************************
850  * Print a short inline help. Message interface is initialized at this stage.
851  *****************************************************************************/
852 static void Usage( const char *psz_module_name )
853 {
854     int i;
855     module_t *p_module;
856     char psz_spaces[30];
857
858     memset( psz_spaces, 32, 30 );
859
860 #ifdef WIN32
861     ShowConsole();
862 #endif
863
864     /* Enumerate the config of each module */
865     for( p_module = p_module_bank->first ;
866          p_module != NULL ;
867          p_module = p_module->next )
868     {
869
870         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
871             continue;
872
873         /* ignore plugins without config options */
874         if( !p_module->i_config_items ) continue;
875
876         /* print module name */
877         intf_Msg( "%s options:\n", p_module->psz_name );
878
879         for( i = 0; i < p_module->i_config_lines; i++ )
880         {
881             int j;
882
883             switch( p_module->p_config[i].i_type )
884             {
885             case MODULE_CONFIG_HINT_CATEGORY:
886                 intf_Msg( " %s", p_module->p_config[i].psz_text );
887                 break;
888
889             case MODULE_CONFIG_ITEM_STRING:
890             case MODULE_CONFIG_ITEM_FILE:
891             case MODULE_CONFIG_ITEM_PLUGIN:
892                 /* Nasty hack, but right now I'm too tired to think about
893                  * a nice solution */
894                 j = 25 - strlen( p_module->p_config[i].psz_name )
895                     - strlen(" <string>") - 1;
896                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
897
898                 intf_Msg( "  --%s <string>%s %s",
899                           p_module->p_config[i].psz_name, psz_spaces,
900                           p_module->p_config[i].psz_text );
901                 psz_spaces[j] = 32;
902                 break;
903             case MODULE_CONFIG_ITEM_INTEGER:
904                 /* Nasty hack, but right now I'm too tired to think about
905                  * a nice solution */
906                 j = 25 - strlen( p_module->p_config[i].psz_name )
907                     - strlen(" <integer>") - 1;
908                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
909
910                 intf_Msg( "  --%s <integer>%s %s",
911                           p_module->p_config[i].psz_name, psz_spaces,
912                           p_module->p_config[i].psz_text );
913                 psz_spaces[j] = 32;
914                 break;
915             case MODULE_CONFIG_ITEM_BOOL:
916                 /* Nasty hack, but right now I'm too tired to think about
917                  * a nice solution */
918                 j = 25 - strlen( p_module->p_config[i].psz_name ) - 1;
919                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
920
921                 intf_Msg( "  --%s%s %s",
922                           p_module->p_config[i].psz_name, psz_spaces,
923                           p_module->p_config[i].psz_text );
924                 psz_spaces[j] = 32;
925                 break;
926             }
927         }
928
929         /* Yet another nasty hack.
930          * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
931          * for each module?? */
932         if( !strcmp( "main", p_module->psz_name ) )
933             intf_Msg( "\nPlaylist items:"
934                 "\n  *.mpg, *.vob                   \tPlain MPEG-1/2 files"
935                 "\n  [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
936                 "\n                                 \tDVD device"
937                 "\n  [vcd:][device][@[title][,[chapter]]"
938                 "\n                                 \tVCD device"
939                 "\n  udpstream:[<server>[:<server port>]][@[<bind address>]"
940                       "[:<bind port>]]"
941                 "\n                                 \tUDP stream sent by VLS"
942                 "\n  vlc:loop                       \tLoop execution of the "
943                       "playlist"
944                 "\n  vlc:pause                      \tPause execution of "
945                       "playlist items"
946                 "\n  vlc:quit                       \tQuit VLC" );
947
948         intf_Msg( "" );
949
950     }
951
952 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
953         intf_Msg( "\nPress the RETURN key to continue..." );
954         getchar();
955 #endif
956 }
957
958 /*****************************************************************************
959  * ListModules: list the available modules with their description
960  *****************************************************************************
961  * Print a list of all available modules (builtins and plugins) and a short
962  * description for each one.
963  *****************************************************************************/
964 static void ListModules( void )
965 {
966     module_t *p_module;
967     char psz_spaces[20];
968
969     memset( psz_spaces, 32, 20 );
970
971 #ifdef WIN32
972     ShowConsole();
973 #endif
974
975     /* Usage */
976     intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
977               p_main->psz_arg0 );
978
979     intf_Msg( "[plugin]              [description]" );
980
981     /* Enumerate each module */
982     for( p_module = p_module_bank->first ;
983          p_module != NULL ;
984          p_module = p_module->next )
985     {
986         int i;
987
988         /* Nasty hack, but right now I'm too tired to think about a nice
989          * solution */
990         i = 20 - strlen( p_module->psz_name ) - 1;
991         if( i < 0 ) i = 0;
992         psz_spaces[i] = 0;
993
994         intf_Msg( "  %s%s %s", p_module->psz_name, psz_spaces,
995                   p_module->psz_longname );
996
997         psz_spaces[i] = 32;
998
999     }
1000
1001 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1002         intf_Msg( "\nPress the RETURN key to continue..." );
1003         getchar();
1004 #endif
1005 }
1006
1007 /*****************************************************************************
1008  * Version: print complete program version
1009  *****************************************************************************
1010  * Print complete program version and build number.
1011  *****************************************************************************/
1012 static void Version( void )
1013 {
1014 #ifdef WIN32
1015     ShowConsole();
1016 #endif
1017
1018     intf_Msg( VERSION_MESSAGE
1019         "This program comes with NO WARRANTY, to the extent permitted by "
1020         "law.\nYou may redistribute it under the terms of the GNU General "
1021         "Public License;\nsee the file named COPYING for details.\n"
1022         "Written by the VideoLAN team at Ecole Centrale, Paris." );
1023
1024 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1025         intf_Msg( "\nPress the RETURN key to continue..." );
1026         getchar();
1027 #endif
1028 }
1029
1030 /*****************************************************************************
1031  * InitSignalHandler: system signal handler initialization
1032  *****************************************************************************
1033  * Set the signal handlers. SIGTERM is not intercepted, because we need at
1034  * at least a method to kill the program when all other methods failed, and
1035  * when we don't want to use SIGKILL.
1036  *****************************************************************************/
1037 static void InitSignalHandler( void )
1038 {
1039     /* Termination signals */
1040 #ifndef WIN32
1041     signal( SIGINT,  FatalSignalHandler );
1042     signal( SIGHUP,  FatalSignalHandler );
1043     signal( SIGQUIT, FatalSignalHandler );
1044
1045     /* Other signals */
1046     signal( SIGALRM, SimpleSignalHandler );
1047     signal( SIGPIPE, SimpleSignalHandler );
1048 #endif
1049 }
1050
1051 /*****************************************************************************
1052  * SimpleSignalHandler: system signal handler
1053  *****************************************************************************
1054  * This function is called when a non fatal signal is received by the program.
1055  *****************************************************************************/
1056 static void SimpleSignalHandler( int i_signal )
1057 {
1058     /* Acknowledge the signal received */
1059     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1060 }
1061
1062 /*****************************************************************************
1063  * FatalSignalHandler: system signal handler
1064  *****************************************************************************
1065  * This function is called when a fatal signal is received by the program.
1066  * It tries to end the program in a clean way.
1067  *****************************************************************************/
1068 static void FatalSignalHandler( int i_signal )
1069 {
1070     /* Once a signal has been trapped, the termination sequence will be
1071      * armed and following signals will be ignored to avoid sending messages
1072      * to an interface having been destroyed */
1073 #ifndef WIN32
1074     signal( SIGINT,  SIG_IGN );
1075     signal( SIGHUP,  SIG_IGN );
1076     signal( SIGQUIT, SIG_IGN );
1077 #endif
1078
1079     /* Acknowledge the signal received */
1080     intf_ErrMsg( "intf error: signal %d received, exiting", i_signal );
1081
1082     /* Try to terminate everything - this is done by requesting the end of the
1083      * interface thread */
1084     p_main->p_intf->b_die = 1;
1085 }
1086
1087 /*****************************************************************************
1088  * IllegalSignalHandler: system signal handler
1089  *****************************************************************************
1090  * This function is called when an illegal instruction signal is received by
1091  * the program. We use this function to test OS and CPU capabilities
1092  *****************************************************************************/
1093 static void IllegalSignalHandler( int i_signal )
1094 {
1095     /* Acknowledge the signal received */
1096     i_illegal = 1;
1097
1098 #ifdef HAVE_SIGRELSE
1099     sigrelse( i_signal );
1100 #endif
1101
1102     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1103                      "operating system.\n", psz_capability );
1104     fprintf( stderr, "         some optimizations will be disabled unless "
1105                      "you upgrade your OS\n" );
1106 #ifdef SYS_LINUX
1107     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)" );
1108 #endif
1109
1110     longjmp( env, 1 );
1111 }
1112
1113 /*****************************************************************************
1114  * CPUCapabilities: list the processors MMX support and other capabilities
1115  *****************************************************************************
1116  * This function is called to list extensions the CPU may have.
1117  *****************************************************************************/
1118 static u32 CPUCapabilities( void )
1119 {
1120     volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1121
1122 #if defined( SYS_DARWIN )
1123     struct host_basic_info hi;
1124     kern_return_t          ret;
1125     host_name_port_t       host;
1126
1127     int i_size;
1128     char *psz_name, *psz_subname;
1129
1130     i_capabilities |= CPU_CAPABILITY_FPU;
1131
1132     /* Should 'never' fail? */
1133     host = mach_host_self();
1134
1135     i_size = sizeof( hi ) / sizeof( int );
1136     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1137
1138     if( ret != KERN_SUCCESS )
1139     {
1140         fprintf( stderr, "error: couldn't get CPU information\n" );
1141         return( i_capabilities );
1142     }
1143
1144     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1145     /* FIXME: need better way to detect newer proccessors.
1146      * could do strncmp(a,b,5), but that's real ugly */
1147     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1148     {
1149         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1150     }
1151
1152     return( i_capabilities );
1153
1154 #elif defined( __i386__ )
1155     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
1156     volatile boolean_t     b_amd;
1157
1158     /* Needed for x86 CPU capabilities detection */
1159 #   define cpuid( a )                      \
1160         asm volatile ( "pushl %%ebx\n\t"   \
1161                        "cpuid\n\t"         \
1162                        "movl %%ebx,%1\n\t" \
1163                        "popl %%ebx\n\t"    \
1164                      : "=a" ( i_eax ),     \
1165                        "=r" ( i_ebx ),     \
1166                        "=c" ( i_ecx ),     \
1167                        "=d" ( i_edx )      \
1168                      : "a"  ( a )          \
1169                      : "cc" );
1170
1171     i_capabilities |= CPU_CAPABILITY_FPU;
1172
1173 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1174     signal( SIGILL, IllegalSignalHandler );
1175 #   endif
1176
1177     /* test for a 486 CPU */
1178     asm volatile ( "pushl %%ebx\n\t"
1179                    "pushfl\n\t"
1180                    "popl %%eax\n\t"
1181                    "movl %%eax, %%ebx\n\t"
1182                    "xorl $0x200000, %%eax\n\t"
1183                    "pushl %%eax\n\t"
1184                    "popfl\n\t"
1185                    "pushfl\n\t"
1186                    "popl %%eax\n\t"
1187                    "movl %%ebx,%1\n\t"
1188                    "popl %%ebx\n\t"
1189                  : "=a" ( i_eax ),
1190                    "=r" ( i_ebx )
1191                  :
1192                  : "cc" );
1193
1194     if( i_eax == i_ebx )
1195     {
1196 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1197         signal( SIGILL, NULL );
1198 #   endif
1199         return( i_capabilities );
1200     }
1201
1202     i_capabilities |= CPU_CAPABILITY_486;
1203
1204     /* the CPU supports the CPUID instruction - get its level */
1205     cpuid( 0x00000000 );
1206
1207     if( !i_eax )
1208     {
1209 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1210         signal( SIGILL, NULL );
1211 #   endif
1212         return( i_capabilities );
1213     }
1214
1215     /* FIXME: this isn't correct, since some 486s have cpuid */
1216     i_capabilities |= CPU_CAPABILITY_586;
1217
1218     /* borrowed from mpeg2dec */
1219     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1220                     && ( i_edx == 0x69746e65 );
1221
1222     /* test for the MMX flag */
1223     cpuid( 0x00000001 );
1224
1225     if( ! (i_edx & 0x00800000) )
1226     {
1227 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1228         signal( SIGILL, NULL );
1229 #   endif
1230         return( i_capabilities );
1231     }
1232
1233     i_capabilities |= CPU_CAPABILITY_MMX;
1234
1235     if( i_edx & 0x02000000 )
1236     {
1237         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1238
1239 #   ifdef CAN_COMPILE_SSE
1240         /* We test if OS support the SSE instructions */
1241         psz_capability = "SSE";
1242         i_illegal = 0;
1243         if( setjmp( env ) == 0 )
1244         {
1245             /* Test a SSE instruction */
1246             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1247         }
1248
1249         if( i_illegal == 0 )
1250         {
1251             i_capabilities |= CPU_CAPABILITY_SSE;
1252         }
1253 #   endif
1254     }
1255
1256     /* test for additional capabilities */
1257     cpuid( 0x80000000 );
1258
1259     if( i_eax < 0x80000001 )
1260     {
1261 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1262         signal( SIGILL, NULL );
1263 #   endif
1264         return( i_capabilities );
1265     }
1266
1267     /* list these additional capabilities */
1268     cpuid( 0x80000001 );
1269
1270 #   ifdef CAN_COMPILE_3DNOW
1271     if( i_edx & 0x80000000 )
1272     {
1273         psz_capability = "3D Now!";
1274         i_illegal = 0;
1275         if( setjmp( env ) == 0 )
1276         {
1277             /* Test a 3D Now! instruction */
1278             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1279         }
1280
1281         if( i_illegal == 0 )
1282         {
1283             i_capabilities |= CPU_CAPABILITY_3DNOW;
1284         }
1285     }
1286 #   endif
1287
1288     if( b_amd && ( i_edx & 0x00400000 ) )
1289     {
1290         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1291     }
1292
1293 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1294     signal( SIGILL, NULL );
1295 #   endif
1296     return( i_capabilities );
1297
1298 #elif defined( __powerpc__ )
1299
1300     i_capabilities |= CPU_CAPABILITY_FPU;
1301
1302 #   ifdef CAN_COMPILE_ALTIVEC
1303     signal( SIGILL, IllegalSignalHandler );
1304
1305     psz_capability = "AltiVec";
1306     i_illegal = 0;
1307     if( setjmp( env ) == 0 )
1308     {
1309         asm volatile ("mtspr 256, %0\n\t"
1310                       "vand %%v0, %%v0, %%v0"
1311                       :
1312                       : "r" (-1));
1313     }
1314
1315     if( i_illegal == 0 )
1316     {
1317         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1318     }
1319
1320     signal( SIGILL, NULL );
1321 #   endif
1322
1323     return( i_capabilities );
1324
1325 #else
1326     /* default behaviour */
1327     return( i_capabilities );
1328
1329 #endif
1330 }
1331
1332 /*****************************************************************************
1333  * ShowConsole: On Win32, create an output console for debug messages
1334  *****************************************************************************
1335  * This function is usefull only on Win32.
1336  *****************************************************************************/
1337 #ifdef WIN32 /*  */
1338 static void ShowConsole( void )
1339 {
1340     AllocConsole();
1341     freopen( "CONOUT$", "w", stdout );
1342     freopen( "CONOUT$", "w", stderr );
1343     freopen( "CONIN$", "r", stdin );
1344     return;
1345 }
1346 #endif