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