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