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