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