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