1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Pretend we are a builtin module
28 *****************************************************************************/
29 #define MODULE_NAME main
30 #define MODULE_PATH main
33 /*****************************************************************************
35 *****************************************************************************/
39 # include <errno.h> /* ENOMEM */
41 #include <stdio.h> /* sprintf() */
42 #include <string.h> /* strerror() */
43 #include <stdlib.h> /* free() */
46 # include <netinet/in.h> /* BSD: struct in_addr */
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
55 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
56 # include "extras/getopt.h"
63 #include "vlc_cpu.h" /* CPU detection */
64 #include "os_specific.h"
66 #include "vlc_error.h"
68 #include "stream_control.h"
69 #include "input_ext-intf.h"
71 #include "vlc_playlist.h"
72 #include "vlc_interface.h"
74 #include "audio_output.h"
76 #include "vlc_video.h"
77 #include "video_output.h"
79 #include "stream_output.h"
83 /*****************************************************************************
84 * The evil global variable. We handle it with care, don't worry.
85 *****************************************************************************/
86 static libvlc_t libvlc;
87 static libvlc_t * p_libvlc;
88 static vlc_t * p_static_vlc;
90 /*****************************************************************************
92 *****************************************************************************/
93 static void SetLanguage ( char const * );
94 static int GetFilenames ( vlc_t *, int, char *[] );
95 static void Usage ( vlc_t *, char const *psz_module_name );
96 static void ListModules ( vlc_t * );
97 static void Version ( void );
100 static void ShowConsole ( void );
102 static int ConsoleWidth ( void );
104 static int VerboseCallback( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
107 /*****************************************************************************
108 * vlc_current_object: return the current object.
109 *****************************************************************************
110 * If i_object is non-zero, return the corresponding object. Otherwise,
111 * return the statically allocated p_vlc object.
112 *****************************************************************************/
113 vlc_t * vlc_current_object( int i_object )
117 return vlc_object_get( p_libvlc, i_object );
123 /*****************************************************************************
124 * VLC_Version: return the libvlc version.
125 *****************************************************************************
126 * This function returns full version string (numeric version and codename).
127 *****************************************************************************/
128 char const * VLC_Version( void )
130 return VERSION_MESSAGE;
133 /*****************************************************************************
134 * VLC_Error: strerror() equivalent
135 *****************************************************************************
136 * This function returns full version string (numeric version and codename).
137 *****************************************************************************/
138 char const * VLC_Error( int i_err )
140 return vlc_error( i_err );
143 /*****************************************************************************
144 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
145 *****************************************************************************
146 * This function allocates a vlc_t structure and returns a negative value
147 * in case of failure. Also, the thread system is initialized.
148 *****************************************************************************/
149 int VLC_Create( void )
152 vlc_t * p_vlc = NULL;
155 /* &libvlc never changes, so we can safely call this multiple times. */
158 /* vlc_threads_init *must* be the first internal call! No other call is
159 * allowed before the thread system has been initialized. */
160 i_ret = vlc_threads_init( p_libvlc );
166 /* Now that the thread system is initialized, we don't have much, but
167 * at least we have var_Create */
168 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
169 var_Get( p_libvlc, "libvlc", &lockval );
170 vlc_mutex_lock( lockval.p_address );
171 if( !libvlc.b_ready )
175 /* Guess what CPU we have */
176 libvlc.i_cpu = CPUCapabilities();
178 /* Find verbosity from VLC_VERBOSE environment variable */
179 psz_env = getenv( "VLC_VERBOSE" );
180 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
182 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
183 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
185 libvlc.b_color = VLC_FALSE;
188 /* Initialize message queue */
189 msg_Create( p_libvlc );
191 /* Announce who we are */
192 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
193 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
195 /* The module bank will be initialized later */
196 libvlc.p_module_bank = NULL;
198 libvlc.b_ready = VLC_TRUE;
200 vlc_mutex_unlock( lockval.p_address );
201 var_Destroy( p_libvlc, "libvlc" );
203 /* Allocate a vlc object */
204 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
209 p_vlc->thread_id = 0;
210 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
212 p_vlc->psz_object_name = "root";
214 /* Initialize mutexes */
215 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
217 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
220 /* Store our newly allocated structure in the global list */
221 vlc_object_attach( p_vlc, p_libvlc );
223 /* Store data for the non-reentrant API */
224 p_static_vlc = p_vlc;
226 return p_vlc->i_object_id;
229 /*****************************************************************************
230 * VLC_Init: initialize a vlc_t structure.
231 *****************************************************************************
232 * This function initializes a previously allocated vlc_t structure:
234 * - gettext initialization
235 * - message queue, module bank and playlist initialization
236 * - configuration and commandline parsing
237 *****************************************************************************/
238 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
240 char p_capabilities[200];
245 vlc_bool_t b_exit = VLC_FALSE;
246 vlc_t * p_vlc = vlc_current_object( i_object );
247 module_t *p_help_module;
248 playlist_t *p_playlist;
257 * System specific initialization code
259 system_Init( p_vlc, &i_argc, ppsz_argv );
261 /* Get the executable name (similar to the basename command) */
264 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
267 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
273 p_vlc->psz_object_name = "vlc";
277 * Support for gettext
281 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
282 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
284 /* Initialize the module bank and load the configuration of the
285 * main module. We need to do this at this stage to be able to display
286 * a short help if required by the user. (short help == main module
288 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
289 var_Get( p_libvlc, "libvlc", &lockval );
290 vlc_mutex_lock( lockval.p_address );
291 if( libvlc.p_module_bank == NULL )
293 module_InitBank( p_vlc );
294 module_LoadMain( p_vlc );
296 vlc_mutex_unlock( lockval.p_address );
297 var_Destroy( p_libvlc, "libvlc" );
299 /* Hack: insert the help module here */
300 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
301 if( p_help_module == NULL )
303 /*module_EndBank( p_vlc );*/
304 if( i_object ) vlc_object_release( p_vlc );
307 p_help_module->psz_object_name = "help";
308 p_help_module->psz_longname = N_("Help options");
309 config_Duplicate( p_help_module, p_help_config );
310 vlc_object_attach( p_help_module, libvlc.p_module_bank );
313 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
315 vlc_object_detach( p_help_module );
316 config_Free( p_help_module );
317 vlc_object_destroy( p_help_module );
318 /*module_EndBank( p_vlc );*/
319 if( i_object ) vlc_object_release( p_vlc );
323 /* Check for short help option */
324 if( config_GetInt( p_vlc, "help" ) )
326 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
327 p_vlc->psz_object_name );
328 Usage( p_vlc, "main" );
329 Usage( p_vlc, "help" );
332 /* Check for version option */
333 else if( config_GetInt( p_vlc, "version" ) )
339 /* Set the config file stuff */
340 p_vlc->psz_homedir = config_GetHomeDir();
341 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
343 /* Check for plugins cache options */
344 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
346 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
349 /* Hack: remove the help module here */
350 vlc_object_detach( p_help_module );
355 config_Free( p_help_module );
356 vlc_object_destroy( p_help_module );
357 /*module_EndBank( p_vlc );*/
358 if( i_object ) vlc_object_release( p_vlc );
362 /* Check for translation config option */
363 #if defined( ENABLE_NLS ) \
364 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
366 /* This ain't really nice to have to reload the config here but it seems
367 * the only way to do it. */
368 config_LoadConfigFile( p_vlc, "main" );
369 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
371 /* Check if the user specified a custom language */
372 psz_language = config_GetPsz( p_vlc, "language" );
373 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
375 /* Reset the default domain */
376 SetLanguage( psz_language );
378 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
379 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
381 textdomain( PACKAGE );
383 #if defined( ENABLE_UTF8 )
384 bind_textdomain_codeset( PACKAGE, "UTF-8" );
387 module_EndBank( p_vlc );
388 module_InitBank( p_vlc );
389 module_LoadMain( p_vlc );
390 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
392 if( psz_language ) free( psz_language );
396 * Load the builtins and plugins into the module_bank.
397 * We have to do it before config_Load*() because this also gets the
398 * list of configuration options exported by each module and loads their
401 module_LoadBuiltins( p_vlc );
402 module_LoadPlugins( p_vlc );
408 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
409 libvlc.p_module_bank->i_children );
411 /* Hack: insert the help module here */
412 vlc_object_attach( p_help_module, libvlc.p_module_bank );
415 /* Check for help on modules */
416 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
418 Usage( p_vlc, p_tmp );
422 /* Check for long help option */
423 else if( config_GetInt( p_vlc, "longhelp" ) )
425 Usage( p_vlc, NULL );
428 /* Check for module list option */
429 else if( config_GetInt( p_vlc, "list" ) )
431 ListModules( p_vlc );
435 /* Check for config file options */
436 if( config_GetInt( p_vlc, "reset-config" ) )
438 vlc_object_detach( p_help_module );
439 config_ResetAll( p_vlc );
440 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
441 config_SaveConfigFile( p_vlc, NULL );
442 vlc_object_attach( p_help_module, libvlc.p_module_bank );
444 if( config_GetInt( p_vlc, "save-config" ) )
446 vlc_object_detach( p_help_module );
447 config_LoadConfigFile( p_vlc, NULL );
448 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
449 config_SaveConfigFile( p_vlc, NULL );
450 vlc_object_attach( p_help_module, libvlc.p_module_bank );
453 /* Hack: remove the help module here */
454 vlc_object_detach( p_help_module );
459 config_Free( p_help_module );
460 vlc_object_destroy( p_help_module );
461 /*module_EndBank( p_vlc );*/
462 if( i_object ) vlc_object_release( p_vlc );
467 * Override default configuration with config file settings
469 config_LoadConfigFile( p_vlc, NULL );
471 /* Hack: insert the help module here */
472 vlc_object_attach( p_help_module, libvlc.p_module_bank );
476 * Override configuration with command line settings
478 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
482 /* Pause the console because it's destroyed when we exit */
483 fprintf( stderr, "The command line options couldn't be loaded, check "
484 "that they are valid.\nPress the RETURN key to continue..." );
487 vlc_object_detach( p_help_module );
488 config_Free( p_help_module );
489 vlc_object_destroy( p_help_module );
490 /*module_EndBank( p_vlc );*/
491 if( i_object ) vlc_object_release( p_vlc );
495 /* Hack: remove the help module here */
496 vlc_object_detach( p_help_module );
497 config_Free( p_help_module );
498 vlc_object_destroy( p_help_module );
502 * System specific configuration
504 system_Configure( p_vlc, &i_argc, ppsz_argv );
507 * Message queue options
510 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
511 if( config_GetInt( p_vlc, "quiet" ) )
515 var_Set( p_vlc, "verbose", val );
517 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
518 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
520 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
523 * Output messages that may still be in the queue
527 /* p_vlc initialization. FIXME ? */
529 #if defined( __i386__ )
530 if( !config_GetInt( p_vlc, "mmx" ) )
531 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
532 if( !config_GetInt( p_vlc, "3dn" ) )
533 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
534 if( !config_GetInt( p_vlc, "mmxext" ) )
535 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
536 if( !config_GetInt( p_vlc, "sse" ) )
537 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
538 if( !config_GetInt( p_vlc, "sse2" ) )
539 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
541 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
542 if( !config_GetInt( p_vlc, "altivec" ) )
543 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
546 #define PRINT_CAPABILITY( capability, string ) \
547 if( libvlc.i_cpu & capability ) \
549 strncat( p_capabilities, string " ", \
550 sizeof(p_capabilities) - strlen(p_capabilities) ); \
551 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
554 p_capabilities[0] = '\0';
555 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
556 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
557 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
558 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
559 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
560 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
561 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
562 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
563 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
564 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
565 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
568 * Choose the best memcpy module
570 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
572 if( p_vlc->pf_memcpy == NULL )
574 p_vlc->pf_memcpy = memcpy;
577 if( p_vlc->pf_memset == NULL )
579 p_vlc->pf_memset = memset;
583 * Initialize hotkey handling
585 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
586 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
587 /* Do a copy (we don't need to modify the strings) */
588 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
591 * Initialize playlist and get commandline files
593 p_playlist = playlist_Create( p_vlc );
596 msg_Err( p_vlc, "playlist initialization failed" );
597 if( p_vlc->p_memcpy_module != NULL )
599 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
601 /*module_EndBank( p_vlc );*/
602 if( i_object ) vlc_object_release( p_vlc );
607 * Load background interfaces
609 psz_modules = config_GetPsz( p_vlc, "extraintf" );
610 psz_parser = psz_modules;
611 while ( psz_parser && *psz_parser )
613 char *psz_module, *psz_temp;
614 psz_module = psz_parser;
615 psz_parser = strchr( psz_module, ',' );
621 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
624 sprintf( psz_temp, "%s,none", psz_module );
625 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
635 * Allways load the hotkeys interface if it exists
637 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
640 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
642 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
644 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
645 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
646 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
647 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
648 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
649 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
650 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
651 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
652 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
653 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
656 * Get input filenames given as commandline arguments
658 GetFilenames( p_vlc, i_argc, ppsz_argv );
660 if( i_object ) vlc_object_release( p_vlc );
664 /*****************************************************************************
665 * VLC_AddIntf: add an interface
666 *****************************************************************************
667 * This function opens an interface plugin and runs it. If b_block is set
668 * to 0, VLC_AddIntf will return immediately and let the interface run in a
669 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
670 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
671 * the playlist when it is completely initialised.
672 *****************************************************************************/
673 int VLC_AddIntf( int i_object, char const *psz_module,
674 vlc_bool_t b_block, vlc_bool_t b_play )
677 intf_thread_t *p_intf;
678 vlc_t *p_vlc = vlc_current_object( i_object );
685 /* Try to create the interface */
686 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
690 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
691 if( i_object ) vlc_object_release( p_vlc );
695 /* Interface doesn't handle play on start so do it ourselves */
696 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
698 /* Try to run the interface */
699 p_intf->b_play = b_play;
700 p_intf->b_block = b_block;
701 i_err = intf_RunThread( p_intf );
704 vlc_object_detach( p_intf );
705 intf_Destroy( p_intf );
706 if( i_object ) vlc_object_release( p_vlc );
710 if( i_object ) vlc_object_release( p_vlc );
714 /*****************************************************************************
715 * VLC_Destroy: stop playing and destroy everything.
716 *****************************************************************************
717 * This function requests the running threads to finish, waits for their
718 * termination, and destroys their structure.
719 *****************************************************************************/
720 int VLC_Destroy( int i_object )
722 vlc_t *p_vlc = vlc_current_object( i_object );
730 * Free allocated memory
732 if( p_vlc->p_memcpy_module )
734 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
735 p_vlc->p_memcpy_module = NULL;
739 * XXX: Free module bank !
741 module_EndBank( p_vlc );
743 if( p_vlc->psz_homedir )
745 free( p_vlc->psz_homedir );
746 p_vlc->psz_homedir = NULL;
749 if( p_vlc->psz_configfile )
751 free( p_vlc->psz_configfile );
752 p_vlc->psz_configfile = NULL;
755 if( p_vlc->p_hotkeys )
757 free( p_vlc->p_hotkeys );
758 p_vlc->p_hotkeys = NULL;
762 * System specific cleaning code
766 /* Destroy mutexes */
767 vlc_mutex_destroy( &p_vlc->config_lock );
769 vlc_mutex_destroy( &p_vlc->quicktime_lock );
772 vlc_object_detach( p_vlc );
774 /* Release object before destroying it */
775 if( i_object ) vlc_object_release( p_vlc );
777 vlc_object_destroy( p_vlc );
779 /* Stop thread system: last one out please shut the door! */
780 vlc_threads_end( p_libvlc );
785 /*****************************************************************************
786 * VLC_Die: ask vlc to die.
787 *****************************************************************************
788 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
789 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
790 *****************************************************************************/
791 int VLC_Die( int i_object )
793 vlc_t *p_vlc = vlc_current_object( i_object );
800 p_vlc->b_die = VLC_TRUE;
802 if( i_object ) vlc_object_release( p_vlc );
806 /*****************************************************************************
807 * VLC_AddTarget: adds a target for playing.
808 *****************************************************************************
809 * This function adds psz_target to the current playlist. If a playlist does
810 * not exist, it will create one.
811 *****************************************************************************/
812 int VLC_AddTarget( int i_object, char const *psz_target,
813 char const **ppsz_options, int i_options,
814 int i_mode, int i_pos )
817 playlist_t *p_playlist;
818 vlc_t *p_vlc = vlc_current_object( i_object );
825 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
827 if( p_playlist == NULL )
829 msg_Dbg( p_vlc, "no playlist present, creating one" );
830 p_playlist = playlist_Create( p_vlc );
832 if( p_playlist == NULL )
834 if( i_object ) vlc_object_release( p_vlc );
838 vlc_object_yield( p_playlist );
841 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
842 i_mode, i_pos, -1, ppsz_options, i_options);
844 vlc_object_release( p_playlist );
846 if( i_object ) vlc_object_release( p_vlc );
850 /*****************************************************************************
851 * VLC_Set: set a vlc variable
852 *****************************************************************************
854 *****************************************************************************/
855 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
857 vlc_t *p_vlc = vlc_current_object( i_object );
865 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
866 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
867 if( !strncmp( psz_var, "conf::", 6 ) )
869 module_config_t *p_item;
870 char const *psz_newvar = psz_var + 6;
872 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
876 switch( p_item->i_type )
878 case CONFIG_ITEM_BOOL:
879 config_PutInt( p_vlc, psz_newvar, value.b_bool );
881 case CONFIG_ITEM_INTEGER:
882 config_PutInt( p_vlc, psz_newvar, value.i_int );
884 case CONFIG_ITEM_FLOAT:
885 config_PutFloat( p_vlc, psz_newvar, value.f_float );
888 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
891 if( i_object ) vlc_object_release( p_vlc );
896 i_ret = var_Set( p_vlc, psz_var, value );
898 if( i_object ) vlc_object_release( p_vlc );
902 /*****************************************************************************
903 * VLC_Get: get a vlc variable
904 *****************************************************************************
906 *****************************************************************************/
907 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
909 vlc_t *p_vlc = vlc_current_object( i_object );
917 i_ret = var_Get( p_vlc, psz_var, p_value );
919 if( i_object ) vlc_object_release( p_vlc );
923 /* FIXME: temporary hacks */
926 /*****************************************************************************
927 * VLC_IsPlaying: Query for Playlist Status
928 *****************************************************************************/
929 vlc_bool_t VLC_IsPlaying( int i_object )
932 playlist_t * p_playlist;
935 vlc_t *p_vlc = vlc_current_object( i_object );
937 /* Check that the handle is valid */
943 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
947 if( i_object ) vlc_object_release( p_vlc );
951 playing = playlist_IsPlaying( p_playlist );
953 vlc_object_release( p_playlist );
955 if( i_object ) vlc_object_release( p_vlc );
962 /*****************************************************************************
963 * VLC_ClearPlaylist: Query for Playlist Status
966 *****************************************************************************/
967 int VLC_ClearPlaylist( int i_object )
970 playlist_t * p_playlist;
971 vlc_t *p_vlc = vlc_current_object( i_object );
973 /* Check that the handle is valid */
979 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
983 if( i_object ) vlc_object_release( p_vlc );
987 playlist_Clear(p_playlist);
989 vlc_object_release( p_playlist );
991 if( i_object ) vlc_object_release( p_vlc );
996 /*****************************************************************************
998 *****************************************************************************/
999 int VLC_Play( int i_object )
1001 playlist_t * p_playlist;
1002 vlc_t *p_vlc = vlc_current_object( i_object );
1004 /* Check that the handle is valid */
1010 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1014 if( i_object ) vlc_object_release( p_vlc );
1018 vlc_mutex_lock( &p_playlist->object_lock );
1019 if( p_playlist->i_size )
1021 vlc_mutex_unlock( &p_playlist->object_lock );
1022 playlist_Play( p_playlist );
1026 vlc_mutex_unlock( &p_playlist->object_lock );
1029 vlc_object_release( p_playlist );
1031 if( i_object ) vlc_object_release( p_vlc );
1035 /*****************************************************************************
1037 *****************************************************************************/
1038 int VLC_Stop( int i_object )
1040 intf_thread_t * p_intf;
1041 playlist_t * p_playlist;
1042 vout_thread_t * p_vout;
1043 aout_instance_t * p_aout;
1044 announce_handler_t * p_announce;
1045 vlc_t *p_vlc = vlc_current_object( i_object );
1047 /* Check that the handle is valid */
1054 * Ask the interfaces to stop and destroy them
1056 msg_Dbg( p_vlc, "removing all interfaces" );
1058 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1060 intf_StopThread( p_intf );
1061 vlc_object_detach( p_intf );
1062 vlc_object_release( p_intf );
1063 intf_Destroy( p_intf );
1070 msg_Dbg( p_vlc, "removing all playlists" );
1071 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1074 vlc_object_detach( p_playlist );
1075 vlc_object_release( p_playlist );
1076 playlist_Destroy( p_playlist );
1080 * Free video outputs
1082 msg_Dbg( p_vlc, "removing all video outputs" );
1083 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1085 vlc_object_detach( p_vout );
1086 vlc_object_release( p_vout );
1087 vout_Destroy( p_vout );
1091 * Free audio outputs
1093 msg_Dbg( p_vlc, "removing all audio outputs" );
1094 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1096 vlc_object_detach( (vlc_object_t *)p_aout );
1097 vlc_object_release( (vlc_object_t *)p_aout );
1098 aout_Delete( p_aout );
1102 * Free announce handler(s?)
1104 msg_Dbg( p_vlc, "removing announce handler" );
1105 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
1108 vlc_object_detach( p_announce );
1109 vlc_object_release( p_announce );
1110 announce_HandlerDestroy( p_announce );
1113 if( i_object ) vlc_object_release( p_vlc );
1117 /*****************************************************************************
1118 * VLC_Pause: toggle pause
1119 *****************************************************************************/
1120 int VLC_Pause( int i_object )
1122 input_thread_t *p_input;
1123 vlc_t *p_vlc = vlc_current_object( i_object );
1130 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1134 if( i_object ) vlc_object_release( p_vlc );
1138 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1139 vlc_object_release( p_input );
1141 if( i_object ) vlc_object_release( p_vlc );
1145 /*****************************************************************************
1146 * VLC_FullScreen: toggle fullscreen mode
1147 *****************************************************************************/
1148 int VLC_FullScreen( int i_object )
1150 vout_thread_t *p_vout;
1151 vlc_t *p_vlc = vlc_current_object( i_object );
1158 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1162 if( i_object ) vlc_object_release( p_vlc );
1166 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1167 vlc_object_release( p_vout );
1169 if( i_object ) vlc_object_release( p_vlc );
1173 /* following functions are local */
1175 /*****************************************************************************
1176 * SetLanguage: set the interface language.
1177 *****************************************************************************
1178 * We set the LC_MESSAGES locale category for interface messages and buttons,
1179 * as well as the LC_CTYPE category for string sorting and possible wide
1180 * character support.
1181 *****************************************************************************/
1182 static void SetLanguage ( char const *psz_lang )
1184 #if defined( ENABLE_NLS ) \
1185 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1188 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1192 if( psz_lang && !*psz_lang )
1194 # if defined( HAVE_LC_MESSAGES )
1195 setlocale( LC_MESSAGES, psz_lang );
1197 setlocale( LC_CTYPE, psz_lang );
1202 /* I need that under Darwin, please check it doesn't disturb
1203 * other platforms. --Meuuh */
1204 setenv( "LANG", psz_lang, 1 );
1206 #elif defined( SYS_BEOS ) || defined( WIN32 )
1207 /* We set LC_ALL manually because it is the only way to set
1208 * the language at runtime under eg. Windows. Beware that this
1209 * makes the environment unconsistent when libvlc is unloaded and
1210 * should probably be moved to a safer place like vlc.c. */
1211 static char psz_lcall[20];
1212 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1213 psz_lcall[19] = '\0';
1214 putenv( psz_lcall );
1217 setlocale( LC_ALL, psz_lang );
1220 /* Specify where to find the locales for current domain */
1221 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1222 psz_path = LOCALEDIR;
1224 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1228 if( !bindtextdomain( PACKAGE, psz_path ) )
1230 fprintf( stderr, "warning: no domain %s in directory %s\n",
1231 PACKAGE, psz_path );
1234 /* Set the default domain */
1235 textdomain( PACKAGE );
1237 #if defined( ENABLE_UTF8 )
1238 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1244 /*****************************************************************************
1245 * GetFilenames: parse command line options which are not flags
1246 *****************************************************************************
1247 * Parse command line for input files as well as their associated options.
1248 * An option always follows its associated input and begins with a ":".
1249 *****************************************************************************/
1250 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1252 int i_opt, i_options;
1254 /* We assume that the remaining parameters are filenames
1255 * and their input options */
1256 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1260 /* Count the input options */
1261 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1267 /* TODO: write an internal function of this one, to avoid
1268 * unnecessary lookups. */
1269 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1270 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1272 PLAYLIST_INSERT, 0 );
1278 /*****************************************************************************
1279 * Usage: print program usage
1280 *****************************************************************************
1281 * Print a short inline help. Message interface is initialized at this stage.
1282 *****************************************************************************/
1283 static void Usage( vlc_t *p_this, char const *psz_module_name )
1285 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1286 /* short option ------' | | | | | | |
1287 * option name ------------' | | | | | |
1288 * <bra -------------------------' | | | | |
1289 * option type or "" --------------' | | | |
1290 * ket> -----------------------------' | | |
1291 * padding spaces ---------------------' | |
1292 * comment -------------------------------' |
1293 * comment suffix --------------------------'
1295 * The purpose of having bra and ket is that we might i18n them as well.
1297 #define LINE_START 8
1298 #define PADDING_SPACES 25
1301 module_config_t *p_item;
1302 char psz_spaces[PADDING_SPACES+LINE_START+1];
1303 char psz_format[sizeof(FORMAT_STRING)];
1304 char psz_buffer[1000];
1307 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1308 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1310 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1311 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1313 strcpy( psz_format, FORMAT_STRING );
1319 /* List all modules */
1320 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1322 /* Enumerate the config for each module */
1323 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1325 vlc_bool_t b_help_module;
1327 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1329 if( psz_module_name && strcmp( psz_module_name,
1330 p_parser->psz_object_name ) )
1335 /* Ignore modules without config options */
1336 if( !p_parser->i_config_items )
1341 /* Ignore modules with only advanced config options if requested */
1344 for( p_item = p_parser->p_config;
1345 p_item->i_type != CONFIG_HINT_END;
1348 if( (p_item->i_type & CONFIG_ITEM) &&
1349 !p_item->b_advanced ) break;
1351 if( p_item->i_type == CONFIG_HINT_END ) continue;
1354 /* Print name of module */
1355 if( strcmp( "main", p_parser->psz_object_name ) )
1356 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1358 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1360 /* Print module options */
1361 for( p_item = p_parser->p_config;
1362 p_item->i_type != CONFIG_HINT_END;
1366 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1367 char *psz_suf = "", *psz_prefix = NULL;
1370 /* Skip advanced options if requested */
1371 if( p_item->b_advanced && !b_advanced )
1376 switch( p_item->i_type )
1378 case CONFIG_HINT_CATEGORY:
1379 case CONFIG_HINT_USAGE:
1380 if( !strcmp( "main", p_parser->psz_object_name ) )
1381 fprintf( stdout, "\n %s\n", p_item->psz_text );
1384 case CONFIG_ITEM_STRING:
1385 case CONFIG_ITEM_FILE:
1386 case CONFIG_ITEM_DIRECTORY:
1387 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1388 if( !p_item->ppsz_list )
1390 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1396 psz_type = psz_buffer;
1398 for( i = 0; p_item->ppsz_list[i]; i++ )
1400 if( i ) strcat( psz_type, "," );
1401 strcat( psz_type, p_item->ppsz_list[i] );
1406 case CONFIG_ITEM_INTEGER:
1407 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1408 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1410 case CONFIG_ITEM_FLOAT:
1411 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1413 case CONFIG_ITEM_BOOL:
1414 psz_bra = ""; psz_type = ""; psz_ket = "";
1415 if( !b_help_module )
1417 psz_suf = p_item->i_value ? _(" (default enabled)") :
1418 _(" (default disabled)");
1428 /* Add short option if any */
1429 if( p_item->i_short )
1431 sprintf( psz_short, "-%c,", p_item->i_short );
1435 strcpy( psz_short, " " );
1438 i = PADDING_SPACES - strlen( p_item->psz_name )
1439 - strlen( psz_bra ) - strlen( psz_type )
1440 - strlen( psz_ket ) - 1;
1442 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1444 /* If option is of type --foo-bar, we print its counterpart
1445 * as --no-foo-bar, but if it is of type --foobar (without
1446 * dashes in the name) we print it as --nofoobar. Both
1447 * values are of course valid, only the display changes. */
1448 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1450 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1455 psz_spaces[0] = '\n';
1460 psz_spaces[i] = '\0';
1463 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1465 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1466 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1467 psz_ket, psz_spaces );
1471 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1472 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1475 psz_spaces[i] = ' ';
1477 /* We wrap the rest of the output */
1478 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1479 psz_text = psz_buffer;
1482 char *psz_parser, *psz_word;
1483 int i_end = strlen( psz_text );
1485 /* If the remaining text fits in a line, print it. */
1486 if( i_end <= i_width )
1488 fprintf( stdout, "%s\n", psz_text );
1492 /* Otherwise, eat as many words as possible */
1493 psz_parser = psz_text;
1496 psz_word = psz_parser;
1497 psz_parser = strchr( psz_word, ' ' );
1498 /* If no space was found, we reached the end of the text
1499 * block; otherwise, we skip the space we just found. */
1500 psz_parser = psz_parser ? psz_parser + 1
1503 } while( psz_parser - psz_text <= i_width );
1505 /* We cut a word in one of these cases:
1506 * - it's the only word in the line and it's too long.
1507 * - we used less than 80% of the width and the word we are
1508 * going to wrap is longer than 40% of the width, and even
1509 * if the word would have fit in the next line. */
1510 if( psz_word == psz_text
1511 || ( psz_word - psz_text < 80 * i_width / 100
1512 && psz_parser - psz_word > 40 * i_width / 100 ) )
1514 char c = psz_text[i_width];
1515 psz_text[i_width] = '\0';
1516 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1517 psz_text += i_width;
1522 psz_word[-1] = '\0';
1523 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1524 psz_text = psz_word;
1530 /* Release the module list */
1531 vlc_list_release( p_list );
1533 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1534 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1539 /*****************************************************************************
1540 * ListModules: list the available modules with their description
1541 *****************************************************************************
1542 * Print a list of all available modules (builtins and plugins) and a short
1543 * description for each one.
1544 *****************************************************************************/
1545 static void ListModules( vlc_t *p_this )
1549 char psz_spaces[22];
1552 memset( psz_spaces, ' ', 22 );
1559 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1560 p_this->p_vlc->psz_object_name );
1562 fprintf( stdout, _("[module] [description]\n") );
1564 /* List all modules */
1565 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1567 /* Enumerate each module */
1568 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1572 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1574 /* Nasty hack, but right now I'm too tired to think about a nice
1576 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1580 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1581 psz_spaces, p_parser->psz_longname );
1583 psz_spaces[i] = ' ';
1586 vlc_list_release( p_list );
1588 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1589 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1594 /*****************************************************************************
1595 * Version: print complete program version
1596 *****************************************************************************
1597 * Print complete program version and build number.
1598 *****************************************************************************/
1599 static void Version( void )
1605 fprintf( stdout, VERSION_MESSAGE "\n" );
1607 _("This program comes with NO WARRANTY, to the extent permitted by "
1608 "law.\nYou may redistribute it under the terms of the GNU General "
1609 "Public License;\nsee the file named COPYING for details.\n"
1610 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1612 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1613 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1618 /*****************************************************************************
1619 * ShowConsole: On Win32, create an output console for debug messages
1620 *****************************************************************************
1621 * This function is useful only on Win32.
1622 *****************************************************************************/
1624 static void ShowConsole( void )
1628 freopen( "CONOUT$", "w", stdout );
1629 freopen( "CONOUT$", "w", stderr );
1630 freopen( "CONIN$", "r", stdin );
1636 /*****************************************************************************
1637 * ConsoleWidth: Return the console width in characters
1638 *****************************************************************************
1639 * We use the stty shell command to get the console width; if this fails or
1640 * if the width is less than 80, we default to 80.
1641 *****************************************************************************/
1642 static int ConsoleWidth( void )
1647 char buf[20], *psz_parser;
1651 file = popen( "stty size 2>/dev/null", "r" );
1654 i_ret = fread( buf, 1, 20, file );
1658 psz_parser = strchr( buf, ' ' );
1661 i_ret = atoi( psz_parser + 1 );
1676 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1677 vlc_value_t old_val, vlc_value_t new_val, void *param)
1679 vlc_t *p_vlc = (vlc_t *)p_this;
1681 if( new_val.i_int >= -1 )
1683 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );