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