1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN (Centrale Réseaux) and its contributors
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 Help ( vlc_t *, char const *psz_help_name );
97 static void Usage ( vlc_t *, char const *psz_module_name );
98 static void ListModules ( vlc_t * );
99 static void Version ( void );
102 static void ShowConsole ( void );
103 static void PauseConsole ( void );
105 static int ConsoleWidth ( void );
107 static int VerboseCallback( vlc_object_t *, char const *,
108 vlc_value_t, vlc_value_t, void * );
110 static void InitDeviceValues( vlc_t * );
112 /*****************************************************************************
113 * vlc_current_object: return the current object.
114 *****************************************************************************
115 * If i_object is non-zero, return the corresponding object. Otherwise,
116 * return the statically allocated p_vlc object.
117 *****************************************************************************/
118 vlc_t * vlc_current_object( int i_object )
122 return vlc_object_get( p_libvlc, i_object );
128 /*****************************************************************************
129 * VLC_Version: return the libvlc version.
130 *****************************************************************************
131 * This function returns full version string (numeric version and codename).
132 *****************************************************************************/
133 char const * VLC_Version( void )
135 return VERSION_MESSAGE;
138 /*****************************************************************************
139 * VLC_Error: strerror() equivalent
140 *****************************************************************************
141 * This function returns full version string (numeric version and codename).
142 *****************************************************************************/
143 char const * VLC_Error( int i_err )
145 return vlc_error( i_err );
148 /*****************************************************************************
149 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
150 *****************************************************************************
151 * This function allocates a vlc_t structure and returns a negative value
152 * in case of failure. Also, the thread system is initialized.
153 *****************************************************************************/
154 int VLC_Create( void )
157 vlc_t * p_vlc = NULL;
160 /* &libvlc never changes, so we can safely call this multiple times. */
163 /* vlc_threads_init *must* be the first internal call! No other call is
164 * allowed before the thread system has been initialized. */
165 i_ret = vlc_threads_init( p_libvlc );
171 /* Now that the thread system is initialized, we don't have much, but
172 * at least we have var_Create */
173 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
174 var_Get( p_libvlc, "libvlc", &lockval );
175 vlc_mutex_lock( lockval.p_address );
176 if( !libvlc.b_ready )
180 /* Guess what CPU we have */
181 libvlc.i_cpu = CPUCapabilities();
183 /* Find verbosity from VLC_VERBOSE environment variable */
184 psz_env = getenv( "VLC_VERBOSE" );
185 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
187 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
188 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
190 libvlc.b_color = VLC_FALSE;
193 /* Initialize message queue */
194 msg_Create( p_libvlc );
196 /* Announce who we are */
197 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
198 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
200 /* The module bank will be initialized later */
201 libvlc.p_module_bank = NULL;
203 libvlc.b_ready = VLC_TRUE;
205 vlc_mutex_unlock( lockval.p_address );
206 var_Destroy( p_libvlc, "libvlc" );
208 /* Allocate a vlc object */
209 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
214 p_vlc->thread_id = 0;
216 p_vlc->psz_object_name = "root";
218 /* Initialize mutexes */
219 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
221 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
222 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
225 /* Store our newly allocated structure in the global list */
226 vlc_object_attach( p_vlc, p_libvlc );
228 /* Store data for the non-reentrant API */
229 p_static_vlc = p_vlc;
231 return p_vlc->i_object_id;
234 /*****************************************************************************
235 * VLC_Init: initialize a vlc_t structure.
236 *****************************************************************************
237 * This function initializes a previously allocated vlc_t structure:
239 * - gettext initialization
240 * - message queue, module bank and playlist initialization
241 * - configuration and commandline parsing
242 *****************************************************************************/
243 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
245 char p_capabilities[200];
251 vlc_bool_t b_exit = VLC_FALSE;
252 vlc_t * p_vlc = vlc_current_object( i_object );
253 module_t *p_help_module;
254 playlist_t *p_playlist;
263 * System specific initialization code
265 system_Init( p_vlc, &i_argc, ppsz_argv );
267 /* Get the executable name (similar to the basename command) */
270 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
273 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
279 p_vlc->psz_object_name = "vlc";
283 * Support for gettext
287 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
288 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
290 /* Initialize the module bank and load the configuration of the
291 * main module. We need to do this at this stage to be able to display
292 * a short help if required by the user. (short help == main module
294 module_InitBank( p_vlc );
296 /* Hack: insert the help module here */
297 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
298 if( p_help_module == NULL )
300 module_EndBank( p_vlc );
301 if( i_object ) vlc_object_release( p_vlc );
304 p_help_module->psz_object_name = "help";
305 p_help_module->psz_longname = N_("Help options");
306 config_Duplicate( p_help_module, p_help_config );
307 vlc_object_attach( p_help_module, libvlc.p_module_bank );
310 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
312 vlc_object_detach( p_help_module );
313 config_Free( p_help_module );
314 vlc_object_destroy( p_help_module );
315 module_EndBank( p_vlc );
316 if( i_object ) vlc_object_release( p_vlc );
320 /* Check for short help option */
321 if( config_GetInt( p_vlc, "help" ) )
323 Help( p_vlc, "help" );
326 /* Check for version option */
327 else if( config_GetInt( p_vlc, "version" ) )
333 /* Set the config file stuff */
334 p_vlc->psz_homedir = config_GetHomeDir();
335 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
337 /* Check for plugins cache options */
338 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
340 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
343 /* Hack: remove the help module here */
344 vlc_object_detach( p_help_module );
347 /* Will be re-done properly later on */
348 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
350 /* Check for daemon mode */
352 if( config_GetInt( p_vlc, "daemon" ) )
355 if( daemon( 0, 0) != 0 )
357 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
361 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
366 if( ( i_pid = fork() ) < 0 )
368 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
373 /* This is the parent, exit right now */
374 msg_Dbg( p_vlc, "closing parent process" );
379 /* We are the child */
380 msg_Dbg( p_vlc, "daemon spawned" );
381 close( STDIN_FILENO );
382 close( STDOUT_FILENO );
383 close( STDERR_FILENO );
385 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
393 config_Free( p_help_module );
394 vlc_object_destroy( p_help_module );
395 module_EndBank( p_vlc );
396 if( i_object ) vlc_object_release( p_vlc );
400 /* Check for translation config option */
401 #if defined( ENABLE_NLS ) \
402 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
404 /* This ain't really nice to have to reload the config here but it seems
405 * the only way to do it. */
406 config_LoadConfigFile( p_vlc, "main" );
407 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
409 /* Check if the user specified a custom language */
410 psz_language = config_GetPsz( p_vlc, "language" );
411 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
413 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
415 /* Reset the default domain */
416 SetLanguage( psz_language );
418 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
419 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
421 textdomain( PACKAGE_NAME );
423 #if defined( ENABLE_UTF8 )
424 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
427 module_EndBank( p_vlc );
428 module_InitBank( p_vlc );
429 config_LoadConfigFile( p_vlc, "main" );
430 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
431 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
433 if( psz_language ) free( psz_language );
437 * Load the builtins and plugins into the module_bank.
438 * We have to do it before config_Load*() because this also gets the
439 * list of configuration options exported by each module and loads their
442 module_LoadBuiltins( p_vlc );
443 module_LoadPlugins( p_vlc );
449 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
450 libvlc.p_module_bank->i_children );
452 /* Hack: insert the help module here */
453 vlc_object_attach( p_help_module, libvlc.p_module_bank );
456 /* Check for help on modules */
457 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
459 Help( p_vlc, p_tmp );
463 /* Check for long help option */
464 else if( config_GetInt( p_vlc, "longhelp" ) )
466 Help( p_vlc, "longhelp" );
469 /* Check for module list option */
470 else if( config_GetInt( p_vlc, "list" ) )
472 ListModules( p_vlc );
476 /* Check for config file options */
477 if( config_GetInt( p_vlc, "reset-config" ) )
479 vlc_object_detach( p_help_module );
480 config_ResetAll( p_vlc );
481 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
482 config_SaveConfigFile( p_vlc, NULL );
483 vlc_object_attach( p_help_module, libvlc.p_module_bank );
485 if( config_GetInt( p_vlc, "save-config" ) )
487 vlc_object_detach( p_help_module );
488 config_LoadConfigFile( p_vlc, NULL );
489 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
490 config_SaveConfigFile( p_vlc, NULL );
491 vlc_object_attach( p_help_module, libvlc.p_module_bank );
494 /* Hack: remove the help module here */
495 vlc_object_detach( p_help_module );
500 config_Free( p_help_module );
501 vlc_object_destroy( p_help_module );
502 module_EndBank( p_vlc );
503 if( i_object ) vlc_object_release( p_vlc );
510 InitDeviceValues( p_vlc );
513 * Override default configuration with config file settings
515 config_LoadConfigFile( p_vlc, NULL );
517 /* Hack: insert the help module here */
518 vlc_object_attach( p_help_module, libvlc.p_module_bank );
522 * Override configuration with command line settings
524 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
528 /* Pause the console because it's destroyed when we exit */
529 fprintf( stderr, "The command line options couldn't be loaded, check "
530 "that they are valid.\n" );
533 vlc_object_detach( p_help_module );
534 config_Free( p_help_module );
535 vlc_object_destroy( p_help_module );
536 module_EndBank( p_vlc );
537 if( i_object ) vlc_object_release( p_vlc );
541 /* Hack: remove the help module here */
542 vlc_object_detach( p_help_module );
543 config_Free( p_help_module );
544 vlc_object_destroy( p_help_module );
548 * System specific configuration
550 system_Configure( p_vlc, &i_argc, ppsz_argv );
553 * Message queue options
556 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
557 if( config_GetInt( p_vlc, "quiet" ) )
560 var_Set( p_vlc, "verbose", val );
562 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
563 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
565 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
568 * Output messages that may still be in the queue
572 /* p_vlc initialization. FIXME ? */
574 if( !config_GetInt( p_vlc, "fpu" ) )
575 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
577 #if defined( __i386__ ) || defined( __x86_64__ )
578 if( !config_GetInt( p_vlc, "mmx" ) )
579 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
580 if( !config_GetInt( p_vlc, "3dn" ) )
581 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
582 if( !config_GetInt( p_vlc, "mmxext" ) )
583 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
584 if( !config_GetInt( p_vlc, "sse" ) )
585 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
586 if( !config_GetInt( p_vlc, "sse2" ) )
587 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
589 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
590 if( !config_GetInt( p_vlc, "altivec" ) )
591 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
594 #define PRINT_CAPABILITY( capability, string ) \
595 if( libvlc.i_cpu & capability ) \
597 strncat( p_capabilities, string " ", \
598 sizeof(p_capabilities) - strlen(p_capabilities) ); \
599 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
602 p_capabilities[0] = '\0';
603 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
604 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
605 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
606 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
607 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
608 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
609 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
610 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
611 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
612 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
613 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
616 * Choose the best memcpy module
618 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
620 if( p_vlc->pf_memcpy == NULL )
622 p_vlc->pf_memcpy = memcpy;
625 if( p_vlc->pf_memset == NULL )
627 p_vlc->pf_memset = memset;
631 * Initialize hotkey handling
633 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
634 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
635 /* Do a copy (we don't need to modify the strings) */
636 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
639 * Initialize playlist and get commandline files
641 p_playlist = playlist_Create( p_vlc );
644 msg_Err( p_vlc, "playlist initialization failed" );
645 if( p_vlc->p_memcpy_module != NULL )
647 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
649 module_EndBank( p_vlc );
650 if( i_object ) vlc_object_release( p_vlc );
654 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
655 if( psz_modules && *psz_modules )
657 /* Add service discovery modules */
658 playlist_AddSDModules( p_playlist, psz_modules );
660 if( psz_modules ) free( psz_modules );
663 * Load background interfaces
665 psz_modules = config_GetPsz( p_vlc, "extraintf" );
666 psz_control = config_GetPsz( p_vlc, "control" );
668 if( psz_modules && *psz_modules && psz_control && *psz_control )
670 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
671 strlen( psz_control ) + 1 );
672 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
674 else if( psz_control && *psz_control )
676 if( psz_modules ) free( psz_modules );
677 psz_modules = strdup( psz_control );
680 psz_parser = psz_modules;
681 while ( psz_parser && *psz_parser )
683 char *psz_module, *psz_temp;
684 psz_module = psz_parser;
685 psz_parser = strchr( psz_module, ':' );
691 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
694 sprintf( psz_temp, "%s,none", psz_module );
695 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
705 * Allways load the hotkeys interface if it exists
707 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
710 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
712 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
713 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
714 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
715 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
716 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
717 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
718 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
719 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
720 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
721 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
722 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
723 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
726 * Get input filenames given as commandline arguments
728 GetFilenames( p_vlc, i_argc, ppsz_argv );
731 * Get --open argument
733 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
734 var_Get( p_vlc, "open", &val );
735 if ( val.psz_string != NULL && *val.psz_string )
737 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
738 PLAYLIST_INSERT, 0 );
740 if ( val.psz_string != NULL ) free( val.psz_string );
742 if( i_object ) vlc_object_release( p_vlc );
746 /*****************************************************************************
747 * VLC_AddIntf: add an interface
748 *****************************************************************************
749 * This function opens an interface plugin and runs it. If b_block is set
750 * to 0, VLC_AddIntf will return immediately and let the interface run in a
751 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
752 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
753 * the playlist when it is completely initialised.
754 *****************************************************************************/
755 int VLC_AddIntf( int i_object, char const *psz_module,
756 vlc_bool_t b_block, vlc_bool_t b_play )
759 intf_thread_t *p_intf;
760 vlc_t *p_vlc = vlc_current_object( i_object );
768 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
771 * We prefer the dummy interface if none is specified. */
772 char *psz_interface = config_GetPsz( p_vlc, "intf" );
773 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
774 if( psz_interface ) free( psz_interface );
778 /* Try to create the interface */
779 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
783 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
784 if( i_object ) vlc_object_release( p_vlc );
788 /* Interface doesn't handle play on start so do it ourselves */
789 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
791 /* Try to run the interface */
792 p_intf->b_play = b_play;
793 p_intf->b_block = b_block;
794 i_err = intf_RunThread( p_intf );
797 vlc_object_detach( p_intf );
798 intf_Destroy( p_intf );
799 if( i_object ) vlc_object_release( p_vlc );
803 if( i_object ) vlc_object_release( p_vlc );
807 /*****************************************************************************
808 * VLC_Die: ask vlc to die.
809 *****************************************************************************
810 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
811 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
812 *****************************************************************************/
813 int VLC_Die( int i_object )
815 vlc_t *p_vlc = vlc_current_object( i_object );
822 p_vlc->b_die = VLC_TRUE;
824 if( i_object ) vlc_object_release( p_vlc );
828 /*****************************************************************************
829 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
830 *****************************************************************************/
831 int VLC_CleanUp( int i_object )
833 intf_thread_t * p_intf;
834 playlist_t * p_playlist;
835 vout_thread_t * p_vout;
836 aout_instance_t * p_aout;
837 announce_handler_t * p_announce;
838 vlc_t *p_vlc = vlc_current_object( i_object );
840 /* Check that the handle is valid */
847 * Ask the interfaces to stop and destroy them
849 msg_Dbg( p_vlc, "removing all interfaces" );
850 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
852 intf_StopThread( p_intf );
853 vlc_object_detach( p_intf );
854 vlc_object_release( p_intf );
855 intf_Destroy( p_intf );
861 msg_Dbg( p_vlc, "removing all playlists" );
862 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
865 vlc_object_detach( p_playlist );
866 vlc_object_release( p_playlist );
867 playlist_Destroy( p_playlist );
873 msg_Dbg( p_vlc, "removing all video outputs" );
874 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
876 vlc_object_detach( p_vout );
877 vlc_object_release( p_vout );
878 vout_Destroy( p_vout );
884 msg_Dbg( p_vlc, "removing all audio outputs" );
885 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
887 vlc_object_detach( (vlc_object_t *)p_aout );
888 vlc_object_release( (vlc_object_t *)p_aout );
889 aout_Delete( p_aout );
893 * Free announce handler(s?)
895 msg_Dbg( p_vlc, "removing announce handler" );
896 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
899 vlc_object_detach( p_announce );
900 vlc_object_release( p_announce );
901 announce_HandlerDestroy( p_announce );
904 if( i_object ) vlc_object_release( p_vlc );
908 /*****************************************************************************
909 * VLC_Destroy: Destroy everything.
910 *****************************************************************************
911 * This function requests the running threads to finish, waits for their
912 * termination, and destroys their structure.
913 *****************************************************************************/
914 int VLC_Destroy( int i_object )
916 vlc_t *p_vlc = vlc_current_object( i_object );
924 * Free allocated memory
926 if( p_vlc->p_memcpy_module )
928 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
929 p_vlc->p_memcpy_module = NULL;
935 module_EndBank( p_vlc );
937 if( p_vlc->psz_homedir )
939 free( p_vlc->psz_homedir );
940 p_vlc->psz_homedir = NULL;
943 if( p_vlc->psz_configfile )
945 free( p_vlc->psz_configfile );
946 p_vlc->psz_configfile = NULL;
949 if( p_vlc->p_hotkeys )
951 free( p_vlc->p_hotkeys );
952 p_vlc->p_hotkeys = NULL;
956 * System specific cleaning code
960 /* Destroy mutexes */
961 vlc_mutex_destroy( &p_vlc->config_lock );
963 vlc_object_detach( p_vlc );
965 /* Release object before destroying it */
966 if( i_object ) vlc_object_release( p_vlc );
968 vlc_object_destroy( p_vlc );
970 /* Stop thread system: last one out please shut the door! */
971 vlc_threads_end( p_libvlc );
976 /*****************************************************************************
977 * VLC_VariableSet: set a vlc variable
978 *****************************************************************************/
979 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
981 vlc_t *p_vlc = vlc_current_object( i_object );
989 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
990 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
991 if( !strncmp( psz_var, "conf::", 6 ) )
993 module_config_t *p_item;
994 char const *psz_newvar = psz_var + 6;
996 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1000 switch( p_item->i_type )
1002 case CONFIG_ITEM_BOOL:
1003 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1005 case CONFIG_ITEM_INTEGER:
1006 config_PutInt( p_vlc, psz_newvar, value.i_int );
1008 case CONFIG_ITEM_FLOAT:
1009 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1012 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1015 if( i_object ) vlc_object_release( p_vlc );
1020 i_ret = var_Set( p_vlc, psz_var, value );
1022 if( i_object ) vlc_object_release( p_vlc );
1026 /*****************************************************************************
1027 * VLC_VariableGet: get a vlc variable
1028 *****************************************************************************/
1029 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1031 vlc_t *p_vlc = vlc_current_object( i_object );
1039 i_ret = var_Get( p_vlc , psz_var, p_value );
1041 if( i_object ) vlc_object_release( p_vlc );
1045 /*****************************************************************************
1046 * VLC_VariableType: get a vlc variable type
1047 *****************************************************************************/
1048 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1051 vlc_t *p_vlc = vlc_current_object( i_object );
1058 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1059 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1060 if( !strncmp( psz_var, "conf::", 6 ) )
1062 module_config_t *p_item;
1063 char const *psz_newvar = psz_var + 6;
1065 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1069 switch( p_item->i_type )
1071 case CONFIG_ITEM_BOOL:
1072 i_type = VLC_VAR_BOOL;
1074 case CONFIG_ITEM_INTEGER:
1075 i_type = VLC_VAR_INTEGER;
1077 case CONFIG_ITEM_FLOAT:
1078 i_type = VLC_VAR_FLOAT;
1081 i_type = VLC_VAR_STRING;
1089 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1091 if( i_object ) vlc_object_release( p_vlc );
1101 /*****************************************************************************
1102 * VLC_AddTarget: adds a target for playing.
1103 *****************************************************************************
1104 * This function adds psz_target to the current playlist. If a playlist does
1105 * not exist, it will create one.
1106 *****************************************************************************/
1107 int VLC_AddTarget( int i_object, char const *psz_target,
1108 char const **ppsz_options, int i_options,
1109 int i_mode, int i_pos )
1112 playlist_t *p_playlist;
1113 vlc_t *p_vlc = vlc_current_object( i_object );
1120 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1122 if( p_playlist == NULL )
1124 msg_Dbg( p_vlc, "no playlist present, creating one" );
1125 p_playlist = playlist_Create( p_vlc );
1127 if( p_playlist == NULL )
1129 if( i_object ) vlc_object_release( p_vlc );
1130 return VLC_EGENERIC;
1133 vlc_object_yield( p_playlist );
1136 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1137 i_mode, i_pos, -1, ppsz_options, i_options);
1139 vlc_object_release( p_playlist );
1141 if( i_object ) vlc_object_release( p_vlc );
1145 /*****************************************************************************
1147 *****************************************************************************/
1148 int VLC_Play( int i_object )
1150 playlist_t * p_playlist;
1151 vlc_t *p_vlc = vlc_current_object( i_object );
1153 /* Check that the handle is valid */
1159 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1163 if( i_object ) vlc_object_release( p_vlc );
1167 playlist_Play( p_playlist );
1168 vlc_object_release( p_playlist );
1170 if( i_object ) vlc_object_release( p_vlc );
1174 /*****************************************************************************
1175 * VLC_Pause: toggle pause
1176 *****************************************************************************/
1177 int VLC_Pause( int i_object )
1179 playlist_t * p_playlist;
1180 vlc_t *p_vlc = vlc_current_object( i_object );
1182 /* Check that the handle is valid */
1188 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1192 if( i_object ) vlc_object_release( p_vlc );
1196 playlist_Pause( p_playlist );
1197 vlc_object_release( p_playlist );
1199 if( i_object ) vlc_object_release( p_vlc );
1203 /*****************************************************************************
1204 * VLC_Pause: toggle pause
1205 *****************************************************************************/
1206 int VLC_Stop( int i_object )
1208 playlist_t * p_playlist;
1209 vlc_t *p_vlc = vlc_current_object( i_object );
1211 /* Check that the handle is valid */
1217 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1221 if( i_object ) vlc_object_release( p_vlc );
1225 playlist_Stop( p_playlist );
1226 vlc_object_release( p_playlist );
1228 if( i_object ) vlc_object_release( p_vlc );
1232 /*****************************************************************************
1233 * VLC_IsPlaying: Query for Playlist Status
1234 *****************************************************************************/
1235 vlc_bool_t VLC_IsPlaying( int i_object )
1237 playlist_t * p_playlist;
1238 vlc_bool_t b_playing;
1240 vlc_t *p_vlc = vlc_current_object( i_object );
1242 /* Check that the handle is valid */
1248 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1252 if( i_object ) vlc_object_release( p_vlc );
1256 b_playing = playlist_IsPlaying( p_playlist );
1257 vlc_object_release( p_playlist );
1259 if( i_object ) vlc_object_release( p_vlc );
1264 * Get the current position in a input
1266 * Return the current position as a float
1267 * \note For some inputs, this will be unknown.
1269 * \param i_object a vlc object id
1270 * \return a float in the range of 0.0 - 1.0
1272 float VLC_PositionGet( int i_object )
1274 input_thread_t *p_input;
1276 vlc_t *p_vlc = vlc_current_object( i_object );
1278 /* Check that the handle is valid */
1284 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1288 if( i_object ) vlc_object_release( p_vlc );
1292 var_Get( p_input, "position", &val );
1293 vlc_object_release( p_input );
1295 if( i_object ) vlc_object_release( p_vlc );
1300 * Set the current position in a input
1302 * Set the current position in a input and then return
1303 * the current position as a float.
1304 * \note For some inputs, this will be unknown.
1306 * \param i_object a vlc object id
1307 * \param i_position a float in the range of 0.0 - 1.0
1308 * \return a float in the range of 0.0 - 1.0
1310 float VLC_PositionSet( int i_object, float i_position )
1312 input_thread_t *p_input;
1314 vlc_t *p_vlc = vlc_current_object( i_object );
1316 /* Check that the handle is valid */
1322 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1326 if( i_object ) vlc_object_release( p_vlc );
1330 val.f_float = i_position;
1331 var_Set( p_input, "position", val );
1332 var_Get( p_input, "position", &val );
1333 vlc_object_release( p_input );
1335 if( i_object ) vlc_object_release( p_vlc );
1340 * Get the current position in a input
1342 * Return the current position in seconds from the start.
1343 * \note For some inputs, this will be unknown.
1345 * \param i_object a vlc object id
1346 * \return the offset from 0:00 in seconds
1348 int VLC_TimeGet( int i_object )
1350 input_thread_t *p_input;
1352 vlc_t *p_vlc = vlc_current_object( i_object );
1354 /* Check that the handle is valid */
1360 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1364 if( i_object ) vlc_object_release( p_vlc );
1368 var_Get( p_input, "time", &val );
1369 vlc_object_release( p_input );
1371 if( i_object ) vlc_object_release( p_vlc );
1372 return val.i_time / 1000000;
1376 * Seek to a position in the current input
1378 * Seek i_seconds in the current input. If b_relative is set,
1379 * then the seek will be relative to the current position, otherwise
1380 * it will seek to i_seconds from the beginning of the input.
1381 * \note For some inputs, this will be unknown.
1383 * \param i_object a vlc object id
1384 * \param i_seconds seconds from current position or from beginning of input
1385 * \param b_relative seek relative from current position
1386 * \return VLC_SUCCESS on success
1388 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1390 input_thread_t *p_input;
1392 vlc_t *p_vlc = vlc_current_object( i_object );
1394 /* Check that the handle is valid */
1400 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1404 if( i_object ) vlc_object_release( p_vlc );
1410 val.i_time = i_seconds;
1411 val.i_time = val.i_time * 1000000L;
1412 var_Set( p_input, "time-offset", val );
1416 val.i_time = i_seconds;
1417 val.i_time = val.i_time * 1000000L;
1418 var_Set( p_input, "time", val );
1420 vlc_object_release( p_input );
1422 if( i_object ) vlc_object_release( p_vlc );
1427 * Get the total length of a input
1429 * Return the total length in seconds from the current input.
1430 * \note For some inputs, this will be unknown.
1432 * \param i_object a vlc object id
1433 * \return the length in seconds
1435 int VLC_LengthGet( int i_object )
1437 input_thread_t *p_input;
1439 vlc_t *p_vlc = vlc_current_object( i_object );
1441 /* Check that the handle is valid */
1447 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1451 if( i_object ) vlc_object_release( p_vlc );
1455 var_Get( p_input, "length", &val );
1456 vlc_object_release( p_input );
1458 if( i_object ) vlc_object_release( p_vlc );
1459 return val.i_time / 1000000L;
1463 * Play the input faster than realtime
1465 * 2x, 4x, 8x faster than realtime
1466 * \note For some inputs, this will be impossible.
1468 * \param i_object a vlc object id
1469 * \return the current speedrate
1471 float VLC_SpeedFaster( int i_object )
1473 input_thread_t *p_input;
1475 vlc_t *p_vlc = vlc_current_object( i_object );
1477 /* Check that the handle is valid */
1483 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1487 if( i_object ) vlc_object_release( p_vlc );
1491 val.b_bool = VLC_TRUE;
1492 var_Set( p_input, "rate-faster", val );
1493 var_Get( p_input, "rate", &val );
1494 vlc_object_release( p_input );
1496 if( i_object ) vlc_object_release( p_vlc );
1497 return val.f_float / INPUT_RATE_DEFAULT;
1501 * Play the input slower than realtime
1503 * 1/2x, 1/4x, 1/8x slower than realtime
1504 * \note For some inputs, this will be impossible.
1506 * \param i_object a vlc object id
1507 * \return the current speedrate
1509 float VLC_SpeedSlower( int i_object )
1511 input_thread_t *p_input;
1513 vlc_t *p_vlc = vlc_current_object( i_object );
1515 /* Check that the handle is valid */
1521 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1525 if( i_object ) vlc_object_release( p_vlc );
1529 val.b_bool = VLC_TRUE;
1530 var_Set( p_input, "rate-slower", val );
1531 var_Get( p_input, "rate", &val );
1532 vlc_object_release( p_input );
1534 if( i_object ) vlc_object_release( p_vlc );
1535 return val.f_float / INPUT_RATE_DEFAULT;
1539 * Return the current playlist item
1541 * Returns the index of the playlistitem that is currently selected for play.
1542 * This is valid even if nothing is currently playing.
1544 * \param i_object a vlc object id
1545 * \return the current index
1547 int VLC_PlaylistIndex( int i_object )
1550 playlist_t * p_playlist;
1551 vlc_t *p_vlc = vlc_current_object( i_object );
1553 /* Check that the handle is valid */
1559 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1563 if( i_object ) vlc_object_release( p_vlc );
1567 i_index = p_playlist->i_index;
1568 vlc_object_release( p_playlist );
1570 if( i_object ) vlc_object_release( p_vlc );
1575 * Total amount of items in the playlist
1577 * \param i_object a vlc object id
1578 * \return amount of playlist items
1580 int VLC_PlaylistNumberOfItems( int i_object )
1583 playlist_t * p_playlist;
1584 vlc_t *p_vlc = vlc_current_object( i_object );
1586 /* Check that the handle is valid */
1592 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1596 if( i_object ) vlc_object_release( p_vlc );
1600 i_size = p_playlist->i_size;
1601 vlc_object_release( p_playlist );
1603 if( i_object ) vlc_object_release( p_vlc );
1608 * Next playlist item
1610 * Skip to the next playlistitem and play it.
1612 * \param i_object a vlc object id
1613 * \return VLC_SUCCESS on success
1615 int VLC_PlaylistNext( int i_object )
1617 playlist_t * p_playlist;
1618 vlc_t *p_vlc = vlc_current_object( i_object );
1620 /* Check that the handle is valid */
1626 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1630 if( i_object ) vlc_object_release( p_vlc );
1634 playlist_Next( p_playlist );
1635 vlc_object_release( p_playlist );
1637 if( i_object ) vlc_object_release( p_vlc );
1642 * Previous playlist item
1644 * Skip to the previous playlistitem and play it.
1646 * \param i_object a vlc object id
1647 * \return VLC_SUCCESS on success
1649 int VLC_PlaylistPrev( int i_object )
1651 playlist_t * p_playlist;
1652 vlc_t *p_vlc = vlc_current_object( i_object );
1654 /* Check that the handle is valid */
1660 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1664 if( i_object ) vlc_object_release( p_vlc );
1668 playlist_Prev( p_playlist );
1669 vlc_object_release( p_playlist );
1671 if( i_object ) vlc_object_release( p_vlc );
1676 /*****************************************************************************
1677 * VLC_PlaylistClear: Empty the playlist
1678 *****************************************************************************/
1679 int VLC_PlaylistClear( int i_object )
1682 playlist_t * p_playlist;
1683 vlc_t *p_vlc = vlc_current_object( i_object );
1685 /* Check that the handle is valid */
1691 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1695 if( i_object ) vlc_object_release( p_vlc );
1699 i_err = playlist_Clear( p_playlist );
1701 vlc_object_release( p_playlist );
1703 if( i_object ) vlc_object_release( p_vlc );
1710 * \param i_object a vlc object id
1711 * \param i_volume something in a range from 0-200
1712 * \return the new volume (range 0-200 %)
1714 int VLC_VolumeSet( int i_object, int i_volume )
1716 audio_volume_t i_vol = 0;
1717 vlc_t *p_vlc = vlc_current_object( i_object );
1719 /* Check that the handle is valid */
1725 if( i_volume >= 0 && i_volume <= 200 )
1727 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1728 aout_VolumeSet( p_vlc, i_vol );
1731 if( i_object ) vlc_object_release( p_vlc );
1732 return i_vol * 200 / AOUT_VOLUME_MAX;
1736 * Get the current volume
1738 * Retrieve the current volume.
1740 * \param i_object a vlc object id
1741 * \return the current volume (range 0-200 %)
1743 int VLC_VolumeGet( int i_object )
1745 audio_volume_t i_volume;
1746 vlc_t *p_vlc = vlc_current_object( i_object );
1748 /* Check that the handle is valid */
1754 aout_VolumeGet( p_vlc, &i_volume );
1756 if( i_object ) vlc_object_release( p_vlc );
1757 return i_volume*200/AOUT_VOLUME_MAX;
1761 * Mute/Unmute the volume
1763 * \param i_object a vlc object id
1764 * \return VLC_SUCCESS on success
1766 int VLC_VolumeMute( int i_object )
1768 vlc_t *p_vlc = vlc_current_object( i_object );
1770 /* Check that the handle is valid */
1776 aout_VolumeMute( p_vlc, NULL );
1778 if( i_object ) vlc_object_release( p_vlc );
1782 /*****************************************************************************
1783 * VLC_FullScreen: toggle fullscreen mode
1784 *****************************************************************************/
1785 int VLC_FullScreen( int i_object )
1787 vout_thread_t *p_vout;
1788 vlc_t *p_vlc = vlc_current_object( i_object );
1795 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1799 if( i_object ) vlc_object_release( p_vlc );
1803 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1804 vlc_object_release( p_vout );
1806 if( i_object ) vlc_object_release( p_vlc );
1810 /* following functions are local */
1812 /*****************************************************************************
1813 * SetLanguage: set the interface language.
1814 *****************************************************************************
1815 * We set the LC_MESSAGES locale category for interface messages and buttons,
1816 * as well as the LC_CTYPE category for string sorting and possible wide
1817 * character support.
1818 *****************************************************************************/
1819 static void SetLanguage ( char const *psz_lang )
1821 #if defined( ENABLE_NLS ) \
1822 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1825 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1829 if( psz_lang && !*psz_lang )
1831 # if defined( HAVE_LC_MESSAGES )
1832 setlocale( LC_MESSAGES, psz_lang );
1834 setlocale( LC_CTYPE, psz_lang );
1839 /* I need that under Darwin, please check it doesn't disturb
1840 * other platforms. --Meuuh */
1841 setenv( "LANG", psz_lang, 1 );
1843 #elif defined( SYS_BEOS ) || defined( WIN32 )
1844 /* We set LC_ALL manually because it is the only way to set
1845 * the language at runtime under eg. Windows. Beware that this
1846 * makes the environment unconsistent when libvlc is unloaded and
1847 * should probably be moved to a safer place like vlc.c. */
1848 static char psz_lcall[20];
1849 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1850 psz_lcall[19] = '\0';
1851 putenv( psz_lcall );
1854 setlocale( LC_ALL, psz_lang );
1857 /* Specify where to find the locales for current domain */
1858 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1859 psz_path = LOCALEDIR;
1861 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1865 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1867 fprintf( stderr, "warning: no domain %s in directory %s\n",
1868 PACKAGE_NAME, psz_path );
1871 /* Set the default domain */
1872 textdomain( PACKAGE_NAME );
1874 #if defined( ENABLE_UTF8 )
1875 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1881 /*****************************************************************************
1882 * GetFilenames: parse command line options which are not flags
1883 *****************************************************************************
1884 * Parse command line for input files as well as their associated options.
1885 * An option always follows its associated input and begins with a ":".
1886 *****************************************************************************/
1887 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1889 int i_opt, i_options;
1891 /* We assume that the remaining parameters are filenames
1892 * and their input options */
1893 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1897 /* Count the input options */
1898 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1904 /* TODO: write an internal function of this one, to avoid
1905 * unnecessary lookups. */
1906 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1907 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1909 PLAYLIST_INSERT, 0 );
1915 /*****************************************************************************
1916 * Help: print program help
1917 *****************************************************************************
1918 * Print a short inline help. Message interface is initialized at this stage.
1919 *****************************************************************************/
1920 static void Help( vlc_t *p_this, char const *psz_help_name )
1926 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1928 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1929 Usage( p_this, "help" );
1930 Usage( p_this, "main" );
1932 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1934 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1935 Usage( p_this, NULL );
1937 else if( psz_help_name )
1939 Usage( p_this, psz_help_name );
1942 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1947 /*****************************************************************************
1948 * Usage: print module usage
1949 *****************************************************************************
1950 * Print a short inline help. Message interface is initialized at this stage.
1951 *****************************************************************************/
1952 static void Usage( vlc_t *p_this, char const *psz_module_name )
1954 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1955 /* short option ------' | | | | | | |
1956 * option name ------------' | | | | | |
1957 * <bra -------------------------' | | | | |
1958 * option type or "" --------------' | | | |
1959 * ket> -----------------------------' | | |
1960 * padding spaces ---------------------' | |
1961 * comment -------------------------------' |
1962 * comment suffix --------------------------'
1964 * The purpose of having bra and ket is that we might i18n them as well.
1966 #define LINE_START 8
1967 #define PADDING_SPACES 25
1970 module_config_t *p_item;
1971 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1972 char psz_spaces_longtext[LINE_START+3];
1973 char psz_format[sizeof(FORMAT_STRING)];
1974 char psz_buffer[10000];
1977 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1978 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1979 vlc_bool_t b_description;
1981 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1982 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1983 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1984 psz_spaces_longtext[LINE_START+2] = '\0';
1986 strcpy( psz_format, FORMAT_STRING );
1988 /* List all modules */
1989 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1991 /* Enumerate the config for each module */
1992 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1994 vlc_bool_t b_help_module;
1996 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1998 if( psz_module_name && strcmp( psz_module_name,
1999 p_parser->psz_object_name ) )
2004 /* Ignore modules without config options */
2005 if( !p_parser->i_config_items )
2010 /* Ignore modules with only advanced config options if requested */
2013 for( p_item = p_parser->p_config;
2014 p_item->i_type != CONFIG_HINT_END;
2017 if( (p_item->i_type & CONFIG_ITEM) &&
2018 !p_item->b_advanced ) break;
2020 if( p_item->i_type == CONFIG_HINT_END ) continue;
2023 /* Print name of module */
2024 if( strcmp( "main", p_parser->psz_object_name ) )
2025 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2027 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2029 /* Print module options */
2030 for( p_item = p_parser->p_config;
2031 p_item->i_type != CONFIG_HINT_END;
2034 char *psz_text, *psz_spaces = psz_spaces_text;
2035 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2036 char *psz_suf = "", *psz_prefix = NULL;
2039 /* Skip deprecated options */
2040 if( p_item->psz_current )
2044 /* Skip advanced options if requested */
2045 if( p_item->b_advanced && !b_advanced )
2050 switch( p_item->i_type )
2052 case CONFIG_HINT_CATEGORY:
2053 case CONFIG_HINT_USAGE:
2054 if( !strcmp( "main", p_parser->psz_object_name ) )
2055 fprintf( stdout, "\n %s\n", p_item->psz_text );
2058 case CONFIG_ITEM_STRING:
2059 case CONFIG_ITEM_FILE:
2060 case CONFIG_ITEM_DIRECTORY:
2061 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2062 case CONFIG_ITEM_MODULE_CAT:
2063 case CONFIG_ITEM_MODULE_LIST:
2064 case CONFIG_ITEM_MODULE_LIST_CAT:
2065 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2067 if( p_item->ppsz_list )
2070 psz_type = psz_buffer;
2072 for( i = 0; p_item->ppsz_list[i]; i++ )
2074 if( i ) strcat( psz_type, "," );
2075 strcat( psz_type, p_item->ppsz_list[i] );
2080 case CONFIG_ITEM_INTEGER:
2081 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2082 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2084 if( p_item->i_list )
2087 psz_type = psz_buffer;
2089 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2091 if( i ) strcat( psz_type, ", " );
2092 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2094 p_item->ppsz_list_text[i] );
2099 case CONFIG_ITEM_FLOAT:
2100 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2102 case CONFIG_ITEM_BOOL:
2103 psz_bra = ""; psz_type = ""; psz_ket = "";
2104 if( !b_help_module )
2106 psz_suf = p_item->i_value ? _(" (default enabled)") :
2107 _(" (default disabled)");
2117 /* Add short option if any */
2118 if( p_item->i_short )
2120 sprintf( psz_short, "-%c,", p_item->i_short );
2124 strcpy( psz_short, " " );
2127 i = PADDING_SPACES - strlen( p_item->psz_name )
2128 - strlen( psz_bra ) - strlen( psz_type )
2129 - strlen( psz_ket ) - 1;
2131 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2133 psz_prefix = ", --no-";
2134 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2139 psz_spaces[0] = '\n';
2144 psz_spaces[i] = '\0';
2147 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2149 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2150 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2151 psz_ket, psz_spaces );
2155 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2156 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2159 psz_spaces[i] = ' ';
2161 /* We wrap the rest of the output */
2162 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2163 b_description = config_GetInt( p_this, "help-verbose" );
2166 psz_text = psz_buffer;
2169 char *psz_parser, *psz_word;
2170 int i_end = strlen( psz_text );
2172 /* If the remaining text fits in a line, print it. */
2173 if( i_end <= i_width )
2175 fprintf( stdout, "%s\n", psz_text );
2179 /* Otherwise, eat as many words as possible */
2180 psz_parser = psz_text;
2183 psz_word = psz_parser;
2184 psz_parser = strchr( psz_word, ' ' );
2185 /* If no space was found, we reached the end of the text
2186 * block; otherwise, we skip the space we just found. */
2187 psz_parser = psz_parser ? psz_parser + 1
2190 } while( psz_parser - psz_text <= i_width );
2192 /* We cut a word in one of these cases:
2193 * - it's the only word in the line and it's too long.
2194 * - we used less than 80% of the width and the word we are
2195 * going to wrap is longer than 40% of the width, and even
2196 * if the word would have fit in the next line. */
2197 if( psz_word == psz_text
2198 || ( psz_word - psz_text < 80 * i_width / 100
2199 && psz_parser - psz_word > 40 * i_width / 100 ) )
2201 char c = psz_text[i_width];
2202 psz_text[i_width] = '\0';
2203 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2204 psz_text += i_width;
2209 psz_word[-1] = '\0';
2210 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2211 psz_text = psz_word;
2215 if( b_description && p_item->psz_longtext )
2217 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2218 b_description = VLC_FALSE;
2219 psz_spaces = psz_spaces_longtext;
2220 fprintf( stdout, "%s", psz_spaces );
2226 /* Release the module list */
2227 vlc_list_release( p_list );
2230 /*****************************************************************************
2231 * ListModules: list the available modules with their description
2232 *****************************************************************************
2233 * Print a list of all available modules (builtins and plugins) and a short
2234 * description for each one.
2235 *****************************************************************************/
2236 static void ListModules( vlc_t *p_this )
2240 char psz_spaces[22];
2243 memset( psz_spaces, ' ', 22 );
2249 /* List all modules */
2250 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2252 /* Enumerate each module */
2253 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2257 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2259 /* Nasty hack, but right now I'm too tired to think about a nice
2261 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2265 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2266 psz_spaces, p_parser->psz_longname );
2268 psz_spaces[i] = ' ';
2271 vlc_list_release( p_list );
2273 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2278 /*****************************************************************************
2279 * Version: print complete program version
2280 *****************************************************************************
2281 * Print complete program version and build number.
2282 *****************************************************************************/
2283 static void Version( void )
2289 fprintf( stdout, VERSION_MESSAGE "\n" );
2291 _("This program comes with NO WARRANTY, to the extent permitted by "
2292 "law.\nYou may redistribute it under the terms of the GNU General "
2293 "Public License;\nsee the file named COPYING for details.\n"
2294 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2296 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2301 /*****************************************************************************
2302 * ShowConsole: On Win32, create an output console for debug messages
2303 *****************************************************************************
2304 * This function is useful only on Win32.
2305 *****************************************************************************/
2307 static void ShowConsole( void )
2311 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2314 freopen( "CONOUT$", "w", stdout );
2315 freopen( "CONOUT$", "w", stderr );
2316 freopen( "CONIN$", "r", stdin );
2322 /*****************************************************************************
2323 * PauseConsole: On Win32, wait for a key press before closing the console
2324 *****************************************************************************
2325 * This function is useful only on Win32.
2326 *****************************************************************************/
2328 static void PauseConsole( void )
2332 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2333 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2340 /*****************************************************************************
2341 * ConsoleWidth: Return the console width in characters
2342 *****************************************************************************
2343 * We use the stty shell command to get the console width; if this fails or
2344 * if the width is less than 80, we default to 80.
2345 *****************************************************************************/
2346 static int ConsoleWidth( void )
2351 char buf[20], *psz_parser;
2355 file = popen( "stty size 2>/dev/null", "r" );
2358 i_ret = fread( buf, 1, 20, file );
2362 psz_parser = strchr( buf, ' ' );
2365 i_ret = atoi( psz_parser + 1 );
2380 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2381 vlc_value_t old_val, vlc_value_t new_val, void *param)
2383 vlc_t *p_vlc = (vlc_t *)p_this;
2385 if( new_val.i_int >= -1 )
2387 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2392 /*****************************************************************************
2393 * InitDeviceValues: initialize device values
2394 *****************************************************************************
2395 * This function inits the dvd, vcd and cd-audio values
2396 *****************************************************************************/
2397 static void InitDeviceValues( vlc_t *p_vlc )
2400 LibHalContext * ctx;
2406 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2408 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2410 for( i = 0; i < i_devices; i++ )
2412 if( !hal_device_property_exists( ctx, devices[ i ],
2413 "storage.cdrom.dvd" ) )
2418 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2419 "storage.cdrom.dvd" );
2420 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2425 config_PutPsz( p_vlc, "dvd", block_dev );
2428 config_PutPsz( p_vlc, "vcd", block_dev );
2429 config_PutPsz( p_vlc, "cd-audio", block_dev );
2431 hal_free_string( block_dev );
2433 hal_free_string_array( devices );
2436 hal_shutdown( ctx );