1 /*****************************************************************************
2 * libvlc.c: main libvlc source
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
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"
85 /*****************************************************************************
86 * The evil global variable. We handle it with care, don't worry.
87 *****************************************************************************/
88 static libvlc_t libvlc;
89 static libvlc_t * p_libvlc;
90 static vlc_t * p_static_vlc;
92 /*****************************************************************************
94 *****************************************************************************/
95 static void SetLanguage ( char const * );
96 static int GetFilenames ( vlc_t *, int, char *[] );
97 static void Help ( vlc_t *, char const *psz_help_name );
98 static void Usage ( vlc_t *, char const *psz_module_name );
99 static void ListModules ( vlc_t * );
100 static void Version ( void );
103 static void ShowConsole ( void );
104 static void PauseConsole ( void );
106 static int ConsoleWidth ( void );
108 static int VerboseCallback( vlc_object_t *, char const *,
109 vlc_value_t, vlc_value_t, void * );
111 static void InitDeviceValues( vlc_t * );
113 /*****************************************************************************
114 * vlc_current_object: return the current object.
115 *****************************************************************************
116 * If i_object is non-zero, return the corresponding object. Otherwise,
117 * return the statically allocated p_vlc object.
118 *****************************************************************************/
119 vlc_t * vlc_current_object( int i_object )
123 return vlc_object_get( p_libvlc, i_object );
129 /*****************************************************************************
130 * VLC_Version: return the libvlc version.
131 *****************************************************************************
132 * This function returns full version string (numeric version and codename).
133 *****************************************************************************/
134 char const * VLC_Version( void )
136 return VERSION_MESSAGE;
139 /*****************************************************************************
140 * VLC_Error: strerror() equivalent
141 *****************************************************************************
142 * This function returns full version string (numeric version and codename).
143 *****************************************************************************/
144 char const * VLC_Error( int i_err )
146 return vlc_error( i_err );
149 /*****************************************************************************
150 * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
151 *****************************************************************************
152 * This function allocates a vlc_t structure and returns a negative value
153 * in case of failure. Also, the thread system is initialized.
154 *****************************************************************************/
155 int VLC_Create( void )
158 vlc_t * p_vlc = NULL;
161 /* &libvlc never changes, so we can safely call this multiple times. */
164 /* vlc_threads_init *must* be the first internal call! No other call is
165 * allowed before the thread system has been initialized. */
166 i_ret = vlc_threads_init( p_libvlc );
172 /* Now that the thread system is initialized, we don't have much, but
173 * at least we have var_Create */
174 var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
175 var_Get( p_libvlc, "libvlc", &lockval );
176 vlc_mutex_lock( lockval.p_address );
177 if( !libvlc.b_ready )
181 /* Guess what CPU we have */
182 libvlc.i_cpu = CPUCapabilities();
184 /* Find verbosity from VLC_VERBOSE environment variable */
185 psz_env = getenv( "VLC_VERBOSE" );
186 libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
188 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
189 libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
191 libvlc.b_color = VLC_FALSE;
194 /* Initialize message queue */
195 msg_Create( p_libvlc );
197 /* Announce who we are */
198 msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
199 msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
201 /* The module bank will be initialized later */
202 libvlc.p_module_bank = NULL;
204 libvlc.b_ready = VLC_TRUE;
206 vlc_mutex_unlock( lockval.p_address );
207 var_Destroy( p_libvlc, "libvlc" );
209 /* Allocate a vlc object */
210 p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
215 p_vlc->thread_id = 0;
217 p_vlc->psz_object_name = "root";
219 /* Initialize mutexes */
220 vlc_mutex_init( p_vlc, &p_vlc->config_lock );
222 vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
223 vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
226 /* Store our newly allocated structure in the global list */
227 vlc_object_attach( p_vlc, p_libvlc );
229 /* Store data for the non-reentrant API */
230 p_static_vlc = p_vlc;
232 return p_vlc->i_object_id;
235 /*****************************************************************************
236 * VLC_Init: initialize a vlc_t structure.
237 *****************************************************************************
238 * This function initializes a previously allocated vlc_t structure:
240 * - gettext initialization
241 * - message queue, module bank and playlist initialization
242 * - configuration and commandline parsing
243 *****************************************************************************/
244 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
246 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;
264 * System specific initialization code
266 system_Init( p_vlc, &i_argc, ppsz_argv );
268 /* Get the executable name (similar to the basename command) */
271 p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
274 if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
280 p_vlc->psz_object_name = "vlc";
284 * Support for gettext
288 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
289 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
291 /* Initialize the module bank and load the configuration of the
292 * main module. We need to do this at this stage to be able to display
293 * a short help if required by the user. (short help == main module
295 module_InitBank( p_vlc );
297 /* Hack: insert the help module here */
298 p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
299 if( p_help_module == NULL )
301 module_EndBank( p_vlc );
302 if( i_object ) vlc_object_release( p_vlc );
305 p_help_module->psz_object_name = "help";
306 p_help_module->psz_longname = N_("Help options");
307 config_Duplicate( p_help_module, p_help_config );
308 vlc_object_attach( p_help_module, libvlc.p_module_bank );
311 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
313 vlc_object_detach( p_help_module );
314 config_Free( p_help_module );
315 vlc_object_destroy( p_help_module );
316 module_EndBank( p_vlc );
317 if( i_object ) vlc_object_release( p_vlc );
321 /* Check for short help option */
322 if( config_GetInt( p_vlc, "help" ) )
324 Help( p_vlc, "help" );
327 /* Check for version option */
328 else if( config_GetInt( p_vlc, "version" ) )
334 /* Set the config file stuff */
335 p_vlc->psz_homedir = config_GetHomeDir();
336 p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
338 /* Check for plugins cache options */
339 if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
341 libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
344 /* Hack: remove the help module here */
345 vlc_object_detach( p_help_module );
348 /* Will be re-done properly later on */
349 p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
351 /* Check for daemon mode */
353 if( config_GetInt( p_vlc, "daemon" ) )
356 if( daemon( 0, 0) != 0 )
358 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
362 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
367 if( ( i_pid = fork() ) < 0 )
369 msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
374 /* This is the parent, exit right now */
375 msg_Dbg( p_vlc, "closing parent process" );
380 /* We are the child */
381 msg_Dbg( p_vlc, "daemon spawned" );
382 close( STDIN_FILENO );
383 close( STDOUT_FILENO );
384 close( STDERR_FILENO );
386 p_vlc->p_libvlc->b_daemon = VLC_TRUE;
394 config_Free( p_help_module );
395 vlc_object_destroy( p_help_module );
396 module_EndBank( p_vlc );
397 if( i_object ) vlc_object_release( p_vlc );
401 /* Check for translation config option */
402 #if defined( ENABLE_NLS ) \
403 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
405 /* This ain't really nice to have to reload the config here but it seems
406 * the only way to do it. */
407 config_LoadConfigFile( p_vlc, "main" );
408 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
410 /* Check if the user specified a custom language */
411 psz_language = config_GetPsz( p_vlc, "language" );
412 if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
414 vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
416 /* Reset the default domain */
417 SetLanguage( psz_language );
419 /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
420 msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
422 textdomain( PACKAGE_NAME );
424 #if defined( ENABLE_UTF8 )
425 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
428 module_EndBank( p_vlc );
429 module_InitBank( p_vlc );
430 config_LoadConfigFile( p_vlc, "main" );
431 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
432 libvlc.p_module_bank->b_cache_delete = b_cache_delete;
434 if( psz_language ) free( psz_language );
438 * Load the builtins and plugins into the module_bank.
439 * We have to do it before config_Load*() because this also gets the
440 * list of configuration options exported by each module and loads their
443 module_LoadBuiltins( p_vlc );
444 module_LoadPlugins( p_vlc );
450 msg_Dbg( p_vlc, "module bank initialized, found %i modules",
451 libvlc.p_module_bank->i_children );
453 /* Hack: insert the help module here */
454 vlc_object_attach( p_help_module, libvlc.p_module_bank );
457 /* Check for help on modules */
458 if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
460 Help( p_vlc, p_tmp );
464 /* Check for long help option */
465 else if( config_GetInt( p_vlc, "longhelp" ) )
467 Help( p_vlc, "longhelp" );
470 /* Check for module list option */
471 else if( config_GetInt( p_vlc, "list" ) )
473 ListModules( p_vlc );
477 /* Check for config file options */
478 if( config_GetInt( p_vlc, "reset-config" ) )
480 vlc_object_detach( p_help_module );
481 config_ResetAll( p_vlc );
482 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
483 config_SaveConfigFile( p_vlc, NULL );
484 vlc_object_attach( p_help_module, libvlc.p_module_bank );
486 if( config_GetInt( p_vlc, "save-config" ) )
488 vlc_object_detach( p_help_module );
489 config_LoadConfigFile( p_vlc, NULL );
490 config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
491 config_SaveConfigFile( p_vlc, NULL );
492 vlc_object_attach( p_help_module, libvlc.p_module_bank );
495 /* Hack: remove the help module here */
496 vlc_object_detach( p_help_module );
501 config_Free( p_help_module );
502 vlc_object_destroy( p_help_module );
503 module_EndBank( p_vlc );
504 if( i_object ) vlc_object_release( p_vlc );
511 InitDeviceValues( p_vlc );
514 * Override default configuration with config file settings
516 config_LoadConfigFile( p_vlc, NULL );
518 /* Hack: insert the help module here */
519 vlc_object_attach( p_help_module, libvlc.p_module_bank );
523 * Override configuration with command line settings
525 if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
529 /* Pause the console because it's destroyed when we exit */
530 fprintf( stderr, "The command line options couldn't be loaded, check "
531 "that they are valid.\n" );
534 vlc_object_detach( p_help_module );
535 config_Free( p_help_module );
536 vlc_object_destroy( p_help_module );
537 module_EndBank( p_vlc );
538 if( i_object ) vlc_object_release( p_vlc );
542 /* Hack: remove the help module here */
543 vlc_object_detach( p_help_module );
544 config_Free( p_help_module );
545 vlc_object_destroy( p_help_module );
549 * System specific configuration
551 system_Configure( p_vlc, &i_argc, ppsz_argv );
554 * Message queue options
557 var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
558 if( config_GetInt( p_vlc, "quiet" ) )
561 var_Set( p_vlc, "verbose", val );
563 var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
564 var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
566 libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
569 * Output messages that may still be in the queue
573 /* p_vlc initialization. FIXME ? */
575 if( !config_GetInt( p_vlc, "fpu" ) )
576 libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
578 #if defined( __i386__ ) || defined( __x86_64__ )
579 if( !config_GetInt( p_vlc, "mmx" ) )
580 libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
581 if( !config_GetInt( p_vlc, "3dn" ) )
582 libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
583 if( !config_GetInt( p_vlc, "mmxext" ) )
584 libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
585 if( !config_GetInt( p_vlc, "sse" ) )
586 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
587 if( !config_GetInt( p_vlc, "sse2" ) )
588 libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
590 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
591 if( !config_GetInt( p_vlc, "altivec" ) )
592 libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
595 #define PRINT_CAPABILITY( capability, string ) \
596 if( libvlc.i_cpu & capability ) \
598 strncat( p_capabilities, string " ", \
599 sizeof(p_capabilities) - strlen(p_capabilities) ); \
600 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
603 p_capabilities[0] = '\0';
604 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
605 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
606 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
607 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
608 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
609 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
610 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
611 PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
612 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
613 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
614 msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
617 * Choose the best memcpy module
619 p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
621 if( p_vlc->pf_memcpy == NULL )
623 p_vlc->pf_memcpy = memcpy;
626 if( p_vlc->pf_memset == NULL )
628 p_vlc->pf_memset = memset;
632 * Initialize hotkey handling
634 var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
635 p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
636 /* Do a copy (we don't need to modify the strings) */
637 memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
640 * Initialize playlist and get commandline files
642 p_playlist = playlist_Create( p_vlc );
645 msg_Err( p_vlc, "playlist initialization failed" );
646 if( p_vlc->p_memcpy_module != NULL )
648 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
650 module_EndBank( p_vlc );
651 if( i_object ) vlc_object_release( p_vlc );
655 psz_modules = config_GetPsz( p_playlist, "services-discovery" );
656 if( psz_modules && *psz_modules )
658 /* Add service discovery modules */
659 playlist_AddSDModules( p_playlist, psz_modules );
661 if( psz_modules ) free( psz_modules );
664 * Load background interfaces
666 psz_modules = config_GetPsz( p_vlc, "extraintf" );
667 psz_control = config_GetPsz( p_vlc, "control" );
669 if( psz_modules && *psz_modules && psz_control && *psz_control )
671 psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
672 strlen( psz_control ) + 1 );
673 sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
675 else if( psz_control && *psz_control )
677 if( psz_modules ) free( psz_modules );
678 psz_modules = strdup( psz_control );
681 psz_parser = psz_modules;
682 while ( psz_parser && *psz_parser )
684 char *psz_module, *psz_temp;
685 psz_module = psz_parser;
686 psz_parser = strchr( psz_module, ':' );
692 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
695 sprintf( psz_temp, "%s,none", psz_module );
696 VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
706 * Allways load the hotkeys interface if it exists
708 VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
711 * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
713 var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
714 var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
715 var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
716 var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
717 var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
718 var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
719 var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
720 var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
721 var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
722 var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
723 var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
724 var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
727 * Get input filenames given as commandline arguments
729 GetFilenames( p_vlc, i_argc, ppsz_argv );
732 * Get --open argument
734 var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
735 var_Get( p_vlc, "open", &val );
736 if ( val.psz_string != NULL && *val.psz_string )
738 VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
739 PLAYLIST_INSERT, 0 );
741 if ( val.psz_string != NULL ) free( val.psz_string );
743 if( i_object ) vlc_object_release( p_vlc );
747 /*****************************************************************************
748 * VLC_AddIntf: add an interface
749 *****************************************************************************
750 * This function opens an interface plugin and runs it. If b_block is set
751 * to 0, VLC_AddIntf will return immediately and let the interface run in a
752 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
753 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
754 * the playlist when it is completely initialised.
755 *****************************************************************************/
756 int VLC_AddIntf( int i_object, char const *psz_module,
757 vlc_bool_t b_block, vlc_bool_t b_play )
760 intf_thread_t *p_intf;
761 vlc_t *p_vlc = vlc_current_object( i_object );
769 if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
772 * We prefer the dummy interface if none is specified. */
773 char *psz_interface = config_GetPsz( p_vlc, "intf" );
774 if( !psz_interface || !*psz_interface ) psz_module = "dummy";
775 if( psz_interface ) free( psz_interface );
779 /* Try to create the interface */
780 p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
784 msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
785 if( i_object ) vlc_object_release( p_vlc );
789 /* Interface doesn't handle play on start so do it ourselves */
790 if( !p_intf->b_play && b_play ) VLC_Play( i_object );
792 /* Try to run the interface */
793 p_intf->b_play = b_play;
794 p_intf->b_block = b_block;
795 i_err = intf_RunThread( p_intf );
798 vlc_object_detach( p_intf );
799 intf_Destroy( p_intf );
800 if( i_object ) vlc_object_release( p_vlc );
804 if( i_object ) vlc_object_release( p_vlc );
808 /*****************************************************************************
809 * VLC_Die: ask vlc to die.
810 *****************************************************************************
811 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
812 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
813 *****************************************************************************/
814 int VLC_Die( int i_object )
816 vlc_t *p_vlc = vlc_current_object( i_object );
823 p_vlc->b_die = VLC_TRUE;
825 if( i_object ) vlc_object_release( p_vlc );
829 /*****************************************************************************
830 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
831 *****************************************************************************/
832 int VLC_CleanUp( int i_object )
834 intf_thread_t * p_intf;
835 playlist_t * p_playlist;
836 vout_thread_t * p_vout;
837 aout_instance_t * p_aout;
838 announce_handler_t * p_announce;
839 vlc_t *p_vlc = vlc_current_object( i_object );
841 /* Check that the handle is valid */
848 * Ask the interfaces to stop and destroy them
850 msg_Dbg( p_vlc, "removing all interfaces" );
851 while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
853 intf_StopThread( p_intf );
854 vlc_object_detach( p_intf );
855 vlc_object_release( p_intf );
856 intf_Destroy( p_intf );
862 msg_Dbg( p_vlc, "removing all playlists" );
863 while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
866 vlc_object_detach( p_playlist );
867 vlc_object_release( p_playlist );
868 playlist_Destroy( p_playlist );
874 msg_Dbg( p_vlc, "removing all video outputs" );
875 while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
877 vlc_object_detach( p_vout );
878 vlc_object_release( p_vout );
879 vout_Destroy( p_vout );
885 msg_Dbg( p_vlc, "removing all audio outputs" );
886 while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
888 vlc_object_detach( (vlc_object_t *)p_aout );
889 vlc_object_release( (vlc_object_t *)p_aout );
890 aout_Delete( p_aout );
894 * Free announce handler(s?)
896 msg_Dbg( p_vlc, "removing announce handler" );
897 while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
900 vlc_object_detach( p_announce );
901 vlc_object_release( p_announce );
902 announce_HandlerDestroy( p_announce );
905 if( i_object ) vlc_object_release( p_vlc );
909 /*****************************************************************************
910 * VLC_Destroy: Destroy everything.
911 *****************************************************************************
912 * This function requests the running threads to finish, waits for their
913 * termination, and destroys their structure.
914 *****************************************************************************/
915 int VLC_Destroy( int i_object )
917 vlc_t *p_vlc = vlc_current_object( i_object );
925 * Free allocated memory
927 if( p_vlc->p_memcpy_module )
929 module_Unneed( p_vlc, p_vlc->p_memcpy_module );
930 p_vlc->p_memcpy_module = NULL;
936 module_EndBank( p_vlc );
938 if( p_vlc->psz_homedir )
940 free( p_vlc->psz_homedir );
941 p_vlc->psz_homedir = NULL;
944 if( p_vlc->psz_configfile )
946 free( p_vlc->psz_configfile );
947 p_vlc->psz_configfile = NULL;
950 if( p_vlc->p_hotkeys )
952 free( p_vlc->p_hotkeys );
953 p_vlc->p_hotkeys = NULL;
957 * System specific cleaning code
962 * Free message queue.
963 * Nobody shall use msg_* afterward.
966 msg_Destroy( p_libvlc );
968 /* Destroy mutexes */
969 vlc_mutex_destroy( &p_vlc->config_lock );
971 vlc_object_detach( p_vlc );
973 /* Release object before destroying it */
974 if( i_object ) vlc_object_release( p_vlc );
976 vlc_object_destroy( p_vlc );
978 /* Stop thread system: last one out please shut the door! */
979 vlc_threads_end( p_libvlc );
984 /*****************************************************************************
985 * VLC_VariableSet: set a vlc variable
986 *****************************************************************************/
987 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
989 vlc_t *p_vlc = vlc_current_object( i_object );
997 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
998 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
999 if( !strncmp( psz_var, "conf::", 6 ) )
1001 module_config_t *p_item;
1002 char const *psz_newvar = psz_var + 6;
1004 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1008 switch( p_item->i_type )
1010 case CONFIG_ITEM_BOOL:
1011 config_PutInt( p_vlc, psz_newvar, value.b_bool );
1013 case CONFIG_ITEM_INTEGER:
1014 config_PutInt( p_vlc, psz_newvar, value.i_int );
1016 case CONFIG_ITEM_FLOAT:
1017 config_PutFloat( p_vlc, psz_newvar, value.f_float );
1020 config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1023 if( i_object ) vlc_object_release( p_vlc );
1028 i_ret = var_Set( p_vlc, psz_var, value );
1030 if( i_object ) vlc_object_release( p_vlc );
1034 /*****************************************************************************
1035 * VLC_VariableGet: get a vlc variable
1036 *****************************************************************************/
1037 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1039 vlc_t *p_vlc = vlc_current_object( i_object );
1047 i_ret = var_Get( p_vlc , psz_var, p_value );
1049 if( i_object ) vlc_object_release( p_vlc );
1053 /*****************************************************************************
1054 * VLC_VariableType: get a vlc variable type
1055 *****************************************************************************/
1056 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1059 vlc_t *p_vlc = vlc_current_object( i_object );
1066 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1067 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1068 if( !strncmp( psz_var, "conf::", 6 ) )
1070 module_config_t *p_item;
1071 char const *psz_newvar = psz_var + 6;
1073 p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1077 switch( p_item->i_type )
1079 case CONFIG_ITEM_BOOL:
1080 i_type = VLC_VAR_BOOL;
1082 case CONFIG_ITEM_INTEGER:
1083 i_type = VLC_VAR_INTEGER;
1085 case CONFIG_ITEM_FLOAT:
1086 i_type = VLC_VAR_FLOAT;
1089 i_type = VLC_VAR_STRING;
1097 i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1099 if( i_object ) vlc_object_release( p_vlc );
1109 /*****************************************************************************
1110 * VLC_AddTarget: adds a target for playing.
1111 *****************************************************************************
1112 * This function adds psz_target to the current playlist. If a playlist does
1113 * not exist, it will create one.
1114 *****************************************************************************/
1115 int VLC_AddTarget( int i_object, char const *psz_target,
1116 char const **ppsz_options, int i_options,
1117 int i_mode, int i_pos )
1120 playlist_t *p_playlist;
1121 vlc_t *p_vlc = vlc_current_object( i_object );
1128 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1130 if( p_playlist == NULL )
1132 msg_Dbg( p_vlc, "no playlist present, creating one" );
1133 p_playlist = playlist_Create( p_vlc );
1135 if( p_playlist == NULL )
1137 if( i_object ) vlc_object_release( p_vlc );
1138 return VLC_EGENERIC;
1141 vlc_object_yield( p_playlist );
1144 i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1145 i_mode, i_pos, -1, ppsz_options, i_options);
1147 vlc_object_release( p_playlist );
1149 if( i_object ) vlc_object_release( p_vlc );
1153 /*****************************************************************************
1155 *****************************************************************************/
1156 int VLC_Play( int i_object )
1158 playlist_t * p_playlist;
1159 vlc_t *p_vlc = vlc_current_object( i_object );
1161 /* Check that the handle is valid */
1167 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1171 if( i_object ) vlc_object_release( p_vlc );
1175 playlist_Play( p_playlist );
1176 vlc_object_release( p_playlist );
1178 if( i_object ) vlc_object_release( p_vlc );
1182 /*****************************************************************************
1183 * VLC_Pause: toggle pause
1184 *****************************************************************************/
1185 int VLC_Pause( int i_object )
1187 playlist_t * p_playlist;
1188 vlc_t *p_vlc = vlc_current_object( i_object );
1190 /* Check that the handle is valid */
1196 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1200 if( i_object ) vlc_object_release( p_vlc );
1204 playlist_Pause( p_playlist );
1205 vlc_object_release( p_playlist );
1207 if( i_object ) vlc_object_release( p_vlc );
1211 /*****************************************************************************
1212 * VLC_Pause: toggle pause
1213 *****************************************************************************/
1214 int VLC_Stop( int i_object )
1216 playlist_t * p_playlist;
1217 vlc_t *p_vlc = vlc_current_object( i_object );
1219 /* Check that the handle is valid */
1225 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1229 if( i_object ) vlc_object_release( p_vlc );
1233 playlist_Stop( p_playlist );
1234 vlc_object_release( p_playlist );
1236 if( i_object ) vlc_object_release( p_vlc );
1240 /*****************************************************************************
1241 * VLC_IsPlaying: Query for Playlist Status
1242 *****************************************************************************/
1243 vlc_bool_t VLC_IsPlaying( int i_object )
1245 playlist_t * p_playlist;
1246 vlc_bool_t b_playing;
1248 vlc_t *p_vlc = vlc_current_object( i_object );
1250 /* Check that the handle is valid */
1256 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1260 if( i_object ) vlc_object_release( p_vlc );
1264 b_playing = playlist_IsPlaying( p_playlist );
1265 vlc_object_release( p_playlist );
1267 if( i_object ) vlc_object_release( p_vlc );
1272 * Get the current position in a input
1274 * Return the current position as a float
1275 * \note For some inputs, this will be unknown.
1277 * \param i_object a vlc object id
1278 * \return a float in the range of 0.0 - 1.0
1280 float VLC_PositionGet( int i_object )
1282 input_thread_t *p_input;
1284 vlc_t *p_vlc = vlc_current_object( i_object );
1286 /* Check that the handle is valid */
1292 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1296 if( i_object ) vlc_object_release( p_vlc );
1300 var_Get( p_input, "position", &val );
1301 vlc_object_release( p_input );
1303 if( i_object ) vlc_object_release( p_vlc );
1308 * Set the current position in a input
1310 * Set the current position in a input and then return
1311 * the current position as a float.
1312 * \note For some inputs, this will be unknown.
1314 * \param i_object a vlc object id
1315 * \param i_position a float in the range of 0.0 - 1.0
1316 * \return a float in the range of 0.0 - 1.0
1318 float VLC_PositionSet( int i_object, float i_position )
1320 input_thread_t *p_input;
1322 vlc_t *p_vlc = vlc_current_object( i_object );
1324 /* Check that the handle is valid */
1330 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1334 if( i_object ) vlc_object_release( p_vlc );
1338 val.f_float = i_position;
1339 var_Set( p_input, "position", val );
1340 var_Get( p_input, "position", &val );
1341 vlc_object_release( p_input );
1343 if( i_object ) vlc_object_release( p_vlc );
1348 * Get the current position in a input
1350 * Return the current position in seconds from the start.
1351 * \note For some inputs, this will be unknown.
1353 * \param i_object a vlc object id
1354 * \return the offset from 0:00 in seconds
1356 int VLC_TimeGet( int i_object )
1358 input_thread_t *p_input;
1360 vlc_t *p_vlc = vlc_current_object( i_object );
1362 /* Check that the handle is valid */
1368 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1372 if( i_object ) vlc_object_release( p_vlc );
1376 var_Get( p_input, "time", &val );
1377 vlc_object_release( p_input );
1379 if( i_object ) vlc_object_release( p_vlc );
1380 return val.i_time / 1000000;
1384 * Seek to a position in the current input
1386 * Seek i_seconds in the current input. If b_relative is set,
1387 * then the seek will be relative to the current position, otherwise
1388 * it will seek to i_seconds from the beginning of the input.
1389 * \note For some inputs, this will be unknown.
1391 * \param i_object a vlc object id
1392 * \param i_seconds seconds from current position or from beginning of input
1393 * \param b_relative seek relative from current position
1394 * \return VLC_SUCCESS on success
1396 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1398 input_thread_t *p_input;
1400 vlc_t *p_vlc = vlc_current_object( i_object );
1402 /* Check that the handle is valid */
1408 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1412 if( i_object ) vlc_object_release( p_vlc );
1418 val.i_time = i_seconds;
1419 val.i_time = val.i_time * 1000000L;
1420 var_Set( p_input, "time-offset", val );
1424 val.i_time = i_seconds;
1425 val.i_time = val.i_time * 1000000L;
1426 var_Set( p_input, "time", val );
1428 vlc_object_release( p_input );
1430 if( i_object ) vlc_object_release( p_vlc );
1435 * Get the total length of a input
1437 * Return the total length in seconds from the current input.
1438 * \note For some inputs, this will be unknown.
1440 * \param i_object a vlc object id
1441 * \return the length in seconds
1443 int VLC_LengthGet( int i_object )
1445 input_thread_t *p_input;
1447 vlc_t *p_vlc = vlc_current_object( i_object );
1449 /* Check that the handle is valid */
1455 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1459 if( i_object ) vlc_object_release( p_vlc );
1463 var_Get( p_input, "length", &val );
1464 vlc_object_release( p_input );
1466 if( i_object ) vlc_object_release( p_vlc );
1467 return val.i_time / 1000000L;
1471 * Play the input faster than realtime
1473 * 2x, 4x, 8x faster than realtime
1474 * \note For some inputs, this will be impossible.
1476 * \param i_object a vlc object id
1477 * \return the current speedrate
1479 float VLC_SpeedFaster( int i_object )
1481 input_thread_t *p_input;
1483 vlc_t *p_vlc = vlc_current_object( i_object );
1485 /* Check that the handle is valid */
1491 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1495 if( i_object ) vlc_object_release( p_vlc );
1499 val.b_bool = VLC_TRUE;
1500 var_Set( p_input, "rate-faster", val );
1501 var_Get( p_input, "rate", &val );
1502 vlc_object_release( p_input );
1504 if( i_object ) vlc_object_release( p_vlc );
1505 return val.f_float / INPUT_RATE_DEFAULT;
1509 * Play the input slower than realtime
1511 * 1/2x, 1/4x, 1/8x slower than realtime
1512 * \note For some inputs, this will be impossible.
1514 * \param i_object a vlc object id
1515 * \return the current speedrate
1517 float VLC_SpeedSlower( int i_object )
1519 input_thread_t *p_input;
1521 vlc_t *p_vlc = vlc_current_object( i_object );
1523 /* Check that the handle is valid */
1529 p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1533 if( i_object ) vlc_object_release( p_vlc );
1537 val.b_bool = VLC_TRUE;
1538 var_Set( p_input, "rate-slower", val );
1539 var_Get( p_input, "rate", &val );
1540 vlc_object_release( p_input );
1542 if( i_object ) vlc_object_release( p_vlc );
1543 return val.f_float / INPUT_RATE_DEFAULT;
1547 * Return the current playlist item
1549 * Returns the index of the playlistitem that is currently selected for play.
1550 * This is valid even if nothing is currently playing.
1552 * \param i_object a vlc object id
1553 * \return the current index
1555 int VLC_PlaylistIndex( int i_object )
1558 playlist_t * p_playlist;
1559 vlc_t *p_vlc = vlc_current_object( i_object );
1561 /* Check that the handle is valid */
1567 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1571 if( i_object ) vlc_object_release( p_vlc );
1575 i_index = p_playlist->i_index;
1576 vlc_object_release( p_playlist );
1578 if( i_object ) vlc_object_release( p_vlc );
1583 * Total amount of items in the playlist
1585 * \param i_object a vlc object id
1586 * \return amount of playlist items
1588 int VLC_PlaylistNumberOfItems( int i_object )
1591 playlist_t * p_playlist;
1592 vlc_t *p_vlc = vlc_current_object( i_object );
1594 /* Check that the handle is valid */
1600 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1604 if( i_object ) vlc_object_release( p_vlc );
1608 i_size = p_playlist->i_size;
1609 vlc_object_release( p_playlist );
1611 if( i_object ) vlc_object_release( p_vlc );
1616 * Next playlist item
1618 * Skip to the next playlistitem and play it.
1620 * \param i_object a vlc object id
1621 * \return VLC_SUCCESS on success
1623 int VLC_PlaylistNext( int i_object )
1625 playlist_t * p_playlist;
1626 vlc_t *p_vlc = vlc_current_object( i_object );
1628 /* Check that the handle is valid */
1634 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1638 if( i_object ) vlc_object_release( p_vlc );
1642 playlist_Next( p_playlist );
1643 vlc_object_release( p_playlist );
1645 if( i_object ) vlc_object_release( p_vlc );
1650 * Previous playlist item
1652 * Skip to the previous playlistitem and play it.
1654 * \param i_object a vlc object id
1655 * \return VLC_SUCCESS on success
1657 int VLC_PlaylistPrev( int i_object )
1659 playlist_t * p_playlist;
1660 vlc_t *p_vlc = vlc_current_object( i_object );
1662 /* Check that the handle is valid */
1668 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1672 if( i_object ) vlc_object_release( p_vlc );
1676 playlist_Prev( p_playlist );
1677 vlc_object_release( p_playlist );
1679 if( i_object ) vlc_object_release( p_vlc );
1684 /*****************************************************************************
1685 * VLC_PlaylistClear: Empty the playlist
1686 *****************************************************************************/
1687 int VLC_PlaylistClear( int i_object )
1690 playlist_t * p_playlist;
1691 vlc_t *p_vlc = vlc_current_object( i_object );
1693 /* Check that the handle is valid */
1699 p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1703 if( i_object ) vlc_object_release( p_vlc );
1707 i_err = playlist_Clear( p_playlist );
1709 vlc_object_release( p_playlist );
1711 if( i_object ) vlc_object_release( p_vlc );
1718 * \param i_object a vlc object id
1719 * \param i_volume something in a range from 0-200
1720 * \return the new volume (range 0-200 %)
1722 int VLC_VolumeSet( int i_object, int i_volume )
1724 audio_volume_t i_vol = 0;
1725 vlc_t *p_vlc = vlc_current_object( i_object );
1727 /* Check that the handle is valid */
1733 if( i_volume >= 0 && i_volume <= 200 )
1735 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1736 aout_VolumeSet( p_vlc, i_vol );
1739 if( i_object ) vlc_object_release( p_vlc );
1740 return i_vol * 200 / AOUT_VOLUME_MAX;
1744 * Get the current volume
1746 * Retrieve the current volume.
1748 * \param i_object a vlc object id
1749 * \return the current volume (range 0-200 %)
1751 int VLC_VolumeGet( int i_object )
1753 audio_volume_t i_volume;
1754 vlc_t *p_vlc = vlc_current_object( i_object );
1756 /* Check that the handle is valid */
1762 aout_VolumeGet( p_vlc, &i_volume );
1764 if( i_object ) vlc_object_release( p_vlc );
1765 return i_volume*200/AOUT_VOLUME_MAX;
1769 * Mute/Unmute the volume
1771 * \param i_object a vlc object id
1772 * \return VLC_SUCCESS on success
1774 int VLC_VolumeMute( int i_object )
1776 vlc_t *p_vlc = vlc_current_object( i_object );
1778 /* Check that the handle is valid */
1784 aout_VolumeMute( p_vlc, NULL );
1786 if( i_object ) vlc_object_release( p_vlc );
1790 /*****************************************************************************
1791 * VLC_FullScreen: toggle fullscreen mode
1792 *****************************************************************************/
1793 int VLC_FullScreen( int i_object )
1795 vout_thread_t *p_vout;
1796 vlc_t *p_vlc = vlc_current_object( i_object );
1803 p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1807 if( i_object ) vlc_object_release( p_vlc );
1811 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1812 vlc_object_release( p_vout );
1814 if( i_object ) vlc_object_release( p_vlc );
1818 /* following functions are local */
1820 /*****************************************************************************
1821 * SetLanguage: set the interface language.
1822 *****************************************************************************
1823 * We set the LC_MESSAGES locale category for interface messages and buttons,
1824 * as well as the LC_CTYPE category for string sorting and possible wide
1825 * character support.
1826 *****************************************************************************/
1827 static void SetLanguage ( char const *psz_lang )
1829 #if defined( ENABLE_NLS ) \
1830 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1833 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1837 if( psz_lang && !*psz_lang )
1839 # if defined( HAVE_LC_MESSAGES )
1840 setlocale( LC_MESSAGES, psz_lang );
1842 setlocale( LC_CTYPE, psz_lang );
1847 /* I need that under Darwin, please check it doesn't disturb
1848 * other platforms. --Meuuh */
1849 setenv( "LANG", psz_lang, 1 );
1851 #elif defined( SYS_BEOS ) || defined( WIN32 )
1852 /* We set LC_ALL manually because it is the only way to set
1853 * the language at runtime under eg. Windows. Beware that this
1854 * makes the environment unconsistent when libvlc is unloaded and
1855 * should probably be moved to a safer place like vlc.c. */
1856 static char psz_lcall[20];
1857 snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1858 psz_lcall[19] = '\0';
1859 putenv( psz_lcall );
1862 setlocale( LC_ALL, psz_lang );
1865 /* Specify where to find the locales for current domain */
1866 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1867 psz_path = LOCALEDIR;
1869 snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1873 if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1875 fprintf( stderr, "warning: no domain %s in directory %s\n",
1876 PACKAGE_NAME, psz_path );
1879 /* Set the default domain */
1880 textdomain( PACKAGE_NAME );
1882 bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1886 /*****************************************************************************
1887 * FromLocale: converts a locale string to UTF-8
1888 *****************************************************************************/
1889 /* FIXME FIXME: it really has to be made quicker */
1890 char *FromLocale( const char *locale )
1894 if( !vlc_current_charset( &psz_charset ) )
1896 char *iptr = (ICONV_CONST char *)locale, *output, *optr;
1899 /* cannot fail (unless vlc_current_charset sucks) */
1900 vlc_iconv_t hd = vlc_iconv_open( "UTF-8", psz_charset );
1901 free( psz_charset );
1904 * We are not allowed to modify the locale pointer, even if we cast it to
1907 inb = strlen( locale );
1910 /* FIXME: I'm not sure about the value for the multiplication
1911 * (for western people, multiplication by 3 (Latin9) is sufficient) */
1912 optr = output = calloc( outb , 1);
1913 while( vlc_iconv( hd, &iptr, &inb, &optr, &outb ) == (size_t)-1 )
1914 *iptr = '?'; /* should not happen, and yes, it sucks */
1916 vlc_iconv_close( hd );
1917 return realloc( output, strlen( output ) + 1 );
1919 free( psz_charset );
1920 return (char *)locale;
1923 /*****************************************************************************
1924 * ToLocale: converts an UTF-8 string to locale
1925 *****************************************************************************/
1926 /* FIXME FIXME: it really has to be made quicker */
1927 char *ToLocale( const char *utf8 )
1931 if( !vlc_current_charset( &psz_charset ) )
1933 char *iptr = (ICONV_CONST char *)utf8, *output, *optr;
1936 /* cannot fail (unless vlc_current_charset sucks) */
1937 vlc_iconv_t hd = vlc_iconv_open( psz_charset, "UTF-8" );
1938 free( psz_charset );
1941 * We are not allowed to modify the locale pointer, even if we cast it to
1944 inb = strlen( utf8 );
1945 /* FIXME: I'm not sure about the value for the multiplication
1946 * (for western people, multiplication is not needed) */
1949 optr = output = calloc( outb, 1 );
1950 while( vlc_iconv( hd, &iptr, &inb, &optr, &outb ) == (size_t)-1 )
1951 *iptr = '?'; /* should not happen, and yes, it sucks */
1953 vlc_iconv_close( hd );
1954 return realloc( output, strlen( output ) + 1 );
1956 free( psz_charset );
1957 return (char *)utf8;
1960 void LocaleFree( const char *str )
1962 /* FIXME: this deserve a price for the most inefficient peice of code */
1965 if( !vlc_current_charset( &psz_charset ) )
1966 free( (char *)str );
1968 free( psz_charset );
1971 /* FIXME: don't use iconv at all */
1972 char *EnsureUTF8( char *str )
1980 inb = outb = strlen( str );
1981 hd = vlc_iconv_open( "UTF-8", "UTF-8" );
1982 while( vlc_iconv( hd, &istr, &inb, &ostr, &outb ) == (size_t)-1 )
1985 vlc_iconv_close( hd );
1989 /*****************************************************************************
1990 * GetFilenames: parse command line options which are not flags
1991 *****************************************************************************
1992 * Parse command line for input files as well as their associated options.
1993 * An option always follows its associated input and begins with a ":".
1994 *****************************************************************************/
1995 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1997 int i_opt, i_options;
1999 /* We assume that the remaining parameters are filenames
2000 * and their input options */
2001 for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
2003 const char *psz_target;
2006 /* Count the input options */
2007 while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
2013 /* TODO: write an internal function of this one, to avoid
2014 * unnecessary lookups. */
2015 /* FIXME: should we convert options to UTF-8 as well ?? */
2016 psz_target = FromLocale( ppsz_argv[ i_opt ] );
2017 VLC_AddTarget( p_vlc->i_object_id, psz_target,
2018 (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
2020 PLAYLIST_INSERT, 0 );
2021 LocaleFree( psz_target );
2027 /*****************************************************************************
2028 * Help: print program help
2029 *****************************************************************************
2030 * Print a short inline help. Message interface is initialized at this stage.
2031 *****************************************************************************/
2032 static void Help( vlc_t *p_this, char const *psz_help_name )
2038 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
2040 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2041 Usage( p_this, "help" );
2042 Usage( p_this, "main" );
2044 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2046 fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2047 Usage( p_this, NULL );
2049 else if( psz_help_name )
2051 Usage( p_this, psz_help_name );
2054 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2059 /*****************************************************************************
2060 * Usage: print module usage
2061 *****************************************************************************
2062 * Print a short inline help. Message interface is initialized at this stage.
2063 *****************************************************************************/
2064 static void Usage( vlc_t *p_this, char const *psz_module_name )
2066 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
2067 /* short option ------' | | | | | | |
2068 * option name ------------' | | | | | |
2069 * <bra -------------------------' | | | | |
2070 * option type or "" --------------' | | | |
2071 * ket> -----------------------------' | | |
2072 * padding spaces ---------------------' | |
2073 * comment -------------------------------' |
2074 * comment suffix --------------------------'
2076 * The purpose of having bra and ket is that we might i18n them as well.
2078 #define LINE_START 8
2079 #define PADDING_SPACES 25
2082 module_config_t *p_item;
2083 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2084 char psz_spaces_longtext[LINE_START+3];
2085 char psz_format[sizeof(FORMAT_STRING)];
2086 char psz_buffer[10000];
2089 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2090 vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2091 vlc_bool_t b_description;
2093 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2094 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2095 memset( psz_spaces_longtext, ' ', LINE_START+2 );
2096 psz_spaces_longtext[LINE_START+2] = '\0';
2098 strcpy( psz_format, FORMAT_STRING );
2100 /* List all modules */
2101 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2103 /* Enumerate the config for each module */
2104 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2106 vlc_bool_t b_help_module;
2108 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2110 if( psz_module_name && strcmp( psz_module_name,
2111 p_parser->psz_object_name ) )
2116 /* Ignore modules without config options */
2117 if( !p_parser->i_config_items )
2122 /* Ignore modules with only advanced config options if requested */
2125 for( p_item = p_parser->p_config;
2126 p_item->i_type != CONFIG_HINT_END;
2129 if( (p_item->i_type & CONFIG_ITEM) &&
2130 !p_item->b_advanced ) break;
2132 if( p_item->i_type == CONFIG_HINT_END ) continue;
2135 /* Print name of module */
2136 if( strcmp( "main", p_parser->psz_object_name ) )
2137 fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2139 b_help_module = !strcmp( "help", p_parser->psz_object_name );
2141 /* Print module options */
2142 for( p_item = p_parser->p_config;
2143 p_item->i_type != CONFIG_HINT_END;
2146 char *psz_text, *psz_spaces = psz_spaces_text;
2147 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2148 char *psz_suf = "", *psz_prefix = NULL;
2151 /* Skip deprecated options */
2152 if( p_item->psz_current )
2156 /* Skip advanced options if requested */
2157 if( p_item->b_advanced && !b_advanced )
2162 switch( p_item->i_type )
2164 case CONFIG_HINT_CATEGORY:
2165 case CONFIG_HINT_USAGE:
2166 if( !strcmp( "main", p_parser->psz_object_name ) )
2167 fprintf( stdout, "\n %s\n", p_item->psz_text );
2170 case CONFIG_ITEM_STRING:
2171 case CONFIG_ITEM_FILE:
2172 case CONFIG_ITEM_DIRECTORY:
2173 case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2174 case CONFIG_ITEM_MODULE_CAT:
2175 case CONFIG_ITEM_MODULE_LIST:
2176 case CONFIG_ITEM_MODULE_LIST_CAT:
2177 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2179 if( p_item->ppsz_list )
2182 psz_type = psz_buffer;
2184 for( i = 0; p_item->ppsz_list[i]; i++ )
2186 if( i ) strcat( psz_type, "," );
2187 strcat( psz_type, p_item->ppsz_list[i] );
2192 case CONFIG_ITEM_INTEGER:
2193 case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2194 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2196 if( p_item->i_list )
2199 psz_type = psz_buffer;
2201 for( i = 0; p_item->ppsz_list_text[i]; i++ )
2203 if( i ) strcat( psz_type, ", " );
2204 sprintf( psz_type + strlen(psz_type), "%i (%s)",
2206 p_item->ppsz_list_text[i] );
2211 case CONFIG_ITEM_FLOAT:
2212 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2214 case CONFIG_ITEM_BOOL:
2215 psz_bra = ""; psz_type = ""; psz_ket = "";
2216 if( !b_help_module )
2218 psz_suf = p_item->i_value ? _(" (default enabled)") :
2219 _(" (default disabled)");
2229 /* Add short option if any */
2230 if( p_item->i_short )
2232 sprintf( psz_short, "-%c,", p_item->i_short );
2236 strcpy( psz_short, " " );
2239 i = PADDING_SPACES - strlen( p_item->psz_name )
2240 - strlen( psz_bra ) - strlen( psz_type )
2241 - strlen( psz_ket ) - 1;
2243 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2245 psz_prefix = ", --no-";
2246 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2251 psz_spaces[0] = '\n';
2256 psz_spaces[i] = '\0';
2259 if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2261 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2262 psz_prefix, p_item->psz_name, psz_bra, psz_type,
2263 psz_ket, psz_spaces );
2267 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2268 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2271 psz_spaces[i] = ' ';
2273 /* We wrap the rest of the output */
2274 sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2275 b_description = config_GetInt( p_this, "help-verbose" );
2278 psz_text = psz_buffer;
2281 char *psz_parser, *psz_word;
2282 size_t i_end = strlen( psz_text );
2284 /* If the remaining text fits in a line, print it. */
2285 if( i_end <= (size_t)i_width )
2287 fprintf( stdout, "%s\n", psz_text );
2291 /* Otherwise, eat as many words as possible */
2292 psz_parser = psz_text;
2295 psz_word = psz_parser;
2296 psz_parser = strchr( psz_word, ' ' );
2297 /* If no space was found, we reached the end of the text
2298 * block; otherwise, we skip the space we just found. */
2299 psz_parser = psz_parser ? psz_parser + 1
2302 } while( psz_parser - psz_text <= i_width );
2304 /* We cut a word in one of these cases:
2305 * - it's the only word in the line and it's too long.
2306 * - we used less than 80% of the width and the word we are
2307 * going to wrap is longer than 40% of the width, and even
2308 * if the word would have fit in the next line. */
2309 if( psz_word == psz_text
2310 || ( psz_word - psz_text < 80 * i_width / 100
2311 && psz_parser - psz_word > 40 * i_width / 100 ) )
2313 char c = psz_text[i_width];
2314 psz_text[i_width] = '\0';
2315 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2316 psz_text += i_width;
2321 psz_word[-1] = '\0';
2322 fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2323 psz_text = psz_word;
2327 if( b_description && p_item->psz_longtext )
2329 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2330 b_description = VLC_FALSE;
2331 psz_spaces = psz_spaces_longtext;
2332 fprintf( stdout, "%s", psz_spaces );
2338 /* Release the module list */
2339 vlc_list_release( p_list );
2342 /*****************************************************************************
2343 * ListModules: list the available modules with their description
2344 *****************************************************************************
2345 * Print a list of all available modules (builtins and plugins) and a short
2346 * description for each one.
2347 *****************************************************************************/
2348 static void ListModules( vlc_t *p_this )
2352 char psz_spaces[22];
2355 memset( psz_spaces, ' ', 22 );
2361 /* List all modules */
2362 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2364 /* Enumerate each module */
2365 for( i_index = 0; i_index < p_list->i_count; i_index++ )
2369 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2371 /* Nasty hack, but right now I'm too tired to think about a nice
2373 i = 22 - strlen( p_parser->psz_object_name ) - 1;
2377 fprintf( stdout, " %s%s %s\n", p_parser->psz_object_name,
2378 psz_spaces, p_parser->psz_longname );
2380 psz_spaces[i] = ' ';
2383 vlc_list_release( p_list );
2385 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2390 /*****************************************************************************
2391 * Version: print complete program version
2392 *****************************************************************************
2393 * Print complete program version and build number.
2394 *****************************************************************************/
2395 static void Version( void )
2401 fprintf( stdout, VERSION_MESSAGE "\n" );
2403 _("This program comes with NO WARRANTY, to the extent permitted by "
2404 "law.\nYou may redistribute it under the terms of the GNU General "
2405 "Public License;\nsee the file named COPYING for details.\n"
2406 "Written by the VideoLAN team; see the AUTHORS file.\n") );
2408 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
2413 /*****************************************************************************
2414 * ShowConsole: On Win32, create an output console for debug messages
2415 *****************************************************************************
2416 * This function is useful only on Win32.
2417 *****************************************************************************/
2419 static void ShowConsole( void )
2423 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2426 freopen( "CONOUT$", "w", stdout );
2427 freopen( "CONOUT$", "w", stderr );
2428 freopen( "CONIN$", "r", stdin );
2434 /*****************************************************************************
2435 * PauseConsole: On Win32, wait for a key press before closing the console
2436 *****************************************************************************
2437 * This function is useful only on Win32.
2438 *****************************************************************************/
2440 static void PauseConsole( void )
2444 if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2445 fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2452 /*****************************************************************************
2453 * ConsoleWidth: Return the console width in characters
2454 *****************************************************************************
2455 * We use the stty shell command to get the console width; if this fails or
2456 * if the width is less than 80, we default to 80.
2457 *****************************************************************************/
2458 static int ConsoleWidth( void )
2463 char buf[20], *psz_parser;
2467 file = popen( "stty size 2>/dev/null", "r" );
2470 i_ret = fread( buf, 1, 20, file );
2474 psz_parser = strchr( buf, ' ' );
2477 i_ret = atoi( psz_parser + 1 );
2492 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2493 vlc_value_t old_val, vlc_value_t new_val, void *param)
2495 vlc_t *p_vlc = (vlc_t *)p_this;
2497 if( new_val.i_int >= -1 )
2499 p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2504 /*****************************************************************************
2505 * InitDeviceValues: initialize device values
2506 *****************************************************************************
2507 * This function inits the dvd, vcd and cd-audio values
2508 *****************************************************************************/
2509 static void InitDeviceValues( vlc_t *p_vlc )
2512 LibHalContext * ctx;
2518 if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2520 if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2522 for( i = 0; i < i_devices; i++ )
2524 if( !hal_device_property_exists( ctx, devices[ i ],
2525 "storage.cdrom.dvd" ) )
2530 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2531 "storage.cdrom.dvd" );
2532 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2537 config_PutPsz( p_vlc, "dvd", block_dev );
2540 config_PutPsz( p_vlc, "vcd", block_dev );
2541 config_PutPsz( p_vlc, "cd-audio", block_dev );
2543 hal_free_string( block_dev );
2545 hal_free_string_array( devices );
2548 hal_shutdown( ctx );