]> git.sesse.net Git - vlc/blob - src/interface/main.c
76fa95902cc044d42ea6c0769e6e1e1e2bb2f164
[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.2.1 2002/06/02 23:01:32 sam Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *          Gildas Bazin <gbazin@netcourrier.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <setjmp.h>                                       /* longjmp, setjmp */
34
35 #include <videolan/vlc.h>
36
37 #ifdef HAVE_GETOPT_LONG
38 #   ifdef HAVE_GETOPT_H
39 #       include <getopt.h>                                       /* getopt() */
40 #   endif
41 #else
42 #   include "GNUgetopt/getopt.h"
43 #endif
44
45 #ifdef SYS_DARWIN
46 #   include <mach/mach.h>                               /* Altivec detection */
47 #   include <mach/mach_error.h>       /* some day the header files||compiler *
48                                                        will define it for us */
49 #   include <mach/bootstrap.h>
50 #endif
51
52 #ifndef WIN32
53 #   include <netinet/in.h>                            /* BSD: struct in_addr */
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( _MSC_VER ) && defined( _WIN32 )
59 #   include <io.h>
60 #endif
61
62 #ifdef HAVE_LOCALE_H
63 #   include <locale.h>
64 #endif
65
66 #include <errno.h>                                                 /* ENOMEM */
67 #include <stdlib.h>                                  /* getenv(), strtol(),  */
68 #include <string.h>                                            /* strerror() */
69
70 #include "netutils.h"                                 /* network_ChannelJoin */
71
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
74
75 #include "intf_playlist.h"
76 #include "interface.h"
77
78 #include "audio_output.h"
79
80 #include "video.h"
81 #include "video_output.h"
82
83 /*****************************************************************************
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%s"
928     /* option name -------------'     | | | |  | |
929      * <bra --------------------------' | | |  | |
930      * option type or "" ---------------' | |  | |
931      * ket> ------------------------------' |  | |
932      * padding spaces ----------------------'  | |
933      * comment --------------------------------' |
934      * comment suffix ---------------------------'
935      *
936      * The purpose of having bra and ket is that we might i18n them as well.
937      */
938 #define LINE_START 8
939 #define PADDING_SPACES 25
940     module_t *p_module;
941     module_config_t *p_item;
942     char psz_spaces[PADDING_SPACES+LINE_START+1];
943     char psz_format[sizeof(FORMAT_STRING)];
944
945     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
946     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
947
948     strcpy( psz_format, FORMAT_STRING );
949
950 #ifdef WIN32
951     ShowConsole();
952 #endif
953
954     /* Enumerate the config for each module */
955     for( p_module = p_module_bank->first ;
956          p_module != NULL ;
957          p_module = p_module->next )
958     {
959         boolean_t b_help_module = !strcmp( "help", p_module->psz_name );
960
961         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
962             continue;
963
964         /* ignore modules without config options */
965         if( !p_module->i_config_items ) continue;
966
967         /* print module name */
968         intf_Msg( _("%s module options:\n"), p_module->psz_name );
969
970         for( p_item = p_module->p_config;
971              p_item->i_type != MODULE_CONFIG_HINT_END;
972              p_item++ )
973         {
974             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
975             char *psz_suf = "", *psz_prefix = NULL;
976             int i;
977
978             switch( p_item->i_type )
979             {
980             case MODULE_CONFIG_HINT_CATEGORY:
981                 intf_Msg( " %s", p_item->psz_text );
982                 break;
983
984             case MODULE_CONFIG_ITEM_STRING:
985             case MODULE_CONFIG_ITEM_FILE:
986             case MODULE_CONFIG_ITEM_MODULE: /* We could also have "=<" here */
987                 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
988                 break;
989             case MODULE_CONFIG_ITEM_INTEGER:
990                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
991                 break;
992             case MODULE_CONFIG_ITEM_FLOAT:
993                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
994                 break;
995             case MODULE_CONFIG_ITEM_BOOL:
996                 psz_bra = ""; psz_type = ""; psz_ket = "";
997                 if( !b_help_module )
998                     psz_suf = p_item->i_value ? _(" (default enabled)") :
999                                                 _(" (default disabled)");
1000                 break;
1001             }
1002
1003             /* Add short option */
1004             if( p_item->i_short )
1005             {
1006                 psz_format[2] = '-';
1007                 psz_format[3] = p_item->i_short;
1008                 psz_format[4] = ',';
1009             }
1010             else
1011             {
1012                 psz_format[2] = ' ';
1013                 psz_format[3] = ' ';
1014                 psz_format[4] = ' ';
1015             }
1016
1017             if( psz_type )
1018             {
1019                 i = PADDING_SPACES - strlen( p_item->psz_name )
1020                      - strlen( psz_bra ) - strlen( psz_type )
1021                      - strlen( psz_ket ) - 1;
1022                 if( p_item->i_type == MODULE_CONFIG_ITEM_BOOL &&
1023                     !b_help_module )
1024                 {
1025                     boolean_t b_dash = 0;
1026                     psz_prefix = p_item->psz_name;
1027                     while( *psz_prefix )
1028                     {
1029                         if( *psz_prefix++ == '-' )
1030                         {
1031                             b_dash = 1;
1032                             break;
1033                         }
1034                     }
1035
1036                     if( b_dash )
1037                     {
1038                         psz_prefix = ", --no-";
1039                         i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
1040                     }
1041                     else
1042                     {
1043                         psz_prefix = ", --no";
1044                         i -= strlen( p_item->psz_name ) + strlen( ", --no" );
1045                     }
1046                 }
1047
1048                 if( i < 0 )
1049                 {
1050                     i = 0;
1051                     psz_spaces[i] = '\n';
1052                 }
1053                 else
1054                 {
1055                     psz_spaces[i] = '\0';
1056                 }
1057
1058                 if( p_item->i_type == MODULE_CONFIG_ITEM_BOOL &&
1059                     !b_help_module )
1060                 {
1061                     intf_Msg( psz_format, p_item->psz_name, psz_prefix,
1062                               p_item->psz_name, psz_bra, psz_type, psz_ket,
1063                               psz_spaces, p_item->psz_text, psz_suf );
1064                 }
1065                 else
1066                 {
1067                     intf_Msg( psz_format, p_item->psz_name, "", "",
1068                               psz_bra, psz_type, psz_ket, psz_spaces,
1069                               p_item->psz_text, psz_suf );
1070                 }
1071                 psz_spaces[i] = ' ';
1072             }
1073         }
1074
1075         /* Yet another nasty hack.
1076          * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
1077          * for each module?? */
1078         if( !strcmp( "main", p_module->psz_name ) )
1079         {
1080             intf_Msg( _("\nPlaylist items:"
1081                 "\n  *.mpg, *.vob                   plain MPEG-1/2 files"
1082                 "\n  [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
1083                 "\n                                 DVD device"
1084                 "\n  [vcd:][device][@[title][,[chapter]]"
1085                 "\n                                 VCD device"
1086                 "\n  udpstream:[@[<bind address>][:<bind port>]]"
1087                 "\n                                 UDP stream sent by VLS"
1088                 "\n  vlc:loop                       loop execution of the "
1089                       "playlist"
1090                 "\n  vlc:pause                      pause execution of "
1091                       "playlist items"
1092                 "\n  vlc:quit                       quit VLC") );
1093         }
1094
1095         intf_Msg( "" );
1096
1097     }
1098
1099 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1100         intf_Msg( _("\nPress the RETURN key to continue...") );
1101         getchar();
1102 #endif
1103 }
1104
1105 /*****************************************************************************
1106  * ListModules: list the available modules with their description
1107  *****************************************************************************
1108  * Print a list of all available modules (builtins and plugins) and a short
1109  * description for each one.
1110  *****************************************************************************/
1111 static void ListModules( void )
1112 {
1113     module_t *p_module;
1114     char psz_spaces[22];
1115
1116     memset( psz_spaces, ' ', 22 );
1117
1118 #ifdef WIN32
1119     ShowConsole();
1120 #endif
1121
1122     /* Usage */
1123     intf_Msg( _("Usage: %s [options] [parameters] [file]...\n"),
1124               p_main->psz_arg0 );
1125
1126     intf_Msg( _("[module]              [description]") );
1127
1128     /* Enumerate each module */
1129     for( p_module = p_module_bank->first ;
1130          p_module != NULL ;
1131          p_module = p_module->next )
1132     {
1133         int i;
1134
1135         /* Nasty hack, but right now I'm too tired to think about a nice
1136          * solution */
1137         i = 22 - strlen( p_module->psz_name ) - 1;
1138         if( i < 0 ) i = 0;
1139         psz_spaces[i] = 0;
1140
1141         intf_Msg( "  %s%s %s", p_module->psz_name, psz_spaces,
1142                   p_module->psz_longname );
1143
1144         psz_spaces[i] = ' ';
1145
1146     }
1147
1148 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1149         intf_Msg( _("\nPress the RETURN key to continue...") );
1150         getchar();
1151 #endif
1152 }
1153
1154 /*****************************************************************************
1155  * Version: print complete program version
1156  *****************************************************************************
1157  * Print complete program version and build number.
1158  *****************************************************************************/
1159 static void Version( void )
1160 {
1161 #ifdef WIN32
1162     ShowConsole();
1163 #endif
1164
1165     intf_Msg( VERSION_MESSAGE );
1166     intf_Msg(
1167       _("This program comes with NO WARRANTY, to the extent permitted by "
1168         "law.\nYou may redistribute it under the terms of the GNU General "
1169         "Public License;\nsee the file named COPYING for details.\n"
1170         "Written by the VideoLAN team at Ecole Centrale, Paris.") );
1171
1172 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1173         intf_Msg( _("\nPress the RETURN key to continue...") );
1174         getchar();
1175 #endif
1176 }
1177
1178 /*****************************************************************************
1179  * InitSignalHandler: system signal handler initialization
1180  *****************************************************************************
1181  * Set the signal handlers. SIGTERM is not intercepted, because we need at
1182  * at least a method to kill the program when all other methods failed, and
1183  * when we don't want to use SIGKILL.
1184  *****************************************************************************/
1185 static void InitSignalHandler( void )
1186 {
1187     /* Termination signals */
1188 #ifndef WIN32
1189     signal( SIGINT,  FatalSignalHandler );
1190     signal( SIGHUP,  FatalSignalHandler );
1191     signal( SIGQUIT, FatalSignalHandler );
1192
1193     /* Other signals */
1194     signal( SIGALRM, SimpleSignalHandler );
1195     signal( SIGPIPE, SimpleSignalHandler );
1196 #endif
1197 }
1198
1199 /*****************************************************************************
1200  * SimpleSignalHandler: system signal handler
1201  *****************************************************************************
1202  * This function is called when a non fatal signal is received by the program.
1203  *****************************************************************************/
1204 static void SimpleSignalHandler( int i_signal )
1205 {
1206     /* Acknowledge the signal received */
1207     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1208 }
1209
1210 /*****************************************************************************
1211  * FatalSignalHandler: system signal handler
1212  *****************************************************************************
1213  * This function is called when a fatal signal is received by the program.
1214  * It tries to end the program in a clean way.
1215  *****************************************************************************/
1216 static void FatalSignalHandler( int i_signal )
1217 {
1218     static mtime_t abort_time = 0;
1219     static volatile boolean_t b_die = 0;
1220
1221     /* Once a signal has been trapped, the termination sequence will be
1222      * armed and following signals will be ignored to avoid sending messages
1223      * to an interface having been destroyed */
1224     if( !b_die )
1225     {
1226         b_die = 1;
1227         abort_time = mdate();
1228
1229         /* Acknowledge the signal received */
1230         intf_ErrMsg( "intf error: signal %d received, exiting - do it again "
1231                      "if vlc gets stuck", i_signal );
1232
1233         /* Try to terminate everything - this is done by requesting the end
1234          * of the interface thread */
1235         p_main->p_intf->b_die = 1;
1236
1237         return;
1238     }
1239
1240     /* If user asks again 1 second later, die badly */
1241     if( mdate() > abort_time + 1000000 )
1242     {
1243 #ifndef WIN32
1244         signal( SIGINT,  SIG_IGN );
1245         signal( SIGHUP,  SIG_IGN );
1246         signal( SIGQUIT, SIG_IGN );
1247 #endif
1248
1249         intf_ErrMsg( "intf error: user insisted too much, dying badly" );
1250         exit( 1 );
1251     }
1252 }
1253
1254 /*****************************************************************************
1255  * IllegalSignalHandler: system signal handler
1256  *****************************************************************************
1257  * This function is called when an illegal instruction signal is received by
1258  * the program. We use this function to test OS and CPU capabilities
1259  *****************************************************************************/
1260 static void IllegalSignalHandler( int i_signal )
1261 {
1262     /* Acknowledge the signal received */
1263     i_illegal = 1;
1264
1265 #ifdef HAVE_SIGRELSE
1266     sigrelse( i_signal );
1267 #endif
1268
1269 #if defined( __i386__ )
1270     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1271                      "operating system.\n", psz_capability );
1272     fprintf( stderr, "         some optimizations will be disabled unless "
1273                      "you upgrade your OS\n" );
1274 #   if defined( SYS_LINUX )
1275     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
1276 #   endif
1277 #endif
1278
1279     longjmp( env, 1 );
1280 }
1281
1282 /*****************************************************************************
1283  * CPUCapabilities: list the processors MMX support and other capabilities
1284  *****************************************************************************
1285  * This function is called to list extensions the CPU may have.
1286  *****************************************************************************/
1287 static u32 CPUCapabilities( void )
1288 {
1289     volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1290
1291 #if defined( SYS_DARWIN )
1292     struct host_basic_info hi;
1293     kern_return_t          ret;
1294     host_name_port_t       host;
1295
1296     int i_size;
1297     char *psz_name, *psz_subname;
1298
1299     i_capabilities |= CPU_CAPABILITY_FPU;
1300
1301     /* Should 'never' fail? */
1302     host = mach_host_self();
1303
1304     i_size = sizeof( hi ) / sizeof( int );
1305     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1306
1307     if( ret != KERN_SUCCESS )
1308     {
1309         fprintf( stderr, "error: couldn't get CPU information\n" );
1310         return( i_capabilities );
1311     }
1312
1313     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1314     /* FIXME: need better way to detect newer proccessors.
1315      * could do strncmp(a,b,5), but that's real ugly */
1316     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1317     {
1318         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1319     }
1320
1321     return( i_capabilities );
1322
1323 #elif defined( __i386__ )
1324     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
1325     volatile boolean_t     b_amd;
1326
1327     /* Needed for x86 CPU capabilities detection */
1328 #   define cpuid( a )                      \
1329         asm volatile ( "pushl %%ebx\n\t"   \
1330                        "cpuid\n\t"         \
1331                        "movl %%ebx,%1\n\t" \
1332                        "popl %%ebx\n\t"    \
1333                      : "=a" ( i_eax ),     \
1334                        "=r" ( i_ebx ),     \
1335                        "=c" ( i_ecx ),     \
1336                        "=d" ( i_edx )      \
1337                      : "a"  ( a )          \
1338                      : "cc" );
1339
1340     i_capabilities |= CPU_CAPABILITY_FPU;
1341
1342 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1343     signal( SIGILL, IllegalSignalHandler );
1344 #   endif
1345
1346     /* test for a 486 CPU */
1347     asm volatile ( "pushl %%ebx\n\t"
1348                    "pushfl\n\t"
1349                    "popl %%eax\n\t"
1350                    "movl %%eax, %%ebx\n\t"
1351                    "xorl $0x200000, %%eax\n\t"
1352                    "pushl %%eax\n\t"
1353                    "popfl\n\t"
1354                    "pushfl\n\t"
1355                    "popl %%eax\n\t"
1356                    "movl %%ebx,%1\n\t"
1357                    "popl %%ebx\n\t"
1358                  : "=a" ( i_eax ),
1359                    "=r" ( i_ebx )
1360                  :
1361                  : "cc" );
1362
1363     if( i_eax == i_ebx )
1364     {
1365 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1366         signal( SIGILL, NULL );
1367 #   endif
1368         return( i_capabilities );
1369     }
1370
1371     i_capabilities |= CPU_CAPABILITY_486;
1372
1373     /* the CPU supports the CPUID instruction - get its level */
1374     cpuid( 0x00000000 );
1375
1376     if( !i_eax )
1377     {
1378 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1379         signal( SIGILL, NULL );
1380 #   endif
1381         return( i_capabilities );
1382     }
1383
1384     /* FIXME: this isn't correct, since some 486s have cpuid */
1385     i_capabilities |= CPU_CAPABILITY_586;
1386
1387     /* borrowed from mpeg2dec */
1388     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1389                     && ( i_edx == 0x69746e65 );
1390
1391     /* test for the MMX flag */
1392     cpuid( 0x00000001 );
1393
1394     if( ! (i_edx & 0x00800000) )
1395     {
1396 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1397         signal( SIGILL, NULL );
1398 #   endif
1399         return( i_capabilities );
1400     }
1401
1402     i_capabilities |= CPU_CAPABILITY_MMX;
1403
1404     if( i_edx & 0x02000000 )
1405     {
1406         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1407
1408 #   ifdef CAN_COMPILE_SSE
1409         /* We test if OS supports the SSE instructions */
1410         psz_capability = "SSE";
1411         i_illegal = 0;
1412         if( setjmp( env ) == 0 )
1413         {
1414             /* Test a SSE instruction */
1415             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1416         }
1417
1418         if( i_illegal == 0 )
1419         {
1420             i_capabilities |= CPU_CAPABILITY_SSE;
1421         }
1422 #   endif
1423     }
1424
1425     /* test for additional capabilities */
1426     cpuid( 0x80000000 );
1427
1428     if( i_eax < 0x80000001 )
1429     {
1430 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1431         signal( SIGILL, NULL );
1432 #   endif
1433         return( i_capabilities );
1434     }
1435
1436     /* list these additional capabilities */
1437     cpuid( 0x80000001 );
1438
1439 #   ifdef CAN_COMPILE_3DNOW
1440     if( i_edx & 0x80000000 )
1441     {
1442         psz_capability = "3D Now!";
1443         i_illegal = 0;
1444         if( setjmp( env ) == 0 )
1445         {
1446             /* Test a 3D Now! instruction */
1447             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1448         }
1449
1450         if( i_illegal == 0 )
1451         {
1452             i_capabilities |= CPU_CAPABILITY_3DNOW;
1453         }
1454     }
1455 #   endif
1456
1457     if( b_amd && ( i_edx & 0x00400000 ) )
1458     {
1459         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1460     }
1461
1462 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1463     signal( SIGILL, NULL );
1464 #   endif
1465     return( i_capabilities );
1466
1467 #elif defined( __powerpc__ )
1468
1469     i_capabilities |= CPU_CAPABILITY_FPU;
1470
1471 #   ifdef CAN_COMPILE_ALTIVEC
1472     signal( SIGILL, IllegalSignalHandler );
1473
1474     psz_capability = "AltiVec";
1475     i_illegal = 0;
1476     if( setjmp( env ) == 0 )
1477     {
1478         asm volatile ("mtspr 256, %0\n\t"
1479                       "vand %%v0, %%v0, %%v0"
1480                       :
1481                       : "r" (-1));
1482     }
1483
1484     if( i_illegal == 0 )
1485     {
1486         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1487     }
1488
1489     signal( SIGILL, NULL );
1490 #   endif
1491
1492     return( i_capabilities );
1493
1494 #elif defined( __sparc__ )
1495
1496     i_capabilities |= CPU_CAPABILITY_FPU;
1497     return( i_capabilities );
1498
1499 #else
1500     /* default behaviour */
1501     return( i_capabilities );
1502
1503 #endif
1504 }
1505
1506 /*****************************************************************************
1507  * ShowConsole: On Win32, create an output console for debug messages
1508  *****************************************************************************
1509  * This function is useful only on Win32.
1510  *****************************************************************************/
1511 #ifdef WIN32 /*  */
1512 static void ShowConsole( void )
1513 {
1514     AllocConsole();
1515     freopen( "CONOUT$", "w", stdout );
1516     freopen( "CONOUT$", "w", stderr );
1517     freopen( "CONIN$", "r", stdin );
1518     return;
1519 }
1520 #endif