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_Error: strerror() equivalent
143 *****************************************************************************
144 * This function returns full version string (numeric version and codename).
145 *****************************************************************************/
146 char const * VLC_Error( int i_err )
148 return vlc_error( i_err );
151 /*****************************************************************************
152 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
153 *****************************************************************************
154 * This function allocates a vlc_t structure and returns a negative value
155 * in case of failure. Also, the thread system is initialized.
156 *****************************************************************************/
157 int VLC_Create( void )
160 vlc_t * p_vlc = NULL;
163 /* &libvlc never changes, so we can safely call this multiple times. */
166 /* vlc_threads_init *must* be the first internal call! No other call is
167 * allowed before the thread system has been initialized. */
168 i_ret = vlc_threads_init( p_libvlc );
174 /* Now that the thread system is initialized, we don't have much, but
175 * at least we have var_Create */
176 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
177 var_Get( p_libvlc, "libvlc", &lockval );
178 vlc_mutex_lock( lockval.p_address );
179 if( !libvlc.b_ready )
183 /* Guess what CPU we have */
184 libvlc.i_cpu = CPUCapabilities();
186 /* Find verbosity from VLC_VERBOSE environment variable */
187 psz_env = getenv( "VLC_VERBOSE" );
188 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
190 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
191 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
193 libvlc.b_color = VLC_FALSE;
196 /* Initialize message queue */
197 msg_Create( p_libvlc );
199 /* Announce who we are */
200 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
201 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
203 /* The module bank will be initialized later */
204 libvlc.p_module_bank = NULL;
206 libvlc.b_ready = VLC_TRUE;
208 /* UTF-8 convertor are initialized after the locale */
209 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
211 vlc_mutex_unlock( lockval.p_address );
212 var_Destroy( p_libvlc, "libvlc" );
214 /* Allocate a vlc object */
215 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
220 p_vlc->thread_id = 0;
222 p_vlc->psz_object_name = "root";
224 /* Initialize mutexes */
225 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
227 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
228 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
231 /* Store our newly allocated structure in the global list */
232 vlc_object_attach( p_vlc, p_libvlc );
234 /* Store data for the non-reentrant API */
235 p_static_vlc = p_vlc;
237 return p_vlc->i_object_id;
240 /*****************************************************************************
241 * VLC_Init: initialize a vlc_t structure.
242 *****************************************************************************
243 * This function initializes a previously allocated vlc_t structure:
245 * - gettext initialization
246 * - message queue, module bank and playlist initialization
247 * - configuration and commandline parsing
248 *****************************************************************************/
249 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
251 char p_capabilities[200];
256 vlc_bool_t b_exit = VLC_FALSE;
257 vlc_t * p_vlc = vlc_current_object( i_object );
258 module_t *p_help_module;
259 playlist_t *p_playlist;
261 #if defined( ENABLE_NLS ) \
262 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
272 * System specific initialization code
274 system_Init( p_vlc, &i_argc, ppsz_argv );
276 /* Get the executable name (similar to the basename command) */
279 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
282 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
288 p_vlc->psz_object_name = "vlc";
292 * Support for gettext
297 * Global iconv, must be done after setlocale()
298 * so that vlc_current_charset() works.
302 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
303 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
305 /* Initialize the module bank and load the configuration of the
306 * main module. We need to do this at this stage to be able to display
307 * a short help if required by the user. (short help == main module
309 module_InitBank( p_vlc );
311 /* Hack: insert the help module here */
312 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
313 if( p_help_module == NULL )
315 module_EndBank( p_vlc );
316 if( i_object ) vlc_object_release( p_vlc );
319 p_help_module->psz_object_name = "help";
320 p_help_module->psz_longname = N_("Help options");
321 config_Duplicate( p_help_module, p_help_config );
322 vlc_object_attach( p_help_module, libvlc.p_module_bank );
325 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
327 vlc_object_detach( p_help_module );
328 config_Free( p_help_module );
329 vlc_object_destroy( p_help_module );
330 module_EndBank( p_vlc );
331 if( i_object ) vlc_object_release( p_vlc );
335 /* Check for short help option */
336 if( config_GetInt( p_vlc, "help" ) )
338 Help( p_vlc, "help" );
341 /* Check for version option */
342 else if( config_GetInt( p_vlc, "version" ) )
348 /* Set the config file stuff */
349 p_vlc->psz_homedir = config_GetHomeDir();
350 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
352 /* Check for plugins cache options */
353 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
355 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
358 /* Hack: remove the help module here */
359 vlc_object_detach( p_help_module );
362 /* Will be re-done properly later on */
363 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
365 /* Check for daemon mode */
367 if( config_GetInt( p_vlc, "daemon" ) )
370 if( daemon( 0, 0) != 0 )
372 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
376 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
381 if( ( i_pid = fork() ) < 0 )
383 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
388 /* This is the parent, exit right now */
389 msg_Dbg( p_vlc, "closing parent process" );
394 /* We are the child */
395 msg_Dbg( p_vlc, "daemon spawned" );
396 close( STDIN_FILENO );
397 close( STDOUT_FILENO );
398 close( STDERR_FILENO );
400 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
408 config_Free( p_help_module );
409 vlc_object_destroy( p_help_module );
410 module_EndBank( p_vlc );
411 if( i_object ) vlc_object_release( p_vlc );
415 /* Check for translation config option */
416 #if defined( ENABLE_NLS ) \
417 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
419 /* This ain't really nice to have to reload the config here but it seems
420 * the only way to do it. */
421 config_LoadConfigFile( p_vlc, "main" );
422 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
424 /* Check if the user specified a custom language */
425 psz_language = config_GetPsz( p_vlc, "language" );
426 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
428 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
430 /* Reset the default domain */
431 SetLanguage( psz_language );
435 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
436 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
438 textdomain( PACKAGE_NAME );
440 #if defined( ENABLE_UTF8 )
441 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
444 module_EndBank( p_vlc );
445 module_InitBank( p_vlc );
446 config_LoadConfigFile( p_vlc, "main" );
447 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
448 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
450 if( psz_language ) free( psz_language );
454 * Load the builtins and plugins into the module_bank.
455 * We have to do it before config_Load*() because this also gets the
456 * list of configuration options exported by each module and loads their
459 module_LoadBuiltins( p_vlc );
460 module_LoadPlugins( p_vlc );
466 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
467 libvlc.p_module_bank->i_children );
469 /* Hack: insert the help module here */
470 vlc_object_attach( p_help_module, libvlc.p_module_bank );
473 /* Check for help on modules */
474 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
476 Help( p_vlc, p_tmp );
480 /* Check for long help option */
481 else if( config_GetInt( p_vlc, "longhelp" ) )
483 Help( p_vlc, "longhelp" );
486 /* Check for module list option */
487 else if( config_GetInt( p_vlc, "list" ) )
489 ListModules( p_vlc );
493 /* Check for config file options */
494 if( config_GetInt( p_vlc, "reset-config" ) )
496 vlc_object_detach( p_help_module );
497 config_ResetAll( p_vlc );
498 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
499 config_SaveConfigFile( p_vlc, NULL );
500 vlc_object_attach( p_help_module, libvlc.p_module_bank );
502 if( config_GetInt( p_vlc, "save-config" ) )
504 vlc_object_detach( p_help_module );
505 config_LoadConfigFile( p_vlc, NULL );
506 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
507 config_SaveConfigFile( p_vlc, NULL );
508 vlc_object_attach( p_help_module, libvlc.p_module_bank );
511 /* Hack: remove the help module here */
512 vlc_object_detach( p_help_module );
517 config_Free( p_help_module );
518 vlc_object_destroy( p_help_module );
519 module_EndBank( p_vlc );
520 if( i_object ) vlc_object_release( p_vlc );
527 InitDeviceValues( p_vlc );
530 * Override default configuration with config file settings
532 /* (dionoea 13/08/2005) : we need to load locale here in order to have
533 * coherent float encoding with subsequent SaveConfigFile calls */
534 setlocale( LC_NUMERIC, "" );
535 config_LoadConfigFile( p_vlc, NULL );
537 /* Hack: insert the help module here */
538 vlc_object_attach( p_help_module, libvlc.p_module_bank );
542 * Override configuration with command line settings
544 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
548 /* Pause the console because it's destroyed when we exit */
549 fprintf( stderr, "The command line options couldn't be loaded, check "
550 "that they are valid.\n" );
553 vlc_object_detach( p_help_module );
554 config_Free( p_help_module );
555 vlc_object_destroy( p_help_module );
556 module_EndBank( p_vlc );
557 if( i_object ) vlc_object_release( p_vlc );
561 /* Hack: remove the help module here */
562 vlc_object_detach( p_help_module );
563 config_Free( p_help_module );
564 vlc_object_destroy( p_help_module );
568 * System specific configuration
570 system_Configure( p_vlc, &i_argc, ppsz_argv );
573 * Message queue options
576 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
577 if( config_GetInt( p_vlc, "quiet" ) )
580 var_Set( p_vlc, "verbose", val );
582 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
583 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
585 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
588 * Output messages that may still be in the queue
592 /* p_vlc initialization. FIXME ? */
594 if( !config_GetInt( p_vlc, "fpu" ) )
595 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
597 #if defined( __i386__ ) || defined( __x86_64__ )
598 if( !config_GetInt( p_vlc, "mmx" ) )
599 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
600 if( !config_GetInt( p_vlc, "3dn" ) )
601 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
602 if( !config_GetInt( p_vlc, "mmxext" ) )
603 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
604 if( !config_GetInt( p_vlc, "sse" ) )
605 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
606 if( !config_GetInt( p_vlc, "sse2" ) )
607 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
609 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
610 if( !config_GetInt( p_vlc, "altivec" ) )
611 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
614 #define PRINT_CAPABILITY( capability, string ) \
615 if( libvlc.i_cpu & capability ) \
617 strncat( p_capabilities, string " ", \
618 sizeof(p_capabilities) - strlen(p_capabilities) ); \
619 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
622 p_capabilities[0] = '\0';
623 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
624 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
625 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
626 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
627 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
628 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
629 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
630 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
631 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
632 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
633 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
636 * Choose the best memcpy module
638 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
640 if( p_vlc->pf_memcpy == NULL )
642 p_vlc->pf_memcpy = memcpy;
645 if( p_vlc->pf_memset == NULL )
647 p_vlc->pf_memset = memset;
651 * Initialize hotkey handling
653 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
654 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
655 /* Do a copy (we don't need to modify the strings) */
656 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
659 * Initialize playlist and get commandline files
661 p_playlist = playlist_Create( p_vlc );
664 msg_Err( p_vlc, "playlist initialization failed" );
665 if( p_vlc->p_memcpy_module != NULL )
667 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
669 module_EndBank( p_vlc );
670 if( i_object ) vlc_object_release( p_vlc );
674 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
675 if( psz_modules && *psz_modules )
677 /* Add service discovery modules */
678 playlist_AddSDModules( p_playlist, psz_modules );
680 if( psz_modules ) free( psz_modules );
683 * Load background interfaces
685 psz_modules = config_GetPsz( p_vlc, "extraintf" );
686 psz_control = config_GetPsz( p_vlc, "control" );
688 if( psz_modules && *psz_modules && psz_control && *psz_control )
690 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
691 strlen( psz_control ) + 1 );
692 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
694 else if( psz_control && *psz_control )
696 if( psz_modules ) free( psz_modules );
697 psz_modules = strdup( psz_control );
700 psz_parser = psz_modules;
701 while ( psz_parser && *psz_parser )
703 char *psz_module, *psz_temp;
704 psz_module = psz_parser;
705 psz_parser = strchr( psz_module, ':' );
711 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
714 sprintf( psz_temp, "%s,none", psz_module );
715 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
725 * Allways load the hotkeys interface if it exists
727 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
730 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
732 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
733 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
734 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
735 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
736 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
737 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
738 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
739 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
740 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
741 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
742 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
743 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
746 * Get input filenames given as commandline arguments
748 GetFilenames( p_vlc, i_argc, ppsz_argv );
751 * Get --open argument
753 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
754 var_Get( p_vlc, "open", &val );
755 if ( val.psz_string != NULL && *val.psz_string )
757 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
758 PLAYLIST_INSERT, 0 );
760 if ( val.psz_string != NULL ) free( val.psz_string );
762 if( i_object ) vlc_object_release( p_vlc );
766 /*****************************************************************************
767 * VLC_AddIntf: add an interface
768 *****************************************************************************
769 * This function opens an interface plugin and runs it. If b_block is set
770 * to 0, VLC_AddIntf will return immediately and let the interface run in a
771 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
772 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
773 * the playlist when it is completely initialised.
774 *****************************************************************************/
775 int VLC_AddIntf( int i_object, char const *psz_module,
776 vlc_bool_t b_block, vlc_bool_t b_play )
779 intf_thread_t *p_intf;
780 vlc_t *p_vlc = vlc_current_object( i_object );
788 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
791 * We prefer the dummy interface if none is specified. */
792 char *psz_interface = config_GetPsz( p_vlc, "intf" );
793 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
794 if( psz_interface ) free( psz_interface );
798 /* Try to create the interface */
799 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
803 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
804 if( i_object ) vlc_object_release( p_vlc );
808 /* Interface doesn't handle play on start so do it ourselves */
809 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
811 /* Try to run the interface */
812 p_intf->b_play = b_play;
813 p_intf->b_block = b_block;
814 i_err = intf_RunThread( p_intf );
817 vlc_object_detach( p_intf );
818 intf_Destroy( p_intf );
819 if( i_object ) vlc_object_release( p_vlc );
823 if( i_object ) vlc_object_release( p_vlc );
827 /*****************************************************************************
828 * VLC_Die: ask vlc to die.
829 *****************************************************************************
830 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
831 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
832 *****************************************************************************/
833 int VLC_Die( int i_object )
835 vlc_t *p_vlc = vlc_current_object( i_object );
842 p_vlc->b_die = VLC_TRUE;
844 if( i_object ) vlc_object_release( p_vlc );
848 /*****************************************************************************
849 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
850 *****************************************************************************/
851 int VLC_CleanUp( int i_object )
853 intf_thread_t * p_intf;
854 playlist_t * p_playlist;
855 vout_thread_t * p_vout;
856 aout_instance_t * p_aout;
857 announce_handler_t * p_announce;
858 vlc_t *p_vlc = vlc_current_object( i_object );
860 /* Check that the handle is valid */
867 * Ask the interfaces to stop and destroy them
869 msg_Dbg( p_vlc, "removing all interfaces" );
870 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
872 intf_StopThread( p_intf );
873 vlc_object_detach( p_intf );
874 vlc_object_release( p_intf );
875 intf_Destroy( p_intf );
881 msg_Dbg( p_vlc, "removing all playlists" );
882 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
885 vlc_object_detach( p_playlist );
886 vlc_object_release( p_playlist );
887 playlist_Destroy( p_playlist );
893 msg_Dbg( p_vlc, "removing all video outputs" );
894 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
896 vlc_object_detach( p_vout );
897 vlc_object_release( p_vout );
898 vout_Destroy( p_vout );
904 msg_Dbg( p_vlc, "removing all audio outputs" );
905 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
907 vlc_object_detach( (vlc_object_t *)p_aout );
908 vlc_object_release( (vlc_object_t *)p_aout );
909 aout_Delete( p_aout );
913 * Free announce handler(s?)
915 msg_Dbg( p_vlc, "removing announce handler" );
916 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
919 vlc_object_detach( p_announce );
920 vlc_object_release( p_announce );
921 announce_HandlerDestroy( p_announce );
924 if( i_object ) vlc_object_release( p_vlc );
928 /*****************************************************************************
929 * VLC_Destroy: Destroy everything.
930 *****************************************************************************
931 * This function requests the running threads to finish, waits for their
932 * termination, and destroys their structure.
933 *****************************************************************************/
934 int VLC_Destroy( int i_object )
936 vlc_t *p_vlc = vlc_current_object( i_object );
944 * Free allocated memory
946 if( p_vlc->p_memcpy_module )
948 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
949 p_vlc->p_memcpy_module = NULL;
955 module_EndBank( p_vlc );
957 if( p_vlc->psz_homedir )
959 free( p_vlc->psz_homedir );
960 p_vlc->psz_homedir = NULL;
963 if( p_vlc->psz_configfile )
965 free( p_vlc->psz_configfile );
966 p_vlc->psz_configfile = NULL;
969 if( p_vlc->p_hotkeys )
971 free( p_vlc->p_hotkeys );
972 p_vlc->p_hotkeys = NULL;
976 * System specific cleaning code
981 * Free message queue.
982 * Nobody shall use msg_* afterward.
985 msg_Destroy( p_libvlc );
987 /* Destroy global iconv */
990 /* Destroy mutexes */
991 vlc_mutex_destroy( &p_vlc->config_lock );
993 vlc_object_detach( p_vlc );
995 /* Release object before destroying it */
996 if( i_object ) vlc_object_release( p_vlc );
998 vlc_object_destroy( p_vlc );
1000 /* Stop thread system: last one out please shut the door! */
1001 vlc_threads_end( p_libvlc );
1006 /*****************************************************************************
1007 * VLC_VariableSet: set a vlc variable
1008 *****************************************************************************/
1009 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
1011 vlc_t *p_vlc = vlc_current_object( i_object );
1019 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1020 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1021 if( !strncmp( psz_var, "conf::", 6 ) )
1023 module_config_t *p_item;
1024 char const *psz_newvar = psz_var + 6;
1026 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1030 switch( p_item->i_type )
1032 case CONFIG_ITEM_BOOL:
1033 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1035 case CONFIG_ITEM_INTEGER:
1036 config_PutInt( p_vlc, psz_newvar, value.i_int );
1038 case CONFIG_ITEM_FLOAT:
1039 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1042 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1045 if( i_object ) vlc_object_release( p_vlc );
1050 i_ret = var_Set( p_vlc, psz_var, value );
1052 if( i_object ) vlc_object_release( p_vlc );
1056 /*****************************************************************************
1057 * VLC_VariableGet: get a vlc variable
1058 *****************************************************************************/
1059 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1061 vlc_t *p_vlc = vlc_current_object( i_object );
1069 i_ret = var_Get( p_vlc , psz_var, p_value );
1071 if( i_object ) vlc_object_release( p_vlc );
1075 /*****************************************************************************
1076 * VLC_VariableType: get a vlc variable type
1077 *****************************************************************************/
1078 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1081 vlc_t *p_vlc = vlc_current_object( i_object );
1088 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1089 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1090 if( !strncmp( psz_var, "conf::", 6 ) )
1092 module_config_t *p_item;
1093 char const *psz_newvar = psz_var + 6;
1095 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1099 switch( p_item->i_type )
1101 case CONFIG_ITEM_BOOL:
1102 i_type = VLC_VAR_BOOL;
1104 case CONFIG_ITEM_INTEGER:
1105 i_type = VLC_VAR_INTEGER;
1107 case CONFIG_ITEM_FLOAT:
1108 i_type = VLC_VAR_FLOAT;
1111 i_type = VLC_VAR_STRING;
1119 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1121 if( i_object ) vlc_object_release( p_vlc );
1131 /*****************************************************************************
1132 * VLC_AddTarget: adds a target for playing.
1133 *****************************************************************************
1134 * This function adds psz_target to the current playlist. If a playlist does
1135 * not exist, it will create one.
1136 *****************************************************************************/
1137 int VLC_AddTarget( int i_object, char const *psz_target,
1138 char const **ppsz_options, int i_options,
1139 int i_mode, int i_pos )
1142 playlist_t *p_playlist;
1143 vlc_t *p_vlc = vlc_current_object( i_object );
1150 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1152 if( p_playlist == NULL )
1154 msg_Dbg( p_vlc, "no playlist present, creating one" );
1155 p_playlist = playlist_Create( p_vlc );
1157 if( p_playlist == NULL )
1159 if( i_object ) vlc_object_release( p_vlc );
1160 return VLC_EGENERIC;
1163 vlc_object_yield( p_playlist );
1166 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1167 i_mode, i_pos, -1, ppsz_options, i_options);
1169 vlc_object_release( p_playlist );
1171 if( i_object ) vlc_object_release( p_vlc );
1175 /*****************************************************************************
1177 *****************************************************************************/
1178 int VLC_Play( int i_object )
1180 playlist_t * p_playlist;
1181 vlc_t *p_vlc = vlc_current_object( i_object );
1183 /* Check that the handle is valid */
1189 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1193 if( i_object ) vlc_object_release( p_vlc );
1197 playlist_Play( p_playlist );
1198 vlc_object_release( p_playlist );
1200 if( i_object ) vlc_object_release( p_vlc );
1204 /*****************************************************************************
1205 * VLC_Pause: toggle pause
1206 *****************************************************************************/
1207 int VLC_Pause( int i_object )
1209 playlist_t * p_playlist;
1210 vlc_t *p_vlc = vlc_current_object( i_object );
1212 /* Check that the handle is valid */
1218 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1222 if( i_object ) vlc_object_release( p_vlc );
1226 playlist_Pause( p_playlist );
1227 vlc_object_release( p_playlist );
1229 if( i_object ) vlc_object_release( p_vlc );
1233 /*****************************************************************************
1234 * VLC_Pause: toggle pause
1235 *****************************************************************************/
1236 int VLC_Stop( int i_object )
1238 playlist_t * p_playlist;
1239 vlc_t *p_vlc = vlc_current_object( i_object );
1241 /* Check that the handle is valid */
1247 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1251 if( i_object ) vlc_object_release( p_vlc );
1255 playlist_Stop( p_playlist );
1256 vlc_object_release( p_playlist );
1258 if( i_object ) vlc_object_release( p_vlc );
1262 /*****************************************************************************
1263 * VLC_IsPlaying: Query for Playlist Status
1264 *****************************************************************************/
1265 vlc_bool_t VLC_IsPlaying( int i_object )
1267 playlist_t * p_playlist;
1268 vlc_bool_t b_playing;
1271 vlc_t *p_vlc = vlc_current_object( i_object );
1273 /* Check that the handle is valid */
1279 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1283 if( i_object ) vlc_object_release( p_vlc );
1286 if( !p_playlist->p_input )
1288 if( i_object ) vlc_object_release( p_vlc );
1291 var_Get( p_playlist->p_input, "state", &val );
1292 b_playing = ( val.i_int == PLAYING_S );
1293 vlc_object_release( p_playlist );
1295 if( i_object ) vlc_object_release( p_vlc );
1300 * Get the current position in a input
1302 * Return the current position as a float
1303 * \note For some inputs, this will be unknown.
1305 * \param i_object a vlc object id
1306 * \return a float in the range of 0.0 - 1.0
1308 float VLC_PositionGet( int i_object )
1310 input_thread_t *p_input;
1312 vlc_t *p_vlc = vlc_current_object( i_object );
1314 /* Check that the handle is valid */
1320 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1324 if( i_object ) vlc_object_release( p_vlc );
1328 var_Get( p_input, "position", &val );
1329 vlc_object_release( p_input );
1331 if( i_object ) vlc_object_release( p_vlc );
1336 * Set the current position in a input
1338 * Set the current position in a input and then return
1339 * the current position as a float.
1340 * \note For some inputs, this will be unknown.
1342 * \param i_object a vlc object id
1343 * \param i_position a float in the range of 0.0 - 1.0
1344 * \return a float in the range of 0.0 - 1.0
1346 float VLC_PositionSet( int i_object, float i_position )
1348 input_thread_t *p_input;
1350 vlc_t *p_vlc = vlc_current_object( i_object );
1352 /* Check that the handle is valid */
1358 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1362 if( i_object ) vlc_object_release( p_vlc );
1366 val.f_float = i_position;
1367 var_Set( p_input, "position", val );
1368 var_Get( p_input, "position", &val );
1369 vlc_object_release( p_input );
1371 if( i_object ) vlc_object_release( p_vlc );
1376 * Get the current position in a input
1378 * Return the current position in seconds from the start.
1379 * \note For some inputs, this will be unknown.
1381 * \param i_object a vlc object id
1382 * \return the offset from 0:00 in seconds
1384 int VLC_TimeGet( int i_object )
1386 input_thread_t *p_input;
1388 vlc_t *p_vlc = vlc_current_object( i_object );
1390 /* Check that the handle is valid */
1396 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1400 if( i_object ) vlc_object_release( p_vlc );
1404 var_Get( p_input, "time", &val );
1405 vlc_object_release( p_input );
1407 if( i_object ) vlc_object_release( p_vlc );
1408 return val.i_time / 1000000;
1412 * Seek to a position in the current input
1414 * Seek i_seconds in the current input. If b_relative is set,
1415 * then the seek will be relative to the current position, otherwise
1416 * it will seek to i_seconds from the beginning of the input.
1417 * \note For some inputs, this will be unknown.
1419 * \param i_object a vlc object id
1420 * \param i_seconds seconds from current position or from beginning of input
1421 * \param b_relative seek relative from current position
1422 * \return VLC_SUCCESS on success
1424 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1426 input_thread_t *p_input;
1428 vlc_t *p_vlc = vlc_current_object( i_object );
1430 /* Check that the handle is valid */
1436 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1440 if( i_object ) vlc_object_release( p_vlc );
1446 val.i_time = i_seconds;
1447 val.i_time = val.i_time * 1000000L;
1448 var_Set( p_input, "time-offset", val );
1452 val.i_time = i_seconds;
1453 val.i_time = val.i_time * 1000000L;
1454 var_Set( p_input, "time", val );
1456 vlc_object_release( p_input );
1458 if( i_object ) vlc_object_release( p_vlc );
1463 * Get the total length of a input
1465 * Return the total length in seconds from the current input.
1466 * \note For some inputs, this will be unknown.
1468 * \param i_object a vlc object id
1469 * \return the length in seconds
1471 int VLC_LengthGet( int i_object )
1473 input_thread_t *p_input;
1475 vlc_t *p_vlc = vlc_current_object( i_object );
1477 /* Check that the handle is valid */
1483 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1487 if( i_object ) vlc_object_release( p_vlc );
1491 var_Get( p_input, "length", &val );
1492 vlc_object_release( p_input );
1494 if( i_object ) vlc_object_release( p_vlc );
1495 return val.i_time / 1000000L;
1499 * Play the input faster than realtime
1501 * 2x, 4x, 8x faster than realtime
1502 * \note For some inputs, this will be impossible.
1504 * \param i_object a vlc object id
1505 * \return the current speedrate
1507 float VLC_SpeedFaster( int i_object )
1509 input_thread_t *p_input;
1511 vlc_t *p_vlc = vlc_current_object( i_object );
1513 /* Check that the handle is valid */
1519 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1523 if( i_object ) vlc_object_release( p_vlc );
1527 val.b_bool = VLC_TRUE;
1528 var_Set( p_input, "rate-faster", val );
1529 var_Get( p_input, "rate", &val );
1530 vlc_object_release( p_input );
1532 if( i_object ) vlc_object_release( p_vlc );
1533 return val.f_float / INPUT_RATE_DEFAULT;
1537 * Play the input slower than realtime
1539 * 1/2x, 1/4x, 1/8x slower than realtime
1540 * \note For some inputs, this will be impossible.
1542 * \param i_object a vlc object id
1543 * \return the current speedrate
1545 float VLC_SpeedSlower( int i_object )
1547 input_thread_t *p_input;
1549 vlc_t *p_vlc = vlc_current_object( i_object );
1551 /* Check that the handle is valid */
1557 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1561 if( i_object ) vlc_object_release( p_vlc );
1565 val.b_bool = VLC_TRUE;
1566 var_Set( p_input, "rate-slower", val );
1567 var_Get( p_input, "rate", &val );
1568 vlc_object_release( p_input );
1570 if( i_object ) vlc_object_release( p_vlc );
1571 return val.f_float / INPUT_RATE_DEFAULT;
1575 * Return the current playlist item
1577 * Returns the index of the playlistitem that is currently selected for play.
1578 * This is valid even if nothing is currently playing.
1580 * \param i_object a vlc object id
1581 * \return the current index
1583 int VLC_PlaylistIndex( int i_object )
1586 playlist_t * p_playlist;
1587 vlc_t *p_vlc = vlc_current_object( i_object );
1589 /* Check that the handle is valid */
1595 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1599 if( i_object ) vlc_object_release( p_vlc );
1603 i_index = p_playlist->i_index;
1604 vlc_object_release( p_playlist );
1606 if( i_object ) vlc_object_release( p_vlc );
1611 * Total amount of items in the playlist
1613 * \param i_object a vlc object id
1614 * \return amount of playlist items
1616 int VLC_PlaylistNumberOfItems( int i_object )
1619 playlist_t * p_playlist;
1620 vlc_t *p_vlc = vlc_current_object( i_object );
1622 /* Check that the handle is valid */
1628 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1632 if( i_object ) vlc_object_release( p_vlc );
1636 i_size = p_playlist->i_size;
1637 vlc_object_release( p_playlist );
1639 if( i_object ) vlc_object_release( p_vlc );
1644 * Next playlist item
1646 * Skip to the next playlistitem and play it.
1648 * \param i_object a vlc object id
1649 * \return VLC_SUCCESS on success
1651 int VLC_PlaylistNext( 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 playlist_Next( p_playlist );
1671 vlc_object_release( p_playlist );
1673 if( i_object ) vlc_object_release( p_vlc );
1678 * Previous playlist item
1680 * Skip to the previous playlistitem and play it.
1682 * \param i_object a vlc object id
1683 * \return VLC_SUCCESS on success
1685 int VLC_PlaylistPrev( int i_object )
1687 playlist_t * p_playlist;
1688 vlc_t *p_vlc = vlc_current_object( i_object );
1690 /* Check that the handle is valid */
1696 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1700 if( i_object ) vlc_object_release( p_vlc );
1704 playlist_Prev( p_playlist );
1705 vlc_object_release( p_playlist );
1707 if( i_object ) vlc_object_release( p_vlc );
1712 /*****************************************************************************
1713 * VLC_PlaylistClear: Empty the playlist
1714 *****************************************************************************/
1715 int VLC_PlaylistClear( int i_object )
1718 playlist_t * p_playlist;
1719 vlc_t *p_vlc = vlc_current_object( i_object );
1721 /* Check that the handle is valid */
1727 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1731 if( i_object ) vlc_object_release( p_vlc );
1735 i_err = playlist_Clear( p_playlist );
1737 vlc_object_release( p_playlist );
1739 if( i_object ) vlc_object_release( p_vlc );
1746 * \param i_object a vlc object id
1747 * \param i_volume something in a range from 0-200
1748 * \return the new volume (range 0-200 %)
1750 int VLC_VolumeSet( int i_object, int i_volume )
1752 audio_volume_t i_vol = 0;
1753 vlc_t *p_vlc = vlc_current_object( i_object );
1755 /* Check that the handle is valid */
1761 if( i_volume >= 0 && i_volume <= 200 )
1763 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1764 aout_VolumeSet( p_vlc, i_vol );
1767 if( i_object ) vlc_object_release( p_vlc );
1768 return i_vol * 200 / AOUT_VOLUME_MAX;
1772 * Get the current volume
1774 * Retrieve the current volume.
1776 * \param i_object a vlc object id
1777 * \return the current volume (range 0-200 %)
1779 int VLC_VolumeGet( int i_object )
1781 audio_volume_t i_volume;
1782 vlc_t *p_vlc = vlc_current_object( i_object );
1784 /* Check that the handle is valid */
1790 aout_VolumeGet( p_vlc, &i_volume );
1792 if( i_object ) vlc_object_release( p_vlc );
1793 return i_volume*200/AOUT_VOLUME_MAX;
1797 * Mute/Unmute the volume
1799 * \param i_object a vlc object id
1800 * \return VLC_SUCCESS on success
1802 int VLC_VolumeMute( int i_object )
1804 vlc_t *p_vlc = vlc_current_object( i_object );
1806 /* Check that the handle is valid */
1812 aout_VolumeMute( p_vlc, NULL );
1814 if( i_object ) vlc_object_release( p_vlc );
1818 /*****************************************************************************
1819 * VLC_FullScreen: toggle fullscreen mode
1820 *****************************************************************************/
1821 int VLC_FullScreen( int i_object )
1823 vout_thread_t *p_vout;
1824 vlc_t *p_vlc = vlc_current_object( i_object );
1831 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1835 if( i_object ) vlc_object_release( p_vlc );
1839 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1840 vlc_object_release( p_vout );
1842 if( i_object ) vlc_object_release( p_vlc );
1846 /* following functions are local */
1848 static void LocaleInit( void )
1852 if( !vlc_current_charset( &psz_charset ) )
1854 vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
1855 vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
1856 libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
1857 libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
1858 if( !libvlc.to_locale )
1860 // Not sure it is the right thing to do, but at least it
1861 // doesn't make vlc crash with msvc !
1862 libvlc.to_locale = (vlc_iconv_t)(-1);
1866 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
1867 free( psz_charset );
1870 static void LocaleDeinit( void )
1872 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
1874 vlc_mutex_destroy( &libvlc.from_locale_lock );
1875 vlc_mutex_destroy( &libvlc.to_locale_lock );
1876 vlc_iconv_close( libvlc.from_locale );
1877 vlc_iconv_close( libvlc.to_locale );
1881 /*****************************************************************************
1882 * SetLanguage: set the interface language.
1883 *****************************************************************************
1884 * We set the LC_MESSAGES locale category for interface messages and buttons,
1885 * as well as the LC_CTYPE category for string sorting and possible wide
1886 * character support.
1887 *****************************************************************************/
1888 static void SetLanguage ( char const *psz_lang )
1890 #if defined( ENABLE_NLS ) \
1891 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1894 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1898 if( psz_lang && !*psz_lang )
1900 # if defined( HAVE_LC_MESSAGES )
1901 setlocale( LC_MESSAGES, psz_lang );
1903 setlocale( LC_CTYPE, psz_lang );
1908 /* I need that under Darwin, please check it doesn't disturb
1909 * other platforms. --Meuuh */
1910 setenv( "LANG", psz_lang, 1 );
1912 #elif defined( SYS_BEOS ) || defined( WIN32 )
1913 /* We set LC_ALL manually because it is the only way to set
1914 * the language at runtime under eg. Windows. Beware that this
1915 * makes the environment unconsistent when libvlc is unloaded and
1916 * should probably be moved to a safer place like vlc.c. */
1917 static char psz_lcall[20];
1918 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1919 psz_lcall[19] = '\0';
1920 putenv( psz_lcall );
1923 setlocale( LC_ALL, psz_lang );
1926 /* Specify where to find the locales for current domain */
1927 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1928 psz_path = LOCALEDIR;
1930 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1934 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1936 fprintf( stderr, "warning: no domain %s in directory %s\n",
1937 PACKAGE_NAME, psz_path );
1940 /* Set the default domain */
1941 textdomain( PACKAGE_NAME );
1943 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1947 /*****************************************************************************
1948 * GetFilenames: parse command line options which are not flags
1949 *****************************************************************************
1950 * Parse command line for input files as well as their associated options.
1951 * An option always follows its associated input and begins with a ":".
1952 *****************************************************************************/
1953 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1955 int i_opt, i_options;
1957 /* We assume that the remaining parameters are filenames
1958 * and their input options */
1959 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1961 const char *psz_target;
1964 /* Count the input options */
1965 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1971 /* TODO: write an internal function of this one, to avoid
1972 * unnecessary lookups. */
1973 /* FIXME: should we convert options to UTF-8 as well ?? */
1974 psz_target = FromLocale( ppsz_argv[ i_opt ] );
1975 VLC_AddTarget( p_vlc->i_object_id, psz_target,
1976 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1978 PLAYLIST_INSERT, 0 );
1979 LocaleFree( psz_target );
1985 /*****************************************************************************
1986 * Help: print program help
1987 *****************************************************************************
1988 * Print a short inline help. Message interface is initialized at this stage.
1989 *****************************************************************************/
1990 static void Help( vlc_t *p_this, char const *psz_help_name )
1996 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1998 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1999 Usage( p_this, "help" );
2000 Usage( p_this, "main" );
2002 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2004 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2005 Usage( p_this, NULL );
2007 else if( psz_help_name )
2009 Usage( p_this, psz_help_name );
2012 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2017 /*****************************************************************************
2018 * Usage: print module usage
2019 *****************************************************************************
2020 * Print a short inline help. Message interface is initialized at this stage.
2021 *****************************************************************************/
2022 static void Usage( vlc_t *p_this, char const *psz_module_name )
2024 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
2025 /* short option ------' | | | | | | |
2026 * option name ------------' | | | | | |
2027 * <bra -------------------------' | | | | |
2028 * option type or "" --------------' | | | |
2029 * ket> -----------------------------' | | |
2030 * padding spaces ---------------------' | |
2031 * comment -------------------------------' |
2032 * comment suffix --------------------------'
2034 * The purpose of having bra and ket is that we might i18n them as well.
2036 #define LINE_START 8
2037 #define PADDING_SPACES 25
2040 module_config_t *p_item;
2041 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2042 char psz_spaces_longtext[LINE_START+3];
2043 char psz_format[sizeof(FORMAT_STRING)];
2044 char psz_buffer[10000];
2047 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2048 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2049 vlc_bool_t b_description;
2051 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2052 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2053 memset( psz_spaces_longtext, ' ', LINE_START+2 );
2054 psz_spaces_longtext[LINE_START+2] = '\0';
2056 strcpy( psz_format, FORMAT_STRING );
2058 /* List all modules */
2059 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2061 /* Enumerate the config for each module */
2062 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2064 vlc_bool_t b_help_module;
2066 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2068 if( psz_module_name && strcmp( psz_module_name,
2069 p_parser->psz_object_name ) )
2074 /* Ignore modules without config options */
2075 if( !p_parser->i_config_items )
2080 /* Ignore modules with only advanced config options if requested */
2083 for( p_item = p_parser->p_config;
2084 p_item->i_type != CONFIG_HINT_END;
2087 if( (p_item->i_type & CONFIG_ITEM) &&
2088 !p_item->b_advanced ) break;
2090 if( p_item->i_type == CONFIG_HINT_END ) continue;
2093 /* Print name of module */
2094 if( strcmp( "main", p_parser->psz_object_name ) )
2095 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2097 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2099 /* Print module options */
2100 for( p_item = p_parser->p_config;
2101 p_item->i_type != CONFIG_HINT_END;
2104 char *psz_text, *psz_spaces = psz_spaces_text;
2105 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2106 char *psz_suf = "", *psz_prefix = NULL;
2109 /* Skip deprecated options */
2110 if( p_item->psz_current )
2114 /* Skip advanced options if requested */
2115 if( p_item->b_advanced && !b_advanced )
2120 switch( p_item->i_type )
2122 case CONFIG_HINT_CATEGORY:
2123 case CONFIG_HINT_USAGE:
2124 if( !strcmp( "main", p_parser->psz_object_name ) )
2125 fprintf( stdout, "\n %s\n", p_item->psz_text );
2128 case CONFIG_ITEM_STRING:
2129 case CONFIG_ITEM_FILE:
2130 case CONFIG_ITEM_DIRECTORY:
2131 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2132 case CONFIG_ITEM_MODULE_CAT:
2133 case CONFIG_ITEM_MODULE_LIST:
2134 case CONFIG_ITEM_MODULE_LIST_CAT:
2135 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2137 if( p_item->ppsz_list )
2140 psz_type = psz_buffer;
2142 for( i = 0; p_item->ppsz_list[i]; i++ )
2144 if( i ) strcat( psz_type, "," );
2145 strcat( psz_type, p_item->ppsz_list[i] );
2150 case CONFIG_ITEM_INTEGER:
2151 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2152 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2154 if( p_item->i_list )
2157 psz_type = psz_buffer;
2159 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2161 if( i ) strcat( psz_type, ", " );
2162 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2164 p_item->ppsz_list_text[i] );
2169 case CONFIG_ITEM_FLOAT:
2170 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2172 case CONFIG_ITEM_BOOL:
2173 psz_bra = ""; psz_type = ""; psz_ket = "";
2174 if( !b_help_module )
2176 psz_suf = p_item->i_value ? _(" (default enabled)") :
2177 _(" (default disabled)");
2187 /* Add short option if any */
2188 if( p_item->i_short )
2190 sprintf( psz_short, "-%c,", p_item->i_short );
2194 strcpy( psz_short, " " );
2197 i = PADDING_SPACES - strlen( p_item->psz_name )
2198 - strlen( psz_bra ) - strlen( psz_type )
2199 - strlen( psz_ket ) - 1;
2201 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2203 psz_prefix = ", --no-";
2204 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2209 psz_spaces[0] = '\n';
2214 psz_spaces[i] = '\0';
2217 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2219 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2220 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2221 psz_ket, psz_spaces );
2225 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2226 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2229 psz_spaces[i] = ' ';
2231 /* We wrap the rest of the output */
2232 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2233 b_description = config_GetInt( p_this, "help-verbose" );
2236 psz_text = psz_buffer;
2239 char *psz_parser, *psz_word;
2240 size_t i_end = strlen( psz_text );
2242 /* If the remaining text fits in a line, print it. */
2243 if( i_end <= (size_t)i_width )
2245 fprintf( stdout, "%s\n", psz_text );
2249 /* Otherwise, eat as many words as possible */
2250 psz_parser = psz_text;
2253 psz_word = psz_parser;
2254 psz_parser = strchr( psz_word, ' ' );
2255 /* If no space was found, we reached the end of the text
2256 * block; otherwise, we skip the space we just found. */
2257 psz_parser = psz_parser ? psz_parser + 1
2260 } while( psz_parser - psz_text <= i_width );
2262 /* We cut a word in one of these cases:
2263 * - it's the only word in the line and it's too long.
2264 * - we used less than 80% of the width and the word we are
2265 * going to wrap is longer than 40% of the width, and even
2266 * if the word would have fit in the next line. */
2267 if( psz_word == psz_text
2268 || ( psz_word - psz_text < 80 * i_width / 100
2269 && psz_parser - psz_word > 40 * i_width / 100 ) )
2271 char c = psz_text[i_width];
2272 psz_text[i_width] = '\0';
2273 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2274 psz_text += i_width;
2279 psz_word[-1] = '\0';
2280 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2281 psz_text = psz_word;
2285 if( b_description && p_item->psz_longtext )
2287 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2288 b_description = VLC_FALSE;
2289 psz_spaces = psz_spaces_longtext;
2290 fprintf( stdout, "%s", psz_spaces );
2296 /* Release the module list */
2297 vlc_list_release( p_list );
2300 /*****************************************************************************
2301 * ListModules: list the available modules with their description
2302 *****************************************************************************
2303 * Print a list of all available modules (builtins and plugins) and a short
2304 * description for each one.
2305 *****************************************************************************/
2306 static void ListModules( vlc_t *p_this )
2310 char psz_spaces[22];
2313 memset( psz_spaces, ' ', 22 );
2319 /* List all modules */
2320 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2322 /* Enumerate each module */
2323 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2327 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2329 /* Nasty hack, but right now I'm too tired to think about a nice
2331 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2335 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2336 psz_spaces, p_parser->psz_longname );
2338 psz_spaces[i] = ' ';
2341 vlc_list_release( p_list );
2343 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2348 /*****************************************************************************
2349 * Version: print complete program version
2350 *****************************************************************************
2351 * Print complete program version and build number.
2352 *****************************************************************************/
2353 static void Version( void )
2359 fprintf( stdout, VERSION_MESSAGE "\n" );
2361 _("This program comes with NO WARRANTY, to the extent permitted by "
2362 "law.\nYou may redistribute it under the terms of the GNU General "
2363 "Public License;\nsee the file named COPYING for details.\n"
2364 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2366 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2371 /*****************************************************************************
2372 * ShowConsole: On Win32, create an output console for debug messages
2373 *****************************************************************************
2374 * This function is useful only on Win32.
2375 *****************************************************************************/
2377 static void ShowConsole( void )
2381 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2384 freopen( "CONOUT$", "w", stdout );
2385 freopen( "CONOUT$", "w", stderr );
2386 freopen( "CONIN$", "r", stdin );
2392 /*****************************************************************************
2393 * PauseConsole: On Win32, wait for a key press before closing the console
2394 *****************************************************************************
2395 * This function is useful only on Win32.
2396 *****************************************************************************/
2398 static void PauseConsole( void )
2402 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2403 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2410 /*****************************************************************************
2411 * ConsoleWidth: Return the console width in characters
2412 *****************************************************************************
2413 * We use the stty shell command to get the console width; if this fails or
2414 * if the width is less than 80, we default to 80.
2415 *****************************************************************************/
2416 static int ConsoleWidth( void )
2421 char buf[20], *psz_parser;
2425 file = popen( "stty size 2>/dev/null", "r" );
2428 i_ret = fread( buf, 1, 20, file );
2432 psz_parser = strchr( buf, ' ' );
2435 i_ret = atoi( psz_parser + 1 );
2450 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2451 vlc_value_t old_val, vlc_value_t new_val, void *param)
2453 vlc_t *p_vlc = (vlc_t *)p_this;
2455 if( new_val.i_int >= -1 )
2457 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2462 /*****************************************************************************
2463 * InitDeviceValues: initialize device values
2464 *****************************************************************************
2465 * This function inits the dvd, vcd and cd-audio values
2466 *****************************************************************************/
2467 static void InitDeviceValues( vlc_t *p_vlc )
2470 LibHalContext * ctx;
2476 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2478 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2480 for( i = 0; i < i_devices; i++ )
2482 if( !hal_device_property_exists( ctx, devices[ i ],
2483 "storage.cdrom.dvd" ) )
2488 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2489 "storage.cdrom.dvd" );
2490 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2495 config_PutPsz( p_vlc, "dvd", block_dev );
2498 config_PutPsz( p_vlc, "vcd", block_dev );
2499 config_PutPsz( p_vlc, "cd-audio", block_dev );
2501 hal_free_string( block_dev );
2503 hal_free_string_array( devices );
2506 hal_shutdown( ctx );
2511 /*****************************************************************************
2512 * FromLocale: converts a locale string to UTF-8
2513 *****************************************************************************/
2514 char *FromLocale( const char *locale )
2516 if( locale == NULL )
2519 if( libvlc.from_locale != (vlc_iconv_t)(-1) )
2521 char *iptr = (char *)locale, *output, *optr;
2525 * We are not allowed to modify the locale pointer, even if we cast it
2528 inb = strlen( locale );
2531 /* FIXME: I'm not sure about the value for the multiplication
2532 * (for western people, multiplication by 3 (Latin9) is sufficient) */
2533 optr = output = calloc( outb , 1);
2535 vlc_mutex_lock( &libvlc.from_locale_lock );
2536 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2538 while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
2543 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2545 vlc_mutex_unlock( &libvlc.from_locale_lock );
2547 return realloc( output, strlen( output ) + 1 );
2549 return (char *)locale;
2552 /*****************************************************************************
2553 * ToLocale: converts an UTF-8 string to locale
2554 *****************************************************************************/
2555 char *ToLocale( const char *utf8 )
2560 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
2562 char *iptr = (char *)utf8, *output, *optr;
2566 * We are not allowed to modify the locale pointer, even if we cast it
2569 inb = strlen( utf8 );
2570 /* FIXME: I'm not sure about the value for the multiplication
2571 * (for western people, multiplication is not needed) */
2574 optr = output = calloc( outb, 1 );
2575 vlc_mutex_lock( &libvlc.to_locale_lock );
2576 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2578 while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
2581 *optr++ = '?'; /* should not happen, and yes, it sucks */
2583 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2585 vlc_mutex_unlock( &libvlc.to_locale_lock );
2587 return realloc( output, strlen( output ) + 1 );
2589 return (char *)utf8;
2592 void LocaleFree( const char *str )
2594 if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
2595 free( (char *)str );