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