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"
47 #include "interface.h"
49 #include "audio_output.h"
52 #include "beos_specific.h"
57 /*****************************************************************************
58 * Command line options constants. If something is changed here, be sure that
59 * GetConfiguration and Usage are also changed.
60 *****************************************************************************/
62 /* Long options return values - note that values corresponding to short options
63 * chars, and in general any regular char, should be avoided */
64 #define OPT_NOAUDIO 150
66 #define OPT_STEREO 152
69 #define OPT_NOVIDEO 160
71 #define OPT_DISPLAY 162
73 #define OPT_HEIGHT 164
76 #define OPT_NOVLANS 170
77 #define OPT_SERVER 171
86 static const struct option longopts[] =
88 /* name, has_arg, flag, val */
90 /* General/common options */
91 { "help", 0, 0, 'h' },
92 { "longhelp", 0, 0, 'H' },
93 { "version", 0, 0, 'v' },
96 { "noaudio", 0, 0, OPT_NOAUDIO },
97 { "aout", 1, 0, OPT_AOUT },
98 { "stereo", 0, 0, OPT_STEREO },
99 { "mono", 0, 0, OPT_MONO },
102 { "novideo", 0, 0, OPT_NOVIDEO },
103 { "vout", 1, 0, OPT_VOUT },
104 { "display", 1, 0, OPT_DISPLAY },
105 { "width", 1, 0, OPT_WIDTH },
106 { "height", 1, 0, OPT_HEIGHT },
107 { "grayscale", 0, 0, 'g' },
108 { "color", 0, 0, OPT_COLOR },
111 { "dvdaudio", 1, 0, 'a' },
112 { "dvdchannel", 1, 0, 'c' },
113 { "dvdsubtitle", 1, 0, 's' },
116 { "novlans", 0, 0, OPT_NOVLANS },
117 { "server", 1, 0, OPT_SERVER },
118 { "port", 1, 0, OPT_PORT },
124 static const char *psz_shortopts = "hHvga:s:c:";
126 /*****************************************************************************
127 * Global variable program_data - this is the one and only, see main.h
128 *****************************************************************************/
131 /*****************************************************************************
133 *****************************************************************************/
134 static void SetDefaultConfiguration ( void );
135 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
136 static void Usage ( int i_fashion );
137 static void Version ( void );
139 static void InitSignalHandler ( void );
140 static void SignalHandler ( int i_signal );
142 static int TestMMX ( void );
145 /*****************************************************************************
146 * main: parse command line, start interface and spawn threads
147 *****************************************************************************
148 * Steps during program execution are:
149 * -configuration parsing and messages interface initialization
150 * -openning of audio output device and some global modules
151 * -execution of interface, which exit on error or on user request
152 * -closing of audio output device and some global modules
153 * On error, the spawned threads are cancelled, and the open devices closed.
154 *****************************************************************************/
155 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
157 main_t main_data; /* root of all data - see main.h */
161 p_main = &main_data; /* set up the global variable */
164 * System specific initialization code
171 * Read configuration, initialize messages interface and set up program
176 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
180 p_main->p_msg = intf_MsgCreate();
181 if( !p_main->p_msg ) /* start messages interface */
183 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
187 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
193 /* get command line files */
196 if( optind < i_argc )
199 p_playlist = malloc( (i_list_index = i_argc - optind)
202 while( i_argc - i_index > optind )
204 p_playlist[ i_index ] = ppsz_argv[ i_argc - i_index - 1];
213 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
216 * Initialize shared resources and libraries
218 if( main_data.b_vlans && input_VlanCreate() )
220 /* On error during vlans initialization, switch of vlans */
221 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
222 main_data.b_vlans = 0;
226 * Open audio device and start aout thread
228 if( main_data.b_audio )
230 main_data.p_aout = aout_CreateThread( NULL );
231 if( main_data.p_aout == NULL )
233 /* On error during audio initialization, switch of audio */
234 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
235 main_data.b_audio = 0;
242 main_data.p_intf = intf_Create();
243 if( main_data.p_intf != NULL )
245 main_data.p_intf->p_playlist = p_playlist;
246 main_data.p_intf->i_list_index = i_list_index;
248 InitSignalHandler(); /* prepare signals for interception */
250 intf_Run( main_data.p_intf );
251 intf_Destroy( main_data.p_intf );
257 if( main_data.b_audio )
259 aout_DestroyThread( main_data.p_aout, NULL );
263 * Free shared resources and libraries
265 if( main_data.b_vlans )
271 * System specific cleaning code
278 * Terminate messages interface and program
280 intf_Msg( "Program terminated.\n" );
285 /*****************************************************************************
286 * main_GetIntVariable: get the int value of an environment variable
287 *****************************************************************************
288 * This function is used to read some default parameters in modules.
289 *****************************************************************************/
290 int main_GetIntVariable( char *psz_name, int i_default )
292 char * psz_env; /* environment value */
293 char * psz_end; /* end of parsing index */
294 long int i_value; /* value */
296 psz_env = getenv( psz_name );
299 i_value = strtol( psz_env, &psz_end, 0 );
300 if( (*psz_env != '\0') && (*psz_end == '\0') )
308 /*****************************************************************************
309 * main_GetPszVariable: get the string value of an environment variable
310 *****************************************************************************
311 * This function is used to read some default parameters in modules.
312 *****************************************************************************/
313 char * main_GetPszVariable( char *psz_name, char *psz_default )
317 psz_env = getenv( psz_name );
322 return( psz_default );
325 /*****************************************************************************
326 * main_PutPszVariable: set the string value of an environment variable
327 *****************************************************************************
328 * This function is used to set some default parameters in modules. The use of
329 * this function will cause some memory leak: since some systems use the pointer
330 * passed to putenv to store the environment string, it can't be freed.
331 *****************************************************************************/
332 void main_PutPszVariable( char *psz_name, char *psz_value )
336 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
337 if( psz_env == NULL )
339 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
343 sprintf( psz_env, "%s=%s", psz_name, psz_value );
344 if( putenv( psz_env ) )
346 intf_ErrMsg( "error: %s\n", strerror(errno) );
351 /*****************************************************************************
352 * main_PutIntVariable: set the integer value of an environment variable
353 *****************************************************************************
354 * This function is used to set some default parameters in modules. The use of
355 * this function will cause some memory leak: since some systems use the pointer
356 * passed to putenv to store the environment string, it can't be freed.
357 *****************************************************************************/
358 void main_PutIntVariable( char *psz_name, int i_value )
360 char psz_value[ 256 ]; /* buffer for value */
362 sprintf( psz_value, "%d", i_value );
363 main_PutPszVariable( psz_name, psz_value );
366 /* following functions are local */
368 /*****************************************************************************
369 * SetDefaultConfiguration: set default options
370 *****************************************************************************
371 * This function is called by GetConfiguration before command line is parsed.
372 * It sets all the default values required later by the program. At this stage,
373 * most structure are not yet allocated, so initialization must be done using
375 *****************************************************************************/
376 static void SetDefaultConfiguration( void )
379 * All features are activated by default
386 /*****************************************************************************
387 * GetConfiguration: parse command line
388 *****************************************************************************
389 * Parse command line and configuration file for configuration. If the inline
390 * help is requested, the function Usage() is called and the function returns
391 * -1 (causing main() to exit). The messages interface is initialized at this
392 * stage, but most structures are not allocated, so only environment should
394 *****************************************************************************/
395 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
399 /* Set default configuration and copy arguments */
400 p_main->i_argc = i_argc;
401 p_main->ppsz_argv = ppsz_argv;
402 p_main->ppsz_env = ppsz_env;
403 SetDefaultConfiguration();
405 /* Parse command line options */
407 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
411 /* General/common options */
412 case 'h': /* -h, --help */
416 case 'H': /* -H, --longhelp */
420 case 'v': /* -v, --version */
426 case OPT_NOAUDIO: /* --noaudio */
429 case OPT_AOUT: /* --aout */
430 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
432 case OPT_STEREO: /* --stereo */
433 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
435 case OPT_MONO: /* --mono */
436 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
440 case OPT_NOVIDEO: /* --novideo */
443 case OPT_VOUT: /* --vout */
444 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
446 case OPT_DISPLAY: /* --display */
447 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
449 case OPT_WIDTH: /* --width */
450 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
452 case OPT_HEIGHT: /* --height */
453 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
456 case 'g': /* -g, --grayscale */
457 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
459 case OPT_COLOR: /* --color */
460 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
465 if ( ! strcmp(optarg, "mpeg") )
466 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
467 else if ( ! strcmp(optarg, "lpcm") )
468 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
469 else if ( ! strcmp(optarg, "off") )
470 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
472 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
475 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
478 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
482 case OPT_NOVLANS: /* --novlans */
485 case OPT_SERVER: /* --server */
486 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
488 case OPT_PORT: /* --port */
489 main_PutPszVariable( INPUT_PORT_VAR, optarg );
492 /* Internal error: unknown option */
495 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
502 /* Parse command line parameters - no check is made for these options */
503 for( i_opt = optind; i_opt < i_argc; i_opt++ )
505 putenv( ppsz_argv[ i_opt ] );
510 /*****************************************************************************
511 * Usage: print program usage
512 *****************************************************************************
513 * Print a short inline help. Message interface is initialized at this stage.
514 *****************************************************************************/
515 static void Usage( int i_fashion )
518 intf_Msg( "Usage: vlc [options] [parameters]\n" );
520 if( i_fashion == USAGE )
522 intf_Msg( "Try `vlc --help' for more information.\n" );
526 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
531 " --noaudio \tdisable audio\n"
532 " --aout <plugin> \taudio output method\n"
533 " --stereo, --mono \tstereo/mono audio\n"
535 " --novideo \tdisable video\n"
536 " --vout <plugin> \tvideo output method\n"
537 " --display <display> \tdisplay string\n"
538 " --width <w>, --height <h> \tdisplay dimensions\n"
539 " -g, --grayscale \tgrayscale output\n"
540 " --color \tcolor output\n"
542 " -a, --dvdaudio \tchoose DVD audio type\n"
543 " -c, --dvdchannel \tchoose DVD audio channel\n"
544 " -s, --dvdsubtitle \tchoose DVD subtitle channel\n"
546 " --novlans \tdisable vlans\n"
547 " --server <host> \tvideo server address\n"
548 " --port <port> \tvideo server port\n"
550 " -h, --help \tprint help and exit\n"
551 " -H, --longhelp \tprint long help and exit\n"
552 " -v, --version \toutput version information and exit\n" );
554 if( i_fashion == SHORT_HELP )
557 /* Interface parameters */
559 "Interface parameters:\n"
560 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
561 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
563 /* Audio parameters */
565 "Audio parameters:\n"
566 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
567 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
568 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
569 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
571 /* Video parameters */
573 "Video parameters:\n"
574 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
575 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
576 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
577 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
578 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
579 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
584 " " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type\n"
585 " " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel\n"
586 " " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel\n" );
588 /* Input parameters */
590 "Input parameters:\n"
591 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
592 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
593 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
594 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
595 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
598 /*****************************************************************************
599 * Version: print complete program version
600 *****************************************************************************
601 * Print complete program version and build number.
602 *****************************************************************************/
603 static void Version( void )
605 intf_Msg( VERSION_MESSAGE
606 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
607 "You may redistribute it under the terms of the GNU General Public License;\n"
608 "see the file named COPYING for details.\n"
609 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
613 /*****************************************************************************
614 * InitSignalHandler: system signal handler initialization
615 *****************************************************************************
616 * Set the signal handlers. SIGTERM is not intercepted, because we need at
617 * at least a method to kill the program when all other methods failed, and
618 * when we don't want to use SIGKILL.
619 *****************************************************************************/
620 static void InitSignalHandler( void )
622 /* Termination signals */
623 signal( SIGHUP, SignalHandler );
624 signal( SIGINT, SignalHandler );
625 signal( SIGQUIT, SignalHandler );
628 /*****************************************************************************
629 * SignalHandler: system signal handler
630 *****************************************************************************
631 * This function is called when a signal is received by the program. It tries to
632 * end the program in a clean way.
633 *****************************************************************************/
634 static void SignalHandler( int i_signal )
636 /* Once a signal has been trapped, the termination sequence will be armed and
637 * following signals will be ignored to avoid sending messages to an interface
638 * having been destroyed */
639 signal( SIGHUP, SIG_IGN );
640 signal( SIGINT, SIG_IGN );
641 signal( SIGQUIT, SIG_IGN );
643 /* Acknowledge the signal received */
644 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
646 /* Try to terminate everything - this is done by requesting the end of the
647 * interface thread */
648 p_main->p_intf->b_die = 1;
652 /*****************************************************************************
653 * TestMMX: tests if the processor has MMX support.
654 *****************************************************************************
655 * This function is called if HAVE_MMX is enabled, to check whether the
656 * cpu really supports MMX.
657 *****************************************************************************/
658 static int TestMMX( void )
660 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
665 int i_reg, i_dummy = 0;
667 /* test for a 386 cpu */
668 asm volatile ( "pushfl
677 andl $0x40000, %%eax"
683 /* test for a 486 cpu */
684 asm volatile ( "movl %%ecx, %%eax
685 xorl $0x200000, %%eax
693 andl $0x200000, %%eax"
699 /* the cpu supports the CPUID instruction - get its level */
700 asm volatile ( "cpuid"
705 : "a" ( 0 ), /* level 0 */
706 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
708 /* this shouldn't happen on a normal cpu */
712 /* test for the MMX flag */
713 asm volatile ( "cpuid
714 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
719 : "a" ( 1 ), /* level 1 */
720 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */