]> git.sesse.net Git - vlc/blob - src/libvlc.c
* src/libvlc.c: When the user dir is not available, fall back onto the
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Pretend we are a builtin module
29  *****************************************************************************/
30 #define MODULE_NAME main
31 #define MODULE_PATH main
32 #define __BUILTIN__
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #include <errno.h>                                                 /* ENOMEM */
41 #include <stdio.h>                                              /* sprintf() */
42 #include <string.h>                                            /* strerror() */
43 #include <stdlib.h>                                                /* free() */
44
45 #ifndef WIN32
46 #   include <netinet/in.h>                            /* BSD: struct in_addr */
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #   include <unistd.h>
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 #   include <io.h>
53 #endif
54
55 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
56 #   include "extras/getopt.h"
57 #endif
58
59 #ifdef HAVE_LOCALE_H
60 #   include <locale.h>
61 #endif
62
63 #ifdef HAVE_HAL
64 #   include <hal/libhal.h>
65 #endif
66
67 #include "vlc_cpu.h"                                        /* CPU detection */
68 #include "os_specific.h"
69
70 #include "vlc_error.h"
71
72 #include "vlc_playlist.h"
73 #include "vlc_interface.h"
74
75 #include "audio_output.h"
76
77 #include "vlc_video.h"
78 #include "video_output.h"
79
80 #include "stream_output.h"
81 #include "charset.h"
82
83 #include "libvlc.h"
84
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;
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
95 static void LocaleInit( void );
96 static void LocaleDeinit( void );
97 static void SetLanguage   ( char const * );
98 static int  GetFilenames  ( vlc_t *, int, char *[] );
99 static void Help          ( vlc_t *, char const *psz_help_name );
100 static void Usage         ( vlc_t *, char const *psz_module_name );
101 static void ListModules   ( vlc_t * );
102 static void Version       ( void );
103
104 #ifdef WIN32
105 static void ShowConsole   ( void );
106 static void PauseConsole  ( void );
107 #endif
108 static int  ConsoleWidth  ( void );
109
110 static int  VerboseCallback( vlc_object_t *, char const *,
111                              vlc_value_t, vlc_value_t, void * );
112
113 static void InitDeviceValues( vlc_t * );
114
115 /*****************************************************************************
116  * vlc_current_object: return the current object.
117  *****************************************************************************
118  * If i_object is non-zero, return the corresponding object. Otherwise,
119  * return the statically allocated p_vlc object.
120  *****************************************************************************/
121 vlc_t * vlc_current_object( int i_object )
122 {
123     if( i_object )
124     {
125          return vlc_object_get( p_libvlc, i_object );
126     }
127
128     return p_static_vlc;
129 }
130
131 /*****************************************************************************
132  * VLC_Version: return the libvlc version.
133  *****************************************************************************
134  * This function returns full version string (numeric version and codename).
135  *****************************************************************************/
136 char const * VLC_Version( void )
137 {
138     return VERSION_MESSAGE;
139 }
140
141 /*****************************************************************************
142  * VLC_CompileBy, VLC_CompileHost, VLC_CompileDomain,
143  * VLC_Compiler, VLC_Changeset
144  *****************************************************************************/
145 #define DECLARE_VLC_VERSION( func, var )                                    \
146 char const * VLC_##func ( void )                                            \
147 {                                                                           \
148     return VLC_##var ;                                                      \
149 }
150
151 DECLARE_VLC_VERSION( CompileBy, COMPILE_BY );
152 DECLARE_VLC_VERSION( CompileHost, COMPILE_HOST );
153 DECLARE_VLC_VERSION( CompileDomain, COMPILE_DOMAIN );
154 DECLARE_VLC_VERSION( Compiler, COMPILER );
155
156 extern const char psz_vlc_changeset[];
157 char const * VLC_Changeset( void )
158 {
159     return psz_vlc_changeset;
160 }
161
162 /*****************************************************************************
163  * VLC_Error: strerror() equivalent
164  *****************************************************************************
165  * This function returns full version string (numeric version and codename).
166  *****************************************************************************/
167 char const * VLC_Error( int i_err )
168 {
169     return vlc_error( i_err );
170 }
171
172 /*****************************************************************************
173  * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
174  *****************************************************************************
175  * This function allocates a vlc_t structure and returns a negative value
176  * in case of failure. Also, the thread system is initialized.
177  *****************************************************************************/
178 int VLC_Create( void )
179 {
180     int i_ret;
181     vlc_t * p_vlc = NULL;
182     vlc_value_t lockval;
183
184     /* &libvlc never changes, so we can safely call this multiple times. */
185     p_libvlc = &libvlc;
186
187     /* vlc_threads_init *must* be the first internal call! No other call is
188      * allowed before the thread system has been initialized. */
189     i_ret = vlc_threads_init( p_libvlc );
190     if( i_ret < 0 )
191     {
192         return i_ret;
193     }
194
195     /* Now that the thread system is initialized, we don't have much, but
196      * at least we have var_Create */
197     var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
198     var_Get( p_libvlc, "libvlc", &lockval );
199     vlc_mutex_lock( lockval.p_address );
200     if( !libvlc.b_ready )
201     {
202         char *psz_env;
203
204         /* Guess what CPU we have */
205         libvlc.i_cpu = CPUCapabilities();
206
207         /* Find verbosity from VLC_VERBOSE environment variable */
208         psz_env = getenv( "VLC_VERBOSE" );
209         libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
210
211 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
212         libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
213 #else
214         libvlc.b_color = VLC_FALSE;
215 #endif
216
217         /* Initialize message queue */
218         msg_Create( p_libvlc );
219
220         /* Announce who we are */
221         msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
222         msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
223
224         /* The module bank will be initialized later */
225         libvlc.p_module_bank = NULL;
226
227         libvlc.b_ready = VLC_TRUE;
228
229         /* UTF-8 convertor are initialized after the locale */
230         libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
231     }
232     vlc_mutex_unlock( lockval.p_address );
233     var_Destroy( p_libvlc, "libvlc" );
234
235     /* Allocate a vlc object */
236     p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
237     if( p_vlc == NULL )
238     {
239         return VLC_EGENERIC;
240     }
241     p_vlc->thread_id = 0;
242
243     p_vlc->psz_object_name = "root";
244
245     /* Initialize mutexes */
246     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
247 #ifdef SYS_DARWIN
248     vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
249     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
250 #endif
251
252     /* Store our newly allocated structure in the global list */
253     vlc_object_attach( p_vlc, p_libvlc );
254
255     /* Store data for the non-reentrant API */
256     p_static_vlc = p_vlc;
257
258     return p_vlc->i_object_id;
259 }
260
261 /*****************************************************************************
262  * VLC_Init: initialize a vlc_t structure.
263  *****************************************************************************
264  * This function initializes a previously allocated vlc_t structure:
265  *  - CPU detection
266  *  - gettext initialization
267  *  - message queue, module bank and playlist initialization
268  *  - configuration and commandline parsing
269  *****************************************************************************/
270 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
271 {
272     char         p_capabilities[200];
273     char *       p_tmp;
274     char *       psz_modules;
275     char *       psz_parser;
276     char *       psz_control;
277     vlc_bool_t   b_exit = VLC_FALSE;
278     vlc_t *      p_vlc = vlc_current_object( i_object );
279     module_t    *p_help_module;
280     playlist_t  *p_playlist;
281     vlc_value_t  val;
282 #if defined( ENABLE_NLS ) \
283      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
284     char *       psz_language;
285 #endif
286
287     if( !p_vlc )
288     {
289         return VLC_ENOOBJ;
290     }
291
292     /*
293      * System specific initialization code
294      */
295     system_Init( p_vlc, &i_argc, ppsz_argv );
296
297     /* Get the executable name (similar to the basename command) */
298     if( i_argc > 0 )
299     {
300         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
301         while( *p_tmp )
302         {
303             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
304             else ++p_tmp;
305         }
306     }
307     else
308     {
309         p_vlc->psz_object_name = "vlc";
310     }
311
312     /*
313      * Support for gettext
314      */
315     SetLanguage( "" );
316
317     /*
318      * Global iconv, must be done after setlocale()
319      * so that vlc_current_charset() works.
320      */
321     LocaleInit();
322
323     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
324     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
325
326     /* Initialize the module bank and load the configuration of the
327      * main module. We need to do this at this stage to be able to display
328      * a short help if required by the user. (short help == main module
329      * options) */
330     module_InitBank( p_vlc );
331
332     /* Hack: insert the help module here */
333     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
334     if( p_help_module == NULL )
335     {
336         module_EndBank( p_vlc );
337         if( i_object ) vlc_object_release( p_vlc );
338         return VLC_EGENERIC;
339     }
340     p_help_module->psz_object_name = "help";
341     p_help_module->psz_longname = N_("Help options");
342     config_Duplicate( p_help_module, p_help_config );
343     vlc_object_attach( p_help_module, libvlc.p_module_bank );
344     /* End hack */
345
346     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
347     {
348         vlc_object_detach( p_help_module );
349         config_Free( p_help_module );
350         vlc_object_destroy( p_help_module );
351         module_EndBank( p_vlc );
352         if( i_object ) vlc_object_release( p_vlc );
353         return VLC_EGENERIC;
354     }
355
356     /* Check for short help option */
357     if( config_GetInt( p_vlc, "help" ) )
358     {
359         Help( p_vlc, "help" );
360         b_exit = VLC_TRUE;
361     }
362     /* Check for version option */
363     else if( config_GetInt( p_vlc, "version" ) )
364     {
365         Version();
366         b_exit = VLC_TRUE;
367     }
368
369     /* Set the config file stuff */
370     p_vlc->psz_homedir = config_GetHomeDir();
371     p_vlc->psz_userdir = config_GetUserDir();
372     if( p_vlc->psz_userdir == NULL )
373         p_vlc->psz_userdir = strdup(p_vlc->psz_homedir);
374     p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
375     if( p_vlc->psz_configfile != NULL && p_vlc->psz_configfile[0] == '~'
376          && p_vlc->psz_configfile[1] == '/' )
377     {
378         char *psz = malloc( strlen(p_vlc->psz_userdir)
379                              + strlen(p_vlc->psz_configfile) );
380         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
381         sprintf( psz, "%s/%s", p_vlc->psz_userdir,
382                                p_vlc->psz_configfile + 2 );
383         free( p_vlc->psz_configfile );
384         p_vlc->psz_configfile = psz;
385     }
386
387     /* Check for plugins cache options */
388     if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
389     {
390         libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
391     }
392
393     /* Hack: remove the help module here */
394     vlc_object_detach( p_help_module );
395     /* End hack */
396
397     /* Will be re-done properly later on */
398     p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
399
400     /* Check for daemon mode */
401 #ifndef WIN32
402     if( config_GetInt( p_vlc, "daemon" ) )
403     {
404 #if HAVE_DAEMON
405         if( daemon( 0, 0) != 0 )
406         {
407             msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
408             b_exit = VLC_TRUE;
409         }
410
411         p_vlc->p_libvlc->b_daemon = VLC_TRUE;
412
413 #else
414         pid_t i_pid;
415
416         if( ( i_pid = fork() ) < 0 )
417         {
418             msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
419             b_exit = VLC_TRUE;
420         }
421         else if( i_pid )
422         {
423             /* This is the parent, exit right now */
424             msg_Dbg( p_vlc, "closing parent process" );
425             b_exit = VLC_TRUE;
426         }
427         else
428         {
429             /* We are the child */
430             msg_Dbg( p_vlc, "daemon spawned" );
431             close( STDIN_FILENO );
432             close( STDOUT_FILENO );
433             close( STDERR_FILENO );
434
435             p_vlc->p_libvlc->b_daemon = VLC_TRUE;
436         }
437 #endif
438     }
439 #endif
440
441     if( b_exit )
442     {
443         config_Free( p_help_module );
444         vlc_object_destroy( p_help_module );
445         module_EndBank( p_vlc );
446         if( i_object ) vlc_object_release( p_vlc );
447         return VLC_EEXIT;
448     }
449
450     /* Check for translation config option */
451 #if defined( ENABLE_NLS ) \
452      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
453
454     /* This ain't really nice to have to reload the config here but it seems
455      * the only way to do it. */
456     config_LoadConfigFile( p_vlc, "main" );
457     config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
458
459     /* Check if the user specified a custom language */
460     psz_language = config_GetPsz( p_vlc, "language" );
461     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
462     {
463         vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
464
465         /* Reset the default domain */
466         SetLanguage( psz_language );
467         LocaleDeinit();
468         LocaleInit();
469
470         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
471         msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
472
473         module_EndBank( p_vlc );
474         module_InitBank( p_vlc );
475         config_LoadConfigFile( p_vlc, "main" );
476         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
477         libvlc.p_module_bank->b_cache_delete = b_cache_delete;
478     }
479     if( psz_language ) free( psz_language );
480 #endif
481
482     /*
483      * Load the builtins and plugins into the module_bank.
484      * We have to do it before config_Load*() because this also gets the
485      * list of configuration options exported by each module and loads their
486      * default values.
487      */
488     module_LoadBuiltins( p_vlc );
489     module_LoadPlugins( p_vlc );
490     if( p_vlc->b_die )
491     {
492         b_exit = VLC_TRUE;
493     }
494
495     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
496                     libvlc.p_module_bank->i_children );
497
498     /* Hack: insert the help module here */
499     vlc_object_attach( p_help_module, libvlc.p_module_bank );
500     /* End hack */
501
502     /* Check for help on modules */
503     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
504     {
505         Help( p_vlc, p_tmp );
506         free( p_tmp );
507         b_exit = VLC_TRUE;
508     }
509     /* Check for long help option */
510     else if( config_GetInt( p_vlc, "longhelp" ) )
511     {
512         Help( p_vlc, "longhelp" );
513         b_exit = VLC_TRUE;
514     }
515     /* Check for module list option */
516     else if( config_GetInt( p_vlc, "list" ) )
517     {
518         ListModules( p_vlc );
519         b_exit = VLC_TRUE;
520     }
521
522     /* Check for config file options */
523     if( config_GetInt( p_vlc, "reset-config" ) )
524     {
525         vlc_object_detach( p_help_module );
526         config_ResetAll( p_vlc );
527         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
528         config_SaveConfigFile( p_vlc, NULL );
529         vlc_object_attach( p_help_module, libvlc.p_module_bank );
530     }
531     if( config_GetInt( p_vlc, "save-config" ) )
532     {
533         vlc_object_detach( p_help_module );
534         config_LoadConfigFile( p_vlc, NULL );
535         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
536         config_SaveConfigFile( p_vlc, NULL );
537         vlc_object_attach( p_help_module, libvlc.p_module_bank );
538     }
539
540     /* Hack: remove the help module here */
541     vlc_object_detach( p_help_module );
542     /* End hack */
543
544     if( b_exit )
545     {
546         config_Free( p_help_module );
547         vlc_object_destroy( p_help_module );
548         module_EndBank( p_vlc );
549         if( i_object ) vlc_object_release( p_vlc );
550         return VLC_EEXIT;
551     }
552
553     /*
554      * Init device values
555      */
556     InitDeviceValues( p_vlc );
557
558     /*
559      * Override default configuration with config file settings
560      */
561     config_LoadConfigFile( p_vlc, NULL );
562
563     /* Hack: insert the help module here */
564     vlc_object_attach( p_help_module, libvlc.p_module_bank );
565     /* End hack */
566
567     /*
568      * Override configuration with command line settings
569      */
570     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
571     {
572 #ifdef WIN32
573         ShowConsole();
574         /* Pause the console because it's destroyed when we exit */
575         fprintf( stderr, "The command line options couldn't be loaded, check "
576                  "that they are valid.\n" );
577         PauseConsole();
578 #endif
579         vlc_object_detach( p_help_module );
580         config_Free( p_help_module );
581         vlc_object_destroy( p_help_module );
582         module_EndBank( p_vlc );
583         if( i_object ) vlc_object_release( p_vlc );
584         return VLC_EGENERIC;
585     }
586
587     /* Hack: remove the help module here */
588     vlc_object_detach( p_help_module );
589     config_Free( p_help_module );
590     vlc_object_destroy( p_help_module );
591     /* End hack */
592
593     /*
594      * System specific configuration
595      */
596     system_Configure( p_vlc, &i_argc, ppsz_argv );
597
598     /*
599      * Message queue options
600      */
601
602     var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
603     if( config_GetInt( p_vlc, "quiet" ) )
604     {
605         val.i_int = -1;
606         var_Set( p_vlc, "verbose", val );
607     }
608     var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
609     var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
610
611     libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
612
613     /*
614      * Output messages that may still be in the queue
615      */
616     msg_Flush( p_vlc );
617
618     /* p_vlc initialization. FIXME ? */
619
620     if( !config_GetInt( p_vlc, "fpu" ) )
621         libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
622
623 #if defined( __i386__ ) || defined( __x86_64__ )
624     if( !config_GetInt( p_vlc, "mmx" ) )
625         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
626     if( !config_GetInt( p_vlc, "3dn" ) )
627         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
628     if( !config_GetInt( p_vlc, "mmxext" ) )
629         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
630     if( !config_GetInt( p_vlc, "sse" ) )
631         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
632     if( !config_GetInt( p_vlc, "sse2" ) )
633         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
634 #endif
635 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
636     if( !config_GetInt( p_vlc, "altivec" ) )
637         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
638 #endif
639
640 #define PRINT_CAPABILITY( capability, string )                              \
641     if( libvlc.i_cpu & capability )                                         \
642     {                                                                       \
643         strncat( p_capabilities, string " ",                                \
644                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
645         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
646     }
647
648     p_capabilities[0] = '\0';
649     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
650     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
651     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
652     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
653     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
654     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
655     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
656     PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
657     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
658     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
659     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
660
661     /*
662      * Choose the best memcpy module
663      */
664     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
665
666     if( p_vlc->pf_memcpy == NULL )
667     {
668         p_vlc->pf_memcpy = memcpy;
669     }
670
671     if( p_vlc->pf_memset == NULL )
672     {
673         p_vlc->pf_memset = memset;
674     }
675
676     /*
677      * Initialize hotkey handling
678      */
679     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
680     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
681     /* Do a copy (we don't need to modify the strings) */
682     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
683
684     /*
685      * Initialize playlist and get commandline files
686      */
687     p_playlist = playlist_Create( p_vlc );
688     if( !p_playlist )
689     {
690         msg_Err( p_vlc, "playlist initialization failed" );
691         if( p_vlc->p_memcpy_module != NULL )
692         {
693             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
694         }
695         module_EndBank( p_vlc );
696         if( i_object ) vlc_object_release( p_vlc );
697         return VLC_EGENERIC;
698     }
699
700     psz_modules = config_GetPsz( p_playlist, "services-discovery" );
701     if( psz_modules && *psz_modules )
702     {
703         /* Add service discovery modules */
704         playlist_AddSDModules( p_playlist, psz_modules );
705     }
706     if( psz_modules ) free( psz_modules );
707
708     /*
709      * Load background interfaces
710      */
711     psz_modules = config_GetPsz( p_vlc, "extraintf" );
712     psz_control = config_GetPsz( p_vlc, "control" );
713
714     if( psz_modules && *psz_modules && psz_control && *psz_control )
715     {
716         psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
717                                                     strlen( psz_control ) + 1 );
718         sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
719     }
720     else if( psz_control && *psz_control )
721     {
722         if( psz_modules ) free( psz_modules );
723         psz_modules = strdup( psz_control );
724     }
725
726     psz_parser = psz_modules;
727     while ( psz_parser && *psz_parser )
728     {
729         char *psz_module, *psz_temp;
730         psz_module = psz_parser;
731         psz_parser = strchr( psz_module, ':' );
732         if ( psz_parser )
733         {
734             *psz_parser = '\0';
735             psz_parser++;
736         }
737         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
738         if( psz_temp )
739         {
740             sprintf( psz_temp, "%s,none", psz_module );
741             VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
742             free( psz_temp );
743         }
744     }
745     if ( psz_modules )
746     {
747         free( psz_modules );
748     }
749
750     /*
751      * Allways load the hotkeys interface if it exists
752      */
753     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
754
755     /*
756      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
757      */
758     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
759     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
760     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
761     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
762     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
763     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
764     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
765     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
766     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
767     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
768     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
769     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
770     var_Create( p_vlc, "width", VLC_VAR_INTEGER );
771     var_Create( p_vlc, "height", VLC_VAR_INTEGER );
772
773     /*
774      * Get input filenames given as commandline arguments
775      */
776     GetFilenames( p_vlc, i_argc, ppsz_argv );
777
778     /*
779      * Get --open argument
780      */
781     var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
782     var_Get( p_vlc, "open", &val );
783     if ( val.psz_string != NULL && *val.psz_string )
784     {
785         VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
786                        PLAYLIST_INSERT, 0 );
787     }
788     if ( val.psz_string != NULL ) free( val.psz_string );
789
790     if( i_object ) vlc_object_release( p_vlc );
791     return VLC_SUCCESS;
792 }
793
794 /*****************************************************************************
795  * VLC_AddIntf: add an interface
796  *****************************************************************************
797  * This function opens an interface plugin and runs it. If b_block is set
798  * to 0, VLC_AddIntf will return immediately and let the interface run in a
799  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
800  * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
801  * the playlist when it is completely initialised.
802  *****************************************************************************/
803 int VLC_AddIntf( int i_object, char const *psz_module,
804                  vlc_bool_t b_block, vlc_bool_t b_play )
805 {
806     int i_err;
807     intf_thread_t *p_intf;
808     vlc_t *p_vlc = vlc_current_object( i_object );
809
810     if( !p_vlc )
811     {
812         return VLC_ENOOBJ;
813     }
814
815 #ifndef WIN32
816     if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
817     {
818         /* Daemon mode hack.
819          * We prefer the dummy interface if none is specified. */
820         char *psz_interface = config_GetPsz( p_vlc, "intf" );
821         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
822         if( psz_interface ) free( psz_interface );
823     }
824 #endif
825
826     /* Try to create the interface */
827     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
828
829     if( p_intf == NULL )
830     {
831         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
832         if( i_object ) vlc_object_release( p_vlc );
833         return VLC_EGENERIC;
834     }
835
836     /* Interface doesn't handle play on start so do it ourselves */
837     if( !p_intf->b_play && b_play ) VLC_Play( i_object );
838
839     /* Try to run the interface */
840     p_intf->b_play = b_play;
841     p_intf->b_block = b_block;
842     i_err = intf_RunThread( p_intf );
843     if( i_err )
844     {
845         vlc_object_detach( p_intf );
846         intf_Destroy( p_intf );
847         if( i_object ) vlc_object_release( p_vlc );
848         return i_err;
849     }
850
851     if( i_object ) vlc_object_release( p_vlc );
852     return VLC_SUCCESS;
853 }
854
855 /*****************************************************************************
856  * VLC_Die: ask vlc to die.
857  *****************************************************************************
858  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
859  * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
860  *****************************************************************************/
861 int VLC_Die( int i_object )
862 {
863     vlc_t *p_vlc = vlc_current_object( i_object );
864
865     if( !p_vlc )
866     {
867         return VLC_ENOOBJ;
868     }
869
870     p_vlc->b_die = VLC_TRUE;
871
872     if( i_object ) vlc_object_release( p_vlc );
873     return VLC_SUCCESS;
874 }
875
876 /*****************************************************************************
877  * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
878  *****************************************************************************/
879 int VLC_CleanUp( int i_object )
880 {
881     intf_thread_t      * p_intf;
882     playlist_t         * p_playlist;
883     vout_thread_t      * p_vout;
884     aout_instance_t    * p_aout;
885     announce_handler_t * p_announce;
886     vlc_t *p_vlc = vlc_current_object( i_object );
887
888     /* Check that the handle is valid */
889     if( !p_vlc )
890     {
891         return VLC_ENOOBJ;
892     }
893
894     /*
895      * Ask the interfaces to stop and destroy them
896      */
897     msg_Dbg( p_vlc, "removing all interfaces" );
898     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
899     {
900         intf_StopThread( p_intf );
901         vlc_object_detach( p_intf );
902         vlc_object_release( p_intf );
903         intf_Destroy( p_intf );
904     }
905
906     /*
907      * Free playlists
908      */
909     msg_Dbg( p_vlc, "removing all playlists" );
910     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
911                                           FIND_CHILD )) )
912     {
913         vlc_object_detach( p_playlist );
914         vlc_object_release( p_playlist );
915         playlist_Destroy( p_playlist );
916     }
917
918     /*
919      * Free video outputs
920      */
921     msg_Dbg( p_vlc, "removing all video outputs" );
922     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
923     {
924         vlc_object_detach( p_vout );
925         vlc_object_release( p_vout );
926         vout_Destroy( p_vout );
927     }
928
929     /*
930      * Free audio outputs
931      */
932     msg_Dbg( p_vlc, "removing all audio outputs" );
933     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
934     {
935         vlc_object_detach( (vlc_object_t *)p_aout );
936         vlc_object_release( (vlc_object_t *)p_aout );
937         aout_Delete( p_aout );
938     }
939
940     /*
941      * Free announce handler(s?)
942      */
943     msg_Dbg( p_vlc, "removing announce handler" );
944     while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
945                                                  FIND_CHILD ) ) )
946    {
947         vlc_object_detach( p_announce );
948         vlc_object_release( p_announce );
949         announce_HandlerDestroy( p_announce );
950    }
951
952     if( i_object ) vlc_object_release( p_vlc );
953     return VLC_SUCCESS;
954 }
955
956 /*****************************************************************************
957  * VLC_Destroy: Destroy everything.
958  *****************************************************************************
959  * This function requests the running threads to finish, waits for their
960  * termination, and destroys their structure.
961  *****************************************************************************/
962 int VLC_Destroy( int i_object )
963 {
964     vlc_t *p_vlc = vlc_current_object( i_object );
965
966     if( !p_vlc )
967     {
968         return VLC_ENOOBJ;
969     }
970
971     /*
972      * Free allocated memory
973      */
974     if( p_vlc->p_memcpy_module )
975     {
976         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
977         p_vlc->p_memcpy_module = NULL;
978     }
979
980     /*
981      * Free module bank !
982      */
983     module_EndBank( p_vlc );
984
985     if( p_vlc->psz_homedir )
986     {
987         free( p_vlc->psz_homedir );
988         p_vlc->psz_homedir = NULL;
989     }
990
991     if( p_vlc->psz_configfile )
992     {
993         free( p_vlc->psz_configfile );
994         p_vlc->psz_configfile = NULL;
995     }
996
997     if( p_vlc->p_hotkeys )
998     {
999         free( p_vlc->p_hotkeys );
1000         p_vlc->p_hotkeys = NULL;
1001     }
1002
1003     /*
1004      * System specific cleaning code
1005      */
1006     system_End( p_vlc );
1007
1008     /*
1009      * Free message queue.
1010      * Nobody shall use msg_* afterward.
1011      */
1012     msg_Flush( p_vlc );
1013     msg_Destroy( p_libvlc );
1014
1015     /* Destroy global iconv */
1016     LocaleDeinit();
1017
1018     /* Destroy mutexes */
1019     vlc_mutex_destroy( &p_vlc->config_lock );
1020
1021     vlc_object_detach( p_vlc );
1022
1023     /* Release object before destroying it */
1024     if( i_object ) vlc_object_release( p_vlc );
1025
1026     vlc_object_destroy( p_vlc );
1027
1028     /* Stop thread system: last one out please shut the door! */
1029     vlc_threads_end( p_libvlc );
1030
1031     return VLC_SUCCESS;
1032 }
1033
1034 /*****************************************************************************
1035  * VLC_VariableSet: set a vlc variable
1036  *****************************************************************************/
1037 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
1038 {
1039     vlc_t *p_vlc = vlc_current_object( i_object );
1040     int i_ret;
1041
1042     if( !p_vlc )
1043     {
1044         return VLC_ENOOBJ;
1045     }
1046
1047     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1048      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1049     if( !strncmp( psz_var, "conf::", 6 ) )
1050     {
1051         module_config_t *p_item;
1052         char const *psz_newvar = psz_var + 6;
1053
1054         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1055
1056         if( p_item )
1057         {
1058             switch( p_item->i_type )
1059             {
1060                 case CONFIG_ITEM_BOOL:
1061                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
1062                     break;
1063                 case CONFIG_ITEM_INTEGER:
1064                     config_PutInt( p_vlc, psz_newvar, value.i_int );
1065                     break;
1066                 case CONFIG_ITEM_FLOAT:
1067                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
1068                     break;
1069                 default:
1070                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
1071                     break;
1072             }
1073             if( i_object ) vlc_object_release( p_vlc );
1074             return VLC_SUCCESS;
1075         }
1076     }
1077
1078     i_ret = var_Set( p_vlc, psz_var, value );
1079
1080     if( i_object ) vlc_object_release( p_vlc );
1081     return i_ret;
1082 }
1083
1084 /*****************************************************************************
1085  * VLC_VariableGet: get a vlc variable
1086  *****************************************************************************/
1087 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
1088 {
1089     vlc_t *p_vlc = vlc_current_object( i_object );
1090     int i_ret;
1091
1092     if( !p_vlc )
1093     {
1094         return VLC_ENOOBJ;
1095     }
1096
1097     i_ret = var_Get( p_vlc , psz_var, p_value );
1098
1099     if( i_object ) vlc_object_release( p_vlc );
1100     return i_ret;
1101 }
1102
1103 /*****************************************************************************
1104  * VLC_VariableType: get a vlc variable type
1105  *****************************************************************************/
1106 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
1107 {
1108     int i_type;
1109     vlc_t *p_vlc = vlc_current_object( i_object );
1110
1111     if( !p_vlc )
1112     {
1113         return VLC_ENOOBJ;
1114     }
1115
1116     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
1117      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
1118     if( !strncmp( psz_var, "conf::", 6 ) )
1119     {
1120         module_config_t *p_item;
1121         char const *psz_newvar = psz_var + 6;
1122
1123         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
1124
1125         if( p_item )
1126         {
1127             switch( p_item->i_type )
1128             {
1129                 case CONFIG_ITEM_BOOL:
1130                     i_type = VLC_VAR_BOOL;
1131                     break;
1132                 case CONFIG_ITEM_INTEGER:
1133                     i_type = VLC_VAR_INTEGER;
1134                     break;
1135                 case CONFIG_ITEM_FLOAT:
1136                     i_type = VLC_VAR_FLOAT;
1137                     break;
1138                 default:
1139                     i_type = VLC_VAR_STRING;
1140                     break;
1141             }
1142         }
1143         else
1144             i_type = 0;
1145     }
1146     else
1147         i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
1148
1149     if( i_object ) vlc_object_release( p_vlc );
1150
1151     if( i_type > 0 )
1152     {
1153         *pi_type = i_type;
1154         return VLC_SUCCESS;
1155     }
1156     return VLC_ENOVAR;
1157 }
1158
1159 /*****************************************************************************
1160  * VLC_AddTarget: adds a target for playing.
1161  *****************************************************************************
1162  * This function adds psz_target to the current playlist. If a playlist does
1163  * not exist, it will create one.
1164  *****************************************************************************/
1165 int VLC_AddTarget( int i_object, char const *psz_target,
1166                    char const **ppsz_options, int i_options,
1167                    int i_mode, int i_pos )
1168 {
1169     int i_err;
1170     playlist_t *p_playlist;
1171     vlc_t *p_vlc = vlc_current_object( i_object );
1172
1173     if( !p_vlc )
1174     {
1175         return VLC_ENOOBJ;
1176     }
1177
1178     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1179
1180     if( p_playlist == NULL )
1181     {
1182         msg_Dbg( p_vlc, "no playlist present, creating one" );
1183         p_playlist = playlist_Create( p_vlc );
1184
1185         if( p_playlist == NULL )
1186         {
1187             if( i_object ) vlc_object_release( p_vlc );
1188             return VLC_EGENERIC;
1189         }
1190
1191         vlc_object_yield( p_playlist );
1192     }
1193
1194     i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1195                              i_mode, i_pos, -1, ppsz_options, i_options);
1196
1197     vlc_object_release( p_playlist );
1198
1199     if( i_object ) vlc_object_release( p_vlc );
1200     return i_err;
1201 }
1202
1203 /*****************************************************************************
1204  * VLC_Play: play
1205  *****************************************************************************/
1206 int VLC_Play( int i_object )
1207 {
1208     playlist_t * p_playlist;
1209     vlc_t *p_vlc = vlc_current_object( i_object );
1210
1211     /* Check that the handle is valid */
1212     if( !p_vlc )
1213     {
1214         return VLC_ENOOBJ;
1215     }
1216
1217     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1218
1219     if( !p_playlist )
1220     {
1221         if( i_object ) vlc_object_release( p_vlc );
1222         return VLC_ENOOBJ;
1223     }
1224
1225     playlist_Play( p_playlist );
1226     vlc_object_release( p_playlist );
1227
1228     if( i_object ) vlc_object_release( p_vlc );
1229     return VLC_SUCCESS;
1230 }
1231
1232 /*****************************************************************************
1233  * VLC_Pause: toggle pause
1234  *****************************************************************************/
1235 int VLC_Pause( int i_object )
1236 {
1237     playlist_t * p_playlist;
1238     vlc_t *p_vlc = vlc_current_object( i_object );
1239
1240     /* Check that the handle is valid */
1241     if( !p_vlc )
1242     {
1243         return VLC_ENOOBJ;
1244     }
1245
1246     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1247
1248     if( !p_playlist )
1249     {
1250         if( i_object ) vlc_object_release( p_vlc );
1251         return VLC_ENOOBJ;
1252     }
1253
1254     playlist_Pause( p_playlist );
1255     vlc_object_release( p_playlist );
1256
1257     if( i_object ) vlc_object_release( p_vlc );
1258     return VLC_SUCCESS;
1259 }
1260
1261 /*****************************************************************************
1262  * VLC_Pause: toggle pause
1263  *****************************************************************************/
1264 int VLC_Stop( int i_object )
1265 {
1266     playlist_t * p_playlist;
1267     vlc_t *p_vlc = vlc_current_object( i_object );
1268
1269     /* Check that the handle is valid */
1270     if( !p_vlc )
1271     {
1272         return VLC_ENOOBJ;
1273     }
1274
1275     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1276
1277     if( !p_playlist )
1278     {
1279         if( i_object ) vlc_object_release( p_vlc );
1280         return VLC_ENOOBJ;
1281     }
1282
1283     playlist_Stop( p_playlist );
1284     vlc_object_release( p_playlist );
1285
1286     if( i_object ) vlc_object_release( p_vlc );
1287     return VLC_SUCCESS;
1288 }
1289
1290 /*****************************************************************************
1291  * VLC_IsPlaying: Query for Playlist Status
1292  *****************************************************************************/
1293 vlc_bool_t VLC_IsPlaying( int i_object )
1294 {
1295     playlist_t * p_playlist;
1296     vlc_bool_t   b_playing;
1297     vlc_value_t  val;
1298
1299     vlc_t *p_vlc = vlc_current_object( i_object );
1300
1301     /* Check that the handle is valid */
1302     if( !p_vlc )
1303     {
1304         return VLC_ENOOBJ;
1305     }
1306
1307     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1308
1309     if( !p_playlist )
1310     {
1311         if( i_object ) vlc_object_release( p_vlc );
1312         return VLC_ENOOBJ;
1313     }
1314     if( !p_playlist->p_input )
1315     {
1316         if( i_object ) vlc_object_release( p_vlc );
1317         return VLC_ENOOBJ;
1318     }
1319     var_Get( p_playlist->p_input, "state", &val );
1320     b_playing = ( val.i_int == PLAYING_S );
1321     vlc_object_release( p_playlist );
1322
1323     if( i_object ) vlc_object_release( p_vlc );
1324     return b_playing;
1325 }
1326
1327 /**
1328  * Get the current position in a input
1329  *
1330  * Return the current position as a float
1331  * \note For some inputs, this will be unknown.
1332  *
1333  * \param i_object a vlc object id
1334  * \return a float in the range of 0.0 - 1.0
1335  */
1336 float VLC_PositionGet( int i_object )
1337 {
1338     input_thread_t *p_input;
1339     vlc_value_t val;
1340     vlc_t *p_vlc = vlc_current_object( i_object );
1341
1342     /* Check that the handle is valid */
1343     if( !p_vlc )
1344     {
1345         return VLC_ENOOBJ;
1346     }
1347
1348     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1349
1350     if( !p_input )
1351     {
1352         if( i_object ) vlc_object_release( p_vlc );
1353         return VLC_ENOOBJ;
1354     }
1355
1356     var_Get( p_input, "position", &val );
1357     vlc_object_release( p_input );
1358
1359     if( i_object ) vlc_object_release( p_vlc );
1360     return val.f_float;
1361 }
1362
1363 /**
1364  * Set the current position in a input
1365  *
1366  * Set the current position in a input and then return
1367  * the current position as a float.
1368  * \note For some inputs, this will be unknown.
1369  *
1370  * \param i_object a vlc object id
1371  * \param i_position a float in the range of 0.0 - 1.0
1372  * \return a float in the range of 0.0 - 1.0
1373  */
1374 float VLC_PositionSet( int i_object, float i_position )
1375 {
1376     input_thread_t *p_input;
1377     vlc_value_t val;
1378     vlc_t *p_vlc = vlc_current_object( i_object );
1379
1380     /* Check that the handle is valid */
1381     if( !p_vlc )
1382     {
1383         return VLC_ENOOBJ;
1384     }
1385
1386     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1387
1388     if( !p_input )
1389     {
1390         if( i_object ) vlc_object_release( p_vlc );
1391         return VLC_ENOOBJ;
1392     }
1393
1394     val.f_float = i_position;
1395     var_Set( p_input, "position", val );
1396     var_Get( p_input, "position", &val );
1397     vlc_object_release( p_input );
1398
1399     if( i_object ) vlc_object_release( p_vlc );
1400     return val.f_float;
1401 }
1402
1403 /**
1404  * Get the current position in a input
1405  *
1406  * Return the current position in seconds from the start.
1407  * \note For some inputs, this will be unknown.
1408  *
1409  * \param i_object a vlc object id
1410  * \return the offset from 0:00 in seconds
1411  */
1412 int VLC_TimeGet( int i_object )
1413 {
1414     input_thread_t *p_input;
1415     vlc_value_t val;
1416     vlc_t *p_vlc = vlc_current_object( i_object );
1417
1418     /* Check that the handle is valid */
1419     if( !p_vlc )
1420     {
1421         return VLC_ENOOBJ;
1422     }
1423
1424     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1425
1426     if( !p_input )
1427     {
1428         if( i_object ) vlc_object_release( p_vlc );
1429         return VLC_ENOOBJ;
1430     }
1431
1432     var_Get( p_input, "time", &val );
1433     vlc_object_release( p_input );
1434
1435     if( i_object ) vlc_object_release( p_vlc );
1436     return val.i_time  / 1000000;
1437 }
1438
1439 /**
1440  * Seek to a position in the current input
1441  *
1442  * Seek i_seconds in the current input. If b_relative is set,
1443  * then the seek will be relative to the current position, otherwise
1444  * it will seek to i_seconds from the beginning of the input.
1445  * \note For some inputs, this will be unknown.
1446  *
1447  * \param i_object a vlc object id
1448  * \param i_seconds seconds from current position or from beginning of input
1449  * \param b_relative seek relative from current position
1450  * \return VLC_SUCCESS on success
1451  */
1452 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1453 {
1454     input_thread_t *p_input;
1455     vlc_value_t val;
1456     vlc_t *p_vlc = vlc_current_object( i_object );
1457
1458     /* Check that the handle is valid */
1459     if( !p_vlc )
1460     {
1461         return VLC_ENOOBJ;
1462     }
1463
1464     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1465
1466     if( !p_input )
1467     {
1468         if( i_object ) vlc_object_release( p_vlc );
1469         return VLC_ENOOBJ;
1470     }
1471
1472     if( b_relative )
1473     {
1474         val.i_time = i_seconds;
1475         val.i_time = val.i_time * 1000000L;
1476         var_Set( p_input, "time-offset", val );
1477     }
1478     else
1479     {
1480         val.i_time = i_seconds;
1481         val.i_time = val.i_time * 1000000L;
1482         var_Set( p_input, "time", val );
1483     }
1484     vlc_object_release( p_input );
1485
1486     if( i_object ) vlc_object_release( p_vlc );
1487     return VLC_SUCCESS;
1488 }
1489
1490 /**
1491  * Get the total length of a input
1492  *
1493  * Return the total length in seconds from the current input.
1494  * \note For some inputs, this will be unknown.
1495  *
1496  * \param i_object a vlc object id
1497  * \return the length in seconds
1498  */
1499 int VLC_LengthGet( int i_object )
1500 {
1501     input_thread_t *p_input;
1502     vlc_value_t val;
1503     vlc_t *p_vlc = vlc_current_object( i_object );
1504
1505     /* Check that the handle is valid */
1506     if( !p_vlc )
1507     {
1508         return VLC_ENOOBJ;
1509     }
1510
1511     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1512
1513     if( !p_input )
1514     {
1515         if( i_object ) vlc_object_release( p_vlc );
1516         return VLC_ENOOBJ;
1517     }
1518
1519     var_Get( p_input, "length", &val );
1520     vlc_object_release( p_input );
1521
1522     if( i_object ) vlc_object_release( p_vlc );
1523     return val.i_time  / 1000000L;
1524 }
1525
1526 /**
1527  * Play the input faster than realtime
1528  *
1529  * 2x, 4x, 8x faster than realtime
1530  * \note For some inputs, this will be impossible.
1531  *
1532  * \param i_object a vlc object id
1533  * \return the current speedrate
1534  */
1535 float VLC_SpeedFaster( int i_object )
1536 {
1537     input_thread_t *p_input;
1538     vlc_value_t val;
1539     vlc_t *p_vlc = vlc_current_object( i_object );
1540
1541     /* Check that the handle is valid */
1542     if( !p_vlc )
1543     {
1544         return VLC_ENOOBJ;
1545     }
1546
1547     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1548
1549     if( !p_input )
1550     {
1551         if( i_object ) vlc_object_release( p_vlc );
1552         return VLC_ENOOBJ;
1553     }
1554
1555     val.b_bool = VLC_TRUE;
1556     var_Set( p_input, "rate-faster", val );
1557     var_Get( p_input, "rate", &val );
1558     vlc_object_release( p_input );
1559
1560     if( i_object ) vlc_object_release( p_vlc );
1561     return val.f_float / INPUT_RATE_DEFAULT;
1562 }
1563
1564 /**
1565  * Play the input slower than realtime
1566  *
1567  * 1/2x, 1/4x, 1/8x slower than realtime
1568  * \note For some inputs, this will be impossible.
1569  *
1570  * \param i_object a vlc object id
1571  * \return the current speedrate
1572  */
1573 float VLC_SpeedSlower( int i_object )
1574 {
1575     input_thread_t *p_input;
1576     vlc_value_t val;
1577     vlc_t *p_vlc = vlc_current_object( i_object );
1578
1579     /* Check that the handle is valid */
1580     if( !p_vlc )
1581     {
1582         return VLC_ENOOBJ;
1583     }
1584
1585     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1586
1587     if( !p_input )
1588     {
1589         if( i_object ) vlc_object_release( p_vlc );
1590         return VLC_ENOOBJ;
1591     }
1592
1593     val.b_bool = VLC_TRUE;
1594     var_Set( p_input, "rate-slower", val );
1595     var_Get( p_input, "rate", &val );
1596     vlc_object_release( p_input );
1597
1598     if( i_object ) vlc_object_release( p_vlc );
1599     return val.f_float / INPUT_RATE_DEFAULT;
1600 }
1601
1602 /**
1603  * Return the current playlist item
1604  *
1605  * Returns the index of the playlistitem that is currently selected for play.
1606  * This is valid even if nothing is currently playing.
1607  *
1608  * \param i_object a vlc object id
1609  * \return the current index
1610  */
1611 int VLC_PlaylistIndex( int i_object )
1612 {
1613     int i_index;
1614     playlist_t * p_playlist;
1615     vlc_t *p_vlc = vlc_current_object( i_object );
1616
1617     /* Check that the handle is valid */
1618     if( !p_vlc )
1619     {
1620         return VLC_ENOOBJ;
1621     }
1622
1623     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1624
1625     if( !p_playlist )
1626     {
1627         if( i_object ) vlc_object_release( p_vlc );
1628         return VLC_ENOOBJ;
1629     }
1630
1631     i_index = p_playlist->i_index;
1632     vlc_object_release( p_playlist );
1633
1634     if( i_object ) vlc_object_release( p_vlc );
1635     return i_index;
1636 }
1637
1638 /**
1639  * Total amount of items in the playlist
1640  *
1641  * \param i_object a vlc object id
1642  * \return amount of playlist items
1643  */
1644 int VLC_PlaylistNumberOfItems( int i_object )
1645 {
1646     int i_size;
1647     playlist_t * p_playlist;
1648     vlc_t *p_vlc = vlc_current_object( i_object );
1649
1650     /* Check that the handle is valid */
1651     if( !p_vlc )
1652     {
1653         return VLC_ENOOBJ;
1654     }
1655
1656     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1657
1658     if( !p_playlist )
1659     {
1660         if( i_object ) vlc_object_release( p_vlc );
1661         return VLC_ENOOBJ;
1662     }
1663
1664     i_size = p_playlist->i_size;
1665     vlc_object_release( p_playlist );
1666
1667     if( i_object ) vlc_object_release( p_vlc );
1668     return i_size;
1669 }
1670
1671 /**
1672  * Next playlist item
1673  *
1674  * Skip to the next playlistitem and play it.
1675  *
1676  * \param i_object a vlc object id
1677  * \return VLC_SUCCESS on success
1678  */
1679 int VLC_PlaylistNext( int i_object )
1680 {
1681     playlist_t * p_playlist;
1682     vlc_t *p_vlc = vlc_current_object( i_object );
1683
1684     /* Check that the handle is valid */
1685     if( !p_vlc )
1686     {
1687         return VLC_ENOOBJ;
1688     }
1689
1690     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1691
1692     if( !p_playlist )
1693     {
1694         if( i_object ) vlc_object_release( p_vlc );
1695         return VLC_ENOOBJ;
1696     }
1697
1698     playlist_Next( p_playlist );
1699     vlc_object_release( p_playlist );
1700
1701     if( i_object ) vlc_object_release( p_vlc );
1702     return VLC_SUCCESS;
1703 }
1704
1705 /**
1706  * Previous playlist item
1707  *
1708  * Skip to the previous playlistitem and play it.
1709  *
1710  * \param i_object a vlc object id
1711  * \return VLC_SUCCESS on success
1712  */
1713 int VLC_PlaylistPrev( int i_object )
1714 {
1715     playlist_t * p_playlist;
1716     vlc_t *p_vlc = vlc_current_object( i_object );
1717
1718     /* Check that the handle is valid */
1719     if( !p_vlc )
1720     {
1721         return VLC_ENOOBJ;
1722     }
1723
1724     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1725
1726     if( !p_playlist )
1727     {
1728         if( i_object ) vlc_object_release( p_vlc );
1729         return VLC_ENOOBJ;
1730     }
1731
1732     playlist_Prev( p_playlist );
1733     vlc_object_release( p_playlist );
1734
1735     if( i_object ) vlc_object_release( p_vlc );
1736     return VLC_SUCCESS;
1737 }
1738
1739
1740 /*****************************************************************************
1741  * VLC_PlaylistClear: Empty the playlist
1742  *****************************************************************************/
1743 int VLC_PlaylistClear( int i_object )
1744 {
1745     int i_err;
1746     playlist_t * p_playlist;
1747     vlc_t *p_vlc = vlc_current_object( i_object );
1748
1749     /* Check that the handle is valid */
1750     if( !p_vlc )
1751     {
1752         return VLC_ENOOBJ;
1753     }
1754
1755     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1756
1757     if( !p_playlist )
1758     {
1759         if( i_object ) vlc_object_release( p_vlc );
1760         return VLC_ENOOBJ;
1761     }
1762
1763     i_err = playlist_Clear( p_playlist );
1764
1765     vlc_object_release( p_playlist );
1766
1767     if( i_object ) vlc_object_release( p_vlc );
1768     return i_err;
1769 }
1770
1771 /**
1772  * Change the volume
1773  *
1774  * \param i_object a vlc object id
1775  * \param i_volume something in a range from 0-200
1776  * \return the new volume (range 0-200 %)
1777  */
1778 int VLC_VolumeSet( int i_object, int i_volume )
1779 {
1780     audio_volume_t i_vol = 0;
1781     vlc_t *p_vlc = vlc_current_object( i_object );
1782
1783     /* Check that the handle is valid */
1784     if( !p_vlc )
1785     {
1786         return VLC_ENOOBJ;
1787     }
1788
1789     if( i_volume >= 0 && i_volume <= 200 )
1790     {
1791         i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1792         aout_VolumeSet( p_vlc, i_vol );
1793     }
1794
1795     if( i_object ) vlc_object_release( p_vlc );
1796     return i_vol * 200 / AOUT_VOLUME_MAX;
1797 }
1798
1799 /**
1800  * Get the current volume
1801  *
1802  * Retrieve the current volume.
1803  *
1804  * \param i_object a vlc object id
1805  * \return the current volume (range 0-200 %)
1806  */
1807 int VLC_VolumeGet( int i_object )
1808 {
1809     audio_volume_t i_volume;
1810     vlc_t *p_vlc = vlc_current_object( i_object );
1811
1812     /* Check that the handle is valid */
1813     if( !p_vlc )
1814     {
1815         return VLC_ENOOBJ;
1816     }
1817
1818     aout_VolumeGet( p_vlc, &i_volume );
1819
1820     if( i_object ) vlc_object_release( p_vlc );
1821     return i_volume*200/AOUT_VOLUME_MAX;
1822 }
1823
1824 /**
1825  * Mute/Unmute the volume
1826  *
1827  * \param i_object a vlc object id
1828  * \return VLC_SUCCESS on success
1829  */
1830 int VLC_VolumeMute( int i_object )
1831 {
1832     vlc_t *p_vlc = vlc_current_object( i_object );
1833
1834     /* Check that the handle is valid */
1835     if( !p_vlc )
1836     {
1837         return VLC_ENOOBJ;
1838     }
1839
1840     aout_VolumeMute( p_vlc, NULL );
1841
1842     if( i_object ) vlc_object_release( p_vlc );
1843     return VLC_SUCCESS;
1844 }
1845
1846 /*****************************************************************************
1847  * VLC_FullScreen: toggle fullscreen mode
1848  *****************************************************************************/
1849 int VLC_FullScreen( int i_object )
1850 {
1851     vout_thread_t *p_vout;
1852     vlc_t *p_vlc = vlc_current_object( i_object );
1853
1854     if( !p_vlc )
1855     {
1856         return VLC_ENOOBJ;
1857     }
1858
1859     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1860
1861     if( !p_vout )
1862     {
1863         if( i_object ) vlc_object_release( p_vlc );
1864         return VLC_ENOOBJ;
1865     }
1866
1867     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1868     vlc_object_release( p_vout );
1869
1870     if( i_object ) vlc_object_release( p_vlc );
1871     return VLC_SUCCESS;
1872 }
1873
1874 /* following functions are local */
1875
1876 static void LocaleInit( void )
1877 {
1878     char *psz_charset;
1879
1880     if( !vlc_current_charset( &psz_charset ) )
1881     {
1882         char *psz_conv = psz_charset;
1883
1884         /*
1885          * Still allow non-ASCII characters when the locale is not set.
1886          * Western Europeans are being favored for historical reasons.
1887          */
1888         psz_conv = strcmp( psz_charset, "ASCII" )
1889             ? psz_charset
1890             : "ISO-8859-15";
1891
1892         vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
1893         vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
1894         libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
1895         libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
1896         if( !libvlc.to_locale )
1897         {
1898             /* Not sure it is the right thing to do, but at least it
1899              doesn't make vlc crash with msvc ! */
1900             libvlc.to_locale = (vlc_iconv_t)(-1);
1901         }
1902     }
1903     else
1904         libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
1905     free( psz_charset );
1906 }
1907
1908 static void LocaleDeinit( void )
1909 {
1910     if( libvlc.to_locale != (vlc_iconv_t)(-1) )
1911     {
1912         vlc_mutex_destroy( &libvlc.from_locale_lock );
1913         vlc_mutex_destroy( &libvlc.to_locale_lock );
1914         vlc_iconv_close( libvlc.from_locale );
1915         vlc_iconv_close( libvlc.to_locale );
1916     }
1917 }
1918
1919 /*****************************************************************************
1920  * SetLanguage: set the interface language.
1921  *****************************************************************************
1922  * We set the LC_MESSAGES locale category for interface messages and buttons,
1923  * as well as the LC_CTYPE category for string sorting and possible wide
1924  * character support.
1925  *****************************************************************************/
1926 static void SetLanguage ( char const *psz_lang )
1927 {
1928 #if defined( ENABLE_NLS ) \
1929      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1930
1931     char *          psz_path;
1932 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1933     char            psz_tmp[1024];
1934 #endif
1935
1936     if( psz_lang && !*psz_lang )
1937     {
1938 #   if defined( HAVE_LC_MESSAGES )
1939         setlocale( LC_MESSAGES, psz_lang );
1940 #   endif
1941         setlocale( LC_CTYPE, psz_lang );
1942     }
1943     else if( psz_lang )
1944     {
1945 #ifdef SYS_DARWIN
1946         /* I need that under Darwin, please check it doesn't disturb
1947          * other platforms. --Meuuh */
1948         setenv( "LANG", psz_lang, 1 );
1949
1950 #elif defined( SYS_BEOS ) || defined( WIN32 )
1951         /* We set LC_ALL manually because it is the only way to set
1952          * the language at runtime under eg. Windows. Beware that this
1953          * makes the environment unconsistent when libvlc is unloaded and
1954          * should probably be moved to a safer place like vlc.c. */
1955         static char psz_lcall[20];
1956         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1957         psz_lcall[19] = '\0';
1958         putenv( psz_lcall );
1959 #endif
1960
1961         setlocale( LC_ALL, psz_lang );
1962         /* many code paths assume that float numbers are formatted according
1963          * to the US standard (ie. with dot as decimal point), so we keep
1964          * C for LC_NUMERIC. */
1965         setlocale(LC_NUMERIC, "C" );
1966     }
1967
1968     /* Specify where to find the locales for current domain */
1969 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1970     psz_path = LOCALEDIR;
1971 #else
1972     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1973               "locale" );
1974     psz_path = psz_tmp;
1975 #endif
1976     if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1977     {
1978         fprintf( stderr, "warning: no domain %s in directory %s\n",
1979                  PACKAGE_NAME, psz_path );
1980     }
1981
1982     /* Set the default domain */
1983     textdomain( PACKAGE_NAME );
1984     bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1985 #endif
1986 }
1987
1988 /*****************************************************************************
1989  * GetFilenames: parse command line options which are not flags
1990  *****************************************************************************
1991  * Parse command line for input files as well as their associated options.
1992  * An option always follows its associated input and begins with a ":".
1993  *****************************************************************************/
1994 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1995 {
1996     int i_opt, i_options;
1997
1998     /* We assume that the remaining parameters are filenames
1999      * and their input options */
2000     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
2001     {
2002         const char *psz_target;
2003         i_options = 0;
2004
2005         /* Count the input options */
2006         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
2007         {
2008             i_options++;
2009             i_opt--;
2010         }
2011
2012         /* TODO: write an internal function of this one, to avoid
2013          *       unnecessary lookups. */
2014         /* FIXME: should we convert options to UTF-8 as well ?? */
2015         psz_target = FromLocale( ppsz_argv[ i_opt ] );
2016         VLC_AddTarget( p_vlc->i_object_id, psz_target,
2017                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
2018                                         NULL ), i_options,
2019                        PLAYLIST_INSERT, 0 );
2020         LocaleFree( psz_target );
2021     }
2022
2023     return VLC_SUCCESS;
2024 }
2025
2026 /*****************************************************************************
2027  * Help: print program help
2028  *****************************************************************************
2029  * Print a short inline help. Message interface is initialized at this stage.
2030  *****************************************************************************/
2031 static void Help( vlc_t *p_this, char const *psz_help_name )
2032 {
2033 #ifdef WIN32
2034     ShowConsole();
2035 #endif
2036
2037     if( psz_help_name && !strcmp( psz_help_name, "help" ) )
2038     {
2039         fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2040         Usage( p_this, "help" );
2041         Usage( p_this, "main" );
2042     }
2043     else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
2044     {
2045         fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
2046         Usage( p_this, NULL );
2047     }
2048     else if( psz_help_name )
2049     {
2050         Usage( p_this, psz_help_name );
2051     }
2052
2053 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2054     PauseConsole();
2055 #endif
2056 }
2057
2058 /*****************************************************************************
2059  * Usage: print module usage
2060  *****************************************************************************
2061  * Print a short inline help. Message interface is initialized at this stage.
2062  *****************************************************************************/
2063 static void Usage( vlc_t *p_this, char const *psz_module_name )
2064 {
2065 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
2066     /* short option ------'    |     | | | |  | |
2067      * option name ------------'     | | | |  | |
2068      * <bra -------------------------' | | |  | |
2069      * option type or "" --------------' | |  | |
2070      * ket> -----------------------------' |  | |
2071      * padding spaces ---------------------'  | |
2072      * comment -------------------------------' |
2073      * comment suffix --------------------------'
2074      *
2075      * The purpose of having bra and ket is that we might i18n them as well.
2076      */
2077 #define LINE_START 8
2078 #define PADDING_SPACES 25
2079     vlc_list_t *p_list;
2080     module_t *p_parser;
2081     module_config_t *p_item;
2082     char psz_spaces_text[PADDING_SPACES+LINE_START+1];
2083     char psz_spaces_longtext[LINE_START+3];
2084     char psz_format[sizeof(FORMAT_STRING)];
2085     char psz_buffer[10000];
2086     char psz_short[4];
2087     int i_index;
2088     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
2089     vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
2090     vlc_bool_t b_description;
2091
2092     memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
2093     psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
2094     memset( psz_spaces_longtext, ' ', LINE_START+2 );
2095     psz_spaces_longtext[LINE_START+2] = '\0';
2096
2097     strcpy( psz_format, FORMAT_STRING );
2098
2099     /* List all modules */
2100     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2101
2102     /* Enumerate the config for each module */
2103     for( i_index = 0; i_index < p_list->i_count; i_index++ )
2104     {
2105         vlc_bool_t b_help_module;
2106
2107         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2108
2109         if( psz_module_name && strcmp( psz_module_name,
2110                                        p_parser->psz_object_name ) )
2111         {
2112             continue;
2113         }
2114
2115         /* Ignore modules without config options */
2116         if( !p_parser->i_config_items )
2117         {
2118             continue;
2119         }
2120
2121         /* Ignore modules with only advanced config options if requested */
2122         if( !b_advanced )
2123         {
2124             for( p_item = p_parser->p_config;
2125                  p_item->i_type != CONFIG_HINT_END;
2126                  p_item++ )
2127             {
2128                 if( (p_item->i_type & CONFIG_ITEM) &&
2129                     !p_item->b_advanced ) break;
2130             }
2131             if( p_item->i_type == CONFIG_HINT_END ) continue;
2132         }
2133
2134         /* Print name of module */
2135         if( strcmp( "main", p_parser->psz_object_name ) )
2136         fprintf( stdout, "\n %s\n", p_parser->psz_longname );
2137
2138         b_help_module = !strcmp( "help", p_parser->psz_object_name );
2139
2140         /* Print module options */
2141         for( p_item = p_parser->p_config;
2142              p_item->i_type != CONFIG_HINT_END;
2143              p_item++ )
2144         {
2145             char *psz_text, *psz_spaces = psz_spaces_text;
2146             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
2147             char *psz_suf = "", *psz_prefix = NULL;
2148             signed int i;
2149
2150             /* Skip deprecated options */
2151             if( p_item->psz_current )
2152             {
2153                 continue;
2154             }
2155             /* Skip advanced options if requested */
2156             if( p_item->b_advanced && !b_advanced )
2157             {
2158                 continue;
2159             }
2160
2161             switch( p_item->i_type )
2162             {
2163             case CONFIG_HINT_CATEGORY:
2164             case CONFIG_HINT_USAGE:
2165                 if( !strcmp( "main", p_parser->psz_object_name ) )
2166                 fprintf( stdout, "\n %s\n", p_item->psz_text );
2167                 break;
2168
2169             case CONFIG_ITEM_STRING:
2170             case CONFIG_ITEM_FILE:
2171             case CONFIG_ITEM_DIRECTORY:
2172             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
2173             case CONFIG_ITEM_MODULE_CAT:
2174             case CONFIG_ITEM_MODULE_LIST:
2175             case CONFIG_ITEM_MODULE_LIST_CAT:
2176                 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
2177
2178                 if( p_item->ppsz_list )
2179                 {
2180                     psz_bra = " {";
2181                     psz_type = psz_buffer;
2182                     psz_type[0] = '\0';
2183                     for( i = 0; p_item->ppsz_list[i]; i++ )
2184                     {
2185                         if( i ) strcat( psz_type, "," );
2186                         strcat( psz_type, p_item->ppsz_list[i] );
2187                     }
2188                     psz_ket = "}";
2189                 }
2190                 break;
2191             case CONFIG_ITEM_INTEGER:
2192             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
2193                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
2194
2195                 if( p_item->i_list )
2196                 {
2197                     psz_bra = " {";
2198                     psz_type = psz_buffer;
2199                     psz_type[0] = '\0';
2200                     for( i = 0; p_item->ppsz_list_text[i]; i++ )
2201                     {
2202                         if( i ) strcat( psz_type, ", " );
2203                         sprintf( psz_type + strlen(psz_type), "%i (%s)",
2204                                  p_item->pi_list[i],
2205                                  p_item->ppsz_list_text[i] );
2206                     }
2207                     psz_ket = "}";
2208                 }
2209                 break;
2210             case CONFIG_ITEM_FLOAT:
2211                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
2212                 break;
2213             case CONFIG_ITEM_BOOL:
2214                 psz_bra = ""; psz_type = ""; psz_ket = "";
2215                 if( !b_help_module )
2216                 {
2217                     psz_suf = p_item->i_value ? _(" (default enabled)") :
2218                                                 _(" (default disabled)");
2219                 }
2220                 break;
2221             }
2222
2223             if( !psz_type )
2224             {
2225                 continue;
2226             }
2227
2228             /* Add short option if any */
2229             if( p_item->i_short )
2230             {
2231                 sprintf( psz_short, "-%c,", p_item->i_short );
2232             }
2233             else
2234             {
2235                 strcpy( psz_short, "   " );
2236             }
2237
2238             i = PADDING_SPACES - strlen( p_item->psz_name )
2239                  - strlen( psz_bra ) - strlen( psz_type )
2240                  - strlen( psz_ket ) - 1;
2241
2242             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2243             {
2244                 psz_prefix =  ", --no-";
2245                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
2246             }
2247
2248             if( i < 0 )
2249             {
2250                 psz_spaces[0] = '\n';
2251                 i = 0;
2252             }
2253             else
2254             {
2255                 psz_spaces[i] = '\0';
2256             }
2257
2258             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
2259             {
2260                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2261                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
2262                          psz_ket, psz_spaces );
2263             }
2264             else
2265             {
2266                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2267                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2268             }
2269
2270             psz_spaces[i] = ' ';
2271
2272             /* We wrap the rest of the output */
2273             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2274             b_description = config_GetInt( p_this, "help-verbose" );
2275
2276  description:
2277             psz_text = psz_buffer;
2278             while( *psz_text )
2279             {
2280                 char *psz_parser, *psz_word;
2281                 size_t i_end = strlen( psz_text );
2282
2283                 /* If the remaining text fits in a line, print it. */
2284                 if( i_end <= (size_t)i_width )
2285                 {
2286                     fprintf( stdout, "%s\n", psz_text );
2287                     break;
2288                 }
2289
2290                 /* Otherwise, eat as many words as possible */
2291                 psz_parser = psz_text;
2292                 do
2293                 {
2294                     psz_word = psz_parser;
2295                     psz_parser = strchr( psz_word, ' ' );
2296                     /* If no space was found, we reached the end of the text
2297                      * block; otherwise, we skip the space we just found. */
2298                     psz_parser = psz_parser ? psz_parser + 1
2299                                             : psz_text + i_end;
2300
2301                 } while( psz_parser - psz_text <= i_width );
2302
2303                 /* We cut a word in one of these cases:
2304                  *  - it's the only word in the line and it's too long.
2305                  *  - we used less than 80% of the width and the word we are
2306                  *    going to wrap is longer than 40% of the width, and even
2307                  *    if the word would have fit in the next line. */
2308                 if( psz_word == psz_text
2309                      || ( psz_word - psz_text < 80 * i_width / 100
2310                            && psz_parser - psz_word > 40 * i_width / 100 ) )
2311                 {
2312                     char c = psz_text[i_width];
2313                     psz_text[i_width] = '\0';
2314                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2315                     psz_text += i_width;
2316                     psz_text[0] = c;
2317                 }
2318                 else
2319                 {
2320                     psz_word[-1] = '\0';
2321                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2322                     psz_text = psz_word;
2323                 }
2324             }
2325
2326             if( b_description && p_item->psz_longtext )
2327             {
2328                 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
2329                 b_description = VLC_FALSE;
2330                 psz_spaces = psz_spaces_longtext;
2331                 fprintf( stdout, "%s", psz_spaces );
2332                 goto description;
2333             }
2334         }
2335     }
2336
2337     /* Release the module list */
2338     vlc_list_release( p_list );
2339 }
2340
2341 /*****************************************************************************
2342  * ListModules: list the available modules with their description
2343  *****************************************************************************
2344  * Print a list of all available modules (builtins and plugins) and a short
2345  * description for each one.
2346  *****************************************************************************/
2347 static void ListModules( vlc_t *p_this )
2348 {
2349     vlc_list_t *p_list;
2350     module_t *p_parser;
2351     char psz_spaces[22];
2352     int i_index;
2353
2354     memset( psz_spaces, ' ', 22 );
2355
2356 #ifdef WIN32
2357     ShowConsole();
2358 #endif
2359
2360     /* List all modules */
2361     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2362
2363     /* Enumerate each module */
2364     for( i_index = 0; i_index < p_list->i_count; i_index++ )
2365     {
2366         int i;
2367
2368         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2369
2370         /* Nasty hack, but right now I'm too tired to think about a nice
2371          * solution */
2372         i = 22 - strlen( p_parser->psz_object_name ) - 1;
2373         if( i < 0 ) i = 0;
2374         psz_spaces[i] = 0;
2375
2376         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
2377                          psz_spaces, p_parser->psz_longname );
2378
2379         psz_spaces[i] = ' ';
2380     }
2381
2382     vlc_list_release( p_list );
2383
2384 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2385     PauseConsole();
2386 #endif
2387 }
2388
2389 /*****************************************************************************
2390  * Version: print complete program version
2391  *****************************************************************************
2392  * Print complete program version and build number.
2393  *****************************************************************************/
2394 static void Version( void )
2395 {
2396 #ifdef WIN32
2397     ShowConsole();
2398 #endif
2399
2400     fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
2401     fprintf( stdout, _("Compiled by %s@%s.%s\n"),
2402              VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
2403     fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
2404     if( strcmp( VLC_Changeset(), "exported" ) )
2405         fprintf( stdout, _("Based upon svn changeset [%s]\n"),
2406                  VLC_Changeset() );
2407     fprintf( stdout,
2408       _("This program comes with NO WARRANTY, to the extent permitted by "
2409         "law.\nYou may redistribute it under the terms of the GNU General "
2410         "Public License;\nsee the file named COPYING for details.\n"
2411         "Written by the VideoLAN team; see the AUTHORS file.\n") );
2412
2413 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2414     PauseConsole();
2415 #endif
2416 }
2417
2418 /*****************************************************************************
2419  * ShowConsole: On Win32, create an output console for debug messages
2420  *****************************************************************************
2421  * This function is useful only on Win32.
2422  *****************************************************************************/
2423 #ifdef WIN32 /*  */
2424 static void ShowConsole( void )
2425 {
2426 #   ifndef UNDER_CE
2427     FILE *f_help;
2428
2429     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2430
2431     AllocConsole();
2432
2433     if( (f_help = fopen( "vlc-help.txt", "wt" )) )
2434     {
2435         fclose( f_help );
2436         freopen( "vlc-help.txt", "wt", stdout );
2437         fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
2438     }
2439
2440     else freopen( "CONOUT$", "w", stdout );
2441
2442     freopen( "CONOUT$", "w", stderr );
2443     freopen( "CONIN$", "r", stdin );
2444
2445 #   endif
2446 }
2447 #endif
2448
2449 /*****************************************************************************
2450  * PauseConsole: On Win32, wait for a key press before closing the console
2451  *****************************************************************************
2452  * This function is useful only on Win32.
2453  *****************************************************************************/
2454 #ifdef WIN32 /*  */
2455 static void PauseConsole( void )
2456 {
2457 #   ifndef UNDER_CE
2458
2459     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
2460
2461     fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
2462     getchar();
2463     fclose( stdout );
2464
2465 #   endif
2466 }
2467 #endif
2468
2469 /*****************************************************************************
2470  * ConsoleWidth: Return the console width in characters
2471  *****************************************************************************
2472  * We use the stty shell command to get the console width; if this fails or
2473  * if the width is less than 80, we default to 80.
2474  *****************************************************************************/
2475 static int ConsoleWidth( void )
2476 {
2477     int i_width = 80;
2478
2479 #ifndef WIN32
2480     char buf[20], *psz_parser;
2481     FILE *file;
2482     int i_ret;
2483
2484     file = popen( "stty size 2>/dev/null", "r" );
2485     if( file )
2486     {
2487         i_ret = fread( buf, 1, 20, file );
2488         if( i_ret > 0 )
2489         {
2490             buf[19] = '\0';
2491             psz_parser = strchr( buf, ' ' );
2492             if( psz_parser )
2493             {
2494                 i_ret = atoi( psz_parser + 1 );
2495                 if( i_ret >= 80 )
2496                 {
2497                     i_width = i_ret;
2498                 }
2499             }
2500         }
2501
2502         pclose( file );
2503     }
2504 #endif
2505
2506     return i_width;
2507 }
2508
2509 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2510                      vlc_value_t old_val, vlc_value_t new_val, void *param)
2511 {
2512     vlc_t *p_vlc = (vlc_t *)p_this;
2513
2514     if( new_val.i_int >= -1 )
2515     {
2516         p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2517     }
2518     return VLC_SUCCESS;
2519 }
2520
2521 /*****************************************************************************
2522  * InitDeviceValues: initialize device values
2523  *****************************************************************************
2524  * This function inits the dvd, vcd and cd-audio values
2525  *****************************************************************************/
2526 static void InitDeviceValues( vlc_t *p_vlc )
2527 {
2528 #ifdef HAVE_HAL
2529     LibHalContext * ctx;
2530     int i, i_devices;
2531     char **devices;
2532     char *block_dev;
2533     dbus_bool_t b_dvd;
2534
2535     if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2536     {
2537         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2538         {
2539             for( i = 0; i < i_devices; i++ )
2540             {
2541                 if( !hal_device_property_exists( ctx, devices[ i ],
2542                                                 "storage.cdrom.dvd" ) )
2543                 {
2544                     continue;
2545                 }
2546
2547                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2548                                                       "storage.cdrom.dvd" );
2549                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2550                                                             "block.device" );
2551
2552                 if( b_dvd )
2553                 {
2554                     config_PutPsz( p_vlc, "dvd", block_dev );
2555                 }
2556
2557                 config_PutPsz( p_vlc, "vcd", block_dev );
2558                 config_PutPsz( p_vlc, "cd-audio", block_dev );
2559
2560                 hal_free_string( block_dev );
2561             }
2562             hal_free_string_array( devices );
2563         }
2564
2565         hal_shutdown( ctx );
2566     }
2567 #endif
2568 }
2569
2570 /*****************************************************************************
2571  * FromLocale: converts a locale string to UTF-8
2572  *****************************************************************************/
2573 char *FromLocale( const char *locale )
2574 {
2575     if( locale == NULL )
2576         return NULL;
2577
2578     if( libvlc.from_locale != (vlc_iconv_t)(-1) )
2579     {
2580         char *iptr = (char *)locale, *output, *optr;
2581         size_t inb, outb;
2582
2583         /*
2584          * We are not allowed to modify the locale pointer, even if we cast it
2585          * to non-const.
2586          */
2587         inb = strlen( locale );
2588         outb = inb * 6 + 1;
2589
2590         /* FIXME: I'm not sure about the value for the multiplication
2591          * (for western people, multiplication by 3 (Latin9) is sufficient) */
2592         optr = output = calloc( outb , 1);
2593
2594         vlc_mutex_lock( &libvlc.from_locale_lock );
2595         vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2596
2597         while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
2598                                                                == (size_t)-1 )
2599         {
2600             *optr++ = '?';
2601             *iptr++;
2602             vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
2603         }
2604         vlc_mutex_unlock( &libvlc.from_locale_lock );
2605
2606         return realloc( output, strlen( output ) + 1 );
2607     }
2608     return (char *)locale;
2609 }
2610
2611 /*****************************************************************************
2612  * ToLocale: converts an UTF-8 string to locale
2613  *****************************************************************************/
2614 char *ToLocale( const char *utf8 )
2615 {
2616     if( utf8 == NULL )
2617         return NULL;
2618
2619     if( libvlc.to_locale != (vlc_iconv_t)(-1) )
2620     {
2621         char *iptr = (char *)utf8, *output, *optr;
2622         size_t inb, outb;
2623
2624         /*
2625          * We are not allowed to modify the locale pointer, even if we cast it
2626          * to non-const.
2627          */
2628         inb = strlen( utf8 );
2629         /* FIXME: I'm not sure about the value for the multiplication
2630          * (for western people, multiplication is not needed) */
2631         outb = inb * 2 + 1;
2632
2633         optr = output = calloc( outb, 1 );
2634         vlc_mutex_lock( &libvlc.to_locale_lock );
2635         vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2636
2637         while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
2638                                                                == (size_t)-1 )
2639         {
2640             *optr++ = '?'; /* should not happen, and yes, it sucks */
2641             *iptr++;
2642             vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
2643         }
2644         vlc_mutex_unlock( &libvlc.to_locale_lock );
2645
2646         return realloc( output, strlen( output ) + 1 );
2647     }
2648     return (char *)utf8;
2649 }
2650
2651 void LocaleFree( const char *str )
2652 {
2653     if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
2654         free( (char *)str );
2655 }