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