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