1 /*******************************************************************************
2 * main.c: main vlc source
4 *******************************************************************************
5 * Includes the main() function for vlc. Parses command line, start interface
7 *******************************************************************************/
9 /*******************************************************************************
11 *******************************************************************************/
19 #include <sys/soundcard.h> /* audio_output.h */
24 #include "vlc_thread.h"
25 #include "input_vlan.h"
27 #include "interface.h"
28 #include "audio_output.h"
31 /*******************************************************************************
32 * Command line options constants. If something is changed here, be sure that
33 * GetConfiguration and Usage are also changed.
34 *******************************************************************************/
36 /* Long options return values - note that values corresponding to short options
37 * chars, and in general any regular char, should be avoided */
38 #define OPT_NOAUDIO 150
39 #define OPT_STEREO 151
42 #define OPT_NOVIDEO 160
43 #define OPT_DISPLAY 161
45 #define OPT_HEIGHT 163
48 #define OPT_NOVLANS 170
49 #define OPT_SERVER 171
53 static const struct option longopts[] =
55 /* name, has_arg, flag, val */
57 /* General/common options */
58 { "help", 0, 0, 'h' },
61 { "noaudio", 0, 0, OPT_NOAUDIO },
62 { "stereo", 0, 0, OPT_STEREO },
63 { "mono", 0, 0, OPT_MONO },
66 { "novideo", 0, 0, OPT_NOVIDEO },
67 { "display", 1, 0, OPT_DISPLAY },
68 { "width", 1, 0, OPT_WIDTH },
69 { "height", 1, 0, OPT_HEIGHT },
70 { "grayscale", 0, 0, 'g' },
71 { "color", 0, 0, OPT_COLOR },
74 { "novlans", 0, 0, OPT_NOVLANS },
75 { "server", 1, 0, OPT_SERVER },
76 { "port", 1, 0, OPT_PORT },
82 static const char *psz_shortopts = "hg";
84 /*******************************************************************************
85 * Global variable program_data - this is the one and only, see main.h
86 *******************************************************************************/
89 /*******************************************************************************
91 *******************************************************************************/
92 static void SetDefaultConfiguration ( void );
93 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
94 static void Usage ( void );
96 static void InitSignalHandler ( void );
97 static void SignalHandler ( int i_signal );
99 /*******************************************************************************
100 * main: parse command line, start interface and spawn threads
101 *******************************************************************************
102 * Steps during program execution are:
103 * -configuration parsing and messages interface initialization
104 * -openning of audio output device and some global modules
105 * -execution of interface, which exit on error or on user request
106 * -closing of audio output device and some global modules
107 * On error, the spawned threads are cancelled, and the openned devices closed.
108 *******************************************************************************/
109 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
111 main_t main_data; /* root of all data - see main.h */
112 p_main = &main_data; /* set up the global variable */
115 * Read configuration, initialize messages interface and set up program
117 p_main->p_msg = intf_MsgCreate();
118 if( !p_main->p_msg ) /* start messages interface */
120 fprintf(stderr, "critical error: can't initialize messages interface (%s)\n",
124 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )/* parse command line */
129 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
132 * Initialize shared resources and libraries
134 if( main_data.b_vlans && input_VlanCreate() )
136 /* On error during vlans initialization, switch of vlans */
137 intf_Msg("Virtual LANs initialization failed : vlans management is desactivated\n");
138 main_data.b_vlans = 0;
142 * Open audio device and start aout thread
144 if( main_data.b_audio )
146 main_data.p_aout = aout_CreateThread( NULL );
147 if( main_data.p_aout == NULL )
149 /* On error during audio initialization, switch of audio */
150 intf_Msg("Audio initialization failed : audio is desactivated\n");
151 main_data.b_audio = 0;
158 main_data.p_intf = intf_Create();
159 if( main_data.p_intf != NULL )
161 InitSignalHandler(); /* prepare signals for interception */
162 intf_Run( main_data.p_intf );
163 intf_Destroy( main_data.p_intf );
169 if( main_data.b_audio )
171 aout_DestroyThread( main_data.p_aout, NULL );
175 * Free shared resources and libraries
177 if( main_data.b_vlans )
183 * Terminate messages interface and program
185 intf_Msg( "Program terminated.\n" );
190 /*******************************************************************************
191 * main_GetIntVariable: get the int value of an environment variable
192 *******************************************************************************
193 * This function is used to read some default parameters in modules.
194 *******************************************************************************/
195 int main_GetIntVariable( char *psz_name, int i_default )
197 char * psz_env; /* environment value */
198 char * psz_end; /* end of parsing index */
199 long int i_value; /* value */
201 psz_env = getenv( psz_name );
204 i_value = strtol( psz_env, &psz_end, 0 );
205 if( (*psz_env != '\0') && (*psz_end == '\0') )
213 /*******************************************************************************
214 * main_GetPszVariable: get the string value of an environment variable
215 *******************************************************************************
216 * This function is used to read some default parameters in modules.
217 *******************************************************************************/
218 char * main_GetPszVariable( char *psz_name, char *psz_default )
222 psz_env = getenv( psz_name );
227 return( psz_default );
230 /*******************************************************************************
231 * main_PutPszVariable: set the string value of an environment variable
232 *******************************************************************************
233 * This function is used to set some default parameters in modules. The use of
234 * this function will cause some memory leak: since some systems use the pointer
235 * passed to putenv to store the environment string, it can't be freed.
236 *******************************************************************************/
237 void main_PutPszVariable( char *psz_name, char *psz_value )
241 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
242 if( psz_env == NULL )
244 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
248 sprintf( psz_env, "%s=%s", psz_name, psz_value );
249 if( putenv( psz_env ) )
251 intf_ErrMsg("error: %s\n", strerror(errno));
256 /*******************************************************************************
257 * main_PutIntVariable: set the integer value of an environment variable
258 *******************************************************************************
259 * This function is used to set some default parameters in modules. The use of
260 * this function will cause some memory leak: since some systems use the pointer
261 * passed to putenv to store the environment string, it can't be freed.
262 *******************************************************************************/
263 void main_PutIntVariable( char *psz_name, int i_value )
265 char psz_value[ 256 ]; /* buffer for value */
267 sprintf(psz_value, "%d", i_value );
268 main_PutPszVariable( psz_name, psz_value );
271 /* following functions are local */
273 /*******************************************************************************
274 * SetDefaultConfiguration: set default options
275 *******************************************************************************
276 * This function is called by GetConfiguration before command line is parsed.
277 * It sets all the default values required later by the program. At this stage,
278 * most structure are not yet allocated, so initialization must be done using
280 *******************************************************************************/
281 static void SetDefaultConfiguration( void )
284 * All features are activated by default
291 /*******************************************************************************
292 * GetConfiguration: parse command line
293 *******************************************************************************
294 * Parse command line and configuration file for configuration. If the inline
295 * help is requested, the function Usage() is called and the function returns
296 * -1 (causing main() to exit). The messages interface is initialized at this
297 * stage, but most structures are not allocated, so only environment should
299 *******************************************************************************/
300 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
304 /* Set default configuration and copy arguments */
305 p_main->i_argc = i_argc;
306 p_main->ppsz_argv = ppsz_argv;
307 p_main->ppsz_env = ppsz_env;
308 SetDefaultConfiguration();
310 /* Parse command line options */
312 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
316 /* General/common options */
317 case 'h': /* -h, --help */
323 case OPT_NOAUDIO: /* --noaudio */
326 case OPT_STEREO: /* --stereo */
327 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
329 case OPT_MONO: /* --mono */
330 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
334 case OPT_NOVIDEO: /* --novideo */
337 case OPT_DISPLAY: /* --display */
338 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
340 case OPT_WIDTH: /* --width */
341 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
343 case OPT_HEIGHT: /* --height */
344 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
347 case 'g': /* -g, --grayscale */
348 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
350 case OPT_COLOR: /* --color */
351 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
355 case OPT_NOVLANS: /* --novlans */
358 case OPT_SERVER: /* --server */
359 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
361 case OPT_PORT: /* --port */
362 main_PutPszVariable( INPUT_PORT_VAR, optarg );
365 /* Internal error: unknown option */
368 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
374 /* Parse command line parameters - no check is made for these options */
375 for( i_opt = optind; i_opt < i_argc; i_opt++ )
377 putenv( ppsz_argv[ i_opt ] );
382 /*******************************************************************************
383 * Usage: print program usage
384 *******************************************************************************
385 * Print a short inline help. Message interface is initialized at this stage.
386 *******************************************************************************/
387 static void Usage( void )
389 intf_Msg(COPYRIGHT_MESSAGE "\n");
392 intf_Msg("usage: vlc [options...] [parameters]\n" );
395 intf_Msg("Options:\n" \
396 " -h, --help \tprint usage\n" \
397 " --noaudio \tdisable audio\n" \
398 " --stereo, --mono \tstereo/mono audio\n" \
399 " --novideo \tdisable video\n" \
400 " --display <display> \tdisplay string\n" \
401 " --width <w>, --height <h> \tdisplay dimensions\n" \
402 " -g, --grayscale, --color \tgrayscale/color video\n" \
403 " --novlans \tdisable vlans\n" \
404 " --server <host>, --port <port> \tvideo server adress\n" \
407 /* Interface parameters */
408 intf_Msg("Interface parameters:\n" \
409 " " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script\n" \
412 /* Audio parameters */
413 intf_Msg("Audio parameters:\n" \
414 " " AOUT_DSP_VAR "=<filename> \tdsp device path\n" \
415 " " AOUT_STEREO_VAR "={1|0} \tstereo or mono output\n" \
416 " " AOUT_RATE_VAR "=<rate> \toutput rate\n" \
419 /* Video parameters */
420 intf_Msg("Video parameters:\n" \
421 " " VOUT_DISPLAY_VAR "=<display name> \tdisplay used\n" \
422 " " VOUT_WIDTH_VAR "=<width> \tdisplay width\n" \
423 " " VOUT_HEIGHT_VAR "=<height> \tdislay height\n" \
424 " " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path\n" \
425 " " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output\n" \
428 /* Input parameters */
429 intf_Msg("Input parameters:\n" \
430 " " INPUT_SERVER_VAR "=<hostname> \tvideo server\n" \
431 " " INPUT_PORT_VAR "=<port> \tvideo server port\n" \
432 " " INPUT_IFACE_VAR "=<interface> \tnetwork interface\n" \
433 " " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server\n" \
434 " " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port\n" \
437 /* Interfaces keys */
438 intf_Msg("Interface keys: most interfaces accept the following commands:\n" \
439 " [esc], q \tquit\n" \
440 " +, -, m \tchange volume, mute\n" \
441 " g, G, c \tchange gamma, toggle grayscale\n" \
442 " 0 - 9 \tselect channel\n" \
443 " [space] \ttoggle info printing\n" \
447 /*******************************************************************************
448 * InitSignalHandler: system signal handler initialization
449 *******************************************************************************
450 * Set the signal handlers. SIGTERM is not intercepted, because we need at
451 * at least a method to kill the program when all other methods failed, and
452 * when we don't want to use SIGKILL.
453 *******************************************************************************/
454 static void InitSignalHandler( void )
456 /* Termination signals */
457 signal( SIGHUP, SignalHandler );
458 signal( SIGINT, SignalHandler );
459 signal( SIGQUIT, SignalHandler );
462 /*******************************************************************************
463 * SignalHandler: system signal handler
464 *******************************************************************************
465 * This function is called when a signal is received by the program. It tries to
466 * end the program in a clean way.
467 *******************************************************************************/
468 static void SignalHandler( int i_signal )
470 /* Once a signal has been trapped, the termination sequence will be armed and
471 * following signals will be ignored to avoid sending messages to an interface
472 * having been destroyed */
473 signal( SIGHUP, SIG_IGN );
474 signal( SIGINT, SIG_IGN );
475 signal( SIGQUIT, SIG_IGN );
477 /* Acknowledge the signal received */
478 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
480 /* Try to terminate everything - this is done by requesting the end of the
481 * interface thread */
482 p_main->p_intf->b_die = 1;