1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
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"
79 #include "stream_output.h"
83 /*****************************************************************************
84 * The evil global variable. We handle it with care, don't worry.
85 *****************************************************************************/
86 static libvlc_t libvlc;
87 static libvlc_t * p_libvlc;
88 static vlc_t * p_static_vlc;
90 /*****************************************************************************
92 *****************************************************************************/
93 static void SetLanguage ( char const * );
94 static int GetFilenames ( vlc_t *, int, char *[] );
95 static void Usage ( vlc_t *, char const *psz_module_name );
96 static void ListModules ( vlc_t * );
97 static void Version ( void );
100 static void ShowConsole ( void );
102 static int ConsoleWidth ( void );
104 static int VerboseCallback( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
107 /*****************************************************************************
108 * vlc_current_object: return the current object.
109 *****************************************************************************
110 * If i_object is non-zero, return the corresponding object. Otherwise,
111 * return the statically allocated p_vlc object.
112 *****************************************************************************/
113 vlc_t * vlc_current_object( int i_object )
117 return vlc_object_get( p_libvlc, i_object );
123 /*****************************************************************************
124 * VLC_Version: return the libvlc version.
125 *****************************************************************************
126 * This function returns full version string (numeric version and codename).
127 *****************************************************************************/
128 char const * VLC_Version( void )
130 return VERSION_MESSAGE;
133 /*****************************************************************************
134 * VLC_Error: strerror() equivalent
135 *****************************************************************************
136 * This function returns full version string (numeric version and codename).
137 *****************************************************************************/
138 char const * VLC_Error( int i_err )
140 return vlc_error( i_err );
143 /*****************************************************************************
144 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
145 *****************************************************************************
146 * This function allocates a vlc_t structure and returns a negative value
147 * in case of failure. Also, the thread system is initialized.
148 *****************************************************************************/
149 int VLC_Create( void )
152 vlc_t * p_vlc = NULL;
155 /* &libvlc never changes, so we can safely call this multiple times. */
158 /* vlc_threads_init *must* be the first internal call! No other call is
159 * allowed before the thread system has been initialized. */
160 i_ret = vlc_threads_init( p_libvlc );
166 /* Now that the thread system is initialized, we don't have much, but
167 * at least we have var_Create */
168 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
169 var_Get( p_libvlc, "libvlc", &lockval );
170 vlc_mutex_lock( lockval.p_address );
171 if( !libvlc.b_ready )
175 /* Guess what CPU we have */
176 libvlc.i_cpu = CPUCapabilities();
178 /* Find verbosity from VLC_VERBOSE environment variable */
179 psz_env = getenv( "VLC_VERBOSE" );
180 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
182 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
183 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
185 libvlc.b_color = VLC_FALSE;
188 /* Initialize message queue */
189 msg_Create( p_libvlc );
191 /* Announce who we are */
192 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
193 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
195 /* The module bank will be initialized later */
196 libvlc.p_module_bank = NULL;
198 libvlc.b_ready = VLC_TRUE;
200 vlc_mutex_unlock( lockval.p_address );
201 var_Destroy( p_libvlc, "libvlc" );
203 /* Allocate a vlc object */
204 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
209 p_vlc->thread_id = 0;
210 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
212 p_vlc->psz_object_name = "root";
214 /* Initialize mutexes */
215 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
217 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
220 /* Store our newly allocated structure in the global list */
221 vlc_object_attach( p_vlc, p_libvlc );
223 /* Store data for the non-reentrant API */
224 p_static_vlc = p_vlc;
226 return p_vlc->i_object_id;
229 /*****************************************************************************
230 * VLC_Init: initialize a vlc_t structure.
231 *****************************************************************************
232 * This function initializes a previously allocated vlc_t structure:
234 * - gettext initialization
235 * - message queue, module bank and playlist initialization
236 * - configuration and commandline parsing
237 *****************************************************************************/
238 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
240 char p_capabilities[200];
245 vlc_bool_t b_exit = VLC_FALSE;
246 vlc_t * p_vlc = vlc_current_object( i_object );
247 module_t *p_help_module;
248 playlist_t *p_playlist;
257 * System specific initialization code
259 system_Init( p_vlc, &i_argc, ppsz_argv );
261 /* Get the executable name (similar to the basename command) */
264 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
267 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
273 p_vlc->psz_object_name = "vlc";
277 * Support for gettext
281 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
282 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
284 /* Initialize the module bank and load the configuration of the
285 * main module. We need to do this at this stage to be able to display
286 * a short help if required by the user. (short help == main module
288 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
289 var_Get( p_libvlc, "libvlc", &lockval );
290 vlc_mutex_lock( lockval.p_address );
291 if( libvlc.p_module_bank == NULL )
293 module_InitBank( p_vlc );
294 module_LoadMain( p_vlc );
296 vlc_mutex_unlock( lockval.p_address );
297 var_Destroy( p_libvlc, "libvlc" );
299 /* Hack: insert the help module here */
300 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
301 if( p_help_module == NULL )
303 /*module_EndBank( p_vlc );*/
304 if( i_object ) vlc_object_release( p_vlc );
307 p_help_module->psz_object_name = "help";
308 p_help_module->psz_longname = N_("Help options");
309 config_Duplicate( p_help_module, p_help_config );
310 vlc_object_attach( p_help_module, libvlc.p_module_bank );
313 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
315 vlc_object_detach( p_help_module );
316 config_Free( p_help_module );
317 vlc_object_destroy( p_help_module );
318 /*module_EndBank( p_vlc );*/
319 if( i_object ) vlc_object_release( p_vlc );
323 /* Check for short help option */
324 if( config_GetInt( p_vlc, "help" ) )
326 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
327 p_vlc->psz_object_name );
328 Usage( p_vlc, "main" );
329 Usage( p_vlc, "help" );
332 /* Check for version option */
333 else if( config_GetInt( p_vlc, "version" ) )
339 /* Set the config file stuff */
340 p_vlc->psz_homedir = config_GetHomeDir();
341 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
343 /* Hack: remove the help module here */
344 vlc_object_detach( p_help_module );
349 config_Free( p_help_module );
350 vlc_object_destroy( p_help_module );
351 /*module_EndBank( p_vlc );*/
352 if( i_object ) vlc_object_release( p_vlc );
356 /* Check for translation config option */
357 #if defined( ENABLE_NLS ) \
358 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
360 /* This ain't really nice to have to reload the config here but it seems
361 * the only way to do it. */
362 config_LoadConfigFile( p_vlc, "main" );
363 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
365 /* Check if the user specified a custom language */
366 psz_language = config_GetPsz( p_vlc, "language" );
367 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
369 /* Reset the default domain */
370 SetLanguage( psz_language );
372 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
373 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
375 textdomain( PACKAGE );
377 #if defined( ENABLE_UTF8 )
378 bind_textdomain_codeset( PACKAGE, "UTF-8" );
381 module_EndBank( p_vlc );
382 module_InitBank( p_vlc );
383 module_LoadMain( p_vlc );
384 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
386 if( psz_language ) free( psz_language );
390 * Load the builtins and plugins into the module_bank.
391 * We have to do it before config_Load*() because this also gets the
392 * list of configuration options exported by each module and loads their
395 module_LoadBuiltins( p_vlc );
396 module_LoadPlugins( p_vlc );
402 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
403 libvlc.p_module_bank->i_children );
405 /* Hack: insert the help module here */
406 vlc_object_attach( p_help_module, libvlc.p_module_bank );
409 /* Check for help on modules */
410 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
412 Usage( p_vlc, p_tmp );
416 /* Check for long help option */
417 else if( config_GetInt( p_vlc, "longhelp" ) )
419 Usage( p_vlc, NULL );
422 /* Check for module list option */
423 else if( config_GetInt( p_vlc, "list" ) )
425 ListModules( p_vlc );
429 /* Check for config file options */
430 if( config_GetInt( p_vlc, "reset-config" ) )
432 vlc_object_detach( p_help_module );
433 config_ResetAll( p_vlc );
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 );
438 if( config_GetInt( p_vlc, "save-config" ) )
440 vlc_object_detach( p_help_module );
441 config_LoadConfigFile( p_vlc, NULL );
442 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
443 config_SaveConfigFile( p_vlc, NULL );
444 vlc_object_attach( p_help_module, libvlc.p_module_bank );
447 /* Hack: remove the help module here */
448 vlc_object_detach( p_help_module );
453 config_Free( p_help_module );
454 vlc_object_destroy( p_help_module );
455 /*module_EndBank( p_vlc );*/
456 if( i_object ) vlc_object_release( p_vlc );
461 * Override default configuration with config file settings
463 config_LoadConfigFile( p_vlc, NULL );
465 /* Hack: insert the help module here */
466 vlc_object_attach( p_help_module, libvlc.p_module_bank );
470 * Override configuration with command line settings
472 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
476 /* Pause the console because it's destroyed when we exit */
477 fprintf( stderr, "The command line options couldn't be loaded, check "
478 "that they are valid.\nPress the RETURN key to continue..." );
481 vlc_object_detach( p_help_module );
482 config_Free( p_help_module );
483 vlc_object_destroy( p_help_module );
484 /*module_EndBank( p_vlc );*/
485 if( i_object ) vlc_object_release( p_vlc );
489 /* Hack: remove the help module here */
490 vlc_object_detach( p_help_module );
491 config_Free( p_help_module );
492 vlc_object_destroy( p_help_module );
496 * System specific configuration
498 system_Configure( p_vlc, &i_argc, ppsz_argv );
501 * Message queue options
504 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505 if( config_GetInt( p_vlc, "quiet" ) )
509 var_Set( p_vlc, "verbose", val );
511 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
512 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
514 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
517 * Output messages that may still be in the queue
521 /* p_vlc initialization. FIXME ? */
523 #if defined( __i386__ )
524 if( !config_GetInt( p_vlc, "mmx" ) )
525 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
526 if( !config_GetInt( p_vlc, "3dn" ) )
527 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
528 if( !config_GetInt( p_vlc, "mmxext" ) )
529 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
530 if( !config_GetInt( p_vlc, "sse" ) )
531 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
532 if( !config_GetInt( p_vlc, "sse2" ) )
533 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
535 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
536 if( !config_GetInt( p_vlc, "altivec" ) )
537 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
540 #define PRINT_CAPABILITY( capability, string ) \
541 if( libvlc.i_cpu & capability ) \
543 strncat( p_capabilities, string " ", \
544 sizeof(p_capabilities) - strlen(p_capabilities) ); \
545 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
548 p_capabilities[0] = '\0';
549 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
554 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
555 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
556 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
557 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
558 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
559 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
562 * Choose the best memcpy module
564 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
566 if( p_vlc->pf_memcpy == NULL )
568 p_vlc->pf_memcpy = memcpy;
571 if( p_vlc->pf_memset == NULL )
573 p_vlc->pf_memset = memset;
577 * Initialize hotkey handling
579 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
580 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
581 /* Do a copy (we don't need to modify the strings) */
582 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
585 * Initialize playlist and get commandline files
587 p_playlist = playlist_Create( p_vlc );
590 msg_Err( p_vlc, "playlist initialization failed" );
591 if( p_vlc->p_memcpy_module != NULL )
593 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
595 /*module_EndBank( p_vlc );*/
596 if( i_object ) vlc_object_release( p_vlc );
601 * Load background interfaces
603 psz_modules = config_GetPsz( p_vlc, "extraintf" );
604 psz_parser = psz_modules;
605 while ( psz_parser && *psz_parser )
607 char *psz_module, *psz_temp;
608 psz_module = psz_parser;
609 psz_parser = strchr( psz_module, ',' );
615 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
618 sprintf( psz_temp, "%s,none", psz_module );
619 VLC_AddIntf( 0, psz_temp, VLC_FALSE );
629 * Allways load the hotkeys interface if it exists
631 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
634 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
636 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
644 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
645 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
646 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
647 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
650 * Get input filenames given as commandline arguments
652 GetFilenames( p_vlc, i_argc, ppsz_argv );
654 if( i_object ) vlc_object_release( p_vlc );
658 /*****************************************************************************
659 * VLC_AddIntf: add an interface
660 *****************************************************************************
661 * This function opens an interface plugin and runs it. If b_block is set
662 * to 0, VLC_AddIntf will return immediately and let the interface run in a
663 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
664 * user requests to quit.
665 *****************************************************************************/
666 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
669 intf_thread_t *p_intf;
670 vlc_t *p_vlc = vlc_current_object( i_object );
677 /* Try to create the interface */
678 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
682 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
683 if( i_object ) vlc_object_release( p_vlc );
687 /* Try to run the interface */
688 p_intf->b_block = b_block;
689 i_err = intf_RunThread( p_intf );
692 vlc_object_detach( p_intf );
693 intf_Destroy( p_intf );
694 if( i_object ) vlc_object_release( p_vlc );
698 if( i_object ) vlc_object_release( p_vlc );
702 /*****************************************************************************
703 * VLC_Destroy: stop playing and destroy everything.
704 *****************************************************************************
705 * This function requests the running threads to finish, waits for their
706 * termination, and destroys their structure.
707 *****************************************************************************/
708 int VLC_Destroy( int i_object )
710 vlc_t *p_vlc = vlc_current_object( i_object );
718 * Free allocated memory
720 if( p_vlc->p_memcpy_module )
722 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
723 p_vlc->p_memcpy_module = NULL;
726 if( p_vlc->psz_homedir )
728 free( p_vlc->psz_homedir );
729 p_vlc->psz_homedir = NULL;
732 if( p_vlc->psz_configfile )
734 free( p_vlc->psz_configfile );
735 p_vlc->psz_configfile = NULL;
738 if( p_vlc->p_hotkeys )
740 free( p_vlc->p_hotkeys );
741 p_vlc->p_hotkeys = NULL;
745 * XXX: Free module bank !
747 /*module_EndBank( p_vlc );*/
750 * System specific cleaning code
754 /* Destroy mutexes */
755 vlc_mutex_destroy( &p_vlc->config_lock );
757 vlc_mutex_destroy( &p_vlc->quicktime_lock );
760 vlc_object_detach( p_vlc );
762 /* Release object before destroying it */
763 if( i_object ) vlc_object_release( p_vlc );
765 vlc_object_destroy( p_vlc );
767 /* Stop thread system: last one out please shut the door! */
768 vlc_threads_end( p_libvlc );
773 /*****************************************************************************
774 * VLC_Die: ask vlc to die.
775 *****************************************************************************
776 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
777 * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
778 *****************************************************************************/
779 int VLC_Die( int i_object )
781 vlc_t *p_vlc = vlc_current_object( i_object );
788 p_vlc->b_die = VLC_TRUE;
790 if( i_object ) vlc_object_release( p_vlc );
794 /*****************************************************************************
795 * VLC_AddTarget: adds a target for playing.
796 *****************************************************************************
797 * This function adds psz_target to the current playlist. If a playlist does
798 * not exist, it will create one.
799 *****************************************************************************/
800 int VLC_AddTarget( int i_object, char const *psz_target,
801 char const **ppsz_options, int i_options,
802 int i_mode, int i_pos )
805 playlist_t *p_playlist;
806 vlc_t *p_vlc = vlc_current_object( i_object );
813 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
815 if( p_playlist == NULL )
817 msg_Dbg( p_vlc, "no playlist present, creating one" );
818 p_playlist = playlist_Create( p_vlc );
820 if( p_playlist == NULL )
822 if( i_object ) vlc_object_release( p_vlc );
826 vlc_object_yield( p_playlist );
829 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
830 i_mode, i_pos, -1, ppsz_options, i_options);
832 vlc_object_release( p_playlist );
834 if( i_object ) vlc_object_release( p_vlc );
838 /*****************************************************************************
839 * VLC_Set: set a vlc variable
840 *****************************************************************************
842 *****************************************************************************/
843 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
845 vlc_t *p_vlc = vlc_current_object( i_object );
853 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
854 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
855 if( !strncmp( psz_var, "conf::", 6 ) )
857 module_config_t *p_item;
858 char const *psz_newvar = psz_var + 6;
860 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
864 switch( p_item->i_type )
866 case CONFIG_ITEM_BOOL:
867 config_PutInt( p_vlc, psz_newvar, value.b_bool );
869 case CONFIG_ITEM_INTEGER:
870 config_PutInt( p_vlc, psz_newvar, value.i_int );
872 case CONFIG_ITEM_FLOAT:
873 config_PutFloat( p_vlc, psz_newvar, value.f_float );
876 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
879 if( i_object ) vlc_object_release( p_vlc );
884 i_ret = var_Set( p_vlc, psz_var, value );
886 if( i_object ) vlc_object_release( p_vlc );
890 /*****************************************************************************
891 * VLC_Get: get a vlc variable
892 *****************************************************************************
894 *****************************************************************************/
895 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
897 vlc_t *p_vlc = vlc_current_object( i_object );
905 i_ret = var_Get( p_vlc, psz_var, p_value );
907 if( i_object ) vlc_object_release( p_vlc );
911 /* FIXME: temporary hacks */
914 /*****************************************************************************
915 * VLC_IsPlaying: Query for Playlist Status
916 *****************************************************************************/
917 vlc_bool_t VLC_IsPlaying( int i_object )
920 playlist_t * p_playlist;
923 vlc_t *p_vlc = vlc_current_object( i_object );
925 /* Check that the handle is valid */
931 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
935 if( i_object ) vlc_object_release( p_vlc );
939 playing = playlist_IsPlaying( p_playlist );
941 vlc_object_release( p_playlist );
943 if( i_object ) vlc_object_release( p_vlc );
950 /*****************************************************************************
951 * VLC_ClearPlaylist: Query for Playlist Status
954 *****************************************************************************/
955 int VLC_ClearPlaylist( int i_object )
958 playlist_t * p_playlist;
959 vlc_t *p_vlc = vlc_current_object( i_object );
961 /* Check that the handle is valid */
967 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
971 if( i_object ) vlc_object_release( p_vlc );
975 playlist_Clear(p_playlist);
977 vlc_object_release( p_playlist );
979 if( i_object ) vlc_object_release( p_vlc );
984 /*****************************************************************************
986 *****************************************************************************/
987 int VLC_Play( int i_object )
989 playlist_t * p_playlist;
990 vlc_t *p_vlc = vlc_current_object( i_object );
992 /* Check that the handle is valid */
998 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1002 if( i_object ) vlc_object_release( p_vlc );
1006 vlc_mutex_lock( &p_playlist->object_lock );
1007 if( p_playlist->i_size )
1009 vlc_mutex_unlock( &p_playlist->object_lock );
1010 playlist_Play( p_playlist );
1014 vlc_mutex_unlock( &p_playlist->object_lock );
1017 vlc_object_release( p_playlist );
1019 if( i_object ) vlc_object_release( p_vlc );
1023 /*****************************************************************************
1025 *****************************************************************************/
1026 int VLC_Stop( int i_object )
1028 intf_thread_t * p_intf;
1029 playlist_t * p_playlist;
1030 vout_thread_t * p_vout;
1031 aout_instance_t * p_aout;
1032 announce_handler_t * p_announce;
1033 vlc_t *p_vlc = vlc_current_object( i_object );
1035 /* Check that the handle is valid */
1042 * Ask the interfaces to stop and destroy them
1044 msg_Dbg( p_vlc, "removing all interfaces" );
1046 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1048 intf_StopThread( p_intf );
1049 vlc_object_detach( p_intf );
1050 vlc_object_release( p_intf );
1051 intf_Destroy( p_intf );
1058 msg_Dbg( p_vlc, "removing all playlists" );
1059 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1062 vlc_object_detach( p_playlist );
1063 vlc_object_release( p_playlist );
1064 playlist_Destroy( p_playlist );
1068 * Free video outputs
1070 msg_Dbg( p_vlc, "removing all video outputs" );
1071 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1073 vlc_object_detach( p_vout );
1074 vlc_object_release( p_vout );
1075 vout_Destroy( p_vout );
1079 * Free audio outputs
1081 msg_Dbg( p_vlc, "removing all audio outputs" );
1082 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1084 vlc_object_detach( (vlc_object_t *)p_aout );
1085 vlc_object_release( (vlc_object_t *)p_aout );
1086 aout_Delete( p_aout );
1090 * Free announce handler(s?)
1092 msg_Dbg( p_vlc, "removing announce handler" );
1093 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
1096 vlc_object_detach( p_announce );
1097 vlc_object_release( p_announce );
1098 announce_HandlerDestroy( p_announce );
1101 if( i_object ) vlc_object_release( p_vlc );
1105 /*****************************************************************************
1106 * VLC_Pause: toggle pause
1107 *****************************************************************************/
1108 int VLC_Pause( int i_object )
1110 input_thread_t *p_input;
1111 vlc_t *p_vlc = vlc_current_object( i_object );
1118 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1122 if( i_object ) vlc_object_release( p_vlc );
1126 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1127 vlc_object_release( p_input );
1129 if( i_object ) vlc_object_release( p_vlc );
1133 /*****************************************************************************
1134 * VLC_FullScreen: toggle fullscreen mode
1135 *****************************************************************************/
1136 int VLC_FullScreen( int i_object )
1138 vout_thread_t *p_vout;
1139 vlc_t *p_vlc = vlc_current_object( i_object );
1146 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1150 if( i_object ) vlc_object_release( p_vlc );
1154 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1155 vlc_object_release( p_vout );
1157 if( i_object ) vlc_object_release( p_vlc );
1161 /* following functions are local */
1163 /*****************************************************************************
1164 * SetLanguage: set the interface language.
1165 *****************************************************************************
1166 * We set the LC_MESSAGES locale category for interface messages and buttons,
1167 * as well as the LC_CTYPE category for string sorting and possible wide
1168 * character support.
1169 *****************************************************************************/
1170 static void SetLanguage ( char const *psz_lang )
1172 #if defined( ENABLE_NLS ) \
1173 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1176 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1180 if( psz_lang && !*psz_lang )
1182 # if defined( HAVE_LC_MESSAGES )
1183 setlocale( LC_MESSAGES, psz_lang );
1185 setlocale( LC_CTYPE, psz_lang );
1190 /* I need that under Darwin, please check it doesn't disturb
1191 * other platforms. --Meuuh */
1192 setenv( "LANG", psz_lang, 1 );
1194 #elif defined( SYS_BEOS ) || defined( WIN32 )
1195 /* We set LC_ALL manually because it is the only way to set
1196 * the language at runtime under eg. Windows. Beware that this
1197 * makes the environment unconsistent when libvlc is unloaded and
1198 * should probably be moved to a safer place like vlc.c. */
1199 static char psz_lcall[20];
1200 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1201 psz_lcall[19] = '\0';
1202 putenv( psz_lcall );
1205 setlocale( LC_ALL, psz_lang );
1208 /* Specify where to find the locales for current domain */
1209 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1210 psz_path = LOCALEDIR;
1212 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1216 if( !bindtextdomain( PACKAGE, psz_path ) )
1218 fprintf( stderr, "warning: no domain %s in directory %s\n",
1219 PACKAGE, psz_path );
1222 /* Set the default domain */
1223 textdomain( PACKAGE );
1225 #if defined( ENABLE_UTF8 )
1226 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1232 /*****************************************************************************
1233 * GetFilenames: parse command line options which are not flags
1234 *****************************************************************************
1235 * Parse command line for input files as well as their associated options.
1236 * An option always follows its associated input and begins with a ":".
1237 *****************************************************************************/
1238 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1240 int i_opt, i_options;
1242 /* We assume that the remaining parameters are filenames
1243 * and their input options */
1244 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1248 /* Count the input options */
1249 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1255 /* TODO: write an internal function of this one, to avoid
1256 * unnecessary lookups. */
1257 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1258 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1260 PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1267 /*****************************************************************************
1268 * Usage: print program usage
1269 *****************************************************************************
1270 * Print a short inline help. Message interface is initialized at this stage.
1271 *****************************************************************************/
1272 static void Usage( vlc_t *p_this, char const *psz_module_name )
1274 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1275 /* short option ------' | | | | | | |
1276 * option name ------------' | | | | | |
1277 * <bra -------------------------' | | | | |
1278 * option type or "" --------------' | | | |
1279 * ket> -----------------------------' | | |
1280 * padding spaces ---------------------' | |
1281 * comment -------------------------------' |
1282 * comment suffix --------------------------'
1284 * The purpose of having bra and ket is that we might i18n them as well.
1286 #define LINE_START 8
1287 #define PADDING_SPACES 25
1290 module_config_t *p_item;
1291 char psz_spaces[PADDING_SPACES+LINE_START+1];
1292 char psz_format[sizeof(FORMAT_STRING)];
1293 char psz_buffer[1000];
1296 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1297 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1299 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1300 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1302 strcpy( psz_format, FORMAT_STRING );
1308 /* List all modules */
1309 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1311 /* Enumerate the config for each module */
1312 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1314 vlc_bool_t b_help_module;
1316 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1318 if( psz_module_name && strcmp( psz_module_name,
1319 p_parser->psz_object_name ) )
1324 /* Ignore modules without config options */
1325 if( !p_parser->i_config_items )
1330 /* Ignore modules with only advanced config options if requested */
1333 for( p_item = p_parser->p_config;
1334 p_item->i_type != CONFIG_HINT_END;
1337 if( (p_item->i_type & CONFIG_ITEM) &&
1338 !p_item->b_advanced ) break;
1340 if( p_item->i_type == CONFIG_HINT_END ) continue;
1343 /* Print name of module */
1344 if( strcmp( "main", p_parser->psz_object_name ) )
1345 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1347 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1349 /* Print module options */
1350 for( p_item = p_parser->p_config;
1351 p_item->i_type != CONFIG_HINT_END;
1355 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1356 char *psz_suf = "", *psz_prefix = NULL;
1359 /* Skip advanced options if requested */
1360 if( p_item->b_advanced && !b_advanced )
1365 switch( p_item->i_type )
1367 case CONFIG_HINT_CATEGORY:
1368 case CONFIG_HINT_USAGE:
1369 if( !strcmp( "main", p_parser->psz_object_name ) )
1370 fprintf( stdout, "\n %s\n", p_item->psz_text );
1373 case CONFIG_ITEM_STRING:
1374 case CONFIG_ITEM_FILE:
1375 case CONFIG_ITEM_DIRECTORY:
1376 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1377 if( !p_item->ppsz_list )
1379 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1385 psz_type = psz_buffer;
1387 for( i = 0; p_item->ppsz_list[i]; i++ )
1389 if( i ) strcat( psz_type, "," );
1390 strcat( psz_type, p_item->ppsz_list[i] );
1395 case CONFIG_ITEM_INTEGER:
1396 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1397 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1399 case CONFIG_ITEM_FLOAT:
1400 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1402 case CONFIG_ITEM_BOOL:
1403 psz_bra = ""; psz_type = ""; psz_ket = "";
1404 if( !b_help_module )
1406 psz_suf = p_item->i_value ? _(" (default enabled)") :
1407 _(" (default disabled)");
1417 /* Add short option if any */
1418 if( p_item->i_short )
1420 sprintf( psz_short, "-%c,", p_item->i_short );
1424 strcpy( psz_short, " " );
1427 i = PADDING_SPACES - strlen( p_item->psz_name )
1428 - strlen( psz_bra ) - strlen( psz_type )
1429 - strlen( psz_ket ) - 1;
1431 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1433 /* If option is of type --foo-bar, we print its counterpart
1434 * as --no-foo-bar, but if it is of type --foobar (without
1435 * dashes in the name) we print it as --nofoobar. Both
1436 * values are of course valid, only the display changes. */
1437 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1439 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1444 psz_spaces[0] = '\n';
1449 psz_spaces[i] = '\0';
1452 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1454 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1455 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1456 psz_ket, psz_spaces );
1460 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1461 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1464 psz_spaces[i] = ' ';
1466 /* We wrap the rest of the output */
1467 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1468 psz_text = psz_buffer;
1471 char *psz_parser, *psz_word;
1472 int i_end = strlen( psz_text );
1474 /* If the remaining text fits in a line, print it. */
1475 if( i_end <= i_width )
1477 fprintf( stdout, "%s\n", psz_text );
1481 /* Otherwise, eat as many words as possible */
1482 psz_parser = psz_text;
1485 psz_word = psz_parser;
1486 psz_parser = strchr( psz_word, ' ' );
1487 /* If no space was found, we reached the end of the text
1488 * block; otherwise, we skip the space we just found. */
1489 psz_parser = psz_parser ? psz_parser + 1
1492 } while( psz_parser - psz_text <= i_width );
1494 /* We cut a word in one of these cases:
1495 * - it's the only word in the line and it's too long.
1496 * - we used less than 80% of the width and the word we are
1497 * going to wrap is longer than 40% of the width, and even
1498 * if the word would have fit in the next line. */
1499 if( psz_word == psz_text
1500 || ( psz_word - psz_text < 80 * i_width / 100
1501 && psz_parser - psz_word > 40 * i_width / 100 ) )
1503 char c = psz_text[i_width];
1504 psz_text[i_width] = '\0';
1505 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1506 psz_text += i_width;
1511 psz_word[-1] = '\0';
1512 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1513 psz_text = psz_word;
1519 /* Release the module list */
1520 vlc_list_release( p_list );
1522 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1523 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1528 /*****************************************************************************
1529 * ListModules: list the available modules with their description
1530 *****************************************************************************
1531 * Print a list of all available modules (builtins and plugins) and a short
1532 * description for each one.
1533 *****************************************************************************/
1534 static void ListModules( vlc_t *p_this )
1538 char psz_spaces[22];
1541 memset( psz_spaces, ' ', 22 );
1548 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1549 p_this->p_vlc->psz_object_name );
1551 fprintf( stdout, _("[module] [description]\n") );
1553 /* List all modules */
1554 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1556 /* Enumerate each module */
1557 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1561 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1563 /* Nasty hack, but right now I'm too tired to think about a nice
1565 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1569 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1570 psz_spaces, p_parser->psz_longname );
1572 psz_spaces[i] = ' ';
1575 vlc_list_release( p_list );
1577 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1578 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1583 /*****************************************************************************
1584 * Version: print complete program version
1585 *****************************************************************************
1586 * Print complete program version and build number.
1587 *****************************************************************************/
1588 static void Version( void )
1594 fprintf( stdout, VERSION_MESSAGE "\n" );
1596 _("This program comes with NO WARRANTY, to the extent permitted by "
1597 "law.\nYou may redistribute it under the terms of the GNU General "
1598 "Public License;\nsee the file named COPYING for details.\n"
1599 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1601 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1602 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1607 /*****************************************************************************
1608 * ShowConsole: On Win32, create an output console for debug messages
1609 *****************************************************************************
1610 * This function is useful only on Win32.
1611 *****************************************************************************/
1613 static void ShowConsole( void )
1617 freopen( "CONOUT$", "w", stdout );
1618 freopen( "CONOUT$", "w", stderr );
1619 freopen( "CONIN$", "r", stdin );
1625 /*****************************************************************************
1626 * ConsoleWidth: Return the console width in characters
1627 *****************************************************************************
1628 * We use the stty shell command to get the console width; if this fails or
1629 * if the width is less than 80, we default to 80.
1630 *****************************************************************************/
1631 static int ConsoleWidth( void )
1636 char buf[20], *psz_parser;
1640 file = popen( "stty size 2>/dev/null", "r" );
1643 i_ret = fread( buf, 1, 20, file );
1647 psz_parser = strchr( buf, ' ' );
1650 i_ret = atoi( psz_parser + 1 );
1665 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1666 vlc_value_t old_val, vlc_value_t new_val, void *param)
1668 vlc_t *p_vlc = (vlc_t *)p_this;
1670 if( new_val.i_int >= -1 )
1672 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );