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;
256 * System specific initialization code
258 system_Init( p_vlc, &i_argc, ppsz_argv );
260 /* Get the executable name (similar to the basename command) */
263 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
266 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
272 p_vlc->psz_object_name = "vlc";
276 * Support for gettext
280 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
281 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
283 /* Initialize the module bank and load the configuration of the
284 * main module. We need to do this at this stage to be able to display
285 * a short help if required by the user. (short help == main module
287 module_InitBank( p_vlc );
289 /* Hack: insert the help module here */
290 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
291 if( p_help_module == NULL )
293 module_EndBank( p_vlc );
294 if( i_object ) vlc_object_release( p_vlc );
297 p_help_module->psz_object_name = "help";
298 p_help_module->psz_longname = N_("Help options");
299 config_Duplicate( p_help_module, p_help_config );
300 vlc_object_attach( p_help_module, libvlc.p_module_bank );
303 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
305 vlc_object_detach( p_help_module );
306 config_Free( p_help_module );
307 vlc_object_destroy( p_help_module );
308 module_EndBank( p_vlc );
309 if( i_object ) vlc_object_release( p_vlc );
313 /* Check for short help option */
314 if( config_GetInt( p_vlc, "help" ) )
316 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
317 p_vlc->psz_object_name );
318 Usage( p_vlc, "main" );
319 Usage( p_vlc, "help" );
322 /* Check for version option */
323 else if( config_GetInt( p_vlc, "version" ) )
329 /* Set the config file stuff */
330 p_vlc->psz_homedir = config_GetHomeDir();
331 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
333 /* Check for plugins cache options */
334 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
336 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
339 /* Hack: remove the help module here */
340 vlc_object_detach( p_help_module );
345 config_Free( p_help_module );
346 vlc_object_destroy( p_help_module );
347 module_EndBank( p_vlc );
348 if( i_object ) vlc_object_release( p_vlc );
352 /* Check for translation config option */
353 #if defined( ENABLE_NLS ) \
354 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
356 /* This ain't really nice to have to reload the config here but it seems
357 * the only way to do it. */
358 config_LoadConfigFile( p_vlc, "main" );
359 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
361 /* Check if the user specified a custom language */
362 psz_language = config_GetPsz( p_vlc, "language" );
363 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
365 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
367 /* Reset the default domain */
368 SetLanguage( psz_language );
370 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
371 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
373 textdomain( PACKAGE );
375 #if defined( ENABLE_UTF8 )
376 bind_textdomain_codeset( PACKAGE, "UTF-8" );
379 module_EndBank( p_vlc );
380 module_InitBank( p_vlc );
381 config_LoadConfigFile( p_vlc, "main" );
382 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
383 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
385 if( psz_language ) free( psz_language );
389 * Load the builtins and plugins into the module_bank.
390 * We have to do it before config_Load*() because this also gets the
391 * list of configuration options exported by each module and loads their
394 module_LoadBuiltins( p_vlc );
395 module_LoadPlugins( p_vlc );
401 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
402 libvlc.p_module_bank->i_children );
404 /* Hack: insert the help module here */
405 vlc_object_attach( p_help_module, libvlc.p_module_bank );
408 /* Check for help on modules */
409 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
411 Usage( p_vlc, p_tmp );
415 /* Check for long help option */
416 else if( config_GetInt( p_vlc, "longhelp" ) )
418 Usage( p_vlc, NULL );
421 /* Check for module list option */
422 else if( config_GetInt( p_vlc, "list" ) )
424 ListModules( p_vlc );
428 /* Check for config file options */
429 if( config_GetInt( p_vlc, "reset-config" ) )
431 vlc_object_detach( p_help_module );
432 config_ResetAll( p_vlc );
433 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
434 config_SaveConfigFile( p_vlc, NULL );
435 vlc_object_attach( p_help_module, libvlc.p_module_bank );
437 if( config_GetInt( p_vlc, "save-config" ) )
439 vlc_object_detach( p_help_module );
440 config_LoadConfigFile( p_vlc, NULL );
441 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
442 config_SaveConfigFile( p_vlc, NULL );
443 vlc_object_attach( p_help_module, libvlc.p_module_bank );
446 /* Hack: remove the help module here */
447 vlc_object_detach( p_help_module );
452 config_Free( p_help_module );
453 vlc_object_destroy( p_help_module );
454 module_EndBank( p_vlc );
455 if( i_object ) vlc_object_release( p_vlc );
460 * Override default configuration with config file settings
462 config_LoadConfigFile( p_vlc, NULL );
464 /* Hack: insert the help module here */
465 vlc_object_attach( p_help_module, libvlc.p_module_bank );
469 * Override configuration with command line settings
471 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
475 /* Pause the console because it's destroyed when we exit */
476 fprintf( stderr, "The command line options couldn't be loaded, check "
477 "that they are valid.\nPress the RETURN key to continue..." );
480 vlc_object_detach( p_help_module );
481 config_Free( p_help_module );
482 vlc_object_destroy( p_help_module );
483 module_EndBank( p_vlc );
484 if( i_object ) vlc_object_release( p_vlc );
488 /* Hack: remove the help module here */
489 vlc_object_detach( p_help_module );
490 config_Free( p_help_module );
491 vlc_object_destroy( p_help_module );
495 * System specific configuration
497 system_Configure( p_vlc, &i_argc, ppsz_argv );
500 * Message queue options
503 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504 if( config_GetInt( p_vlc, "quiet" ) )
508 var_Set( p_vlc, "verbose", val );
510 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
511 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
513 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
516 * Output messages that may still be in the queue
520 /* p_vlc initialization. FIXME ? */
522 #if defined( __i386__ )
523 if( !config_GetInt( p_vlc, "mmx" ) )
524 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
525 if( !config_GetInt( p_vlc, "3dn" ) )
526 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
527 if( !config_GetInt( p_vlc, "mmxext" ) )
528 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
529 if( !config_GetInt( p_vlc, "sse" ) )
530 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
531 if( !config_GetInt( p_vlc, "sse2" ) )
532 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
534 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
535 if( !config_GetInt( p_vlc, "altivec" ) )
536 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
539 #define PRINT_CAPABILITY( capability, string ) \
540 if( libvlc.i_cpu & capability ) \
542 strncat( p_capabilities, string " ", \
543 sizeof(p_capabilities) - strlen(p_capabilities) ); \
544 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
547 p_capabilities[0] = '\0';
548 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
549 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
550 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
551 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
552 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
553 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
554 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
555 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
556 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
557 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
558 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
561 * Choose the best memcpy module
563 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
565 if( p_vlc->pf_memcpy == NULL )
567 p_vlc->pf_memcpy = memcpy;
570 if( p_vlc->pf_memset == NULL )
572 p_vlc->pf_memset = memset;
576 * Initialize hotkey handling
578 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
579 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
580 /* Do a copy (we don't need to modify the strings) */
581 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
584 * Initialize playlist and get commandline files
586 p_playlist = playlist_Create( p_vlc );
589 msg_Err( p_vlc, "playlist initialization failed" );
590 if( p_vlc->p_memcpy_module != NULL )
592 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
594 module_EndBank( p_vlc );
595 if( i_object ) vlc_object_release( p_vlc );
600 * Load background interfaces
602 psz_modules = config_GetPsz( p_vlc, "extraintf" );
603 psz_parser = psz_modules;
604 while ( psz_parser && *psz_parser )
606 char *psz_module, *psz_temp;
607 psz_module = psz_parser;
608 psz_parser = strchr( psz_module, ',' );
614 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
617 sprintf( psz_temp, "%s,none", psz_module );
618 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
628 * Allways load the hotkeys interface if it exists
630 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
633 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
635 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
636 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
637 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
638 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
639 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
640 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
641 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
642 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
643 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
644 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
645 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
646 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
649 * Get input filenames given as commandline arguments
651 GetFilenames( p_vlc, i_argc, ppsz_argv );
653 if( i_object ) vlc_object_release( p_vlc );
657 /*****************************************************************************
658 * VLC_AddIntf: add an interface
659 *****************************************************************************
660 * This function opens an interface plugin and runs it. If b_block is set
661 * to 0, VLC_AddIntf will return immediately and let the interface run in a
662 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
663 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
664 * the playlist when it is completely initialised.
665 *****************************************************************************/
666 int VLC_AddIntf( int i_object, char const *psz_module,
667 vlc_bool_t b_block, vlc_bool_t b_play )
670 intf_thread_t *p_intf;
671 vlc_t *p_vlc = vlc_current_object( i_object );
678 /* Try to create the interface */
679 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
683 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
684 if( i_object ) vlc_object_release( p_vlc );
688 /* Interface doesn't handle play on start so do it ourselves */
689 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
691 /* Try to run the interface */
692 p_intf->b_play = b_play;
693 p_intf->b_block = b_block;
694 i_err = intf_RunThread( p_intf );
697 vlc_object_detach( p_intf );
698 intf_Destroy( p_intf );
699 if( i_object ) vlc_object_release( p_vlc );
703 if( i_object ) vlc_object_release( p_vlc );
707 /*****************************************************************************
708 * VLC_Die: ask vlc to die.
709 *****************************************************************************
710 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
711 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
712 *****************************************************************************/
713 int VLC_Die( int i_object )
715 vlc_t *p_vlc = vlc_current_object( i_object );
722 p_vlc->b_die = VLC_TRUE;
724 if( i_object ) vlc_object_release( p_vlc );
728 /*****************************************************************************
729 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
730 *****************************************************************************/
731 int VLC_CleanUp( int i_object )
733 intf_thread_t * p_intf;
734 playlist_t * p_playlist;
735 vout_thread_t * p_vout;
736 aout_instance_t * p_aout;
737 announce_handler_t * p_announce;
738 vlc_t *p_vlc = vlc_current_object( i_object );
740 /* Check that the handle is valid */
747 * Ask the interfaces to stop and destroy them
749 msg_Dbg( p_vlc, "removing all interfaces" );
750 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
752 intf_StopThread( p_intf );
753 vlc_object_detach( p_intf );
754 vlc_object_release( p_intf );
755 intf_Destroy( p_intf );
761 msg_Dbg( p_vlc, "removing all playlists" );
762 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
765 vlc_object_detach( p_playlist );
766 vlc_object_release( p_playlist );
767 playlist_Destroy( p_playlist );
773 msg_Dbg( p_vlc, "removing all video outputs" );
774 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
776 vlc_object_detach( p_vout );
777 vlc_object_release( p_vout );
778 vout_Destroy( p_vout );
784 msg_Dbg( p_vlc, "removing all audio outputs" );
785 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
787 vlc_object_detach( (vlc_object_t *)p_aout );
788 vlc_object_release( (vlc_object_t *)p_aout );
789 aout_Delete( p_aout );
793 * Free announce handler(s?)
795 msg_Dbg( p_vlc, "removing announce handler" );
796 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
799 vlc_object_detach( p_announce );
800 vlc_object_release( p_announce );
801 announce_HandlerDestroy( p_announce );
804 if( i_object ) vlc_object_release( p_vlc );
808 /*****************************************************************************
809 * VLC_Destroy: Destroy everything.
810 *****************************************************************************
811 * This function requests the running threads to finish, waits for their
812 * termination, and destroys their structure.
813 *****************************************************************************/
814 int VLC_Destroy( int i_object )
816 vlc_t *p_vlc = vlc_current_object( i_object );
824 * Free allocated memory
826 if( p_vlc->p_memcpy_module )
828 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
829 p_vlc->p_memcpy_module = NULL;
835 module_EndBank( p_vlc );
837 if( p_vlc->psz_homedir )
839 free( p_vlc->psz_homedir );
840 p_vlc->psz_homedir = NULL;
843 if( p_vlc->psz_configfile )
845 free( p_vlc->psz_configfile );
846 p_vlc->psz_configfile = NULL;
849 if( p_vlc->p_hotkeys )
851 free( p_vlc->p_hotkeys );
852 p_vlc->p_hotkeys = NULL;
856 * System specific cleaning code
860 /* Destroy mutexes */
861 vlc_mutex_destroy( &p_vlc->config_lock );
863 vlc_object_detach( p_vlc );
865 /* Release object before destroying it */
866 if( i_object ) vlc_object_release( p_vlc );
868 vlc_object_destroy( p_vlc );
870 /* Stop thread system: last one out please shut the door! */
871 vlc_threads_end( p_libvlc );
876 /*****************************************************************************
877 * VLC_Set: set a vlc variable
878 *****************************************************************************/
879 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
881 vlc_t *p_vlc = vlc_current_object( i_object );
889 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
890 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
891 if( !strncmp( psz_var, "conf::", 6 ) )
893 module_config_t *p_item;
894 char const *psz_newvar = psz_var + 6;
896 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
900 switch( p_item->i_type )
902 case CONFIG_ITEM_BOOL:
903 config_PutInt( p_vlc, psz_newvar, value.b_bool );
905 case CONFIG_ITEM_INTEGER:
906 config_PutInt( p_vlc, psz_newvar, value.i_int );
908 case CONFIG_ITEM_FLOAT:
909 config_PutFloat( p_vlc, psz_newvar, value.f_float );
912 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
915 if( i_object ) vlc_object_release( p_vlc );
920 i_ret = var_Set( p_vlc, psz_var, value );
922 if( i_object ) vlc_object_release( p_vlc );
926 /*****************************************************************************
927 * VLC_Get: get a vlc variable
928 *****************************************************************************/
929 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
931 vlc_t *p_vlc = vlc_current_object( i_object );
939 i_ret = var_Get( p_vlc, psz_var, p_value );
941 if( i_object ) vlc_object_release( p_vlc );
945 /*****************************************************************************
946 * VLC_AddTarget: adds a target for playing.
947 *****************************************************************************
948 * This function adds psz_target to the current playlist. If a playlist does
949 * not exist, it will create one.
950 *****************************************************************************/
951 int VLC_AddTarget( int i_object, char const *psz_target,
952 char const **ppsz_options, int i_options,
953 int i_mode, int i_pos )
956 playlist_t *p_playlist;
957 vlc_t *p_vlc = vlc_current_object( i_object );
964 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
966 if( p_playlist == NULL )
968 msg_Dbg( p_vlc, "no playlist present, creating one" );
969 p_playlist = playlist_Create( p_vlc );
971 if( p_playlist == NULL )
973 if( i_object ) vlc_object_release( p_vlc );
977 vlc_object_yield( p_playlist );
980 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
981 i_mode, i_pos, -1, ppsz_options, i_options);
983 vlc_object_release( p_playlist );
985 if( i_object ) vlc_object_release( p_vlc );
989 /*****************************************************************************
991 *****************************************************************************/
992 int VLC_Play( int i_object )
995 playlist_t * p_playlist;
996 vlc_t *p_vlc = vlc_current_object( i_object );
998 /* Check that the handle is valid */
1004 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1008 if( i_object ) vlc_object_release( p_vlc );
1012 i_err = playlist_Play( p_playlist );
1013 vlc_object_release( p_playlist );
1015 if( i_object ) vlc_object_release( p_vlc );
1019 /*****************************************************************************
1020 * VLC_Pause: toggle pause
1021 *****************************************************************************/
1022 int VLC_Pause( int i_object )
1025 playlist_t * p_playlist;
1026 vlc_t *p_vlc = vlc_current_object( i_object );
1028 /* Check that the handle is valid */
1034 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1038 if( i_object ) vlc_object_release( p_vlc );
1042 i_err = playlist_Pause( p_playlist );
1043 vlc_object_release( p_playlist );
1045 if( i_object ) vlc_object_release( p_vlc );
1049 /*****************************************************************************
1050 * VLC_Pause: toggle pause
1051 *****************************************************************************/
1052 int VLC_Stop( int i_object )
1055 playlist_t * p_playlist;
1056 vlc_t *p_vlc = vlc_current_object( i_object );
1058 /* Check that the handle is valid */
1064 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1068 if( i_object ) vlc_object_release( p_vlc );
1072 i_err = playlist_Stop( p_playlist );
1073 vlc_object_release( p_playlist );
1075 if( i_object ) vlc_object_release( p_vlc );
1079 /*****************************************************************************
1080 * VLC_IsPlaying: Query for Playlist Status
1081 *****************************************************************************/
1082 vlc_bool_t VLC_IsPlaying( int i_object )
1084 playlist_t * p_playlist;
1085 vlc_bool_t b_playing;
1087 vlc_t *p_vlc = vlc_current_object( i_object );
1089 /* Check that the handle is valid */
1095 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1099 if( i_object ) vlc_object_release( p_vlc );
1103 b_playing = playlist_IsPlaying( p_playlist );
1105 vlc_object_release( p_playlist );
1107 if( i_object ) vlc_object_release( p_vlc );
1112 /*****************************************************************************
1113 * VLC_ClearPlaylist: Empty the playlist
1114 *****************************************************************************/
1115 int VLC_ClearPlaylist( int i_object )
1118 playlist_t * p_playlist;
1119 vlc_t *p_vlc = vlc_current_object( i_object );
1121 /* Check that the handle is valid */
1127 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1131 if( i_object ) vlc_object_release( p_vlc );
1135 i_err = playlist_Clear(p_playlist);
1137 vlc_object_release( p_playlist );
1139 if( i_object ) vlc_object_release( p_vlc );
1143 /*****************************************************************************
1144 * VLC_FullScreen: toggle fullscreen mode
1145 *****************************************************************************/
1146 int VLC_FullScreen( int i_object )
1148 vout_thread_t *p_vout;
1149 vlc_t *p_vlc = vlc_current_object( i_object );
1156 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1160 if( i_object ) vlc_object_release( p_vlc );
1164 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1165 vlc_object_release( p_vout );
1167 if( i_object ) vlc_object_release( p_vlc );
1171 /* following functions are local */
1173 /*****************************************************************************
1174 * SetLanguage: set the interface language.
1175 *****************************************************************************
1176 * We set the LC_MESSAGES locale category for interface messages and buttons,
1177 * as well as the LC_CTYPE category for string sorting and possible wide
1178 * character support.
1179 *****************************************************************************/
1180 static void SetLanguage ( char const *psz_lang )
1182 #if defined( ENABLE_NLS ) \
1183 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1186 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1190 if( psz_lang && !*psz_lang )
1192 # if defined( HAVE_LC_MESSAGES )
1193 setlocale( LC_MESSAGES, psz_lang );
1195 setlocale( LC_CTYPE, psz_lang );
1200 /* I need that under Darwin, please check it doesn't disturb
1201 * other platforms. --Meuuh */
1202 setenv( "LANG", psz_lang, 1 );
1204 #elif defined( SYS_BEOS ) || defined( WIN32 )
1205 /* We set LC_ALL manually because it is the only way to set
1206 * the language at runtime under eg. Windows. Beware that this
1207 * makes the environment unconsistent when libvlc is unloaded and
1208 * should probably be moved to a safer place like vlc.c. */
1209 static char psz_lcall[20];
1210 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1211 psz_lcall[19] = '\0';
1212 putenv( psz_lcall );
1215 setlocale( LC_ALL, psz_lang );
1218 /* Specify where to find the locales for current domain */
1219 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1220 psz_path = LOCALEDIR;
1222 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1226 if( !bindtextdomain( PACKAGE, psz_path ) )
1228 fprintf( stderr, "warning: no domain %s in directory %s\n",
1229 PACKAGE, psz_path );
1232 /* Set the default domain */
1233 textdomain( PACKAGE );
1235 #if defined( ENABLE_UTF8 )
1236 bind_textdomain_codeset( PACKAGE, "UTF-8" );
1242 /*****************************************************************************
1243 * GetFilenames: parse command line options which are not flags
1244 *****************************************************************************
1245 * Parse command line for input files as well as their associated options.
1246 * An option always follows its associated input and begins with a ":".
1247 *****************************************************************************/
1248 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1250 int i_opt, i_options;
1252 /* We assume that the remaining parameters are filenames
1253 * and their input options */
1254 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1258 /* Count the input options */
1259 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1265 /* TODO: write an internal function of this one, to avoid
1266 * unnecessary lookups. */
1267 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1268 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1270 PLAYLIST_INSERT, 0 );
1276 /*****************************************************************************
1277 * Usage: print program usage
1278 *****************************************************************************
1279 * Print a short inline help. Message interface is initialized at this stage.
1280 *****************************************************************************/
1281 static void Usage( vlc_t *p_this, char const *psz_module_name )
1283 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1284 /* short option ------' | | | | | | |
1285 * option name ------------' | | | | | |
1286 * <bra -------------------------' | | | | |
1287 * option type or "" --------------' | | | |
1288 * ket> -----------------------------' | | |
1289 * padding spaces ---------------------' | |
1290 * comment -------------------------------' |
1291 * comment suffix --------------------------'
1293 * The purpose of having bra and ket is that we might i18n them as well.
1295 #define LINE_START 8
1296 #define PADDING_SPACES 25
1299 module_config_t *p_item;
1300 char psz_spaces[PADDING_SPACES+LINE_START+1];
1301 char psz_format[sizeof(FORMAT_STRING)];
1302 char psz_buffer[1000];
1305 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1306 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1308 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1309 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1311 strcpy( psz_format, FORMAT_STRING );
1317 /* List all modules */
1318 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1320 /* Enumerate the config for each module */
1321 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1323 vlc_bool_t b_help_module;
1325 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1327 if( psz_module_name && strcmp( psz_module_name,
1328 p_parser->psz_object_name ) )
1333 /* Ignore modules without config options */
1334 if( !p_parser->i_config_items )
1339 /* Ignore modules with only advanced config options if requested */
1342 for( p_item = p_parser->p_config;
1343 p_item->i_type != CONFIG_HINT_END;
1346 if( (p_item->i_type & CONFIG_ITEM) &&
1347 !p_item->b_advanced ) break;
1349 if( p_item->i_type == CONFIG_HINT_END ) continue;
1352 /* Print name of module */
1353 if( strcmp( "main", p_parser->psz_object_name ) )
1354 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1356 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1358 /* Print module options */
1359 for( p_item = p_parser->p_config;
1360 p_item->i_type != CONFIG_HINT_END;
1364 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1365 char *psz_suf = "", *psz_prefix = NULL;
1368 /* Skip advanced options if requested */
1369 if( p_item->b_advanced && !b_advanced )
1374 switch( p_item->i_type )
1376 case CONFIG_HINT_CATEGORY:
1377 case CONFIG_HINT_USAGE:
1378 if( !strcmp( "main", p_parser->psz_object_name ) )
1379 fprintf( stdout, "\n %s\n", p_item->psz_text );
1382 case CONFIG_ITEM_STRING:
1383 case CONFIG_ITEM_FILE:
1384 case CONFIG_ITEM_DIRECTORY:
1385 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1386 if( !p_item->ppsz_list )
1388 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1394 psz_type = psz_buffer;
1396 for( i = 0; p_item->ppsz_list[i]; i++ )
1398 if( i ) strcat( psz_type, "," );
1399 strcat( psz_type, p_item->ppsz_list[i] );
1404 case CONFIG_ITEM_INTEGER:
1405 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1406 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1408 case CONFIG_ITEM_FLOAT:
1409 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1411 case CONFIG_ITEM_BOOL:
1412 psz_bra = ""; psz_type = ""; psz_ket = "";
1413 if( !b_help_module )
1415 psz_suf = p_item->i_value ? _(" (default enabled)") :
1416 _(" (default disabled)");
1426 /* Add short option if any */
1427 if( p_item->i_short )
1429 sprintf( psz_short, "-%c,", p_item->i_short );
1433 strcpy( psz_short, " " );
1436 i = PADDING_SPACES - strlen( p_item->psz_name )
1437 - strlen( psz_bra ) - strlen( psz_type )
1438 - strlen( psz_ket ) - 1;
1440 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1442 /* If option is of type --foo-bar, we print its counterpart
1443 * as --no-foo-bar, but if it is of type --foobar (without
1444 * dashes in the name) we print it as --nofoobar. Both
1445 * values are of course valid, only the display changes. */
1446 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1448 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1453 psz_spaces[0] = '\n';
1458 psz_spaces[i] = '\0';
1461 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1463 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1464 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1465 psz_ket, psz_spaces );
1469 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1470 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1473 psz_spaces[i] = ' ';
1475 /* We wrap the rest of the output */
1476 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1477 psz_text = psz_buffer;
1480 char *psz_parser, *psz_word;
1481 int i_end = strlen( psz_text );
1483 /* If the remaining text fits in a line, print it. */
1484 if( i_end <= i_width )
1486 fprintf( stdout, "%s\n", psz_text );
1490 /* Otherwise, eat as many words as possible */
1491 psz_parser = psz_text;
1494 psz_word = psz_parser;
1495 psz_parser = strchr( psz_word, ' ' );
1496 /* If no space was found, we reached the end of the text
1497 * block; otherwise, we skip the space we just found. */
1498 psz_parser = psz_parser ? psz_parser + 1
1501 } while( psz_parser - psz_text <= i_width );
1503 /* We cut a word in one of these cases:
1504 * - it's the only word in the line and it's too long.
1505 * - we used less than 80% of the width and the word we are
1506 * going to wrap is longer than 40% of the width, and even
1507 * if the word would have fit in the next line. */
1508 if( psz_word == psz_text
1509 || ( psz_word - psz_text < 80 * i_width / 100
1510 && psz_parser - psz_word > 40 * i_width / 100 ) )
1512 char c = psz_text[i_width];
1513 psz_text[i_width] = '\0';
1514 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1515 psz_text += i_width;
1520 psz_word[-1] = '\0';
1521 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1522 psz_text = psz_word;
1528 /* Release the module list */
1529 vlc_list_release( p_list );
1531 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1532 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1537 /*****************************************************************************
1538 * ListModules: list the available modules with their description
1539 *****************************************************************************
1540 * Print a list of all available modules (builtins and plugins) and a short
1541 * description for each one.
1542 *****************************************************************************/
1543 static void ListModules( vlc_t *p_this )
1547 char psz_spaces[22];
1550 memset( psz_spaces, ' ', 22 );
1557 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1558 p_this->p_vlc->psz_object_name );
1560 fprintf( stdout, _("[module] [description]\n") );
1562 /* List all modules */
1563 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1565 /* Enumerate each module */
1566 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1570 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1572 /* Nasty hack, but right now I'm too tired to think about a nice
1574 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1578 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1579 psz_spaces, p_parser->psz_longname );
1581 psz_spaces[i] = ' ';
1584 vlc_list_release( p_list );
1586 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1587 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1592 /*****************************************************************************
1593 * Version: print complete program version
1594 *****************************************************************************
1595 * Print complete program version and build number.
1596 *****************************************************************************/
1597 static void Version( void )
1603 fprintf( stdout, VERSION_MESSAGE "\n" );
1605 _("This program comes with NO WARRANTY, to the extent permitted by "
1606 "law.\nYou may redistribute it under the terms of the GNU General "
1607 "Public License;\nsee the file named COPYING for details.\n"
1608 "Written by the VideoLAN team; see the AUTHORS file.\n") );
1610 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1611 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1616 /*****************************************************************************
1617 * ShowConsole: On Win32, create an output console for debug messages
1618 *****************************************************************************
1619 * This function is useful only on Win32.
1620 *****************************************************************************/
1622 static void ShowConsole( void )
1626 freopen( "CONOUT$", "w", stdout );
1627 freopen( "CONOUT$", "w", stderr );
1628 freopen( "CONIN$", "r", stdin );
1634 /*****************************************************************************
1635 * ConsoleWidth: Return the console width in characters
1636 *****************************************************************************
1637 * We use the stty shell command to get the console width; if this fails or
1638 * if the width is less than 80, we default to 80.
1639 *****************************************************************************/
1640 static int ConsoleWidth( void )
1645 char buf[20], *psz_parser;
1649 file = popen( "stty size 2>/dev/null", "r" );
1652 i_ret = fread( buf, 1, 20, file );
1656 psz_parser = strchr( buf, ' ' );
1659 i_ret = atoi( psz_parser + 1 );
1674 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1675 vlc_value_t old_val, vlc_value_t new_val, void *param)
1677 vlc_t *p_vlc = (vlc_t *)p_this;
1679 if( new_val.i_int >= -1 )
1681 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );