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_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 vlc_t * p_vlc = vlc_current_object( i_object );
279 module_t *p_help_module;
280 playlist_t *p_playlist;
282 #if defined( ENABLE_NLS ) \
283 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
293 * System specific initialization code
295 system_Init( p_vlc, &i_argc, ppsz_argv );
297 /* Get the executable name (similar to the basename command) */
300 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
303 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
309 p_vlc->psz_object_name = "vlc";
313 * Support for gettext
318 * Global iconv, must be done after setlocale()
319 * so that vlc_current_charset() works.
323 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
324 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
326 /* Initialize the module bank and load the configuration of the
327 * main module. We need to do this at this stage to be able to display
328 * a short help if required by the user. (short help == main module
330 module_InitBank( p_vlc );
332 /* Hack: insert the help module here */
333 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
334 if( p_help_module == NULL )
336 module_EndBank( p_vlc );
337 if( i_object ) vlc_object_release( p_vlc );
340 p_help_module->psz_object_name = "help";
341 p_help_module->psz_longname = N_("Help options");
342 config_Duplicate( p_help_module, p_help_config );
343 vlc_object_attach( p_help_module, libvlc.p_module_bank );
346 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
348 vlc_object_detach( p_help_module );
349 config_Free( p_help_module );
350 vlc_object_destroy( p_help_module );
351 module_EndBank( p_vlc );
352 if( i_object ) vlc_object_release( p_vlc );
356 /* Check for short help option */
357 if( config_GetInt( p_vlc, "help" ) )
359 Help( p_vlc, "help" );
362 /* Check for version option */
363 else if( config_GetInt( p_vlc, "version" ) )
369 /* Set the config file stuff */
370 p_vlc->psz_homedir = config_GetHomeDir();
371 p_vlc->psz_userdir = config_GetUserDir();
372 if( p_vlc->psz_userdir == NULL )
373 p_vlc->psz_userdir = strdup(p_vlc->psz_homedir);
374 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
375 if( p_vlc->psz_configfile != NULL && p_vlc->psz_configfile[0] == '~'
376 && p_vlc->psz_configfile[1] == '/' )
378 char *psz = malloc( strlen(p_vlc->psz_userdir)
379 + strlen(p_vlc->psz_configfile) );
380 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
381 sprintf( psz, "%s/%s", p_vlc->psz_userdir,
382 p_vlc->psz_configfile + 2 );
383 free( p_vlc->psz_configfile );
384 p_vlc->psz_configfile = psz;
387 /* Check for plugins cache options */
388 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
390 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
393 /* Hack: remove the help module here */
394 vlc_object_detach( p_help_module );
397 /* Will be re-done properly later on */
398 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
400 /* Check for daemon mode */
402 if( config_GetInt( p_vlc, "daemon" ) )
405 if( daemon( 1, 0) != 0 )
407 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
411 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
416 if( ( i_pid = fork() ) < 0 )
418 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
423 /* This is the parent, exit right now */
424 msg_Dbg( p_vlc, "closing parent process" );
429 /* We are the child */
430 msg_Dbg( p_vlc, "daemon spawned" );
431 close( STDIN_FILENO );
432 close( STDOUT_FILENO );
433 close( STDERR_FILENO );
435 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
443 config_Free( p_help_module );
444 vlc_object_destroy( p_help_module );
445 module_EndBank( p_vlc );
446 if( i_object ) vlc_object_release( p_vlc );
450 /* Check for translation config option */
451 #if defined( ENABLE_NLS ) \
452 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
454 /* This ain't really nice to have to reload the config here but it seems
455 * the only way to do it. */
456 config_LoadConfigFile( p_vlc, "main" );
457 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
459 /* Check if the user specified a custom language */
460 psz_language = config_GetPsz( p_vlc, "language" );
461 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
463 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
465 /* Reset the default domain */
466 SetLanguage( psz_language );
470 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
471 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
473 module_EndBank( p_vlc );
474 module_InitBank( p_vlc );
475 config_LoadConfigFile( p_vlc, "main" );
476 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
477 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
479 if( psz_language ) free( psz_language );
483 * Load the builtins and plugins into the module_bank.
484 * We have to do it before config_Load*() because this also gets the
485 * list of configuration options exported by each module and loads their
488 module_LoadBuiltins( p_vlc );
489 module_LoadPlugins( p_vlc );
495 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
496 libvlc.p_module_bank->i_children );
498 /* Hack: insert the help module here */
499 vlc_object_attach( p_help_module, libvlc.p_module_bank );
502 /* Check for help on modules */
503 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
505 Help( p_vlc, p_tmp );
509 /* Check for long help option */
510 else if( config_GetInt( p_vlc, "longhelp" ) )
512 Help( p_vlc, "longhelp" );
515 /* Check for module list option */
516 else if( config_GetInt( p_vlc, "list" ) )
518 ListModules( p_vlc );
522 /* Check for config file options */
523 if( config_GetInt( p_vlc, "reset-config" ) )
525 vlc_object_detach( p_help_module );
526 config_ResetAll( p_vlc );
527 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
528 config_SaveConfigFile( p_vlc, NULL );
529 vlc_object_attach( p_help_module, libvlc.p_module_bank );
531 if( config_GetInt( p_vlc, "save-config" ) )
533 vlc_object_detach( p_help_module );
534 config_LoadConfigFile( p_vlc, NULL );
535 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
536 config_SaveConfigFile( p_vlc, NULL );
537 vlc_object_attach( p_help_module, libvlc.p_module_bank );
540 /* Hack: remove the help module here */
541 vlc_object_detach( p_help_module );
546 config_Free( p_help_module );
547 vlc_object_destroy( p_help_module );
548 module_EndBank( p_vlc );
549 if( i_object ) vlc_object_release( p_vlc );
556 InitDeviceValues( p_vlc );
559 * Override default configuration with config file settings
561 config_LoadConfigFile( p_vlc, NULL );
563 /* Hack: insert the help module here */
564 vlc_object_attach( p_help_module, libvlc.p_module_bank );
568 * Override configuration with command line settings
570 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
574 /* Pause the console because it's destroyed when we exit */
575 fprintf( stderr, "The command line options couldn't be loaded, check "
576 "that they are valid.\n" );
579 vlc_object_detach( p_help_module );
580 config_Free( p_help_module );
581 vlc_object_destroy( p_help_module );
582 module_EndBank( p_vlc );
583 if( i_object ) vlc_object_release( p_vlc );
587 /* Hack: remove the help module here */
588 vlc_object_detach( p_help_module );
589 config_Free( p_help_module );
590 vlc_object_destroy( p_help_module );
594 * System specific configuration
596 system_Configure( p_vlc, &i_argc, ppsz_argv );
599 * Message queue options
602 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
603 if( config_GetInt( p_vlc, "quiet" ) )
606 var_Set( p_vlc, "verbose", val );
608 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
609 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
611 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
614 * Output messages that may still be in the queue
618 /* p_vlc initialization. FIXME ? */
620 if( !config_GetInt( p_vlc, "fpu" ) )
621 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
623 #if defined( __i386__ ) || defined( __x86_64__ )
624 if( !config_GetInt( p_vlc, "mmx" ) )
625 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
626 if( !config_GetInt( p_vlc, "3dn" ) )
627 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
628 if( !config_GetInt( p_vlc, "mmxext" ) )
629 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
630 if( !config_GetInt( p_vlc, "sse" ) )
631 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
632 if( !config_GetInt( p_vlc, "sse2" ) )
633 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
635 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
636 if( !config_GetInt( p_vlc, "altivec" ) )
637 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
640 #define PRINT_CAPABILITY( capability, string ) \
641 if( libvlc.i_cpu & capability ) \
643 strncat( p_capabilities, string " ", \
644 sizeof(p_capabilities) - strlen(p_capabilities) ); \
645 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
648 p_capabilities[0] = '\0';
649 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
650 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
651 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
652 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
653 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
654 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
655 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
656 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
657 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
658 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
659 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
662 * Choose the best memcpy module
664 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
666 if( p_vlc->pf_memcpy == NULL )
668 p_vlc->pf_memcpy = memcpy;
671 if( p_vlc->pf_memset == NULL )
673 p_vlc->pf_memset = memset;
677 * Initialize hotkey handling
679 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
680 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
681 /* Do a copy (we don't need to modify the strings) */
682 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
685 * Initialize playlist and get commandline files
687 p_playlist = playlist_Create( p_vlc );
690 msg_Err( p_vlc, "playlist initialization failed" );
691 if( p_vlc->p_memcpy_module != NULL )
693 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
695 module_EndBank( p_vlc );
696 if( i_object ) vlc_object_release( p_vlc );
700 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
701 if( psz_modules && *psz_modules )
703 /* Add service discovery modules */
704 playlist_AddSDModules( p_playlist, psz_modules );
706 if( psz_modules ) free( psz_modules );
709 * Load background interfaces
711 psz_modules = config_GetPsz( p_vlc, "extraintf" );
712 psz_control = config_GetPsz( p_vlc, "control" );
714 if( psz_modules && *psz_modules && psz_control && *psz_control )
716 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
717 strlen( psz_control ) + 1 );
718 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
720 else if( psz_control && *psz_control )
722 if( psz_modules ) free( psz_modules );
723 psz_modules = strdup( psz_control );
726 psz_parser = psz_modules;
727 while ( psz_parser && *psz_parser )
729 char *psz_module, *psz_temp;
730 psz_module = psz_parser;
731 psz_parser = strchr( psz_module, ':' );
737 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
740 sprintf( psz_temp, "%s,none", psz_module );
741 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
751 * Always load the hotkeys interface if it exists
753 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
756 * If needed, load the Xscreensaver interface
757 * Currently, only for X
759 #ifdef HAVE_X11_XLIB_H
760 if( config_GetInt( p_vlc, "disable-screensaver" ) == 1 )
762 VLC_AddIntf( 0, "screensaver", VLC_FALSE, VLC_FALSE );
767 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
769 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
770 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
771 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
772 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
773 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
774 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
775 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
776 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
777 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
778 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
779 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
780 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
782 /* Create volume callback system. */
783 var_Create( p_vlc, "volume-change", VLC_VAR_BOOL );
786 * Get input filenames given as commandline arguments
788 GetFilenames( p_vlc, i_argc, ppsz_argv );
791 * Get --open argument
793 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
794 var_Get( p_vlc, "open", &val );
795 if ( val.psz_string != NULL && *val.psz_string )
797 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
798 PLAYLIST_INSERT, 0 );
800 if ( val.psz_string != NULL ) free( val.psz_string );
802 if( i_object ) vlc_object_release( p_vlc );
806 /*****************************************************************************
807 * VLC_AddIntf: add an interface
808 *****************************************************************************
809 * This function opens an interface plugin and runs it. If b_block is set
810 * to 0, VLC_AddIntf will return immediately and let the interface run in a
811 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
812 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
813 * the playlist when it is completely initialised.
814 *****************************************************************************/
815 int VLC_AddIntf( int i_object, char const *psz_module,
816 vlc_bool_t b_block, vlc_bool_t b_play )
819 intf_thread_t *p_intf;
820 vlc_t *p_vlc = vlc_current_object( i_object );
828 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
831 * We prefer the dummy interface if none is specified. */
832 char *psz_interface = config_GetPsz( p_vlc, "intf" );
833 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
834 if( psz_interface ) free( psz_interface );
838 /* Try to create the interface */
839 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
843 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
844 if( i_object ) vlc_object_release( p_vlc );
848 /* Interface doesn't handle play on start so do it ourselves */
849 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
851 /* Try to run the interface */
852 p_intf->b_play = b_play;
853 p_intf->b_block = b_block;
854 i_err = intf_RunThread( p_intf );
857 vlc_object_detach( p_intf );
858 intf_Destroy( p_intf );
859 if( i_object ) vlc_object_release( p_vlc );
863 if( i_object ) vlc_object_release( p_vlc );
867 /*****************************************************************************
868 * VLC_Die: ask vlc to die.
869 *****************************************************************************
870 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
871 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
872 *****************************************************************************/
873 int VLC_Die( int i_object )
875 vlc_t *p_vlc = vlc_current_object( i_object );
882 p_vlc->b_die = VLC_TRUE;
884 if( i_object ) vlc_object_release( p_vlc );
888 /*****************************************************************************
889 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
890 *****************************************************************************/
891 int VLC_CleanUp( int i_object )
893 intf_thread_t * p_intf;
894 playlist_t * p_playlist;
895 vout_thread_t * p_vout;
896 aout_instance_t * p_aout;
897 announce_handler_t * p_announce;
898 vlc_t *p_vlc = vlc_current_object( i_object );
900 /* Check that the handle is valid */
907 * Ask the interfaces to stop and destroy them
909 msg_Dbg( p_vlc, "removing all interfaces" );
910 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
912 intf_StopThread( p_intf );
913 vlc_object_detach( p_intf );
914 vlc_object_release( p_intf );
915 intf_Destroy( p_intf );
921 msg_Dbg( p_vlc, "removing all playlists" );
922 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
925 vlc_object_detach( p_playlist );
926 vlc_object_release( p_playlist );
927 playlist_Destroy( p_playlist );
933 msg_Dbg( p_vlc, "removing all video outputs" );
934 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
936 vlc_object_detach( p_vout );
937 vlc_object_release( p_vout );
938 vout_Destroy( p_vout );
944 msg_Dbg( p_vlc, "removing all audio outputs" );
945 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
947 vlc_object_detach( (vlc_object_t *)p_aout );
948 vlc_object_release( (vlc_object_t *)p_aout );
949 aout_Delete( p_aout );
953 * Free announce handler(s?)
955 msg_Dbg( p_vlc, "removing announce handler" );
956 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
959 vlc_object_detach( p_announce );
960 vlc_object_release( p_announce );
961 announce_HandlerDestroy( p_announce );
964 if( i_object ) vlc_object_release( p_vlc );
968 /*****************************************************************************
969 * VLC_Destroy: Destroy everything.
970 *****************************************************************************
971 * This function requests the running threads to finish, waits for their
972 * termination, and destroys their structure.
973 *****************************************************************************/
974 int VLC_Destroy( int i_object )
976 vlc_t *p_vlc = vlc_current_object( i_object );
984 * Free allocated memory
986 if( p_vlc->p_memcpy_module )
988 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
989 p_vlc->p_memcpy_module = NULL;
995 module_EndBank( p_vlc );
997 if( p_vlc->psz_homedir )
999 free( p_vlc->psz_homedir );
1000 p_vlc->psz_homedir = NULL;
1003 if( p_vlc->psz_userdir )
1005 free( p_vlc->psz_userdir );
1006 p_vlc->psz_userdir = NULL;
1009 if( p_vlc->psz_configfile )
1011 free( p_vlc->psz_configfile );
1012 p_vlc->psz_configfile = NULL;
1015 if( p_vlc->p_hotkeys )
1017 free( p_vlc->p_hotkeys );
1018 p_vlc->p_hotkeys = NULL;
1022 * System specific cleaning code
1024 system_End( p_vlc );
1027 * Free message queue.
1028 * Nobody shall use msg_* afterward.
1031 msg_Destroy( p_libvlc );
1033 /* Destroy global iconv */
1036 /* Destroy mutexes */
1037 vlc_mutex_destroy( &p_vlc->config_lock );
1039 vlc_object_detach( p_vlc );
1041 /* Release object before destroying it */
1042 if( i_object ) vlc_object_release( p_vlc );
1044 vlc_object_destroy( p_vlc );
1046 /* Stop thread system: last one out please shut the door! */
1047 vlc_threads_end( p_libvlc );
1052 /*****************************************************************************
1053 * VLC_VariableSet: set a vlc variable
1054 *****************************************************************************/
1055 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
1057 vlc_t *p_vlc = vlc_current_object( i_object );
1065 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1066 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1067 if( !strncmp( psz_var, "conf::", 6 ) )
1069 module_config_t *p_item;
1070 char const *psz_newvar = psz_var + 6;
1072 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1076 switch( p_item->i_type )
1078 case CONFIG_ITEM_BOOL:
1079 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1081 case CONFIG_ITEM_INTEGER:
1082 config_PutInt( p_vlc, psz_newvar, value.i_int );
1084 case CONFIG_ITEM_FLOAT:
1085 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1088 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1091 if( i_object ) vlc_object_release( p_vlc );
1096 i_ret = var_Set( p_vlc, psz_var, value );
1098 if( i_object ) vlc_object_release( p_vlc );
1102 /*****************************************************************************
1103 * VLC_VariableGet: get a vlc variable
1104 *****************************************************************************/
1105 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1107 vlc_t *p_vlc = vlc_current_object( i_object );
1115 i_ret = var_Get( p_vlc , psz_var, p_value );
1117 if( i_object ) vlc_object_release( p_vlc );
1121 /*****************************************************************************
1122 * VLC_VariableType: get a vlc variable type
1123 *****************************************************************************/
1124 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1127 vlc_t *p_vlc = vlc_current_object( i_object );
1134 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1135 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1136 if( !strncmp( psz_var, "conf::", 6 ) )
1138 module_config_t *p_item;
1139 char const *psz_newvar = psz_var + 6;
1141 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1145 switch( p_item->i_type )
1147 case CONFIG_ITEM_BOOL:
1148 i_type = VLC_VAR_BOOL;
1150 case CONFIG_ITEM_INTEGER:
1151 i_type = VLC_VAR_INTEGER;
1153 case CONFIG_ITEM_FLOAT:
1154 i_type = VLC_VAR_FLOAT;
1157 i_type = VLC_VAR_STRING;
1165 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1167 if( i_object ) vlc_object_release( p_vlc );
1177 /*****************************************************************************
1178 * VLC_AddTarget: adds a target for playing.
1179 *****************************************************************************
1180 * This function adds psz_target to the current playlist. If a playlist does
1181 * not exist, it will create one.
1182 *****************************************************************************/
1183 int VLC_AddTarget( int i_object, char const *psz_target,
1184 char const **ppsz_options, int i_options,
1185 int i_mode, int i_pos )
1188 playlist_t *p_playlist;
1189 vlc_t *p_vlc = vlc_current_object( i_object );
1196 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1198 if( p_playlist == NULL )
1200 msg_Dbg( p_vlc, "no playlist present, creating one" );
1201 p_playlist = playlist_Create( p_vlc );
1203 if( p_playlist == NULL )
1205 if( i_object ) vlc_object_release( p_vlc );
1206 return VLC_EGENERIC;
1209 vlc_object_yield( p_playlist );
1212 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1213 i_mode, i_pos, -1, ppsz_options, i_options);
1215 vlc_object_release( p_playlist );
1217 if( i_object ) vlc_object_release( p_vlc );
1221 /*****************************************************************************
1223 *****************************************************************************/
1224 int VLC_Play( int i_object )
1226 playlist_t * p_playlist;
1227 vlc_t *p_vlc = vlc_current_object( i_object );
1229 /* Check that the handle is valid */
1235 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1239 if( i_object ) vlc_object_release( p_vlc );
1243 playlist_Play( p_playlist );
1244 vlc_object_release( p_playlist );
1246 if( i_object ) vlc_object_release( p_vlc );
1250 /*****************************************************************************
1251 * VLC_Pause: toggle pause
1252 *****************************************************************************/
1253 int VLC_Pause( int i_object )
1255 playlist_t * p_playlist;
1256 vlc_t *p_vlc = vlc_current_object( i_object );
1258 /* Check that the handle is valid */
1264 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1268 if( i_object ) vlc_object_release( p_vlc );
1272 playlist_Pause( p_playlist );
1273 vlc_object_release( p_playlist );
1275 if( i_object ) vlc_object_release( p_vlc );
1279 /*****************************************************************************
1280 * VLC_Pause: toggle pause
1281 *****************************************************************************/
1282 int VLC_Stop( int i_object )
1284 playlist_t * p_playlist;
1285 vlc_t *p_vlc = vlc_current_object( i_object );
1287 /* Check that the handle is valid */
1293 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1297 if( i_object ) vlc_object_release( p_vlc );
1301 playlist_Stop( p_playlist );
1302 vlc_object_release( p_playlist );
1304 if( i_object ) vlc_object_release( p_vlc );
1308 /*****************************************************************************
1309 * VLC_IsPlaying: Query for Playlist Status
1310 *****************************************************************************/
1312 vlc_bool_t VLC_IsPlaying( int i_object )
1314 playlist_t * p_playlist;
1315 vlc_bool_t b_playing;
1317 vlc_t *p_vlc = vlc_current_object( i_object );
1319 /* Check that the handle is valid */
1325 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1329 if( i_object ) vlc_object_release( p_vlc );
1333 if( p_playlist->p_input )
1336 var_Get( p_playlist->p_input, "state", &val );
1337 b_playing = ( val.i_int == PLAYING_S );
1341 b_playing = playlist_IsPlaying( p_playlist );
1343 vlc_object_release( p_playlist );
1345 if( i_object ) vlc_object_release( p_vlc );
1350 * Get the current position in a input
1352 * Return the current position as a float
1353 * \note For some inputs, this will be unknown.
1355 * \param i_object a vlc object id
1356 * \return a float in the range of 0.0 - 1.0
1358 float VLC_PositionGet( int i_object )
1360 input_thread_t *p_input;
1362 vlc_t *p_vlc = vlc_current_object( i_object );
1364 /* Check that the handle is valid */
1370 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1374 if( i_object ) vlc_object_release( p_vlc );
1378 var_Get( p_input, "position", &val );
1379 vlc_object_release( p_input );
1381 if( i_object ) vlc_object_release( p_vlc );
1386 * Set the current position in a input
1388 * Set the current position in a input and then return
1389 * the current position as a float.
1390 * \note For some inputs, this will be unknown.
1392 * \param i_object a vlc object id
1393 * \param i_position a float in the range of 0.0 - 1.0
1394 * \return a float in the range of 0.0 - 1.0
1396 float VLC_PositionSet( int i_object, float i_position )
1398 input_thread_t *p_input;
1400 vlc_t *p_vlc = vlc_current_object( i_object );
1402 /* Check that the handle is valid */
1408 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1412 if( i_object ) vlc_object_release( p_vlc );
1416 val.f_float = i_position;
1417 var_Set( p_input, "position", val );
1418 var_Get( p_input, "position", &val );
1419 vlc_object_release( p_input );
1421 if( i_object ) vlc_object_release( p_vlc );
1426 * Get the current position in a input
1428 * Return the current position in seconds from the start.
1429 * \note For some inputs, this will be unknown.
1431 * \param i_object a vlc object id
1432 * \return the offset from 0:00 in seconds
1434 int VLC_TimeGet( int i_object )
1436 input_thread_t *p_input;
1438 vlc_t *p_vlc = vlc_current_object( i_object );
1440 /* Check that the handle is valid */
1446 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1450 if( i_object ) vlc_object_release( p_vlc );
1454 var_Get( p_input, "time", &val );
1455 vlc_object_release( p_input );
1457 if( i_object ) vlc_object_release( p_vlc );
1458 return val.i_time / 1000000;
1462 * Seek to a position in the current input
1464 * Seek i_seconds in the current input. If b_relative is set,
1465 * then the seek will be relative to the current position, otherwise
1466 * it will seek to i_seconds from the beginning of the input.
1467 * \note For some inputs, this will be unknown.
1469 * \param i_object a vlc object id
1470 * \param i_seconds seconds from current position or from beginning of input
1471 * \param b_relative seek relative from current position
1472 * \return VLC_SUCCESS on success
1474 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1476 input_thread_t *p_input;
1478 vlc_t *p_vlc = vlc_current_object( i_object );
1480 /* Check that the handle is valid */
1486 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1490 if( i_object ) vlc_object_release( p_vlc );
1496 val.i_time = i_seconds;
1497 val.i_time = val.i_time * 1000000L;
1498 var_Set( p_input, "time-offset", val );
1502 val.i_time = i_seconds;
1503 val.i_time = val.i_time * 1000000L;
1504 var_Set( p_input, "time", val );
1506 vlc_object_release( p_input );
1508 if( i_object ) vlc_object_release( p_vlc );
1513 * Get the total length of a input
1515 * Return the total length in seconds from the current input.
1516 * \note For some inputs, this will be unknown.
1518 * \param i_object a vlc object id
1519 * \return the length in seconds
1521 int VLC_LengthGet( int i_object )
1523 input_thread_t *p_input;
1525 vlc_t *p_vlc = vlc_current_object( i_object );
1527 /* Check that the handle is valid */
1533 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1537 if( i_object ) vlc_object_release( p_vlc );
1541 var_Get( p_input, "length", &val );
1542 vlc_object_release( p_input );
1544 if( i_object ) vlc_object_release( p_vlc );
1545 return val.i_time / 1000000L;
1549 * Play the input faster than realtime
1551 * 2x, 4x, 8x faster than realtime
1552 * \note For some inputs, this will be impossible.
1554 * \param i_object a vlc object id
1555 * \return the current speedrate
1557 float VLC_SpeedFaster( int i_object )
1559 input_thread_t *p_input;
1561 vlc_t *p_vlc = vlc_current_object( i_object );
1563 /* Check that the handle is valid */
1569 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1573 if( i_object ) vlc_object_release( p_vlc );
1577 val.b_bool = VLC_TRUE;
1578 var_Set( p_input, "rate-faster", val );
1579 var_Get( p_input, "rate", &val );
1580 vlc_object_release( p_input );
1582 if( i_object ) vlc_object_release( p_vlc );
1583 return val.f_float / INPUT_RATE_DEFAULT;
1587 * Play the input slower than realtime
1589 * 1/2x, 1/4x, 1/8x slower than realtime
1590 * \note For some inputs, this will be impossible.
1592 * \param i_object a vlc object id
1593 * \return the current speedrate
1595 float VLC_SpeedSlower( int i_object )
1597 input_thread_t *p_input;
1599 vlc_t *p_vlc = vlc_current_object( i_object );
1601 /* Check that the handle is valid */
1607 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1611 if( i_object ) vlc_object_release( p_vlc );
1615 val.b_bool = VLC_TRUE;
1616 var_Set( p_input, "rate-slower", val );
1617 var_Get( p_input, "rate", &val );
1618 vlc_object_release( p_input );
1620 if( i_object ) vlc_object_release( p_vlc );
1621 return val.f_float / INPUT_RATE_DEFAULT;
1625 * Return the current playlist item
1627 * Returns the index of the playlistitem that is currently selected for play.
1628 * This is valid even if nothing is currently playing.
1630 * \param i_object a vlc object id
1631 * \return the current index
1633 int VLC_PlaylistIndex( int i_object )
1636 playlist_t * p_playlist;
1637 vlc_t *p_vlc = vlc_current_object( i_object );
1639 /* Check that the handle is valid */
1645 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1649 if( i_object ) vlc_object_release( p_vlc );
1653 i_index = p_playlist->i_index;
1654 vlc_object_release( p_playlist );
1656 if( i_object ) vlc_object_release( p_vlc );
1661 * Total amount of items in the playlist
1663 * \param i_object a vlc object id
1664 * \return amount of playlist items
1666 int VLC_PlaylistNumberOfItems( int i_object )
1669 playlist_t * p_playlist;
1670 vlc_t *p_vlc = vlc_current_object( i_object );
1672 /* Check that the handle is valid */
1678 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1682 if( i_object ) vlc_object_release( p_vlc );
1686 i_size = p_playlist->i_size;
1687 vlc_object_release( p_playlist );
1689 if( i_object ) vlc_object_release( p_vlc );
1694 * Next playlist item
1696 * Skip to the next playlistitem and play it.
1698 * \param i_object a vlc object id
1699 * \return VLC_SUCCESS on success
1701 int VLC_PlaylistNext( int i_object )
1703 playlist_t * p_playlist;
1704 vlc_t *p_vlc = vlc_current_object( i_object );
1706 /* Check that the handle is valid */
1712 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1716 if( i_object ) vlc_object_release( p_vlc );
1720 playlist_Next( p_playlist );
1721 vlc_object_release( p_playlist );
1723 if( i_object ) vlc_object_release( p_vlc );
1728 * Previous playlist item
1730 * Skip to the previous playlistitem and play it.
1732 * \param i_object a vlc object id
1733 * \return VLC_SUCCESS on success
1735 int VLC_PlaylistPrev( int i_object )
1737 playlist_t * p_playlist;
1738 vlc_t *p_vlc = vlc_current_object( i_object );
1740 /* Check that the handle is valid */
1746 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1750 if( i_object ) vlc_object_release( p_vlc );
1754 playlist_Prev( p_playlist );
1755 vlc_object_release( p_playlist );
1757 if( i_object ) vlc_object_release( p_vlc );
1762 /*****************************************************************************
1763 * VLC_PlaylistClear: Empty the playlist
1764 *****************************************************************************/
1765 int VLC_PlaylistClear( int i_object )
1768 playlist_t * p_playlist;
1769 vlc_t *p_vlc = vlc_current_object( i_object );
1771 /* Check that the handle is valid */
1777 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1781 if( i_object ) vlc_object_release( p_vlc );
1785 i_err = playlist_Clear( p_playlist );
1787 vlc_object_release( p_playlist );
1789 if( i_object ) vlc_object_release( p_vlc );
1796 * \param i_object a vlc object id
1797 * \param i_volume something in a range from 0-200
1798 * \return the new volume (range 0-200 %)
1800 int VLC_VolumeSet( int i_object, int i_volume )
1802 audio_volume_t i_vol = 0;
1803 vlc_t *p_vlc = vlc_current_object( i_object );
1805 /* Check that the handle is valid */
1811 if( i_volume >= 0 && i_volume <= 200 )
1813 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1814 aout_VolumeSet( p_vlc, i_vol );
1817 if( i_object ) vlc_object_release( p_vlc );
1818 return i_vol * 200 / AOUT_VOLUME_MAX;
1822 * Get the current volume
1824 * Retrieve the current volume.
1826 * \param i_object a vlc object id
1827 * \return the current volume (range 0-200 %)
1829 int VLC_VolumeGet( int i_object )
1831 audio_volume_t i_volume;
1832 vlc_t *p_vlc = vlc_current_object( i_object );
1834 /* Check that the handle is valid */
1840 aout_VolumeGet( p_vlc, &i_volume );
1842 if( i_object ) vlc_object_release( p_vlc );
1843 return i_volume*200/AOUT_VOLUME_MAX;
1847 * Mute/Unmute the volume
1849 * \param i_object a vlc object id
1850 * \return VLC_SUCCESS on success
1852 int VLC_VolumeMute( int i_object )
1854 vlc_t *p_vlc = vlc_current_object( i_object );
1856 /* Check that the handle is valid */
1862 aout_VolumeMute( p_vlc, NULL );
1864 if( i_object ) vlc_object_release( p_vlc );
1868 /*****************************************************************************
1869 * VLC_FullScreen: toggle fullscreen mode
1870 *****************************************************************************/
1871 int VLC_FullScreen( int i_object )
1873 vout_thread_t *p_vout;
1874 vlc_t *p_vlc = vlc_current_object( i_object );
1881 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1885 if( i_object ) vlc_object_release( p_vlc );
1889 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1890 vlc_object_release( p_vout );
1892 if( i_object ) vlc_object_release( p_vlc );
1896 /* following functions are local */
1898 static void LocaleInit( void )
1902 if( !vlc_current_charset( &psz_charset ) )
1904 char *psz_conv = psz_charset;
1907 * Still allow non-ASCII characters when the locale is not set.
1908 * Western Europeans are being favored for historical reasons.
1910 psz_conv = strcmp( psz_charset, "ASCII" )
1914 vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
1915 vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
1916 libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
1917 libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
1918 if( !libvlc.to_locale )
1920 /* Not sure it is the right thing to do, but at least it
1921 doesn't make vlc crash with msvc ! */
1922 libvlc.to_locale = (vlc_iconv_t)(-1);
1926 libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
1927 free( psz_charset );
1930 static void LocaleDeinit( void )
1932 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
1934 vlc_mutex_destroy( &libvlc.from_locale_lock );
1935 vlc_mutex_destroy( &libvlc.to_locale_lock );
1936 vlc_iconv_close( libvlc.from_locale );
1937 vlc_iconv_close( libvlc.to_locale );
1941 /*****************************************************************************
1942 * SetLanguage: set the interface language.
1943 *****************************************************************************
1944 * We set the LC_MESSAGES locale category for interface messages and buttons,
1945 * as well as the LC_CTYPE category for string sorting and possible wide
1946 * character support.
1947 *****************************************************************************/
1948 static void SetLanguage ( char const *psz_lang )
1950 #if defined( ENABLE_NLS ) \
1951 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1954 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1958 if( psz_lang && !*psz_lang )
1960 # if defined( HAVE_LC_MESSAGES )
1961 setlocale( LC_MESSAGES, psz_lang );
1963 setlocale( LC_CTYPE, psz_lang );
1968 /* I need that under Darwin, please check it doesn't disturb
1969 * other platforms. --Meuuh */
1970 setenv( "LANG", psz_lang, 1 );
1972 #elif defined( SYS_BEOS ) || defined( WIN32 )
1973 /* We set LC_ALL manually because it is the only way to set
1974 * the language at runtime under eg. Windows. Beware that this
1975 * makes the environment unconsistent when libvlc is unloaded and
1976 * should probably be moved to a safer place like vlc.c. */
1977 static char psz_lcall[20];
1978 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1979 psz_lcall[19] = '\0';
1980 putenv( psz_lcall );
1983 setlocale( LC_ALL, psz_lang );
1984 /* many code paths assume that float numbers are formatted according
1985 * to the US standard (ie. with dot as decimal point), so we keep
1986 * C for LC_NUMERIC. */
1987 setlocale(LC_NUMERIC, "C" );
1990 /* Specify where to find the locales for current domain */
1991 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1992 psz_path = LOCALEDIR;
1994 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1998 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
2000 fprintf( stderr, "warning: couldn't bind domain %s in directory %s\n",
2001 PACKAGE_NAME, psz_path );
2004 /* Set the default domain */
2005 textdomain( PACKAGE_NAME );
2006 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
2010 /*****************************************************************************
2011 * GetFilenames: parse command line options which are not flags
2012 *****************************************************************************
2013 * Parse command line for input files as well as their associated options.
2014 * An option always follows its associated input and begins with a ":".
2015 *****************************************************************************/
2016 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
2018 int i_opt, i_options;
2020 /* We assume that the remaining parameters are filenames
2021 * and their input options */
2022 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
2024 const char *psz_target;
2027 /* Count the input options */
2028 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
2034 /* TODO: write an internal function of this one, to avoid
2035 * unnecessary lookups. */
2036 /* FIXME: should we convert options to UTF-8 as well ?? */
2037 psz_target = FromLocale( ppsz_argv[ i_opt ] );
2038 VLC_AddTarget( p_vlc->i_object_id, psz_target,
2039 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
2041 PLAYLIST_INSERT, 0 );
2042 LocaleFree( psz_target );
2048 /*****************************************************************************
2049 * Help: print program help
2050 *****************************************************************************
2051 * Print a short inline help. Message interface is initialized at this stage.
2052 *****************************************************************************/
2053 static void Help( vlc_t *p_this, char const *psz_help_name )
2059 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
2061 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2062 Usage( p_this, "help" );
2063 Usage( p_this, "main" );
2065 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2067 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2068 Usage( p_this, NULL );
2070 else if( psz_help_name )
2072 Usage( p_this, psz_help_name );
2075 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2080 /*****************************************************************************
2081 * Usage: print module usage
2082 *****************************************************************************
2083 * Print a short inline help. Message interface is initialized at this stage.
2084 *****************************************************************************/
2085 static void Usage( vlc_t *p_this, char const *psz_module_name )
2087 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
2088 /* short option ------' | | | | | | |
2089 * option name ------------' | | | | | |
2090 * <bra -------------------------' | | | | |
2091 * option type or "" --------------' | | | |
2092 * ket> -----------------------------' | | |
2093 * padding spaces ---------------------' | |
2094 * comment -------------------------------' |
2095 * comment suffix --------------------------'
2097 * The purpose of having bra and ket is that we might i18n them as well.
2099 #define LINE_START 8
2100 #define PADDING_SPACES 25
2103 module_config_t *p_item;
2104 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2105 char psz_spaces_longtext[LINE_START+3];
2106 char psz_format[sizeof(FORMAT_STRING)];
2107 char psz_buffer[10000];
2110 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2111 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2112 vlc_bool_t b_description;
2114 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2115 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2116 memset( psz_spaces_longtext, ' ', LINE_START+2 );
2117 psz_spaces_longtext[LINE_START+2] = '\0';
2119 strcpy( psz_format, FORMAT_STRING );
2121 /* List all modules */
2122 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2124 /* Enumerate the config for each module */
2125 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2127 vlc_bool_t b_help_module;
2129 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2131 if( psz_module_name && strcmp( psz_module_name,
2132 p_parser->psz_object_name ) )
2137 /* Ignore modules without config options */
2138 if( !p_parser->i_config_items )
2143 /* Ignore modules with only advanced config options if requested */
2146 for( p_item = p_parser->p_config;
2147 p_item->i_type != CONFIG_HINT_END;
2150 if( (p_item->i_type & CONFIG_ITEM) &&
2151 !p_item->b_advanced ) break;
2153 if( p_item->i_type == CONFIG_HINT_END ) continue;
2156 /* Print name of module */
2157 if( strcmp( "main", p_parser->psz_object_name ) )
2158 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2160 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2162 /* Print module options */
2163 for( p_item = p_parser->p_config;
2164 p_item->i_type != CONFIG_HINT_END;
2167 char *psz_text, *psz_spaces = psz_spaces_text;
2168 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2169 char *psz_suf = "", *psz_prefix = NULL;
2172 /* Skip deprecated options */
2173 if( p_item->psz_current )
2177 /* Skip advanced options if requested */
2178 if( p_item->b_advanced && !b_advanced )
2183 switch( p_item->i_type )
2185 case CONFIG_HINT_CATEGORY:
2186 case CONFIG_HINT_USAGE:
2187 if( !strcmp( "main", p_parser->psz_object_name ) )
2188 fprintf( stdout, "\n %s\n", p_item->psz_text );
2191 case CONFIG_ITEM_STRING:
2192 case CONFIG_ITEM_FILE:
2193 case CONFIG_ITEM_DIRECTORY:
2194 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2195 case CONFIG_ITEM_MODULE_CAT:
2196 case CONFIG_ITEM_MODULE_LIST:
2197 case CONFIG_ITEM_MODULE_LIST_CAT:
2198 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2200 if( p_item->ppsz_list )
2203 psz_type = psz_buffer;
2205 for( i = 0; p_item->ppsz_list[i]; i++ )
2207 if( i ) strcat( psz_type, "," );
2208 strcat( psz_type, p_item->ppsz_list[i] );
2213 case CONFIG_ITEM_INTEGER:
2214 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2215 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2217 if( p_item->i_list )
2220 psz_type = psz_buffer;
2222 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2224 if( i ) strcat( psz_type, ", " );
2225 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2227 p_item->ppsz_list_text[i] );
2232 case CONFIG_ITEM_FLOAT:
2233 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2235 case CONFIG_ITEM_BOOL:
2236 psz_bra = ""; psz_type = ""; psz_ket = "";
2237 if( !b_help_module )
2239 psz_suf = p_item->i_value ? _(" (default enabled)") :
2240 _(" (default disabled)");
2250 /* Add short option if any */
2251 if( p_item->i_short )
2253 sprintf( psz_short, "-%c,", p_item->i_short );
2257 strcpy( psz_short, " " );
2260 i = PADDING_SPACES - strlen( p_item->psz_name )
2261 - strlen( psz_bra ) - strlen( psz_type )
2262 - strlen( psz_ket ) - 1;
2264 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2266 psz_prefix = ", --no-";
2267 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2272 psz_spaces[0] = '\n';
2277 psz_spaces[i] = '\0';
2280 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2282 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2283 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2284 psz_ket, psz_spaces );
2288 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2289 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2292 psz_spaces[i] = ' ';
2294 /* We wrap the rest of the output */
2295 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2296 b_description = config_GetInt( p_this, "help-verbose" );
2299 psz_text = psz_buffer;
2302 char *psz_parser, *psz_word;
2303 size_t i_end = strlen( psz_text );
2305 /* If the remaining text fits in a line, print it. */
2306 if( i_end <= (size_t)i_width )
2308 fprintf( stdout, "%s\n", psz_text );
2312 /* Otherwise, eat as many words as possible */
2313 psz_parser = psz_text;
2316 psz_word = psz_parser;
2317 psz_parser = strchr( psz_word, ' ' );
2318 /* If no space was found, we reached the end of the text
2319 * block; otherwise, we skip the space we just found. */
2320 psz_parser = psz_parser ? psz_parser + 1
2323 } while( psz_parser - psz_text <= i_width );
2325 /* We cut a word in one of these cases:
2326 * - it's the only word in the line and it's too long.
2327 * - we used less than 80% of the width and the word we are
2328 * going to wrap is longer than 40% of the width, and even
2329 * if the word would have fit in the next line. */
2330 if( psz_word == psz_text
2331 || ( psz_word - psz_text < 80 * i_width / 100
2332 && psz_parser - psz_word > 40 * i_width / 100 ) )
2334 char c = psz_text[i_width];
2335 psz_text[i_width] = '\0';
2336 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2337 psz_text += i_width;
2342 psz_word[-1] = '\0';
2343 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2344 psz_text = psz_word;
2348 if( b_description && p_item->psz_longtext )
2350 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2351 b_description = VLC_FALSE;
2352 psz_spaces = psz_spaces_longtext;
2353 fprintf( stdout, "%s", psz_spaces );
2359 /* Release the module list */
2360 vlc_list_release( p_list );
2363 /*****************************************************************************
2364 * ListModules: list the available modules with their description
2365 *****************************************************************************
2366 * Print a list of all available modules (builtins and plugins) and a short
2367 * description for each one.
2368 *****************************************************************************/
2369 static void ListModules( vlc_t *p_this )
2373 char psz_spaces[22];
2376 memset( psz_spaces, ' ', 22 );
2382 /* List all modules */
2383 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2385 /* Enumerate each module */
2386 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2390 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2392 /* Nasty hack, but right now I'm too tired to think about a nice
2394 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2398 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2399 psz_spaces, p_parser->psz_longname );
2401 psz_spaces[i] = ' ';
2404 vlc_list_release( p_list );
2406 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2411 /*****************************************************************************
2412 * Version: print complete program version
2413 *****************************************************************************
2414 * Print complete program version and build number.
2415 *****************************************************************************/
2416 static void Version( void )
2422 fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
2423 fprintf( stdout, _("Compiled by %s@%s.%s\n"),
2424 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
2425 fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
2426 if( strcmp( VLC_Changeset(), "exported" ) )
2427 fprintf( stdout, _("Based upon svn changeset [%s]\n"),
2430 _("This program comes with NO WARRANTY, to the extent permitted by "
2431 "law.\nYou may redistribute it under the terms of the GNU General "
2432 "Public License;\nsee the file named COPYING for details.\n"
2433 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2435 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2440 /*****************************************************************************
2441 * ShowConsole: On Win32, create an output console for debug messages
2442 *****************************************************************************
2443 * This function is useful only on Win32.
2444 *****************************************************************************/
2446 static void ShowConsole( void )
2451 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2455 freopen( "CONOUT$", "w", stderr );
2456 freopen( "CONIN$", "r", stdin );
2458 if( (f_help = fopen( "vlc-help.txt", "wt" )) )
2461 freopen( "vlc-help.txt", "wt", stdout );
2462 fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
2465 else freopen( "CONOUT$", "w", stdout );
2471 /*****************************************************************************
2472 * PauseConsole: On Win32, wait for a key press before closing the console
2473 *****************************************************************************
2474 * This function is useful only on Win32.
2475 *****************************************************************************/
2477 static void PauseConsole( void )
2481 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2483 fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2491 /*****************************************************************************
2492 * ConsoleWidth: Return the console width in characters
2493 *****************************************************************************
2494 * We use the stty shell command to get the console width; if this fails or
2495 * if the width is less than 80, we default to 80.
2496 *****************************************************************************/
2497 static int ConsoleWidth( void )
2502 char buf[20], *psz_parser;
2506 file = popen( "stty size 2>/dev/null", "r" );
2509 i_ret = fread( buf, 1, 20, file );
2513 psz_parser = strchr( buf, ' ' );
2516 i_ret = atoi( psz_parser + 1 );
2531 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2532 vlc_value_t old_val, vlc_value_t new_val, void *param)
2534 vlc_t *p_vlc = (vlc_t *)p_this;
2536 if( new_val.i_int >= -1 )
2538 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2543 /*****************************************************************************
2544 * InitDeviceValues: initialize device values
2545 *****************************************************************************
2546 * This function inits the dvd, vcd and cd-audio values
2547 *****************************************************************************/
2548 static void InitDeviceValues( vlc_t *p_vlc )
2551 LibHalContext * ctx;
2557 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2559 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2561 for( i = 0; i < i_devices; i++ )
2563 if( !hal_device_property_exists( ctx, devices[ i ],
2564 "storage.cdrom.dvd" ) )
2569 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2570 "storage.cdrom.dvd" );
2571 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2576 config_PutPsz( p_vlc, "dvd", block_dev );
2579 config_PutPsz( p_vlc, "vcd", block_dev );
2580 config_PutPsz( p_vlc, "cd-audio", block_dev );
2582 hal_free_string( block_dev );
2584 hal_free_string_array( devices );
2587 hal_shutdown( ctx );
2592 /*****************************************************************************
2593 * FromLocale: converts a locale string to UTF-8
2594 *****************************************************************************/
2595 char *FromLocale( const char *locale )
2597 if( locale == NULL )
2600 if( libvlc.from_locale != (vlc_iconv_t)(-1) )
2602 char *iptr = (char *)locale, *output, *optr;
2606 * We are not allowed to modify the locale pointer, even if we cast it
2609 inb = strlen( locale );
2612 /* FIXME: I'm not sure about the value for the multiplication
2613 * (for western people, multiplication by 3 (Latin9) is sufficient) */
2614 optr = output = calloc( outb , 1);
2616 vlc_mutex_lock( &libvlc.from_locale_lock );
2617 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2619 while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
2625 vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2627 vlc_mutex_unlock( &libvlc.from_locale_lock );
2629 return realloc( output, strlen( output ) + 1 );
2631 return (char *)locale;
2634 /*****************************************************************************
2635 * ToLocale: converts an UTF-8 string to locale
2636 *****************************************************************************/
2637 char *ToLocale( const char *utf8 )
2642 if( libvlc.to_locale != (vlc_iconv_t)(-1) )
2644 char *iptr = (char *)utf8, *output, *optr;
2648 * We are not allowed to modify the locale pointer, even if we cast it
2651 inb = strlen( utf8 );
2652 /* FIXME: I'm not sure about the value for the multiplication
2653 * (for western people, multiplication is not needed) */
2656 optr = output = calloc( outb, 1 );
2657 vlc_mutex_lock( &libvlc.to_locale_lock );
2658 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2660 while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
2663 *optr = '?'; /* should not happen, and yes, it sucks */
2666 vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2668 vlc_mutex_unlock( &libvlc.to_locale_lock );
2670 return realloc( output, strlen( output ) + 1 );
2672 return (char *)utf8;
2675 void LocaleFree( const char *str )
2677 if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
2678 free( (char *)str );