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