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