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