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