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_search );
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 /* Now that the thread system is initialized, we don't have much, but
186 * at least we have variables */
187 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
188 if( i_instances == 0 )
190 /* Guess what CPU we have */
191 cpu_flags = CPUCapabilities();
192 /* The module bank will be initialized later */
193 p_module_bank = NULL;
196 /* Allocate a libvlc instance object */
197 p_libvlc = __vlc_custom_create( NULL, sizeof (*priv),
198 VLC_OBJECT_LIBVLC, "libvlc" );
199 if( p_libvlc != NULL )
201 vlc_mutex_unlock( lock );
203 if( p_libvlc == NULL )
206 priv = libvlc_priv (p_libvlc);
207 priv->p_playlist = NULL;
208 priv->p_interaction = NULL;
210 p_libvlc->psz_object_name = strdup( "libvlc" );
212 /* Initialize message queue */
213 msg_Create( p_libvlc );
215 /* Find verbosity from VLC_VERBOSE environment variable */
216 psz_env = getenv( "VLC_VERBOSE" );
217 if( psz_env != NULL )
218 priv->i_verbose = atoi( psz_env );
221 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
222 priv->b_color = isatty( 2 ); /* 2 is for stderr */
224 priv->b_color = false;
227 /* Announce who we are - Do it only for first instance ? */
228 msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
229 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
231 /* Initialize mutexes */
232 vlc_mutex_init( &priv->timer_lock );
233 vlc_mutex_init( &priv->config_lock );
235 /* Store data for the non-reentrant API */
236 p_static_vlc = p_libvlc;
242 * Initialize a libvlc instance
243 * This function initializes a previously allocated libvlc instance:
245 * - gettext initialization
246 * - message queue, module bank and playlist initialization
247 * - configuration and commandline parsing
249 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
250 const char *ppsz_argv[] )
252 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
253 char p_capabilities[200];
255 char * psz_modules = NULL;
256 char * psz_parser = NULL;
257 char * psz_control = NULL;
259 int i_ret = VLC_EEXIT;
260 playlist_t *p_playlist = NULL;
262 #if defined( ENABLE_NLS ) \
263 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
264 # if defined (WIN32) || defined (__APPLE__)
269 /* System specific initialization code */
270 system_Init( p_libvlc, &i_argc, ppsz_argv );
273 * Support for gettext
277 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
278 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
280 /* Initialize the module bank and load the configuration of the
281 * main module. We need to do this at this stage to be able to display
282 * a short help if required by the user. (short help == main module
284 module_InitBank( p_libvlc );
286 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
288 module_EndBank( p_libvlc );
292 /* Check for short help option */
293 if( config_GetInt( p_libvlc, "help" ) > 0 )
295 Help( p_libvlc, "help" );
297 i_ret = VLC_EEXITSUCCESS;
299 /* Check for version option */
300 else if( config_GetInt( p_libvlc, "version" ) > 0 )
304 i_ret = VLC_EEXITSUCCESS;
307 /* Set the config file stuff */
308 priv->psz_configfile = config_GetCustomConfigFile( p_libvlc );
310 /* Check for plugins cache options */
311 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
313 p_module_bank->b_cache_delete = true;
316 /* Will be re-done properly later on */
317 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
319 /* Check for daemon mode */
321 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
324 char *psz_pidfile = NULL;
326 if( daemon( 1, 0) != 0 )
328 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
333 /* lets check if we need to write the pidfile */
334 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
335 if( psz_pidfile != NULL )
338 pid_t i_pid = getpid ();
339 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
340 i_pid, psz_pidfile );
341 pidfile = utf8_fopen( psz_pidfile,"w" );
342 if( pidfile != NULL )
344 utf8_fprintf( pidfile, "%d", (int)i_pid );
349 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
358 if( ( i_pid = fork() ) < 0 )
360 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
365 /* This is the parent, exit right now */
366 msg_Dbg( p_libvlc, "closing parent process" );
368 i_ret = VLC_EEXITSUCCESS;
372 /* We are the child */
373 msg_Dbg( p_libvlc, "daemon spawned" );
374 close( STDIN_FILENO );
375 close( STDOUT_FILENO );
376 close( STDERR_FILENO );
386 module_EndBank( p_libvlc );
390 /* Check for translation config option */
391 #if defined( ENABLE_NLS ) \
392 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
393 # if defined (WIN32) || defined (__APPLE__)
394 /* This ain't really nice to have to reload the config here but it seems
395 * the only way to do it. */
397 if( !config_GetInt( p_libvlc, "ignore-config" ) )
398 config_LoadConfigFile( p_libvlc, "main" );
399 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
401 /* Check if the user specified a custom language */
402 psz_language = config_GetPsz( p_libvlc, "language" );
403 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
405 bool b_cache_delete = p_module_bank->b_cache_delete;
407 /* Reset the default domain */
408 SetLanguage( psz_language );
410 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
411 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
413 module_EndBank( p_libvlc );
414 module_InitBank( p_libvlc );
415 if( !config_GetInt( p_libvlc, "ignore-config" ) )
416 config_LoadConfigFile( p_libvlc, "main" );
417 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
418 p_module_bank->b_cache_delete = b_cache_delete;
420 free( psz_language );
425 * Load the builtins and plugins into the module_bank.
426 * We have to do it before config_Load*() because this also gets the
427 * list of configuration options exported by each module and loads their
430 module_LoadBuiltins( p_libvlc );
431 module_LoadPlugins( p_libvlc );
432 if( p_libvlc->b_die )
437 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
438 vlc_internals( p_module_bank )->i_children );
440 /* Check for help on modules */
441 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
443 Help( p_libvlc, p_tmp );
446 i_ret = VLC_EEXITSUCCESS;
448 /* Check for full help option */
449 else if( config_GetInt( p_libvlc, "full-help" ) > 0 )
451 config_PutInt( p_libvlc, "advanced", 1);
452 config_PutInt( p_libvlc, "help-verbose", 1);
453 Help( p_libvlc, "full-help" );
455 i_ret = VLC_EEXITSUCCESS;
457 /* Check for long help option */
458 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
460 Help( p_libvlc, "longhelp" );
462 i_ret = VLC_EEXITSUCCESS;
464 /* Check for module list option */
465 else if( config_GetInt( p_libvlc, "list" ) > 0 )
467 ListModules( p_libvlc, false );
469 i_ret = VLC_EEXITSUCCESS;
471 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
473 ListModules( p_libvlc, true );
475 i_ret = VLC_EEXITSUCCESS;
478 /* Check for config file options */
479 if( !config_GetInt( p_libvlc, "ignore-config" ) )
481 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
483 config_ResetAll( p_libvlc );
484 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
485 config_SaveConfigFile( p_libvlc, NULL );
487 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
489 config_LoadConfigFile( p_libvlc, NULL );
490 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
491 config_SaveConfigFile( p_libvlc, NULL );
497 module_EndBank( p_libvlc );
504 InitDeviceValues( p_libvlc );
507 * Override default configuration with config file settings
509 if( !config_GetInt( p_libvlc, "ignore-config" ) )
510 config_LoadConfigFile( p_libvlc, NULL );
513 * Override configuration with command line settings
515 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
518 ShowConsole( false );
519 /* Pause the console because it's destroyed when we exit */
520 fprintf( stderr, "The command line options couldn't be loaded, check "
521 "that they are valid.\n" );
524 module_EndBank( p_libvlc );
529 * System specific configuration
531 system_Configure( p_libvlc, &i_argc, ppsz_argv );
533 /* FIXME: could be replaced by using Unix sockets */
535 dbus_threads_init_default();
537 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
539 /* Initialise D-Bus interface, check for other instances */
540 DBusConnection *p_conn = NULL;
541 DBusError dbus_error;
543 dbus_error_init( &dbus_error );
545 /* connect to the session bus */
546 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
549 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
550 dbus_error.message );
551 dbus_error_free( &dbus_error );
555 /* check if VLC is available on the bus
556 * if not: D-Bus control is not enabled on the other
557 * instance and we can't pass MRLs to it */
558 DBusMessage *p_test_msg = NULL;
559 DBusMessage *p_test_reply = NULL;
560 p_test_msg = dbus_message_new_method_call(
561 "org.mpris.vlc", "/",
562 "org.freedesktop.MediaPlayer", "Identity" );
563 /* block until a reply arrives */
564 p_test_reply = dbus_connection_send_with_reply_and_block(
565 p_conn, p_test_msg, -1, &dbus_error );
566 dbus_message_unref( p_test_msg );
567 if( p_test_reply == NULL )
569 dbus_error_free( &dbus_error );
570 msg_Dbg( p_libvlc, "No Media Player is running. "
571 "Continuing normally." );
576 DBusMessage* p_dbus_msg = NULL;
577 DBusMessageIter dbus_args;
578 DBusPendingCall* p_dbus_pending = NULL;
581 dbus_message_unref( p_test_reply );
582 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
584 for( i_input = optind;i_input < i_argc;i_input++ )
586 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
587 ppsz_argv[i_input] );
589 p_dbus_msg = dbus_message_new_method_call(
590 "org.mpris.vlc", "/TrackList",
591 "org.freedesktop.MediaPlayer", "AddTrack" );
593 if ( NULL == p_dbus_msg )
595 msg_Err( p_libvlc, "D-Bus problem" );
596 system_End( p_libvlc );
597 exit( VLC_ETIMEOUT );
601 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
602 if ( !dbus_message_iter_append_basic( &dbus_args,
603 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
605 dbus_message_unref( p_dbus_msg );
606 system_End( p_libvlc );
610 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
612 if ( !dbus_message_iter_append_basic( &dbus_args,
613 DBUS_TYPE_BOOLEAN, &b_play ) )
615 dbus_message_unref( p_dbus_msg );
616 system_End( p_libvlc );
620 /* send message and get a handle for a reply */
621 if ( !dbus_connection_send_with_reply ( p_conn,
622 p_dbus_msg, &p_dbus_pending, -1 ) )
624 msg_Err( p_libvlc, "D-Bus problem" );
625 dbus_message_unref( p_dbus_msg );
626 system_End( p_libvlc );
627 exit( VLC_ETIMEOUT );
630 if ( NULL == p_dbus_pending )
632 msg_Err( p_libvlc, "D-Bus problem" );
633 dbus_message_unref( p_dbus_msg );
634 system_End( p_libvlc );
635 exit( VLC_ETIMEOUT );
637 dbus_connection_flush( p_conn );
638 dbus_message_unref( p_dbus_msg );
639 /* block until we receive a reply */
640 dbus_pending_call_block( p_dbus_pending );
641 dbus_pending_call_unref( p_dbus_pending );
642 } /* processes all command line MRLs */
645 system_End( p_libvlc );
649 /* we unreference the connection when we've finished with it */
650 if( p_conn ) dbus_connection_unref( p_conn );
655 * Message queue options
657 char * psz_verbose_objects = config_GetPsz( p_libvlc, "verbose-objects" );
658 if( psz_verbose_objects )
660 char * psz_object, * iter = psz_verbose_objects;
661 while( (psz_object = strsep( &iter, "," )) )
663 switch( psz_object[0] )
665 printf("%s\n", psz_object+1);
666 case '+': msg_EnableObjectPrinting(p_libvlc, psz_object+1); break;
667 case '-': msg_DisableObjectPrinting(p_libvlc, psz_object+1); break;
669 msg_Err( p_libvlc, "verbose-objects usage: \n"
670 "--verbose-objects=+printthatobject,"
671 "-dontprintthatone\n"
672 "(keyword 'all' to applies to all objects)\n");
673 free( psz_verbose_objects );
677 free( psz_verbose_objects );
680 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
681 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
684 var_Set( p_libvlc, "verbose", val );
686 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
687 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
690 priv->b_color = config_GetInt( p_libvlc, "color" ) > 0;
693 * Output messages that may still be in the queue
695 msg_Flush( p_libvlc );
697 if( !config_GetInt( p_libvlc, "fpu" ) )
698 cpu_flags &= ~CPU_CAPABILITY_FPU;
700 #if defined( __i386__ ) || defined( __x86_64__ )
701 if( !config_GetInt( p_libvlc, "mmx" ) )
702 cpu_flags &= ~CPU_CAPABILITY_MMX;
703 if( !config_GetInt( p_libvlc, "3dn" ) )
704 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
705 if( !config_GetInt( p_libvlc, "mmxext" ) )
706 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
707 if( !config_GetInt( p_libvlc, "sse" ) )
708 cpu_flags &= ~CPU_CAPABILITY_SSE;
709 if( !config_GetInt( p_libvlc, "sse2" ) )
710 cpu_flags &= ~CPU_CAPABILITY_SSE2;
712 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
713 if( !config_GetInt( p_libvlc, "altivec" ) )
714 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
717 #define PRINT_CAPABILITY( capability, string ) \
718 if( vlc_CPU() & capability ) \
720 strncat( p_capabilities, string " ", \
721 sizeof(p_capabilities) - strlen(p_capabilities) ); \
722 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
725 p_capabilities[0] = '\0';
726 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
727 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
728 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
729 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
730 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
731 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
732 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
733 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
734 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
735 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
736 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
739 * Choose the best memcpy module
741 priv->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
743 priv->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
745 priv->pp_timers = NULL;
748 p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
749 if( !p_libvlc->p_stats )
751 vlc_object_release( p_libvlc );
754 vlc_mutex_init( &p_libvlc->p_stats->lock );
755 priv->p_stats_computer = NULL;
757 /* Init the array that holds every input item */
758 ARRAY_INIT( priv->input_items );
759 priv->i_last_input_id = 0;
762 * Initialize hotkey handling
764 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
765 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
766 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
767 /* Do a copy (we don't need to modify the strings) */
768 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
769 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
770 p_libvlc->p_hotkeys );
772 /* Initialize interaction */
773 priv->p_interaction = interaction_Init( p_libvlc );
775 /* Initialize playlist and get commandline files */
776 playlist_ThreadCreate( p_libvlc );
777 if( !priv->p_playlist )
779 msg_Err( p_libvlc, "playlist initialization failed" );
780 if( priv->p_memcpy_module != NULL )
782 module_Unneed( p_libvlc, priv->p_memcpy_module );
784 module_EndBank( p_libvlc );
787 p_playlist = priv->p_playlist;
789 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
790 if( psz_modules && *psz_modules )
792 /* Add service discovery modules */
793 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
798 /* Initialize VLM if vlm-conf is specified */
799 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
800 if( psz_parser && *psz_parser )
802 priv->p_vlm = vlm_New( p_libvlc );
804 msg_Err( p_libvlc, "VLM initialization failed" );
810 * Load background interfaces
812 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
813 psz_control = config_GetPsz( p_libvlc, "control" );
815 if( psz_modules && *psz_modules && psz_control && *psz_control )
817 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
818 strlen( psz_control ) + 1 );
819 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
821 else if( psz_control && *psz_control )
824 psz_modules = strdup( psz_control );
827 psz_parser = psz_modules;
828 while ( psz_parser && *psz_parser )
830 char *psz_module, *psz_temp;
831 psz_module = psz_parser;
832 psz_parser = strchr( psz_module, ':' );
838 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
841 sprintf( psz_temp, "%s,none", psz_module );
842 libvlc_InternalAddIntf( p_libvlc, psz_temp );
850 * Always load the hotkeys interface if it exists
852 libvlc_InternalAddIntf( p_libvlc, "hotkeys,none" );
855 /* loads dbus control interface if in one-instance mode
856 * we do it only when playlist exists, because dbus module needs it */
857 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
858 libvlc_InternalAddIntf( p_libvlc, "dbus,none" );
860 /* Prevents the power management daemon from suspending the system
861 * when VLC is active */
862 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
863 libvlc_InternalAddIntf( p_libvlc, "inhibit,none" );
867 * If needed, load the Xscreensaver interface
868 * Currently, only for X
870 #ifdef HAVE_X11_XLIB_H
871 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
873 libvlc_InternalAddIntf( p_libvlc, "screensaver,none" );
877 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
879 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
882 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
884 char *logmode = var_CreateGetString( p_libvlc, "logmode" );
885 var_SetString( p_libvlc, "logmode", "syslog" );
886 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
890 var_SetString( p_libvlc, "logmode", logmode );
894 var_Destroy( p_libvlc, "logmode" );
898 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
900 libvlc_InternalAddIntf( p_libvlc, "showintf,none" );
903 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
905 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
909 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
911 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
914 char *psz_morecodecs;
915 if( asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs) != -1 )
917 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
918 free( psz_morecodecs );
922 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
928 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
930 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
931 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
932 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
933 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
934 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
935 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
936 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
937 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
938 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
940 /* Create volume callback system. */
941 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
943 /* Create a variable for showing the interface (moved from playlist). */
944 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
945 var_SetBool( p_libvlc, "intf-show", true );
947 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
950 * Get input filenames given as commandline arguments
952 GetFilenames( p_libvlc, i_argc, ppsz_argv );
955 * Get --open argument
957 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
958 var_Get( p_libvlc, "open", &val );
959 if ( val.psz_string != NULL && *val.psz_string )
961 playlist_t *p_playlist = pl_Yield( p_libvlc );
962 playlist_AddExt( p_playlist, val.psz_string, NULL, PLAYLIST_INSERT, 0,
963 -1, NULL, 0, true, pl_Unlocked );
964 pl_Release( p_libvlc );
966 free( val.psz_string );
972 * Cleanup a libvlc instance. The instance is not completely deallocated
973 * \param p_libvlc the instance to clean
975 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
977 intf_thread_t * p_intf = NULL;
978 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
980 /* Ask the interfaces to stop and destroy them */
981 msg_Dbg( p_libvlc, "removing all interfaces" );
982 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
984 intf_StopThread( p_intf );
985 vlc_object_detach( p_intf );
986 vlc_object_release( p_intf ); /* for intf_Create() */
987 vlc_object_release( p_intf ); /* for vlc_object_find() */
991 /* Destroy VLM if created in libvlc_InternalInit */
994 vlm_Delete( priv->p_vlm );
998 playlist_t *p_playlist = priv->p_playlist;
999 /* Remove all services discovery */
1000 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
1001 playlist_ServicesDiscoveryKillAll( p_playlist );
1004 /* Any thread still running must not assume pl_Yield() succeeds. */
1005 msg_Dbg( p_libvlc, "removing playlist" );
1006 priv->p_playlist = NULL;
1007 vlc_object_kill( p_playlist ); /* <-- memory barrier for pl_Yield() */
1008 vlc_thread_join( p_playlist );
1009 vlc_object_release( p_playlist );
1011 /* Free interaction */
1012 msg_Dbg( p_libvlc, "removing interaction" );
1013 interaction_Destroy( priv->p_interaction );
1015 /* Free video outputs */
1016 msg_Dbg( p_libvlc, "removing all video outputs" );
1017 vlc_list_t *list = vlc_list_find (p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD);
1018 for (int i = 0; i < list->i_count; i++)
1019 vlc_object_release (list->p_values[i].p_object);
1020 vlc_list_release (list);
1022 stats_TimersDumpAll( p_libvlc );
1023 stats_TimersCleanAll( p_libvlc );
1026 announce_handler_t * p_announce;
1028 /* Free announce handler(s?) */
1029 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1032 msg_Dbg( p_libvlc, "removing announce handler" );
1033 vlc_object_detach( p_announce );
1034 vlc_object_release( p_announce );
1035 announce_HandlerDestroy( p_announce );
1039 bool b_clean = true;
1040 FOREACH_ARRAY( input_item_t *p_del, priv->input_items )
1041 msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1042 p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1046 ARRAY_RESET( priv->input_items );
1048 msg_Dbg( p_libvlc, "removing stats" );
1049 vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1050 FREENULL( p_libvlc->p_stats );
1056 * Destroy everything.
1057 * This function requests the running threads to finish, waits for their
1058 * termination, and destroys their structure.
1059 * It stops the thread systems: no instance can run after this has run
1060 * \param p_libvlc the instance to destroy
1062 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
1065 return VLC_EGENERIC;
1067 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1070 char* psz_pidfile = NULL;
1074 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1075 if( psz_pidfile != NULL )
1077 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1078 if( unlink( psz_pidfile ) == -1 )
1080 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1084 free( psz_pidfile );
1088 if( priv->p_memcpy_module )
1090 module_Unneed( p_libvlc, priv->p_memcpy_module );
1091 priv->p_memcpy_module = NULL;
1094 /* Free module bank. It is refcounted, so we call this each time */
1095 module_EndBank( p_libvlc );
1097 FREENULL( priv->psz_configfile );
1098 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1099 p_libvlc->p_hotkeys );
1100 FREENULL( p_libvlc->p_hotkeys );
1102 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1105 if( i_instances == 0 )
1107 /* System specific cleaning code */
1108 system_End( p_libvlc );
1110 vlc_mutex_unlock( lock );
1112 msg_Flush( p_libvlc );
1113 msg_Destroy( p_libvlc );
1115 /* Destroy mutexes */
1116 vlc_mutex_destroy( &priv->config_lock );
1117 vlc_mutex_destroy( &priv->timer_lock );
1119 vlc_object_release( p_libvlc );
1122 /* Stop thread system: last one out please shut the door!
1123 * The number of initializations of the thread system is counted, we
1124 * can call this each time */
1131 * Add an interface plugin and run it
1133 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
1136 intf_thread_t *p_intf = NULL;
1139 return VLC_EGENERIC;
1141 if( !psz_module ) /* requesting the default interface */
1143 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1144 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1148 /* Daemon mode hack.
1149 * We prefer the dummy interface if none is specified. */
1150 psz_module = "dummy";
1153 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1155 free( psz_interface );
1158 /* Try to create the interface */
1159 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
1160 if( p_intf == NULL )
1162 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1164 return VLC_EGENERIC;
1167 /* Try to run the interface */
1168 i_err = intf_RunThread( p_intf );
1171 vlc_object_detach( p_intf );
1172 vlc_object_release( p_intf );
1179 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1180 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1181 /*****************************************************************************
1182 * SetLanguage: set the interface language.
1183 *****************************************************************************
1184 * We set the LC_MESSAGES locale category for interface messages and buttons,
1185 * as well as the LC_CTYPE category for string sorting and possible wide
1186 * character support.
1187 *****************************************************************************/
1188 static void SetLanguage ( const char *psz_lang )
1191 /* I need that under Darwin, please check it doesn't disturb
1192 * other platforms. --Meuuh */
1193 setenv( "LANG", psz_lang, 1 );
1196 /* We set LC_ALL manually because it is the only way to set
1197 * the language at runtime under eg. Windows. Beware that this
1198 * makes the environment unconsistent when libvlc is unloaded and
1199 * should probably be moved to a safer place like vlc.c. */
1200 static char psz_lcall[20];
1201 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1202 psz_lcall[19] = '\0';
1203 putenv( psz_lcall );
1206 setlocale( LC_ALL, psz_lang );
1211 static inline int LoadMessages (void)
1213 #if defined( ENABLE_NLS ) \
1214 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1215 /* Specify where to find the locales for current domain */
1216 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1217 static const char psz_path[] = LOCALEDIR;
1219 char psz_path[1024];
1220 if (snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "%s",
1221 config_GetDataDir(), "locale")
1222 >= (int)sizeof (psz_path))
1226 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1228 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1229 " to directory %s\n", psz_path);
1233 /* LibVLC wants all messages in UTF-8.
1234 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1235 * and other functions that are not part of our text domain.
1237 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1239 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1241 // Unbinds the text domain to avoid broken encoding
1242 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1246 /* LibVLC does NOT set the default textdomain, since it is a library.
1247 * This could otherwise break programs using LibVLC (other than VLC).
1248 * textdomain (PACKAGE_NAME);
1254 /*****************************************************************************
1255 * GetFilenames: parse command line options which are not flags
1256 *****************************************************************************
1257 * Parse command line for input files as well as their associated options.
1258 * An option always follows its associated input and begins with a ":".
1259 *****************************************************************************/
1260 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1262 int i_opt, i_options;
1264 /* We assume that the remaining parameters are filenames
1265 * and their input options */
1266 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1270 /* Count the input options */
1271 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1277 /* TODO: write an internal function of this one, to avoid
1278 * unnecessary lookups. */
1280 playlist_t *p_playlist = pl_Yield( p_vlc );
1281 playlist_AddExt( p_playlist, ppsz_argv[i_opt], NULL, PLAYLIST_INSERT,
1282 0, -1, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ),
1283 i_options, true, pl_Unlocked );
1284 pl_Release( p_vlc );
1290 /*****************************************************************************
1291 * Help: print program help
1292 *****************************************************************************
1293 * Print a short inline help. Message interface is initialized at this stage.
1294 *****************************************************************************/
1295 static inline void print_help_on_full_help( void )
1297 utf8_fprintf( stdout, "\n" );
1298 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
1301 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1304 ShowConsole( true );
1307 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1309 utf8_fprintf( stdout, vlc_usage, "vlc" );
1310 Usage( p_this, "=help" );
1311 Usage( p_this, "=main" );
1312 print_help_on_full_help();
1314 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1316 utf8_fprintf( stdout, vlc_usage, "vlc" );
1317 Usage( p_this, NULL );
1318 print_help_on_full_help();
1320 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
1322 utf8_fprintf( stdout, vlc_usage, "vlc" );
1323 Usage( p_this, NULL );
1325 else if( psz_help_name )
1327 Usage( p_this, psz_help_name );
1330 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1335 /*****************************************************************************
1336 * Usage: print module usage
1337 *****************************************************************************
1338 * Print a short inline help. Message interface is initialized at this stage.
1339 *****************************************************************************/
1340 # define COL(x) "\033[" #x ";1m"
1341 # define RED COL(31)
1342 # define GREEN COL(32)
1343 # define YELLOW COL(33)
1344 # define BLUE COL(34)
1345 # define MAGENTA COL(35)
1346 # define CYAN COL(36)
1347 # define WHITE COL(0)
1348 # define GRAY "\033[0m"
1349 static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
1351 if( !p_item ) return;
1354 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1356 if( b_description && p_item->psz_longtext )
1357 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1358 p_item->psz_longtext );
1362 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1363 if( b_description && p_item->psz_longtext )
1364 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1368 static void Usage( libvlc_int_t *p_this, char const *psz_search )
1370 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1371 /* short option ------' | | | | | | |
1372 * option name ------------' | | | | | |
1373 * <bra ---------------------' | | | | |
1374 * option type or "" ----------' | | | |
1375 * ket> -------------------------' | | |
1376 * padding spaces -----------------' | |
1377 * comment --------------------------' |
1378 * comment suffix ---------------------'
1380 * The purpose of having bra and ket is that we might i18n them as well.
1383 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1384 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1386 #define LINE_START 8
1387 #define PADDING_SPACES 25
1389 # define OPTION_VALUE_SEP "="
1391 # define OPTION_VALUE_SEP " "
1393 vlc_list_t *p_list = NULL;
1394 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1395 char psz_spaces_longtext[LINE_START+3];
1396 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1397 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1398 char psz_buffer[10000];
1401 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1402 int i_width_description = i_width + PADDING_SPACES - 1;
1403 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1404 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1405 bool b_description_hack;
1406 bool b_color = config_GetInt( p_this, "color" ) > 0;
1407 bool b_has_advanced = false;
1408 bool b_found = false;
1409 int i_only_advanced = 0; /* Number of modules ignored because they
1410 * only have advanced options */
1411 bool b_strict = psz_search && *psz_search == '=';
1412 if( b_strict ) psz_search++;
1414 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1415 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1416 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1417 psz_spaces_longtext[LINE_START+2] = '\0';
1421 b_color = false; // don't put color control codes in a .txt file
1425 strcpy( psz_format, COLOR_FORMAT_STRING );
1426 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1430 strcpy( psz_format, FORMAT_STRING );
1431 strcpy( psz_format_bool, FORMAT_STRING );
1434 /* List all modules */
1435 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1437 /* Ugly hack to make sure that the help options always come first
1440 Usage( p_this, "help" );
1442 /* Enumerate the config for each module */
1443 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1446 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1447 module_config_t *p_item = NULL;
1448 module_config_t *p_section = NULL;
1449 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1452 ( b_strict ? strcmp( psz_search, p_parser->psz_object_name )
1453 : !strstr( p_parser->psz_object_name, psz_search ) ) )
1455 char *const *pp_shortcut = p_parser->pp_shortcuts;
1456 while( *pp_shortcut )
1458 if( b_strict ? !strcmp( psz_search, *pp_shortcut )
1459 : !!strstr( *pp_shortcut, psz_search ) )
1467 /* Ignore modules without config options */
1468 if( !p_parser->i_config_items )
1473 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1474 /* Ugly hack to make sure that the help options always come first
1476 if( !psz_search && b_help_module )
1479 /* Ignore modules with only advanced config options if requested */
1482 for( p_item = p_parser->p_config;
1486 if( (p_item->i_type & CONFIG_ITEM) &&
1487 !p_item->b_advanced && !p_item->b_removed ) break;
1490 if( p_item == p_end )
1499 /* Print name of module */
1500 if( strcmp( "main", p_parser->psz_object_name ) )
1503 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
1504 p_parser->psz_longname,
1505 p_parser->psz_object_name );
1507 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1509 if( p_parser->psz_help )
1512 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1514 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1517 /* Print module options */
1518 for( p_item = p_parser->p_config;
1522 char *psz_text, *psz_spaces = psz_spaces_text;
1523 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1524 const char *psz_suf = "", *psz_prefix = NULL;
1528 /* Skip removed options */
1529 if( p_item->b_removed )
1533 /* Skip advanced options if requested */
1534 if( p_item->b_advanced && !b_advanced )
1536 b_has_advanced = true;
1540 switch( p_item->i_type )
1542 case CONFIG_HINT_CATEGORY:
1543 case CONFIG_HINT_USAGE:
1544 if( !strcmp( "main", p_parser->psz_object_name ) )
1547 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1550 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1552 if( b_description && p_item->psz_longtext )
1555 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1556 p_item->psz_longtext );
1558 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1562 case CONFIG_HINT_SUBCATEGORY:
1563 if( strcmp( "main", p_parser->psz_object_name ) )
1565 case CONFIG_SECTION:
1569 case CONFIG_ITEM_STRING:
1570 case CONFIG_ITEM_FILE:
1571 case CONFIG_ITEM_DIRECTORY:
1572 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1573 case CONFIG_ITEM_MODULE_CAT:
1574 case CONFIG_ITEM_MODULE_LIST:
1575 case CONFIG_ITEM_MODULE_LIST_CAT:
1576 case CONFIG_ITEM_FONT:
1577 case CONFIG_ITEM_PASSWORD:
1578 print_help_section( p_section, b_color, b_description );
1580 psz_bra = OPTION_VALUE_SEP "<";
1581 psz_type = _("string");
1584 if( p_item->ppsz_list )
1586 psz_bra = OPTION_VALUE_SEP "{";
1587 psz_type = psz_buffer;
1588 psz_buffer[0] = '\0';
1589 for( i = 0; p_item->ppsz_list[i]; i++ )
1591 if( i ) strcat( psz_buffer, "," );
1592 strcat( psz_buffer, p_item->ppsz_list[i] );
1597 case CONFIG_ITEM_INTEGER:
1598 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1599 print_help_section( p_section, b_color, b_description );
1601 psz_bra = OPTION_VALUE_SEP "<";
1602 psz_type = _("integer");
1605 if( p_item->min.i || p_item->max.i )
1607 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1608 p_item->min.i, p_item->max.i );
1609 psz_type = psz_buffer;
1612 if( p_item->i_list )
1614 psz_bra = OPTION_VALUE_SEP "{";
1615 psz_type = psz_buffer;
1616 psz_buffer[0] = '\0';
1617 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1619 if( i ) strcat( psz_buffer, ", " );
1620 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1622 p_item->ppsz_list_text[i] );
1627 case CONFIG_ITEM_FLOAT:
1628 print_help_section( p_section, b_color, b_description );
1630 psz_bra = OPTION_VALUE_SEP "<";
1631 psz_type = _("float");
1633 if( p_item->min.f || p_item->max.f )
1635 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1636 p_item->min.f, p_item->max.f );
1637 psz_type = psz_buffer;
1640 case CONFIG_ITEM_BOOL:
1641 print_help_section( p_section, b_color, b_description );
1643 psz_bra = ""; psz_type = ""; psz_ket = "";
1644 if( !b_help_module )
1646 psz_suf = p_item->value.i ? _(" (default enabled)") :
1647 _(" (default disabled)");
1657 /* Add short option if any */
1658 if( p_item->i_short )
1660 sprintf( psz_short, "-%c,", p_item->i_short );
1664 strcpy( psz_short, " " );
1667 i = PADDING_SPACES - strlen( p_item->psz_name )
1668 - strlen( psz_bra ) - strlen( psz_type )
1669 - strlen( psz_ket ) - 1;
1671 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1673 psz_prefix = ", --no-";
1674 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1679 psz_spaces[0] = '\n';
1684 psz_spaces[i] = '\0';
1687 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1689 utf8_fprintf( stdout, psz_format_bool, psz_short,
1690 p_item->psz_name, psz_prefix, p_item->psz_name,
1691 psz_bra, psz_type, psz_ket, psz_spaces );
1695 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1696 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1699 psz_spaces[i] = ' ';
1701 /* We wrap the rest of the output */
1702 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1703 b_description_hack = b_description;
1706 psz_text = psz_buffer;
1707 i_cur_width = b_description && !b_description_hack
1708 ? i_width_description
1712 char *psz_parser, *psz_word;
1713 size_t i_end = strlen( psz_text );
1715 /* If the remaining text fits in a line, print it. */
1716 if( i_end <= i_cur_width )
1720 if( !b_description || b_description_hack )
1721 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1723 utf8_fprintf( stdout, "%s\n", psz_text );
1727 utf8_fprintf( stdout, "%s\n", psz_text );
1732 /* Otherwise, eat as many words as possible */
1733 psz_parser = psz_text;
1736 psz_word = psz_parser;
1737 psz_parser = strchr( psz_word, ' ' );
1738 /* If no space was found, we reached the end of the text
1739 * block; otherwise, we skip the space we just found. */
1740 psz_parser = psz_parser ? psz_parser + 1
1743 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1745 /* We cut a word in one of these cases:
1746 * - it's the only word in the line and it's too long.
1747 * - we used less than 80% of the width and the word we are
1748 * going to wrap is longer than 40% of the width, and even
1749 * if the word would have fit in the next line. */
1750 if( psz_word == psz_text
1751 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1752 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1754 char c = psz_text[i_cur_width];
1755 psz_text[i_cur_width] = '\0';
1758 if( !b_description || b_description_hack )
1759 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1760 psz_text, psz_spaces );
1762 utf8_fprintf( stdout, "%s\n%s",
1763 psz_text, psz_spaces );
1767 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1769 psz_text += i_cur_width;
1774 psz_word[-1] = '\0';
1777 if( !b_description || b_description_hack )
1778 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1779 psz_text, psz_spaces );
1781 utf8_fprintf( stdout, "%s\n%s",
1782 psz_text, psz_spaces );
1786 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1788 psz_text = psz_word;
1792 if( b_description_hack && p_item->psz_longtext )
1794 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1795 b_description_hack = false;
1796 psz_spaces = psz_spaces_longtext;
1797 utf8_fprintf( stdout, "%s", psz_spaces );
1803 if( b_has_advanced )
1806 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1807 _( "add --advanced to your command line to see advanced options."));
1809 utf8_fprintf( stdout, "\n%s %s\n", _( "Note:" ),
1810 _( "add --advanced to your command line to see advanced options."));
1813 if( i_only_advanced > 0 )
1817 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " ", _( "Note:" ) );
1818 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1822 utf8_fprintf( stdout, "\n%s ", _( "Note:" ) );
1823 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1829 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY "\n",
1830 _( "No matching module found. Use --list or" \
1831 "--list-verbose to list available modules." ) );
1833 utf8_fprintf( stdout, "\n%s\n",
1834 _( "No matching module found. Use --list or" \
1835 "--list-verbose to list available modules." ) );
1838 /* Release the module list */
1839 vlc_list_release( p_list );
1842 /*****************************************************************************
1843 * ListModules: list the available modules with their description
1844 *****************************************************************************
1845 * Print a list of all available modules (builtins and plugins) and a short
1846 * description for each one.
1847 *****************************************************************************/
1848 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1850 vlc_list_t *p_list = NULL;
1851 module_t *p_parser = NULL;
1852 char psz_spaces[22];
1855 bool b_color = config_GetInt( p_this, "color" ) > 0;
1857 memset( psz_spaces, ' ', 22 );
1860 ShowConsole( true );
1863 /* List all modules */
1864 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1866 /* Enumerate each module */
1867 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1871 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1873 /* Nasty hack, but right now I'm too tired to think about a nice
1875 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1880 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1881 p_parser->psz_object_name,
1883 p_parser->psz_longname );
1885 utf8_fprintf( stdout, " %s%s %s\n",
1886 p_parser->psz_object_name,
1887 psz_spaces, p_parser->psz_longname );
1891 char *const *pp_shortcut = p_parser->pp_shortcuts;
1892 while( *pp_shortcut )
1894 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1897 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1900 utf8_fprintf( stdout, " s %s\n",
1905 if( p_parser->psz_capability )
1908 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1909 p_parser->psz_capability,
1910 p_parser->i_score );
1912 utf8_fprintf( stdout, " c %s (%d)\n",
1913 p_parser->psz_capability,
1914 p_parser->i_score );
1918 psz_spaces[i] = ' ';
1921 vlc_list_release( p_list );
1923 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1928 /*****************************************************************************
1929 * Version: print complete program version
1930 *****************************************************************************
1931 * Print complete program version and build number.
1932 *****************************************************************************/
1933 static void Version( void )
1936 ShowConsole( true );
1939 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1940 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1941 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1942 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1943 if( strcmp( VLC_Changeset(), "exported" ) )
1944 utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1946 utf8_fprintf( stdout, LICENSE_MSG );
1948 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1953 /*****************************************************************************
1954 * ShowConsole: On Win32, create an output console for debug messages
1955 *****************************************************************************
1956 * This function is useful only on Win32.
1957 *****************************************************************************/
1959 static void ShowConsole( bool b_dofile )
1962 FILE *f_help = NULL;
1964 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1967 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1968 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1969 * page (e.g. CP437 or CP850). */
1970 SetConsoleOutputCP (GetACP ());
1971 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1973 freopen( "CONOUT$", "w", stderr );
1974 freopen( "CONIN$", "r", stdin );
1976 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1979 freopen( "vlc-help.txt", "wt", stdout );
1980 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1982 else freopen( "CONOUT$", "w", stdout );
1988 /*****************************************************************************
1989 * PauseConsole: On Win32, wait for a key press before closing the console
1990 *****************************************************************************
1991 * This function is useful only on Win32.
1992 *****************************************************************************/
1994 static void PauseConsole( void )
1998 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2000 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2008 /*****************************************************************************
2009 * ConsoleWidth: Return the console width in characters
2010 *****************************************************************************
2011 * We use the stty shell command to get the console width; if this fails or
2012 * if the width is less than 80, we default to 80.
2013 *****************************************************************************/
2014 static int ConsoleWidth( void )
2016 unsigned i_width = 80;
2019 FILE *file = popen( "stty size 2>/dev/null", "r" );
2022 if (fscanf (file, "%*u %u", &i_width) <= 0)
2027 CONSOLE_SCREEN_BUFFER_INFO buf;
2029 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
2030 i_width = buf.dwSize.X;
2036 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2037 vlc_value_t old_val, vlc_value_t new_val, void *param)
2039 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
2044 if( new_val.i_int >= -1 )
2046 libvlc_priv (p_libvlc)->i_verbose = __MIN( new_val.i_int, 2 );
2051 /*****************************************************************************
2052 * InitDeviceValues: initialize device values
2053 *****************************************************************************
2054 * This function inits the dvd, vcd and cd-audio values
2055 *****************************************************************************/
2056 static void InitDeviceValues( libvlc_int_t *p_vlc )
2059 LibHalContext * ctx = NULL;
2061 char **devices = NULL;
2062 char *block_dev = NULL;
2065 DBusConnection *p_connection = NULL;
2068 ctx = libhal_ctx_new();
2070 dbus_error_init( &error );
2071 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2072 if( dbus_error_is_set( &error ) || !p_connection )
2074 libhal_ctx_free( ctx );
2075 dbus_error_free( &error );
2078 libhal_ctx_set_dbus_connection( ctx, p_connection );
2079 if( libhal_ctx_init( ctx, &error ) )
2081 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2083 for( i = 0; i < i_devices; i++ )
2085 if( !libhal_device_property_exists( ctx, devices[i],
2086 "storage.cdrom.dvd", NULL ) )
2090 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2091 "storage.cdrom.dvd", NULL );
2092 block_dev = libhal_device_get_property_string( ctx,
2093 devices[ i ], "block.device" , NULL );
2096 config_PutPsz( p_vlc, "dvd", block_dev );
2099 config_PutPsz( p_vlc, "vcd", block_dev );
2100 config_PutPsz( p_vlc, "cd-audio", block_dev );
2101 libhal_free_string( block_dev );
2103 libhal_free_string_array( devices );
2105 libhal_ctx_shutdown( ctx, NULL );
2106 dbus_connection_unref( p_connection );
2107 libhal_ctx_free( ctx );
2111 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2115 #endif /* HAVE_HAL */