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