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