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"
87 #include <vlc_charset.h>
91 #include "playlist/playlist_internal.h"
97 /*****************************************************************************
98 * The evil global variables. We handle them with care, don't worry.
99 *****************************************************************************/
100 static libvlc_int_t * p_static_vlc = NULL;
101 static unsigned i_instances = 0;
104 static bool b_daemon = false;
112 * Atomically set the reference count to 1.
113 * @param p_gc reference counted object
114 * @param pf_destruct destruction calback
117 void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *))
119 p_gc->pf_destructor = pf_destruct;
121 /* Nobody else can possibly lock the spin - it's there as a barrier */
122 vlc_spin_init (&p_gc->spin);
123 vlc_spin_lock (&p_gc->spin);
125 vlc_spin_unlock (&p_gc->spin);
130 * Atomically increment the reference count.
131 * @param p_gc reference counted object
134 void *vlc_hold (gc_object_t * p_gc)
138 vlc_spin_lock (&p_gc->spin);
139 assert (p_gc->refs > 0);
141 vlc_spin_unlock (&p_gc->spin);
146 * Atomically decrement the reference count and, if it reaches zero, destroy.
147 * @param p_gc reference counted object.
149 void vlc_release (gc_object_t *p_gc)
154 vlc_spin_lock (&p_gc->spin);
155 assert (p_gc->refs > 0);
156 dead = !--p_gc->refs;
157 vlc_spin_unlock (&p_gc->spin);
161 vlc_spin_destroy (&p_gc->spin);
162 p_gc->pf_destructor (p_gc);
166 /*****************************************************************************
168 *****************************************************************************/
169 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
170 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
171 static void SetLanguage ( char const * );
173 static inline int LoadMessages (void);
174 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
175 static void Help ( libvlc_int_t *, char const *psz_help_name );
176 static void Usage ( libvlc_int_t *, char const *psz_search );
177 static void ListModules ( libvlc_int_t *, bool );
178 static void Version ( void );
181 static void ShowConsole ( bool );
182 static void PauseConsole ( void );
184 static int ConsoleWidth ( void );
186 static int VerboseCallback( vlc_object_t *, char const *,
187 vlc_value_t, vlc_value_t, void * );
189 static void InitDeviceValues( libvlc_int_t * );
192 * Allocate a libvlc instance, initialize global data if needed
193 * It also initializes the threading system
195 libvlc_int_t * libvlc_InternalCreate( void )
197 libvlc_int_t *p_libvlc;
199 char *psz_env = NULL;
201 /* vlc_threads_init *must* be the first internal call! No other call is
202 * allowed before the thread system has been initialized. */
203 if (vlc_threads_init ())
206 /* Now that the thread system is initialized, we don't have much, but
207 * at least we have variables */
208 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
209 if( i_instances == 0 )
211 /* Guess what CPU we have */
212 cpu_flags = CPUCapabilities();
213 /* The module bank will be initialized later */
214 p_module_bank = NULL;
217 /* Allocate a libvlc instance object */
218 p_libvlc = __vlc_custom_create( NULL, sizeof (*priv),
219 VLC_OBJECT_LIBVLC, "libvlc" );
220 if( p_libvlc != NULL )
222 vlc_mutex_unlock( lock );
224 if( p_libvlc == NULL )
227 priv = libvlc_priv (p_libvlc);
228 priv->p_playlist = NULL;
229 priv->p_interaction = NULL;
231 p_libvlc->psz_object_name = strdup( "libvlc" );
233 /* Initialize message queue */
234 msg_Create( p_libvlc );
236 /* Find verbosity from VLC_VERBOSE environment variable */
237 psz_env = getenv( "VLC_VERBOSE" );
238 if( psz_env != NULL )
239 priv->i_verbose = atoi( psz_env );
242 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
243 priv->b_color = isatty( 2 ); /* 2 is for stderr */
245 priv->b_color = false;
248 /* Announce who we are - Do it only for first instance ? */
249 msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
250 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
252 /* Initialize mutexes */
253 vlc_mutex_init( &priv->timer_lock );
254 vlc_mutex_init( &priv->config_lock );
256 /* Store data for the non-reentrant API */
257 p_static_vlc = p_libvlc;
263 * Initialize a libvlc instance
264 * This function initializes a previously allocated libvlc instance:
266 * - gettext initialization
267 * - message queue, module bank and playlist initialization
268 * - configuration and commandline parsing
270 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
271 const char *ppsz_argv[] )
273 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
274 char p_capabilities[200];
276 char * psz_modules = NULL;
277 char * psz_parser = NULL;
278 char * psz_control = NULL;
280 int i_ret = VLC_EEXIT;
281 playlist_t *p_playlist = NULL;
283 #if defined( ENABLE_NLS ) \
284 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
285 # if defined (WIN32) || defined (__APPLE__)
290 /* System specific initialization code */
291 system_Init( p_libvlc, &i_argc, ppsz_argv );
294 * Support for gettext
298 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
299 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
301 /* Initialize the module bank and load the configuration of the
302 * main module. We need to do this at this stage to be able to display
303 * a short help if required by the user. (short help == main module
305 module_InitBank( p_libvlc );
307 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
309 module_EndBank( p_libvlc );
313 /* Check for short help option */
314 if( config_GetInt( p_libvlc, "help" ) > 0 )
316 Help( p_libvlc, "help" );
318 i_ret = VLC_EEXITSUCCESS;
320 /* Check for version option */
321 else if( config_GetInt( p_libvlc, "version" ) > 0 )
325 i_ret = VLC_EEXITSUCCESS;
328 /* Set the config file stuff */
329 priv->psz_configfile = config_GetCustomConfigFile( p_libvlc );
331 /* Check for plugins cache options */
332 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
334 p_module_bank->b_cache_delete = true;
337 /* Will be re-done properly later on */
338 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
340 /* Check for daemon mode */
342 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
345 char *psz_pidfile = NULL;
347 if( daemon( 1, 0) != 0 )
349 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
354 /* lets check if we need to write the pidfile */
355 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
356 if( psz_pidfile != NULL )
359 pid_t i_pid = getpid ();
360 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
361 i_pid, psz_pidfile );
362 pidfile = utf8_fopen( psz_pidfile,"w" );
363 if( pidfile != NULL )
365 utf8_fprintf( pidfile, "%d", (int)i_pid );
370 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
379 if( ( i_pid = fork() ) < 0 )
381 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
386 /* This is the parent, exit right now */
387 msg_Dbg( p_libvlc, "closing parent process" );
389 i_ret = VLC_EEXITSUCCESS;
393 /* We are the child */
394 msg_Dbg( p_libvlc, "daemon spawned" );
395 close( STDIN_FILENO );
396 close( STDOUT_FILENO );
397 close( STDERR_FILENO );
407 module_EndBank( p_libvlc );
411 /* Check for translation config option */
412 #if defined( ENABLE_NLS ) \
413 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
414 # if defined (WIN32) || defined (__APPLE__)
415 /* This ain't really nice to have to reload the config here but it seems
416 * the only way to do it. */
418 if( !config_GetInt( p_libvlc, "ignore-config" ) )
419 config_LoadConfigFile( p_libvlc, "main" );
420 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
422 /* Check if the user specified a custom language */
423 psz_language = config_GetPsz( p_libvlc, "language" );
424 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
426 bool b_cache_delete = p_module_bank->b_cache_delete;
428 /* Reset the default domain */
429 SetLanguage( psz_language );
431 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
432 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
434 module_EndBank( p_libvlc );
435 module_InitBank( p_libvlc );
436 if( !config_GetInt( p_libvlc, "ignore-config" ) )
437 config_LoadConfigFile( p_libvlc, "main" );
438 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
439 p_module_bank->b_cache_delete = b_cache_delete;
441 free( psz_language );
446 * Load the builtins and plugins into the module_bank.
447 * We have to do it before config_Load*() because this also gets the
448 * list of configuration options exported by each module and loads their
451 module_LoadBuiltins( p_libvlc );
452 module_LoadPlugins( p_libvlc );
453 if( p_libvlc->b_die )
458 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
459 vlc_internals( p_module_bank )->i_children );
461 /* Check for help on modules */
462 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
464 Help( p_libvlc, p_tmp );
467 i_ret = VLC_EEXITSUCCESS;
469 /* Check for full help option */
470 else if( config_GetInt( p_libvlc, "full-help" ) > 0 )
472 config_PutInt( p_libvlc, "advanced", 1);
473 config_PutInt( p_libvlc, "help-verbose", 1);
474 Help( p_libvlc, "full-help" );
476 i_ret = VLC_EEXITSUCCESS;
478 /* Check for long help option */
479 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
481 Help( p_libvlc, "longhelp" );
483 i_ret = VLC_EEXITSUCCESS;
485 /* Check for module list option */
486 else if( config_GetInt( p_libvlc, "list" ) > 0 )
488 ListModules( p_libvlc, false );
490 i_ret = VLC_EEXITSUCCESS;
492 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
494 ListModules( p_libvlc, true );
496 i_ret = VLC_EEXITSUCCESS;
499 /* Check for config file options */
500 if( !config_GetInt( p_libvlc, "ignore-config" ) )
502 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
504 config_ResetAll( p_libvlc );
505 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
506 config_SaveConfigFile( p_libvlc, NULL );
508 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
510 config_LoadConfigFile( p_libvlc, NULL );
511 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
512 config_SaveConfigFile( p_libvlc, NULL );
518 module_EndBank( p_libvlc );
525 InitDeviceValues( p_libvlc );
528 * Override default configuration with config file settings
530 if( !config_GetInt( p_libvlc, "ignore-config" ) )
531 config_LoadConfigFile( p_libvlc, NULL );
534 * Override configuration with command line settings
536 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
539 ShowConsole( false );
540 /* Pause the console because it's destroyed when we exit */
541 fprintf( stderr, "The command line options couldn't be loaded, check "
542 "that they are valid.\n" );
545 module_EndBank( p_libvlc );
550 * System specific configuration
552 system_Configure( p_libvlc, &i_argc, ppsz_argv );
554 /* FIXME: could be replaced by using Unix sockets */
556 dbus_threads_init_default();
558 if( config_GetInt( p_libvlc, "one-instance" ) > 0
559 || ( config_GetInt( p_libvlc, "one-instance-when-started-from-file" )
560 && config_GetInt( p_libvlc, "started-from-file" ) ) )
562 /* Initialise D-Bus interface, check for other instances */
563 DBusConnection *p_conn = NULL;
564 DBusError dbus_error;
566 dbus_error_init( &dbus_error );
568 /* connect to the session bus */
569 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
572 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
573 dbus_error.message );
574 dbus_error_free( &dbus_error );
578 /* check if VLC is available on the bus
579 * if not: D-Bus control is not enabled on the other
580 * instance and we can't pass MRLs to it */
581 DBusMessage *p_test_msg = NULL;
582 DBusMessage *p_test_reply = NULL;
583 p_test_msg = dbus_message_new_method_call(
584 "org.mpris.vlc", "/",
585 "org.freedesktop.MediaPlayer", "Identity" );
586 /* block until a reply arrives */
587 p_test_reply = dbus_connection_send_with_reply_and_block(
588 p_conn, p_test_msg, -1, &dbus_error );
589 dbus_message_unref( p_test_msg );
590 if( p_test_reply == NULL )
592 dbus_error_free( &dbus_error );
593 msg_Dbg( p_libvlc, "No Media Player is running. "
594 "Continuing normally." );
599 DBusMessage* p_dbus_msg = NULL;
600 DBusMessageIter dbus_args;
601 DBusPendingCall* p_dbus_pending = NULL;
604 dbus_message_unref( p_test_reply );
605 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
607 for( i_input = optind;i_input < i_argc;i_input++ )
609 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
610 ppsz_argv[i_input] );
612 p_dbus_msg = dbus_message_new_method_call(
613 "org.mpris.vlc", "/TrackList",
614 "org.freedesktop.MediaPlayer", "AddTrack" );
616 if ( NULL == p_dbus_msg )
618 msg_Err( p_libvlc, "D-Bus problem" );
619 system_End( p_libvlc );
620 exit( VLC_ETIMEOUT );
624 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
625 if ( !dbus_message_iter_append_basic( &dbus_args,
626 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
628 dbus_message_unref( p_dbus_msg );
629 system_End( p_libvlc );
633 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
635 if ( !dbus_message_iter_append_basic( &dbus_args,
636 DBUS_TYPE_BOOLEAN, &b_play ) )
638 dbus_message_unref( p_dbus_msg );
639 system_End( p_libvlc );
643 /* send message and get a handle for a reply */
644 if ( !dbus_connection_send_with_reply ( p_conn,
645 p_dbus_msg, &p_dbus_pending, -1 ) )
647 msg_Err( p_libvlc, "D-Bus problem" );
648 dbus_message_unref( p_dbus_msg );
649 system_End( p_libvlc );
650 exit( VLC_ETIMEOUT );
653 if ( NULL == p_dbus_pending )
655 msg_Err( p_libvlc, "D-Bus problem" );
656 dbus_message_unref( p_dbus_msg );
657 system_End( p_libvlc );
658 exit( VLC_ETIMEOUT );
660 dbus_connection_flush( p_conn );
661 dbus_message_unref( p_dbus_msg );
662 /* block until we receive a reply */
663 dbus_pending_call_block( p_dbus_pending );
664 dbus_pending_call_unref( p_dbus_pending );
665 } /* processes all command line MRLs */
668 system_End( p_libvlc );
672 /* we unreference the connection when we've finished with it */
673 if( p_conn ) dbus_connection_unref( p_conn );
678 * Message queue options
680 char * psz_verbose_objects = config_GetPsz( p_libvlc, "verbose-objects" );
681 if( psz_verbose_objects )
683 char * psz_object, * iter = psz_verbose_objects;
684 while( (psz_object = strsep( &iter, "," )) )
686 switch( psz_object[0] )
688 printf("%s\n", psz_object+1);
689 case '+': msg_EnableObjectPrinting(p_libvlc, psz_object+1); break;
690 case '-': msg_DisableObjectPrinting(p_libvlc, psz_object+1); break;
692 msg_Err( p_libvlc, "verbose-objects usage: \n"
693 "--verbose-objects=+printthatobject,"
694 "-dontprintthatone\n"
695 "(keyword 'all' to applies to all objects)\n");
696 free( psz_verbose_objects );
700 free( psz_verbose_objects );
703 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
704 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
707 var_Set( p_libvlc, "verbose", val );
709 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
710 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
713 priv->b_color = config_GetInt( p_libvlc, "color" ) > 0;
716 * Output messages that may still be in the queue
718 msg_Flush( p_libvlc );
720 if( !config_GetInt( p_libvlc, "fpu" ) )
721 cpu_flags &= ~CPU_CAPABILITY_FPU;
723 #if defined( __i386__ ) || defined( __x86_64__ )
724 if( !config_GetInt( p_libvlc, "mmx" ) )
725 cpu_flags &= ~CPU_CAPABILITY_MMX;
726 if( !config_GetInt( p_libvlc, "3dn" ) )
727 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
728 if( !config_GetInt( p_libvlc, "mmxext" ) )
729 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
730 if( !config_GetInt( p_libvlc, "sse" ) )
731 cpu_flags &= ~CPU_CAPABILITY_SSE;
732 if( !config_GetInt( p_libvlc, "sse2" ) )
733 cpu_flags &= ~CPU_CAPABILITY_SSE2;
735 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
736 if( !config_GetInt( p_libvlc, "altivec" ) )
737 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
740 #define PRINT_CAPABILITY( capability, string ) \
741 if( vlc_CPU() & capability ) \
743 strncat( p_capabilities, string " ", \
744 sizeof(p_capabilities) - strlen(p_capabilities) ); \
745 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
748 p_capabilities[0] = '\0';
749 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
750 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
751 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
752 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
753 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
754 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
755 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
756 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
757 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
758 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
759 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
762 * Choose the best memcpy module
764 priv->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
766 priv->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
768 priv->pp_timers = NULL;
771 p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
772 if( !p_libvlc->p_stats )
774 vlc_object_release( p_libvlc );
777 vlc_mutex_init( &p_libvlc->p_stats->lock );
778 priv->p_stats_computer = NULL;
780 /* Init the array that holds every input item */
781 ARRAY_INIT( priv->input_items );
782 priv->i_last_input_id = 0;
785 * Initialize hotkey handling
787 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
788 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
789 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
790 /* Do a copy (we don't need to modify the strings) */
791 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
792 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
793 p_libvlc->p_hotkeys );
795 /* Initialize interaction */
796 priv->p_interaction = interaction_Init( p_libvlc );
798 /* Initialize playlist and get commandline files */
799 playlist_ThreadCreate( p_libvlc );
800 if( !priv->p_playlist )
802 msg_Err( p_libvlc, "playlist initialization failed" );
803 if( priv->p_memcpy_module != NULL )
805 module_Unneed( p_libvlc, priv->p_memcpy_module );
807 module_EndBank( p_libvlc );
810 p_playlist = priv->p_playlist;
812 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
813 if( psz_modules && *psz_modules )
815 /* Add service discovery modules */
816 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
821 /* Initialize VLM if vlm-conf is specified */
822 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
823 if( psz_parser && *psz_parser )
825 priv->p_vlm = vlm_New( p_libvlc );
827 msg_Err( p_libvlc, "VLM initialization failed" );
833 * Load background interfaces
835 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
836 psz_control = config_GetPsz( p_libvlc, "control" );
838 if( psz_modules && *psz_modules && psz_control && *psz_control )
840 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
841 strlen( psz_control ) + 1 );
842 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
844 else if( psz_control && *psz_control )
847 psz_modules = strdup( psz_control );
850 psz_parser = psz_modules;
851 while ( psz_parser && *psz_parser )
853 char *psz_module, *psz_temp;
854 psz_module = psz_parser;
855 psz_parser = strchr( psz_module, ':' );
861 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
864 sprintf( psz_temp, "%s,none", psz_module );
865 libvlc_InternalAddIntf( p_libvlc, psz_temp );
873 * Always load the hotkeys interface if it exists
875 libvlc_InternalAddIntf( p_libvlc, "hotkeys,none" );
878 /* loads dbus control interface if in one-instance mode
879 * we do it only when playlist exists, because dbus module needs it */
880 if( config_GetInt( p_libvlc, "one-instance" ) > 0
881 || ( config_GetInt( p_libvlc, "one-instance-when-started-from-file" )
882 && config_GetInt( p_libvlc, "started-from-file" ) ) )
883 libvlc_InternalAddIntf( p_libvlc, "dbus,none" );
885 /* Prevents the power management daemon from suspending the system
886 * when VLC is active */
887 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
888 libvlc_InternalAddIntf( p_libvlc, "inhibit,none" );
892 * If needed, load the Xscreensaver interface
893 * Currently, only for X
895 #ifdef HAVE_X11_XLIB_H
896 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
898 libvlc_InternalAddIntf( p_libvlc, "screensaver,none" );
902 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
904 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
907 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
909 char *logmode = var_CreateGetString( p_libvlc, "logmode" );
910 var_SetString( p_libvlc, "logmode", "syslog" );
911 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
915 var_SetString( p_libvlc, "logmode", logmode );
919 var_Destroy( p_libvlc, "logmode" );
923 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
925 libvlc_InternalAddIntf( p_libvlc, "showintf,none" );
928 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
930 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
934 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
936 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
939 char *psz_morecodecs;
940 if( asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs) != -1 )
942 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
943 free( psz_morecodecs );
947 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
953 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
955 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
956 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
957 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
958 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
959 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
960 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
961 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
962 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
963 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
965 /* Create volume callback system. */
966 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
968 /* Create a variable for showing the interface (moved from playlist). */
969 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
970 var_SetBool( p_libvlc, "intf-show", true );
972 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
975 * Get input filenames given as commandline arguments
977 GetFilenames( p_libvlc, i_argc, ppsz_argv );
980 * Get --open argument
982 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
983 var_Get( p_libvlc, "open", &val );
984 if ( val.psz_string != NULL && *val.psz_string )
986 playlist_t *p_playlist = pl_Yield( p_libvlc );
987 playlist_AddExt( p_playlist, val.psz_string, NULL, PLAYLIST_INSERT, 0,
988 -1, NULL, 0, true, pl_Unlocked );
989 pl_Release( p_libvlc );
991 free( val.psz_string );
997 * Cleanup a libvlc instance. The instance is not completely deallocated
998 * \param p_libvlc the instance to clean
1000 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
1002 intf_thread_t * p_intf = NULL;
1003 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1005 /* Ask the interfaces to stop and destroy them */
1006 msg_Dbg( p_libvlc, "removing all interfaces" );
1007 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1009 intf_StopThread( p_intf );
1010 vlc_object_detach( p_intf );
1011 vlc_object_release( p_intf ); /* for intf_Create() */
1012 vlc_object_release( p_intf ); /* for vlc_object_find() */
1016 /* Destroy VLM if created in libvlc_InternalInit */
1019 vlm_Delete( priv->p_vlm );
1023 playlist_t *p_playlist = priv->p_playlist;
1024 /* Remove all services discovery */
1025 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
1026 playlist_ServicesDiscoveryKillAll( p_playlist );
1029 /* Any thread still running must not assume pl_Yield() succeeds. */
1030 msg_Dbg( p_libvlc, "removing playlist" );
1031 priv->p_playlist = NULL;
1032 vlc_object_kill( p_playlist ); /* <-- memory barrier for pl_Yield() */
1033 vlc_thread_join( p_playlist );
1034 vlc_object_release( p_playlist );
1036 /* Free interaction */
1037 msg_Dbg( p_libvlc, "removing interaction" );
1038 interaction_Destroy( priv->p_interaction );
1040 /* Free video outputs */
1041 msg_Dbg( p_libvlc, "removing all video outputs" );
1042 vlc_list_t *list = vlc_list_find (p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD);
1043 for (int i = 0; i < list->i_count; i++)
1044 vlc_object_release (list->p_values[i].p_object);
1045 vlc_list_release (list);
1047 stats_TimersDumpAll( p_libvlc );
1048 stats_TimersCleanAll( p_libvlc );
1050 bool b_clean = true;
1051 FOREACH_ARRAY( input_item_t *p_del, priv->input_items )
1052 msg_Err( p_libvlc, "input item %p has not been deleted properly: name %s",
1053 p_del, p_del->psz_name ? p_del->psz_name : "(null)" );
1057 ARRAY_RESET( priv->input_items );
1059 msg_Dbg( p_libvlc, "removing stats" );
1060 vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1061 FREENULL( p_libvlc->p_stats );
1067 * Destroy everything.
1068 * This function requests the running threads to finish, waits for their
1069 * termination, and destroys their structure.
1070 * It stops the thread systems: no instance can run after this has run
1071 * \param p_libvlc the instance to destroy
1073 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
1076 return VLC_EGENERIC;
1078 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1081 char* psz_pidfile = NULL;
1085 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1086 if( psz_pidfile != NULL )
1088 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1089 if( unlink( psz_pidfile ) == -1 )
1091 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1095 free( psz_pidfile );
1099 if( priv->p_memcpy_module )
1101 module_Unneed( p_libvlc, priv->p_memcpy_module );
1102 priv->p_memcpy_module = NULL;
1105 /* Free module bank. It is refcounted, so we call this each time */
1106 module_EndBank( p_libvlc );
1108 FREENULL( priv->psz_configfile );
1109 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1110 p_libvlc->p_hotkeys );
1111 FREENULL( p_libvlc->p_hotkeys );
1113 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1116 if( i_instances == 0 )
1118 /* System specific cleaning code */
1119 system_End( p_libvlc );
1121 vlc_mutex_unlock( lock );
1123 msg_Flush( p_libvlc );
1124 msg_Destroy( p_libvlc );
1126 /* Destroy mutexes */
1127 vlc_mutex_destroy( &priv->config_lock );
1128 vlc_mutex_destroy( &priv->timer_lock );
1130 vlc_object_release( p_libvlc );
1133 /* Stop thread system: last one out please shut the door!
1134 * The number of initializations of the thread system is counted, we
1135 * can call this each time */
1142 * Add an interface plugin and run it
1144 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
1147 intf_thread_t *p_intf = NULL;
1150 return VLC_EGENERIC;
1152 if( !psz_module ) /* requesting the default interface */
1154 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1155 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1159 /* Daemon mode hack.
1160 * We prefer the dummy interface if none is specified. */
1161 psz_module = "dummy";
1164 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1166 free( psz_interface );
1169 /* Try to create the interface */
1170 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
1171 if( p_intf == NULL )
1173 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1175 return VLC_EGENERIC;
1178 /* Try to run the interface */
1179 i_err = intf_RunThread( p_intf );
1182 vlc_object_detach( p_intf );
1183 vlc_object_release( p_intf );
1190 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1191 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1192 /*****************************************************************************
1193 * SetLanguage: set the interface language.
1194 *****************************************************************************
1195 * We set the LC_MESSAGES locale category for interface messages and buttons,
1196 * as well as the LC_CTYPE category for string sorting and possible wide
1197 * character support.
1198 *****************************************************************************/
1199 static void SetLanguage ( const char *psz_lang )
1202 /* I need that under Darwin, please check it doesn't disturb
1203 * other platforms. --Meuuh */
1204 setenv( "LANG", psz_lang, 1 );
1207 /* We set LC_ALL manually because it is the only way to set
1208 * the language at runtime under eg. Windows. Beware that this
1209 * makes the environment unconsistent when libvlc is unloaded and
1210 * should probably be moved to a safer place like vlc.c. */
1211 static char psz_lcall[20];
1212 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1213 psz_lcall[19] = '\0';
1214 putenv( psz_lcall );
1217 setlocale( LC_ALL, psz_lang );
1222 static inline int LoadMessages (void)
1224 #if defined( ENABLE_NLS ) \
1225 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1226 /* Specify where to find the locales for current domain */
1227 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1228 static const char psz_path[] = LOCALEDIR;
1230 char psz_path[1024];
1231 if (snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "%s",
1232 config_GetDataDir(), "locale")
1233 >= (int)sizeof (psz_path))
1237 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1239 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1240 " to directory %s\n", psz_path);
1244 /* LibVLC wants all messages in UTF-8.
1245 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1246 * and other functions that are not part of our text domain.
1248 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1250 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1252 // Unbinds the text domain to avoid broken encoding
1253 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1257 /* LibVLC does NOT set the default textdomain, since it is a library.
1258 * This could otherwise break programs using LibVLC (other than VLC).
1259 * textdomain (PACKAGE_NAME);
1265 /*****************************************************************************
1266 * GetFilenames: parse command line options which are not flags
1267 *****************************************************************************
1268 * Parse command line for input files as well as their associated options.
1269 * An option always follows its associated input and begins with a ":".
1270 *****************************************************************************/
1271 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1273 int i_opt, i_options;
1275 /* We assume that the remaining parameters are filenames
1276 * and their input options */
1277 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1281 /* Count the input options */
1282 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1288 /* TODO: write an internal function of this one, to avoid
1289 * unnecessary lookups. */
1291 playlist_t *p_playlist = pl_Yield( p_vlc );
1292 playlist_AddExt( p_playlist, ppsz_argv[i_opt], NULL, PLAYLIST_INSERT,
1293 0, -1, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ),
1294 i_options, true, pl_Unlocked );
1295 pl_Release( p_vlc );
1301 /*****************************************************************************
1302 * Help: print program help
1303 *****************************************************************************
1304 * Print a short inline help. Message interface is initialized at this stage.
1305 *****************************************************************************/
1306 static inline void print_help_on_full_help( void )
1308 utf8_fprintf( stdout, "\n" );
1309 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
1312 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1315 ShowConsole( true );
1318 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1320 utf8_fprintf( stdout, vlc_usage, "vlc" );
1321 Usage( p_this, "=help" );
1322 Usage( p_this, "=main" );
1323 print_help_on_full_help();
1325 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1327 utf8_fprintf( stdout, vlc_usage, "vlc" );
1328 Usage( p_this, NULL );
1329 print_help_on_full_help();
1331 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
1333 utf8_fprintf( stdout, vlc_usage, "vlc" );
1334 Usage( p_this, NULL );
1336 else if( psz_help_name )
1338 Usage( p_this, psz_help_name );
1341 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1346 /*****************************************************************************
1347 * Usage: print module usage
1348 *****************************************************************************
1349 * Print a short inline help. Message interface is initialized at this stage.
1350 *****************************************************************************/
1351 # define COL(x) "\033[" #x ";1m"
1352 # define RED COL(31)
1353 # define GREEN COL(32)
1354 # define YELLOW COL(33)
1355 # define BLUE COL(34)
1356 # define MAGENTA COL(35)
1357 # define CYAN COL(36)
1358 # define WHITE COL(0)
1359 # define GRAY "\033[0m"
1360 static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
1362 if( !p_item ) return;
1365 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1367 if( b_description && p_item->psz_longtext )
1368 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1369 p_item->psz_longtext );
1373 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1374 if( b_description && p_item->psz_longtext )
1375 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1379 static void Usage( libvlc_int_t *p_this, char const *psz_search )
1381 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1382 /* short option ------' | | | | | | |
1383 * option name ------------' | | | | | |
1384 * <bra ---------------------' | | | | |
1385 * option type or "" ----------' | | | |
1386 * ket> -------------------------' | | |
1387 * padding spaces -----------------' | |
1388 * comment --------------------------' |
1389 * comment suffix ---------------------'
1391 * The purpose of having bra and ket is that we might i18n them as well.
1394 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1395 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1397 #define LINE_START 8
1398 #define PADDING_SPACES 25
1400 # define OPTION_VALUE_SEP "="
1402 # define OPTION_VALUE_SEP " "
1404 vlc_list_t *p_list = NULL;
1405 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1406 char psz_spaces_longtext[LINE_START+3];
1407 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1408 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1409 char psz_buffer[10000];
1412 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1413 int i_width_description = i_width + PADDING_SPACES - 1;
1414 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1415 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1416 bool b_description_hack;
1417 bool b_color = config_GetInt( p_this, "color" ) > 0;
1418 bool b_has_advanced = false;
1419 bool b_found = false;
1420 int i_only_advanced = 0; /* Number of modules ignored because they
1421 * only have advanced options */
1422 bool b_strict = psz_search && *psz_search == '=';
1423 if( b_strict ) psz_search++;
1425 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1426 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1427 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1428 psz_spaces_longtext[LINE_START+2] = '\0';
1432 b_color = false; // don't put color control codes in a .txt file
1436 strcpy( psz_format, COLOR_FORMAT_STRING );
1437 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1441 strcpy( psz_format, FORMAT_STRING );
1442 strcpy( psz_format_bool, FORMAT_STRING );
1445 /* List all modules */
1446 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1448 /* Ugly hack to make sure that the help options always come first
1451 Usage( p_this, "help" );
1453 /* Enumerate the config for each module */
1454 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1457 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1458 module_config_t *p_item = NULL;
1459 module_config_t *p_section = NULL;
1460 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1463 ( b_strict ? strcmp( psz_search, p_parser->psz_object_name )
1464 : !strstr( p_parser->psz_object_name, psz_search ) ) )
1466 char *const *pp_shortcut = p_parser->pp_shortcuts;
1467 while( *pp_shortcut )
1469 if( b_strict ? !strcmp( psz_search, *pp_shortcut )
1470 : !!strstr( *pp_shortcut, psz_search ) )
1478 /* Ignore modules without config options */
1479 if( !p_parser->i_config_items )
1484 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1485 /* Ugly hack to make sure that the help options always come first
1487 if( !psz_search && b_help_module )
1490 /* Ignore modules with only advanced config options if requested */
1493 for( p_item = p_parser->p_config;
1497 if( (p_item->i_type & CONFIG_ITEM) &&
1498 !p_item->b_advanced && !p_item->b_removed ) break;
1501 if( p_item == p_end )
1510 /* Print name of module */
1511 if( strcmp( "main", p_parser->psz_object_name ) )
1514 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
1515 p_parser->psz_longname,
1516 p_parser->psz_object_name );
1518 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1520 if( p_parser->psz_help )
1523 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1525 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1528 /* Print module options */
1529 for( p_item = p_parser->p_config;
1533 char *psz_text, *psz_spaces = psz_spaces_text;
1534 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1535 const char *psz_suf = "", *psz_prefix = NULL;
1539 /* Skip removed options */
1540 if( p_item->b_removed )
1544 /* Skip advanced options if requested */
1545 if( p_item->b_advanced && !b_advanced )
1547 b_has_advanced = true;
1551 switch( p_item->i_type )
1553 case CONFIG_HINT_CATEGORY:
1554 case CONFIG_HINT_USAGE:
1555 if( !strcmp( "main", p_parser->psz_object_name ) )
1558 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1561 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1563 if( b_description && p_item->psz_longtext )
1566 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1567 p_item->psz_longtext );
1569 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1573 case CONFIG_HINT_SUBCATEGORY:
1574 if( strcmp( "main", p_parser->psz_object_name ) )
1576 case CONFIG_SECTION:
1580 case CONFIG_ITEM_STRING:
1581 case CONFIG_ITEM_FILE:
1582 case CONFIG_ITEM_DIRECTORY:
1583 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1584 case CONFIG_ITEM_MODULE_CAT:
1585 case CONFIG_ITEM_MODULE_LIST:
1586 case CONFIG_ITEM_MODULE_LIST_CAT:
1587 case CONFIG_ITEM_FONT:
1588 case CONFIG_ITEM_PASSWORD:
1589 print_help_section( p_section, b_color, b_description );
1591 psz_bra = OPTION_VALUE_SEP "<";
1592 psz_type = _("string");
1595 if( p_item->ppsz_list )
1597 psz_bra = OPTION_VALUE_SEP "{";
1598 psz_type = psz_buffer;
1599 psz_buffer[0] = '\0';
1600 for( i = 0; p_item->ppsz_list[i]; i++ )
1602 if( i ) strcat( psz_buffer, "," );
1603 strcat( psz_buffer, p_item->ppsz_list[i] );
1608 case CONFIG_ITEM_INTEGER:
1609 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1610 print_help_section( p_section, b_color, b_description );
1612 psz_bra = OPTION_VALUE_SEP "<";
1613 psz_type = _("integer");
1616 if( p_item->min.i || p_item->max.i )
1618 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1619 p_item->min.i, p_item->max.i );
1620 psz_type = psz_buffer;
1623 if( p_item->i_list )
1625 psz_bra = OPTION_VALUE_SEP "{";
1626 psz_type = psz_buffer;
1627 psz_buffer[0] = '\0';
1628 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1630 if( i ) strcat( psz_buffer, ", " );
1631 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1633 p_item->ppsz_list_text[i] );
1638 case CONFIG_ITEM_FLOAT:
1639 print_help_section( p_section, b_color, b_description );
1641 psz_bra = OPTION_VALUE_SEP "<";
1642 psz_type = _("float");
1644 if( p_item->min.f || p_item->max.f )
1646 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1647 p_item->min.f, p_item->max.f );
1648 psz_type = psz_buffer;
1651 case CONFIG_ITEM_BOOL:
1652 print_help_section( p_section, b_color, b_description );
1654 psz_bra = ""; psz_type = ""; psz_ket = "";
1655 if( !b_help_module )
1657 psz_suf = p_item->value.i ? _(" (default enabled)") :
1658 _(" (default disabled)");
1668 /* Add short option if any */
1669 if( p_item->i_short )
1671 sprintf( psz_short, "-%c,", p_item->i_short );
1675 strcpy( psz_short, " " );
1678 i = PADDING_SPACES - strlen( p_item->psz_name )
1679 - strlen( psz_bra ) - strlen( psz_type )
1680 - strlen( psz_ket ) - 1;
1682 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1684 psz_prefix = ", --no-";
1685 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1690 psz_spaces[0] = '\n';
1695 psz_spaces[i] = '\0';
1698 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1700 utf8_fprintf( stdout, psz_format_bool, psz_short,
1701 p_item->psz_name, psz_prefix, p_item->psz_name,
1702 psz_bra, psz_type, psz_ket, psz_spaces );
1706 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1707 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1710 psz_spaces[i] = ' ';
1712 /* We wrap the rest of the output */
1713 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1714 b_description_hack = b_description;
1717 psz_text = psz_buffer;
1718 i_cur_width = b_description && !b_description_hack
1719 ? i_width_description
1723 char *psz_parser, *psz_word;
1724 size_t i_end = strlen( psz_text );
1726 /* If the remaining text fits in a line, print it. */
1727 if( i_end <= i_cur_width )
1731 if( !b_description || b_description_hack )
1732 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1734 utf8_fprintf( stdout, "%s\n", psz_text );
1738 utf8_fprintf( stdout, "%s\n", psz_text );
1743 /* Otherwise, eat as many words as possible */
1744 psz_parser = psz_text;
1747 psz_word = psz_parser;
1748 psz_parser = strchr( psz_word, ' ' );
1749 /* If no space was found, we reached the end of the text
1750 * block; otherwise, we skip the space we just found. */
1751 psz_parser = psz_parser ? psz_parser + 1
1754 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1756 /* We cut a word in one of these cases:
1757 * - it's the only word in the line and it's too long.
1758 * - we used less than 80% of the width and the word we are
1759 * going to wrap is longer than 40% of the width, and even
1760 * if the word would have fit in the next line. */
1761 if( psz_word == psz_text
1762 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1763 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1765 char c = psz_text[i_cur_width];
1766 psz_text[i_cur_width] = '\0';
1769 if( !b_description || b_description_hack )
1770 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1771 psz_text, psz_spaces );
1773 utf8_fprintf( stdout, "%s\n%s",
1774 psz_text, psz_spaces );
1778 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1780 psz_text += i_cur_width;
1785 psz_word[-1] = '\0';
1788 if( !b_description || b_description_hack )
1789 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1790 psz_text, psz_spaces );
1792 utf8_fprintf( stdout, "%s\n%s",
1793 psz_text, psz_spaces );
1797 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1799 psz_text = psz_word;
1803 if( b_description_hack && p_item->psz_longtext )
1805 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1806 b_description_hack = false;
1807 psz_spaces = psz_spaces_longtext;
1808 utf8_fprintf( stdout, "%s", psz_spaces );
1814 if( b_has_advanced )
1817 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1818 _( "add --advanced to your command line to see advanced options."));
1820 utf8_fprintf( stdout, "\n%s %s\n", _( "Note:" ),
1821 _( "add --advanced to your command line to see advanced options."));
1824 if( i_only_advanced > 0 )
1828 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " ", _( "Note:" ) );
1829 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1833 utf8_fprintf( stdout, "\n%s ", _( "Note:" ) );
1834 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1840 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY "\n",
1841 _( "No matching module found. Use --list or" \
1842 "--list-verbose to list available modules." ) );
1844 utf8_fprintf( stdout, "\n%s\n",
1845 _( "No matching module found. Use --list or" \
1846 "--list-verbose to list available modules." ) );
1849 /* Release the module list */
1850 vlc_list_release( p_list );
1853 /*****************************************************************************
1854 * ListModules: list the available modules with their description
1855 *****************************************************************************
1856 * Print a list of all available modules (builtins and plugins) and a short
1857 * description for each one.
1858 *****************************************************************************/
1859 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1861 vlc_list_t *p_list = NULL;
1862 module_t *p_parser = NULL;
1863 char psz_spaces[22];
1866 bool b_color = config_GetInt( p_this, "color" ) > 0;
1868 memset( psz_spaces, ' ', 22 );
1871 ShowConsole( true );
1874 /* List all modules */
1875 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1877 /* Enumerate each module */
1878 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1882 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1884 /* Nasty hack, but right now I'm too tired to think about a nice
1886 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1891 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1892 p_parser->psz_object_name,
1894 p_parser->psz_longname );
1896 utf8_fprintf( stdout, " %s%s %s\n",
1897 p_parser->psz_object_name,
1898 psz_spaces, p_parser->psz_longname );
1902 char *const *pp_shortcut = p_parser->pp_shortcuts;
1903 while( *pp_shortcut )
1905 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1908 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1911 utf8_fprintf( stdout, " s %s\n",
1916 if( p_parser->psz_capability )
1919 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1920 p_parser->psz_capability,
1921 p_parser->i_score );
1923 utf8_fprintf( stdout, " c %s (%d)\n",
1924 p_parser->psz_capability,
1925 p_parser->i_score );
1929 psz_spaces[i] = ' ';
1932 vlc_list_release( p_list );
1934 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1939 /*****************************************************************************
1940 * Version: print complete program version
1941 *****************************************************************************
1942 * Print complete program version and build number.
1943 *****************************************************************************/
1944 static void Version( void )
1947 ShowConsole( true );
1950 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1951 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1952 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1953 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1954 if( strcmp( VLC_Changeset(), "exported" ) )
1955 utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1957 utf8_fprintf( stdout, LICENSE_MSG );
1959 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1964 /*****************************************************************************
1965 * ShowConsole: On Win32, create an output console for debug messages
1966 *****************************************************************************
1967 * This function is useful only on Win32.
1968 *****************************************************************************/
1970 static void ShowConsole( bool b_dofile )
1973 FILE *f_help = NULL;
1975 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1978 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1979 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1980 * page (e.g. CP437 or CP850). */
1981 SetConsoleOutputCP (GetACP ());
1982 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1984 freopen( "CONOUT$", "w", stderr );
1985 freopen( "CONIN$", "r", stdin );
1987 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1990 freopen( "vlc-help.txt", "wt", stdout );
1991 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1993 else freopen( "CONOUT$", "w", stdout );
1999 /*****************************************************************************
2000 * PauseConsole: On Win32, wait for a key press before closing the console
2001 *****************************************************************************
2002 * This function is useful only on Win32.
2003 *****************************************************************************/
2005 static void PauseConsole( void )
2009 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2011 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2019 /*****************************************************************************
2020 * ConsoleWidth: Return the console width in characters
2021 *****************************************************************************
2022 * We use the stty shell command to get the console width; if this fails or
2023 * if the width is less than 80, we default to 80.
2024 *****************************************************************************/
2025 static int ConsoleWidth( void )
2027 unsigned i_width = 80;
2030 FILE *file = popen( "stty size 2>/dev/null", "r" );
2033 if (fscanf (file, "%*u %u", &i_width) <= 0)
2038 CONSOLE_SCREEN_BUFFER_INFO buf;
2040 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
2041 i_width = buf.dwSize.X;
2047 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2048 vlc_value_t old_val, vlc_value_t new_val, void *param)
2050 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
2055 if( new_val.i_int >= -1 )
2057 libvlc_priv (p_libvlc)->i_verbose = __MIN( new_val.i_int, 2 );
2062 /*****************************************************************************
2063 * InitDeviceValues: initialize device values
2064 *****************************************************************************
2065 * This function inits the dvd, vcd and cd-audio values
2066 *****************************************************************************/
2067 static void InitDeviceValues( libvlc_int_t *p_vlc )
2070 LibHalContext * ctx = NULL;
2072 char **devices = NULL;
2073 char *block_dev = NULL;
2076 DBusConnection *p_connection = NULL;
2079 ctx = libhal_ctx_new();
2081 dbus_error_init( &error );
2082 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2083 if( dbus_error_is_set( &error ) || !p_connection )
2085 libhal_ctx_free( ctx );
2086 dbus_error_free( &error );
2089 libhal_ctx_set_dbus_connection( ctx, p_connection );
2090 if( libhal_ctx_init( ctx, &error ) )
2092 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2094 for( i = 0; i < i_devices; i++ )
2096 if( !libhal_device_property_exists( ctx, devices[i],
2097 "storage.cdrom.dvd", NULL ) )
2101 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2102 "storage.cdrom.dvd", NULL );
2103 block_dev = libhal_device_get_property_string( ctx,
2104 devices[ i ], "block.device" , NULL );
2107 config_PutPsz( p_vlc, "dvd", block_dev );
2110 config_PutPsz( p_vlc, "vcd", block_dev );
2111 config_PutPsz( p_vlc, "cd-audio", block_dev );
2112 libhal_free_string( block_dev );
2114 libhal_free_string_array( devices );
2116 libhal_ctx_shutdown( ctx, NULL );
2117 dbus_connection_unref( p_connection );
2118 libhal_ctx_free( ctx );
2122 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2126 #endif /* HAVE_HAL */