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
78 static const struct option longopts[] =
80 /* name, has_arg, flag, val */
82 /* General/common options */
83 { "help", 0, 0, 'h' },
84 { "longhelp", 0, 0, 'H' },
85 { "version", 0, 0, 'v' },
88 { "noaudio", 0, 0, OPT_NOAUDIO },
89 { "aout", 1, 0, OPT_AOUT },
90 { "stereo", 0, 0, OPT_STEREO },
91 { "mono", 0, 0, OPT_MONO },
94 { "novideo", 0, 0, OPT_NOVIDEO },
95 { "vout", 1, 0, OPT_VOUT },
96 { "display", 1, 0, OPT_DISPLAY },
97 { "width", 1, 0, OPT_WIDTH },
98 { "height", 1, 0, OPT_HEIGHT },
99 { "grayscale", 0, 0, 'g' },
100 { "color", 0, 0, OPT_COLOR },
103 { "novlans", 0, 0, OPT_NOVLANS },
104 { "server", 1, 0, OPT_SERVER },
105 { "port", 1, 0, OPT_PORT },
111 static const char *psz_shortopts = "hHvg";
113 /*****************************************************************************
114 * Global variable program_data - this is the one and only, see main.h
115 *****************************************************************************/
118 /*****************************************************************************
120 *****************************************************************************/
121 static void SetDefaultConfiguration ( void );
122 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
123 static void Usage ( int i_fashion );
124 static void Version ( void );
126 static void InitSignalHandler ( void );
127 static void SignalHandler ( int i_signal );
128 static int TestMMX ( void );
130 /*****************************************************************************
131 * main: parse command line, start interface and spawn threads
132 *****************************************************************************
133 * Steps during program execution are:
134 * -configuration parsing and messages interface initialization
135 * -openning of audio output device and some global modules
136 * -execution of interface, which exit on error or on user request
137 * -closing of audio output device and some global modules
138 * On error, the spawned threads are cancelled, and the open devices closed.
139 *****************************************************************************/
140 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
142 main_t main_data; /* root of all data - see main.h */
143 p_main = &main_data; /* set up the global variable */
146 * Read configuration, initialize messages interface and set up program
151 fprintf( stderr, "Sorry, this program needs an MMX processor. Please run the non-MMX version.\n" );
155 p_main->p_msg = intf_MsgCreate();
156 if( !p_main->p_msg ) /* start messages interface */
158 fprintf(stderr, "critical error: can't initialize messages interface (%s)\n",
162 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
167 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
170 * Initialize shared resources and libraries
172 if( main_data.b_vlans && input_VlanCreate() )
174 /* On error during vlans initialization, switch of vlans */
175 intf_Msg("Virtual LANs initialization failed : vlans management is deactivated\n");
176 main_data.b_vlans = 0;
180 * Open audio device and start aout thread
182 if( main_data.b_audio )
184 main_data.p_aout = aout_CreateThread( NULL );
185 if( main_data.p_aout == NULL )
187 /* On error during audio initialization, switch of audio */
188 intf_Msg("Audio initialization failed : audio is deactivated\n");
189 main_data.b_audio = 0;
196 main_data.p_intf = intf_Create();
197 if( main_data.p_intf != NULL )
199 InitSignalHandler(); /* prepare signals for interception */
200 intf_Run( main_data.p_intf );
201 intf_Destroy( main_data.p_intf );
207 if( main_data.b_audio )
209 aout_DestroyThread( main_data.p_aout, NULL );
213 * Free shared resources and libraries
215 if( main_data.b_vlans )
221 * Terminate messages interface and program
223 intf_Msg( "Program terminated.\n" );
228 /*****************************************************************************
229 * main_GetIntVariable: get the int value of an environment variable
230 *****************************************************************************
231 * This function is used to read some default parameters in modules.
232 *****************************************************************************/
233 int main_GetIntVariable( char *psz_name, int i_default )
235 char * psz_env; /* environment value */
236 char * psz_end; /* end of parsing index */
237 long int i_value; /* value */
239 psz_env = getenv( psz_name );
242 i_value = strtol( psz_env, &psz_end, 0 );
243 if( (*psz_env != '\0') && (*psz_end == '\0') )
251 /*****************************************************************************
252 * main_GetPszVariable: get the string value of an environment variable
253 *****************************************************************************
254 * This function is used to read some default parameters in modules.
255 *****************************************************************************/
256 char * main_GetPszVariable( char *psz_name, char *psz_default )
260 psz_env = getenv( psz_name );
265 return( psz_default );
268 /*****************************************************************************
269 * main_PutPszVariable: set the string value of an environment variable
270 *****************************************************************************
271 * This function is used to set some default parameters in modules. The use of
272 * this function will cause some memory leak: since some systems use the pointer
273 * passed to putenv to store the environment string, it can't be freed.
274 *****************************************************************************/
275 void main_PutPszVariable( char *psz_name, char *psz_value )
279 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
280 if( psz_env == NULL )
282 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
286 sprintf( psz_env, "%s=%s", psz_name, psz_value );
287 if( putenv( psz_env ) )
289 intf_ErrMsg("error: %s\n", strerror(errno));
294 /*****************************************************************************
295 * main_PutIntVariable: set the integer value of an environment variable
296 *****************************************************************************
297 * This function is used to set some default parameters in modules. The use of
298 * this function will cause some memory leak: since some systems use the pointer
299 * passed to putenv to store the environment string, it can't be freed.
300 *****************************************************************************/
301 void main_PutIntVariable( char *psz_name, int i_value )
303 char psz_value[ 256 ]; /* buffer for value */
305 sprintf(psz_value, "%d", i_value );
306 main_PutPszVariable( psz_name, psz_value );
309 /* following functions are local */
311 /*****************************************************************************
312 * SetDefaultConfiguration: set default options
313 *****************************************************************************
314 * This function is called by GetConfiguration before command line is parsed.
315 * It sets all the default values required later by the program. At this stage,
316 * most structure are not yet allocated, so initialization must be done using
318 *****************************************************************************/
319 static void SetDefaultConfiguration( void )
322 * All features are activated by default
329 /*****************************************************************************
330 * GetConfiguration: parse command line
331 *****************************************************************************
332 * Parse command line and configuration file for configuration. If the inline
333 * help is requested, the function Usage() is called and the function returns
334 * -1 (causing main() to exit). The messages interface is initialized at this
335 * stage, but most structures are not allocated, so only environment should
337 *****************************************************************************/
338 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
342 /* Set default configuration and copy arguments */
343 p_main->i_argc = i_argc;
344 p_main->ppsz_argv = ppsz_argv;
345 p_main->ppsz_env = ppsz_env;
346 SetDefaultConfiguration();
348 /* Parse command line options */
350 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
354 /* General/common options */
355 case 'h': /* -h, --help */
359 case 'H': /* -H, --longhelp */
363 case 'v': /* -v, --version */
369 case OPT_NOAUDIO: /* --noaudio */
372 case OPT_AOUT: /* --aout */
373 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
375 case OPT_STEREO: /* --stereo */
376 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
378 case OPT_MONO: /* --mono */
379 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
383 case OPT_NOVIDEO: /* --novideo */
386 case OPT_VOUT: /* --vout */
387 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
389 case OPT_DISPLAY: /* --display */
390 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
392 case OPT_WIDTH: /* --width */
393 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
395 case OPT_HEIGHT: /* --height */
396 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
399 case 'g': /* -g, --grayscale */
400 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
402 case OPT_COLOR: /* --color */
403 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
407 case OPT_NOVLANS: /* --novlans */
410 case OPT_SERVER: /* --server */
411 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
413 case OPT_PORT: /* --port */
414 main_PutPszVariable( INPUT_PORT_VAR, optarg );
417 /* Internal error: unknown option */
420 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
426 /* Parse command line parameters - no check is made for these options */
427 for( i_opt = optind; i_opt < i_argc; i_opt++ )
429 putenv( ppsz_argv[ i_opt ] );
434 /*****************************************************************************
435 * Usage: print program usage
436 *****************************************************************************
437 * Print a short inline help. Message interface is initialized at this stage.
438 *****************************************************************************/
439 static void Usage( int i_fashion )
441 intf_Msg(COPYRIGHT_MESSAGE "\n");
444 intf_Msg("usage: vlc [options...] [parameters]\n" );
447 intf_Msg("Options:\n" \
448 " -h, --help, -H, --longhelp \tprint short/long usage\n" \
449 " -v, --version \tprint version information\n"\
450 " --noaudio, --novideo \tdisable audio/video\n" \
451 " --aout {" AUDIO_OPTIONS "} \taudio output method\n"\
452 " --stereo, --mono \tstereo/mono audio\n" \
453 " --vout {" VIDEO_OPTIONS "} \tvideo output method\n"\
454 " --display <display> \tdisplay string\n" \
455 " --width <w>, --height <h> \tdisplay dimensions\n" \
456 " -g, --grayscale, --color \tgrayscale/color video\n" \
457 " --novlans \tdisable vlans\n" \
458 " --server <host>, --port <port> \tvideo server adress\n" \
461 if( i_fashion == SHORT_HELP )
464 /* Interface parameters */
465 intf_Msg("Interface parameters:\n" \
466 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n" \
467 " " INTF_CHANNELS_VAR "=<filename> \tchannels list\n"\
470 /* Audio parameters */
471 intf_Msg("Audio parameters:\n" \
472 " " AOUT_METHOD_VAR "=<method name> \taudio method (" AUDIO_OPTIONS ")\n" \
473 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n" \
474 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n" \
475 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" \
478 /* Video parameters */
479 intf_Msg("Video parameters:\n" \
480 " " VOUT_METHOD_VAR "=<method name> \tdisplay method (" VIDEO_OPTIONS ")\n" \
481 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n" \
482 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n" \
483 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n" \
484 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n" \
485 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" \
488 /* Input parameters */
489 intf_Msg("Input parameters:\n" \
490 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n" \
491 " " INPUT_PORT_VAR "=<port> \tvideo server port\n" \
492 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n"\
493 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n" \
494 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n"\
498 /*****************************************************************************
499 * Version: print complete program version
500 *****************************************************************************
501 * Print complete program version and build number.
502 *****************************************************************************/
503 static void Version( void )
505 intf_Msg(VERSION_MESSAGE "\n\n");
506 intf_Msg("This is free software; see the documentation or contact <videolan@via.ecp.fr>\n" \
507 "for use and copying conditions.\n" \
509 "This software is protected by the international copyright laws, and is\n" \
510 "provided without any warranty, including the implied warranties of\n" \
511 "merchantibility and fitness for a particular purpose.\n" \
515 /*****************************************************************************
516 * InitSignalHandler: system signal handler initialization
517 *****************************************************************************
518 * Set the signal handlers. SIGTERM is not intercepted, because we need at
519 * at least a method to kill the program when all other methods failed, and
520 * when we don't want to use SIGKILL.
521 *****************************************************************************/
522 static void InitSignalHandler( void )
524 /* Termination signals */
525 signal( SIGHUP, SignalHandler );
526 signal( SIGINT, SignalHandler );
527 signal( SIGQUIT, SignalHandler );
530 /*****************************************************************************
531 * SignalHandler: system signal handler
532 *****************************************************************************
533 * This function is called when a signal is received by the program. It tries to
534 * end the program in a clean way.
535 *****************************************************************************/
536 static void SignalHandler( int i_signal )
538 /* Once a signal has been trapped, the termination sequence will be armed and
539 * following signals will be ignored to avoid sending messages to an interface
540 * having been destroyed */
541 signal( SIGHUP, SIG_IGN );
542 signal( SIGINT, SIG_IGN );
543 signal( SIGQUIT, SIG_IGN );
545 /* Acknowledge the signal received */
546 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
548 /* Try to terminate everything - this is done by requesting the end of the
549 * interface thread */
550 p_main->p_intf->b_die = 1;
554 /*****************************************************************************
555 * TestMMX: tests if the processor has MMX support.
556 *****************************************************************************
557 * This function is called if HAVE_MMX is enabled, to check whether the
558 * cpu really supports MMX.
559 *****************************************************************************/
560 static int TestMMX( void )
562 int i_reg, i_dummy = 0;
564 /* test for a 386 cpu */
565 asm volatile ( "pushfl
574 andl $0x40000, %%eax"
580 /* test for a 486 cpu */
581 asm volatile ( "movl %%ecx, %%eax
582 xorl $0x200000, %%eax
590 andl $0x200000, %%eax"
596 /* the cpu supports the CPUID instruction - get its level */
597 asm volatile ( "cpuid"
602 : "a" ( 0 ), /* level 0 */
603 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */
605 /* this shouldn't happen on a normal cpu */
609 /* test for the MMX flag */
610 asm volatile ( "cpuid
611 andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
616 : "a" ( 1 ), /* level 1 */
617 "b" ( i_dummy ) ); /* buggy compiler shouldn't complain */