1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: libvlc.c,v 1.112 2004/01/25 11:48:17 gbazin Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Pretend we are a builtin module
28 *****************************************************************************/
29 #define MODULE_NAME main
30 #define MODULE_PATH main
33 /*****************************************************************************
35 *****************************************************************************/
39 # include <errno.h> /* ENOMEM */
41 #include <stdio.h> /* sprintf() */
42 #include <string.h> /* strerror() */
43 #include <stdlib.h> /* free() */
46 # include <netinet/in.h> /* BSD: struct in_addr */
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
55 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
56 # include "extras/getopt.h"
63 #include "vlc_cpu.h" /* CPU detection */
64 #include "os_specific.h"
66 #include "vlc_error.h"
68 #include "stream_control.h"
69 #include "input_ext-intf.h"
71 #include "vlc_playlist.h"
72 #include "vlc_interface.h"
74 #include "audio_output.h"
76 #include "vlc_video.h"
77 #include "video_output.h"
81 /*****************************************************************************
82 * The evil global variable. We handle it with care, don't worry.
83 *****************************************************************************/
84 static libvlc_t libvlc;
85 static libvlc_t * p_libvlc;
86 static vlc_t * p_static_vlc;
88 /*****************************************************************************
90 *****************************************************************************/
91 static void SetLanguage ( char const * );
92 static int GetFilenames ( vlc_t *, int, char *[] );
93 static void Usage ( vlc_t *, char const *psz_module_name );
94 static void ListModules ( vlc_t * );
95 static void Version ( void );
98 static void ShowConsole ( void );
100 static int ConsoleWidth ( void );
102 static int VerboseCallback( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
105 /*****************************************************************************
106 * vlc_current_object: return the current object.
107 *****************************************************************************
108 * If i_object is non-zero, return the corresponding object. Otherwise,
109 * return the statically allocated p_vlc object.
110 *****************************************************************************/
111 vlc_t * vlc_current_object( int i_object )
115 return vlc_object_get( p_libvlc, i_object );
121 /*****************************************************************************
122 * VLC_Version: return the libvlc version.
123 *****************************************************************************
124 * This function returns full version string (numeric version and codename).
125 *****************************************************************************/
126 char const * VLC_Version( void )
128 return VERSION_MESSAGE;
131 /*****************************************************************************
132 * VLC_Error: strerror() equivalent
133 *****************************************************************************
134 * This function returns full version string (numeric version and codename).
135 *****************************************************************************/
136 char const * VLC_Error( int i_err )
138 return vlc_error( i_err );
141 /*****************************************************************************
142 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
143 *****************************************************************************
144 * This function allocates a vlc_t structure and returns a negative value
145 * in case of failure. Also, the thread system is initialized.
146 *****************************************************************************/
147 int VLC_Create( void )
150 vlc_t * p_vlc = NULL;
153 /* &libvlc never changes, so we can safely call this multiple times. */
156 /* vlc_threads_init *must* be the first internal call! No other call is
157 * allowed before the thread system has been initialized. */
158 i_ret = vlc_threads_init( p_libvlc );
164 /* Now that the thread system is initialized, we don't have much, but
165 * at least we have var_Create */
166 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
167 var_Get( p_libvlc, "libvlc", &lockval );
168 vlc_mutex_lock( lockval.p_address );
169 if( !libvlc.b_ready )
173 /* Guess what CPU we have */
174 libvlc.i_cpu = CPUCapabilities();
176 /* Find verbosity from VLC_VERBOSE environment variable */
177 psz_env = getenv( "VLC_VERBOSE" );
178 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
180 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
181 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
183 libvlc.b_color = VLC_FALSE;
186 /* Initialize message queue */
187 msg_Create( p_libvlc );
189 /* Announce who we are */
190 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
191 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
193 /* The module bank will be initialized later */
194 libvlc.p_module_bank = NULL;
196 libvlc.b_ready = VLC_TRUE;
198 vlc_mutex_unlock( lockval.p_address );
199 var_Destroy( p_libvlc, "libvlc" );
201 /* Allocate a vlc object */
202 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
207 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
209 p_vlc->psz_object_name = "root";
211 /* Initialize mutexes */
212 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
214 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
217 /* Store our newly allocated structure in the global list */
218 vlc_object_attach( p_vlc, p_libvlc );
220 /* Store data for the non-reentrant API */
221 p_static_vlc = p_vlc;
223 return p_vlc->i_object_id;
226 /*****************************************************************************
227 * VLC_Init: initialize a vlc_t structure.
228 *****************************************************************************
229 * This function initializes a previously allocated vlc_t structure:
231 * - gettext initialization
232 * - message queue, module bank and playlist initialization
233 * - configuration and commandline parsing
234 *****************************************************************************/
235 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
237 char p_capabilities[200];
242 vlc_bool_t b_exit = VLC_FALSE;
243 vlc_t * p_vlc = vlc_current_object( i_object );
244 module_t *p_help_module;
245 playlist_t *p_playlist;
254 * System specific initialization code
256 system_Init( p_vlc, &i_argc, ppsz_argv );
258 /* Get the executable name (similar to the basename command) */
261 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
264 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
270 p_vlc->psz_object_name = "vlc";
274 * Support for gettext
278 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
279 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
281 /* Initialize the module bank and load the configuration of the
282 * main module. We need to do this at this stage to be able to display
283 * a short help if required by the user. (short help == main module
285 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
286 var_Get( p_libvlc, "libvlc", &lockval );
287 vlc_mutex_lock( lockval.p_address );
288 if( libvlc.p_module_bank == NULL )
290 module_InitBank( p_vlc );
291 module_LoadMain( p_vlc );
293 vlc_mutex_unlock( lockval.p_address );
294 var_Destroy( p_libvlc, "libvlc" );
296 /* Hack: insert the help module here */
297 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
298 if( p_help_module == NULL )
300 /*module_EndBank( p_vlc );*/
301 if( i_object ) vlc_object_release( p_vlc );
304 p_help_module->psz_object_name = "help";
305 p_help_module->psz_longname = N_("Help options");
306 config_Duplicate( p_help_module, p_help_config );
307 vlc_object_attach( p_help_module, libvlc.p_module_bank );
310 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
312 vlc_object_detach( p_help_module );
313 config_Free( p_help_module );
314 vlc_object_destroy( p_help_module );
315 /*module_EndBank( p_vlc );*/
316 if( i_object ) vlc_object_release( p_vlc );
320 /* Check for short help option */
321 if( config_GetInt( p_vlc, "help" ) )
323 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
324 p_vlc->psz_object_name );
325 Usage( p_vlc, "main" );
326 Usage( p_vlc, "help" );
329 /* Check for version option */
330 else if( config_GetInt( p_vlc, "version" ) )
336 /* Set the config file stuff */
337 p_vlc->psz_homedir = config_GetHomeDir();
338 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
340 /* Hack: remove the help module here */
341 vlc_object_detach( p_help_module );
346 config_Free( p_help_module );
347 vlc_object_destroy( p_help_module );
348 /*module_EndBank( p_vlc );*/
349 if( i_object ) vlc_object_release( p_vlc );
353 /* Check for translation config option */
354 #if defined( ENABLE_NLS ) \
355 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
357 /* This ain't really nice to have to reload the config here but it seems
358 * the only way to do it. */
359 config_LoadConfigFile( p_vlc, "main" );
360 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
362 /* Check if the user specified a custom language */
363 psz_language = config_GetPsz( p_vlc, "language" );
364 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
366 /* Reset the default domain */
367 SetLanguage( psz_language );
369 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
370 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
372 textdomain( PACKAGE );
374 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
375 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
376 /* BeOS only support UTF8 strings */
377 /* Mac OS X prefers UTF8 */
378 bind_textdomain_codeset( PACKAGE, "UTF-8" );
381 module_EndBank( p_vlc );
382 module_InitBank( p_vlc );
383 module_LoadMain( p_vlc );
384 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
386 if( psz_language ) free( psz_language );
390 * Load the builtins and plugins into the module_bank.
391 * We have to do it before config_Load*() because this also gets the
392 * list of configuration options exported by each module and loads their
395 module_LoadBuiltins( p_vlc );
396 module_LoadPlugins( p_vlc );
402 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
403 libvlc.p_module_bank->i_children );
405 /* Hack: insert the help module here */
406 vlc_object_attach( p_help_module, libvlc.p_module_bank );
409 /* Check for help on modules */
410 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
412 Usage( p_vlc, p_tmp );
416 /* Check for long help option */
417 else if( config_GetInt( p_vlc, "longhelp" ) )
419 Usage( p_vlc, NULL );
422 /* Check for module list option */
423 else if( config_GetInt( p_vlc, "list" ) )
425 ListModules( p_vlc );
429 /* Check for config file options */
430 if( config_GetInt( p_vlc, "reset-config" ) )
432 vlc_object_detach( p_help_module );
433 config_ResetAll( p_vlc );
434 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
435 config_SaveConfigFile( p_vlc, NULL );
436 vlc_object_attach( p_help_module, libvlc.p_module_bank );
438 if( config_GetInt( p_vlc, "save-config" ) )
440 vlc_object_detach( p_help_module );
441 config_LoadConfigFile( p_vlc, NULL );
442 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
443 config_SaveConfigFile( p_vlc, NULL );
444 vlc_object_attach( p_help_module, libvlc.p_module_bank );
447 /* Hack: remove the help module here */
448 vlc_object_detach( p_help_module );
453 config_Free( p_help_module );
454 vlc_object_destroy( p_help_module );
455 /*module_EndBank( p_vlc );*/
456 if( i_object ) vlc_object_release( p_vlc );
461 * Override default configuration with config file settings
463 config_LoadConfigFile( p_vlc, NULL );
465 /* Hack: insert the help module here */
466 vlc_object_attach( p_help_module, libvlc.p_module_bank );
470 * Override configuration with command line settings
472 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
476 /* Pause the console because it's destroyed when we exit */
477 fprintf( stderr, "The command line options couldn't be loaded, check "
478 "that they are valid.\nPress the RETURN key to continue..." );
481 vlc_object_detach( p_help_module );
482 config_Free( p_help_module );
483 vlc_object_destroy( p_help_module );
484 /*module_EndBank( p_vlc );*/
485 if( i_object ) vlc_object_release( p_vlc );
489 /* Hack: remove the help module here */
490 vlc_object_detach( p_help_module );
491 config_Free( p_help_module );
492 vlc_object_destroy( p_help_module );
496 * System specific configuration
498 system_Configure( p_vlc, &i_argc, ppsz_argv );
501 * Message queue options
504 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505 if( config_GetInt( p_vlc, "quiet" ) )
509 var_Set( p_vlc, "verbose", val );
511 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
512 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
514 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
517 * Output messages that may still be in the queue
521 /* p_vlc initialization. FIXME ? */
523 #if defined( __i386__ )
524 if( !config_GetInt( p_vlc, "mmx" ) )
525 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
526 if( !config_GetInt( p_vlc, "3dn" ) )
527 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
528 if( !config_GetInt( p_vlc, "mmxext" ) )
529 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
530 if( !config_GetInt( p_vlc, "sse" ) )
531 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
533 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
534 if( !config_GetInt( p_vlc, "altivec" ) )
535 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
538 #define PRINT_CAPABILITY( capability, string ) \
539 if( libvlc.i_cpu & capability ) \
541 strncat( p_capabilities, string " ", \
542 sizeof(p_capabilities) - strlen(p_capabilities) ); \
543 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
546 p_capabilities[0] = '\0';
547 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
548 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
549 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
554 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
555 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
556 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
559 * Choose the best memcpy module
561 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
563 if( p_vlc->pf_memcpy == NULL )
565 p_vlc->pf_memcpy = memcpy;
568 if( p_vlc->pf_memset == NULL )
570 p_vlc->pf_memset = memset;
574 * Initialize hotkey handling
576 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
577 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
578 /* Do a copy (we don't need to modify the strings) */
579 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
582 * Initialize playlist and get commandline files
584 p_playlist = playlist_Create( p_vlc );
587 msg_Err( p_vlc, "playlist initialization failed" );
588 if( p_vlc->p_memcpy_module != NULL )
590 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
592 /*module_EndBank( p_vlc );*/
593 if( i_object ) vlc_object_release( p_vlc );
598 * Load background interfaces
600 psz_modules = config_GetPsz( p_vlc, "extraintf" );
601 psz_parser = psz_modules;
602 while ( psz_parser && *psz_parser )
604 char *psz_module, *psz_temp;
605 psz_module = psz_parser;
606 psz_parser = strchr( psz_module, ',' );
612 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
615 sprintf( psz_temp, "%s,none", psz_module );
616 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
626 * Allways load the hotkeys interface if it exists
628 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
631 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
633 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
634 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
635 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
644 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
647 * Get input filenames given as commandline arguments
649 GetFilenames( p_vlc, i_argc, ppsz_argv );
651 if( i_object ) vlc_object_release( p_vlc );
655 /*****************************************************************************
656 * VLC_AddIntf: add an interface
657 *****************************************************************************
658 * This function opens an interface plugin and runs it. If b_block is set
659 * to 0, VLC_AddIntf will return immediately and let the interface run in a
660 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
661 * user requests to quit.
662 *****************************************************************************/
663 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
666 intf_thread_t *p_intf;
667 vlc_t *p_vlc = vlc_current_object( i_object );
674 /* Try to create the interface */
675 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
679 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
680 if( i_object ) vlc_object_release( p_vlc );
684 /* Try to run the interface */
685 p_intf->b_block = b_block;
686 i_err = intf_RunThread( p_intf );
689 vlc_object_detach( p_intf );
690 intf_Destroy( p_intf );
691 if( i_object ) vlc_object_release( p_vlc );
695 if( i_object ) vlc_object_release( p_vlc );
699 /*****************************************************************************
700 * VLC_Destroy: stop playing and destroy everything.
701 *****************************************************************************
702 * This function requests the running threads to finish, waits for their
703 * termination, and destroys their structure.
704 *****************************************************************************/
705 int VLC_Destroy( int i_object )
707 vlc_t *p_vlc = vlc_current_object( i_object );
715 * Free allocated memory
717 if( p_vlc->p_memcpy_module )
719 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
720 p_vlc->p_memcpy_module = NULL;
723 if( p_vlc->psz_homedir )
725 free( p_vlc->psz_homedir );
726 p_vlc->psz_homedir = NULL;
729 if( p_vlc->psz_configfile )
731 free( p_vlc->psz_configfile );
732 p_vlc->psz_configfile = NULL;
735 if( p_vlc->p_hotkeys )
737 free( p_vlc->p_hotkeys );
738 p_vlc->p_hotkeys = NULL;
742 * XXX: Free module bank !
744 /*module_EndBank( p_vlc );*/
747 * System specific cleaning code
751 /* Destroy mutexes */
752 vlc_mutex_destroy( &p_vlc->config_lock );
754 vlc_mutex_destroy( &p_vlc->quicktime_lock );
757 vlc_object_detach( p_vlc );
759 /* Release object before destroying it */
760 if( i_object ) vlc_object_release( p_vlc );
762 vlc_object_destroy( p_vlc );
764 /* Stop thread system: last one out please shut the door! */
765 vlc_threads_end( p_libvlc );
770 /*****************************************************************************
771 * VLC_Die: ask vlc to die.
772 *****************************************************************************
773 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
774 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
775 *****************************************************************************/
776 int VLC_Die( int i_object )
778 vlc_t *p_vlc = vlc_current_object( i_object );
785 p_vlc->b_die = VLC_TRUE;
787 if( i_object ) vlc_object_release( p_vlc );
791 /*****************************************************************************
792 * VLC_AddTarget: adds a target for playing.
793 *****************************************************************************
794 * This function adds psz_target to the current playlist. If a playlist does
795 * not exist, it will create one.
796 *****************************************************************************/
797 int VLC_AddTarget( int i_object, char const *psz_target,
798 char const **ppsz_options, int i_options,
799 int i_mode, int i_pos )
803 playlist_t *p_playlist;
804 vlc_t *p_vlc = vlc_current_object( i_object );
811 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
813 if( p_playlist == NULL )
815 msg_Dbg( p_vlc, "no playlist present, creating one" );
816 p_playlist = playlist_Create( p_vlc );
818 if( p_playlist == NULL )
820 if( i_object ) vlc_object_release( p_vlc );
824 vlc_object_yield( p_playlist );
827 i_err = playlist_Add( p_playlist, psz_target, psz_target,
830 for( i = 0 ; i< i_options ; i++ )
832 playlist_AddOption( p_playlist, i_err , ppsz_options[i] );
835 vlc_object_release( p_playlist );
837 if( i_object ) vlc_object_release( p_vlc );
841 /*****************************************************************************
842 * VLC_Set: set a vlc variable
843 *****************************************************************************
845 *****************************************************************************/
846 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
848 vlc_t *p_vlc = vlc_current_object( i_object );
856 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
857 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
858 if( !strncmp( psz_var, "conf::", 6 ) )
860 module_config_t *p_item;
861 char const *psz_newvar = psz_var + 6;
863 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
867 switch( p_item->i_type )
869 case CONFIG_ITEM_BOOL:
870 config_PutInt( p_vlc, psz_newvar, value.b_bool );
872 case CONFIG_ITEM_INTEGER:
873 config_PutInt( p_vlc, psz_newvar, value.i_int );
875 case CONFIG_ITEM_FLOAT:
876 config_PutFloat( p_vlc, psz_newvar, value.f_float );
879 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
882 if( i_object ) vlc_object_release( p_vlc );
887 i_ret = var_Set( p_vlc, psz_var, value );
889 if( i_object ) vlc_object_release( p_vlc );
893 /*****************************************************************************
894 * VLC_Get: get a vlc variable
895 *****************************************************************************
897 *****************************************************************************/
898 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
900 vlc_t *p_vlc = vlc_current_object( i_object );
908 i_ret = var_Get( p_vlc, psz_var, p_value );
910 if( i_object ) vlc_object_release( p_vlc );
914 /* FIXME: temporary hacks */
917 /*****************************************************************************
918 * VLC_IsPlaying: Query for Playlist Status
919 *****************************************************************************/
920 vlc_bool_t VLC_IsPlaying( int i_object )
923 playlist_t * p_playlist;
926 vlc_t *p_vlc = vlc_current_object( i_object );
928 /* Check that the handle is valid */
934 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
938 if( i_object ) vlc_object_release( p_vlc );
942 playing = playlist_IsPlaying( p_playlist );
944 vlc_object_release( p_playlist );
946 if( i_object ) vlc_object_release( p_vlc );
953 /*****************************************************************************
954 * VLC_ClearPlaylist: Query for Playlist Status
957 *****************************************************************************/
958 int VLC_ClearPlaylist( int i_object )
961 playlist_t * p_playlist;
962 vlc_t *p_vlc = vlc_current_object( i_object );
964 /* Check that the handle is valid */
970 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
974 if( i_object ) vlc_object_release( p_vlc );
978 playlist_Clear(p_playlist);
980 vlc_object_release( p_playlist );
982 if( i_object ) vlc_object_release( p_vlc );
987 /*****************************************************************************
989 *****************************************************************************/
990 int VLC_Play( int i_object )
992 playlist_t * p_playlist;
993 vlc_t *p_vlc = vlc_current_object( i_object );
995 /* Check that the handle is valid */
1001 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1005 if( i_object ) vlc_object_release( p_vlc );
1009 vlc_mutex_lock( &p_playlist->object_lock );
1010 if( p_playlist->i_size )
1012 vlc_mutex_unlock( &p_playlist->object_lock );
1013 playlist_Play( p_playlist );
1017 vlc_mutex_unlock( &p_playlist->object_lock );
1020 vlc_object_release( p_playlist );
1022 if( i_object ) vlc_object_release( p_vlc );
1026 /*****************************************************************************
1028 *****************************************************************************/
1029 int VLC_Stop( int i_object )
1031 intf_thread_t * p_intf;
1032 playlist_t * p_playlist;
1033 vout_thread_t * p_vout;
1034 aout_instance_t * p_aout;
1035 vlc_t *p_vlc = vlc_current_object( i_object );
1037 /* Check that the handle is valid */
1044 * Ask the interfaces to stop and destroy them
1046 msg_Dbg( p_vlc, "removing all interfaces" );
1048 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1050 intf_StopThread( p_intf );
1051 vlc_object_detach( p_intf );
1052 vlc_object_release( p_intf );
1053 intf_Destroy( p_intf );
1060 msg_Dbg( p_vlc, "removing all playlists" );
1061 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1064 vlc_object_detach( p_playlist );
1065 vlc_object_release( p_playlist );
1066 playlist_Destroy( p_playlist );
1070 * Free video outputs
1072 msg_Dbg( p_vlc, "removing all video outputs" );
1073 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1075 vlc_object_detach( p_vout );
1076 vlc_object_release( p_vout );
1077 vout_Destroy( p_vout );
1081 * Free audio outputs
1083 msg_Dbg( p_vlc, "removing all audio outputs" );
1084 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1086 vlc_object_detach( (vlc_object_t *)p_aout );
1087 vlc_object_release( (vlc_object_t *)p_aout );
1088 aout_Delete( p_aout );
1091 if( i_object ) vlc_object_release( p_vlc );
1095 /*****************************************************************************
1096 * VLC_Pause: toggle pause
1097 *****************************************************************************/
1098 int VLC_Pause( int i_object )
1100 input_thread_t *p_input;
1101 vlc_t *p_vlc = vlc_current_object( i_object );
1108 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1112 if( i_object ) vlc_object_release( p_vlc );
1116 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1117 vlc_object_release( p_input );
1119 if( i_object ) vlc_object_release( p_vlc );
1123 /*****************************************************************************
1124 * VLC_FullScreen: toggle fullscreen mode
1125 *****************************************************************************/
1126 int VLC_FullScreen( int i_object )
1128 vout_thread_t *p_vout;
1129 vlc_t *p_vlc = vlc_current_object( i_object );
1136 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1140 if( i_object ) vlc_object_release( p_vlc );
1144 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1145 vlc_object_release( p_vout );
1147 if( i_object ) vlc_object_release( p_vlc );
1151 /* following functions are local */
1153 /*****************************************************************************
1154 * SetLanguage: set the interface language.
1155 *****************************************************************************
1156 * We set the LC_MESSAGES locale category for interface messages and buttons,
1157 * as well as the LC_CTYPE category for string sorting and possible wide
1158 * character support.
1159 *****************************************************************************/
1160 static void SetLanguage ( char const *psz_lang )
1162 #if defined( ENABLE_NLS ) \
1163 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1166 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1170 if( psz_lang && !*psz_lang )
1172 # if defined( HAVE_LC_MESSAGES )
1173 setlocale( LC_MESSAGES, psz_lang );
1175 setlocale( LC_CTYPE, psz_lang );
1180 /* I need that under Darwin, please check it doesn't disturb
1181 * other platforms. --Meuuh */
1182 setenv( "LANG", psz_lang, 1 );
1184 #elif defined( SYS_BEOS ) || defined( WIN32 )
1185 /* We set LC_ALL manually because it is the only way to set
1186 * the language at runtime under eg. Windows. Beware that this
1187 * makes the environment unconsistent when libvlc is unloaded and
1188 * should probably be moved to a safer place like vlc.c. */
1189 static char psz_lcall[20];
1190 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1191 psz_lcall[19] = '\0';
1192 putenv( psz_lcall );
1195 setlocale( LC_ALL, psz_lang );
1198 /* Specify where to find the locales for current domain */
1199 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1200 psz_path = LOCALEDIR;
1202 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1206 if( !bindtextdomain( PACKAGE, psz_path ) )
1208 fprintf( stderr, "warning: no domain %s in directory %s\n",
1209 PACKAGE, psz_path );
1212 /* Set the default domain */
1213 textdomain( PACKAGE );
1215 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1216 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1217 /* BeOS only support UTF8 strings */
1218 /* Mac OS X prefers UTF8 */
1219 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1225 /*****************************************************************************
1226 * GetFilenames: parse command line options which are not flags
1227 *****************************************************************************
1228 * Parse command line for input files as well as their associated options.
1229 * An option always follows its associated input and begins with a ":".
1230 *****************************************************************************/
1231 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1233 int i_opt, i_options;
1235 /* We assume that the remaining parameters are filenames
1236 * and their input options */
1237 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1241 /* Count the input options */
1242 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1248 /* TODO: write an internal function of this one, to avoid
1249 * unnecessary lookups. */
1250 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1251 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1253 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1260 /*****************************************************************************
1261 * Usage: print program usage
1262 *****************************************************************************
1263 * Print a short inline help. Message interface is initialized at this stage.
1264 *****************************************************************************/
1265 static void Usage( vlc_t *p_this, char const *psz_module_name )
1267 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1268 /* short option ------' | | | | | | |
1269 * option name ------------' | | | | | |
1270 * <bra -------------------------' | | | | |
1271 * option type or "" --------------' | | | |
1272 * ket> -----------------------------' | | |
1273 * padding spaces ---------------------' | |
1274 * comment -------------------------------' |
1275 * comment suffix --------------------------'
1277 * The purpose of having bra and ket is that we might i18n them as well.
1279 #define LINE_START 8
1280 #define PADDING_SPACES 25
1283 module_config_t *p_item;
1284 char psz_spaces[PADDING_SPACES+LINE_START+1];
1285 char psz_format[sizeof(FORMAT_STRING)];
1286 char psz_buffer[1000];
1289 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1290 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1292 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1293 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1295 strcpy( psz_format, FORMAT_STRING );
1301 /* List all modules */
1302 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1304 /* Enumerate the config for each module */
1305 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1307 vlc_bool_t b_help_module;
1309 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1311 if( psz_module_name && strcmp( psz_module_name,
1312 p_parser->psz_object_name ) )
1317 /* Ignore modules without config options */
1318 if( !p_parser->i_config_items )
1323 /* Ignore modules with only advanced config options if requested */
1326 for( p_item = p_parser->p_config;
1327 p_item->i_type != CONFIG_HINT_END;
1330 if( (p_item->i_type & CONFIG_ITEM) &&
1331 !p_item->b_advanced ) break;
1333 if( p_item->i_type == CONFIG_HINT_END ) continue;
1336 /* Print name of module */
1337 if( strcmp( "main", p_parser->psz_object_name ) )
1338 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1340 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1342 /* Print module options */
1343 for( p_item = p_parser->p_config;
1344 p_item->i_type != CONFIG_HINT_END;
1348 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1349 char *psz_suf = "", *psz_prefix = NULL;
1352 /* Skip advanced options if requested */
1353 if( p_item->b_advanced && !b_advanced )
1358 switch( p_item->i_type )
1360 case CONFIG_HINT_CATEGORY:
1361 case CONFIG_HINT_USAGE:
1362 if( !strcmp( "main", p_parser->psz_object_name ) )
1363 fprintf( stdout, "\n %s\n", p_item->psz_text );
1366 case CONFIG_ITEM_STRING:
1367 case CONFIG_ITEM_FILE:
1368 case CONFIG_ITEM_DIRECTORY:
1369 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1370 if( !p_item->ppsz_list )
1372 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1378 psz_type = psz_buffer;
1380 for( i = 0; p_item->ppsz_list[i]; i++ )
1382 if( i ) strcat( psz_type, "," );
1383 strcat( psz_type, p_item->ppsz_list[i] );
1388 case CONFIG_ITEM_INTEGER:
1389 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1390 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1392 case CONFIG_ITEM_FLOAT:
1393 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1395 case CONFIG_ITEM_BOOL:
1396 psz_bra = ""; psz_type = ""; psz_ket = "";
1397 if( !b_help_module )
1399 psz_suf = p_item->i_value ? _(" (default enabled)") :
1400 _(" (default disabled)");
1410 /* Add short option if any */
1411 if( p_item->i_short )
1413 sprintf( psz_short, "-%c,", p_item->i_short );
1417 strcpy( psz_short, " " );
1420 i = PADDING_SPACES - strlen( p_item->psz_name )
1421 - strlen( psz_bra ) - strlen( psz_type )
1422 - strlen( psz_ket ) - 1;
1424 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1426 /* If option is of type --foo-bar, we print its counterpart
1427 * as --no-foo-bar, but if it is of type --foobar (without
1428 * dashes in the name) we print it as --nofoobar. Both
1429 * values are of course valid, only the display changes. */
1430 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1432 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1437 psz_spaces[0] = '\n';
1442 psz_spaces[i] = '\0';
1445 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1447 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1448 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1449 psz_ket, psz_spaces );
1453 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1454 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1457 psz_spaces[i] = ' ';
1459 /* We wrap the rest of the output */
1460 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1461 psz_text = psz_buffer;
1464 char *psz_parser, *psz_word;
1465 int i_end = strlen( psz_text );
1467 /* If the remaining text fits in a line, print it. */
1468 if( i_end <= i_width )
1470 fprintf( stdout, "%s\n", psz_text );
1474 /* Otherwise, eat as many words as possible */
1475 psz_parser = psz_text;
1478 psz_word = psz_parser;
1479 psz_parser = strchr( psz_word, ' ' );
1480 /* If no space was found, we reached the end of the text
1481 * block; otherwise, we skip the space we just found. */
1482 psz_parser = psz_parser ? psz_parser + 1
1485 } while( psz_parser - psz_text <= i_width );
1487 /* We cut a word in one of these cases:
1488 * - it's the only word in the line and it's too long.
1489 * - we used less than 80% of the width and the word we are
1490 * going to wrap is longer than 40% of the width, and even
1491 * if the word would have fit in the next line. */
1492 if( psz_word == psz_text
1493 || ( psz_word - psz_text < 80 * i_width / 100
1494 && psz_parser - psz_word > 40 * i_width / 100 ) )
1496 char c = psz_text[i_width];
1497 psz_text[i_width] = '\0';
1498 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1499 psz_text += i_width;
1504 psz_word[-1] = '\0';
1505 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1506 psz_text = psz_word;
1512 /* Release the module list */
1513 vlc_list_release( p_list );
1515 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1516 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1521 /*****************************************************************************
1522 * ListModules: list the available modules with their description
1523 *****************************************************************************
1524 * Print a list of all available modules (builtins and plugins) and a short
1525 * description for each one.
1526 *****************************************************************************/
1527 static void ListModules( vlc_t *p_this )
1531 char psz_spaces[22];
1534 memset( psz_spaces, ' ', 22 );
1541 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1542 p_this->p_vlc->psz_object_name );
1544 fprintf( stdout, _("[module] [description]\n") );
1546 /* List all modules */
1547 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1549 /* Enumerate each module */
1550 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1554 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1556 /* Nasty hack, but right now I'm too tired to think about a nice
1558 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1562 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1563 psz_spaces, p_parser->psz_longname );
1565 psz_spaces[i] = ' ';
1568 vlc_list_release( p_list );
1570 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1571 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1576 /*****************************************************************************
1577 * Version: print complete program version
1578 *****************************************************************************
1579 * Print complete program version and build number.
1580 *****************************************************************************/
1581 static void Version( void )
1587 fprintf( stdout, VERSION_MESSAGE "\n" );
1589 _("This program comes with NO WARRANTY, to the extent permitted by "
1590 "law.\nYou may redistribute it under the terms of the GNU General "
1591 "Public License;\nsee the file named COPYING for details.\n"
1592 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1594 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1595 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1600 /*****************************************************************************
1601 * ShowConsole: On Win32, create an output console for debug messages
1602 *****************************************************************************
1603 * This function is useful only on Win32.
1604 *****************************************************************************/
1606 static void ShowConsole( void )
1610 freopen( "CONOUT$", "w", stdout );
1611 freopen( "CONOUT$", "w", stderr );
1612 freopen( "CONIN$", "r", stdin );
1618 /*****************************************************************************
1619 * ConsoleWidth: Return the console width in characters
1620 *****************************************************************************
1621 * We use the stty shell command to get the console width; if this fails or
1622 * if the width is less than 80, we default to 80.
1623 *****************************************************************************/
1624 static int ConsoleWidth( void )
1629 char buf[20], *psz_parser;
1633 file = popen( "stty size 2>/dev/null", "r" );
1636 i_ret = fread( buf, 1, 20, file );
1640 psz_parser = strchr( buf, ' ' );
1643 i_ret = atoi( psz_parser + 1 );
1658 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1659 vlc_value_t old_val, vlc_value_t new_val, void *param)
1661 vlc_t *p_vlc = (vlc_t *)p_this;
1663 if( new_val.i_int >= -1 )
1665 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );