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