1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: libvlc.c,v 1.111 2004/01/25 11:32:32 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 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
209 p_vlc->psz_object_name = "root";
211 /* Initialize mutexes */
212 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
214 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
217 /* Store our newly allocated structure in the global list */
218 vlc_object_attach( p_vlc, p_libvlc );
220 /* Store data for the non-reentrant API */
221 p_static_vlc = p_vlc;
223 return p_vlc->i_object_id;
226 /*****************************************************************************
227 * VLC_Init: initialize a vlc_t structure.
228 *****************************************************************************
229 * This function initializes a previously allocated vlc_t structure:
231 * - gettext initialization
232 * - message queue, module bank and playlist initialization
233 * - configuration and commandline parsing
234 *****************************************************************************/
235 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
237 char p_capabilities[200];
242 vlc_bool_t b_exit = VLC_FALSE;
243 vlc_t * p_vlc = vlc_current_object( i_object );
244 module_t *p_help_module;
245 playlist_t *p_playlist;
254 * System specific initialization code
256 system_Init( p_vlc, &i_argc, ppsz_argv );
258 /* Get the executable name (similar to the basename command) */
261 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
264 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
270 p_vlc->psz_object_name = "vlc";
274 * Support for gettext
278 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
279 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
281 /* Initialize the module bank and load the configuration of the
282 * main module. We need to do this at this stage to be able to display
283 * a short help if required by the user. (short help == main module
285 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
286 var_Get( p_libvlc, "libvlc", &lockval );
287 vlc_mutex_lock( lockval.p_address );
288 if( libvlc.p_module_bank == NULL )
290 module_InitBank( p_vlc );
291 module_LoadMain( p_vlc );
293 vlc_mutex_unlock( lockval.p_address );
294 var_Destroy( p_libvlc, "libvlc" );
296 /* Hack: insert the help module here */
297 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
298 if( p_help_module == NULL )
300 /*module_EndBank( p_vlc );*/
301 if( i_object ) vlc_object_release( p_vlc );
304 p_help_module->psz_object_name = "help";
305 config_Duplicate( p_help_module, p_help_config );
306 vlc_object_attach( p_help_module, libvlc.p_module_bank );
309 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
311 vlc_object_detach( p_help_module );
312 config_Free( p_help_module );
313 vlc_object_destroy( p_help_module );
314 /*module_EndBank( p_vlc );*/
315 if( i_object ) vlc_object_release( p_vlc );
319 /* Check for short help option */
320 if( config_GetInt( p_vlc, "help" ) )
322 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
323 p_vlc->psz_object_name );
324 Usage( p_vlc, "main" );
325 Usage( p_vlc, "help" );
328 /* Check for version option */
329 else if( config_GetInt( p_vlc, "version" ) )
335 /* Set the config file stuff */
336 p_vlc->psz_homedir = config_GetHomeDir();
337 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
339 /* Hack: remove the help module here */
340 vlc_object_detach( p_help_module );
345 config_Free( p_help_module );
346 vlc_object_destroy( p_help_module );
347 /*module_EndBank( p_vlc );*/
348 if( i_object ) vlc_object_release( p_vlc );
352 /* Check for translation config option */
353 #if defined( ENABLE_NLS ) \
354 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
356 /* This ain't really nice to have to reload the config here but it seems
357 * the only way to do it. */
358 config_LoadConfigFile( p_vlc, "main" );
359 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
361 /* Check if the user specified a custom language */
362 psz_language = config_GetPsz( p_vlc, "language" );
363 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
365 /* Reset the default domain */
366 SetLanguage( psz_language );
368 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
369 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
371 textdomain( PACKAGE );
373 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
374 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
375 /* BeOS only support UTF8 strings */
376 /* Mac OS X prefers UTF8 */
377 bind_textdomain_codeset( PACKAGE, "UTF-8" );
380 module_EndBank( p_vlc );
381 module_InitBank( p_vlc );
382 module_LoadMain( p_vlc );
383 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
385 if( psz_language ) free( psz_language );
389 * Load the builtins and plugins into the module_bank.
390 * We have to do it before config_Load*() because this also gets the
391 * list of configuration options exported by each module and loads their
394 module_LoadBuiltins( p_vlc );
395 module_LoadPlugins( p_vlc );
401 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
402 libvlc.p_module_bank->i_children );
404 /* Hack: insert the help module here */
405 vlc_object_attach( p_help_module, libvlc.p_module_bank );
408 /* Check for help on modules */
409 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
411 Usage( p_vlc, p_tmp );
415 /* Check for long help option */
416 else if( config_GetInt( p_vlc, "longhelp" ) )
418 Usage( p_vlc, NULL );
421 /* Check for module list option */
422 else if( config_GetInt( p_vlc, "list" ) )
424 ListModules( p_vlc );
428 /* Check for config file options */
429 if( config_GetInt( p_vlc, "reset-config" ) )
431 vlc_object_detach( p_help_module );
432 config_ResetAll( p_vlc );
433 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
434 config_SaveConfigFile( p_vlc, NULL );
435 vlc_object_attach( p_help_module, libvlc.p_module_bank );
437 if( config_GetInt( p_vlc, "save-config" ) )
439 vlc_object_detach( p_help_module );
440 config_LoadConfigFile( p_vlc, NULL );
441 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
442 config_SaveConfigFile( p_vlc, NULL );
443 vlc_object_attach( p_help_module, libvlc.p_module_bank );
446 /* Hack: remove the help module here */
447 vlc_object_detach( p_help_module );
452 config_Free( p_help_module );
453 vlc_object_destroy( p_help_module );
454 /*module_EndBank( p_vlc );*/
455 if( i_object ) vlc_object_release( p_vlc );
460 * Override default configuration with config file settings
462 config_LoadConfigFile( p_vlc, NULL );
464 /* Hack: insert the help module here */
465 vlc_object_attach( p_help_module, libvlc.p_module_bank );
469 * Override configuration with command line settings
471 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
475 /* Pause the console because it's destroyed when we exit */
476 fprintf( stderr, "The command line options couldn't be loaded, check "
477 "that they are valid.\nPress the RETURN key to continue..." );
480 vlc_object_detach( p_help_module );
481 config_Free( p_help_module );
482 vlc_object_destroy( p_help_module );
483 /*module_EndBank( p_vlc );*/
484 if( i_object ) vlc_object_release( p_vlc );
488 /* Hack: remove the help module here */
489 vlc_object_detach( p_help_module );
490 config_Free( p_help_module );
491 vlc_object_destroy( p_help_module );
495 * System specific configuration
497 system_Configure( p_vlc, &i_argc, ppsz_argv );
500 * Message queue options
503 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504 if( config_GetInt( p_vlc, "quiet" ) )
508 var_Set( p_vlc, "verbose", val );
510 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
511 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
513 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
516 * Output messages that may still be in the queue
520 /* p_vlc initialization. FIXME ? */
522 #if defined( __i386__ )
523 if( !config_GetInt( p_vlc, "mmx" ) )
524 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
525 if( !config_GetInt( p_vlc, "3dn" ) )
526 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
527 if( !config_GetInt( p_vlc, "mmxext" ) )
528 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
529 if( !config_GetInt( p_vlc, "sse" ) )
530 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
532 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
533 if( !config_GetInt( p_vlc, "altivec" ) )
534 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
537 #define PRINT_CAPABILITY( capability, string ) \
538 if( libvlc.i_cpu & capability ) \
540 strncat( p_capabilities, string " ", \
541 sizeof(p_capabilities) - strlen(p_capabilities) ); \
542 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
545 p_capabilities[0] = '\0';
546 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
547 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
548 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
549 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
554 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
555 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
558 * Choose the best memcpy module
560 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
562 if( p_vlc->pf_memcpy == NULL )
564 p_vlc->pf_memcpy = memcpy;
567 if( p_vlc->pf_memset == NULL )
569 p_vlc->pf_memset = memset;
573 * Initialize hotkey handling
575 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
576 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
577 /* Do a copy (we don't need to modify the strings) */
578 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
581 * Initialize playlist and get commandline files
583 p_playlist = playlist_Create( p_vlc );
586 msg_Err( p_vlc, "playlist initialization failed" );
587 if( p_vlc->p_memcpy_module != NULL )
589 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
591 /*module_EndBank( p_vlc );*/
592 if( i_object ) vlc_object_release( p_vlc );
597 * Load background interfaces
599 psz_modules = config_GetPsz( p_vlc, "extraintf" );
600 psz_parser = psz_modules;
601 while ( psz_parser && *psz_parser )
603 char *psz_module, *psz_temp;
604 psz_module = psz_parser;
605 psz_parser = strchr( psz_module, ',' );
611 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
614 sprintf( psz_temp, "%s,none", psz_module );
615 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
625 * Allways load the hotkeys interface if it exists
627 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
630 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
632 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
633 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
634 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
635 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
646 * Get input filenames given as commandline arguments
648 GetFilenames( p_vlc, i_argc, ppsz_argv );
650 if( i_object ) vlc_object_release( p_vlc );
654 /*****************************************************************************
655 * VLC_AddIntf: add an interface
656 *****************************************************************************
657 * This function opens an interface plugin and runs it. If b_block is set
658 * to 0, VLC_AddIntf will return immediately and let the interface run in a
659 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
660 * user requests to quit.
661 *****************************************************************************/
662 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
665 intf_thread_t *p_intf;
666 vlc_t *p_vlc = vlc_current_object( i_object );
673 /* Try to create the interface */
674 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
678 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
679 if( i_object ) vlc_object_release( p_vlc );
683 /* Try to run the interface */
684 p_intf->b_block = b_block;
685 i_err = intf_RunThread( p_intf );
688 vlc_object_detach( p_intf );
689 intf_Destroy( p_intf );
690 if( i_object ) vlc_object_release( p_vlc );
694 if( i_object ) vlc_object_release( p_vlc );
698 /*****************************************************************************
699 * VLC_Destroy: stop playing and destroy everything.
700 *****************************************************************************
701 * This function requests the running threads to finish, waits for their
702 * termination, and destroys their structure.
703 *****************************************************************************/
704 int VLC_Destroy( int i_object )
706 vlc_t *p_vlc = vlc_current_object( i_object );
714 * Free allocated memory
716 if( p_vlc->p_memcpy_module )
718 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
719 p_vlc->p_memcpy_module = NULL;
722 if( p_vlc->psz_homedir )
724 free( p_vlc->psz_homedir );
725 p_vlc->psz_homedir = NULL;
728 if( p_vlc->psz_configfile )
730 free( p_vlc->psz_configfile );
731 p_vlc->psz_configfile = NULL;
734 if( p_vlc->p_hotkeys )
736 free( p_vlc->p_hotkeys );
737 p_vlc->p_hotkeys = NULL;
741 * XXX: Free module bank !
743 /*module_EndBank( p_vlc );*/
746 * System specific cleaning code
750 /* Destroy mutexes */
751 vlc_mutex_destroy( &p_vlc->config_lock );
753 vlc_mutex_destroy( &p_vlc->quicktime_lock );
756 vlc_object_detach( p_vlc );
758 /* Release object before destroying it */
759 if( i_object ) vlc_object_release( p_vlc );
761 vlc_object_destroy( p_vlc );
763 /* Stop thread system: last one out please shut the door! */
764 vlc_threads_end( p_libvlc );
769 /*****************************************************************************
770 * VLC_Die: ask vlc to die.
771 *****************************************************************************
772 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
773 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
774 *****************************************************************************/
775 int VLC_Die( int i_object )
777 vlc_t *p_vlc = vlc_current_object( i_object );
784 p_vlc->b_die = VLC_TRUE;
786 if( i_object ) vlc_object_release( p_vlc );
790 /*****************************************************************************
791 * VLC_AddTarget: adds a target for playing.
792 *****************************************************************************
793 * This function adds psz_target to the current playlist. If a playlist does
794 * not exist, it will create one.
795 *****************************************************************************/
796 int VLC_AddTarget( int i_object, char const *psz_target,
797 char const **ppsz_options, int i_options,
798 int i_mode, int i_pos )
802 playlist_t *p_playlist;
803 vlc_t *p_vlc = vlc_current_object( i_object );
810 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
812 if( p_playlist == NULL )
814 msg_Dbg( p_vlc, "no playlist present, creating one" );
815 p_playlist = playlist_Create( p_vlc );
817 if( p_playlist == NULL )
819 if( i_object ) vlc_object_release( p_vlc );
823 vlc_object_yield( p_playlist );
826 i_err = playlist_Add( p_playlist, psz_target, psz_target,
829 for( i = 0 ; i< i_options ; i++ )
831 playlist_AddOption( p_playlist, i_err , ppsz_options[i] );
834 vlc_object_release( p_playlist );
836 if( i_object ) vlc_object_release( p_vlc );
840 /*****************************************************************************
841 * VLC_Set: set a vlc variable
842 *****************************************************************************
844 *****************************************************************************/
845 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
847 vlc_t *p_vlc = vlc_current_object( i_object );
855 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
856 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
857 if( !strncmp( psz_var, "conf::", 6 ) )
859 module_config_t *p_item;
860 char const *psz_newvar = psz_var + 6;
862 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
866 switch( p_item->i_type )
868 case CONFIG_ITEM_BOOL:
869 config_PutInt( p_vlc, psz_newvar, value.b_bool );
871 case CONFIG_ITEM_INTEGER:
872 config_PutInt( p_vlc, psz_newvar, value.i_int );
874 case CONFIG_ITEM_FLOAT:
875 config_PutFloat( p_vlc, psz_newvar, value.f_float );
878 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
881 if( i_object ) vlc_object_release( p_vlc );
886 i_ret = var_Set( p_vlc, psz_var, value );
888 if( i_object ) vlc_object_release( p_vlc );
892 /*****************************************************************************
893 * VLC_Get: get a vlc variable
894 *****************************************************************************
896 *****************************************************************************/
897 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
899 vlc_t *p_vlc = vlc_current_object( i_object );
907 i_ret = var_Get( p_vlc, psz_var, p_value );
909 if( i_object ) vlc_object_release( p_vlc );
913 /* FIXME: temporary hacks */
916 /*****************************************************************************
917 * VLC_IsPlaying: Query for Playlist Status
918 *****************************************************************************/
919 vlc_bool_t VLC_IsPlaying( int i_object )
922 playlist_t * p_playlist;
925 vlc_t *p_vlc = vlc_current_object( i_object );
927 /* Check that the handle is valid */
933 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
937 if( i_object ) vlc_object_release( p_vlc );
941 playing = playlist_IsPlaying( p_playlist );
943 vlc_object_release( p_playlist );
945 if( i_object ) vlc_object_release( p_vlc );
952 /*****************************************************************************
953 * VLC_ClearPlaylist: Query for Playlist Status
956 *****************************************************************************/
957 int VLC_ClearPlaylist( int i_object )
960 playlist_t * p_playlist;
961 vlc_t *p_vlc = vlc_current_object( i_object );
963 /* Check that the handle is valid */
969 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
973 if( i_object ) vlc_object_release( p_vlc );
977 playlist_Clear(p_playlist);
979 vlc_object_release( p_playlist );
981 if( i_object ) vlc_object_release( p_vlc );
986 /*****************************************************************************
988 *****************************************************************************/
989 int VLC_Play( int i_object )
991 playlist_t * p_playlist;
992 vlc_t *p_vlc = vlc_current_object( i_object );
994 /* Check that the handle is valid */
1000 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1004 if( i_object ) vlc_object_release( p_vlc );
1008 vlc_mutex_lock( &p_playlist->object_lock );
1009 if( p_playlist->i_size )
1011 vlc_mutex_unlock( &p_playlist->object_lock );
1012 playlist_Play( p_playlist );
1016 vlc_mutex_unlock( &p_playlist->object_lock );
1019 vlc_object_release( p_playlist );
1021 if( i_object ) vlc_object_release( p_vlc );
1025 /*****************************************************************************
1027 *****************************************************************************/
1028 int VLC_Stop( int i_object )
1030 intf_thread_t * p_intf;
1031 playlist_t * p_playlist;
1032 vout_thread_t * p_vout;
1033 aout_instance_t * p_aout;
1034 vlc_t *p_vlc = vlc_current_object( i_object );
1036 /* Check that the handle is valid */
1043 * Ask the interfaces to stop and destroy them
1045 msg_Dbg( p_vlc, "removing all interfaces" );
1047 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1049 intf_StopThread( p_intf );
1050 vlc_object_detach( p_intf );
1051 vlc_object_release( p_intf );
1052 intf_Destroy( p_intf );
1059 msg_Dbg( p_vlc, "removing all playlists" );
1060 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1063 vlc_object_detach( p_playlist );
1064 vlc_object_release( p_playlist );
1065 playlist_Destroy( p_playlist );
1069 * Free video outputs
1071 msg_Dbg( p_vlc, "removing all video outputs" );
1072 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1074 vlc_object_detach( p_vout );
1075 vlc_object_release( p_vout );
1076 vout_Destroy( p_vout );
1080 * Free audio outputs
1082 msg_Dbg( p_vlc, "removing all audio outputs" );
1083 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1085 vlc_object_detach( (vlc_object_t *)p_aout );
1086 vlc_object_release( (vlc_object_t *)p_aout );
1087 aout_Delete( p_aout );
1090 if( i_object ) vlc_object_release( p_vlc );
1094 /*****************************************************************************
1095 * VLC_Pause: toggle pause
1096 *****************************************************************************/
1097 int VLC_Pause( int i_object )
1099 input_thread_t *p_input;
1100 vlc_t *p_vlc = vlc_current_object( i_object );
1107 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1111 if( i_object ) vlc_object_release( p_vlc );
1115 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1116 vlc_object_release( p_input );
1118 if( i_object ) vlc_object_release( p_vlc );
1122 /*****************************************************************************
1123 * VLC_FullScreen: toggle fullscreen mode
1124 *****************************************************************************/
1125 int VLC_FullScreen( int i_object )
1127 vout_thread_t *p_vout;
1128 vlc_t *p_vlc = vlc_current_object( i_object );
1135 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1139 if( i_object ) vlc_object_release( p_vlc );
1143 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1144 vlc_object_release( p_vout );
1146 if( i_object ) vlc_object_release( p_vlc );
1150 /* following functions are local */
1152 /*****************************************************************************
1153 * SetLanguage: set the interface language.
1154 *****************************************************************************
1155 * We set the LC_MESSAGES locale category for interface messages and buttons,
1156 * as well as the LC_CTYPE category for string sorting and possible wide
1157 * character support.
1158 *****************************************************************************/
1159 static void SetLanguage ( char const *psz_lang )
1161 #if defined( ENABLE_NLS ) \
1162 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1165 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1169 if( psz_lang && !*psz_lang )
1171 # if defined( HAVE_LC_MESSAGES )
1172 setlocale( LC_MESSAGES, psz_lang );
1174 setlocale( LC_CTYPE, psz_lang );
1179 /* I need that under Darwin, please check it doesn't disturb
1180 * other platforms. --Meuuh */
1181 setenv( "LANG", psz_lang, 1 );
1183 #elif defined( SYS_BEOS ) || defined( WIN32 )
1184 /* We set LC_ALL manually because it is the only way to set
1185 * the language at runtime under eg. Windows. Beware that this
1186 * makes the environment unconsistent when libvlc is unloaded and
1187 * should probably be moved to a safer place like vlc.c. */
1188 static char psz_lcall[20];
1189 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1190 psz_lcall[19] = '\0';
1191 putenv( psz_lcall );
1194 setlocale( LC_ALL, psz_lang );
1197 /* Specify where to find the locales for current domain */
1198 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1199 psz_path = LOCALEDIR;
1201 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1205 if( !bindtextdomain( PACKAGE, psz_path ) )
1207 fprintf( stderr, "warning: no domain %s in directory %s\n",
1208 PACKAGE, psz_path );
1211 /* Set the default domain */
1212 textdomain( PACKAGE );
1214 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1215 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1216 /* BeOS only support UTF8 strings */
1217 /* Mac OS X prefers UTF8 */
1218 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1224 /*****************************************************************************
1225 * GetFilenames: parse command line options which are not flags
1226 *****************************************************************************
1227 * Parse command line for input files as well as their associated options.
1228 * An option always follows its associated input and begins with a ":".
1229 *****************************************************************************/
1230 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1232 int i_opt, i_options;
1234 /* We assume that the remaining parameters are filenames
1235 * and their input options */
1236 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1240 /* Count the input options */
1241 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1247 /* TODO: write an internal function of this one, to avoid
1248 * unnecessary lookups. */
1249 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1250 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1252 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1259 /*****************************************************************************
1260 * Usage: print program usage
1261 *****************************************************************************
1262 * Print a short inline help. Message interface is initialized at this stage.
1263 *****************************************************************************/
1264 static void Usage( vlc_t *p_this, char const *psz_module_name )
1266 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1267 /* short option ------' | | | | | | |
1268 * option name ------------' | | | | | |
1269 * <bra -------------------------' | | | | |
1270 * option type or "" --------------' | | | |
1271 * ket> -----------------------------' | | |
1272 * padding spaces ---------------------' | |
1273 * comment -------------------------------' |
1274 * comment suffix --------------------------'
1276 * The purpose of having bra and ket is that we might i18n them as well.
1278 #define LINE_START 8
1279 #define PADDING_SPACES 25
1282 module_config_t *p_item;
1283 char psz_spaces[PADDING_SPACES+LINE_START+1];
1284 char psz_format[sizeof(FORMAT_STRING)];
1285 char psz_buffer[1000];
1288 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1289 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1291 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1292 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1294 strcpy( psz_format, FORMAT_STRING );
1300 /* List all modules */
1301 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1303 /* Enumerate the config for each module */
1304 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1306 vlc_bool_t b_help_module;
1308 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1310 if( psz_module_name && strcmp( psz_module_name,
1311 p_parser->psz_object_name ) )
1316 /* Ignore modules without config options */
1317 if( !p_parser->i_config_items )
1322 /* Ignore modules with only advanced config options if requested */
1325 for( p_item = p_parser->p_config;
1326 p_item->i_type != CONFIG_HINT_END;
1329 if( (p_item->i_type & CONFIG_ITEM) &&
1330 !p_item->b_advanced ) break;
1332 if( p_item->i_type == CONFIG_HINT_END ) continue;
1335 /* Print name of module */
1336 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1338 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1340 /* Print module options */
1341 for( p_item = p_parser->p_config;
1342 p_item->i_type != CONFIG_HINT_END;
1346 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1347 char *psz_suf = "", *psz_prefix = NULL;
1350 /* Skip advanced options if requested */
1351 if( p_item->b_advanced && !b_advanced )
1356 switch( p_item->i_type )
1358 case CONFIG_HINT_CATEGORY:
1359 case CONFIG_HINT_USAGE:
1360 if( !strcmp( "main", p_parser->psz_object_name ) )
1361 fprintf( stdout, "\n %s\n", p_item->psz_text );
1364 case CONFIG_ITEM_STRING:
1365 case CONFIG_ITEM_FILE:
1366 case CONFIG_ITEM_DIRECTORY:
1367 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1368 if( !p_item->ppsz_list )
1370 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1376 psz_type = psz_buffer;
1378 for( i = 0; p_item->ppsz_list[i]; i++ )
1380 if( i ) strcat( psz_type, "," );
1381 strcat( psz_type, p_item->ppsz_list[i] );
1386 case CONFIG_ITEM_INTEGER:
1387 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1388 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1390 case CONFIG_ITEM_FLOAT:
1391 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1393 case CONFIG_ITEM_BOOL:
1394 psz_bra = ""; psz_type = ""; psz_ket = "";
1395 if( !b_help_module )
1397 psz_suf = p_item->i_value ? _(" (default enabled)") :
1398 _(" (default disabled)");
1408 /* Add short option if any */
1409 if( p_item->i_short )
1411 sprintf( psz_short, "-%c,", p_item->i_short );
1415 strcpy( psz_short, " " );
1418 i = PADDING_SPACES - strlen( p_item->psz_name )
1419 - strlen( psz_bra ) - strlen( psz_type )
1420 - strlen( psz_ket ) - 1;
1422 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1424 /* If option is of type --foo-bar, we print its counterpart
1425 * as --no-foo-bar, but if it is of type --foobar (without
1426 * dashes in the name) we print it as --nofoobar. Both
1427 * values are of course valid, only the display changes. */
1428 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1430 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1435 psz_spaces[0] = '\n';
1440 psz_spaces[i] = '\0';
1443 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1445 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1446 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1447 psz_ket, psz_spaces );
1451 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1452 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1455 psz_spaces[i] = ' ';
1457 /* We wrap the rest of the output */
1458 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1459 psz_text = psz_buffer;
1462 char *psz_parser, *psz_word;
1463 int i_end = strlen( psz_text );
1465 /* If the remaining text fits in a line, print it. */
1466 if( i_end <= i_width )
1468 fprintf( stdout, "%s\n", psz_text );
1472 /* Otherwise, eat as many words as possible */
1473 psz_parser = psz_text;
1476 psz_word = psz_parser;
1477 psz_parser = strchr( psz_word, ' ' );
1478 /* If no space was found, we reached the end of the text
1479 * block; otherwise, we skip the space we just found. */
1480 psz_parser = psz_parser ? psz_parser + 1
1483 } while( psz_parser - psz_text <= i_width );
1485 /* We cut a word in one of these cases:
1486 * - it's the only word in the line and it's too long.
1487 * - we used less than 80% of the width and the word we are
1488 * going to wrap is longer than 40% of the width, and even
1489 * if the word would have fit in the next line. */
1490 if( psz_word == psz_text
1491 || ( psz_word - psz_text < 80 * i_width / 100
1492 && psz_parser - psz_word > 40 * i_width / 100 ) )
1494 char c = psz_text[i_width];
1495 psz_text[i_width] = '\0';
1496 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1497 psz_text += i_width;
1502 psz_word[-1] = '\0';
1503 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1504 psz_text = psz_word;
1510 /* Release the module list */
1511 vlc_list_release( p_list );
1513 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1514 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1519 /*****************************************************************************
1520 * ListModules: list the available modules with their description
1521 *****************************************************************************
1522 * Print a list of all available modules (builtins and plugins) and a short
1523 * description for each one.
1524 *****************************************************************************/
1525 static void ListModules( vlc_t *p_this )
1529 char psz_spaces[22];
1532 memset( psz_spaces, ' ', 22 );
1539 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1540 p_this->p_vlc->psz_object_name );
1542 fprintf( stdout, _("[module] [description]\n") );
1544 /* List all modules */
1545 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1547 /* Enumerate each module */
1548 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1552 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1554 /* Nasty hack, but right now I'm too tired to think about a nice
1556 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1560 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1561 psz_spaces, p_parser->psz_longname );
1563 psz_spaces[i] = ' ';
1566 vlc_list_release( p_list );
1568 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1569 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1574 /*****************************************************************************
1575 * Version: print complete program version
1576 *****************************************************************************
1577 * Print complete program version and build number.
1578 *****************************************************************************/
1579 static void Version( void )
1585 fprintf( stdout, VERSION_MESSAGE "\n" );
1587 _("This program comes with NO WARRANTY, to the extent permitted by "
1588 "law.\nYou may redistribute it under the terms of the GNU General "
1589 "Public License;\nsee the file named COPYING for details.\n"
1590 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1592 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1593 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1598 /*****************************************************************************
1599 * ShowConsole: On Win32, create an output console for debug messages
1600 *****************************************************************************
1601 * This function is useful only on Win32.
1602 *****************************************************************************/
1604 static void ShowConsole( void )
1608 freopen( "CONOUT$", "w", stdout );
1609 freopen( "CONOUT$", "w", stderr );
1610 freopen( "CONIN$", "r", stdin );
1616 /*****************************************************************************
1617 * ConsoleWidth: Return the console width in characters
1618 *****************************************************************************
1619 * We use the stty shell command to get the console width; if this fails or
1620 * if the width is less than 80, we default to 80.
1621 *****************************************************************************/
1622 static int ConsoleWidth( void )
1627 char buf[20], *psz_parser;
1631 file = popen( "stty size 2>/dev/null", "r" );
1634 i_ret = fread( buf, 1, 20, file );
1638 psz_parser = strchr( buf, ' ' );
1641 i_ret = atoi( psz_parser + 1 );
1656 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1657 vlc_value_t old_val, vlc_value_t new_val, void *param)
1659 vlc_t *p_vlc = (vlc_t *)p_this;
1661 if( new_val.i_int >= -1 )
1663 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );