1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Pretend we are a builtin module
29 *****************************************************************************/
30 #define MODULE_NAME main
31 #define MODULE_PATH main
34 /*****************************************************************************
36 *****************************************************************************/
38 #include <vlc/input.h>
40 #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"
64 # include <hal/libhal.h>
67 #include "vlc_cpu.h" /* CPU detection */
68 #include "os_specific.h"
70 #include "vlc_error.h"
72 #include "vlc_playlist.h"
73 #include "vlc_interface.h"
75 #include "audio_output.h"
77 #include "vlc_video.h"
78 #include "video_output.h"
80 #include "stream_output.h"
85 /*****************************************************************************
86 * The evil global variable. We handle it with care, don't worry.
87 *****************************************************************************/
88 static libvlc_t libvlc;
89 static libvlc_t * p_libvlc;
90 static vlc_t * p_static_vlc;
92 /*****************************************************************************
94 *****************************************************************************/
95 static void LocaleInit( void );
96 static void LocaleDeinit( void );
97 static void SetLanguage ( char const * );
98 static int GetFilenames ( vlc_t *, int, char *[] );
99 static void Help ( vlc_t *, char const *psz_help_name );
100 static void Usage ( vlc_t *, char const *psz_module_name );
101 static void ListModules ( vlc_t * );
102 static void Version ( void );
105 static void ShowConsole ( void );
106 static void PauseConsole ( void );
108 static int ConsoleWidth ( void );
110 static int VerboseCallback( vlc_object_t *, char const *,
111 vlc_value_t, vlc_value_t, void * );
113 static void InitDeviceValues( vlc_t * );
115 /*****************************************************************************
116 * vlc_current_object: return the current object.
117 *****************************************************************************
118 * If i_object is non-zero, return the corresponding object. Otherwise,
119 * return the statically allocated p_vlc object.
120 *****************************************************************************/
121 vlc_t * vlc_current_object( int i_object )
125 return vlc_object_get( p_libvlc, i_object );
131 /*****************************************************************************
132 * VLC_Version: return the libvlc version.
133 *****************************************************************************
134 * This function returns full version string (numeric version and codename).
135 *****************************************************************************/
136 char const * VLC_Version( void )
138 return VERSION_MESSAGE;
141 /*****************************************************************************
142 * VLC_CompileTime, VLC_CompileBy, VLC_CompileHost, VLC_CompileDomain,
143 * VLC_Compiler, VLC_Changeset
144 *****************************************************************************/
145 #define DECLARE_VLC_VERSION( func, var ) \
146 extern const char psz_vlc_##var []; \
147 char const * VLC_##func ( void ) \
149 return psz_vlc_##var ; \
152 DECLARE_VLC_VERSION( CompileTime, compile_time );
153 DECLARE_VLC_VERSION( CompileBy, compile_by );
154 DECLARE_VLC_VERSION( CompileHost, compile_host );
155 DECLARE_VLC_VERSION( CompileDomain, compile_domain );
156 DECLARE_VLC_VERSION( Compiler, compiler );
157 DECLARE_VLC_VERSION( Changeset, changeset );
159 /*****************************************************************************
160 * VLC_Error: strerror() equivalent
161 *****************************************************************************
162 * This function returns full version string (numeric version and codename).
163 *****************************************************************************/
164 char const * VLC_Error( int i_err )
166 return vlc_error( i_err );
169 /*****************************************************************************
170 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
171 *****************************************************************************
172 * This function allocates a vlc_t structure and returns a negative value
173 * in case of failure. Also, the thread system is initialized.
174 *****************************************************************************/
175 int VLC_Create( void )
178 vlc_t * p_vlc = NULL;
181 /* &libvlc never changes, so we can safely call this multiple times. */
184 /* vlc_threads_init *must* be the first internal call! No other call is
185 * allowed before the thread system has been initialized. */
186 i_ret = vlc_threads_init( p_libvlc );
192 /* Now that the thread system is initialized, we don't have much, but
193 * at least we have var_Create */
194 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
195 var_Get( p_libvlc, "libvlc", &lockval );
196 vlc_mutex_lock( lockval.p_address );
197 if( !libvlc.b_ready )
201 /* Guess what CPU we have */
202 libvlc.i_cpu = CPUCapabilities();
204 /* Find verbosity from VLC_VERBOSE environment variable */
205 psz_env = getenv( "VLC_VERBOSE" );
206 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
208 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
209 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
211 libvlc.b_color = VLC_FALSE;
214 /* Initialize message queue */
215 msg_Create( p_libvlc );
217 /* Announce who we are */
218 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
219 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
221 /* The module bank will be initialized later */
222 libvlc.p_module_bank = NULL;
224 libvlc.b_ready = VLC_TRUE;
226 /* UTF-8 convertor are initialized after the locale */
227 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
229 vlc_mutex_unlock( lockval.p_address );
230 var_Destroy( p_libvlc, "libvlc" );
232 /* Allocate a vlc object */
233 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
238 p_vlc->thread_id = 0;
240 p_vlc->psz_object_name = "root";
242 /* Initialize mutexes */
243 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
245 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
246 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
249 /* Store our newly allocated structure in the global list */
250 vlc_object_attach( p_vlc, p_libvlc );
252 /* Store data for the non-reentrant API */
253 p_static_vlc = p_vlc;
255 return p_vlc->i_object_id;
258 /*****************************************************************************
259 * VLC_Init: initialize a vlc_t structure.
260 *****************************************************************************
261 * This function initializes a previously allocated vlc_t structure:
263 * - gettext initialization
264 * - message queue, module bank and playlist initialization
265 * - configuration and commandline parsing
266 *****************************************************************************/
267 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
269 char p_capabilities[200];
274 vlc_bool_t b_exit = VLC_FALSE;
275 vlc_t * p_vlc = vlc_current_object( i_object );
276 module_t *p_help_module;
277 playlist_t *p_playlist;
279 #if defined( ENABLE_NLS ) \
280 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
290 * System specific initialization code
292 system_Init( p_vlc, &i_argc, ppsz_argv );
294 /* Get the executable name (similar to the basename command) */
297 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
300 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
306 p_vlc->psz_object_name = "vlc";
310 * Support for gettext
315 * Global iconv, must be done after setlocale()
316 * so that vlc_current_charset() works.
320 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
321 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
323 /* Initialize the module bank and load the configuration of the
324 * main module. We need to do this at this stage to be able to display
325 * a short help if required by the user. (short help == main module
327 module_InitBank( p_vlc );
329 /* Hack: insert the help module here */
330 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
331 if( p_help_module == NULL )
333 module_EndBank( p_vlc );
334 if( i_object ) vlc_object_release( p_vlc );
337 p_help_module->psz_object_name = "help";
338 p_help_module->psz_longname = N_("Help options");
339 config_Duplicate( p_help_module, p_help_config );
340 vlc_object_attach( p_help_module, libvlc.p_module_bank );
343 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
345 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 short help option */
354 if( config_GetInt( p_vlc, "help" ) )
356 Help( p_vlc, "help" );
359 /* Check for version option */
360 else if( config_GetInt( p_vlc, "version" ) )
366 /* Set the config file stuff */
367 p_vlc->psz_homedir = config_GetHomeDir();
368 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
370 /* Check for plugins cache options */
371 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
373 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
376 /* Hack: remove the help module here */
377 vlc_object_detach( p_help_module );
380 /* Will be re-done properly later on */
381 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
383 /* Check for daemon mode */
385 if( config_GetInt( p_vlc, "daemon" ) )
388 if( daemon( 0, 0) != 0 )
390 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
394 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
399 if( ( i_pid = fork() ) < 0 )
401 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
406 /* This is the parent, exit right now */
407 msg_Dbg( p_vlc, "closing parent process" );
412 /* We are the child */
413 msg_Dbg( p_vlc, "daemon spawned" );
414 close( STDIN_FILENO );
415 close( STDOUT_FILENO );
416 close( STDERR_FILENO );
418 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
426 config_Free( p_help_module );
427 vlc_object_destroy( p_help_module );
428 module_EndBank( p_vlc );
429 if( i_object ) vlc_object_release( p_vlc );
433 /* Check for translation config option */
434 #if defined( ENABLE_NLS ) \
435 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
437 /* This ain't really nice to have to reload the config here but it seems
438 * the only way to do it. */
439 config_LoadConfigFile( p_vlc, "main" );
440 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
442 /* Check if the user specified a custom language */
443 psz_language = config_GetPsz( p_vlc, "language" );
444 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
446 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
448 /* Reset the default domain */
449 SetLanguage( psz_language );
453 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
454 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
456 module_EndBank( p_vlc );
457 module_InitBank( p_vlc );
458 config_LoadConfigFile( p_vlc, "main" );
459 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
460 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
462 if( psz_language ) free( psz_language );
466 * Load the builtins and plugins into the module_bank.
467 * We have to do it before config_Load*() because this also gets the
468 * list of configuration options exported by each module and loads their
471 module_LoadBuiltins( p_vlc );
472 module_LoadPlugins( p_vlc );
478 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
479 libvlc.p_module_bank->i_children );
481 /* Hack: insert the help module here */
482 vlc_object_attach( p_help_module, libvlc.p_module_bank );
485 /* Check for help on modules */
486 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
488 Help( p_vlc, p_tmp );
492 /* Check for long help option */
493 else if( config_GetInt( p_vlc, "longhelp" ) )
495 Help( p_vlc, "longhelp" );
498 /* Check for module list option */
499 else if( config_GetInt( p_vlc, "list" ) )
501 ListModules( p_vlc );
505 /* Check for config file options */
506 if( config_GetInt( p_vlc, "reset-config" ) )
508 vlc_object_detach( p_help_module );
509 config_ResetAll( p_vlc );
510 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
511 config_SaveConfigFile( p_vlc, NULL );
512 vlc_object_attach( p_help_module, libvlc.p_module_bank );
514 if( config_GetInt( p_vlc, "save-config" ) )
516 vlc_object_detach( p_help_module );
517 config_LoadConfigFile( p_vlc, NULL );
518 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
519 config_SaveConfigFile( p_vlc, NULL );
520 vlc_object_attach( p_help_module, libvlc.p_module_bank );
523 /* Hack: remove the help module here */
524 vlc_object_detach( p_help_module );
529 config_Free( p_help_module );
530 vlc_object_destroy( p_help_module );
531 module_EndBank( p_vlc );
532 if( i_object ) vlc_object_release( p_vlc );
539 InitDeviceValues( p_vlc );
542 * Override default configuration with config file settings
544 config_LoadConfigFile( p_vlc, NULL );
546 /* Hack: insert the help module here */
547 vlc_object_attach( p_help_module, libvlc.p_module_bank );
551 * Override configuration with command line settings
553 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
557 /* Pause the console because it's destroyed when we exit */
558 fprintf( stderr, "The command line options couldn't be loaded, check "
559 "that they are valid.\n" );
562 vlc_object_detach( p_help_module );
563 config_Free( p_help_module );
564 vlc_object_destroy( p_help_module );
565 module_EndBank( p_vlc );
566 if( i_object ) vlc_object_release( p_vlc );
570 /* Hack: remove the help module here */
571 vlc_object_detach( p_help_module );
572 config_Free( p_help_module );
573 vlc_object_destroy( p_help_module );
577 * System specific configuration
579 system_Configure( p_vlc, &i_argc, ppsz_argv );
582 * Message queue options
585 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
586 if( config_GetInt( p_vlc, "quiet" ) )
589 var_Set( p_vlc, "verbose", val );
591 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
592 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
594 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
597 * Output messages that may still be in the queue
601 /* p_vlc initialization. FIXME ? */
603 if( !config_GetInt( p_vlc, "fpu" ) )
604 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
606 #if defined( __i386__ ) || defined( __x86_64__ )
607 if( !config_GetInt( p_vlc, "mmx" ) )
608 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
609 if( !config_GetInt( p_vlc, "3dn" ) )
610 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
611 if( !config_GetInt( p_vlc, "mmxext" ) )
612 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
613 if( !config_GetInt( p_vlc, "sse" ) )
614 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
615 if( !config_GetInt( p_vlc, "sse2" ) )
616 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
618 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
619 if( !config_GetInt( p_vlc, "altivec" ) )
620 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
623 #define PRINT_CAPABILITY( capability, string ) \
624 if( libvlc.i_cpu & capability ) \
626 strncat( p_capabilities, string " ", \
627 sizeof(p_capabilities) - strlen(p_capabilities) ); \
628 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
631 p_capabilities[0] = '\0';
632 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
633 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
634 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
635 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
636 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
637 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
638 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
639 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
640 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
641 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
642 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
645 * Choose the best memcpy module
647 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
649 if( p_vlc->pf_memcpy == NULL )
651 p_vlc->pf_memcpy = memcpy;
654 if( p_vlc->pf_memset == NULL )
656 p_vlc->pf_memset = memset;
660 * Initialize hotkey handling
662 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
663 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
664 /* Do a copy (we don't need to modify the strings) */
665 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
668 * Initialize playlist and get commandline files
670 p_playlist = playlist_Create( p_vlc );
673 msg_Err( p_vlc, "playlist initialization failed" );
674 if( p_vlc->p_memcpy_module != NULL )
676 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
678 module_EndBank( p_vlc );
679 if( i_object ) vlc_object_release( p_vlc );
683 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
684 if( psz_modules && *psz_modules )
686 /* Add service discovery modules */
687 playlist_AddSDModules( p_playlist, psz_modules );
689 if( psz_modules ) free( psz_modules );
692 * Load background interfaces
694 psz_modules = config_GetPsz( p_vlc, "extraintf" );
695 psz_control = config_GetPsz( p_vlc, "control" );
697 if( psz_modules && *psz_modules && psz_control && *psz_control )
699 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
700 strlen( psz_control ) + 1 );
701 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
703 else if( psz_control && *psz_control )
705 if( psz_modules ) free( psz_modules );
706 psz_modules = strdup( psz_control );
709 psz_parser = psz_modules;
710 while ( psz_parser && *psz_parser )
712 char *psz_module, *psz_temp;
713 psz_module = psz_parser;
714 psz_parser = strchr( psz_module, ':' );
720 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
723 sprintf( psz_temp, "%s,none", psz_module );
724 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
734 * Allways load the hotkeys interface if it exists
736 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
739 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
741 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
742 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
743 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
744 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
745 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
746 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
747 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
748 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
749 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
750 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
751 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
752 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
755 * Get input filenames given as commandline arguments
757 GetFilenames( p_vlc, i_argc, ppsz_argv );
760 * Get --open argument
762 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
763 var_Get( p_vlc, "open", &val );
764 if ( val.psz_string != NULL && *val.psz_string )
766 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
767 PLAYLIST_INSERT, 0 );
769 if ( val.psz_string != NULL ) free( val.psz_string );
771 if( i_object ) vlc_object_release( p_vlc );
775 /*****************************************************************************
776 * VLC_AddIntf: add an interface
777 *****************************************************************************
778 * This function opens an interface plugin and runs it. If b_block is set
779 * to 0, VLC_AddIntf will return immediately and let the interface run in a
780 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
781 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
782 * the playlist when it is completely initialised.
783 *****************************************************************************/
784 int VLC_AddIntf( int i_object, char const *psz_module,
785 vlc_bool_t b_block, vlc_bool_t b_play )
788 intf_thread_t *p_intf;
789 vlc_t *p_vlc = vlc_current_object( i_object );
797 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
800 * We prefer the dummy interface if none is specified. */
801 char *psz_interface = config_GetPsz( p_vlc, "intf" );
802 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
803 if( psz_interface ) free( psz_interface );
807 /* Try to create the interface */
808 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
812 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
813 if( i_object ) vlc_object_release( p_vlc );
817 /* Interface doesn't handle play on start so do it ourselves */
818 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
820 /* Try to run the interface */
821 p_intf->b_play = b_play;
822 p_intf->b_block = b_block;
823 i_err = intf_RunThread( p_intf );
826 vlc_object_detach( p_intf );
827 intf_Destroy( p_intf );
828 if( i_object ) vlc_object_release( p_vlc );
832 if( i_object ) vlc_object_release( p_vlc );
836 /*****************************************************************************
837 * VLC_Die: ask vlc to die.
838 *****************************************************************************
839 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
840 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
841 *****************************************************************************/
842 int VLC_Die( int i_object )
844 vlc_t *p_vlc = vlc_current_object( i_object );
851 p_vlc->b_die = VLC_TRUE;
853 if( i_object ) vlc_object_release( p_vlc );
857 /*****************************************************************************
858 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
859 *****************************************************************************/
860 int VLC_CleanUp( int i_object )
862 intf_thread_t * p_intf;
863 playlist_t * p_playlist;
864 vout_thread_t * p_vout;
865 aout_instance_t * p_aout;
866 announce_handler_t * p_announce;
867 vlc_t *p_vlc = vlc_current_object( i_object );
869 /* Check that the handle is valid */
876 * Ask the interfaces to stop and destroy them
878 msg_Dbg( p_vlc, "removing all interfaces" );
879 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
881 intf_StopThread( p_intf );
882 vlc_object_detach( p_intf );
883 vlc_object_release( p_intf );
884 intf_Destroy( p_intf );
890 msg_Dbg( p_vlc, "removing all playlists" );
891 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
894 vlc_object_detach( p_playlist );
895 vlc_object_release( p_playlist );
896 playlist_Destroy( p_playlist );
902 msg_Dbg( p_vlc, "removing all video outputs" );
903 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
905 vlc_object_detach( p_vout );
906 vlc_object_release( p_vout );
907 vout_Destroy( p_vout );
913 msg_Dbg( p_vlc, "removing all audio outputs" );
914 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
916 vlc_object_detach( (vlc_object_t *)p_aout );
917 vlc_object_release( (vlc_object_t *)p_aout );
918 aout_Delete( p_aout );
922 * Free announce handler(s?)
924 msg_Dbg( p_vlc, "removing announce handler" );
925 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
928 vlc_object_detach( p_announce );
929 vlc_object_release( p_announce );
930 announce_HandlerDestroy( p_announce );
933 if( i_object ) vlc_object_release( p_vlc );
937 /*****************************************************************************
938 * VLC_Destroy: Destroy everything.
939 *****************************************************************************
940 * This function requests the running threads to finish, waits for their
941 * termination, and destroys their structure.
942 *****************************************************************************/
943 int VLC_Destroy( int i_object )
945 vlc_t *p_vlc = vlc_current_object( i_object );
953 * Free allocated memory
955 if( p_vlc->p_memcpy_module )
957 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
958 p_vlc->p_memcpy_module = NULL;
964 module_EndBank( p_vlc );
966 if( p_vlc->psz_homedir )
968 free( p_vlc->psz_homedir );
969 p_vlc->psz_homedir = NULL;
972 if( p_vlc->psz_configfile )
974 free( p_vlc->psz_configfile );
975 p_vlc->psz_configfile = NULL;
978 if( p_vlc->p_hotkeys )
980 free( p_vlc->p_hotkeys );
981 p_vlc->p_hotkeys = NULL;
985 * System specific cleaning code
990 * Free message queue.
991 * Nobody shall use msg_* afterward.
994 msg_Destroy( p_libvlc );
996 /* Destroy global iconv */
999 /* Destroy mutexes */
1000 vlc_mutex_destroy( &p_vlc->config_lock );
1002 vlc_object_detach( p_vlc );
1004 /* Release object before destroying it */
1005 if( i_object ) vlc_object_release( p_vlc );
1007 vlc_object_destroy( p_vlc );
1009 /* Stop thread system: last one out please shut the door! */
1010 vlc_threads_end( p_libvlc );
1015 /*****************************************************************************
1016 * VLC_VariableSet: set a vlc variable
1017 *****************************************************************************/
1018 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
1020 vlc_t *p_vlc = vlc_current_object( i_object );
1028 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1029 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1030 if( !strncmp( psz_var, "conf::", 6 ) )
1032 module_config_t *p_item;
1033 char const *psz_newvar = psz_var + 6;
1035 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1039 switch( p_item->i_type )
1041 case CONFIG_ITEM_BOOL:
1042 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1044 case CONFIG_ITEM_INTEGER:
1045 config_PutInt( p_vlc, psz_newvar, value.i_int );
1047 case CONFIG_ITEM_FLOAT:
1048 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1051 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1054 if( i_object ) vlc_object_release( p_vlc );
1059 i_ret = var_Set( p_vlc, psz_var, value );
1061 if( i_object ) vlc_object_release( p_vlc );
1065 /*****************************************************************************
1066 * VLC_VariableGet: get a vlc variable
1067 *****************************************************************************/
1068 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1070 vlc_t *p_vlc = vlc_current_object( i_object );
1078 i_ret = var_Get( p_vlc , psz_var, p_value );
1080 if( i_object ) vlc_object_release( p_vlc );
1084 /*****************************************************************************
1085 * VLC_VariableType: get a vlc variable type
1086 *****************************************************************************/
1087 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1090 vlc_t *p_vlc = vlc_current_object( i_object );
1097 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1098 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1099 if( !strncmp( psz_var, "conf::", 6 ) )
1101 module_config_t *p_item;
1102 char const *psz_newvar = psz_var + 6;
1104 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1108 switch( p_item->i_type )
1110 case CONFIG_ITEM_BOOL:
1111 i_type = VLC_VAR_BOOL;
1113 case CONFIG_ITEM_INTEGER:
1114 i_type = VLC_VAR_INTEGER;
1116 case CONFIG_ITEM_FLOAT:
1117 i_type = VLC_VAR_FLOAT;
1120 i_type = VLC_VAR_STRING;
1128 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1130 if( i_object ) vlc_object_release( p_vlc );
1140 /*****************************************************************************
1141 * VLC_AddTarget: adds a target for playing.
1142 *****************************************************************************
1143 * This function adds psz_target to the current playlist. If a playlist does
1144 * not exist, it will create one.
1145 *****************************************************************************/
1146 int VLC_AddTarget( int i_object, char const *psz_target,
1147 char const **ppsz_options, int i_options,
1148 int i_mode, int i_pos )
1151 playlist_t *p_playlist;
1152 vlc_t *p_vlc = vlc_current_object( i_object );
1159 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1161 if( p_playlist == NULL )
1163 msg_Dbg( p_vlc, "no playlist present, creating one" );
1164 p_playlist = playlist_Create( p_vlc );
1166 if( p_playlist == NULL )
1168 if( i_object ) vlc_object_release( p_vlc );
1169 return VLC_EGENERIC;
1172 vlc_object_yield( p_playlist );
1175 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1176 i_mode, i_pos, -1, ppsz_options, i_options);
1178 vlc_object_release( p_playlist );
1180 if( i_object ) vlc_object_release( p_vlc );
1184 /*****************************************************************************
1186 *****************************************************************************/
1187 int VLC_Play( int i_object )
1189 playlist_t * p_playlist;
1190 vlc_t *p_vlc = vlc_current_object( i_object );
1192 /* Check that the handle is valid */
1198 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1202 if( i_object ) vlc_object_release( p_vlc );
1206 playlist_Play( p_playlist );
1207 vlc_object_release( p_playlist );
1209 if( i_object ) vlc_object_release( p_vlc );
1213 /*****************************************************************************
1214 * VLC_Pause: toggle pause
1215 *****************************************************************************/
1216 int VLC_Pause( int i_object )
1218 playlist_t * p_playlist;
1219 vlc_t *p_vlc = vlc_current_object( i_object );
1221 /* Check that the handle is valid */
1227 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1231 if( i_object ) vlc_object_release( p_vlc );
1235 playlist_Pause( p_playlist );
1236 vlc_object_release( p_playlist );
1238 if( i_object ) vlc_object_release( p_vlc );
1242 /*****************************************************************************
1243 * VLC_Pause: toggle pause
1244 *****************************************************************************/
1245 int VLC_Stop( int i_object )
1247 playlist_t * p_playlist;
1248 vlc_t *p_vlc = vlc_current_object( i_object );
1250 /* Check that the handle is valid */
1256 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1260 if( i_object ) vlc_object_release( p_vlc );
1264 playlist_Stop( p_playlist );
1265 vlc_object_release( p_playlist );
1267 if( i_object ) vlc_object_release( p_vlc );
1271 /*****************************************************************************
1272 * VLC_IsPlaying: Query for Playlist Status
1273 *****************************************************************************/
1274 vlc_bool_t VLC_IsPlaying( int i_object )
1276 playlist_t * p_playlist;
1277 vlc_bool_t b_playing;
1280 vlc_t *p_vlc = vlc_current_object( i_object );
1282 /* Check that the handle is valid */
1288 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1292 if( i_object ) vlc_object_release( p_vlc );
1295 if( !p_playlist->p_input )
1297 if( i_object ) vlc_object_release( p_vlc );
1300 var_Get( p_playlist->p_input, "state", &val );
1301 b_playing = ( val.i_int == PLAYING_S );
1302 vlc_object_release( p_playlist );
1304 if( i_object ) vlc_object_release( p_vlc );
1309 * Get the current position in a input
1311 * Return the current position as a float
1312 * \note For some inputs, this will be unknown.
1314 * \param i_object a vlc object id
1315 * \return a float in the range of 0.0 - 1.0
1317 float VLC_PositionGet( int i_object )
1319 input_thread_t *p_input;
1321 vlc_t *p_vlc = vlc_current_object( i_object );
1323 /* Check that the handle is valid */
1329 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1333 if( i_object ) vlc_object_release( p_vlc );
1337 var_Get( p_input, "position", &val );
1338 vlc_object_release( p_input );
1340 if( i_object ) vlc_object_release( p_vlc );
1345 * Set the current position in a input
1347 * Set the current position in a input and then return
1348 * the current position as a float.
1349 * \note For some inputs, this will be unknown.
1351 * \param i_object a vlc object id
1352 * \param i_position a float in the range of 0.0 - 1.0
1353 * \return a float in the range of 0.0 - 1.0
1355 float VLC_PositionSet( int i_object, float i_position )
1357 input_thread_t *p_input;
1359 vlc_t *p_vlc = vlc_current_object( i_object );
1361 /* Check that the handle is valid */
1367 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1371 if( i_object ) vlc_object_release( p_vlc );
1375 val.f_float = i_position;
1376 var_Set( p_input, "position", val );
1377 var_Get( p_input, "position", &val );
1378 vlc_object_release( p_input );
1380 if( i_object ) vlc_object_release( p_vlc );
1385 * Get the current position in a input
1387 * Return the current position in seconds from the start.
1388 * \note For some inputs, this will be unknown.
1390 * \param i_object a vlc object id
1391 * \return the offset from 0:00 in seconds
1393 int VLC_TimeGet( int i_object )
1395 input_thread_t *p_input;
1397 vlc_t *p_vlc = vlc_current_object( i_object );
1399 /* Check that the handle is valid */
1405 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1409 if( i_object ) vlc_object_release( p_vlc );
1413 var_Get( p_input, "time", &val );
1414 vlc_object_release( p_input );
1416 if( i_object ) vlc_object_release( p_vlc );
1417 return val.i_time / 1000000;
1421 * Seek to a position in the current input
1423 * Seek i_seconds in the current input. If b_relative is set,
1424 * then the seek will be relative to the current position, otherwise
1425 * it will seek to i_seconds from the beginning of the input.
1426 * \note For some inputs, this will be unknown.
1428 * \param i_object a vlc object id
1429 * \param i_seconds seconds from current position or from beginning of input
1430 * \param b_relative seek relative from current position
1431 * \return VLC_SUCCESS on success
1433 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1435 input_thread_t *p_input;
1437 vlc_t *p_vlc = vlc_current_object( i_object );
1439 /* Check that the handle is valid */
1445 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1449 if( i_object ) vlc_object_release( p_vlc );
1455 val.i_time = i_seconds;
1456 val.i_time = val.i_time * 1000000L;
1457 var_Set( p_input, "time-offset", val );
1461 val.i_time = i_seconds;
1462 val.i_time = val.i_time * 1000000L;
1463 var_Set( p_input, "time", val );
1465 vlc_object_release( p_input );
1467 if( i_object ) vlc_object_release( p_vlc );
1472 * Get the total length of a input
1474 * Return the total length in seconds from the current input.
1475 * \note For some inputs, this will be unknown.
1477 * \param i_object a vlc object id
1478 * \return the length in seconds
1480 int VLC_LengthGet( int i_object )
1482 input_thread_t *p_input;
1484 vlc_t *p_vlc = vlc_current_object( i_object );
1486 /* Check that the handle is valid */
1492 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1496 if( i_object ) vlc_object_release( p_vlc );
1500 var_Get( p_input, "length", &val );
1501 vlc_object_release( p_input );
1503 if( i_object ) vlc_object_release( p_vlc );
1504 return val.i_time / 1000000L;
1508 * Play the input faster than realtime
1510 * 2x, 4x, 8x faster than realtime
1511 * \note For some inputs, this will be impossible.
1513 * \param i_object a vlc object id
1514 * \return the current speedrate
1516 float VLC_SpeedFaster( int i_object )
1518 input_thread_t *p_input;
1520 vlc_t *p_vlc = vlc_current_object( i_object );
1522 /* Check that the handle is valid */
1528 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1532 if( i_object ) vlc_object_release( p_vlc );
1536 val.b_bool = VLC_TRUE;
1537 var_Set( p_input, "rate-faster", val );
1538 var_Get( p_input, "rate", &val );
1539 vlc_object_release( p_input );
1541 if( i_object ) vlc_object_release( p_vlc );
1542 return val.f_float / INPUT_RATE_DEFAULT;
1546 * Play the input slower than realtime
1548 * 1/2x, 1/4x, 1/8x slower than realtime
1549 * \note For some inputs, this will be impossible.
1551 * \param i_object a vlc object id
1552 * \return the current speedrate
1554 float VLC_SpeedSlower( int i_object )
1556 input_thread_t *p_input;
1558 vlc_t *p_vlc = vlc_current_object( i_object );
1560 /* Check that the handle is valid */
1566 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1570 if( i_object ) vlc_object_release( p_vlc );
1574 val.b_bool = VLC_TRUE;
1575 var_Set( p_input, "rate-slower", val );
1576 var_Get( p_input, "rate", &val );
1577 vlc_object_release( p_input );
1579 if( i_object ) vlc_object_release( p_vlc );
1580 return val.f_float / INPUT_RATE_DEFAULT;
1584 * Return the current playlist item
1586 * Returns the index of the playlistitem that is currently selected for play.
1587 * This is valid even if nothing is currently playing.
1589 * \param i_object a vlc object id
1590 * \return the current index
1592 int VLC_PlaylistIndex( int i_object )
1595 playlist_t * p_playlist;
1596 vlc_t *p_vlc = vlc_current_object( i_object );
1598 /* Check that the handle is valid */
1604 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1608 if( i_object ) vlc_object_release( p_vlc );
1612 i_index = p_playlist->i_index;
1613 vlc_object_release( p_playlist );
1615 if( i_object ) vlc_object_release( p_vlc );
1620 * Total amount of items in the playlist
1622 * \param i_object a vlc object id
1623 * \return amount of playlist items
1625 int VLC_PlaylistNumberOfItems( int i_object )
1628 playlist_t * p_playlist;
1629 vlc_t *p_vlc = vlc_current_object( i_object );
1631 /* Check that the handle is valid */
1637 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1641 if( i_object ) vlc_object_release( p_vlc );
1645 i_size = p_playlist->i_size;
1646 vlc_object_release( p_playlist );
1648 if( i_object ) vlc_object_release( p_vlc );
1653 * Next playlist item
1655 * Skip to the next playlistitem and play it.
1657 * \param i_object a vlc object id
1658 * \return VLC_SUCCESS on success
1660 int VLC_PlaylistNext( int i_object )
1662 playlist_t * p_playlist;
1663 vlc_t *p_vlc = vlc_current_object( i_object );
1665 /* Check that the handle is valid */
1671 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1675 if( i_object ) vlc_object_release( p_vlc );
1679 playlist_Next( p_playlist );
1680 vlc_object_release( p_playlist );
1682 if( i_object ) vlc_object_release( p_vlc );
1687 * Previous playlist item
1689 * Skip to the previous playlistitem and play it.
1691 * \param i_object a vlc object id
1692 * \return VLC_SUCCESS on success
1694 int VLC_PlaylistPrev( int i_object )
1696 playlist_t * p_playlist;
1697 vlc_t *p_vlc = vlc_current_object( i_object );
1699 /* Check that the handle is valid */
1705 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1709 if( i_object ) vlc_object_release( p_vlc );
1713 playlist_Prev( p_playlist );
1714 vlc_object_release( p_playlist );
1716 if( i_object ) vlc_object_release( p_vlc );
1721 /*****************************************************************************
1722 * VLC_PlaylistClear: Empty the playlist
1723 *****************************************************************************/
1724 int VLC_PlaylistClear( int i_object )
1727 playlist_t * p_playlist;
1728 vlc_t *p_vlc = vlc_current_object( i_object );
1730 /* Check that the handle is valid */
1736 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1740 if( i_object ) vlc_object_release( p_vlc );
1744 i_err = playlist_Clear( p_playlist );
1746 vlc_object_release( p_playlist );
1748 if( i_object ) vlc_object_release( p_vlc );
1755 * \param i_object a vlc object id
1756 * \param i_volume something in a range from 0-200
1757 * \return the new volume (range 0-200 %)
1759 int VLC_VolumeSet( int i_object, int i_volume )
1761 audio_volume_t i_vol = 0;
1762 vlc_t *p_vlc = vlc_current_object( i_object );
1764 /* Check that the handle is valid */
1770 if( i_volume >= 0 && i_volume <= 200 )
1772 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1773 aout_VolumeSet( p_vlc, i_vol );
1776 if( i_object ) vlc_object_release( p_vlc );
1777 return i_vol * 200 / AOUT_VOLUME_MAX;
1781 * Get the current volume
1783 * Retrieve the current volume.
1785 * \param i_object a vlc object id
1786 * \return the current volume (range 0-200 %)
1788 int VLC_VolumeGet( int i_object )
1790 audio_volume_t i_volume;
1791 vlc_t *p_vlc = vlc_current_object( i_object );
1793 /* Check that the handle is valid */
1799 aout_VolumeGet( p_vlc, &i_volume );
1801 if( i_object ) vlc_object_release( p_vlc );
1802 return i_volume*200/AOUT_VOLUME_MAX;
1806 * Mute/Unmute the volume
1808 * \param i_object a vlc object id
1809 * \return VLC_SUCCESS on success
1811 int VLC_VolumeMute( int i_object )
1813 vlc_t *p_vlc = vlc_current_object( i_object );
1815 /* Check that the handle is valid */
1821 aout_VolumeMute( p_vlc, NULL );
1823 if( i_object ) vlc_object_release( p_vlc );
1827 /*****************************************************************************
1828 * VLC_FullScreen: toggle fullscreen mode
1829 *****************************************************************************/
1830 int VLC_FullScreen( int i_object )
1832 vout_thread_t *p_vout;
1833 vlc_t *p_vlc = vlc_current_object( i_object );
1840 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1844 if( i_object ) vlc_object_release( p_vlc );
1848 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1849 vlc_object_release( p_vout );
1851 if( i_object ) vlc_object_release( p_vlc );
1855 /* following functions are local */
1857 static void LocaleInit( void )
1861 if( !vlc_current_charset( &psz_charset ) )
1863 char *psz_conv = psz_charset;
1866 * Still allow non-ASCII characters when the locale is not set.
1867 * Western Europeans are being favored for historical reasons.
1869 psz_conv = strcmp( psz_charset, "ASCII" )
1873 vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
1874 vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
1875 libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
1876 libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
1877 if( !libvlc.to_locale )
1879 /* Not sure it is the right thing to do, but at least it
1880 doesn't make vlc crash with msvc ! */
1881 libvlc.to_locale = (vlc_iconv_t)(-1);
1885 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
1886 free( psz_charset );
1889 static void LocaleDeinit( void )
1891 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
1893 vlc_mutex_destroy( &libvlc.from_locale_lock );
1894 vlc_mutex_destroy( &libvlc.to_locale_lock );
1895 vlc_iconv_close( libvlc.from_locale );
1896 vlc_iconv_close( libvlc.to_locale );
1900 /*****************************************************************************
1901 * SetLanguage: set the interface language.
1902 *****************************************************************************
1903 * We set the LC_MESSAGES locale category for interface messages and buttons,
1904 * as well as the LC_CTYPE category for string sorting and possible wide
1905 * character support.
1906 *****************************************************************************/
1907 static void SetLanguage ( char const *psz_lang )
1909 #if defined( ENABLE_NLS ) \
1910 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1913 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1917 if( psz_lang && !*psz_lang )
1919 # if defined( HAVE_LC_MESSAGES )
1920 setlocale( LC_MESSAGES, psz_lang );
1922 setlocale( LC_CTYPE, psz_lang );
1927 /* I need that under Darwin, please check it doesn't disturb
1928 * other platforms. --Meuuh */
1929 setenv( "LANG", psz_lang, 1 );
1931 #elif defined( SYS_BEOS ) || defined( WIN32 )
1932 /* We set LC_ALL manually because it is the only way to set
1933 * the language at runtime under eg. Windows. Beware that this
1934 * makes the environment unconsistent when libvlc is unloaded and
1935 * should probably be moved to a safer place like vlc.c. */
1936 static char psz_lcall[20];
1937 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1938 psz_lcall[19] = '\0';
1939 putenv( psz_lcall );
1942 setlocale( LC_ALL, psz_lang );
1943 /* many code paths assume that float numbers are formatted according
1944 * to the US standard (ie. with dot as decimal point), so we keep
1945 * C for LC_NUMERIC. */
1946 setlocale(LC_NUMERIC, "C" );
1949 /* Specify where to find the locales for current domain */
1950 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1951 psz_path = LOCALEDIR;
1953 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1957 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1959 fprintf( stderr, "warning: no domain %s in directory %s\n",
1960 PACKAGE_NAME, psz_path );
1963 /* Set the default domain */
1964 textdomain( PACKAGE_NAME );
1965 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1969 /*****************************************************************************
1970 * GetFilenames: parse command line options which are not flags
1971 *****************************************************************************
1972 * Parse command line for input files as well as their associated options.
1973 * An option always follows its associated input and begins with a ":".
1974 *****************************************************************************/
1975 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1977 int i_opt, i_options;
1979 /* We assume that the remaining parameters are filenames
1980 * and their input options */
1981 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1983 const char *psz_target;
1986 /* Count the input options */
1987 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1993 /* TODO: write an internal function of this one, to avoid
1994 * unnecessary lookups. */
1995 /* FIXME: should we convert options to UTF-8 as well ?? */
1996 psz_target = FromLocale( ppsz_argv[ i_opt ] );
1997 VLC_AddTarget( p_vlc->i_object_id, psz_target,
1998 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
2000 PLAYLIST_INSERT, 0 );
2001 LocaleFree( psz_target );
2007 /*****************************************************************************
2008 * Help: print program help
2009 *****************************************************************************
2010 * Print a short inline help. Message interface is initialized at this stage.
2011 *****************************************************************************/
2012 static void Help( vlc_t *p_this, char const *psz_help_name )
2018 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
2020 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2021 Usage( p_this, "help" );
2022 Usage( p_this, "main" );
2024 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2026 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2027 Usage( p_this, NULL );
2029 else if( psz_help_name )
2031 Usage( p_this, psz_help_name );
2034 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2039 /*****************************************************************************
2040 * Usage: print module usage
2041 *****************************************************************************
2042 * Print a short inline help. Message interface is initialized at this stage.
2043 *****************************************************************************/
2044 static void Usage( vlc_t *p_this, char const *psz_module_name )
2046 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
2047 /* short option ------' | | | | | | |
2048 * option name ------------' | | | | | |
2049 * <bra -------------------------' | | | | |
2050 * option type or "" --------------' | | | |
2051 * ket> -----------------------------' | | |
2052 * padding spaces ---------------------' | |
2053 * comment -------------------------------' |
2054 * comment suffix --------------------------'
2056 * The purpose of having bra and ket is that we might i18n them as well.
2058 #define LINE_START 8
2059 #define PADDING_SPACES 25
2062 module_config_t *p_item;
2063 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2064 char psz_spaces_longtext[LINE_START+3];
2065 char psz_format[sizeof(FORMAT_STRING)];
2066 char psz_buffer[10000];
2069 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2070 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2071 vlc_bool_t b_description;
2073 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2074 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2075 memset( psz_spaces_longtext, ' ', LINE_START+2 );
2076 psz_spaces_longtext[LINE_START+2] = '\0';
2078 strcpy( psz_format, FORMAT_STRING );
2080 /* List all modules */
2081 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2083 /* Enumerate the config for each module */
2084 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2086 vlc_bool_t b_help_module;
2088 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2090 if( psz_module_name && strcmp( psz_module_name,
2091 p_parser->psz_object_name ) )
2096 /* Ignore modules without config options */
2097 if( !p_parser->i_config_items )
2102 /* Ignore modules with only advanced config options if requested */
2105 for( p_item = p_parser->p_config;
2106 p_item->i_type != CONFIG_HINT_END;
2109 if( (p_item->i_type & CONFIG_ITEM) &&
2110 !p_item->b_advanced ) break;
2112 if( p_item->i_type == CONFIG_HINT_END ) continue;
2115 /* Print name of module */
2116 if( strcmp( "main", p_parser->psz_object_name ) )
2117 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2119 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2121 /* Print module options */
2122 for( p_item = p_parser->p_config;
2123 p_item->i_type != CONFIG_HINT_END;
2126 char *psz_text, *psz_spaces = psz_spaces_text;
2127 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2128 char *psz_suf = "", *psz_prefix = NULL;
2131 /* Skip deprecated options */
2132 if( p_item->psz_current )
2136 /* Skip advanced options if requested */
2137 if( p_item->b_advanced && !b_advanced )
2142 switch( p_item->i_type )
2144 case CONFIG_HINT_CATEGORY:
2145 case CONFIG_HINT_USAGE:
2146 if( !strcmp( "main", p_parser->psz_object_name ) )
2147 fprintf( stdout, "\n %s\n", p_item->psz_text );
2150 case CONFIG_ITEM_STRING:
2151 case CONFIG_ITEM_FILE:
2152 case CONFIG_ITEM_DIRECTORY:
2153 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2154 case CONFIG_ITEM_MODULE_CAT:
2155 case CONFIG_ITEM_MODULE_LIST:
2156 case CONFIG_ITEM_MODULE_LIST_CAT:
2157 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2159 if( p_item->ppsz_list )
2162 psz_type = psz_buffer;
2164 for( i = 0; p_item->ppsz_list[i]; i++ )
2166 if( i ) strcat( psz_type, "," );
2167 strcat( psz_type, p_item->ppsz_list[i] );
2172 case CONFIG_ITEM_INTEGER:
2173 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2174 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2176 if( p_item->i_list )
2179 psz_type = psz_buffer;
2181 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2183 if( i ) strcat( psz_type, ", " );
2184 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2186 p_item->ppsz_list_text[i] );
2191 case CONFIG_ITEM_FLOAT:
2192 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2194 case CONFIG_ITEM_BOOL:
2195 psz_bra = ""; psz_type = ""; psz_ket = "";
2196 if( !b_help_module )
2198 psz_suf = p_item->i_value ? _(" (default enabled)") :
2199 _(" (default disabled)");
2209 /* Add short option if any */
2210 if( p_item->i_short )
2212 sprintf( psz_short, "-%c,", p_item->i_short );
2216 strcpy( psz_short, " " );
2219 i = PADDING_SPACES - strlen( p_item->psz_name )
2220 - strlen( psz_bra ) - strlen( psz_type )
2221 - strlen( psz_ket ) - 1;
2223 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2225 psz_prefix = ", --no-";
2226 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2231 psz_spaces[0] = '\n';
2236 psz_spaces[i] = '\0';
2239 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2241 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2242 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2243 psz_ket, psz_spaces );
2247 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2248 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2251 psz_spaces[i] = ' ';
2253 /* We wrap the rest of the output */
2254 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2255 b_description = config_GetInt( p_this, "help-verbose" );
2258 psz_text = psz_buffer;
2261 char *psz_parser, *psz_word;
2262 size_t i_end = strlen( psz_text );
2264 /* If the remaining text fits in a line, print it. */
2265 if( i_end <= (size_t)i_width )
2267 fprintf( stdout, "%s\n", psz_text );
2271 /* Otherwise, eat as many words as possible */
2272 psz_parser = psz_text;
2275 psz_word = psz_parser;
2276 psz_parser = strchr( psz_word, ' ' );
2277 /* If no space was found, we reached the end of the text
2278 * block; otherwise, we skip the space we just found. */
2279 psz_parser = psz_parser ? psz_parser + 1
2282 } while( psz_parser - psz_text <= i_width );
2284 /* We cut a word in one of these cases:
2285 * - it's the only word in the line and it's too long.
2286 * - we used less than 80% of the width and the word we are
2287 * going to wrap is longer than 40% of the width, and even
2288 * if the word would have fit in the next line. */
2289 if( psz_word == psz_text
2290 || ( psz_word - psz_text < 80 * i_width / 100
2291 && psz_parser - psz_word > 40 * i_width / 100 ) )
2293 char c = psz_text[i_width];
2294 psz_text[i_width] = '\0';
2295 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2296 psz_text += i_width;
2301 psz_word[-1] = '\0';
2302 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2303 psz_text = psz_word;
2307 if( b_description && p_item->psz_longtext )
2309 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2310 b_description = VLC_FALSE;
2311 psz_spaces = psz_spaces_longtext;
2312 fprintf( stdout, "%s", psz_spaces );
2318 /* Release the module list */
2319 vlc_list_release( p_list );
2322 /*****************************************************************************
2323 * ListModules: list the available modules with their description
2324 *****************************************************************************
2325 * Print a list of all available modules (builtins and plugins) and a short
2326 * description for each one.
2327 *****************************************************************************/
2328 static void ListModules( vlc_t *p_this )
2332 char psz_spaces[22];
2335 memset( psz_spaces, ' ', 22 );
2341 /* List all modules */
2342 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2344 /* Enumerate each module */
2345 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2349 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2351 /* Nasty hack, but right now I'm too tired to think about a nice
2353 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2357 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2358 psz_spaces, p_parser->psz_longname );
2360 psz_spaces[i] = ' ';
2363 vlc_list_release( p_list );
2365 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2370 /*****************************************************************************
2371 * Version: print complete program version
2372 *****************************************************************************
2373 * Print complete program version and build number.
2374 *****************************************************************************/
2375 static void Version( void )
2381 fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
2382 fprintf( stdout, _("Compiled on %s by %s@%s.%s\n"), VLC_CompileTime(),
2383 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
2384 fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
2385 if( *VLC_Changeset() )
2386 fprintf( stdout, _("Based upon svn changeset [%s]\n"),
2389 _("This program comes with NO WARRANTY, to the extent permitted by "
2390 "law.\nYou may redistribute it under the terms of the GNU General "
2391 "Public License;\nsee the file named COPYING for details.\n"
2392 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2394 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2399 /*****************************************************************************
2400 * ShowConsole: On Win32, create an output console for debug messages
2401 *****************************************************************************
2402 * This function is useful only on Win32.
2403 *****************************************************************************/
2405 static void ShowConsole( void )
2409 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2412 freopen( "CONOUT$", "w", stdout );
2413 freopen( "CONOUT$", "w", stderr );
2414 freopen( "CONIN$", "r", stdin );
2420 /*****************************************************************************
2421 * PauseConsole: On Win32, wait for a key press before closing the console
2422 *****************************************************************************
2423 * This function is useful only on Win32.
2424 *****************************************************************************/
2426 static void PauseConsole( void )
2430 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2431 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2438 /*****************************************************************************
2439 * ConsoleWidth: Return the console width in characters
2440 *****************************************************************************
2441 * We use the stty shell command to get the console width; if this fails or
2442 * if the width is less than 80, we default to 80.
2443 *****************************************************************************/
2444 static int ConsoleWidth( void )
2449 char buf[20], *psz_parser;
2453 file = popen( "stty size 2>/dev/null", "r" );
2456 i_ret = fread( buf, 1, 20, file );
2460 psz_parser = strchr( buf, ' ' );
2463 i_ret = atoi( psz_parser + 1 );
2478 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2479 vlc_value_t old_val, vlc_value_t new_val, void *param)
2481 vlc_t *p_vlc = (vlc_t *)p_this;
2483 if( new_val.i_int >= -1 )
2485 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2490 /*****************************************************************************
2491 * InitDeviceValues: initialize device values
2492 *****************************************************************************
2493 * This function inits the dvd, vcd and cd-audio values
2494 *****************************************************************************/
2495 static void InitDeviceValues( vlc_t *p_vlc )
2498 LibHalContext * ctx;
2504 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2506 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2508 for( i = 0; i < i_devices; i++ )
2510 if( !hal_device_property_exists( ctx, devices[ i ],
2511 "storage.cdrom.dvd" ) )
2516 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2517 "storage.cdrom.dvd" );
2518 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2523 config_PutPsz( p_vlc, "dvd", block_dev );
2526 config_PutPsz( p_vlc, "vcd", block_dev );
2527 config_PutPsz( p_vlc, "cd-audio", block_dev );
2529 hal_free_string( block_dev );
2531 hal_free_string_array( devices );
2534 hal_shutdown( ctx );
2539 /*****************************************************************************
2540 * FromLocale: converts a locale string to UTF-8
2541 *****************************************************************************/
2542 char *FromLocale( const char *locale )
2544 if( locale == NULL )
2547 if( libvlc.from_locale != (vlc_iconv_t)(-1) )
2549 char *iptr = (char *)locale, *output, *optr;
2553 * We are not allowed to modify the locale pointer, even if we cast it
2556 inb = strlen( locale );
2559 /* FIXME: I'm not sure about the value for the multiplication
2560 * (for western people, multiplication by 3 (Latin9) is sufficient) */
2561 optr = output = calloc( outb , 1);
2563 vlc_mutex_lock( &libvlc.from_locale_lock );
2564 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2566 while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
2571 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2573 vlc_mutex_unlock( &libvlc.from_locale_lock );
2575 return realloc( output, strlen( output ) + 1 );
2577 return (char *)locale;
2580 /*****************************************************************************
2581 * ToLocale: converts an UTF-8 string to locale
2582 *****************************************************************************/
2583 char *ToLocale( const char *utf8 )
2588 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
2590 char *iptr = (char *)utf8, *output, *optr;
2594 * We are not allowed to modify the locale pointer, even if we cast it
2597 inb = strlen( utf8 );
2598 /* FIXME: I'm not sure about the value for the multiplication
2599 * (for western people, multiplication is not needed) */
2602 optr = output = calloc( outb, 1 );
2603 vlc_mutex_lock( &libvlc.to_locale_lock );
2604 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2606 while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
2609 *optr++ = '?'; /* should not happen, and yes, it sucks */
2611 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2613 vlc_mutex_unlock( &libvlc.to_locale_lock );
2615 return realloc( output, strlen( output ) + 1 );
2617 return (char *)utf8;
2620 void LocaleFree( const char *str )
2622 if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
2623 free( (char *)str );