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 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;
262 * System specific initialization code
264 system_Init( p_vlc, &i_argc, ppsz_argv );
266 /* Get the executable name (similar to the basename command) */
269 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
272 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
278 p_vlc->psz_object_name = "vlc";
282 * Support for gettext
286 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
287 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
289 /* Initialize the module bank and load the configuration of the
290 * main module. We need to do this at this stage to be able to display
291 * a short help if required by the user. (short help == main module
293 module_InitBank( p_vlc );
295 /* Hack: insert the help module here */
296 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
297 if( p_help_module == NULL )
299 module_EndBank( p_vlc );
300 if( i_object ) vlc_object_release( p_vlc );
303 p_help_module->psz_object_name = "help";
304 p_help_module->psz_longname = N_("Help options");
305 config_Duplicate( p_help_module, p_help_config );
306 vlc_object_attach( p_help_module, libvlc.p_module_bank );
309 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
311 vlc_object_detach( p_help_module );
312 config_Free( p_help_module );
313 vlc_object_destroy( p_help_module );
314 module_EndBank( p_vlc );
315 if( i_object ) vlc_object_release( p_vlc );
319 /* Check for short help option */
320 if( config_GetInt( p_vlc, "help" ) )
322 Help( p_vlc, "help" );
325 /* Check for version option */
326 else if( config_GetInt( p_vlc, "version" ) )
332 /* Set the config file stuff */
333 p_vlc->psz_homedir = config_GetHomeDir();
334 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
336 /* Check for plugins cache options */
337 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
339 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
342 /* Hack: remove the help module here */
343 vlc_object_detach( p_help_module );
346 /* Will be re-done properly later on */
347 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
349 /* Check for daemon mode */
351 if( config_GetInt( p_vlc, "daemon" ) )
354 if( daemon( 0, 0) != 0 )
356 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
360 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
365 if( ( i_pid = fork() ) < 0 )
367 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
372 /* This is the parent, exit right now */
373 msg_Dbg( p_vlc, "closing parent process" );
378 /* We are the child */
379 msg_Dbg( p_vlc, "daemon spawned" );
380 close( STDIN_FILENO );
381 close( STDOUT_FILENO );
382 close( STDERR_FILENO );
384 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
392 config_Free( p_help_module );
393 vlc_object_destroy( p_help_module );
394 module_EndBank( p_vlc );
395 if( i_object ) vlc_object_release( p_vlc );
399 /* Check for translation config option */
400 #if defined( ENABLE_NLS ) \
401 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
403 /* This ain't really nice to have to reload the config here but it seems
404 * the only way to do it. */
405 config_LoadConfigFile( p_vlc, "main" );
406 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
408 /* Check if the user specified a custom language */
409 psz_language = config_GetPsz( p_vlc, "language" );
410 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
412 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
414 /* Reset the default domain */
415 SetLanguage( psz_language );
417 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
418 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
420 textdomain( PACKAGE_NAME );
422 #if defined( ENABLE_UTF8 )
423 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
426 module_EndBank( p_vlc );
427 module_InitBank( p_vlc );
428 config_LoadConfigFile( p_vlc, "main" );
429 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
430 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
432 if( psz_language ) free( psz_language );
436 * Load the builtins and plugins into the module_bank.
437 * We have to do it before config_Load*() because this also gets the
438 * list of configuration options exported by each module and loads their
441 module_LoadBuiltins( p_vlc );
442 module_LoadPlugins( p_vlc );
448 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
449 libvlc.p_module_bank->i_children );
451 /* Hack: insert the help module here */
452 vlc_object_attach( p_help_module, libvlc.p_module_bank );
455 /* Check for help on modules */
456 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
458 Help( p_vlc, p_tmp );
462 /* Check for long help option */
463 else if( config_GetInt( p_vlc, "longhelp" ) )
465 Help( p_vlc, "longhelp" );
468 /* Check for module list option */
469 else if( config_GetInt( p_vlc, "list" ) )
471 ListModules( p_vlc );
475 /* Check for config file options */
476 if( config_GetInt( p_vlc, "reset-config" ) )
478 vlc_object_detach( p_help_module );
479 config_ResetAll( p_vlc );
480 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
481 config_SaveConfigFile( p_vlc, NULL );
482 vlc_object_attach( p_help_module, libvlc.p_module_bank );
484 if( config_GetInt( p_vlc, "save-config" ) )
486 vlc_object_detach( p_help_module );
487 config_LoadConfigFile( p_vlc, NULL );
488 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
489 config_SaveConfigFile( p_vlc, NULL );
490 vlc_object_attach( p_help_module, libvlc.p_module_bank );
493 /* Hack: remove the help module here */
494 vlc_object_detach( p_help_module );
499 config_Free( p_help_module );
500 vlc_object_destroy( p_help_module );
501 module_EndBank( p_vlc );
502 if( i_object ) vlc_object_release( p_vlc );
509 InitDeviceValues( p_vlc );
512 * Override default configuration with config file settings
514 config_LoadConfigFile( p_vlc, NULL );
516 /* Hack: insert the help module here */
517 vlc_object_attach( p_help_module, libvlc.p_module_bank );
521 * Override configuration with command line settings
523 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
527 /* Pause the console because it's destroyed when we exit */
528 fprintf( stderr, "The command line options couldn't be loaded, check "
529 "that they are valid.\n" );
532 vlc_object_detach( p_help_module );
533 config_Free( p_help_module );
534 vlc_object_destroy( p_help_module );
535 module_EndBank( p_vlc );
536 if( i_object ) vlc_object_release( p_vlc );
540 /* Hack: remove the help module here */
541 vlc_object_detach( p_help_module );
542 config_Free( p_help_module );
543 vlc_object_destroy( p_help_module );
547 * System specific configuration
549 system_Configure( p_vlc, &i_argc, ppsz_argv );
552 * Message queue options
555 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
556 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 if( psz_parser && *psz_parser &&
682 strstr( psz_parser, ",") && !strstr(psz_parser, ":" ) )
684 msg_Info( p_vlc, "Warning: you are using a deprecated syntax for "
685 "extraintf / control." );
686 msg_Info( p_vlc, "You must now use ':' as separator instead of ','." );
688 while ( psz_parser && *psz_parser )
690 char *psz_module, *psz_temp;
691 psz_module = psz_parser;
692 psz_parser = strchr( psz_module, ':' );
693 if( !psz_parser ) psz_parser = strchr( psz_module, ',' );
699 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
702 sprintf( psz_temp, "%s,none", psz_module );
703 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
713 * Allways load the hotkeys interface if it exists
715 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
718 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
720 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
721 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
722 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
723 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
724 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
725 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
726 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
727 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
728 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
729 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
730 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
731 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
734 * Get input filenames given as commandline arguments
736 GetFilenames( p_vlc, i_argc, ppsz_argv );
738 if( i_object ) vlc_object_release( p_vlc );
742 /*****************************************************************************
743 * VLC_AddIntf: add an interface
744 *****************************************************************************
745 * This function opens an interface plugin and runs it. If b_block is set
746 * to 0, VLC_AddIntf will return immediately and let the interface run in a
747 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
748 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
749 * the playlist when it is completely initialised.
750 *****************************************************************************/
751 int VLC_AddIntf( int i_object, char const *psz_module,
752 vlc_bool_t b_block, vlc_bool_t b_play )
755 intf_thread_t *p_intf;
756 vlc_t *p_vlc = vlc_current_object( i_object );
764 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
767 * We prefer the dummy interface if none is specified. */
768 char *psz_interface = config_GetPsz( p_vlc, "intf" );
769 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
770 if( psz_interface ) free( psz_interface );
774 /* Try to create the interface */
775 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
779 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
780 if( i_object ) vlc_object_release( p_vlc );
784 /* Interface doesn't handle play on start so do it ourselves */
785 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
787 /* Try to run the interface */
788 p_intf->b_play = b_play;
789 p_intf->b_block = b_block;
790 i_err = intf_RunThread( p_intf );
793 vlc_object_detach( p_intf );
794 intf_Destroy( p_intf );
795 if( i_object ) vlc_object_release( p_vlc );
799 if( i_object ) vlc_object_release( p_vlc );
803 /*****************************************************************************
804 * VLC_Die: ask vlc to die.
805 *****************************************************************************
806 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
807 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
808 *****************************************************************************/
809 int VLC_Die( int i_object )
811 vlc_t *p_vlc = vlc_current_object( i_object );
818 p_vlc->b_die = VLC_TRUE;
820 if( i_object ) vlc_object_release( p_vlc );
824 /*****************************************************************************
825 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
826 *****************************************************************************/
827 int VLC_CleanUp( int i_object )
829 intf_thread_t * p_intf;
830 playlist_t * p_playlist;
831 vout_thread_t * p_vout;
832 aout_instance_t * p_aout;
833 announce_handler_t * p_announce;
834 vlc_t *p_vlc = vlc_current_object( i_object );
836 /* Check that the handle is valid */
843 * Ask the interfaces to stop and destroy them
845 msg_Dbg( p_vlc, "removing all interfaces" );
846 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
848 intf_StopThread( p_intf );
849 vlc_object_detach( p_intf );
850 vlc_object_release( p_intf );
851 intf_Destroy( p_intf );
857 msg_Dbg( p_vlc, "removing all playlists" );
858 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
861 vlc_object_detach( p_playlist );
862 vlc_object_release( p_playlist );
863 playlist_Destroy( p_playlist );
869 msg_Dbg( p_vlc, "removing all video outputs" );
870 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
872 vlc_object_detach( p_vout );
873 vlc_object_release( p_vout );
874 vout_Destroy( p_vout );
880 msg_Dbg( p_vlc, "removing all audio outputs" );
881 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
883 vlc_object_detach( (vlc_object_t *)p_aout );
884 vlc_object_release( (vlc_object_t *)p_aout );
885 aout_Delete( p_aout );
889 * Free announce handler(s?)
891 msg_Dbg( p_vlc, "removing announce handler" );
892 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
895 vlc_object_detach( p_announce );
896 vlc_object_release( p_announce );
897 announce_HandlerDestroy( p_announce );
900 if( i_object ) vlc_object_release( p_vlc );
904 /*****************************************************************************
905 * VLC_Destroy: Destroy everything.
906 *****************************************************************************
907 * This function requests the running threads to finish, waits for their
908 * termination, and destroys their structure.
909 *****************************************************************************/
910 int VLC_Destroy( int i_object )
912 vlc_t *p_vlc = vlc_current_object( i_object );
920 * Free allocated memory
922 if( p_vlc->p_memcpy_module )
924 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
925 p_vlc->p_memcpy_module = NULL;
931 module_EndBank( p_vlc );
933 if( p_vlc->psz_homedir )
935 free( p_vlc->psz_homedir );
936 p_vlc->psz_homedir = NULL;
939 if( p_vlc->psz_configfile )
941 free( p_vlc->psz_configfile );
942 p_vlc->psz_configfile = NULL;
945 if( p_vlc->p_hotkeys )
947 free( p_vlc->p_hotkeys );
948 p_vlc->p_hotkeys = NULL;
952 * System specific cleaning code
956 /* Destroy mutexes */
957 vlc_mutex_destroy( &p_vlc->config_lock );
959 vlc_object_detach( p_vlc );
961 /* Release object before destroying it */
962 if( i_object ) vlc_object_release( p_vlc );
964 vlc_object_destroy( p_vlc );
966 /* Stop thread system: last one out please shut the door! */
967 vlc_threads_end( p_libvlc );
972 /*****************************************************************************
973 * VLC_VariableSet: set a vlc variable
974 *****************************************************************************/
975 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
977 vlc_t *p_vlc = vlc_current_object( i_object );
985 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
986 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
987 if( !strncmp( psz_var, "conf::", 6 ) )
989 module_config_t *p_item;
990 char const *psz_newvar = psz_var + 6;
992 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
996 switch( p_item->i_type )
998 case CONFIG_ITEM_BOOL:
999 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1001 case CONFIG_ITEM_INTEGER:
1002 config_PutInt( p_vlc, psz_newvar, value.i_int );
1004 case CONFIG_ITEM_FLOAT:
1005 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1008 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1011 if( i_object ) vlc_object_release( p_vlc );
1016 i_ret = var_Set( p_vlc, psz_var, value );
1018 if( i_object ) vlc_object_release( p_vlc );
1022 /*****************************************************************************
1023 * VLC_VariableGet: get a vlc variable
1024 *****************************************************************************/
1025 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1027 vlc_t *p_vlc = vlc_current_object( i_object );
1035 i_ret = var_Get( p_vlc , psz_var, p_value );
1037 if( i_object ) vlc_object_release( p_vlc );
1041 /*****************************************************************************
1042 * VLC_VariableType: get a vlc variable type
1043 *****************************************************************************/
1044 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1047 vlc_t *p_vlc = vlc_current_object( i_object );
1054 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1055 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1056 if( !strncmp( psz_var, "conf::", 6 ) )
1058 module_config_t *p_item;
1059 char const *psz_newvar = psz_var + 6;
1061 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1065 switch( p_item->i_type )
1067 case CONFIG_ITEM_BOOL:
1068 i_type = VLC_VAR_BOOL;
1070 case CONFIG_ITEM_INTEGER:
1071 i_type = VLC_VAR_INTEGER;
1073 case CONFIG_ITEM_FLOAT:
1074 i_type = VLC_VAR_FLOAT;
1077 i_type = VLC_VAR_STRING;
1085 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1087 if( i_object ) vlc_object_release( p_vlc );
1097 /*****************************************************************************
1098 * VLC_AddTarget: adds a target for playing.
1099 *****************************************************************************
1100 * This function adds psz_target to the current playlist. If a playlist does
1101 * not exist, it will create one.
1102 *****************************************************************************/
1103 int VLC_AddTarget( int i_object, char const *psz_target,
1104 char const **ppsz_options, int i_options,
1105 int i_mode, int i_pos )
1108 playlist_t *p_playlist;
1109 vlc_t *p_vlc = vlc_current_object( i_object );
1116 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1118 if( p_playlist == NULL )
1120 msg_Dbg( p_vlc, "no playlist present, creating one" );
1121 p_playlist = playlist_Create( p_vlc );
1123 if( p_playlist == NULL )
1125 if( i_object ) vlc_object_release( p_vlc );
1126 return VLC_EGENERIC;
1129 vlc_object_yield( p_playlist );
1132 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1133 i_mode, i_pos, -1, ppsz_options, i_options);
1135 vlc_object_release( p_playlist );
1137 if( i_object ) vlc_object_release( p_vlc );
1141 /*****************************************************************************
1143 *****************************************************************************/
1144 int VLC_Play( int i_object )
1146 playlist_t * p_playlist;
1147 vlc_t *p_vlc = vlc_current_object( i_object );
1149 /* Check that the handle is valid */
1155 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1159 if( i_object ) vlc_object_release( p_vlc );
1163 playlist_Play( p_playlist );
1164 vlc_object_release( p_playlist );
1166 if( i_object ) vlc_object_release( p_vlc );
1170 /*****************************************************************************
1171 * VLC_Pause: toggle pause
1172 *****************************************************************************/
1173 int VLC_Pause( int i_object )
1175 playlist_t * p_playlist;
1176 vlc_t *p_vlc = vlc_current_object( i_object );
1178 /* Check that the handle is valid */
1184 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1188 if( i_object ) vlc_object_release( p_vlc );
1192 playlist_Pause( p_playlist );
1193 vlc_object_release( p_playlist );
1195 if( i_object ) vlc_object_release( p_vlc );
1199 /*****************************************************************************
1200 * VLC_Pause: toggle pause
1201 *****************************************************************************/
1202 int VLC_Stop( int i_object )
1204 playlist_t * p_playlist;
1205 vlc_t *p_vlc = vlc_current_object( i_object );
1207 /* Check that the handle is valid */
1213 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1217 if( i_object ) vlc_object_release( p_vlc );
1221 playlist_Stop( p_playlist );
1222 vlc_object_release( p_playlist );
1224 if( i_object ) vlc_object_release( p_vlc );
1228 /*****************************************************************************
1229 * VLC_IsPlaying: Query for Playlist Status
1230 *****************************************************************************/
1231 vlc_bool_t VLC_IsPlaying( int i_object )
1233 playlist_t * p_playlist;
1234 vlc_bool_t b_playing;
1236 vlc_t *p_vlc = vlc_current_object( i_object );
1238 /* Check that the handle is valid */
1244 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1248 if( i_object ) vlc_object_release( p_vlc );
1252 b_playing = playlist_IsPlaying( p_playlist );
1253 vlc_object_release( p_playlist );
1255 if( i_object ) vlc_object_release( p_vlc );
1260 * Get the current position in a input
1262 * Return the current position as a float
1263 * \note For some inputs, this will be unknown.
1265 * \param i_object a vlc object id
1266 * \return a float in the range of 0.0 - 1.0
1268 float VLC_PositionGet( int i_object )
1270 input_thread_t *p_input;
1272 vlc_t *p_vlc = vlc_current_object( i_object );
1274 /* Check that the handle is valid */
1280 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1284 if( i_object ) vlc_object_release( p_vlc );
1288 var_Get( p_input, "position", &val );
1289 vlc_object_release( p_input );
1291 if( i_object ) vlc_object_release( p_vlc );
1296 * Set the current position in a input
1298 * Set the current position in a input and then return
1299 * the current position as a float.
1300 * \note For some inputs, this will be unknown.
1302 * \param i_object a vlc object id
1303 * \param i_position a float in the range of 0.0 - 1.0
1304 * \return a float in the range of 0.0 - 1.0
1306 float VLC_PositionSet( int i_object, float i_position )
1308 input_thread_t *p_input;
1310 vlc_t *p_vlc = vlc_current_object( i_object );
1312 /* Check that the handle is valid */
1318 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1322 if( i_object ) vlc_object_release( p_vlc );
1326 val.f_float = i_position;
1327 var_Set( p_input, "position", val );
1328 var_Get( p_input, "position", &val );
1329 vlc_object_release( p_input );
1331 if( i_object ) vlc_object_release( p_vlc );
1336 * Get the current position in a input
1338 * Return the current position in seconds from the start.
1339 * \note For some inputs, this will be unknown.
1341 * \param i_object a vlc object id
1342 * \return the offset from 0:00 in seconds
1344 int VLC_TimeGet( int i_object )
1346 input_thread_t *p_input;
1348 vlc_t *p_vlc = vlc_current_object( i_object );
1350 /* Check that the handle is valid */
1356 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1360 if( i_object ) vlc_object_release( p_vlc );
1364 var_Get( p_input, "time", &val );
1365 vlc_object_release( p_input );
1367 if( i_object ) vlc_object_release( p_vlc );
1368 return val.i_time / 1000000;
1372 * Seek to a position in the current input
1374 * Seek i_seconds in the current input. If b_relative is set,
1375 * then the seek will be relative to the current position, otherwise
1376 * it will seek to i_seconds from the beginning of the input.
1377 * \note For some inputs, this will be unknown.
1379 * \param i_object a vlc object id
1380 * \param i_seconds seconds from current position or from beginning of input
1381 * \param b_relative seek relative from current position
1382 * \return VLC_SUCCESS on success
1384 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1386 input_thread_t *p_input;
1388 vlc_t *p_vlc = vlc_current_object( i_object );
1390 /* Check that the handle is valid */
1396 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1400 if( i_object ) vlc_object_release( p_vlc );
1406 val.i_time = i_seconds;
1407 val.i_time = val.i_time * 1000000L;
1408 var_Set( p_input, "time-offset", val );
1412 val.i_time = i_seconds;
1413 val.i_time = val.i_time * 1000000L;
1414 var_Set( p_input, "time", val );
1416 vlc_object_release( p_input );
1418 if( i_object ) vlc_object_release( p_vlc );
1423 * Get the total length of a input
1425 * Return the total length in seconds from the current input.
1426 * \note For some inputs, this will be unknown.
1428 * \param i_object a vlc object id
1429 * \return the length in seconds
1431 int VLC_LengthGet( int i_object )
1433 input_thread_t *p_input;
1435 vlc_t *p_vlc = vlc_current_object( i_object );
1437 /* Check that the handle is valid */
1443 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1447 if( i_object ) vlc_object_release( p_vlc );
1451 var_Get( p_input, "length", &val );
1452 vlc_object_release( p_input );
1454 if( i_object ) vlc_object_release( p_vlc );
1455 return val.i_time / 1000000L;
1459 * Play the input faster than realtime
1461 * 2x, 4x, 8x faster than realtime
1462 * \note For some inputs, this will be impossible.
1464 * \param i_object a vlc object id
1465 * \return the current speedrate
1467 float VLC_SpeedFaster( int i_object )
1469 input_thread_t *p_input;
1471 vlc_t *p_vlc = vlc_current_object( i_object );
1473 /* Check that the handle is valid */
1479 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1483 if( i_object ) vlc_object_release( p_vlc );
1487 val.b_bool = VLC_TRUE;
1488 var_Set( p_input, "rate-faster", val );
1489 var_Get( p_input, "rate", &val );
1490 vlc_object_release( p_input );
1492 if( i_object ) vlc_object_release( p_vlc );
1493 return val.f_float / INPUT_RATE_DEFAULT;
1497 * Play the input slower than realtime
1499 * 1/2x, 1/4x, 1/8x slower than realtime
1500 * \note For some inputs, this will be impossible.
1502 * \param i_object a vlc object id
1503 * \return the current speedrate
1505 float VLC_SpeedSlower( int i_object )
1507 input_thread_t *p_input;
1509 vlc_t *p_vlc = vlc_current_object( i_object );
1511 /* Check that the handle is valid */
1517 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1521 if( i_object ) vlc_object_release( p_vlc );
1525 val.b_bool = VLC_TRUE;
1526 var_Set( p_input, "rate-slower", val );
1527 var_Get( p_input, "rate", &val );
1528 vlc_object_release( p_input );
1530 if( i_object ) vlc_object_release( p_vlc );
1531 return val.f_float / INPUT_RATE_DEFAULT;
1535 * Return the current playlist item
1537 * Returns the index of the playlistitem that is currently selected for play.
1538 * This is valid even if nothing is currently playing.
1540 * \param i_object a vlc object id
1541 * \return the current index
1543 int VLC_PlaylistIndex( 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 i_index = p_playlist->i_index;
1564 vlc_object_release( p_playlist );
1566 if( i_object ) vlc_object_release( p_vlc );
1571 * Total amount of items in the playlist
1573 * \param i_object a vlc object id
1574 * \return amount of playlist items
1576 int VLC_PlaylistNumberOfItems( int i_object )
1579 playlist_t * p_playlist;
1580 vlc_t *p_vlc = vlc_current_object( i_object );
1582 /* Check that the handle is valid */
1588 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1592 if( i_object ) vlc_object_release( p_vlc );
1596 i_size = p_playlist->i_size;
1597 vlc_object_release( p_playlist );
1599 if( i_object ) vlc_object_release( p_vlc );
1604 * Next playlist item
1606 * Skip to the next playlistitem and play it.
1608 * \param i_object a vlc object id
1609 * \return VLC_SUCCESS on success
1611 int VLC_PlaylistNext( int i_object )
1613 playlist_t * p_playlist;
1614 vlc_t *p_vlc = vlc_current_object( i_object );
1616 /* Check that the handle is valid */
1622 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1626 if( i_object ) vlc_object_release( p_vlc );
1630 playlist_Next( p_playlist );
1631 vlc_object_release( p_playlist );
1633 if( i_object ) vlc_object_release( p_vlc );
1638 * Previous playlist item
1640 * Skip to the previous playlistitem and play it.
1642 * \param i_object a vlc object id
1643 * \return VLC_SUCCESS on success
1645 int VLC_PlaylistPrev( int i_object )
1647 playlist_t * p_playlist;
1648 vlc_t *p_vlc = vlc_current_object( i_object );
1650 /* Check that the handle is valid */
1656 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1660 if( i_object ) vlc_object_release( p_vlc );
1664 playlist_Prev( p_playlist );
1665 vlc_object_release( p_playlist );
1667 if( i_object ) vlc_object_release( p_vlc );
1672 /*****************************************************************************
1673 * VLC_PlaylistClear: Empty the playlist
1674 *****************************************************************************/
1675 int VLC_PlaylistClear( int i_object )
1678 playlist_t * p_playlist;
1679 vlc_t *p_vlc = vlc_current_object( i_object );
1681 /* Check that the handle is valid */
1687 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1691 if( i_object ) vlc_object_release( p_vlc );
1695 i_err = playlist_Clear( p_playlist );
1697 vlc_object_release( p_playlist );
1699 if( i_object ) vlc_object_release( p_vlc );
1706 * \param i_object a vlc object id
1707 * \param i_volume something in a range from 0-200
1708 * \return the new volume (range 0-200 %)
1710 int VLC_VolumeSet( int i_object, int i_volume )
1712 audio_volume_t i_vol = 0;
1713 vlc_t *p_vlc = vlc_current_object( i_object );
1715 /* Check that the handle is valid */
1721 if( i_volume >= 0 && i_volume <= 200 )
1723 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1724 aout_VolumeSet( p_vlc, i_vol );
1727 if( i_object ) vlc_object_release( p_vlc );
1728 return i_vol * 200 / AOUT_VOLUME_MAX;
1732 * Get the current volume
1734 * Retrieve the current volume.
1736 * \param i_object a vlc object id
1737 * \return the current volume (range 0-200 %)
1739 int VLC_VolumeGet( int i_object )
1741 audio_volume_t i_volume;
1742 vlc_t *p_vlc = vlc_current_object( i_object );
1744 /* Check that the handle is valid */
1750 aout_VolumeGet( p_vlc, &i_volume );
1752 if( i_object ) vlc_object_release( p_vlc );
1753 return i_volume*200/AOUT_VOLUME_MAX;
1757 * Mute/Unmute the volume
1759 * \param i_object a vlc object id
1760 * \return VLC_SUCCESS on success
1762 int VLC_VolumeMute( int i_object )
1764 vlc_t *p_vlc = vlc_current_object( i_object );
1766 /* Check that the handle is valid */
1772 aout_VolumeMute( p_vlc, NULL );
1774 if( i_object ) vlc_object_release( p_vlc );
1778 /*****************************************************************************
1779 * VLC_FullScreen: toggle fullscreen mode
1780 *****************************************************************************/
1781 int VLC_FullScreen( int i_object )
1783 vout_thread_t *p_vout;
1784 vlc_t *p_vlc = vlc_current_object( i_object );
1791 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1795 if( i_object ) vlc_object_release( p_vlc );
1799 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1800 vlc_object_release( p_vout );
1802 if( i_object ) vlc_object_release( p_vlc );
1806 /* following functions are local */
1808 /*****************************************************************************
1809 * SetLanguage: set the interface language.
1810 *****************************************************************************
1811 * We set the LC_MESSAGES locale category for interface messages and buttons,
1812 * as well as the LC_CTYPE category for string sorting and possible wide
1813 * character support.
1814 *****************************************************************************/
1815 static void SetLanguage ( char const *psz_lang )
1817 #if defined( ENABLE_NLS ) \
1818 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1821 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1825 if( psz_lang && !*psz_lang )
1827 # if defined( HAVE_LC_MESSAGES )
1828 setlocale( LC_MESSAGES, psz_lang );
1830 setlocale( LC_CTYPE, psz_lang );
1835 /* I need that under Darwin, please check it doesn't disturb
1836 * other platforms. --Meuuh */
1837 setenv( "LANG", psz_lang, 1 );
1839 #elif defined( SYS_BEOS ) || defined( WIN32 )
1840 /* We set LC_ALL manually because it is the only way to set
1841 * the language at runtime under eg. Windows. Beware that this
1842 * makes the environment unconsistent when libvlc is unloaded and
1843 * should probably be moved to a safer place like vlc.c. */
1844 static char psz_lcall[20];
1845 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1846 psz_lcall[19] = '\0';
1847 putenv( psz_lcall );
1850 setlocale( LC_ALL, psz_lang );
1853 /* Specify where to find the locales for current domain */
1854 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1855 psz_path = LOCALEDIR;
1857 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1861 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1863 fprintf( stderr, "warning: no domain %s in directory %s\n",
1864 PACKAGE_NAME, psz_path );
1867 /* Set the default domain */
1868 textdomain( PACKAGE_NAME );
1870 #if defined( ENABLE_UTF8 )
1871 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1877 /*****************************************************************************
1878 * GetFilenames: parse command line options which are not flags
1879 *****************************************************************************
1880 * Parse command line for input files as well as their associated options.
1881 * An option always follows its associated input and begins with a ":".
1882 *****************************************************************************/
1883 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1885 int i_opt, i_options;
1887 /* We assume that the remaining parameters are filenames
1888 * and their input options */
1889 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1893 /* Count the input options */
1894 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1900 /* TODO: write an internal function of this one, to avoid
1901 * unnecessary lookups. */
1902 VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1903 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1905 PLAYLIST_INSERT, 0 );
1911 /*****************************************************************************
1912 * Help: print program help
1913 *****************************************************************************
1914 * Print a short inline help. Message interface is initialized at this stage.
1915 *****************************************************************************/
1916 static void Help( vlc_t *p_this, char const *psz_help_name )
1922 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1924 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1925 Usage( p_this, "help" );
1926 Usage( p_this, "main" );
1928 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1930 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1931 Usage( p_this, NULL );
1933 else if( psz_help_name )
1935 Usage( p_this, psz_help_name );
1938 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1943 /*****************************************************************************
1944 * Usage: print module usage
1945 *****************************************************************************
1946 * Print a short inline help. Message interface is initialized at this stage.
1947 *****************************************************************************/
1948 static void Usage( vlc_t *p_this, char const *psz_module_name )
1950 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
1951 /* short option ------' | | | | | | |
1952 * option name ------------' | | | | | |
1953 * <bra -------------------------' | | | | |
1954 * option type or "" --------------' | | | |
1955 * ket> -----------------------------' | | |
1956 * padding spaces ---------------------' | |
1957 * comment -------------------------------' |
1958 * comment suffix --------------------------'
1960 * The purpose of having bra and ket is that we might i18n them as well.
1962 #define LINE_START 8
1963 #define PADDING_SPACES 25
1966 module_config_t *p_item;
1967 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1968 char psz_spaces_longtext[LINE_START+3];
1969 char psz_format[sizeof(FORMAT_STRING)];
1970 char psz_buffer[10000];
1973 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1974 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1975 vlc_bool_t b_description;
1977 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1978 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1979 memset( psz_spaces_longtext, ' ', LINE_START+2 );
1980 psz_spaces_longtext[LINE_START+2] = '\0';
1982 strcpy( psz_format, FORMAT_STRING );
1984 /* List all modules */
1985 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1987 /* Enumerate the config for each module */
1988 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1990 vlc_bool_t b_help_module;
1992 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1994 if( psz_module_name && strcmp( psz_module_name,
1995 p_parser->psz_object_name ) )
2000 /* Ignore modules without config options */
2001 if( !p_parser->i_config_items )
2006 /* Ignore modules with only advanced config options if requested */
2009 for( p_item = p_parser->p_config;
2010 p_item->i_type != CONFIG_HINT_END;
2013 if( (p_item->i_type & CONFIG_ITEM) &&
2014 !p_item->b_advanced ) break;
2016 if( p_item->i_type == CONFIG_HINT_END ) continue;
2019 /* Print name of module */
2020 if( strcmp( "main", p_parser->psz_object_name ) )
2021 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2023 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2025 /* Print module options */
2026 for( p_item = p_parser->p_config;
2027 p_item->i_type != CONFIG_HINT_END;
2030 char *psz_text, *psz_spaces = psz_spaces_text;
2031 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2032 char *psz_suf = "", *psz_prefix = NULL;
2035 /* Skip advanced options if requested */
2036 if( p_item->b_advanced && !b_advanced )
2041 switch( p_item->i_type )
2043 case CONFIG_HINT_CATEGORY:
2044 case CONFIG_HINT_USAGE:
2045 if( !strcmp( "main", p_parser->psz_object_name ) )
2046 fprintf( stdout, "\n %s\n", p_item->psz_text );
2049 case CONFIG_ITEM_STRING:
2050 case CONFIG_ITEM_FILE:
2051 case CONFIG_ITEM_DIRECTORY:
2052 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2053 case CONFIG_ITEM_MODULE_CAT:
2054 case CONFIG_ITEM_MODULE_LIST:
2055 case CONFIG_ITEM_MODULE_LIST_CAT:
2056 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2058 if( p_item->ppsz_list )
2061 psz_type = psz_buffer;
2063 for( i = 0; p_item->ppsz_list[i]; i++ )
2065 if( i ) strcat( psz_type, "," );
2066 strcat( psz_type, p_item->ppsz_list[i] );
2071 case CONFIG_ITEM_INTEGER:
2072 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2073 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2075 if( p_item->i_list )
2078 psz_type = psz_buffer;
2080 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2082 if( i ) strcat( psz_type, ", " );
2083 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2085 p_item->ppsz_list_text[i] );
2090 case CONFIG_ITEM_FLOAT:
2091 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2093 case CONFIG_ITEM_BOOL:
2094 psz_bra = ""; psz_type = ""; psz_ket = "";
2095 if( !b_help_module )
2097 psz_suf = p_item->i_value ? _(" (default enabled)") :
2098 _(" (default disabled)");
2108 /* Add short option if any */
2109 if( p_item->i_short )
2111 sprintf( psz_short, "-%c,", p_item->i_short );
2115 strcpy( psz_short, " " );
2118 i = PADDING_SPACES - strlen( p_item->psz_name )
2119 - strlen( psz_bra ) - strlen( psz_type )
2120 - strlen( psz_ket ) - 1;
2122 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2124 /* If option is of type --foo-bar, we print its counterpart
2125 * as --no-foo-bar, but if it is of type --foobar (without
2126 * dashes in the name) we print it as --nofoobar. Both
2127 * values are of course valid, only the display changes. */
2128 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
2130 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2135 psz_spaces[0] = '\n';
2140 psz_spaces[i] = '\0';
2143 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2145 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2146 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2147 psz_ket, psz_spaces );
2151 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2152 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2155 psz_spaces[i] = ' ';
2157 /* We wrap the rest of the output */
2158 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2159 b_description = config_GetInt( p_this, "help-verbose" );
2162 psz_text = psz_buffer;
2165 char *psz_parser, *psz_word;
2166 int i_end = strlen( psz_text );
2168 /* If the remaining text fits in a line, print it. */
2169 if( i_end <= i_width )
2171 fprintf( stdout, "%s\n", psz_text );
2175 /* Otherwise, eat as many words as possible */
2176 psz_parser = psz_text;
2179 psz_word = psz_parser;
2180 psz_parser = strchr( psz_word, ' ' );
2181 /* If no space was found, we reached the end of the text
2182 * block; otherwise, we skip the space we just found. */
2183 psz_parser = psz_parser ? psz_parser + 1
2186 } while( psz_parser - psz_text <= i_width );
2188 /* We cut a word in one of these cases:
2189 * - it's the only word in the line and it's too long.
2190 * - we used less than 80% of the width and the word we are
2191 * going to wrap is longer than 40% of the width, and even
2192 * if the word would have fit in the next line. */
2193 if( psz_word == psz_text
2194 || ( psz_word - psz_text < 80 * i_width / 100
2195 && psz_parser - psz_word > 40 * i_width / 100 ) )
2197 char c = psz_text[i_width];
2198 psz_text[i_width] = '\0';
2199 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2200 psz_text += i_width;
2205 psz_word[-1] = '\0';
2206 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2207 psz_text = psz_word;
2211 if( b_description && p_item->psz_longtext )
2213 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2214 b_description = VLC_FALSE;
2215 psz_spaces = psz_spaces_longtext;
2216 fprintf( stdout, "%s", psz_spaces );
2222 /* Release the module list */
2223 vlc_list_release( p_list );
2226 /*****************************************************************************
2227 * ListModules: list the available modules with their description
2228 *****************************************************************************
2229 * Print a list of all available modules (builtins and plugins) and a short
2230 * description for each one.
2231 *****************************************************************************/
2232 static void ListModules( vlc_t *p_this )
2236 char psz_spaces[22];
2239 memset( psz_spaces, ' ', 22 );
2245 /* List all modules */
2246 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2248 /* Enumerate each module */
2249 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2253 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2255 /* Nasty hack, but right now I'm too tired to think about a nice
2257 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2261 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2262 psz_spaces, p_parser->psz_longname );
2264 psz_spaces[i] = ' ';
2267 vlc_list_release( p_list );
2269 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2274 /*****************************************************************************
2275 * Version: print complete program version
2276 *****************************************************************************
2277 * Print complete program version and build number.
2278 *****************************************************************************/
2279 static void Version( void )
2285 fprintf( stdout, VERSION_MESSAGE "\n" );
2287 _("This program comes with NO WARRANTY, to the extent permitted by "
2288 "law.\nYou may redistribute it under the terms of the GNU General "
2289 "Public License;\nsee the file named COPYING for details.\n"
2290 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2292 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2297 /*****************************************************************************
2298 * ShowConsole: On Win32, create an output console for debug messages
2299 *****************************************************************************
2300 * This function is useful only on Win32.
2301 *****************************************************************************/
2303 static void ShowConsole( void )
2307 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2310 freopen( "CONOUT$", "w", stdout );
2311 freopen( "CONOUT$", "w", stderr );
2312 freopen( "CONIN$", "r", stdin );
2318 /*****************************************************************************
2319 * PauseConsole: On Win32, wait for a key press before closing the console
2320 *****************************************************************************
2321 * This function is useful only on Win32.
2322 *****************************************************************************/
2324 static void PauseConsole( void )
2328 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2329 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2336 /*****************************************************************************
2337 * ConsoleWidth: Return the console width in characters
2338 *****************************************************************************
2339 * We use the stty shell command to get the console width; if this fails or
2340 * if the width is less than 80, we default to 80.
2341 *****************************************************************************/
2342 static int ConsoleWidth( void )
2347 char buf[20], *psz_parser;
2351 file = popen( "stty size 2>/dev/null", "r" );
2354 i_ret = fread( buf, 1, 20, file );
2358 psz_parser = strchr( buf, ' ' );
2361 i_ret = atoi( psz_parser + 1 );
2376 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2377 vlc_value_t old_val, vlc_value_t new_val, void *param)
2379 vlc_t *p_vlc = (vlc_t *)p_this;
2381 if( new_val.i_int >= -1 )
2383 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2388 /*****************************************************************************
2389 * InitDeviceValues: initialize device values
2390 *****************************************************************************
2391 * This function inits the dvd, vcd and cd-audio values
2392 *****************************************************************************/
2393 static void InitDeviceValues( vlc_t *p_vlc )
2396 LibHalContext * ctx;
2402 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2404 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2406 for( i = 0; i < i_devices; i++ )
2408 if( !hal_device_property_exists( ctx, devices[ i ],
2409 "storage.cdrom.dvd" ) )
2414 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2415 "storage.cdrom.dvd" );
2416 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2421 config_PutPsz( p_vlc, "dvd", block_dev );
2424 config_PutPsz( p_vlc, "vcd", block_dev );
2425 config_PutPsz( p_vlc, "cd-audio", block_dev );
2427 hal_free_string( block_dev );
2429 hal_free_string_array( devices );
2432 hal_shutdown( ctx );