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