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
45 #define OPT_NOVLANS 170
48 static const struct option longopts[] =
50 /* name, has_arg, flag, val */
52 /* General/common options */
53 { "help", 0, 0, 'h' },
56 { "noaudio", 0, 0, OPT_NOAUDIO },
57 { "stereo", 0, 0, OPT_STEREO },
58 { "mono", 0, 0, OPT_MONO },
61 { "novideo", 0, 0, OPT_NOVIDEO },
62 { "grayscale", 0, 0, 'g' },
63 { "color", 0, 0, OPT_COLOR },
65 /* VLAN management options */
66 { "novlans", 0, 0, OPT_NOVLANS },
72 static const char *psz_shortopts = "hg";
74 /*******************************************************************************
75 * Global variable program_data - this is the one and only, see main.h
76 *******************************************************************************/
79 /*******************************************************************************
81 *******************************************************************************/
82 static void SetDefaultConfiguration ( void );
83 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
84 static void Usage ( void );
86 static void InitSignalHandler ( void );
87 static void SignalHandler ( int i_signal );
89 /*******************************************************************************
90 * main: parse command line, start interface and spawn threads
91 *******************************************************************************
92 * Steps during program execution are:
93 * -configuration parsing and messages interface initialization
94 * -openning of audio output device and some global modules
95 * -execution of interface, which exit on error or on user request
96 * -closing of audio output device and some global modules
97 * On error, the spawned threads are cancelled, and the openned devices closed.
98 *******************************************************************************/
99 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
101 main_t main_data; /* root of all data - see main.h */
102 p_main = &main_data; /* set up the global variable */
105 * Read configuration, initialize messages interface and set up program
107 p_main->p_msg = intf_MsgCreate();
108 if( !p_main->p_msg ) /* start messages interface */
110 fprintf(stderr, "critical error: can't initialize messages interface (%s)\n",
114 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )/* parse command line */
119 intf_MsgImm( COPYRIGHT_MESSAGE "\n" ); /* print welcome message */
122 * Initialize shared resources and libraries
124 if( main_data.b_vlans && input_VlanCreate() )
126 /* On error during vlans initialization, switch of vlans */
127 intf_Msg("Virtual LANs initialization failed : vlans management is desactivated\n");
128 main_data.b_vlans = 0;
132 * Open audio device and start aout thread
134 if( main_data.b_audio )
136 main_data.p_aout = aout_CreateThread( NULL );
137 if( main_data.p_aout == NULL )
139 /* On error during audio initialization, switch of audio */
140 intf_Msg("Audio initialization failed : audio is desactivated\n");
141 main_data.b_audio = 0;
148 main_data.p_intf = intf_Create();
149 if( main_data.p_intf != NULL )
151 InitSignalHandler(); /* prepare signals for interception */
152 intf_Run( main_data.p_intf );
153 intf_Destroy( main_data.p_intf );
159 if( main_data.b_audio )
161 aout_DestroyThread( main_data.p_aout, NULL );
165 * Free shared resources and libraries
167 if( main_data.b_vlans )
173 * Terminate messages interface and program
175 intf_Msg( "Program terminated.\n" );
180 /*******************************************************************************
181 * main_GetIntVariable: get the int value of an environment variable
182 *******************************************************************************
183 * This function is used to read some default parameters in modules.
184 *******************************************************************************/
185 int main_GetIntVariable( char *psz_name, int i_default )
187 char * psz_env; /* environment value */
188 char * psz_end; /* end of parsing index */
189 long int i_value; /* value */
191 psz_env = getenv( psz_name );
194 i_value = strtol( psz_env, &psz_end, 0 );
195 if( (*psz_env != '\0') && (*psz_end == '\0') )
203 /*******************************************************************************
204 * main_GetPszVariable: get the string value of an environment variable
205 *******************************************************************************
206 * This function is used to read some default parameters in modules.
207 *******************************************************************************/
208 char * main_GetPszVariable( char *psz_name, char *psz_default )
212 psz_env = getenv( psz_name );
217 return( psz_default );
220 /*******************************************************************************
221 * main_PutPszVariable: set the string value of an environment variable
222 *******************************************************************************
223 * This function is used to set some default parameters in modules. The use of
224 * this function will cause some memory leak: since some systems use the pointer
225 * passed to putenv to store the environment string, it can't be freed.
226 *******************************************************************************/
227 void main_PutPszVariable( char *psz_name, char *psz_value )
231 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
232 if( psz_env == NULL )
234 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
238 sprintf( psz_env, "%s=%s", psz_name, psz_value );
239 if( putenv( psz_env ) )
241 intf_ErrMsg("error: %s\n", strerror(errno));
246 /*******************************************************************************
247 * main_PutIntVariable: set the integer value of an environment variable
248 *******************************************************************************
249 * This function is used to set some default parameters in modules. The use of
250 * this function will cause some memory leak: since some systems use the pointer
251 * passed to putenv to store the environment string, it can't be freed.
252 *******************************************************************************/
253 void main_PutIntVariable( char *psz_name, int i_value )
255 char psz_value[ 256 ]; /* buffer for value */
257 sprintf(psz_value, "%d", i_value );
258 main_PutPszVariable( psz_name, psz_value );
261 /* following functions are local */
263 /*******************************************************************************
264 * SetDefaultConfiguration: set default options
265 *******************************************************************************
266 * This function is called by GetConfiguration before command line is parsed.
267 * It sets all the default values required later by the program. At this stage,
268 * most structure are not yet allocated, so initialization must be done using
270 *******************************************************************************/
271 static void SetDefaultConfiguration( void )
274 * All features are activated by default
281 * Audio output thread configuration
284 // ?? initialization using structures is no more available, use putenv/getenv
290 * Video output thread configuration
292 // p_data->vout_cfg.i_properties = 0;
294 /* VLAN management */
295 /*??? p_data->cfg.b_vlans = 0;
296 p_data->cfg.psz_input_vlan_server = VLAN_DEFAULT_SERVER;
297 p_data->cfg.i_input_vlan_server_port = VLAN_DEFAULT_SERVER_PORT;
301 /*******************************************************************************
302 * GetConfiguration: parse command line
303 *******************************************************************************
304 * Parse command line and configuration file for configuration. If the inline
305 * help is requested, the function Usage() is called and the function returns
306 * -1 (causing main() to exit). The messages interface is initialized at this
307 * stage, but most structures are not allocated, so only environment should
309 *******************************************************************************/
310 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
314 /* Set default configuration and copy arguments */
315 p_main->i_argc = i_argc;
316 p_main->ppsz_argv = ppsz_argv;
317 p_main->ppsz_env = ppsz_env;
318 SetDefaultConfiguration();
320 /* Parse command line options */
322 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
326 /* General/common options */
327 case 'h': /* -h, --help */
333 case OPT_NOAUDIO: /* --noaudio */
336 case OPT_STEREO: /* --stereo */
337 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
339 case OPT_MONO: /* --mono */
340 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
344 case OPT_NOVIDEO: /* --novideo */
347 case 'g': /* -g, --grayscale */
348 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
350 case OPT_COLOR: /* --color */
351 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
354 /* VLAN management options */
355 case OPT_NOVLANS: /* --novlans */
359 /* Internal error: unknown option */
362 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
368 /* Parse command line parameters - no check is made for these options */
369 for( i_opt = optind; i_opt < i_argc; i_opt++ )
371 putenv( ppsz_argv[ i_opt ] );
376 /*******************************************************************************
377 * Usage: print program usage
378 *******************************************************************************
379 * Print a short inline help. Message interface is initialized at this stage.
380 *******************************************************************************/
381 static void Usage( void )
383 intf_Msg(COPYRIGHT_MESSAGE "\n");
386 intf_Msg("usage: vlc [options...] [parameters]\n" \
387 " parameters can be passed using environment variables\n" \
388 " example: vlan_server=vlan-server.via.ecp.fr:1234\n" \
392 intf_Msg("Options:\n" \
393 " -h, --help print usage\n" \
394 " -g, --grayscale grayscale video\n" \
395 " --noaudio disable audio\n" \
396 " --stereo enable stereo\n" \
397 " --mono disable stereo\n"
398 " --novideo disable video\n" \
399 " --color color video\n" \
400 " --novlans disable vlans\n" \
403 /* Interface parameters */
404 intf_Msg("Interface parameters:\n" \
405 " " INTF_INIT_SCRIPT_VAR "=<filename> initialization script\n" \
408 /* Audio parameters */
409 intf_Msg("Audio parameters:\n" \
410 " " AOUT_DSP_VAR "=<filename> dsp device path\n" \
411 " " AOUT_STEREO_VAR "={1|0} stereo or mono output\n" \
412 " " AOUT_RATE_VAR "=<rate> output rate\n" \
415 /* Video parameters */
416 intf_Msg("Video parameters:\n" \
417 " " VOUT_DISPLAY_VAR "=<display name> display used\n"
418 " " VOUT_WIDTH_VAR "=<width> display width\n"
419 " " VOUT_HEIGHT_VAR "=<height> dislay height\n"
420 " " VOUT_FB_DEV_VAR "=<filename> framebuffer device path\n" \
421 " " VOUT_GRAYSCALE_VAR "={1|0} grayscale or color output\n" \
424 /* Vlan parameters */
425 intf_Msg("VLANs (Virtual Local Aera Networks) parameters:\n" \
426 " vlan_server=<host>[:<port>] VLANs server address and port\n" \
429 /* Interfaces keys */
430 intf_Msg("Interface keys: most interfaces accept the following commands:\n" \
432 " +, -, m change volume, mute\n" \
433 " g, G, c change gamma, toggle grayscale\n" \
434 " 0 - 9 select channel\n" \
435 " [space] toggle info printing\n" \
439 /*******************************************************************************
440 * InitSignalHandler: system signal handler initialization
441 *******************************************************************************
442 * Set the signal handlers. SIGTERM is not intercepted, because we need at
443 * at least a method to kill the program when all other methods failed, and
444 * when we don't want to use SIGKILL.
445 *******************************************************************************/
446 static void InitSignalHandler( void )
448 /* Termination signals */
449 signal( SIGHUP, SignalHandler );
450 signal( SIGINT, SignalHandler );
451 signal( SIGQUIT, SignalHandler );
454 /*******************************************************************************
455 * SignalHandler: system signal handler
456 *******************************************************************************
457 * This function is called when a signal is received by the program. It tries to
458 * end the program in a clean way.
459 *******************************************************************************/
460 static void SignalHandler( int i_signal )
462 /* Once a signal has been trapped, the termination sequence will be armed and
463 * following signals will be ignored to avoid sending messages to an interface
464 * having been destroyed */
465 signal( SIGHUP, SIG_IGN );
466 signal( SIGINT, SIG_IGN );
467 signal( SIGQUIT, SIG_IGN );
469 /* Acknowledge the signal received */
470 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
472 /* Try to terminate everything - this is done by requesting the end of the
473 * interface thread */
474 p_main->p_intf->b_die = 1;