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