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 );
136 static int TestMMX ( void );
139 /*****************************************************************************
140 * main: parse command line, start interface and spawn threads
141 *****************************************************************************
142 * Steps during program execution are:
143 * -configuration parsing and messages interface initialization
144 * -openning of audio output device and some global modules
145 * -execution of interface, which exit on error or on user request
146 * -closing of audio output device and some global modules
147 * On error, the spawned threads are cancelled, and the open devices closed.
148 *****************************************************************************/
149 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
151 main_t main_data; /* root of all data - see main.h */
152 p_main = &main_data; /* set up the global variable */
155 * System specific initialization code
162 * Read configuration, initialize messages interface and set up program
167 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
171 p_main->p_msg = intf_MsgCreate();
172 if( !p_main->p_msg ) /* start messages interface */
174 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
178 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
183 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
186 * Initialize shared resources and libraries
188 if( main_data.b_vlans && input_VlanCreate() )
190 /* On error during vlans initialization, switch of vlans */
191 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
192 main_data.b_vlans = 0;
196 * Open audio device and start aout thread
198 if( main_data.b_audio )
200 main_data.p_aout = aout_CreateThread( NULL );
201 if( main_data.p_aout == NULL )
203 /* On error during audio initialization, switch of audio */
204 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
205 main_data.b_audio = 0;
212 main_data.p_intf = intf_Create();
213 if( main_data.p_intf != NULL )
215 InitSignalHandler(); /* prepare signals for interception */
216 intf_Run( main_data.p_intf );
217 intf_Destroy( main_data.p_intf );
223 if( main_data.b_audio )
225 aout_DestroyThread( main_data.p_aout, NULL );
229 * Free shared resources and libraries
231 if( main_data.b_vlans )
237 * System specific cleaning code
244 * Terminate messages interface and program
246 intf_Msg( "Program terminated.\n" );
251 /*****************************************************************************
252 * main_GetIntVariable: get the int value of an environment variable
253 *****************************************************************************
254 * This function is used to read some default parameters in modules.
255 *****************************************************************************/
256 int main_GetIntVariable( char *psz_name, int i_default )
258 char * psz_env; /* environment value */
259 char * psz_end; /* end of parsing index */
260 long int i_value; /* value */
262 psz_env = getenv( psz_name );
265 i_value = strtol( psz_env, &psz_end, 0 );
266 if( (*psz_env != '\0') && (*psz_end == '\0') )
274 /*****************************************************************************
275 * main_GetPszVariable: get the string value of an environment variable
276 *****************************************************************************
277 * This function is used to read some default parameters in modules.
278 *****************************************************************************/
279 char * main_GetPszVariable( char *psz_name, char *psz_default )
283 psz_env = getenv( psz_name );
288 return( psz_default );
291 /*****************************************************************************
292 * main_PutPszVariable: set the string value of an environment variable
293 *****************************************************************************
294 * This function is used to set some default parameters in modules. The use of
295 * this function will cause some memory leak: since some systems use the pointer
296 * passed to putenv to store the environment string, it can't be freed.
297 *****************************************************************************/
298 void main_PutPszVariable( char *psz_name, char *psz_value )
302 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
303 if( psz_env == NULL )
305 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
309 sprintf( psz_env, "%s=%s", psz_name, psz_value );
310 if( putenv( psz_env ) )
312 intf_ErrMsg( "error: %s\n", strerror(errno) );
317 /*****************************************************************************
318 * main_PutIntVariable: set the integer value of an environment variable
319 *****************************************************************************
320 * This function is used to set some default parameters in modules. The use of
321 * this function will cause some memory leak: since some systems use the pointer
322 * passed to putenv to store the environment string, it can't be freed.
323 *****************************************************************************/
324 void main_PutIntVariable( char *psz_name, int i_value )
326 char psz_value[ 256 ]; /* buffer for value */
328 sprintf( psz_value, "%d", i_value );
329 main_PutPszVariable( psz_name, psz_value );
332 /* following functions are local */
334 /*****************************************************************************
335 * SetDefaultConfiguration: set default options
336 *****************************************************************************
337 * This function is called by GetConfiguration before command line is parsed.
338 * It sets all the default values required later by the program. At this stage,
339 * most structure are not yet allocated, so initialization must be done using
341 *****************************************************************************/
342 static void SetDefaultConfiguration( void )
345 * All features are activated by default
352 /*****************************************************************************
353 * GetConfiguration: parse command line
354 *****************************************************************************
355 * Parse command line and configuration file for configuration. If the inline
356 * help is requested, the function Usage() is called and the function returns
357 * -1 (causing main() to exit). The messages interface is initialized at this
358 * stage, but most structures are not allocated, so only environment should
360 *****************************************************************************/
361 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
365 /* Set default configuration and copy arguments */
366 p_main->i_argc = i_argc;
367 p_main->ppsz_argv = ppsz_argv;
368 p_main->ppsz_env = ppsz_env;
369 SetDefaultConfiguration();
371 /* Parse command line options */
373 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
377 /* General/common options */
378 case 'h': /* -h, --help */
382 case 'H': /* -H, --longhelp */
386 case 'v': /* -v, --version */
392 case OPT_NOAUDIO: /* --noaudio */
395 case OPT_AOUT: /* --aout */
396 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
398 case OPT_STEREO: /* --stereo */
399 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
401 case OPT_MONO: /* --mono */
402 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
406 case OPT_NOVIDEO: /* --novideo */
409 case OPT_VOUT: /* --vout */
410 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
412 case OPT_DISPLAY: /* --display */
413 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
415 case OPT_WIDTH: /* --width */
416 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
418 case OPT_HEIGHT: /* --height */
419 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
422 case 'g': /* -g, --grayscale */
423 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
425 case OPT_COLOR: /* --color */
426 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
430 case OPT_NOVLANS: /* --novlans */
433 case OPT_SERVER: /* --server */
434 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
436 case OPT_PORT: /* --port */
437 main_PutPszVariable( INPUT_PORT_VAR, optarg );
440 /* Internal error: unknown option */
443 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
450 /* Parse command line parameters - no check is made for these options */
451 for( i_opt = optind; i_opt < i_argc; i_opt++ )
453 putenv( ppsz_argv[ i_opt ] );
458 /*****************************************************************************
459 * Usage: print program usage
460 *****************************************************************************
461 * Print a short inline help. Message interface is initialized at this stage.
462 *****************************************************************************/
463 static void Usage( int i_fashion )
466 intf_Msg( "Usage: vlc [options] [parameters]\n" );
468 if( i_fashion == USAGE )
470 intf_Msg( "Try `vlc --help' for more information.\n" );
474 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
479 " --noaudio \tdisable audio\n"
480 " --aout <plugin> \taudio output method\n"
481 " --stereo, --mono \tstereo/mono audio\n"
483 " --novideo \tdisable audio\n"
484 " --vout <plugin> \tvideo output method\n"
485 " --display <display> \tdisplay string\n"
486 " --width <w>, --height <h> \tdisplay dimensions\n"
487 " -g, --grayscale \tgrayscale output\n"
488 " --color \tcolor output\n"
490 " --novlans \tdisable vlans\n"
491 " --server <host> \tvideo server address\n"
492 " --port <port> \tvideo server port\n"
494 " -h, --help \tprint help and exit\n"
495 " -H, --longhelp \tprint long help and exit\n"
496 " -v, --version \toutput version information and exit\n" );
498 if( i_fashion == SHORT_HELP )
501 /* Interface parameters */
503 "Interface parameters:\n"
504 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
505 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
507 /* Audio parameters */
509 "Audio parameters:\n"
510 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
511 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
512 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
513 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
515 /* Video parameters */
517 "Video parameters:\n"
518 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
519 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
520 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
521 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
522 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
523 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
525 /* Input parameters */
527 "Input parameters:\n"
528 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
529 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
530 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
531 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
532 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
535 /*****************************************************************************
536 * Version: print complete program version
537 *****************************************************************************
538 * Print complete program version and build number.
539 *****************************************************************************/
540 static void Version( void )
542 intf_Msg( VERSION_MESSAGE
543 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
544 "You may redistribute it under the terms of the GNU General Public License;\n"
545 "see the file named COPYING for details.\n"
546 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
550 /*****************************************************************************
551 * InitSignalHandler: system signal handler initialization
552 *****************************************************************************
553 * Set the signal handlers. SIGTERM is not intercepted, because we need at
554 * at least a method to kill the program when all other methods failed, and
555 * when we don't want to use SIGKILL.
556 *****************************************************************************/
557 static void InitSignalHandler( void )
559 /* Termination signals */
560 signal( SIGHUP, SignalHandler );
561 signal( SIGINT, SignalHandler );
562 signal( SIGQUIT, SignalHandler );
565 /*****************************************************************************
566 * SignalHandler: system signal handler
567 *****************************************************************************
568 * This function is called when a signal is received by the program. It tries to
569 * end the program in a clean way.
570 *****************************************************************************/
571 static void SignalHandler( int i_signal )
573 /* Once a signal has been trapped, the termination sequence will be armed and
574 * following signals will be ignored to avoid sending messages to an interface
575 * having been destroyed */
576 signal( SIGHUP, SIG_IGN );
577 signal( SIGINT, SIG_IGN );
578 signal( SIGQUIT, SIG_IGN );
580 /* Acknowledge the signal received */
581 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
583 /* Try to terminate everything - this is done by requesting the end of the
584 * interface thread */
585 p_main->p_intf->b_die = 1;
589 /*****************************************************************************
590 * TestMMX: tests if the processor has MMX support.
591 *****************************************************************************
592 * This function is called if HAVE_MMX is enabled, to check whether the
593 * cpu really supports MMX.
594 *****************************************************************************/
595 static int TestMMX( void )
597 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
602 int i_reg, i_dummy = 0;
604 /* test for a 386 cpu */
605 asm volatile ( "pushfl
614 andl $0x40000, %%eax"
620 /* test for a 486 cpu */
621 asm volatile ( "movl %%ecx, %%eax
622 xorl $0x200000, %%eax
630 andl $0x200000, %%eax"
636 /* the cpu supports the CPUID instruction - get its level */
637 asm volatile ( "cpuid"
642 : "a" ( 0 ), /* level 0 */
643 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
645 /* this shouldn't happen on a normal cpu */
649 /* test for the MMX flag */
650 asm volatile ( "cpuid
651 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
656 : "a" ( 1 ), /* level 1 */
657 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */