1 /*****************************************************************************
2 * libvlc-common.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2006 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * RĂ©mi Denis-Courmont <rem # videolan : org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
34 *****************************************************************************/
40 #include "control/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
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"
98 /*****************************************************************************
99 * The evil global variable. We handle it with care, don't worry.
100 *****************************************************************************/
101 static libvlc_global_data_t libvlc_global;
102 static libvlc_global_data_t *p_libvlc_global = &libvlc_global;
103 static libvlc_int_t * p_static_vlc = NULL;
104 static volatile unsigned int i_instances = 0;
106 /*****************************************************************************
108 *****************************************************************************/
109 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
110 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
111 static void SetLanguage ( char const * );
113 static inline int LoadMessages (void);
114 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
115 static void Help ( libvlc_int_t *, char const *psz_help_name );
116 static void Usage ( libvlc_int_t *, char const *psz_module_name );
117 static void ListModules ( libvlc_int_t *, bool );
118 static void Version ( void );
121 static void ShowConsole ( bool );
122 static void PauseConsole ( void );
124 static int ConsoleWidth ( void );
126 static int VerboseCallback( vlc_object_t *, char const *,
127 vlc_value_t, vlc_value_t, void * );
129 static void InitDeviceValues( libvlc_int_t * );
131 libvlc_global_data_t *vlc_global( void )
133 return p_libvlc_global;
136 /*****************************************************************************
137 * vlc_current_object: return the current object.
138 *****************************************************************************
139 * If i_object is non-zero, return the corresponding object. Otherwise,
140 * return the statically allocated p_vlc object.
141 *****************************************************************************/
142 libvlc_int_t * vlc_current_object( int i_object )
144 return i_object ? vlc_object_get( i_object ) : p_static_vlc;
149 * Allocate a libvlc instance, initialize global data if needed
150 * It also initializes the threading system
152 libvlc_int_t * libvlc_InternalCreate( void )
155 libvlc_int_t * p_libvlc = NULL;
156 char *psz_env = NULL;
158 /* vlc_threads_init *must* be the first internal call! No other call is
159 * allowed before the thread system has been initialized. */
160 i_ret = vlc_threads_init( p_libvlc_global );
161 if( i_ret < 0 ) return NULL;
163 /* Now that the thread system is initialized, we don't have much, but
164 * at least we have variables */
165 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
169 if( !p_libvlc_global->b_ready )
171 /* Guess what CPU we have */
172 cpu_flags = CPUCapabilities();
173 /* The module bank will be initialized later */
174 p_libvlc_global->p_module_bank = NULL;
176 p_libvlc_global->b_ready = true;
178 vlc_mutex_unlock( lock );
180 /* Allocate a libvlc instance object */
181 p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
182 if( p_libvlc == NULL )
187 p_libvlc->p_playlist = NULL;
188 p_libvlc->p_interaction = NULL;
189 p_libvlc->p_vlm = NULL;
190 p_libvlc->psz_object_name = strdup( "libvlc" );
192 /* Initialize message queue */
193 msg_Create( p_libvlc );
195 /* Find verbosity from VLC_VERBOSE environment variable */
196 psz_env = getenv( "VLC_VERBOSE" );
197 if( psz_env != NULL )
198 p_libvlc->i_verbose = atoi( psz_env );
200 p_libvlc->i_verbose = 3;
201 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
202 p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
204 p_libvlc->b_color = false;
207 /* Announce who we are - Do it only for first instance ? */
208 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
209 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
211 /* Initialize mutexes */
212 vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
213 vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
215 vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
216 vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
218 /* Store data for the non-reentrant API */
219 p_static_vlc = p_libvlc;
225 * Initialize a libvlc instance
226 * This function initializes a previously allocated libvlc instance:
228 * - gettext initialization
229 * - message queue, module bank and playlist initialization
230 * - configuration and commandline parsing
232 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
233 const char *ppsz_argv[] )
235 char p_capabilities[200];
237 char * psz_modules = NULL;
238 char * psz_parser = NULL;
239 char * psz_control = NULL;
241 int i_ret = VLC_EEXIT;
242 playlist_t *p_playlist = NULL;
244 #if defined( ENABLE_NLS ) \
245 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
246 # if defined (WIN32) || defined (__APPLE__)
251 /* System specific initialization code */
252 system_Init( p_libvlc, &i_argc, ppsz_argv );
254 /* Get the executable name (similar to the basename command) */
257 const char *exe = strdup( ppsz_argv[0] );
258 const char *tmp = exe;
264 p_libvlc->psz_object_name = strdup( tmp );
268 p_libvlc->psz_object_name = strdup( "vlc" );
272 * Support for gettext
276 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
277 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
279 /* Initialize the module bank and load the configuration of the
280 * main module. We need to do this at this stage to be able to display
281 * a short help if required by the user. (short help == main module
283 module_InitBank( p_libvlc );
285 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
287 module_EndBank( p_libvlc );
291 /* Check for short help option */
292 if( config_GetInt( p_libvlc, "help" ) > 0 )
294 Help( p_libvlc, "help" );
296 i_ret = VLC_EEXITSUCCESS;
298 /* Check for version option */
299 else if( config_GetInt( p_libvlc, "version" ) > 0 )
303 i_ret = VLC_EEXITSUCCESS;
306 /* Set the config file stuff */
307 p_libvlc->psz_homedir = config_GetHomeDir();
308 p_libvlc->psz_configdir = config_GetConfigDir( p_libvlc );
309 p_libvlc->psz_datadir = config_GetUserDataDir( p_libvlc );
310 p_libvlc->psz_cachedir = config_GetCacheDir( p_libvlc );
311 p_libvlc->psz_configfile = config_GetCustomConfigFile( p_libvlc );
313 /* Check for plugins cache options */
314 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
316 p_libvlc_global->p_module_bank->b_cache_delete = true;
319 /* Will be re-done properly later on */
320 p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
322 /* Check for daemon mode */
324 if( config_GetInt( p_libvlc, "daemon" ) )
327 char *psz_pidfile = NULL;
329 if( daemon( 1, 0) != 0 )
331 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
334 p_libvlc_global->b_daemon = true;
336 /* lets check if we need to write the pidfile */
337 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
338 if( psz_pidfile != NULL )
341 pid_t i_pid = getpid ();
342 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
343 i_pid, psz_pidfile );
344 pidfile = utf8_fopen( psz_pidfile,"w" );
345 if( pidfile != NULL )
347 utf8_fprintf( pidfile, "%d", (int)i_pid );
352 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
361 if( ( i_pid = fork() ) < 0 )
363 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
368 /* This is the parent, exit right now */
369 msg_Dbg( p_libvlc, "closing parent process" );
371 i_ret = VLC_EEXITSUCCESS;
375 /* We are the child */
376 msg_Dbg( p_libvlc, "daemon spawned" );
377 close( STDIN_FILENO );
378 close( STDOUT_FILENO );
379 close( STDERR_FILENO );
381 p_libvlc_global->b_daemon = true;
389 module_EndBank( p_libvlc );
393 /* Check for translation config option */
394 #if defined( ENABLE_NLS ) \
395 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
396 # if defined (WIN32) || defined (__APPLE__)
397 /* This ain't really nice to have to reload the config here but it seems
398 * the only way to do it. */
400 if( !config_GetInt( p_libvlc, "ignore-config" ) )
401 config_LoadConfigFile( p_libvlc, "main" );
402 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
404 /* Check if the user specified a custom language */
405 psz_language = config_GetPsz( p_libvlc, "language" );
406 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
408 bool b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
410 /* Reset the default domain */
411 SetLanguage( psz_language );
413 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
414 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
416 module_EndBank( p_libvlc );
417 module_InitBank( p_libvlc );
418 if( !config_GetInt( p_libvlc, "ignore-config" ) )
419 config_LoadConfigFile( p_libvlc, "main" );
420 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
421 p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
423 free( psz_language );
428 * Load the builtins and plugins into the module_bank.
429 * We have to do it before config_Load*() because this also gets the
430 * list of configuration options exported by each module and loads their
433 module_LoadBuiltins( p_libvlc );
434 module_LoadPlugins( p_libvlc );
435 if( p_libvlc->b_die )
440 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
441 p_libvlc_global->p_module_bank->i_children );
443 /* Check for help on modules */
444 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
446 Help( p_libvlc, p_tmp );
449 i_ret = VLC_EEXITSUCCESS;
451 /* Check for long help option */
452 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
454 Help( p_libvlc, "longhelp" );
456 i_ret = VLC_EEXITSUCCESS;
458 /* Check for module list option */
459 else if( config_GetInt( p_libvlc, "list" ) > 0 )
461 ListModules( p_libvlc, false );
463 i_ret = VLC_EEXITSUCCESS;
465 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
467 ListModules( p_libvlc, true );
469 i_ret = VLC_EEXITSUCCESS;
472 /* Check for config file options */
473 if( !config_GetInt( p_libvlc, "ignore-config" ) )
475 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
477 config_ResetAll( p_libvlc );
478 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
479 config_SaveConfigFile( p_libvlc, NULL );
481 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
483 config_LoadConfigFile( p_libvlc, NULL );
484 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
485 config_SaveConfigFile( p_libvlc, NULL );
491 module_EndBank( p_libvlc );
498 InitDeviceValues( p_libvlc );
501 * Override default configuration with config file settings
503 if( !config_GetInt( p_libvlc, "ignore-config" ) )
504 config_LoadConfigFile( p_libvlc, NULL );
507 * Override configuration with command line settings
509 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
512 ShowConsole( false );
513 /* Pause the console because it's destroyed when we exit */
514 fprintf( stderr, "The command line options couldn't be loaded, check "
515 "that they are valid.\n" );
518 module_EndBank( p_libvlc );
523 * System specific configuration
525 system_Configure( p_libvlc, &i_argc, ppsz_argv );
527 /* FIXME: could be replaced by using Unix sockets */
529 dbus_threads_init_default();
531 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
533 /* Initialise D-Bus interface, check for other instances */
534 DBusConnection *p_conn = NULL;
535 DBusError dbus_error;
537 dbus_error_init( &dbus_error );
539 /* connect to the session bus */
540 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
543 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
544 dbus_error.message );
545 dbus_error_free( &dbus_error );
549 /* check if VLC is available on the bus
550 * if not: D-Bus control is not enabled on the other
551 * instance and we can't pass MRLs to it */
552 DBusMessage *p_test_msg = NULL;
553 DBusMessage *p_test_reply = NULL;
554 p_test_msg = dbus_message_new_method_call(
555 "org.mpris.vlc", "/",
556 "org.freedesktop.MediaPlayer", "Identity" );
557 /* block until a reply arrives */
558 p_test_reply = dbus_connection_send_with_reply_and_block(
559 p_conn, p_test_msg, -1, &dbus_error );
560 dbus_message_unref( p_test_msg );
561 if( p_test_reply == NULL )
563 dbus_error_free( &dbus_error );
564 msg_Dbg( p_libvlc, "No Media Player is running. "
565 "Continuing normally." );
570 DBusMessage* p_dbus_msg = NULL;
571 DBusMessageIter dbus_args;
572 DBusPendingCall* p_dbus_pending = NULL;
575 dbus_message_unref( p_test_reply );
576 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
578 for( i_input = optind;i_input < i_argc;i_input++ )
580 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
581 ppsz_argv[i_input] );
583 p_dbus_msg = dbus_message_new_method_call(
584 "org.mpris.vlc", "/TrackList",
585 "org.freedesktop.MediaPlayer", "AddTrack" );
587 if ( NULL == p_dbus_msg )
589 msg_Err( p_libvlc, "D-Bus problem" );
590 system_End( p_libvlc );
591 exit( VLC_ETIMEOUT );
595 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
596 if ( !dbus_message_iter_append_basic( &dbus_args,
597 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
599 msg_Err( p_libvlc, "Out of memory" );
600 dbus_message_unref( p_dbus_msg );
601 system_End( p_libvlc );
605 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
607 if ( !dbus_message_iter_append_basic( &dbus_args,
608 DBUS_TYPE_BOOLEAN, &b_play ) )
610 msg_Err( p_libvlc, "Out of memory" );
611 dbus_message_unref( p_dbus_msg );
612 system_End( p_libvlc );
616 /* send message and get a handle for a reply */
617 if ( !dbus_connection_send_with_reply ( p_conn,
618 p_dbus_msg, &p_dbus_pending, -1 ) )
620 msg_Err( p_libvlc, "D-Bus problem" );
621 dbus_message_unref( p_dbus_msg );
622 system_End( p_libvlc );
623 exit( VLC_ETIMEOUT );
626 if ( NULL == p_dbus_pending )
628 msg_Err( p_libvlc, "D-Bus problem" );
629 dbus_message_unref( p_dbus_msg );
630 system_End( p_libvlc );
631 exit( VLC_ETIMEOUT );
633 dbus_connection_flush( p_conn );
634 dbus_message_unref( p_dbus_msg );
635 /* block until we receive a reply */
636 dbus_pending_call_block( p_dbus_pending );
637 dbus_pending_call_unref( p_dbus_pending );
638 } /* processes all command line MRLs */
641 system_End( p_libvlc );
645 /* we unreference the connection when we've finished with it */
646 if( p_conn ) dbus_connection_unref( p_conn );
651 * Message queue options
654 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
655 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
658 var_Set( p_libvlc, "verbose", val );
660 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
661 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
663 if( p_libvlc->b_color )
664 p_libvlc->b_color = config_GetInt( p_libvlc, "color" ) > 0;
667 * Output messages that may still be in the queue
669 msg_Flush( p_libvlc );
671 if( !config_GetInt( p_libvlc, "fpu" ) )
672 cpu_flags &= ~CPU_CAPABILITY_FPU;
674 #if defined( __i386__ ) || defined( __x86_64__ )
675 if( !config_GetInt( p_libvlc, "mmx" ) )
676 cpu_flags &= ~CPU_CAPABILITY_MMX;
677 if( !config_GetInt( p_libvlc, "3dn" ) )
678 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
679 if( !config_GetInt( p_libvlc, "mmxext" ) )
680 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
681 if( !config_GetInt( p_libvlc, "sse" ) )
682 cpu_flags &= ~CPU_CAPABILITY_SSE;
683 if( !config_GetInt( p_libvlc, "sse2" ) )
684 cpu_flags &= ~CPU_CAPABILITY_SSE2;
686 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
687 if( !config_GetInt( p_libvlc, "altivec" ) )
688 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
691 #define PRINT_CAPABILITY( capability, string ) \
692 if( vlc_CPU() & capability ) \
694 strncat( p_capabilities, string " ", \
695 sizeof(p_capabilities) - strlen(p_capabilities) ); \
696 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
699 p_capabilities[0] = '\0';
700 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
701 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
702 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
703 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
704 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
705 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
706 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
707 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
708 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
709 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
710 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
713 * Choose the best memcpy module
715 p_libvlc->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
717 if( p_libvlc->pf_memcpy == NULL )
719 p_libvlc->pf_memcpy = memcpy;
722 if( p_libvlc->pf_memset == NULL )
724 p_libvlc->pf_memset = memset;
727 p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
728 p_libvlc->i_timers = 0;
729 p_libvlc->pp_timers = NULL;
732 p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
733 if( !p_libvlc->p_stats )
735 vlc_object_release( p_libvlc );
738 vlc_mutex_init( p_libvlc, &p_libvlc->p_stats->lock );
739 p_libvlc->p_stats_computer = NULL;
741 /* Init the array that holds every input item */
742 ARRAY_INIT( p_libvlc->input_items );
743 p_libvlc->i_last_input_id = 0;
746 * Initialize hotkey handling
748 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
749 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
750 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
751 /* Do a copy (we don't need to modify the strings) */
752 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
753 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
754 p_libvlc->p_hotkeys );
756 /* Initialize interaction */
757 p_libvlc->p_interaction = interaction_Init( p_libvlc );
759 /* Initialize playlist and get commandline files */
760 playlist_ThreadCreate( p_libvlc );
761 if( !p_libvlc->p_playlist )
763 msg_Err( p_libvlc, "playlist initialization failed" );
764 if( p_libvlc->p_memcpy_module != NULL )
766 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
768 module_EndBank( p_libvlc );
771 p_playlist = p_libvlc->p_playlist;
773 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
774 if( psz_modules && *psz_modules )
776 /* Add service discovery modules */
777 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
782 /* Initialize VLM if vlm-conf is specified */
783 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
784 if( psz_parser && *psz_parser )
786 p_libvlc->p_vlm = vlm_New( p_libvlc );
787 if( !p_libvlc->p_vlm )
788 msg_Err( p_libvlc, "VLM initialization failed" );
794 * Load background interfaces
796 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
797 psz_control = config_GetPsz( p_libvlc, "control" );
799 if( psz_modules && *psz_modules && psz_control && *psz_control )
801 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
802 strlen( psz_control ) + 1 );
803 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
805 else if( psz_control && *psz_control )
808 psz_modules = strdup( psz_control );
811 psz_parser = psz_modules;
812 while ( psz_parser && *psz_parser )
814 char *psz_module, *psz_temp;
815 psz_module = psz_parser;
816 psz_parser = strchr( psz_module, ':' );
822 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
825 sprintf( psz_temp, "%s,none", psz_module );
826 VLC_AddIntf( 0, psz_temp, false, false );
834 * Always load the hotkeys interface if it exists
836 VLC_AddIntf( 0, "hotkeys,none", false, false );
839 /* loads dbus control interface if in one-instance mode
840 * we do it only when playlist exists, because dbus module needs it */
841 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
842 VLC_AddIntf( 0, "dbus,none", false, false );
844 /* Prevents the power management daemon to suspend the computer
845 * when VLC is active */
846 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
847 VLC_AddIntf( 0, "inhibit,none", false, false );
851 * If needed, load the Xscreensaver interface
852 * Currently, only for X
854 #ifdef HAVE_X11_XLIB_H
855 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
857 VLC_AddIntf( 0, "screensaver,none", false, false );
861 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
863 VLC_AddIntf( 0, "logger,none", false, false );
866 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
868 const char *psz_logmode = "logmode=syslog";
869 libvlc_InternalAddIntf( p_libvlc, "logger,none", false, false,
874 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
876 VLC_AddIntf( 0, "showintf,none", false, false );
879 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
881 VLC_AddIntf( 0, "netsync,none", false, false );
885 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
887 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
890 char *psz_morecodecs;
891 asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs);
894 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
895 free( psz_morecodecs );
899 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
905 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
907 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
908 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
909 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
910 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
911 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
912 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
913 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
914 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
915 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
917 /* Create volume callback system. */
918 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
921 * Get input filenames given as commandline arguments
923 GetFilenames( p_libvlc, i_argc, ppsz_argv );
926 * Get --open argument
928 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
929 var_Get( p_libvlc, "open", &val );
930 if ( val.psz_string != NULL && *val.psz_string )
932 VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
933 PLAYLIST_INSERT, 0 );
935 free( val.psz_string );
941 * Cleanup a libvlc instance. The instance is not completely deallocated
942 * \param p_libvlc the instance to clean
944 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
946 intf_thread_t * p_intf = NULL;
947 vout_thread_t * p_vout = NULL;
948 aout_instance_t * p_aout = NULL;
950 /* Ask the interfaces to stop and destroy them */
951 msg_Dbg( p_libvlc, "removing all interfaces" );
952 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
954 intf_StopThread( p_intf );
955 vlc_object_detach( p_intf );
956 vlc_object_release( p_intf );
957 intf_Destroy( p_intf );
962 msg_Dbg( p_libvlc, "removing playlist" );
963 playlist_ThreadDestroy( p_libvlc->p_playlist );
965 /* Free video outputs */
966 msg_Dbg( p_libvlc, "removing all video outputs" );
967 while( (p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
969 vlc_object_detach( p_vout );
970 vlc_object_release( p_vout );
971 vout_Destroy( p_vout );
974 /* Free audio outputs */
975 msg_Dbg( p_libvlc, "removing all audio outputs" );
976 while( (p_aout = vlc_object_find( p_libvlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
978 vlc_object_detach( (vlc_object_t *)p_aout );
979 vlc_object_release( (vlc_object_t *)p_aout );
980 aout_Delete( p_aout );
984 sout_instance_t * p_sout;
986 p_sout = vlc_object_find( p_libvlc, VLC_OBJECT_SOUT, FIND_CHILD );
989 msg_Dbg( p_sout, "removing kept stream output" );
990 vlc_object_detach( (vlc_object_t*)p_sout );
991 vlc_object_release( (vlc_object_t*)p_sout );
992 sout_DeleteInstance( p_sout );
995 /* Destroy VLM if created in libvlc_InternalInit */
996 if( p_libvlc->p_vlm )
998 vlm_Delete( p_libvlc->p_vlm );
1002 /* Free interaction */
1003 msg_Dbg( p_libvlc, "removing interaction" );
1004 vlc_object_release( p_libvlc->p_interaction );
1006 stats_TimersDumpAll( p_libvlc );
1007 stats_TimersCleanAll( p_libvlc );
1010 announce_handler_t * p_announce;
1012 /* Free announce handler(s?) */
1013 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1016 msg_Dbg( p_libvlc, "removing announce handler" );
1017 vlc_object_detach( p_announce );
1018 vlc_object_release( p_announce );
1019 announce_HandlerDestroy( p_announce );
1023 bool b_clean = true;
1024 FOREACH_ARRAY( input_item_t *p_del, p_libvlc->input_items )
1025 msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1026 p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1030 ARRAY_RESET( p_libvlc->input_items );
1032 msg_Dbg( p_libvlc, "removing stats" );
1033 vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1034 FREENULL( p_libvlc->p_stats );
1040 * Destroy everything.
1041 * This function requests the running threads to finish, waits for their
1042 * termination, and destroys their structure.
1043 * It stops the thread systems: no instance can run after this has run
1044 * \param p_libvlc the instance to destroy
1045 * \param b_release whether we should do a release on the instance
1047 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, bool b_release )
1050 return VLC_EGENERIC;
1053 char* psz_pidfile = NULL;
1055 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
1057 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1058 if( psz_pidfile != NULL )
1060 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1061 if( unlink( psz_pidfile ) == -1 )
1063 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1067 free( psz_pidfile );
1071 if( p_libvlc->p_memcpy_module )
1073 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
1074 p_libvlc->p_memcpy_module = NULL;
1077 /* Free module bank. It is refcounted, so we call this each time */
1078 module_EndBank( p_libvlc );
1080 FREENULL( p_libvlc->psz_homedir );
1081 FREENULL( p_libvlc->psz_configdir );
1082 FREENULL( p_libvlc->psz_datadir );
1083 FREENULL( p_libvlc->psz_cachedir );
1084 FREENULL( p_libvlc->psz_configfile );
1085 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1086 p_libvlc->p_hotkeys );
1087 FREENULL( p_libvlc->p_hotkeys );
1089 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1092 if( i_instances == 0 )
1094 /* System specific cleaning code */
1095 system_End( p_libvlc );
1097 vlc_mutex_unlock( lock );
1099 msg_Flush( p_libvlc );
1100 msg_Destroy( p_libvlc );
1102 /* Destroy mutexes */
1103 vlc_mutex_destroy( &p_libvlc->config_lock );
1104 vlc_mutex_destroy( &p_libvlc->timer_lock );
1106 if( b_release ) vlc_object_release( p_libvlc );
1107 vlc_object_release( p_libvlc );
1110 /* Stop thread system: last one out please shut the door!
1111 * The number of initializations of the thread system is counted, we
1112 * can call this each time */
1113 vlc_threads_end( p_libvlc_global );
1119 * Add an interface plugin and run it
1121 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
1122 char const *psz_module,
1123 bool b_block, bool b_play,
1124 int i_options, const char *const *ppsz_options )
1127 intf_thread_t *p_intf = NULL;
1130 return VLC_EGENERIC;
1132 if( !psz_module ) /* requesting the default interface */
1134 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1135 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1136 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1137 free( psz_interface );
1141 if( p_libvlc_global->b_daemon && b_block && !psz_module )
1143 /* Daemon mode hack.
1144 * We prefer the dummy interface if none is specified. */
1145 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1146 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
1147 free( psz_interface );
1151 /* Try to create the interface */
1152 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
1153 i_options, ppsz_options );
1154 if( p_intf == NULL )
1156 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1158 return VLC_EGENERIC;
1161 /* Interface doesn't handle play on start so do it ourselves */
1162 if( !p_intf->b_play && b_play )
1163 playlist_Play( p_libvlc->p_playlist );
1165 /* Try to run the interface */
1166 p_intf->b_play = b_play;
1167 i_err = intf_RunThread( p_intf );
1168 if( i_err || p_intf->b_should_run_on_first_thread )
1170 vlc_object_detach( p_intf );
1171 intf_Destroy( p_intf );
1178 /* FIXME: should be moved to interface/interface.c */
1179 if( p_intf->pf_run )
1180 vlc_thread_join( p_intf );
1182 while( vlc_object_lock_and_wait( p_intf ) == 0 );
1184 vlc_object_detach( p_intf );
1185 intf_Destroy( p_intf );
1191 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1192 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1193 /*****************************************************************************
1194 * SetLanguage: set the interface language.
1195 *****************************************************************************
1196 * We set the LC_MESSAGES locale category for interface messages and buttons,
1197 * as well as the LC_CTYPE category for string sorting and possible wide
1198 * character support.
1199 *****************************************************************************/
1200 static void SetLanguage ( const char *psz_lang )
1203 /* I need that under Darwin, please check it doesn't disturb
1204 * other platforms. --Meuuh */
1205 setenv( "LANG", psz_lang, 1 );
1208 /* We set LC_ALL manually because it is the only way to set
1209 * the language at runtime under eg. Windows. Beware that this
1210 * makes the environment unconsistent when libvlc is unloaded and
1211 * should probably be moved to a safer place like vlc.c. */
1212 static char psz_lcall[20];
1213 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1214 psz_lcall[19] = '\0';
1215 putenv( psz_lcall );
1218 setlocale( LC_ALL, psz_lang );
1223 static inline int LoadMessages (void)
1225 #if defined( ENABLE_NLS ) \
1226 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1227 /* Specify where to find the locales for current domain */
1228 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1229 static const char psz_path[] = LOCALEDIR;
1231 char psz_path[1024];
1232 if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
1233 p_libvlc_global->psz_vlcpath, "locale")
1234 >= (int)sizeof (psz_path))
1238 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1240 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1241 " to directory %s\n", psz_path);
1245 /* LibVLC wants all messages in UTF-8.
1246 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1247 * and other functions that are not part of our text domain.
1249 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1251 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1253 // Unbinds the text domain to avoid broken encoding
1254 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1258 /* LibVLC does NOT set the default textdomain, since it is a library.
1259 * This could otherwise break programs using LibVLC (other than VLC).
1260 * textdomain (PACKAGE_NAME);
1266 /*****************************************************************************
1267 * GetFilenames: parse command line options which are not flags
1268 *****************************************************************************
1269 * Parse command line for input files as well as their associated options.
1270 * An option always follows its associated input and begins with a ":".
1271 *****************************************************************************/
1272 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1274 int i_opt, i_options;
1276 /* We assume that the remaining parameters are filenames
1277 * and their input options */
1278 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1282 /* Count the input options */
1283 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1289 /* TODO: write an internal function of this one, to avoid
1290 * unnecessary lookups. */
1292 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1293 ( i_options ? &ppsz_argv[i_opt + 1] :
1295 PLAYLIST_INSERT, 0 );
1301 /*****************************************************************************
1302 * Help: print program help
1303 *****************************************************************************
1304 * Print a short inline help. Message interface is initialized at this stage.
1305 *****************************************************************************/
1306 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1309 ShowConsole( true );
1312 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1314 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1315 Usage( p_this, "help" );
1316 Usage( p_this, "main" );
1318 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1320 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1321 Usage( p_this, NULL );
1323 else if( psz_help_name )
1325 Usage( p_this, psz_help_name );
1328 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1333 /*****************************************************************************
1334 * Usage: print module usage
1335 *****************************************************************************
1336 * Print a short inline help. Message interface is initialized at this stage.
1337 *****************************************************************************/
1338 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1340 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1341 /* short option ------' | | | | | | |
1342 * option name ------------' | | | | | |
1343 * <bra ---------------------' | | | | |
1344 * option type or "" ----------' | | | |
1345 * ket> -------------------------' | | |
1346 * padding spaces -----------------' | |
1347 * comment --------------------------' |
1348 * comment suffix ---------------------'
1350 * The purpose of having bra and ket is that we might i18n them as well.
1353 # define COL(x) "\033[" #x ";1m"
1354 # define RED COL(31)
1355 # define GREEN COL(32)
1356 # define YELLOW COL(33)
1357 # define BLUE COL(34)
1358 # define MAGENTA COL(35)
1359 # define CYAN COL(36)
1360 # define WHITE COL(0)
1361 # define GRAY "\033[0m"
1362 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1363 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1365 #define LINE_START 8
1366 #define PADDING_SPACES 25
1368 # define OPTION_VALUE_SEP "="
1370 # define OPTION_VALUE_SEP " "
1372 vlc_list_t *p_list = NULL;
1373 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1374 char psz_spaces_longtext[LINE_START+3];
1375 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1376 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1377 char psz_buffer[10000];
1380 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1381 int i_width_description = i_width + PADDING_SPACES - 1;
1382 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1383 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1384 bool b_description_hack;
1385 bool b_color = config_GetInt( p_this, "color" ) > 0;
1386 bool b_has_advanced = false;
1388 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1389 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1390 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1391 psz_spaces_longtext[LINE_START+2] = '\0';
1393 b_color = false; // don't put color control codes in a .txt file
1398 strcpy( psz_format, COLOR_FORMAT_STRING );
1399 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1403 strcpy( psz_format, FORMAT_STRING );
1404 strcpy( psz_format_bool, FORMAT_STRING );
1407 /* List all modules */
1408 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1410 /* Ugly hack to make sure that the help options always come first
1412 if( !psz_module_name )
1413 Usage( p_this, "help" );
1415 /* Enumerate the config for each module */
1416 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1419 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1420 module_config_t *p_item = NULL;
1421 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1423 if( psz_module_name && strcmp( psz_module_name,
1424 p_parser->psz_object_name ) )
1426 const char *const *pp_shortcut = p_parser->pp_shortcuts;
1427 while( *pp_shortcut )
1429 if( !strcmp( psz_module_name, *pp_shortcut ) )
1437 /* Ignore modules without config options */
1438 if( !p_parser->i_config_items )
1443 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1444 /* Ugly hack to make sure that the help options always come first
1446 if( !psz_module_name && b_help_module )
1449 /* Ignore modules with only advanced config options if requested */
1452 for( p_item = p_parser->p_config;
1456 if( (p_item->i_type & CONFIG_ITEM) &&
1457 !p_item->b_advanced ) break;
1461 /* Print name of module */
1462 if( strcmp( "main", p_parser->psz_object_name ) )
1465 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1466 p_parser->psz_longname );
1468 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1470 if( p_parser->psz_help )
1473 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1475 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1478 /* Print module options */
1479 for( p_item = p_parser->p_config;
1483 char *psz_text, *psz_spaces = psz_spaces_text;
1484 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1485 const char *psz_suf = "", *psz_prefix = NULL;
1489 /* Skip removed options */
1490 if( p_item->b_removed )
1494 /* Skip advanced options if requested */
1495 if( p_item->b_advanced && !b_advanced )
1497 b_has_advanced = true;
1501 switch( p_item->i_type )
1503 case CONFIG_HINT_CATEGORY:
1504 case CONFIG_HINT_USAGE:
1505 if( !strcmp( "main", p_parser->psz_object_name ) )
1508 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1511 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1513 if( b_description && p_item->psz_longtext )
1516 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1517 p_item->psz_longtext );
1519 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1523 case CONFIG_HINT_SUBCATEGORY:
1524 if( strcmp( "main", p_parser->psz_object_name ) )
1526 case CONFIG_SECTION:
1529 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1531 if( b_description && p_item->psz_longtext )
1532 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1533 p_item->psz_longtext );
1537 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1538 if( b_description && p_item->psz_longtext )
1539 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1543 case CONFIG_ITEM_STRING:
1544 case CONFIG_ITEM_FILE:
1545 case CONFIG_ITEM_DIRECTORY:
1546 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1547 case CONFIG_ITEM_MODULE_CAT:
1548 case CONFIG_ITEM_MODULE_LIST:
1549 case CONFIG_ITEM_MODULE_LIST_CAT:
1550 case CONFIG_ITEM_FONT:
1551 case CONFIG_ITEM_PASSWORD:
1552 psz_bra = OPTION_VALUE_SEP "<";
1553 psz_type = _("string");
1556 if( p_item->ppsz_list )
1558 psz_bra = OPTION_VALUE_SEP "{";
1559 psz_type = psz_buffer;
1560 psz_buffer[0] = '\0';
1561 for( i = 0; p_item->ppsz_list[i]; i++ )
1563 if( i ) strcat( psz_buffer, "," );
1564 strcat( psz_buffer, p_item->ppsz_list[i] );
1569 case CONFIG_ITEM_INTEGER:
1570 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1571 psz_bra = OPTION_VALUE_SEP "<";
1572 psz_type = _("integer");
1575 if( p_item->min.i || p_item->max.i )
1577 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1578 p_item->min.i, p_item->max.i );
1579 psz_type = psz_buffer;
1582 if( p_item->i_list )
1584 psz_bra = OPTION_VALUE_SEP "{";
1585 psz_type = psz_buffer;
1586 psz_buffer[0] = '\0';
1587 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1589 if( i ) strcat( psz_buffer, ", " );
1590 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1592 p_item->ppsz_list_text[i] );
1597 case CONFIG_ITEM_FLOAT:
1598 psz_bra = OPTION_VALUE_SEP "<";
1599 psz_type = _("float");
1601 if( p_item->min.f || p_item->max.f )
1603 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1604 p_item->min.f, p_item->max.f );
1605 psz_type = psz_buffer;
1608 case CONFIG_ITEM_BOOL:
1609 psz_bra = ""; psz_type = ""; psz_ket = "";
1610 if( !b_help_module )
1612 psz_suf = p_item->value.i ? _(" (default enabled)") :
1613 _(" (default disabled)");
1623 /* Add short option if any */
1624 if( p_item->i_short )
1626 sprintf( psz_short, "-%c,", p_item->i_short );
1630 strcpy( psz_short, " " );
1633 i = PADDING_SPACES - strlen( p_item->psz_name )
1634 - strlen( psz_bra ) - strlen( psz_type )
1635 - strlen( psz_ket ) - 1;
1637 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1639 psz_prefix = ", --no-";
1640 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1645 psz_spaces[0] = '\n';
1650 psz_spaces[i] = '\0';
1653 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1655 utf8_fprintf( stdout, psz_format_bool, psz_short,
1656 p_item->psz_name, psz_prefix, p_item->psz_name,
1657 psz_bra, psz_type, psz_ket, psz_spaces );
1661 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1662 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1665 psz_spaces[i] = ' ';
1667 /* We wrap the rest of the output */
1668 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1669 b_description_hack = b_description;
1672 psz_text = psz_buffer;
1673 i_cur_width = b_description && !b_description_hack
1674 ? i_width_description
1678 char *psz_parser, *psz_word;
1679 size_t i_end = strlen( psz_text );
1681 /* If the remaining text fits in a line, print it. */
1682 if( i_end <= i_cur_width )
1686 if( !b_description || b_description_hack )
1687 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1689 utf8_fprintf( stdout, "%s\n", psz_text );
1693 utf8_fprintf( stdout, "%s\n", psz_text );
1698 /* Otherwise, eat as many words as possible */
1699 psz_parser = psz_text;
1702 psz_word = psz_parser;
1703 psz_parser = strchr( psz_word, ' ' );
1704 /* If no space was found, we reached the end of the text
1705 * block; otherwise, we skip the space we just found. */
1706 psz_parser = psz_parser ? psz_parser + 1
1709 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1711 /* We cut a word in one of these cases:
1712 * - it's the only word in the line and it's too long.
1713 * - we used less than 80% of the width and the word we are
1714 * going to wrap is longer than 40% of the width, and even
1715 * if the word would have fit in the next line. */
1716 if( psz_word == psz_text
1717 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1718 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1720 char c = psz_text[i_cur_width];
1721 psz_text[i_cur_width] = '\0';
1724 if( !b_description || b_description_hack )
1725 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1726 psz_text, psz_spaces );
1728 utf8_fprintf( stdout, "%s\n%s",
1729 psz_text, psz_spaces );
1733 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1735 psz_text += i_cur_width;
1740 psz_word[-1] = '\0';
1743 if( !b_description || b_description_hack )
1744 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1745 psz_text, psz_spaces );
1747 utf8_fprintf( stdout, "%s\n%s",
1748 psz_text, psz_spaces );
1752 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1754 psz_text = psz_word;
1758 if( b_description_hack && p_item->psz_longtext )
1760 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1761 b_description_hack = false;
1762 psz_spaces = psz_spaces_longtext;
1763 utf8_fprintf( stdout, "%s", psz_spaces );
1769 if( b_has_advanced )
1772 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1773 _( "add --advanced to your command line to see advanced options."));
1775 utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
1776 _( "add --advanced to your command line to see advanced options."));
1779 /* Release the module list */
1780 vlc_list_release( p_list );
1783 /*****************************************************************************
1784 * ListModules: list the available modules with their description
1785 *****************************************************************************
1786 * Print a list of all available modules (builtins and plugins) and a short
1787 * description for each one.
1788 *****************************************************************************/
1789 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1791 vlc_list_t *p_list = NULL;
1792 module_t *p_parser = NULL;
1793 char psz_spaces[22];
1796 bool b_color = config_GetInt( p_this, "color" ) > 0;
1798 memset( psz_spaces, ' ', 22 );
1801 ShowConsole( true );
1804 /* List all modules */
1805 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1807 /* Enumerate each module */
1808 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1812 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1814 /* Nasty hack, but right now I'm too tired to think about a nice
1816 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1821 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1822 p_parser->psz_object_name,
1824 p_parser->psz_longname );
1826 utf8_fprintf( stdout, " %s%s %s\n",
1827 p_parser->psz_object_name,
1828 psz_spaces, p_parser->psz_longname );
1832 const char *const *pp_shortcut = p_parser->pp_shortcuts;
1833 while( *pp_shortcut )
1835 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1838 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1841 utf8_fprintf( stdout, " s %s\n",
1846 if( p_parser->psz_capability )
1849 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1850 p_parser->psz_capability,
1851 p_parser->i_score );
1853 utf8_fprintf( stdout, " c %s (%d)\n",
1854 p_parser->psz_capability,
1855 p_parser->i_score );
1859 psz_spaces[i] = ' ';
1862 vlc_list_release( p_list );
1864 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1869 /*****************************************************************************
1870 * Version: print complete program version
1871 *****************************************************************************
1872 * Print complete program version and build number.
1873 *****************************************************************************/
1874 static void Version( void )
1877 ShowConsole( true );
1880 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1881 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1882 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1883 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1884 if( strcmp( VLC_Changeset(), "exported" ) )
1885 utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1887 utf8_fprintf( stdout, LICENSE_MSG );
1889 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1894 /*****************************************************************************
1895 * ShowConsole: On Win32, create an output console for debug messages
1896 *****************************************************************************
1897 * This function is useful only on Win32.
1898 *****************************************************************************/
1900 static void ShowConsole( bool b_dofile )
1903 FILE *f_help = NULL;
1905 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1908 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1909 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1910 * page (e.g. CP437 or CP850). */
1911 SetConsoleOutputCP (GetACP ());
1912 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1914 freopen( "CONOUT$", "w", stderr );
1915 freopen( "CONIN$", "r", stdin );
1917 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1920 freopen( "vlc-help.txt", "wt", stdout );
1921 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1923 else freopen( "CONOUT$", "w", stdout );
1929 /*****************************************************************************
1930 * PauseConsole: On Win32, wait for a key press before closing the console
1931 *****************************************************************************
1932 * This function is useful only on Win32.
1933 *****************************************************************************/
1935 static void PauseConsole( void )
1939 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1941 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1949 /*****************************************************************************
1950 * ConsoleWidth: Return the console width in characters
1951 *****************************************************************************
1952 * We use the stty shell command to get the console width; if this fails or
1953 * if the width is less than 80, we default to 80.
1954 *****************************************************************************/
1955 static int ConsoleWidth( void )
1957 unsigned i_width = 80;
1960 FILE *file = popen( "stty size 2>/dev/null", "r" );
1963 if (fscanf (file, "%*u %u", &i_width) <= 0)
1968 CONSOLE_SCREEN_BUFFER_INFO buf;
1970 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
1971 i_width = buf.dwSize.X;
1977 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1978 vlc_value_t old_val, vlc_value_t new_val, void *param)
1980 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1985 if( new_val.i_int >= -1 )
1987 p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1992 /*****************************************************************************
1993 * InitDeviceValues: initialize device values
1994 *****************************************************************************
1995 * This function inits the dvd, vcd and cd-audio values
1996 *****************************************************************************/
1997 static void InitDeviceValues( libvlc_int_t *p_vlc )
2000 LibHalContext * ctx = NULL;
2002 char **devices = NULL;
2003 char *block_dev = NULL;
2007 DBusConnection *p_connection = NULL;
2010 ctx = libhal_ctx_new();
2012 dbus_error_init( &error );
2013 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2014 if( dbus_error_is_set( &error ) || !p_connection )
2016 dbus_error_free( &error );
2019 libhal_ctx_set_dbus_connection( ctx, p_connection );
2020 if( libhal_ctx_init( ctx, &error ) )
2022 ctx = hal_initialize( NULL, FALSE );
2028 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2030 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2033 for( i = 0; i < i_devices; i++ )
2036 if( !libhal_device_property_exists( ctx, devices[i],
2037 "storage.cdrom.dvd", NULL ) )
2039 if( !hal_device_property_exists( ctx, devices[ i ],
2040 "storage.cdrom.dvd" ) )
2046 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2047 "storage.cdrom.dvd", NULL );
2048 block_dev = libhal_device_get_property_string( ctx,
2049 devices[ i ], "block.device" , NULL );
2051 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2052 "storage.cdrom.dvd" );
2053 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2058 config_PutPsz( p_vlc, "dvd", block_dev );
2061 config_PutPsz( p_vlc, "vcd", block_dev );
2062 config_PutPsz( p_vlc, "cd-audio", block_dev );
2064 libhal_free_string( block_dev );
2066 hal_free_string( block_dev );
2070 libhal_free_string_array( devices );
2072 hal_free_string_array( devices );
2077 libhal_ctx_shutdown( ctx, NULL );
2078 dbus_connection_unref( p_connection );
2079 libhal_ctx_free( ctx );
2081 hal_shutdown( ctx );
2086 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2090 #endif /* HAVE_HAL */