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];
210 p_playlist = malloc( sizeof(int) );
211 p_playlist[ 0 ] = "-";
215 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
218 * Initialize shared resources and libraries
220 if( main_data.b_vlans && input_VlanCreate() )
222 /* On error during vlans initialization, switch of vlans */
223 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
224 main_data.b_vlans = 0;
228 * Open audio device and start aout thread
230 if( main_data.b_audio )
232 main_data.p_aout = aout_CreateThread( NULL );
233 if( main_data.p_aout == NULL )
235 /* On error during audio initialization, switch of audio */
236 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
237 main_data.b_audio = 0;
244 main_data.p_intf = intf_Create();
245 if( main_data.p_intf != NULL )
247 main_data.p_intf->p_playlist = p_playlist;
248 main_data.p_intf->i_list_index = i_list_index;
250 InitSignalHandler(); /* prepare signals for interception */
252 intf_Run( main_data.p_intf );
253 intf_Destroy( main_data.p_intf );
259 if( main_data.b_audio )
261 aout_DestroyThread( main_data.p_aout, NULL );
265 * Free shared resources and libraries
267 if( main_data.b_vlans )
273 * System specific cleaning code
280 * Terminate messages interface and program
282 intf_Msg( "Program terminated.\n" );
287 /*****************************************************************************
288 * main_GetIntVariable: get the int value of an environment variable
289 *****************************************************************************
290 * This function is used to read some default parameters in modules.
291 *****************************************************************************/
292 int main_GetIntVariable( char *psz_name, int i_default )
294 char * psz_env; /* environment value */
295 char * psz_end; /* end of parsing index */
296 long int i_value; /* value */
298 psz_env = getenv( psz_name );
301 i_value = strtol( psz_env, &psz_end, 0 );
302 if( (*psz_env != '\0') && (*psz_end == '\0') )
310 /*****************************************************************************
311 * main_GetPszVariable: get the string value of an environment variable
312 *****************************************************************************
313 * This function is used to read some default parameters in modules.
314 *****************************************************************************/
315 char * main_GetPszVariable( char *psz_name, char *psz_default )
319 psz_env = getenv( psz_name );
324 return( psz_default );
327 /*****************************************************************************
328 * main_PutPszVariable: set the string value of an environment variable
329 *****************************************************************************
330 * This function is used to set some default parameters in modules. The use of
331 * this function will cause some memory leak: since some systems use the pointer
332 * passed to putenv to store the environment string, it can't be freed.
333 *****************************************************************************/
334 void main_PutPszVariable( char *psz_name, char *psz_value )
338 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
339 if( psz_env == NULL )
341 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
345 sprintf( psz_env, "%s=%s", psz_name, psz_value );
346 if( putenv( psz_env ) )
348 intf_ErrMsg( "error: %s\n", strerror(errno) );
353 /*****************************************************************************
354 * main_PutIntVariable: set the integer value of an environment variable
355 *****************************************************************************
356 * This function is used to set some default parameters in modules. The use of
357 * this function will cause some memory leak: since some systems use the pointer
358 * passed to putenv to store the environment string, it can't be freed.
359 *****************************************************************************/
360 void main_PutIntVariable( char *psz_name, int i_value )
362 char psz_value[ 256 ]; /* buffer for value */
364 sprintf( psz_value, "%d", i_value );
365 main_PutPszVariable( psz_name, psz_value );
368 /* following functions are local */
370 /*****************************************************************************
371 * SetDefaultConfiguration: set default options
372 *****************************************************************************
373 * This function is called by GetConfiguration before command line is parsed.
374 * It sets all the default values required later by the program. At this stage,
375 * most structure are not yet allocated, so initialization must be done using
377 *****************************************************************************/
378 static void SetDefaultConfiguration( void )
381 * All features are activated by default
388 /*****************************************************************************
389 * GetConfiguration: parse command line
390 *****************************************************************************
391 * Parse command line and configuration file for configuration. If the inline
392 * help is requested, the function Usage() is called and the function returns
393 * -1 (causing main() to exit). The messages interface is initialized at this
394 * stage, but most structures are not allocated, so only environment should
396 *****************************************************************************/
397 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
401 /* Set default configuration and copy arguments */
402 p_main->i_argc = i_argc;
403 p_main->ppsz_argv = ppsz_argv;
404 p_main->ppsz_env = ppsz_env;
405 SetDefaultConfiguration();
407 /* Parse command line options */
409 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
413 /* General/common options */
414 case 'h': /* -h, --help */
418 case 'H': /* -H, --longhelp */
422 case 'v': /* -v, --version */
428 case OPT_NOAUDIO: /* --noaudio */
431 case OPT_AOUT: /* --aout */
432 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
434 case OPT_STEREO: /* --stereo */
435 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
437 case OPT_MONO: /* --mono */
438 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
442 case OPT_NOVIDEO: /* --novideo */
445 case OPT_VOUT: /* --vout */
446 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
448 case OPT_DISPLAY: /* --display */
449 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
451 case OPT_WIDTH: /* --width */
452 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
454 case OPT_HEIGHT: /* --height */
455 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
458 case 'g': /* -g, --grayscale */
459 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
461 case OPT_COLOR: /* --color */
462 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
467 if ( ! strcmp(optarg, "mpeg") )
468 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
469 else if ( ! strcmp(optarg, "lpcm") )
470 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
471 else if ( ! strcmp(optarg, "off") )
472 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
474 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
477 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
480 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
484 case OPT_NOVLANS: /* --novlans */
487 case OPT_SERVER: /* --server */
488 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
490 case OPT_PORT: /* --port */
491 main_PutPszVariable( INPUT_PORT_VAR, optarg );
494 /* Internal error: unknown option */
497 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
504 /* Parse command line parameters - no check is made for these options */
505 for( i_opt = optind; i_opt < i_argc; i_opt++ )
507 putenv( ppsz_argv[ i_opt ] );
512 /*****************************************************************************
513 * Usage: print program usage
514 *****************************************************************************
515 * Print a short inline help. Message interface is initialized at this stage.
516 *****************************************************************************/
517 static void Usage( int i_fashion )
520 intf_Msg( "Usage: vlc [options] [parameters]\n" );
522 if( i_fashion == USAGE )
524 intf_Msg( "Try `vlc --help' for more information.\n" );
528 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
533 " --noaudio \tdisable audio\n"
534 " --aout <plugin> \taudio output method\n"
535 " --stereo, --mono \tstereo/mono audio\n"
537 " --novideo \tdisable video\n"
538 " --vout <plugin> \tvideo output method\n"
539 " --display <display> \tdisplay string\n"
540 " --width <w>, --height <h> \tdisplay dimensions\n"
541 " -g, --grayscale \tgrayscale output\n"
542 " --color \tcolor output\n"
544 " -a, --dvdaudio \tchoose DVD audio type\n"
545 " -c, --dvdchannel \tchoose DVD audio channel\n"
546 " -s, --dvdsubtitle \tchoose DVD subtitle channel\n"
548 " --novlans \tdisable vlans\n"
549 " --server <host> \tvideo server address\n"
550 " --port <port> \tvideo server port\n"
552 " -h, --help \tprint help and exit\n"
553 " -H, --longhelp \tprint long help and exit\n"
554 " -v, --version \toutput version information and exit\n" );
556 if( i_fashion == SHORT_HELP )
559 /* Interface parameters */
561 "Interface parameters:\n"
562 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
563 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
565 /* Audio parameters */
567 "Audio parameters:\n"
568 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
569 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
570 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
571 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
573 /* Video parameters */
575 "Video parameters:\n"
576 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
577 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
578 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
579 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
580 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
581 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
586 " " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type\n"
587 " " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel\n"
588 " " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel\n" );
590 /* Input parameters */
592 "Input parameters:\n"
593 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
594 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
595 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
596 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
597 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
600 /*****************************************************************************
601 * Version: print complete program version
602 *****************************************************************************
603 * Print complete program version and build number.
604 *****************************************************************************/
605 static void Version( void )
607 intf_Msg( VERSION_MESSAGE
608 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
609 "You may redistribute it under the terms of the GNU General Public License;\n"
610 "see the file named COPYING for details.\n"
611 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
615 /*****************************************************************************
616 * InitSignalHandler: system signal handler initialization
617 *****************************************************************************
618 * Set the signal handlers. SIGTERM is not intercepted, because we need at
619 * at least a method to kill the program when all other methods failed, and
620 * when we don't want to use SIGKILL.
621 *****************************************************************************/
622 static void InitSignalHandler( void )
624 /* Termination signals */
625 signal( SIGHUP, SignalHandler );
626 signal( SIGINT, SignalHandler );
627 signal( SIGQUIT, SignalHandler );
630 /*****************************************************************************
631 * SignalHandler: system signal handler
632 *****************************************************************************
633 * This function is called when a signal is received by the program. It tries to
634 * end the program in a clean way.
635 *****************************************************************************/
636 static void SignalHandler( int i_signal )
638 /* Once a signal has been trapped, the termination sequence will be armed and
639 * following signals will be ignored to avoid sending messages to an interface
640 * having been destroyed */
641 signal( SIGHUP, SIG_IGN );
642 signal( SIGINT, SIG_IGN );
643 signal( SIGQUIT, SIG_IGN );
645 /* Acknowledge the signal received */
646 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
648 /* Try to terminate everything - this is done by requesting the end of the
649 * interface thread */
650 p_main->p_intf->b_die = 1;
654 /*****************************************************************************
655 * TestMMX: tests if the processor has MMX support.
656 *****************************************************************************
657 * This function is called if HAVE_MMX is enabled, to check whether the
658 * cpu really supports MMX.
659 *****************************************************************************/
660 static int TestMMX( void )
662 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
667 int i_reg, i_dummy = 0;
669 /* test for a 386 cpu */
670 asm volatile ( "pushfl
679 andl $0x40000, %%eax"
685 /* test for a 486 cpu */
686 asm volatile ( "movl %%ecx, %%eax
687 xorl $0x200000, %%eax
695 andl $0x200000, %%eax"
701 /* the cpu supports the CPUID instruction - get its level */
702 asm volatile ( "cpuid"
707 : "a" ( 0 ), /* level 0 */
708 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
710 /* this shouldn't happen on a normal cpu */
714 /* test for the MMX flag */
715 asm volatile ( "cpuid
716 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
721 : "a" ( 1 ), /* level 1 */
722 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */