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"
46 #include <errno.h> /* ENOMEM */
47 #include <stdio.h> /* sprintf() */
49 #include <stdlib.h> /* free() */
52 # include <netinet/in.h> /* BSD: struct in_addr */
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
62 # include "extras/getopt.h"
71 # include <libintl.h> /* bindtextdomain */
75 /* used for one-instance mode */
76 # include <dbus/dbus.h>
80 # include <hal/libhal.h>
83 #include <vlc_playlist.h>
84 #include <vlc_interface.h>
87 #include "audio_output/aout_internal.h"
89 #include <vlc_charset.h>
93 #include "playlist/playlist_internal.h"
98 # include <libkern/OSAtomic.h>
103 /*****************************************************************************
104 * The evil global variables. We handle them with care, don't worry.
105 *****************************************************************************/
106 static unsigned i_instances = 0;
109 static bool b_daemon = false;
117 * Atomically set the reference count to 1.
118 * @param p_gc reference counted object
119 * @param pf_destruct destruction calback
122 void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *))
124 /* There is no point in using the GC if there is no destructor... */
125 assert (pf_destruct);
126 p_gc->pf_destructor = pf_destruct;
129 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
130 __sync_synchronize ();
131 #elif defined (WIN32) && defined (__GNUC__)
132 #elif defined(__APPLE__)
135 /* Nobody else can possibly lock the spin - it's there as a barrier */
136 vlc_spin_init (&p_gc->spin);
137 vlc_spin_lock (&p_gc->spin);
138 vlc_spin_unlock (&p_gc->spin);
144 * Atomically increment the reference count.
145 * @param p_gc reference counted object
148 void *vlc_hold (gc_object_t * p_gc)
152 assert ((((uintptr_t)&p_gc->refs) & (sizeof (void *) - 1)) == 0); /* alignment */
154 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
155 refs = __sync_add_and_fetch (&p_gc->refs, 1);
156 #elif defined (WIN64)
157 refs = InterlockedIncrement64 (&p_gc->refs);
158 #elif defined (WIN32)
159 refs = InterlockedIncrement (&p_gc->refs);
160 #elif defined(__APPLE__)
161 refs = OSAtomicIncrement32Barrier((int*)&p_gc->refs);
163 vlc_spin_lock (&p_gc->spin);
165 vlc_spin_unlock (&p_gc->spin);
167 assert (refs != 1); /* there had to be a reference already */
172 * Atomically decrement the reference count and, if it reaches zero, destroy.
173 * @param p_gc reference counted object.
175 void vlc_release (gc_object_t *p_gc)
180 assert ((((uintptr_t)&p_gc->refs) & (sizeof (void *) - 1)) == 0); /* alignment */
182 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
183 refs = __sync_sub_and_fetch (&p_gc->refs, 1);
184 #elif defined (WIN64)
185 refs = InterlockedDecrement64 (&p_gc->refs);
186 #elif defined (WIN32)
187 refs = InterlockedDecrement (&p_gc->refs);
188 #elif defined(__APPLE__)
189 refs = OSAtomicDecrement32Barrier((int*)&p_gc->refs);
191 vlc_spin_lock (&p_gc->spin);
193 vlc_spin_unlock (&p_gc->spin);
196 assert (refs != (uintptr_t)(-1)); /* reference underflow?! */
200 #elif defined (WIN32) && defined (__GNUC__)
201 #elif defined(__APPLE__)
203 vlc_spin_destroy (&p_gc->spin);
205 p_gc->pf_destructor (p_gc);
209 /*****************************************************************************
211 *****************************************************************************/
212 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
213 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
214 static void SetLanguage ( char const * );
216 static inline int LoadMessages (void);
217 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
218 static void Help ( libvlc_int_t *, char const *psz_help_name );
219 static void Usage ( libvlc_int_t *, char const *psz_search );
220 static void ListModules ( libvlc_int_t *, bool );
221 static void Version ( void );
224 static void ShowConsole ( bool );
225 static void PauseConsole ( void );
227 static int ConsoleWidth ( void );
229 static void InitDeviceValues( libvlc_int_t * );
231 static vlc_mutex_t global_lock = VLC_STATIC_MUTEX;
234 * Allocate a libvlc instance, initialize global data if needed
235 * It also initializes the threading system
237 libvlc_int_t * libvlc_InternalCreate( void )
239 libvlc_int_t *p_libvlc;
241 char *psz_env = NULL;
243 /* Now that the thread system is initialized, we don't have much, but
244 * at least we have variables */
245 vlc_mutex_lock( &global_lock );
246 if( i_instances == 0 )
248 /* Guess what CPU we have */
249 cpu_flags = CPUCapabilities();
250 /* The module bank will be initialized later */
253 /* Allocate a libvlc instance object */
254 p_libvlc = __vlc_custom_create( NULL, sizeof (*priv),
255 VLC_OBJECT_GENERIC, "libvlc" );
256 if( p_libvlc != NULL )
258 vlc_mutex_unlock( &global_lock );
260 if( p_libvlc == NULL )
263 priv = libvlc_priv (p_libvlc);
264 priv->p_playlist = NULL;
265 priv->p_dialog_provider = NULL;
267 p_libvlc->psz_object_name = strdup( "libvlc" );
269 /* Initialize message queue */
270 msg_Create( p_libvlc );
272 /* Find verbosity from VLC_VERBOSE environment variable */
273 psz_env = getenv( "VLC_VERBOSE" );
274 if( psz_env != NULL )
275 priv->i_verbose = atoi( psz_env );
278 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
279 priv->b_color = isatty( 2 ); /* 2 is for stderr */
281 priv->b_color = false;
284 /* Initialize mutexes */
285 vlc_mutex_init( &priv->timer_lock );
286 vlc_mutex_init( &priv->config_lock );
287 vlc_cond_init( &priv->exiting );
293 * Initialize a libvlc instance
294 * This function initializes a previously allocated libvlc instance:
296 * - gettext initialization
297 * - message queue, module bank and playlist initialization
298 * - configuration and commandline parsing
300 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
301 const char *ppsz_argv[] )
303 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
304 char p_capabilities[200];
306 char * psz_modules = NULL;
307 char * psz_parser = NULL;
308 char * psz_control = NULL;
310 int i_ret = VLC_EEXIT;
311 playlist_t *p_playlist = NULL;
313 #if defined( ENABLE_NLS ) \
314 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
315 # if defined (WIN32) || defined (__APPLE__)
320 /* System specific initialization code */
321 system_Init( p_libvlc, &i_argc, ppsz_argv );
324 * Support for gettext
328 /* Initialize the module bank and load the configuration of the
329 * main module. We need to do this at this stage to be able to display
330 * a short help if required by the user. (short help == main module
332 module_InitBank( p_libvlc );
334 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
336 module_EndBank( p_libvlc, false );
340 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
341 /* Announce who we are - Do it only for first instance ? */
342 msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
343 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
344 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
345 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
347 /* Check for short help option */
348 if( config_GetInt( p_libvlc, "help" ) > 0 )
350 Help( p_libvlc, "help" );
352 i_ret = VLC_EEXITSUCCESS;
354 /* Check for version option */
355 else if( config_GetInt( p_libvlc, "version" ) > 0 )
359 i_ret = VLC_EEXITSUCCESS;
362 /* Set the config file stuff */
363 priv->psz_configfile = config_GetCustomConfigFile( p_libvlc );
365 /* Check for plugins cache options */
366 bool b_cache_delete = config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0;
368 /* Check for daemon mode */
370 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
373 char *psz_pidfile = NULL;
375 if( daemon( 1, 0) != 0 )
377 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
382 /* lets check if we need to write the pidfile */
383 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
384 if( psz_pidfile != NULL )
387 pid_t i_pid = getpid ();
388 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
389 i_pid, psz_pidfile );
390 pidfile = utf8_fopen( psz_pidfile,"w" );
391 if( pidfile != NULL )
393 utf8_fprintf( pidfile, "%d", (int)i_pid );
398 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
407 if( ( i_pid = fork() ) < 0 )
409 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
414 /* This is the parent, exit right now */
415 msg_Dbg( p_libvlc, "closing parent process" );
417 i_ret = VLC_EEXITSUCCESS;
421 /* We are the child */
422 msg_Dbg( p_libvlc, "daemon spawned" );
423 close( STDIN_FILENO );
424 close( STDOUT_FILENO );
425 close( STDERR_FILENO );
435 free( priv->psz_configfile );
436 module_EndBank( p_libvlc, false );
440 /* Check for translation config option */
441 #if defined( ENABLE_NLS ) \
442 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
443 # if defined (WIN32) || defined (__APPLE__)
444 /* This ain't really nice to have to reload the config here but it seems
445 * the only way to do it. */
447 if( !config_GetInt( p_libvlc, "ignore-config" ) )
448 config_LoadConfigFile( p_libvlc, "main" );
449 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
450 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
452 /* Check if the user specified a custom language */
453 psz_language = config_GetPsz( p_libvlc, "language" );
454 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
456 /* Reset the default domain */
457 SetLanguage( psz_language );
459 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
460 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
462 module_EndBank( p_libvlc, false );
463 module_InitBank( p_libvlc );
464 if( !config_GetInt( p_libvlc, "ignore-config" ) )
465 config_LoadConfigFile( p_libvlc, "main" );
466 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
467 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
469 free( psz_language );
474 * Load the builtins and plugins into the module_bank.
475 * We have to do it before config_Load*() because this also gets the
476 * list of configuration options exported by each module and loads their
479 module_LoadPlugins( p_libvlc, b_cache_delete );
480 if( p_libvlc->b_die )
486 module_t **list = module_list_get( &module_count );
487 module_list_free( list );
488 msg_Dbg( p_libvlc, "module bank initialized (%zu modules)", module_count );
490 /* Check for help on modules */
491 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
493 Help( p_libvlc, p_tmp );
496 i_ret = VLC_EEXITSUCCESS;
498 /* Check for full help option */
499 else if( config_GetInt( p_libvlc, "full-help" ) > 0 )
501 config_PutInt( p_libvlc, "advanced", 1);
502 config_PutInt( p_libvlc, "help-verbose", 1);
503 Help( p_libvlc, "full-help" );
505 i_ret = VLC_EEXITSUCCESS;
507 /* Check for long help option */
508 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
510 Help( p_libvlc, "longhelp" );
512 i_ret = VLC_EEXITSUCCESS;
514 /* Check for module list option */
515 else if( config_GetInt( p_libvlc, "list" ) > 0 )
517 ListModules( p_libvlc, false );
519 i_ret = VLC_EEXITSUCCESS;
521 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
523 ListModules( p_libvlc, true );
525 i_ret = VLC_EEXITSUCCESS;
528 /* Check for config file options */
529 if( !config_GetInt( p_libvlc, "ignore-config" ) )
531 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
533 config_ResetAll( p_libvlc );
534 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
535 config_SaveConfigFile( p_libvlc, NULL );
537 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
539 config_LoadConfigFile( p_libvlc, NULL );
540 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
541 config_SaveConfigFile( p_libvlc, NULL );
547 free( priv->psz_configfile );
548 module_EndBank( p_libvlc, true );
555 InitDeviceValues( p_libvlc );
558 * Override default configuration with config file settings
560 if( !config_GetInt( p_libvlc, "ignore-config" ) )
561 config_LoadConfigFile( p_libvlc, NULL );
564 * Override configuration with command line settings
566 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
569 ShowConsole( false );
570 /* Pause the console because it's destroyed when we exit */
571 fprintf( stderr, "The command line options couldn't be loaded, check "
572 "that they are valid.\n" );
575 free( priv->psz_configfile );
576 module_EndBank( p_libvlc, true );
579 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
582 * System specific configuration
584 system_Configure( p_libvlc, &i_argc, ppsz_argv );
586 /* FIXME: could be replaced by using Unix sockets */
588 dbus_threads_init_default();
590 if( config_GetInt( p_libvlc, "one-instance" ) > 0
591 || ( config_GetInt( p_libvlc, "one-instance-when-started-from-file" )
592 && config_GetInt( p_libvlc, "started-from-file" ) ) )
594 /* Initialise D-Bus interface, check for other instances */
595 DBusConnection *p_conn = NULL;
596 DBusError dbus_error;
598 dbus_error_init( &dbus_error );
600 /* connect to the session bus */
601 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
604 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
605 dbus_error.message );
606 dbus_error_free( &dbus_error );
610 /* check if VLC is available on the bus
611 * if not: D-Bus control is not enabled on the other
612 * instance and we can't pass MRLs to it */
613 DBusMessage *p_test_msg = NULL;
614 DBusMessage *p_test_reply = NULL;
615 p_test_msg = dbus_message_new_method_call(
616 "org.mpris.vlc", "/",
617 "org.freedesktop.MediaPlayer", "Identity" );
618 /* block until a reply arrives */
619 p_test_reply = dbus_connection_send_with_reply_and_block(
620 p_conn, p_test_msg, -1, &dbus_error );
621 dbus_message_unref( p_test_msg );
622 if( p_test_reply == NULL )
624 dbus_error_free( &dbus_error );
625 msg_Dbg( p_libvlc, "No Media Player is running. "
626 "Continuing normally." );
631 DBusMessage* p_dbus_msg = NULL;
632 DBusMessageIter dbus_args;
633 DBusPendingCall* p_dbus_pending = NULL;
636 dbus_message_unref( p_test_reply );
637 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
639 for( i_input = optind;i_input < i_argc;i_input++ )
641 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
642 ppsz_argv[i_input] );
644 p_dbus_msg = dbus_message_new_method_call(
645 "org.mpris.vlc", "/TrackList",
646 "org.freedesktop.MediaPlayer", "AddTrack" );
648 if ( NULL == p_dbus_msg )
650 msg_Err( p_libvlc, "D-Bus problem" );
651 system_End( p_libvlc );
652 exit( VLC_ETIMEOUT );
656 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
657 if ( !dbus_message_iter_append_basic( &dbus_args,
658 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
660 dbus_message_unref( p_dbus_msg );
661 system_End( p_libvlc );
665 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
667 if ( !dbus_message_iter_append_basic( &dbus_args,
668 DBUS_TYPE_BOOLEAN, &b_play ) )
670 dbus_message_unref( p_dbus_msg );
671 system_End( p_libvlc );
675 /* send message and get a handle for a reply */
676 if ( !dbus_connection_send_with_reply ( p_conn,
677 p_dbus_msg, &p_dbus_pending, -1 ) )
679 msg_Err( p_libvlc, "D-Bus problem" );
680 dbus_message_unref( p_dbus_msg );
681 system_End( p_libvlc );
682 exit( VLC_ETIMEOUT );
685 if ( NULL == p_dbus_pending )
687 msg_Err( p_libvlc, "D-Bus problem" );
688 dbus_message_unref( p_dbus_msg );
689 system_End( p_libvlc );
690 exit( VLC_ETIMEOUT );
692 dbus_connection_flush( p_conn );
693 dbus_message_unref( p_dbus_msg );
694 /* block until we receive a reply */
695 dbus_pending_call_block( p_dbus_pending );
696 dbus_pending_call_unref( p_dbus_pending );
697 } /* processes all command line MRLs */
700 system_End( p_libvlc );
704 /* we unreference the connection when we've finished with it */
705 if( p_conn ) dbus_connection_unref( p_conn );
710 * Message queue options
712 char * psz_verbose_objects = config_GetPsz( p_libvlc, "verbose-objects" );
713 if( psz_verbose_objects )
715 char * psz_object, * iter = psz_verbose_objects;
716 while( (psz_object = strsep( &iter, "," )) )
718 switch( psz_object[0] )
720 printf("%s\n", psz_object+1);
721 case '+': msg_EnableObjectPrinting(p_libvlc, psz_object+1); break;
722 case '-': msg_DisableObjectPrinting(p_libvlc, psz_object+1); break;
724 msg_Err( p_libvlc, "verbose-objects usage: \n"
725 "--verbose-objects=+printthatobject,"
726 "-dontprintthatone\n"
727 "(keyword 'all' to applies to all objects)");
728 free( psz_verbose_objects );
729 /* FIXME: leaks!!!! */
733 free( psz_verbose_objects );
736 /* Last chance to set the verbosity. Once we start interfaces and other
737 * threads, verbosity becomes read-only. */
738 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
739 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
741 var_SetInteger( p_libvlc, "verbose", -1 );
742 priv->i_verbose = -1;
746 priv->b_color = config_GetInt( p_libvlc, "color" ) > 0;
748 if( !config_GetInt( p_libvlc, "fpu" ) )
749 cpu_flags &= ~CPU_CAPABILITY_FPU;
751 #if defined( __i386__ ) || defined( __x86_64__ )
752 if( !config_GetInt( p_libvlc, "mmx" ) )
753 cpu_flags &= ~CPU_CAPABILITY_MMX;
754 if( !config_GetInt( p_libvlc, "3dn" ) )
755 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
756 if( !config_GetInt( p_libvlc, "mmxext" ) )
757 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
758 if( !config_GetInt( p_libvlc, "sse" ) )
759 cpu_flags &= ~CPU_CAPABILITY_SSE;
760 if( !config_GetInt( p_libvlc, "sse2" ) )
761 cpu_flags &= ~CPU_CAPABILITY_SSE2;
763 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
764 if( !config_GetInt( p_libvlc, "altivec" ) )
765 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
768 #define PRINT_CAPABILITY( capability, string ) \
769 if( vlc_CPU() & capability ) \
771 strncat( p_capabilities, string " ", \
772 sizeof(p_capabilities) - strlen(p_capabilities) ); \
773 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
776 p_capabilities[0] = '\0';
777 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
778 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
779 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
780 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
781 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
782 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
783 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
784 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
785 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
786 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
787 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
790 * Choose the best memcpy module
792 priv->p_memcpy_module = module_need( p_libvlc, "memcpy", "$memcpy", false );
794 priv->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
796 priv->pp_timers = NULL;
798 priv->i_last_input_id = 0; /* Not very safe, should be removed */
801 * Initialize hotkey handling
803 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
804 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
806 struct hotkey *p_keys =
807 malloc( (libvlc_actions_count + 1) * sizeof (*p_keys) );
809 /* Initialize from configuration */
810 for( size_t i = 0; i < libvlc_actions_count; i++ )
812 p_keys[i].psz_action = libvlc_actions[i].name;
813 p_keys[i].i_key = config_GetInt( p_libvlc,
814 libvlc_actions[i].name );
815 p_keys[i].i_action = libvlc_actions[i].value;
817 p_keys[libvlc_actions_count].psz_action = NULL;
818 p_keys[libvlc_actions_count].i_key = 0;
819 p_keys[libvlc_actions_count].i_action = 0;
820 p_libvlc->p_hotkeys = p_keys;
821 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
825 /* Initialize playlist and get commandline files */
826 p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
829 msg_Err( p_libvlc, "playlist initialization failed" );
830 if( priv->p_memcpy_module != NULL )
832 module_unneed( p_libvlc, priv->p_memcpy_module );
834 module_EndBank( p_libvlc, true );
835 free( priv->psz_configfile );
838 playlist_Activate( p_playlist );
839 vlc_object_attach( p_playlist, p_libvlc );
841 /* Add service discovery modules */
842 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
843 if( psz_modules && *psz_modules )
845 char *p = psz_modules, *m;
846 while( ( m = strsep( &p, " :," ) ) != NULL )
847 playlist_ServicesDiscoveryAdd( p_playlist, m );
852 /* Initialize VLM if vlm-conf is specified */
853 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
854 if( psz_parser && *psz_parser )
856 priv->p_vlm = vlm_New( p_libvlc );
858 msg_Err( p_libvlc, "VLM initialization failed" );
864 * Load background interfaces
866 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
867 psz_control = config_GetPsz( p_libvlc, "control" );
869 if( psz_modules && *psz_modules && psz_control && *psz_control )
872 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
875 psz_modules = psz_tmp;
878 else if( psz_control && *psz_control )
881 psz_modules = strdup( psz_control );
884 psz_parser = psz_modules;
885 while ( psz_parser && *psz_parser )
887 char *psz_module, *psz_temp;
888 psz_module = psz_parser;
889 psz_parser = strchr( psz_module, ':' );
895 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
897 libvlc_InternalAddIntf( p_libvlc, psz_temp );
905 * Always load the hotkeys interface if it exists
907 libvlc_InternalAddIntf( p_libvlc, "hotkeys,none" );
908 if( module_exists( "globalhotkeys" ) )
909 libvlc_InternalAddIntf( p_libvlc, "globalhotkeys,none" );
912 /* loads dbus control interface if in one-instance mode
913 * we do it only when playlist exists, because dbus module needs it */
914 if( config_GetInt( p_libvlc, "one-instance" ) > 0
915 || ( config_GetInt( p_libvlc, "one-instance-when-started-from-file" )
916 && config_GetInt( p_libvlc, "started-from-file" ) ) )
917 libvlc_InternalAddIntf( p_libvlc, "dbus,none" );
919 /* Prevents the power management daemon from suspending the system
920 * when VLC is active */
921 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
922 libvlc_InternalAddIntf( p_libvlc, "inhibit,none" );
926 * If needed, load the Xscreensaver interface
927 * Currently, only for X
929 #ifdef HAVE_X11_XLIB_H
930 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
932 libvlc_InternalAddIntf( p_libvlc, "screensaver,none" );
936 if( (config_GetInt( p_libvlc, "file-logging" ) > 0) &&
937 !config_GetInt( p_libvlc, "syslog" ) )
939 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
942 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
944 char *logmode = var_CreateGetString( p_libvlc, "logmode" );
945 var_SetString( p_libvlc, "logmode", "syslog" );
946 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
950 var_SetString( p_libvlc, "logmode", logmode );
954 var_Destroy( p_libvlc, "logmode" );
958 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
960 libvlc_InternalAddIntf( p_libvlc, "showintf,none" );
963 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
965 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
969 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
971 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
974 char *psz_morecodecs;
975 if( asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs) != -1 )
977 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
978 free( psz_morecodecs );
982 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
988 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
990 var_Create( p_libvlc, "drawable-xid", VLC_VAR_INTEGER );
991 var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_ADDRESS );
992 var_Create( p_libvlc, "drawable-agl", VLC_VAR_INTEGER );
993 var_Create( p_libvlc, "drawable-gl", VLC_VAR_INTEGER );
995 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
996 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
997 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
998 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
999 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
1000 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
1001 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
1002 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
1004 /* Create volume callback system. */
1005 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
1007 /* Create a variable for showing the interface (moved from playlist). */
1008 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
1009 var_SetBool( p_libvlc, "intf-show", true );
1011 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
1014 * Get input filenames given as commandline arguments
1016 GetFilenames( p_libvlc, i_argc, ppsz_argv );
1019 * Get --open argument
1021 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1022 var_Get( p_libvlc, "open", &val );
1023 if ( val.psz_string != NULL && *val.psz_string )
1025 playlist_t *p_playlist = pl_Hold( p_libvlc );
1026 playlist_AddExt( p_playlist, val.psz_string, NULL, PLAYLIST_INSERT, 0,
1027 -1, 0, NULL, 0, true, pl_Unlocked );
1028 pl_Release( p_libvlc );
1030 free( val.psz_string );
1036 * Cleanup a libvlc instance. The instance is not completely deallocated
1037 * \param p_libvlc the instance to clean
1039 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
1041 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1042 playlist_t *p_playlist = priv->p_playlist;
1044 /* Deactivate the playlist */
1045 msg_Dbg( p_libvlc, "deactivating the playlist" );
1046 playlist_Deactivate( p_playlist );
1048 /* Remove all services discovery */
1049 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
1050 playlist_ServicesDiscoveryKillAll( p_playlist );
1052 /* Ask the interfaces to stop and destroy them */
1053 msg_Dbg( p_libvlc, "removing all interfaces" );
1054 intf_thread_t *p_intf;
1055 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1057 intf_StopThread( p_intf );
1058 vlc_object_detach( p_intf );
1059 vlc_object_release( p_intf ); /* for intf_Create() */
1060 vlc_object_release( p_intf ); /* for vlc_object_find() */
1064 /* Destroy VLM if created in libvlc_InternalInit */
1067 vlm_Delete( priv->p_vlm );
1072 /* Any thread still running must not assume pl_Hold() succeeds. */
1073 msg_Dbg( p_libvlc, "removing playlist" );
1075 libvlc_priv(p_playlist->p_libvlc)->p_playlist = NULL;
1076 barrier(); /* FIXME is that correct ? */
1078 vlc_object_release( p_playlist );
1080 stats_TimersDumpAll( p_libvlc );
1081 stats_TimersCleanAll( p_libvlc );
1083 msg_Dbg( p_libvlc, "removing stats" );
1086 char* psz_pidfile = NULL;
1090 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1091 if( psz_pidfile != NULL )
1093 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1094 if( unlink( psz_pidfile ) == -1 )
1096 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1100 free( psz_pidfile );
1104 if( priv->p_memcpy_module )
1106 module_unneed( p_libvlc, priv->p_memcpy_module );
1107 priv->p_memcpy_module = NULL;
1110 /* Free module bank. It is refcounted, so we call this each time */
1111 module_EndBank( p_libvlc, true );
1113 FREENULL( priv->psz_configfile );
1114 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1115 (void *)p_libvlc->p_hotkeys );
1116 free( (void *)p_libvlc->p_hotkeys );
1120 * Destroy everything.
1121 * This function requests the running threads to finish, waits for their
1122 * termination, and destroys their structure.
1123 * It stops the thread systems: no instance can run after this has run
1124 * \param p_libvlc the instance to destroy
1126 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
1128 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
1130 vlc_mutex_lock( &global_lock );
1133 if( i_instances == 0 )
1135 /* System specific cleaning code */
1136 system_End( p_libvlc );
1138 vlc_mutex_unlock( &global_lock );
1140 msg_Destroy( p_libvlc );
1142 /* Destroy mutexes */
1143 vlc_cond_destroy( &priv->exiting );
1144 vlc_mutex_destroy( &priv->config_lock );
1145 vlc_mutex_destroy( &priv->timer_lock );
1147 #ifndef NDEBUG /* Hack to dump leaked objects tree */
1148 if( vlc_internals( p_libvlc )->i_refcount > 1 )
1149 while( vlc_internals( p_libvlc )->i_refcount > 0 )
1150 vlc_object_release( p_libvlc );
1153 assert( vlc_internals( p_libvlc )->i_refcount == 1 );
1154 vlc_object_release( p_libvlc );
1158 * Add an interface plugin and run it
1160 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
1163 intf_thread_t *p_intf = NULL;
1166 return VLC_EGENERIC;
1168 if( !psz_module ) /* requesting the default interface */
1170 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1171 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1175 /* Daemon mode hack.
1176 * We prefer the dummy interface if none is specified. */
1177 psz_module = "dummy";
1180 msg_Info( p_libvlc, "%s",
1181 _("Running vlc with the default interface. "
1182 "Use 'cvlc' to use vlc without interface.") );
1184 free( psz_interface );
1187 /* Try to create the interface */
1188 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
1189 if( p_intf == NULL )
1191 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1193 return VLC_EGENERIC;
1196 /* Try to run the interface */
1197 i_err = intf_RunThread( p_intf );
1200 vlc_object_detach( p_intf );
1201 vlc_object_release( p_intf );
1208 static vlc_mutex_t exit_lock = VLC_STATIC_MUTEX;
1211 * Waits until the LibVLC instance gets an exit signal. Normally, this happens
1212 * when the user "exits" an interface plugin.
1214 void libvlc_InternalWait( libvlc_int_t *p_libvlc )
1216 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
1218 vlc_mutex_lock( &exit_lock );
1219 while( vlc_object_alive( p_libvlc ) )
1220 vlc_cond_wait( &priv->exiting, &exit_lock );
1221 vlc_mutex_unlock( &exit_lock );
1225 * Posts an exit signal to LibVLC instance. This will normally initiate the
1226 * cleanup and destroy process. It should only be called on behalf of the user.
1228 void libvlc_Quit( libvlc_int_t *p_libvlc )
1230 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
1232 vlc_mutex_lock( &exit_lock );
1233 vlc_object_kill( p_libvlc );
1234 vlc_cond_signal( &priv->exiting );
1235 vlc_mutex_unlock( &exit_lock );
1238 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1239 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1240 /*****************************************************************************
1241 * SetLanguage: set the interface language.
1242 *****************************************************************************
1243 * We set the LC_MESSAGES locale category for interface messages and buttons,
1244 * as well as the LC_CTYPE category for string sorting and possible wide
1245 * character support.
1246 *****************************************************************************/
1247 static void SetLanguage ( const char *psz_lang )
1250 /* I need that under Darwin, please check it doesn't disturb
1251 * other platforms. --Meuuh */
1252 setenv( "LANG", psz_lang, 1 );
1255 /* We set LC_ALL manually because it is the only way to set
1256 * the language at runtime under eg. Windows. Beware that this
1257 * makes the environment unconsistent when libvlc is unloaded and
1258 * should probably be moved to a safer place like vlc.c. */
1259 static char psz_lcall[20];
1260 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1261 psz_lcall[19] = '\0';
1262 putenv( psz_lcall );
1265 setlocale( LC_ALL, psz_lang );
1270 static inline int LoadMessages (void)
1272 #if defined( ENABLE_NLS ) \
1273 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1274 /* Specify where to find the locales for current domain */
1275 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1276 static const char psz_path[] = LOCALEDIR;
1278 char psz_path[1024];
1279 if (snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "%s",
1280 config_GetDataDir(), "locale")
1281 >= (int)sizeof (psz_path))
1285 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1287 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1288 " to directory %s\n", psz_path);
1292 /* LibVLC wants all messages in UTF-8.
1293 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1294 * and other functions that are not part of our text domain.
1296 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1298 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1300 // Unbinds the text domain to avoid broken encoding
1301 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1305 /* LibVLC does NOT set the default textdomain, since it is a library.
1306 * This could otherwise break programs using LibVLC (other than VLC).
1307 * textdomain (PACKAGE_NAME);
1313 /*****************************************************************************
1314 * GetFilenames: parse command line options which are not flags
1315 *****************************************************************************
1316 * Parse command line for input files as well as their associated options.
1317 * An option always follows its associated input and begins with a ":".
1318 *****************************************************************************/
1319 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1321 int i_opt, i_options;
1323 /* We assume that the remaining parameters are filenames
1324 * and their input options */
1325 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1329 /* Count the input options */
1330 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1336 /* TODO: write an internal function of this one, to avoid
1337 * unnecessary lookups. */
1339 playlist_t *p_playlist = pl_Hold( p_vlc );
1340 playlist_AddExt( p_playlist, ppsz_argv[i_opt], NULL, PLAYLIST_INSERT,
1342 i_options, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ), VLC_INPUT_OPTION_TRUSTED,
1343 true, pl_Unlocked );
1344 pl_Release( p_vlc );
1350 /*****************************************************************************
1351 * Help: print program help
1352 *****************************************************************************
1353 * Print a short inline help. Message interface is initialized at this stage.
1354 *****************************************************************************/
1355 static inline void print_help_on_full_help( void )
1357 utf8_fprintf( stdout, "\n" );
1358 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
1361 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1364 ShowConsole( true );
1367 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1369 utf8_fprintf( stdout, vlc_usage, "vlc" );
1370 Usage( p_this, "=help" );
1371 Usage( p_this, "=main" );
1372 print_help_on_full_help();
1374 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1376 utf8_fprintf( stdout, vlc_usage, "vlc" );
1377 Usage( p_this, NULL );
1378 print_help_on_full_help();
1380 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
1382 utf8_fprintf( stdout, vlc_usage, "vlc" );
1383 Usage( p_this, NULL );
1385 else if( psz_help_name )
1387 Usage( p_this, psz_help_name );
1390 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1395 /*****************************************************************************
1396 * Usage: print module usage
1397 *****************************************************************************
1398 * Print a short inline help. Message interface is initialized at this stage.
1399 *****************************************************************************/
1400 # define COL(x) "\033[" #x ";1m"
1401 # define RED COL(31)
1402 # define GREEN COL(32)
1403 # define YELLOW COL(33)
1404 # define BLUE COL(34)
1405 # define MAGENTA COL(35)
1406 # define CYAN COL(36)
1407 # define WHITE COL(0)
1408 # define GRAY "\033[0m"
1409 static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
1411 if( !p_item ) return;
1414 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1416 if( b_description && p_item->psz_longtext )
1417 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1418 p_item->psz_longtext );
1422 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1423 if( b_description && p_item->psz_longtext )
1424 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1428 static void Usage( libvlc_int_t *p_this, char const *psz_search )
1430 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1431 /* short option ------' | | | | | | |
1432 * option name ------------' | | | | | |
1433 * <bra ---------------------' | | | | |
1434 * option type or "" ----------' | | | |
1435 * ket> -------------------------' | | |
1436 * padding spaces -----------------' | |
1437 * comment --------------------------' |
1438 * comment suffix ---------------------'
1440 * The purpose of having bra and ket is that we might i18n them as well.
1443 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1444 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1446 #define LINE_START 8
1447 #define PADDING_SPACES 25
1449 # define OPTION_VALUE_SEP "="
1451 # define OPTION_VALUE_SEP " "
1453 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1454 char psz_spaces_longtext[LINE_START+3];
1455 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1456 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1457 char psz_buffer[10000];
1459 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1460 int i_width_description = i_width + PADDING_SPACES - 1;
1461 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1462 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1463 bool b_description_hack;
1464 bool b_color = config_GetInt( p_this, "color" ) > 0;
1465 bool b_has_advanced = false;
1466 bool b_found = false;
1467 int i_only_advanced = 0; /* Number of modules ignored because they
1468 * only have advanced options */
1469 bool b_strict = psz_search && *psz_search == '=';
1470 if( b_strict ) psz_search++;
1472 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1473 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1474 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1475 psz_spaces_longtext[LINE_START+2] = '\0';
1479 b_color = false; // don't put color control codes in a .txt file
1483 strcpy( psz_format, COLOR_FORMAT_STRING );
1484 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1488 strcpy( psz_format, FORMAT_STRING );
1489 strcpy( psz_format_bool, FORMAT_STRING );
1492 /* List all modules */
1493 module_t **list = module_list_get (NULL);
1497 /* Ugly hack to make sure that the help options always come first
1500 Usage( p_this, "help" );
1502 /* Enumerate the config for each module */
1503 for (size_t i = 0; list[i]; i++)
1506 module_t *p_parser = list[i];
1507 module_config_t *p_item = NULL;
1508 module_config_t *p_section = NULL;
1509 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1512 ( b_strict ? strcmp( psz_search, p_parser->psz_object_name )
1513 : !strstr( p_parser->psz_object_name, psz_search ) ) )
1515 char *const *pp_shortcut = p_parser->pp_shortcuts;
1516 while( *pp_shortcut )
1518 if( b_strict ? !strcmp( psz_search, *pp_shortcut )
1519 : !!strstr( *pp_shortcut, psz_search ) )
1527 /* Ignore modules without config options */
1528 if( !p_parser->i_config_items )
1533 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1534 /* Ugly hack to make sure that the help options always come first
1536 if( !psz_search && b_help_module )
1539 /* Ignore modules with only advanced config options if requested */
1542 for( p_item = p_parser->p_config;
1546 if( (p_item->i_type & CONFIG_ITEM) &&
1547 !p_item->b_advanced && !p_item->b_removed ) break;
1550 if( p_item == p_end )
1559 /* Print name of module */
1560 if( strcmp( "main", p_parser->psz_object_name ) )
1563 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
1564 p_parser->psz_longname,
1565 p_parser->psz_object_name );
1567 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1569 if( p_parser->psz_help )
1572 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1574 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1577 /* Print module options */
1578 for( p_item = p_parser->p_config;
1582 char *psz_text, *psz_spaces = psz_spaces_text;
1583 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1584 const char *psz_suf = "", *psz_prefix = NULL;
1588 /* Skip removed options */
1589 if( p_item->b_removed )
1593 /* Skip advanced options if requested */
1594 if( p_item->b_advanced && !b_advanced )
1596 b_has_advanced = true;
1600 switch( p_item->i_type )
1602 case CONFIG_HINT_CATEGORY:
1603 case CONFIG_HINT_USAGE:
1604 if( !strcmp( "main", p_parser->psz_object_name ) )
1607 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1610 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1612 if( b_description && p_item->psz_longtext )
1615 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1616 p_item->psz_longtext );
1618 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1622 case CONFIG_HINT_SUBCATEGORY:
1623 if( strcmp( "main", p_parser->psz_object_name ) )
1625 case CONFIG_SECTION:
1629 case CONFIG_ITEM_STRING:
1630 case CONFIG_ITEM_FILE:
1631 case CONFIG_ITEM_DIRECTORY:
1632 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1633 case CONFIG_ITEM_MODULE_CAT:
1634 case CONFIG_ITEM_MODULE_LIST:
1635 case CONFIG_ITEM_MODULE_LIST_CAT:
1636 case CONFIG_ITEM_FONT:
1637 case CONFIG_ITEM_PASSWORD:
1638 print_help_section( p_section, b_color, b_description );
1640 psz_bra = OPTION_VALUE_SEP "<";
1641 psz_type = _("string");
1644 if( p_item->ppsz_list )
1646 psz_bra = OPTION_VALUE_SEP "{";
1647 psz_type = psz_buffer;
1648 psz_buffer[0] = '\0';
1649 for( i = 0; p_item->ppsz_list[i]; i++ )
1651 if( i ) strcat( psz_buffer, "," );
1652 strcat( psz_buffer, p_item->ppsz_list[i] );
1657 case CONFIG_ITEM_INTEGER:
1658 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1659 print_help_section( p_section, b_color, b_description );
1661 psz_bra = OPTION_VALUE_SEP "<";
1662 psz_type = _("integer");
1665 if( p_item->min.i || p_item->max.i )
1667 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1668 p_item->min.i, p_item->max.i );
1669 psz_type = psz_buffer;
1672 if( p_item->i_list )
1674 psz_bra = OPTION_VALUE_SEP "{";
1675 psz_type = psz_buffer;
1676 psz_buffer[0] = '\0';
1677 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1679 if( i ) strcat( psz_buffer, ", " );
1680 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1682 p_item->ppsz_list_text[i] );
1687 case CONFIG_ITEM_FLOAT:
1688 print_help_section( p_section, b_color, b_description );
1690 psz_bra = OPTION_VALUE_SEP "<";
1691 psz_type = _("float");
1693 if( p_item->min.f || p_item->max.f )
1695 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1696 p_item->min.f, p_item->max.f );
1697 psz_type = psz_buffer;
1700 case CONFIG_ITEM_BOOL:
1701 print_help_section( p_section, b_color, b_description );
1703 psz_bra = ""; psz_type = ""; psz_ket = "";
1704 if( !b_help_module )
1706 psz_suf = p_item->value.i ? _(" (default enabled)") :
1707 _(" (default disabled)");
1717 /* Add short option if any */
1718 if( p_item->i_short )
1720 sprintf( psz_short, "-%c,", p_item->i_short );
1724 strcpy( psz_short, " " );
1727 i = PADDING_SPACES - strlen( p_item->psz_name )
1728 - strlen( psz_bra ) - strlen( psz_type )
1729 - strlen( psz_ket ) - 1;
1731 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1733 psz_prefix = ", --no-";
1734 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1739 psz_spaces[0] = '\n';
1744 psz_spaces[i] = '\0';
1747 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1749 utf8_fprintf( stdout, psz_format_bool, psz_short,
1750 p_item->psz_name, psz_prefix, p_item->psz_name,
1751 psz_bra, psz_type, psz_ket, psz_spaces );
1755 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1756 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1759 psz_spaces[i] = ' ';
1761 /* We wrap the rest of the output */
1762 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1763 b_description_hack = b_description;
1766 psz_text = psz_buffer;
1767 i_cur_width = b_description && !b_description_hack
1768 ? i_width_description
1772 char *psz_parser, *psz_word;
1773 size_t i_end = strlen( psz_text );
1775 /* If the remaining text fits in a line, print it. */
1776 if( i_end <= i_cur_width )
1780 if( !b_description || b_description_hack )
1781 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1783 utf8_fprintf( stdout, "%s\n", psz_text );
1787 utf8_fprintf( stdout, "%s\n", psz_text );
1792 /* Otherwise, eat as many words as possible */
1793 psz_parser = psz_text;
1796 psz_word = psz_parser;
1797 psz_parser = strchr( psz_word, ' ' );
1798 /* If no space was found, we reached the end of the text
1799 * block; otherwise, we skip the space we just found. */
1800 psz_parser = psz_parser ? psz_parser + 1
1803 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1805 /* We cut a word in one of these cases:
1806 * - it's the only word in the line and it's too long.
1807 * - we used less than 80% of the width and the word we are
1808 * going to wrap is longer than 40% of the width, and even
1809 * if the word would have fit in the next line. */
1810 if( psz_word == psz_text
1811 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1812 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1814 char c = psz_text[i_cur_width];
1815 psz_text[i_cur_width] = '\0';
1818 if( !b_description || b_description_hack )
1819 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1820 psz_text, psz_spaces );
1822 utf8_fprintf( stdout, "%s\n%s",
1823 psz_text, psz_spaces );
1827 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1829 psz_text += i_cur_width;
1834 psz_word[-1] = '\0';
1837 if( !b_description || b_description_hack )
1838 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1839 psz_text, psz_spaces );
1841 utf8_fprintf( stdout, "%s\n%s",
1842 psz_text, psz_spaces );
1846 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1848 psz_text = psz_word;
1852 if( b_description_hack && p_item->psz_longtext )
1854 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1855 b_description_hack = false;
1856 psz_spaces = psz_spaces_longtext;
1857 utf8_fprintf( stdout, "%s", psz_spaces );
1863 if( b_has_advanced )
1866 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1867 _( "add --advanced to your command line to see advanced options."));
1869 utf8_fprintf( stdout, "\n%s %s\n", _( "Note:" ),
1870 _( "add --advanced to your command line to see advanced options."));
1873 if( i_only_advanced > 0 )
1877 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " ", _( "Note:" ) );
1878 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1882 utf8_fprintf( stdout, "\n%s ", _( "Note:" ) );
1883 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
1889 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY "\n",
1890 _( "No matching module found. Use --list or" \
1891 "--list-verbose to list available modules." ) );
1893 utf8_fprintf( stdout, "\n%s\n",
1894 _( "No matching module found. Use --list or" \
1895 "--list-verbose to list available modules." ) );
1898 /* Release the module list */
1899 module_list_free (list);
1902 /*****************************************************************************
1903 * ListModules: list the available modules with their description
1904 *****************************************************************************
1905 * Print a list of all available modules (builtins and plugins) and a short
1906 * description for each one.
1907 *****************************************************************************/
1908 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1911 char psz_spaces[22];
1913 bool b_color = config_GetInt( p_this, "color" ) > 0;
1915 memset( psz_spaces, ' ', 22 );
1918 ShowConsole( true );
1921 /* List all modules */
1922 module_t **list = module_list_get (NULL);
1924 /* Enumerate each module */
1925 for (size_t j = 0; (p_parser = list[j]) != NULL; j++)
1929 /* Nasty hack, but right now I'm too tired to think about a nice
1931 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1936 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1937 p_parser->psz_object_name,
1939 p_parser->psz_longname );
1941 utf8_fprintf( stdout, " %s%s %s\n",
1942 p_parser->psz_object_name,
1943 psz_spaces, p_parser->psz_longname );
1947 char *const *pp_shortcut = p_parser->pp_shortcuts;
1948 while( *pp_shortcut )
1950 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1953 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1956 utf8_fprintf( stdout, " s %s\n",
1961 if( p_parser->psz_capability )
1964 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1965 p_parser->psz_capability,
1966 p_parser->i_score );
1968 utf8_fprintf( stdout, " c %s (%d)\n",
1969 p_parser->psz_capability,
1970 p_parser->i_score );
1974 psz_spaces[i] = ' ';
1976 module_list_free (list);
1978 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1983 /*****************************************************************************
1984 * Version: print complete program version
1985 *****************************************************************************
1986 * Print complete program version and build number.
1987 *****************************************************************************/
1988 static void Version( void )
1991 ShowConsole( true );
1994 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1995 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1996 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1997 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1998 utf8_fprintf( stdout, "%s", LICENSE_MSG );
2000 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2005 /*****************************************************************************
2006 * ShowConsole: On Win32, create an output console for debug messages
2007 *****************************************************************************
2008 * This function is useful only on Win32.
2009 *****************************************************************************/
2011 static void ShowConsole( bool b_dofile )
2014 FILE *f_help = NULL;
2016 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2019 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
2020 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
2021 * page (e.g. CP437 or CP850). */
2022 SetConsoleOutputCP (GetACP ());
2023 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
2025 freopen( "CONOUT$", "w", stderr );
2026 freopen( "CONIN$", "r", stdin );
2028 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
2031 freopen( "vlc-help.txt", "wt", stdout );
2032 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
2034 else freopen( "CONOUT$", "w", stdout );
2040 /*****************************************************************************
2041 * PauseConsole: On Win32, wait for a key press before closing the console
2042 *****************************************************************************
2043 * This function is useful only on Win32.
2044 *****************************************************************************/
2046 static void PauseConsole( void )
2050 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2052 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2060 /*****************************************************************************
2061 * ConsoleWidth: Return the console width in characters
2062 *****************************************************************************
2063 * We use the stty shell command to get the console width; if this fails or
2064 * if the width is less than 80, we default to 80.
2065 *****************************************************************************/
2066 static int ConsoleWidth( void )
2068 unsigned i_width = 80;
2071 FILE *file = popen( "stty size 2>/dev/null", "r" );
2074 if (fscanf (file, "%*u %u", &i_width) <= 0)
2078 #elif !defined (UNDER_CE)
2079 CONSOLE_SCREEN_BUFFER_INFO buf;
2081 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
2082 i_width = buf.dwSize.X;
2088 /*****************************************************************************
2089 * InitDeviceValues: initialize device values
2090 *****************************************************************************
2091 * This function inits the dvd, vcd and cd-audio values
2092 *****************************************************************************/
2093 static void InitDeviceValues( libvlc_int_t *p_vlc )
2096 LibHalContext * ctx = NULL;
2098 char **devices = NULL;
2099 char *block_dev = NULL;
2102 DBusConnection *p_connection = NULL;
2105 ctx = libhal_ctx_new();
2107 dbus_error_init( &error );
2108 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2109 if( dbus_error_is_set( &error ) || !p_connection )
2111 libhal_ctx_free( ctx );
2112 dbus_error_free( &error );
2115 libhal_ctx_set_dbus_connection( ctx, p_connection );
2116 if( libhal_ctx_init( ctx, &error ) )
2118 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2120 for( i = 0; i < i_devices; i++ )
2122 if( !libhal_device_property_exists( ctx, devices[i],
2123 "storage.cdrom.dvd", NULL ) )
2127 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2128 "storage.cdrom.dvd", NULL );
2129 block_dev = libhal_device_get_property_string( ctx,
2130 devices[ i ], "block.device" , NULL );
2133 config_PutPsz( p_vlc, "dvd", block_dev );
2136 config_PutPsz( p_vlc, "vcd", block_dev );
2137 config_PutPsz( p_vlc, "cd-audio", block_dev );
2138 libhal_free_string( block_dev );
2140 libhal_free_string_array( devices );
2142 libhal_ctx_shutdown( ctx, NULL );
2143 dbus_connection_unref( p_connection );
2144 libhal_ctx_free( ctx );
2148 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2152 #endif /* HAVE_HAL */
2155 #include <vlc_avcodec.h>
2157 void vlc_avcodec_mutex (bool acquire)
2159 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
2162 vlc_mutex_lock (&lock);
2164 vlc_mutex_unlock (&lock);