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