]> git.sesse.net Git - vlc/blob - src/interface/main.c
* AC3 IMDCT and downmix functions are now in plugins, --imdct and
[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, 1999, 2000 VideoLAN
7  * $Id: main.c,v 1.95 2001/05/15 16:19:42 sam Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
33 #include <stdio.h>                                              /* sprintf() */
34
35 #ifdef HAVE_GETOPT_LONG
36 #   ifdef HAVE_GETOPT_H
37 #       include <getopt.h>                                       /* getopt() */
38 #   endif
39 #else
40 #   include "GNUgetopt/getopt.h"
41 #endif
42
43 #ifdef SYS_DARWIN1_3
44 #   include <mach/mach.h>                               /* Altivec detection */
45 #   include <mach/mach_error.h>       /* some day the header files||compiler *
46                                                        will define it for us */
47 #   include <mach/bootstrap.h>
48 #endif
49
50 #include <unistd.h>
51 #include <errno.h>                                                 /* ENOMEM */
52 #include <stdlib.h>                                  /* getenv(), strtol(),  */
53 #include <string.h>                                            /* strerror() */
54
55 #include "config.h"
56 #include "common.h"
57 #include "debug.h"
58 #include "threads.h"
59 #include "mtime.h"
60 #include "tests.h"                                              /* TestCPU() */
61 #include "modules.h"
62
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65
66 #include "intf_msg.h"
67 #include "intf_playlist.h"
68 #include "interface.h"
69
70 #include "audio_output.h"
71
72 #include "video.h"
73 #include "video_output.h"
74
75 #ifdef SYS_BEOS
76 #   include "beos_specific.h"
77 #endif
78
79 #ifdef SYS_DARWIN1_3
80 #   include "darwin_specific.h"
81 #endif
82
83 #include "netutils.h"                                 /* network_ChannelJoin */
84
85 #include "main.h"
86
87 /*****************************************************************************
88  * Command line options constants. If something is changed here, be sure that
89  * GetConfiguration and Usage are also changed.
90  *****************************************************************************/
91
92 /* Long options return values - note that values corresponding to short options
93  * chars, and in general any regular char, should be avoided */
94 #define OPT_NOAUDIO             150
95 #define OPT_STEREO              151
96 #define OPT_MONO                152
97 #define OPT_SPDIF               153
98
99 #define OPT_NOVIDEO             160
100 #define OPT_DISPLAY             161
101 #define OPT_WIDTH               162
102 #define OPT_HEIGHT              163
103 #define OPT_COLOR               164
104 #define OPT_FULLSCREEN          165
105 #define OPT_OVERLAY             166
106
107 #define OPT_CHANNELS            170
108 #define OPT_SERVER              171
109 #define OPT_PORT                172
110 #define OPT_BROADCAST           173
111
112 #define OPT_INPUT               180
113 #define OPT_MOTION              181
114 #define OPT_IDCT                182
115 #define OPT_YUV                 183
116 #define OPT_DOWNMIX             184
117 #define OPT_IMDCT               185
118
119 #define OPT_SYNCHRO             190
120 #define OPT_WARNING             191
121 #define OPT_VERSION             192
122
123 /* Usage fashion */
124 #define USAGE                     0
125 #define SHORT_HELP                1
126 #define LONG_HELP                 2
127
128 /* Long options */
129 static const struct option longopts[] =
130 {
131     /*  name,               has_arg,    flag,   val */
132
133     /* General/common options */
134     {   "help",             0,          0,      'h' },
135     {   "longhelp",         0,          0,      'H' },
136     {   "version",          0,          0,      OPT_VERSION },
137
138     /* Interface options */
139     {   "intf",             1,          0,      'I' },
140     {   "warning",          1,          0,      OPT_WARNING },
141
142     /* Audio options */
143     {   "noaudio",          0,          0,      OPT_NOAUDIO },
144     {   "aout",             1,          0,      'A' },
145     {   "stereo",           0,          0,      OPT_STEREO },
146     {   "mono",             0,          0,      OPT_MONO },
147     {   "spdif",            0,          0,      OPT_SPDIF },
148     {   "downmix",          1,          0,      OPT_DOWNMIX },
149     {   "imdct",            1,          0,      OPT_IMDCT },
150
151     /* Video options */
152     {   "novideo",          0,          0,      OPT_NOVIDEO },
153     {   "vout",             1,          0,      'V' },
154     {   "display",          1,          0,      OPT_DISPLAY },
155     {   "width",            1,          0,      OPT_WIDTH },
156     {   "height",           1,          0,      OPT_HEIGHT },
157     {   "grayscale",        0,          0,      'g' },
158     {   "color",            0,          0,      OPT_COLOR },
159     {   "motion",           1,          0,      OPT_MOTION },
160     {   "idct",             1,          0,      OPT_IDCT },
161     {   "yuv",              1,          0,      OPT_YUV },
162     {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
163     {   "overlay",          0,          0,      OPT_OVERLAY },
164
165     /* DVD options */
166     {   "dvdtitle",         1,          0,      't' },
167     {   "dvdchapter",       1,          0,      'T' },
168     {   "dvdangle",         1,          0,      'u' },
169     {   "dvdaudio",         1,          0,      'a' },
170     {   "dvdchannel",       1,          0,      'c' },
171     {   "dvdsubtitle",      1,          0,      's' },
172     
173     /* Input options */
174     {   "input",            1,          0,      OPT_INPUT },
175     {   "channels",         0,          0,      OPT_CHANNELS },
176     {   "server",           1,          0,      OPT_SERVER },
177     {   "port",             1,          0,      OPT_PORT },
178     {   "broadcast",        1,          0,      OPT_BROADCAST },
179
180     /* Synchro options */
181     {   "synchro",          1,          0,      OPT_SYNCHRO },
182     {   0,                  0,          0,      0 }
183 };
184
185 /* Short options */
186 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
187
188 /*****************************************************************************
189  * Global variable program_data - these are the only ones, see main.h and
190  * modules.h
191  *****************************************************************************/
192 main_t        *p_main;
193 module_bank_t *p_module_bank;
194 aout_bank_t   *p_aout_bank;
195 vout_bank_t   *p_vout_bank;
196
197 /*****************************************************************************
198  * Local prototypes
199  *****************************************************************************/
200 static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
201                                       char *ppsz_env[] );
202 static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
203 static void Usage                   ( int i_fashion );
204 static void Version                 ( void );
205
206 static void InitSignalHandler       ( void );
207 static void SimpleSignalHandler     ( int i_signal );
208 static void FatalSignalHandler      ( int i_signal );
209
210 static int  CPUCapabilities         ( void );
211
212 /*****************************************************************************
213  * main: parse command line, start interface and spawn threads
214  *****************************************************************************
215  * Steps during program execution are:
216  *      -configuration parsing and messages interface initialization
217  *      -opening of audio output device and some global modules
218  *      -execution of interface, which exit on error or on user request
219  *      -closing of audio output device and some global modules
220  * On error, the spawned threads are canceled, and the open devices closed.
221  *****************************************************************************/
222 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
223 {
224     main_t        main_data;                /* root of all data - see main.h */
225     module_bank_t module_bank;
226     aout_bank_t   aout_bank;
227     vout_bank_t   vout_bank;
228
229     p_main        = &main_data;               /* set up the global variables */
230     p_module_bank = &module_bank;
231     p_aout_bank   = &aout_bank;
232     p_vout_bank   = &vout_bank;
233
234     /*
235      * Test if our code is likely to run on this CPU 
236      */
237     p_main->i_cpu_capabilities = CPUCapabilities();
238     
239 #if defined( __pentium__ ) || defined( __pentiumpro__ )
240     if( ! TestCPU( CPU_CAPABILITY_586 ) )
241     {
242         fprintf( stderr, "error: this program needs a Pentium CPU,\n"
243                          "please try a version without Pentium support\n" );
244         return( 1 );
245     }
246 #endif
247
248     /*
249      * System specific initialization code
250      */
251 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
252     system_Init( &i_argc, ppsz_argv, ppsz_env );
253 #endif
254
255     /*
256      * Initialize messages interface
257      */
258     p_main->p_msg = intf_MsgCreate();
259     if( !p_main->p_msg )                         /* start messages interface */
260     {
261         fprintf( stderr, "error: can't initialize messages interface (%s)\n",
262                  strerror(errno) );
263         return( errno );
264     }
265
266     intf_MsgImm( COPYRIGHT_MESSAGE );
267
268     /*
269      * Read configuration
270      */
271     if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
272     {
273         intf_MsgDestroy();
274         return( errno );
275     }
276
277     /*
278      * Initialize playlist and get commandline files
279      */
280     p_main->p_playlist = intf_PlaylistCreate();
281     if( !p_main->p_playlist )
282     {
283         intf_ErrMsg( "playlist error: playlist initialization failed" );
284         intf_MsgDestroy();
285         return( errno );
286     }
287     intf_PlaylistInit( p_main->p_playlist );
288
289     /*
290      * Get input filenames given as commandline arguments
291      */
292     GetFilenames( i_argc, ppsz_argv );
293
294     /*
295      * Initialize module, aout and vout banks
296      */
297     module_InitBank();
298     aout_InitBank();
299     vout_InitBank();
300
301     /*
302      * Initialize shared resources and libraries
303      */
304     if( p_main->b_channels && network_ChannelCreate() )
305     {
306         /* On error during Channels initialization, switch off channels */
307         intf_Msg( "Channels initialization failed : "
308                   "Channel management is deactivated" );
309         p_main->b_channels = 0;
310     }
311
312     /*
313      * Try to run the interface
314      */
315     p_main->p_intf = intf_Create();
316     if( p_main->p_intf == NULL )
317     {
318         intf_ErrMsg( "intf error: interface initialization failed" );
319     }
320     else
321     {
322         /*
323          * Set signal handling policy for all threads
324          */
325         InitSignalHandler();
326
327         /*
328          * This is the main loop
329          */
330         p_main->p_intf->pf_run( p_main->p_intf );
331
332         /*
333          * Finished, destroy the interface
334          */
335         intf_Destroy( p_main->p_intf );
336
337         /*
338          * Go back into channel 0 which is the network
339          */
340         if( p_main->b_channels )
341         {
342             network_ChannelJoin( COMMON_CHANNEL );
343         }
344     }
345
346     /*
347      * Free module, aout and vout banks
348      */
349     vout_EndBank();
350     aout_EndBank();
351     module_EndBank();
352
353     /*
354      * Free playlist
355      */
356     intf_PlaylistDestroy( p_main->p_playlist );
357
358     /*
359      * System specific cleaning code
360      */
361 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
362     system_End();
363 #endif
364
365     /*
366      * Terminate messages interface and program
367      */
368     intf_Msg( "intf: program terminated" );
369     intf_MsgDestroy();
370
371     return 0;
372 }
373
374 /*****************************************************************************
375  * main_GetIntVariable: get the int value of an environment variable
376  *****************************************************************************
377  * This function is used to read some default parameters in modules.
378  *****************************************************************************/
379 int main_GetIntVariable( char *psz_name, int i_default )
380 {
381     char *      psz_env;                                /* environment value */
382     char *      psz_end;                             /* end of parsing index */
383     long int    i_value;                                            /* value */
384
385     psz_env = getenv( psz_name );
386     if( psz_env )
387     {
388         i_value = strtol( psz_env, &psz_end, 0 );
389         if( (*psz_env != '\0') && (*psz_end == '\0') )
390         {
391             return( i_value );
392         }
393     }
394     return( i_default );
395 }
396
397 /*****************************************************************************
398  * main_GetPszVariable: get the string value of an environment variable
399  *****************************************************************************
400  * This function is used to read some default parameters in modules.
401  *****************************************************************************/
402 char * main_GetPszVariable( char *psz_name, char *psz_default )
403 {
404     char *psz_env;
405
406     psz_env = getenv( psz_name );
407     if( psz_env )
408     {
409         return( psz_env );
410     }
411     return( psz_default );
412 }
413
414 /*****************************************************************************
415  * main_PutPszVariable: set the string value of an environment variable
416  *****************************************************************************
417  * This function is used to set some default parameters in modules. The use of
418  * this function will cause some memory leak: since some systems use the pointer
419  * passed to putenv to store the environment string, it can't be freed.
420  *****************************************************************************/
421 void main_PutPszVariable( char *psz_name, char *psz_value )
422 {
423     char *psz_env;
424
425     psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
426     if( psz_env == NULL )
427     {
428         intf_ErrMsg( "intf error: cannot create psz_env (%s)",
429                      strerror(ENOMEM) );
430     }
431     else
432     {
433         sprintf( psz_env, "%s=%s", psz_name, psz_value );
434         if( putenv( psz_env ) )
435         {
436             intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
437         }
438     }
439 }
440
441 /*****************************************************************************
442  * main_PutIntVariable: set the integer value of an environment variable
443  *****************************************************************************
444  * This function is used to set some default parameters in modules. The use of
445  * this function will cause some memory leak: since some systems use the pointer
446  * passed to putenv to store the environment string, it can't be freed.
447  *****************************************************************************/
448 void main_PutIntVariable( char *psz_name, int i_value )
449 {
450     char psz_value[ 256 ];                               /* buffer for value */
451
452     sprintf( psz_value, "%d", i_value );
453     main_PutPszVariable( psz_name, psz_value );
454 }
455
456 /* following functions are local */
457
458 /*****************************************************************************
459  * GetConfiguration: parse command line
460  *****************************************************************************
461  * Parse command line and configuration file for configuration. If the inline
462  * help is requested, the function Usage() is called and the function returns
463  * -1 (causing main() to exit). The messages interface is initialized at this
464  * stage, but most structures are not allocated, so only environment should
465  * be used.
466  *****************************************************************************/
467 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
468 {
469     int   i_cmd;
470     char *p_tmp;
471
472     /* Set default configuration and copy arguments */
473     p_main->i_argc    = *pi_argc;
474     p_main->ppsz_argv = ppsz_argv;
475     p_main->ppsz_env  = ppsz_env;
476
477     p_main->b_audio     = 1;
478     p_main->b_video     = 1;
479     p_main->b_channels  = 0;
480
481     p_main->i_warning_level = 0;
482
483     /* Get the executable name (similar to the basename command) */
484     p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
485     while( *p_tmp )
486     {
487         if( *p_tmp == '/' )
488         {
489             p_main->psz_arg0 = ++p_tmp;
490         }
491         else
492         {
493             ++p_tmp;
494         }
495     }
496
497 #ifdef SYS_DARWIN1_3
498     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
499      * is the PSN - process serial number (a unique PID-ish thingie)
500      * still ok for real Darwin & when run from command line */
501     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
502                                         /* for example -psn_0_9306113 */
503     {
504         /* GDMF!... I can't do this or else the MacOSX window server will
505          * not pick up the PSN and not register the app and we crash...
506          * hence the following kludge otherwise we'll get confused w/ argv[1]
507          * being an input file name */
508 #if 0
509         ppsz_argv[ 1 ] = NULL;
510 #endif
511         *pi_argc = *pi_argc - 1;
512         pi_argc--;
513         return( 0 );
514     }
515 #endif
516
517     /* Parse command line options */
518     opterr = 0;
519     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
520                                    psz_shortopts, longopts, 0 ) ) != EOF )
521     {
522         switch( i_cmd )
523         {
524         /* General/common options */
525         case 'h':                                              /* -h, --help */
526             Usage( SHORT_HELP );
527             return( -1 );
528             break;
529         case 'H':                                          /* -H, --longhelp */
530             Usage( LONG_HELP );
531             return( -1 );
532             break;
533         case OPT_VERSION:                                       /* --version */
534             Version();
535             return( -1 );
536             break;
537         case 'v':                                           /* -v, --verbose */
538             p_main->i_warning_level++;
539             break;
540
541         /* Interface warning messages level */
542         case 'I':                                              /* -I, --intf */
543             main_PutPszVariable( INTF_METHOD_VAR, optarg );
544             break;
545         case OPT_WARNING:                                       /* --warning */
546             intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
547             p_main->i_warning_level = atoi(optarg);
548             break;
549
550         /* Audio options */
551         case OPT_NOAUDIO:                                       /* --noaudio */
552             p_main->b_audio = 0;
553             break;
554         case 'A':                                              /* -A, --aout */
555             main_PutPszVariable( AOUT_METHOD_VAR, optarg );
556             break;
557         case OPT_STEREO:                                         /* --stereo */
558             main_PutIntVariable( AOUT_STEREO_VAR, 1 );
559             break;
560         case OPT_MONO:                                             /* --mono */
561             main_PutIntVariable( AOUT_STEREO_VAR, 0 );
562             break;
563         case OPT_SPDIF:                                           /* --spdif */
564             main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
565             break;
566         case OPT_DOWNMIX:                                       /* --downmix */
567             main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
568             break;
569         case OPT_IMDCT:                                           /* --imdct */
570             main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
571             break;
572
573         /* Video options */
574         case OPT_NOVIDEO:                                       /* --novideo */
575             p_main->b_video = 0;
576             break;
577         case 'V':                                              /* -V, --vout */
578             main_PutPszVariable( VOUT_METHOD_VAR, optarg );
579             break;
580         case OPT_DISPLAY:                                       /* --display */
581             main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
582             break;
583         case OPT_WIDTH:                                           /* --width */
584             main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
585             break;
586         case OPT_HEIGHT:                                         /* --height */
587             main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
588             break;
589         case 'g':                                         /* -g, --grayscale */
590             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
591             break;
592         case OPT_COLOR:                                           /* --color */
593             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
594             break;
595         case OPT_FULLSCREEN:                                 /* --fullscreen */
596             main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
597             break;
598         case OPT_OVERLAY:                                       /* --overlay */
599             main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
600             break;
601         case OPT_MOTION:                                         /* --motion */
602             main_PutPszVariable( MOTION_METHOD_VAR, optarg );
603             break;
604         case OPT_IDCT:                                             /* --idct */
605             main_PutPszVariable( IDCT_METHOD_VAR, optarg );
606             break;
607         case OPT_YUV:                                               /* --yuv */
608             main_PutPszVariable( YUV_METHOD_VAR, optarg );
609             break;
610
611         /* DVD options */
612         case 't':
613             main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
614             break;
615         case 'T':
616             main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
617             break;
618         case 'u':
619             main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
620             break;
621         case 'a':
622             if ( ! strcmp(optarg, "ac3") )
623                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
624             else if ( ! strcmp(optarg, "lpcm") )
625                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
626             else if ( ! strcmp(optarg, "mpeg") )
627                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
628             else
629                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
630             break;
631         case 'c':
632             main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
633             break;
634         case 's':
635             main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
636             break;
637
638         /* Input options */
639         case OPT_INPUT:                                           /* --input */
640             main_PutPszVariable( INPUT_METHOD_VAR, optarg );
641             break;
642         case OPT_CHANNELS:                                     /* --channels */
643             p_main->b_channels = 1;
644             break;
645         case OPT_SERVER:                                         /* --server */
646             main_PutPszVariable( INPUT_SERVER_VAR, optarg );
647             break;
648         case OPT_PORT:                                             /* --port */
649             main_PutPszVariable( INPUT_PORT_VAR, optarg );
650             break;
651         case OPT_BROADCAST:                                   /* --broadcast */
652             main_PutPszVariable( INPUT_BROADCAST_VAR, optarg );
653             break;
654
655         /* Synchro options */
656         case OPT_SYNCHRO:                                      
657             main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
658             break;
659             
660         /* Internal error: unknown option */
661         case '?':
662         default:
663             intf_ErrMsg( "intf error: unknown option `%s'",
664                          ppsz_argv[optind - 1] );
665             Usage( USAGE );
666             return( EINVAL );
667             break;
668         }
669     }
670
671     if( p_main->i_warning_level < 0 )
672     {
673         p_main->i_warning_level = 0;
674     }
675
676     return( 0 );
677 }
678
679 /*****************************************************************************
680  * GetFilenames: parse command line options which are not flags
681  *****************************************************************************
682  * Parse command line for input files.
683  *****************************************************************************/
684 static int GetFilenames( int i_argc, char *ppsz_argv[] )
685 {
686     int i_opt;
687
688     /* We assume that the remaining parameters are filenames */
689     for( i_opt = optind; i_opt < i_argc; i_opt++ )
690     {
691         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
692                           ppsz_argv[ i_opt ] );
693     }
694
695     return( 0 );
696 }
697
698 /*****************************************************************************
699  * Usage: print program usage
700  *****************************************************************************
701  * Print a short inline help. Message interface is initialized at this stage.
702  *****************************************************************************/
703 static void Usage( int i_fashion )
704 {
705     /* Usage */
706     intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
707                  p_main->psz_arg0 );
708
709     if( i_fashion == USAGE )
710     {
711         intf_MsgImm( "Try `%s --help' for more information.",
712                      p_main->psz_arg0 );
713         return;
714     }
715
716     /* Options */
717     intf_MsgImm( "\nOptions:"
718           "\n  -I, --intf <module>            \tinterface method"
719           "\n  -v, --verbose                  \tverbose mode (cumulative)"
720           "\n"
721           "\n      --noaudio                  \tdisable audio"
722           "\n  -A, --aout <module>            \taudio output method"
723           "\n      --stereo, --mono           \tstereo/mono audio"
724           "\n      --spdif                    \tAC3 pass-through mode"
725           "\n      --downmix <module>         \tAC3 downmix method"
726           "\n      --imdct <module>           \tAC3 IMDCT method"
727           "\n"
728           "\n      --novideo                  \tdisable video"
729           "\n  -V, --vout <module>            \tvideo output method"
730           "\n      --display <display>        \tdisplay string"
731           "\n      --width <w>, --height <h>  \tdisplay dimensions"
732           "\n  -g, --grayscale                \tgrayscale output"
733           "\n      --fullscreen               \tfullscreen output"
734           "\n      --overlay                  \taccelerated display"
735           "\n      --color                    \tcolor output"
736           "\n      --motion <module>          \tmotion compensation method"
737           "\n      --idct <module>            \tIDCT method"
738           "\n      --yuv <module>             \tYUV method"
739           "\n      --synchro <type>           \tforce synchro algorithm"
740           "\n"
741           "\n  -t, --dvdtitle <num>           \tchoose DVD title"
742           "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
743           "\n  -u, --dvdangle <num>           \tchoose DVD angle"
744           "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
745           "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
746           "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
747           "\n"
748           "\n      --input                    \tinput method"
749           "\n      --channels                 \tenable channels"
750           "\n      --server <host>            \tvideo server address"
751           "\n      --port <port>              \tvideo server port"
752           "\n      --broadcast                \tlisten to a broadcast"
753           "\n"
754           "\n  -h, --help                     \tprint help and exit"
755           "\n  -H, --longhelp                 \tprint long help and exit"
756           "\n      --version                  \toutput version information and exit" );
757
758     if( i_fashion == SHORT_HELP )
759         return;
760
761     /* Interface parameters */
762     intf_MsgImm( "\nInterface parameters:"
763         "\n  " INTF_METHOD_VAR "=<method name>          \tinterface method"
764         "\n  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script"
765         "\n  " INTF_CHANNELS_VAR "=<filename>            \tchannels list" );
766
767     /* Audio parameters */
768     intf_MsgImm( "\nAudio parameters:"
769         "\n  " AOUT_METHOD_VAR "=<method name>        \taudio method"
770         "\n  " AOUT_DSP_VAR "=<filename>              \tdsp device path"
771         "\n  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output"
772         "\n  " AOUT_SPDIF_VAR "={1|0}                 \tAC3 pass-through mode"
773         "\n  " DOWNMIX_METHOD_VAR "=<method name>     \tAC3 downmix method"
774         "\n  " IMDCT_METHOD_VAR "=<method name>       \tAC3 IMDCT method"
775         "\n  " AOUT_RATE_VAR "=<rate>             \toutput rate" );
776
777     /* Video parameters */
778     intf_MsgImm( "\nVideo parameters:"
779         "\n  " VOUT_METHOD_VAR "=<method name>        \tdisplay method"
780         "\n  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used"
781         "\n  " VOUT_WIDTH_VAR "=<width>               \tdisplay width"
782         "\n  " VOUT_HEIGHT_VAR "=<height>             \tdislay height"
783         "\n  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path"
784         "\n  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output"
785         "\n  " VOUT_FULLSCREEN_VAR "={1|0}            \tfullscreen"
786         "\n  " VOUT_OVERLAY_VAR "={1|0}               \toverlay"
787         "\n  " MOTION_METHOD_VAR "=<method name>      \tmotion compensation method"
788         "\n  " IDCT_METHOD_VAR "=<method name>        \tIDCT method"
789         "\n  " YUV_METHOD_VAR "=<method name>         \tYUV method"
790         "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm" );
791
792     /* DVD parameters */
793     intf_MsgImm( "\nDVD parameters:"
794         "\n  " INPUT_DVD_DEVICE_VAR "=<device>           \tDVD device"
795         "\n  " INPUT_TITLE_VAR "=<title>             \ttitle number"
796         "\n  " INPUT_CHAPTER_VAR "=<chapter>         \tchapter number"
797         "\n  " INPUT_ANGLE_VAR "=<angle>             \tangle number"
798         "\n  " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
799         "\n  " INPUT_CHANNEL_VAR "=[0-15]            \taudio channel"
800         "\n  " INPUT_SUBTITLE_VAR "=[0-31]           \tsubtitle channel" );
801
802     /* Input parameters */
803     intf_MsgImm( "\nInput parameters:"
804         "\n  " INPUT_SERVER_VAR "=<hostname>          \tvideo server"
805         "\n  " INPUT_PORT_VAR "=<port>            \tvideo server port"
806         "\n  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface"
807         "\n  " INPUT_BROADCAST_VAR "=<addr>            \tbroadcast mode"
808         "\n  " INPUT_CHANNEL_SERVER_VAR "=<hostname>     \tchannel server"
809         "\n  " INPUT_CHANNEL_PORT_VAR "=<port>         \tchannel server port" );
810
811 }
812
813 /*****************************************************************************
814  * Version: print complete program version
815  *****************************************************************************
816  * Print complete program version and build number.
817  *****************************************************************************/
818 static void Version( void )
819 {
820     intf_MsgImm( VERSION_MESSAGE
821         "This program comes with NO WARRANTY, to the extent permitted by law.\n"
822         "You may redistribute it under the terms of the GNU General Public License;\n"
823         "see the file named COPYING for details.\n"
824         "Written by the VideoLAN team at Ecole Centrale, Paris." );
825 }
826
827 /*****************************************************************************
828  * InitSignalHandler: system signal handler initialization
829  *****************************************************************************
830  * Set the signal handlers. SIGTERM is not intercepted, because we need at
831  * at least a method to kill the program when all other methods failed, and
832  * when we don't want to use SIGKILL.
833  *****************************************************************************/
834 static void InitSignalHandler( void )
835 {
836     /* Termination signals */
837 #ifndef WIN32
838     signal( SIGINT,  FatalSignalHandler );
839     signal( SIGHUP,  FatalSignalHandler );
840     signal( SIGQUIT, FatalSignalHandler );
841
842     /* Other signals */
843     signal( SIGALRM, SimpleSignalHandler );
844     signal( SIGPIPE, SimpleSignalHandler );
845 #endif
846 }
847
848
849 /*****************************************************************************
850  * SimpleSignalHandler: system signal handler
851  *****************************************************************************
852  * This function is called when a non fatal signal is received by the program.
853  *****************************************************************************/
854 static void SimpleSignalHandler( int i_signal )
855 {
856     /* Acknowledge the signal received */
857     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
858 }
859
860
861 /*****************************************************************************
862  * FatalSignalHandler: system signal handler
863  *****************************************************************************
864  * This function is called when a fatal signal is received by the program.
865  * It tries to end the program in a clean way.
866  *****************************************************************************/
867 static void FatalSignalHandler( int i_signal )
868 {
869     /* Once a signal has been trapped, the termination sequence will be
870      * armed and following signals will be ignored to avoid sending messages
871      * to an interface having been destroyed */
872 #ifndef WIN32
873     signal( SIGINT,  SIG_IGN );
874     signal( SIGHUP,  SIG_IGN );
875     signal( SIGQUIT, SIG_IGN );
876 #endif
877
878     /* Acknowledge the signal received */
879     intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
880
881     /* Try to terminate everything - this is done by requesting the end of the
882      * interface thread */
883     p_main->p_intf->b_die = 1;
884 }
885
886 /*****************************************************************************
887  * CPUCapabilities: list the processors MMX support and other capabilities
888  *****************************************************************************
889  * This function is called to list extensions the CPU may have.
890  *****************************************************************************/
891 static int CPUCapabilities( void )
892 {
893     int i_capabilities = CPU_CAPABILITY_NONE;
894
895 #if defined( SYS_BEOS )
896     i_capabilities |= CPU_CAPABILITY_486
897                       | CPU_CAPABILITY_586
898                       | CPU_CAPABILITY_MMX;
899
900 #elif defined( SYS_DARWIN1_3 )
901
902     struct host_basic_info hi;
903     kern_return_t          ret;
904     host_name_port_t       host;
905
906     int i_size;
907     char *psz_name, *psz_subname;
908
909     /* Should 'never' fail? */
910     host = mach_host_self();
911
912     i_size = sizeof( hi ) / sizeof( int );
913     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
914
915     if( ret != KERN_SUCCESS )
916     {
917         intf_ErrMsg( "error: couldn't get CPU information" );
918         return( i_capabilities );
919     }
920
921     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
922     /* FIXME: need better way to detect newer proccessors.
923      * could do strncmp(a,b,5), but that's real ugly */
924     if( strcmp(psz_name, "ppc7400") || strcmp(psz_name, "ppc7450") )
925     {
926         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
927     }
928
929 #elif defined( __i386__ )
930     unsigned int  i_eax, i_ebx, i_ecx, i_edx;
931     boolean_t     b_amd;
932
933 #   define cpuid( a )              \
934     asm volatile ( "cpuid"         \
935                  : "=a" ( i_eax ), \
936                    "=b" ( i_ebx ), \
937                    "=c" ( i_ecx ), \
938                    "=d" ( i_edx )  \
939                  : "a"  ( a )      \
940                  : "cc" );         \
941
942     /* test for a 486 CPU */
943     asm volatile ( "pushfl\n\t"
944                    "popl %%eax\n\t"
945                    "movl %%eax, %%ebx\n\t"
946                    "xorl $0x200000, %%eax\n\t"
947                    "pushl %%eax\n\t"
948                    "popfl\n\t"
949                    "pushfl\n\t"
950                    "popl %%eax"
951                  : "=a" ( i_eax ),
952                    "=b" ( i_ebx )
953                  :
954                  : "cc" );
955
956     if( i_eax == i_ebx )
957     {
958         return( i_capabilities );
959     }
960
961     i_capabilities |= CPU_CAPABILITY_486;
962
963     /* the CPU supports the CPUID instruction - get its level */
964     cpuid( 0x00000000 );
965
966     if( !i_eax )
967     {
968         return( i_capabilities );
969     }
970
971     /* FIXME: this isn't correct, since some 486s have cpuid */
972     i_capabilities |= CPU_CAPABILITY_586;
973
974     /* borrowed from mpeg2dec */
975     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
976                     && ( i_edx == 0x69746e65 );
977
978     /* test for the MMX flag */
979     cpuid( 0x00000001 );
980
981     if( ! (i_edx & 0x00800000) )
982     {
983         return( i_capabilities );
984     }
985
986     i_capabilities |= CPU_CAPABILITY_MMX;
987
988     if( i_edx & 0x02000000 )
989     {
990         i_capabilities |= CPU_CAPABILITY_MMXEXT;
991         i_capabilities |= CPU_CAPABILITY_SSE;
992     }
993     
994     /* test for additional capabilities */
995     cpuid( 0x80000000 );
996
997     if( i_eax < 0x80000001 )
998     {
999         return( i_capabilities );
1000     }
1001
1002     /* list these additional capabilities */
1003     cpuid( 0x80000001 );
1004
1005     if( i_edx & 0x80000000 )
1006     {
1007         i_capabilities |= CPU_CAPABILITY_3DNOW;
1008     }
1009
1010     if( b_amd && ( i_edx & 0x00400000 ) )
1011     {
1012         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1013     }
1014 #else
1015     /* default behaviour */
1016
1017 #endif
1018     return( i_capabilities );
1019 }
1020