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