]> git.sesse.net Git - vlc/blob - src/interface/main.c
. correction des conneries de BBP :)
[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  *
8  * Authors:
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
31 #include <getopt.h>                                              /* getopt() */
32 #include <stdio.h>                                              /* sprintf() */
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                  /* getenv(), strtol(),  */
36 #include <string.h>                                            /* strerror() */
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42 #include "plugins.h"
43 #include "input_vlan.h"
44
45 #include "intf_msg.h"
46 #include "interface.h"
47
48 #include "audio_output.h"
49
50 #ifdef SYS_BEOS
51 #include "beos_specific.h"
52 #endif
53
54 #include "main.h"
55
56 /*****************************************************************************
57  * Command line options constants. If something is changed here, be sure that
58  * GetConfiguration and Usage are also changed.
59  *****************************************************************************/
60
61 /* Long options return values - note that values corresponding to short options
62  * chars, and in general any regular char, should be avoided */
63 #define OPT_NOAUDIO             150
64 #define OPT_AOUT                151
65 #define OPT_STEREO              152
66 #define OPT_MONO                153
67
68 #define OPT_NOVIDEO             160
69 #define OPT_VOUT                161
70 #define OPT_DISPLAY             162
71 #define OPT_WIDTH               163
72 #define OPT_HEIGHT              164
73 #define OPT_COLOR               165
74
75 #define OPT_NOVLANS             170
76 #define OPT_SERVER              171
77 #define OPT_PORT                172
78
79 /* Usage fashion */
80 #define USAGE                     0
81 #define SHORT_HELP                1
82 #define LONG_HELP                 2
83
84 /* Long options */
85 static const struct option longopts[] =
86 {
87     /*  name,               has_arg,    flag,   val */
88
89     /* General/common options */
90     {   "help",             0,          0,      'h' },
91     {   "longhelp",         0,          0,      'H' },
92     {   "version",          0,          0,      'v' },
93
94     /* Audio options */
95     {   "noaudio",          0,          0,      OPT_NOAUDIO },
96     {   "aout",             1,          0,      OPT_AOUT },
97     {   "stereo",           0,          0,      OPT_STEREO },
98     {   "mono",             0,          0,      OPT_MONO },
99
100     /* Video options */
101     {   "novideo",          0,          0,      OPT_NOVIDEO },
102     {   "vout",             1,          0,      OPT_VOUT },
103     {   "display",          1,          0,      OPT_DISPLAY },
104     {   "width",            1,          0,      OPT_WIDTH },
105     {   "height",           1,          0,      OPT_HEIGHT },
106     {   "grayscale",        0,          0,      'g' },
107     {   "color",            0,          0,      OPT_COLOR },
108
109     /* Input options */
110     {   "novlans",          0,          0,      OPT_NOVLANS },
111     {   "server",           1,          0,      OPT_SERVER },
112     {   "port",             1,          0,      OPT_PORT },
113
114     {   0,                  0,          0,      0 }
115 };
116
117 /* Short options */
118 static const char *psz_shortopts = "hHvg";
119
120 /*****************************************************************************
121  * Global variable program_data - this is the one and only, see main.h
122  *****************************************************************************/
123 main_t *p_main;
124
125 /*****************************************************************************
126  * Local prototypes
127  *****************************************************************************/
128 static void SetDefaultConfiguration ( void );
129 static int  GetConfiguration        ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
130 static void Usage                   ( int i_fashion );
131 static void Version                 ( void );
132
133 static void InitSignalHandler       ( void );
134 static void SignalHandler           ( int i_signal );
135 #ifdef HAVE_MMX
136 static int  TestMMX                 ( void );
137 #endif
138
139 /*****************************************************************************
140  * main: parse command line, start interface and spawn threads
141  *****************************************************************************
142  * Steps during program execution are:
143  *      -configuration parsing and messages interface initialization
144  *      -openning of audio output device and some global modules
145  *      -execution of interface, which exit on error or on user request
146  *      -closing of audio output device and some global modules
147  * On error, the spawned threads are cancelled, and the open devices closed.
148  *****************************************************************************/
149 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
150 {
151     main_t  main_data;                      /* root of all data - see main.h */
152     p_main = &main_data;                       /* set up the global variable */
153
154     /*
155      * System specific initialization code
156      */
157 #ifdef SYS_BEOS
158     beos_Init();
159 #endif
160
161     /*
162      * Read configuration, initialize messages interface and set up program
163      */
164 #ifdef HAVE_MMX
165     if( !TestMMX() )
166     {
167         fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
168         return( 1 );
169     }
170 #endif
171     p_main->p_msg = intf_MsgCreate();
172     if( !p_main->p_msg )                         /* start messages interface */
173     {
174         fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
175                 strerror(errno) );
176         return( errno );
177     }
178     if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
179     {
180         intf_MsgDestroy();
181         return( errno );
182     }
183     intf_MsgImm( COPYRIGHT_MESSAGE "\n" );          /* print welcome message */
184
185     /*
186      * Initialize shared resources and libraries
187      */
188     if( main_data.b_vlans && input_VlanCreate() )
189     {
190         /* On error during vlans initialization, switch of vlans */
191         intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
192         main_data.b_vlans = 0;
193     }
194
195     /*
196      * Open audio device and start aout thread
197      */
198     if( main_data.b_audio )
199     {
200         main_data.p_aout = aout_CreateThread( NULL );
201         if( main_data.p_aout == NULL )
202         {
203             /* On error during audio initialization, switch of audio */
204             intf_Msg( "Audio initialization failed : audio is deactivated\n" );
205             main_data.b_audio = 0;
206         }
207     }
208
209     /*
210      * Run interface
211      */
212     main_data.p_intf = intf_Create();
213     if( main_data.p_intf != NULL )
214     {
215         InitSignalHandler();             /* prepare signals for interception */
216         intf_Run( main_data.p_intf );
217         intf_Destroy( main_data.p_intf );
218     }
219
220     /*
221      * Close audio device
222      */
223     if( main_data.b_audio )
224     {
225         aout_DestroyThread( main_data.p_aout, NULL );
226     }
227
228     /*
229      * Free shared resources and libraries
230      */
231     if( main_data.b_vlans )
232     {
233         input_VlanDestroy();
234     }
235
236     /*
237      * System specific cleaning code
238      */
239 #ifdef SYS_BEOS
240     beos_Clean();
241 #endif
242
243     /*
244      * Terminate messages interface and program
245      */
246     intf_Msg( "Program terminated.\n" );
247     intf_MsgDestroy();
248     return( 0 );
249 }
250
251 /*****************************************************************************
252  * main_GetIntVariable: get the int value of an environment variable
253  *****************************************************************************
254  * This function is used to read some default parameters in modules.
255  *****************************************************************************/
256 int main_GetIntVariable( char *psz_name, int i_default )
257 {
258     char *      psz_env;                                /* environment value */
259     char *      psz_end;                             /* end of parsing index */
260     long int    i_value;                                            /* value */
261
262     psz_env = getenv( psz_name );
263     if( psz_env )
264     {
265         i_value = strtol( psz_env, &psz_end, 0 );
266         if( (*psz_env != '\0') && (*psz_end == '\0') )
267         {
268             return( i_value );
269         }
270     }
271     return( i_default );
272 }
273
274 /*****************************************************************************
275  * main_GetPszVariable: get the string value of an environment variable
276  *****************************************************************************
277  * This function is used to read some default parameters in modules.
278  *****************************************************************************/
279 char * main_GetPszVariable( char *psz_name, char *psz_default )
280 {
281     char *psz_env;
282
283     psz_env = getenv( psz_name );
284     if( psz_env )
285     {
286         return( psz_env );
287     }
288     return( psz_default );
289 }
290
291 /*****************************************************************************
292  * main_PutPszVariable: set the string value of an environment variable
293  *****************************************************************************
294  * This function is used to set some default parameters in modules. The use of
295  * this function will cause some memory leak: since some systems use the pointer
296  * passed to putenv to store the environment string, it can't be freed.
297  *****************************************************************************/
298 void main_PutPszVariable( char *psz_name, char *psz_value )
299 {
300     char *psz_env;
301
302     psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
303     if( psz_env == NULL )
304     {
305         intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
306     }
307     else
308     {
309         sprintf( psz_env, "%s=%s", psz_name, psz_value );
310         if( putenv( psz_env ) )
311         {
312             intf_ErrMsg( "error: %s\n", strerror(errno) );
313         }
314     }
315 }
316
317 /*****************************************************************************
318  * main_PutIntVariable: set the integer value of an environment variable
319  *****************************************************************************
320  * This function is used to set some default parameters in modules. The use of
321  * this function will cause some memory leak: since some systems use the pointer
322  * passed to putenv to store the environment string, it can't be freed.
323  *****************************************************************************/
324 void main_PutIntVariable( char *psz_name, int i_value )
325 {
326     char psz_value[ 256 ];                               /* buffer for value */
327
328     sprintf( psz_value, "%d", i_value );
329     main_PutPszVariable( psz_name, psz_value );
330 }
331
332 /* following functions are local */
333
334 /*****************************************************************************
335  * SetDefaultConfiguration: set default options
336  *****************************************************************************
337  * This function is called by GetConfiguration before command line is parsed.
338  * It sets all the default values required later by the program. At this stage,
339  * most structure are not yet allocated, so initialization must be done using
340  * environment.
341  *****************************************************************************/
342 static void SetDefaultConfiguration( void )
343 {
344     /*
345      * All features are activated by default
346      */
347     p_main->b_audio  = 1;
348     p_main->b_video  = 1;
349     p_main->b_vlans  = 1;
350 }
351
352 /*****************************************************************************
353  * GetConfiguration: parse command line
354  *****************************************************************************
355  * Parse command line and configuration file for configuration. If the inline
356  * help is requested, the function Usage() is called and the function returns
357  * -1 (causing main() to exit). The messages interface is initialized at this
358  * stage, but most structures are not allocated, so only environment should
359  * be used.
360  *****************************************************************************/
361 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
362 {
363     int c, i_opt;
364
365     /* Set default configuration and copy arguments */
366     p_main->i_argc    = i_argc;
367     p_main->ppsz_argv = ppsz_argv;
368     p_main->ppsz_env  = ppsz_env;
369     SetDefaultConfiguration();
370
371     /* Parse command line options */
372     opterr = 0;
373     while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
374     {
375         switch( c )
376         {
377         /* General/common options */
378         case 'h':                                              /* -h, --help */
379             Usage( SHORT_HELP );
380             return( -1 );
381             break;
382         case 'H':                                          /* -H, --longhelp */
383             Usage( LONG_HELP );
384             return( -1 );
385             break;
386         case 'v':                                           /* -v, --version */
387             Version();
388             return( -1 );
389             break;
390
391         /* Audio options */
392         case OPT_NOAUDIO:                                       /* --noaudio */
393             p_main->b_audio = 0;
394             break;
395         case OPT_AOUT:                                             /* --aout */
396             main_PutPszVariable( AOUT_METHOD_VAR, optarg );
397             break;
398         case OPT_STEREO:                                         /* --stereo */
399             main_PutIntVariable( AOUT_STEREO_VAR, 1 );
400             break;
401         case OPT_MONO:                                             /* --mono */
402             main_PutIntVariable( AOUT_STEREO_VAR, 0 );
403             break;
404
405         /* Video options */
406         case OPT_NOVIDEO:                                       /* --novideo */
407             p_main->b_video = 0;
408             break;
409         case OPT_VOUT:                                             /* --vout */
410             main_PutPszVariable( VOUT_METHOD_VAR, optarg );
411             break;
412         case OPT_DISPLAY:                                       /* --display */
413             main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
414             break;
415         case OPT_WIDTH:                                           /* --width */
416             main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
417             break;
418         case OPT_HEIGHT:                                         /* --height */
419             main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
420             break;
421
422         case 'g':                                         /* -g, --grayscale */
423             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
424             break;
425         case OPT_COLOR:                                           /* --color */
426             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
427             break;
428
429         /* Input options */
430         case OPT_NOVLANS:                                       /* --novlans */
431             p_main->b_vlans = 0;
432             break;
433         case OPT_SERVER:                                         /* --server */
434             main_PutPszVariable( INPUT_SERVER_VAR, optarg );
435             break;
436         case OPT_PORT:                                             /* --port */
437             main_PutPszVariable( INPUT_PORT_VAR, optarg );
438             break;
439
440         /* Internal error: unknown option */
441         case '?':
442         default:
443             intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
444             Usage( USAGE );
445             return( EINVAL );
446             break;
447         }
448     }
449
450     /* Parse command line parameters - no check is made for these options */
451     for( i_opt = optind; i_opt < i_argc; i_opt++ )
452     {
453         putenv( ppsz_argv[ i_opt ] );
454     }
455     return( 0 );
456 }
457
458 /*****************************************************************************
459  * Usage: print program usage
460  *****************************************************************************
461  * Print a short inline help. Message interface is initialized at this stage.
462  *****************************************************************************/
463 static void Usage( int i_fashion )
464 {
465     /* Usage */
466     intf_Msg( "Usage: vlc [options] [parameters]\n" );
467
468     if( i_fashion == USAGE )
469     {
470         intf_Msg( "Try `vlc --help' for more information.\n" );
471         return;
472     }
473
474     intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
475
476     /* Options */
477     intf_Msg( "\n"
478               "Options:\n"
479               "      --noaudio                  \tdisable audio\n"
480               "      --aout <plugin>            \taudio output method\n"
481               "      --stereo, --mono           \tstereo/mono audio\n"
482               "\n"
483               "      --novideo                  \tdisable video\n"
484               "      --vout <plugin>            \tvideo output method\n"
485               "      --display <display>        \tdisplay string\n"
486               "      --width <w>, --height <h>  \tdisplay dimensions\n"
487               "  -g, --grayscale                \tgrayscale output\n"
488               "      --color                    \tcolor output\n"
489               "\n"
490               "      --novlans                  \tdisable vlans\n"
491               "      --server <host>            \tvideo server address\n"
492               "      --port <port>              \tvideo server port\n"
493               "\n"
494               "  -h, --help                     \tprint help and exit\n"
495               "  -H, --longhelp                 \tprint long help and exit\n"
496               "  -v, --version                  \toutput version information and exit\n" );
497
498     if( i_fashion == SHORT_HELP )
499         return;
500
501     /* Interface parameters */
502     intf_Msg( "\n"
503               "Interface parameters:\n"
504               "  " INTF_INIT_SCRIPT_VAR "=<filename>             \tinitialization script\n"
505               "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n" );
506
507     /* Audio parameters */
508     intf_Msg( "\n"
509               "Audio parameters:\n"
510               "  " AOUT_METHOD_VAR "=<method name>        \taudio method\n"
511               "  " AOUT_DSP_VAR "=<filename>              \tdsp device path\n"
512               "  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output\n"
513               "  " AOUT_RATE_VAR "=<rate>             \toutput rate\n" );
514
515     /* Video parameters */
516     intf_Msg( "\n"
517               "Video parameters:\n"
518               "  " VOUT_METHOD_VAR "=<method name>        \tdisplay method\n"
519               "  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used\n"
520               "  " VOUT_WIDTH_VAR "=<width>               \tdisplay width\n"
521               "  " VOUT_HEIGHT_VAR "=<height>             \tdislay height\n"
522               "  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path\n"
523               "  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output\n" );
524
525     /* Input parameters */
526     intf_Msg( "\n"
527               "Input parameters:\n"
528               "  " INPUT_SERVER_VAR "=<hostname>          \tvideo server\n"
529               "  " INPUT_PORT_VAR "=<port>            \tvideo server port\n"
530               "  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface\n"
531               "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
532               "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
533 }
534
535 /*****************************************************************************
536  * Version: print complete program version
537  *****************************************************************************
538  * Print complete program version and build number.
539  *****************************************************************************/
540 static void Version( void )
541 {
542     intf_Msg( VERSION_MESSAGE
543               "This program comes with NO WARRANTY, to the extent permitted by law.\n"
544               "You may redistribute it under the terms of the GNU General Public License;\n"
545               "see the file named COPYING for details.\n"
546               "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
547             
548 }
549
550 /*****************************************************************************
551  * InitSignalHandler: system signal handler initialization
552  *****************************************************************************
553  * Set the signal handlers. SIGTERM is not intercepted, because we need at
554  * at least a method to kill the program when all other methods failed, and
555  * when we don't want to use SIGKILL.
556  *****************************************************************************/
557 static void InitSignalHandler( void )
558 {
559     /* Termination signals */
560     signal( SIGHUP,  SignalHandler );
561     signal( SIGINT,  SignalHandler );
562     signal( SIGQUIT, SignalHandler );
563 }
564
565 /*****************************************************************************
566  * SignalHandler: system signal handler
567  *****************************************************************************
568  * This function is called when a signal is received by the program. It tries to
569  * end the program in a clean way.
570  *****************************************************************************/
571 static void SignalHandler( int i_signal )
572 {
573     /* Once a signal has been trapped, the termination sequence will be armed and
574      * following signals will be ignored to avoid sending messages to an interface
575      * having been destroyed */
576     signal( SIGHUP,  SIG_IGN );
577     signal( SIGINT,  SIG_IGN );
578     signal( SIGQUIT, SIG_IGN );
579
580     /* Acknowledge the signal received */
581     intf_ErrMsgImm("intf: signal %d received\n", i_signal );
582
583     /* Try to terminate everything - this is done by requesting the end of the
584      * interface thread */
585     p_main->p_intf->b_die = 1;
586 }
587
588 #ifdef HAVE_MMX
589 /*****************************************************************************
590  * TestMMX: tests if the processor has MMX support.
591  *****************************************************************************
592  * This function is called if HAVE_MMX is enabled, to check whether the
593  * cpu really supports MMX.
594  *****************************************************************************/
595 static int TestMMX( void )
596 {
597 /* FIXME: under beos, gcc does not support the foolowing inline assembly */ 
598 #ifdef SYS_BEOS
599     return( 1 );
600 #else
601
602     int i_reg, i_dummy = 0;
603
604     /* test for a 386 cpu */
605     asm volatile ( "pushfl
606                     popl %%eax
607                     movl %%eax, %%ecx
608                     xorl $0x40000, %%eax
609                     pushl %%eax
610                     popfl
611                     pushfl
612                     popl %%eax
613                     xorl %%ecx, %%eax
614                     andl $0x40000, %%eax"
615                  : "=a" ( i_reg ) );
616
617     if( !i_reg )
618         return( 0 );
619
620     /* test for a 486 cpu */
621     asm volatile ( "movl %%ecx, %%eax
622                     xorl $0x200000, %%eax
623                     pushl %%eax
624                     popfl
625                     pushfl
626                     popl %%eax
627                     xorl %%ecx, %%eax
628                     pushl %%ecx
629                     popfl
630                     andl $0x200000, %%eax"
631                  : "=a" ( i_reg ) );
632
633     if( !i_reg )
634         return( 0 );
635
636     /* the cpu supports the CPUID instruction - get its level */
637     asm volatile ( "cpuid"
638                  : "=a" ( i_reg ),
639                    "=b" ( i_dummy ),
640                    "=c" ( i_dummy ),
641                    "=d" ( i_dummy )
642                  : "a"  ( 0 ),       /* level 0 */
643                    "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
644
645     /* this shouldn't happen on a normal cpu */
646     if( !i_reg )
647         return( 0 );
648
649     /* test for the MMX flag */
650     asm volatile ( "cpuid
651                     andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
652                  : "=a" ( i_dummy ),
653                    "=b" ( i_dummy ),
654                    "=c" ( i_dummy ),
655                    "=d" ( i_reg )
656                  : "a"  ( 1 ),       /* level 1 */
657                    "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
658
659     if( !i_reg )
660         return( 0 );
661
662     return( 1 );
663
664 #endif
665 }
666 #endif