1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: libvlc.c,v 1.87 2003/05/21 15:40:03 hartman 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 module_EndBank( p_vlc );
350 module_InitBank( &libvlc );
351 module_LoadMain( &libvlc );
352 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
354 if( psz_language ) free( psz_language );
358 * Load the builtins and plugins into the module_bank.
359 * We have to do it before config_Load*() because this also gets the
360 * list of configuration options exported by each module and loads their
363 module_LoadBuiltins( &libvlc );
364 module_LoadPlugins( &libvlc );
365 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
366 libvlc.p_module_bank->i_children );
368 /* Hack: insert the help module here */
369 vlc_object_attach( p_help_module, libvlc.p_module_bank );
372 /* Check for help on modules */
373 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
375 Usage( p_vlc, p_tmp );
379 /* Check for long help option */
380 else if( config_GetInt( p_vlc, "longhelp" ) )
382 Usage( p_vlc, NULL );
385 /* Check for module list option */
386 else if( config_GetInt( p_vlc, "list" ) )
388 ListModules( p_vlc );
392 /* Hack: remove the help module here */
393 vlc_object_detach( p_help_module );
394 config_Free( p_help_module );
395 vlc_object_destroy( p_help_module );
400 /*module_EndBank( p_vlc );*/
401 if( i_object ) vlc_object_release( p_vlc );
406 * Override default configuration with config file settings
408 p_vlc->psz_homedir = config_GetHomeDir();
409 config_LoadConfigFile( p_vlc, NULL );
412 * Override configuration with command line settings
414 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
418 /* Pause the console because it's destroyed when we exit */
419 fprintf( stderr, "The command line options couldn't be loaded, check "
420 "that they are valid.\nPress the RETURN key to continue..." );
423 /*module_EndBank( p_vlc );*/
424 if( i_object ) vlc_object_release( p_vlc );
429 * System specific configuration
431 system_Configure( p_vlc );
434 * Message queue options
436 if( config_GetInt( p_vlc, "quiet" ) )
438 libvlc.i_verbose = -1;
442 int i_tmp = config_GetInt( p_vlc, "verbose" );
445 libvlc.i_verbose = __MIN( i_tmp, 2 );
448 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
451 * Output messages that may still be in the queue
455 /* p_vlc initialization. FIXME ? */
456 p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
458 #if defined( __i386__ )
459 if( !config_GetInt( p_vlc, "mmx" ) )
460 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
461 if( !config_GetInt( p_vlc, "3dn" ) )
462 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
463 if( !config_GetInt( p_vlc, "mmxext" ) )
464 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
465 if( !config_GetInt( p_vlc, "sse" ) )
466 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
468 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
469 if( !config_GetInt( p_vlc, "altivec" ) )
470 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
473 #define PRINT_CAPABILITY( capability, string ) \
474 if( libvlc.i_cpu & capability ) \
476 strncat( p_capabilities, string " ", \
477 sizeof(p_capabilities) - strlen(p_capabilities) ); \
478 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
481 p_capabilities[0] = '\0';
482 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
483 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
484 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
485 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
486 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
487 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
488 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
489 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
490 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
491 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
494 * Choose the best memcpy module
496 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
498 if( p_vlc->pf_memcpy == NULL )
500 p_vlc->pf_memcpy = memcpy;
503 if( p_vlc->pf_memset == NULL )
505 p_vlc->pf_memset = memset;
509 * Initialize playlist and get commandline files
511 p_playlist = playlist_Create( p_vlc );
514 msg_Err( p_vlc, "playlist initialization failed" );
515 if( p_vlc->p_memcpy_module != NULL )
517 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
519 /*module_EndBank( p_vlc );*/
520 if( i_object ) vlc_object_release( p_vlc );
525 * Load background interfaces
527 psz_modules = config_GetPsz( p_vlc, "extraintf" );
528 psz_parser = psz_modules;
529 while ( psz_parser && *psz_parser )
531 char *psz_module, *psz_temp;
532 psz_module = psz_parser;
533 psz_parser = strchr( psz_module, ',' );
539 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
542 sprintf( psz_temp, "%s,none", psz_module );
543 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
553 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
555 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
558 * Get input filenames given as commandline arguments
560 GetFilenames( p_vlc, i_argc, ppsz_argv );
562 if( i_object ) vlc_object_release( p_vlc );
566 /*****************************************************************************
567 * VLC_AddIntf: add an interface
568 *****************************************************************************
569 * This function opens an interface plugin and runs it. If b_block is set
570 * to 0, VLC_AddIntf will return immediately and let the interface run in a
571 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
572 * user requests to quit.
573 *****************************************************************************/
574 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
577 intf_thread_t *p_intf;
580 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
587 /* Try to create the interface */
588 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
592 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
593 if( i_object ) vlc_object_release( p_vlc );
597 /* Try to run the interface */
598 p_intf->b_block = b_block;
599 i_err = intf_RunThread( p_intf );
602 vlc_object_detach( p_intf );
603 intf_Destroy( p_intf );
604 if( i_object ) vlc_object_release( p_vlc );
608 if( i_object ) vlc_object_release( p_vlc );
612 /*****************************************************************************
613 * VLC_Destroy: stop playing and destroy everything.
614 *****************************************************************************
615 * This function requests the running threads to finish, waits for their
616 * termination, and destroys their structure.
617 *****************************************************************************/
618 int VLC_Destroy( int i_object )
622 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
630 * Free allocated memory
632 if( p_vlc->p_memcpy_module )
634 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
635 p_vlc->p_memcpy_module = NULL;
638 if( p_vlc->psz_homedir )
640 free( p_vlc->psz_homedir );
641 p_vlc->psz_homedir = NULL;
645 * XXX: Free module bank !
647 /*module_EndBank( p_vlc );*/
650 * System specific cleaning code
654 /* Destroy mutexes */
655 vlc_mutex_destroy( &p_vlc->config_lock );
657 vlc_mutex_destroy( &p_vlc->quicktime_lock );
660 vlc_object_detach( p_vlc );
662 /* Release object before destroying it */
663 if( i_object ) vlc_object_release( p_vlc );
665 vlc_object_destroy( p_vlc );
667 /* Stop thread system: last one out please shut the door! */
668 vlc_threads_end( &libvlc );
673 /*****************************************************************************
674 * VLC_Die: ask vlc to die.
675 *****************************************************************************
676 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
677 * task. It is your duty to call vlc_end and VLC_Destroy afterwards.
678 *****************************************************************************/
679 int VLC_Die( int i_object )
683 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
690 p_vlc->b_die = VLC_TRUE;
692 if( i_object ) vlc_object_release( p_vlc );
696 /*****************************************************************************
697 * VLC_AddTarget: adds a target for playing.
698 *****************************************************************************
699 * This function adds psz_target to the current playlist. If a playlist does
700 * not exist, it will create one.
701 *****************************************************************************/
702 int VLC_AddTarget( int i_object, char const *psz_target, int i_mode, int i_pos )
705 playlist_t *p_playlist;
708 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
715 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
717 if( p_playlist == NULL )
719 msg_Dbg( p_vlc, "no playlist present, creating one" );
720 p_playlist = playlist_Create( p_vlc );
722 if( p_playlist == NULL )
724 if( i_object ) vlc_object_release( p_vlc );
728 vlc_object_yield( p_playlist );
731 i_err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
733 vlc_object_release( p_playlist );
735 if( i_object ) vlc_object_release( p_vlc );
739 /*****************************************************************************
740 * VLC_Set: set a vlc variable
741 *****************************************************************************
743 *****************************************************************************/
744 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
749 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
756 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
757 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
758 if( !strncmp( psz_var, "conf::", 6 ) )
760 module_config_t *p_item;
761 char const *psz_newvar = psz_var + 6;
763 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
767 switch( p_item->i_type )
769 case CONFIG_ITEM_BOOL:
770 config_PutInt( p_vlc, psz_newvar, value.b_bool );
772 case CONFIG_ITEM_INTEGER:
773 config_PutInt( p_vlc, psz_newvar, value.i_int );
775 case CONFIG_ITEM_FLOAT:
776 config_PutFloat( p_vlc, psz_newvar, value.f_float );
779 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
782 if( i_object ) vlc_object_release( p_vlc );
787 i_ret = var_Set( p_vlc, psz_var, value );
789 if( i_object ) vlc_object_release( p_vlc );
793 /*****************************************************************************
794 * VLC_Get: get a vlc variable
795 *****************************************************************************
797 *****************************************************************************/
798 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
803 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
810 i_ret = var_Get( p_vlc, psz_var, p_value );
812 if( i_object ) vlc_object_release( p_vlc );
816 /* FIXME: temporary hacks */
818 /*****************************************************************************
820 *****************************************************************************/
821 int VLC_Play( int i_object )
823 playlist_t * p_playlist;
826 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
828 /* Check that the handle is valid */
834 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
838 if( i_object ) vlc_object_release( p_vlc );
842 vlc_mutex_lock( &p_playlist->object_lock );
843 if( p_playlist->i_size )
845 vlc_mutex_unlock( &p_playlist->object_lock );
846 playlist_Play( p_playlist );
850 vlc_mutex_unlock( &p_playlist->object_lock );
853 vlc_object_release( p_playlist );
855 if( i_object ) vlc_object_release( p_vlc );
859 /*****************************************************************************
861 *****************************************************************************/
862 int VLC_Stop( int i_object )
864 intf_thread_t * p_intf;
865 playlist_t * p_playlist;
866 vout_thread_t * p_vout;
867 aout_instance_t * p_aout;
870 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
872 /* Check that the handle is valid */
879 * Ask the interfaces to stop and destroy them
881 msg_Dbg( p_vlc, "removing all interfaces" );
882 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
884 intf_StopThread( p_intf );
885 vlc_object_detach( p_intf );
886 vlc_object_release( p_intf );
887 intf_Destroy( p_intf );
893 msg_Dbg( p_vlc, "removing all playlists" );
894 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
897 vlc_object_detach( p_playlist );
898 vlc_object_release( p_playlist );
899 playlist_Destroy( p_playlist );
905 msg_Dbg( p_vlc, "removing all video outputs" );
906 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
908 vlc_object_detach( p_vout );
909 vlc_object_release( p_vout );
910 vout_Destroy( p_vout );
916 msg_Dbg( p_vlc, "removing all audio outputs" );
917 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
919 vlc_object_detach( (vlc_object_t *)p_aout );
920 vlc_object_release( (vlc_object_t *)p_aout );
921 aout_Delete( p_aout );
924 if( i_object ) vlc_object_release( p_vlc );
928 /*****************************************************************************
929 * VLC_Pause: toggle pause
930 *****************************************************************************/
931 int VLC_Pause( int i_object )
933 input_thread_t *p_input;
936 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
943 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
947 if( i_object ) vlc_object_release( p_vlc );
951 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
952 vlc_object_release( p_input );
954 if( i_object ) vlc_object_release( p_vlc );
958 /*****************************************************************************
959 * VLC_FullScreen: toggle fullscreen mode
960 *****************************************************************************/
961 int VLC_FullScreen( int i_object )
963 vout_thread_t *p_vout;
966 p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
973 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
977 if( i_object ) vlc_object_release( p_vlc );
981 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
982 vlc_object_release( p_vout );
984 if( i_object ) vlc_object_release( p_vlc );
988 /* following functions are local */
990 /*****************************************************************************
991 * SetLanguage: set the interface language.
992 *****************************************************************************
993 * We set the LC_MESSAGES locale category for interface messages and buttons,
994 * as well as the LC_CTYPE category for string sorting and possible wide
996 *****************************************************************************/
997 static void SetLanguage ( char const *psz_lang )
999 #if defined( ENABLE_NLS ) \
1000 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1003 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1007 # if defined( HAVE_INCLUDED_GETTEXT ) && !defined( HAVE_LC_MESSAGES )
1010 /* We set LC_ALL manually because it is the only way to set
1011 * the language at runtime under eg. Windows. Beware that this
1012 * makes the environment unconsistent when libvlc is unloaded and
1013 * should probably be moved to a safer place like vlc.c. */
1014 static char psz_lcall[20];
1015 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1016 psz_lcall[19] = '\0';
1017 putenv( psz_lcall );
1021 if( psz_lang && !*psz_lang )
1023 # if defined( HAVE_LC_MESSAGES )
1024 setlocale( LC_MESSAGES, psz_lang );
1026 setlocale( LC_CTYPE, psz_lang );
1031 static char psz_lcall[20];
1033 setlocale( LC_ALL, psz_lang );
1035 /* I need that under Darwin, please check it doesn't disturb
1036 * other platforms. --Meuuh */
1037 setenv( "LANG", psz_lang, 1 );
1040 /* I need this under BeOS... */
1041 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1042 psz_lcall[19] = '\0';
1043 putenv( psz_lcall );
1047 /* Specify where to find the locales for current domain */
1048 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1049 psz_path = LOCALEDIR;
1051 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1055 if( !bindtextdomain( PACKAGE, psz_path ) )
1057 fprintf( stderr, "warning: no domain %s in directory %s\n",
1058 PACKAGE, psz_path );
1061 /* Set the default domain */
1062 textdomain( PACKAGE );
1065 /* BeOS only support UTF8 strings */
1066 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1072 /*****************************************************************************
1073 * GetFilenames: parse command line options which are not flags
1074 *****************************************************************************
1075 * Parse command line for input files.
1076 *****************************************************************************/
1077 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1081 /* We assume that the remaining parameters are filenames */
1082 for( i_opt = i_argc - 1; i_opt > optind; i_opt-- )
1084 /* TODO: write an internal function of this one, to avoid
1085 * unnecessary lookups. */
1086 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1087 PLAYLIST_INSERT, 0 );
1090 /* If there is at least one target, play it */
1091 if( i_argc > optind )
1093 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ optind ],
1094 PLAYLIST_INSERT | PLAYLIST_GO, 0 );
1100 /*****************************************************************************
1101 * Usage: print program usage
1102 *****************************************************************************
1103 * Print a short inline help. Message interface is initialized at this stage.
1104 *****************************************************************************/
1105 static void Usage( vlc_t *p_this, char const *psz_module_name )
1107 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1108 /* short option ------' | | | | | | |
1109 * option name ------------' | | | | | |
1110 * <bra -------------------------' | | | | |
1111 * option type or "" --------------' | | | |
1112 * ket> -----------------------------' | | |
1113 * padding spaces ---------------------' | |
1114 * comment -------------------------------' |
1115 * comment suffix --------------------------'
1117 * The purpose of having bra and ket is that we might i18n them as well.
1119 #define LINE_START 8
1120 #define PADDING_SPACES 25
1123 module_config_t *p_item;
1124 char psz_spaces[PADDING_SPACES+LINE_START+1];
1125 char psz_format[sizeof(FORMAT_STRING)];
1126 char psz_buffer[1000];
1129 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1131 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1132 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1134 strcpy( psz_format, FORMAT_STRING );
1140 /* List all modules */
1141 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1143 /* Enumerate the config for each module */
1144 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1146 vlc_bool_t b_help_module;
1148 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1150 if( psz_module_name && strcmp( psz_module_name,
1151 p_parser->psz_object_name ) )
1156 /* Ignore modules without config options */
1157 if( !p_parser->i_config_items )
1162 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1164 /* Print module options */
1165 for( p_item = p_parser->p_config;
1166 p_item->i_type != CONFIG_HINT_END;
1170 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1171 char *psz_suf = "", *psz_prefix = NULL;
1173 if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1177 switch( p_item->i_type )
1179 case CONFIG_HINT_CATEGORY:
1180 case CONFIG_HINT_USAGE:
1181 fprintf( stdout, " %s\n", p_item->psz_text );
1184 case CONFIG_ITEM_STRING:
1185 case CONFIG_ITEM_FILE:
1186 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1187 if( !p_item->ppsz_list )
1189 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1195 psz_type = psz_buffer;
1197 for( i = 0; p_item->ppsz_list[i]; i++ )
1199 if( i ) strcat( psz_type, "," );
1200 strcat( psz_type, p_item->ppsz_list[i] );
1205 case CONFIG_ITEM_INTEGER:
1206 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1208 case CONFIG_ITEM_FLOAT:
1209 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1211 case CONFIG_ITEM_BOOL:
1212 psz_bra = ""; psz_type = ""; psz_ket = "";
1213 if( !b_help_module )
1215 psz_suf = p_item->i_value ? _(" (default enabled)") :
1216 _(" (default disabled)");
1226 /* Add short option if any */
1227 if( p_item->i_short )
1229 sprintf( psz_short, "-%c,", p_item->i_short );
1233 strcpy( psz_short, " " );
1236 i = PADDING_SPACES - strlen( p_item->psz_name )
1237 - strlen( psz_bra ) - strlen( psz_type )
1238 - strlen( psz_ket ) - 1;
1240 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1242 /* If option is of type --foo-bar, we print its counterpart
1243 * as --no-foo-bar, but if it is of type --foobar (without
1244 * dashes in the name) we print it as --nofoobar. Both
1245 * values are of course valid, only the display changes. */
1246 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1248 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1253 psz_spaces[0] = '\n';
1258 psz_spaces[i] = '\0';
1261 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1263 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1264 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1265 psz_ket, psz_spaces );
1269 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1270 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1273 psz_spaces[i] = ' ';
1275 /* We wrap the rest of the output */
1276 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1277 psz_text = psz_buffer;
1280 char *psz_parser, *psz_word;
1281 int i_end = strlen( psz_text );
1283 /* If the remaining text fits in a line, print it. */
1284 if( i_end <= i_width )
1286 fprintf( stdout, "%s\n", psz_text );
1290 /* Otherwise, eat as many words as possible */
1291 psz_parser = psz_text;
1294 psz_word = psz_parser;
1295 psz_parser = strchr( psz_word, ' ' );
1296 /* If no space was found, we reached the end of the text
1297 * block; otherwise, we skip the space we just found. */
1298 psz_parser = psz_parser ? psz_parser + 1
1301 } while( psz_parser - psz_text <= i_width );
1303 /* We cut a word in one of these cases:
1304 * - it's the only word in the line and it's too long.
1305 * - we used less than 80% of the width and the word we are
1306 * going to wrap is longer than 40% of the width, and even
1307 * if the word would have fit in the next line. */
1308 if( psz_word == psz_text
1309 || ( psz_word - psz_text < 80 * i_width / 100
1310 && psz_parser - psz_word > 40 * i_width / 100 ) )
1312 char c = psz_text[i_width];
1313 psz_text[i_width] = '\0';
1314 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1315 psz_text += i_width;
1320 psz_word[-1] = '\0';
1321 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1322 psz_text = psz_word;
1328 /* Release the module list */
1329 vlc_list_release( p_list );
1331 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1332 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1337 /*****************************************************************************
1338 * ListModules: list the available modules with their description
1339 *****************************************************************************
1340 * Print a list of all available modules (builtins and plugins) and a short
1341 * description for each one.
1342 *****************************************************************************/
1343 static void ListModules( vlc_t *p_this )
1347 char psz_spaces[22];
1350 memset( psz_spaces, ' ', 22 );
1357 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1358 p_this->p_vlc->psz_object_name );
1360 fprintf( stdout, _("[module] [description]\n") );
1362 /* List all modules */
1363 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1365 /* Enumerate each module */
1366 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1370 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1372 /* Nasty hack, but right now I'm too tired to think about a nice
1374 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1378 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1379 psz_spaces, p_parser->psz_longname );
1381 psz_spaces[i] = ' ';
1384 vlc_list_release( p_list );
1386 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1387 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1392 /*****************************************************************************
1393 * Version: print complete program version
1394 *****************************************************************************
1395 * Print complete program version and build number.
1396 *****************************************************************************/
1397 static void Version( void )
1403 fprintf( stdout, VERSION_MESSAGE "\n" );
1405 _("This program comes with NO WARRANTY, to the extent permitted by "
1406 "law.\nYou may redistribute it under the terms of the GNU General "
1407 "Public License;\nsee the file named COPYING for details.\n"
1408 "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1410 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1411 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1416 /*****************************************************************************
1417 * ShowConsole: On Win32, create an output console for debug messages
1418 *****************************************************************************
1419 * This function is useful only on Win32.
1420 *****************************************************************************/
1422 static void ShowConsole( void )
1426 freopen( "CONOUT$", "w", stdout );
1427 freopen( "CONOUT$", "w", stderr );
1428 freopen( "CONIN$", "r", stdin );
1434 /*****************************************************************************
1435 * ConsoleWidth: Return the console width in characters
1436 *****************************************************************************
1437 * We use the stty shell command to get the console width; if this fails or
1438 * if the width is less than 80, we default to 80.
1439 *****************************************************************************/
1440 static int ConsoleWidth( void )
1445 char buf[20], *psz_parser;
1449 file = popen( "stty size 2>/dev/null", "r" );
1452 i_ret = fread( buf, 1, 20, file );
1456 psz_parser = strchr( buf, ' ' );
1459 i_ret = atoi( psz_parser + 1 );