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