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>
34 #include "input_vlan.h"
35 #include "input_netlist.h"
36 #include "decoder_fifo.h"
38 #include "audio_output.h"
39 #include "audio_decoder.h"
42 #include "video_output.h"
43 #include "video_decoder.h"
46 #include "interface.h"
53 * Command line options constants. If something is changed here, be sure that
54 * GetConfiguration and Usage are also changed.
57 /* Long options return values - note that values corresponding to short options
58 * chars, and in general any regular char, should be avoided */
59 #define OPT_DISPLAY 130
61 #define OPT_CONSOLE_DISPLAY 140
62 #define OPT_CONSOLE_GEOMETRY 141
64 #define OPT_NOAUDIO 150
65 #define OPT_STEREO 151
69 #define OPT_NOVIDEO 160
71 #define OPT_NOXSHM 162
73 #define OPT_NOVLANS 170
74 #define OPT_VLAN_SERVER 171
77 static const struct option longopts[] =
79 /* name, has_arg, flag, val */
81 /* General/common options */
82 { "help", 0, 0, 'h' },
83 { "display", 1, 0, OPT_DISPLAY },
85 /* Interface options */
86 { "console-display", 1, 0, OPT_CONSOLE_DISPLAY },
87 { "console-geometry", 1, 0, OPT_CONSOLE_GEOMETRY },
90 { "noaudio", 0, 0, OPT_NOAUDIO },
91 { "stereo", 0, 0, OPT_STEREO },
92 { "mono", 0, 0, OPT_MONO },
93 { "rate", 0, 0, OPT_RATE },
96 { "novideo", 0, 0, OPT_NOVIDEO },
97 { "xshm", 0, 0, OPT_XSHM },
98 { "noxshm", 0, 0, OPT_NOXSHM },
100 /* VLAN management options */
101 { "novlans", 0, 0, OPT_NOVLANS },
102 { "vlanserver", 1, 0, OPT_VLAN_SERVER },
108 static const char *psz_shortopts = "h";
111 * Global variable program_data
113 program_data_t *p_program_data; /* see pgm_data.h */
118 static void SetDefaultConfiguration ( program_data_t *p_data );
119 static int GetConfiguration ( program_data_t *p_config, int i_argc, char *ppsz_argv[],
121 static void Usage ( void );
122 static long GetIntParam ( const char *psz_param, long i_min, long i_max, int *pi_err );
123 static void InitSignalHandler ( void );
124 static void SignalHandler ( int i_signal );
126 /*******************************************************************************
127 * main: parse command line, start interface and spawn threads
128 *******************************************************************************
129 * Steps during program execution are:
130 * -configuration parsing and messages interface initialization
131 * -openning of audio output device
132 * -execution of interface, which exit on error or on user request
133 * -closing of audio output device
134 * On error, the spawned threads are cancelled, and the openned devices closed.
135 *******************************************************************************
136 * ?? Signal handlers should be restored to default once the interface has
137 * been closed, since they will cause a crash if the message interface is no
139 *******************************************************************************/
140 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
142 program_data_t program_data; /* root of all data - see pgm_data.h */
145 * Read configuration, initialize messages interface and set up program
147 p_program_data = &program_data; /* set up the global variable */
148 if( intf_InitMsg( &program_data.intf_msg ) ) /* start messages interface */
150 fprintf(stderr, "intf critical: can't initialize messages interface (%s)\n",
154 if( GetConfiguration( &program_data, /* parse command line */
155 i_argc, ppsz_argv, ppsz_env ) )
157 intf_TerminateMsg( &program_data.intf_msg );
160 intf_MsgImm( COPYRIGHT_MESSAGE ); /* print welcome message */
161 InitSignalHandler(); /* prepare signals for interception */
164 * Initialize shared resources and libraries
166 if( program_data.cfg.b_vlans
167 && input_VlanMethodInit( &program_data.input_vlan_method,
168 program_data.cfg.psz_input_vlan_server,
169 program_data.cfg.i_input_vlan_server_port) )
171 /* On error during vlans initialization, switch of vlans */
172 intf_Msg("intf: switching off vlans\n");
173 program_data.cfg.b_vlans = 0;
179 if( program_data.cfg.b_audio )
181 if( aout_Open( &program_data.aout_thread ) )
183 /* On error during audio initialization, switch of audio */
184 intf_Msg("intf: switching off audio\n");
185 program_data.cfg.b_audio = 0;
187 else if( aout_SpawnThread( &program_data.aout_thread ) )
189 aout_Close( &program_data.aout_thread );
190 input_VlanMethodFree( &program_data.input_vlan_method );
191 intf_TerminateMsg( &program_data.intf_msg );
194 program_data.intf_thread.p_aout = &program_data.aout_thread;
200 intf_DbgMsg("intf debug: starting interface\n");
201 intf_Run( &program_data.intf_thread );
202 intf_DbgMsg("intf debug: interface terminated\n");
207 if( program_data.cfg.b_audio )
209 aout_CancelThread( &program_data.aout_thread );
210 aout_Close( &program_data.aout_thread );
214 * Free shared resources and libraries
216 if( program_data.cfg.b_vlans )
218 input_VlanMethodFree( &program_data.input_vlan_method );
222 * Terminate messages interface and program
224 intf_Msg( "program terminated.\n" );
225 intf_TerminateMsg( &program_data.intf_msg );
229 /* following functions are local */
231 /*******************************************************************************
232 * SetDefaultConfiguration: set default options
233 *******************************************************************************
234 * This function is called by GetConfiguration before command line is parsed.
235 * It sets all the default values required later by the program. Note that
236 * all properties must be initialized, wether they are command-line dependant
238 *******************************************************************************/
239 static void SetDefaultConfiguration( program_data_t *p_data )
242 * Audio output thread configuration
244 p_data->cfg.b_audio = 1; /* audio is activated by default */
245 p_data->aout_thread.dsp.psz_device = AOUT_DEFAULT_DEVICE;
246 /* je rajouterai la détection des formats supportés quand le reste
248 p_data->aout_thread.dsp.i_format = AOUT_DEFAULT_FORMAT;
249 p_data->aout_thread.dsp.b_stereo = AOUT_DEFAULT_STEREO;
250 p_data->aout_thread.dsp.l_rate = AOUT_DEFAULT_RATE;
253 * Interface thread configuration
256 p_data->intf_thread.xconsole.psz_display = NULL;
257 p_data->intf_thread.xconsole.psz_geometry = INTF_XCONSOLE_GEOMETRY;
259 /* --- ?? following are ok */
262 * Video output thread configuration
264 p_data->cfg.b_video = 1;
265 p_data->vout_cfg.i_properties = 0;
267 /* VLAN management */
268 p_data->cfg.b_vlans = 1;
269 p_data->cfg.psz_input_vlan_server = VLAN_DEFAULT_SERVER;
270 p_data->cfg.i_input_vlan_server_port = VLAN_DEFAULT_SERVER_PORT;
273 /*******************************************************************************
274 * GetConfiguration: parse command line
275 *******************************************************************************
276 * Parse command line and configuration file for configuration. If the inline
277 * help is requested, the function Usage() is called and the function returns
278 * -1 (causing main() to exit). Note than messages interface is initialized at
280 *******************************************************************************/
281 static int GetConfiguration( program_data_t *p_data, int i_argc,
282 char *ppsz_argv[], char *ppsz_env[] )
286 /* Set default configuration and copy arguments */
287 p_data->i_argc = i_argc;
288 p_data->ppsz_argv = ppsz_argv;
289 p_data->ppsz_env = ppsz_env;
290 SetDefaultConfiguration( p_data );
292 /* Parse command line */
294 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
298 /* General/common options */
299 case 'h': /* -h, --help */
303 case OPT_DISPLAY: /* --display */
304 p_data->vout_cfg.psz_display = optarg;
305 p_data->vout_cfg.i_properties |= VIDEO_CFG_DISPLAY;
306 p_data->intf_thread.xconsole.psz_display = optarg;
309 /* Interface options */
310 case OPT_CONSOLE_DISPLAY: /* --console-display */
311 p_data->intf_thread.xconsole.psz_display = optarg;
313 case OPT_CONSOLE_GEOMETRY: /* --console-geometry */
314 p_data->intf_thread.xconsole.psz_geometry = optarg;
318 case OPT_NOAUDIO: /* --noaudio */
319 p_data->cfg.b_audio = 0;
321 case OPT_STEREO: /* --stereo */
322 p_data->aout_thread.dsp.b_stereo = 1;
324 case OPT_MONO: /* --mono */
325 p_data->aout_thread.dsp.b_stereo = 0;
327 case OPT_RATE: /* --rate */
328 p_data->aout_thread.dsp.l_rate = GetIntParam(optarg, AOUT_MIN_RATE, AOUT_MAX_RATE, &i_err );
336 case OPT_NOVIDEO: /* --novideo */
337 p_data->cfg.b_video = 0;
339 case OPT_XSHM: /* --xshm */
340 p_data->vout_cfg.b_shm_ext = 1;
341 p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
343 case OPT_NOXSHM: /* --noxshm */
344 p_data->vout_cfg.b_shm_ext = 0;
345 p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
348 /* VLAN management options */
349 case OPT_NOVLANS: /* --novlans */
350 p_data->cfg.b_vlans = 0;
352 case OPT_VLAN_SERVER: /* --vlanserver */
353 p_data->cfg.i_input_vlan_server_port = ServerPort( optarg );
354 p_data->cfg.psz_input_vlan_server = optarg;
357 /* Internal error: unknown option */
360 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
369 /*******************************************************************************
370 * Usage: print program usage
371 *******************************************************************************
372 * Print a short inline help. Message interface is initialized at this stage.
373 *******************************************************************************/
374 static void Usage( void )
376 intf_Msg(COPYRIGHT_MESSAGE);
377 /* General options */
378 intf_Msg("usage: vlc [options...]\n" \
379 " -h, --help print usage\n" \
382 intf_Msg(" --noaudio disable audio\n" \
383 " --stereo enable stereo\n" \
384 " --mono disable stereo\n"
385 " --rate <rate> audio output rate (kHz)\n" \
388 intf_Msg(" --novideo disable video\n" \
389 " --xshm, --noxshm enable/disable use of XShm extension\n" \
390 " -d, --display <display> set display name\n" \
393 /* VLAN management options */
394 intf_Msg(" --novlans disable vlans\n" \
395 " --vlanserver <server[:port]> set vlan server address\n" \
399 /*******************************************************************************
400 * GetIntParam: convert a string to an integer
401 *******************************************************************************
402 * This function convert a string to an integer, check range of the value
403 * and that there is no error during convertion. pi_err is a pointer to an
404 * error flag, which contains non 0 on error. This function prints its own
406 *******************************************************************************/
407 static long GetIntParam( const char *psz_param, long i_min, long i_max, int *pi_err )
412 i_value = strtol( psz_param, &psz_endptr, 0 );
413 if( (psz_param[0] == '\0') && (*psz_endptr != '\0') ) /* conversion error */
415 intf_ErrMsg("intf error: conversion error ('%s' should be an integer between %ld and %ld)\n",
416 psz_param, i_min, i_max );
420 if( (i_value < i_min) || (i_value > i_max) ) /* range error */
422 intf_ErrMsg("intf error: range error ('%s' should be an integer between %ld and %ld)\n",
423 psz_param, i_min, i_max );
430 /*******************************************************************************
431 * InitSignalHandler: system signal handler initialization
432 *******************************************************************************
433 * Set the signal handlers. SIGTERM is not intercepted, because we need at
434 * at least a method to kill the program when all other methods failed, and
435 * when we don't want to use SIGKILL.
436 *******************************************************************************/
437 static void InitSignalHandler( void )
439 /* Termination signals */
440 signal( SIGHUP, SignalHandler );
441 signal( SIGINT, SignalHandler );
442 signal( SIGQUIT, SignalHandler );
445 /*******************************************************************************
446 * SignalHandler: system signal handler
447 *******************************************************************************
448 * This function is called when a signal is received by the program. It ignores
449 * it or tries to terminate cleanly.
450 *******************************************************************************/
451 static void SignalHandler( int i_signal )
453 /* Once a signal has been trapped, the signal handler needs to be re-armed on
455 signal( i_signal, SignalHandler );
457 /* Acknowledge the signal received */
458 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
460 /* Try to terminate everything */
461 /* ?? this probably needs to be changed */
462 p_program_data->intf_thread.b_die = 1;