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