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 GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
32 #include <getopt.h> /* getopt() */
33 #include <stdio.h> /* sprintf() */
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* getenv(), strtol(), */
37 #include <string.h> /* strerror() */
44 #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 { "novlans", 0, 0, OPT_NOVLANS },
112 { "server", 1, 0, OPT_SERVER },
113 { "port", 1, 0, OPT_PORT },
119 static const char *psz_shortopts = "hHvg";
121 /*****************************************************************************
122 * Global variable program_data - this is the one and only, see main.h
123 *****************************************************************************/
126 /*****************************************************************************
128 *****************************************************************************/
129 static void SetDefaultConfiguration ( void );
130 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
131 static void Usage ( int i_fashion );
132 static void Version ( void );
134 static void InitSignalHandler ( void );
135 static void SignalHandler ( int i_signal );
136 static int TestMMX ( void );
138 /*****************************************************************************
139 * main: parse command line, start interface and spawn threads
140 *****************************************************************************
141 * Steps during program execution are:
142 * -configuration parsing and messages interface initialization
143 * -openning of audio output device and some global modules
144 * -execution of interface, which exit on error or on user request
145 * -closing of audio output device and some global modules
146 * On error, the spawned threads are cancelled, and the open devices closed.
147 *****************************************************************************/
148 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
150 main_t main_data; /* root of all data - see main.h */
151 p_main = &main_data; /* set up the global variable */
154 * System specific initialization code
161 * Read configuration, initialize messages interface and set up program
166 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
170 p_main->p_msg = intf_MsgCreate();
171 if( !p_main->p_msg ) /* start messages interface */
173 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
177 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
182 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
185 * Initialize shared resources and libraries
187 if( main_data.b_vlans && input_VlanCreate() )
189 /* On error during vlans initialization, switch of vlans */
190 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
191 main_data.b_vlans = 0;
195 * Open audio device and start aout thread
197 if( main_data.b_audio )
199 main_data.p_aout = aout_CreateThread( NULL );
200 if( main_data.p_aout == NULL )
202 /* On error during audio initialization, switch of audio */
203 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
204 main_data.b_audio = 0;
211 main_data.p_intf = intf_Create();
212 if( main_data.p_intf != NULL )
214 InitSignalHandler(); /* prepare signals for interception */
215 intf_Run( main_data.p_intf );
216 intf_Destroy( main_data.p_intf );
222 if( main_data.b_audio )
224 aout_DestroyThread( main_data.p_aout, NULL );
228 * Free shared resources and libraries
230 if( main_data.b_vlans )
236 * System specific cleaning code
243 * Terminate messages interface and program
245 intf_Msg( "Program terminated.\n" );
250 /*****************************************************************************
251 * main_GetIntVariable: get the int value of an environment variable
252 *****************************************************************************
253 * This function is used to read some default parameters in modules.
254 *****************************************************************************/
255 int main_GetIntVariable( char *psz_name, int i_default )
257 char * psz_env; /* environment value */
258 char * psz_end; /* end of parsing index */
259 long int i_value; /* value */
261 psz_env = getenv( psz_name );
264 i_value = strtol( psz_env, &psz_end, 0 );
265 if( (*psz_env != '\0') && (*psz_end == '\0') )
273 /*****************************************************************************
274 * main_GetPszVariable: get the string value of an environment variable
275 *****************************************************************************
276 * This function is used to read some default parameters in modules.
277 *****************************************************************************/
278 char * main_GetPszVariable( char *psz_name, char *psz_default )
282 psz_env = getenv( psz_name );
287 return( psz_default );
290 /*****************************************************************************
291 * main_PutPszVariable: set the string value of an environment variable
292 *****************************************************************************
293 * This function is used to set some default parameters in modules. The use of
294 * this function will cause some memory leak: since some systems use the pointer
295 * passed to putenv to store the environment string, it can't be freed.
296 *****************************************************************************/
297 void main_PutPszVariable( char *psz_name, char *psz_value )
301 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
302 if( psz_env == NULL )
304 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
308 sprintf( psz_env, "%s=%s", psz_name, psz_value );
309 if( putenv( psz_env ) )
311 intf_ErrMsg( "error: %s\n", strerror(errno) );
316 /*****************************************************************************
317 * main_PutIntVariable: set the integer value of an environment variable
318 *****************************************************************************
319 * This function is used to set some default parameters in modules. The use of
320 * this function will cause some memory leak: since some systems use the pointer
321 * passed to putenv to store the environment string, it can't be freed.
322 *****************************************************************************/
323 void main_PutIntVariable( char *psz_name, int i_value )
325 char psz_value[ 256 ]; /* buffer for value */
327 sprintf( psz_value, "%d", i_value );
328 main_PutPszVariable( psz_name, psz_value );
331 /* following functions are local */
333 /*****************************************************************************
334 * SetDefaultConfiguration: set default options
335 *****************************************************************************
336 * This function is called by GetConfiguration before command line is parsed.
337 * It sets all the default values required later by the program. At this stage,
338 * most structure are not yet allocated, so initialization must be done using
340 *****************************************************************************/
341 static void SetDefaultConfiguration( void )
344 * All features are activated by default
351 /*****************************************************************************
352 * GetConfiguration: parse command line
353 *****************************************************************************
354 * Parse command line and configuration file for configuration. If the inline
355 * help is requested, the function Usage() is called and the function returns
356 * -1 (causing main() to exit). The messages interface is initialized at this
357 * stage, but most structures are not allocated, so only environment should
359 *****************************************************************************/
360 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
364 /* Set default configuration and copy arguments */
365 p_main->i_argc = i_argc;
366 p_main->ppsz_argv = ppsz_argv;
367 p_main->ppsz_env = ppsz_env;
368 SetDefaultConfiguration();
370 /* Parse command line options */
372 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
376 /* General/common options */
377 case 'h': /* -h, --help */
381 case 'H': /* -H, --longhelp */
385 case 'v': /* -v, --version */
391 case OPT_NOAUDIO: /* --noaudio */
394 case OPT_AOUT: /* --aout */
395 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
397 case OPT_STEREO: /* --stereo */
398 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
400 case OPT_MONO: /* --mono */
401 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
405 case OPT_NOVIDEO: /* --novideo */
408 case OPT_VOUT: /* --vout */
409 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
411 case OPT_DISPLAY: /* --display */
412 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
414 case OPT_WIDTH: /* --width */
415 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
417 case OPT_HEIGHT: /* --height */
418 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
421 case 'g': /* -g, --grayscale */
422 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
424 case OPT_COLOR: /* --color */
425 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
429 case OPT_NOVLANS: /* --novlans */
432 case OPT_SERVER: /* --server */
433 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
435 case OPT_PORT: /* --port */
436 main_PutPszVariable( INPUT_PORT_VAR, optarg );
439 /* Internal error: unknown option */
442 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
449 /* Parse command line parameters - no check is made for these options */
450 for( i_opt = optind; i_opt < i_argc; i_opt++ )
452 putenv( ppsz_argv[ i_opt ] );
457 /*****************************************************************************
458 * Usage: print program usage
459 *****************************************************************************
460 * Print a short inline help. Message interface is initialized at this stage.
461 *****************************************************************************/
462 static void Usage( int i_fashion )
465 intf_Msg( "Usage: vlc [options] [parameters]\n" );
467 if( i_fashion == USAGE )
469 intf_Msg( "Try `vlc --help' for more information.\n" );
473 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
478 " --noaudio \tdisable audio\n"
479 " --aout <plugin> \taudio output method\n"
480 " --stereo, --mono \tstereo/mono audio\n"
482 " --novideo \tdisable audio\n"
483 " --vout <plugin> \tvideo output method\n"
484 " --display <display> \tdisplay string\n"
485 " --width <w>, --height <h> \tdisplay dimensions\n"
486 " -g, --grayscale \tgrayscale output\n"
487 " --color \tcolor output\n"
489 " --novlans \tdisable vlans\n"
490 " --server <host> \tvideo server address\n"
491 " --port <port> \tvideo server port\n"
493 " -h, --help \tprint help and exit\n"
494 " -H, --longhelp \tprint long help and exit\n"
495 " -v, --version \toutput version information and exit\n" );
497 if( i_fashion == SHORT_HELP )
500 /* Interface parameters */
502 "Interface parameters:\n"
503 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
504 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
506 /* Audio parameters */
508 "Audio parameters:\n"
509 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
510 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
511 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
512 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
514 /* Video parameters */
516 "Video parameters:\n"
517 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
518 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
519 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
520 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
521 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
522 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
524 /* Input parameters */
526 "Input parameters:\n"
527 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
528 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
529 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
530 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
531 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
534 /*****************************************************************************
535 * Version: print complete program version
536 *****************************************************************************
537 * Print complete program version and build number.
538 *****************************************************************************/
539 static void Version( void )
541 intf_Msg( VERSION_MESSAGE
542 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
543 "You may redistribute it under the terms of the GNU General Public License;\n"
544 "see the file named COPYING for details.\n"
545 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
549 /*****************************************************************************
550 * InitSignalHandler: system signal handler initialization
551 *****************************************************************************
552 * Set the signal handlers. SIGTERM is not intercepted, because we need at
553 * at least a method to kill the program when all other methods failed, and
554 * when we don't want to use SIGKILL.
555 *****************************************************************************/
556 static void InitSignalHandler( void )
558 /* Termination signals */
559 signal( SIGHUP, SignalHandler );
560 signal( SIGINT, SignalHandler );
561 signal( SIGQUIT, SignalHandler );
564 /*****************************************************************************
565 * SignalHandler: system signal handler
566 *****************************************************************************
567 * This function is called when a signal is received by the program. It tries to
568 * end the program in a clean way.
569 *****************************************************************************/
570 static void SignalHandler( int i_signal )
572 /* Once a signal has been trapped, the termination sequence will be armed and
573 * following signals will be ignored to avoid sending messages to an interface
574 * having been destroyed */
575 signal( SIGHUP, SIG_IGN );
576 signal( SIGINT, SIG_IGN );
577 signal( SIGQUIT, SIG_IGN );
579 /* Acknowledge the signal received */
580 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
582 /* Try to terminate everything - this is done by requesting the end of the
583 * interface thread */
584 p_main->p_intf->b_die = 1;
588 /*****************************************************************************
589 * TestMMX: tests if the processor has MMX support.
590 *****************************************************************************
591 * This function is called if HAVE_MMX is enabled, to check whether the
592 * cpu really supports MMX.
593 *****************************************************************************/
594 static int TestMMX( void )
596 /* FIXME: under beos, gcc does not support the foolowing inline assembly */
601 int i_reg, i_dummy = 0;
603 /* test for a 386 cpu */
604 asm volatile ( "pushfl
613 andl $0x40000, %%eax"
619 /* test for a 486 cpu */
620 asm volatile ( "movl %%ecx, %%eax
621 xorl $0x200000, %%eax
629 andl $0x200000, %%eax"
635 /* the cpu supports the CPUID instruction - get its level */
636 asm volatile ( "cpuid"
641 : "a" ( 0 ), /* level 0 */
642 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
644 /* this shouldn't happen on a normal cpu */
648 /* test for the MMX flag */
649 asm volatile ( "cpuid
650 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
655 : "a" ( 1 ), /* level 1 */
656 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */