1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: libvlc.c,v 1.103 2003/11/25 00:56:34 fenrir 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_libvlc );
289 module_LoadMain( p_libvlc );
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_libvlc );
380 module_LoadMain( p_libvlc );
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_libvlc );
393 module_LoadPlugins( p_libvlc );
394 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
395 libvlc.p_module_bank->i_children );
397 /* Hack: insert the help module here */
398 vlc_object_attach( p_help_module, libvlc.p_module_bank );
401 /* Check for help on modules */
402 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
404 Usage( p_vlc, p_tmp );
408 /* Check for long help option */
409 else if( config_GetInt( p_vlc, "longhelp" ) )
411 Usage( p_vlc, NULL );
414 /* Check for module list option */
415 else if( config_GetInt( p_vlc, "list" ) )
417 ListModules( p_vlc );
421 /* Check for config file options */
422 if( config_GetInt( p_vlc, "reset-config" ) )
424 vlc_object_detach( p_help_module );
425 config_ResetAll( p_vlc );
426 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
427 config_SaveConfigFile( p_vlc, NULL );
428 vlc_object_attach( p_help_module, libvlc.p_module_bank );
430 if( config_GetInt( p_vlc, "save-config" ) )
432 vlc_object_detach( p_help_module );
433 config_LoadConfigFile( p_vlc, NULL );
434 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
435 config_SaveConfigFile( p_vlc, NULL );
436 vlc_object_attach( p_help_module, libvlc.p_module_bank );
439 /* Hack: remove the help module here */
440 vlc_object_detach( p_help_module );
445 config_Free( p_help_module );
446 vlc_object_destroy( p_help_module );
447 /*module_EndBank( p_vlc );*/
448 if( i_object ) vlc_object_release( p_vlc );
453 * Override default configuration with config file settings
455 config_LoadConfigFile( p_vlc, NULL );
457 /* Hack: insert the help module here */
458 vlc_object_attach( p_help_module, libvlc.p_module_bank );
462 * Override configuration with command line settings
464 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
468 /* Pause the console because it's destroyed when we exit */
469 fprintf( stderr, "The command line options couldn't be loaded, check "
470 "that they are valid.\nPress the RETURN key to continue..." );
473 vlc_object_detach( p_help_module );
474 config_Free( p_help_module );
475 vlc_object_destroy( p_help_module );
476 /*module_EndBank( p_vlc );*/
477 if( i_object ) vlc_object_release( p_vlc );
481 /* Hack: remove the help module here */
482 vlc_object_detach( p_help_module );
483 config_Free( p_help_module );
484 vlc_object_destroy( p_help_module );
488 * System specific configuration
490 system_Configure( p_vlc, &i_argc, ppsz_argv );
493 * Message queue options
495 if( config_GetInt( p_vlc, "quiet" ) )
497 libvlc.i_verbose = -1;
501 int i_tmp = config_GetInt( p_vlc, "verbose" );
504 libvlc.i_verbose = __MIN( i_tmp, 2 );
507 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
510 * Output messages that may still be in the queue
514 /* p_vlc initialization. FIXME ? */
516 #if defined( __i386__ )
517 if( !config_GetInt( p_vlc, "mmx" ) )
518 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
519 if( !config_GetInt( p_vlc, "3dn" ) )
520 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
521 if( !config_GetInt( p_vlc, "mmxext" ) )
522 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
523 if( !config_GetInt( p_vlc, "sse" ) )
524 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
526 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
527 if( !config_GetInt( p_vlc, "altivec" ) )
528 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
531 #define PRINT_CAPABILITY( capability, string ) \
532 if( libvlc.i_cpu & capability ) \
534 strncat( p_capabilities, string " ", \
535 sizeof(p_capabilities) - strlen(p_capabilities) ); \
536 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
539 p_capabilities[0] = '\0';
540 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
541 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
542 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
543 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
544 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
545 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
546 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
547 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
548 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
549 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
552 * Choose the best memcpy module
554 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
556 if( p_vlc->pf_memcpy == NULL )
558 p_vlc->pf_memcpy = memcpy;
561 if( p_vlc->pf_memset == NULL )
563 p_vlc->pf_memset = memset;
567 * Initialize hotkey handling
569 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
570 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
571 /* Do a copy (we don't need to modify the strings) */
572 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
575 * Initialize playlist and get commandline files
577 p_playlist = playlist_Create( p_vlc );
580 msg_Err( p_vlc, "playlist initialization failed" );
581 if( p_vlc->p_memcpy_module != NULL )
583 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
585 /*module_EndBank( p_vlc );*/
586 if( i_object ) vlc_object_release( p_vlc );
591 * Load background interfaces
593 psz_modules = config_GetPsz( p_vlc, "extraintf" );
594 psz_parser = psz_modules;
595 while ( psz_parser && *psz_parser )
597 char *psz_module, *psz_temp;
598 psz_module = psz_parser;
599 psz_parser = strchr( psz_module, ',' );
605 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
608 sprintf( psz_temp, "%s,none", psz_module );
609 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
619 * Allways load the hotkeys interface if it exists
621 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
624 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
626 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
627 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
628 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
629 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
630 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
631 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
632 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
633 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
634 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
635 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
640 * Get input filenames given as commandline arguments
642 GetFilenames( p_vlc, i_argc, ppsz_argv );
644 if( i_object ) vlc_object_release( p_vlc );
648 /*****************************************************************************
649 * VLC_AddIntf: add an interface
650 *****************************************************************************
651 * This function opens an interface plugin and runs it. If b_block is set
652 * to 0, VLC_AddIntf will return immediately and let the interface run in a
653 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
654 * user requests to quit.
655 *****************************************************************************/
656 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
659 intf_thread_t *p_intf;
660 vlc_t *p_vlc = vlc_current_object( i_object );
667 /* Try to create the interface */
668 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
672 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
673 if( i_object ) vlc_object_release( p_vlc );
677 /* Try to run the interface */
678 p_intf->b_block = b_block;
679 i_err = intf_RunThread( p_intf );
682 vlc_object_detach( p_intf );
683 intf_Destroy( p_intf );
684 if( i_object ) vlc_object_release( p_vlc );
688 if( i_object ) vlc_object_release( p_vlc );
692 /*****************************************************************************
693 * VLC_Destroy: stop playing and destroy everything.
694 *****************************************************************************
695 * This function requests the running threads to finish, waits for their
696 * termination, and destroys their structure.
697 *****************************************************************************/
698 int VLC_Destroy( int i_object )
700 vlc_t *p_vlc = vlc_current_object( i_object );
708 * Free allocated memory
710 if( p_vlc->p_memcpy_module )
712 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
713 p_vlc->p_memcpy_module = NULL;
716 if( p_vlc->psz_homedir )
718 free( p_vlc->psz_homedir );
719 p_vlc->psz_homedir = NULL;
722 if( p_vlc->psz_configfile )
724 free( p_vlc->psz_configfile );
725 p_vlc->psz_configfile = NULL;
728 if( p_vlc->p_hotkeys )
730 free( p_vlc->p_hotkeys );
731 p_vlc->p_hotkeys = NULL;
735 * XXX: Free module bank !
737 /*module_EndBank( p_vlc );*/
740 * System specific cleaning code
744 /* Destroy mutexes */
745 vlc_mutex_destroy( &p_vlc->config_lock );
747 vlc_mutex_destroy( &p_vlc->quicktime_lock );
750 vlc_object_detach( p_vlc );
752 /* Release object before destroying it */
753 if( i_object ) vlc_object_release( p_vlc );
755 vlc_object_destroy( p_vlc );
757 /* Stop thread system: last one out please shut the door! */
758 vlc_threads_end( p_libvlc );
763 /*****************************************************************************
764 * VLC_Die: ask vlc to die.
765 *****************************************************************************
766 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
767 * task. It is your duty to call vlc_end and VLC_Destroy afterwards.
768 *****************************************************************************/
769 int VLC_Die( int i_object )
771 vlc_t *p_vlc = vlc_current_object( i_object );
778 p_vlc->b_die = VLC_TRUE;
780 if( i_object ) vlc_object_release( p_vlc );
784 /*****************************************************************************
785 * VLC_AddTarget: adds a target for playing.
786 *****************************************************************************
787 * This function adds psz_target to the current playlist. If a playlist does
788 * not exist, it will create one.
789 *****************************************************************************/
790 int VLC_AddTarget( int i_object, char const *psz_target,
791 char const **ppsz_options, int i_options,
792 int i_mode, int i_pos )
795 playlist_t *p_playlist;
796 vlc_t *p_vlc = vlc_current_object( i_object );
803 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
805 if( p_playlist == NULL )
807 msg_Dbg( p_vlc, "no playlist present, creating one" );
808 p_playlist = playlist_Create( p_vlc );
810 if( p_playlist == NULL )
812 if( i_object ) vlc_object_release( p_vlc );
816 vlc_object_yield( p_playlist );
819 i_err = playlist_Add( p_playlist, psz_target, ppsz_options, i_options,
822 vlc_object_release( p_playlist );
824 if( i_object ) vlc_object_release( p_vlc );
828 /*****************************************************************************
829 * VLC_Set: set a vlc variable
830 *****************************************************************************
832 *****************************************************************************/
833 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
835 vlc_t *p_vlc = vlc_current_object( i_object );
843 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
844 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
845 if( !strncmp( psz_var, "conf::", 6 ) )
847 module_config_t *p_item;
848 char const *psz_newvar = psz_var + 6;
850 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
854 switch( p_item->i_type )
856 case CONFIG_ITEM_BOOL:
857 config_PutInt( p_vlc, psz_newvar, value.b_bool );
859 case CONFIG_ITEM_INTEGER:
860 config_PutInt( p_vlc, psz_newvar, value.i_int );
862 case CONFIG_ITEM_FLOAT:
863 config_PutFloat( p_vlc, psz_newvar, value.f_float );
866 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
869 if( i_object ) vlc_object_release( p_vlc );
874 i_ret = var_Set( p_vlc, psz_var, value );
876 if( i_object ) vlc_object_release( p_vlc );
880 /*****************************************************************************
881 * VLC_Get: get a vlc variable
882 *****************************************************************************
884 *****************************************************************************/
885 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
887 vlc_t *p_vlc = vlc_current_object( i_object );
895 i_ret = var_Get( p_vlc, psz_var, p_value );
897 if( i_object ) vlc_object_release( p_vlc );
901 /* FIXME: temporary hacks */
903 /*****************************************************************************
905 *****************************************************************************/
906 int VLC_Play( int i_object )
908 playlist_t * p_playlist;
909 vlc_t *p_vlc = vlc_current_object( i_object );
911 /* Check that the handle is valid */
917 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
921 if( i_object ) vlc_object_release( p_vlc );
925 vlc_mutex_lock( &p_playlist->object_lock );
926 if( p_playlist->i_size )
928 vlc_mutex_unlock( &p_playlist->object_lock );
929 playlist_Play( p_playlist );
933 vlc_mutex_unlock( &p_playlist->object_lock );
936 vlc_object_release( p_playlist );
938 if( i_object ) vlc_object_release( p_vlc );
942 /*****************************************************************************
944 *****************************************************************************/
945 int VLC_Stop( int i_object )
947 intf_thread_t * p_intf;
948 playlist_t * p_playlist;
949 vout_thread_t * p_vout;
950 aout_instance_t * p_aout;
951 vlc_t *p_vlc = vlc_current_object( i_object );
953 /* Check that the handle is valid */
960 * Ask the interfaces to stop and destroy them
962 msg_Dbg( p_vlc, "removing all interfaces" );
964 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
966 intf_StopThread( p_intf );
967 vlc_object_detach( p_intf );
968 vlc_object_release( p_intf );
969 intf_Destroy( p_intf );
976 msg_Dbg( p_vlc, "removing all playlists" );
977 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
980 vlc_object_detach( p_playlist );
981 vlc_object_release( p_playlist );
982 playlist_Destroy( p_playlist );
988 msg_Dbg( p_vlc, "removing all video outputs" );
989 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
991 vlc_object_detach( p_vout );
992 vlc_object_release( p_vout );
993 vout_Destroy( p_vout );
999 msg_Dbg( p_vlc, "removing all audio outputs" );
1000 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1002 vlc_object_detach( (vlc_object_t *)p_aout );
1003 vlc_object_release( (vlc_object_t *)p_aout );
1004 aout_Delete( p_aout );
1007 if( i_object ) vlc_object_release( p_vlc );
1011 /*****************************************************************************
1012 * VLC_Pause: toggle pause
1013 *****************************************************************************/
1014 int VLC_Pause( int i_object )
1016 input_thread_t *p_input;
1017 vlc_t *p_vlc = vlc_current_object( i_object );
1024 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1028 if( i_object ) vlc_object_release( p_vlc );
1032 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1033 vlc_object_release( p_input );
1035 if( i_object ) vlc_object_release( p_vlc );
1039 /*****************************************************************************
1040 * VLC_FullScreen: toggle fullscreen mode
1041 *****************************************************************************/
1042 int VLC_FullScreen( int i_object )
1044 vout_thread_t *p_vout;
1045 vlc_t *p_vlc = vlc_current_object( i_object );
1052 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1056 if( i_object ) vlc_object_release( p_vlc );
1060 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1061 vlc_object_release( p_vout );
1063 if( i_object ) vlc_object_release( p_vlc );
1067 /* following functions are local */
1069 /*****************************************************************************
1070 * SetLanguage: set the interface language.
1071 *****************************************************************************
1072 * We set the LC_MESSAGES locale category for interface messages and buttons,
1073 * as well as the LC_CTYPE category for string sorting and possible wide
1074 * character support.
1075 *****************************************************************************/
1076 static void SetLanguage ( char const *psz_lang )
1078 #if defined( ENABLE_NLS ) \
1079 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1082 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1086 if( psz_lang && !*psz_lang )
1088 # if defined( HAVE_LC_MESSAGES )
1089 setlocale( LC_MESSAGES, psz_lang );
1091 setlocale( LC_CTYPE, psz_lang );
1096 /* I need that under Darwin, please check it doesn't disturb
1097 * other platforms. --Meuuh */
1098 setenv( "LANG", psz_lang, 1 );
1100 #elif defined( SYS_BEOS ) || defined( WIN32 )
1101 /* We set LC_ALL manually because it is the only way to set
1102 * the language at runtime under eg. Windows. Beware that this
1103 * makes the environment unconsistent when libvlc is unloaded and
1104 * should probably be moved to a safer place like vlc.c. */
1105 static char psz_lcall[20];
1106 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1107 psz_lcall[19] = '\0';
1108 putenv( psz_lcall );
1111 setlocale( LC_ALL, psz_lang );
1114 /* Specify where to find the locales for current domain */
1115 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1116 psz_path = LOCALEDIR;
1118 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1122 if( !bindtextdomain( PACKAGE, psz_path ) )
1124 fprintf( stderr, "warning: no domain %s in directory %s\n",
1125 PACKAGE, psz_path );
1128 /* Set the default domain */
1129 textdomain( PACKAGE );
1131 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1132 ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1133 /* BeOS only support UTF8 strings */
1134 /* Mac OS X prefers UTF8 */
1135 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1141 /*****************************************************************************
1142 * GetFilenames: parse command line options which are not flags
1143 *****************************************************************************
1144 * Parse command line for input files as well as their associated options.
1145 * An option always follows its associated input and begins with a ":".
1146 *****************************************************************************/
1147 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1149 int i_opt, i_options;
1151 /* We assume that the remaining parameters are filenames
1152 * and their input options */
1153 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1157 /* Count the input options */
1158 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1164 /* TODO: write an internal function of this one, to avoid
1165 * unnecessary lookups. */
1166 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1167 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1169 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1176 /*****************************************************************************
1177 * Usage: print program usage
1178 *****************************************************************************
1179 * Print a short inline help. Message interface is initialized at this stage.
1180 *****************************************************************************/
1181 static void Usage( vlc_t *p_this, char const *psz_module_name )
1183 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1184 /* short option ------' | | | | | | |
1185 * option name ------------' | | | | | |
1186 * <bra -------------------------' | | | | |
1187 * option type or "" --------------' | | | |
1188 * ket> -----------------------------' | | |
1189 * padding spaces ---------------------' | |
1190 * comment -------------------------------' |
1191 * comment suffix --------------------------'
1193 * The purpose of having bra and ket is that we might i18n them as well.
1195 #define LINE_START 8
1196 #define PADDING_SPACES 25
1199 module_config_t *p_item;
1200 char psz_spaces[PADDING_SPACES+LINE_START+1];
1201 char psz_format[sizeof(FORMAT_STRING)];
1202 char psz_buffer[1000];
1205 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1207 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1208 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1210 strcpy( psz_format, FORMAT_STRING );
1216 /* List all modules */
1217 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1219 /* Enumerate the config for each module */
1220 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1222 vlc_bool_t b_help_module;
1224 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1226 if( psz_module_name && strcmp( psz_module_name,
1227 p_parser->psz_object_name ) )
1232 /* Ignore modules without config options */
1233 if( !p_parser->i_config_items )
1238 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1240 /* Print module options */
1241 for( p_item = p_parser->p_config;
1242 p_item->i_type != CONFIG_HINT_END;
1246 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1247 char *psz_suf = "", *psz_prefix = NULL;
1249 if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1253 switch( p_item->i_type )
1255 case CONFIG_HINT_CATEGORY:
1256 case CONFIG_HINT_USAGE:
1257 fprintf( stdout, " %s\n", p_item->psz_text );
1260 case CONFIG_ITEM_STRING:
1261 case CONFIG_ITEM_FILE:
1262 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1263 if( !p_item->ppsz_list )
1265 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1271 psz_type = psz_buffer;
1273 for( i = 0; p_item->ppsz_list[i]; i++ )
1275 if( i ) strcat( psz_type, "," );
1276 strcat( psz_type, p_item->ppsz_list[i] );
1281 case CONFIG_ITEM_INTEGER:
1282 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1284 case CONFIG_ITEM_FLOAT:
1285 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1287 case CONFIG_ITEM_BOOL:
1288 psz_bra = ""; psz_type = ""; psz_ket = "";
1289 if( !b_help_module )
1291 psz_suf = p_item->i_value ? _(" (default enabled)") :
1292 _(" (default disabled)");
1302 /* Add short option if any */
1303 if( p_item->i_short )
1305 sprintf( psz_short, "-%c,", p_item->i_short );
1309 strcpy( psz_short, " " );
1312 i = PADDING_SPACES - strlen( p_item->psz_name )
1313 - strlen( psz_bra ) - strlen( psz_type )
1314 - strlen( psz_ket ) - 1;
1316 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1318 /* If option is of type --foo-bar, we print its counterpart
1319 * as --no-foo-bar, but if it is of type --foobar (without
1320 * dashes in the name) we print it as --nofoobar. Both
1321 * values are of course valid, only the display changes. */
1322 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1324 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1329 psz_spaces[0] = '\n';
1334 psz_spaces[i] = '\0';
1337 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1339 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1340 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1341 psz_ket, psz_spaces );
1345 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1346 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1349 psz_spaces[i] = ' ';
1351 /* We wrap the rest of the output */
1352 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1353 psz_text = psz_buffer;
1356 char *psz_parser, *psz_word;
1357 int i_end = strlen( psz_text );
1359 /* If the remaining text fits in a line, print it. */
1360 if( i_end <= i_width )
1362 fprintf( stdout, "%s\n", psz_text );
1366 /* Otherwise, eat as many words as possible */
1367 psz_parser = psz_text;
1370 psz_word = psz_parser;
1371 psz_parser = strchr( psz_word, ' ' );
1372 /* If no space was found, we reached the end of the text
1373 * block; otherwise, we skip the space we just found. */
1374 psz_parser = psz_parser ? psz_parser + 1
1377 } while( psz_parser - psz_text <= i_width );
1379 /* We cut a word in one of these cases:
1380 * - it's the only word in the line and it's too long.
1381 * - we used less than 80% of the width and the word we are
1382 * going to wrap is longer than 40% of the width, and even
1383 * if the word would have fit in the next line. */
1384 if( psz_word == psz_text
1385 || ( psz_word - psz_text < 80 * i_width / 100
1386 && psz_parser - psz_word > 40 * i_width / 100 ) )
1388 char c = psz_text[i_width];
1389 psz_text[i_width] = '\0';
1390 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1391 psz_text += i_width;
1396 psz_word[-1] = '\0';
1397 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1398 psz_text = psz_word;
1404 /* Release the module list */
1405 vlc_list_release( p_list );
1407 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1408 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1413 /*****************************************************************************
1414 * ListModules: list the available modules with their description
1415 *****************************************************************************
1416 * Print a list of all available modules (builtins and plugins) and a short
1417 * description for each one.
1418 *****************************************************************************/
1419 static void ListModules( vlc_t *p_this )
1423 char psz_spaces[22];
1426 memset( psz_spaces, ' ', 22 );
1433 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1434 p_this->p_vlc->psz_object_name );
1436 fprintf( stdout, _("[module] [description]\n") );
1438 /* List all modules */
1439 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1441 /* Enumerate each module */
1442 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1446 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1448 /* Nasty hack, but right now I'm too tired to think about a nice
1450 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1454 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1455 psz_spaces, p_parser->psz_longname );
1457 psz_spaces[i] = ' ';
1460 vlc_list_release( p_list );
1462 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1463 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1468 /*****************************************************************************
1469 * Version: print complete program version
1470 *****************************************************************************
1471 * Print complete program version and build number.
1472 *****************************************************************************/
1473 static void Version( void )
1479 fprintf( stdout, VERSION_MESSAGE "\n" );
1481 _("This program comes with NO WARRANTY, to the extent permitted by "
1482 "law.\nYou may redistribute it under the terms of the GNU General "
1483 "Public License;\nsee the file named COPYING for details.\n"
1484 "Written by the VideoLAN team; see AUTHORS file.\n") );
1486 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1487 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1492 /*****************************************************************************
1493 * ShowConsole: On Win32, create an output console for debug messages
1494 *****************************************************************************
1495 * This function is useful only on Win32.
1496 *****************************************************************************/
1498 static void ShowConsole( void )
1502 freopen( "CONOUT$", "w", stdout );
1503 freopen( "CONOUT$", "w", stderr );
1504 freopen( "CONIN$", "r", stdin );
1510 /*****************************************************************************
1511 * ConsoleWidth: Return the console width in characters
1512 *****************************************************************************
1513 * We use the stty shell command to get the console width; if this fails or
1514 * if the width is less than 80, we default to 80.
1515 *****************************************************************************/
1516 static int ConsoleWidth( void )
1521 char buf[20], *psz_parser;
1525 file = popen( "stty size 2>/dev/null", "r" );
1528 i_ret = fread( buf, 1, 20, file );
1532 psz_parser = strchr( buf, ' ' );
1535 i_ret = atoi( psz_parser + 1 );