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