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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_CompileBy, VLC_CompileHost, VLC_CompileDomain,
143 * VLC_Compiler, VLC_Changeset
144 *****************************************************************************/
145 #define DECLARE_VLC_VERSION( func, var ) \
146 char const * VLC_##func ( void ) \
151 DECLARE_VLC_VERSION( CompileBy, COMPILE_BY );
152 DECLARE_VLC_VERSION( CompileHost, COMPILE_HOST );
153 DECLARE_VLC_VERSION( CompileDomain, COMPILE_DOMAIN );
154 DECLARE_VLC_VERSION( Compiler, COMPILER );
156 extern const char psz_vlc_changeset[];
157 char const * VLC_Changeset( void )
159 return psz_vlc_changeset;
162 /*****************************************************************************
163 * VLC_Error: strerror() equivalent
164 *****************************************************************************
165 * This function returns full version string (numeric version and codename).
166 *****************************************************************************/
167 char const * VLC_Error( int i_err )
169 return vlc_error( i_err );
172 /*****************************************************************************
173 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
174 *****************************************************************************
175 * This function allocates a vlc_t structure and returns a negative value
176 * in case of failure. Also, the thread system is initialized.
177 *****************************************************************************/
178 int VLC_Create( void )
181 vlc_t * p_vlc = NULL;
184 /* &libvlc never changes, so we can safely call this multiple times. */
187 /* vlc_threads_init *must* be the first internal call! No other call is
188 * allowed before the thread system has been initialized. */
189 i_ret = vlc_threads_init( p_libvlc );
195 /* Now that the thread system is initialized, we don't have much, but
196 * at least we have var_Create */
197 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
198 var_Get( p_libvlc, "libvlc", &lockval );
199 vlc_mutex_lock( lockval.p_address );
200 if( !libvlc.b_ready )
204 /* Guess what CPU we have */
205 libvlc.i_cpu = CPUCapabilities();
207 /* Find verbosity from VLC_VERBOSE environment variable */
208 psz_env = getenv( "VLC_VERBOSE" );
209 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
211 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
212 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
214 libvlc.b_color = VLC_FALSE;
217 /* Initialize message queue */
218 msg_Create( p_libvlc );
220 /* Announce who we are */
221 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
222 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
224 /* The module bank will be initialized later */
225 libvlc.p_module_bank = NULL;
227 libvlc.b_ready = VLC_TRUE;
229 /* UTF-8 convertor are initialized after the locale */
230 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
232 vlc_mutex_unlock( lockval.p_address );
233 var_Destroy( p_libvlc, "libvlc" );
235 /* Allocate a vlc object */
236 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
241 p_vlc->thread_id = 0;
243 p_vlc->psz_object_name = "root";
245 /* Initialize mutexes */
246 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
248 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
249 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
252 /* Store our newly allocated structure in the global list */
253 vlc_object_attach( p_vlc, p_libvlc );
255 /* Store data for the non-reentrant API */
256 p_static_vlc = p_vlc;
258 return p_vlc->i_object_id;
261 /*****************************************************************************
262 * VLC_Init: initialize a vlc_t structure.
263 *****************************************************************************
264 * This function initializes a previously allocated vlc_t structure:
266 * - gettext initialization
267 * - message queue, module bank and playlist initialization
268 * - configuration and commandline parsing
269 *****************************************************************************/
270 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
272 char p_capabilities[200];
277 vlc_bool_t b_exit = VLC_FALSE;
278 int i_ret = VLC_EEXIT;
279 vlc_t * p_vlc = vlc_current_object( i_object );
280 module_t *p_help_module;
281 playlist_t *p_playlist;
283 #if defined( ENABLE_NLS ) \
284 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
294 * System specific initialization code
296 system_Init( p_vlc, &i_argc, ppsz_argv );
298 /* Get the executable name (similar to the basename command) */
301 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
304 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
310 p_vlc->psz_object_name = "vlc";
314 * Support for gettext
319 * Global iconv, must be done after setlocale()
320 * so that vlc_current_charset() works.
324 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
325 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
327 /* Initialize the module bank and load the configuration of the
328 * main module. We need to do this at this stage to be able to display
329 * a short help if required by the user. (short help == main module
331 module_InitBank( p_vlc );
333 /* Hack: insert the help module here */
334 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
335 if( p_help_module == NULL )
337 module_EndBank( p_vlc );
338 if( i_object ) vlc_object_release( p_vlc );
341 p_help_module->psz_object_name = "help";
342 p_help_module->psz_longname = N_("Help options");
343 config_Duplicate( p_help_module, p_help_config );
344 vlc_object_attach( p_help_module, libvlc.p_module_bank );
347 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
349 vlc_object_detach( p_help_module );
350 config_Free( p_help_module );
351 vlc_object_destroy( p_help_module );
352 module_EndBank( p_vlc );
353 if( i_object ) vlc_object_release( p_vlc );
357 /* Check for short help option */
358 if( config_GetInt( p_vlc, "help" ) )
360 Help( p_vlc, "help" );
362 i_ret = VLC_EEXITSUCCESS;
364 /* Check for version option */
365 else if( config_GetInt( p_vlc, "version" ) )
369 i_ret = VLC_EEXITSUCCESS;
372 /* Set the config file stuff */
373 p_vlc->psz_homedir = config_GetHomeDir();
374 p_vlc->psz_userdir = config_GetUserDir();
375 if( p_vlc->psz_userdir == NULL )
376 p_vlc->psz_userdir = strdup(p_vlc->psz_homedir);
377 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
378 if( p_vlc->psz_configfile != NULL && p_vlc->psz_configfile[0] == '~'
379 && p_vlc->psz_configfile[1] == '/' )
381 char *psz = malloc( strlen(p_vlc->psz_userdir)
382 + strlen(p_vlc->psz_configfile) );
383 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
384 sprintf( psz, "%s/%s", p_vlc->psz_userdir,
385 p_vlc->psz_configfile + 2 );
386 free( p_vlc->psz_configfile );
387 p_vlc->psz_configfile = psz;
390 /* Check for plugins cache options */
391 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
393 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
396 /* Hack: remove the help module here */
397 vlc_object_detach( p_help_module );
400 /* Will be re-done properly later on */
401 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
403 /* Check for daemon mode */
405 if( config_GetInt( p_vlc, "daemon" ) )
408 if( daemon( 1, 0) != 0 )
410 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
414 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
419 if( ( i_pid = fork() ) < 0 )
421 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
426 /* This is the parent, exit right now */
427 msg_Dbg( p_vlc, "closing parent process" );
429 i_ret = VLC_EEXITSUCCESS;
433 /* We are the child */
434 msg_Dbg( p_vlc, "daemon spawned" );
435 close( STDIN_FILENO );
436 close( STDOUT_FILENO );
437 close( STDERR_FILENO );
439 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
447 config_Free( p_help_module );
448 vlc_object_destroy( p_help_module );
449 module_EndBank( p_vlc );
450 if( i_object ) vlc_object_release( p_vlc );
454 /* Check for translation config option */
455 #if defined( ENABLE_NLS ) \
456 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
458 /* This ain't really nice to have to reload the config here but it seems
459 * the only way to do it. */
460 config_LoadConfigFile( p_vlc, "main" );
461 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
463 /* Check if the user specified a custom language */
464 psz_language = config_GetPsz( p_vlc, "language" );
465 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
467 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
469 /* Reset the default domain */
470 SetLanguage( psz_language );
474 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
475 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
477 module_EndBank( p_vlc );
478 module_InitBank( p_vlc );
479 config_LoadConfigFile( p_vlc, "main" );
480 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
481 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
483 if( psz_language ) free( psz_language );
487 * Load the builtins and plugins into the module_bank.
488 * We have to do it before config_Load*() because this also gets the
489 * list of configuration options exported by each module and loads their
492 module_LoadBuiltins( p_vlc );
493 module_LoadPlugins( p_vlc );
499 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
500 libvlc.p_module_bank->i_children );
502 /* Hack: insert the help module here */
503 vlc_object_attach( p_help_module, libvlc.p_module_bank );
506 /* Check for help on modules */
507 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
509 Help( p_vlc, p_tmp );
512 i_ret = VLC_EEXITSUCCESS;
514 /* Check for long help option */
515 else if( config_GetInt( p_vlc, "longhelp" ) )
517 Help( p_vlc, "longhelp" );
519 i_ret = VLC_EEXITSUCCESS;
521 /* Check for module list option */
522 else if( config_GetInt( p_vlc, "list" ) )
524 ListModules( p_vlc );
526 i_ret = VLC_EEXITSUCCESS;
529 /* Check for config file options */
530 if( config_GetInt( p_vlc, "reset-config" ) )
532 vlc_object_detach( p_help_module );
533 config_ResetAll( p_vlc );
534 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
535 config_SaveConfigFile( p_vlc, NULL );
536 vlc_object_attach( p_help_module, libvlc.p_module_bank );
538 if( config_GetInt( p_vlc, "save-config" ) )
540 vlc_object_detach( p_help_module );
541 config_LoadConfigFile( p_vlc, NULL );
542 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
543 config_SaveConfigFile( p_vlc, NULL );
544 vlc_object_attach( p_help_module, libvlc.p_module_bank );
547 /* Hack: remove the help module here */
548 vlc_object_detach( p_help_module );
553 config_Free( p_help_module );
554 vlc_object_destroy( p_help_module );
555 module_EndBank( p_vlc );
556 if( i_object ) vlc_object_release( p_vlc );
563 InitDeviceValues( p_vlc );
566 * Override default configuration with config file settings
568 config_LoadConfigFile( p_vlc, NULL );
570 /* Hack: insert the help module here */
571 vlc_object_attach( p_help_module, libvlc.p_module_bank );
575 * Override configuration with command line settings
577 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
581 /* Pause the console because it's destroyed when we exit */
582 fprintf( stderr, "The command line options couldn't be loaded, check "
583 "that they are valid.\n" );
586 vlc_object_detach( p_help_module );
587 config_Free( p_help_module );
588 vlc_object_destroy( p_help_module );
589 module_EndBank( p_vlc );
590 if( i_object ) vlc_object_release( p_vlc );
594 /* Hack: remove the help module here */
595 vlc_object_detach( p_help_module );
596 config_Free( p_help_module );
597 vlc_object_destroy( p_help_module );
601 * System specific configuration
603 system_Configure( p_vlc, &i_argc, ppsz_argv );
606 * Message queue options
609 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
610 if( config_GetInt( p_vlc, "quiet" ) )
613 var_Set( p_vlc, "verbose", val );
615 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
616 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
618 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
621 * Output messages that may still be in the queue
625 /* p_vlc initialization. FIXME ? */
627 if( !config_GetInt( p_vlc, "fpu" ) )
628 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
630 #if defined( __i386__ ) || defined( __x86_64__ )
631 if( !config_GetInt( p_vlc, "mmx" ) )
632 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
633 if( !config_GetInt( p_vlc, "3dn" ) )
634 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
635 if( !config_GetInt( p_vlc, "mmxext" ) )
636 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
637 if( !config_GetInt( p_vlc, "sse" ) )
638 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
639 if( !config_GetInt( p_vlc, "sse2" ) )
640 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
642 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
643 if( !config_GetInt( p_vlc, "altivec" ) )
644 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
647 #define PRINT_CAPABILITY( capability, string ) \
648 if( libvlc.i_cpu & capability ) \
650 strncat( p_capabilities, string " ", \
651 sizeof(p_capabilities) - strlen(p_capabilities) ); \
652 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
655 p_capabilities[0] = '\0';
656 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
657 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
658 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
659 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
660 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
661 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
662 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
663 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
664 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
665 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
666 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
669 * Choose the best memcpy module
671 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
673 if( p_vlc->pf_memcpy == NULL )
675 p_vlc->pf_memcpy = memcpy;
678 if( p_vlc->pf_memset == NULL )
680 p_vlc->pf_memset = memset;
683 libvlc.b_stats = config_GetInt( p_vlc, "stats" );
686 * Initialize hotkey handling
688 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
689 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
690 /* Do a copy (we don't need to modify the strings) */
691 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
694 * Initialize playlist and get commandline files
696 p_playlist = playlist_Create( p_vlc );
699 msg_Err( p_vlc, "playlist initialization failed" );
700 if( p_vlc->p_memcpy_module != NULL )
702 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
704 module_EndBank( p_vlc );
705 if( i_object ) vlc_object_release( p_vlc );
709 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
710 if( psz_modules && *psz_modules )
712 /* Add service discovery modules */
713 playlist_AddSDModules( p_playlist, psz_modules );
715 if( psz_modules ) free( psz_modules );
718 * Load background interfaces
720 psz_modules = config_GetPsz( p_vlc, "extraintf" );
721 psz_control = config_GetPsz( p_vlc, "control" );
723 if( psz_modules && *psz_modules && psz_control && *psz_control )
725 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
726 strlen( psz_control ) + 1 );
727 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
729 else if( psz_control && *psz_control )
731 if( psz_modules ) free( psz_modules );
732 psz_modules = strdup( psz_control );
735 psz_parser = psz_modules;
736 while ( psz_parser && *psz_parser )
738 char *psz_module, *psz_temp;
739 psz_module = psz_parser;
740 psz_parser = strchr( psz_module, ':' );
746 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
749 sprintf( psz_temp, "%s,none", psz_module );
750 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
760 * Always load the hotkeys interface if it exists
762 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
765 * If needed, load the Xscreensaver interface
766 * Currently, only for X
768 #ifdef HAVE_X11_XLIB_H
769 if( config_GetInt( p_vlc, "disable-screensaver" ) == 1 )
771 VLC_AddIntf( 0, "screensaver", VLC_FALSE, VLC_FALSE );
776 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
778 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
779 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
780 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
781 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
782 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
783 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
784 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
785 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
786 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
787 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
788 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
789 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
791 /* Create volume callback system. */
792 var_Create( p_vlc, "volume-change", VLC_VAR_BOOL );
795 * Get input filenames given as commandline arguments
797 GetFilenames( p_vlc, i_argc, ppsz_argv );
800 * Get --open argument
802 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
803 var_Get( p_vlc, "open", &val );
804 if ( val.psz_string != NULL && *val.psz_string )
806 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
807 PLAYLIST_INSERT, 0 );
809 if ( val.psz_string != NULL ) free( val.psz_string );
811 if( i_object ) vlc_object_release( p_vlc );
815 /*****************************************************************************
816 * VLC_AddIntf: add an interface
817 *****************************************************************************
818 * This function opens an interface plugin and runs it. If b_block is set
819 * to 0, VLC_AddIntf will return immediately and let the interface run in a
820 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
821 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
822 * the playlist when it is completely initialised.
823 *****************************************************************************/
824 int VLC_AddIntf( int i_object, char const *psz_module,
825 vlc_bool_t b_block, vlc_bool_t b_play )
828 intf_thread_t *p_intf;
829 vlc_t *p_vlc = vlc_current_object( i_object );
837 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
840 * We prefer the dummy interface if none is specified. */
841 char *psz_interface = config_GetPsz( p_vlc, "intf" );
842 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
843 if( psz_interface ) free( psz_interface );
847 /* Try to create the interface */
848 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
852 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
853 if( i_object ) vlc_object_release( p_vlc );
857 /* Interface doesn't handle play on start so do it ourselves */
858 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
860 /* Try to run the interface */
861 p_intf->b_play = b_play;
862 p_intf->b_block = b_block;
863 i_err = intf_RunThread( p_intf );
866 vlc_object_detach( p_intf );
867 intf_Destroy( p_intf );
868 if( i_object ) vlc_object_release( p_vlc );
872 if( i_object ) vlc_object_release( p_vlc );
876 /*****************************************************************************
877 * VLC_Die: ask vlc to die.
878 *****************************************************************************
879 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
880 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
881 *****************************************************************************/
882 int VLC_Die( int i_object )
884 vlc_t *p_vlc = vlc_current_object( i_object );
891 p_vlc->b_die = VLC_TRUE;
893 if( i_object ) vlc_object_release( p_vlc );
897 /*****************************************************************************
898 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
899 *****************************************************************************/
900 int VLC_CleanUp( int i_object )
902 intf_thread_t * p_intf;
903 playlist_t * p_playlist;
904 vout_thread_t * p_vout;
905 aout_instance_t * p_aout;
906 announce_handler_t * p_announce;
907 stats_handler_t * p_stats;
908 vlc_t *p_vlc = vlc_current_object( i_object );
910 /* Check that the handle is valid */
917 * Ask the interfaces to stop and destroy them
919 msg_Dbg( p_vlc, "removing all interfaces" );
920 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
922 intf_StopThread( p_intf );
923 vlc_object_detach( p_intf );
924 vlc_object_release( p_intf );
925 intf_Destroy( p_intf );
931 msg_Dbg( p_vlc, "removing playlist handler" );
932 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
935 vlc_object_detach( p_playlist );
936 vlc_object_release( p_playlist );
937 playlist_Destroy( p_playlist );
943 msg_Dbg( p_vlc, "removing all video outputs" );
944 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
946 vlc_object_detach( p_vout );
947 vlc_object_release( p_vout );
948 vout_Destroy( p_vout );
954 msg_Dbg( p_vlc, "removing all audio outputs" );
955 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
957 vlc_object_detach( (vlc_object_t *)p_aout );
958 vlc_object_release( (vlc_object_t *)p_aout );
959 aout_Delete( p_aout );
962 while( ( p_stats = vlc_object_find( p_vlc, VLC_OBJECT_STATS, FIND_CHILD) ))
964 vlc_object_detach( (vlc_object_t*) p_stats );
965 vlc_object_release( (vlc_object_t *)p_stats );
970 * Free announce handler(s?)
972 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
975 msg_Dbg( p_vlc, "removing announce handler" );
976 vlc_object_detach( p_announce );
977 vlc_object_release( p_announce );
978 announce_HandlerDestroy( p_announce );
981 if( i_object ) vlc_object_release( p_vlc );
985 /*****************************************************************************
986 * VLC_Destroy: Destroy everything.
987 *****************************************************************************
988 * This function requests the running threads to finish, waits for their
989 * termination, and destroys their structure.
990 *****************************************************************************/
991 int VLC_Destroy( int i_object )
993 vlc_t *p_vlc = vlc_current_object( i_object );
1001 * Free allocated memory
1003 if( p_vlc->p_memcpy_module )
1005 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
1006 p_vlc->p_memcpy_module = NULL;
1010 * Free module bank !
1012 module_EndBank( p_vlc );
1014 if( p_vlc->psz_homedir )
1016 free( p_vlc->psz_homedir );
1017 p_vlc->psz_homedir = NULL;
1020 if( p_vlc->psz_userdir )
1022 free( p_vlc->psz_userdir );
1023 p_vlc->psz_userdir = NULL;
1026 if( p_vlc->psz_configfile )
1028 free( p_vlc->psz_configfile );
1029 p_vlc->psz_configfile = NULL;
1032 if( p_vlc->p_hotkeys )
1034 free( p_vlc->p_hotkeys );
1035 p_vlc->p_hotkeys = NULL;
1039 * System specific cleaning code
1041 system_End( p_vlc );
1044 * Free message queue.
1045 * Nobody shall use msg_* afterward.
1048 msg_Destroy( p_libvlc );
1050 /* Destroy global iconv */
1053 /* Destroy mutexes */
1054 vlc_mutex_destroy( &p_vlc->config_lock );
1056 vlc_object_detach( p_vlc );
1058 /* Release object before destroying it */
1059 if( i_object ) vlc_object_release( p_vlc );
1061 vlc_object_destroy( p_vlc );
1063 /* Stop thread system: last one out please shut the door! */
1064 vlc_threads_end( p_libvlc );
1069 /*****************************************************************************
1070 * VLC_VariableSet: set a vlc variable
1071 *****************************************************************************/
1072 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
1074 vlc_t *p_vlc = vlc_current_object( i_object );
1082 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1083 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1084 if( !strncmp( psz_var, "conf::", 6 ) )
1086 module_config_t *p_item;
1087 char const *psz_newvar = psz_var + 6;
1089 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1093 switch( p_item->i_type )
1095 case CONFIG_ITEM_BOOL:
1096 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1098 case CONFIG_ITEM_INTEGER:
1099 config_PutInt( p_vlc, psz_newvar, value.i_int );
1101 case CONFIG_ITEM_FLOAT:
1102 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1105 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1108 if( i_object ) vlc_object_release( p_vlc );
1113 i_ret = var_Set( p_vlc, psz_var, value );
1115 if( i_object ) vlc_object_release( p_vlc );
1119 /*****************************************************************************
1120 * VLC_VariableGet: get a vlc variable
1121 *****************************************************************************/
1122 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1124 vlc_t *p_vlc = vlc_current_object( i_object );
1132 i_ret = var_Get( p_vlc , psz_var, p_value );
1134 if( i_object ) vlc_object_release( p_vlc );
1138 /*****************************************************************************
1139 * VLC_VariableType: get a vlc variable type
1140 *****************************************************************************/
1141 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1144 vlc_t *p_vlc = vlc_current_object( i_object );
1151 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1152 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1153 if( !strncmp( psz_var, "conf::", 6 ) )
1155 module_config_t *p_item;
1156 char const *psz_newvar = psz_var + 6;
1158 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1162 switch( p_item->i_type )
1164 case CONFIG_ITEM_BOOL:
1165 i_type = VLC_VAR_BOOL;
1167 case CONFIG_ITEM_INTEGER:
1168 i_type = VLC_VAR_INTEGER;
1170 case CONFIG_ITEM_FLOAT:
1171 i_type = VLC_VAR_FLOAT;
1174 i_type = VLC_VAR_STRING;
1182 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1184 if( i_object ) vlc_object_release( p_vlc );
1194 /*****************************************************************************
1195 * VLC_AddTarget: adds a target for playing.
1196 *****************************************************************************
1197 * This function adds psz_target to the current playlist. If a playlist does
1198 * not exist, it will create one.
1199 *****************************************************************************/
1200 int VLC_AddTarget( int i_object, char const *psz_target,
1201 char const **ppsz_options, int i_options,
1202 int i_mode, int i_pos )
1205 playlist_t *p_playlist;
1206 vlc_t *p_vlc = vlc_current_object( i_object );
1213 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1215 if( p_playlist == NULL )
1217 msg_Dbg( p_vlc, "no playlist present, creating one" );
1218 p_playlist = playlist_Create( p_vlc );
1220 if( p_playlist == NULL )
1222 if( i_object ) vlc_object_release( p_vlc );
1223 return VLC_EGENERIC;
1226 vlc_object_yield( p_playlist );
1229 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1230 i_mode, i_pos, -1, ppsz_options, i_options);
1232 vlc_object_release( p_playlist );
1234 if( i_object ) vlc_object_release( p_vlc );
1238 /*****************************************************************************
1239 * VLC_Play: play the playlist
1240 *****************************************************************************/
1241 int VLC_Play( int i_object )
1243 playlist_t * p_playlist;
1244 vlc_t *p_vlc = vlc_current_object( i_object );
1246 /* Check that the handle is valid */
1252 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1256 if( i_object ) vlc_object_release( p_vlc );
1260 playlist_Play( p_playlist );
1261 vlc_object_release( p_playlist );
1263 if( i_object ) vlc_object_release( p_vlc );
1267 /*****************************************************************************
1268 * VLC_Pause: toggle pause
1269 *****************************************************************************/
1270 int VLC_Pause( int i_object )
1272 playlist_t * p_playlist;
1273 vlc_t *p_vlc = vlc_current_object( i_object );
1275 /* Check that the handle is valid */
1281 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1285 if( i_object ) vlc_object_release( p_vlc );
1289 playlist_Pause( p_playlist );
1290 vlc_object_release( p_playlist );
1292 if( i_object ) vlc_object_release( p_vlc );
1296 /*****************************************************************************
1297 * VLC_Pause: toggle pause
1298 *****************************************************************************/
1299 int VLC_Stop( int i_object )
1301 playlist_t * p_playlist;
1302 vlc_t *p_vlc = vlc_current_object( i_object );
1304 /* Check that the handle is valid */
1310 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1314 if( i_object ) vlc_object_release( p_vlc );
1318 playlist_Stop( p_playlist );
1319 vlc_object_release( p_playlist );
1321 if( i_object ) vlc_object_release( p_vlc );
1325 /*****************************************************************************
1326 * VLC_IsPlaying: Query for Playlist Status
1327 *****************************************************************************/
1328 vlc_bool_t VLC_IsPlaying( int i_object )
1330 playlist_t * p_playlist;
1331 vlc_bool_t b_playing;
1333 vlc_t *p_vlc = vlc_current_object( i_object );
1335 /* Check that the handle is valid */
1341 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1345 if( i_object ) vlc_object_release( p_vlc );
1349 if( p_playlist->p_input )
1352 var_Get( p_playlist->p_input, "state", &val );
1353 b_playing = ( val.i_int == PLAYING_S );
1357 msg_Dbg(p_vlc, "polling playlist_IsPlaying");
1358 b_playing = playlist_IsPlaying( p_playlist );
1360 vlc_object_release( p_playlist );
1362 if( i_object ) vlc_object_release( p_vlc );
1367 * Get the current position in a input
1369 * Return the current position as a float
1370 * \note For some inputs, this will be unknown.
1372 * \param i_object a vlc object id
1373 * \return a float in the range of 0.0 - 1.0
1375 float VLC_PositionGet( int i_object )
1377 input_thread_t *p_input;
1379 vlc_t *p_vlc = vlc_current_object( i_object );
1381 /* Check that the handle is valid */
1387 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1391 if( i_object ) vlc_object_release( p_vlc );
1395 var_Get( p_input, "position", &val );
1396 vlc_object_release( p_input );
1398 if( i_object ) vlc_object_release( p_vlc );
1403 * Set the current position in a input
1405 * Set the current position in a input and then return
1406 * the current position as a float.
1407 * \note For some inputs, this will be unknown.
1409 * \param i_object a vlc object id
1410 * \param i_position a float in the range of 0.0 - 1.0
1411 * \return a float in the range of 0.0 - 1.0
1413 float VLC_PositionSet( int i_object, float i_position )
1415 input_thread_t *p_input;
1417 vlc_t *p_vlc = vlc_current_object( i_object );
1419 /* Check that the handle is valid */
1425 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1429 if( i_object ) vlc_object_release( p_vlc );
1433 val.f_float = i_position;
1434 var_Set( p_input, "position", val );
1435 var_Get( p_input, "position", &val );
1436 vlc_object_release( p_input );
1438 if( i_object ) vlc_object_release( p_vlc );
1443 * Get the current position in a input
1445 * Return the current position in seconds from the start.
1446 * \note For some inputs, this will be unknown.
1448 * \param i_object a vlc object id
1449 * \return the offset from 0:00 in seconds
1451 int VLC_TimeGet( int i_object )
1453 input_thread_t *p_input;
1455 vlc_t *p_vlc = vlc_current_object( i_object );
1457 /* Check that the handle is valid */
1463 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1467 if( i_object ) vlc_object_release( p_vlc );
1471 var_Get( p_input, "time", &val );
1472 vlc_object_release( p_input );
1474 if( i_object ) vlc_object_release( p_vlc );
1475 return val.i_time / 1000000;
1479 * Seek to a position in the current input
1481 * Seek i_seconds in the current input. If b_relative is set,
1482 * then the seek will be relative to the current position, otherwise
1483 * it will seek to i_seconds from the beginning of the input.
1484 * \note For some inputs, this will be unknown.
1486 * \param i_object a vlc object id
1487 * \param i_seconds seconds from current position or from beginning of input
1488 * \param b_relative seek relative from current position
1489 * \return VLC_SUCCESS on success
1491 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1493 input_thread_t *p_input;
1495 vlc_t *p_vlc = vlc_current_object( i_object );
1497 /* Check that the handle is valid */
1503 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1507 if( i_object ) vlc_object_release( p_vlc );
1513 val.i_time = i_seconds;
1514 val.i_time = val.i_time * 1000000L;
1515 var_Set( p_input, "time-offset", val );
1519 val.i_time = i_seconds;
1520 val.i_time = val.i_time * 1000000L;
1521 var_Set( p_input, "time", val );
1523 vlc_object_release( p_input );
1525 if( i_object ) vlc_object_release( p_vlc );
1530 * Get the total length of a input
1532 * Return the total length in seconds from the current input.
1533 * \note For some inputs, this will be unknown.
1535 * \param i_object a vlc object id
1536 * \return the length in seconds
1538 int VLC_LengthGet( int i_object )
1540 input_thread_t *p_input;
1542 vlc_t *p_vlc = vlc_current_object( i_object );
1544 /* Check that the handle is valid */
1550 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1554 if( i_object ) vlc_object_release( p_vlc );
1558 var_Get( p_input, "length", &val );
1559 vlc_object_release( p_input );
1561 if( i_object ) vlc_object_release( p_vlc );
1562 return val.i_time / 1000000L;
1566 * Play the input faster than realtime
1568 * 2x, 4x, 8x faster than realtime
1569 * \note For some inputs, this will be impossible.
1571 * \param i_object a vlc object id
1572 * \return the current speedrate
1574 float VLC_SpeedFaster( int i_object )
1576 input_thread_t *p_input;
1578 vlc_t *p_vlc = vlc_current_object( i_object );
1580 /* Check that the handle is valid */
1586 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1590 if( i_object ) vlc_object_release( p_vlc );
1594 val.b_bool = VLC_TRUE;
1595 var_Set( p_input, "rate-faster", val );
1596 var_Get( p_input, "rate", &val );
1597 vlc_object_release( p_input );
1599 if( i_object ) vlc_object_release( p_vlc );
1600 return val.f_float / INPUT_RATE_DEFAULT;
1604 * Play the input slower than realtime
1606 * 1/2x, 1/4x, 1/8x slower than realtime
1607 * \note For some inputs, this will be impossible.
1609 * \param i_object a vlc object id
1610 * \return the current speedrate
1612 float VLC_SpeedSlower( int i_object )
1614 input_thread_t *p_input;
1616 vlc_t *p_vlc = vlc_current_object( i_object );
1618 /* Check that the handle is valid */
1624 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1628 if( i_object ) vlc_object_release( p_vlc );
1632 val.b_bool = VLC_TRUE;
1633 var_Set( p_input, "rate-slower", val );
1634 var_Get( p_input, "rate", &val );
1635 vlc_object_release( p_input );
1637 if( i_object ) vlc_object_release( p_vlc );
1638 return val.f_float / INPUT_RATE_DEFAULT;
1642 * Return the current playlist item
1644 * Returns the index of the playlistitem that is currently selected for play.
1645 * This is valid even if nothing is currently playing.
1647 * \param i_object a vlc object id
1648 * \return the current index
1650 int VLC_PlaylistIndex( int i_object )
1653 playlist_t * p_playlist;
1654 vlc_t *p_vlc = vlc_current_object( i_object );
1656 /* Check that the handle is valid */
1662 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1666 if( i_object ) vlc_object_release( p_vlc );
1670 i_index = p_playlist->i_index;
1671 vlc_object_release( p_playlist );
1673 if( i_object ) vlc_object_release( p_vlc );
1678 * Total amount of items in the playlist
1680 * \param i_object a vlc object id
1681 * \return amount of playlist items
1683 int VLC_PlaylistNumberOfItems( int i_object )
1686 playlist_t * p_playlist;
1687 vlc_t *p_vlc = vlc_current_object( i_object );
1689 /* Check that the handle is valid */
1695 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1699 if( i_object ) vlc_object_release( p_vlc );
1703 i_size = p_playlist->i_size;
1704 vlc_object_release( p_playlist );
1706 if( i_object ) vlc_object_release( p_vlc );
1711 * Next playlist item
1713 * Skip to the next playlistitem and play it.
1715 * \param i_object a vlc object id
1716 * \return VLC_SUCCESS on success
1718 int VLC_PlaylistNext( int i_object )
1720 playlist_t * p_playlist;
1721 vlc_t *p_vlc = vlc_current_object( i_object );
1723 /* Check that the handle is valid */
1729 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1733 if( i_object ) vlc_object_release( p_vlc );
1737 playlist_Next( p_playlist );
1738 vlc_object_release( p_playlist );
1740 if( i_object ) vlc_object_release( p_vlc );
1745 * Previous playlist item
1747 * Skip to the previous playlistitem and play it.
1749 * \param i_object a vlc object id
1750 * \return VLC_SUCCESS on success
1752 int VLC_PlaylistPrev( int i_object )
1754 playlist_t * p_playlist;
1755 vlc_t *p_vlc = vlc_current_object( i_object );
1757 /* Check that the handle is valid */
1763 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1767 if( i_object ) vlc_object_release( p_vlc );
1771 playlist_Prev( p_playlist );
1772 vlc_object_release( p_playlist );
1774 if( i_object ) vlc_object_release( p_vlc );
1779 /*****************************************************************************
1780 * VLC_PlaylistClear: Empty the playlist
1781 *****************************************************************************/
1782 int VLC_PlaylistClear( int i_object )
1785 playlist_t * p_playlist;
1786 vlc_t *p_vlc = vlc_current_object( i_object );
1788 /* Check that the handle is valid */
1794 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1798 if( i_object ) vlc_object_release( p_vlc );
1802 i_err = playlist_Clear( p_playlist );
1804 vlc_object_release( p_playlist );
1806 if( i_object ) vlc_object_release( p_vlc );
1813 * \param i_object a vlc object id
1814 * \param i_volume something in a range from 0-200
1815 * \return the new volume (range 0-200 %)
1817 int VLC_VolumeSet( int i_object, int i_volume )
1819 audio_volume_t i_vol = 0;
1820 vlc_t *p_vlc = vlc_current_object( i_object );
1822 /* Check that the handle is valid */
1828 if( i_volume >= 0 && i_volume <= 200 )
1830 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1831 aout_VolumeSet( p_vlc, i_vol );
1834 if( i_object ) vlc_object_release( p_vlc );
1835 return i_vol * 200 / AOUT_VOLUME_MAX;
1839 * Get the current volume
1841 * Retrieve the current volume.
1843 * \param i_object a vlc object id
1844 * \return the current volume (range 0-200 %)
1846 int VLC_VolumeGet( int i_object )
1848 audio_volume_t i_volume;
1849 vlc_t *p_vlc = vlc_current_object( i_object );
1851 /* Check that the handle is valid */
1857 aout_VolumeGet( p_vlc, &i_volume );
1859 if( i_object ) vlc_object_release( p_vlc );
1860 return i_volume*200/AOUT_VOLUME_MAX;
1864 * Mute/Unmute the volume
1866 * \param i_object a vlc object id
1867 * \return VLC_SUCCESS on success
1869 int VLC_VolumeMute( int i_object )
1871 vlc_t *p_vlc = vlc_current_object( i_object );
1873 /* Check that the handle is valid */
1879 aout_VolumeMute( p_vlc, NULL );
1881 if( i_object ) vlc_object_release( p_vlc );
1885 /*****************************************************************************
1886 * VLC_FullScreen: toggle fullscreen mode
1887 *****************************************************************************/
1888 int VLC_FullScreen( int i_object )
1890 vout_thread_t *p_vout;
1891 vlc_t *p_vlc = vlc_current_object( i_object );
1898 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1902 if( i_object ) vlc_object_release( p_vlc );
1906 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1907 vlc_object_release( p_vout );
1909 if( i_object ) vlc_object_release( p_vlc );
1913 /* following functions are local */
1915 static void LocaleInit( void )
1919 if( !vlc_current_charset( &psz_charset ) )
1921 char *psz_conv = psz_charset;
1924 * Still allow non-ASCII characters when the locale is not set.
1925 * Western Europeans are being favored for historical reasons.
1927 psz_conv = strcmp( psz_charset, "ASCII" )
1931 vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
1932 vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
1933 libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
1934 libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
1935 if( !libvlc.to_locale )
1937 /* Not sure it is the right thing to do, but at least it
1938 doesn't make vlc crash with msvc ! */
1939 libvlc.to_locale = (vlc_iconv_t)(-1);
1943 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
1944 free( psz_charset );
1947 static void LocaleDeinit( void )
1949 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
1951 vlc_mutex_destroy( &libvlc.from_locale_lock );
1952 vlc_mutex_destroy( &libvlc.to_locale_lock );
1953 vlc_iconv_close( libvlc.from_locale );
1954 vlc_iconv_close( libvlc.to_locale );
1958 /*****************************************************************************
1959 * SetLanguage: set the interface language.
1960 *****************************************************************************
1961 * We set the LC_MESSAGES locale category for interface messages and buttons,
1962 * as well as the LC_CTYPE category for string sorting and possible wide
1963 * character support.
1964 *****************************************************************************/
1965 static void SetLanguage ( char const *psz_lang )
1967 #if defined( ENABLE_NLS ) \
1968 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1971 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1975 if( psz_lang && !*psz_lang )
1977 # if defined( HAVE_LC_MESSAGES )
1978 setlocale( LC_MESSAGES, psz_lang );
1980 setlocale( LC_CTYPE, psz_lang );
1985 /* I need that under Darwin, please check it doesn't disturb
1986 * other platforms. --Meuuh */
1987 setenv( "LANG", psz_lang, 1 );
1989 #elif defined( SYS_BEOS ) || defined( WIN32 )
1990 /* We set LC_ALL manually because it is the only way to set
1991 * the language at runtime under eg. Windows. Beware that this
1992 * makes the environment unconsistent when libvlc is unloaded and
1993 * should probably be moved to a safer place like vlc.c. */
1994 static char psz_lcall[20];
1995 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1996 psz_lcall[19] = '\0';
1997 putenv( psz_lcall );
2000 setlocale( LC_ALL, psz_lang );
2001 /* many code paths assume that float numbers are formatted according
2002 * to the US standard (ie. with dot as decimal point), so we keep
2003 * C for LC_NUMERIC. */
2004 setlocale(LC_NUMERIC, "C" );
2007 /* Specify where to find the locales for current domain */
2008 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
2009 psz_path = LOCALEDIR;
2011 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
2015 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
2017 fprintf( stderr, "warning: couldn't bind domain %s in directory %s\n",
2018 PACKAGE_NAME, psz_path );
2021 /* Set the default domain */
2022 textdomain( PACKAGE_NAME );
2023 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
2027 /*****************************************************************************
2028 * GetFilenames: parse command line options which are not flags
2029 *****************************************************************************
2030 * Parse command line for input files as well as their associated options.
2031 * An option always follows its associated input and begins with a ":".
2032 *****************************************************************************/
2033 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
2035 int i_opt, i_options;
2037 /* We assume that the remaining parameters are filenames
2038 * and their input options */
2039 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
2041 const char *psz_target;
2044 /* Count the input options */
2045 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
2051 /* TODO: write an internal function of this one, to avoid
2052 * unnecessary lookups. */
2053 /* FIXME: should we convert options to UTF-8 as well ?? */
2054 psz_target = FromLocale( ppsz_argv[ i_opt ] );
2055 VLC_AddTarget( p_vlc->i_object_id, psz_target,
2056 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
2058 PLAYLIST_INSERT, 0 );
2059 LocaleFree( psz_target );
2065 /*****************************************************************************
2066 * Help: print program help
2067 *****************************************************************************
2068 * Print a short inline help. Message interface is initialized at this stage.
2069 *****************************************************************************/
2070 static void Help( vlc_t *p_this, char const *psz_help_name )
2076 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
2078 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2079 Usage( p_this, "help" );
2080 Usage( p_this, "main" );
2082 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2084 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2085 Usage( p_this, NULL );
2087 else if( psz_help_name )
2089 Usage( p_this, psz_help_name );
2092 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2097 /*****************************************************************************
2098 * Usage: print module usage
2099 *****************************************************************************
2100 * Print a short inline help. Message interface is initialized at this stage.
2101 *****************************************************************************/
2102 static void Usage( vlc_t *p_this, char const *psz_module_name )
2104 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
2105 /* short option ------' | | | | | | |
2106 * option name ------------' | | | | | |
2107 * <bra -------------------------' | | | | |
2108 * option type or "" --------------' | | | |
2109 * ket> -----------------------------' | | |
2110 * padding spaces ---------------------' | |
2111 * comment -------------------------------' |
2112 * comment suffix --------------------------'
2114 * The purpose of having bra and ket is that we might i18n them as well.
2116 #define LINE_START 8
2117 #define PADDING_SPACES 25
2120 module_config_t *p_item;
2121 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2122 char psz_spaces_longtext[LINE_START+3];
2123 char psz_format[sizeof(FORMAT_STRING)];
2124 char psz_buffer[10000];
2127 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2128 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2129 vlc_bool_t b_description;
2131 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2132 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2133 memset( psz_spaces_longtext, ' ', LINE_START+2 );
2134 psz_spaces_longtext[LINE_START+2] = '\0';
2136 strcpy( psz_format, FORMAT_STRING );
2138 /* List all modules */
2139 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2141 /* Enumerate the config for each module */
2142 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2144 vlc_bool_t b_help_module;
2146 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2148 if( psz_module_name && strcmp( psz_module_name,
2149 p_parser->psz_object_name ) )
2154 /* Ignore modules without config options */
2155 if( !p_parser->i_config_items )
2160 /* Ignore modules with only advanced config options if requested */
2163 for( p_item = p_parser->p_config;
2164 p_item->i_type != CONFIG_HINT_END;
2167 if( (p_item->i_type & CONFIG_ITEM) &&
2168 !p_item->b_advanced ) break;
2170 if( p_item->i_type == CONFIG_HINT_END ) continue;
2173 /* Print name of module */
2174 if( strcmp( "main", p_parser->psz_object_name ) )
2175 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2177 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2179 /* Print module options */
2180 for( p_item = p_parser->p_config;
2181 p_item->i_type != CONFIG_HINT_END;
2184 char *psz_text, *psz_spaces = psz_spaces_text;
2185 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2186 char *psz_suf = "", *psz_prefix = NULL;
2189 /* Skip deprecated options */
2190 if( p_item->psz_current )
2194 /* Skip advanced options if requested */
2195 if( p_item->b_advanced && !b_advanced )
2200 switch( p_item->i_type )
2202 case CONFIG_HINT_CATEGORY:
2203 case CONFIG_HINT_USAGE:
2204 if( !strcmp( "main", p_parser->psz_object_name ) )
2205 fprintf( stdout, "\n %s\n", p_item->psz_text );
2208 case CONFIG_ITEM_STRING:
2209 case CONFIG_ITEM_FILE:
2210 case CONFIG_ITEM_DIRECTORY:
2211 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2212 case CONFIG_ITEM_MODULE_CAT:
2213 case CONFIG_ITEM_MODULE_LIST:
2214 case CONFIG_ITEM_MODULE_LIST_CAT:
2215 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2217 if( p_item->ppsz_list )
2220 psz_type = psz_buffer;
2222 for( i = 0; p_item->ppsz_list[i]; i++ )
2224 if( i ) strcat( psz_type, "," );
2225 strcat( psz_type, p_item->ppsz_list[i] );
2230 case CONFIG_ITEM_INTEGER:
2231 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2232 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2234 if( p_item->i_list )
2237 psz_type = psz_buffer;
2239 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2241 if( i ) strcat( psz_type, ", " );
2242 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2244 p_item->ppsz_list_text[i] );
2249 case CONFIG_ITEM_FLOAT:
2250 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2252 case CONFIG_ITEM_BOOL:
2253 psz_bra = ""; psz_type = ""; psz_ket = "";
2254 if( !b_help_module )
2256 psz_suf = p_item->i_value ? _(" (default enabled)") :
2257 _(" (default disabled)");
2267 /* Add short option if any */
2268 if( p_item->i_short )
2270 sprintf( psz_short, "-%c,", p_item->i_short );
2274 strcpy( psz_short, " " );
2277 i = PADDING_SPACES - strlen( p_item->psz_name )
2278 - strlen( psz_bra ) - strlen( psz_type )
2279 - strlen( psz_ket ) - 1;
2281 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2283 psz_prefix = ", --no-";
2284 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2289 psz_spaces[0] = '\n';
2294 psz_spaces[i] = '\0';
2297 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2299 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2300 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2301 psz_ket, psz_spaces );
2305 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2306 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2309 psz_spaces[i] = ' ';
2311 /* We wrap the rest of the output */
2312 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2313 b_description = config_GetInt( p_this, "help-verbose" );
2316 psz_text = psz_buffer;
2319 char *psz_parser, *psz_word;
2320 size_t i_end = strlen( psz_text );
2322 /* If the remaining text fits in a line, print it. */
2323 if( i_end <= (size_t)i_width )
2325 fprintf( stdout, "%s\n", psz_text );
2329 /* Otherwise, eat as many words as possible */
2330 psz_parser = psz_text;
2333 psz_word = psz_parser;
2334 psz_parser = strchr( psz_word, ' ' );
2335 /* If no space was found, we reached the end of the text
2336 * block; otherwise, we skip the space we just found. */
2337 psz_parser = psz_parser ? psz_parser + 1
2340 } while( psz_parser - psz_text <= i_width );
2342 /* We cut a word in one of these cases:
2343 * - it's the only word in the line and it's too long.
2344 * - we used less than 80% of the width and the word we are
2345 * going to wrap is longer than 40% of the width, and even
2346 * if the word would have fit in the next line. */
2347 if( psz_word == psz_text
2348 || ( psz_word - psz_text < 80 * i_width / 100
2349 && psz_parser - psz_word > 40 * i_width / 100 ) )
2351 char c = psz_text[i_width];
2352 psz_text[i_width] = '\0';
2353 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2354 psz_text += i_width;
2359 psz_word[-1] = '\0';
2360 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2361 psz_text = psz_word;
2365 if( b_description && p_item->psz_longtext )
2367 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2368 b_description = VLC_FALSE;
2369 psz_spaces = psz_spaces_longtext;
2370 fprintf( stdout, "%s", psz_spaces );
2376 /* Release the module list */
2377 vlc_list_release( p_list );
2380 /*****************************************************************************
2381 * ListModules: list the available modules with their description
2382 *****************************************************************************
2383 * Print a list of all available modules (builtins and plugins) and a short
2384 * description for each one.
2385 *****************************************************************************/
2386 static void ListModules( vlc_t *p_this )
2390 char psz_spaces[22];
2393 memset( psz_spaces, ' ', 22 );
2399 /* List all modules */
2400 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2402 /* Enumerate each module */
2403 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2407 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2409 /* Nasty hack, but right now I'm too tired to think about a nice
2411 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2415 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2416 psz_spaces, p_parser->psz_longname );
2418 psz_spaces[i] = ' ';
2421 vlc_list_release( p_list );
2423 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2428 /*****************************************************************************
2429 * Version: print complete program version
2430 *****************************************************************************
2431 * Print complete program version and build number.
2432 *****************************************************************************/
2433 static void Version( void )
2439 fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
2440 fprintf( stdout, _("Compiled by %s@%s.%s\n"),
2441 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
2442 fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
2443 if( strcmp( VLC_Changeset(), "exported" ) )
2444 fprintf( stdout, _("Based upon svn changeset [%s]\n"),
2446 fprintf( stdout, LICENSE_MSG );
2448 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2453 /*****************************************************************************
2454 * ShowConsole: On Win32, create an output console for debug messages
2455 *****************************************************************************
2456 * This function is useful only on Win32.
2457 *****************************************************************************/
2459 static void ShowConsole( void )
2464 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2468 freopen( "CONOUT$", "w", stderr );
2469 freopen( "CONIN$", "r", stdin );
2471 if( (f_help = fopen( "vlc-help.txt", "wt" )) )
2474 freopen( "vlc-help.txt", "wt", stdout );
2475 fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
2478 else freopen( "CONOUT$", "w", stdout );
2484 /*****************************************************************************
2485 * PauseConsole: On Win32, wait for a key press before closing the console
2486 *****************************************************************************
2487 * This function is useful only on Win32.
2488 *****************************************************************************/
2490 static void PauseConsole( void )
2494 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2496 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2504 /*****************************************************************************
2505 * ConsoleWidth: Return the console width in characters
2506 *****************************************************************************
2507 * We use the stty shell command to get the console width; if this fails or
2508 * if the width is less than 80, we default to 80.
2509 *****************************************************************************/
2510 static int ConsoleWidth( void )
2515 char buf[20], *psz_parser;
2519 file = popen( "stty size 2>/dev/null", "r" );
2522 i_ret = fread( buf, 1, 20, file );
2526 psz_parser = strchr( buf, ' ' );
2529 i_ret = atoi( psz_parser + 1 );
2544 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2545 vlc_value_t old_val, vlc_value_t new_val, void *param)
2547 vlc_t *p_vlc = (vlc_t *)p_this;
2549 if( new_val.i_int >= -1 )
2551 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2556 /*****************************************************************************
2557 * InitDeviceValues: initialize device values
2558 *****************************************************************************
2559 * This function inits the dvd, vcd and cd-audio values
2560 *****************************************************************************/
2561 static void InitDeviceValues( vlc_t *p_vlc )
2564 LibHalContext * ctx;
2569 DBusConnection *p_connection;
2573 ctx = libhal_ctx_new();
2575 dbus_error_init( &error );
2576 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2577 if( dbus_error_is_set( &error ) )
2579 dbus_error_free( &error );
2582 libhal_ctx_set_dbus_connection( ctx, p_connection );
2583 if( libhal_ctx_init( ctx, &error ) )
2585 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2589 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2591 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2594 for( i = 0; i < i_devices; i++ )
2597 if( !libhal_device_property_exists( ctx, devices[i],
2598 "storage.cdrom.dvd", NULL ) )
2600 if( !hal_device_property_exists( ctx, devices[ i ],
2601 "storage.cdrom.dvd" ) )
2607 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2608 "storage.cdrom.dvd", NULL );
2609 block_dev = libhal_device_get_property_string( ctx,
2610 devices[ i ], "block.device" , NULL );
2612 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2613 "storage.cdrom.dvd" );
2614 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2619 config_PutPsz( p_vlc, "dvd", block_dev );
2622 config_PutPsz( p_vlc, "vcd", block_dev );
2623 config_PutPsz( p_vlc, "cd-audio", block_dev );
2625 libhal_free_string( block_dev );
2627 hal_free_string( block_dev );
2631 libhal_free_string_array( devices );
2633 hal_free_string_array( devices );
2638 libhal_ctx_shutdown( ctx, NULL );
2640 hal_shutdown( ctx );
2645 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2650 /*****************************************************************************
2651 * FromLocale: converts a locale string to UTF-8
2652 *****************************************************************************/
2653 char *FromLocale( const char *locale )
2655 if( locale == NULL )
2658 if( libvlc.from_locale != (vlc_iconv_t)(-1) )
2660 char *iptr = (char *)locale, *output, *optr;
2664 * We are not allowed to modify the locale pointer, even if we cast it
2667 inb = strlen( locale );
2670 /* FIXME: I'm not sure about the value for the multiplication
2671 * (for western people, multiplication by 3 (Latin9) is sufficient) */
2672 optr = output = calloc( outb , 1);
2674 vlc_mutex_lock( &libvlc.from_locale_lock );
2675 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2677 while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
2683 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2685 vlc_mutex_unlock( &libvlc.from_locale_lock );
2687 return realloc( output, strlen( output ) + 1 );
2689 return (char *)locale;
2692 /*****************************************************************************
2693 * ToLocale: converts an UTF-8 string to locale
2694 *****************************************************************************/
2695 char *ToLocale( const char *utf8 )
2700 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
2702 char *iptr = (char *)utf8, *output, *optr;
2706 * We are not allowed to modify the locale pointer, even if we cast it
2709 inb = strlen( utf8 );
2710 /* FIXME: I'm not sure about the value for the multiplication
2711 * (for western people, multiplication is not needed) */
2714 optr = output = calloc( outb, 1 );
2715 vlc_mutex_lock( &libvlc.to_locale_lock );
2716 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2718 while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
2721 *optr = '?'; /* should not happen, and yes, it sucks */
2724 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2726 vlc_mutex_unlock( &libvlc.to_locale_lock );
2728 return realloc( output, strlen( output ) + 1 );
2730 return (char *)utf8;
2733 void LocaleFree( const char *str )
2735 if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
2736 free( (char *)str );