1 /*****************************************************************************
2 * libvlc-common.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2006 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * RĂ©mi Denis-Courmont <rem # videolan : org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
34 *****************************************************************************/
40 #include "control/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
46 #include <errno.h> /* ENOMEM */
47 #include <stdio.h> /* sprintf() */
49 #include <stdlib.h> /* free() */
52 # include <netinet/in.h> /* BSD: struct in_addr */
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
62 # include "extras/getopt.h"
70 /* used for one-instance mode */
71 # include <dbus/dbus.h>
75 # include <hal/libhal.h>
78 #include "vlc_os_specific.h"
80 #include <vlc_playlist.h>
81 #include <vlc_interface.h>
84 #include "audio_output/aout_internal.h"
89 #include "stream_output/stream_output.h"
91 #include <vlc_charset.h>
95 #include "playlist/playlist_internal.h"
97 /*****************************************************************************
98 * The evil global variable. We handle it with care, don't worry.
99 *****************************************************************************/
100 static libvlc_global_data_t libvlc_global;
101 #define p_libvlc_global (&libvlc_global)
102 static libvlc_int_t * p_static_vlc = NULL;
103 static volatile unsigned int i_instances = 0;
105 /*****************************************************************************
107 *****************************************************************************/
108 #if defined (__APPLE__) || defined (WIN32)
109 static void SetLanguage ( char const * );
111 static inline int LoadMessages (void);
112 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
113 static void Help ( libvlc_int_t *, char const *psz_help_name );
114 static void Usage ( libvlc_int_t *, char const *psz_module_name );
115 static void ListModules ( libvlc_int_t *, vlc_bool_t );
116 static void Version ( void );
119 static void ShowConsole ( vlc_bool_t );
120 static void PauseConsole ( void );
122 static int ConsoleWidth ( void );
124 static int VerboseCallback( vlc_object_t *, char const *,
125 vlc_value_t, vlc_value_t, void * );
127 static void InitDeviceValues( libvlc_int_t * );
129 libvlc_global_data_t *vlc_global( void )
131 return &libvlc_global;
134 /*****************************************************************************
135 * vlc_current_object: return the current object.
136 *****************************************************************************
137 * If i_object is non-zero, return the corresponding object. Otherwise,
138 * return the statically allocated p_vlc object.
139 *****************************************************************************/
140 libvlc_int_t * vlc_current_object( int i_object )
144 return vlc_object_get( p_libvlc_global, i_object );
152 * Allocate a libvlc instance, initialize global data if needed
153 * It also initializes the threading system
155 libvlc_int_t * libvlc_InternalCreate( void )
158 libvlc_int_t * p_libvlc = NULL;
160 char *psz_env = NULL;
163 /* &libvlc_global never changes,
164 * so we can safely call this multiple times. */
165 p_libvlc_global = &libvlc_global;
168 /* vlc_threads_init *must* be the first internal call! No other call is
169 * allowed before the thread system has been initialized. */
170 i_ret = vlc_threads_init( p_libvlc_global );
171 if( i_ret < 0 ) return NULL;
173 /* Now that the thread system is initialized, we don't have much, but
174 * at least we have var_Create */
175 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
176 var_Get( p_libvlc_global, "libvlc", &lockval );
177 vlc_mutex_lock( lockval.p_address );
181 if( !libvlc_global.b_ready )
183 /* Guess what CPU we have */
184 cpu_flags = CPUCapabilities();
185 /* The module bank will be initialized later */
186 libvlc_global.p_module_bank = NULL;
188 libvlc_global.b_ready = VLC_TRUE;
190 vlc_mutex_unlock( lockval.p_address );
191 var_Destroy( p_libvlc_global, "libvlc" );
193 /* Allocate a libvlc instance object */
194 p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
195 if( p_libvlc == NULL )
200 p_libvlc->p_playlist = NULL;
201 p_libvlc->psz_object_name = "libvlc";
203 /* Initialize message queue */
204 msg_Create( p_libvlc );
206 /* Find verbosity from VLC_VERBOSE environment variable */
207 psz_env = getenv( "VLC_VERBOSE" );
208 if( psz_env != NULL )
209 p_libvlc->i_verbose = atoi( psz_env );
211 p_libvlc->i_verbose = 3;
212 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
213 p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
215 p_libvlc->b_color = VLC_FALSE;
218 /* Announce who we are - Do it only for first instance ? */
219 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
220 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
222 /* Initialize mutexes */
223 vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
225 vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
226 vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
228 /* Store data for the non-reentrant API */
229 p_static_vlc = p_libvlc;
235 * Initialize a libvlc instance
236 * This function initializes a previously allocated libvlc instance:
238 * - gettext initialization
239 * - message queue, module bank and playlist initialization
240 * - configuration and commandline parsing
242 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
243 const char *ppsz_argv[] )
245 char p_capabilities[200];
247 char * psz_modules = NULL;
248 char * psz_parser = NULL;
249 char * psz_control = NULL;
250 vlc_bool_t b_exit = VLC_FALSE;
251 int i_ret = VLC_EEXIT;
252 playlist_t *p_playlist = NULL;
254 #if defined( ENABLE_NLS ) \
255 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
256 # if defined (WIN32) || defined (__APPLE__)
261 /* System specific initialization code */
262 system_Init( p_libvlc, &i_argc, ppsz_argv );
264 /* Get the executable name (similar to the basename command) */
267 const char *exe = p_libvlc->psz_object_name = ppsz_argv[0];
271 p_libvlc->psz_object_name = exe;
276 p_libvlc->psz_object_name = "vlc";
280 * Support for gettext
284 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
285 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
287 /* Initialize the module bank and load the configuration of the
288 * main module. We need to do this at this stage to be able to display
289 * a short help if required by the user. (short help == main module
291 module_InitBank( p_libvlc );
293 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE ) )
295 module_EndBank( p_libvlc );
299 /* Check for short help option */
300 if( config_GetInt( p_libvlc, "help" ) > 0 )
302 Help( p_libvlc, "help" );
304 i_ret = VLC_EEXITSUCCESS;
306 /* Check for version option */
307 else if( config_GetInt( p_libvlc, "version" ) > 0 )
311 i_ret = VLC_EEXITSUCCESS;
314 /* Set the config file stuff */
315 p_libvlc->psz_homedir = config_GetHomeDir();
316 p_libvlc->psz_configdir = config_GetConfigDir( p_libvlc );
317 p_libvlc->psz_datadir = config_GetUserDataDir( p_libvlc );
318 p_libvlc->psz_cachedir = config_GetCacheDir( p_libvlc );
319 p_libvlc->psz_configfile = config_GetCustomConfigFile( p_libvlc );
321 /* Check for plugins cache options */
322 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
324 libvlc_global.p_module_bank->b_cache_delete = VLC_TRUE;
327 /* Will be re-done properly later on */
328 p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
330 /* Check for daemon mode */
332 if( config_GetInt( p_libvlc, "daemon" ) )
335 char *psz_pidfile = NULL;
337 if( daemon( 1, 0) != 0 )
339 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
342 libvlc_global.b_daemon = VLC_TRUE;
344 /* lets check if we need to write the pidfile */
345 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
346 if( psz_pidfile != NULL )
349 pid_t i_pid = getpid ();
350 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
351 i_pid, psz_pidfile );
352 pidfile = utf8_fopen( psz_pidfile,"w" );
353 if( pidfile != NULL )
355 utf8_fprintf( pidfile, "%d", (int)i_pid );
360 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
369 if( ( i_pid = fork() ) < 0 )
371 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
376 /* This is the parent, exit right now */
377 msg_Dbg( p_libvlc, "closing parent process" );
379 i_ret = VLC_EEXITSUCCESS;
383 /* We are the child */
384 msg_Dbg( p_libvlc, "daemon spawned" );
385 close( STDIN_FILENO );
386 close( STDOUT_FILENO );
387 close( STDERR_FILENO );
389 libvlc_global.b_daemon = VLC_TRUE;
397 module_EndBank( p_libvlc );
401 /* Check for translation config option */
402 #if defined( ENABLE_NLS ) \
403 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
404 # if defined (WIN32) || defined (__APPLE__)
405 /* This ain't really nice to have to reload the config here but it seems
406 * the only way to do it. */
407 config_LoadConfigFile( p_libvlc, "main" );
408 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
410 /* Check if the user specified a custom language */
411 psz_language = config_GetPsz( p_libvlc, "language" );
412 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
414 vlc_bool_t b_cache_delete = libvlc_global.p_module_bank->b_cache_delete;
416 /* Reset the default domain */
417 SetLanguage( psz_language );
419 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
420 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
422 module_EndBank( p_libvlc );
423 module_InitBank( p_libvlc );
424 config_LoadConfigFile( p_libvlc, "main" );
425 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
426 libvlc_global.p_module_bank->b_cache_delete = b_cache_delete;
428 if( psz_language ) free( psz_language );
433 * Load the builtins and plugins into the module_bank.
434 * We have to do it before config_Load*() because this also gets the
435 * list of configuration options exported by each module and loads their
438 module_LoadBuiltins( p_libvlc );
439 module_LoadPlugins( p_libvlc );
440 if( p_libvlc->b_die )
445 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
446 libvlc_global.p_module_bank->i_children );
448 /* Check for help on modules */
449 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
451 Help( p_libvlc, p_tmp );
454 i_ret = VLC_EEXITSUCCESS;
456 /* Check for long help option */
457 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
459 Help( p_libvlc, "longhelp" );
461 i_ret = VLC_EEXITSUCCESS;
463 /* Check for module list option */
464 else if( config_GetInt( p_libvlc, "list" ) > 0 )
466 ListModules( p_libvlc, VLC_FALSE );
468 i_ret = VLC_EEXITSUCCESS;
470 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
472 ListModules( p_libvlc, VLC_TRUE );
474 i_ret = VLC_EEXITSUCCESS;
477 /* Check for config file options */
478 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
480 config_ResetAll( p_libvlc );
481 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
482 config_SaveConfigFile( p_libvlc, NULL );
484 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
486 config_LoadConfigFile( p_libvlc, NULL );
487 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
488 config_SaveConfigFile( p_libvlc, NULL );
493 module_EndBank( p_libvlc );
500 InitDeviceValues( p_libvlc );
503 * Override default configuration with config file settings
505 config_LoadConfigFile( p_libvlc, NULL );
508 * Override configuration with command line settings
510 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_FALSE ) )
513 ShowConsole( VLC_FALSE );
514 /* Pause the console because it's destroyed when we exit */
515 fprintf( stderr, "The command line options couldn't be loaded, check "
516 "that they are valid.\n" );
519 module_EndBank( p_libvlc );
524 * System specific configuration
526 system_Configure( p_libvlc, &i_argc, ppsz_argv );
528 /* FIXME: could be replaced by using Unix sockets */
530 dbus_threads_init_default();
532 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
534 /* Initialise D-Bus interface, check for other instances */
535 DBusConnection *p_conn = NULL;
536 DBusError dbus_error;
538 dbus_error_init( &dbus_error );
540 /* connect to the session bus */
541 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
544 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
545 dbus_error.message );
546 dbus_error_free( &dbus_error );
550 /* check if VLC is available on the bus
551 * if not: D-Bus control is not enabled on the other
552 * instance and we can't pass MRLs to it */
553 DBusMessage *p_test_msg = NULL;
554 DBusMessage *p_test_reply = NULL;
555 p_test_msg = dbus_message_new_method_call(
556 "org.mpris.vlc", "/",
557 "org.freedesktop.MediaPlayer", "Identity" );
558 /* block until a reply arrives */
559 p_test_reply = dbus_connection_send_with_reply_and_block(
560 p_conn, p_test_msg, -1, &dbus_error );
561 dbus_message_unref( p_test_msg );
562 if( p_test_reply == NULL )
564 dbus_error_free( &dbus_error );
565 msg_Dbg( p_libvlc, "No Media Player is running. "
566 "Continuing normally." );
571 DBusMessage* p_dbus_msg = NULL;
572 DBusMessageIter dbus_args;
573 DBusPendingCall* p_dbus_pending = NULL;
576 dbus_message_unref( p_test_reply );
577 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
579 for( i_input = optind;i_input < i_argc;i_input++ )
581 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
582 ppsz_argv[i_input] );
584 p_dbus_msg = dbus_message_new_method_call(
585 "org.mpris.vlc", "/TrackList",
586 "org.freedesktop.MediaPlayer", "AddTrack" );
588 if ( NULL == p_dbus_msg )
590 msg_Err( p_libvlc, "D-Bus problem" );
591 system_End( p_libvlc );
592 exit( VLC_ETIMEOUT );
596 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
597 if ( !dbus_message_iter_append_basic( &dbus_args,
598 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
600 msg_Err( p_libvlc, "Out of memory" );
601 dbus_message_unref( p_dbus_msg );
602 system_End( p_libvlc );
606 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
608 if ( !dbus_message_iter_append_basic( &dbus_args,
609 DBUS_TYPE_BOOLEAN, &b_play ) )
611 msg_Err( p_libvlc, "Out of memory" );
612 dbus_message_unref( p_dbus_msg );
613 system_End( p_libvlc );
617 /* send message and get a handle for a reply */
618 if ( !dbus_connection_send_with_reply ( p_conn,
619 p_dbus_msg, &p_dbus_pending, -1 ) )
621 msg_Err( p_libvlc, "D-Bus problem" );
622 dbus_message_unref( p_dbus_msg );
623 system_End( p_libvlc );
624 exit( VLC_ETIMEOUT );
627 if ( NULL == p_dbus_pending )
629 msg_Err( p_libvlc, "D-Bus problem" );
630 dbus_message_unref( p_dbus_msg );
631 system_End( p_libvlc );
632 exit( VLC_ETIMEOUT );
634 dbus_connection_flush( p_conn );
635 dbus_message_unref( p_dbus_msg );
636 /* block until we receive a reply */
637 dbus_pending_call_block( p_dbus_pending );
638 dbus_pending_call_unref( p_dbus_pending );
639 } /* processes all command line MRLs */
642 system_End( p_libvlc );
646 /* we unreference the connection when we've finished with it */
647 if( p_conn ) dbus_connection_unref( p_conn );
652 * Message queue options
655 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
656 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
659 var_Set( p_libvlc, "verbose", val );
661 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
662 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
664 if( p_libvlc->b_color )
665 p_libvlc->b_color = config_GetInt( p_libvlc, "color" ) > 0;
668 * Output messages that may still be in the queue
670 msg_Flush( p_libvlc );
672 if( !config_GetInt( p_libvlc, "fpu" ) )
673 cpu_flags &= ~CPU_CAPABILITY_FPU;
675 #if defined( __i386__ ) || defined( __x86_64__ )
676 if( !config_GetInt( p_libvlc, "mmx" ) )
677 cpu_flags &= ~CPU_CAPABILITY_MMX;
678 if( !config_GetInt( p_libvlc, "3dn" ) )
679 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
680 if( !config_GetInt( p_libvlc, "mmxext" ) )
681 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
682 if( !config_GetInt( p_libvlc, "sse" ) )
683 cpu_flags &= ~CPU_CAPABILITY_SSE;
684 if( !config_GetInt( p_libvlc, "sse2" ) )
685 cpu_flags &= ~CPU_CAPABILITY_SSE2;
687 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
688 if( !config_GetInt( p_libvlc, "altivec" ) )
689 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
692 #define PRINT_CAPABILITY( capability, string ) \
693 if( vlc_CPU() & capability ) \
695 strncat( p_capabilities, string " ", \
696 sizeof(p_capabilities) - strlen(p_capabilities) ); \
697 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
700 p_capabilities[0] = '\0';
701 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
702 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
703 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
704 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
705 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
706 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
707 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
708 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
709 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
710 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
711 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
714 * Choose the best memcpy module
716 p_libvlc->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
718 if( p_libvlc->pf_memcpy == NULL )
720 p_libvlc->pf_memcpy = memcpy;
723 if( p_libvlc->pf_memset == NULL )
725 p_libvlc->pf_memset = memset;
728 p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
729 p_libvlc->i_timers = 0;
730 p_libvlc->pp_timers = NULL;
731 vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
734 * Initialize hotkey handling
736 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
737 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
738 /* Do a copy (we don't need to modify the strings) */
739 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
741 /* Initialize playlist and get commandline files */
742 playlist_ThreadCreate( p_libvlc );
743 if( !p_libvlc->p_playlist )
745 msg_Err( p_libvlc, "playlist initialization failed" );
746 if( p_libvlc->p_memcpy_module != NULL )
748 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
750 module_EndBank( p_libvlc );
753 p_playlist = p_libvlc->p_playlist;
755 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
756 if( psz_modules && *psz_modules )
758 /* Add service discovery modules */
759 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
761 if( psz_modules ) free( psz_modules );
764 * Load background interfaces
766 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
767 psz_control = config_GetPsz( p_libvlc, "control" );
769 if( psz_modules && *psz_modules && psz_control && *psz_control )
771 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
772 strlen( psz_control ) + 1 );
773 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
775 else if( psz_control && *psz_control )
778 psz_modules = strdup( psz_control );
781 psz_parser = psz_modules;
782 while ( psz_parser && *psz_parser )
784 char *psz_module, *psz_temp;
785 psz_module = psz_parser;
786 psz_parser = strchr( psz_module, ':' );
792 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
795 sprintf( psz_temp, "%s,none", psz_module );
796 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
804 * Always load the hotkeys interface if it exists
806 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
809 /* loads dbus control interface if in one-instance mode
810 * we do it only when playlist exists, because dbus module needs it */
811 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
812 VLC_AddIntf( 0, "dbus,none", VLC_FALSE, VLC_FALSE );
814 /* Prevents the power management daemon to suspend the computer
815 * when VLC is active */
816 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
817 VLC_AddIntf( 0, "inhibit,none", VLC_FALSE, VLC_FALSE );
821 * If needed, load the Xscreensaver interface
822 * Currently, only for X
824 #ifdef HAVE_X11_XLIB_H
825 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
827 VLC_AddIntf( 0, "screensaver,none", VLC_FALSE, VLC_FALSE );
831 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
833 VLC_AddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE );
836 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
838 const char *psz_logmode = "logmode=syslog";
839 libvlc_InternalAddIntf( p_libvlc, "logger,none", VLC_FALSE, VLC_FALSE,
844 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
846 VLC_AddIntf( 0, "showintf,none", VLC_FALSE, VLC_FALSE );
849 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
851 VLC_AddIntf( 0, "netsync,none", VLC_FALSE, VLC_FALSE );
855 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
857 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
860 char *psz_morecodecs;
861 asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs);
864 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
865 free(psz_morecodecs);
869 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
875 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
877 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
878 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
879 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
880 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
881 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
882 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
883 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
884 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
885 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
887 /* Create volume callback system. */
888 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
891 * Get input filenames given as commandline arguments
893 GetFilenames( p_libvlc, i_argc, ppsz_argv );
896 * Get --open argument
898 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
899 var_Get( p_libvlc, "open", &val );
900 if ( val.psz_string != NULL && *val.psz_string )
902 VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
903 PLAYLIST_INSERT, 0 );
905 if ( val.psz_string != NULL ) free( val.psz_string );
911 * Cleanup a libvlc instance. The instance is not completely deallocated
912 * \param p_libvlc the instance to clean
914 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
916 intf_thread_t * p_intf = NULL;
917 vout_thread_t * p_vout = NULL;
918 aout_instance_t * p_aout = NULL;
919 announce_handler_t * p_announce = NULL;
921 /* Ask the interfaces to stop and destroy them */
922 msg_Dbg( p_libvlc, "removing all interfaces" );
923 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
925 intf_StopThread( p_intf );
926 vlc_object_detach( p_intf );
927 vlc_object_release( p_intf );
928 intf_Destroy( p_intf );
933 msg_Dbg( p_libvlc, "removing playlist" );
934 playlist_ThreadDestroy( p_libvlc->p_playlist );
936 /* Free video outputs */
937 msg_Dbg( p_libvlc, "removing all video outputs" );
938 while( (p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
940 vlc_object_detach( p_vout );
941 vlc_object_release( p_vout );
942 vout_Destroy( p_vout );
945 /* Free audio outputs */
946 msg_Dbg( p_libvlc, "removing all audio outputs" );
947 while( (p_aout = vlc_object_find( p_libvlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
949 vlc_object_detach( (vlc_object_t *)p_aout );
950 vlc_object_release( (vlc_object_t *)p_aout );
951 aout_Delete( p_aout );
954 stats_TimersDumpAll( p_libvlc );
955 stats_TimersClean( p_libvlc );
957 /* Free announce handler(s?) */
958 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
961 msg_Dbg( p_libvlc, "removing announce handler" );
962 vlc_object_detach( p_announce );
963 vlc_object_release( p_announce );
964 announce_HandlerDestroy( p_announce );
970 * Destroy everything.
971 * This function requests the running threads to finish, waits for their
972 * termination, and destroys their structure.
973 * It stops the thread systems: no instance can run after this has run
974 * \param p_libvlc the instance to destroy
975 * \param b_release whether we should do a release on the instance
977 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
985 char* psz_pidfile = NULL;
987 if( libvlc_global.p_module_bank )
988 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
990 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
991 if( psz_pidfile != NULL )
993 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
994 if( unlink( psz_pidfile ) == -1 )
996 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1000 free ( psz_pidfile );
1004 if( p_libvlc->p_memcpy_module )
1006 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
1007 p_libvlc->p_memcpy_module = NULL;
1010 /* Free module bank. It is refcounted, so we call this each time */
1011 module_EndBank( p_libvlc );
1013 FREENULL( p_libvlc->psz_homedir );
1014 FREENULL( p_libvlc->psz_configdir );
1015 FREENULL( p_libvlc->psz_datadir );
1016 FREENULL( p_libvlc->psz_cachedir );
1017 FREENULL( p_libvlc->psz_configfile );
1018 FREENULL( p_libvlc->p_hotkeys );
1020 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
1021 var_Get( p_libvlc_global, "libvlc", &lockval );
1022 vlc_mutex_lock( lockval.p_address );
1025 if( i_instances == 0 )
1027 /* System specific cleaning code */
1028 system_End( p_libvlc );
1030 vlc_mutex_unlock( lockval.p_address );
1031 var_Destroy( p_libvlc_global, "libvlc" );
1033 msg_Flush( p_libvlc );
1034 msg_Destroy( p_libvlc );
1036 /* Destroy mutexes */
1037 vlc_mutex_destroy( &p_libvlc->config_lock );
1038 vlc_mutex_destroy( &p_libvlc->timer_lock );
1040 if( b_release ) vlc_object_release( p_libvlc );
1041 vlc_object_destroy( p_libvlc );
1044 /* Stop thread system: last one out please shut the door!
1045 * The number of initializations of the thread system is counted, we
1046 * can call this each time */
1047 vlc_threads_end( p_libvlc_global );
1053 * Add an interface plugin and run it
1055 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
1056 char const *psz_module,
1057 vlc_bool_t b_block, vlc_bool_t b_play,
1058 int i_options, const char *const *ppsz_options )
1061 intf_thread_t *p_intf = NULL;
1064 return VLC_EGENERIC;
1066 if( !psz_module ) /* requesting the default interface */
1068 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1069 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1070 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1071 free( psz_interface );
1075 if( libvlc_global.b_daemon && b_block && !psz_module )
1077 /* Daemon mode hack.
1078 * We prefer the dummy interface if none is specified. */
1079 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1080 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
1081 if( psz_interface ) free( psz_interface );
1085 /* Try to create the interface */
1086 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
1087 i_options, ppsz_options );
1088 if( p_intf == NULL )
1090 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1092 return VLC_EGENERIC;
1095 /* Interface doesn't handle play on start so do it ourselves */
1096 if( !p_intf->b_play && b_play )
1097 playlist_Play( p_libvlc->p_playlist );
1099 /* Try to run the interface */
1100 p_intf->b_play = b_play;
1101 i_err = intf_RunThread( p_intf );
1102 if( i_err || p_intf->b_should_run_on_first_thread )
1104 vlc_object_detach( p_intf );
1105 intf_Destroy( p_intf );
1112 /* FIXME: should be moved to interface/interface.c */
1113 if( p_intf->pf_run )
1114 vlc_thread_join( p_intf );
1116 while( vlc_object_lock_and_wait( p_intf ) == 0 );
1118 vlc_object_detach( p_intf );
1119 intf_Destroy( p_intf );
1125 #if defined (__APPLE__) || defined (WIN32)
1126 /*****************************************************************************
1127 * SetLanguage: set the interface language.
1128 *****************************************************************************
1129 * We set the LC_MESSAGES locale category for interface messages and buttons,
1130 * as well as the LC_CTYPE category for string sorting and possible wide
1131 * character support.
1132 *****************************************************************************/
1133 static void SetLanguage ( const char *psz_lang )
1136 /* I need that under Darwin, please check it doesn't disturb
1137 * other platforms. --Meuuh */
1138 setenv( "LANG", psz_lang, 1 );
1141 /* We set LC_ALL manually because it is the only way to set
1142 * the language at runtime under eg. Windows. Beware that this
1143 * makes the environment unconsistent when libvlc is unloaded and
1144 * should probably be moved to a safer place like vlc.c. */
1145 static char psz_lcall[20];
1146 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1147 psz_lcall[19] = '\0';
1148 putenv( psz_lcall );
1151 setlocale( LC_ALL, psz_lang );
1156 static inline int LoadMessages (void)
1158 #if defined( ENABLE_NLS ) \
1159 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1160 /* Specify where to find the locales for current domain */
1161 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1162 static const char psz_path[] = LOCALEDIR;
1164 char psz_path[1024];
1165 if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
1166 libvlc_global.psz_vlcpath, "locale")
1167 >= (int)sizeof (psz_path))
1171 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1173 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1174 " to directory %s\n", psz_path);
1178 /* LibVLC wants all messages in UTF-8.
1179 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1180 * and other functions that are not part of our text domain.
1182 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1184 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1186 // Unbinds the text domain to avoid broken encoding
1187 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1191 /* LibVLC does NOT set the default textdomain, since it is a library.
1192 * This could otherwise break programs using LibVLC (other than VLC).
1193 * textdomain (PACKAGE_NAME);
1199 /*****************************************************************************
1200 * GetFilenames: parse command line options which are not flags
1201 *****************************************************************************
1202 * Parse command line for input files as well as their associated options.
1203 * An option always follows its associated input and begins with a ":".
1204 *****************************************************************************/
1205 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1207 int i_opt, i_options;
1209 /* We assume that the remaining parameters are filenames
1210 * and their input options */
1211 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1215 /* Count the input options */
1216 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1222 /* TODO: write an internal function of this one, to avoid
1223 * unnecessary lookups. */
1225 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1226 ( i_options ? &ppsz_argv[i_opt + 1] :
1228 PLAYLIST_INSERT, 0 );
1234 /*****************************************************************************
1235 * Help: print program help
1236 *****************************************************************************
1237 * Print a short inline help. Message interface is initialized at this stage.
1238 *****************************************************************************/
1239 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1242 ShowConsole( VLC_TRUE );
1245 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1247 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1248 Usage( p_this, "help" );
1249 Usage( p_this, "main" );
1251 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1253 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1254 Usage( p_this, NULL );
1256 else if( psz_help_name )
1258 Usage( p_this, psz_help_name );
1261 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1266 /*****************************************************************************
1267 * Usage: print module usage
1268 *****************************************************************************
1269 * Print a short inline help. Message interface is initialized at this stage.
1270 *****************************************************************************/
1271 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1273 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1274 /* short option ------' | | | | | | |
1275 * option name ------------' | | | | | |
1276 * <bra ---------------------' | | | | |
1277 * option type or "" ----------' | | | |
1278 * ket> -------------------------' | | |
1279 * padding spaces -----------------' | |
1280 * comment --------------------------' |
1281 * comment suffix ---------------------'
1283 * The purpose of having bra and ket is that we might i18n them as well.
1286 # define COL(x) "\033[" #x ";1m"
1287 # define RED COL(31)
1288 # define GREEN COL(32)
1289 # define YELLOW COL(33)
1290 # define BLUE COL(34)
1291 # define MAGENTA COL(35)
1292 # define CYAN COL(36)
1293 # define WHITE COL(0)
1294 # define GRAY "\033[0m"
1295 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1296 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1298 #define LINE_START 8
1299 #define PADDING_SPACES 25
1301 # define OPTION_VALUE_SEP "="
1303 # define OPTION_VALUE_SEP " "
1305 vlc_list_t *p_list = NULL;
1306 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1307 char psz_spaces_longtext[LINE_START+3];
1308 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1309 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1310 char psz_buffer[10000];
1313 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1314 int i_width_description = i_width + PADDING_SPACES - 1;
1315 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1316 vlc_bool_t b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1317 vlc_bool_t b_description_hack;
1318 vlc_bool_t b_color = config_GetInt( p_this, "color" ) > 0;
1319 vlc_bool_t b_has_advanced = VLC_FALSE;
1321 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1322 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1323 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1324 psz_spaces_longtext[LINE_START+2] = '\0';
1328 strcpy( psz_format, COLOR_FORMAT_STRING );
1329 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1333 strcpy( psz_format, FORMAT_STRING );
1334 strcpy( psz_format_bool, FORMAT_STRING );
1337 /* List all modules */
1338 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1340 /* Ugly hack to make sure that the help options always come first
1342 if( !psz_module_name )
1343 Usage( p_this, "help" );
1345 /* Enumerate the config for each module */
1346 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1348 vlc_bool_t b_help_module;
1349 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1350 module_config_t *p_item = NULL;
1351 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1353 if( psz_module_name && strcmp( psz_module_name,
1354 p_parser->psz_object_name ) )
1356 const char *const *pp_shortcut = p_parser->pp_shortcuts;
1357 while( *pp_shortcut )
1359 if( !strcmp( psz_module_name, *pp_shortcut ) )
1367 /* Ignore modules without config options */
1368 if( !p_parser->i_config_items )
1373 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1374 /* Ugly hack to make sure that the help options always come first
1376 if( !psz_module_name && b_help_module )
1379 /* Ignore modules with only advanced config options if requested */
1382 for( p_item = p_parser->p_config;
1386 if( (p_item->i_type & CONFIG_ITEM) &&
1387 !p_item->b_advanced ) break;
1391 /* Print name of module */
1392 if( strcmp( "main", p_parser->psz_object_name ) )
1395 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1396 p_parser->psz_longname );
1398 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1400 if( p_parser->psz_help )
1403 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1405 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1408 /* Print module options */
1409 for( p_item = p_parser->p_config;
1413 char *psz_text, *psz_spaces = psz_spaces_text;
1414 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1415 const char *psz_suf = "", *psz_prefix = NULL;
1419 /* Skip removed options */
1420 if( p_item->b_removed )
1424 /* Skip advanced options if requested */
1425 if( p_item->b_advanced && !b_advanced )
1427 b_has_advanced = VLC_TRUE;
1431 switch( p_item->i_type )
1433 case CONFIG_HINT_CATEGORY:
1434 case CONFIG_HINT_USAGE:
1435 if( !strcmp( "main", p_parser->psz_object_name ) )
1438 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1441 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1443 if( b_description && p_item->psz_longtext )
1446 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1447 p_item->psz_longtext );
1449 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1453 case CONFIG_HINT_SUBCATEGORY:
1454 if( strcmp( "main", p_parser->psz_object_name ) )
1456 case CONFIG_SECTION:
1459 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1461 if( b_description && p_item->psz_longtext )
1462 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1463 p_item->psz_longtext );
1467 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1468 if( b_description && p_item->psz_longtext )
1469 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1473 case CONFIG_ITEM_STRING:
1474 case CONFIG_ITEM_FILE:
1475 case CONFIG_ITEM_DIRECTORY:
1476 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1477 case CONFIG_ITEM_MODULE_CAT:
1478 case CONFIG_ITEM_MODULE_LIST:
1479 case CONFIG_ITEM_MODULE_LIST_CAT:
1480 case CONFIG_ITEM_FONT:
1481 case CONFIG_ITEM_PASSWORD:
1482 psz_bra = OPTION_VALUE_SEP "<";
1483 psz_type = _("string");
1486 if( p_item->ppsz_list )
1488 psz_bra = OPTION_VALUE_SEP "{";
1489 psz_type = psz_buffer;
1490 psz_buffer[0] = '\0';
1491 for( i = 0; p_item->ppsz_list[i]; i++ )
1493 if( i ) strcat( psz_buffer, "," );
1494 strcat( psz_buffer, p_item->ppsz_list[i] );
1499 case CONFIG_ITEM_INTEGER:
1500 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1501 psz_bra = OPTION_VALUE_SEP "<";
1502 psz_type = _("integer");
1505 if( p_item->min.i || p_item->max.i )
1507 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1508 p_item->min.i, p_item->max.i );
1509 psz_type = psz_buffer;
1512 if( p_item->i_list )
1514 psz_bra = OPTION_VALUE_SEP "{";
1515 psz_type = psz_buffer;
1516 psz_buffer[0] = '\0';
1517 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1519 if( i ) strcat( psz_buffer, ", " );
1520 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1522 p_item->ppsz_list_text[i] );
1527 case CONFIG_ITEM_FLOAT:
1528 psz_bra = OPTION_VALUE_SEP "<";
1529 psz_type = _("float");
1531 if( p_item->min.f || p_item->max.f )
1533 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1534 p_item->min.f, p_item->max.f );
1535 psz_type = psz_buffer;
1538 case CONFIG_ITEM_BOOL:
1539 psz_bra = ""; psz_type = ""; psz_ket = "";
1540 if( !b_help_module )
1542 psz_suf = p_item->value.i ? _(" (default enabled)") :
1543 _(" (default disabled)");
1553 /* Add short option if any */
1554 if( p_item->i_short )
1556 sprintf( psz_short, "-%c,", p_item->i_short );
1560 strcpy( psz_short, " " );
1563 i = PADDING_SPACES - strlen( p_item->psz_name )
1564 - strlen( psz_bra ) - strlen( psz_type )
1565 - strlen( psz_ket ) - 1;
1567 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1569 psz_prefix = ", --no-";
1570 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1575 psz_spaces[0] = '\n';
1580 psz_spaces[i] = '\0';
1583 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1585 utf8_fprintf( stdout, psz_format_bool, psz_short,
1586 p_item->psz_name, psz_prefix, p_item->psz_name,
1587 psz_bra, psz_type, psz_ket, psz_spaces );
1591 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1592 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1595 psz_spaces[i] = ' ';
1597 /* We wrap the rest of the output */
1598 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1599 b_description_hack = b_description;
1602 psz_text = psz_buffer;
1603 i_cur_width = b_description && !b_description_hack
1604 ? i_width_description
1608 char *psz_parser, *psz_word;
1609 size_t i_end = strlen( psz_text );
1611 /* If the remaining text fits in a line, print it. */
1612 if( i_end <= i_cur_width )
1616 if( !b_description || b_description_hack )
1617 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1619 utf8_fprintf( stdout, "%s\n", psz_text );
1623 utf8_fprintf( stdout, "%s\n", psz_text );
1628 /* Otherwise, eat as many words as possible */
1629 psz_parser = psz_text;
1632 psz_word = psz_parser;
1633 psz_parser = strchr( psz_word, ' ' );
1634 /* If no space was found, we reached the end of the text
1635 * block; otherwise, we skip the space we just found. */
1636 psz_parser = psz_parser ? psz_parser + 1
1639 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1641 /* We cut a word in one of these cases:
1642 * - it's the only word in the line and it's too long.
1643 * - we used less than 80% of the width and the word we are
1644 * going to wrap is longer than 40% of the width, and even
1645 * if the word would have fit in the next line. */
1646 if( psz_word == psz_text
1647 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1648 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1650 char c = psz_text[i_cur_width];
1651 psz_text[i_cur_width] = '\0';
1654 if( !b_description || b_description_hack )
1655 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1656 psz_text, psz_spaces );
1658 utf8_fprintf( stdout, "%s\n%s",
1659 psz_text, psz_spaces );
1663 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1665 psz_text += i_cur_width;
1670 psz_word[-1] = '\0';
1673 if( !b_description || b_description_hack )
1674 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1675 psz_text, psz_spaces );
1677 utf8_fprintf( stdout, "%s\n%s",
1678 psz_text, psz_spaces );
1682 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1684 psz_text = psz_word;
1688 if( b_description_hack && p_item->psz_longtext )
1690 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1691 b_description_hack = VLC_FALSE;
1692 psz_spaces = psz_spaces_longtext;
1693 utf8_fprintf( stdout, "%s", psz_spaces );
1699 if( b_has_advanced )
1700 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1701 _( "add --advanced to your command line to see advanced options." ) );
1703 /* Release the module list */
1704 vlc_list_release( p_list );
1707 /*****************************************************************************
1708 * ListModules: list the available modules with their description
1709 *****************************************************************************
1710 * Print a list of all available modules (builtins and plugins) and a short
1711 * description for each one.
1712 *****************************************************************************/
1713 static void ListModules( libvlc_int_t *p_this, vlc_bool_t b_verbose )
1715 vlc_list_t *p_list = NULL;
1716 module_t *p_parser = NULL;
1717 char psz_spaces[22];
1720 vlc_bool_t b_color = config_GetInt( p_this, "color" ) > 0;
1722 memset( psz_spaces, ' ', 22 );
1725 ShowConsole( VLC_TRUE );
1728 /* List all modules */
1729 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1731 /* Enumerate each module */
1732 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1736 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1738 /* Nasty hack, but right now I'm too tired to think about a nice
1740 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1745 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1746 p_parser->psz_object_name,
1748 p_parser->psz_longname );
1750 utf8_fprintf( stdout, " %s%s %s\n",
1751 p_parser->psz_object_name,
1752 psz_spaces, p_parser->psz_longname );
1756 const char *const *pp_shortcut = p_parser->pp_shortcuts;
1757 while( *pp_shortcut )
1759 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1762 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1765 utf8_fprintf( stdout, " s %s\n",
1770 if( p_parser->psz_capability )
1773 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1774 p_parser->psz_capability,
1775 p_parser->i_score );
1777 utf8_fprintf( stdout, " c %s (%d)\n",
1778 p_parser->psz_capability,
1779 p_parser->i_score );
1783 psz_spaces[i] = ' ';
1786 vlc_list_release( p_list );
1788 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1793 /*****************************************************************************
1794 * Version: print complete program version
1795 *****************************************************************************
1796 * Print complete program version and build number.
1797 *****************************************************************************/
1798 static void Version( void )
1801 ShowConsole( VLC_TRUE );
1804 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1805 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1806 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1807 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1808 if( strcmp( VLC_Changeset(), "exported" ) )
1809 utf8_fprintf( stdout, _("Based upon svn changeset [%s]\n"),
1811 utf8_fprintf( stdout, LICENSE_MSG );
1813 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1818 /*****************************************************************************
1819 * ShowConsole: On Win32, create an output console for debug messages
1820 *****************************************************************************
1821 * This function is useful only on Win32.
1822 *****************************************************************************/
1824 static void ShowConsole( vlc_bool_t b_dofile )
1827 FILE *f_help = NULL;
1829 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1833 freopen( "CONOUT$", "w", stderr );
1834 freopen( "CONIN$", "r", stdin );
1836 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1839 freopen( "vlc-help.txt", "wt", stdout );
1840 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1842 else freopen( "CONOUT$", "w", stdout );
1848 /*****************************************************************************
1849 * PauseConsole: On Win32, wait for a key press before closing the console
1850 *****************************************************************************
1851 * This function is useful only on Win32.
1852 *****************************************************************************/
1854 static void PauseConsole( void )
1858 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1860 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1868 /*****************************************************************************
1869 * ConsoleWidth: Return the console width in characters
1870 *****************************************************************************
1871 * We use the stty shell command to get the console width; if this fails or
1872 * if the width is less than 80, we default to 80.
1873 *****************************************************************************/
1874 static int ConsoleWidth( void )
1880 char *psz_parser = NULL;
1884 file = popen( "stty size 2>/dev/null", "r" );
1887 i_ret = fread( buf, 1, 20, file );
1891 psz_parser = strchr( buf, ' ' );
1894 i_ret = atoi( psz_parser + 1 );
1909 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1910 vlc_value_t old_val, vlc_value_t new_val, void *param)
1912 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1917 if( new_val.i_int >= -1 )
1919 p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1924 /*****************************************************************************
1925 * InitDeviceValues: initialize device values
1926 *****************************************************************************
1927 * This function inits the dvd, vcd and cd-audio values
1928 *****************************************************************************/
1929 static void InitDeviceValues( libvlc_int_t *p_vlc )
1932 LibHalContext * ctx = NULL;
1934 char **devices = NULL;
1935 char *block_dev = NULL;
1939 DBusConnection *p_connection = NULL;
1942 ctx = libhal_ctx_new();
1944 dbus_error_init( &error );
1945 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
1946 if( dbus_error_is_set( &error ) || !p_connection )
1948 dbus_error_free( &error );
1951 libhal_ctx_set_dbus_connection( ctx, p_connection );
1952 if( libhal_ctx_init( ctx, &error ) )
1954 ctx = hal_initialize( NULL, FALSE );
1960 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
1962 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
1965 for( i = 0; i < i_devices; i++ )
1968 if( !libhal_device_property_exists( ctx, devices[i],
1969 "storage.cdrom.dvd", NULL ) )
1971 if( !hal_device_property_exists( ctx, devices[ i ],
1972 "storage.cdrom.dvd" ) )
1978 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
1979 "storage.cdrom.dvd", NULL );
1980 block_dev = libhal_device_get_property_string( ctx,
1981 devices[ i ], "block.device" , NULL );
1983 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
1984 "storage.cdrom.dvd" );
1985 block_dev = hal_device_get_property_string( ctx, devices[ i ],
1990 config_PutPsz( p_vlc, "dvd", block_dev );
1993 config_PutPsz( p_vlc, "vcd", block_dev );
1994 config_PutPsz( p_vlc, "cd-audio", block_dev );
1996 libhal_free_string( block_dev );
1998 hal_free_string( block_dev );
2002 libhal_free_string_array( devices );
2004 hal_free_string_array( devices );
2009 libhal_ctx_shutdown( ctx, NULL );
2010 dbus_connection_unref( p_connection );
2012 hal_shutdown( ctx );
2017 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2021 #endif /* HAVE_HAL */