1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: libvlc.c,v 1.108 2004/01/06 08:50:20 zorglub 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 );
103 /*****************************************************************************
104 * vlc_current_object: return the current object.
105 *****************************************************************************
106 * If i_object is non-zero, return the corresponding object. Otherwise,
107 * return the statically allocated p_vlc object.
108 *****************************************************************************/
109 vlc_t * vlc_current_object( int i_object )
113 return vlc_object_get( p_libvlc, i_object );
119 /*****************************************************************************
120 * VLC_Version: return the libvlc version.
121 *****************************************************************************
122 * This function returns full version string (numeric version and codename).
123 *****************************************************************************/
124 char const * VLC_Version( void )
126 return VERSION_MESSAGE;
129 /*****************************************************************************
130 * VLC_Error: strerror() equivalent
131 *****************************************************************************
132 * This function returns full version string (numeric version and codename).
133 *****************************************************************************/
134 char const * VLC_Error( int i_err )
136 return vlc_error( i_err );
139 /*****************************************************************************
140 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
141 *****************************************************************************
142 * This function allocates a vlc_t structure and returns a negative value
143 * in case of failure. Also, the thread system is initialized.
144 *****************************************************************************/
145 int VLC_Create( void )
148 vlc_t * p_vlc = NULL;
151 /* &libvlc never changes, so we can safely call this multiple times. */
154 /* vlc_threads_init *must* be the first internal call! No other call is
155 * allowed before the thread system has been initialized. */
156 i_ret = vlc_threads_init( p_libvlc );
162 /* Now that the thread system is initialized, we don't have much, but
163 * at least we have var_Create */
164 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
165 var_Get( p_libvlc, "libvlc", &lockval );
166 vlc_mutex_lock( lockval.p_address );
167 if( !libvlc.b_ready )
171 /* Guess what CPU we have */
172 libvlc.i_cpu = CPUCapabilities();
174 /* Find verbosity from VLC_VERBOSE environment variable */
175 psz_env = getenv( "VLC_VERBOSE" );
176 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
178 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
179 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
181 libvlc.b_color = VLC_FALSE;
184 /* Initialize message queue */
185 msg_Create( p_libvlc );
187 /* Announce who we are */
188 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
189 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
191 /* The module bank will be initialized later */
192 libvlc.p_module_bank = NULL;
194 libvlc.b_ready = VLC_TRUE;
196 vlc_mutex_unlock( lockval.p_address );
197 var_Destroy( p_libvlc, "libvlc" );
199 /* Allocate a vlc object */
200 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
205 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
207 p_vlc->psz_object_name = "root";
209 /* Initialize mutexes */
210 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
212 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
215 /* Store our newly allocated structure in the global list */
216 vlc_object_attach( p_vlc, p_libvlc );
218 /* Store data for the non-reentrant API */
219 p_static_vlc = p_vlc;
221 return p_vlc->i_object_id;
224 /*****************************************************************************
225 * VLC_Init: initialize a vlc_t structure.
226 *****************************************************************************
227 * This function initializes a previously allocated vlc_t structure:
229 * - gettext initialization
230 * - message queue, module bank and playlist initialization
231 * - configuration and commandline parsing
232 *****************************************************************************/
233 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
235 char p_capabilities[200];
240 vlc_bool_t b_exit = VLC_FALSE;
241 vlc_t * p_vlc = vlc_current_object( i_object );
242 module_t *p_help_module;
243 playlist_t *p_playlist;
252 * System specific initialization code
254 system_Init( p_vlc, &i_argc, ppsz_argv );
256 /* Get the executable name (similar to the basename command) */
259 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
262 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
268 p_vlc->psz_object_name = "vlc";
272 * Support for gettext
276 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
277 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
279 /* Initialize the module bank and load the configuration of the
280 * main module. We need to do this at this stage to be able to display
281 * a short help if required by the user. (short help == main module
283 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
284 var_Get( p_libvlc, "libvlc", &lockval );
285 vlc_mutex_lock( lockval.p_address );
286 if( libvlc.p_module_bank == NULL )
288 module_InitBank( p_vlc );
289 module_LoadMain( p_vlc );
291 vlc_mutex_unlock( lockval.p_address );
292 var_Destroy( p_libvlc, "libvlc" );
294 /* Hack: insert the help module here */
295 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
296 if( p_help_module == NULL )
298 /*module_EndBank( p_vlc );*/
299 if( i_object ) vlc_object_release( p_vlc );
302 p_help_module->psz_object_name = "help";
303 config_Duplicate( p_help_module, p_help_config );
304 vlc_object_attach( p_help_module, libvlc.p_module_bank );
307 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
309 vlc_object_detach( p_help_module );
310 config_Free( p_help_module );
311 vlc_object_destroy( p_help_module );
312 /*module_EndBank( p_vlc );*/
313 if( i_object ) vlc_object_release( p_vlc );
317 /* Check for short help option */
318 if( config_GetInt( p_vlc, "help" ) )
320 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
321 p_vlc->psz_object_name );
322 Usage( p_vlc, "main" );
323 Usage( p_vlc, "help" );
326 /* Check for version option */
327 else if( config_GetInt( p_vlc, "version" ) )
333 /* Set the config file stuff */
334 p_vlc->psz_homedir = config_GetHomeDir();
335 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
337 /* Hack: remove the help module here */
338 vlc_object_detach( p_help_module );
343 config_Free( p_help_module );
344 vlc_object_destroy( p_help_module );
345 /*module_EndBank( p_vlc );*/
346 if( i_object ) vlc_object_release( p_vlc );
350 /* Check for translation config option */
351 #if defined( ENABLE_NLS ) \
352 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
354 /* This ain't really nice to have to reload the config here but it seems
355 * the only way to do it. */
356 config_LoadConfigFile( p_vlc, "main" );
357 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
359 /* Check if the user specified a custom language */
360 psz_language = config_GetPsz( p_vlc, "language" );
361 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
363 /* Reset the default domain */
364 SetLanguage( psz_language );
366 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
367 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
369 textdomain( PACKAGE );
371 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
372 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
373 /* BeOS only support UTF8 strings */
374 /* Mac OS X prefers UTF8 */
375 bind_textdomain_codeset( PACKAGE, "UTF-8" );
378 module_EndBank( p_vlc );
379 module_InitBank( p_vlc );
380 module_LoadMain( p_vlc );
381 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
383 if( psz_language ) free( psz_language );
387 * Load the builtins and plugins into the module_bank.
388 * We have to do it before config_Load*() because this also gets the
389 * list of configuration options exported by each module and loads their
392 module_LoadBuiltins( p_vlc );
393 module_LoadPlugins( p_vlc );
399 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
400 libvlc.p_module_bank->i_children );
402 /* Hack: insert the help module here */
403 vlc_object_attach( p_help_module, libvlc.p_module_bank );
406 /* Check for help on modules */
407 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
409 Usage( p_vlc, p_tmp );
413 /* Check for long help option */
414 else if( config_GetInt( p_vlc, "longhelp" ) )
416 Usage( p_vlc, NULL );
419 /* Check for module list option */
420 else if( config_GetInt( p_vlc, "list" ) )
422 ListModules( p_vlc );
426 /* Check for config file options */
427 if( config_GetInt( p_vlc, "reset-config" ) )
429 vlc_object_detach( p_help_module );
430 config_ResetAll( p_vlc );
431 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
432 config_SaveConfigFile( p_vlc, NULL );
433 vlc_object_attach( p_help_module, libvlc.p_module_bank );
435 if( config_GetInt( p_vlc, "save-config" ) )
437 vlc_object_detach( p_help_module );
438 config_LoadConfigFile( p_vlc, NULL );
439 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
440 config_SaveConfigFile( p_vlc, NULL );
441 vlc_object_attach( p_help_module, libvlc.p_module_bank );
444 /* Hack: remove the help module here */
445 vlc_object_detach( p_help_module );
450 config_Free( p_help_module );
451 vlc_object_destroy( p_help_module );
452 /*module_EndBank( p_vlc );*/
453 if( i_object ) vlc_object_release( p_vlc );
458 * Override default configuration with config file settings
460 config_LoadConfigFile( p_vlc, NULL );
462 /* Hack: insert the help module here */
463 vlc_object_attach( p_help_module, libvlc.p_module_bank );
467 * Override configuration with command line settings
469 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
473 /* Pause the console because it's destroyed when we exit */
474 fprintf( stderr, "The command line options couldn't be loaded, check "
475 "that they are valid.\nPress the RETURN key to continue..." );
478 vlc_object_detach( p_help_module );
479 config_Free( p_help_module );
480 vlc_object_destroy( p_help_module );
481 /*module_EndBank( p_vlc );*/
482 if( i_object ) vlc_object_release( p_vlc );
486 /* Hack: remove the help module here */
487 vlc_object_detach( p_help_module );
488 config_Free( p_help_module );
489 vlc_object_destroy( p_help_module );
493 * System specific configuration
495 system_Configure( p_vlc, &i_argc, ppsz_argv );
498 * Message queue options
500 if( config_GetInt( p_vlc, "quiet" ) )
502 libvlc.i_verbose = -1;
506 int i_tmp = config_GetInt( p_vlc, "verbose" );
509 libvlc.i_verbose = __MIN( i_tmp, 2 );
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;
531 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
532 if( !config_GetInt( p_vlc, "altivec" ) )
533 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
536 #define PRINT_CAPABILITY( capability, string ) \
537 if( libvlc.i_cpu & capability ) \
539 strncat( p_capabilities, string " ", \
540 sizeof(p_capabilities) - strlen(p_capabilities) ); \
541 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
544 p_capabilities[0] = '\0';
545 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
546 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
547 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
548 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
549 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
554 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
557 * Choose the best memcpy module
559 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
561 if( p_vlc->pf_memcpy == NULL )
563 p_vlc->pf_memcpy = memcpy;
566 if( p_vlc->pf_memset == NULL )
568 p_vlc->pf_memset = memset;
572 * Initialize hotkey handling
574 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
575 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
576 /* Do a copy (we don't need to modify the strings) */
577 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
580 * Initialize playlist and get commandline files
582 p_playlist = playlist_Create( p_vlc );
585 msg_Err( p_vlc, "playlist initialization failed" );
586 if( p_vlc->p_memcpy_module != NULL )
588 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
590 /*module_EndBank( p_vlc );*/
591 if( i_object ) vlc_object_release( p_vlc );
596 * Load background interfaces
598 psz_modules = config_GetPsz( p_vlc, "extraintf" );
599 psz_parser = psz_modules;
600 while ( psz_parser && *psz_parser )
602 char *psz_module, *psz_temp;
603 psz_module = psz_parser;
604 psz_parser = strchr( psz_module, ',' );
610 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
613 sprintf( psz_temp, "%s,none", psz_module );
614 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
624 * Allways load the hotkeys interface if it exists
626 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
629 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
631 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
632 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
633 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
634 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
635 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
645 * Get input filenames given as commandline arguments
647 GetFilenames( p_vlc, i_argc, ppsz_argv );
649 if( i_object ) vlc_object_release( p_vlc );
653 /*****************************************************************************
654 * VLC_AddIntf: add an interface
655 *****************************************************************************
656 * This function opens an interface plugin and runs it. If b_block is set
657 * to 0, VLC_AddIntf will return immediately and let the interface run in a
658 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
659 * user requests to quit.
660 *****************************************************************************/
661 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
664 intf_thread_t *p_intf;
665 vlc_t *p_vlc = vlc_current_object( i_object );
672 /* Try to create the interface */
673 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
677 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
678 if( i_object ) vlc_object_release( p_vlc );
682 /* Try to run the interface */
683 p_intf->b_block = b_block;
684 i_err = intf_RunThread( p_intf );
687 vlc_object_detach( p_intf );
688 intf_Destroy( p_intf );
689 if( i_object ) vlc_object_release( p_vlc );
693 if( i_object ) vlc_object_release( p_vlc );
697 /*****************************************************************************
698 * VLC_Destroy: stop playing and destroy everything.
699 *****************************************************************************
700 * This function requests the running threads to finish, waits for their
701 * termination, and destroys their structure.
702 *****************************************************************************/
703 int VLC_Destroy( int i_object )
705 vlc_t *p_vlc = vlc_current_object( i_object );
713 * Free allocated memory
715 if( p_vlc->p_memcpy_module )
717 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
718 p_vlc->p_memcpy_module = NULL;
721 if( p_vlc->psz_homedir )
723 free( p_vlc->psz_homedir );
724 p_vlc->psz_homedir = NULL;
727 if( p_vlc->psz_configfile )
729 free( p_vlc->psz_configfile );
730 p_vlc->psz_configfile = NULL;
733 if( p_vlc->p_hotkeys )
735 free( p_vlc->p_hotkeys );
736 p_vlc->p_hotkeys = NULL;
740 * XXX: Free module bank !
742 /*module_EndBank( p_vlc );*/
745 * System specific cleaning code
749 /* Destroy mutexes */
750 vlc_mutex_destroy( &p_vlc->config_lock );
752 vlc_mutex_destroy( &p_vlc->quicktime_lock );
755 vlc_object_detach( p_vlc );
757 /* Release object before destroying it */
758 if( i_object ) vlc_object_release( p_vlc );
760 vlc_object_destroy( p_vlc );
762 /* Stop thread system: last one out please shut the door! */
763 vlc_threads_end( p_libvlc );
768 /*****************************************************************************
769 * VLC_Die: ask vlc to die.
770 *****************************************************************************
771 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
772 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
773 *****************************************************************************/
774 int VLC_Die( int i_object )
776 vlc_t *p_vlc = vlc_current_object( i_object );
783 p_vlc->b_die = VLC_TRUE;
785 if( i_object ) vlc_object_release( p_vlc );
789 /*****************************************************************************
790 * VLC_AddTarget: adds a target for playing.
791 *****************************************************************************
792 * This function adds psz_target to the current playlist. If a playlist does
793 * not exist, it will create one.
794 *****************************************************************************/
795 int VLC_AddTarget( int i_object, char const *psz_target,
796 char const **ppsz_options, int i_options,
797 int i_mode, int i_pos )
801 playlist_t *p_playlist;
802 vlc_t *p_vlc = vlc_current_object( i_object );
809 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
811 if( p_playlist == NULL )
813 msg_Dbg( p_vlc, "no playlist present, creating one" );
814 p_playlist = playlist_Create( p_vlc );
816 if( p_playlist == NULL )
818 if( i_object ) vlc_object_release( p_vlc );
822 vlc_object_yield( p_playlist );
825 i_err = playlist_Add( p_playlist, psz_target, psz_target,
828 for( i = 0 ; i< i_options ; i++ )
830 playlist_AddOption( p_playlist, i_err , ppsz_options[i] );
833 vlc_object_release( p_playlist );
835 if( i_object ) vlc_object_release( p_vlc );
839 /*****************************************************************************
840 * VLC_Set: set a vlc variable
841 *****************************************************************************
843 *****************************************************************************/
844 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
846 vlc_t *p_vlc = vlc_current_object( i_object );
854 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
855 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
856 if( !strncmp( psz_var, "conf::", 6 ) )
858 module_config_t *p_item;
859 char const *psz_newvar = psz_var + 6;
861 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
865 switch( p_item->i_type )
867 case CONFIG_ITEM_BOOL:
868 config_PutInt( p_vlc, psz_newvar, value.b_bool );
870 case CONFIG_ITEM_INTEGER:
871 config_PutInt( p_vlc, psz_newvar, value.i_int );
873 case CONFIG_ITEM_FLOAT:
874 config_PutFloat( p_vlc, psz_newvar, value.f_float );
877 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
880 if( i_object ) vlc_object_release( p_vlc );
885 i_ret = var_Set( p_vlc, psz_var, value );
887 if( i_object ) vlc_object_release( p_vlc );
891 /*****************************************************************************
892 * VLC_Get: get a vlc variable
893 *****************************************************************************
895 *****************************************************************************/
896 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
898 vlc_t *p_vlc = vlc_current_object( i_object );
906 i_ret = var_Get( p_vlc, psz_var, p_value );
908 if( i_object ) vlc_object_release( p_vlc );
912 /* FIXME: temporary hacks */
915 /*****************************************************************************
916 * VLC_IsPlaying: Query for Playlist Status
917 *****************************************************************************/
918 vlc_bool_t VLC_IsPlaying( int i_object )
921 playlist_t * p_playlist;
924 vlc_t *p_vlc = vlc_current_object( i_object );
926 /* Check that the handle is valid */
932 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
936 if( i_object ) vlc_object_release( p_vlc );
940 playing = playlist_IsPlaying( p_playlist );
942 vlc_object_release( p_playlist );
944 if( i_object ) vlc_object_release( p_vlc );
951 /*****************************************************************************
952 * VLC_ClearPlaylist: Query for Playlist Status
955 *****************************************************************************/
956 int VLC_ClearPlaylist( int i_object )
959 playlist_t * p_playlist;
960 vlc_t *p_vlc = vlc_current_object( i_object );
962 /* Check that the handle is valid */
968 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
972 if( i_object ) vlc_object_release( p_vlc );
976 playlist_Clear(p_playlist);
978 vlc_object_release( p_playlist );
980 if( i_object ) vlc_object_release( p_vlc );
985 /*****************************************************************************
987 *****************************************************************************/
988 int VLC_Play( int i_object )
990 playlist_t * p_playlist;
991 vlc_t *p_vlc = vlc_current_object( i_object );
993 /* Check that the handle is valid */
999 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1003 if( i_object ) vlc_object_release( p_vlc );
1007 vlc_mutex_lock( &p_playlist->object_lock );
1008 if( p_playlist->i_size )
1010 vlc_mutex_unlock( &p_playlist->object_lock );
1011 playlist_Play( p_playlist );
1015 vlc_mutex_unlock( &p_playlist->object_lock );
1018 vlc_object_release( p_playlist );
1020 if( i_object ) vlc_object_release( p_vlc );
1024 /*****************************************************************************
1026 *****************************************************************************/
1027 int VLC_Stop( int i_object )
1029 intf_thread_t * p_intf;
1030 playlist_t * p_playlist;
1031 vout_thread_t * p_vout;
1032 aout_instance_t * p_aout;
1033 vlc_t *p_vlc = vlc_current_object( i_object );
1035 /* Check that the handle is valid */
1042 * Ask the interfaces to stop and destroy them
1044 msg_Dbg( p_vlc, "removing all interfaces" );
1046 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1048 intf_StopThread( p_intf );
1049 vlc_object_detach( p_intf );
1050 vlc_object_release( p_intf );
1051 intf_Destroy( p_intf );
1058 msg_Dbg( p_vlc, "removing all playlists" );
1059 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1062 vlc_object_detach( p_playlist );
1063 vlc_object_release( p_playlist );
1064 playlist_Destroy( p_playlist );
1068 * Free video outputs
1070 msg_Dbg( p_vlc, "removing all video outputs" );
1071 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1073 vlc_object_detach( p_vout );
1074 vlc_object_release( p_vout );
1075 vout_Destroy( p_vout );
1079 * Free audio outputs
1081 msg_Dbg( p_vlc, "removing all audio outputs" );
1082 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1084 vlc_object_detach( (vlc_object_t *)p_aout );
1085 vlc_object_release( (vlc_object_t *)p_aout );
1086 aout_Delete( p_aout );
1089 if( i_object ) vlc_object_release( p_vlc );
1093 /*****************************************************************************
1094 * VLC_Pause: toggle pause
1095 *****************************************************************************/
1096 int VLC_Pause( int i_object )
1098 input_thread_t *p_input;
1099 vlc_t *p_vlc = vlc_current_object( i_object );
1106 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1110 if( i_object ) vlc_object_release( p_vlc );
1114 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1115 vlc_object_release( p_input );
1117 if( i_object ) vlc_object_release( p_vlc );
1121 /*****************************************************************************
1122 * VLC_FullScreen: toggle fullscreen mode
1123 *****************************************************************************/
1124 int VLC_FullScreen( int i_object )
1126 vout_thread_t *p_vout;
1127 vlc_t *p_vlc = vlc_current_object( i_object );
1134 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1138 if( i_object ) vlc_object_release( p_vlc );
1142 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1143 vlc_object_release( p_vout );
1145 if( i_object ) vlc_object_release( p_vlc );
1149 /* following functions are local */
1151 /*****************************************************************************
1152 * SetLanguage: set the interface language.
1153 *****************************************************************************
1154 * We set the LC_MESSAGES locale category for interface messages and buttons,
1155 * as well as the LC_CTYPE category for string sorting and possible wide
1156 * character support.
1157 *****************************************************************************/
1158 static void SetLanguage ( char const *psz_lang )
1160 #if defined( ENABLE_NLS ) \
1161 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1164 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1168 if( psz_lang && !*psz_lang )
1170 # if defined( HAVE_LC_MESSAGES )
1171 setlocale( LC_MESSAGES, psz_lang );
1173 setlocale( LC_CTYPE, psz_lang );
1178 /* I need that under Darwin, please check it doesn't disturb
1179 * other platforms. --Meuuh */
1180 setenv( "LANG", psz_lang, 1 );
1182 #elif defined( SYS_BEOS ) || defined( WIN32 )
1183 /* We set LC_ALL manually because it is the only way to set
1184 * the language at runtime under eg. Windows. Beware that this
1185 * makes the environment unconsistent when libvlc is unloaded and
1186 * should probably be moved to a safer place like vlc.c. */
1187 static char psz_lcall[20];
1188 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1189 psz_lcall[19] = '\0';
1190 putenv( psz_lcall );
1193 setlocale( LC_ALL, psz_lang );
1196 /* Specify where to find the locales for current domain */
1197 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1198 psz_path = LOCALEDIR;
1200 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1204 if( !bindtextdomain( PACKAGE, psz_path ) )
1206 fprintf( stderr, "warning: no domain %s in directory %s\n",
1207 PACKAGE, psz_path );
1210 /* Set the default domain */
1211 textdomain( PACKAGE );
1213 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1214 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1215 /* BeOS only support UTF8 strings */
1216 /* Mac OS X prefers UTF8 */
1217 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1223 /*****************************************************************************
1224 * GetFilenames: parse command line options which are not flags
1225 *****************************************************************************
1226 * Parse command line for input files as well as their associated options.
1227 * An option always follows its associated input and begins with a ":".
1228 *****************************************************************************/
1229 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1231 int i_opt, i_options;
1233 /* We assume that the remaining parameters are filenames
1234 * and their input options */
1235 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1239 /* Count the input options */
1240 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1246 /* TODO: write an internal function of this one, to avoid
1247 * unnecessary lookups. */
1248 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1249 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1251 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1258 /*****************************************************************************
1259 * Usage: print program usage
1260 *****************************************************************************
1261 * Print a short inline help. Message interface is initialized at this stage.
1262 *****************************************************************************/
1263 static void Usage( vlc_t *p_this, char const *psz_module_name )
1265 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1266 /* short option ------' | | | | | | |
1267 * option name ------------' | | | | | |
1268 * <bra -------------------------' | | | | |
1269 * option type or "" --------------' | | | |
1270 * ket> -----------------------------' | | |
1271 * padding spaces ---------------------' | |
1272 * comment -------------------------------' |
1273 * comment suffix --------------------------'
1275 * The purpose of having bra and ket is that we might i18n them as well.
1277 #define LINE_START 8
1278 #define PADDING_SPACES 25
1281 module_config_t *p_item;
1282 char psz_spaces[PADDING_SPACES+LINE_START+1];
1283 char psz_format[sizeof(FORMAT_STRING)];
1284 char psz_buffer[1000];
1287 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1289 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1290 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1292 strcpy( psz_format, FORMAT_STRING );
1298 /* List all modules */
1299 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1301 /* Enumerate the config for each module */
1302 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1304 vlc_bool_t b_help_module;
1306 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1308 if( psz_module_name && strcmp( psz_module_name,
1309 p_parser->psz_object_name ) )
1314 /* Ignore modules without config options */
1315 if( !p_parser->i_config_items )
1320 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1322 /* Print module options */
1323 for( p_item = p_parser->p_config;
1324 p_item->i_type != CONFIG_HINT_END;
1328 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1329 char *psz_suf = "", *psz_prefix = NULL;
1331 if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1335 switch( p_item->i_type )
1337 case CONFIG_HINT_CATEGORY:
1338 case CONFIG_HINT_USAGE:
1339 fprintf( stdout, "\n %s\n", p_item->psz_text );
1342 case CONFIG_ITEM_STRING:
1343 case CONFIG_ITEM_FILE:
1344 case CONFIG_ITEM_DIRECTORY:
1345 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1346 if( !p_item->ppsz_list )
1348 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1354 psz_type = psz_buffer;
1356 for( i = 0; p_item->ppsz_list[i]; i++ )
1358 if( i ) strcat( psz_type, "," );
1359 strcat( psz_type, p_item->ppsz_list[i] );
1364 case CONFIG_ITEM_INTEGER:
1365 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1366 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1368 case CONFIG_ITEM_FLOAT:
1369 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1371 case CONFIG_ITEM_BOOL:
1372 psz_bra = ""; psz_type = ""; psz_ket = "";
1373 if( !b_help_module )
1375 psz_suf = p_item->i_value ? _(" (default enabled)") :
1376 _(" (default disabled)");
1386 /* Add short option if any */
1387 if( p_item->i_short )
1389 sprintf( psz_short, "-%c,", p_item->i_short );
1393 strcpy( psz_short, " " );
1396 i = PADDING_SPACES - strlen( p_item->psz_name )
1397 - strlen( psz_bra ) - strlen( psz_type )
1398 - strlen( psz_ket ) - 1;
1400 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1402 /* If option is of type --foo-bar, we print its counterpart
1403 * as --no-foo-bar, but if it is of type --foobar (without
1404 * dashes in the name) we print it as --nofoobar. Both
1405 * values are of course valid, only the display changes. */
1406 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1408 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1413 psz_spaces[0] = '\n';
1418 psz_spaces[i] = '\0';
1421 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1423 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1424 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1425 psz_ket, psz_spaces );
1429 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1430 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1433 psz_spaces[i] = ' ';
1435 /* We wrap the rest of the output */
1436 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1437 psz_text = psz_buffer;
1440 char *psz_parser, *psz_word;
1441 int i_end = strlen( psz_text );
1443 /* If the remaining text fits in a line, print it. */
1444 if( i_end <= i_width )
1446 fprintf( stdout, "%s\n", psz_text );
1450 /* Otherwise, eat as many words as possible */
1451 psz_parser = psz_text;
1454 psz_word = psz_parser;
1455 psz_parser = strchr( psz_word, ' ' );
1456 /* If no space was found, we reached the end of the text
1457 * block; otherwise, we skip the space we just found. */
1458 psz_parser = psz_parser ? psz_parser + 1
1461 } while( psz_parser - psz_text <= i_width );
1463 /* We cut a word in one of these cases:
1464 * - it's the only word in the line and it's too long.
1465 * - we used less than 80% of the width and the word we are
1466 * going to wrap is longer than 40% of the width, and even
1467 * if the word would have fit in the next line. */
1468 if( psz_word == psz_text
1469 || ( psz_word - psz_text < 80 * i_width / 100
1470 && psz_parser - psz_word > 40 * i_width / 100 ) )
1472 char c = psz_text[i_width];
1473 psz_text[i_width] = '\0';
1474 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1475 psz_text += i_width;
1480 psz_word[-1] = '\0';
1481 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1482 psz_text = psz_word;
1488 /* Release the module list */
1489 vlc_list_release( p_list );
1491 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1492 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1497 /*****************************************************************************
1498 * ListModules: list the available modules with their description
1499 *****************************************************************************
1500 * Print a list of all available modules (builtins and plugins) and a short
1501 * description for each one.
1502 *****************************************************************************/
1503 static void ListModules( vlc_t *p_this )
1507 char psz_spaces[22];
1510 memset( psz_spaces, ' ', 22 );
1517 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1518 p_this->p_vlc->psz_object_name );
1520 fprintf( stdout, _("[module] [description]\n") );
1522 /* List all modules */
1523 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1525 /* Enumerate each module */
1526 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1530 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1532 /* Nasty hack, but right now I'm too tired to think about a nice
1534 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1538 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1539 psz_spaces, p_parser->psz_longname );
1541 psz_spaces[i] = ' ';
1544 vlc_list_release( p_list );
1546 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1547 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1552 /*****************************************************************************
1553 * Version: print complete program version
1554 *****************************************************************************
1555 * Print complete program version and build number.
1556 *****************************************************************************/
1557 static void Version( void )
1563 fprintf( stdout, VERSION_MESSAGE "\n" );
1565 _("This program comes with NO WARRANTY, to the extent permitted by "
1566 "law.\nYou may redistribute it under the terms of the GNU General "
1567 "Public License;\nsee the file named COPYING for details.\n"
1568 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1570 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1571 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1576 /*****************************************************************************
1577 * ShowConsole: On Win32, create an output console for debug messages
1578 *****************************************************************************
1579 * This function is useful only on Win32.
1580 *****************************************************************************/
1582 static void ShowConsole( void )
1586 freopen( "CONOUT$", "w", stdout );
1587 freopen( "CONOUT$", "w", stderr );
1588 freopen( "CONIN$", "r", stdin );
1594 /*****************************************************************************
1595 * ConsoleWidth: Return the console width in characters
1596 *****************************************************************************
1597 * We use the stty shell command to get the console width; if this fails or
1598 * if the width is less than 80, we default to 80.
1599 *****************************************************************************/
1600 static int ConsoleWidth( void )
1605 char buf[20], *psz_parser;
1609 file = popen( "stty size 2>/dev/null", "r" );
1612 i_ret = fread( buf, 1, 20, file );
1616 psz_parser = strchr( buf, ' ' );
1619 i_ret = atoi( psz_parser + 1 );