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