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