1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Pretend we are a builtin module
29 *****************************************************************************/
30 #define MODULE_NAME main
31 #define MODULE_PATH main
34 /*****************************************************************************
36 *****************************************************************************/
38 #include <vlc/input.h>
40 #include <errno.h> /* ENOMEM */
41 #include <stdio.h> /* sprintf() */
42 #include <string.h> /* strerror() */
43 #include <stdlib.h> /* free() */
46 # include <netinet/in.h> /* BSD: struct in_addr */
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
55 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
56 # include "extras/getopt.h"
64 # include <hal/libhal.h>
67 #include "vlc_cpu.h" /* CPU detection */
68 #include "os_specific.h"
70 #include "vlc_error.h"
72 #include "vlc_playlist.h"
73 #include "vlc_interface.h"
75 #include "audio_output.h"
77 #include "vlc_video.h"
78 #include "video_output.h"
80 #include "stream_output.h"
84 /*****************************************************************************
85 * The evil global variable. We handle it with care, don't worry.
86 *****************************************************************************/
87 static libvlc_t libvlc;
88 static libvlc_t * p_libvlc;
89 static vlc_t * p_static_vlc;
91 /*****************************************************************************
93 *****************************************************************************/
94 static void SetLanguage ( char const * );
95 static int GetFilenames ( vlc_t *, int, char *[] );
96 static void Usage ( vlc_t *, char const *psz_module_name );
97 static void ListModules ( vlc_t * );
98 static void Version ( void );
101 static void ShowConsole ( void );
102 static void PauseConsole ( void );
104 static int ConsoleWidth ( void );
106 static int VerboseCallback( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 static void InitDeviceValues( vlc_t * );
111 /*****************************************************************************
112 * vlc_current_object: return the current object.
113 *****************************************************************************
114 * If i_object is non-zero, return the corresponding object. Otherwise,
115 * return the statically allocated p_vlc object.
116 *****************************************************************************/
117 vlc_t * vlc_current_object( int i_object )
121 return vlc_object_get( p_libvlc, i_object );
127 /*****************************************************************************
128 * VLC_Version: return the libvlc version.
129 *****************************************************************************
130 * This function returns full version string (numeric version and codename).
131 *****************************************************************************/
132 char const * VLC_Version( void )
134 return VERSION_MESSAGE;
137 /*****************************************************************************
138 * VLC_Error: strerror() equivalent
139 *****************************************************************************
140 * This function returns full version string (numeric version and codename).
141 *****************************************************************************/
142 char const * VLC_Error( int i_err )
144 return vlc_error( i_err );
147 /*****************************************************************************
148 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
149 *****************************************************************************
150 * This function allocates a vlc_t structure and returns a negative value
151 * in case of failure. Also, the thread system is initialized.
152 *****************************************************************************/
153 int VLC_Create( void )
156 vlc_t * p_vlc = NULL;
159 /* &libvlc never changes, so we can safely call this multiple times. */
162 /* vlc_threads_init *must* be the first internal call! No other call is
163 * allowed before the thread system has been initialized. */
164 i_ret = vlc_threads_init( p_libvlc );
170 /* Now that the thread system is initialized, we don't have much, but
171 * at least we have var_Create */
172 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
173 var_Get( p_libvlc, "libvlc", &lockval );
174 vlc_mutex_lock( lockval.p_address );
175 if( !libvlc.b_ready )
179 /* Guess what CPU we have */
180 libvlc.i_cpu = CPUCapabilities();
182 /* Find verbosity from VLC_VERBOSE environment variable */
183 psz_env = getenv( "VLC_VERBOSE" );
184 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
186 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
187 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
189 libvlc.b_color = VLC_FALSE;
192 /* Initialize message queue */
193 msg_Create( p_libvlc );
195 /* Announce who we are */
196 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
197 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
199 /* The module bank will be initialized later */
200 libvlc.p_module_bank = NULL;
202 libvlc.b_ready = VLC_TRUE;
204 vlc_mutex_unlock( lockval.p_address );
205 var_Destroy( p_libvlc, "libvlc" );
207 /* Allocate a vlc object */
208 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
213 p_vlc->thread_id = 0;
215 p_vlc->psz_object_name = "root";
217 /* Initialize mutexes */
218 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
220 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
221 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
224 /* Store our newly allocated structure in the global list */
225 vlc_object_attach( p_vlc, p_libvlc );
227 /* Store data for the non-reentrant API */
228 p_static_vlc = p_vlc;
230 return p_vlc->i_object_id;
233 /*****************************************************************************
234 * VLC_Init: initialize a vlc_t structure.
235 *****************************************************************************
236 * This function initializes a previously allocated vlc_t structure:
238 * - gettext initialization
239 * - message queue, module bank and playlist initialization
240 * - configuration and commandline parsing
241 *****************************************************************************/
242 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
244 char p_capabilities[200];
250 vlc_bool_t b_exit = VLC_FALSE;
251 vlc_t * p_vlc = vlc_current_object( i_object );
252 module_t *p_help_module;
253 playlist_t *p_playlist;
261 * System specific initialization code
263 system_Init( p_vlc, &i_argc, ppsz_argv );
265 /* Get the executable name (similar to the basename command) */
268 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
271 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
277 p_vlc->psz_object_name = "vlc";
281 * Support for gettext
285 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
286 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
288 /* Initialize the module bank and load the configuration of the
289 * main module. We need to do this at this stage to be able to display
290 * a short help if required by the user. (short help == main module
292 module_InitBank( p_vlc );
294 /* Hack: insert the help module here */
295 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
296 if( p_help_module == NULL )
298 module_EndBank( p_vlc );
299 if( i_object ) vlc_object_release( p_vlc );
302 p_help_module->psz_object_name = "help";
303 p_help_module->psz_longname = N_("Help options");
304 config_Duplicate( p_help_module, p_help_config );
305 vlc_object_attach( p_help_module, libvlc.p_module_bank );
308 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
310 vlc_object_detach( p_help_module );
311 config_Free( p_help_module );
312 vlc_object_destroy( p_help_module );
313 module_EndBank( p_vlc );
314 if( i_object ) vlc_object_release( p_vlc );
318 /* Check for short help option */
319 if( config_GetInt( p_vlc, "help" ) )
321 fprintf( stdout, _("Usage: %s [options] [items]...\n"),
322 p_vlc->psz_object_name );
323 Usage( p_vlc, "main" );
324 Usage( p_vlc, "help" );
327 /* Check for version option */
328 else if( config_GetInt( p_vlc, "version" ) )
334 /* Set the config file stuff */
335 p_vlc->psz_homedir = config_GetHomeDir();
336 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
338 /* Check for plugins cache options */
339 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
341 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
344 /* Hack: remove the help module here */
345 vlc_object_detach( p_help_module );
348 /* Will be re-done properly later on */
349 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
351 /* Check for daemon mode */
353 if( config_GetInt( p_vlc, "daemon" ) )
356 if( daemon( 0, 0) != 0 )
358 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
362 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
367 if( ( i_pid = fork() ) < 0 )
369 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
374 /* This is the parent, exit right now */
375 msg_Dbg( p_vlc, "closing parent process" );
380 /* We are the child */
381 msg_Dbg( p_vlc, "daemon spawned" );
382 close( STDIN_FILENO );
383 close( STDOUT_FILENO );
384 close( STDERR_FILENO );
386 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
394 config_Free( p_help_module );
395 vlc_object_destroy( p_help_module );
396 module_EndBank( p_vlc );
397 if( i_object ) vlc_object_release( p_vlc );
401 /* Check for translation config option */
402 #if defined( ENABLE_NLS ) \
403 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
405 /* This ain't really nice to have to reload the config here but it seems
406 * the only way to do it. */
407 config_LoadConfigFile( p_vlc, "main" );
408 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
410 /* Check if the user specified a custom language */
411 psz_language = config_GetPsz( p_vlc, "language" );
412 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
414 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
416 /* Reset the default domain */
417 SetLanguage( psz_language );
419 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
420 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
422 textdomain( PACKAGE_NAME );
424 #if defined( ENABLE_UTF8 )
425 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
428 module_EndBank( p_vlc );
429 module_InitBank( p_vlc );
430 config_LoadConfigFile( p_vlc, "main" );
431 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
432 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
434 if( psz_language ) free( psz_language );
438 * Load the builtins and plugins into the module_bank.
439 * We have to do it before config_Load*() because this also gets the
440 * list of configuration options exported by each module and loads their
443 module_LoadBuiltins( p_vlc );
444 module_LoadPlugins( p_vlc );
450 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
451 libvlc.p_module_bank->i_children );
453 /* Hack: insert the help module here */
454 vlc_object_attach( p_help_module, libvlc.p_module_bank );
457 /* Check for help on modules */
458 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
460 Usage( p_vlc, p_tmp );
464 /* Check for long help option */
465 else if( config_GetInt( p_vlc, "longhelp" ) )
467 Usage( p_vlc, NULL );
470 /* Check for module list option */
471 else if( config_GetInt( p_vlc, "list" ) )
473 ListModules( p_vlc );
477 /* Check for config file options */
478 if( config_GetInt( p_vlc, "reset-config" ) )
480 vlc_object_detach( p_help_module );
481 config_ResetAll( p_vlc );
482 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
483 config_SaveConfigFile( p_vlc, NULL );
484 vlc_object_attach( p_help_module, libvlc.p_module_bank );
486 if( config_GetInt( p_vlc, "save-config" ) )
488 vlc_object_detach( p_help_module );
489 config_LoadConfigFile( p_vlc, NULL );
490 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
491 config_SaveConfigFile( p_vlc, NULL );
492 vlc_object_attach( p_help_module, libvlc.p_module_bank );
495 /* Hack: remove the help module here */
496 vlc_object_detach( p_help_module );
501 config_Free( p_help_module );
502 vlc_object_destroy( p_help_module );
503 module_EndBank( p_vlc );
504 if( i_object ) vlc_object_release( p_vlc );
511 InitDeviceValues( p_vlc );
514 * Override default configuration with config file settings
516 config_LoadConfigFile( p_vlc, NULL );
518 /* Hack: insert the help module here */
519 vlc_object_attach( p_help_module, libvlc.p_module_bank );
523 * Override configuration with command line settings
525 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
529 /* Pause the console because it's destroyed when we exit */
530 fprintf( stderr, "The command line options couldn't be loaded, check "
531 "that they are valid.\n" );
534 vlc_object_detach( p_help_module );
535 config_Free( p_help_module );
536 vlc_object_destroy( p_help_module );
537 module_EndBank( p_vlc );
538 if( i_object ) vlc_object_release( p_vlc );
542 /* Hack: remove the help module here */
543 vlc_object_detach( p_help_module );
544 config_Free( p_help_module );
545 vlc_object_destroy( p_help_module );
549 * System specific configuration
551 system_Configure( p_vlc, &i_argc, ppsz_argv );
554 * Message queue options
557 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
558 if( config_GetInt( p_vlc, "quiet" ) )
562 var_Set( p_vlc, "verbose", val );
564 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
565 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
567 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
570 * Output messages that may still be in the queue
574 /* p_vlc initialization. FIXME ? */
576 #if defined( __i386__ )
577 if( !config_GetInt( p_vlc, "mmx" ) )
578 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
579 if( !config_GetInt( p_vlc, "3dn" ) )
580 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
581 if( !config_GetInt( p_vlc, "mmxext" ) )
582 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
583 if( !config_GetInt( p_vlc, "sse" ) )
584 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
585 if( !config_GetInt( p_vlc, "sse2" ) )
586 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
588 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
589 if( !config_GetInt( p_vlc, "altivec" ) )
590 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
593 #define PRINT_CAPABILITY( capability, string ) \
594 if( libvlc.i_cpu & capability ) \
596 strncat( p_capabilities, string " ", \
597 sizeof(p_capabilities) - strlen(p_capabilities) ); \
598 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
601 p_capabilities[0] = '\0';
602 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
603 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
604 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
605 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
606 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
607 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
608 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
609 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
610 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
611 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
612 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
615 * Choose the best memcpy module
617 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
619 if( p_vlc->pf_memcpy == NULL )
621 p_vlc->pf_memcpy = memcpy;
624 if( p_vlc->pf_memset == NULL )
626 p_vlc->pf_memset = memset;
630 * Initialize hotkey handling
632 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
633 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
634 /* Do a copy (we don't need to modify the strings) */
635 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
638 * Initialize playlist and get commandline files
640 p_playlist = playlist_Create( p_vlc );
643 msg_Err( p_vlc, "playlist initialization failed" );
644 if( p_vlc->p_memcpy_module != NULL )
646 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
648 module_EndBank( p_vlc );
649 if( i_object ) vlc_object_release( p_vlc );
653 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
654 if( psz_modules && *psz_modules )
656 /* Add service discovery modules */
657 playlist_AddSDModules( p_playlist, psz_modules );
659 if( psz_modules ) free( psz_modules );
662 * Load background interfaces
664 psz_modules = config_GetPsz( p_vlc, "extraintf" );
665 psz_control = config_GetPsz( p_vlc, "control" );
667 if( psz_modules && *psz_modules && psz_control && *psz_control )
669 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
670 strlen( psz_control ) + 1 );
671 sprintf( psz_modules, "%s,%s", psz_modules, psz_control );
673 else if( psz_control && *psz_control )
675 if( psz_modules ) free( psz_modules );
676 psz_modules = strdup( psz_control );
679 psz_parser = psz_modules;
680 while ( psz_parser && *psz_parser )
682 char *psz_module, *psz_temp;
683 psz_module = psz_parser;
684 psz_parser = strchr( psz_module, ',' );
690 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
693 sprintf( psz_temp, "%s,none", psz_module );
694 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
704 * Allways load the hotkeys interface if it exists
706 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
709 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
711 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
712 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
713 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
714 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
715 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
716 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
717 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
718 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
719 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
720 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
721 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
722 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
725 * Get input filenames given as commandline arguments
727 GetFilenames( p_vlc, i_argc, ppsz_argv );
729 if( i_object ) vlc_object_release( p_vlc );
733 /*****************************************************************************
734 * VLC_AddIntf: add an interface
735 *****************************************************************************
736 * This function opens an interface plugin and runs it. If b_block is set
737 * to 0, VLC_AddIntf will return immediately and let the interface run in a
738 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
739 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
740 * the playlist when it is completely initialised.
741 *****************************************************************************/
742 int VLC_AddIntf( int i_object, char const *psz_module,
743 vlc_bool_t b_block, vlc_bool_t b_play )
746 intf_thread_t *p_intf;
747 vlc_t *p_vlc = vlc_current_object( i_object );
755 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
758 * We prefer the dummy interface if none is specified. */
759 char *psz_interface = config_GetPsz( p_vlc, "intf" );
760 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
761 if( psz_interface ) free( psz_interface );
765 /* Try to create the interface */
766 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
770 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
771 if( i_object ) vlc_object_release( p_vlc );
775 /* Interface doesn't handle play on start so do it ourselves */
776 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
778 /* Try to run the interface */
779 p_intf->b_play = b_play;
780 p_intf->b_block = b_block;
781 i_err = intf_RunThread( p_intf );
784 vlc_object_detach( p_intf );
785 intf_Destroy( p_intf );
786 if( i_object ) vlc_object_release( p_vlc );
790 if( i_object ) vlc_object_release( p_vlc );
794 /*****************************************************************************
795 * VLC_Die: ask vlc to die.
796 *****************************************************************************
797 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
798 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
799 *****************************************************************************/
800 int VLC_Die( int i_object )
802 vlc_t *p_vlc = vlc_current_object( i_object );
809 p_vlc->b_die = VLC_TRUE;
811 if( i_object ) vlc_object_release( p_vlc );
815 /*****************************************************************************
816 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
817 *****************************************************************************/
818 int VLC_CleanUp( int i_object )
820 intf_thread_t * p_intf;
821 playlist_t * p_playlist;
822 vout_thread_t * p_vout;
823 aout_instance_t * p_aout;
824 announce_handler_t * p_announce;
825 vlc_t *p_vlc = vlc_current_object( i_object );
827 /* Check that the handle is valid */
834 * Ask the interfaces to stop and destroy them
836 msg_Dbg( p_vlc, "removing all interfaces" );
837 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
839 intf_StopThread( p_intf );
840 vlc_object_detach( p_intf );
841 vlc_object_release( p_intf );
842 intf_Destroy( p_intf );
848 msg_Dbg( p_vlc, "removing all playlists" );
849 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
852 vlc_object_detach( p_playlist );
853 vlc_object_release( p_playlist );
854 playlist_Destroy( p_playlist );
860 msg_Dbg( p_vlc, "removing all video outputs" );
861 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
863 vlc_object_detach( p_vout );
864 vlc_object_release( p_vout );
865 vout_Destroy( p_vout );
871 msg_Dbg( p_vlc, "removing all audio outputs" );
872 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
874 vlc_object_detach( (vlc_object_t *)p_aout );
875 vlc_object_release( (vlc_object_t *)p_aout );
876 aout_Delete( p_aout );
880 * Free announce handler(s?)
882 msg_Dbg( p_vlc, "removing announce handler" );
883 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
886 vlc_object_detach( p_announce );
887 vlc_object_release( p_announce );
888 announce_HandlerDestroy( p_announce );
891 if( i_object ) vlc_object_release( p_vlc );
895 /*****************************************************************************
896 * VLC_Destroy: Destroy everything.
897 *****************************************************************************
898 * This function requests the running threads to finish, waits for their
899 * termination, and destroys their structure.
900 *****************************************************************************/
901 int VLC_Destroy( int i_object )
903 vlc_t *p_vlc = vlc_current_object( i_object );
911 * Free allocated memory
913 if( p_vlc->p_memcpy_module )
915 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
916 p_vlc->p_memcpy_module = NULL;
922 module_EndBank( p_vlc );
924 if( p_vlc->psz_homedir )
926 free( p_vlc->psz_homedir );
927 p_vlc->psz_homedir = NULL;
930 if( p_vlc->psz_configfile )
932 free( p_vlc->psz_configfile );
933 p_vlc->psz_configfile = NULL;
936 if( p_vlc->p_hotkeys )
938 free( p_vlc->p_hotkeys );
939 p_vlc->p_hotkeys = NULL;
943 * System specific cleaning code
947 /* Destroy mutexes */
948 vlc_mutex_destroy( &p_vlc->config_lock );
950 vlc_object_detach( p_vlc );
952 /* Release object before destroying it */
953 if( i_object ) vlc_object_release( p_vlc );
955 vlc_object_destroy( p_vlc );
957 /* Stop thread system: last one out please shut the door! */
958 vlc_threads_end( p_libvlc );
963 /*****************************************************************************
964 * VLC_VariableSet: set a vlc variable
965 *****************************************************************************/
966 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
968 vlc_t *p_vlc = vlc_current_object( i_object );
976 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
977 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
978 if( !strncmp( psz_var, "conf::", 6 ) )
980 module_config_t *p_item;
981 char const *psz_newvar = psz_var + 6;
983 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
987 switch( p_item->i_type )
989 case CONFIG_ITEM_BOOL:
990 config_PutInt( p_vlc, psz_newvar, value.b_bool );
992 case CONFIG_ITEM_INTEGER:
993 config_PutInt( p_vlc, psz_newvar, value.i_int );
995 case CONFIG_ITEM_FLOAT:
996 config_PutFloat( p_vlc, psz_newvar, value.f_float );
999 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1002 if( i_object ) vlc_object_release( p_vlc );
1007 i_ret = var_Set( p_vlc, psz_var, value );
1009 if( i_object ) vlc_object_release( p_vlc );
1013 /*****************************************************************************
1014 * VLC_VariableGet: get a vlc variable
1015 *****************************************************************************/
1016 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1018 vlc_t *p_vlc = vlc_current_object( i_object );
1026 i_ret = var_Get( p_vlc , psz_var, p_value );
1028 if( i_object ) vlc_object_release( p_vlc );
1032 /*****************************************************************************
1033 * VLC_AddTarget: adds a target for playing.
1034 *****************************************************************************
1035 * This function adds psz_target to the current playlist. If a playlist does
1036 * not exist, it will create one.
1037 *****************************************************************************/
1038 int VLC_AddTarget( int i_object, char const *psz_target,
1039 char const **ppsz_options, int i_options,
1040 int i_mode, int i_pos )
1043 playlist_t *p_playlist;
1044 vlc_t *p_vlc = vlc_current_object( i_object );
1051 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1053 if( p_playlist == NULL )
1055 msg_Dbg( p_vlc, "no playlist present, creating one" );
1056 p_playlist = playlist_Create( p_vlc );
1058 if( p_playlist == NULL )
1060 if( i_object ) vlc_object_release( p_vlc );
1061 return VLC_EGENERIC;
1064 vlc_object_yield( p_playlist );
1067 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1068 i_mode, i_pos, -1, ppsz_options, i_options);
1070 vlc_object_release( p_playlist );
1072 if( i_object ) vlc_object_release( p_vlc );
1076 /*****************************************************************************
1078 *****************************************************************************/
1079 int VLC_Play( int i_object )
1081 playlist_t * p_playlist;
1082 vlc_t *p_vlc = vlc_current_object( i_object );
1084 /* Check that the handle is valid */
1090 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1094 if( i_object ) vlc_object_release( p_vlc );
1098 playlist_Play( p_playlist );
1099 vlc_object_release( p_playlist );
1101 if( i_object ) vlc_object_release( p_vlc );
1105 /*****************************************************************************
1106 * VLC_Pause: toggle pause
1107 *****************************************************************************/
1108 int VLC_Pause( int i_object )
1110 playlist_t * p_playlist;
1111 vlc_t *p_vlc = vlc_current_object( i_object );
1113 /* Check that the handle is valid */
1119 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1123 if( i_object ) vlc_object_release( p_vlc );
1127 playlist_Pause( p_playlist );
1128 vlc_object_release( p_playlist );
1130 if( i_object ) vlc_object_release( p_vlc );
1134 /*****************************************************************************
1135 * VLC_Pause: toggle pause
1136 *****************************************************************************/
1137 int VLC_Stop( int i_object )
1139 playlist_t * p_playlist;
1140 vlc_t *p_vlc = vlc_current_object( i_object );
1142 /* Check that the handle is valid */
1148 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1152 if( i_object ) vlc_object_release( p_vlc );
1156 playlist_Stop( p_playlist );
1157 vlc_object_release( p_playlist );
1159 if( i_object ) vlc_object_release( p_vlc );
1163 /*****************************************************************************
1164 * VLC_IsPlaying: Query for Playlist Status
1165 *****************************************************************************/
1166 vlc_bool_t VLC_IsPlaying( int i_object )
1168 playlist_t * p_playlist;
1169 vlc_bool_t b_playing;
1171 vlc_t *p_vlc = vlc_current_object( i_object );
1173 /* Check that the handle is valid */
1179 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1183 if( i_object ) vlc_object_release( p_vlc );
1187 b_playing = playlist_IsPlaying( p_playlist );
1188 vlc_object_release( p_playlist );
1190 if( i_object ) vlc_object_release( p_vlc );
1195 * Get the current position in a input
1197 * Return the current position as a float
1198 * \note For some inputs, this will be unknown.
1200 * \param i_object a vlc object id
1201 * \return a float in the range of 0.0 - 1.0
1203 float VLC_PositionGet( int i_object )
1205 input_thread_t *p_input;
1207 vlc_t *p_vlc = vlc_current_object( i_object );
1209 /* Check that the handle is valid */
1215 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1219 if( i_object ) vlc_object_release( p_vlc );
1223 var_Get( p_input, "position", &val );
1224 vlc_object_release( p_input );
1226 if( i_object ) vlc_object_release( p_vlc );
1231 * Set the current position in a input
1233 * Set the current position in a input and then return
1234 * the current position as a float.
1235 * \note For some inputs, this will be unknown.
1237 * \param i_object a vlc object id
1238 * \param i_position a float in the range of 0.0 - 1.0
1239 * \return a float in the range of 0.0 - 1.0
1241 float VLC_PositionSet( int i_object, float i_position )
1243 input_thread_t *p_input;
1245 vlc_t *p_vlc = vlc_current_object( i_object );
1247 /* Check that the handle is valid */
1253 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1257 if( i_object ) vlc_object_release( p_vlc );
1261 val.f_float = i_position;
1262 var_Set( p_input, "position", val );
1263 var_Get( p_input, "position", &val );
1264 vlc_object_release( p_input );
1266 if( i_object ) vlc_object_release( p_vlc );
1271 * Get the current position in a input
1273 * Return the current position in seconds from the start.
1274 * \note For some inputs, this will be unknown.
1276 * \param i_object a vlc object id
1277 * \return the offset from 0:00 in seconds
1279 int VLC_TimeGet( int i_object )
1281 input_thread_t *p_input;
1283 vlc_t *p_vlc = vlc_current_object( i_object );
1285 /* Check that the handle is valid */
1291 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1295 if( i_object ) vlc_object_release( p_vlc );
1299 var_Get( p_input, "time", &val );
1300 vlc_object_release( p_input );
1302 if( i_object ) vlc_object_release( p_vlc );
1303 return val.i_time / 1000000;
1307 * Seek to a position in the current input
1309 * Seek i_seconds in the current input. If b_relative is set,
1310 * then the seek will be relative to the current position, otherwise
1311 * it will seek to i_seconds from the beginning of the input.
1312 * \note For some inputs, this will be unknown.
1314 * \param i_object a vlc object id
1315 * \param i_seconds seconds from current position or from beginning of input
1316 * \param b_relative seek relative from current position
1317 * \return VLC_SUCCESS on success
1319 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1321 input_thread_t *p_input;
1323 vlc_t *p_vlc = vlc_current_object( i_object );
1325 /* Check that the handle is valid */
1331 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1335 if( i_object ) vlc_object_release( p_vlc );
1341 val.i_time = i_seconds * 1000000;
1342 var_Set( p_input, "time-offset", val );
1346 val.i_time = i_seconds * 1000000;
1347 var_Set( p_input, "time", val );
1349 vlc_object_release( p_input );
1351 if( i_object ) vlc_object_release( p_vlc );
1356 * Get the total length of a input
1358 * Return the total length in seconds from the current input.
1359 * \note For some inputs, this will be unknown.
1361 * \param i_object a vlc object id
1362 * \return the length in seconds
1364 int VLC_LengthGet( int i_object )
1366 input_thread_t *p_input;
1368 vlc_t *p_vlc = vlc_current_object( i_object );
1370 /* Check that the handle is valid */
1376 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1380 if( i_object ) vlc_object_release( p_vlc );
1384 var_Get( p_input, "length", &val );
1385 vlc_object_release( p_input );
1387 if( i_object ) vlc_object_release( p_vlc );
1388 return val.i_time / 1000000;
1392 * Play the input faster than realtime
1394 * 2x, 4x, 8x faster than realtime
1395 * \note For some inputs, this will be impossible.
1397 * \param i_object a vlc object id
1398 * \return the current speedrate
1400 float VLC_SpeedFaster( int i_object )
1402 input_thread_t *p_input;
1404 vlc_t *p_vlc = vlc_current_object( i_object );
1406 /* Check that the handle is valid */
1412 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1416 if( i_object ) vlc_object_release( p_vlc );
1420 val.b_bool = VLC_TRUE;
1421 var_Set( p_input, "rate-faster", val );
1422 var_Get( p_input, "rate", &val );
1423 vlc_object_release( p_input );
1425 if( i_object ) vlc_object_release( p_vlc );
1426 return val.f_float / INPUT_RATE_DEFAULT;
1430 * Play the input slower than realtime
1432 * 1/2x, 1/4x, 1/8x slower than realtime
1433 * \note For some inputs, this will be impossible.
1435 * \param i_object a vlc object id
1436 * \return the current speedrate
1438 float VLC_SpeedSlower( int i_object )
1440 input_thread_t *p_input;
1442 vlc_t *p_vlc = vlc_current_object( i_object );
1444 /* Check that the handle is valid */
1450 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1454 if( i_object ) vlc_object_release( p_vlc );
1458 val.b_bool = VLC_TRUE;
1459 var_Set( p_input, "rate-slower", val );
1460 var_Get( p_input, "rate", &val );
1461 vlc_object_release( p_input );
1463 if( i_object ) vlc_object_release( p_vlc );
1464 return val.f_float / INPUT_RATE_DEFAULT;
1468 * Return the current playlist item
1470 * Returns the index of the playlistitem that is currently selected for play.
1471 * This is valid even if nothing is currently playing.
1473 * \param i_object a vlc object id
1474 * \return the current index
1476 int VLC_PlaylistIndex( int i_object )
1479 playlist_t * p_playlist;
1480 vlc_t *p_vlc = vlc_current_object( i_object );
1482 /* Check that the handle is valid */
1488 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1492 if( i_object ) vlc_object_release( p_vlc );
1496 i_index = p_playlist->i_index;
1497 vlc_object_release( p_playlist );
1499 if( i_object ) vlc_object_release( p_vlc );
1504 * Total amount of items in the playlist
1506 * \param i_object a vlc object id
1507 * \return amount of playlist items
1509 int VLC_PlaylistNumberOfItems( int i_object )
1512 playlist_t * p_playlist;
1513 vlc_t *p_vlc = vlc_current_object( i_object );
1515 /* Check that the handle is valid */
1521 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1525 if( i_object ) vlc_object_release( p_vlc );
1529 i_size = p_playlist->i_size;
1530 vlc_object_release( p_playlist );
1532 if( i_object ) vlc_object_release( p_vlc );
1537 * Next playlist item
1539 * Skip to the next playlistitem and play it.
1541 * \param i_object a vlc object id
1542 * \return VLC_SUCCESS on success
1544 int VLC_PlaylistNext( int i_object )
1546 playlist_t * p_playlist;
1547 vlc_t *p_vlc = vlc_current_object( i_object );
1549 /* Check that the handle is valid */
1555 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1559 if( i_object ) vlc_object_release( p_vlc );
1563 playlist_Next( p_playlist );
1564 vlc_object_release( p_playlist );
1566 if( i_object ) vlc_object_release( p_vlc );
1571 * Previous playlist item
1573 * Skip to the previous playlistitem and play it.
1575 * \param i_object a vlc object id
1576 * \return VLC_SUCCESS on success
1578 int VLC_PlaylistPrev( int i_object )
1580 playlist_t * p_playlist;
1581 vlc_t *p_vlc = vlc_current_object( i_object );
1583 /* Check that the handle is valid */
1589 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1593 if( i_object ) vlc_object_release( p_vlc );
1597 playlist_Prev( p_playlist );
1598 vlc_object_release( p_playlist );
1600 if( i_object ) vlc_object_release( p_vlc );
1605 /*****************************************************************************
1606 * VLC_PlaylistClear: Empty the playlist
1607 *****************************************************************************/
1608 int VLC_PlaylistClear( int i_object )
1611 playlist_t * p_playlist;
1612 vlc_t *p_vlc = vlc_current_object( i_object );
1614 /* Check that the handle is valid */
1620 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1624 if( i_object ) vlc_object_release( p_vlc );
1628 i_err = playlist_Clear( p_playlist );
1630 vlc_object_release( p_playlist );
1632 if( i_object ) vlc_object_release( p_vlc );
1639 * \param i_object a vlc object id
1640 * \param i_volume something in a range from 0-200
1641 * \return the new volume (range 0-200 %)
1643 int VLC_VolumeSet( int i_object, int i_volume )
1645 audio_volume_t i_vol = 0;
1646 vlc_t *p_vlc = vlc_current_object( i_object );
1648 /* Check that the handle is valid */
1654 if( i_volume >= 0 && i_volume <= 200 )
1656 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1657 aout_VolumeSet( p_vlc, i_vol );
1660 if( i_object ) vlc_object_release( p_vlc );
1661 return i_vol * 200 / AOUT_VOLUME_MAX;
1665 * Get the current volume
1667 * Retrieve the current volume.
1669 * \param i_object a vlc object id
1670 * \return the current volume (range 0-200 %)
1672 int VLC_VolumeGet( int i_object )
1674 audio_volume_t i_volume;
1675 vlc_t *p_vlc = vlc_current_object( i_object );
1677 /* Check that the handle is valid */
1683 aout_VolumeGet( p_vlc, &i_volume );
1685 if( i_object ) vlc_object_release( p_vlc );
1686 return i_volume*200/AOUT_VOLUME_MAX;
1690 * Mute/Unmute the volume
1692 * \param i_object a vlc object id
1693 * \return VLC_SUCCESS on success
1695 int VLC_VolumeMute( int i_object )
1697 vlc_t *p_vlc = vlc_current_object( i_object );
1699 /* Check that the handle is valid */
1705 aout_VolumeMute( p_vlc, NULL );
1707 if( i_object ) vlc_object_release( p_vlc );
1711 /*****************************************************************************
1712 * VLC_FullScreen: toggle fullscreen mode
1713 *****************************************************************************/
1714 int VLC_FullScreen( int i_object )
1716 vout_thread_t *p_vout;
1717 vlc_t *p_vlc = vlc_current_object( i_object );
1724 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1728 if( i_object ) vlc_object_release( p_vlc );
1732 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1733 vlc_object_release( p_vout );
1735 if( i_object ) vlc_object_release( p_vlc );
1739 /* following functions are local */
1741 /*****************************************************************************
1742 * SetLanguage: set the interface language.
1743 *****************************************************************************
1744 * We set the LC_MESSAGES locale category for interface messages and buttons,
1745 * as well as the LC_CTYPE category for string sorting and possible wide
1746 * character support.
1747 *****************************************************************************/
1748 static void SetLanguage ( char const *psz_lang )
1750 #if defined( ENABLE_NLS ) \
1751 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1754 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1758 if( psz_lang && !*psz_lang )
1760 # if defined( HAVE_LC_MESSAGES )
1761 setlocale( LC_MESSAGES, psz_lang );
1763 setlocale( LC_CTYPE, psz_lang );
1768 /* I need that under Darwin, please check it doesn't disturb
1769 * other platforms. --Meuuh */
1770 setenv( "LANG", psz_lang, 1 );
1772 #elif defined( SYS_BEOS ) || defined( WIN32 )
1773 /* We set LC_ALL manually because it is the only way to set
1774 * the language at runtime under eg. Windows. Beware that this
1775 * makes the environment unconsistent when libvlc is unloaded and
1776 * should probably be moved to a safer place like vlc.c. */
1777 static char psz_lcall[20];
1778 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1779 psz_lcall[19] = '\0';
1780 putenv( psz_lcall );
1783 setlocale( LC_ALL, psz_lang );
1786 /* Specify where to find the locales for current domain */
1787 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1788 psz_path = LOCALEDIR;
1790 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1794 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1796 fprintf( stderr, "warning: no domain %s in directory %s\n",
1797 PACKAGE_NAME, psz_path );
1800 /* Set the default domain */
1801 textdomain( PACKAGE_NAME );
1803 #if defined( ENABLE_UTF8 )
1804 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1810 /*****************************************************************************
1811 * GetFilenames: parse command line options which are not flags
1812 *****************************************************************************
1813 * Parse command line for input files as well as their associated options.
1814 * An option always follows its associated input and begins with a ":".
1815 *****************************************************************************/
1816 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1818 int i_opt, i_options;
1820 /* We assume that the remaining parameters are filenames
1821 * and their input options */
1822 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1826 /* Count the input options */
1827 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1833 /* TODO: write an internal function of this one, to avoid
1834 * unnecessary lookups. */
1835 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1836 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1838 PLAYLIST_INSERT, 0 );
1844 /*****************************************************************************
1845 * Usage: print program usage
1846 *****************************************************************************
1847 * Print a short inline help. Message interface is initialized at this stage.
1848 *****************************************************************************/
1849 static void Usage( vlc_t *p_this, char const *psz_module_name )
1851 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1852 /* short option ------' | | | | | | |
1853 * option name ------------' | | | | | |
1854 * <bra -------------------------' | | | | |
1855 * option type or "" --------------' | | | |
1856 * ket> -----------------------------' | | |
1857 * padding spaces ---------------------' | |
1858 * comment -------------------------------' |
1859 * comment suffix --------------------------'
1861 * The purpose of having bra and ket is that we might i18n them as well.
1863 #define LINE_START 8
1864 #define PADDING_SPACES 25
1867 module_config_t *p_item;
1868 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1869 char psz_spaces_longtext[LINE_START+3];
1870 char psz_format[sizeof(FORMAT_STRING)];
1871 char psz_buffer[10000];
1874 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1875 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1876 vlc_bool_t b_description;
1878 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1879 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1880 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1881 psz_spaces_longtext[LINE_START+2] = '\0';
1883 strcpy( psz_format, FORMAT_STRING );
1889 /* List all modules */
1890 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1892 /* Enumerate the config for each module */
1893 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1895 vlc_bool_t b_help_module;
1897 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1899 if( psz_module_name && strcmp( psz_module_name,
1900 p_parser->psz_object_name ) )
1905 /* Ignore modules without config options */
1906 if( !p_parser->i_config_items )
1911 /* Ignore modules with only advanced config options if requested */
1914 for( p_item = p_parser->p_config;
1915 p_item->i_type != CONFIG_HINT_END;
1918 if( (p_item->i_type & CONFIG_ITEM) &&
1919 !p_item->b_advanced ) break;
1921 if( p_item->i_type == CONFIG_HINT_END ) continue;
1924 /* Print name of module */
1925 if( strcmp( "main", p_parser->psz_object_name ) )
1926 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1928 b_help_module = !strcmp( "help", p_parser->psz_object_name );
1930 /* Print module options */
1931 for( p_item = p_parser->p_config;
1932 p_item->i_type != CONFIG_HINT_END;
1935 char *psz_text, *psz_spaces = psz_spaces_text;
1936 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1937 char *psz_suf = "", *psz_prefix = NULL;
1940 /* Skip advanced options if requested */
1941 if( p_item->b_advanced && !b_advanced )
1946 switch( p_item->i_type )
1948 case CONFIG_HINT_CATEGORY:
1949 case CONFIG_HINT_USAGE:
1950 if( !strcmp( "main", p_parser->psz_object_name ) )
1951 fprintf( stdout, "\n %s\n", p_item->psz_text );
1954 case CONFIG_ITEM_STRING:
1955 case CONFIG_ITEM_FILE:
1956 case CONFIG_ITEM_DIRECTORY:
1957 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1958 case CONFIG_ITEM_MODULE_CAT:
1959 case CONFIG_ITEM_MODULE_LIST:
1960 case CONFIG_ITEM_MODULE_LIST_CAT:
1961 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1963 if( p_item->ppsz_list )
1966 psz_type = psz_buffer;
1968 for( i = 0; p_item->ppsz_list[i]; i++ )
1970 if( i ) strcat( psz_type, "," );
1971 strcat( psz_type, p_item->ppsz_list[i] );
1976 case CONFIG_ITEM_INTEGER:
1977 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1978 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1980 if( p_item->i_list )
1983 psz_type = psz_buffer;
1985 for( i = 0; p_item->ppsz_list_text[i]; i++ )
1987 if( i ) strcat( psz_type, ", " );
1988 sprintf( psz_type + strlen(psz_type), "%i (%s)",
1990 p_item->ppsz_list_text[i] );
1995 case CONFIG_ITEM_FLOAT:
1996 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1998 case CONFIG_ITEM_BOOL:
1999 psz_bra = ""; psz_type = ""; psz_ket = "";
2000 if( !b_help_module )
2002 psz_suf = p_item->i_value ? _(" (default enabled)") :
2003 _(" (default disabled)");
2013 /* Add short option if any */
2014 if( p_item->i_short )
2016 sprintf( psz_short, "-%c,", p_item->i_short );
2020 strcpy( psz_short, " " );
2023 i = PADDING_SPACES - strlen( p_item->psz_name )
2024 - strlen( psz_bra ) - strlen( psz_type )
2025 - strlen( psz_ket ) - 1;
2027 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2029 /* If option is of type --foo-bar, we print its counterpart
2030 * as --no-foo-bar, but if it is of type --foobar (without
2031 * dashes in the name) we print it as --nofoobar. Both
2032 * values are of course valid, only the display changes. */
2033 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
2035 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2040 psz_spaces[0] = '\n';
2045 psz_spaces[i] = '\0';
2048 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2050 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2051 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2052 psz_ket, psz_spaces );
2056 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2057 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2060 psz_spaces[i] = ' ';
2062 /* We wrap the rest of the output */
2063 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2064 b_description = config_GetInt( p_this, "help-verbose" );
2067 psz_text = psz_buffer;
2070 char *psz_parser, *psz_word;
2071 int i_end = strlen( psz_text );
2073 /* If the remaining text fits in a line, print it. */
2074 if( i_end <= i_width )
2076 fprintf( stdout, "%s\n", psz_text );
2080 /* Otherwise, eat as many words as possible */
2081 psz_parser = psz_text;
2084 psz_word = psz_parser;
2085 psz_parser = strchr( psz_word, ' ' );
2086 /* If no space was found, we reached the end of the text
2087 * block; otherwise, we skip the space we just found. */
2088 psz_parser = psz_parser ? psz_parser + 1
2091 } while( psz_parser - psz_text <= i_width );
2093 /* We cut a word in one of these cases:
2094 * - it's the only word in the line and it's too long.
2095 * - we used less than 80% of the width and the word we are
2096 * going to wrap is longer than 40% of the width, and even
2097 * if the word would have fit in the next line. */
2098 if( psz_word == psz_text
2099 || ( psz_word - psz_text < 80 * i_width / 100
2100 && psz_parser - psz_word > 40 * i_width / 100 ) )
2102 char c = psz_text[i_width];
2103 psz_text[i_width] = '\0';
2104 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2105 psz_text += i_width;
2110 psz_word[-1] = '\0';
2111 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2112 psz_text = psz_word;
2116 if( b_description && p_item->psz_longtext )
2118 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2119 b_description = VLC_FALSE;
2120 psz_spaces = psz_spaces_longtext;
2121 fprintf( stdout, "%s", psz_spaces );
2127 /* Release the module list */
2128 vlc_list_release( p_list );
2130 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2135 /*****************************************************************************
2136 * ListModules: list the available modules with their description
2137 *****************************************************************************
2138 * Print a list of all available modules (builtins and plugins) and a short
2139 * description for each one.
2140 *****************************************************************************/
2141 static void ListModules( vlc_t *p_this )
2145 char psz_spaces[22];
2148 memset( psz_spaces, ' ', 22 );
2155 fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
2156 p_this->p_vlc->psz_object_name );
2158 fprintf( stdout, _("[module] [description]\n") );
2160 /* List all modules */
2161 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2163 /* Enumerate each module */
2164 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2168 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2170 /* Nasty hack, but right now I'm too tired to think about a nice
2172 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2176 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2177 psz_spaces, p_parser->psz_longname );
2179 psz_spaces[i] = ' ';
2182 vlc_list_release( p_list );
2184 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2189 /*****************************************************************************
2190 * Version: print complete program version
2191 *****************************************************************************
2192 * Print complete program version and build number.
2193 *****************************************************************************/
2194 static void Version( void )
2200 fprintf( stdout, VERSION_MESSAGE "\n" );
2202 _("This program comes with NO WARRANTY, to the extent permitted by "
2203 "law.\nYou may redistribute it under the terms of the GNU General "
2204 "Public License;\nsee the file named COPYING for details.\n"
2205 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2207 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2212 /*****************************************************************************
2213 * ShowConsole: On Win32, create an output console for debug messages
2214 *****************************************************************************
2215 * This function is useful only on Win32.
2216 *****************************************************************************/
2218 static void ShowConsole( void )
2222 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2225 freopen( "CONOUT$", "w", stdout );
2226 freopen( "CONOUT$", "w", stderr );
2227 freopen( "CONIN$", "r", stdin );
2233 /*****************************************************************************
2234 * PauseConsole: On Win32, wait for a key press before closing the console
2235 *****************************************************************************
2236 * This function is useful only on Win32.
2237 *****************************************************************************/
2239 static void PauseConsole( void )
2243 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2244 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2251 /*****************************************************************************
2252 * ConsoleWidth: Return the console width in characters
2253 *****************************************************************************
2254 * We use the stty shell command to get the console width; if this fails or
2255 * if the width is less than 80, we default to 80.
2256 *****************************************************************************/
2257 static int ConsoleWidth( void )
2262 char buf[20], *psz_parser;
2266 file = popen( "stty size 2>/dev/null", "r" );
2269 i_ret = fread( buf, 1, 20, file );
2273 psz_parser = strchr( buf, ' ' );
2276 i_ret = atoi( psz_parser + 1 );
2291 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2292 vlc_value_t old_val, vlc_value_t new_val, void *param)
2294 vlc_t *p_vlc = (vlc_t *)p_this;
2296 if( new_val.i_int >= -1 )
2298 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2303 /*****************************************************************************
2304 * InitDeviceValues: initialize device values
2305 *****************************************************************************
2306 * This function inits the dvd, vcd and cd-audio values
2307 *****************************************************************************/
2308 static void InitDeviceValues( vlc_t *p_vlc )
2311 LibHalContext * ctx;
2317 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2319 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2321 for( i = 0; i < i_devices; i++ )
2323 if( !hal_device_property_exists( ctx, devices[ i ],
2324 "storage.cdrom.dvd" ) )
2329 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2330 "storage.cdrom.dvd" );
2331 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2336 config_PutPsz( p_vlc, "dvd", block_dev );
2339 config_PutPsz( p_vlc, "vcd", block_dev );
2340 config_PutPsz( p_vlc, "cd-audio", block_dev );
2342 hal_free_string( block_dev );
2346 hal_shutdown( ctx );