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 <netinet/in.h>
20 #include <sys/soundcard.h>
23 #include <X11/extensions/XShm.h>
31 #include "input_vlan.h"
32 #include "decoder_fifo.h"
34 #include "audio_output.h"
35 #include "audio_decoder.h"
38 #include "video_output.h"
39 #include "video_decoder.h"
42 #include "interface.h"
50 * Command line options constants. If something is changed here, be sure that
51 * GetConfiguration and Usage are also changed.
54 /* Long options return values - note that values corresponding to short options
55 * chars, and in general any regular char, should be avoided */
56 #define OPT_DISPLAY 130
58 #define OPT_CONSOLE_DISPLAY 140
59 #define OPT_CONSOLE_GEOMETRY 141
61 #define OPT_NOAUDIO 150
62 #define OPT_STEREO 151
66 #define OPT_NOVIDEO 160
68 #define OPT_NOXSHM 162
70 #define OPT_NOVLANS 170
71 #define OPT_VLAN_SERVER 171
74 static const struct option longopts[] =
76 /* name, has_arg, flag, val */
78 /* General/common options */
79 { "help", 0, 0, 'h' },
80 { "display", 1, 0, OPT_DISPLAY },
82 /* Interface options */
83 { "console-display", 1, 0, OPT_CONSOLE_DISPLAY },
84 { "console-geometry", 1, 0, OPT_CONSOLE_GEOMETRY },
87 { "noaudio", 0, 0, OPT_NOAUDIO },
88 { "stereo", 0, 0, OPT_STEREO },
89 { "mono", 0, 0, OPT_MONO },
90 { "rate", 0, 0, OPT_RATE },
93 { "novideo", 0, 0, OPT_NOVIDEO },
94 { "xshm", 0, 0, OPT_XSHM },
95 { "noxshm", 0, 0, OPT_NOXSHM },
97 /* VLAN management options */
98 { "novlans", 0, 0, OPT_NOVLANS },
99 { "vlanserver", 1, 0, OPT_VLAN_SERVER },
105 static const char *psz_shortopts = "h";
108 * Global variable program_data
110 program_data_t *p_program_data; /* see pgm_data.h */
115 static void SetDefaultConfiguration ( program_data_t *p_data );
116 static int GetConfiguration ( program_data_t *p_config, int i_argc, char *ppsz_argv[],
118 static void Usage ( void );
119 static long GetIntParam ( const char *psz_param, long i_min, long i_max, int *pi_err );
120 static void InitSignalHandler ( void );
121 static void SignalHandler ( int i_signal );
123 /*******************************************************************************
124 * main: parse command line, start interface and spawn threads
125 *******************************************************************************
126 * Steps during program execution are:
127 * -configuration parsing and messages interface initialization
128 * -openning of audio output device
129 * -execution of interface, which exit on error or on user request
130 * -closing of audio output device
131 * On error, the spawned threads are cancelled, and the openned devices closed.
132 *******************************************************************************
133 * ?? Signal handlers should be restored to default once the interface has
134 * been closed, since they will cause a crash if the message interface is no
136 *******************************************************************************/
137 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
139 program_data_t program_data; /* root of all data - see pgm_data.h */
142 * Read configuration, initialize messages interface and set up program
144 p_program_data = &program_data; /* set up the global variable */
145 if( intf_InitMsg( &program_data.intf_msg ) ) /* start messages interface */
147 fprintf(stderr, "intf critical: can't initialize messages interface (%s)\n",
151 if( GetConfiguration( &program_data, /* parse command line */
152 i_argc, ppsz_argv, ppsz_env ) )
154 intf_TerminateMsg( &program_data.intf_msg );
157 intf_MsgImm( COPYRIGHT_MESSAGE ); /* print welcome message */
158 InitSignalHandler(); /* prepare signals for interception */
161 * Initialize shared resources and libraries
163 if( program_data.cfg.b_vlans
164 && input_VlanMethodInit( &program_data.input_vlan_method,
165 program_data.cfg.psz_input_vlan_server,
166 program_data.cfg.i_input_vlan_server_port) )
168 /* On error during vlans initialization, switch of vlans */
169 intf_Msg("intf: switching off vlans\n");
170 program_data.cfg.b_vlans = 0;
176 if( program_data.cfg.b_audio )
178 if( aout_Open( &program_data.aout_thread ) )
180 /* On error during audio initialization, switch of audio */
181 intf_Msg("intf: switching off audio\n");
182 program_data.cfg.b_audio = 0;
184 else if( aout_SpawnThread( &program_data.aout_thread ) )
186 aout_Close( &program_data.aout_thread );
187 input_VlanMethodFree( &program_data.input_vlan_method );
188 intf_TerminateMsg( &program_data.intf_msg );
191 program_data.intf_thread.p_aout = &program_data.aout_thread;
197 intf_DbgMsg("intf debug: starting interface\n");
198 intf_Run( &program_data.intf_thread );
199 intf_DbgMsg("intf debug: interface terminated\n");
204 if( program_data.cfg.b_audio )
206 aout_CancelThread( &program_data.aout_thread );
207 aout_Close( &program_data.aout_thread );
211 * Free shared resources and libraries
213 if( program_data.cfg.b_vlans )
215 input_VlanMethodFree( &program_data.input_vlan_method );
219 * Terminate messages interface and program
221 intf_Msg( "program terminated.\n" );
222 intf_TerminateMsg( &program_data.intf_msg );
226 /* following functions are local */
228 /*******************************************************************************
229 * SetDefaultConfiguration: set default options
230 *******************************************************************************
231 * This function is called by GetConfiguration before command line is parsed.
232 * It sets all the default values required later by the program. Note that
233 * all properties must be initialized, wether they are command-line dependant
235 *******************************************************************************/
236 static void SetDefaultConfiguration( program_data_t *p_data )
239 * Audio output thread configuration
241 p_data->cfg.b_audio = 1; /* audio is activated by default */
242 p_data->aout_thread.dsp.psz_device = AOUT_DEFAULT_DEVICE;
243 /* je rajouterai la détection des formats supportés quand le reste
245 p_data->aout_thread.dsp.i_format = AOUT_DEFAULT_FORMAT;
246 p_data->aout_thread.dsp.b_stereo = AOUT_DEFAULT_STEREO;
247 p_data->aout_thread.dsp.l_rate = AOUT_DEFAULT_RATE;
250 * Interface thread configuration
253 p_data->intf_thread.xconsole.psz_display = NULL;
254 p_data->intf_thread.xconsole.psz_geometry = INTF_XCONSOLE_GEOMETRY;
256 /* --- ?? following are ok */
259 * Video output thread configuration
261 p_data->cfg.b_video = 1;
262 p_data->vout_cfg.i_properties = 0;
264 /* VLAN management */
265 p_data->cfg.b_vlans = 1;
266 p_data->cfg.psz_input_vlan_server = VLAN_DEFAULT_SERVER;
267 p_data->cfg.i_input_vlan_server_port = VLAN_DEFAULT_SERVER_PORT;
270 /*******************************************************************************
271 * GetConfiguration: parse command line
272 *******************************************************************************
273 * Parse command line and configuration file for configuration. If the inline
274 * help is requested, the function Usage() is called and the function returns
275 * -1 (causing main() to exit). Note than messages interface is initialized at
277 *******************************************************************************/
278 static int GetConfiguration( program_data_t *p_data, int i_argc,
279 char *ppsz_argv[], char *ppsz_env[] )
283 /* Set default configuration and copy arguments */
284 p_data->i_argc = i_argc;
285 p_data->ppsz_argv = ppsz_argv;
286 p_data->ppsz_env = ppsz_env;
287 SetDefaultConfiguration( p_data );
289 /* Parse command line */
291 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
295 /* General/common options */
296 case 'h': /* -h, --help */
300 case OPT_DISPLAY: /* --display */
301 p_data->vout_cfg.psz_display = optarg;
302 p_data->vout_cfg.i_properties |= VIDEO_CFG_DISPLAY;
303 p_data->intf_thread.xconsole.psz_display = optarg;
306 /* Interface options */
307 case OPT_CONSOLE_DISPLAY: /* --console-display */
308 p_data->intf_thread.xconsole.psz_display = optarg;
310 case OPT_CONSOLE_GEOMETRY: /* --console-geometry */
311 p_data->intf_thread.xconsole.psz_geometry = optarg;
315 case OPT_NOAUDIO: /* --noaudio */
316 p_data->cfg.b_audio = 0;
318 case OPT_STEREO: /* --stereo */
319 p_data->aout_thread.dsp.b_stereo = 1;
321 case OPT_MONO: /* --mono */
322 p_data->aout_thread.dsp.b_stereo = 0;
324 case OPT_RATE: /* --rate */
325 p_data->aout_thread.dsp.l_rate = GetIntParam(optarg, AOUT_MIN_RATE, AOUT_MAX_RATE, &i_err );
333 case OPT_NOVIDEO: /* --novideo */
334 p_data->cfg.b_video = 0;
336 case OPT_XSHM: /* --xshm */
337 p_data->vout_cfg.b_shm_ext = 1;
338 p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
340 case OPT_NOXSHM: /* --noxshm */
341 p_data->vout_cfg.b_shm_ext = 0;
342 p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
345 /* VLAN management options */
346 case OPT_NOVLANS: /* --novlans */
347 p_data->cfg.b_vlans = 0;
349 case OPT_VLAN_SERVER: /* --vlanserver */
350 p_data->cfg.i_input_vlan_server_port = ServerPort( optarg );
351 p_data->cfg.psz_input_vlan_server = optarg;
354 /* Internal error: unknown option */
357 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
366 /*******************************************************************************
367 * Usage: print program usage
368 *******************************************************************************
369 * Print a short inline help. Message interface is initialized at this stage.
370 *******************************************************************************/
371 static void Usage( void )
373 intf_Msg(COPYRIGHT_MESSAGE);
374 /* General options */
375 intf_Msg("usage: vlc [options...]\n" \
376 " -h, --help print usage\n" \
379 intf_Msg(" --noaudio disable audio\n" \
380 " --stereo enable stereo\n" \
381 " --mono disable stereo\n"
382 " --rate <rate> audio output rate (kHz)\n" \
385 intf_Msg(" --novideo disable video\n" \
386 " --xshm, --noxshm enable/disable use of XShm extension\n" \
387 " -d, --display <display> set display name\n" \
390 /* VLAN management options */
391 intf_Msg(" --novlans disable vlans\n" \
392 " --vlanserver <server[:port]> set vlan server address\n" \
396 /*******************************************************************************
397 * GetIntParam: convert a string to an integer
398 *******************************************************************************
399 * This function convert a string to an integer, check range of the value
400 * and that there is no error during convertion. pi_err is a pointer to an
401 * error flag, which contains non 0 on error. This function prints its own
403 *******************************************************************************/
404 static long GetIntParam( const char *psz_param, long i_min, long i_max, int *pi_err )
409 i_value = strtol( psz_param, &psz_endptr, 0 );
410 if( (psz_param[0] == '\0') && (*psz_endptr != '\0') ) /* conversion error */
412 intf_ErrMsg("intf error: conversion error ('%s' should be an integer between %ld and %ld)\n",
413 psz_param, i_min, i_max );
417 if( (i_value < i_min) || (i_value > i_max) ) /* range error */
419 intf_ErrMsg("intf error: range error ('%s' should be an integer between %ld and %ld)\n",
420 psz_param, i_min, i_max );
427 /*******************************************************************************
428 * InitSignalHandler: system signal handler initialization
429 *******************************************************************************
430 * Set the signal handlers. SIGTERM is not intercepted, because we need at
431 * at least a method to kill the program when all other methods failed, and
432 * when we don't want to use SIGKILL.
433 *******************************************************************************/
434 static void InitSignalHandler( void )
436 /* Termination signals */
437 signal( SIGHUP, SignalHandler );
438 signal( SIGINT, SignalHandler );
439 signal( SIGQUIT, SignalHandler );
442 /*******************************************************************************
443 * SignalHandler: system signal handler
444 *******************************************************************************
445 * This function is called when a signal is received by the program. It ignores
446 * it or tries to terminate cleanly.
447 *******************************************************************************/
448 static void SignalHandler( int i_signal )
450 /* Once a signal has been trapped, the signal handler needs to be re-armed on
452 signal( i_signal, SignalHandler );
454 /* Acknowledge the signal received */
455 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
457 /* Try to terminate everything */
458 /* ?? this probably needs to be changed */
459 /* p_program_data->intf_thread.b_die = 1; */