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