]> git.sesse.net Git - vlc/blob - src/interface/main.c
-Changes and bugfixes to make network work in VLAN Broadcast mode.
[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.97 2001/05/30 05:19:03 stef 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             main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
649             break;
650         case OPT_SERVER:                                         /* --server */
651             main_PutPszVariable( INPUT_SERVER_VAR, optarg );
652             break;
653         case OPT_PORT:                                             /* --port */
654             main_PutPszVariable( INPUT_PORT_VAR, optarg );
655             break;
656         case OPT_BROADCAST:                                   /* --broadcast */
657             main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
658             main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
659             break;
660
661         /* Synchro options */
662         case OPT_SYNCHRO:                                      
663             main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
664             break;
665             
666         /* Internal error: unknown option */
667         case '?':
668         default:
669             intf_ErrMsg( "intf error: unknown option `%s'",
670                          ppsz_argv[optind - 1] );
671             Usage( USAGE );
672             return( EINVAL );
673             break;
674         }
675     }
676
677     if( p_main->i_warning_level < 0 )
678     {
679         p_main->i_warning_level = 0;
680     }
681
682     return( 0 );
683 }
684
685 /*****************************************************************************
686  * GetFilenames: parse command line options which are not flags
687  *****************************************************************************
688  * Parse command line for input files.
689  *****************************************************************************/
690 static int GetFilenames( int i_argc, char *ppsz_argv[] )
691 {
692     int i_opt;
693
694     /* We assume that the remaining parameters are filenames */
695     for( i_opt = optind; i_opt < i_argc; i_opt++ )
696     {
697         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
698                           ppsz_argv[ i_opt ] );
699     }
700
701     return( 0 );
702 }
703
704 /*****************************************************************************
705  * Usage: print program usage
706  *****************************************************************************
707  * Print a short inline help. Message interface is initialized at this stage.
708  *****************************************************************************/
709 static void Usage( int i_fashion )
710 {
711     /* Usage */
712     intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
713                  p_main->psz_arg0 );
714
715     if( i_fashion == USAGE )
716     {
717         intf_MsgImm( "Try `%s --help' for more information.",
718                      p_main->psz_arg0 );
719         return;
720     }
721
722     /* Options */
723     intf_MsgImm( "\nOptions:"
724           "\n  -I, --intf <module>            \tinterface method"
725           "\n  -v, --verbose                  \tverbose mode (cumulative)"
726           "\n"
727           "\n      --noaudio                  \tdisable audio"
728           "\n  -A, --aout <module>            \taudio output method"
729           "\n      --stereo, --mono           \tstereo/mono audio"
730           "\n      --spdif                    \tAC3 pass-through mode"
731           "\n      --downmix <module>         \tAC3 downmix method"
732           "\n      --imdct <module>           \tAC3 IMDCT method"
733           "\n"
734           "\n      --novideo                  \tdisable video"
735           "\n  -V, --vout <module>            \tvideo output method"
736           "\n      --display <display>        \tdisplay string"
737           "\n      --width <w>, --height <h>  \tdisplay dimensions"
738           "\n  -g, --grayscale                \tgrayscale output"
739           "\n      --fullscreen               \tfullscreen output"
740           "\n      --overlay                  \taccelerated display"
741           "\n      --color                    \tcolor output"
742           "\n      --motion <module>          \tmotion compensation method"
743           "\n      --idct <module>            \tIDCT method"
744           "\n      --yuv <module>             \tYUV method"
745           "\n      --synchro <type>           \tforce synchro algorithm"
746           "\n"
747           "\n  -t, --dvdtitle <num>           \tchoose DVD title"
748           "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
749           "\n  -u, --dvdangle <num>           \tchoose DVD angle"
750           "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
751           "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
752           "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
753           "\n"
754           "\n      --input                    \tinput method"
755           "\n      --channels                 \tenable channels"
756           "\n      --server <host>            \tvideo server address"
757           "\n      --port <port>              \tvideo server port"
758           "\n      --broadcast                \tlisten to a broadcast"
759           "\n"
760           "\n  -h, --help                     \tprint help and exit"
761           "\n  -H, --longhelp                 \tprint long help and exit"
762           "\n      --version                  \toutput version information and exit" );
763
764     if( i_fashion == SHORT_HELP )
765         return;
766
767     /* Interface parameters */
768     intf_MsgImm( "\nInterface parameters:"
769         "\n  " INTF_METHOD_VAR "=<method name>          \tinterface method"
770         "\n  " INTF_INIT_SCRIPT_VAR "=<filename>               \tinitialization script"
771         "\n  " INTF_CHANNELS_VAR "=<filename>            \tchannels list" );
772
773     /* Audio parameters */
774     intf_MsgImm( "\nAudio parameters:"
775         "\n  " AOUT_METHOD_VAR "=<method name>        \taudio method"
776         "\n  " AOUT_DSP_VAR "=<filename>              \tdsp device path"
777         "\n  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output"
778         "\n  " AOUT_SPDIF_VAR "={1|0}                 \tAC3 pass-through mode"
779         "\n  " DOWNMIX_METHOD_VAR "=<method name>     \tAC3 downmix method"
780         "\n  " IMDCT_METHOD_VAR "=<method name>       \tAC3 IMDCT method"
781         "\n  " AOUT_RATE_VAR "=<rate>             \toutput rate" );
782
783     /* Video parameters */
784     intf_MsgImm( "\nVideo parameters:"
785         "\n  " VOUT_METHOD_VAR "=<method name>        \tdisplay method"
786         "\n  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used"
787         "\n  " VOUT_WIDTH_VAR "=<width>               \tdisplay width"
788         "\n  " VOUT_HEIGHT_VAR "=<height>             \tdislay height"
789         "\n  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path"
790         "\n  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output"
791         "\n  " VOUT_FULLSCREEN_VAR "={1|0}            \tfullscreen"
792         "\n  " VOUT_OVERLAY_VAR "={1|0}               \toverlay"
793         "\n  " MOTION_METHOD_VAR "=<method name>      \tmotion compensation method"
794         "\n  " IDCT_METHOD_VAR "=<method name>        \tIDCT method"
795         "\n  " YUV_METHOD_VAR "=<method name>         \tYUV method"
796         "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm" );
797
798     /* DVD parameters */
799     intf_MsgImm( "\nDVD parameters:"
800         "\n  " INPUT_DVD_DEVICE_VAR "=<device>           \tDVD device"
801         "\n  " INPUT_TITLE_VAR "=<title>             \ttitle number"
802         "\n  " INPUT_CHAPTER_VAR "=<chapter>         \tchapter number"
803         "\n  " INPUT_ANGLE_VAR "=<angle>             \tangle number"
804         "\n  " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
805         "\n  " INPUT_CHANNEL_VAR "=[0-15]            \taudio channel"
806         "\n  " INPUT_SUBTITLE_VAR "=[0-31]           \tsubtitle channel" );
807
808     /* Input parameters */
809     intf_MsgImm( "\nInput parameters:"
810         "\n  " INPUT_SERVER_VAR "=<hostname>          \tvideo server"
811         "\n  " INPUT_PORT_VAR "=<port>            \tvideo server port"
812         "\n  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface"
813         "\n  " INPUT_BROADCAST_VAR "=<addr>            \tbroadcast mode"
814         "\n  " INPUT_CHANNEL_SERVER_VAR "=<hostname>     \tchannel server"
815         "\n  " INPUT_CHANNEL_PORT_VAR "=<port>         \tchannel server port" );
816
817 }
818
819 /*****************************************************************************
820  * Version: print complete program version
821  *****************************************************************************
822  * Print complete program version and build number.
823  *****************************************************************************/
824 static void Version( void )
825 {
826     intf_MsgImm( VERSION_MESSAGE
827         "This program comes with NO WARRANTY, to the extent permitted by law.\n"
828         "You may redistribute it under the terms of the GNU General Public License;\n"
829         "see the file named COPYING for details.\n"
830         "Written by the VideoLAN team at Ecole Centrale, Paris." );
831 }
832
833 /*****************************************************************************
834  * InitSignalHandler: system signal handler initialization
835  *****************************************************************************
836  * Set the signal handlers. SIGTERM is not intercepted, because we need at
837  * at least a method to kill the program when all other methods failed, and
838  * when we don't want to use SIGKILL.
839  *****************************************************************************/
840 static void InitSignalHandler( void )
841 {
842     /* Termination signals */
843 #ifndef WIN32
844     signal( SIGINT,  FatalSignalHandler );
845     signal( SIGHUP,  FatalSignalHandler );
846     signal( SIGQUIT, FatalSignalHandler );
847
848     /* Other signals */
849     signal( SIGALRM, SimpleSignalHandler );
850     signal( SIGPIPE, SimpleSignalHandler );
851 #endif
852 }
853
854
855 /*****************************************************************************
856  * SimpleSignalHandler: system signal handler
857  *****************************************************************************
858  * This function is called when a non fatal signal is received by the program.
859  *****************************************************************************/
860 static void SimpleSignalHandler( int i_signal )
861 {
862     /* Acknowledge the signal received */
863     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
864 }
865
866
867 /*****************************************************************************
868  * FatalSignalHandler: system signal handler
869  *****************************************************************************
870  * This function is called when a fatal signal is received by the program.
871  * It tries to end the program in a clean way.
872  *****************************************************************************/
873 static void FatalSignalHandler( int i_signal )
874 {
875     /* Once a signal has been trapped, the termination sequence will be
876      * armed and following signals will be ignored to avoid sending messages
877      * to an interface having been destroyed */
878 #ifndef WIN32
879     signal( SIGINT,  SIG_IGN );
880     signal( SIGHUP,  SIG_IGN );
881     signal( SIGQUIT, SIG_IGN );
882 #endif
883
884     /* Acknowledge the signal received */
885     intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
886
887     /* Try to terminate everything - this is done by requesting the end of the
888      * interface thread */
889     p_main->p_intf->b_die = 1;
890 }
891
892 /*****************************************************************************
893  * CPUCapabilities: list the processors MMX support and other capabilities
894  *****************************************************************************
895  * This function is called to list extensions the CPU may have.
896  *****************************************************************************/
897 static int CPUCapabilities( void )
898 {
899     int i_capabilities = CPU_CAPABILITY_NONE;
900
901 #if defined( SYS_BEOS )
902     i_capabilities |= CPU_CAPABILITY_486
903                       | CPU_CAPABILITY_586
904                       | CPU_CAPABILITY_MMX;
905
906 #elif defined( SYS_DARWIN1_3 )
907
908     struct host_basic_info hi;
909     kern_return_t          ret;
910     host_name_port_t       host;
911
912     int i_size;
913     char *psz_name, *psz_subname;
914
915     /* Should 'never' fail? */
916     host = mach_host_self();
917
918     i_size = sizeof( hi ) / sizeof( int );
919     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
920
921     if( ret != KERN_SUCCESS )
922     {
923         intf_ErrMsg( "error: couldn't get CPU information" );
924         return( i_capabilities );
925     }
926
927     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
928     /* FIXME: need better way to detect newer proccessors.
929      * could do strncmp(a,b,5), but that's real ugly */
930     if( strcmp(psz_name, "ppc7400") || strcmp(psz_name, "ppc7450") )
931     {
932         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
933     }
934
935 #elif defined( __i386__ )
936     unsigned int  i_eax, i_ebx, i_ecx, i_edx;
937     boolean_t     b_amd;
938
939 #   define cpuid( a )              \
940     asm volatile ( "cpuid"         \
941                  : "=a" ( i_eax ), \
942                    "=b" ( i_ebx ), \
943                    "=c" ( i_ecx ), \
944                    "=d" ( i_edx )  \
945                  : "a"  ( a )      \
946                  : "cc" );         \
947
948     /* test for a 486 CPU */
949     asm volatile ( "pushfl\n\t"
950                    "popl %%eax\n\t"
951                    "movl %%eax, %%ebx\n\t"
952                    "xorl $0x200000, %%eax\n\t"
953                    "pushl %%eax\n\t"
954                    "popfl\n\t"
955                    "pushfl\n\t"
956                    "popl %%eax"
957                  : "=a" ( i_eax ),
958                    "=b" ( i_ebx )
959                  :
960                  : "cc" );
961
962     if( i_eax == i_ebx )
963     {
964         return( i_capabilities );
965     }
966
967     i_capabilities |= CPU_CAPABILITY_486;
968
969     /* the CPU supports the CPUID instruction - get its level */
970     cpuid( 0x00000000 );
971
972     if( !i_eax )
973     {
974         return( i_capabilities );
975     }
976
977     /* FIXME: this isn't correct, since some 486s have cpuid */
978     i_capabilities |= CPU_CAPABILITY_586;
979
980     /* borrowed from mpeg2dec */
981     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
982                     && ( i_edx == 0x69746e65 );
983
984     /* test for the MMX flag */
985     cpuid( 0x00000001 );
986
987     if( ! (i_edx & 0x00800000) )
988     {
989         return( i_capabilities );
990     }
991
992     i_capabilities |= CPU_CAPABILITY_MMX;
993
994     if( i_edx & 0x02000000 )
995     {
996         i_capabilities |= CPU_CAPABILITY_MMXEXT;
997         i_capabilities |= CPU_CAPABILITY_SSE;
998     }
999     
1000     /* test for additional capabilities */
1001     cpuid( 0x80000000 );
1002
1003     if( i_eax < 0x80000001 )
1004     {
1005         return( i_capabilities );
1006     }
1007
1008     /* list these additional capabilities */
1009     cpuid( 0x80000001 );
1010
1011     if( i_edx & 0x80000000 )
1012     {
1013         i_capabilities |= CPU_CAPABILITY_3DNOW;
1014     }
1015
1016     if( b_amd && ( i_edx & 0x00400000 ) )
1017     {
1018         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1019     }
1020 #else
1021     /* default behaviour */
1022
1023 #endif
1024     return( i_capabilities );
1025 }
1026