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