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