1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: libvlc.c,v 1.104 2003/11/25 12:35:15 sam 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\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 )
800 playlist_t *p_playlist;
801 vlc_t *p_vlc = vlc_current_object( i_object );
808 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
810 if( p_playlist == NULL )
812 msg_Dbg( p_vlc, "no playlist present, creating one" );
813 p_playlist = playlist_Create( p_vlc );
815 if( p_playlist == NULL )
817 if( i_object ) vlc_object_release( p_vlc );
821 vlc_object_yield( p_playlist );
824 i_err = playlist_Add( p_playlist, psz_target, ppsz_options, i_options,
827 vlc_object_release( p_playlist );
829 if( i_object ) vlc_object_release( p_vlc );
833 /*****************************************************************************
834 * VLC_Set: set a vlc variable
835 *****************************************************************************
837 *****************************************************************************/
838 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
840 vlc_t *p_vlc = vlc_current_object( i_object );
848 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
849 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
850 if( !strncmp( psz_var, "conf::", 6 ) )
852 module_config_t *p_item;
853 char const *psz_newvar = psz_var + 6;
855 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
859 switch( p_item->i_type )
861 case CONFIG_ITEM_BOOL:
862 config_PutInt( p_vlc, psz_newvar, value.b_bool );
864 case CONFIG_ITEM_INTEGER:
865 config_PutInt( p_vlc, psz_newvar, value.i_int );
867 case CONFIG_ITEM_FLOAT:
868 config_PutFloat( p_vlc, psz_newvar, value.f_float );
871 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
874 if( i_object ) vlc_object_release( p_vlc );
879 i_ret = var_Set( p_vlc, psz_var, value );
881 if( i_object ) vlc_object_release( p_vlc );
885 /*****************************************************************************
886 * VLC_Get: get a vlc variable
887 *****************************************************************************
889 *****************************************************************************/
890 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
892 vlc_t *p_vlc = vlc_current_object( i_object );
900 i_ret = var_Get( p_vlc, psz_var, p_value );
902 if( i_object ) vlc_object_release( p_vlc );
906 /* FIXME: temporary hacks */
908 /*****************************************************************************
910 *****************************************************************************/
911 int VLC_Play( int i_object )
913 playlist_t * p_playlist;
914 vlc_t *p_vlc = vlc_current_object( i_object );
916 /* Check that the handle is valid */
922 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
926 if( i_object ) vlc_object_release( p_vlc );
930 vlc_mutex_lock( &p_playlist->object_lock );
931 if( p_playlist->i_size )
933 vlc_mutex_unlock( &p_playlist->object_lock );
934 playlist_Play( p_playlist );
938 vlc_mutex_unlock( &p_playlist->object_lock );
941 vlc_object_release( p_playlist );
943 if( i_object ) vlc_object_release( p_vlc );
947 /*****************************************************************************
949 *****************************************************************************/
950 int VLC_Stop( int i_object )
952 intf_thread_t * p_intf;
953 playlist_t * p_playlist;
954 vout_thread_t * p_vout;
955 aout_instance_t * p_aout;
956 vlc_t *p_vlc = vlc_current_object( i_object );
958 /* Check that the handle is valid */
965 * Ask the interfaces to stop and destroy them
967 msg_Dbg( p_vlc, "removing all interfaces" );
969 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
971 intf_StopThread( p_intf );
972 vlc_object_detach( p_intf );
973 vlc_object_release( p_intf );
974 intf_Destroy( p_intf );
981 msg_Dbg( p_vlc, "removing all playlists" );
982 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
985 vlc_object_detach( p_playlist );
986 vlc_object_release( p_playlist );
987 playlist_Destroy( p_playlist );
993 msg_Dbg( p_vlc, "removing all video outputs" );
994 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
996 vlc_object_detach( p_vout );
997 vlc_object_release( p_vout );
998 vout_Destroy( p_vout );
1002 * Free audio outputs
1004 msg_Dbg( p_vlc, "removing all audio outputs" );
1005 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1007 vlc_object_detach( (vlc_object_t *)p_aout );
1008 vlc_object_release( (vlc_object_t *)p_aout );
1009 aout_Delete( p_aout );
1012 if( i_object ) vlc_object_release( p_vlc );
1016 /*****************************************************************************
1017 * VLC_Pause: toggle pause
1018 *****************************************************************************/
1019 int VLC_Pause( int i_object )
1021 input_thread_t *p_input;
1022 vlc_t *p_vlc = vlc_current_object( i_object );
1029 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1033 if( i_object ) vlc_object_release( p_vlc );
1037 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1038 vlc_object_release( p_input );
1040 if( i_object ) vlc_object_release( p_vlc );
1044 /*****************************************************************************
1045 * VLC_FullScreen: toggle fullscreen mode
1046 *****************************************************************************/
1047 int VLC_FullScreen( int i_object )
1049 vout_thread_t *p_vout;
1050 vlc_t *p_vlc = vlc_current_object( i_object );
1057 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1061 if( i_object ) vlc_object_release( p_vlc );
1065 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1066 vlc_object_release( p_vout );
1068 if( i_object ) vlc_object_release( p_vlc );
1072 /* following functions are local */
1074 /*****************************************************************************
1075 * SetLanguage: set the interface language.
1076 *****************************************************************************
1077 * We set the LC_MESSAGES locale category for interface messages and buttons,
1078 * as well as the LC_CTYPE category for string sorting and possible wide
1079 * character support.
1080 *****************************************************************************/
1081 static void SetLanguage ( char const *psz_lang )
1083 #if defined( ENABLE_NLS ) \
1084 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1087 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1091 if( psz_lang && !*psz_lang )
1093 # if defined( HAVE_LC_MESSAGES )
1094 setlocale( LC_MESSAGES, psz_lang );
1096 setlocale( LC_CTYPE, psz_lang );
1101 /* I need that under Darwin, please check it doesn't disturb
1102 * other platforms. --Meuuh */
1103 setenv( "LANG", psz_lang, 1 );
1105 #elif defined( SYS_BEOS ) || defined( WIN32 )
1106 /* We set LC_ALL manually because it is the only way to set
1107 * the language at runtime under eg. Windows. Beware that this
1108 * makes the environment unconsistent when libvlc is unloaded and
1109 * should probably be moved to a safer place like vlc.c. */
1110 static char psz_lcall[20];
1111 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1112 psz_lcall[19] = '\0';
1113 putenv( psz_lcall );
1116 setlocale( LC_ALL, psz_lang );
1119 /* Specify where to find the locales for current domain */
1120 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1121 psz_path = LOCALEDIR;
1123 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1127 if( !bindtextdomain( PACKAGE, psz_path ) )
1129 fprintf( stderr, "warning: no domain %s in directory %s\n",
1130 PACKAGE, psz_path );
1133 /* Set the default domain */
1134 textdomain( PACKAGE );
1136 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1137 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1138 /* BeOS only support UTF8 strings */
1139 /* Mac OS X prefers UTF8 */
1140 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1146 /*****************************************************************************
1147 * GetFilenames: parse command line options which are not flags
1148 *****************************************************************************
1149 * Parse command line for input files as well as their associated options.
1150 * An option always follows its associated input and begins with a ":".
1151 *****************************************************************************/
1152 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1154 int i_opt, i_options;
1156 /* We assume that the remaining parameters are filenames
1157 * and their input options */
1158 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1162 /* Count the input options */
1163 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1169 /* TODO: write an internal function of this one, to avoid
1170 * unnecessary lookups. */
1171 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1172 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1174 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1181 /*****************************************************************************
1182 * Usage: print program usage
1183 *****************************************************************************
1184 * Print a short inline help. Message interface is initialized at this stage.
1185 *****************************************************************************/
1186 static void Usage( vlc_t *p_this, char const *psz_module_name )
1188 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1189 /* short option ------' | | | | | | |
1190 * option name ------------' | | | | | |
1191 * <bra -------------------------' | | | | |
1192 * option type or "" --------------' | | | |
1193 * ket> -----------------------------' | | |
1194 * padding spaces ---------------------' | |
1195 * comment -------------------------------' |
1196 * comment suffix --------------------------'
1198 * The purpose of having bra and ket is that we might i18n them as well.
1200 #define LINE_START 8
1201 #define PADDING_SPACES 25
1204 module_config_t *p_item;
1205 char psz_spaces[PADDING_SPACES+LINE_START+1];
1206 char psz_format[sizeof(FORMAT_STRING)];
1207 char psz_buffer[1000];
1210 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1212 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1213 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1215 strcpy( psz_format, FORMAT_STRING );
1221 /* List all modules */
1222 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1224 /* Enumerate the config for each module */
1225 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1227 vlc_bool_t b_help_module;
1229 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1231 if( psz_module_name && strcmp( psz_module_name,
1232 p_parser->psz_object_name ) )
1237 /* Ignore modules without config options */
1238 if( !p_parser->i_config_items )
1243 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1245 /* Print module options */
1246 for( p_item = p_parser->p_config;
1247 p_item->i_type != CONFIG_HINT_END;
1251 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1252 char *psz_suf = "", *psz_prefix = NULL;
1254 if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1258 switch( p_item->i_type )
1260 case CONFIG_HINT_CATEGORY:
1261 case CONFIG_HINT_USAGE:
1262 fprintf( stdout, " %s\n", p_item->psz_text );
1265 case CONFIG_ITEM_STRING:
1266 case CONFIG_ITEM_FILE:
1267 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1268 if( !p_item->ppsz_list )
1270 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1276 psz_type = psz_buffer;
1278 for( i = 0; p_item->ppsz_list[i]; i++ )
1280 if( i ) strcat( psz_type, "," );
1281 strcat( psz_type, p_item->ppsz_list[i] );
1286 case CONFIG_ITEM_INTEGER:
1287 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1289 case CONFIG_ITEM_FLOAT:
1290 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1292 case CONFIG_ITEM_BOOL:
1293 psz_bra = ""; psz_type = ""; psz_ket = "";
1294 if( !b_help_module )
1296 psz_suf = p_item->i_value ? _(" (default enabled)") :
1297 _(" (default disabled)");
1307 /* Add short option if any */
1308 if( p_item->i_short )
1310 sprintf( psz_short, "-%c,", p_item->i_short );
1314 strcpy( psz_short, " " );
1317 i = PADDING_SPACES - strlen( p_item->psz_name )
1318 - strlen( psz_bra ) - strlen( psz_type )
1319 - strlen( psz_ket ) - 1;
1321 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1323 /* If option is of type --foo-bar, we print its counterpart
1324 * as --no-foo-bar, but if it is of type --foobar (without
1325 * dashes in the name) we print it as --nofoobar. Both
1326 * values are of course valid, only the display changes. */
1327 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1329 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1334 psz_spaces[0] = '\n';
1339 psz_spaces[i] = '\0';
1342 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1344 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1345 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1346 psz_ket, psz_spaces );
1350 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1351 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1354 psz_spaces[i] = ' ';
1356 /* We wrap the rest of the output */
1357 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1358 psz_text = psz_buffer;
1361 char *psz_parser, *psz_word;
1362 int i_end = strlen( psz_text );
1364 /* If the remaining text fits in a line, print it. */
1365 if( i_end <= i_width )
1367 fprintf( stdout, "%s\n", psz_text );
1371 /* Otherwise, eat as many words as possible */
1372 psz_parser = psz_text;
1375 psz_word = psz_parser;
1376 psz_parser = strchr( psz_word, ' ' );
1377 /* If no space was found, we reached the end of the text
1378 * block; otherwise, we skip the space we just found. */
1379 psz_parser = psz_parser ? psz_parser + 1
1382 } while( psz_parser - psz_text <= i_width );
1384 /* We cut a word in one of these cases:
1385 * - it's the only word in the line and it's too long.
1386 * - we used less than 80% of the width and the word we are
1387 * going to wrap is longer than 40% of the width, and even
1388 * if the word would have fit in the next line. */
1389 if( psz_word == psz_text
1390 || ( psz_word - psz_text < 80 * i_width / 100
1391 && psz_parser - psz_word > 40 * i_width / 100 ) )
1393 char c = psz_text[i_width];
1394 psz_text[i_width] = '\0';
1395 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1396 psz_text += i_width;
1401 psz_word[-1] = '\0';
1402 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1403 psz_text = psz_word;
1409 /* Release the module list */
1410 vlc_list_release( p_list );
1412 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1413 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1418 /*****************************************************************************
1419 * ListModules: list the available modules with their description
1420 *****************************************************************************
1421 * Print a list of all available modules (builtins and plugins) and a short
1422 * description for each one.
1423 *****************************************************************************/
1424 static void ListModules( vlc_t *p_this )
1428 char psz_spaces[22];
1431 memset( psz_spaces, ' ', 22 );
1438 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1439 p_this->p_vlc->psz_object_name );
1441 fprintf( stdout, _("[module] [description]\n") );
1443 /* List all modules */
1444 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1446 /* Enumerate each module */
1447 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1451 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1453 /* Nasty hack, but right now I'm too tired to think about a nice
1455 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1459 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1460 psz_spaces, p_parser->psz_longname );
1462 psz_spaces[i] = ' ';
1465 vlc_list_release( p_list );
1467 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1468 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1473 /*****************************************************************************
1474 * Version: print complete program version
1475 *****************************************************************************
1476 * Print complete program version and build number.
1477 *****************************************************************************/
1478 static void Version( void )
1484 fprintf( stdout, VERSION_MESSAGE "\n" );
1486 _("This program comes with NO WARRANTY, to the extent permitted by "
1487 "law.\nYou may redistribute it under the terms of the GNU General "
1488 "Public License;\nsee the file named COPYING for details.\n"
1489 "Written by the VideoLAN team; see AUTHORS file.\n") );
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 * ShowConsole: On Win32, create an output console for debug messages
1499 *****************************************************************************
1500 * This function is useful only on Win32.
1501 *****************************************************************************/
1503 static void ShowConsole( void )
1507 freopen( "CONOUT$", "w", stdout );
1508 freopen( "CONOUT$", "w", stderr );
1509 freopen( "CONIN$", "r", stdin );
1515 /*****************************************************************************
1516 * ConsoleWidth: Return the console width in characters
1517 *****************************************************************************
1518 * We use the stty shell command to get the console width; if this fails or
1519 * if the width is less than 80, we default to 80.
1520 *****************************************************************************/
1521 static int ConsoleWidth( void )
1526 char buf[20], *psz_parser;
1530 file = popen( "stty size 2>/dev/null", "r" );
1533 i_ret = fread( buf, 1, 20, file );
1537 psz_parser = strchr( buf, ' ' );
1540 i_ret = atoi( psz_parser + 1 );