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 *****************************************************************************/
31 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
32 #include <getopt.h> /* getopt() */
33 #include <stdio.h> /* sprintf() */
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* getenv(), strtol(), */
37 #include <string.h> /* strerror() */
44 #include "input_vlan.h"
47 #include "interface.h"
49 #include "audio_output.h"
53 /*****************************************************************************
54 * Command line options constants. If something is changed here, be sure that
55 * GetConfiguration and Usage are also changed.
56 *****************************************************************************/
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
62 #define OPT_STEREO 152
65 #define OPT_NOVIDEO 160
67 #define OPT_DISPLAY 162
69 #define OPT_HEIGHT 164
72 #define OPT_NOVLANS 170
73 #define OPT_SERVER 171
82 static const struct option longopts[] =
84 /* name, has_arg, flag, val */
86 /* General/common options */
87 { "help", 0, 0, 'h' },
88 { "longhelp", 0, 0, 'H' },
89 { "version", 0, 0, 'v' },
92 { "noaudio", 0, 0, OPT_NOAUDIO },
93 { "aout", 1, 0, OPT_AOUT },
94 { "stereo", 0, 0, OPT_STEREO },
95 { "mono", 0, 0, OPT_MONO },
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 },
107 { "novlans", 0, 0, OPT_NOVLANS },
108 { "server", 1, 0, OPT_SERVER },
109 { "port", 1, 0, OPT_PORT },
115 static const char *psz_shortopts = "hHvg";
117 /*****************************************************************************
118 * Global variable program_data - this is the one and only, see main.h
119 *****************************************************************************/
122 /*****************************************************************************
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 );
130 static void InitSignalHandler ( void );
131 static void SignalHandler ( int i_signal );
132 static int TestMMX ( void );
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[] )
146 main_t main_data; /* root of all data - see main.h */
147 p_main = &main_data; /* set up the global variable */
150 * Read configuration, initialize messages interface and set up program
155 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
159 p_main->p_msg = intf_MsgCreate();
160 if( !p_main->p_msg ) /* start messages interface */
162 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
166 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
171 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
174 * Initialize shared resources and libraries
176 if( main_data.b_vlans && input_VlanCreate() )
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;
184 * Open audio device and start aout thread
186 if( main_data.b_audio )
188 main_data.p_aout = aout_CreateThread( NULL );
189 if( main_data.p_aout == NULL )
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;
200 main_data.p_intf = intf_Create();
201 if( main_data.p_intf != NULL )
203 InitSignalHandler(); /* prepare signals for interception */
204 intf_Run( main_data.p_intf );
205 intf_Destroy( main_data.p_intf );
211 if( main_data.b_audio )
213 aout_DestroyThread( main_data.p_aout, NULL );
217 * Free shared resources and libraries
219 if( main_data.b_vlans )
225 * Terminate messages interface and program
227 intf_Msg( "Program terminated.\n" );
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 )
239 char * psz_env; /* environment value */
240 char * psz_end; /* end of parsing index */
241 long int i_value; /* value */
243 psz_env = getenv( psz_name );
246 i_value = strtol( psz_env, &psz_end, 0 );
247 if( (*psz_env != '\0') && (*psz_end == '\0') )
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 )
264 psz_env = getenv( psz_name );
269 return( psz_default );
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 )
283 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
284 if( psz_env == NULL )
286 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
290 sprintf( psz_env, "%s=%s", psz_name, psz_value );
291 if( putenv( psz_env ) )
293 intf_ErrMsg( "error: %s\n", strerror(errno) );
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 )
307 char psz_value[ 256 ]; /* buffer for value */
309 sprintf( psz_value, "%d", i_value );
310 main_PutPszVariable( psz_name, psz_value );
313 /* following functions are local */
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
322 *****************************************************************************/
323 static void SetDefaultConfiguration( void )
326 * All features are activated by default
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
341 *****************************************************************************/
342 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
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();
352 /* Parse command line options */
354 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
358 /* General/common options */
359 case 'h': /* -h, --help */
363 case 'H': /* -H, --longhelp */
367 case 'v': /* -v, --version */
373 case OPT_NOAUDIO: /* --noaudio */
376 case OPT_AOUT: /* --aout */
377 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
379 case OPT_STEREO: /* --stereo */
380 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
382 case OPT_MONO: /* --mono */
383 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
387 case OPT_NOVIDEO: /* --novideo */
390 case OPT_VOUT: /* --vout */
391 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
393 case OPT_DISPLAY: /* --display */
394 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
396 case OPT_WIDTH: /* --width */
397 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
399 case OPT_HEIGHT: /* --height */
400 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
403 case 'g': /* -g, --grayscale */
404 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
406 case OPT_COLOR: /* --color */
407 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
411 case OPT_NOVLANS: /* --novlans */
414 case OPT_SERVER: /* --server */
415 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
417 case OPT_PORT: /* --port */
418 main_PutPszVariable( INPUT_PORT_VAR, optarg );
421 /* Internal error: unknown option */
424 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
431 /* Parse command line parameters - no check is made for these options */
432 for( i_opt = optind; i_opt < i_argc; i_opt++ )
434 putenv( ppsz_argv[ i_opt ] );
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 )
447 intf_Msg( "Usage: vlc [options] [parameters]\n" );
449 if( i_fashion == USAGE )
451 intf_Msg( "Try `vlc --help' for more information.\n" );
455 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
460 " --noaudio \tdisable audio\n"
461 " --aout <plugin> \taudio output method\n"
462 " --stereo, --mono \tstereo/mono audio\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"
471 " --novlans \tdisable vlans\n"
472 " --server <host> \tvideo server address\n"
473 " --port <port> \tvideo server port\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" );
479 if( i_fashion == SHORT_HELP )
482 /* Interface parameters */
484 "Interface parameters:\n"
485 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
486 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
488 /* Audio parameters */
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" );
496 /* Video parameters */
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" );
506 /* Input parameters */
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" );
516 /*****************************************************************************
517 * Version: print complete program version
518 *****************************************************************************
519 * Print complete program version and build number.
520 *****************************************************************************/
521 static void Version( void )
523 intf_Msg( "vlc " PROGRAM_VERSION " " PROGRAM_CODENAME
524 " (" PROGRAM_BUILD ") (" PROGRAM_OPTIONS ")\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" );
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 )
542 /* Termination signals */
543 signal( SIGHUP, SignalHandler );
544 signal( SIGINT, SignalHandler );
545 signal( SIGQUIT, SignalHandler );
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 )
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 );
563 /* Acknowledge the signal received */
564 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
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;
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 )
580 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
585 int i_reg, i_dummy = 0;
587 /* test for a 386 cpu */
588 asm volatile ( "pushfl
597 andl $0x40000, %%eax"
603 /* test for a 486 cpu */
604 asm volatile ( "movl %%ecx, %%eax
605 xorl $0x200000, %%eax
613 andl $0x200000, %%eax"
619 /* the cpu supports the CPUID instruction - get its level */
620 asm volatile ( "cpuid"
625 : "a" ( 0 ), /* level 0 */
626 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
628 /* this shouldn't happen on a normal cpu */
632 /* test for the MMX flag */
633 asm volatile ( "cpuid
634 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
639 : "a" ( 1 ), /* level 1 */
640 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */