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 *****************************************************************************/
29 #include <getopt.h> /* getopt() */
30 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
31 #include <stdio.h> /* sprintf() */
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* getenv(), strtol(), */
35 #include <string.h> /* strerror() */
41 #include "input_vlan.h"
44 #include "interface.h"
46 #include "audio_output.h"
50 /*****************************************************************************
51 * Command line options constants. If something is changed here, be sure that
52 * GetConfiguration and Usage are also changed.
53 *****************************************************************************/
55 /* Long options return values - note that values corresponding to short options
56 * chars, and in general any regular char, should be avoided */
57 #define OPT_NOAUDIO 150
59 #define OPT_STEREO 152
62 #define OPT_NOVIDEO 160
64 #define OPT_DISPLAY 162
66 #define OPT_HEIGHT 164
69 #define OPT_NOVLANS 170
70 #define OPT_SERVER 171
79 static const struct option longopts[] =
81 /* name, has_arg, flag, val */
83 /* General/common options */
84 { "help", 0, 0, 'h' },
85 { "longhelp", 0, 0, 'H' },
86 { "version", 0, 0, 'v' },
89 { "noaudio", 0, 0, OPT_NOAUDIO },
90 { "aout", 1, 0, OPT_AOUT },
91 { "stereo", 0, 0, OPT_STEREO },
92 { "mono", 0, 0, OPT_MONO },
95 { "novideo", 0, 0, OPT_NOVIDEO },
96 { "vout", 1, 0, OPT_VOUT },
97 { "display", 1, 0, OPT_DISPLAY },
98 { "width", 1, 0, OPT_WIDTH },
99 { "height", 1, 0, OPT_HEIGHT },
100 { "grayscale", 0, 0, 'g' },
101 { "color", 0, 0, OPT_COLOR },
104 { "novlans", 0, 0, OPT_NOVLANS },
105 { "server", 1, 0, OPT_SERVER },
106 { "port", 1, 0, OPT_PORT },
112 static const char *psz_shortopts = "hHvg";
114 /*****************************************************************************
115 * Global variable program_data - this is the one and only, see main.h
116 *****************************************************************************/
119 /*****************************************************************************
121 *****************************************************************************/
122 static void SetDefaultConfiguration ( void );
123 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
124 static void Usage ( int i_fashion );
125 static void Version ( void );
127 static void InitSignalHandler ( void );
128 static void SignalHandler ( int i_signal );
129 static int TestMMX ( void );
131 /*****************************************************************************
132 * main: parse command line, start interface and spawn threads
133 *****************************************************************************
134 * Steps during program execution are:
135 * -configuration parsing and messages interface initialization
136 * -openning of audio output device and some global modules
137 * -execution of interface, which exit on error or on user request
138 * -closing of audio output device and some global modules
139 * On error, the spawned threads are cancelled, and the open devices closed.
140 *****************************************************************************/
141 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
143 main_t main_data; /* root of all data - see main.h */
144 p_main = &main_data; /* set up the global variable */
147 * Read configuration, initialize messages interface and set up program
152 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
156 p_main->p_msg = intf_MsgCreate();
157 if( !p_main->p_msg ) /* start messages interface */
159 fprintf( stderr, "critical error: can't initialize messages interface (%s)\n",
163 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
168 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
171 * Initialize shared resources and libraries
173 if( main_data.b_vlans && input_VlanCreate() )
175 /* On error during vlans initialization, switch of vlans */
176 intf_Msg( "Virtual LANs initialization failed : vlans management is deactivated\n" );
177 main_data.b_vlans = 0;
181 * Open audio device and start aout thread
183 if( main_data.b_audio )
185 main_data.p_aout = aout_CreateThread( NULL );
186 if( main_data.p_aout == NULL )
188 /* On error during audio initialization, switch of audio */
189 intf_Msg( "Audio initialization failed : audio is deactivated\n" );
190 main_data.b_audio = 0;
197 main_data.p_intf = intf_Create();
198 if( main_data.p_intf != NULL )
200 InitSignalHandler(); /* prepare signals for interception */
201 intf_Run( main_data.p_intf );
202 intf_Destroy( main_data.p_intf );
208 if( main_data.b_audio )
210 aout_DestroyThread( main_data.p_aout, NULL );
214 * Free shared resources and libraries
216 if( main_data.b_vlans )
222 * Terminate messages interface and program
224 intf_Msg( "Program terminated.\n" );
229 /*****************************************************************************
230 * main_GetIntVariable: get the int value of an environment variable
231 *****************************************************************************
232 * This function is used to read some default parameters in modules.
233 *****************************************************************************/
234 int main_GetIntVariable( char *psz_name, int i_default )
236 char * psz_env; /* environment value */
237 char * psz_end; /* end of parsing index */
238 long int i_value; /* value */
240 psz_env = getenv( psz_name );
243 i_value = strtol( psz_env, &psz_end, 0 );
244 if( (*psz_env != '\0') && (*psz_end == '\0') )
252 /*****************************************************************************
253 * main_GetPszVariable: get the string value of an environment variable
254 *****************************************************************************
255 * This function is used to read some default parameters in modules.
256 *****************************************************************************/
257 char * main_GetPszVariable( char *psz_name, char *psz_default )
261 psz_env = getenv( psz_name );
266 return( psz_default );
269 /*****************************************************************************
270 * main_PutPszVariable: set the string value of an environment variable
271 *****************************************************************************
272 * This function is used to set some default parameters in modules. The use of
273 * this function will cause some memory leak: since some systems use the pointer
274 * passed to putenv to store the environment string, it can't be freed.
275 *****************************************************************************/
276 void main_PutPszVariable( char *psz_name, char *psz_value )
280 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
281 if( psz_env == NULL )
283 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
287 sprintf( psz_env, "%s=%s", psz_name, psz_value );
288 if( putenv( psz_env ) )
290 intf_ErrMsg( "error: %s\n", strerror(errno) );
295 /*****************************************************************************
296 * main_PutIntVariable: set the integer value of an environment variable
297 *****************************************************************************
298 * This function is used to set some default parameters in modules. The use of
299 * this function will cause some memory leak: since some systems use the pointer
300 * passed to putenv to store the environment string, it can't be freed.
301 *****************************************************************************/
302 void main_PutIntVariable( char *psz_name, int i_value )
304 char psz_value[ 256 ]; /* buffer for value */
306 sprintf( psz_value, "%d", i_value );
307 main_PutPszVariable( psz_name, psz_value );
310 /* following functions are local */
312 /*****************************************************************************
313 * SetDefaultConfiguration: set default options
314 *****************************************************************************
315 * This function is called by GetConfiguration before command line is parsed.
316 * It sets all the default values required later by the program. At this stage,
317 * most structure are not yet allocated, so initialization must be done using
319 *****************************************************************************/
320 static void SetDefaultConfiguration( void )
323 * All features are activated by default
330 /*****************************************************************************
331 * GetConfiguration: parse command line
332 *****************************************************************************
333 * Parse command line and configuration file for configuration. If the inline
334 * help is requested, the function Usage() is called and the function returns
335 * -1 (causing main() to exit). The messages interface is initialized at this
336 * stage, but most structures are not allocated, so only environment should
338 *****************************************************************************/
339 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
343 /* Set default configuration and copy arguments */
344 p_main->i_argc = i_argc;
345 p_main->ppsz_argv = ppsz_argv;
346 p_main->ppsz_env = ppsz_env;
347 SetDefaultConfiguration();
349 /* Parse command line options */
351 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
355 /* General/common options */
356 case 'h': /* -h, --help */
360 case 'H': /* -H, --longhelp */
364 case 'v': /* -v, --version */
370 case OPT_NOAUDIO: /* --noaudio */
373 case OPT_AOUT: /* --aout */
374 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
376 case OPT_STEREO: /* --stereo */
377 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
379 case OPT_MONO: /* --mono */
380 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
384 case OPT_NOVIDEO: /* --novideo */
387 case OPT_VOUT: /* --vout */
388 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
390 case OPT_DISPLAY: /* --display */
391 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
393 case OPT_WIDTH: /* --width */
394 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
396 case OPT_HEIGHT: /* --height */
397 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
400 case 'g': /* -g, --grayscale */
401 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
403 case OPT_COLOR: /* --color */
404 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
408 case OPT_NOVLANS: /* --novlans */
411 case OPT_SERVER: /* --server */
412 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
414 case OPT_PORT: /* --port */
415 main_PutPszVariable( INPUT_PORT_VAR, optarg );
418 /* Internal error: unknown option */
421 intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
428 /* Parse command line parameters - no check is made for these options */
429 for( i_opt = optind; i_opt < i_argc; i_opt++ )
431 putenv( ppsz_argv[ i_opt ] );
436 /*****************************************************************************
437 * Usage: print program usage
438 *****************************************************************************
439 * Print a short inline help. Message interface is initialized at this stage.
440 *****************************************************************************/
441 static void Usage( int i_fashion )
444 intf_Msg( "Usage: vlc [options] [parameters]\n" );
446 if( i_fashion == USAGE )
448 intf_Msg( "Try `vlc --help' for more information.\n" );
452 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
457 " --noaudio \tdisable audio\n"
458 " --aout <plugin> \taudio output method\n"
459 " --stereo, --mono \tstereo/mono audio\n"
461 " --novideo \tdisable audio\n"
462 " --vout <plugin> \tvideo output method\n"
463 " --display <display> \tdisplay string\n"
464 " --width <w>, --height <h> \tdisplay dimensions\n"
465 " -g, --grayscale \tgrayscale output\n"
466 " --color \tcolor output\n"
468 " --novlans \tdisable vlans\n"
469 " --server <host> \tvideo server address\n"
470 " --port <port> \tvideo server port\n"
472 " -h, --help \tprint help and exit\n"
473 " -H, --longhelp \tprint long help and exit\n"
474 " -v, --version \toutput version information and exit\n" );
476 if( i_fashion == SHORT_HELP )
479 /* Interface parameters */
481 "Interface parameters:\n"
482 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n"
483 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n" );
485 /* Audio parameters */
487 "Audio parameters:\n"
488 " " AOUT_METHOD_VAR "=<method name> \taudio method\n"
489 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n"
490 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n"
491 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" );
493 /* Video parameters */
495 "Video parameters:\n"
496 " " VOUT_METHOD_VAR "=<method name> \tdisplay method\n"
497 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n"
498 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n"
499 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n"
500 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n"
501 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" );
503 /* Input parameters */
505 "Input parameters:\n"
506 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n"
507 " " INPUT_PORT_VAR "=<port> \tvideo server port\n"
508 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"
509 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n"
510 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" );
513 /*****************************************************************************
514 * Version: print complete program version
515 *****************************************************************************
516 * Print complete program version and build number.
517 *****************************************************************************/
518 static void Version( void )
520 intf_Msg( "vlc " PROGRAM_VERSION " " PROGRAM_CODENAME
521 " (" PROGRAM_BUILD ") (" PROGRAM_OPTIONS ")\n"
522 "Copyright 1996-2000 VideoLAN\n"
523 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
524 "You may redistribute it under the terms of the GNU General Public License;\n"
525 "see the file named COPYING for details.\n"
526 "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
530 /*****************************************************************************
531 * InitSignalHandler: system signal handler initialization
532 *****************************************************************************
533 * Set the signal handlers. SIGTERM is not intercepted, because we need at
534 * at least a method to kill the program when all other methods failed, and
535 * when we don't want to use SIGKILL.
536 *****************************************************************************/
537 static void InitSignalHandler( void )
539 /* Termination signals */
540 signal( SIGHUP, SignalHandler );
541 signal( SIGINT, SignalHandler );
542 signal( SIGQUIT, SignalHandler );
545 /*****************************************************************************
546 * SignalHandler: system signal handler
547 *****************************************************************************
548 * This function is called when a signal is received by the program. It tries to
549 * end the program in a clean way.
550 *****************************************************************************/
551 static void SignalHandler( int i_signal )
553 /* Once a signal has been trapped, the termination sequence will be armed and
554 * following signals will be ignored to avoid sending messages to an interface
555 * having been destroyed */
556 signal( SIGHUP, SIG_IGN );
557 signal( SIGINT, SIG_IGN );
558 signal( SIGQUIT, SIG_IGN );
560 /* Acknowledge the signal received */
561 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
563 /* Try to terminate everything - this is done by requesting the end of the
564 * interface thread */
565 p_main->p_intf->b_die = 1;
569 /*****************************************************************************
570 * TestMMX: tests if the processor has MMX support.
571 *****************************************************************************
572 * This function is called if HAVE_MMX is enabled, to check whether the
573 * cpu really supports MMX.
574 *****************************************************************************/
575 static int TestMMX( void )
577 int i_reg, i_dummy = 0;
579 /* test for a 386 cpu */
580 asm volatile ( "pushfl
589 andl $0x40000, %%eax"
595 /* test for a 486 cpu */
596 asm volatile ( "movl %%ecx, %%eax
597 xorl $0x200000, %%eax
605 andl $0x200000, %%eax"
611 /* the cpu supports the CPUID instruction - get its level */
612 asm volatile ( "cpuid"
617 : "a" ( 0 ), /* level 0 */
618 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
620 /* this shouldn't happen on a normal cpu */
624 /* test for the MMX flag */
625 asm volatile ( "cpuid
626 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
631 : "a" ( 1 ), /* level 1 */
632 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */