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