1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 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 *****************************************************************************/
39 #include <vlc_common.h>
40 #include "control/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45 #include "interface/interface.h"
47 #include <errno.h> /* ENOMEM */
48 #include <stdio.h> /* sprintf() */
50 #include <stdlib.h> /* free() */
53 # include <netinet/in.h> /* BSD: struct in_addr */
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
62 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
63 # include "extras/getopt.h"
71 /* used for one-instance mode */
72 # include <dbus/dbus.h>
76 # include <hal/libhal.h>
79 #include <vlc_playlist.h>
80 #include <vlc_interface.h>
83 #include "audio_output/aout_internal.h"
88 #include "stream_output/stream_output.h"
90 #include <vlc_charset.h>
94 #include "playlist/playlist_internal.h"
100 /*****************************************************************************
101 * The evil global variables. We handle them with care, don't worry.
102 *****************************************************************************/
103 static libvlc_int_t * p_static_vlc = NULL;
104 static unsigned i_instances = 0;
107 static bool b_daemon = false;
110 /*****************************************************************************
112 *****************************************************************************/
113 void __vlc_gc_incref( gc_object_t * p_gc )
115 assert( p_gc->i_gc_refcount > 0 );
117 /* FIXME: atomic version needed! */
118 p_gc->i_gc_refcount ++;
121 void __vlc_gc_decref( gc_object_t *p_gc )
124 assert( p_gc->i_gc_refcount > 0 );
126 /* FIXME: atomic version needed! */
127 p_gc->i_gc_refcount -- ;
129 if( p_gc->i_gc_refcount == 0 )
131 p_gc->pf_destructor( p_gc );
132 /* Do not use the p_gc pointer from now on ! */
137 __vlc_gc_init( gc_object_t * p_gc, void (*pf_destructor)( gc_object_t * ),
140 p_gc->i_gc_refcount = 1;
141 p_gc->pf_destructor = pf_destructor;
142 p_gc->p_destructor_arg = arg;
145 /*****************************************************************************
147 *****************************************************************************/
148 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
149 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
150 static void SetLanguage ( char const * );
152 static inline int LoadMessages (void);
153 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
154 static void Help ( libvlc_int_t *, char const *psz_help_name );
155 static void Usage ( libvlc_int_t *, char const *psz_module_name );
156 static void ListModules ( libvlc_int_t *, bool );
157 static void Version ( void );
160 static void ShowConsole ( bool );
161 static void PauseConsole ( void );
163 static int ConsoleWidth ( void );
165 static int VerboseCallback( vlc_object_t *, char const *,
166 vlc_value_t, vlc_value_t, void * );
168 static void InitDeviceValues( libvlc_int_t * );
171 * Allocate a libvlc instance, initialize global data if needed
172 * It also initializes the threading system
174 libvlc_int_t * libvlc_InternalCreate( void )
176 libvlc_int_t *p_libvlc;
178 char *psz_env = NULL;
180 /* vlc_threads_init *must* be the first internal call! No other call is
181 * allowed before the thread system has been initialized. */
182 if (vlc_threads_init ())
185 libvlc_global_data_t *p_libvlc_global = vlc_global();
186 /* Now that the thread system is initialized, we don't have much, but
187 * at least we have variables */
188 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
189 if( i_instances == 0 )
191 /* Guess what CPU we have */
192 cpu_flags = CPUCapabilities();
193 /* The module bank will be initialized later */
194 p_libvlc_global->p_module_bank = NULL;
197 /* Allocate a libvlc instance object */
198 p_libvlc = vlc_custom_create( VLC_OBJECT(p_libvlc_global),
199 sizeof (*p_libvlc) + sizeof (libvlc_priv_t),
200 VLC_OBJECT_LIBVLC, "libvlc" );
201 if( p_libvlc != NULL )
203 vlc_mutex_unlock( lock );
205 if( p_libvlc == NULL )
208 priv = libvlc_priv (p_libvlc);
209 priv->p_playlist = NULL;
210 priv->p_interaction = NULL;
212 p_libvlc->psz_object_name = strdup( "libvlc" );
214 /* Initialize message queue */
215 msg_Create( p_libvlc );
217 /* Find verbosity from VLC_VERBOSE environment variable */
218 psz_env = getenv( "VLC_VERBOSE" );
219 if( psz_env != NULL )
220 priv->i_verbose = atoi( psz_env );
223 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
224 priv->b_color = isatty( 2 ); /* 2 is for stderr */
226 priv->b_color = false;
229 /* Announce who we are - Do it only for first instance ? */
230 msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
231 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
233 /* Initialize mutexes */
234 vlc_mutex_init( &priv->timer_lock );
235 vlc_mutex_init( &priv->config_lock );
237 priv->threads_count = 0;
238 vlc_mutex_init (&priv->threads_lock);
239 vlc_cond_init (NULL, &priv->threads_wait);
241 /* Store data for the non-reentrant API */
242 p_static_vlc = p_libvlc;
248 * Initialize a libvlc instance
249 * This function initializes a previously allocated libvlc instance:
251 * - gettext initialization
252 * - message queue, module bank and playlist initialization
253 * - configuration and commandline parsing
255 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
256 const char *ppsz_argv[] )
258 libvlc_global_data_t *p_libvlc_global = vlc_global();
259 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
260 char p_capabilities[200];
262 char * psz_modules = NULL;
263 char * psz_parser = NULL;
264 char * psz_control = NULL;
266 int i_ret = VLC_EEXIT;
267 playlist_t *p_playlist = NULL;
269 #if defined( ENABLE_NLS ) \
270 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
271 # if defined (WIN32) || defined (__APPLE__)
276 /* System specific initialization code */
277 system_Init( p_libvlc, &i_argc, ppsz_argv );
279 /* Get the executable name (similar to the basename command) */
280 if( i_argc > 0 && ppsz_argv[0][0] )
282 free( p_libvlc->psz_object_name );
284 const char *psz_exe = strrchr( ppsz_argv[0], '/' );
285 if( psz_exe && *(psz_exe + 1) )
286 p_libvlc->psz_object_name = strdup( psz_exe + 1 );
288 p_libvlc->psz_object_name = strdup( ppsz_argv[0] );
292 * Support for gettext
296 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
297 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
299 /* Initialize the module bank and load the configuration of the
300 * main module. We need to do this at this stage to be able to display
301 * a short help if required by the user. (short help == main module
303 module_InitBank( p_libvlc );
305 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
307 module_EndBank( p_libvlc );
311 /* Check for short help option */
312 if( config_GetInt( p_libvlc, "help" ) > 0 )
314 Help( p_libvlc, "help" );
316 i_ret = VLC_EEXITSUCCESS;
318 /* Check for version option */
319 else if( config_GetInt( p_libvlc, "version" ) > 0 )
323 i_ret = VLC_EEXITSUCCESS;
326 /* Set the config file stuff */
327 priv->psz_configfile = config_GetCustomConfigFile( p_libvlc );
329 /* Check for plugins cache options */
330 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
332 p_libvlc_global->p_module_bank->b_cache_delete = true;
335 /* Will be re-done properly later on */
336 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
338 /* Check for daemon mode */
340 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
343 char *psz_pidfile = NULL;
345 if( daemon( 1, 0) != 0 )
347 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
352 /* lets check if we need to write the pidfile */
353 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
354 if( psz_pidfile != NULL )
357 pid_t i_pid = getpid ();
358 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
359 i_pid, psz_pidfile );
360 pidfile = utf8_fopen( psz_pidfile,"w" );
361 if( pidfile != NULL )
363 utf8_fprintf( pidfile, "%d", (int)i_pid );
368 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
377 if( ( i_pid = fork() ) < 0 )
379 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
384 /* This is the parent, exit right now */
385 msg_Dbg( p_libvlc, "closing parent process" );
387 i_ret = VLC_EEXITSUCCESS;
391 /* We are the child */
392 msg_Dbg( p_libvlc, "daemon spawned" );
393 close( STDIN_FILENO );
394 close( STDOUT_FILENO );
395 close( STDERR_FILENO );
405 module_EndBank( p_libvlc );
409 /* Check for translation config option */
410 #if defined( ENABLE_NLS ) \
411 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
412 # if defined (WIN32) || defined (__APPLE__)
413 /* This ain't really nice to have to reload the config here but it seems
414 * the only way to do it. */
416 if( !config_GetInt( p_libvlc, "ignore-config" ) )
417 config_LoadConfigFile( p_libvlc, "main" );
418 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
420 /* Check if the user specified a custom language */
421 psz_language = config_GetPsz( p_libvlc, "language" );
422 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
424 bool b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
426 /* Reset the default domain */
427 SetLanguage( psz_language );
429 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
430 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
432 module_EndBank( p_libvlc );
433 module_InitBank( p_libvlc );
434 if( !config_GetInt( p_libvlc, "ignore-config" ) )
435 config_LoadConfigFile( p_libvlc, "main" );
436 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
437 p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
439 free( psz_language );
444 * Load the builtins and plugins into the module_bank.
445 * We have to do it before config_Load*() because this also gets the
446 * list of configuration options exported by each module and loads their
449 module_LoadBuiltins( p_libvlc );
450 module_LoadPlugins( p_libvlc );
451 if( p_libvlc->b_die )
456 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
457 vlc_internals( p_libvlc_global->p_module_bank )->i_children );
459 /* Check for help on modules */
460 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
462 Help( p_libvlc, p_tmp );
465 i_ret = VLC_EEXITSUCCESS;
467 /* Check for full help option */
468 else if( config_GetInt( p_libvlc, "full-help" ) > 0 )
470 config_PutInt( p_libvlc, "advanced", 1);
471 config_PutInt( p_libvlc, "help-verbose", 1);
472 Help( p_libvlc, "full-help" );
474 i_ret = VLC_EEXITSUCCESS;
476 /* Check for long help option */
477 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
479 Help( p_libvlc, "longhelp" );
481 i_ret = VLC_EEXITSUCCESS;
483 /* Check for module list option */
484 else if( config_GetInt( p_libvlc, "list" ) > 0 )
486 ListModules( p_libvlc, false );
488 i_ret = VLC_EEXITSUCCESS;
490 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
492 ListModules( p_libvlc, true );
494 i_ret = VLC_EEXITSUCCESS;
497 /* Check for config file options */
498 if( !config_GetInt( p_libvlc, "ignore-config" ) )
500 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
502 config_ResetAll( p_libvlc );
503 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
504 config_SaveConfigFile( p_libvlc, NULL );
506 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
508 config_LoadConfigFile( p_libvlc, NULL );
509 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
510 config_SaveConfigFile( p_libvlc, NULL );
516 module_EndBank( p_libvlc );
523 InitDeviceValues( p_libvlc );
526 * Override default configuration with config file settings
528 if( !config_GetInt( p_libvlc, "ignore-config" ) )
529 config_LoadConfigFile( p_libvlc, NULL );
532 * Override configuration with command line settings
534 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
537 ShowConsole( false );
538 /* Pause the console because it's destroyed when we exit */
539 fprintf( stderr, "The command line options couldn't be loaded, check "
540 "that they are valid.\n" );
543 module_EndBank( p_libvlc );
548 * System specific configuration
550 system_Configure( p_libvlc, &i_argc, ppsz_argv );
552 /* FIXME: could be replaced by using Unix sockets */
554 dbus_threads_init_default();
556 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
558 /* Initialise D-Bus interface, check for other instances */
559 DBusConnection *p_conn = NULL;
560 DBusError dbus_error;
562 dbus_error_init( &dbus_error );
564 /* connect to the session bus */
565 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
568 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
569 dbus_error.message );
570 dbus_error_free( &dbus_error );
574 /* check if VLC is available on the bus
575 * if not: D-Bus control is not enabled on the other
576 * instance and we can't pass MRLs to it */
577 DBusMessage *p_test_msg = NULL;
578 DBusMessage *p_test_reply = NULL;
579 p_test_msg = dbus_message_new_method_call(
580 "org.mpris.vlc", "/",
581 "org.freedesktop.MediaPlayer", "Identity" );
582 /* block until a reply arrives */
583 p_test_reply = dbus_connection_send_with_reply_and_block(
584 p_conn, p_test_msg, -1, &dbus_error );
585 dbus_message_unref( p_test_msg );
586 if( p_test_reply == NULL )
588 dbus_error_free( &dbus_error );
589 msg_Dbg( p_libvlc, "No Media Player is running. "
590 "Continuing normally." );
595 DBusMessage* p_dbus_msg = NULL;
596 DBusMessageIter dbus_args;
597 DBusPendingCall* p_dbus_pending = NULL;
600 dbus_message_unref( p_test_reply );
601 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
603 for( i_input = optind;i_input < i_argc;i_input++ )
605 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
606 ppsz_argv[i_input] );
608 p_dbus_msg = dbus_message_new_method_call(
609 "org.mpris.vlc", "/TrackList",
610 "org.freedesktop.MediaPlayer", "AddTrack" );
612 if ( NULL == p_dbus_msg )
614 msg_Err( p_libvlc, "D-Bus problem" );
615 system_End( p_libvlc );
616 exit( VLC_ETIMEOUT );
620 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
621 if ( !dbus_message_iter_append_basic( &dbus_args,
622 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
624 dbus_message_unref( p_dbus_msg );
625 system_End( p_libvlc );
629 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
631 if ( !dbus_message_iter_append_basic( &dbus_args,
632 DBUS_TYPE_BOOLEAN, &b_play ) )
634 dbus_message_unref( p_dbus_msg );
635 system_End( p_libvlc );
639 /* send message and get a handle for a reply */
640 if ( !dbus_connection_send_with_reply ( p_conn,
641 p_dbus_msg, &p_dbus_pending, -1 ) )
643 msg_Err( p_libvlc, "D-Bus problem" );
644 dbus_message_unref( p_dbus_msg );
645 system_End( p_libvlc );
646 exit( VLC_ETIMEOUT );
649 if ( NULL == p_dbus_pending )
651 msg_Err( p_libvlc, "D-Bus problem" );
652 dbus_message_unref( p_dbus_msg );
653 system_End( p_libvlc );
654 exit( VLC_ETIMEOUT );
656 dbus_connection_flush( p_conn );
657 dbus_message_unref( p_dbus_msg );
658 /* block until we receive a reply */
659 dbus_pending_call_block( p_dbus_pending );
660 dbus_pending_call_unref( p_dbus_pending );
661 } /* processes all command line MRLs */
664 system_End( p_libvlc );
668 /* we unreference the connection when we've finished with it */
669 if( p_conn ) dbus_connection_unref( p_conn );
674 * Message queue options
677 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
678 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
681 var_Set( p_libvlc, "verbose", val );
683 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
684 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
687 priv->b_color = config_GetInt( p_libvlc, "color" ) > 0;
690 * Output messages that may still be in the queue
692 msg_Flush( p_libvlc );
694 if( !config_GetInt( p_libvlc, "fpu" ) )
695 cpu_flags &= ~CPU_CAPABILITY_FPU;
697 #if defined( __i386__ ) || defined( __x86_64__ )
698 if( !config_GetInt( p_libvlc, "mmx" ) )
699 cpu_flags &= ~CPU_CAPABILITY_MMX;
700 if( !config_GetInt( p_libvlc, "3dn" ) )
701 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
702 if( !config_GetInt( p_libvlc, "mmxext" ) )
703 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
704 if( !config_GetInt( p_libvlc, "sse" ) )
705 cpu_flags &= ~CPU_CAPABILITY_SSE;
706 if( !config_GetInt( p_libvlc, "sse2" ) )
707 cpu_flags &= ~CPU_CAPABILITY_SSE2;
709 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
710 if( !config_GetInt( p_libvlc, "altivec" ) )
711 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
714 #define PRINT_CAPABILITY( capability, string ) \
715 if( vlc_CPU() & capability ) \
717 strncat( p_capabilities, string " ", \
718 sizeof(p_capabilities) - strlen(p_capabilities) ); \
719 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
722 p_capabilities[0] = '\0';
723 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
724 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
725 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
726 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
727 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
728 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
729 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
730 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
731 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
732 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
733 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
736 * Choose the best memcpy module
738 priv->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
740 priv->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
742 priv->pp_timers = NULL;
745 p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
746 if( !p_libvlc->p_stats )
748 vlc_object_release( p_libvlc );
751 vlc_mutex_init( &p_libvlc->p_stats->lock );
752 priv->p_stats_computer = NULL;
754 /* Init the array that holds every input item */
755 ARRAY_INIT( priv->input_items );
756 priv->i_last_input_id = 0;
759 * Initialize hotkey handling
761 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
762 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
763 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
764 /* Do a copy (we don't need to modify the strings) */
765 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
766 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
767 p_libvlc->p_hotkeys );
769 /* Initialize interaction */
770 priv->p_interaction = interaction_Init( p_libvlc );
772 /* Initialize playlist and get commandline files */
773 playlist_ThreadCreate( p_libvlc );
774 if( !priv->p_playlist )
776 msg_Err( p_libvlc, "playlist initialization failed" );
777 if( priv->p_memcpy_module != NULL )
779 module_Unneed( p_libvlc, priv->p_memcpy_module );
781 module_EndBank( p_libvlc );
784 p_playlist = priv->p_playlist;
786 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
787 if( psz_modules && *psz_modules )
789 /* Add service discovery modules */
790 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
795 /* Initialize VLM if vlm-conf is specified */
796 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
797 if( psz_parser && *psz_parser )
799 priv->p_vlm = vlm_New( p_libvlc );
801 msg_Err( p_libvlc, "VLM initialization failed" );
807 * Load background interfaces
809 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
810 psz_control = config_GetPsz( p_libvlc, "control" );
812 if( psz_modules && *psz_modules && psz_control && *psz_control )
814 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
815 strlen( psz_control ) + 1 );
816 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
818 else if( psz_control && *psz_control )
821 psz_modules = strdup( psz_control );
824 psz_parser = psz_modules;
825 while ( psz_parser && *psz_parser )
827 char *psz_module, *psz_temp;
828 psz_module = psz_parser;
829 psz_parser = strchr( psz_module, ':' );
835 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
838 sprintf( psz_temp, "%s,none", psz_module );
839 libvlc_InternalAddIntf( p_libvlc, psz_temp );
847 * Always load the hotkeys interface if it exists
849 libvlc_InternalAddIntf( p_libvlc, "hotkeys,none" );
852 /* loads dbus control interface if in one-instance mode
853 * we do it only when playlist exists, because dbus module needs it */
854 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
855 libvlc_InternalAddIntf( p_libvlc, "dbus,none" );
857 /* Prevents the power management daemon from suspending the system
858 * when VLC is active */
859 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
860 libvlc_InternalAddIntf( p_libvlc, "inhibit,none" );
864 * If needed, load the Xscreensaver interface
865 * Currently, only for X
867 #ifdef HAVE_X11_XLIB_H
868 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
870 libvlc_InternalAddIntf( p_libvlc, "screensaver,none" );
874 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
876 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
879 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
881 char *logmode = var_CreateGetString( p_libvlc, "logmode" );
882 var_SetString( p_libvlc, "logmode", "syslog" );
883 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
887 var_SetString( p_libvlc, "logmode", logmode );
891 var_Destroy( p_libvlc, "logmode" );
895 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
897 libvlc_InternalAddIntf( p_libvlc, "showintf,none" );
900 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
902 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
906 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
908 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
911 char *psz_morecodecs;
912 if( asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs) != -1 )
914 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
915 free( psz_morecodecs );
919 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
925 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
927 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
928 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
929 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
930 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
931 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
932 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
933 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
934 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
935 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
937 /* Create volume callback system. */
938 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
940 /* Create a variable for showing the interface (moved from playlist). */
941 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
942 var_SetBool( p_libvlc, "intf-show", true );
944 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
947 * Get input filenames given as commandline arguments
949 GetFilenames( p_libvlc, i_argc, ppsz_argv );
952 * Get --open argument
954 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
955 var_Get( p_libvlc, "open", &val );
956 if ( val.psz_string != NULL && *val.psz_string )
958 playlist_t *p_playlist = pl_Yield( p_libvlc );
959 playlist_AddExt( p_playlist, val.psz_string, NULL, PLAYLIST_INSERT, 0,
960 -1, NULL, 0, true, pl_Unlocked );
961 pl_Release( p_libvlc );
963 free( val.psz_string );
969 * Cleanup a libvlc instance. The instance is not completely deallocated
970 * \param p_libvlc the instance to clean
972 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
974 intf_thread_t * p_intf = NULL;
975 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
977 /* Ask the interfaces to stop and destroy them */
978 msg_Dbg( p_libvlc, "removing all interfaces" );
979 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
981 intf_StopThread( p_intf );
982 vlc_object_detach( p_intf );
983 vlc_object_release( p_intf ); /* for intf_Create() */
984 vlc_object_release( p_intf ); /* for vlc_object_find() */
988 /* Destroy VLM if created in libvlc_InternalInit */
991 vlm_Delete( priv->p_vlm );
995 playlist_t *p_playlist = priv->p_playlist;
996 /* Remove all services discovery */
997 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
998 playlist_ServicesDiscoveryKillAll( p_playlist );
1001 /* Any thread still running must not assume pl_Yield() succeeds. */
1002 msg_Dbg( p_libvlc, "removing playlist" );
1003 priv->p_playlist = NULL;
1004 vlc_object_kill( p_playlist ); /* <-- memory barrier for pl_Yield() */
1005 vlc_thread_join( p_playlist );
1006 vlc_object_release( p_playlist );
1008 /* Free interaction */
1009 msg_Dbg( p_libvlc, "removing interaction" );
1010 interaction_Destroy( priv->p_interaction );
1012 /* Free video outputs */
1013 msg_Dbg( p_libvlc, "removing all video outputs" );
1014 vlc_list_t *list = vlc_list_find (p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD);
1015 for (int i = 0; i < list->i_count; i++)
1016 vlc_object_release (list->p_values[i].p_object);
1017 vlc_list_release (list);
1019 stats_TimersDumpAll( p_libvlc );
1020 stats_TimersCleanAll( p_libvlc );
1023 announce_handler_t * p_announce;
1025 /* Free announce handler(s?) */
1026 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1029 msg_Dbg( p_libvlc, "removing announce handler" );
1030 vlc_object_detach( p_announce );
1031 vlc_object_release( p_announce );
1032 announce_HandlerDestroy( p_announce );
1036 /* Make sure all threads are completed before we start looking for
1037 * reference leaks and deinitializing core LibVLC subsytems. */
1038 vlc_mutex_lock (&priv->threads_lock);
1039 while (priv->threads_count)
1041 msg_Dbg (p_libvlc, "waiting for %u remaining threads",
1042 priv->threads_count);
1043 vlc_cond_wait (&priv->threads_wait, &priv->threads_lock);
1045 vlc_mutex_unlock (&priv->threads_lock);
1047 bool b_clean = true;
1048 FOREACH_ARRAY( input_item_t *p_del, priv->input_items )
1049 msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1050 p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1054 ARRAY_RESET( priv->input_items );
1056 msg_Dbg( p_libvlc, "removing stats" );
1057 vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1058 FREENULL( p_libvlc->p_stats );
1064 * Destroy everything.
1065 * This function requests the running threads to finish, waits for their
1066 * termination, and destroys their structure.
1067 * It stops the thread systems: no instance can run after this has run
1068 * \param p_libvlc the instance to destroy
1070 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
1073 return VLC_EGENERIC;
1075 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1078 char* psz_pidfile = NULL;
1082 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1083 if( psz_pidfile != NULL )
1085 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1086 if( unlink( psz_pidfile ) == -1 )
1088 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1092 free( psz_pidfile );
1096 if( priv->p_memcpy_module )
1098 module_Unneed( p_libvlc, priv->p_memcpy_module );
1099 priv->p_memcpy_module = NULL;
1102 /* Free module bank. It is refcounted, so we call this each time */
1103 module_EndBank( p_libvlc );
1105 FREENULL( priv->psz_configfile );
1106 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1107 p_libvlc->p_hotkeys );
1108 FREENULL( p_libvlc->p_hotkeys );
1110 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1113 if( i_instances == 0 )
1115 /* System specific cleaning code */
1116 system_End( p_libvlc );
1118 vlc_mutex_unlock( lock );
1120 msg_Flush( p_libvlc );
1121 msg_Destroy( p_libvlc );
1123 /* Destroy mutexes */
1124 vlc_mutex_destroy( &priv->config_lock );
1125 vlc_mutex_destroy( &priv->timer_lock );
1126 vlc_cond_destroy (&priv->threads_wait);
1127 vlc_mutex_destroy (&priv->threads_lock);
1129 vlc_object_release( p_libvlc );
1132 /* Stop thread system: last one out please shut the door!
1133 * The number of initializations of the thread system is counted, we
1134 * can call this each time */
1141 * Add an interface plugin and run it
1143 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
1146 intf_thread_t *p_intf = NULL;
1149 return VLC_EGENERIC;
1151 if( !psz_module ) /* requesting the default interface */
1153 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1154 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1158 /* Daemon mode hack.
1159 * We prefer the dummy interface if none is specified. */
1160 psz_module = "dummy";
1163 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1165 free( psz_interface );
1168 /* Try to create the interface */
1169 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
1170 if( p_intf == NULL )
1172 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1174 return VLC_EGENERIC;
1177 /* Try to run the interface */
1178 i_err = intf_RunThread( p_intf );
1181 vlc_object_detach( p_intf );
1182 vlc_object_release( p_intf );
1189 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1190 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1191 /*****************************************************************************
1192 * SetLanguage: set the interface language.
1193 *****************************************************************************
1194 * We set the LC_MESSAGES locale category for interface messages and buttons,
1195 * as well as the LC_CTYPE category for string sorting and possible wide
1196 * character support.
1197 *****************************************************************************/
1198 static void SetLanguage ( const char *psz_lang )
1201 /* I need that under Darwin, please check it doesn't disturb
1202 * other platforms. --Meuuh */
1203 setenv( "LANG", psz_lang, 1 );
1206 /* We set LC_ALL manually because it is the only way to set
1207 * the language at runtime under eg. Windows. Beware that this
1208 * makes the environment unconsistent when libvlc is unloaded and
1209 * should probably be moved to a safer place like vlc.c. */
1210 static char psz_lcall[20];
1211 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1212 psz_lcall[19] = '\0';
1213 putenv( psz_lcall );
1216 setlocale( LC_ALL, psz_lang );
1221 static inline int LoadMessages (void)
1223 #if defined( ENABLE_NLS ) \
1224 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1225 /* Specify where to find the locales for current domain */
1226 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1227 static const char psz_path[] = LOCALEDIR;
1229 char psz_path[1024];
1230 if (snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "%s",
1231 config_GetDataDir(), "locale")
1232 >= (int)sizeof (psz_path))
1236 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1238 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1239 " to directory %s\n", psz_path);
1243 /* LibVLC wants all messages in UTF-8.
1244 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1245 * and other functions that are not part of our text domain.
1247 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1249 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1251 // Unbinds the text domain to avoid broken encoding
1252 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1256 /* LibVLC does NOT set the default textdomain, since it is a library.
1257 * This could otherwise break programs using LibVLC (other than VLC).
1258 * textdomain (PACKAGE_NAME);
1264 /*****************************************************************************
1265 * GetFilenames: parse command line options which are not flags
1266 *****************************************************************************
1267 * Parse command line for input files as well as their associated options.
1268 * An option always follows its associated input and begins with a ":".
1269 *****************************************************************************/
1270 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1272 int i_opt, i_options;
1274 /* We assume that the remaining parameters are filenames
1275 * and their input options */
1276 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1280 /* Count the input options */
1281 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1287 /* TODO: write an internal function of this one, to avoid
1288 * unnecessary lookups. */
1290 playlist_t *p_playlist = pl_Yield( p_vlc );
1291 playlist_AddExt( p_playlist, ppsz_argv[i_opt], NULL, PLAYLIST_INSERT,
1292 0, -1, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ),
1293 i_options, true, pl_Unlocked );
1294 pl_Release( p_vlc );
1300 /*****************************************************************************
1301 * Help: print program help
1302 *****************************************************************************
1303 * Print a short inline help. Message interface is initialized at this stage.
1304 *****************************************************************************/
1305 static inline void print_help_on_full_help( void )
1307 utf8_fprintf( stdout, "\n" );
1308 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
1311 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1314 ShowConsole( true );
1317 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1319 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1320 Usage( p_this, "help" );
1321 Usage( p_this, "main" );
1322 print_help_on_full_help();
1324 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1326 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1327 Usage( p_this, NULL );
1328 print_help_on_full_help();
1330 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
1332 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1333 Usage( p_this, NULL );
1335 else if( psz_help_name )
1337 Usage( p_this, psz_help_name );
1340 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1345 /*****************************************************************************
1346 * Usage: print module usage
1347 *****************************************************************************
1348 * Print a short inline help. Message interface is initialized at this stage.
1349 *****************************************************************************/
1350 # define COL(x) "\033[" #x ";1m"
1351 # define RED COL(31)
1352 # define GREEN COL(32)
1353 # define YELLOW COL(33)
1354 # define BLUE COL(34)
1355 # define MAGENTA COL(35)
1356 # define CYAN COL(36)
1357 # define WHITE COL(0)
1358 # define GRAY "\033[0m"
1359 static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
1361 if( !p_item ) return;
1364 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1366 if( b_description && p_item->psz_longtext )
1367 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1368 p_item->psz_longtext );
1372 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1373 if( b_description && p_item->psz_longtext )
1374 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1378 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1380 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1381 /* short option ------' | | | | | | |
1382 * option name ------------' | | | | | |
1383 * <bra ---------------------' | | | | |
1384 * option type or "" ----------' | | | |
1385 * ket> -------------------------' | | |
1386 * padding spaces -----------------' | |
1387 * comment --------------------------' |
1388 * comment suffix ---------------------'
1390 * The purpose of having bra and ket is that we might i18n them as well.
1393 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1394 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1396 #define LINE_START 8
1397 #define PADDING_SPACES 25
1399 # define OPTION_VALUE_SEP "="
1401 # define OPTION_VALUE_SEP " "
1403 vlc_list_t *p_list = NULL;
1404 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1405 char psz_spaces_longtext[LINE_START+3];
1406 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1407 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1408 char psz_buffer[10000];
1411 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1412 int i_width_description = i_width + PADDING_SPACES - 1;
1413 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1414 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1415 bool b_description_hack;
1416 bool b_color = config_GetInt( p_this, "color" ) > 0;
1417 bool b_has_advanced = false;
1419 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1420 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1421 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1422 psz_spaces_longtext[LINE_START+2] = '\0';
1426 b_color = false; // don't put color control codes in a .txt file
1430 strcpy( psz_format, COLOR_FORMAT_STRING );
1431 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1435 strcpy( psz_format, FORMAT_STRING );
1436 strcpy( psz_format_bool, FORMAT_STRING );
1439 /* List all modules */
1440 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1442 /* Ugly hack to make sure that the help options always come first
1444 if( !psz_module_name )
1445 Usage( p_this, "help" );
1447 /* Enumerate the config for each module */
1448 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1451 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1452 module_config_t *p_item = NULL;
1453 module_config_t *p_section = NULL;
1454 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1456 if( psz_module_name && strcmp( psz_module_name,
1457 p_parser->psz_object_name ) )
1459 char *const *pp_shortcut = p_parser->pp_shortcuts;
1460 while( *pp_shortcut )
1462 if( !strcmp( psz_module_name, *pp_shortcut ) )
1470 /* Ignore modules without config options */
1471 if( !p_parser->i_config_items )
1476 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1477 /* Ugly hack to make sure that the help options always come first
1479 if( !psz_module_name && b_help_module )
1482 /* Ignore modules with only advanced config options if requested */
1485 for( p_item = p_parser->p_config;
1489 if( (p_item->i_type & CONFIG_ITEM) &&
1490 !p_item->b_advanced ) break;
1494 /* Print name of module */
1495 if( strcmp( "main", p_parser->psz_object_name ) )
1498 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1499 p_parser->psz_longname );
1501 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1503 if( p_parser->psz_help )
1506 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1508 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1511 /* Print module options */
1512 for( p_item = p_parser->p_config;
1516 char *psz_text, *psz_spaces = psz_spaces_text;
1517 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1518 const char *psz_suf = "", *psz_prefix = NULL;
1522 /* Skip removed options */
1523 if( p_item->b_removed )
1527 /* Skip advanced options if requested */
1528 if( p_item->b_advanced && !b_advanced )
1530 b_has_advanced = true;
1534 switch( p_item->i_type )
1536 case CONFIG_HINT_CATEGORY:
1537 case CONFIG_HINT_USAGE:
1538 if( !strcmp( "main", p_parser->psz_object_name ) )
1541 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1544 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1546 if( b_description && p_item->psz_longtext )
1549 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1550 p_item->psz_longtext );
1552 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1556 case CONFIG_HINT_SUBCATEGORY:
1557 if( strcmp( "main", p_parser->psz_object_name ) )
1559 case CONFIG_SECTION:
1563 case CONFIG_ITEM_STRING:
1564 case CONFIG_ITEM_FILE:
1565 case CONFIG_ITEM_DIRECTORY:
1566 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1567 case CONFIG_ITEM_MODULE_CAT:
1568 case CONFIG_ITEM_MODULE_LIST:
1569 case CONFIG_ITEM_MODULE_LIST_CAT:
1570 case CONFIG_ITEM_FONT:
1571 case CONFIG_ITEM_PASSWORD:
1572 print_help_section( p_section, b_color, b_description );
1574 psz_bra = OPTION_VALUE_SEP "<";
1575 psz_type = _("string");
1578 if( p_item->ppsz_list )
1580 psz_bra = OPTION_VALUE_SEP "{";
1581 psz_type = psz_buffer;
1582 psz_buffer[0] = '\0';
1583 for( i = 0; p_item->ppsz_list[i]; i++ )
1585 if( i ) strcat( psz_buffer, "," );
1586 strcat( psz_buffer, p_item->ppsz_list[i] );
1591 case CONFIG_ITEM_INTEGER:
1592 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1593 print_help_section( p_section, b_color, b_description );
1595 psz_bra = OPTION_VALUE_SEP "<";
1596 psz_type = _("integer");
1599 if( p_item->min.i || p_item->max.i )
1601 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1602 p_item->min.i, p_item->max.i );
1603 psz_type = psz_buffer;
1606 if( p_item->i_list )
1608 psz_bra = OPTION_VALUE_SEP "{";
1609 psz_type = psz_buffer;
1610 psz_buffer[0] = '\0';
1611 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1613 if( i ) strcat( psz_buffer, ", " );
1614 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1616 p_item->ppsz_list_text[i] );
1621 case CONFIG_ITEM_FLOAT:
1622 print_help_section( p_section, b_color, b_description );
1624 psz_bra = OPTION_VALUE_SEP "<";
1625 psz_type = _("float");
1627 if( p_item->min.f || p_item->max.f )
1629 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1630 p_item->min.f, p_item->max.f );
1631 psz_type = psz_buffer;
1634 case CONFIG_ITEM_BOOL:
1635 print_help_section( p_section, b_color, b_description );
1637 psz_bra = ""; psz_type = ""; psz_ket = "";
1638 if( !b_help_module )
1640 psz_suf = p_item->value.i ? _(" (default enabled)") :
1641 _(" (default disabled)");
1651 /* Add short option if any */
1652 if( p_item->i_short )
1654 sprintf( psz_short, "-%c,", p_item->i_short );
1658 strcpy( psz_short, " " );
1661 i = PADDING_SPACES - strlen( p_item->psz_name )
1662 - strlen( psz_bra ) - strlen( psz_type )
1663 - strlen( psz_ket ) - 1;
1665 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1667 psz_prefix = ", --no-";
1668 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1673 psz_spaces[0] = '\n';
1678 psz_spaces[i] = '\0';
1681 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1683 utf8_fprintf( stdout, psz_format_bool, psz_short,
1684 p_item->psz_name, psz_prefix, p_item->psz_name,
1685 psz_bra, psz_type, psz_ket, psz_spaces );
1689 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1690 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1693 psz_spaces[i] = ' ';
1695 /* We wrap the rest of the output */
1696 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1697 b_description_hack = b_description;
1700 psz_text = psz_buffer;
1701 i_cur_width = b_description && !b_description_hack
1702 ? i_width_description
1706 char *psz_parser, *psz_word;
1707 size_t i_end = strlen( psz_text );
1709 /* If the remaining text fits in a line, print it. */
1710 if( i_end <= i_cur_width )
1714 if( !b_description || b_description_hack )
1715 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1717 utf8_fprintf( stdout, "%s\n", psz_text );
1721 utf8_fprintf( stdout, "%s\n", psz_text );
1726 /* Otherwise, eat as many words as possible */
1727 psz_parser = psz_text;
1730 psz_word = psz_parser;
1731 psz_parser = strchr( psz_word, ' ' );
1732 /* If no space was found, we reached the end of the text
1733 * block; otherwise, we skip the space we just found. */
1734 psz_parser = psz_parser ? psz_parser + 1
1737 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1739 /* We cut a word in one of these cases:
1740 * - it's the only word in the line and it's too long.
1741 * - we used less than 80% of the width and the word we are
1742 * going to wrap is longer than 40% of the width, and even
1743 * if the word would have fit in the next line. */
1744 if( psz_word == psz_text
1745 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1746 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1748 char c = psz_text[i_cur_width];
1749 psz_text[i_cur_width] = '\0';
1752 if( !b_description || b_description_hack )
1753 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1754 psz_text, psz_spaces );
1756 utf8_fprintf( stdout, "%s\n%s",
1757 psz_text, psz_spaces );
1761 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1763 psz_text += i_cur_width;
1768 psz_word[-1] = '\0';
1771 if( !b_description || b_description_hack )
1772 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1773 psz_text, psz_spaces );
1775 utf8_fprintf( stdout, "%s\n%s",
1776 psz_text, psz_spaces );
1780 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1782 psz_text = psz_word;
1786 if( b_description_hack && p_item->psz_longtext )
1788 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1789 b_description_hack = false;
1790 psz_spaces = psz_spaces_longtext;
1791 utf8_fprintf( stdout, "%s", psz_spaces );
1797 if( b_has_advanced )
1800 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1801 _( "add --advanced to your command line to see advanced options."));
1803 utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
1804 _( "add --advanced to your command line to see advanced options."));
1807 /* Release the module list */
1808 vlc_list_release( p_list );
1811 /*****************************************************************************
1812 * ListModules: list the available modules with their description
1813 *****************************************************************************
1814 * Print a list of all available modules (builtins and plugins) and a short
1815 * description for each one.
1816 *****************************************************************************/
1817 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1819 vlc_list_t *p_list = NULL;
1820 module_t *p_parser = NULL;
1821 char psz_spaces[22];
1824 bool b_color = config_GetInt( p_this, "color" ) > 0;
1826 memset( psz_spaces, ' ', 22 );
1829 ShowConsole( true );
1832 /* List all modules */
1833 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1835 /* Enumerate each module */
1836 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1840 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1842 /* Nasty hack, but right now I'm too tired to think about a nice
1844 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1849 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1850 p_parser->psz_object_name,
1852 p_parser->psz_longname );
1854 utf8_fprintf( stdout, " %s%s %s\n",
1855 p_parser->psz_object_name,
1856 psz_spaces, p_parser->psz_longname );
1860 char *const *pp_shortcut = p_parser->pp_shortcuts;
1861 while( *pp_shortcut )
1863 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1866 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1869 utf8_fprintf( stdout, " s %s\n",
1874 if( p_parser->psz_capability )
1877 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1878 p_parser->psz_capability,
1879 p_parser->i_score );
1881 utf8_fprintf( stdout, " c %s (%d)\n",
1882 p_parser->psz_capability,
1883 p_parser->i_score );
1887 psz_spaces[i] = ' ';
1890 vlc_list_release( p_list );
1892 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1897 /*****************************************************************************
1898 * Version: print complete program version
1899 *****************************************************************************
1900 * Print complete program version and build number.
1901 *****************************************************************************/
1902 static void Version( void )
1905 ShowConsole( true );
1908 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1909 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1910 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1911 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1912 if( strcmp( VLC_Changeset(), "exported" ) )
1913 utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1915 utf8_fprintf( stdout, LICENSE_MSG );
1917 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1922 /*****************************************************************************
1923 * ShowConsole: On Win32, create an output console for debug messages
1924 *****************************************************************************
1925 * This function is useful only on Win32.
1926 *****************************************************************************/
1928 static void ShowConsole( bool b_dofile )
1931 FILE *f_help = NULL;
1933 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1936 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1937 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1938 * page (e.g. CP437 or CP850). */
1939 SetConsoleOutputCP (GetACP ());
1940 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1942 freopen( "CONOUT$", "w", stderr );
1943 freopen( "CONIN$", "r", stdin );
1945 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1948 freopen( "vlc-help.txt", "wt", stdout );
1949 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1951 else freopen( "CONOUT$", "w", stdout );
1957 /*****************************************************************************
1958 * PauseConsole: On Win32, wait for a key press before closing the console
1959 *****************************************************************************
1960 * This function is useful only on Win32.
1961 *****************************************************************************/
1963 static void PauseConsole( void )
1967 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1969 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1977 /*****************************************************************************
1978 * ConsoleWidth: Return the console width in characters
1979 *****************************************************************************
1980 * We use the stty shell command to get the console width; if this fails or
1981 * if the width is less than 80, we default to 80.
1982 *****************************************************************************/
1983 static int ConsoleWidth( void )
1985 unsigned i_width = 80;
1988 FILE *file = popen( "stty size 2>/dev/null", "r" );
1991 if (fscanf (file, "%*u %u", &i_width) <= 0)
1996 CONSOLE_SCREEN_BUFFER_INFO buf;
1998 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
1999 i_width = buf.dwSize.X;
2005 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2006 vlc_value_t old_val, vlc_value_t new_val, void *param)
2008 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
2013 if( new_val.i_int >= -1 )
2015 libvlc_priv (p_libvlc)->i_verbose = __MIN( new_val.i_int, 2 );
2020 /*****************************************************************************
2021 * InitDeviceValues: initialize device values
2022 *****************************************************************************
2023 * This function inits the dvd, vcd and cd-audio values
2024 *****************************************************************************/
2025 static void InitDeviceValues( libvlc_int_t *p_vlc )
2028 LibHalContext * ctx = NULL;
2030 char **devices = NULL;
2031 char *block_dev = NULL;
2034 DBusConnection *p_connection = NULL;
2037 ctx = libhal_ctx_new();
2039 dbus_error_init( &error );
2040 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2041 if( dbus_error_is_set( &error ) || !p_connection )
2043 libhal_ctx_free( ctx );
2044 dbus_error_free( &error );
2047 libhal_ctx_set_dbus_connection( ctx, p_connection );
2048 if( libhal_ctx_init( ctx, &error ) )
2050 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2052 for( i = 0; i < i_devices; i++ )
2054 if( !libhal_device_property_exists( ctx, devices[i],
2055 "storage.cdrom.dvd", NULL ) )
2059 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2060 "storage.cdrom.dvd", NULL );
2061 block_dev = libhal_device_get_property_string( ctx,
2062 devices[ i ], "block.device" , NULL );
2065 config_PutPsz( p_vlc, "dvd", block_dev );
2068 config_PutPsz( p_vlc, "vcd", block_dev );
2069 config_PutPsz( p_vlc, "cd-audio", block_dev );
2070 libhal_free_string( block_dev );
2072 libhal_free_string_array( devices );
2074 libhal_ctx_shutdown( ctx, NULL );
2075 dbus_connection_unref( p_connection );
2076 libhal_ctx_free( ctx );
2080 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2084 #endif /* HAVE_HAL */