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