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() */
44 #include "input_vlan.h"
48 #include "interface.h"
50 #include "audio_output.h"
53 #include "beos_specific.h"
58 /*****************************************************************************
59 * Command line options constants. If something is changed here, be sure that
60 * GetConfiguration and Usage are also changed.
61 *****************************************************************************/
63 /* Long options return values - note that values corresponding to short options
64 * chars, and in general any regular char, should be avoided */
65 #define OPT_NOAUDIO 150
67 #define OPT_STEREO 152
70 #define OPT_NOVIDEO 160
72 #define OPT_DISPLAY 162
74 #define OPT_HEIGHT 164
77 #define OPT_NOVLANS 170
78 #define OPT_SERVER 171
87 static const struct option longopts[] =
89 /* name, has_arg, flag, val */
91 /* General/common options */
92 { "help", 0, 0, 'h' },
93 { "longhelp", 0, 0, 'H' },
94 { "version", 0, 0, 'v' },
97 { "noaudio", 0, 0, OPT_NOAUDIO },
98 { "aout", 1, 0, OPT_AOUT },
99 { "stereo", 0, 0, OPT_STEREO },
100 { "mono", 0, 0, OPT_MONO },
103 { "novideo", 0, 0, OPT_NOVIDEO },
104 { "vout", 1, 0, OPT_VOUT },
105 { "display", 1, 0, OPT_DISPLAY },
106 { "width", 1, 0, OPT_WIDTH },
107 { "height", 1, 0, OPT_HEIGHT },
108 { "grayscale", 0, 0, 'g' },
109 { "color", 0, 0, OPT_COLOR },
112 { "dvdaudio", 1, 0, 'a' },
113 { "dvdchannel", 1, 0, 'c' },
114 { "dvdsubtitle", 1, 0, 's' },
117 { "novlans", 0, 0, OPT_NOVLANS },
118 { "server", 1, 0, OPT_SERVER },
119 { "port", 1, 0, OPT_PORT },
125 static const char *psz_shortopts = "hHvga:s:c:";
127 /*****************************************************************************
128 * Global variable program_data - this is the one and only, see main.h
129 *****************************************************************************/
132 /*****************************************************************************
134 *****************************************************************************/
135 static void SetDefaultConfiguration ( void );
136 static int GetConfiguration ( int i_argc, char *ppsz_argv[],
138 static void Usage ( int i_fashion );
139 static void Version ( void );
141 static void InitSignalHandler ( void );
142 static void SignalHandler ( int i_signal );
144 static int TestMMX ( void );
147 /*****************************************************************************
148 * main: parse command line, start interface and spawn threads
149 *****************************************************************************
150 * Steps during program execution are:
151 * -configuration parsing and messages interface initialization
152 * -opening of audio output device and some global modules
153 * -execution of interface, which exit on error or on user request
154 * -closing of audio output device and some global modules
155 * On error, the spawned threads are canceled, and the open devices closed.
156 *****************************************************************************/
157 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
159 main_t main_data; /* root of all data - see main.h */
161 p_main = &main_data; /* set up the global variable */
165 * System specific initialization code
172 * Test if our code is likely to run on this CPU
176 fprintf( stderr, "Sorry, this program needs an MMX processor. "
177 "Please run the non-MMX version.\n" );
183 * Initialize messages interface
185 p_main->p_msg = intf_MsgCreate();
186 if( !p_main->p_msg ) /* start messages interface */
188 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
196 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
199 fprintf( stderr, "error: can't read configuration (%s)\n",
205 * Initialize playlist and get commandline files
207 p_main->p_playlist = playlist_Create( );
208 if( !p_main->p_playlist )
210 intf_Msg( "Playlist initialization failed\n" );
214 playlist_Init( p_main->p_playlist, optind );
217 * Initialize plugin bank
219 p_main->p_bank = bank_Create( );
220 if( !p_main->p_bank )
222 intf_Msg( "Plugin bank initialization failed\n" );
223 playlist_Destroy( p_main->p_playlist );
227 bank_Init( p_main->p_bank );
230 * Initialize shared resources and libraries
232 if( p_main->b_vlans && input_VlanCreate() )
234 /* On error during vlans initialization, switch off vlans */
235 intf_Msg( "Virtual LANs initialization failed : "
236 "vlans management is deactivated\n" );
241 * Open audio device and start aout thread
243 if( p_main->b_audio )
245 p_main->p_aout = aout_CreateThread( NULL );
246 if( p_main->p_aout == NULL )
248 /* On error during audio initialization, switch off audio */
249 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
257 p_main->p_intf = intf_Create();
258 if( p_main->p_intf != NULL )
260 InitSignalHandler(); /* prepare signals for interception */
262 intf_Run( p_main->p_intf );
264 intf_Destroy( p_main->p_intf );
270 if( p_main->b_audio )
272 aout_DestroyThread( p_main->p_aout, NULL );
276 * Free shared resources and libraries
278 if( p_main->b_vlans )
286 bank_Destroy( p_main->p_bank );
291 playlist_Destroy( p_main->p_playlist );
295 * System specific cleaning code
301 * Terminate messages interface and program
303 intf_Msg( "Program terminated.\n" );
309 /*****************************************************************************
310 * main_GetIntVariable: get the int value of an environment variable
311 *****************************************************************************
312 * This function is used to read some default parameters in modules.
313 *****************************************************************************/
314 int main_GetIntVariable( char *psz_name, int i_default )
316 char * psz_env; /* environment value */
317 char * psz_end; /* end of parsing index */
318 long int i_value; /* value */
320 psz_env = getenv( psz_name );
323 i_value = strtol( psz_env, &psz_end, 0 );
324 if( (*psz_env != '\0') && (*psz_end == '\0') )
332 /*****************************************************************************
333 * main_GetPszVariable: get the string value of an environment variable
334 *****************************************************************************
335 * This function is used to read some default parameters in modules.
336 *****************************************************************************/
337 char * main_GetPszVariable( char *psz_name, char *psz_default )
341 psz_env = getenv( psz_name );
346 return( psz_default );
349 /*****************************************************************************
350 * main_PutPszVariable: set the string value of an environment variable
351 *****************************************************************************
352 * This function is used to set some default parameters in modules. The use of
353 * this function will cause some memory leak: since some systems use the pointer
354 * passed to putenv to store the environment string, it can't be freed.
355 *****************************************************************************/
356 void main_PutPszVariable( char *psz_name, char *psz_value )
360 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
361 if( psz_env == NULL )
363 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
367 sprintf( psz_env, "%s=%s", psz_name, psz_value );
368 if( putenv( psz_env ) )
370 intf_ErrMsg( "error: %s\n", strerror(errno) );
375 /*****************************************************************************
376 * main_PutIntVariable: set the integer value of an environment variable
377 *****************************************************************************
378 * This function is used to set some default parameters in modules. The use of
379 * this function will cause some memory leak: since some systems use the pointer
380 * passed to putenv to store the environment string, it can't be freed.
381 *****************************************************************************/
382 void main_PutIntVariable( char *psz_name, int i_value )
384 char psz_value[ 256 ]; /* buffer for value */
386 sprintf( psz_value, "%d", i_value );
387 main_PutPszVariable( psz_name, psz_value );
390 /* following functions are local */
392 /*****************************************************************************
393 * SetDefaultConfiguration: set default options
394 *****************************************************************************
395 * This function is called by GetConfiguration before command line is parsed.
396 * It sets all the default values required later by the program. At this stage,
397 * most structure are not yet allocated, so initialization must be done using
399 *****************************************************************************/
400 static void SetDefaultConfiguration( void )
403 * All features are activated by default
410 /*****************************************************************************
411 * GetConfiguration: parse command line
412 *****************************************************************************
413 * Parse command line and configuration file for configuration. If the inline
414 * help is requested, the function Usage() is called and the function returns
415 * -1 (causing main() to exit). The messages interface is initialized at this
416 * stage, but most structures are not allocated, so only environment should
418 *****************************************************************************/
419 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
423 /* Set default configuration and copy arguments */
424 p_main->i_argc = i_argc;
425 p_main->ppsz_argv = ppsz_argv;
426 p_main->ppsz_env = ppsz_env;
427 SetDefaultConfiguration();
429 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
431 /* Parse command line options */
433 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
437 /* General/common options */
438 case 'h': /* -h, --help */
442 case 'H': /* -H, --longhelp */
446 case 'v': /* -v, --version */
452 case OPT_NOAUDIO: /* --noaudio */
455 case OPT_AOUT: /* --aout */
456 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
458 case OPT_STEREO: /* --stereo */
459 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
461 case OPT_MONO: /* --mono */
462 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
466 case OPT_NOVIDEO: /* --novideo */
469 case OPT_VOUT: /* --vout */
470 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
472 case OPT_DISPLAY: /* --display */
473 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
475 case OPT_WIDTH: /* --width */
476 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
478 case OPT_HEIGHT: /* --height */
479 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
482 case 'g': /* -g, --grayscale */
483 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
485 case OPT_COLOR: /* --color */
486 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
491 if ( ! strcmp(optarg, "mpeg") )
492 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
493 else if ( ! strcmp(optarg, "lpcm") )
494 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
495 else if ( ! strcmp(optarg, "off") )
496 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
498 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
501 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
504 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
508 case OPT_NOVLANS: /* --novlans */
511 case OPT_SERVER: /* --server */
512 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
514 case OPT_PORT: /* --port */
515 main_PutPszVariable( INPUT_PORT_VAR, optarg );
518 /* Internal error: unknown option */
521 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
528 /* Parse command line parameters - no check is made for these options */
529 for( i_opt = optind; i_opt < i_argc; i_opt++ )
531 putenv( ppsz_argv[ i_opt ] );
536 /*****************************************************************************
537 * Usage: print program usage
538 *****************************************************************************
539 * Print a short inline help. Message interface is initialized at this stage.
540 *****************************************************************************/
541 static void Usage( int i_fashion )
544 intf_Msg( "Usage: vlc [options] [parameters] [file]...\n" );
546 if( i_fashion == USAGE )
548 intf_Msg( "Try `vlc --help' for more information.\n" );
555 " --noaudio \tdisable audio\n"
556 " --aout <plugin> \taudio output method\n"
557 " --stereo, --mono \tstereo/mono audio\n"
559 " --novideo \tdisable video\n"
560 " --vout <plugin> \tvideo output method\n"
561 " --display <display> \tdisplay string\n"
562 " --width <w>, --height <h> \tdisplay dimensions\n"
563 " -g, --grayscale \tgrayscale output\n"
564 " --color \tcolor output\n"
566 " -a, --dvdaudio \tchoose DVD audio type\n"
567 " -c, --dvdchannel \tchoose DVD audio channel\n"
568 " -s, --dvdsubtitle \tchoose DVD subtitle channel\n"
570 " --novlans \tdisable vlans\n"
571 " --server <host> \tvideo server address\n"
572 " --port <port> \tvideo server port\n"
574 " -h, --help \tprint help and exit\n"
575 " -H, --longhelp \tprint long help and exit\n"
576 " -v, --version \toutput version information and exit\n" );
578 if( i_fashion == SHORT_HELP )
581 /* Interface parameters */
583 "Interface parameters:\n"
584 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
585 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
587 /* Audio parameters */
589 "Audio parameters:\n"
590 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
591 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
592 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
593 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
595 /* Video parameters */
597 "Video parameters:\n"
598 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
599 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
600 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
601 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
602 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
603 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
608 " " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type\n"
609 " " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel\n"
610 " " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel\n" );
612 /* Input parameters */
614 "Input parameters:\n"
615 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
616 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
617 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
618 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
619 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
622 /*****************************************************************************
623 * Version: print complete program version
624 *****************************************************************************
625 * Print complete program version and build number.
626 *****************************************************************************/
627 static void Version( void )
629 intf_Msg( VERSION_MESSAGE
630 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
631 "You may redistribute it under the terms of the GNU General Public License;\n"
632 "see the file named COPYING for details.\n"
633 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
637 /*****************************************************************************
638 * InitSignalHandler: system signal handler initialization
639 *****************************************************************************
640 * Set the signal handlers. SIGTERM is not intercepted, because we need at
641 * at least a method to kill the program when all other methods failed, and
642 * when we don't want to use SIGKILL.
643 *****************************************************************************/
644 static void InitSignalHandler( void )
646 /* Termination signals */
647 signal( SIGHUP, SignalHandler );
648 signal( SIGINT, SignalHandler );
649 signal( SIGQUIT, SignalHandler );
652 /*****************************************************************************
653 * SignalHandler: system signal handler
654 *****************************************************************************
655 * This function is called when a signal is received by the program. It tries to
656 * end the program in a clean way.
657 *****************************************************************************/
658 static void SignalHandler( int i_signal )
660 /* Once a signal has been trapped, the termination sequence will be armed and
661 * following signals will be ignored to avoid sending messages to an interface
662 * having been destroyed */
663 signal( SIGHUP, SIG_IGN );
664 signal( SIGINT, SIG_IGN );
665 signal( SIGQUIT, SIG_IGN );
667 /* Acknowledge the signal received */
668 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
670 /* Try to terminate everything - this is done by requesting the end of the
671 * interface thread */
672 p_main->p_intf->b_die = 1;
676 /*****************************************************************************
677 * TestMMX: tests if the processor has MMX support.
678 *****************************************************************************
679 * This function is called if HAVE_MMX is enabled, to check whether the
680 * CPU really supports MMX.
681 *****************************************************************************/
682 static int TestMMX( void )
684 /* FIXME: under beos, gcc does not support the following inline assembly */
689 int i_reg, i_dummy = 0;
691 /* test for a 386 CPU */
692 asm volatile ( "pushfl
701 andl $0x40000, %%eax"
707 /* test for a 486 CPU */
708 asm volatile ( "movl %%ecx, %%eax
709 xorl $0x200000, %%eax
717 andl $0x200000, %%eax"
723 /* the CPU supports the CPUID instruction - get its level */
724 asm volatile ( "cpuid"
729 : "a" ( 0 ), /* level 0 */
730 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
732 /* this shouldn't happen on a normal CPU */
736 /* test for the MMX flag */
737 asm volatile ( "cpuid
738 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
743 : "a" ( 1 ), /* level 1 */
744 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */