1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: libvlc.c,v 1.118 2004/03/03 20:39:52 gbazin Exp $
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"
81 /*****************************************************************************
82 * The evil global variable. We handle it with care, don't worry.
83 *****************************************************************************/
84 static libvlc_t libvlc;
85 static libvlc_t * p_libvlc;
86 static vlc_t * p_static_vlc;
88 /*****************************************************************************
90 *****************************************************************************/
91 static void SetLanguage ( char const * );
92 static int GetFilenames ( vlc_t *, int, char *[] );
93 static void Usage ( vlc_t *, char const *psz_module_name );
94 static void ListModules ( vlc_t * );
95 static void Version ( void );
98 static void ShowConsole ( void );
100 static int ConsoleWidth ( void );
102 static int VerboseCallback( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
105 /*****************************************************************************
106 * vlc_current_object: return the current object.
107 *****************************************************************************
108 * If i_object is non-zero, return the corresponding object. Otherwise,
109 * return the statically allocated p_vlc object.
110 *****************************************************************************/
111 vlc_t * vlc_current_object( int i_object )
115 return vlc_object_get( p_libvlc, i_object );
121 /*****************************************************************************
122 * VLC_Version: return the libvlc version.
123 *****************************************************************************
124 * This function returns full version string (numeric version and codename).
125 *****************************************************************************/
126 char const * VLC_Version( void )
128 return VERSION_MESSAGE;
131 /*****************************************************************************
132 * VLC_Error: strerror() equivalent
133 *****************************************************************************
134 * This function returns full version string (numeric version and codename).
135 *****************************************************************************/
136 char const * VLC_Error( int i_err )
138 return vlc_error( i_err );
141 /*****************************************************************************
142 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
143 *****************************************************************************
144 * This function allocates a vlc_t structure and returns a negative value
145 * in case of failure. Also, the thread system is initialized.
146 *****************************************************************************/
147 int VLC_Create( void )
150 vlc_t * p_vlc = NULL;
153 /* &libvlc never changes, so we can safely call this multiple times. */
156 /* vlc_threads_init *must* be the first internal call! No other call is
157 * allowed before the thread system has been initialized. */
158 i_ret = vlc_threads_init( p_libvlc );
164 /* Now that the thread system is initialized, we don't have much, but
165 * at least we have var_Create */
166 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
167 var_Get( p_libvlc, "libvlc", &lockval );
168 vlc_mutex_lock( lockval.p_address );
169 if( !libvlc.b_ready )
173 /* Guess what CPU we have */
174 libvlc.i_cpu = CPUCapabilities();
176 /* Find verbosity from VLC_VERBOSE environment variable */
177 psz_env = getenv( "VLC_VERBOSE" );
178 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
180 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
181 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
183 libvlc.b_color = VLC_FALSE;
186 /* Initialize message queue */
187 msg_Create( p_libvlc );
189 /* Announce who we are */
190 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
191 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
193 /* The module bank will be initialized later */
194 libvlc.p_module_bank = NULL;
196 libvlc.b_ready = VLC_TRUE;
198 vlc_mutex_unlock( lockval.p_address );
199 var_Destroy( p_libvlc, "libvlc" );
201 /* Allocate a vlc object */
202 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
207 p_vlc->thread_id = 0;
208 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
210 p_vlc->psz_object_name = "root";
212 /* Initialize mutexes */
213 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
215 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
218 /* Store our newly allocated structure in the global list */
219 vlc_object_attach( p_vlc, p_libvlc );
221 /* Store data for the non-reentrant API */
222 p_static_vlc = p_vlc;
224 return p_vlc->i_object_id;
227 /*****************************************************************************
228 * VLC_Init: initialize a vlc_t structure.
229 *****************************************************************************
230 * This function initializes a previously allocated vlc_t structure:
232 * - gettext initialization
233 * - message queue, module bank and playlist initialization
234 * - configuration and commandline parsing
235 *****************************************************************************/
236 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
238 char p_capabilities[200];
243 vlc_bool_t b_exit = VLC_FALSE;
244 vlc_t * p_vlc = vlc_current_object( i_object );
245 module_t *p_help_module;
246 playlist_t *p_playlist;
255 * System specific initialization code
257 system_Init( p_vlc, &i_argc, ppsz_argv );
259 /* Get the executable name (similar to the basename command) */
262 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
265 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
271 p_vlc->psz_object_name = "vlc";
275 * Support for gettext
279 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
280 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
282 /* Initialize the module bank and load the configuration of the
283 * main module. We need to do this at this stage to be able to display
284 * a short help if required by the user. (short help == main module
286 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
287 var_Get( p_libvlc, "libvlc", &lockval );
288 vlc_mutex_lock( lockval.p_address );
289 if( libvlc.p_module_bank == NULL )
291 module_InitBank( p_vlc );
292 module_LoadMain( p_vlc );
294 vlc_mutex_unlock( lockval.p_address );
295 var_Destroy( p_libvlc, "libvlc" );
297 /* Hack: insert the help module here */
298 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
299 if( p_help_module == NULL )
301 /*module_EndBank( p_vlc );*/
302 if( i_object ) vlc_object_release( p_vlc );
305 p_help_module->psz_object_name = "help";
306 p_help_module->psz_longname = N_("Help options");
307 config_Duplicate( p_help_module, p_help_config );
308 vlc_object_attach( p_help_module, libvlc.p_module_bank );
311 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
313 vlc_object_detach( p_help_module );
314 config_Free( p_help_module );
315 vlc_object_destroy( p_help_module );
316 /*module_EndBank( p_vlc );*/
317 if( i_object ) vlc_object_release( p_vlc );
321 /* Check for short help option */
322 if( config_GetInt( p_vlc, "help" ) )
324 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
325 p_vlc->psz_object_name );
326 Usage( p_vlc, "main" );
327 Usage( p_vlc, "help" );
330 /* Check for version option */
331 else if( config_GetInt( p_vlc, "version" ) )
337 /* Set the config file stuff */
338 p_vlc->psz_homedir = config_GetHomeDir();
339 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
341 /* Hack: remove the help module here */
342 vlc_object_detach( p_help_module );
347 config_Free( p_help_module );
348 vlc_object_destroy( p_help_module );
349 /*module_EndBank( p_vlc );*/
350 if( i_object ) vlc_object_release( p_vlc );
354 /* Check for translation config option */
355 #if defined( ENABLE_NLS ) \
356 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
358 /* This ain't really nice to have to reload the config here but it seems
359 * the only way to do it. */
360 config_LoadConfigFile( p_vlc, "main" );
361 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
363 /* Check if the user specified a custom language */
364 psz_language = config_GetPsz( p_vlc, "language" );
365 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
367 /* Reset the default domain */
368 SetLanguage( psz_language );
370 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
371 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
373 textdomain( PACKAGE );
375 #if defined( ENABLE_UTF8 )
376 bind_textdomain_codeset( PACKAGE, "UTF-8" );
379 module_EndBank( p_vlc );
380 module_InitBank( p_vlc );
381 module_LoadMain( p_vlc );
382 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
384 if( psz_language ) free( psz_language );
388 * Load the builtins and plugins into the module_bank.
389 * We have to do it before config_Load*() because this also gets the
390 * list of configuration options exported by each module and loads their
393 module_LoadBuiltins( p_vlc );
394 module_LoadPlugins( p_vlc );
400 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
401 libvlc.p_module_bank->i_children );
403 /* Hack: insert the help module here */
404 vlc_object_attach( p_help_module, libvlc.p_module_bank );
407 /* Check for help on modules */
408 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
410 Usage( p_vlc, p_tmp );
414 /* Check for long help option */
415 else if( config_GetInt( p_vlc, "longhelp" ) )
417 Usage( p_vlc, NULL );
420 /* Check for module list option */
421 else if( config_GetInt( p_vlc, "list" ) )
423 ListModules( p_vlc );
427 /* Check for config file options */
428 if( config_GetInt( p_vlc, "reset-config" ) )
430 vlc_object_detach( p_help_module );
431 config_ResetAll( p_vlc );
432 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
433 config_SaveConfigFile( p_vlc, NULL );
434 vlc_object_attach( p_help_module, libvlc.p_module_bank );
436 if( config_GetInt( p_vlc, "save-config" ) )
438 vlc_object_detach( p_help_module );
439 config_LoadConfigFile( p_vlc, NULL );
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 );
445 /* Hack: remove the help module here */
446 vlc_object_detach( p_help_module );
451 config_Free( p_help_module );
452 vlc_object_destroy( p_help_module );
453 /*module_EndBank( p_vlc );*/
454 if( i_object ) vlc_object_release( p_vlc );
459 * Override default configuration with config file settings
461 config_LoadConfigFile( p_vlc, NULL );
463 /* Hack: insert the help module here */
464 vlc_object_attach( p_help_module, libvlc.p_module_bank );
468 * Override configuration with command line settings
470 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
474 /* Pause the console because it's destroyed when we exit */
475 fprintf( stderr, "The command line options couldn't be loaded, check "
476 "that they are valid.\nPress the RETURN key to continue..." );
479 vlc_object_detach( p_help_module );
480 config_Free( p_help_module );
481 vlc_object_destroy( p_help_module );
482 /*module_EndBank( p_vlc );*/
483 if( i_object ) vlc_object_release( p_vlc );
487 /* Hack: remove the help module here */
488 vlc_object_detach( p_help_module );
489 config_Free( p_help_module );
490 vlc_object_destroy( p_help_module );
494 * System specific configuration
496 system_Configure( p_vlc, &i_argc, ppsz_argv );
499 * Message queue options
502 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
503 if( config_GetInt( p_vlc, "quiet" ) )
507 var_Set( p_vlc, "verbose", val );
509 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
510 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
512 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
515 * Output messages that may still be in the queue
519 /* p_vlc initialization. FIXME ? */
521 #if defined( __i386__ )
522 if( !config_GetInt( p_vlc, "mmx" ) )
523 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
524 if( !config_GetInt( p_vlc, "3dn" ) )
525 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
526 if( !config_GetInt( p_vlc, "mmxext" ) )
527 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
528 if( !config_GetInt( p_vlc, "sse" ) )
529 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
530 if( !config_GetInt( p_vlc, "sse2" ) )
531 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
533 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
534 if( !config_GetInt( p_vlc, "altivec" ) )
535 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
538 #define PRINT_CAPABILITY( capability, string ) \
539 if( libvlc.i_cpu & capability ) \
541 strncat( p_capabilities, string " ", \
542 sizeof(p_capabilities) - strlen(p_capabilities) ); \
543 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
546 p_capabilities[0] = '\0';
547 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
548 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
549 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
554 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
555 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
556 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
557 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
560 * Choose the best memcpy module
562 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
564 if( p_vlc->pf_memcpy == NULL )
566 p_vlc->pf_memcpy = memcpy;
569 if( p_vlc->pf_memset == NULL )
571 p_vlc->pf_memset = memset;
575 * Initialize hotkey handling
577 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
578 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
579 /* Do a copy (we don't need to modify the strings) */
580 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
583 * Initialize playlist and get commandline files
585 p_playlist = playlist_Create( p_vlc );
588 msg_Err( p_vlc, "playlist initialization failed" );
589 if( p_vlc->p_memcpy_module != NULL )
591 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
593 /*module_EndBank( p_vlc );*/
594 if( i_object ) vlc_object_release( p_vlc );
599 * Load background interfaces
601 psz_modules = config_GetPsz( p_vlc, "extraintf" );
602 psz_parser = psz_modules;
603 while ( psz_parser && *psz_parser )
605 char *psz_module, *psz_temp;
606 psz_module = psz_parser;
607 psz_parser = strchr( psz_module, ',' );
613 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
616 sprintf( psz_temp, "%s,none", psz_module );
617 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
627 * Allways load the hotkeys interface if it exists
629 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
632 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
634 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
635 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
644 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
645 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
648 * Get input filenames given as commandline arguments
650 GetFilenames( p_vlc, i_argc, ppsz_argv );
652 if( i_object ) vlc_object_release( p_vlc );
656 /*****************************************************************************
657 * VLC_AddIntf: add an interface
658 *****************************************************************************
659 * This function opens an interface plugin and runs it. If b_block is set
660 * to 0, VLC_AddIntf will return immediately and let the interface run in a
661 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
662 * user requests to quit.
663 *****************************************************************************/
664 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
667 intf_thread_t *p_intf;
668 vlc_t *p_vlc = vlc_current_object( i_object );
675 /* Try to create the interface */
676 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
680 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
681 if( i_object ) vlc_object_release( p_vlc );
685 /* Try to run the interface */
686 p_intf->b_block = b_block;
687 i_err = intf_RunThread( p_intf );
690 vlc_object_detach( p_intf );
691 intf_Destroy( p_intf );
692 if( i_object ) vlc_object_release( p_vlc );
696 if( i_object ) vlc_object_release( p_vlc );
700 /*****************************************************************************
701 * VLC_Destroy: stop playing and destroy everything.
702 *****************************************************************************
703 * This function requests the running threads to finish, waits for their
704 * termination, and destroys their structure.
705 *****************************************************************************/
706 int VLC_Destroy( int i_object )
708 vlc_t *p_vlc = vlc_current_object( i_object );
716 * Free allocated memory
718 if( p_vlc->p_memcpy_module )
720 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
721 p_vlc->p_memcpy_module = NULL;
724 if( p_vlc->psz_homedir )
726 free( p_vlc->psz_homedir );
727 p_vlc->psz_homedir = NULL;
730 if( p_vlc->psz_configfile )
732 free( p_vlc->psz_configfile );
733 p_vlc->psz_configfile = NULL;
736 if( p_vlc->p_hotkeys )
738 free( p_vlc->p_hotkeys );
739 p_vlc->p_hotkeys = NULL;
743 * XXX: Free module bank !
745 /*module_EndBank( p_vlc );*/
748 * System specific cleaning code
752 /* Destroy mutexes */
753 vlc_mutex_destroy( &p_vlc->config_lock );
755 vlc_mutex_destroy( &p_vlc->quicktime_lock );
758 vlc_object_detach( p_vlc );
760 /* Release object before destroying it */
761 if( i_object ) vlc_object_release( p_vlc );
763 vlc_object_destroy( p_vlc );
765 /* Stop thread system: last one out please shut the door! */
766 vlc_threads_end( p_libvlc );
771 /*****************************************************************************
772 * VLC_Die: ask vlc to die.
773 *****************************************************************************
774 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
775 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
776 *****************************************************************************/
777 int VLC_Die( int i_object )
779 vlc_t *p_vlc = vlc_current_object( i_object );
786 p_vlc->b_die = VLC_TRUE;
788 if( i_object ) vlc_object_release( p_vlc );
792 /*****************************************************************************
793 * VLC_AddTarget: adds a target for playing.
794 *****************************************************************************
795 * This function adds psz_target to the current playlist. If a playlist does
796 * not exist, it will create one.
797 *****************************************************************************/
798 int VLC_AddTarget( int i_object, char const *psz_target,
799 char const **ppsz_options, int i_options,
800 int i_mode, int i_pos )
803 playlist_t *p_playlist;
804 vlc_t *p_vlc = vlc_current_object( i_object );
811 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
813 if( p_playlist == NULL )
815 msg_Dbg( p_vlc, "no playlist present, creating one" );
816 p_playlist = playlist_Create( p_vlc );
818 if( p_playlist == NULL )
820 if( i_object ) vlc_object_release( p_vlc );
824 vlc_object_yield( p_playlist );
827 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
828 i_mode, i_pos, -1, ppsz_options, i_options);
830 vlc_object_release( p_playlist );
832 if( i_object ) vlc_object_release( p_vlc );
836 /*****************************************************************************
837 * VLC_Set: set a vlc variable
838 *****************************************************************************
840 *****************************************************************************/
841 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
843 vlc_t *p_vlc = vlc_current_object( i_object );
851 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
852 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
853 if( !strncmp( psz_var, "conf::", 6 ) )
855 module_config_t *p_item;
856 char const *psz_newvar = psz_var + 6;
858 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
862 switch( p_item->i_type )
864 case CONFIG_ITEM_BOOL:
865 config_PutInt( p_vlc, psz_newvar, value.b_bool );
867 case CONFIG_ITEM_INTEGER:
868 config_PutInt( p_vlc, psz_newvar, value.i_int );
870 case CONFIG_ITEM_FLOAT:
871 config_PutFloat( p_vlc, psz_newvar, value.f_float );
874 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
877 if( i_object ) vlc_object_release( p_vlc );
882 i_ret = var_Set( p_vlc, psz_var, value );
884 if( i_object ) vlc_object_release( p_vlc );
888 /*****************************************************************************
889 * VLC_Get: get a vlc variable
890 *****************************************************************************
892 *****************************************************************************/
893 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
895 vlc_t *p_vlc = vlc_current_object( i_object );
903 i_ret = var_Get( p_vlc, psz_var, p_value );
905 if( i_object ) vlc_object_release( p_vlc );
909 /* FIXME: temporary hacks */
912 /*****************************************************************************
913 * VLC_IsPlaying: Query for Playlist Status
914 *****************************************************************************/
915 vlc_bool_t VLC_IsPlaying( int i_object )
918 playlist_t * p_playlist;
921 vlc_t *p_vlc = vlc_current_object( i_object );
923 /* Check that the handle is valid */
929 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
933 if( i_object ) vlc_object_release( p_vlc );
937 playing = playlist_IsPlaying( p_playlist );
939 vlc_object_release( p_playlist );
941 if( i_object ) vlc_object_release( p_vlc );
948 /*****************************************************************************
949 * VLC_ClearPlaylist: Query for Playlist Status
952 *****************************************************************************/
953 int VLC_ClearPlaylist( int i_object )
956 playlist_t * p_playlist;
957 vlc_t *p_vlc = vlc_current_object( i_object );
959 /* Check that the handle is valid */
965 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
969 if( i_object ) vlc_object_release( p_vlc );
973 playlist_Clear(p_playlist);
975 vlc_object_release( p_playlist );
977 if( i_object ) vlc_object_release( p_vlc );
982 /*****************************************************************************
984 *****************************************************************************/
985 int VLC_Play( int i_object )
987 playlist_t * p_playlist;
988 vlc_t *p_vlc = vlc_current_object( i_object );
990 /* Check that the handle is valid */
996 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1000 if( i_object ) vlc_object_release( p_vlc );
1004 vlc_mutex_lock( &p_playlist->object_lock );
1005 if( p_playlist->i_size )
1007 vlc_mutex_unlock( &p_playlist->object_lock );
1008 playlist_Play( p_playlist );
1012 vlc_mutex_unlock( &p_playlist->object_lock );
1015 vlc_object_release( p_playlist );
1017 if( i_object ) vlc_object_release( p_vlc );
1021 /*****************************************************************************
1023 *****************************************************************************/
1024 int VLC_Stop( int i_object )
1026 intf_thread_t * p_intf;
1027 playlist_t * p_playlist;
1028 vout_thread_t * p_vout;
1029 aout_instance_t * p_aout;
1030 vlc_t *p_vlc = vlc_current_object( i_object );
1032 /* Check that the handle is valid */
1039 * Ask the interfaces to stop and destroy them
1041 msg_Dbg( p_vlc, "removing all interfaces" );
1043 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1045 intf_StopThread( p_intf );
1046 vlc_object_detach( p_intf );
1047 vlc_object_release( p_intf );
1048 intf_Destroy( p_intf );
1055 msg_Dbg( p_vlc, "removing all playlists" );
1056 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1059 vlc_object_detach( p_playlist );
1060 vlc_object_release( p_playlist );
1061 playlist_Destroy( p_playlist );
1065 * Free video outputs
1067 msg_Dbg( p_vlc, "removing all video outputs" );
1068 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1070 vlc_object_detach( p_vout );
1071 vlc_object_release( p_vout );
1072 vout_Destroy( p_vout );
1076 * Free audio outputs
1078 msg_Dbg( p_vlc, "removing all audio outputs" );
1079 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1081 vlc_object_detach( (vlc_object_t *)p_aout );
1082 vlc_object_release( (vlc_object_t *)p_aout );
1083 aout_Delete( p_aout );
1086 if( i_object ) vlc_object_release( p_vlc );
1090 /*****************************************************************************
1091 * VLC_Pause: toggle pause
1092 *****************************************************************************/
1093 int VLC_Pause( int i_object )
1095 input_thread_t *p_input;
1096 vlc_t *p_vlc = vlc_current_object( i_object );
1103 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1107 if( i_object ) vlc_object_release( p_vlc );
1111 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1112 vlc_object_release( p_input );
1114 if( i_object ) vlc_object_release( p_vlc );
1118 /*****************************************************************************
1119 * VLC_FullScreen: toggle fullscreen mode
1120 *****************************************************************************/
1121 int VLC_FullScreen( int i_object )
1123 vout_thread_t *p_vout;
1124 vlc_t *p_vlc = vlc_current_object( i_object );
1131 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1135 if( i_object ) vlc_object_release( p_vlc );
1139 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1140 vlc_object_release( p_vout );
1142 if( i_object ) vlc_object_release( p_vlc );
1146 /* following functions are local */
1148 /*****************************************************************************
1149 * SetLanguage: set the interface language.
1150 *****************************************************************************
1151 * We set the LC_MESSAGES locale category for interface messages and buttons,
1152 * as well as the LC_CTYPE category for string sorting and possible wide
1153 * character support.
1154 *****************************************************************************/
1155 static void SetLanguage ( char const *psz_lang )
1157 #if defined( ENABLE_NLS ) \
1158 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1161 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1165 if( psz_lang && !*psz_lang )
1167 # if defined( HAVE_LC_MESSAGES )
1168 setlocale( LC_MESSAGES, psz_lang );
1170 setlocale( LC_CTYPE, psz_lang );
1175 /* I need that under Darwin, please check it doesn't disturb
1176 * other platforms. --Meuuh */
1177 setenv( "LANG", psz_lang, 1 );
1179 #elif defined( SYS_BEOS ) || defined( WIN32 )
1180 /* We set LC_ALL manually because it is the only way to set
1181 * the language at runtime under eg. Windows. Beware that this
1182 * makes the environment unconsistent when libvlc is unloaded and
1183 * should probably be moved to a safer place like vlc.c. */
1184 static char psz_lcall[20];
1185 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1186 psz_lcall[19] = '\0';
1187 putenv( psz_lcall );
1190 setlocale( LC_ALL, psz_lang );
1193 /* Specify where to find the locales for current domain */
1194 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1195 psz_path = LOCALEDIR;
1197 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1201 if( !bindtextdomain( PACKAGE, psz_path ) )
1203 fprintf( stderr, "warning: no domain %s in directory %s\n",
1204 PACKAGE, psz_path );
1207 /* Set the default domain */
1208 textdomain( PACKAGE );
1210 #if defined( ENABLE_UTF8 )
1211 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1217 /*****************************************************************************
1218 * GetFilenames: parse command line options which are not flags
1219 *****************************************************************************
1220 * Parse command line for input files as well as their associated options.
1221 * An option always follows its associated input and begins with a ":".
1222 *****************************************************************************/
1223 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1225 int i_opt, i_options;
1227 /* We assume that the remaining parameters are filenames
1228 * and their input options */
1229 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1233 /* Count the input options */
1234 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1240 /* TODO: write an internal function of this one, to avoid
1241 * unnecessary lookups. */
1242 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1243 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1245 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1252 /*****************************************************************************
1253 * Usage: print program usage
1254 *****************************************************************************
1255 * Print a short inline help. Message interface is initialized at this stage.
1256 *****************************************************************************/
1257 static void Usage( vlc_t *p_this, char const *psz_module_name )
1259 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1260 /* short option ------' | | | | | | |
1261 * option name ------------' | | | | | |
1262 * <bra -------------------------' | | | | |
1263 * option type or "" --------------' | | | |
1264 * ket> -----------------------------' | | |
1265 * padding spaces ---------------------' | |
1266 * comment -------------------------------' |
1267 * comment suffix --------------------------'
1269 * The purpose of having bra and ket is that we might i18n them as well.
1271 #define LINE_START 8
1272 #define PADDING_SPACES 25
1275 module_config_t *p_item;
1276 char psz_spaces[PADDING_SPACES+LINE_START+1];
1277 char psz_format[sizeof(FORMAT_STRING)];
1278 char psz_buffer[1000];
1281 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1282 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1284 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1285 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1287 strcpy( psz_format, FORMAT_STRING );
1293 /* List all modules */
1294 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1296 /* Enumerate the config for each module */
1297 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1299 vlc_bool_t b_help_module;
1301 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1303 if( psz_module_name && strcmp( psz_module_name,
1304 p_parser->psz_object_name ) )
1309 /* Ignore modules without config options */
1310 if( !p_parser->i_config_items )
1315 /* Ignore modules with only advanced config options if requested */
1318 for( p_item = p_parser->p_config;
1319 p_item->i_type != CONFIG_HINT_END;
1322 if( (p_item->i_type & CONFIG_ITEM) &&
1323 !p_item->b_advanced ) break;
1325 if( p_item->i_type == CONFIG_HINT_END ) continue;
1328 /* Print name of module */
1329 if( strcmp( "main", p_parser->psz_object_name ) )
1330 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1332 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1334 /* Print module options */
1335 for( p_item = p_parser->p_config;
1336 p_item->i_type != CONFIG_HINT_END;
1340 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1341 char *psz_suf = "", *psz_prefix = NULL;
1344 /* Skip advanced options if requested */
1345 if( p_item->b_advanced && !b_advanced )
1350 switch( p_item->i_type )
1352 case CONFIG_HINT_CATEGORY:
1353 case CONFIG_HINT_USAGE:
1354 if( !strcmp( "main", p_parser->psz_object_name ) )
1355 fprintf( stdout, "\n %s\n", p_item->psz_text );
1358 case CONFIG_ITEM_STRING:
1359 case CONFIG_ITEM_FILE:
1360 case CONFIG_ITEM_DIRECTORY:
1361 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1362 if( !p_item->ppsz_list )
1364 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1370 psz_type = psz_buffer;
1372 for( i = 0; p_item->ppsz_list[i]; i++ )
1374 if( i ) strcat( psz_type, "," );
1375 strcat( psz_type, p_item->ppsz_list[i] );
1380 case CONFIG_ITEM_INTEGER:
1381 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1382 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1384 case CONFIG_ITEM_FLOAT:
1385 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1387 case CONFIG_ITEM_BOOL:
1388 psz_bra = ""; psz_type = ""; psz_ket = "";
1389 if( !b_help_module )
1391 psz_suf = p_item->i_value ? _(" (default enabled)") :
1392 _(" (default disabled)");
1402 /* Add short option if any */
1403 if( p_item->i_short )
1405 sprintf( psz_short, "-%c,", p_item->i_short );
1409 strcpy( psz_short, " " );
1412 i = PADDING_SPACES - strlen( p_item->psz_name )
1413 - strlen( psz_bra ) - strlen( psz_type )
1414 - strlen( psz_ket ) - 1;
1416 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1418 /* If option is of type --foo-bar, we print its counterpart
1419 * as --no-foo-bar, but if it is of type --foobar (without
1420 * dashes in the name) we print it as --nofoobar. Both
1421 * values are of course valid, only the display changes. */
1422 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1424 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1429 psz_spaces[0] = '\n';
1434 psz_spaces[i] = '\0';
1437 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1439 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1440 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1441 psz_ket, psz_spaces );
1445 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1446 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1449 psz_spaces[i] = ' ';
1451 /* We wrap the rest of the output */
1452 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1453 psz_text = psz_buffer;
1456 char *psz_parser, *psz_word;
1457 int i_end = strlen( psz_text );
1459 /* If the remaining text fits in a line, print it. */
1460 if( i_end <= i_width )
1462 fprintf( stdout, "%s\n", psz_text );
1466 /* Otherwise, eat as many words as possible */
1467 psz_parser = psz_text;
1470 psz_word = psz_parser;
1471 psz_parser = strchr( psz_word, ' ' );
1472 /* If no space was found, we reached the end of the text
1473 * block; otherwise, we skip the space we just found. */
1474 psz_parser = psz_parser ? psz_parser + 1
1477 } while( psz_parser - psz_text <= i_width );
1479 /* We cut a word in one of these cases:
1480 * - it's the only word in the line and it's too long.
1481 * - we used less than 80% of the width and the word we are
1482 * going to wrap is longer than 40% of the width, and even
1483 * if the word would have fit in the next line. */
1484 if( psz_word == psz_text
1485 || ( psz_word - psz_text < 80 * i_width / 100
1486 && psz_parser - psz_word > 40 * i_width / 100 ) )
1488 char c = psz_text[i_width];
1489 psz_text[i_width] = '\0';
1490 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1491 psz_text += i_width;
1496 psz_word[-1] = '\0';
1497 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1498 psz_text = psz_word;
1504 /* Release the module list */
1505 vlc_list_release( p_list );
1507 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1508 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1513 /*****************************************************************************
1514 * ListModules: list the available modules with their description
1515 *****************************************************************************
1516 * Print a list of all available modules (builtins and plugins) and a short
1517 * description for each one.
1518 *****************************************************************************/
1519 static void ListModules( vlc_t *p_this )
1523 char psz_spaces[22];
1526 memset( psz_spaces, ' ', 22 );
1533 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1534 p_this->p_vlc->psz_object_name );
1536 fprintf( stdout, _("[module] [description]\n") );
1538 /* List all modules */
1539 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1541 /* Enumerate each module */
1542 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1546 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1548 /* Nasty hack, but right now I'm too tired to think about a nice
1550 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1554 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1555 psz_spaces, p_parser->psz_longname );
1557 psz_spaces[i] = ' ';
1560 vlc_list_release( p_list );
1562 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1563 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1568 /*****************************************************************************
1569 * Version: print complete program version
1570 *****************************************************************************
1571 * Print complete program version and build number.
1572 *****************************************************************************/
1573 static void Version( void )
1579 fprintf( stdout, VERSION_MESSAGE "\n" );
1581 _("This program comes with NO WARRANTY, to the extent permitted by "
1582 "law.\nYou may redistribute it under the terms of the GNU General "
1583 "Public License;\nsee the file named COPYING for details.\n"
1584 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1586 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1587 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1592 /*****************************************************************************
1593 * ShowConsole: On Win32, create an output console for debug messages
1594 *****************************************************************************
1595 * This function is useful only on Win32.
1596 *****************************************************************************/
1598 static void ShowConsole( void )
1602 freopen( "CONOUT$", "w", stdout );
1603 freopen( "CONOUT$", "w", stderr );
1604 freopen( "CONIN$", "r", stdin );
1610 /*****************************************************************************
1611 * ConsoleWidth: Return the console width in characters
1612 *****************************************************************************
1613 * We use the stty shell command to get the console width; if this fails or
1614 * if the width is less than 80, we default to 80.
1615 *****************************************************************************/
1616 static int ConsoleWidth( void )
1621 char buf[20], *psz_parser;
1625 file = popen( "stty size 2>/dev/null", "r" );
1628 i_ret = fread( buf, 1, 20, file );
1632 psz_parser = strchr( buf, ' ' );
1635 i_ret = atoi( psz_parser + 1 );
1650 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1651 vlc_value_t old_val, vlc_value_t new_val, void *param)
1653 vlc_t *p_vlc = (vlc_t *)p_this;
1655 if( new_val.i_int >= -1 )
1657 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );