1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 * Includes the main() function for vlc. Parses command line, starts playlist
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: libvlc.c,v 1.2 2002/06/01 14:31:32 sam Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
11 * Gildas Bazin <gbazin@netcourrier.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Pretend we are a builtin module
30 *****************************************************************************/
31 #define MODULE_NAME main
34 /*****************************************************************************
36 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
38 #include <stdio.h> /* sprintf() */
39 #include <string.h> /* strerror() */
40 #include <stdlib.h> /* free() */
41 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
45 #ifdef HAVE_GETOPT_LONG
47 # include <getopt.h> /* getopt() */
50 # include "GNUgetopt/getopt.h"
54 # include <netinet/in.h> /* BSD: struct in_addr */
59 #elif defined( _MSC_VER ) && defined( _WIN32 )
67 #include "vlc_cpu.h" /* CPU detection */
68 #include "os_specific.h"
70 #include "netutils.h" /* network_ChannelJoin */
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
76 #include "interface.h"
78 #include "audio_output.h"
81 #include "video_output.h"
85 /*****************************************************************************
86 * The evil global variables. We handle them with care, don't worry.
87 *****************************************************************************/
89 /* This global lock is used for critical sections - don't abuse it! */
90 static vlc_mutex_t global_lock;
93 /* A list of all the currently allocated vlc objects */
94 static volatile int i_vlc = 0;
95 static volatile vlc_t **pp_vlc = NULL;
97 /*****************************************************************************
99 *****************************************************************************/
100 static int GetFilenames ( vlc_t *, int, char *[] );
101 static void Usage ( vlc_object_t *, const char *psz_module_name );
102 static void ListModules ( vlc_object_t * );
103 static void Version ( void );
104 static void Build ( void );
106 static void InitSignalHandler ( void );
107 static void SimpleSignalHandler ( int i_signal );
108 static void FatalSignalHandler ( int i_signal );
111 static void ShowConsole ( void );
114 /*****************************************************************************
115 * vlc_create: allocate a vlc_t structure, and initialize libvlc if needed.
116 *****************************************************************************
117 * This function allocates a vlc_t structure and returns NULL in case of
118 * failure. Also, the thread system and the signal handlers are initialized.
119 *****************************************************************************/
120 vlc_t * vlc_create( void )
122 vlc_t * p_vlc = NULL;
124 /* Allocate the main structure */
125 p_vlc = vlc_object_create( p_vlc, VLC_OBJECT_ROOT );
131 p_vlc->psz_object_name = "root";
133 p_vlc->p_global_lock = &global_lock;
134 p_vlc->pp_global_data = &p_global_data;
136 p_vlc->b_verbose = 0;
137 p_vlc->b_quiet = 0; /* FIXME: delay message queue output! */
139 /* Initialize the threads system */
140 vlc_threads_init( p_vlc->p_this );
142 /* Initialize mutexes */
143 vlc_mutex_init( p_vlc->p_this, &p_vlc->config_lock );
144 vlc_mutex_init( p_vlc->p_this, &p_vlc->structure_lock );
146 /* Set signal handling policy for all threads */
147 InitSignalHandler( );
149 /* Store our newly allocated structure in the global list */
150 vlc_mutex_lock( p_vlc->p_global_lock );
151 pp_vlc = realloc( pp_vlc, (i_vlc+1) * sizeof( vlc_t * ) );
152 pp_vlc[ i_vlc ] = p_vlc;
154 vlc_mutex_unlock( p_vlc->p_global_lock );
156 /* Update the handle status */
157 p_vlc->i_status = VLC_STATUS_CREATED;
162 /*****************************************************************************
163 * vlc_init: initialize a vlc_t structure.
164 *****************************************************************************
165 * This function initializes a previously allocated vlc_t structure:
167 * - gettext initialization
168 * - message queue, module bank and playlist initialization
169 * - configuration and commandline parsing
170 *****************************************************************************/
171 vlc_error_t vlc_init( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
173 char p_capabilities[200];
176 module_t *p_help_module;
177 playlist_t *p_playlist;
179 /* Check that the handle is valid */
180 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
182 fprintf( stderr, "error: invalid status\n" );
186 fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
188 /* Guess what CPU we have */
189 p_vlc->i_cpu_capabilities = CPUCapabilities( p_vlc->p_this );
192 * Support for gettext
194 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
195 # if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
196 if( !setlocale( LC_MESSAGES, "" ) )
198 fprintf( stderr, "warning: unsupported locale settings\n" );
201 setlocale( LC_CTYPE, "" );
204 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
206 fprintf( stderr, "warning: no domain %s in directory %s\n",
207 PACKAGE, LOCALEDIR );
210 textdomain( PACKAGE );
214 * System specific initialization code
216 system_Init( p_vlc->p_this, &i_argc, ppsz_argv );
219 * Initialize message queue
221 msg_Create( p_vlc->p_this );
223 /* Get the executable name (similar to the basename command) */
226 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
229 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
235 p_vlc->psz_object_name = "vlc";
239 * Initialize the module bank and and load the configuration of the main
240 * module. We need to do this at this stage to be able to display a short
241 * help if required by the user. (short help == main module options)
243 module_InitBank( p_vlc->p_this );
244 module_LoadMain( p_vlc->p_this );
246 /* Hack: insert the help module here */
247 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
248 if( p_help_module == NULL )
250 module_EndBank( p_vlc->p_this );
251 msg_Destroy( p_vlc->p_this );
254 p_help_module->psz_object_name = "help";
255 config_Duplicate( p_help_module, p_help_config );
256 p_help_module->next = p_vlc->module_bank.first;
257 p_vlc->module_bank.first = p_help_module;
260 if( config_LoadCmdLine( p_vlc->p_this, &i_argc, ppsz_argv, 1 ) )
262 p_vlc->module_bank.first = p_help_module->next;
263 config_Free( p_help_module );
264 vlc_object_destroy( p_help_module );
265 module_EndBank( p_vlc->p_this );
266 msg_Destroy( p_vlc->p_this );
270 /* Check for short help option */
271 if( config_GetInt( p_vlc, "help" ) )
273 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n"),
274 p_vlc->psz_object_name );
276 Usage( p_vlc->p_this, "help" );
277 Usage( p_vlc->p_this, "main" );
278 p_vlc->module_bank.first = p_help_module->next;
279 config_Free( p_help_module );
280 vlc_object_destroy( p_help_module );
281 module_EndBank( p_vlc->p_this );
282 msg_Destroy( p_vlc->p_this );
286 /* Check for version option */
287 if( config_GetInt( p_vlc, "version" ) )
290 p_vlc->module_bank.first = p_help_module->next;
291 config_Free( p_help_module );
292 vlc_object_destroy( p_help_module );
293 module_EndBank( p_vlc->p_this );
294 msg_Destroy( p_vlc->p_this );
298 /* Check for build option */
299 if( config_GetInt( p_vlc, "build" ) )
302 p_vlc->module_bank.first = p_help_module->next;
303 config_Free( p_help_module );
304 vlc_object_destroy( p_help_module );
305 module_EndBank( p_vlc->p_this );
306 msg_Destroy( p_vlc->p_this );
310 /* Hack: remove the help module here */
311 p_vlc->module_bank.first = p_help_module->next;
315 * Load the builtins and plugins into the module_bank.
316 * We have to do it before config_Load*() because this also gets the
317 * list of configuration options exported by each module and loads their
320 module_LoadBuiltins( p_vlc->p_this );
321 module_LoadPlugins( p_vlc->p_this );
322 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
323 p_vlc->module_bank.i_count );
325 /* Hack: insert the help module here */
326 p_help_module->next = p_vlc->module_bank.first;
327 p_vlc->module_bank.first = p_help_module;
330 /* Check for help on modules */
331 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
333 Usage( p_vlc->p_this, p_tmp );
335 p_vlc->module_bank.first = p_help_module->next;
336 config_Free( p_help_module );
337 vlc_object_destroy( p_help_module );
338 module_EndBank( p_vlc->p_this );
339 msg_Destroy( p_vlc->p_this );
343 /* Check for long help option */
344 if( config_GetInt( p_vlc, "longhelp" ) )
346 Usage( p_vlc->p_this, NULL );
347 p_vlc->module_bank.first = p_help_module->next;
348 config_Free( p_help_module );
349 vlc_object_destroy( p_help_module );
350 module_EndBank( p_vlc->p_this );
351 msg_Destroy( p_vlc->p_this );
355 /* Check for module list option */
356 if( config_GetInt( p_vlc, "list" ) )
358 ListModules( p_vlc->p_this );
359 p_vlc->module_bank.first = p_help_module->next;
360 config_Free( p_help_module );
361 vlc_object_destroy( p_help_module );
362 module_EndBank( p_vlc->p_this );
363 msg_Destroy( p_vlc->p_this );
367 /* Hack: remove the help module here */
368 p_vlc->module_bank.first = p_help_module->next;
369 config_Free( p_help_module );
370 vlc_object_destroy( p_help_module );
374 * Override default configuration with config file settings
376 p_vlc->psz_homedir = config_GetHomeDir();
377 config_LoadConfigFile( p_vlc->p_this, NULL );
380 * Override configuration with command line settings
382 if( config_LoadCmdLine( p_vlc->p_this, &i_argc, ppsz_argv, 0 ) )
386 /* Pause the console because it's destroyed when we exit */
387 fprintf( stderr, "The command line options couldn't be loaded, check "
388 "that they are valid.\nPress the RETURN key to continue..." );
391 module_EndBank( p_vlc->p_this );
392 msg_Destroy( p_vlc->p_this );
398 * System specific configuration
400 system_Configure( p_vlc->p_this );
402 /* p_vlc inititalization. FIXME ? */
403 p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
404 p_vlc->b_verbose = config_GetInt( p_vlc, "verbose" );
405 p_vlc->b_quiet = config_GetInt( p_vlc, "quiet" );
406 p_vlc->b_color = config_GetInt( p_vlc, "color" );
407 if( !config_GetInt( p_vlc, "mmx" ) )
408 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
409 if( !config_GetInt( p_vlc, "3dn" ) )
410 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
411 if( !config_GetInt( p_vlc, "mmxext" ) )
412 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
413 if( !config_GetInt( p_vlc, "sse" ) )
414 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
415 if( !config_GetInt( p_vlc, "altivec" ) )
416 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
418 #define PRINT_CAPABILITY( capability, string ) \
419 if( p_vlc->i_cpu_capabilities & capability ) \
421 strncat( p_capabilities, string " ", \
422 sizeof(p_capabilities) - strlen(p_capabilities) ); \
423 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
426 p_capabilities[0] = '\0';
427 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
428 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
429 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
430 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
431 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
432 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
433 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
434 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
435 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
436 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
439 * Choose the best memcpy module
441 psz_module = config_GetPsz( p_vlc, "memcpy" );
442 p_vlc->p_memcpy_module =
443 module_Need( p_vlc, MODULE_CAPABILITY_MEMCPY, psz_module, NULL );
444 if( psz_module ) free( psz_module );
445 if( p_vlc->p_memcpy_module == NULL )
447 msg_Err( p_vlc, "no suitable memcpy module, using libc default" );
448 p_vlc->pf_memcpy = memcpy;
452 p_vlc->pf_memcpy = p_vlc->p_memcpy_module->p_functions
453 ->memcpy.functions.memcpy.pf_memcpy;
457 * Initialize shared resources and libraries
459 if( config_GetInt( p_vlc, "network-channel" )
460 && network_ChannelCreate( p_vlc->p_this ) )
462 /* On error during Channels initialization, switch off channels */
464 "channels initialization failed, deactivating channels" );
465 config_PutInt( p_vlc, "network-channel", 0 );
468 /* Update the handle status */
469 p_vlc->i_status = VLC_STATUS_STOPPED;
471 /* XXX XXX XXX XXX XXX XXX XXX XXX */
472 /* XXX XXX XXX XXX XXX XXX XXX XXX */
473 /* XXX XXX XXX XXX XXX XXX XXX XXX */
475 * Initialize playlist and get commandline files
477 p_playlist = playlist_Create( p_vlc->p_this );
480 msg_Err( p_vlc, "playlist initialization failed" );
481 module_EndBank( p_vlc->p_this );
482 msg_Destroy( p_vlc->p_this );
487 * Get input filenames given as commandline arguments
489 GetFilenames( p_vlc, i_argc, ppsz_argv );
490 /* XXX XXX XXX XXX XXX XXX XXX XXX */
491 /* XXX XXX XXX XXX XXX XXX XXX XXX */
492 /* XXX XXX XXX XXX XXX XXX XXX XXX */
497 /*****************************************************************************
499 *****************************************************************************
500 * XXX: This function opens an interface plugin and runs it. If b_block is set
501 * to 0, vlc_add_intf will return immediately and let the interface run in a
502 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
503 * user requests to quit.
504 *****************************************************************************/
505 vlc_error_t vlc_run( vlc_t *p_vlc )
507 /* Check that the handle is valid */
508 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
510 fprintf( stderr, "error: invalid status\n" );
514 /* Update the handle status */
515 p_vlc->i_status = VLC_STATUS_RUNNING;
520 /*****************************************************************************
521 * vlc_add_intf: add an interface
522 *****************************************************************************
523 * This function opens an interface plugin and runs it. If b_block is set
524 * to 0, vlc_add_intf will return immediately and let the interface run in a
525 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
526 * user requests to quit.
527 *****************************************************************************/
528 vlc_error_t vlc_add_intf( vlc_t *p_vlc, char *psz_module, vlc_bool_t b_block )
531 intf_thread_t *p_intf;
532 char *psz_oldmodule = NULL;
534 /* Check that the handle is valid */
535 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
537 fprintf( stderr, "error: invalid status\n" );
543 psz_oldmodule = config_GetPsz( p_vlc, "intf" );
544 config_PutPsz( p_vlc, "intf", psz_module );
547 /* Try to create the interface */
548 p_intf = intf_Create( p_vlc->p_this );
552 config_PutPsz( p_vlc, "intf", psz_oldmodule );
555 free( psz_oldmodule );
561 msg_Err( p_vlc, "interface initialization failed" );
565 /* Try to run the interface */
566 p_intf->b_block = b_block;
567 err = intf_RunThread( p_intf );
570 intf_Destroy( p_intf );
577 /*****************************************************************************
578 * vlc_stop: stop playing.
579 *****************************************************************************
580 * This function requests the interface threads to finish, waits for their
581 * termination, and destroys their structure.
582 *****************************************************************************/
583 vlc_error_t vlc_stop( vlc_t *p_vlc )
585 intf_thread_t *p_intf;
586 playlist_t *p_playlist;
587 vout_thread_t *p_vout;
588 aout_thread_t *p_aout;
590 /* Check that the handle is valid */
591 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
593 fprintf( stderr, "error: invalid status\n" );
598 * Ask the interfaces to stop and destroy them
600 msg_Dbg( p_vlc, "removing all interfaces" );
601 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
603 intf_StopThread( p_intf );
604 vlc_object_unlink_all( p_intf );
605 vlc_object_release( p_intf );
606 intf_Destroy( p_intf );
612 msg_Dbg( p_vlc, "removing all playlists" );
613 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
616 vlc_object_unlink_all( p_playlist );
617 vlc_object_release( p_playlist );
618 playlist_Destroy( p_playlist );
624 msg_Dbg( p_vlc, "removing all video outputs" );
625 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
627 vlc_object_unlink_all( p_vout );
628 vlc_object_release( p_vout );
629 vout_DestroyThread( p_vout );
635 msg_Dbg( p_vlc, "removing all audio outputs" );
636 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
638 vlc_object_unlink_all( p_aout );
639 vlc_object_release( p_aout );
640 aout_DestroyThread( p_aout );
643 /* Update the handle status */
644 p_vlc->i_status = VLC_STATUS_STOPPED;
649 /*****************************************************************************
650 * vlc_end: uninitialize everything.
651 *****************************************************************************
652 * This function uninitializes every vlc component that was activated in
653 * vlc_init: audio and video outputs, playlist, module bank and message queue.
654 *****************************************************************************/
655 vlc_error_t vlc_end( vlc_t *p_vlc )
657 /* Check that the handle is valid */
658 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
660 fprintf( stderr, "error: invalid status\n" );
665 * Go back into channel 0 which is the network
667 if( config_GetInt( p_vlc, "network-channel" ) && p_vlc->p_channel )
669 network_ChannelJoin( p_vlc->p_this, COMMON_CHANNEL );
673 * Free allocated memory
675 if( p_vlc->p_memcpy_module != NULL )
677 module_Unneed( p_vlc->p_memcpy_module );
680 free( p_vlc->psz_homedir );
685 module_EndBank( p_vlc->p_this );
688 * System specific cleaning code
690 system_End( p_vlc->p_this );
693 * Terminate messages interface and program
695 msg_Destroy( p_vlc->p_this );
697 /* Update the handle status */
698 p_vlc->i_status = VLC_STATUS_CREATED;
703 /*****************************************************************************
704 * vlc_destroy: free allocated resources.
705 *****************************************************************************
706 * This function frees the previously allocated vlc_t structure.
707 *****************************************************************************/
708 vlc_error_t vlc_destroy( vlc_t *p_vlc )
712 /* Check that the handle is valid */
713 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
715 fprintf( stderr, "error: invalid status\n" );
719 /* Update the handle status, just in case */
720 p_vlc->i_status = VLC_STATUS_NONE;
722 /* Remove our structure from the global list */
723 vlc_mutex_lock( p_vlc->p_global_lock );
724 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
726 if( pp_vlc[ i_index ] == p_vlc )
732 if( i_index == i_vlc )
734 fprintf( stderr, "error: trying to unregister %p which is not in "
735 "the list\n", p_vlc );
736 vlc_mutex_unlock( p_vlc->p_global_lock );
737 vlc_object_destroy( p_vlc );
741 for( i_index++ ; i_index < i_vlc ; i_index++ )
743 pp_vlc[ i_index - 1 ] = pp_vlc[ i_index ];
749 pp_vlc = realloc( pp_vlc, i_vlc * sizeof( vlc_t * ) );
756 vlc_mutex_unlock( p_vlc->p_global_lock );
758 /* Stop thread system FIXME: last one out please shut the door! */
759 //vlc_threads_end( );
761 /* Destroy mutexes */
762 vlc_mutex_destroy( &p_vlc->structure_lock );
763 vlc_mutex_destroy( &p_vlc->config_lock );
765 vlc_object_destroy( p_vlc );
770 vlc_status_t vlc_status( vlc_t *p_vlc )
774 return VLC_STATUS_NONE;
777 return p_vlc->i_status;
780 vlc_error_t vlc_add_target( vlc_t *p_vlc, char *psz_target )
782 if( !p_vlc || ( p_vlc->i_status != VLC_STATUS_STOPPED
783 && p_vlc->i_status != VLC_STATUS_RUNNING ) )
785 fprintf( stderr, "error: invalid status\n" );
789 playlist_Add( p_vlc->p_this, PLAYLIST_END, psz_target );
794 /* following functions are local */
796 /*****************************************************************************
797 * GetFilenames: parse command line options which are not flags
798 *****************************************************************************
799 * Parse command line for input files.
800 *****************************************************************************/
801 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
805 /* We assume that the remaining parameters are filenames */
806 for( i_opt = optind; i_opt < i_argc; i_opt++ )
808 playlist_Add( p_vlc->p_this, PLAYLIST_END, ppsz_argv[ i_opt ] );
814 /*****************************************************************************
815 * Usage: print program usage
816 *****************************************************************************
817 * Print a short inline help. Message interface is initialized at this stage.
818 *****************************************************************************/
819 static void Usage( vlc_object_t *p_this, const char *psz_module_name )
821 #define FORMAT_STRING " --%s%s%s%s%s%s %s%s\n"
822 /* option name prefix ------' | | | | | | |
823 * option name ---------------' | | | | | |
824 * <bra ------------------------' | | | | |
825 * option type or "" -------------' | | | |
826 * ket> ----------------------------' | | |
827 * padding spaces --------------------' | |
828 * comment ------------------------------' |
829 * comment suffix -------------------------'
831 * The purpose of having bra and ket is that we might i18n them as well.
834 #define PADDING_SPACES 25
836 module_config_t *p_item;
837 char psz_spaces[PADDING_SPACES+LINE_START+1];
838 char psz_format[sizeof(FORMAT_STRING)];
840 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
841 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
843 strcpy( psz_format, FORMAT_STRING );
849 /* Enumerate the config for each module */
850 for( p_module = p_this->p_vlc->module_bank.first ;
852 p_module = p_module->next )
854 vlc_bool_t b_help_module = !strcmp( "help", p_module->psz_object_name );
856 if( psz_module_name && strcmp( psz_module_name,
857 p_module->psz_object_name ) )
860 /* Ignore modules without config options */
861 if( !p_module->i_config_items ) continue;
863 /* Print module name */
864 fprintf( stderr, _("%s module options:\n\n"),
865 p_module->psz_object_name );
867 for( p_item = p_module->p_config;
868 p_item->i_type != MODULE_CONFIG_HINT_END;
871 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
875 switch( p_item->i_type )
877 case MODULE_CONFIG_HINT_CATEGORY:
878 fprintf( stderr, " %s\n", p_item->psz_text );
881 case MODULE_CONFIG_ITEM_STRING:
882 case MODULE_CONFIG_ITEM_FILE:
883 case MODULE_CONFIG_ITEM_MODULE: /* We could also have "=<" here */
884 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
886 case MODULE_CONFIG_ITEM_INTEGER:
887 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
889 case MODULE_CONFIG_ITEM_FLOAT:
890 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
892 case MODULE_CONFIG_ITEM_BOOL:
893 psz_bra = ""; psz_type = ""; psz_ket = "";
896 psz_suf = p_item->i_value ? _(" (default enabled)") :
897 _(" (default disabled)");
902 /* Add short option */
903 if( p_item->i_short )
906 psz_format[3] = p_item->i_short;
918 i = PADDING_SPACES - strlen( p_item->psz_name )
919 - strlen( psz_bra ) - strlen( psz_type )
920 - strlen( psz_ket ) - 1;
921 if( p_item->i_type == MODULE_CONFIG_ITEM_BOOL
930 psz_spaces[i] = '\n';
934 psz_spaces[i] = '\0';
937 fprintf( stderr, psz_format,
938 ( p_item->i_type == MODULE_CONFIG_ITEM_BOOL
939 && !b_help_module ) ? "(no-)" : "",
940 p_item->psz_name, psz_bra, psz_type, psz_ket,
941 psz_spaces, p_item->psz_text, psz_suf );
946 /* Yet another nasty hack.
947 * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
948 * for each module?? */
949 if( !strcmp( "main", p_module->psz_object_name ) )
951 fprintf( stderr, _("\nPlaylist items:"
952 "\n *.mpg, *.vob plain MPEG-1/2 files"
953 "\n [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
955 "\n [vcd:][device][@[title][,[chapter]]"
957 "\n udpstream:[@[<bind address>][:<bind port>]]"
958 "\n UDP stream sent by VLS"
959 "\n vlc:loop loop execution of the "
961 "\n vlc:pause pause execution of "
963 "\n vlc:quit quit VLC"
967 fprintf( stderr, "\n" );
971 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
972 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
977 /*****************************************************************************
978 * ListModules: list the available modules with their description
979 *****************************************************************************
980 * Print a list of all available modules (builtins and plugins) and a short
981 * description for each one.
982 *****************************************************************************/
983 static void ListModules( vlc_object_t *p_this )
988 memset( psz_spaces, ' ', 22 );
995 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n\n"),
996 p_this->p_vlc->psz_object_name );
998 fprintf( stderr, _("[module] [description]\n") );
1000 /* Enumerate each module */
1001 for( p_module = p_this->p_vlc->module_bank.first ;
1003 p_module = p_module->next )
1007 /* Nasty hack, but right now I'm too tired to think about a nice
1009 i = 22 - strlen( p_module->psz_object_name ) - 1;
1013 fprintf( stderr, " %s%s %s\n", p_module->psz_object_name, psz_spaces,
1014 p_module->psz_longname );
1016 psz_spaces[i] = ' ';
1020 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1021 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1026 /*****************************************************************************
1027 * Version: print complete program version
1028 *****************************************************************************
1029 * Print complete program version and build number.
1030 *****************************************************************************/
1031 static void Version( void )
1037 fprintf( stderr, VERSION_MESSAGE "\n" );
1039 _("This program comes with NO WARRANTY, to the extent permitted by "
1040 "law.\nYou may redistribute it under the terms of the GNU General "
1041 "Public License;\nsee the file named COPYING for details.\n"
1042 "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1044 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1045 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1050 /*****************************************************************************
1051 * Build: print information about the vlc build
1052 *****************************************************************************
1053 * Print the ./configure command line and other information.
1054 *****************************************************************************/
1055 static void Build( void )
1061 fprintf( stderr, "configured with %s\n", CONFIGURE_LINE );
1063 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1064 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1069 /*****************************************************************************
1070 * ShowConsole: On Win32, create an output console for debug messages
1071 *****************************************************************************
1072 * This function is useful only on Win32.
1073 *****************************************************************************/
1075 static void ShowConsole( void )
1078 freopen( "CONOUT$", "w", stdout );
1079 freopen( "CONOUT$", "w", stderr );
1080 freopen( "CONIN$", "r", stdin );
1085 /*****************************************************************************
1086 * InitSignalHandler: system signal handler initialization
1087 *****************************************************************************
1088 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1089 * at least a method to kill the program when all other methods failed, and
1090 * when we don't want to use SIGKILL.
1091 *****************************************************************************/
1092 static void InitSignalHandler( void )
1095 /* Termination signals */
1096 signal( SIGINT, FatalSignalHandler );
1097 signal( SIGHUP, FatalSignalHandler );
1098 signal( SIGQUIT, FatalSignalHandler );
1101 signal( SIGALRM, SimpleSignalHandler );
1102 signal( SIGPIPE, SimpleSignalHandler );
1106 /*****************************************************************************
1107 * SimpleSignalHandler: system signal handler
1108 *****************************************************************************
1109 * This function is called when a non fatal signal is received by the program.
1110 *****************************************************************************/
1111 static void SimpleSignalHandler( int i_signal )
1115 /* Acknowledge the signal received and warn all the p_vlc structures */
1116 vlc_mutex_lock( &global_lock );
1117 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1119 msg_Warn( pp_vlc[ i_index ], "ignoring signal %d", i_signal );
1121 vlc_mutex_unlock( &global_lock );
1124 /*****************************************************************************
1125 * FatalSignalHandler: system signal handler
1126 *****************************************************************************
1127 * This function is called when a fatal signal is received by the program.
1128 * It tries to end the program in a clean way.
1129 *****************************************************************************/
1130 static void FatalSignalHandler( int i_signal )
1132 static mtime_t abort_time = 0;
1133 static volatile vlc_bool_t b_die = 0;
1136 /* Once a signal has been trapped, the termination sequence will be
1137 * armed and following signals will be ignored to avoid sending messages
1138 * to an interface having been destroyed */
1140 vlc_mutex_lock( &global_lock );
1144 abort_time = mdate();
1146 /* Try to terminate everything - this is done by requesting the end of
1147 * all the p_vlc structures */
1148 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1150 /* Acknowledge the signal received */
1151 msg_Err( pp_vlc[ i_index ], "signal %d received, exiting - do it "
1152 "again in case vlc gets stuck", i_signal );
1153 pp_vlc[ i_index ]->b_die = 1;
1156 else if( mdate() > abort_time + 1000000 )
1158 /* If user asks again 1 second later, die badly */
1160 signal( SIGINT, SIG_IGN );
1161 signal( SIGHUP, SIG_IGN );
1162 signal( SIGQUIT, SIG_IGN );
1165 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1167 msg_Err( pp_vlc[ i_index ], "user insisted too much, dying badly" );
1170 vlc_mutex_unlock( &global_lock );
1174 vlc_mutex_unlock( &global_lock );