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.18 2002/07/21 18:57:02 sigmunau 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 int volatile i_vlc = 0;
95 static int volatile i_unique = 0;
96 static vlc_t ** volatile 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_error_t vlc_create( void )
125 vlc_t * p_vlc = vlc_create_r();
126 return p_vlc ? VLC_SUCCESS : VLC_EGENERIC;
129 vlc_t * vlc_create_r( void )
131 vlc_t * p_vlc = NULL;
133 /* Allocate the main structure */
134 p_vlc = vlc_object_create( p_vlc, VLC_OBJECT_ROOT );
140 p_vlc->psz_object_name = "root";
142 p_vlc->p_global_lock = &global_lock;
143 p_vlc->pp_global_data = &p_global_data;
145 p_vlc->b_verbose = VLC_FALSE;
146 p_vlc->b_quiet = VLC_FALSE; /* FIXME: delay message queue output! */
148 /* Initialize the threads system */
149 vlc_threads_init( p_vlc );
151 /* Initialize mutexes */
152 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
153 vlc_mutex_init( p_vlc, &p_vlc->structure_lock );
155 /* Set signal handling policy for all threads */
157 InitSignalHandler( );
160 /* Store our newly allocated structure in the global list */
161 vlc_mutex_lock( p_vlc->p_global_lock );
162 pp_vlc = realloc( pp_vlc, (i_vlc+1) * sizeof( vlc_t * ) );
163 pp_vlc[ i_vlc ] = p_vlc;
165 p_vlc->i_unique = i_unique;
167 vlc_mutex_unlock( p_vlc->p_global_lock );
169 /* Update the handle status */
170 p_vlc->i_status = VLC_STATUS_CREATED;
175 /*****************************************************************************
176 * vlc_init: initialize a vlc_t structure.
177 *****************************************************************************
178 * This function initializes a previously allocated vlc_t structure:
180 * - gettext initialization
181 * - message queue, module bank and playlist initialization
182 * - configuration and commandline parsing
183 *****************************************************************************/
184 vlc_error_t vlc_init( int i_argc, char *ppsz_argv[] )
186 return vlc_init_r( ( i_vlc == 1 ) ? *pp_vlc : NULL, i_argc, ppsz_argv );
189 vlc_error_t vlc_init_r( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
191 char p_capabilities[200];
194 module_t *p_help_module;
195 playlist_t *p_playlist;
197 /* Check that the handle is valid */
198 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
200 fprintf( stderr, "error: invalid status (!CREATED)\n" );
204 fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
206 /* Guess what CPU we have */
207 p_vlc->i_cpu_capabilities = CPUCapabilities( p_vlc );
210 * Support for gettext
212 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
213 # if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
214 if( !setlocale( LC_MESSAGES, "" ) )
216 fprintf( stderr, "warning: unsupported locale settings\n" );
219 setlocale( LC_CTYPE, "" );
222 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
224 fprintf( stderr, "warning: no domain %s in directory %s\n",
225 PACKAGE, LOCALEDIR );
228 textdomain( PACKAGE );
232 * System specific initialization code
234 system_Init( p_vlc, &i_argc, ppsz_argv );
237 * Initialize message queue
241 /* Get the executable name (similar to the basename command) */
244 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
247 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
253 p_vlc->psz_object_name = "vlc";
256 /* Announce who we are */
257 msg_Dbg( p_vlc, COPYRIGHT_MESSAGE );
258 msg_Dbg( p_vlc, "libvlc was configured with %s", CONFIGURE_LINE );
261 * Initialize the module bank and and load the configuration of the main
262 * module. We need to do this at this stage to be able to display a short
263 * help if required by the user. (short help == main module options)
265 module_InitBank( p_vlc );
266 module_LoadMain( p_vlc );
268 /* Hack: insert the help module here */
269 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
270 if( p_help_module == NULL )
272 module_EndBank( p_vlc );
273 msg_Destroy( p_vlc );
276 p_help_module->psz_object_name = "help";
277 config_Duplicate( p_help_module, p_help_config );
278 p_help_module->next = p_vlc->module_bank.first;
279 p_vlc->module_bank.first = p_help_module;
282 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
284 p_vlc->module_bank.first = p_help_module->next;
285 config_Free( p_help_module );
286 vlc_object_destroy( p_help_module );
287 module_EndBank( p_vlc );
288 msg_Destroy( p_vlc );
292 /* Check for short help option */
293 if( config_GetInt( p_vlc, "help" ) )
295 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n"),
296 p_vlc->psz_object_name );
298 Usage( p_vlc, "help" );
299 Usage( p_vlc, "main" );
300 p_vlc->module_bank.first = p_help_module->next;
301 config_Free( p_help_module );
302 vlc_object_destroy( p_help_module );
303 module_EndBank( p_vlc );
304 msg_Destroy( p_vlc );
308 /* Check for version option */
309 if( config_GetInt( p_vlc, "version" ) )
312 p_vlc->module_bank.first = p_help_module->next;
313 config_Free( p_help_module );
314 vlc_object_destroy( p_help_module );
315 module_EndBank( p_vlc );
316 msg_Destroy( p_vlc );
320 /* Check for build option */
321 if( config_GetInt( p_vlc, "build" ) )
324 p_vlc->module_bank.first = p_help_module->next;
325 config_Free( p_help_module );
326 vlc_object_destroy( p_help_module );
327 module_EndBank( p_vlc );
328 msg_Destroy( p_vlc );
332 /* Hack: remove the help module here */
333 p_vlc->module_bank.first = p_help_module->next;
337 * Load the builtins and plugins into the module_bank.
338 * We have to do it before config_Load*() because this also gets the
339 * list of configuration options exported by each module and loads their
342 module_LoadBuiltins( p_vlc );
343 module_LoadPlugins( p_vlc );
344 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
345 p_vlc->module_bank.i_count );
347 /* Hack: insert the help module here */
348 p_help_module->next = p_vlc->module_bank.first;
349 p_vlc->module_bank.first = p_help_module;
352 /* Check for help on modules */
353 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
355 Usage( p_vlc, p_tmp );
357 p_vlc->module_bank.first = p_help_module->next;
358 config_Free( p_help_module );
359 vlc_object_destroy( p_help_module );
360 module_EndBank( p_vlc );
361 msg_Destroy( p_vlc );
365 /* Check for long help option */
366 if( config_GetInt( p_vlc, "longhelp" ) )
368 Usage( p_vlc, NULL );
369 p_vlc->module_bank.first = p_help_module->next;
370 config_Free( p_help_module );
371 vlc_object_destroy( p_help_module );
372 module_EndBank( p_vlc );
373 msg_Destroy( p_vlc );
377 /* Check for module list option */
378 if( config_GetInt( p_vlc, "list" ) )
380 ListModules( p_vlc );
381 p_vlc->module_bank.first = p_help_module->next;
382 config_Free( p_help_module );
383 vlc_object_destroy( p_help_module );
384 module_EndBank( p_vlc );
385 msg_Destroy( p_vlc );
389 /* Hack: remove the help module here */
390 p_vlc->module_bank.first = p_help_module->next;
391 config_Free( p_help_module );
392 vlc_object_destroy( p_help_module );
396 * Override default configuration with config file settings
398 p_vlc->psz_homedir = config_GetHomeDir();
399 config_LoadConfigFile( p_vlc, NULL );
402 * Override configuration with command line settings
404 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
408 /* Pause the console because it's destroyed when we exit */
409 fprintf( stderr, "The command line options couldn't be loaded, check "
410 "that they are valid.\nPress the RETURN key to continue..." );
413 module_EndBank( p_vlc );
414 msg_Destroy( p_vlc );
419 * System specific configuration
421 system_Configure( p_vlc );
424 * Output messages that may still be in the queue
426 p_vlc->b_verbose = config_GetInt( p_vlc, "verbose" );
427 p_vlc->b_quiet = config_GetInt( p_vlc, "quiet" );
428 p_vlc->b_color = config_GetInt( p_vlc, "color" );
431 /* p_vlc inititalization. FIXME ? */
432 p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
433 if( !config_GetInt( p_vlc, "mmx" ) )
434 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
435 if( !config_GetInt( p_vlc, "3dn" ) )
436 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
437 if( !config_GetInt( p_vlc, "mmxext" ) )
438 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
439 if( !config_GetInt( p_vlc, "sse" ) )
440 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
441 if( !config_GetInt( p_vlc, "altivec" ) )
442 p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
444 #define PRINT_CAPABILITY( capability, string ) \
445 if( p_vlc->i_cpu_capabilities & capability ) \
447 strncat( p_capabilities, string " ", \
448 sizeof(p_capabilities) - strlen(p_capabilities) ); \
449 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
452 p_capabilities[0] = '\0';
453 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
454 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
455 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
456 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
457 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
458 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
459 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
460 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
461 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
462 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
465 * Choose the best memcpy module
467 psz_module = config_GetPsz( p_vlc, "memcpy" );
468 p_vlc->p_memcpy_module =
469 module_Need( p_vlc, MODULE_CAPABILITY_MEMCPY, psz_module, NULL );
470 if( psz_module ) free( psz_module );
471 if( p_vlc->p_memcpy_module == NULL )
473 msg_Err( p_vlc, "no suitable memcpy module, using libc default" );
474 p_vlc->pf_memcpy = memcpy;
478 p_vlc->pf_memcpy = p_vlc->p_memcpy_module->p_functions
479 ->memcpy.functions.memcpy.pf_memcpy;
483 * Initialize shared resources and libraries
485 if( config_GetInt( p_vlc, "network-channel" )
486 && network_ChannelCreate( p_vlc ) )
488 /* On error during Channels initialization, switch off channels */
490 "channels initialization failed, deactivating channels" );
491 config_PutInt( p_vlc, "network-channel", VLC_FALSE );
495 * Initialize playlist and get commandline files
497 p_playlist = playlist_Create( p_vlc );
500 msg_Err( p_vlc, "playlist initialization failed" );
501 if( p_vlc->p_memcpy_module != NULL )
503 module_Unneed( p_vlc->p_memcpy_module );
505 module_EndBank( p_vlc );
506 msg_Destroy( p_vlc );
510 /* Update the handle status */
511 p_vlc->i_status = VLC_STATUS_STOPPED;
514 * Get input filenames given as commandline arguments
516 GetFilenames( p_vlc, i_argc, ppsz_argv );
521 /*****************************************************************************
523 *****************************************************************************
524 * XXX: This function opens an interface plugin and runs it. If b_block is set
525 * to 0, vlc_add_intf will return immediately and let the interface run in a
526 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
527 * user requests to quit.
528 *****************************************************************************/
529 vlc_error_t vlc_run( void )
531 return vlc_run_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
534 vlc_error_t vlc_run_r( vlc_t *p_vlc )
536 /* Check that the handle is valid */
537 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
539 fprintf( stderr, "error: invalid status (!STOPPED)\n" );
543 /* Update the handle status */
544 p_vlc->i_status = VLC_STATUS_RUNNING;
549 /*****************************************************************************
550 * vlc_add_intf: add an interface
551 *****************************************************************************
552 * This function opens an interface plugin and runs it. If b_block is set
553 * to 0, vlc_add_intf will return immediately and let the interface run in a
554 * separate thread. If b_block is set to 1, vlc_add_intf will continue until
555 * user requests to quit.
556 *****************************************************************************/
557 vlc_error_t vlc_add_intf( const char *psz_module, vlc_bool_t b_block )
559 return vlc_add_intf_r( ( i_vlc == 1 ) ? *pp_vlc : NULL,
560 psz_module, b_block );
563 vlc_error_t vlc_add_intf_r( vlc_t *p_vlc, const char *psz_module,
567 intf_thread_t *p_intf;
568 char *psz_oldmodule = NULL;
570 /* Check that the handle is valid */
571 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
573 fprintf( stderr, "error: invalid status (!RUNNING)\n" );
579 psz_oldmodule = config_GetPsz( p_vlc, "intf" );
580 config_PutPsz( p_vlc, "intf", psz_module );
583 /* Try to create the interface */
584 p_intf = intf_Create( p_vlc );
588 config_PutPsz( p_vlc, "intf", psz_oldmodule );
591 free( psz_oldmodule );
597 msg_Err( p_vlc, "interface initialization failed" );
601 /* Try to run the interface */
602 p_intf->b_block = b_block;
603 err = intf_RunThread( p_intf );
606 vlc_object_detach_all( p_intf );
607 intf_Destroy( p_intf );
614 /*****************************************************************************
615 * vlc_stop: stop playing.
616 *****************************************************************************
617 * This function requests the interface threads to finish, waits for their
618 * termination, and destroys their structure.
619 *****************************************************************************/
620 vlc_error_t vlc_stop( void )
622 return vlc_stop_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
625 vlc_error_t vlc_stop_r( vlc_t *p_vlc )
627 intf_thread_t *p_intf;
628 playlist_t *p_playlist;
629 vout_thread_t *p_vout;
630 aout_thread_t *p_aout;
632 /* Check that the handle is valid */
633 if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
635 fprintf( stderr, "error: invalid status (!RUNNING)\n" );
640 * Ask the interfaces to stop and destroy them
642 msg_Dbg( p_vlc, "removing all interfaces" );
643 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
645 intf_StopThread( p_intf );
646 vlc_object_detach_all( p_intf );
647 vlc_object_release( p_intf );
648 intf_Destroy( p_intf );
654 msg_Dbg( p_vlc, "removing all playlists" );
655 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
658 vlc_object_detach_all( p_playlist );
659 vlc_object_release( p_playlist );
660 playlist_Destroy( p_playlist );
666 msg_Dbg( p_vlc, "removing all video outputs" );
667 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
669 vlc_object_detach_all( p_vout );
670 vlc_object_release( p_vout );
671 vout_DestroyThread( p_vout );
677 msg_Dbg( p_vlc, "removing all audio outputs" );
678 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
680 vlc_object_detach_all( p_aout );
681 vlc_object_release( p_aout );
682 aout_DestroyThread( p_aout );
685 /* Update the handle status */
686 p_vlc->i_status = VLC_STATUS_STOPPED;
691 /*****************************************************************************
692 * vlc_end: uninitialize everything.
693 *****************************************************************************
694 * This function uninitializes every vlc component that was activated in
695 * vlc_init: audio and video outputs, playlist, module bank and message queue.
696 *****************************************************************************/
697 vlc_error_t vlc_end( void )
699 return vlc_end_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
702 vlc_error_t vlc_end_r( vlc_t *p_vlc )
704 /* Check that the handle is valid */
705 if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
707 fprintf( stderr, "error: invalid status (!STOPPED)\n" );
712 * Go back into channel 0 which is the network
714 if( config_GetInt( p_vlc, "network-channel" ) && p_vlc->p_channel )
716 network_ChannelJoin( p_vlc, COMMON_CHANNEL );
720 * Free allocated memory
722 if( p_vlc->p_memcpy_module != NULL )
724 module_Unneed( p_vlc->p_memcpy_module );
727 free( p_vlc->psz_homedir );
732 module_EndBank( p_vlc );
735 * System specific cleaning code
740 * Terminate messages interface and program
742 msg_Destroy( p_vlc );
744 /* Update the handle status */
745 p_vlc->i_status = VLC_STATUS_CREATED;
750 /*****************************************************************************
751 * vlc_destroy: free allocated resources.
752 *****************************************************************************
753 * This function frees the previously allocated vlc_t structure.
754 *****************************************************************************/
755 vlc_error_t vlc_destroy( void )
757 return vlc_destroy_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
760 vlc_error_t vlc_destroy_r( vlc_t *p_vlc )
764 /* Check that the handle is valid */
765 if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
767 fprintf( stderr, "error: invalid status (!CREATED)\n" );
771 /* Update the handle status, just in case */
772 p_vlc->i_status = VLC_STATUS_NONE;
774 /* Remove our structure from the global list */
775 vlc_mutex_lock( p_vlc->p_global_lock );
776 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
778 if( pp_vlc[ i_index ] == p_vlc )
784 if( i_index == i_vlc )
786 fprintf( stderr, "error: trying to unregister %p which is not in "
787 "the list\n", p_vlc );
788 vlc_mutex_unlock( p_vlc->p_global_lock );
789 vlc_object_destroy( p_vlc );
793 for( i_index++ ; i_index < i_vlc ; i_index++ )
795 pp_vlc[ i_index - 1 ] = pp_vlc[ i_index ];
801 pp_vlc = realloc( pp_vlc, i_vlc * sizeof( vlc_t * ) );
808 vlc_mutex_unlock( p_vlc->p_global_lock );
810 /* Stop thread system: last one out please shut the door! */
811 vlc_threads_end( p_vlc );
813 /* Destroy mutexes */
814 vlc_mutex_destroy( &p_vlc->structure_lock );
815 vlc_mutex_destroy( &p_vlc->config_lock );
817 vlc_object_destroy( p_vlc );
822 vlc_status_t vlc_status( void )
824 return vlc_status_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
827 vlc_status_t vlc_status_r( vlc_t *p_vlc )
831 return VLC_STATUS_NONE;
834 return p_vlc->i_status;
837 vlc_error_t vlc_add_target( const char *psz_target, int i_mode, int i_pos )
839 return vlc_add_target_r( ( i_vlc == 1 ) ? *pp_vlc : NULL,
840 psz_target, i_mode, i_pos );
843 vlc_error_t vlc_add_target_r( vlc_t *p_vlc, const char *psz_target,
844 int i_mode, int i_pos )
847 playlist_t *p_playlist;
849 if( !p_vlc || ( p_vlc->i_status != VLC_STATUS_STOPPED
850 && p_vlc->i_status != VLC_STATUS_RUNNING ) )
852 fprintf( stderr, "error: invalid status (!STOPPED&&!RUNNING)\n" );
856 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
858 if( p_playlist == NULL )
860 msg_Dbg( p_vlc, "no playlist present, creating one" );
861 p_playlist = playlist_Create( p_vlc );
863 if( p_playlist == NULL )
868 vlc_object_yield( p_playlist );
871 err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
873 vlc_object_release( p_playlist );
878 /* following functions are local */
880 /*****************************************************************************
881 * GetFilenames: parse command line options which are not flags
882 *****************************************************************************
883 * Parse command line for input files.
884 *****************************************************************************/
885 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
889 /* We assume that the remaining parameters are filenames */
890 for( i_opt = optind; i_opt < i_argc; i_opt++ )
892 vlc_add_target_r( p_vlc, ppsz_argv[ i_opt ],
893 PLAYLIST_APPEND, PLAYLIST_END );
899 /*****************************************************************************
900 * Usage: print program usage
901 *****************************************************************************
902 * Print a short inline help. Message interface is initialized at this stage.
903 *****************************************************************************/
904 static void Usage( vlc_t *p_this, const char *psz_module_name )
906 #define FORMAT_STRING " --%s%s%s%s%s%s%s %s%s\n"
907 /* option name -------------' | | | | | |
908 * <bra --------------------------' | | | | |
909 * option type or "" ---------------' | | | |
910 * ket> ------------------------------' | | |
911 * padding spaces ----------------------' | |
912 * comment --------------------------------' |
913 * comment suffix ---------------------------'
915 * The purpose of having bra and ket is that we might i18n them as well.
918 #define PADDING_SPACES 25
920 module_config_t *p_item;
921 char psz_spaces[PADDING_SPACES+LINE_START+1];
922 char psz_format[sizeof(FORMAT_STRING)];
924 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
925 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
927 strcpy( psz_format, FORMAT_STRING );
933 /* Enumerate the config for each module */
934 for( p_module = p_this->p_vlc->module_bank.first ;
936 p_module = p_module->next )
938 vlc_bool_t b_help_module = !strcmp( "help", p_module->psz_object_name );
940 if( psz_module_name && strcmp( psz_module_name,
941 p_module->psz_object_name ) )
946 /* Ignore modules without config options */
947 if( !p_module->i_config_items )
952 /* Print module name */
953 fprintf( stderr, _("%s module options:\n\n"),
954 p_module->psz_object_name );
956 for( p_item = p_module->p_config;
957 p_item->i_type != CONFIG_HINT_END;
960 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
961 char *psz_suf = "", *psz_prefix = NULL;
964 switch( p_item->i_type )
966 case CONFIG_HINT_CATEGORY:
967 case CONFIG_HINT_USAGE:
968 fprintf( stderr, " %s\n", p_item->psz_text );
971 case CONFIG_ITEM_STRING:
972 case CONFIG_ITEM_FILE:
973 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
974 if( !p_item->ppsz_list )
976 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
982 psz_type = malloc( 1000 );
983 memset( psz_type, 0, 1000 );
984 for( i=0; p_item->ppsz_list[i]; i++ )
986 strcat( psz_type, p_item->ppsz_list[i] );
987 strcat( psz_type, "|" );
989 psz_type[ strlen( psz_type ) - 1 ] = '\0';
993 case CONFIG_ITEM_INTEGER:
994 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
996 case CONFIG_ITEM_FLOAT:
997 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
999 case CONFIG_ITEM_BOOL:
1000 psz_bra = ""; psz_type = ""; psz_ket = "";
1001 if( !b_help_module )
1003 psz_suf = p_item->i_value ? _(" (default enabled)") :
1004 _(" (default disabled)");
1009 /* Add short option */
1010 if( p_item->i_short )
1012 psz_format[2] = '-';
1013 psz_format[3] = p_item->i_short;
1014 psz_format[4] = ',';
1018 psz_format[2] = ' ';
1019 psz_format[3] = ' ';
1020 psz_format[4] = ' ';
1025 i = PADDING_SPACES - strlen( p_item->psz_name )
1026 - strlen( psz_bra ) - strlen( psz_type )
1027 - strlen( psz_ket ) - 1;
1028 if( p_item->i_type == CONFIG_ITEM_BOOL
1031 vlc_bool_t b_dash = VLC_FALSE;
1032 psz_prefix = p_item->psz_name;
1033 while( *psz_prefix )
1035 if( *psz_prefix++ == '-' )
1044 psz_prefix = ", --no-";
1045 i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
1049 psz_prefix = ", --no";
1050 i -= strlen( p_item->psz_name ) + strlen( ", --no" );
1057 psz_spaces[i] = '\n';
1061 psz_spaces[i] = '\0';
1064 if( p_item->i_type == CONFIG_ITEM_BOOL &&
1067 fprintf( stderr, psz_format, p_item->psz_name, psz_prefix,
1068 p_item->psz_name, psz_bra, psz_type, psz_ket,
1069 psz_spaces, p_item->psz_text, psz_suf );
1073 fprintf( stderr, psz_format, p_item->psz_name, "", "",
1074 psz_bra, psz_type, psz_ket, psz_spaces,
1075 p_item->psz_text, psz_suf );
1077 psz_spaces[i] = ' ';
1078 if ( p_item->ppsz_list )
1085 fprintf( stderr, "\n" );
1089 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1090 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1095 /*****************************************************************************
1096 * ListModules: list the available modules with their description
1097 *****************************************************************************
1098 * Print a list of all available modules (builtins and plugins) and a short
1099 * description for each one.
1100 *****************************************************************************/
1101 static void ListModules( vlc_t *p_this )
1104 char psz_spaces[22];
1106 memset( psz_spaces, ' ', 22 );
1113 fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n\n"),
1114 p_this->p_vlc->psz_object_name );
1116 fprintf( stderr, _("[module] [description]\n") );
1118 /* Enumerate each module */
1119 for( p_module = p_this->p_vlc->module_bank.first ;
1121 p_module = p_module->next )
1125 /* Nasty hack, but right now I'm too tired to think about a nice
1127 i = 22 - strlen( p_module->psz_object_name ) - 1;
1131 fprintf( stderr, " %s%s %s\n", p_module->psz_object_name, psz_spaces,
1132 p_module->psz_longname );
1134 psz_spaces[i] = ' ';
1138 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1139 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1144 /*****************************************************************************
1145 * Version: print complete program version
1146 *****************************************************************************
1147 * Print complete program version and build number.
1148 *****************************************************************************/
1149 static void Version( void )
1155 fprintf( stderr, VERSION_MESSAGE "\n" );
1157 _("This program comes with NO WARRANTY, to the extent permitted by "
1158 "law.\nYou may redistribute it under the terms of the GNU General "
1159 "Public License;\nsee the file named COPYING for details.\n"
1160 "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1162 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1163 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1168 /*****************************************************************************
1169 * Build: print information about the vlc build
1170 *****************************************************************************
1171 * Print the ./configure command line and other information.
1172 *****************************************************************************/
1173 static void Build( void )
1179 fprintf( stderr, "configured with %s\n", CONFIGURE_LINE );
1181 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1182 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1187 /*****************************************************************************
1188 * ShowConsole: On Win32, create an output console for debug messages
1189 *****************************************************************************
1190 * This function is useful only on Win32.
1191 *****************************************************************************/
1193 static void ShowConsole( void )
1196 freopen( "CONOUT$", "w", stdout );
1197 freopen( "CONOUT$", "w", stderr );
1198 freopen( "CONIN$", "r", stdin );
1204 /*****************************************************************************
1205 * InitSignalHandler: system signal handler initialization
1206 *****************************************************************************
1207 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1208 * at least a method to kill the program when all other methods failed, and
1209 * when we don't want to use SIGKILL.
1210 *****************************************************************************/
1211 static void InitSignalHandler( void )
1213 /* Termination signals */
1214 signal( SIGINT, FatalSignalHandler );
1215 signal( SIGHUP, FatalSignalHandler );
1216 signal( SIGQUIT, FatalSignalHandler );
1219 signal( SIGALRM, SimpleSignalHandler );
1220 signal( SIGPIPE, SimpleSignalHandler );
1223 /*****************************************************************************
1224 * SimpleSignalHandler: system signal handler
1225 *****************************************************************************
1226 * This function is called when a non fatal signal is received by the program.
1227 *****************************************************************************/
1228 static void SimpleSignalHandler( int i_signal )
1232 /* Acknowledge the signal received and warn all the p_vlc structures */
1233 vlc_mutex_lock( &global_lock );
1234 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1236 msg_Warn( pp_vlc[ i_index ], "ignoring signal %d", i_signal );
1238 vlc_mutex_unlock( &global_lock );
1241 /*****************************************************************************
1242 * FatalSignalHandler: system signal handler
1243 *****************************************************************************
1244 * This function is called when a fatal signal is received by the program.
1245 * It tries to end the program in a clean way.
1246 *****************************************************************************/
1247 static void FatalSignalHandler( int i_signal )
1249 static mtime_t abort_time = 0;
1250 static volatile vlc_bool_t b_die = VLC_FALSE;
1253 /* Once a signal has been trapped, the termination sequence will be
1254 * armed and following signals will be ignored to avoid sending messages
1255 * to an interface having been destroyed */
1260 abort_time = mdate();
1262 fprintf( stderr, "signal %d received, terminating libvlc - do it "
1263 "again in case your process gets stuck\n", i_signal );
1265 /* Try to terminate everything - this is done by requesting the end of
1266 * all the p_vlc structures */
1267 for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1269 /* Acknowledge the signal received */
1270 pp_vlc[ i_index ]->b_die = VLC_TRUE;
1273 else if( mdate() > abort_time + 1000000 )
1275 /* If user asks again 1 second later, die badly */
1276 signal( SIGINT, SIG_IGN );
1277 signal( SIGHUP, SIG_IGN );
1278 signal( SIGQUIT, SIG_IGN );
1280 fprintf( stderr, "user insisted too much, dying badly\n" );