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.16 2002/07/18 01:00:41 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"
75 #include "vlc_playlist.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 int i_unique = 0;
96 static volatile vlc_t **pp_vlc = NULL;
98 /*****************************************************************************
100 *****************************************************************************/
101 static int GetFilenames ( vlc_t *, int, char *[] );
102 static void Usage ( vlc_t *, const char *psz_module_name );
103 static void ListModules ( vlc_t * );
104 static void Version ( void );
105 static void Build ( void );
108 static void InitSignalHandler ( void );
109 static void SimpleSignalHandler ( int i_signal );
110 static void FatalSignalHandler ( int i_signal );
114 static void ShowConsole ( void );
117 /*****************************************************************************
118 * vlc_create: allocate a vlc_t structure, and initialize libvlc if needed.
119 *****************************************************************************
120 * This function allocates a vlc_t structure and returns NULL in case of
121 * failure. Also, the thread system and the signal handlers are initialized.
122 *****************************************************************************/
123 vlc_t * vlc_create( void )
125 vlc_t * p_vlc = NULL;
127 /* Allocate the main structure */
128 p_vlc = vlc_object_create( p_vlc, VLC_OBJECT_ROOT );
134 p_vlc->psz_object_name = "root";
136 p_vlc->p_global_lock = &global_lock;
137 p_vlc->pp_global_data = &p_global_data;
139 p_vlc->b_verbose = VLC_FALSE;
140 p_vlc->b_quiet = VLC_FALSE; /* FIXME: delay message queue output! */
142 /* Initialize the threads system */
143 vlc_threads_init( p_vlc );
145 /* Initialize mutexes */
146 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
147 vlc_mutex_init( p_vlc, &p_vlc->structure_lock );
149 /* Set signal handling policy for all threads */
151 InitSignalHandler( );
154 /* Store our newly allocated structure in the global list */
155 vlc_mutex_lock( p_vlc->p_global_lock );
156 pp_vlc = realloc( pp_vlc, (i_vlc+1) * sizeof( vlc_t * ) );
157 pp_vlc[ i_vlc ] = p_vlc;
159 p_vlc->i_unique = i_unique;
161 vlc_mutex_unlock( p_vlc->p_global_lock );
163 /* Update the handle status */
164 p_vlc->i_status = VLC_STATUS_CREATED;
169 /*****************************************************************************
170 * vlc_init: initialize a vlc_t structure.
171 *****************************************************************************
172 * This function initializes a previously allocated vlc_t structure:
174 * - gettext initialization
175 * - message queue, module bank and playlist initialization
176 * - configuration and commandline parsing
177 *****************************************************************************/
178 vlc_error_t vlc_init( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
180 char p_capabilities[200];
183 module_t *p_help_module;
184 playlist_t *p_playlist;
186 /* Check that the handle is valid */
187 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
189 fprintf( stderr, "error: invalid status (!CREATED)\n" );
193 fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
195 /* Guess what CPU we have */
196 p_vlc->i_cpu_capabilities = CPUCapabilities( p_vlc );
199 * Support for gettext
201 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
202 # if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
203 if( !setlocale( LC_MESSAGES, "" ) )
205 fprintf( stderr, "warning: unsupported locale settings\n" );
208 setlocale( LC_CTYPE, "" );
211 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
213 fprintf( stderr, "warning: no domain %s in directory %s\n",
214 PACKAGE, LOCALEDIR );
217 textdomain( PACKAGE );
221 * System specific initialization code
223 system_Init( p_vlc, &i_argc, ppsz_argv );
226 * Initialize message queue
230 /* Get the executable name (similar to the basename command) */
233 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
236 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
242 p_vlc->psz_object_name = "vlc";
245 /* Announce who we are */
246 msg_Dbg( p_vlc, COPYRIGHT_MESSAGE );
247 msg_Dbg( p_vlc, "libvlc was configured with %s", CONFIGURE_LINE );
250 * Initialize the module bank and and load the configuration of the main
251 * module. We need to do this at this stage to be able to display a short
252 * help if required by the user. (short help == main module options)
254 module_InitBank( p_vlc );
255 module_LoadMain( p_vlc );
257 /* Hack: insert the help module here */
258 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
259 if( p_help_module == NULL )
261 module_EndBank( p_vlc );
262 msg_Destroy( p_vlc );
265 p_help_module->psz_object_name = "help";
266 config_Duplicate( p_help_module, p_help_config );
267 p_help_module->next = p_vlc->module_bank.first;
268 p_vlc->module_bank.first = p_help_module;
271 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
273 p_vlc->module_bank.first = p_help_module->next;
274 config_Free( p_help_module );
275 vlc_object_destroy( p_help_module );
276 module_EndBank( p_vlc );
277 msg_Destroy( p_vlc );
281 /* Check for short help option */
282 if( config_GetInt( p_vlc, "help" ) )
284 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n"),
285 p_vlc->psz_object_name );
287 Usage( p_vlc, "help" );
288 Usage( p_vlc, "main" );
289 p_vlc->module_bank.first = p_help_module->next;
290 config_Free( p_help_module );
291 vlc_object_destroy( p_help_module );
292 module_EndBank( p_vlc );
293 msg_Destroy( p_vlc );
297 /* Check for version option */
298 if( config_GetInt( p_vlc, "version" ) )
301 p_vlc->module_bank.first = p_help_module->next;
302 config_Free( p_help_module );
303 vlc_object_destroy( p_help_module );
304 module_EndBank( p_vlc );
305 msg_Destroy( p_vlc );
309 /* Check for build option */
310 if( config_GetInt( p_vlc, "build" ) )
313 p_vlc->module_bank.first = p_help_module->next;
314 config_Free( p_help_module );
315 vlc_object_destroy( p_help_module );
316 module_EndBank( p_vlc );
317 msg_Destroy( p_vlc );
321 /* Hack: remove the help module here */
322 p_vlc->module_bank.first = p_help_module->next;
326 * Load the builtins and plugins into the module_bank.
327 * We have to do it before config_Load*() because this also gets the
328 * list of configuration options exported by each module and loads their
331 module_LoadBuiltins( p_vlc );
332 module_LoadPlugins( p_vlc );
333 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
334 p_vlc->module_bank.i_count );
336 /* Hack: insert the help module here */
337 p_help_module->next = p_vlc->module_bank.first;
338 p_vlc->module_bank.first = p_help_module;
341 /* Check for help on modules */
342 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
344 Usage( p_vlc, p_tmp );
346 p_vlc->module_bank.first = p_help_module->next;
347 config_Free( p_help_module );
348 vlc_object_destroy( p_help_module );
349 module_EndBank( p_vlc );
350 msg_Destroy( p_vlc );
354 /* Check for long help option */
355 if( config_GetInt( p_vlc, "longhelp" ) )
357 Usage( p_vlc, NULL );
358 p_vlc->module_bank.first = p_help_module->next;
359 config_Free( p_help_module );
360 vlc_object_destroy( p_help_module );
361 module_EndBank( p_vlc );
362 msg_Destroy( p_vlc );
366 /* Check for module list option */
367 if( config_GetInt( p_vlc, "list" ) )
369 ListModules( p_vlc );
370 p_vlc->module_bank.first = p_help_module->next;
371 config_Free( p_help_module );
372 vlc_object_destroy( p_help_module );
373 module_EndBank( p_vlc );
374 msg_Destroy( p_vlc );
378 /* Hack: remove the help module here */
379 p_vlc->module_bank.first = p_help_module->next;
380 config_Free( p_help_module );
381 vlc_object_destroy( p_help_module );
385 * Override default configuration with config file settings
387 p_vlc->psz_homedir = config_GetHomeDir();
388 config_LoadConfigFile( p_vlc, NULL );
391 * Override configuration with command line settings
393 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
397 /* Pause the console because it's destroyed when we exit */
398 fprintf( stderr, "The command line options couldn't be loaded, check "
399 "that they are valid.\nPress the RETURN key to continue..." );
402 module_EndBank( p_vlc );
403 msg_Destroy( p_vlc );
408 * System specific configuration
410 system_Configure( p_vlc );
413 * Output messages that may still be in the queue
415 p_vlc->b_verbose = config_GetInt( p_vlc, "verbose" );
416 p_vlc->b_quiet = config_GetInt( p_vlc, "quiet" );
417 p_vlc->b_color = config_GetInt( p_vlc, "color" );
420 /* p_vlc inititalization. FIXME ? */
421 p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
422 if( !config_GetInt( p_vlc, "mmx" ) )
423 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
424 if( !config_GetInt( p_vlc, "3dn" ) )
425 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
426 if( !config_GetInt( p_vlc, "mmxext" ) )
427 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
428 if( !config_GetInt( p_vlc, "sse" ) )
429 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
430 if( !config_GetInt( p_vlc, "altivec" ) )
431 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
433 #define PRINT_CAPABILITY( capability, string ) \
434 if( p_vlc->i_cpu_capabilities & capability ) \
436 strncat( p_capabilities, string " ", \
437 sizeof(p_capabilities) - strlen(p_capabilities) ); \
438 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
441 p_capabilities[0] = '\0';
442 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
443 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
444 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
445 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
446 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
447 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
448 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
449 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
450 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
451 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
454 * Choose the best memcpy module
456 psz_module = config_GetPsz( p_vlc, "memcpy" );
457 p_vlc->p_memcpy_module =
458 module_Need( p_vlc, MODULE_CAPABILITY_MEMCPY, psz_module, NULL );
459 if( psz_module ) free( psz_module );
460 if( p_vlc->p_memcpy_module == NULL )
462 msg_Err( p_vlc, "no suitable memcpy module, using libc default" );
463 p_vlc->pf_memcpy = memcpy;
467 p_vlc->pf_memcpy = p_vlc->p_memcpy_module->p_functions
468 ->memcpy.functions.memcpy.pf_memcpy;
472 * Initialize shared resources and libraries
474 if( config_GetInt( p_vlc, "network-channel" )
475 && network_ChannelCreate( p_vlc ) )
477 /* On error during Channels initialization, switch off channels */
479 "channels initialization failed, deactivating channels" );
480 config_PutInt( p_vlc, "network-channel", VLC_FALSE );
484 * Initialize playlist and get commandline files
486 p_playlist = playlist_Create( p_vlc );
489 msg_Err( p_vlc, "playlist initialization failed" );
490 if( p_vlc->p_memcpy_module != NULL )
492 module_Unneed( p_vlc->p_memcpy_module );
494 module_EndBank( p_vlc );
495 msg_Destroy( p_vlc );
499 /* Update the handle status */
500 p_vlc->i_status = VLC_STATUS_STOPPED;
503 * Get input filenames given as commandline arguments
505 GetFilenames( p_vlc, i_argc, ppsz_argv );
510 /*****************************************************************************
512 *****************************************************************************
513 * XXX: This function opens an interface plugin and runs it. If b_block is set
514 * to 0, vlc_add_intf will return immediately and let the interface run in a
515 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
516 * user requests to quit.
517 *****************************************************************************/
518 vlc_error_t vlc_run( vlc_t *p_vlc )
520 /* Check that the handle is valid */
521 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
523 fprintf( stderr, "error: invalid status (!STOPPED)\n" );
527 /* Update the handle status */
528 p_vlc->i_status = VLC_STATUS_RUNNING;
533 /*****************************************************************************
534 * vlc_add_intf: add an interface
535 *****************************************************************************
536 * This function opens an interface plugin and runs it. If b_block is set
537 * to 0, vlc_add_intf will return immediately and let the interface run in a
538 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
539 * user requests to quit.
540 *****************************************************************************/
541 vlc_error_t vlc_add_intf( vlc_t *p_vlc, const char *psz_module,
545 intf_thread_t *p_intf;
546 char *psz_oldmodule = NULL;
548 /* Check that the handle is valid */
549 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
551 fprintf( stderr, "error: invalid status (!RUNNING)\n" );
557 psz_oldmodule = config_GetPsz( p_vlc, "intf" );
558 config_PutPsz( p_vlc, "intf", psz_module );
561 /* Try to create the interface */
562 p_intf = intf_Create( p_vlc );
566 config_PutPsz( p_vlc, "intf", psz_oldmodule );
569 free( psz_oldmodule );
575 msg_Err( p_vlc, "interface initialization failed" );
579 /* Try to run the interface */
580 p_intf->b_block = b_block;
581 err = intf_RunThread( p_intf );
584 vlc_object_detach_all( p_intf );
585 intf_Destroy( p_intf );
592 /*****************************************************************************
593 * vlc_stop: stop playing.
594 *****************************************************************************
595 * This function requests the interface threads to finish, waits for their
596 * termination, and destroys their structure.
597 *****************************************************************************/
598 vlc_error_t vlc_stop( vlc_t *p_vlc )
600 intf_thread_t *p_intf;
601 playlist_t *p_playlist;
602 vout_thread_t *p_vout;
603 aout_thread_t *p_aout;
605 /* Check that the handle is valid */
606 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
608 fprintf( stderr, "error: invalid status (!RUNNING)\n" );
613 * Ask the interfaces to stop and destroy them
615 msg_Dbg( p_vlc, "removing all interfaces" );
616 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
618 intf_StopThread( p_intf );
619 vlc_object_detach_all( p_intf );
620 vlc_object_release( p_intf );
621 intf_Destroy( p_intf );
627 msg_Dbg( p_vlc, "removing all playlists" );
628 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
631 vlc_object_detach_all( p_playlist );
632 vlc_object_release( p_playlist );
633 playlist_Destroy( p_playlist );
639 msg_Dbg( p_vlc, "removing all video outputs" );
640 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
642 vlc_object_detach_all( p_vout );
643 vlc_object_release( p_vout );
644 vout_DestroyThread( p_vout );
650 msg_Dbg( p_vlc, "removing all audio outputs" );
651 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
653 vlc_object_detach_all( p_aout );
654 vlc_object_release( p_aout );
655 aout_DestroyThread( p_aout );
658 /* Update the handle status */
659 p_vlc->i_status = VLC_STATUS_STOPPED;
664 /*****************************************************************************
665 * vlc_end: uninitialize everything.
666 *****************************************************************************
667 * This function uninitializes every vlc component that was activated in
668 * vlc_init: audio and video outputs, playlist, module bank and message queue.
669 *****************************************************************************/
670 vlc_error_t vlc_end( vlc_t *p_vlc )
672 /* Check that the handle is valid */
673 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
675 fprintf( stderr, "error: invalid status (!STOPPED)\n" );
680 * Go back into channel 0 which is the network
682 if( config_GetInt( p_vlc, "network-channel" ) && p_vlc->p_channel )
684 network_ChannelJoin( p_vlc, COMMON_CHANNEL );
688 * Free allocated memory
690 if( p_vlc->p_memcpy_module != NULL )
692 module_Unneed( p_vlc->p_memcpy_module );
695 free( p_vlc->psz_homedir );
700 module_EndBank( p_vlc );
703 * System specific cleaning code
708 * Terminate messages interface and program
710 msg_Destroy( p_vlc );
712 /* Update the handle status */
713 p_vlc->i_status = VLC_STATUS_CREATED;
718 /*****************************************************************************
719 * vlc_destroy: free allocated resources.
720 *****************************************************************************
721 * This function frees the previously allocated vlc_t structure.
722 *****************************************************************************/
723 vlc_error_t vlc_destroy( vlc_t *p_vlc )
727 /* Check that the handle is valid */
728 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
730 fprintf( stderr, "error: invalid status (!CREATED)\n" );
734 /* Update the handle status, just in case */
735 p_vlc->i_status = VLC_STATUS_NONE;
737 /* Remove our structure from the global list */
738 vlc_mutex_lock( p_vlc->p_global_lock );
739 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
741 if( pp_vlc[ i_index ] == p_vlc )
747 if( i_index == i_vlc )
749 fprintf( stderr, "error: trying to unregister %p which is not in "
750 "the list\n", p_vlc );
751 vlc_mutex_unlock( p_vlc->p_global_lock );
752 vlc_object_destroy( p_vlc );
756 for( i_index++ ; i_index < i_vlc ; i_index++ )
758 pp_vlc[ i_index - 1 ] = pp_vlc[ i_index ];
764 pp_vlc = realloc( pp_vlc, i_vlc * sizeof( vlc_t * ) );
771 vlc_mutex_unlock( p_vlc->p_global_lock );
773 /* Stop thread system: last one out please shut the door! */
774 vlc_threads_end( p_vlc );
776 /* Destroy mutexes */
777 vlc_mutex_destroy( &p_vlc->structure_lock );
778 vlc_mutex_destroy( &p_vlc->config_lock );
780 vlc_object_destroy( p_vlc );
785 vlc_status_t vlc_status( vlc_t *p_vlc )
789 return VLC_STATUS_NONE;
792 return p_vlc->i_status;
795 vlc_error_t vlc_add_target( vlc_t *p_vlc, const char *psz_target,
796 int i_mode, int i_pos )
799 playlist_t *p_playlist;
801 if( !p_vlc || ( p_vlc->i_status != VLC_STATUS_STOPPED
802 && p_vlc->i_status != VLC_STATUS_RUNNING ) )
804 fprintf( stderr, "error: invalid status (!STOPPED&&!RUNNING)\n" );
808 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
810 if( p_playlist == NULL )
812 msg_Dbg( p_vlc, "no playlist present, creating one" );
813 p_playlist = playlist_Create( p_vlc );
815 if( p_playlist == NULL )
820 vlc_object_yield( p_playlist );
823 err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
825 vlc_object_release( p_playlist );
830 /* following functions are local */
832 /*****************************************************************************
833 * GetFilenames: parse command line options which are not flags
834 *****************************************************************************
835 * Parse command line for input files.
836 *****************************************************************************/
837 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
841 /* We assume that the remaining parameters are filenames */
842 for( i_opt = optind; i_opt < i_argc; i_opt++ )
844 vlc_add_target( p_vlc, ppsz_argv[ i_opt ],
845 PLAYLIST_APPEND, PLAYLIST_END );
851 /*****************************************************************************
852 * Usage: print program usage
853 *****************************************************************************
854 * Print a short inline help. Message interface is initialized at this stage.
855 *****************************************************************************/
856 static void Usage( vlc_t *p_this, const char *psz_module_name )
858 #define FORMAT_STRING " --%s%s%s%s%s%s%s %s%s\n"
859 /* option name -------------' | | | | | |
860 * <bra --------------------------' | | | | |
861 * option type or "" ---------------' | | | |
862 * ket> ------------------------------' | | |
863 * padding spaces ----------------------' | |
864 * comment --------------------------------' |
865 * comment suffix ---------------------------'
867 * The purpose of having bra and ket is that we might i18n them as well.
870 #define PADDING_SPACES 25
872 module_config_t *p_item;
873 char psz_spaces[PADDING_SPACES+LINE_START+1];
874 char psz_format[sizeof(FORMAT_STRING)];
876 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
877 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
879 strcpy( psz_format, FORMAT_STRING );
885 /* Enumerate the config for each module */
886 for( p_module = p_this->p_vlc->module_bank.first ;
888 p_module = p_module->next )
890 vlc_bool_t b_help_module = !strcmp( "help", p_module->psz_object_name );
892 if( psz_module_name && strcmp( psz_module_name,
893 p_module->psz_object_name ) )
898 /* Ignore modules without config options */
899 if( !p_module->i_config_items )
904 /* Print module name */
905 fprintf( stderr, _("%s module options:\n\n"),
906 p_module->psz_object_name );
908 for( p_item = p_module->p_config;
909 p_item->i_type != CONFIG_HINT_END;
912 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
913 char *psz_suf = "", *psz_prefix = NULL;
916 switch( p_item->i_type )
918 case CONFIG_HINT_CATEGORY:
919 case CONFIG_HINT_USAGE:
920 fprintf( stderr, " %s\n", p_item->psz_text );
923 case CONFIG_ITEM_STRING:
924 case CONFIG_ITEM_FILE:
925 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
926 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
928 case CONFIG_ITEM_INTEGER:
929 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
931 case CONFIG_ITEM_FLOAT:
932 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
934 case CONFIG_ITEM_BOOL:
935 psz_bra = ""; psz_type = ""; psz_ket = "";
938 psz_suf = p_item->i_value ? _(" (default enabled)") :
939 _(" (default disabled)");
944 /* Add short option */
945 if( p_item->i_short )
948 psz_format[3] = p_item->i_short;
960 i = PADDING_SPACES - strlen( p_item->psz_name )
961 - strlen( psz_bra ) - strlen( psz_type )
962 - strlen( psz_ket ) - 1;
963 if( p_item->i_type == CONFIG_ITEM_BOOL
966 vlc_bool_t b_dash = VLC_FALSE;
967 psz_prefix = p_item->psz_name;
970 if( *psz_prefix++ == '-' )
979 psz_prefix = ", --no-";
980 i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
984 psz_prefix = ", --no";
985 i -= strlen( p_item->psz_name ) + strlen( ", --no" );
992 psz_spaces[i] = '\n';
996 psz_spaces[i] = '\0';
999 if( p_item->i_type == CONFIG_ITEM_BOOL &&
1002 fprintf( stderr, psz_format, p_item->psz_name, psz_prefix,
1003 p_item->psz_name, psz_bra, psz_type, psz_ket,
1004 psz_spaces, p_item->psz_text, psz_suf );
1008 fprintf( stderr, psz_format, p_item->psz_name, "", "",
1009 psz_bra, psz_type, psz_ket, psz_spaces,
1010 p_item->psz_text, psz_suf );
1012 psz_spaces[i] = ' ';
1016 fprintf( stderr, "\n" );
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 * ListModules: list the available modules with their description
1028 *****************************************************************************
1029 * Print a list of all available modules (builtins and plugins) and a short
1030 * description for each one.
1031 *****************************************************************************/
1032 static void ListModules( vlc_t *p_this )
1035 char psz_spaces[22];
1037 memset( psz_spaces, ' ', 22 );
1044 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n\n"),
1045 p_this->p_vlc->psz_object_name );
1047 fprintf( stderr, _("[module] [description]\n") );
1049 /* Enumerate each module */
1050 for( p_module = p_this->p_vlc->module_bank.first ;
1052 p_module = p_module->next )
1056 /* Nasty hack, but right now I'm too tired to think about a nice
1058 i = 22 - strlen( p_module->psz_object_name ) - 1;
1062 fprintf( stderr, " %s%s %s\n", p_module->psz_object_name, psz_spaces,
1063 p_module->psz_longname );
1065 psz_spaces[i] = ' ';
1069 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1070 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1075 /*****************************************************************************
1076 * Version: print complete program version
1077 *****************************************************************************
1078 * Print complete program version and build number.
1079 *****************************************************************************/
1080 static void Version( void )
1086 fprintf( stderr, VERSION_MESSAGE "\n" );
1088 _("This program comes with NO WARRANTY, to the extent permitted by "
1089 "law.\nYou may redistribute it under the terms of the GNU General "
1090 "Public License;\nsee the file named COPYING for details.\n"
1091 "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1093 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1094 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1099 /*****************************************************************************
1100 * Build: print information about the vlc build
1101 *****************************************************************************
1102 * Print the ./configure command line and other information.
1103 *****************************************************************************/
1104 static void Build( void )
1110 fprintf( stderr, "configured with %s\n", CONFIGURE_LINE );
1112 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1113 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1118 /*****************************************************************************
1119 * ShowConsole: On Win32, create an output console for debug messages
1120 *****************************************************************************
1121 * This function is useful only on Win32.
1122 *****************************************************************************/
1124 static void ShowConsole( void )
1127 freopen( "CONOUT$", "w", stdout );
1128 freopen( "CONOUT$", "w", stderr );
1129 freopen( "CONIN$", "r", stdin );
1135 /*****************************************************************************
1136 * InitSignalHandler: system signal handler initialization
1137 *****************************************************************************
1138 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1139 * at least a method to kill the program when all other methods failed, and
1140 * when we don't want to use SIGKILL.
1141 *****************************************************************************/
1142 static void InitSignalHandler( void )
1144 /* Termination signals */
1145 signal( SIGINT, FatalSignalHandler );
1146 signal( SIGHUP, FatalSignalHandler );
1147 signal( SIGQUIT, FatalSignalHandler );
1150 signal( SIGALRM, SimpleSignalHandler );
1151 signal( SIGPIPE, SimpleSignalHandler );
1154 /*****************************************************************************
1155 * SimpleSignalHandler: system signal handler
1156 *****************************************************************************
1157 * This function is called when a non fatal signal is received by the program.
1158 *****************************************************************************/
1159 static void SimpleSignalHandler( int i_signal )
1163 /* Acknowledge the signal received and warn all the p_vlc structures */
1164 vlc_mutex_lock( &global_lock );
1165 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1167 msg_Warn( pp_vlc[ i_index ], "ignoring signal %d", i_signal );
1169 vlc_mutex_unlock( &global_lock );
1172 /*****************************************************************************
1173 * FatalSignalHandler: system signal handler
1174 *****************************************************************************
1175 * This function is called when a fatal signal is received by the program.
1176 * It tries to end the program in a clean way.
1177 *****************************************************************************/
1178 static void FatalSignalHandler( int i_signal )
1180 static mtime_t abort_time = 0;
1181 static volatile vlc_bool_t b_die = VLC_FALSE;
1184 /* Once a signal has been trapped, the termination sequence will be
1185 * armed and following signals will be ignored to avoid sending messages
1186 * to an interface having been destroyed */
1191 abort_time = mdate();
1193 fprintf( stderr, "signal %d received, terminating libvlc - do it "
1194 "again in case your process gets stuck\n", i_signal );
1196 /* Try to terminate everything - this is done by requesting the end of
1197 * all the p_vlc structures */
1198 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1200 /* Acknowledge the signal received */
1201 pp_vlc[ i_index ]->b_die = VLC_TRUE;
1204 else if( mdate() > abort_time + 1000000 )
1206 /* If user asks again 1 second later, die badly */
1207 signal( SIGINT, SIG_IGN );
1208 signal( SIGHUP, SIG_IGN );
1209 signal( SIGQUIT, SIG_IGN );
1211 fprintf( stderr, "user insisted too much, dying badly\n" );