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