Fix description
[vlc.git] / src / libvlc.c
blob7340e365f8634e003cca0401c586559986fbd691
1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
28 /** \file
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <vlc_common.h>
40 #include "control/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45 #include "interface/interface.h"
47 #include <errno.h> /* ENOMEM */
48 #include <stdio.h> /* sprintf() */
49 #include <string.h>
50 #include <stdlib.h> /* free() */
52 #ifndef WIN32
53 # include <netinet/in.h> /* BSD: struct in_addr */
54 #endif
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
59 # include <io.h>
60 #endif
62 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
63 # include "extras/getopt.h"
64 #endif
66 #ifdef HAVE_LOCALE_H
67 # include <locale.h>
68 #endif
70 #ifdef HAVE_DBUS
71 /* used for one-instance mode */
72 # include <dbus/dbus.h>
73 #endif
75 #ifdef HAVE_HAL
76 # include <hal/libhal.h>
77 #endif
79 #include <vlc_playlist.h>
80 #include <vlc_interface.h>
82 #include <vlc_aout.h>
83 #include "audio_output/aout_internal.h"
85 #include <vlc_vout.h>
87 #include <vlc_sout.h>
88 #include "stream_output/stream_output.h"
90 #include <vlc_charset.h>
92 #include "libvlc.h"
94 #include "playlist/playlist_internal.h"
96 #include <vlc_vlm.h>
98 #include <assert.h>
100 /*****************************************************************************
101 * The evil global variables. We handle them with care, don't worry.
102 *****************************************************************************/
103 static libvlc_int_t * p_static_vlc = NULL;
104 static unsigned i_instances = 0;
106 static bool b_daemon = false;
108 /*****************************************************************************
109 * vlc_gc_*.
110 *****************************************************************************/
111 void __vlc_gc_incref( gc_object_t * p_gc )
113 assert( p_gc->i_gc_refcount > 0 );
115 /* FIXME: atomic version needed! */
116 p_gc->i_gc_refcount ++;
119 void __vlc_gc_decref( gc_object_t *p_gc )
121 assert( p_gc );
122 assert( p_gc->i_gc_refcount > 0 );
124 /* FIXME: atomic version needed! */
125 p_gc->i_gc_refcount -- ;
127 if( p_gc->i_gc_refcount == 0 )
129 p_gc->pf_destructor( p_gc );
130 /* Do not use the p_gc pointer from now on ! */
134 void
135 __vlc_gc_init( gc_object_t * p_gc, void (*pf_destructor)( gc_object_t * ),
136 void * arg)
138 p_gc->i_gc_refcount = 1;
139 p_gc->pf_destructor = pf_destructor;
140 p_gc->p_destructor_arg = arg;
143 /*****************************************************************************
144 * Local prototypes
145 *****************************************************************************/
146 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
147 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
148 static void SetLanguage ( char const * );
149 #endif
150 static inline int LoadMessages (void);
151 static int GetFilenames ( libvlc_int_t *, int, const char *[] );
152 static void Help ( libvlc_int_t *, char const *psz_help_name );
153 static void Usage ( libvlc_int_t *, char const *psz_module_name );
154 static void ListModules ( libvlc_int_t *, bool );
155 static void Version ( void );
157 #ifdef WIN32
158 static void ShowConsole ( bool );
159 static void PauseConsole ( void );
160 #endif
161 static int ConsoleWidth ( void );
163 static int VerboseCallback( vlc_object_t *, char const *,
164 vlc_value_t, vlc_value_t, void * );
166 static void InitDeviceValues( libvlc_int_t * );
169 * Allocate a libvlc instance, initialize global data if needed
170 * It also initializes the threading system
172 libvlc_int_t * libvlc_InternalCreate( void )
174 libvlc_int_t *p_libvlc;
175 libvlc_priv_t *priv;
176 char *psz_env = NULL;
178 /* vlc_threads_init *must* be the first internal call! No other call is
179 * allowed before the thread system has been initialized. */
180 if (vlc_threads_init ())
181 return NULL;
183 libvlc_global_data_t *p_libvlc_global = vlc_global();
184 /* Now that the thread system is initialized, we don't have much, but
185 * at least we have variables */
186 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
187 if( i_instances == 0 )
189 /* Guess what CPU we have */
190 cpu_flags = CPUCapabilities();
191 /* The module bank will be initialized later */
192 p_libvlc_global->p_module_bank = NULL;
195 /* Allocate a libvlc instance object */
196 p_libvlc = vlc_custom_create( VLC_OBJECT(p_libvlc_global),
197 sizeof (*p_libvlc) + sizeof (libvlc_priv_t),
198 VLC_OBJECT_LIBVLC, "libvlc" );
199 if( p_libvlc != NULL )
200 i_instances++;
201 vlc_mutex_unlock( lock );
203 if( p_libvlc == NULL )
204 return NULL;
206 priv = libvlc_priv (p_libvlc);
207 priv->p_playlist = NULL;
208 priv->p_interaction = NULL;
209 priv->p_vlm = NULL;
210 p_libvlc->psz_object_name = strdup( "libvlc" );
212 /* Initialize message queue */
213 msg_Create( p_libvlc );
215 /* Find verbosity from VLC_VERBOSE environment variable */
216 psz_env = getenv( "VLC_VERBOSE" );
217 if( psz_env != NULL )
218 priv->i_verbose = atoi( psz_env );
219 else
220 priv->i_verbose = 3;
221 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
222 priv->b_color = isatty( 2 ); /* 2 is for stderr */
223 #else
224 priv->b_color = false;
225 #endif
227 /* Announce who we are - Do it only for first instance ? */
228 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
229 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
231 /* Initialize mutexes */
232 vlc_mutex_init( &priv->timer_lock );
233 vlc_mutex_init( &priv->config_lock );
235 priv->threads_count = 0;
236 vlc_mutex_init (&priv->threads_lock);
237 vlc_cond_init (NULL, &priv->threads_wait);
239 /* Store data for the non-reentrant API */
240 p_static_vlc = p_libvlc;
242 return p_libvlc;
246 * Initialize a libvlc instance
247 * This function initializes a previously allocated libvlc instance:
248 * - CPU detection
249 * - gettext initialization
250 * - message queue, module bank and playlist initialization
251 * - configuration and commandline parsing
253 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
254 const char *ppsz_argv[] )
256 libvlc_global_data_t *p_libvlc_global = vlc_global();
257 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
258 char p_capabilities[200];
259 char * p_tmp = NULL;
260 char * psz_modules = NULL;
261 char * psz_parser = NULL;
262 char * psz_control = NULL;
263 bool b_exit = false;
264 int i_ret = VLC_EEXIT;
265 playlist_t *p_playlist = NULL;
266 vlc_value_t val;
267 #if defined( ENABLE_NLS ) \
268 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
269 # if defined (WIN32) || defined (__APPLE__)
270 char * psz_language;
271 #endif
272 #endif
274 /* System specific initialization code */
275 system_Init( p_libvlc, &i_argc, ppsz_argv );
277 /* Get the executable name (similar to the basename command) */
278 if( i_argc > 0 && ppsz_argv[0][0] )
280 free( p_libvlc->psz_object_name );
282 const char *psz_exe = strrchr( ppsz_argv[0], '/' );
283 if( psz_exe && *(psz_exe + 1) )
284 p_libvlc->psz_object_name = strdup( psz_exe + 1 );
285 else
286 p_libvlc->psz_object_name = strdup( ppsz_argv[0] );
290 * Support for gettext
292 LoadMessages ();
294 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
295 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
297 /* Initialize the module bank and load the configuration of the
298 * main module. We need to do this at this stage to be able to display
299 * a short help if required by the user. (short help == main module
300 * options) */
301 module_InitBank( p_libvlc );
303 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
305 module_EndBank( p_libvlc );
306 return VLC_EGENERIC;
309 #ifdef __APPLE__
310 /* vlc_thread_set_priority needs to query the config,
311 * so this is the earliest moment where we can set this */
312 vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
313 #endif
315 /* Check for short help option */
316 if( config_GetInt( p_libvlc, "help" ) > 0 )
318 Help( p_libvlc, "help" );
319 b_exit = true;
320 i_ret = VLC_EEXITSUCCESS;
322 /* Check for version option */
323 else if( config_GetInt( p_libvlc, "version" ) > 0 )
325 Version();
326 b_exit = true;
327 i_ret = VLC_EEXITSUCCESS;
330 /* Set the config file stuff */
331 priv->psz_configfile = config_GetCustomConfigFile( p_libvlc );
333 /* Check for plugins cache options */
334 if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
336 p_libvlc_global->p_module_bank->b_cache_delete = true;
339 /* Will be re-done properly later on */
340 priv->i_verbose = config_GetInt( p_libvlc, "verbose" );
342 /* Check for daemon mode */
343 #ifndef WIN32
344 if( config_GetInt( p_libvlc, "daemon" ) > 0 )
346 #ifdef HAVE_DAEMON
347 char *psz_pidfile = NULL;
349 if( daemon( 1, 0) != 0 )
351 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
352 b_exit = true;
354 b_daemon = true;
356 /* lets check if we need to write the pidfile */
357 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
358 if( psz_pidfile != NULL )
360 FILE *pidfile;
361 pid_t i_pid = getpid ();
362 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
363 i_pid, psz_pidfile );
364 pidfile = utf8_fopen( psz_pidfile,"w" );
365 if( pidfile != NULL )
367 utf8_fprintf( pidfile, "%d", (int)i_pid );
368 fclose( pidfile );
370 else
372 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
373 psz_pidfile );
376 free( psz_pidfile );
378 #else
379 pid_t i_pid;
381 if( ( i_pid = fork() ) < 0 )
383 msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
384 b_exit = true;
386 else if( i_pid )
388 /* This is the parent, exit right now */
389 msg_Dbg( p_libvlc, "closing parent process" );
390 b_exit = true;
391 i_ret = VLC_EEXITSUCCESS;
393 else
395 /* We are the child */
396 msg_Dbg( p_libvlc, "daemon spawned" );
397 close( STDIN_FILENO );
398 close( STDOUT_FILENO );
399 close( STDERR_FILENO );
401 b_daemon = true;
403 #endif
405 #endif
407 if( b_exit )
409 module_EndBank( p_libvlc );
410 return i_ret;
413 /* Check for translation config option */
414 #if defined( ENABLE_NLS ) \
415 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
416 # if defined (WIN32) || defined (__APPLE__)
417 /* This ain't really nice to have to reload the config here but it seems
418 * the only way to do it. */
420 if( !config_GetInt( p_libvlc, "ignore-config" ) )
421 config_LoadConfigFile( p_libvlc, "main" );
422 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
424 /* Check if the user specified a custom language */
425 psz_language = config_GetPsz( p_libvlc, "language" );
426 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
428 bool b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
430 /* Reset the default domain */
431 SetLanguage( psz_language );
433 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
434 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
436 module_EndBank( p_libvlc );
437 module_InitBank( p_libvlc );
438 if( !config_GetInt( p_libvlc, "ignore-config" ) )
439 config_LoadConfigFile( p_libvlc, "main" );
440 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
441 p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
443 free( psz_language );
444 # endif
445 #endif
448 * Load the builtins and plugins into the module_bank.
449 * We have to do it before config_Load*() because this also gets the
450 * list of configuration options exported by each module and loads their
451 * default values.
453 module_LoadBuiltins( p_libvlc );
454 module_LoadPlugins( p_libvlc );
455 if( p_libvlc->b_die )
457 b_exit = true;
460 msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
461 vlc_internals( p_libvlc_global->p_module_bank )->i_children );
463 /* Check for help on modules */
464 if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
466 Help( p_libvlc, p_tmp );
467 free( p_tmp );
468 b_exit = true;
469 i_ret = VLC_EEXITSUCCESS;
471 /* Check for full help option */
472 else if( config_GetInt( p_libvlc, "full-help" ) > 0 )
474 config_PutInt( p_libvlc, "advanced", 1);
475 config_PutInt( p_libvlc, "help-verbose", 1);
476 Help( p_libvlc, "full-help" );
477 b_exit = true;
478 i_ret = VLC_EEXITSUCCESS;
480 /* Check for long help option */
481 else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
483 Help( p_libvlc, "longhelp" );
484 b_exit = true;
485 i_ret = VLC_EEXITSUCCESS;
487 /* Check for module list option */
488 else if( config_GetInt( p_libvlc, "list" ) > 0 )
490 ListModules( p_libvlc, false );
491 b_exit = true;
492 i_ret = VLC_EEXITSUCCESS;
494 else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
496 ListModules( p_libvlc, true );
497 b_exit = true;
498 i_ret = VLC_EEXITSUCCESS;
501 /* Check for config file options */
502 if( !config_GetInt( p_libvlc, "ignore-config" ) )
504 if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
506 config_ResetAll( p_libvlc );
507 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
508 config_SaveConfigFile( p_libvlc, NULL );
510 if( config_GetInt( p_libvlc, "save-config" ) > 0 )
512 config_LoadConfigFile( p_libvlc, NULL );
513 config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
514 config_SaveConfigFile( p_libvlc, NULL );
518 if( b_exit )
520 module_EndBank( p_libvlc );
521 return i_ret;
525 * Init device values
527 InitDeviceValues( p_libvlc );
530 * Override default configuration with config file settings
532 if( !config_GetInt( p_libvlc, "ignore-config" ) )
533 config_LoadConfigFile( p_libvlc, NULL );
536 * Override configuration with command line settings
538 if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
540 #ifdef WIN32
541 ShowConsole( false );
542 /* Pause the console because it's destroyed when we exit */
543 fprintf( stderr, "The command line options couldn't be loaded, check "
544 "that they are valid.\n" );
545 PauseConsole();
546 #endif
547 module_EndBank( p_libvlc );
548 return VLC_EGENERIC;
552 * System specific configuration
554 system_Configure( p_libvlc, &i_argc, ppsz_argv );
556 /* FIXME: could be replaced by using Unix sockets */
557 #ifdef HAVE_DBUS
558 dbus_threads_init_default();
560 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
562 /* Initialise D-Bus interface, check for other instances */
563 DBusConnection *p_conn = NULL;
564 DBusError dbus_error;
566 dbus_error_init( &dbus_error );
568 /* connect to the session bus */
569 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
570 if( !p_conn )
572 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
573 dbus_error.message );
574 dbus_error_free( &dbus_error );
576 else
578 /* check if VLC is available on the bus
579 * if not: D-Bus control is not enabled on the other
580 * instance and we can't pass MRLs to it */
581 DBusMessage *p_test_msg = NULL;
582 DBusMessage *p_test_reply = NULL;
583 p_test_msg = dbus_message_new_method_call(
584 "org.mpris.vlc", "/",
585 "org.freedesktop.MediaPlayer", "Identity" );
586 /* block until a reply arrives */
587 p_test_reply = dbus_connection_send_with_reply_and_block(
588 p_conn, p_test_msg, -1, &dbus_error );
589 dbus_message_unref( p_test_msg );
590 if( p_test_reply == NULL )
592 dbus_error_free( &dbus_error );
593 msg_Dbg( p_libvlc, "No Media Player is running. "
594 "Continuing normally." );
596 else
598 int i_input;
599 DBusMessage* p_dbus_msg = NULL;
600 DBusMessageIter dbus_args;
601 DBusPendingCall* p_dbus_pending = NULL;
602 dbus_bool_t b_play;
604 dbus_message_unref( p_test_reply );
605 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
607 for( i_input = optind;i_input < i_argc;i_input++ )
609 msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
610 ppsz_argv[i_input] );
612 p_dbus_msg = dbus_message_new_method_call(
613 "org.mpris.vlc", "/TrackList",
614 "org.freedesktop.MediaPlayer", "AddTrack" );
616 if ( NULL == p_dbus_msg )
618 msg_Err( p_libvlc, "D-Bus problem" );
619 system_End( p_libvlc );
620 exit( VLC_ETIMEOUT );
623 /* append MRLs */
624 dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
625 if ( !dbus_message_iter_append_basic( &dbus_args,
626 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
628 dbus_message_unref( p_dbus_msg );
629 system_End( p_libvlc );
630 exit( VLC_ENOMEM );
632 b_play = TRUE;
633 if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
634 b_play = FALSE;
635 if ( !dbus_message_iter_append_basic( &dbus_args,
636 DBUS_TYPE_BOOLEAN, &b_play ) )
638 dbus_message_unref( p_dbus_msg );
639 system_End( p_libvlc );
640 exit( VLC_ENOMEM );
643 /* send message and get a handle for a reply */
644 if ( !dbus_connection_send_with_reply ( p_conn,
645 p_dbus_msg, &p_dbus_pending, -1 ) )
647 msg_Err( p_libvlc, "D-Bus problem" );
648 dbus_message_unref( p_dbus_msg );
649 system_End( p_libvlc );
650 exit( VLC_ETIMEOUT );
653 if ( NULL == p_dbus_pending )
655 msg_Err( p_libvlc, "D-Bus problem" );
656 dbus_message_unref( p_dbus_msg );
657 system_End( p_libvlc );
658 exit( VLC_ETIMEOUT );
660 dbus_connection_flush( p_conn );
661 dbus_message_unref( p_dbus_msg );
662 /* block until we receive a reply */
663 dbus_pending_call_block( p_dbus_pending );
664 dbus_pending_call_unref( p_dbus_pending );
665 } /* processes all command line MRLs */
667 /* bye bye */
668 system_End( p_libvlc );
669 exit( VLC_SUCCESS );
672 /* we unreference the connection when we've finished with it */
673 if( p_conn ) dbus_connection_unref( p_conn );
675 #endif
678 * Message queue options
681 var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
682 if( config_GetInt( p_libvlc, "quiet" ) > 0 )
684 val.i_int = -1;
685 var_Set( p_libvlc, "verbose", val );
687 var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
688 var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
690 if( priv->b_color )
691 priv->b_color = config_GetInt( p_libvlc, "color" ) > 0;
694 * Output messages that may still be in the queue
696 msg_Flush( p_libvlc );
698 if( !config_GetInt( p_libvlc, "fpu" ) )
699 cpu_flags &= ~CPU_CAPABILITY_FPU;
701 #if defined( __i386__ ) || defined( __x86_64__ )
702 if( !config_GetInt( p_libvlc, "mmx" ) )
703 cpu_flags &= ~CPU_CAPABILITY_MMX;
704 if( !config_GetInt( p_libvlc, "3dn" ) )
705 cpu_flags &= ~CPU_CAPABILITY_3DNOW;
706 if( !config_GetInt( p_libvlc, "mmxext" ) )
707 cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
708 if( !config_GetInt( p_libvlc, "sse" ) )
709 cpu_flags &= ~CPU_CAPABILITY_SSE;
710 if( !config_GetInt( p_libvlc, "sse2" ) )
711 cpu_flags &= ~CPU_CAPABILITY_SSE2;
712 #endif
713 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
714 if( !config_GetInt( p_libvlc, "altivec" ) )
715 cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
716 #endif
718 #define PRINT_CAPABILITY( capability, string ) \
719 if( vlc_CPU() & capability ) \
721 strncat( p_capabilities, string " ", \
722 sizeof(p_capabilities) - strlen(p_capabilities) ); \
723 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
726 p_capabilities[0] = '\0';
727 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
728 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
729 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
730 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
731 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
732 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
733 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
734 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
735 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
736 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
737 msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
740 * Choose the best memcpy module
742 priv->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
744 priv->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
745 priv->i_timers = 0;
746 priv->pp_timers = NULL;
748 /* Init stats */
749 p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
750 if( !p_libvlc->p_stats )
752 vlc_object_release( p_libvlc );
753 return VLC_ENOMEM;
755 vlc_mutex_init( &p_libvlc->p_stats->lock );
756 priv->p_stats_computer = NULL;
758 /* Init the array that holds every input item */
759 ARRAY_INIT( priv->input_items );
760 priv->i_last_input_id = 0;
763 * Initialize hotkey handling
765 var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
766 var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
767 p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
768 /* Do a copy (we don't need to modify the strings) */
769 memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
770 var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
771 p_libvlc->p_hotkeys );
773 /* Initialize interaction */
774 priv->p_interaction = interaction_Init( p_libvlc );
776 /* Initialize playlist and get commandline files */
777 playlist_ThreadCreate( p_libvlc );
778 if( !priv->p_playlist )
780 msg_Err( p_libvlc, "playlist initialization failed" );
781 if( priv->p_memcpy_module != NULL )
783 module_Unneed( p_libvlc, priv->p_memcpy_module );
785 module_EndBank( p_libvlc );
786 return VLC_EGENERIC;
788 p_playlist = priv->p_playlist;
790 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
791 if( psz_modules && *psz_modules )
793 /* Add service discovery modules */
794 playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
796 free( psz_modules );
798 #ifdef ENABLE_SOUT
799 /* Initialize VLM if vlm-conf is specified */
800 psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
801 if( psz_parser && *psz_parser )
803 priv->p_vlm = vlm_New( p_libvlc );
804 if( !priv->p_vlm )
805 msg_Err( p_libvlc, "VLM initialization failed" );
807 free( psz_parser );
808 #endif
811 * Load background interfaces
813 psz_modules = config_GetPsz( p_libvlc, "extraintf" );
814 psz_control = config_GetPsz( p_libvlc, "control" );
816 if( psz_modules && *psz_modules && psz_control && *psz_control )
818 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
819 strlen( psz_control ) + 1 );
820 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
822 else if( psz_control && *psz_control )
824 free( psz_modules );
825 psz_modules = strdup( psz_control );
828 psz_parser = psz_modules;
829 while ( psz_parser && *psz_parser )
831 char *psz_module, *psz_temp;
832 psz_module = psz_parser;
833 psz_parser = strchr( psz_module, ':' );
834 if ( psz_parser )
836 *psz_parser = '\0';
837 psz_parser++;
839 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
840 if( psz_temp )
842 sprintf( psz_temp, "%s,none", psz_module );
843 libvlc_InternalAddIntf( p_libvlc, psz_temp );
844 free( psz_temp );
847 free( psz_modules );
848 free( psz_control );
851 * Always load the hotkeys interface if it exists
853 libvlc_InternalAddIntf( p_libvlc, "hotkeys,none" );
855 #ifdef HAVE_DBUS
856 /* loads dbus control interface if in one-instance mode
857 * we do it only when playlist exists, because dbus module needs it */
858 if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
859 libvlc_InternalAddIntf( p_libvlc, "dbus,none" );
861 /* Prevents the power management daemon from suspending the system
862 * when VLC is active */
863 if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
864 libvlc_InternalAddIntf( p_libvlc, "inhibit,none" );
865 #endif
868 * If needed, load the Xscreensaver interface
869 * Currently, only for X
871 #ifdef HAVE_X11_XLIB_H
872 if( config_GetInt( p_libvlc, "disable-screensaver" ) )
874 libvlc_InternalAddIntf( p_libvlc, "screensaver,none" );
876 #endif
878 if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
880 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
882 #ifdef HAVE_SYSLOG_H
883 if( config_GetInt( p_libvlc, "syslog" ) > 0 )
885 char *logmode = var_CreateGetString( p_libvlc, "logmode" );
886 var_SetString( p_libvlc, "logmode", "syslog" );
887 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
889 if( logmode )
891 var_SetString( p_libvlc, "logmode", logmode );
892 free( logmode );
894 else
895 var_Destroy( p_libvlc, "logmode" );
897 #endif
899 if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
901 libvlc_InternalAddIntf( p_libvlc, "showintf,none" );
904 if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
906 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
909 #ifdef WIN32
910 if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
912 char *psz_codecs = config_GetPsz( p_playlist, "codec" );
913 if( psz_codecs )
915 char *psz_morecodecs;
916 asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs);
917 if( psz_morecodecs )
919 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
920 free( psz_morecodecs );
923 else
924 config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
925 free( psz_codecs );
927 #endif
930 * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
932 var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
933 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
934 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
935 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
936 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
937 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
938 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
939 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
940 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
942 /* Create volume callback system. */
943 var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
945 /* Create a variable for showing the interface (moved from playlist). */
946 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
947 var_SetBool( p_libvlc, "intf-show", true );
949 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
952 * Get input filenames given as commandline arguments
954 GetFilenames( p_libvlc, i_argc, ppsz_argv );
957 * Get --open argument
959 var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
960 var_Get( p_libvlc, "open", &val );
961 if ( val.psz_string != NULL && *val.psz_string )
963 playlist_t *p_playlist = pl_Yield( p_libvlc );
964 playlist_AddExt( p_playlist, val.psz_string, NULL, PLAYLIST_INSERT, 0,
965 -1, NULL, 0, true, false );
966 pl_Release( p_libvlc );
968 free( val.psz_string );
970 return VLC_SUCCESS;
974 * Cleanup a libvlc instance. The instance is not completely deallocated
975 * \param p_libvlc the instance to clean
977 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
979 intf_thread_t * p_intf = NULL;
980 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
982 /* Ask the interfaces to stop and destroy them */
983 msg_Dbg( p_libvlc, "removing all interfaces" );
984 while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
986 intf_StopThread( p_intf );
987 vlc_object_detach( p_intf );
988 vlc_object_release( p_intf ); /* for intf_Create() */
989 vlc_object_release( p_intf ); /* for vlc_object_find() */
992 #ifdef ENABLE_SOUT
993 /* Destroy VLM if created in libvlc_InternalInit */
994 if( priv->p_vlm )
996 vlm_Delete( priv->p_vlm );
998 #endif
1000 playlist_t *p_playlist = priv->p_playlist;
1001 /* Remove all services discovery */
1002 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
1003 playlist_ServicesDiscoveryKillAll( p_playlist );
1005 /* Free playlist */
1006 /* Any thread still running must not assume pl_Yield() succeeds. */
1007 msg_Dbg( p_libvlc, "removing playlist" );
1008 priv->p_playlist = NULL;
1009 vlc_object_kill( p_playlist ); /* <-- memory barrier for pl_Yield() */
1010 vlc_thread_join( p_playlist );
1011 vlc_object_release( p_playlist );
1013 /* Free interaction */
1014 msg_Dbg( p_libvlc, "removing interaction" );
1015 interaction_Destroy( priv->p_interaction );
1017 /* Free video outputs */
1018 msg_Dbg( p_libvlc, "removing all video outputs" );
1019 vlc_list_t *list = vlc_list_find (p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD);
1020 for (int i = 0; i < list->i_count; i++)
1021 vlc_object_release (list->p_values[i].p_object);
1022 vlc_list_release (list);
1024 stats_TimersDumpAll( p_libvlc );
1025 stats_TimersCleanAll( p_libvlc );
1027 #ifdef ENABLE_SOUT
1028 announce_handler_t * p_announce;
1030 /* Free announce handler(s?) */
1031 while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1032 FIND_CHILD ) ) )
1034 msg_Dbg( p_libvlc, "removing announce handler" );
1035 vlc_object_detach( p_announce );
1036 vlc_object_release( p_announce );
1037 announce_HandlerDestroy( p_announce );
1039 #endif
1041 /* Make sure all threads are completed before we start looking for
1042 * reference leaks and deinitializing core LibVLC subsytems. */
1043 vlc_mutex_lock (&priv->threads_lock);
1044 while (priv->threads_count)
1046 msg_Dbg (p_libvlc, "waiting for %u remaining threads",
1047 priv->threads_count);
1048 vlc_cond_wait (&priv->threads_wait, &priv->threads_lock);
1050 vlc_mutex_unlock (&priv->threads_lock);
1052 bool b_clean = true;
1053 FOREACH_ARRAY( input_item_t *p_del, priv->input_items )
1054 msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1055 p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1056 b_clean = false;
1057 FOREACH_END();
1058 assert( b_clean );
1059 ARRAY_RESET( priv->input_items );
1061 msg_Dbg( p_libvlc, "removing stats" );
1062 vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1063 FREENULL( p_libvlc->p_stats );
1065 return VLC_SUCCESS;
1069 * Destroy everything.
1070 * This function requests the running threads to finish, waits for their
1071 * termination, and destroys their structure.
1072 * It stops the thread systems: no instance can run after this has run
1073 * \param p_libvlc the instance to destroy
1075 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
1077 if( !p_libvlc )
1078 return VLC_EGENERIC;
1080 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
1082 #ifndef WIN32
1083 char* psz_pidfile = NULL;
1085 if( b_daemon )
1087 psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1088 if( psz_pidfile != NULL )
1090 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1091 if( unlink( psz_pidfile ) == -1 )
1093 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1094 psz_pidfile );
1097 free( psz_pidfile );
1099 #endif
1101 if( priv->p_memcpy_module )
1103 module_Unneed( p_libvlc, priv->p_memcpy_module );
1104 priv->p_memcpy_module = NULL;
1107 /* Free module bank. It is refcounted, so we call this each time */
1108 module_EndBank( p_libvlc );
1110 FREENULL( priv->psz_configfile );
1111 var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1112 p_libvlc->p_hotkeys );
1113 FREENULL( p_libvlc->p_hotkeys );
1115 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1116 i_instances--;
1118 if( i_instances == 0 )
1120 /* System specific cleaning code */
1121 system_End( p_libvlc );
1123 vlc_mutex_unlock( lock );
1125 msg_Flush( p_libvlc );
1126 msg_Destroy( p_libvlc );
1128 /* Destroy mutexes */
1129 vlc_mutex_destroy( &priv->config_lock );
1130 vlc_mutex_destroy( &priv->timer_lock );
1131 vlc_cond_destroy (&priv->threads_wait);
1132 vlc_mutex_destroy (&priv->threads_lock);
1134 vlc_object_release( p_libvlc );
1135 p_libvlc = NULL;
1137 /* Stop thread system: last one out please shut the door!
1138 * The number of initializations of the thread system is counted, we
1139 * can call this each time */
1140 vlc_threads_end ();
1142 return VLC_SUCCESS;
1146 * Add an interface plugin and run it
1148 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
1150 int i_err;
1151 intf_thread_t *p_intf = NULL;
1153 if( !p_libvlc )
1154 return VLC_EGENERIC;
1156 if( !psz_module ) /* requesting the default interface */
1158 char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1159 if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1161 #ifndef WIN32
1162 if( b_daemon )
1163 /* Daemon mode hack.
1164 * We prefer the dummy interface if none is specified. */
1165 psz_module = "dummy";
1166 else
1167 #endif
1168 msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1170 free( psz_interface );
1173 /* Try to create the interface */
1174 p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
1175 if( p_intf == NULL )
1177 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1178 psz_module );
1179 return VLC_EGENERIC;
1182 /* Try to run the interface */
1183 i_err = intf_RunThread( p_intf );
1184 if( i_err )
1186 vlc_object_detach( p_intf );
1187 vlc_object_release( p_intf );
1188 return i_err;
1191 return VLC_SUCCESS;
1194 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1195 ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1196 /*****************************************************************************
1197 * SetLanguage: set the interface language.
1198 *****************************************************************************
1199 * We set the LC_MESSAGES locale category for interface messages and buttons,
1200 * as well as the LC_CTYPE category for string sorting and possible wide
1201 * character support.
1202 *****************************************************************************/
1203 static void SetLanguage ( const char *psz_lang )
1205 #ifdef __APPLE__
1206 /* I need that under Darwin, please check it doesn't disturb
1207 * other platforms. --Meuuh */
1208 setenv( "LANG", psz_lang, 1 );
1210 #else
1211 /* We set LC_ALL manually because it is the only way to set
1212 * the language at runtime under eg. Windows. Beware that this
1213 * makes the environment unconsistent when libvlc is unloaded and
1214 * should probably be moved to a safer place like vlc.c. */
1215 static char psz_lcall[20];
1216 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1217 psz_lcall[19] = '\0';
1218 putenv( psz_lcall );
1219 #endif
1221 setlocale( LC_ALL, psz_lang );
1223 #endif
1226 static inline int LoadMessages (void)
1228 #if defined( ENABLE_NLS ) \
1229 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1230 /* Specify where to find the locales for current domain */
1231 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1232 static const char psz_path[] = LOCALEDIR;
1233 #else
1234 char psz_path[1024];
1235 if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
1236 vlc_global()->psz_vlcpath, "locale")
1237 >= (int)sizeof (psz_path))
1238 return -1;
1240 #endif
1241 if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1243 fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1244 " to directory %s\n", psz_path);
1245 return -1;
1248 /* LibVLC wants all messages in UTF-8.
1249 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1250 * and other functions that are not part of our text domain.
1252 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1254 fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1255 PACKAGE_NAME"\n");
1256 // Unbinds the text domain to avoid broken encoding
1257 bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1258 return -1;
1261 /* LibVLC does NOT set the default textdomain, since it is a library.
1262 * This could otherwise break programs using LibVLC (other than VLC).
1263 * textdomain (PACKAGE_NAME);
1265 #endif
1266 return 0;
1269 /*****************************************************************************
1270 * GetFilenames: parse command line options which are not flags
1271 *****************************************************************************
1272 * Parse command line for input files as well as their associated options.
1273 * An option always follows its associated input and begins with a ":".
1274 *****************************************************************************/
1275 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1277 int i_opt, i_options;
1279 /* We assume that the remaining parameters are filenames
1280 * and their input options */
1281 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1283 i_options = 0;
1285 /* Count the input options */
1286 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1288 i_options++;
1289 i_opt--;
1292 /* TODO: write an internal function of this one, to avoid
1293 * unnecessary lookups. */
1295 playlist_t *p_playlist = pl_Yield( p_vlc );
1296 playlist_AddExt( p_playlist, ppsz_argv[i_opt], NULL, PLAYLIST_INSERT,
1297 0, -1, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ),
1298 i_options, true, false );
1299 pl_Release( p_vlc );
1302 return VLC_SUCCESS;
1305 /*****************************************************************************
1306 * Help: print program help
1307 *****************************************************************************
1308 * Print a short inline help. Message interface is initialized at this stage.
1309 *****************************************************************************/
1310 static inline void print_help_on_full_help( void )
1312 utf8_fprintf( stdout, "\n" );
1313 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
1316 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1318 #ifdef WIN32
1319 ShowConsole( true );
1320 #endif
1322 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1324 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1325 Usage( p_this, "help" );
1326 Usage( p_this, "main" );
1327 print_help_on_full_help();
1329 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1331 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1332 Usage( p_this, NULL );
1333 print_help_on_full_help();
1335 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
1337 utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1338 Usage( p_this, NULL );
1340 else if( psz_help_name )
1342 Usage( p_this, psz_help_name );
1345 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1346 PauseConsole();
1347 #endif
1350 /*****************************************************************************
1351 * Usage: print module usage
1352 *****************************************************************************
1353 * Print a short inline help. Message interface is initialized at this stage.
1354 *****************************************************************************/
1355 # define COL(x) "\033[" #x ";1m"
1356 # define RED COL(31)
1357 # define GREEN COL(32)
1358 # define YELLOW COL(33)
1359 # define BLUE COL(34)
1360 # define MAGENTA COL(35)
1361 # define CYAN COL(36)
1362 # define WHITE COL(0)
1363 # define GRAY "\033[0m"
1364 static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
1366 if( !p_item ) return;
1367 if( b_color )
1369 utf8_fprintf( stdout, RED" %s:\n"GRAY,
1370 p_item->psz_text );
1371 if( b_description && p_item->psz_longtext )
1372 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
1373 p_item->psz_longtext );
1375 else
1377 utf8_fprintf( stdout, " %s:\n", p_item->psz_text );
1378 if( b_description && p_item->psz_longtext )
1379 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1383 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1385 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1386 /* short option ------' | | | | | | |
1387 * option name ------------' | | | | | |
1388 * <bra ---------------------' | | | | |
1389 * option type or "" ----------' | | | |
1390 * ket> -------------------------' | | |
1391 * padding spaces -----------------' | |
1392 * comment --------------------------' |
1393 * comment suffix ---------------------'
1395 * The purpose of having bra and ket is that we might i18n them as well.
1398 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1399 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
1401 #define LINE_START 8
1402 #define PADDING_SPACES 25
1403 #ifdef WIN32
1404 # define OPTION_VALUE_SEP "="
1405 #else
1406 # define OPTION_VALUE_SEP " "
1407 #endif
1408 vlc_list_t *p_list = NULL;
1409 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1410 char psz_spaces_longtext[LINE_START+3];
1411 char psz_format[sizeof(COLOR_FORMAT_STRING)];
1412 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1413 char psz_buffer[10000];
1414 char psz_short[4];
1415 int i_index;
1416 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1417 int i_width_description = i_width + PADDING_SPACES - 1;
1418 bool b_advanced = config_GetInt( p_this, "advanced" ) > 0;
1419 bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1420 bool b_description_hack;
1421 bool b_color = config_GetInt( p_this, "color" ) > 0;
1422 bool b_has_advanced = false;
1424 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1425 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1426 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1427 psz_spaces_longtext[LINE_START+2] = '\0';
1428 #ifndef WIN32
1429 if( !isatty( 1 ) )
1430 #endif
1431 b_color = false; // don't put color control codes in a .txt file
1433 if( b_color )
1435 strcpy( psz_format, COLOR_FORMAT_STRING );
1436 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1438 else
1440 strcpy( psz_format, FORMAT_STRING );
1441 strcpy( psz_format_bool, FORMAT_STRING );
1444 /* List all modules */
1445 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1447 /* Ugly hack to make sure that the help options always come first
1448 * (part 1) */
1449 if( !psz_module_name )
1450 Usage( p_this, "help" );
1452 /* Enumerate the config for each module */
1453 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1455 bool b_help_module;
1456 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1457 module_config_t *p_item = NULL;
1458 module_config_t *p_section = NULL;
1459 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1461 if( psz_module_name && strcmp( psz_module_name,
1462 p_parser->psz_object_name ) )
1464 char *const *pp_shortcut = p_parser->pp_shortcuts;
1465 while( *pp_shortcut )
1467 if( !strcmp( psz_module_name, *pp_shortcut ) )
1468 break;
1469 pp_shortcut ++;
1471 if( !*pp_shortcut )
1472 continue;
1475 /* Ignore modules without config options */
1476 if( !p_parser->i_config_items )
1478 continue;
1481 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1482 /* Ugly hack to make sure that the help options always come first
1483 * (part 2) */
1484 if( !psz_module_name && b_help_module )
1485 continue;
1487 /* Ignore modules with only advanced config options if requested */
1488 if( !b_advanced )
1490 for( p_item = p_parser->p_config;
1491 p_item < p_end;
1492 p_item++ )
1494 if( (p_item->i_type & CONFIG_ITEM) &&
1495 !p_item->b_advanced ) break;
1499 /* Print name of module */
1500 if( strcmp( "main", p_parser->psz_object_name ) )
1502 if( b_color )
1503 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1504 p_parser->psz_longname );
1505 else
1506 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1508 if( p_parser->psz_help )
1510 if( b_color )
1511 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1512 else
1513 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1516 /* Print module options */
1517 for( p_item = p_parser->p_config;
1518 p_item < p_end;
1519 p_item++ )
1521 char *psz_text, *psz_spaces = psz_spaces_text;
1522 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1523 const char *psz_suf = "", *psz_prefix = NULL;
1524 signed int i;
1525 size_t i_cur_width;
1527 /* Skip removed options */
1528 if( p_item->b_removed )
1530 continue;
1532 /* Skip advanced options if requested */
1533 if( p_item->b_advanced && !b_advanced )
1535 b_has_advanced = true;
1536 continue;
1539 switch( p_item->i_type )
1541 case CONFIG_HINT_CATEGORY:
1542 case CONFIG_HINT_USAGE:
1543 if( !strcmp( "main", p_parser->psz_object_name ) )
1545 if( b_color )
1546 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1547 p_item->psz_text );
1548 else
1549 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1551 if( b_description && p_item->psz_longtext )
1553 if( b_color )
1554 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1555 p_item->psz_longtext );
1556 else
1557 utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1559 break;
1561 case CONFIG_HINT_SUBCATEGORY:
1562 if( strcmp( "main", p_parser->psz_object_name ) )
1563 break;
1564 case CONFIG_SECTION:
1565 p_section = p_item;
1566 break;
1568 case CONFIG_ITEM_STRING:
1569 case CONFIG_ITEM_FILE:
1570 case CONFIG_ITEM_DIRECTORY:
1571 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1572 case CONFIG_ITEM_MODULE_CAT:
1573 case CONFIG_ITEM_MODULE_LIST:
1574 case CONFIG_ITEM_MODULE_LIST_CAT:
1575 case CONFIG_ITEM_FONT:
1576 case CONFIG_ITEM_PASSWORD:
1577 print_help_section( p_section, b_color, b_description );
1578 p_section = NULL;
1579 psz_bra = OPTION_VALUE_SEP "<";
1580 psz_type = _("string");
1581 psz_ket = ">";
1583 if( p_item->ppsz_list )
1585 psz_bra = OPTION_VALUE_SEP "{";
1586 psz_type = psz_buffer;
1587 psz_buffer[0] = '\0';
1588 for( i = 0; p_item->ppsz_list[i]; i++ )
1590 if( i ) strcat( psz_buffer, "," );
1591 strcat( psz_buffer, p_item->ppsz_list[i] );
1593 psz_ket = "}";
1595 break;
1596 case CONFIG_ITEM_INTEGER:
1597 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1598 print_help_section( p_section, b_color, b_description );
1599 p_section = NULL;
1600 psz_bra = OPTION_VALUE_SEP "<";
1601 psz_type = _("integer");
1602 psz_ket = ">";
1604 if( p_item->min.i || p_item->max.i )
1606 sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1607 p_item->min.i, p_item->max.i );
1608 psz_type = psz_buffer;
1611 if( p_item->i_list )
1613 psz_bra = OPTION_VALUE_SEP "{";
1614 psz_type = psz_buffer;
1615 psz_buffer[0] = '\0';
1616 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1618 if( i ) strcat( psz_buffer, ", " );
1619 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1620 p_item->pi_list[i],
1621 p_item->ppsz_list_text[i] );
1623 psz_ket = "}";
1625 break;
1626 case CONFIG_ITEM_FLOAT:
1627 print_help_section( p_section, b_color, b_description );
1628 p_section = NULL;
1629 psz_bra = OPTION_VALUE_SEP "<";
1630 psz_type = _("float");
1631 psz_ket = ">";
1632 if( p_item->min.f || p_item->max.f )
1634 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1635 p_item->min.f, p_item->max.f );
1636 psz_type = psz_buffer;
1638 break;
1639 case CONFIG_ITEM_BOOL:
1640 print_help_section( p_section, b_color, b_description );
1641 p_section = NULL;
1642 psz_bra = ""; psz_type = ""; psz_ket = "";
1643 if( !b_help_module )
1645 psz_suf = p_item->value.i ? _(" (default enabled)") :
1646 _(" (default disabled)");
1648 break;
1651 if( !psz_type )
1653 continue;
1656 /* Add short option if any */
1657 if( p_item->i_short )
1659 sprintf( psz_short, "-%c,", p_item->i_short );
1661 else
1663 strcpy( psz_short, " " );
1666 i = PADDING_SPACES - strlen( p_item->psz_name )
1667 - strlen( psz_bra ) - strlen( psz_type )
1668 - strlen( psz_ket ) - 1;
1670 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1672 psz_prefix = ", --no-";
1673 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1676 if( i < 0 )
1678 psz_spaces[0] = '\n';
1679 i = 0;
1681 else
1683 psz_spaces[i] = '\0';
1686 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1688 utf8_fprintf( stdout, psz_format_bool, psz_short,
1689 p_item->psz_name, psz_prefix, p_item->psz_name,
1690 psz_bra, psz_type, psz_ket, psz_spaces );
1692 else
1694 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1695 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1698 psz_spaces[i] = ' ';
1700 /* We wrap the rest of the output */
1701 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1702 b_description_hack = b_description;
1704 description:
1705 psz_text = psz_buffer;
1706 i_cur_width = b_description && !b_description_hack
1707 ? i_width_description
1708 : i_width;
1709 while( *psz_text )
1711 char *psz_parser, *psz_word;
1712 size_t i_end = strlen( psz_text );
1714 /* If the remaining text fits in a line, print it. */
1715 if( i_end <= i_cur_width )
1717 if( b_color )
1719 if( !b_description || b_description_hack )
1720 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1721 else
1722 utf8_fprintf( stdout, "%s\n", psz_text );
1724 else
1726 utf8_fprintf( stdout, "%s\n", psz_text );
1728 break;
1731 /* Otherwise, eat as many words as possible */
1732 psz_parser = psz_text;
1735 psz_word = psz_parser;
1736 psz_parser = strchr( psz_word, ' ' );
1737 /* If no space was found, we reached the end of the text
1738 * block; otherwise, we skip the space we just found. */
1739 psz_parser = psz_parser ? psz_parser + 1
1740 : psz_text + i_end;
1742 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1744 /* We cut a word in one of these cases:
1745 * - it's the only word in the line and it's too long.
1746 * - we used less than 80% of the width and the word we are
1747 * going to wrap is longer than 40% of the width, and even
1748 * if the word would have fit in the next line. */
1749 if( psz_word == psz_text
1750 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1751 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1753 char c = psz_text[i_cur_width];
1754 psz_text[i_cur_width] = '\0';
1755 if( b_color )
1757 if( !b_description || b_description_hack )
1758 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1759 psz_text, psz_spaces );
1760 else
1761 utf8_fprintf( stdout, "%s\n%s",
1762 psz_text, psz_spaces );
1764 else
1766 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1768 psz_text += i_cur_width;
1769 psz_text[0] = c;
1771 else
1773 psz_word[-1] = '\0';
1774 if( b_color )
1776 if( !b_description || b_description_hack )
1777 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1778 psz_text, psz_spaces );
1779 else
1780 utf8_fprintf( stdout, "%s\n%s",
1781 psz_text, psz_spaces );
1783 else
1785 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1787 psz_text = psz_word;
1791 if( b_description_hack && p_item->psz_longtext )
1793 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1794 b_description_hack = false;
1795 psz_spaces = psz_spaces_longtext;
1796 utf8_fprintf( stdout, "%s", psz_spaces );
1797 goto description;
1802 if( b_has_advanced )
1804 if( b_color )
1805 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1806 _( "add --advanced to your command line to see advanced options."));
1807 else
1808 utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
1809 _( "add --advanced to your command line to see advanced options."));
1812 /* Release the module list */
1813 vlc_list_release( p_list );
1816 /*****************************************************************************
1817 * ListModules: list the available modules with their description
1818 *****************************************************************************
1819 * Print a list of all available modules (builtins and plugins) and a short
1820 * description for each one.
1821 *****************************************************************************/
1822 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1824 vlc_list_t *p_list = NULL;
1825 module_t *p_parser = NULL;
1826 char psz_spaces[22];
1827 int i_index;
1829 bool b_color = config_GetInt( p_this, "color" ) > 0;
1831 memset( psz_spaces, ' ', 22 );
1833 #ifdef WIN32
1834 ShowConsole( true );
1835 #endif
1837 /* List all modules */
1838 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1840 /* Enumerate each module */
1841 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1843 int i;
1845 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1847 /* Nasty hack, but right now I'm too tired to think about a nice
1848 * solution */
1849 i = 22 - strlen( p_parser->psz_object_name ) - 1;
1850 if( i < 0 ) i = 0;
1851 psz_spaces[i] = 0;
1853 if( b_color )
1854 utf8_fprintf( stdout, GREEN" %s%s "WHITE"%s\n"GRAY,
1855 p_parser->psz_object_name,
1856 psz_spaces,
1857 p_parser->psz_longname );
1858 else
1859 utf8_fprintf( stdout, " %s%s %s\n",
1860 p_parser->psz_object_name,
1861 psz_spaces, p_parser->psz_longname );
1863 if( b_verbose )
1865 char *const *pp_shortcut = p_parser->pp_shortcuts;
1866 while( *pp_shortcut )
1868 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1870 if( b_color )
1871 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
1872 *pp_shortcut );
1873 else
1874 utf8_fprintf( stdout, " s %s\n",
1875 *pp_shortcut );
1877 pp_shortcut++;
1879 if( p_parser->psz_capability )
1881 if( b_color )
1882 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
1883 p_parser->psz_capability,
1884 p_parser->i_score );
1885 else
1886 utf8_fprintf( stdout, " c %s (%d)\n",
1887 p_parser->psz_capability,
1888 p_parser->i_score );
1892 psz_spaces[i] = ' ';
1895 vlc_list_release( p_list );
1897 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1898 PauseConsole();
1899 #endif
1902 /*****************************************************************************
1903 * Version: print complete program version
1904 *****************************************************************************
1905 * Print complete program version and build number.
1906 *****************************************************************************/
1907 static void Version( void )
1909 #ifdef WIN32
1910 ShowConsole( true );
1911 #endif
1913 utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1914 utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1915 VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1916 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1917 if( strcmp( VLC_Changeset(), "exported" ) )
1918 utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1919 VLC_Changeset() );
1920 utf8_fprintf( stdout, LICENSE_MSG );
1922 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1923 PauseConsole();
1924 #endif
1927 /*****************************************************************************
1928 * ShowConsole: On Win32, create an output console for debug messages
1929 *****************************************************************************
1930 * This function is useful only on Win32.
1931 *****************************************************************************/
1932 #ifdef WIN32 /* */
1933 static void ShowConsole( bool b_dofile )
1935 # ifndef UNDER_CE
1936 FILE *f_help = NULL;
1938 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1940 AllocConsole();
1941 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1942 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1943 * page (e.g. CP437 or CP850). */
1944 SetConsoleOutputCP (GetACP ());
1945 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1947 freopen( "CONOUT$", "w", stderr );
1948 freopen( "CONIN$", "r", stdin );
1950 if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1952 fclose( f_help );
1953 freopen( "vlc-help.txt", "wt", stdout );
1954 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1956 else freopen( "CONOUT$", "w", stdout );
1958 # endif
1960 #endif
1962 /*****************************************************************************
1963 * PauseConsole: On Win32, wait for a key press before closing the console
1964 *****************************************************************************
1965 * This function is useful only on Win32.
1966 *****************************************************************************/
1967 #ifdef WIN32 /* */
1968 static void PauseConsole( void )
1970 # ifndef UNDER_CE
1972 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1974 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1975 getchar();
1976 fclose( stdout );
1978 # endif
1980 #endif
1982 /*****************************************************************************
1983 * ConsoleWidth: Return the console width in characters
1984 *****************************************************************************
1985 * We use the stty shell command to get the console width; if this fails or
1986 * if the width is less than 80, we default to 80.
1987 *****************************************************************************/
1988 static int ConsoleWidth( void )
1990 unsigned i_width = 80;
1992 #ifndef WIN32
1993 FILE *file = popen( "stty size 2>/dev/null", "r" );
1994 if (file != NULL)
1996 if (fscanf (file, "%*u %u", &i_width) <= 0)
1997 i_width = 80;
1998 pclose( file );
2000 #else
2001 CONSOLE_SCREEN_BUFFER_INFO buf;
2003 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
2004 i_width = buf.dwSize.X;
2005 #endif
2007 return i_width;
2010 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2011 vlc_value_t old_val, vlc_value_t new_val, void *param)
2013 libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
2014 (void)psz_variable;
2015 (void)old_val;
2016 (void)param;
2018 if( new_val.i_int >= -1 )
2020 libvlc_priv (p_libvlc)->i_verbose = __MIN( new_val.i_int, 2 );
2022 return VLC_SUCCESS;
2025 /*****************************************************************************
2026 * InitDeviceValues: initialize device values
2027 *****************************************************************************
2028 * This function inits the dvd, vcd and cd-audio values
2029 *****************************************************************************/
2030 static void InitDeviceValues( libvlc_int_t *p_vlc )
2032 #ifdef HAVE_HAL
2033 LibHalContext * ctx = NULL;
2034 int i, i_devices;
2035 char **devices = NULL;
2036 char *block_dev = NULL;
2037 dbus_bool_t b_dvd;
2039 DBusConnection *p_connection = NULL;
2040 DBusError error;
2042 ctx = libhal_ctx_new();
2043 if( !ctx ) return;
2044 dbus_error_init( &error );
2045 p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2046 if( dbus_error_is_set( &error ) || !p_connection )
2048 libhal_ctx_free( ctx );
2049 dbus_error_free( &error );
2050 return;
2052 libhal_ctx_set_dbus_connection( ctx, p_connection );
2053 if( libhal_ctx_init( ctx, &error ) )
2055 if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2057 for( i = 0; i < i_devices; i++ )
2059 if( !libhal_device_property_exists( ctx, devices[i],
2060 "storage.cdrom.dvd", NULL ) )
2062 continue;
2064 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2065 "storage.cdrom.dvd", NULL );
2066 block_dev = libhal_device_get_property_string( ctx,
2067 devices[ i ], "block.device" , NULL );
2068 if( b_dvd )
2070 config_PutPsz( p_vlc, "dvd", block_dev );
2073 config_PutPsz( p_vlc, "vcd", block_dev );
2074 config_PutPsz( p_vlc, "cd-audio", block_dev );
2075 libhal_free_string( block_dev );
2077 libhal_free_string_array( devices );
2079 libhal_ctx_shutdown( ctx, NULL );
2080 dbus_connection_unref( p_connection );
2081 libhal_ctx_free( ctx );
2083 else
2085 msg_Warn( p_vlc, "Unable to get HAL device properties" );
2087 #else
2088 (void)p_vlc;
2089 #endif /* HAVE_HAL */