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 /*****************************************************************************
33 * Pretend we are a builtin module
34 *****************************************************************************/
35 #define MODULE_NAME main
36 #define MODULE_PATH main
39 /*****************************************************************************
41 *****************************************************************************/
43 #include "control/libvlc_internal.h"
44 #include <vlc_input.h>
46 #include "misc/modules.h"
47 #include "misc/configuration.h"
49 #include <errno.h> /* ENOMEM */
50 #include <stdio.h> /* sprintf() */
51 #include <string.h> /* strerror() */
52 #include <stdlib.h> /* free() */
55 # include <netinet/in.h> /* BSD: struct in_addr */
60 #elif defined( WIN32 ) && !defined( UNDER_CE )
64 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
65 # include "extras/getopt.h"
73 # include <dbus/dbus.h>
75 /* this is also defined in modules/control/dbus.h */
76 /* names registered on the session bus */
77 #define VLC_DBUS_SERVICE "org.videolan.vlc"
78 #define VLC_DBUS_INTERFACE "org.videolan.vlc"
79 #define VLC_DBUS_OBJECT_PATH "/org/videolan/vlc"
83 # include <hal/libhal.h>
86 #include "vlc_os_specific.h"
88 #include <vlc_playlist.h>
89 #include <vlc_interface.h>
92 #include "audio_output/aout_internal.h"
97 #include "stream_output/stream_output.h"
99 #include <vlc_charset.h>
103 #include "playlist/playlist_internal.h"
105 /*****************************************************************************
106 * The evil global variable. We handle it with care, don't worry.
107 *****************************************************************************/
108 static libvlc_global_data_t libvlc_global;
109 static libvlc_global_data_t * p_libvlc_global;
110 static libvlc_int_t * p_static_vlc;
111 static volatile unsigned int i_instances = 0;
113 /*****************************************************************************
115 *****************************************************************************/
116 void LocaleInit( vlc_object_t * );
117 void LocaleDeinit( void );
118 static void SetLanguage ( char const * );
119 static int GetFilenames ( libvlc_int_t *, int, char *[] );
120 static void Help ( libvlc_int_t *, char const *psz_help_name );
121 static void Usage ( libvlc_int_t *, char const *psz_module_name );
122 static void ListModules ( libvlc_int_t * );
123 static void Version ( void );
126 static void ShowConsole ( vlc_bool_t );
127 static void PauseConsole ( void );
129 static int ConsoleWidth ( void );
131 static int VerboseCallback( vlc_object_t *, char const *,
132 vlc_value_t, vlc_value_t, void * );
134 static void InitDeviceValues( libvlc_int_t * );
136 /* Refs misc/cpu.c */
137 uint32_t CPUCapabilities( void );
139 /*****************************************************************************
140 * vlc_current_object: return the current object.
141 *****************************************************************************
142 * If i_object is non-zero, return the corresponding object. Otherwise,
143 * return the statically allocated p_vlc object.
144 *****************************************************************************/
145 libvlc_int_t * vlc_current_object( int i_object )
149 return vlc_object_get( p_libvlc_global, i_object );
157 * Allocate a libvlc instance, initialize global data if needed
158 * It also initializes the threading system
160 libvlc_int_t * libvlc_InternalCreate( void )
163 libvlc_int_t * p_libvlc = NULL;
167 /* &libvlc_global never changes,
168 * so we can safely call this multiple times. */
169 p_libvlc_global = &libvlc_global;
171 /* vlc_threads_init *must* be the first internal call! No other call is
172 * allowed before the thread system has been initialized. */
173 i_ret = vlc_threads_init( p_libvlc_global );
174 if( i_ret < 0 ) return NULL;
176 /* Now that the thread system is initialized, we don't have much, but
177 * at least we have var_Create */
178 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
179 var_Get( p_libvlc_global, "libvlc", &lockval );
180 vlc_mutex_lock( lockval.p_address );
184 if( !libvlc_global.b_ready )
186 /* Guess what CPU we have */
187 libvlc_global.i_cpu = CPUCapabilities();
188 /* The module bank will be initialized later */
189 libvlc_global.p_module_bank = NULL;
191 libvlc_global.b_ready = VLC_TRUE;
193 vlc_mutex_unlock( lockval.p_address );
194 var_Destroy( p_libvlc_global, "libvlc" );
196 /* Allocate a libvlc instance object */
197 p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
198 if( p_libvlc == NULL ) { i_instances--; return NULL; }
199 p_libvlc->thread_id = 0;
200 p_libvlc->p_playlist = NULL;
201 p_libvlc->psz_object_name = "libvlc";
203 /* Initialize message queue */
204 msg_Create( p_libvlc );
205 /* Announce who we are - Do it only for first instance ? */
206 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
207 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
209 /* Find verbosity from VLC_VERBOSE environment variable */
210 psz_env = getenv( "VLC_VERBOSE" );
211 p_libvlc->i_verbose = psz_env ? atoi( psz_env ) : -1;
213 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
214 p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
216 p_libvlc->b_color = VLC_FALSE;
219 /* Initialize mutexes */
220 vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
222 vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
223 vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
225 /* Fake attachment */
226 p_libvlc->b_attached = VLC_TRUE;
227 /* Store data for the non-reentrant API */
228 p_static_vlc = p_libvlc;
234 * D-Bus callback needed in libvlc_InternalInit()
237 /* Handling of messages received on / object */
238 static DBusHandlerResult handle_root
239 ( DBusConnection *p_conn, DBusMessage *p_from, void *p_data )
241 DBusMessage* p_msg = dbus_message_new_method_return( p_from );
242 if( !p_msg ) return DBUS_HANDLER_RESULT_NEED_MEMORY;
244 DBusMessageIter args;
245 dbus_message_iter_init_append( p_msg, &args );
247 char *p_root = malloc( strlen( "<node name='/'></node>" ) );
248 if (!p_root ) return DBUS_HANDLER_RESULT_NEED_MEMORY;
249 sprintf( p_root, "<node name='/'></node>" );
251 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &p_root ) )
252 return DBUS_HANDLER_RESULT_NEED_MEMORY;
254 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
255 return DBUS_HANDLER_RESULT_NEED_MEMORY;
256 dbus_connection_flush( p_conn );
257 dbus_message_unref( p_msg );
258 return DBUS_HANDLER_RESULT_HANDLED;
260 /* vtable passed to dbus_connection_register_object_path() */
261 static DBusObjectPathVTable vlc_dbus_root_vtable = {
262 NULL, handle_root, NULL, NULL, NULL, NULL
268 * Initialize a libvlc instance
269 * This function initializes a previously allocated libvlc instance:
271 * - gettext initialization
272 * - message queue, module bank and playlist initialization
273 * - configuration and commandline parsing
275 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc, char *ppsz_argv[] )
277 char p_capabilities[200];
282 vlc_bool_t b_exit = VLC_FALSE;
283 int i_ret = VLC_EEXIT;
284 module_t *p_help_module;
285 playlist_t *p_playlist;
287 #if defined( ENABLE_NLS ) \
288 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
289 # if defined (WIN32) || defined (__APPLE__)
294 /* System specific initialization code */
295 system_Init( p_libvlc, &i_argc, ppsz_argv );
297 /* Get the executable name (similar to the basename command) */
300 p_libvlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
303 if( *p_tmp == '/' ) p_libvlc->psz_object_name = ++p_tmp;
309 p_libvlc->psz_object_name = "vlc";
313 * Support for gettext
318 * Global iconv, must be done after setlocale()
319 * so that vlc_current_charset() works.
321 LocaleInit( (vlc_object_t *)p_libvlc );
323 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
324 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
326 /* Initialize the module bank and load the configuration of the
327 * main module. We need to do this at this stage to be able to display
328 * a short help if required by the user. (short help == main module
330 module_InitBank( p_libvlc );
332 /* Hack: insert the help module here */
333 p_help_module = vlc_object_create( p_libvlc, VLC_OBJECT_MODULE );
334 if( p_help_module == NULL )
336 module_EndBank( p_libvlc );
339 p_help_module->psz_object_name = "help";
340 p_help_module->psz_longname = N_("Help options");
341 config_Duplicate( p_help_module, p_help_config,
342 sizeof (p_help_config) / sizeof (p_help_config[0]) );
343 vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
346 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE ) )
348 vlc_object_detach( p_help_module );
349 config_Free( p_help_module );
350 vlc_object_destroy( p_help_module );
351 module_EndBank( p_libvlc );
355 /* Check for short help option */
356 if( config_GetInt( p_libvlc, "help" ) )
358 Help( p_libvlc, "help" );
360 i_ret = VLC_EEXITSUCCESS;
362 /* Check for version option */
363 else if( config_GetInt( p_libvlc, "version" ) )
367 i_ret = VLC_EEXITSUCCESS;
370 /* Set the config file stuff */
371 p_libvlc->psz_homedir = config_GetHomeDir();
372 p_libvlc->psz_userdir = config_GetUserDir();
373 if( p_libvlc->psz_userdir == NULL )
374 p_libvlc->psz_userdir = strdup(p_libvlc->psz_homedir);
375 p_libvlc->psz_configfile = config_GetPsz( p_libvlc, "config" );
376 if( p_libvlc->psz_configfile != NULL && p_libvlc->psz_configfile[0] == '~'
377 && p_libvlc->psz_configfile[1] == '/' )
379 char *psz = malloc( strlen(p_libvlc->psz_userdir)
380 + strlen(p_libvlc->psz_configfile) );
381 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
382 sprintf( psz, "%s/%s", p_libvlc->psz_userdir,
383 p_libvlc->psz_configfile + 2 );
384 free( p_libvlc->psz_configfile );
385 p_libvlc->psz_configfile = psz;
388 /* Check for plugins cache options */
389 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) )
391 libvlc_global.p_module_bank->b_cache_delete = VLC_TRUE;
394 /* Hack: remove the help module here */
395 vlc_object_detach( p_help_module );
398 /* Will be re-done properly later on */
399 p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
401 /* Check for daemon mode */
403 if( config_GetInt( p_libvlc, "daemon" ) )
406 if( daemon( 1, 0) != 0 )
408 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
412 p_libvlc->p_libvlc_global->b_daemon = VLC_TRUE;
414 /* lets check if we need to write the pidfile */
415 char * psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
417 if( psz_pidfile != NULL )
420 pid_t i_pid = getpid ();
421 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
422 i_pid, psz_pidfile );
423 pidfile = utf8_fopen( psz_pidfile,"w" );
424 if( pidfile != NULL )
426 utf8_fprintf( pidfile, "%d", (int)i_pid );
431 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%s)",
432 psz_pidfile, strerror(errno) );
440 if( ( i_pid = fork() ) < 0 )
442 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
447 /* This is the parent, exit right now */
448 msg_Dbg( p_libvlc, "closing parent process" );
450 i_ret = VLC_EEXITSUCCESS;
454 /* We are the child */
455 msg_Dbg( p_libvlc, "daemon spawned" );
456 close( STDIN_FILENO );
457 close( STDOUT_FILENO );
458 close( STDERR_FILENO );
460 p_libvlc->p_libvlc_global->b_daemon = VLC_TRUE;
468 config_Free( p_help_module );
469 vlc_object_destroy( p_help_module );
470 module_EndBank( p_libvlc );
474 /* Check for translation config option */
475 #if defined( ENABLE_NLS ) \
476 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
477 # if defined (WIN32) || defined (__APPLE__)
478 /* This ain't really nice to have to reload the config here but it seems
479 * the only way to do it. */
480 config_LoadConfigFile( p_libvlc, "main" );
481 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
483 /* Check if the user specified a custom language */
484 psz_language = config_GetPsz( p_libvlc, "language" );
485 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
487 vlc_bool_t b_cache_delete = libvlc_global.p_module_bank->b_cache_delete;
489 /* Reset the default domain */
490 SetLanguage( psz_language );
492 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
493 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
495 module_EndBank( p_libvlc );
496 module_InitBank( p_libvlc );
497 config_LoadConfigFile( p_libvlc, "main" );
498 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
499 libvlc_global.p_module_bank->b_cache_delete = b_cache_delete;
501 if( psz_language ) free( psz_language );
506 * Load the builtins and plugins into the module_bank.
507 * We have to do it before config_Load*() because this also gets the
508 * list of configuration options exported by each module and loads their
511 module_LoadBuiltins( p_libvlc );
512 module_LoadPlugins( p_libvlc );
513 if( p_libvlc->b_die )
518 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
519 libvlc_global.p_module_bank->i_children );
521 /* Hack: insert the help module here */
522 vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
525 /* Check for help on modules */
526 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
528 Help( p_libvlc, p_tmp );
531 i_ret = VLC_EEXITSUCCESS;
533 /* Check for long help option */
534 else if( config_GetInt( p_libvlc, "longhelp" ) )
536 Help( p_libvlc, "longhelp" );
538 i_ret = VLC_EEXITSUCCESS;
540 /* Check for module list option */
541 else if( config_GetInt( p_libvlc, "list" ) )
543 ListModules( p_libvlc );
545 i_ret = VLC_EEXITSUCCESS;
548 /* Check for config file options */
549 if( config_GetInt( p_libvlc, "reset-config" ) )
551 vlc_object_detach( p_help_module );
552 config_ResetAll( p_libvlc );
553 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
554 config_SaveConfigFile( p_libvlc, NULL );
555 vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
557 if( config_GetInt( p_libvlc, "save-config" ) )
559 vlc_object_detach( p_help_module );
560 config_LoadConfigFile( p_libvlc, NULL );
561 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
562 config_SaveConfigFile( p_libvlc, NULL );
563 vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
566 /* Hack: remove the help module here */
567 vlc_object_detach( p_help_module );
572 config_Free( p_help_module );
573 vlc_object_destroy( p_help_module );
574 module_EndBank( p_libvlc );
581 InitDeviceValues( p_libvlc );
584 * Override default configuration with config file settings
586 config_LoadConfigFile( p_libvlc, NULL );
588 /* Hack: insert the help module here */
589 vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
593 * Override configuration with command line settings
595 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_FALSE ) )
598 ShowConsole( VLC_FALSE );
599 /* Pause the console because it's destroyed when we exit */
600 fprintf( stderr, "The command line options couldn't be loaded, check "
601 "that they are valid.\n" );
604 vlc_object_detach( p_help_module );
605 config_Free( p_help_module );
606 vlc_object_destroy( p_help_module );
607 module_EndBank( p_libvlc );
611 /* Hack: remove the help module here */
612 vlc_object_detach( p_help_module );
613 config_Free( p_help_module );
614 vlc_object_destroy( p_help_module );
618 * System specific configuration
620 system_Configure( p_libvlc, &i_argc, ppsz_argv );
622 /* FIXME: could be replaced by using Unix sockets */
624 /* Initialise D-Bus interface, check for other instances */
625 DBusConnection *p_conn;
626 DBusError dbus_error;
629 dbus_threads_init_default();
630 dbus_error_init( &dbus_error );
632 /* connect to the session bus */
633 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
636 msg_Err( p_libvlc, "Failed to connect to the D-Bus session daemon: %s",
637 dbus_error.message );
638 dbus_error_free( &dbus_error );
642 /* we request the service org.videolan.vlc */
643 i_dbus_service = dbus_bus_request_name( p_conn, VLC_DBUS_SERVICE, 0,
645 if( dbus_error_is_set( &dbus_error ) )
647 msg_Err( p_libvlc, "Error requesting %s service: %s\n",
648 VLC_DBUS_SERVICE, dbus_error.message );
649 dbus_error_free( &dbus_error );
653 if( i_dbus_service != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
654 { /* the name is already registered by another instance of vlc */
655 if( config_GetInt( p_libvlc, "one-instance" ) )
657 /* check if /org/videolan/vlc exists
658 * if not: D-Bus control is not enabled on the other
659 * instance and we can't pass MRLs to it */
660 DBusMessage *p_test_msg, *p_test_reply;
661 p_test_msg = dbus_message_new_method_call(
662 VLC_DBUS_SERVICE, VLC_DBUS_OBJECT_PATH,
663 VLC_DBUS_INTERFACE, "Nothing" );
664 /* block unti a reply arrives */
665 p_test_reply = dbus_connection_send_with_reply_and_block(
666 p_conn, p_test_msg, -1, &dbus_error );
667 dbus_message_unref( p_test_msg );
668 if( p_test_reply == NULL )
670 dbus_error_free( &dbus_error );
671 msg_Err( p_libvlc, "one instance mode has been "
672 "set but D-Bus control interface is not "
673 "enabled. Enable it and restart vlc, or "
674 "disable one instance mode." );
678 dbus_message_unref( p_test_reply );
680 "Another vlc instance exists: will now exit");
683 DBusMessage* p_dbus_msg;
684 DBusMessageIter dbus_args;
685 DBusPendingCall* p_dbus_pending;
688 for( i_input = optind;i_input < i_argc;i_input++ )
690 msg_Dbg( p_libvlc, "Give %s to other vlc\n",
691 ppsz_argv[i_input] );
693 p_dbus_msg = dbus_message_new_method_call(
694 VLC_DBUS_SERVICE, VLC_DBUS_OBJECT_PATH,
695 VLC_DBUS_INTERFACE, "AddMRL" );
697 if ( NULL == p_dbus_msg )
699 msg_Err( p_libvlc, "D-Bus problem" );
700 system_End( p_libvlc );
705 dbus_message_iter_init_append( p_dbus_msg,
707 if ( !dbus_message_iter_append_basic( &dbus_args,
709 &ppsz_argv[i_input] ) )
711 msg_Err( p_libvlc, "Out of memory" );
712 dbus_message_unref( p_dbus_msg );
713 system_End( p_libvlc );
717 if( config_GetInt( p_libvlc, "playlist-enqueue" ) )
719 if ( !dbus_message_iter_append_basic( &dbus_args,
720 DBUS_TYPE_BOOLEAN, &b_play ) )
722 msg_Err( p_libvlc, "Out of memory" );
723 dbus_message_unref( p_dbus_msg );
724 system_End( p_libvlc );
728 /* send message and get a handle for a reply */
729 if ( !dbus_connection_send_with_reply ( p_conn,
730 p_dbus_msg, &p_dbus_pending, -1 ) )
732 msg_Err( p_libvlc, "D-Bus problem" );
733 dbus_message_unref( p_dbus_msg );
734 system_End( p_libvlc );
738 if ( NULL == p_dbus_pending )
740 msg_Err( p_libvlc, "D-Bus problem" );
741 dbus_message_unref( p_dbus_msg );
742 system_End( p_libvlc );
745 dbus_connection_flush( p_conn );
746 dbus_message_unref( p_dbus_msg );
747 /* block until we receive a reply */
748 dbus_pending_call_block( p_dbus_pending );
749 dbus_pending_call_unref( p_dbus_pending );
750 } /* processes all command line MRLs */
753 system_End( p_libvlc );
756 } /* we're not in one-instance mode */
760 "%s is already registered on the session bus\n",
763 } /* the named is owned by something else */
766 /* register "/" object */
767 if( !dbus_connection_register_object_path( p_conn, "/",
768 &vlc_dbus_root_vtable, NULL ) )
770 msg_Err( p_libvlc, "Out of memory" );
773 "We are the primary owner of %s on the session bus",
776 } /* no error when requesting the name on the bus */
777 /* we unreference the connection when we've finished with it */
778 dbus_connection_unref( p_conn );
779 } /* ( p_conn != NULL ) */
783 * Message queue options
786 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
787 if( config_GetInt( p_libvlc, "quiet" ) )
790 var_Set( p_libvlc, "verbose", val );
792 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
793 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
795 p_libvlc->b_color = p_libvlc->b_color && config_GetInt( p_libvlc, "color" );
798 * Output messages that may still be in the queue
800 msg_Flush( p_libvlc );
802 /* p_libvlc initialization. FIXME ? */
804 if( !config_GetInt( p_libvlc, "fpu" ) )
805 libvlc_global.i_cpu &= ~CPU_CAPABILITY_FPU;
807 #if defined( __i386__ ) || defined( __x86_64__ )
808 if( !config_GetInt( p_libvlc, "mmx" ) )
809 libvlc_global.i_cpu &= ~CPU_CAPABILITY_MMX;
810 if( !config_GetInt( p_libvlc, "3dn" ) )
811 libvlc_global.i_cpu &= ~CPU_CAPABILITY_3DNOW;
812 if( !config_GetInt( p_libvlc, "mmxext" ) )
813 libvlc_global.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
814 if( !config_GetInt( p_libvlc, "sse" ) )
815 libvlc_global.i_cpu &= ~CPU_CAPABILITY_SSE;
816 if( !config_GetInt( p_libvlc, "sse2" ) )
817 libvlc_global.i_cpu &= ~CPU_CAPABILITY_SSE2;
819 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
820 if( !config_GetInt( p_libvlc, "altivec" ) )
821 libvlc_global.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
824 #define PRINT_CAPABILITY( capability, string ) \
825 if( libvlc_global.i_cpu & capability ) \
827 strncat( p_capabilities, string " ", \
828 sizeof(p_capabilities) - strlen(p_capabilities) ); \
829 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
832 p_capabilities[0] = '\0';
833 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
834 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
835 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
836 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
837 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
838 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
839 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
840 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
841 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
842 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
843 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
846 * Choose the best memcpy module
848 p_libvlc->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
850 if( p_libvlc->pf_memcpy == NULL )
852 p_libvlc->pf_memcpy = memcpy;
855 if( p_libvlc->pf_memset == NULL )
857 p_libvlc->pf_memset = memset;
860 p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" );
861 p_libvlc->i_timers = 0;
862 p_libvlc->pp_timers = NULL;
863 vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
866 * Initialize hotkey handling
868 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
869 p_libvlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
870 /* Do a copy (we don't need to modify the strings) */
871 memcpy( p_libvlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
873 /* Initialize playlist and get commandline files */
874 playlist_ThreadCreate( p_libvlc );
875 if( !p_libvlc->p_playlist )
877 msg_Err( p_libvlc, "playlist initialization failed" );
878 if( p_libvlc->p_memcpy_module != NULL )
880 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
882 module_EndBank( p_libvlc );
885 p_playlist = p_libvlc->p_playlist;
887 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
888 if( psz_modules && *psz_modules )
890 /* Add service discovery modules */
891 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
893 if( psz_modules ) free( psz_modules );
896 * Load background interfaces
898 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
899 psz_control = config_GetPsz( p_libvlc, "control" );
901 if( psz_modules && *psz_modules && psz_control && *psz_control )
903 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
904 strlen( psz_control ) + 1 );
905 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
907 else if( psz_control && *psz_control )
909 if( psz_modules ) free( psz_modules );
910 psz_modules = strdup( psz_control );
913 psz_parser = psz_modules;
914 while ( psz_parser && *psz_parser )
916 char *psz_module, *psz_temp;
917 psz_module = psz_parser;
918 psz_parser = strchr( psz_module, ':' );
924 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
927 sprintf( psz_temp, "%s,none", psz_module );
928 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
938 * Always load the hotkeys interface if it exists
940 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
943 * If needed, load the Xscreensaver interface
944 * Currently, only for X
946 #ifdef HAVE_X11_XLIB_H
947 if( config_GetInt( p_libvlc, "disable-screensaver" ) == 1 )
949 VLC_AddIntf( 0, "screensaver,none", VLC_FALSE, VLC_FALSE );
953 if( config_GetInt( p_libvlc, "file-logging" ) == 1 )
955 VLC_AddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE );
958 if( config_GetInt( p_libvlc, "syslog" ) == 1 )
960 const char *psz_logmode = "logmode=syslog";
961 libvlc_InternalAddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE,
966 if( config_GetInt( p_libvlc, "show-intf" ) == 1 )
968 VLC_AddIntf( 0, "showintf,none", VLC_FALSE, VLC_FALSE );
971 if( config_GetInt( p_libvlc, "network-synchronisation") == 1 )
973 VLC_AddIntf( 0, "netsync,none", VLC_FALSE, VLC_FALSE );
977 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
979 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
980 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
981 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
982 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
983 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
984 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
985 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
986 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
987 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
989 /* Create volume callback system. */
990 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
993 * Get input filenames given as commandline arguments
995 GetFilenames( p_libvlc, i_argc, ppsz_argv );
998 * Get --open argument
1000 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1001 var_Get( p_libvlc, "open", &val );
1002 if ( val.psz_string != NULL && *val.psz_string )
1004 VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
1005 PLAYLIST_INSERT, 0 );
1007 if ( val.psz_string != NULL ) free( val.psz_string );
1013 * Cleanup a libvlc instance. The instance is not completely deallocated
1014 * \param p_libvlc the instance to clean
1016 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
1018 intf_thread_t * p_intf;
1019 vout_thread_t * p_vout;
1020 aout_instance_t * p_aout;
1021 announce_handler_t * p_announce;
1023 /* Ask the interfaces to stop and destroy them */
1024 msg_Dbg( p_libvlc, "removing all interfaces" );
1025 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1027 intf_StopThread( p_intf );
1028 vlc_object_detach( p_intf );
1029 vlc_object_release( p_intf );
1030 intf_Destroy( p_intf );
1034 msg_Dbg( p_libvlc, "removing playlist" );
1035 playlist_ThreadDestroy( p_libvlc->p_playlist );
1037 /* Free video outputs */
1038 msg_Dbg( p_libvlc, "removing all video outputs" );
1039 while( (p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1041 vlc_object_detach( p_vout );
1042 vlc_object_release( p_vout );
1043 vout_Destroy( p_vout );
1046 /* Free audio outputs */
1047 msg_Dbg( p_libvlc, "removing all audio outputs" );
1048 while( (p_aout = vlc_object_find( p_libvlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1050 vlc_object_detach( (vlc_object_t *)p_aout );
1051 vlc_object_release( (vlc_object_t *)p_aout );
1052 aout_Delete( p_aout );
1055 stats_TimersDumpAll( p_libvlc );
1056 stats_TimersClean( p_libvlc );
1058 /* Free announce handler(s?) */
1059 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1062 msg_Dbg( p_libvlc, "removing announce handler" );
1063 vlc_object_detach( p_announce );
1064 vlc_object_release( p_announce );
1065 announce_HandlerDestroy( p_announce );
1071 * Destroy everything.
1072 * This function requests the running threads to finish, waits for their
1073 * termination, and destroys their structure.
1074 * It stops the thread systems: no instance can run after this has run
1075 * \param p_libvlc the instance to destroy
1076 * \param b_release whether we should do a release on the instance
1078 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
1080 vlc_value_t lockval;
1082 if( p_libvlc->p_memcpy_module )
1084 module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
1085 p_libvlc->p_memcpy_module = NULL;
1088 /* Free module bank. It is refcounted, so we call this each time */
1089 module_EndBank( p_libvlc );
1091 FREENULL( p_libvlc->psz_homedir );
1092 FREENULL( p_libvlc->psz_userdir );
1093 FREENULL( p_libvlc->psz_configfile );
1094 FREENULL( p_libvlc->p_hotkeys );
1096 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
1097 var_Get( p_libvlc_global, "libvlc", &lockval );
1098 vlc_mutex_lock( lockval.p_address );
1101 if( i_instances == 0 )
1103 /* System specific cleaning code */
1104 system_End( p_libvlc );
1106 /* Destroy global iconv */
1109 vlc_mutex_unlock( lockval.p_address );
1110 var_Destroy( p_libvlc_global, "libvlc" );
1112 msg_Flush( p_libvlc );
1113 msg_Destroy( p_libvlc );
1115 /* Destroy mutexes */
1116 vlc_mutex_destroy( &p_libvlc->config_lock );
1117 vlc_mutex_destroy( &p_libvlc->timer_lock );
1119 if( b_release ) vlc_object_release( p_libvlc );
1120 vlc_object_destroy( p_libvlc );
1122 /* Stop thread system: last one out please shut the door!
1123 * The number of initializations of the thread system is counted, we
1124 * can call this each time */
1125 vlc_threads_end( p_libvlc_global );
1131 * Add an interface plugin and run it
1133 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
1134 char const *psz_module,
1135 vlc_bool_t b_block, vlc_bool_t b_play,
1136 int i_options, const char *const *ppsz_options )
1139 intf_thread_t *p_intf;
1142 if( p_libvlc->p_libvlc_global->b_daemon && b_block && !psz_module )
1144 /* Daemon mode hack.
1145 * We prefer the dummy interface if none is specified. */
1146 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1147 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
1148 if( psz_interface ) free( psz_interface );
1152 /* Try to create the interface */
1153 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
1154 i_options, ppsz_options );
1156 if( p_intf == NULL )
1158 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1160 return VLC_EGENERIC;
1163 /* Interface doesn't handle play on start so do it ourselves */
1164 if( !p_intf->b_play && b_play )
1165 playlist_Play( p_libvlc->p_playlist );
1167 /* Try to run the interface */
1168 p_intf->b_play = b_play;
1169 p_intf->b_block = b_block;
1170 i_err = intf_RunThread( p_intf );
1173 vlc_object_detach( p_intf );
1174 intf_Destroy( p_intf );
1181 /*****************************************************************************
1182 * SetLanguage: set the interface language.
1183 *****************************************************************************
1184 * We set the LC_MESSAGES locale category for interface messages and buttons,
1185 * as well as the LC_CTYPE category for string sorting and possible wide
1186 * character support.
1187 *****************************************************************************/
1188 static void SetLanguage ( char const *psz_lang )
1190 #if defined( ENABLE_NLS ) \
1191 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1193 const char * psz_path;
1194 #if defined( __APPLE__ ) || defined ( WIN32 ) || defined( SYS_BEOS )
1198 if( psz_lang && !*psz_lang )
1200 # if defined( HAVE_LC_MESSAGES )
1201 setlocale( LC_MESSAGES, psz_lang );
1203 setlocale( LC_CTYPE, psz_lang );
1208 /* I need that under Darwin, please check it doesn't disturb
1209 * other platforms. --Meuuh */
1210 setenv( "LANG", psz_lang, 1 );
1212 #elif defined( SYS_BEOS ) || defined( WIN32 )
1213 /* We set LC_ALL manually because it is the only way to set
1214 * the language at runtime under eg. Windows. Beware that this
1215 * makes the environment unconsistent when libvlc is unloaded and
1216 * should probably be moved to a safer place like vlc.c. */
1217 static char psz_lcall[20];
1218 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1219 psz_lcall[19] = '\0';
1220 putenv( psz_lcall );
1223 setlocale( LC_ALL, psz_lang );
1226 /* Specify where to find the locales for current domain */
1227 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1228 psz_path = LOCALEDIR;
1230 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc_global.psz_vlcpath,
1234 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1236 fprintf( stderr, "warning: couldn't bind domain %s in directory %s\n",
1237 PACKAGE_NAME, psz_path );
1240 /* Set the default domain */
1241 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1245 /*****************************************************************************
1246 * GetFilenames: parse command line options which are not flags
1247 *****************************************************************************
1248 * Parse command line for input files as well as their associated options.
1249 * An option always follows its associated input and begins with a ":".
1250 *****************************************************************************/
1251 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, char *ppsz_argv[] )
1253 int i_opt, i_options;
1255 /* We assume that the remaining parameters are filenames
1256 * and their input options */
1257 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1259 const char *psz_target;
1262 /* Count the input options */
1263 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1269 /* TODO: write an internal function of this one, to avoid
1270 * unnecessary lookups. */
1271 /* FIXME: should we convert options to UTF-8 as well ?? */
1274 if( GetVersion() < 0x80000000 )
1276 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1277 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1279 PLAYLIST_INSERT, 0 );
1284 psz_target = FromLocale( ppsz_argv[ i_opt ] );
1285 VLC_AddTarget( p_vlc->i_object_id, psz_target,
1286 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1288 PLAYLIST_INSERT, 0 );
1289 LocaleFree( psz_target );
1296 /*****************************************************************************
1297 * Help: print program help
1298 *****************************************************************************
1299 * Print a short inline help. Message interface is initialized at this stage.
1300 *****************************************************************************/
1301 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1304 ShowConsole( VLC_TRUE );
1307 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1309 utf8_fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1310 Usage( p_this, "help" );
1311 Usage( p_this, "main" );
1313 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1315 utf8_fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1316 Usage( p_this, NULL );
1318 else if( psz_help_name )
1320 Usage( p_this, psz_help_name );
1323 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1328 /*****************************************************************************
1329 * Usage: print module usage
1330 *****************************************************************************
1331 * Print a short inline help. Message interface is initialized at this stage.
1332 *****************************************************************************/
1333 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1335 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1336 /* short option ------' | | | | | | |
1337 * option name ------------' | | | | | |
1338 * <bra ---------------------' | | | | |
1339 * option type or "" ----------' | | | |
1340 * ket> -------------------------' | | |
1341 * padding spaces -----------------' | |
1342 * comment --------------------------' |
1343 * comment suffix ---------------------'
1345 * The purpose of having bra and ket is that we might i18n them as well.
1347 #define LINE_START 8
1348 #define PADDING_SPACES 25
1350 # define OPTION_VALUE_SEP "="
1352 # define OPTION_VALUE_SEP " "
1355 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1356 char psz_spaces_longtext[LINE_START+3];
1357 char psz_format[sizeof(FORMAT_STRING)];
1358 char psz_buffer[10000];
1361 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1362 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1363 vlc_bool_t b_description;
1365 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1366 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1367 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1368 psz_spaces_longtext[LINE_START+2] = '\0';
1370 strcpy( psz_format, FORMAT_STRING );
1372 /* List all modules */
1373 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1375 /* Enumerate the config for each module */
1376 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1378 vlc_bool_t b_help_module;
1379 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1380 module_config_t *p_item,
1381 *p_end = p_parser->p_config + p_parser->confsize;
1383 if( psz_module_name && strcmp( psz_module_name,
1384 p_parser->psz_object_name ) )
1389 /* Ignore modules without config options */
1390 if( !p_parser->i_config_items )
1395 /* Ignore modules with only advanced config options if requested */
1398 for( p_item = p_parser->p_config;
1402 if( (p_item->i_type & CONFIG_ITEM) &&
1403 !p_item->b_advanced ) break;
1407 /* Print name of module */
1408 if( strcmp( "main", p_parser->psz_object_name ) )
1409 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1411 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1413 /* Print module options */
1414 for( p_item = p_parser->p_config;
1418 char *psz_text, *psz_spaces = psz_spaces_text;
1419 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1420 const char *psz_suf = "", *psz_prefix = NULL;
1423 /* Skip deprecated options */
1424 if( p_item->psz_current )
1428 /* Skip advanced options if requested */
1429 if( p_item->b_advanced && !b_advanced )
1434 switch( p_item->i_type )
1436 case CONFIG_HINT_CATEGORY:
1437 case CONFIG_HINT_USAGE:
1438 if( !strcmp( "main", p_parser->psz_object_name ) )
1439 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1442 case CONFIG_ITEM_STRING:
1443 case CONFIG_ITEM_FILE:
1444 case CONFIG_ITEM_DIRECTORY:
1445 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1446 case CONFIG_ITEM_MODULE_CAT:
1447 case CONFIG_ITEM_MODULE_LIST:
1448 case CONFIG_ITEM_MODULE_LIST_CAT:
1449 psz_bra = OPTION_VALUE_SEP "<";
1450 psz_type = _("string");
1453 if( p_item->ppsz_list )
1455 psz_bra = OPTION_VALUE_SEP "{";
1456 psz_type = psz_buffer;
1457 psz_buffer[0] = '\0';
1458 for( i = 0; p_item->ppsz_list[i]; i++ )
1460 if( i ) strcat( psz_buffer, "," );
1461 strcat( psz_buffer, p_item->ppsz_list[i] );
1466 case CONFIG_ITEM_INTEGER:
1467 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1468 psz_bra = OPTION_VALUE_SEP "<";
1469 psz_type = _("integer");
1472 if( p_item->i_list )
1474 psz_bra = OPTION_VALUE_SEP "{";
1475 psz_type = psz_buffer;
1476 psz_buffer[0] = '\0';
1477 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1479 if( i ) strcat( psz_buffer, ", " );
1480 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1482 p_item->ppsz_list_text[i] );
1487 case CONFIG_ITEM_FLOAT:
1488 psz_bra = OPTION_VALUE_SEP "<";
1489 psz_type = _("float");
1492 case CONFIG_ITEM_BOOL:
1493 psz_bra = ""; psz_type = ""; psz_ket = "";
1494 if( !b_help_module )
1496 psz_suf = p_item->value.i ? _(" (default enabled)") :
1497 _(" (default disabled)");
1507 /* Add short option if any */
1508 if( p_item->i_short )
1510 sprintf( psz_short, "-%c,", p_item->i_short );
1514 strcpy( psz_short, " " );
1517 i = PADDING_SPACES - strlen( p_item->psz_name )
1518 - strlen( psz_bra ) - strlen( psz_type )
1519 - strlen( psz_ket ) - 1;
1521 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1523 psz_prefix = ", --no-";
1524 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1529 psz_spaces[0] = '\n';
1534 psz_spaces[i] = '\0';
1537 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1539 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1540 psz_prefix, p_item->psz_name, psz_bra, psz_type,
1541 psz_ket, psz_spaces );
1545 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1546 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1549 psz_spaces[i] = ' ';
1551 /* We wrap the rest of the output */
1552 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1553 b_description = config_GetInt( p_this, "help-verbose" );
1556 psz_text = psz_buffer;
1559 char *psz_parser, *psz_word;
1560 size_t i_end = strlen( psz_text );
1562 /* If the remaining text fits in a line, print it. */
1563 if( i_end <= (size_t)i_width )
1565 utf8_fprintf( stdout, "%s\n", psz_text );
1569 /* Otherwise, eat as many words as possible */
1570 psz_parser = psz_text;
1573 psz_word = psz_parser;
1574 psz_parser = strchr( psz_word, ' ' );
1575 /* If no space was found, we reached the end of the text
1576 * block; otherwise, we skip the space we just found. */
1577 psz_parser = psz_parser ? psz_parser + 1
1580 } while( psz_parser - psz_text <= i_width );
1582 /* We cut a word in one of these cases:
1583 * - it's the only word in the line and it's too long.
1584 * - we used less than 80% of the width and the word we are
1585 * going to wrap is longer than 40% of the width, and even
1586 * if the word would have fit in the next line. */
1587 if( psz_word == psz_text
1588 || ( psz_word - psz_text < 80 * i_width / 100
1589 && psz_parser - psz_word > 40 * i_width / 100 ) )
1591 char c = psz_text[i_width];
1592 psz_text[i_width] = '\0';
1593 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1594 psz_text += i_width;
1599 psz_word[-1] = '\0';
1600 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1601 psz_text = psz_word;
1605 if( b_description && p_item->psz_longtext )
1607 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1608 b_description = VLC_FALSE;
1609 psz_spaces = psz_spaces_longtext;
1610 utf8_fprintf( stdout, "%s", psz_spaces );
1616 /* Release the module list */
1617 vlc_list_release( p_list );
1620 /*****************************************************************************
1621 * ListModules: list the available modules with their description
1622 *****************************************************************************
1623 * Print a list of all available modules (builtins and plugins) and a short
1624 * description for each one.
1625 *****************************************************************************/
1626 static void ListModules( libvlc_int_t *p_this )
1630 char psz_spaces[22];
1633 memset( psz_spaces, ' ', 22 );
1636 ShowConsole( VLC_TRUE );
1639 /* List all modules */
1640 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1642 /* Enumerate each module */
1643 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1647 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1649 /* Nasty hack, but right now I'm too tired to think about a nice
1651 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1655 utf8_fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
1656 psz_spaces, p_parser->psz_longname );
1658 psz_spaces[i] = ' ';
1661 vlc_list_release( p_list );
1663 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1668 /*****************************************************************************
1669 * Version: print complete program version
1670 *****************************************************************************
1671 * Print complete program version and build number.
1672 *****************************************************************************/
1673 static void Version( void )
1676 ShowConsole( VLC_TRUE );
1679 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1680 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1681 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1682 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1683 #ifndef HAVE_SHARED_LIBVLC
1684 if( strcmp( VLC_Changeset(), "exported" ) )
1685 utf8_fprintf( stdout, _("Based upon svn changeset [%s]\n"),
1688 utf8_fprintf( stdout, LICENSE_MSG );
1690 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1695 /*****************************************************************************
1696 * ShowConsole: On Win32, create an output console for debug messages
1697 *****************************************************************************
1698 * This function is useful only on Win32.
1699 *****************************************************************************/
1701 static void ShowConsole( vlc_bool_t b_dofile )
1706 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1710 freopen( "CONOUT$", "w", stderr );
1711 freopen( "CONIN$", "r", stdin );
1713 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1716 freopen( "vlc-help.txt", "wt", stdout );
1717 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1720 else freopen( "CONOUT$", "w", stdout );
1726 /*****************************************************************************
1727 * PauseConsole: On Win32, wait for a key press before closing the console
1728 *****************************************************************************
1729 * This function is useful only on Win32.
1730 *****************************************************************************/
1732 static void PauseConsole( void )
1736 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1738 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1746 /*****************************************************************************
1747 * ConsoleWidth: Return the console width in characters
1748 *****************************************************************************
1749 * We use the stty shell command to get the console width; if this fails or
1750 * if the width is less than 80, we default to 80.
1751 *****************************************************************************/
1752 static int ConsoleWidth( void )
1757 char buf[20], *psz_parser;
1761 file = popen( "stty size 2>/dev/null", "r" );
1764 i_ret = fread( buf, 1, 20, file );
1768 psz_parser = strchr( buf, ' ' );
1771 i_ret = atoi( psz_parser + 1 );
1786 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1787 vlc_value_t old_val, vlc_value_t new_val, void *param)
1789 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1791 if( new_val.i_int >= -1 )
1793 p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1798 /*****************************************************************************
1799 * InitDeviceValues: initialize device values
1800 *****************************************************************************
1801 * This function inits the dvd, vcd and cd-audio values
1802 *****************************************************************************/
1803 static void InitDeviceValues( libvlc_int_t *p_vlc )
1806 LibHalContext * ctx;
1811 DBusConnection *p_connection;
1815 ctx = libhal_ctx_new();
1817 dbus_error_init( &error );
1818 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
1819 if( dbus_error_is_set( &error ) )
1821 dbus_error_free( &error );
1824 libhal_ctx_set_dbus_connection( ctx, p_connection );
1825 if( libhal_ctx_init( ctx, &error ) )
1827 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
1831 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
1833 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
1836 for( i = 0; i < i_devices; i++ )
1839 if( !libhal_device_property_exists( ctx, devices[i],
1840 "storage.cdrom.dvd", NULL ) )
1842 if( !hal_device_property_exists( ctx, devices[ i ],
1843 "storage.cdrom.dvd" ) )
1849 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
1850 "storage.cdrom.dvd", NULL );
1851 block_dev = libhal_device_get_property_string( ctx,
1852 devices[ i ], "block.device" , NULL );
1854 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
1855 "storage.cdrom.dvd" );
1856 block_dev = hal_device_get_property_string( ctx, devices[ i ],
1861 config_PutPsz( p_vlc, "dvd", block_dev );
1864 config_PutPsz( p_vlc, "vcd", block_dev );
1865 config_PutPsz( p_vlc, "cd-audio", block_dev );
1867 libhal_free_string( block_dev );
1869 hal_free_string( block_dev );
1873 libhal_free_string_array( devices );
1875 hal_free_string_array( devices );
1880 libhal_ctx_shutdown( ctx, NULL );
1881 dbus_connection_unref( p_connection );
1883 hal_shutdown( ctx );
1888 msg_Warn( p_vlc, "Unable to get HAL device properties" );