1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: libvlc.c,v 1.88 2003/05/25 17:27:13 massiot 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"
68 #include "stream_control.h"
69 #include "input_ext-intf.h"
71 #include "vlc_playlist.h"
72 #include "interface.h"
74 #include "audio_output.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 vlc_t * p_static_vlc;
87 /*****************************************************************************
89 *****************************************************************************/
90 static void SetLanguage ( char const * );
91 static int GetFilenames ( vlc_t *, int, char *[] );
92 static void Usage ( vlc_t *, char const *psz_module_name );
93 static void ListModules ( vlc_t * );
94 static void Version ( void );
97 static void ShowConsole ( void );
99 static int ConsoleWidth ( void );
101 /*****************************************************************************
102 * VLC_Version: return the libvlc version.
103 *****************************************************************************
104 * This function returns full version string (numeric version and codename).
105 *****************************************************************************/
106 char const * VLC_Version( void )
108 return VERSION_MESSAGE;
111 /*****************************************************************************
112 * VLC_Error: strerror() equivalent
113 *****************************************************************************
114 * This function returns full version string (numeric version and codename).
115 *****************************************************************************/
116 char const * VLC_Error( int i_err )
118 return vlc_error( i_err );
121 /*****************************************************************************
122 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
123 *****************************************************************************
124 * This function allocates a vlc_t structure and returns a negative value
125 * in case of failure. Also, the thread system is initialized.
126 *****************************************************************************/
127 int VLC_Create( void )
130 vlc_t * p_vlc = NULL;
133 /* vlc_threads_init *must* be the first internal call! No other call is
134 * allowed before the thread system has been initialized. */
135 i_ret = vlc_threads_init( &libvlc );
141 /* Now that the thread system is initialized, we don't have much, but
142 * at least we have var_Create */
143 var_Create( &libvlc, "libvlc", VLC_VAR_MUTEX );
144 var_Get( &libvlc, "libvlc", &lockval );
145 vlc_mutex_lock( lockval.p_address );
146 if( !libvlc.b_ready )
150 /* Guess what CPU we have */
151 libvlc.i_cpu = CPUCapabilities();
153 /* Find verbosity from VLC_VERBOSE environment variable */
154 psz_env = getenv( "VLC_VERBOSE" );
155 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
157 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
158 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
160 libvlc.b_color = VLC_FALSE;
163 /* Initialize message queue */
164 msg_Create( &libvlc );
166 /* Announce who we are */
167 msg_Dbg( &libvlc, COPYRIGHT_MESSAGE );
168 msg_Dbg( &libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
170 /* The module bank will be initialized later */
171 libvlc.p_module_bank = NULL;
173 libvlc.b_ready = VLC_TRUE;
175 vlc_mutex_unlock( lockval.p_address );
176 var_Destroy( &libvlc, "libvlc" );
178 /* Allocate a vlc object */
179 p_vlc = vlc_object_create( &libvlc, VLC_OBJECT_VLC );
184 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
186 p_vlc->psz_object_name = "root";
188 /* Initialize mutexes */
189 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
191 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
194 /* Store our newly allocated structure in the global list */
195 vlc_object_attach( p_vlc, &libvlc );
197 /* Store data for the non-reentrant API */
198 p_static_vlc = p_vlc;
200 return p_vlc->i_object_id;
203 /*****************************************************************************
204 * VLC_Init: initialize a vlc_t structure.
205 *****************************************************************************
206 * This function initializes a previously allocated vlc_t structure:
208 * - gettext initialization
209 * - message queue, module bank and playlist initialization
210 * - configuration and commandline parsing
211 *****************************************************************************/
212 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
214 char p_capabilities[200];
219 vlc_bool_t b_exit = VLC_FALSE;
221 module_t *p_help_module;
222 playlist_t *p_playlist;
225 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
233 * System specific initialization code
235 system_Init( p_vlc, &i_argc, ppsz_argv );
237 /* Get the executable name (similar to the basename command) */
240 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
243 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
249 p_vlc->psz_object_name = "vlc";
253 * Support for gettext
257 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
258 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
260 /* Initialize the module bank and load the configuration of the
261 * main module. We need to do this at this stage to be able to display
262 * a short help if required by the user. (short help == main module
264 var_Create( &libvlc, "libvlc", VLC_VAR_MUTEX );
265 var_Get( &libvlc, "libvlc", &lockval );
266 vlc_mutex_lock( lockval.p_address );
267 if( libvlc.p_module_bank == NULL )
269 module_InitBank( &libvlc );
270 module_LoadMain( &libvlc );
272 vlc_mutex_unlock( lockval.p_address );
273 var_Destroy( &libvlc, "libvlc" );
275 /* Hack: insert the help module here */
276 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
277 if( p_help_module == NULL )
279 /*module_EndBank( p_vlc );*/
280 if( i_object ) vlc_object_release( p_vlc );
283 p_help_module->psz_object_name = "help";
284 config_Duplicate( p_help_module, p_help_config );
285 vlc_object_attach( p_help_module, libvlc.p_module_bank );
288 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
290 vlc_object_detach( p_help_module );
291 config_Free( p_help_module );
292 vlc_object_destroy( p_help_module );
293 /*module_EndBank( p_vlc );*/
294 if( i_object ) vlc_object_release( p_vlc );
298 /* Check for short help option */
299 if( config_GetInt( p_vlc, "help" ) )
301 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
302 p_vlc->psz_object_name );
303 Usage( p_vlc, "main" );
304 Usage( p_vlc, "help" );
307 /* Check for version option */
308 else if( config_GetInt( p_vlc, "version" ) )
314 /* Hack: remove the help module here */
315 vlc_object_detach( p_help_module );
320 config_Free( p_help_module );
321 vlc_object_destroy( p_help_module );
322 /*module_EndBank( p_vlc );*/
323 if( i_object ) vlc_object_release( p_vlc );
327 /* Check for translation config option */
328 #if defined( ENABLE_NLS ) \
329 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
331 /* This ain't really nice to have to reload the config here but it seems
332 * the only way to do it. */
333 p_vlc->psz_homedir = config_GetHomeDir();
334 config_LoadConfigFile( p_vlc, "main" );
335 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
337 /* Check if the user specified a custom language */
338 psz_language = config_GetPsz( p_vlc, "language" );
339 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
341 /* Reset the default domain */
342 SetLanguage( psz_language );
344 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
345 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
347 textdomain( PACKAGE );
349 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN )
350 /* BeOS only support UTF8 strings */
351 /* Mac OS X prefers UTF8 */
352 bind_textdomain_codeset( PACKAGE, "UTF-8" );
355 module_EndBank( p_vlc );
356 module_InitBank( &libvlc );
357 module_LoadMain( &libvlc );
358 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
360 if( psz_language ) free( psz_language );
364 * Load the builtins and plugins into the module_bank.
365 * We have to do it before config_Load*() because this also gets the
366 * list of configuration options exported by each module and loads their
369 module_LoadBuiltins( &libvlc );
370 module_LoadPlugins( &libvlc );
371 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
372 libvlc.p_module_bank->i_children );
374 /* Hack: insert the help module here */
375 vlc_object_attach( p_help_module, libvlc.p_module_bank );
378 /* Check for help on modules */
379 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
381 Usage( p_vlc, p_tmp );
385 /* Check for long help option */
386 else if( config_GetInt( p_vlc, "longhelp" ) )
388 Usage( p_vlc, NULL );
391 /* Check for module list option */
392 else if( config_GetInt( p_vlc, "list" ) )
394 ListModules( p_vlc );
398 /* Hack: remove the help module here */
399 vlc_object_detach( p_help_module );
400 config_Free( p_help_module );
401 vlc_object_destroy( p_help_module );
406 /*module_EndBank( p_vlc );*/
407 if( i_object ) vlc_object_release( p_vlc );
412 * Override default configuration with config file settings
414 p_vlc->psz_homedir = config_GetHomeDir();
415 config_LoadConfigFile( p_vlc, NULL );
418 * Override configuration with command line settings
420 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
424 /* Pause the console because it's destroyed when we exit */
425 fprintf( stderr, "The command line options couldn't be loaded, check "
426 "that they are valid.\nPress the RETURN key to continue..." );
429 /*module_EndBank( p_vlc );*/
430 if( i_object ) vlc_object_release( p_vlc );
435 * System specific configuration
437 system_Configure( p_vlc );
440 * Message queue options
442 if( config_GetInt( p_vlc, "quiet" ) )
444 libvlc.i_verbose = -1;
448 int i_tmp = config_GetInt( p_vlc, "verbose" );
451 libvlc.i_verbose = __MIN( i_tmp, 2 );
454 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
457 * Output messages that may still be in the queue
461 /* p_vlc initialization. FIXME ? */
462 p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
464 #if defined( __i386__ )
465 if( !config_GetInt( p_vlc, "mmx" ) )
466 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
467 if( !config_GetInt( p_vlc, "3dn" ) )
468 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
469 if( !config_GetInt( p_vlc, "mmxext" ) )
470 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
471 if( !config_GetInt( p_vlc, "sse" ) )
472 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
474 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
475 if( !config_GetInt( p_vlc, "altivec" ) )
476 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
479 #define PRINT_CAPABILITY( capability, string ) \
480 if( libvlc.i_cpu & capability ) \
482 strncat( p_capabilities, string " ", \
483 sizeof(p_capabilities) - strlen(p_capabilities) ); \
484 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
487 p_capabilities[0] = '\0';
488 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
489 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
490 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
491 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
492 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
493 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
494 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
495 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
496 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
497 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
500 * Choose the best memcpy module
502 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
504 if( p_vlc->pf_memcpy == NULL )
506 p_vlc->pf_memcpy = memcpy;
509 if( p_vlc->pf_memset == NULL )
511 p_vlc->pf_memset = memset;
515 * Initialize playlist and get commandline files
517 p_playlist = playlist_Create( p_vlc );
520 msg_Err( p_vlc, "playlist initialization failed" );
521 if( p_vlc->p_memcpy_module != NULL )
523 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
525 /*module_EndBank( p_vlc );*/
526 if( i_object ) vlc_object_release( p_vlc );
531 * Load background interfaces
533 psz_modules = config_GetPsz( p_vlc, "extraintf" );
534 psz_parser = psz_modules;
535 while ( psz_parser && *psz_parser )
537 char *psz_module, *psz_temp;
538 psz_module = psz_parser;
539 psz_parser = strchr( psz_module, ',' );
545 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
548 sprintf( psz_temp, "%s,none", psz_module );
549 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
559 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
561 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
564 * Get input filenames given as commandline arguments
566 GetFilenames( p_vlc, i_argc, ppsz_argv );
568 if( i_object ) vlc_object_release( p_vlc );
572 /*****************************************************************************
573 * VLC_AddIntf: add an interface
574 *****************************************************************************
575 * This function opens an interface plugin and runs it. If b_block is set
576 * to 0, VLC_AddIntf will return immediately and let the interface run in a
577 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
578 * user requests to quit.
579 *****************************************************************************/
580 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
583 intf_thread_t *p_intf;
586 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
593 /* Try to create the interface */
594 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
598 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
599 if( i_object ) vlc_object_release( p_vlc );
603 /* Try to run the interface */
604 p_intf->b_block = b_block;
605 i_err = intf_RunThread( p_intf );
608 vlc_object_detach( p_intf );
609 intf_Destroy( p_intf );
610 if( i_object ) vlc_object_release( p_vlc );
614 if( i_object ) vlc_object_release( p_vlc );
618 /*****************************************************************************
619 * VLC_Destroy: stop playing and destroy everything.
620 *****************************************************************************
621 * This function requests the running threads to finish, waits for their
622 * termination, and destroys their structure.
623 *****************************************************************************/
624 int VLC_Destroy( int i_object )
628 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
636 * Free allocated memory
638 if( p_vlc->p_memcpy_module )
640 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
641 p_vlc->p_memcpy_module = NULL;
644 if( p_vlc->psz_homedir )
646 free( p_vlc->psz_homedir );
647 p_vlc->psz_homedir = NULL;
651 * XXX: Free module bank !
653 /*module_EndBank( p_vlc );*/
656 * System specific cleaning code
660 /* Destroy mutexes */
661 vlc_mutex_destroy( &p_vlc->config_lock );
663 vlc_mutex_destroy( &p_vlc->quicktime_lock );
666 vlc_object_detach( p_vlc );
668 /* Release object before destroying it */
669 if( i_object ) vlc_object_release( p_vlc );
671 vlc_object_destroy( p_vlc );
673 /* Stop thread system: last one out please shut the door! */
674 vlc_threads_end( &libvlc );
679 /*****************************************************************************
680 * VLC_Die: ask vlc to die.
681 *****************************************************************************
682 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
683 * task. It is your duty to call vlc_end and VLC_Destroy afterwards.
684 *****************************************************************************/
685 int VLC_Die( int i_object )
689 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
696 p_vlc->b_die = VLC_TRUE;
698 if( i_object ) vlc_object_release( p_vlc );
702 /*****************************************************************************
703 * VLC_AddTarget: adds a target for playing.
704 *****************************************************************************
705 * This function adds psz_target to the current playlist. If a playlist does
706 * not exist, it will create one.
707 *****************************************************************************/
708 int VLC_AddTarget( int i_object, char const *psz_target, int i_mode, int i_pos )
711 playlist_t *p_playlist;
714 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
721 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
723 if( p_playlist == NULL )
725 msg_Dbg( p_vlc, "no playlist present, creating one" );
726 p_playlist = playlist_Create( p_vlc );
728 if( p_playlist == NULL )
730 if( i_object ) vlc_object_release( p_vlc );
734 vlc_object_yield( p_playlist );
737 i_err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
739 vlc_object_release( p_playlist );
741 if( i_object ) vlc_object_release( p_vlc );
745 /*****************************************************************************
746 * VLC_Set: set a vlc variable
747 *****************************************************************************
749 *****************************************************************************/
750 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
755 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
762 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
763 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
764 if( !strncmp( psz_var, "conf::", 6 ) )
766 module_config_t *p_item;
767 char const *psz_newvar = psz_var + 6;
769 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
773 switch( p_item->i_type )
775 case CONFIG_ITEM_BOOL:
776 config_PutInt( p_vlc, psz_newvar, value.b_bool );
778 case CONFIG_ITEM_INTEGER:
779 config_PutInt( p_vlc, psz_newvar, value.i_int );
781 case CONFIG_ITEM_FLOAT:
782 config_PutFloat( p_vlc, psz_newvar, value.f_float );
785 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
788 if( i_object ) vlc_object_release( p_vlc );
793 i_ret = var_Set( p_vlc, psz_var, value );
795 if( i_object ) vlc_object_release( p_vlc );
799 /*****************************************************************************
800 * VLC_Get: get a vlc variable
801 *****************************************************************************
803 *****************************************************************************/
804 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
809 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
816 i_ret = var_Get( p_vlc, psz_var, p_value );
818 if( i_object ) vlc_object_release( p_vlc );
822 /* FIXME: temporary hacks */
824 /*****************************************************************************
826 *****************************************************************************/
827 int VLC_Play( int i_object )
829 playlist_t * p_playlist;
832 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
834 /* Check that the handle is valid */
840 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
844 if( i_object ) vlc_object_release( p_vlc );
848 vlc_mutex_lock( &p_playlist->object_lock );
849 if( p_playlist->i_size )
851 vlc_mutex_unlock( &p_playlist->object_lock );
852 playlist_Play( p_playlist );
856 vlc_mutex_unlock( &p_playlist->object_lock );
859 vlc_object_release( p_playlist );
861 if( i_object ) vlc_object_release( p_vlc );
865 /*****************************************************************************
867 *****************************************************************************/
868 int VLC_Stop( int i_object )
870 intf_thread_t * p_intf;
871 playlist_t * p_playlist;
872 vout_thread_t * p_vout;
873 aout_instance_t * p_aout;
876 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
878 /* Check that the handle is valid */
885 * Ask the interfaces to stop and destroy them
887 msg_Dbg( p_vlc, "removing all interfaces" );
888 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
890 intf_StopThread( p_intf );
891 vlc_object_detach( p_intf );
892 vlc_object_release( p_intf );
893 intf_Destroy( p_intf );
899 msg_Dbg( p_vlc, "removing all playlists" );
900 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
903 vlc_object_detach( p_playlist );
904 vlc_object_release( p_playlist );
905 playlist_Destroy( p_playlist );
911 msg_Dbg( p_vlc, "removing all video outputs" );
912 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
914 vlc_object_detach( p_vout );
915 vlc_object_release( p_vout );
916 vout_Destroy( p_vout );
922 msg_Dbg( p_vlc, "removing all audio outputs" );
923 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
925 vlc_object_detach( (vlc_object_t *)p_aout );
926 vlc_object_release( (vlc_object_t *)p_aout );
927 aout_Delete( p_aout );
930 if( i_object ) vlc_object_release( p_vlc );
934 /*****************************************************************************
935 * VLC_Pause: toggle pause
936 *****************************************************************************/
937 int VLC_Pause( int i_object )
939 input_thread_t *p_input;
942 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
949 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
953 if( i_object ) vlc_object_release( p_vlc );
957 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
958 vlc_object_release( p_input );
960 if( i_object ) vlc_object_release( p_vlc );
964 /*****************************************************************************
965 * VLC_FullScreen: toggle fullscreen mode
966 *****************************************************************************/
967 int VLC_FullScreen( int i_object )
969 vout_thread_t *p_vout;
972 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
979 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
983 if( i_object ) vlc_object_release( p_vlc );
987 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
988 vlc_object_release( p_vout );
990 if( i_object ) vlc_object_release( p_vlc );
994 /* following functions are local */
996 /*****************************************************************************
997 * SetLanguage: set the interface language.
998 *****************************************************************************
999 * We set the LC_MESSAGES locale category for interface messages and buttons,
1000 * as well as the LC_CTYPE category for string sorting and possible wide
1001 * character support.
1002 *****************************************************************************/
1003 static void SetLanguage ( char const *psz_lang )
1005 #if defined( ENABLE_NLS ) \
1006 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1009 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1013 # if defined( HAVE_INCLUDED_GETTEXT ) && !defined( HAVE_LC_MESSAGES )
1016 /* We set LC_ALL manually because it is the only way to set
1017 * the language at runtime under eg. Windows. Beware that this
1018 * makes the environment unconsistent when libvlc is unloaded and
1019 * should probably be moved to a safer place like vlc.c. */
1020 static char psz_lcall[20];
1021 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1022 psz_lcall[19] = '\0';
1023 putenv( psz_lcall );
1027 if( psz_lang && !*psz_lang )
1029 # if defined( HAVE_LC_MESSAGES )
1030 setlocale( LC_MESSAGES, psz_lang );
1032 setlocale( LC_CTYPE, psz_lang );
1037 static char psz_lcall[20];
1039 setlocale( LC_ALL, psz_lang );
1041 /* I need that under Darwin, please check it doesn't disturb
1042 * other platforms. --Meuuh */
1043 setenv( "LANG", psz_lang, 1 );
1046 /* I need this under BeOS... */
1047 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1048 psz_lcall[19] = '\0';
1049 putenv( psz_lcall );
1053 /* Specify where to find the locales for current domain */
1054 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1055 psz_path = LOCALEDIR;
1057 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1061 if( !bindtextdomain( PACKAGE, psz_path ) )
1063 fprintf( stderr, "warning: no domain %s in directory %s\n",
1064 PACKAGE, psz_path );
1067 /* Set the default domain */
1068 textdomain( PACKAGE );
1070 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN )
1071 /* BeOS only support UTF8 strings */
1072 /* Mac OS X prefers UTF8 */
1073 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1079 /*****************************************************************************
1080 * GetFilenames: parse command line options which are not flags
1081 *****************************************************************************
1082 * Parse command line for input files.
1083 *****************************************************************************/
1084 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1088 /* We assume that the remaining parameters are filenames */
1089 for( i_opt = i_argc - 1; i_opt > optind; i_opt-- )
1091 /* TODO: write an internal function of this one, to avoid
1092 * unnecessary lookups. */
1093 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1094 PLAYLIST_INSERT, 0 );
1097 /* If there is at least one target, play it */
1098 if( i_argc > optind )
1100 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ optind ],
1101 PLAYLIST_INSERT | PLAYLIST_GO, 0 );
1107 /*****************************************************************************
1108 * Usage: print program usage
1109 *****************************************************************************
1110 * Print a short inline help. Message interface is initialized at this stage.
1111 *****************************************************************************/
1112 static void Usage( vlc_t *p_this, char const *psz_module_name )
1114 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1115 /* short option ------' | | | | | | |
1116 * option name ------------' | | | | | |
1117 * <bra -------------------------' | | | | |
1118 * option type or "" --------------' | | | |
1119 * ket> -----------------------------' | | |
1120 * padding spaces ---------------------' | |
1121 * comment -------------------------------' |
1122 * comment suffix --------------------------'
1124 * The purpose of having bra and ket is that we might i18n them as well.
1126 #define LINE_START 8
1127 #define PADDING_SPACES 25
1130 module_config_t *p_item;
1131 char psz_spaces[PADDING_SPACES+LINE_START+1];
1132 char psz_format[sizeof(FORMAT_STRING)];
1133 char psz_buffer[1000];
1136 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1138 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1139 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1141 strcpy( psz_format, FORMAT_STRING );
1147 /* List all modules */
1148 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1150 /* Enumerate the config for each module */
1151 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1153 vlc_bool_t b_help_module;
1155 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1157 if( psz_module_name && strcmp( psz_module_name,
1158 p_parser->psz_object_name ) )
1163 /* Ignore modules without config options */
1164 if( !p_parser->i_config_items )
1169 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1171 /* Print module options */
1172 for( p_item = p_parser->p_config;
1173 p_item->i_type != CONFIG_HINT_END;
1177 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1178 char *psz_suf = "", *psz_prefix = NULL;
1180 if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1184 switch( p_item->i_type )
1186 case CONFIG_HINT_CATEGORY:
1187 case CONFIG_HINT_USAGE:
1188 fprintf( stdout, " %s\n", p_item->psz_text );
1191 case CONFIG_ITEM_STRING:
1192 case CONFIG_ITEM_FILE:
1193 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1194 if( !p_item->ppsz_list )
1196 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1202 psz_type = psz_buffer;
1204 for( i = 0; p_item->ppsz_list[i]; i++ )
1206 if( i ) strcat( psz_type, "," );
1207 strcat( psz_type, p_item->ppsz_list[i] );
1212 case CONFIG_ITEM_INTEGER:
1213 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1215 case CONFIG_ITEM_FLOAT:
1216 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1218 case CONFIG_ITEM_BOOL:
1219 psz_bra = ""; psz_type = ""; psz_ket = "";
1220 if( !b_help_module )
1222 psz_suf = p_item->i_value ? _(" (default enabled)") :
1223 _(" (default disabled)");
1233 /* Add short option if any */
1234 if( p_item->i_short )
1236 sprintf( psz_short, "-%c,", p_item->i_short );
1240 strcpy( psz_short, " " );
1243 i = PADDING_SPACES - strlen( p_item->psz_name )
1244 - strlen( psz_bra ) - strlen( psz_type )
1245 - strlen( psz_ket ) - 1;
1247 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1249 /* If option is of type --foo-bar, we print its counterpart
1250 * as --no-foo-bar, but if it is of type --foobar (without
1251 * dashes in the name) we print it as --nofoobar. Both
1252 * values are of course valid, only the display changes. */
1253 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1255 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1260 psz_spaces[0] = '\n';
1265 psz_spaces[i] = '\0';
1268 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1270 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1271 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1272 psz_ket, psz_spaces );
1276 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1277 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1280 psz_spaces[i] = ' ';
1282 /* We wrap the rest of the output */
1283 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1284 psz_text = psz_buffer;
1287 char *psz_parser, *psz_word;
1288 int i_end = strlen( psz_text );
1290 /* If the remaining text fits in a line, print it. */
1291 if( i_end <= i_width )
1293 fprintf( stdout, "%s\n", psz_text );
1297 /* Otherwise, eat as many words as possible */
1298 psz_parser = psz_text;
1301 psz_word = psz_parser;
1302 psz_parser = strchr( psz_word, ' ' );
1303 /* If no space was found, we reached the end of the text
1304 * block; otherwise, we skip the space we just found. */
1305 psz_parser = psz_parser ? psz_parser + 1
1308 } while( psz_parser - psz_text <= i_width );
1310 /* We cut a word in one of these cases:
1311 * - it's the only word in the line and it's too long.
1312 * - we used less than 80% of the width and the word we are
1313 * going to wrap is longer than 40% of the width, and even
1314 * if the word would have fit in the next line. */
1315 if( psz_word == psz_text
1316 || ( psz_word - psz_text < 80 * i_width / 100
1317 && psz_parser - psz_word > 40 * i_width / 100 ) )
1319 char c = psz_text[i_width];
1320 psz_text[i_width] = '\0';
1321 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1322 psz_text += i_width;
1327 psz_word[-1] = '\0';
1328 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1329 psz_text = psz_word;
1335 /* Release the module list */
1336 vlc_list_release( p_list );
1338 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1339 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1344 /*****************************************************************************
1345 * ListModules: list the available modules with their description
1346 *****************************************************************************
1347 * Print a list of all available modules (builtins and plugins) and a short
1348 * description for each one.
1349 *****************************************************************************/
1350 static void ListModules( vlc_t *p_this )
1354 char psz_spaces[22];
1357 memset( psz_spaces, ' ', 22 );
1364 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1365 p_this->p_vlc->psz_object_name );
1367 fprintf( stdout, _("[module] [description]\n") );
1369 /* List all modules */
1370 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1372 /* Enumerate each module */
1373 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1377 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1379 /* Nasty hack, but right now I'm too tired to think about a nice
1381 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1385 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1386 psz_spaces, p_parser->psz_longname );
1388 psz_spaces[i] = ' ';
1391 vlc_list_release( p_list );
1393 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1394 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1399 /*****************************************************************************
1400 * Version: print complete program version
1401 *****************************************************************************
1402 * Print complete program version and build number.
1403 *****************************************************************************/
1404 static void Version( void )
1410 fprintf( stdout, VERSION_MESSAGE "\n" );
1412 _("This program comes with NO WARRANTY, to the extent permitted by "
1413 "law.\nYou may redistribute it under the terms of the GNU General "
1414 "Public License;\nsee the file named COPYING for details.\n"
1415 "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1417 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1418 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1423 /*****************************************************************************
1424 * ShowConsole: On Win32, create an output console for debug messages
1425 *****************************************************************************
1426 * This function is useful only on Win32.
1427 *****************************************************************************/
1429 static void ShowConsole( void )
1433 freopen( "CONOUT$", "w", stdout );
1434 freopen( "CONOUT$", "w", stderr );
1435 freopen( "CONIN$", "r", stdin );
1441 /*****************************************************************************
1442 * ConsoleWidth: Return the console width in characters
1443 *****************************************************************************
1444 * We use the stty shell command to get the console width; if this fails or
1445 * if the width is less than 80, we default to 80.
1446 *****************************************************************************/
1447 static int ConsoleWidth( void )
1452 char buf[20], *psz_parser;
1456 file = popen( "stty size 2>/dev/null", "r" );
1459 i_ret = fread( buf, 1, 20, file );
1463 psz_parser = strchr( buf, ' ' );
1466 i_ret = atoi( psz_parser + 1 );