]> git.sesse.net Git - vlc/blob - src/interface/main.c
* IPv6 network module, courtesy of Alexis Guillard <alexis.guillard@bt.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-2001 VideoLAN
7  * $Id: main.c,v 1.160 2002/03/04 23:56:38 massiot Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *          Gildas Bazin <gbazin@netcourrier.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <setjmp.h>                                       /* longjmp, setjmp */
34
35 #include <videolan/vlc.h>
36
37 #ifdef HAVE_GETOPT_LONG
38 #   ifdef HAVE_GETOPT_H
39 #       include <getopt.h>                                       /* getopt() */
40 #   endif
41 #else
42 #   include "GNUgetopt/getopt.h"
43 #endif
44
45 #ifdef SYS_DARWIN
46 #   include <mach/mach.h>                               /* Altivec detection */
47 #   include <mach/mach_error.h>       /* some day the header files||compiler *
48                                                        will define it for us */
49 #   include <mach/bootstrap.h>
50 #endif
51
52 #ifndef WIN32
53 #   include <netinet/in.h>                            /* BSD: struct in_addr */
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( _MSC_VER ) && defined( _WIN32 )
59 #   include <io.h>
60 #endif
61
62 #ifdef HAVE_LOCALE_H
63 #    include <locale.h>
64 #endif
65
66 #include <errno.h>                                                 /* ENOMEM */
67 #include <stdlib.h>                                  /* getenv(), strtol(),  */
68 #include <string.h>                                            /* strerror() */
69 #include <fcntl.h>                                       /* open(), O_WRONLY */
70 #include <sys/stat.h>                                             /* S_IREAD */
71
72 #include "netutils.h"                                 /* network_ChannelJoin */
73
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76
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 #include "debug.h"
86
87 /*****************************************************************************
88  * Configuration options for the main program. Each plugin will also separatly
89  * define its own configuration options.
90  * Look into configuration.h if you need to know more about the following
91  * macros.
92  * 
93  *****************************************************************************/
94 #define BUILTIN
95 #define MODULE_NAME main
96 #include "modules_inner.h"                        /* for configuration stuff */
97
98 /* Quick usage guide
99 MODULE_CONFIG_START
100 MODULE_CONFIG_STOP
101 ADD_CATEGORY_HINT( text, longtext )
102 ADD_SUBCATEGORY_HINT( text, longtext )
103 ADD_STRING( option_name, value, p_callback, text, longtext )
104 ADD_FILE( option_name, psz_value, p_callback, text, longtext )
105 ADD_PLUGIN( option_name, psz_value, i_capability, p_callback, text, longtext )
106 ADD_INTEGER( option_name, i_value, p_callback, text, longtext )
107 ADD_BOOL( option_name, p_callback, text, longtext )
108 */
109
110 MODULE_CONFIG_START
111
112 /* Help options */
113 ADD_CATEGORY_HINT( "Help Options", NULL )
114 ADD_BOOL    ( "help", NULL,"print help and exit (or use -h)", NULL )
115 ADD_BOOL    ( "longhelp", NULL, "print long help version and exit (or use -H)",
116               NULL )
117 ADD_BOOL    ( "list", NULL, "list available plugins (or use -l)", NULL )
118 ADD_STRING  ( "pluginhelp", NULL, NULL,"print help on a plugin and exit",NULL )
119 ADD_BOOL    ( "version", NULL, "output version information and exit", NULL )
120
121 /* Interface options */
122 ADD_CATEGORY_HINT( "Interface Options", NULL)
123 ADD_PLUGIN  ( INTF_METHOD_VAR, MODULE_CAPABILITY_INTF, NULL, NULL,
124               "interface method", NULL )
125 ADD_INTEGER ( INTF_WARNING_VAR, 0, NULL, "warning level (or use -v)", NULL )
126 ADD_BOOL    ( INTF_STATS_VAR, NULL, "output statistics", NULL )
127 ADD_STRING  ( INTF_PATH_VAR, NULL, NULL, "interface default search path", NULL)
128
129 /* Audio Options */
130 ADD_CATEGORY_HINT( "Audio Options", NULL)
131 ADD_BOOL    ( AOUT_NOAUDIO_VAR, NULL, "disable audio", NULL )
132 ADD_PLUGIN  ( AOUT_METHOD_VAR, MODULE_CAPABILITY_AOUT, NULL, NULL,
133               "audio output method", NULL )
134 ADD_BOOL    ( AOUT_MONO_VAR, NULL, "mono audio", NULL )
135 ADD_INTEGER ( AOUT_VOLUME_VAR, VOLUME_DEFAULT, NULL, "VLC output volume", NULL)
136 ADD_INTEGER ( AOUT_RATE_VAR, 44100, NULL, "VLC output frequency", NULL )
137 ADD_INTEGER ( AOUT_DESYNC_VAR, 0, NULL, "Compensate desynchronization of the "
138                                         "audio (in ms)", NULL )
139
140 /* Video options */
141 ADD_CATEGORY_HINT( "Video Options", NULL )
142 ADD_BOOL    ( VOUT_NOVIDEO_VAR, NULL, "disable video", NULL )
143 ADD_PLUGIN  ( VOUT_METHOD_VAR, MODULE_CAPABILITY_VOUT, NULL, NULL,
144               "video output method", NULL )
145 ADD_STRING  ( VOUT_DISPLAY_VAR, NULL, NULL, "display string", NULL )
146 ADD_INTEGER ( VOUT_WIDTH_VAR, 720, NULL, "display width", NULL )
147 ADD_INTEGER ( VOUT_HEIGHT_VAR, 576, NULL, "display height", NULL )
148 ADD_BOOL    ( VOUT_GRAYSCALE_VAR, NULL, "grayscale output", NULL )
149 ADD_BOOL    ( VOUT_FULLSCREEN_VAR, NULL, "fullscreen output", NULL )
150 ADD_BOOL    ( VOUT_NOOVERLAY_VAR, NULL, "disable accelerated display", NULL )
151 ADD_PLUGIN  ( VOUT_FILTER_VAR, MODULE_CAPABILITY_VOUT, NULL, NULL,
152               "video filter module", NULL )
153 ADD_INTEGER ( VOUT_SPUMARGIN_VAR, -1, NULL, "force SPU position", NULL )
154
155 /* Input options */
156 ADD_CATEGORY_HINT( "Input Options", NULL )
157 ADD_STRING  ( INPUT_METHOD_VAR, NULL, NULL, "input method", NULL )
158 ADD_INTEGER ( INPUT_PORT_VAR, 1234, NULL, "server port", NULL )
159 ADD_BOOL    ( INPUT_NETWORK_CHANNEL_VAR, NULL, "enable network channel mode",
160               NULL )
161 ADD_STRING  ( INPUT_CHANNEL_SERVER_VAR, "localhost", NULL,
162               "channel server address", NULL )
163 ADD_INTEGER ( INPUT_CHANNEL_PORT_VAR, 6010, NULL, "channel server port", NULL )
164 ADD_STRING  ( INPUT_IFACE_VAR, "eth0", NULL, "network interface", NULL )
165
166 ADD_INTEGER ( INPUT_AUDIO_VAR, -1, NULL, "choose audio", NULL )
167 ADD_INTEGER ( INPUT_CHANNEL_VAR, -1, NULL, "choose channel", NULL )
168 ADD_INTEGER ( INPUT_SUBTITLE_VAR, -1, NULL, "choose subtitles", NULL )
169
170 ADD_STRING  ( INPUT_DVD_DEVICE_VAR, "/dev/dvd", NULL, "DVD device", NULL )
171 ADD_STRING  ( INPUT_VCD_DEVICE_VAR, "/dev/cdrom", NULL, "VCD device", NULL )
172 ADD_BOOL    ( INPUT_IPV6_VAR, NULL, "force IPv6", NULL )
173 ADD_BOOL    ( INPUT_IPV4_VAR, NULL, "force IPv4", NULL )
174
175 /* Decoder options */
176 ADD_CATEGORY_HINT( "Decoders Options", NULL )
177 ADD_PLUGIN  ( ADEC_MPEG_VAR, MODULE_CAPABILITY_DECODER, NULL, NULL,
178               "choose MPEG audio decoder", NULL )
179 ADD_PLUGIN  ( ADEC_AC3_VAR, MODULE_CAPABILITY_DECODER, NULL, NULL,
180               "choose AC3 audio decoder", NULL )
181 ADD_INTEGER ( VDEC_SMP_VAR, 0, NULL, "use additional processors", NULL )
182 ADD_STRING  ( VPAR_SYNCHRO_VAR, NULL, NULL, "force synchro algorithm "
183                                             "{I|I+|IP|IP+|IPB}", NULL )
184
185 /* CPU options */
186 ADD_CATEGORY_HINT( "CPU Options Options", NULL )
187 ADD_BOOL    ( NOMMX_VAR, NULL, "disable CPU's MMX support", NULL )
188 ADD_BOOL    ( NO3DN_VAR, NULL, "disable CPU's 3D Now! support", NULL )
189 ADD_BOOL    ( NOMMXEXT_VAR, NULL, "disable CPU's MMX EXT support", NULL )
190 ADD_BOOL    ( NOSSE_VAR, NULL, "disable CPU's SSE support", NULL )
191 ADD_BOOL    ( NOALTIVEC_VAR, NULL, "disable CPU's AltiVec support", NULL )
192
193 /* Playlist options */
194 ADD_BOOL    ( PLAYLIST_STARTUP_VAR, NULL, "launch playlist on startup", NULL )
195 ADD_BOOL    ( PLAYLIST_ENQUEUE_VAR, NULL, "enqueue playlist as default", NULL )
196 ADD_BOOL    ( PLAYLIST_LOOP_VAR, NULL, "loop on playlist end", NULL )
197
198 /* Misc options */
199 ADD_CATEGORY_HINT( "Miscellaneous Options", NULL )
200 ADD_PLUGIN  ( MEMCPY_METHOD_VAR, MODULE_CAPABILITY_MEMCPY, NULL, NULL,
201               "memory copy method", NULL )
202
203 MODULE_CONFIG_STOP
204
205 MODULE_INIT_START
206     SET_DESCRIPTION( "Main program" )
207     ADD_CAPABILITY( MAIN, 100/*whatever*/ )
208 MODULE_INIT_STOP
209
210 MODULE_ACTIVATE_START
211 MODULE_ACTIVATE_STOP
212
213 MODULE_DEACTIVATE_START
214 MODULE_DEACTIVATE_STOP
215 /*****************************************************************************
216  * End configuration.
217  *****************************************************************************/
218
219 /*****************************************************************************
220  * Global variables - these are the only ones, see main.h and modules.h
221  *****************************************************************************/
222 main_t        *p_main;
223 module_bank_t *p_module_bank;
224 input_bank_t  *p_input_bank;
225 aout_bank_t   *p_aout_bank;
226 vout_bank_t   *p_vout_bank;
227
228 /*****************************************************************************
229  * Local prototypes
230  *****************************************************************************/
231 static int  GetConfigurationFromFile    ( void ){return 0;};
232 static int  GetConfigurationFromCmdLine ( int *pi_argc, char *ppsz_argv[],
233                                           boolean_t b_ignore_errors );
234 static int  GetFilenames                ( int i_argc, char *ppsz_argv[] );
235 static void Usage                       ( const char *psz_module_name );
236 static void ListModules                 ( void );
237 static void Version                     ( void );
238
239 static void InitSignalHandler           ( void );
240 static void SimpleSignalHandler         ( int i_signal );
241 static void FatalSignalHandler          ( int i_signal );
242 static void IllegalSignalHandler        ( int i_signal );
243 static u32  CPUCapabilities             ( void );
244
245 #ifdef WIN32
246 static void ShowConsole                 ( void );
247 #endif
248
249 static jmp_buf env;
250 static int     i_illegal;
251 static char   *psz_capability;
252
253 /*****************************************************************************
254  * main: parse command line, start interface and spawn threads
255  *****************************************************************************
256  * Steps during program execution are:
257  *      -configuration parsing and messages interface initialization
258  *      -opening of audio output device and some global modules
259  *      -execution of interface, which exit on error or on user request
260  *      -closing of audio output device and some global modules
261  * On error, the spawned threads are canceled, and the open devices closed.
262  *****************************************************************************/
263 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
264 {
265     main_t        main_data;                /* root of all data - see main.h */
266     module_bank_t module_bank;
267     input_bank_t  input_bank;
268     aout_bank_t   aout_bank;
269     vout_bank_t   vout_bank;
270     char *p_tmp;
271
272     p_main        = &main_data;               /* set up the global variables */
273     p_module_bank = &module_bank;
274     p_input_bank  = &input_bank;
275     p_aout_bank   = &aout_bank;
276     p_vout_bank   = &vout_bank;
277
278     p_main->i_warning_level = 0;
279
280 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
281     /*
282      * Support for getext
283      */
284 #if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
285     if( !setlocale( LC_MESSAGES, "" ) )
286     {
287         fprintf( stderr, "warning: unsupported locale.\n" );
288     }
289 #endif
290
291     if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
292     {
293         fprintf( stderr, "warning: no domain %s in directory %s\n",
294                  PACKAGE, LOCALEDIR );
295     }
296
297     textdomain( PACKAGE );
298 #endif
299
300     /*
301      * Initialize threads system
302      */
303     vlc_threads_init( );
304
305     /*
306      * Test if our code is likely to run on this CPU
307      */
308     p_main->i_cpu_capabilities = CPUCapabilities();
309
310     /*
311      * System specific initialization code
312      */
313 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
314     system_Init( &i_argc, ppsz_argv, ppsz_env );
315
316 #elif defined( SYS_LINUX )
317 #   ifdef DEBUG
318     /* Activate malloc checking routines to detect heap corruptions. */
319     putenv( "MALLOC_CHECK_=2" );
320 #   endif
321 #endif
322
323     /*
324      * Initialize messages interface
325      */
326     intf_MsgCreate();
327
328     intf_Msg( COPYRIGHT_MESSAGE "\n" );
329
330
331     /* Get the executable name (similar to the basename command) */
332     p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
333     while( *p_tmp )
334     {
335         if( *p_tmp == '/' ) p_main->psz_arg0 = ++p_tmp;
336         else ++p_tmp;
337     }
338
339     /*
340      * Initialize the module bank and and load the configuration of the main
341      * module. We need to do this at this stage to be able to display a short
342      * help if required by the user. (short help == main module options)
343      */
344     module_InitBank();
345     module_LoadMain();
346
347     if( GetConfigurationFromCmdLine( &i_argc, ppsz_argv, 1 ) )
348     {
349         intf_MsgDestroy();
350         return( errno );
351     }
352
353     /* Check for short help option */
354     if( config_GetIntVariable( "help" ) )
355     {
356         Usage( "main" );
357         return( -1 );
358     }
359
360     /* Check for version option */
361     if( config_GetIntVariable( "version" ) )
362     {
363         Version();
364         return( -1 );
365     }
366
367     /*
368      * Load the builtins and plugins into the module_bank.
369      * We have to do it before GetConfiguration() because this also gets the
370      * list of configuration options exported by each plugin and loads their
371      * default values.
372      */
373     module_LoadBuiltins();
374     module_LoadPlugins();
375     intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
376                   p_module_bank->i_count );
377
378     /* Check for help on plugins */
379     if( (p_tmp = config_GetPszVariable( "pluginhelp" )) )
380     {
381         Usage( p_tmp );
382         free( p_tmp );
383         return( -1 );
384     }
385
386     /* Check for long help option */
387     if( config_GetIntVariable( "longhelp" ) )
388     {
389         Usage( NULL );
390         return( -1 );
391     }
392
393     /* Check for plugin list option */
394     if( config_GetIntVariable( "list" ) )
395     {
396         ListModules();
397         return( -1 );
398     }
399
400     /*
401      * Override default configuration with config file settings
402      */
403     if( GetConfigurationFromFile() )
404     {
405         intf_MsgDestroy();
406         return( errno );
407     }
408
409     /*
410      * Override configuration with command line settings
411      */
412     if( GetConfigurationFromCmdLine( &i_argc, ppsz_argv, 0 ) )
413     {
414         intf_MsgDestroy();
415         return( errno );
416     }
417
418     /* p_main inititalization. FIXME ? */
419     p_main->i_desync = (mtime_t)config_GetIntVariable( AOUT_DESYNC_VAR )
420       * (mtime_t)1000;
421     p_main->b_stats = config_GetIntVariable( INTF_STATS_VAR );
422     p_main->b_audio = !config_GetIntVariable( AOUT_NOAUDIO_VAR );
423     p_main->b_stereo= !config_GetIntVariable( AOUT_MONO_VAR );
424     p_main->b_video = !config_GetIntVariable( VOUT_NOVIDEO_VAR );
425     if( config_GetIntVariable( NOMMX_VAR ) )
426         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
427     if( config_GetIntVariable( NO3DN_VAR ) )
428         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
429     if( config_GetIntVariable( NOMMXEXT_VAR ) )
430         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
431     if( config_GetIntVariable( NOSSE_VAR ) )
432         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
433     if( config_GetIntVariable( NOALTIVEC_VAR ) )
434         p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
435
436
437     if( p_main->b_stats )
438     {
439         char          p_capabilities[200];
440         p_capabilities[0] = '\0';
441
442 #define PRINT_CAPABILITY( capability, string )                              \
443         if( p_main->i_cpu_capabilities & capability )                       \
444         {                                                                   \
445             strncat( p_capabilities, string " ",                            \
446                      sizeof(p_capabilities) - strlen(p_capabilities) );     \
447             p_capabilities[sizeof(p_capabilities) - 1] = '\0';              \
448         }
449
450         PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
451         PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
452         PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
453         PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
454         PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
455         PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
456         PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
457         PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
458         PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
459         intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
460     }
461
462     /*
463      * Initialize playlist and get commandline files
464      */
465     p_main->p_playlist = intf_PlaylistCreate();
466     if( !p_main->p_playlist )
467     {
468         intf_ErrMsg( "playlist error: playlist initialization failed" );
469         intf_MsgDestroy();
470         return( errno );
471     }
472     intf_PlaylistInit( p_main->p_playlist );
473
474     /*
475      * Get input filenames given as commandline arguments
476      */
477     GetFilenames( i_argc, ppsz_argv );
478
479     /*
480      * Initialize input, aout and vout banks
481      */
482     input_InitBank();
483     aout_InitBank();
484     vout_InitBank();
485
486     /*
487      * Choose the best memcpy module
488      */
489     p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL,
490                                            NULL );
491     if( p_main->p_memcpy_module == NULL )
492     {
493         intf_ErrMsg( "intf error: no suitable memcpy module, "
494                      "using libc default" );
495         p_main->pf_memcpy = memcpy;
496     }
497     else
498     {
499         p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
500                                   ->memcpy.functions.memcpy.pf_memcpy;
501     }
502
503     /*
504      * Initialize shared resources and libraries
505      */
506     if( config_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR ) &&
507         network_ChannelCreate() )
508     {
509         /* On error during Channels initialization, switch off channels */
510         intf_ErrMsg( "intf error: channels initialization failed, " 
511                                  "deactivating channels" );
512         config_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
513     }
514
515     /*
516      * Try to run the interface
517      */
518     p_main->p_intf = intf_Create();
519     if( p_main->p_intf == NULL )
520     {
521         intf_ErrMsg( "intf error: interface initialization failed" );
522     }
523     else
524     {
525         /*
526          * Set signal handling policy for all threads
527          */
528         InitSignalHandler();
529
530         /*
531          * This is the main loop
532          */
533         p_main->p_intf->pf_run( p_main->p_intf );
534
535         /*
536          * Finished, destroy the interface
537          */
538         intf_Destroy( p_main->p_intf );
539
540         /*
541          * Go back into channel 0 which is the network
542          */
543         if( config_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR ) )
544         {
545             network_ChannelJoin( COMMON_CHANNEL );
546         }
547     }
548
549     /*
550      * Free input, aout and vout banks
551      */
552     input_EndBank();
553     vout_EndBank();
554     aout_EndBank();
555
556     /*
557      * Free playlist
558      */
559     intf_PlaylistDestroy( p_main->p_playlist );
560
561     /*
562      * Free memcpy module if it was allocated
563      */
564     if( p_main->p_memcpy_module != NULL )
565     {
566         module_Unneed( p_main->p_memcpy_module );
567     }
568
569     /*
570      * Free module bank
571      */
572     module_EndBank();
573
574     /*
575      * System specific cleaning code
576      */
577 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
578     system_End();
579 #endif
580
581
582     /*
583      * Terminate messages interface and program
584      */
585     intf_WarnMsg( 1, "intf: program terminated" );
586     intf_MsgDestroy();
587
588     /*
589      * Stop threads system
590      */
591     vlc_threads_end( );
592
593     return 0;
594 }
595
596
597 /* following functions are local */
598
599 /*****************************************************************************
600  * GetConfigurationFromCmdLine: parse command line
601  *****************************************************************************
602  * Parse command line for configuration. If the inline help is requested, the
603  * function Usage() is called and the function returns -1 (causing main() to
604  * exit).
605  * Now that the module_bank has been initialized, we can dynamically
606  * generate the longopts structure used by getops. We have to do it this way
607  * because we don't know (and don't want to know) in advance the configuration
608  * options used (ie. exported) by each module.
609  *****************************************************************************/
610 static int GetConfigurationFromCmdLine( int *pi_argc, char *ppsz_argv[],
611                                         boolean_t b_ignore_errors )
612 {
613     int i_cmd, i, i_index, i_longopts_size;
614     module_t *p_module;
615     struct option *p_longopts;
616
617     /* Short options */
618     const char *psz_shortopts = "hHvl";
619
620
621     /* Set default configuration and copy arguments */
622     p_main->i_argc    = *pi_argc;
623     p_main->ppsz_argv = ppsz_argv;
624
625     p_main->p_channel = NULL;
626
627 #ifdef SYS_DARWIN
628     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
629      * is the PSN - process serial number (a unique PID-ish thingie)
630      * still ok for real Darwin & when run from command line */
631     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
632                                         /* for example -psn_0_9306113 */
633     {
634         /* GDMF!... I can't do this or else the MacOSX window server will
635          * not pick up the PSN and not register the app and we crash...
636          * hence the following kludge otherwise we'll get confused w/ argv[1]
637          * being an input file name */
638 #if 0
639         ppsz_argv[ 1 ] = NULL;
640 #endif
641         *pi_argc = *pi_argc - 1;
642         pi_argc--;
643         return( 0 );
644     }
645 #endif
646
647
648     /*
649      * Generate the longopts structure used by getopt_long
650      */
651     i_longopts_size = 0;
652     for( p_module = p_module_bank->first ;
653          p_module != NULL ;
654          p_module = p_module->next )
655     {
656         /* count the number of exported configuration options (to allocate
657          * longopts). The i_config_options we use is an approximation of the
658          * real number of options (it also includes markers like: category ...)
659          * but it is enough for our purpose */
660         i_longopts_size += p_module->i_config_options -1;
661     }
662
663     p_longopts = (struct option *)malloc( sizeof(struct option)
664                                           * (i_longopts_size + 1) );
665     if( p_longopts == NULL )
666     {
667         intf_ErrMsg( "GetConfigurationFromCmdLine error: "
668                      "can't allocate p_longopts" );
669         return( -1 );
670     }
671
672     /* Fill the longopts structure */
673     i_index = 0;
674     for( p_module = p_module_bank->first ;
675          p_module != NULL ;
676          p_module = p_module->next )
677     {
678         for( i = 1; i < (p_module->i_config_options -1); i++ )
679         {
680             if( (p_module->p_config[i].i_type == MODULE_CONFIG_ITEM_CATEGORY)||
681                 (p_module->p_config[i].i_type ==
682                      MODULE_CONFIG_ITEM_SUBCATEGORY)||
683                 (p_module->p_config[i].i_type ==
684                      MODULE_CONFIG_ITEM_SUBCATEGORY_END) )
685                  continue;
686             p_longopts[i_index].name = p_module->p_config[i].psz_name;
687             p_longopts[i_index].has_arg =
688                 (p_module->p_config[i].i_type == MODULE_CONFIG_ITEM_BOOL)?
689                                                no_argument : required_argument;
690             p_longopts[i_index].flag = 0;
691             p_longopts[i_index].val = 0;
692             i_index++;
693         }
694     }
695     /* Close the longopts structure */
696     memset( &p_longopts[i_index], 0, sizeof(struct option) );
697
698
699     /*
700      * Parse the command line options
701      */
702     opterr = 0;
703     optind = 1;
704     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
705                                   p_longopts, &i_index ) ) != EOF )
706     {
707
708         if( i_cmd == 0 )
709         {
710             /* A long option has been recognized */
711
712             module_config_t *p_conf;
713
714             /* Store the configuration option */
715             p_conf = config_FindConfig( p_longopts[i_index].name );
716
717             switch( p_conf->i_type )
718             {
719             case MODULE_CONFIG_ITEM_STRING:
720             case MODULE_CONFIG_ITEM_FILE:
721             case MODULE_CONFIG_ITEM_PLUGIN:
722                 config_PutPszVariable( p_longopts[i_index].name, optarg );
723                 break;
724             case MODULE_CONFIG_ITEM_INTEGER:
725                 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
726                 break;
727             case MODULE_CONFIG_ITEM_BOOL:
728                 config_PutIntVariable( p_longopts[i_index].name, 1 );
729                 break;
730             }
731
732             continue;
733         }
734
735         /* short options handled here for now */
736         switch( i_cmd )
737         {
738
739         /* General/common options */
740         case 'h':                                              /* -h, --help */
741             config_PutIntVariable( "help", 1 );
742             break;
743         case 'H':                                          /* -H, --longhelp */
744             config_PutIntVariable( "longhelp", 1 );
745             break;
746         case 'l':                                              /* -l, --list */
747             config_PutIntVariable( "list", 1 );
748             break;
749         case 'v':                                           /* -v, --verbose */
750             p_main->i_warning_level++;
751             break;
752
753         /* Internal error: unknown option */
754         case '?':
755         default:
756             if( !b_ignore_errors )
757             {
758                 intf_ErrMsg( "intf error: unknown option `%s'",
759                              ppsz_argv[optind] );
760                 intf_Msg( "Try `%s --help' for more information.\n",
761                           p_main->psz_arg0 );
762
763 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
764                 intf_Msg( "\nPress the RETURN key to continue..." );
765                 getchar();
766 #endif
767                 free( p_longopts );
768                 return( EINVAL );
769                 break;
770             }
771         }
772
773     }
774
775     if( p_main->i_warning_level < 0 )
776     {
777         p_main->i_warning_level = 0;
778     }
779
780     free( p_longopts );
781     return( 0 );
782 }
783
784 /*****************************************************************************
785  * GetFilenames: parse command line options which are not flags
786  *****************************************************************************
787  * Parse command line for input files.
788  *****************************************************************************/
789 static int GetFilenames( int i_argc, char *ppsz_argv[] )
790 {
791     int i_opt;
792
793     /* We assume that the remaining parameters are filenames */
794     for( i_opt = optind; i_opt < i_argc; i_opt++ )
795     {
796         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
797                           ppsz_argv[ i_opt ] );
798     }
799
800     return( 0 );
801 }
802
803 /*****************************************************************************
804  * Usage: print program usage
805  *****************************************************************************
806  * Print a short inline help. Message interface is initialized at this stage.
807  *****************************************************************************/
808 static void Usage( const char *psz_module_name )
809 {
810     int i;
811     module_t *p_module;
812     char psz_spaces[30];
813
814     memset( psz_spaces, 32, 30 );
815
816 #ifdef WIN32
817     ShowConsole();
818 #endif
819
820     /* Usage */
821     intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
822               p_main->psz_arg0 );
823
824     /* Enumerate the config of each module */
825     for( p_module = p_module_bank->first ;
826          p_module != NULL ;
827          p_module = p_module->next )
828     {
829
830         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
831             continue;
832
833         /* print module name */
834         intf_Msg( "%s configuration:\n", p_module->psz_name );
835
836         for( i = 0; i < (p_module->i_config_options -1); i++ )
837         {
838             int j;
839
840             switch( p_module->p_config[i].i_type )
841             {
842             case MODULE_CONFIG_ITEM_CATEGORY:
843                 intf_Msg( " %s", p_module->p_config[i].psz_text );
844                 break;
845
846             case MODULE_CONFIG_ITEM_STRING:
847             case MODULE_CONFIG_ITEM_FILE:
848             case MODULE_CONFIG_ITEM_PLUGIN:
849                 /* Nasty hack, but right now I'm too tired to think about
850                  * a nice solution */
851                 j = 25 - strlen( p_module->p_config[i].psz_name )
852                     - strlen(" <string>") - 1;
853                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
854
855                 intf_Msg( "  --%s <string>%s %s",
856                           p_module->p_config[i].psz_name, psz_spaces,
857                           p_module->p_config[i].psz_text );
858                 psz_spaces[j] = 32;
859                 break;
860             case MODULE_CONFIG_ITEM_INTEGER:
861                 /* Nasty hack, but right now I'm too tired to think about
862                  * a nice solution */
863                 j = 25 - strlen( p_module->p_config[i].psz_name )
864                     - strlen(" <integer>") - 1;
865                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
866
867                 intf_Msg( "  --%s <integer>%s %s",
868                           p_module->p_config[i].psz_name, psz_spaces,
869                           p_module->p_config[i].psz_text );
870                 psz_spaces[j] = 32;
871                 break;
872             default:
873                 /* Nasty hack, but right now I'm too tired to think about
874                  * a nice solution */
875                 j = 25 - strlen( p_module->p_config[i].psz_name ) - 1;
876                 if( j < 0 ) j = 0; psz_spaces[j] = 0;
877
878                 intf_Msg( "  --%s%s %s",
879                           p_module->p_config[i].psz_name, psz_spaces,
880                           p_module->p_config[i].psz_text );
881                 psz_spaces[j] = 32;
882                 break;
883             }
884         }
885
886         /* Yet another nasty hack.
887          * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
888          * for each module?? */
889         if( !strcmp( "main", p_module->psz_name ) )
890             intf_Msg( "\nPlaylist items:"
891                 "\n  *.mpg, *.vob                   \tPlain MPEG-1/2 files"
892                 "\n  [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
893                 "\n                                 \tDVD device"
894                 "\n  vcd:<device>                   \tVCD device"
895                 "\n  udpstream:[<server>[:<server port>]][@[<bind address>]"
896                       "[:<bind port>]]"
897                 "\n                                 \tUDP stream sent by VLS"
898                 "\n  vlc:loop                       \tLoop execution of the "
899                       "playlist"
900                 "\n  vlc:pause                      \tPause execution of "
901                       "playlist items"
902                 "\n  vlc:quit                       \tQuit VLC" );
903
904         intf_Msg( "" );
905
906     }
907
908 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
909         intf_Msg( "\nPress the RETURN key to continue..." );
910         getchar();
911 #endif
912 }
913
914 /*****************************************************************************
915  * ListModules: list the available modules with their description
916  *****************************************************************************
917  * Print a list of all available modules (builtins and plugins) and a short
918  * description for each one.
919  *****************************************************************************/
920 static void ListModules( void )
921 {
922     module_t *p_module;
923     char psz_spaces[20];
924
925     memset( psz_spaces, 32, 20 );
926
927 #ifdef WIN32
928     ShowConsole();
929 #endif
930
931     /* Usage */
932     intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
933               p_main->psz_arg0 );
934
935     intf_Msg( "[plugin]              [description]" );
936
937     /* Enumerate each module */
938     for( p_module = p_module_bank->first ;
939          p_module != NULL ;
940          p_module = p_module->next )
941     {
942         int i;
943
944         /* Nasty hack, but right now I'm too tired to think about a nice
945          * solution */
946         i = 20 - strlen( p_module->psz_name ) - 1;
947         if( i < 0 ) i = 0;
948         psz_spaces[i] = 0;
949
950         intf_Msg( "  %s%s %s", p_module->psz_name, psz_spaces,
951                   p_module->psz_longname );
952
953         psz_spaces[i] = 32;
954
955     }
956
957 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
958         intf_Msg( "\nPress the RETURN key to continue..." );
959         getchar();
960 #endif
961 }
962
963 /*****************************************************************************
964  * Version: print complete program version
965  *****************************************************************************
966  * Print complete program version and build number.
967  *****************************************************************************/
968 static void Version( void )
969 {
970 #ifdef WIN32
971     ShowConsole();
972 #endif
973     intf_Msg( VERSION_MESSAGE
974         "This program comes with NO WARRANTY, to the extent permitted by law.\n"
975         "You may redistribute it under the terms of the GNU General Public License;\n"
976         "see the file named COPYING for details.\n"
977         "Written by the VideoLAN team at Ecole Centrale, Paris." );
978 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
979         intf_Msg( "\nPress the RETURN key to continue..." );
980         getchar();
981 #endif
982 }
983
984 /*****************************************************************************
985  * InitSignalHandler: system signal handler initialization
986  *****************************************************************************
987  * Set the signal handlers. SIGTERM is not intercepted, because we need at
988  * at least a method to kill the program when all other methods failed, and
989  * when we don't want to use SIGKILL.
990  *****************************************************************************/
991 static void InitSignalHandler( void )
992 {
993     /* Termination signals */
994 #ifndef WIN32
995     signal( SIGINT,  FatalSignalHandler );
996     signal( SIGHUP,  FatalSignalHandler );
997     signal( SIGQUIT, FatalSignalHandler );
998
999     /* Other signals */
1000     signal( SIGALRM, SimpleSignalHandler );
1001     signal( SIGPIPE, SimpleSignalHandler );
1002 #endif
1003 }
1004
1005 /*****************************************************************************
1006  * SimpleSignalHandler: system signal handler
1007  *****************************************************************************
1008  * This function is called when a non fatal signal is received by the program.
1009  *****************************************************************************/
1010 static void SimpleSignalHandler( int i_signal )
1011 {
1012     /* Acknowledge the signal received */
1013     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1014 }
1015
1016 /*****************************************************************************
1017  * FatalSignalHandler: system signal handler
1018  *****************************************************************************
1019  * This function is called when a fatal signal is received by the program.
1020  * It tries to end the program in a clean way.
1021  *****************************************************************************/
1022 static void FatalSignalHandler( int i_signal )
1023 {
1024     /* Once a signal has been trapped, the termination sequence will be
1025      * armed and following signals will be ignored to avoid sending messages
1026      * to an interface having been destroyed */
1027 #ifndef WIN32
1028     signal( SIGINT,  SIG_IGN );
1029     signal( SIGHUP,  SIG_IGN );
1030     signal( SIGQUIT, SIG_IGN );
1031 #endif
1032
1033     /* Acknowledge the signal received */
1034     intf_ErrMsg( "intf error: signal %d received, exiting", i_signal );
1035
1036     /* Try to terminate everything - this is done by requesting the end of the
1037      * interface thread */
1038     p_main->p_intf->b_die = 1;
1039 }
1040
1041 /*****************************************************************************
1042  * IllegalSignalHandler: system signal handler
1043  *****************************************************************************
1044  * This function is called when an illegal instruction signal is received by
1045  * the program. We use this function to test OS and CPU capabilities
1046  *****************************************************************************/
1047 static void IllegalSignalHandler( int i_signal )
1048 {
1049     /* Acknowledge the signal received */
1050     i_illegal = 1;
1051
1052 #ifdef HAVE_SIGRELSE
1053     sigrelse( i_signal );
1054 #endif
1055
1056     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1057                      "operating system.\n", psz_capability );
1058     fprintf( stderr, "         some optimizations will be disabled unless "
1059                      "you upgrade your OS\n" );
1060 #ifdef SYS_LINUX
1061     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)" );
1062 #endif
1063
1064     longjmp( env, 1 );
1065 }
1066
1067 /*****************************************************************************
1068  * CPUCapabilities: list the processors MMX support and other capabilities
1069  *****************************************************************************
1070  * This function is called to list extensions the CPU may have.
1071  *****************************************************************************/
1072 static u32 CPUCapabilities( void )
1073 {
1074     volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1075
1076 #if defined( SYS_DARWIN )
1077     struct host_basic_info hi;
1078     kern_return_t          ret;
1079     host_name_port_t       host;
1080
1081     int i_size;
1082     char *psz_name, *psz_subname;
1083
1084     i_capabilities |= CPU_CAPABILITY_FPU;
1085
1086     /* Should 'never' fail? */
1087     host = mach_host_self();
1088
1089     i_size = sizeof( hi ) / sizeof( int );
1090     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1091
1092     if( ret != KERN_SUCCESS )
1093     {
1094         fprintf( stderr, "error: couldn't get CPU information\n" );
1095         return( i_capabilities );
1096     }
1097
1098     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1099     /* FIXME: need better way to detect newer proccessors.
1100      * could do strncmp(a,b,5), but that's real ugly */
1101     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1102     {
1103         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1104     }
1105
1106     return( i_capabilities );
1107
1108 #elif defined( __i386__ )
1109     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
1110     volatile boolean_t     b_amd;
1111
1112     /* Needed for x86 CPU capabilities detection */
1113 #   define cpuid( a )                      \
1114         asm volatile ( "pushl %%ebx\n\t"   \
1115                        "cpuid\n\t"         \
1116                        "movl %%ebx,%1\n\t" \
1117                        "popl %%ebx\n\t"    \
1118                      : "=a" ( i_eax ),     \
1119                        "=r" ( i_ebx ),     \
1120                        "=c" ( i_ecx ),     \
1121                        "=d" ( i_edx )      \
1122                      : "a"  ( a )          \
1123                      : "cc" );
1124
1125     i_capabilities |= CPU_CAPABILITY_FPU;
1126
1127 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1128     signal( SIGILL, IllegalSignalHandler );
1129 #   endif
1130
1131     /* test for a 486 CPU */
1132     asm volatile ( "pushl %%ebx\n\t"
1133                    "pushfl\n\t"
1134                    "popl %%eax\n\t"
1135                    "movl %%eax, %%ebx\n\t"
1136                    "xorl $0x200000, %%eax\n\t"
1137                    "pushl %%eax\n\t"
1138                    "popfl\n\t"
1139                    "pushfl\n\t"
1140                    "popl %%eax\n\t"
1141                    "movl %%ebx,%1\n\t"
1142                    "popl %%ebx\n\t"
1143                  : "=a" ( i_eax ),
1144                    "=r" ( i_ebx )
1145                  :
1146                  : "cc" );
1147
1148     if( i_eax == i_ebx )
1149     {
1150 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1151         signal( SIGILL, NULL );
1152 #   endif
1153         return( i_capabilities );
1154     }
1155
1156     i_capabilities |= CPU_CAPABILITY_486;
1157
1158     /* the CPU supports the CPUID instruction - get its level */
1159     cpuid( 0x00000000 );
1160
1161     if( !i_eax )
1162     {
1163 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1164         signal( SIGILL, NULL );
1165 #   endif
1166         return( i_capabilities );
1167     }
1168
1169     /* FIXME: this isn't correct, since some 486s have cpuid */
1170     i_capabilities |= CPU_CAPABILITY_586;
1171
1172     /* borrowed from mpeg2dec */
1173     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1174                     && ( i_edx == 0x69746e65 );
1175
1176     /* test for the MMX flag */
1177     cpuid( 0x00000001 );
1178
1179     if( ! (i_edx & 0x00800000) )
1180     {
1181 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1182         signal( SIGILL, NULL );
1183 #   endif
1184         return( i_capabilities );
1185     }
1186
1187     i_capabilities |= CPU_CAPABILITY_MMX;
1188
1189     if( i_edx & 0x02000000 )
1190     {
1191         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1192
1193 #   ifdef CAN_COMPILE_SSE
1194         /* We test if OS support the SSE instructions */
1195         psz_capability = "SSE";
1196         i_illegal = 0;
1197         if( setjmp( env ) == 0 )
1198         {
1199             /* Test a SSE instruction */
1200             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1201         }
1202
1203         if( i_illegal == 0 )
1204         {
1205             i_capabilities |= CPU_CAPABILITY_SSE;
1206         }
1207 #   endif
1208     }
1209
1210     /* test for additional capabilities */
1211     cpuid( 0x80000000 );
1212
1213     if( i_eax < 0x80000001 )
1214     {
1215 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1216         signal( SIGILL, NULL );
1217 #   endif
1218         return( i_capabilities );
1219     }
1220
1221     /* list these additional capabilities */
1222     cpuid( 0x80000001 );
1223
1224 #   ifdef CAN_COMPILE_3DNOW
1225     if( i_edx & 0x80000000 )
1226     {
1227         psz_capability = "3D Now!";
1228         i_illegal = 0;
1229         if( setjmp( env ) == 0 )
1230         {
1231             /* Test a 3D Now! instruction */
1232             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1233         }
1234
1235         if( i_illegal == 0 )
1236         {
1237             i_capabilities |= CPU_CAPABILITY_3DNOW;
1238         }
1239     }
1240 #   endif
1241
1242     if( b_amd && ( i_edx & 0x00400000 ) )
1243     {
1244         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1245     }
1246
1247 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1248     signal( SIGILL, NULL );
1249 #   endif
1250     return( i_capabilities );
1251
1252 #elif defined( __powerpc__ )
1253
1254     i_capabilities |= CPU_CAPABILITY_FPU;
1255
1256 #   ifdef CAN_COMPILE_ALTIVEC
1257     signal( SIGILL, IllegalSignalHandler );
1258
1259     psz_capability = "AltiVec";
1260     i_illegal = 0;
1261     if( setjmp( env ) == 0 )
1262     {
1263         asm volatile ("mtspr 256, %0\n\t"
1264                       "vand %%v0, %%v0, %%v0"
1265                       :
1266                       : "r" (-1));
1267     }
1268
1269     if( i_illegal == 0 )
1270     {
1271         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1272     }
1273
1274     signal( SIGILL, NULL );
1275 #   endif
1276
1277     return( i_capabilities );
1278
1279 #else
1280     /* default behaviour */
1281     return( i_capabilities );
1282
1283 #endif
1284 }
1285
1286 /*****************************************************************************
1287  * ShowConsole: On Win32, create an output console for debug messages
1288  *****************************************************************************
1289  * This function is usefull only on Win32.
1290  *****************************************************************************/
1291 #ifdef WIN32 /*  */
1292 static void ShowConsole( void )
1293 {
1294     AllocConsole();
1295     freopen( "CONOUT$", "w", stdout );
1296     freopen( "CONOUT$", "w", stderr );
1297     freopen( "CONIN$", "r", stdin );
1298     return;
1299 }
1300 #endif