1 /*****************************************************************************
2 * main.c: main vlc source
3 * Includes the main() function for vlc. Parses command line, start interface
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
30 #include <getopt.h> /* getopt() */
31 #include <stdio.h> /* sprintf() */
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* getenv(), strtol(), */
35 #include <string.h> /* strerror() */
42 #include "input_vlan.h"
45 #include "interface.h"
47 #include "audio_output.h"
51 /*****************************************************************************
52 * Command line options constants. If something is changed here, be sure that
53 * GetConfiguration and Usage are also changed.
54 *****************************************************************************/
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
60 #define OPT_STEREO 152
63 #define OPT_NOVIDEO 160
65 #define OPT_DISPLAY 162
67 #define OPT_HEIGHT 164
70 #define OPT_NOVLANS 170
71 #define OPT_SERVER 171
80 static const struct option longopts[] =
82 /* name, has_arg, flag, val */
84 /* General/common options */
85 { "help", 0, 0, 'h' },
86 { "longhelp", 0, 0, 'H' },
87 { "version", 0, 0, 'v' },
90 { "noaudio", 0, 0, OPT_NOAUDIO },
91 { "aout", 1, 0, OPT_AOUT },
92 { "stereo", 0, 0, OPT_STEREO },
93 { "mono", 0, 0, OPT_MONO },
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 },
105 { "novlans", 0, 0, OPT_NOVLANS },
106 { "server", 1, 0, OPT_SERVER },
107 { "port", 1, 0, OPT_PORT },
113 static const char *psz_shortopts = "hHvg";
115 /*****************************************************************************
116 * Global variable program_data - this is the one and only, see main.h
117 *****************************************************************************/
120 /*****************************************************************************
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 );
128 static void InitSignalHandler ( void );
129 static void SignalHandler ( int i_signal );
130 static int TestMMX ( void );
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[] )
144 main_t main_data; /* root of all data - see main.h */
145 p_main = &main_data; /* set up the global variable */
148 * Read configuration, initialize messages interface and set up program
153 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
157 p_main->p_msg = intf_MsgCreate();
158 if( !p_main->p_msg ) /* start messages interface */
160 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
164 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
169 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
172 * Initialize shared resources and libraries
174 if( main_data.b_vlans && input_VlanCreate() )
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;
182 * Open audio device and start aout thread
184 if( main_data.b_audio )
186 main_data.p_aout = aout_CreateThread( NULL );
187 if( main_data.p_aout == NULL )
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;
198 main_data.p_intf = intf_Create();
199 if( main_data.p_intf != NULL )
201 InitSignalHandler(); /* prepare signals for interception */
202 intf_Run( main_data.p_intf );
203 intf_Destroy( main_data.p_intf );
209 if( main_data.b_audio )
211 aout_DestroyThread( main_data.p_aout, NULL );
215 * Free shared resources and libraries
217 if( main_data.b_vlans )
223 * Terminate messages interface and program
225 intf_Msg( "Program terminated.\n" );
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 )
237 char * psz_env; /* environment value */
238 char * psz_end; /* end of parsing index */
239 long int i_value; /* value */
241 psz_env = getenv( psz_name );
244 i_value = strtol( psz_env, &psz_end, 0 );
245 if( (*psz_env != '\0') && (*psz_end == '\0') )
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 )
262 psz_env = getenv( psz_name );
267 return( psz_default );
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 )
281 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
282 if( psz_env == NULL )
284 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
288 sprintf( psz_env, "%s=%s", psz_name, psz_value );
289 if( putenv( psz_env ) )
291 intf_ErrMsg( "error: %s\n", strerror(errno) );
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 )
305 char psz_value[ 256 ]; /* buffer for value */
307 sprintf( psz_value, "%d", i_value );
308 main_PutPszVariable( psz_name, psz_value );
311 /* following functions are local */
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
320 *****************************************************************************/
321 static void SetDefaultConfiguration( void )
324 * All features are activated by default
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
339 *****************************************************************************/
340 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
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();
350 /* Parse command line options */
352 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
356 /* General/common options */
357 case 'h': /* -h, --help */
361 case 'H': /* -H, --longhelp */
365 case 'v': /* -v, --version */
371 case OPT_NOAUDIO: /* --noaudio */
374 case OPT_AOUT: /* --aout */
375 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
377 case OPT_STEREO: /* --stereo */
378 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
380 case OPT_MONO: /* --mono */
381 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
385 case OPT_NOVIDEO: /* --novideo */
388 case OPT_VOUT: /* --vout */
389 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
391 case OPT_DISPLAY: /* --display */
392 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
394 case OPT_WIDTH: /* --width */
395 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
397 case OPT_HEIGHT: /* --height */
398 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
401 case 'g': /* -g, --grayscale */
402 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
404 case OPT_COLOR: /* --color */
405 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
409 case OPT_NOVLANS: /* --novlans */
412 case OPT_SERVER: /* --server */
413 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
415 case OPT_PORT: /* --port */
416 main_PutPszVariable( INPUT_PORT_VAR, optarg );
419 /* Internal error: unknown option */
422 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
429 /* Parse command line parameters - no check is made for these options */
430 for( i_opt = optind; i_opt < i_argc; i_opt++ )
432 putenv( ppsz_argv[ i_opt ] );
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 )
445 intf_Msg( "Usage: vlc [options] [parameters]\n" );
447 if( i_fashion == USAGE )
449 intf_Msg( "Try `vlc --help' for more information.\n" );
453 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
458 " --noaudio \tdisable audio\n"
459 " --aout <plugin> \taudio output method\n"
460 " --stereo, --mono \tstereo/mono audio\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"
469 " --novlans \tdisable vlans\n"
470 " --server <host> \tvideo server address\n"
471 " --port <port> \tvideo server port\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" );
477 if( i_fashion == SHORT_HELP )
480 /* Interface parameters */
482 "Interface parameters:\n"
483 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
484 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
486 /* Audio parameters */
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" );
494 /* Video parameters */
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" );
504 /* Input parameters */
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" );
514 /*****************************************************************************
515 * Version: print complete program version
516 *****************************************************************************
517 * Print complete program version and build number.
518 *****************************************************************************/
519 static void Version( void )
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" );
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 )
540 /* Termination signals */
541 signal( SIGHUP, SignalHandler );
542 signal( SIGINT, SignalHandler );
543 signal( SIGQUIT, SignalHandler );
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 )
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 );
561 /* Acknowledge the signal received */
562 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
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;
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 )
578 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
581 int i_reg, i_dummy = 0;
583 /* test for a 386 cpu */
584 asm volatile ( "pushfl
593 andl $0x40000, %%eax"
599 /* test for a 486 cpu */
600 asm volatile ( "movl %%ecx, %%eax
601 xorl $0x200000, %%eax
609 andl $0x200000, %%eax"
615 /* the cpu supports the CPUID instruction - get its level */
616 asm volatile ( "cpuid"
621 : "a" ( 0 ), /* level 0 */
622 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
624 /* this shouldn't happen on a normal cpu */
628 /* test for the MMX flag */
629 asm volatile ( "cpuid
630 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
635 : "a" ( 1 ), /* level 1 */
636 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
645 #endif /* SYS_BEOS */