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