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