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
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
31 #include <getopt.h> /* getopt() */
32 #include <stdio.h> /* sprintf() */
34 #include <errno.h> /* ENOMEM */
35 #include <stdlib.h> /* getenv(), strtol(), */
36 #include <string.h> /* strerror() */
43 #include "input_vlan.h"
46 #include "interface.h"
48 #include "audio_output.h"
51 #include "beos_specific.h"
56 /*****************************************************************************
57 * Command line options constants. If something is changed here, be sure that
58 * GetConfiguration and Usage are also changed.
59 *****************************************************************************/
61 /* Long options return values - note that values corresponding to short options
62 * chars, and in general any regular char, should be avoided */
63 #define OPT_NOAUDIO 150
65 #define OPT_STEREO 152
68 #define OPT_NOVIDEO 160
70 #define OPT_DISPLAY 162
72 #define OPT_HEIGHT 164
75 #define OPT_NOVLANS 170
76 #define OPT_SERVER 171
85 static const struct option longopts[] =
87 /* name, has_arg, flag, val */
89 /* General/common options */
90 { "help", 0, 0, 'h' },
91 { "longhelp", 0, 0, 'H' },
92 { "version", 0, 0, 'v' },
95 { "noaudio", 0, 0, OPT_NOAUDIO },
96 { "aout", 1, 0, OPT_AOUT },
97 { "stereo", 0, 0, OPT_STEREO },
98 { "mono", 0, 0, OPT_MONO },
101 { "novideo", 0, 0, OPT_NOVIDEO },
102 { "vout", 1, 0, OPT_VOUT },
103 { "display", 1, 0, OPT_DISPLAY },
104 { "width", 1, 0, OPT_WIDTH },
105 { "height", 1, 0, OPT_HEIGHT },
106 { "grayscale", 0, 0, 'g' },
107 { "color", 0, 0, OPT_COLOR },
110 { "novlans", 0, 0, OPT_NOVLANS },
111 { "server", 1, 0, OPT_SERVER },
112 { "port", 1, 0, OPT_PORT },
118 static const char *psz_shortopts = "hHvg";
120 /*****************************************************************************
121 * Global variable program_data - this is the one and only, see main.h
122 *****************************************************************************/
125 /*****************************************************************************
127 *****************************************************************************/
128 static void SetDefaultConfiguration ( void );
129 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
130 static void Usage ( int i_fashion );
131 static void Version ( void );
133 static void InitSignalHandler ( void );
134 static void SignalHandler ( int i_signal );
135 static int TestMMX ( void );
137 /*****************************************************************************
138 * main: parse command line, start interface and spawn threads
139 *****************************************************************************
140 * Steps during program execution are:
141 * -configuration parsing and messages interface initialization
142 * -openning of audio output device and some global modules
143 * -execution of interface, which exit on error or on user request
144 * -closing of audio output device and some global modules
145 * On error, the spawned threads are cancelled, and the open devices closed.
146 *****************************************************************************/
147 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
149 main_t main_data; /* root of all data - see main.h */
150 p_main = &main_data; /* set up the global variable */
153 * System specific initialization code
160 * Read configuration, initialize messages interface and set up program
165 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
169 p_main->p_msg = intf_MsgCreate();
170 if( !p_main->p_msg ) /* start messages interface */
172 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
176 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
181 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
184 * Initialize shared resources and libraries
186 if( main_data.b_vlans && input_VlanCreate() )
188 /* On error during vlans initialization, switch of vlans */
189 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
190 main_data.b_vlans = 0;
194 * Open audio device and start aout thread
196 if( main_data.b_audio )
198 main_data.p_aout = aout_CreateThread( NULL );
199 if( main_data.p_aout == NULL )
201 /* On error during audio initialization, switch of audio */
202 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
203 main_data.b_audio = 0;
210 main_data.p_intf = intf_Create();
211 if( main_data.p_intf != NULL )
213 InitSignalHandler(); /* prepare signals for interception */
214 intf_Run( main_data.p_intf );
215 intf_Destroy( main_data.p_intf );
221 if( main_data.b_audio )
223 aout_DestroyThread( main_data.p_aout, NULL );
227 * Free shared resources and libraries
229 if( main_data.b_vlans )
235 * System specific cleaning code
242 * Terminate messages interface and program
244 intf_Msg( "Program terminated.\n" );
249 /*****************************************************************************
250 * main_GetIntVariable: get the int value of an environment variable
251 *****************************************************************************
252 * This function is used to read some default parameters in modules.
253 *****************************************************************************/
254 int main_GetIntVariable( char *psz_name, int i_default )
256 char * psz_env; /* environment value */
257 char * psz_end; /* end of parsing index */
258 long int i_value; /* value */
260 psz_env = getenv( psz_name );
263 i_value = strtol( psz_env, &psz_end, 0 );
264 if( (*psz_env != '\0') && (*psz_end == '\0') )
272 /*****************************************************************************
273 * main_GetPszVariable: get the string value of an environment variable
274 *****************************************************************************
275 * This function is used to read some default parameters in modules.
276 *****************************************************************************/
277 char * main_GetPszVariable( char *psz_name, char *psz_default )
281 psz_env = getenv( psz_name );
286 return( psz_default );
289 /*****************************************************************************
290 * main_PutPszVariable: set the string value of an environment variable
291 *****************************************************************************
292 * This function is used to set some default parameters in modules. The use of
293 * this function will cause some memory leak: since some systems use the pointer
294 * passed to putenv to store the environment string, it can't be freed.
295 *****************************************************************************/
296 void main_PutPszVariable( char *psz_name, char *psz_value )
300 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
301 if( psz_env == NULL )
303 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
307 sprintf( psz_env, "%s=%s", psz_name, psz_value );
308 if( putenv( psz_env ) )
310 intf_ErrMsg( "error: %s\n", strerror(errno) );
315 /*****************************************************************************
316 * main_PutIntVariable: set the integer value of an environment variable
317 *****************************************************************************
318 * This function is used to set some default parameters in modules. The use of
319 * this function will cause some memory leak: since some systems use the pointer
320 * passed to putenv to store the environment string, it can't be freed.
321 *****************************************************************************/
322 void main_PutIntVariable( char *psz_name, int i_value )
324 char psz_value[ 256 ]; /* buffer for value */
326 sprintf( psz_value, "%d", i_value );
327 main_PutPszVariable( psz_name, psz_value );
330 /* following functions are local */
332 /*****************************************************************************
333 * SetDefaultConfiguration: set default options
334 *****************************************************************************
335 * This function is called by GetConfiguration before command line is parsed.
336 * It sets all the default values required later by the program. At this stage,
337 * most structure are not yet allocated, so initialization must be done using
339 *****************************************************************************/
340 static void SetDefaultConfiguration( void )
343 * All features are activated by default
350 /*****************************************************************************
351 * GetConfiguration: parse command line
352 *****************************************************************************
353 * Parse command line and configuration file for configuration. If the inline
354 * help is requested, the function Usage() is called and the function returns
355 * -1 (causing main() to exit). The messages interface is initialized at this
356 * stage, but most structures are not allocated, so only environment should
358 *****************************************************************************/
359 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
363 /* Set default configuration and copy arguments */
364 p_main->i_argc = i_argc;
365 p_main->ppsz_argv = ppsz_argv;
366 p_main->ppsz_env = ppsz_env;
367 SetDefaultConfiguration();
369 /* Parse command line options */
371 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
375 /* General/common options */
376 case 'h': /* -h, --help */
380 case 'H': /* -H, --longhelp */
384 case 'v': /* -v, --version */
390 case OPT_NOAUDIO: /* --noaudio */
393 case OPT_AOUT: /* --aout */
394 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
396 case OPT_STEREO: /* --stereo */
397 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
399 case OPT_MONO: /* --mono */
400 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
404 case OPT_NOVIDEO: /* --novideo */
407 case OPT_VOUT: /* --vout */
408 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
410 case OPT_DISPLAY: /* --display */
411 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
413 case OPT_WIDTH: /* --width */
414 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
416 case OPT_HEIGHT: /* --height */
417 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
420 case 'g': /* -g, --grayscale */
421 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
423 case OPT_COLOR: /* --color */
424 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
428 case OPT_NOVLANS: /* --novlans */
431 case OPT_SERVER: /* --server */
432 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
434 case OPT_PORT: /* --port */
435 main_PutPszVariable( INPUT_PORT_VAR, optarg );
438 /* Internal error: unknown option */
441 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
448 /* Parse command line parameters - no check is made for these options */
449 for( i_opt = optind; i_opt < i_argc; i_opt++ )
451 putenv( ppsz_argv[ i_opt ] );
456 /*****************************************************************************
457 * Usage: print program usage
458 *****************************************************************************
459 * Print a short inline help. Message interface is initialized at this stage.
460 *****************************************************************************/
461 static void Usage( int i_fashion )
464 intf_Msg( "Usage: vlc [options] [parameters]\n" );
466 if( i_fashion == USAGE )
468 intf_Msg( "Try `vlc --help' for more information.\n" );
472 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
477 " --noaudio \tdisable audio\n"
478 " --aout <plugin> \taudio output method\n"
479 " --stereo, --mono \tstereo/mono audio\n"
481 " --novideo \tdisable audio\n"
482 " --vout <plugin> \tvideo output method\n"
483 " --display <display> \tdisplay string\n"
484 " --width <w>, --height <h> \tdisplay dimensions\n"
485 " -g, --grayscale \tgrayscale output\n"
486 " --color \tcolor output\n"
488 " --novlans \tdisable vlans\n"
489 " --server <host> \tvideo server address\n"
490 " --port <port> \tvideo server port\n"
492 " -h, --help \tprint help and exit\n"
493 " -H, --longhelp \tprint long help and exit\n"
494 " -v, --version \toutput version information and exit\n" );
496 if( i_fashion == SHORT_HELP )
499 /* Interface parameters */
501 "Interface parameters:\n"
502 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
503 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
505 /* Audio parameters */
507 "Audio parameters:\n"
508 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
509 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
510 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
511 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
513 /* Video parameters */
515 "Video parameters:\n"
516 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
517 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
518 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
519 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
520 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
521 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
523 /* Input parameters */
525 "Input parameters:\n"
526 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
527 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
528 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
529 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
530 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
533 /*****************************************************************************
534 * Version: print complete program version
535 *****************************************************************************
536 * Print complete program version and build number.
537 *****************************************************************************/
538 static void Version( void )
540 intf_Msg( VERSION_MESSAGE
541 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
542 "You may redistribute it under the terms of the GNU General Public License;\n"
543 "see the file named COPYING for details.\n"
544 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
548 /*****************************************************************************
549 * InitSignalHandler: system signal handler initialization
550 *****************************************************************************
551 * Set the signal handlers. SIGTERM is not intercepted, because we need at
552 * at least a method to kill the program when all other methods failed, and
553 * when we don't want to use SIGKILL.
554 *****************************************************************************/
555 static void InitSignalHandler( void )
557 /* Termination signals */
558 signal( SIGHUP, SignalHandler );
559 signal( SIGINT, SignalHandler );
560 signal( SIGQUIT, SignalHandler );
563 /*****************************************************************************
564 * SignalHandler: system signal handler
565 *****************************************************************************
566 * This function is called when a signal is received by the program. It tries to
567 * end the program in a clean way.
568 *****************************************************************************/
569 static void SignalHandler( int i_signal )
571 /* Once a signal has been trapped, the termination sequence will be armed and
572 * following signals will be ignored to avoid sending messages to an interface
573 * having been destroyed */
574 signal( SIGHUP, SIG_IGN );
575 signal( SIGINT, SIG_IGN );
576 signal( SIGQUIT, SIG_IGN );
578 /* Acknowledge the signal received */
579 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
581 /* Try to terminate everything - this is done by requesting the end of the
582 * interface thread */
583 p_main->p_intf->b_die = 1;
587 /*****************************************************************************
588 * TestMMX: tests if the processor has MMX support.
589 *****************************************************************************
590 * This function is called if HAVE_MMX is enabled, to check whether the
591 * cpu really supports MMX.
592 *****************************************************************************/
593 static int TestMMX( void )
595 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
600 int i_reg, i_dummy = 0;
602 /* test for a 386 cpu */
603 asm volatile ( "pushfl
612 andl $0x40000, %%eax"
618 /* test for a 486 cpu */
619 asm volatile ( "movl %%ecx, %%eax
620 xorl $0x200000, %%eax
628 andl $0x200000, %%eax"
634 /* the cpu supports the CPUID instruction - get its level */
635 asm volatile ( "cpuid"
640 : "a" ( 0 ), /* level 0 */
641 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
643 /* this shouldn't happen on a normal cpu */
647 /* test for the MMX flag */
648 asm volatile ( "cpuid
649 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
654 : "a" ( 1 ), /* level 1 */
655 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */